@@ -176,26 +176,19 @@ class AArch64InstructionSelector : public InstructionSelector {
176
176
std::initializer_list<llvm::SrcOp> SrcOps,
177
177
MachineIRBuilder &MIRBuilder,
178
178
const ComplexRendererFns &RenderFns = None) const ;
179
- // / Helper function to emit a binary operation such as an ADD, ADDS, etc.
180
- // /
181
- // / This is intended for instructions with the following opcode variants:
182
- // /
183
- // / - Xri, Wri (arithmetic immediate form)
184
- // / - Xrs, Wrs (shifted register form)
185
- // / - Xrr, Wrr (register form)
186
- // /
187
- // / For example, for ADD, we have ADDXri, ADDWri, ADDXrs, etc.
179
+ // / Helper function to emit an add or sub instruction.
188
180
// /
189
181
// / \p AddrModeAndSizeToOpcode must contain each of the opcode variants above
190
182
// / in a specific order.
191
183
// /
192
184
// / Below is an example of the expected input to \p AddrModeAndSizeToOpcode.
193
185
// /
194
186
// / \code
195
- // / const std::array<std::array<unsigned, 2>, 3 > Table {
187
+ // / const std::array<std::array<unsigned, 2>, 4 > Table {
196
188
// / {{AArch64::ADDXri, AArch64::ADDWri},
197
189
// / {AArch64::ADDXrs, AArch64::ADDWrs},
198
- // / {AArch64::ADDXrr, AArch64::ADDWrr}}};
190
+ // / {AArch64::ADDXrr, AArch64::ADDWrr},
191
+ // / {AArch64::SUBXri, AArch64::SUBWri}}};
199
192
// / \endcode
200
193
// /
201
194
// / Each row in the table corresponds to a different addressing mode. Each
@@ -205,6 +198,7 @@ class AArch64InstructionSelector : public InstructionSelector {
205
198
// / - Row 0: The ri opcode variants
206
199
// / - Row 1: The rs opcode variants
207
200
// / - Row 2: The rr opcode variants
201
+ // / - Row 3: The ri opcode variants for negative immediates
208
202
// /
209
203
// / \attention Columns must be structured as follows:
210
204
// / - Column 0: The 64-bit opcode variants
@@ -213,8 +207,8 @@ class AArch64InstructionSelector : public InstructionSelector {
213
207
// / \p Dst is the destination register of the binop to emit.
214
208
// / \p LHS is the left-hand operand of the binop to emit.
215
209
// / \p RHS is the right-hand operand of the binop to emit.
216
- MachineInstr *emitBinOp (
217
- const std::array<std::array<unsigned , 2 >, 3 > &AddrModeAndSizeToOpcode,
210
+ MachineInstr *emitAddSub (
211
+ const std::array<std::array<unsigned , 2 >, 4 > &AddrModeAndSizeToOpcode,
218
212
Register Dst, MachineOperand &LHS, MachineOperand &RHS,
219
213
MachineIRBuilder &MIRBuilder) const ;
220
214
MachineInstr *emitADD (Register DefReg, MachineOperand &LHS,
@@ -3826,8 +3820,8 @@ MachineInstr *AArch64InstructionSelector::emitInstr(
3826
3820
return &*MI;
3827
3821
}
3828
3822
3829
- MachineInstr *AArch64InstructionSelector::emitBinOp (
3830
- const std::array<std::array<unsigned , 2 >, 3 > &AddrModeAndSizeToOpcode,
3823
+ MachineInstr *AArch64InstructionSelector::emitAddSub (
3824
+ const std::array<std::array<unsigned , 2 >, 4 > &AddrModeAndSizeToOpcode,
3831
3825
Register Dst, MachineOperand &LHS, MachineOperand &RHS,
3832
3826
MachineIRBuilder &MIRBuilder) const {
3833
3827
MachineRegisterInfo &MRI = MIRBuilder.getMF ().getRegInfo ();
@@ -3837,9 +3831,18 @@ MachineInstr *AArch64InstructionSelector::emitBinOp(
3837
3831
unsigned Size = Ty.getSizeInBits ();
3838
3832
assert ((Size == 32 || Size == 64 ) && " Expected a 32-bit or 64-bit type only" );
3839
3833
bool Is32Bit = Size == 32 ;
3834
+
3835
+ // INSTRri form with positive arithmetic immediate.
3840
3836
if (auto Fns = selectArithImmed (RHS))
3841
3837
return emitInstr (AddrModeAndSizeToOpcode[0 ][Is32Bit], {Dst}, {LHS},
3842
3838
MIRBuilder, Fns);
3839
+
3840
+ // INSTRri form with negative arithmetic immediate.
3841
+ if (auto Fns = selectNegArithImmed (RHS))
3842
+ return emitInstr (AddrModeAndSizeToOpcode[3 ][Is32Bit], {Dst}, {LHS},
3843
+ MIRBuilder, Fns);
3844
+
3845
+ // INSTRrs form.
3843
3846
if (auto Fns = selectShiftedRegister (RHS))
3844
3847
return emitInstr (AddrModeAndSizeToOpcode[1 ][Is32Bit], {Dst}, {LHS},
3845
3848
MIRBuilder, Fns);
@@ -3851,33 +3854,36 @@ MachineInstr *
3851
3854
AArch64InstructionSelector::emitADD (Register DefReg, MachineOperand &LHS,
3852
3855
MachineOperand &RHS,
3853
3856
MachineIRBuilder &MIRBuilder) const {
3854
- const std::array<std::array<unsigned , 2 >, 3 > OpcTable{
3857
+ const std::array<std::array<unsigned , 2 >, 4 > OpcTable{
3855
3858
{{AArch64::ADDXri, AArch64::ADDWri},
3856
3859
{AArch64::ADDXrs, AArch64::ADDWrs},
3857
- {AArch64::ADDXrr, AArch64::ADDWrr}}};
3858
- return emitBinOp (OpcTable, DefReg, LHS, RHS, MIRBuilder);
3860
+ {AArch64::ADDXrr, AArch64::ADDWrr},
3861
+ {AArch64::SUBXri, AArch64::SUBWri}}};
3862
+ return emitAddSub (OpcTable, DefReg, LHS, RHS, MIRBuilder);
3859
3863
}
3860
3864
3861
3865
MachineInstr *
3862
3866
AArch64InstructionSelector::emitADDS (Register Dst, MachineOperand &LHS,
3863
3867
MachineOperand &RHS,
3864
3868
MachineIRBuilder &MIRBuilder) const {
3865
- const std::array<std::array<unsigned , 2 >, 3 > OpcTable{
3869
+ const std::array<std::array<unsigned , 2 >, 4 > OpcTable{
3866
3870
{{AArch64::ADDSXri, AArch64::ADDSWri},
3867
3871
{AArch64::ADDSXrs, AArch64::ADDSWrs},
3868
- {AArch64::ADDSXrr, AArch64::ADDSWrr}}};
3869
- return emitBinOp (OpcTable, Dst, LHS, RHS, MIRBuilder);
3872
+ {AArch64::ADDSXrr, AArch64::ADDSWrr},
3873
+ {AArch64::SUBSXri, AArch64::SUBSWri}}};
3874
+ return emitAddSub (OpcTable, Dst, LHS, RHS, MIRBuilder);
3870
3875
}
3871
3876
3872
3877
MachineInstr *
3873
3878
AArch64InstructionSelector::emitSUBS (Register Dst, MachineOperand &LHS,
3874
3879
MachineOperand &RHS,
3875
3880
MachineIRBuilder &MIRBuilder) const {
3876
- const std::array<std::array<unsigned , 2 >, 3 > OpcTable{
3881
+ const std::array<std::array<unsigned , 2 >, 4 > OpcTable{
3877
3882
{{AArch64::SUBSXri, AArch64::SUBSWri},
3878
3883
{AArch64::SUBSXrs, AArch64::SUBSWrs},
3879
- {AArch64::SUBSXrr, AArch64::SUBSWrr}}};
3880
- return emitBinOp (OpcTable, Dst, LHS, RHS, MIRBuilder);
3884
+ {AArch64::SUBSXrr, AArch64::SUBSWrr},
3885
+ {AArch64::ADDSXri, AArch64::ADDSWri}}};
3886
+ return emitAddSub (OpcTable, Dst, LHS, RHS, MIRBuilder);
3881
3887
}
3882
3888
3883
3889
MachineInstr *
0 commit comments