Skip to content

Commit e2c9109

Browse files
authored
[GlobalISel] Handle more commutable instructions in commute_constant_to_rhs
Reviewers: rupprecht, aartbik, cyndyishida, Pierre-vh, aemerson, ftynse, hanhanW, banach-space, jayfoad, nicolasvasilache, daniel-grumberg, arsenm, PeimingLiu, JDevlieghere, matthias-springer Reviewed By: arsenm, Pierre-vh Pull Request: llvm#87424
1 parent 6da1966 commit e2c9109

File tree

12 files changed

+815
-41
lines changed

12 files changed

+815
-41
lines changed

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,20 @@ def select_constant_cmp: GICombineRule<
443443
// TODO: handle compares (currently not marked as isCommutable)
444444
def commute_int_constant_to_rhs : GICombineRule<
445445
(defs root:$root),
446-
(match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR):$root,
446+
(match (wip_match_opcode G_ADD, G_MUL, G_AND, G_OR, G_XOR,
447+
G_SMIN, G_SMAX, G_UMIN, G_UMAX, G_UADDO, G_SADDO,
448+
G_UMULO, G_SMULO, G_UMULH, G_SMULH,
449+
G_UADDSAT, G_SADDSAT, G_SMULFIX, G_UMULFIX,
450+
G_SMULFIXSAT, G_UMULFIXSAT):$root,
447451
[{ return Helper.matchCommuteConstantToRHS(*${root}); }]),
448452
(apply [{ Helper.applyCommuteBinOpOperands(*${root}); }])
449453
>;
450454

451455
def commute_fp_constant_to_rhs : GICombineRule<
452456
(defs root:$root),
453-
(match (wip_match_opcode G_FADD, G_FMUL):$root,
457+
(match (wip_match_opcode G_FADD, G_FMUL, G_FMINNUM, G_FMAXNUM,
458+
G_FMINNUM_IEEE, G_FMAXNUM_IEEE,
459+
G_FMINIMUM, G_FMAXIMUM):$root,
454460
[{ return Helper.matchCommuteFPConstantToRHS(*${root}); }]),
455461
(apply [{ Helper.applyCommuteBinOpOperands(*${root}); }])
456462
>;

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6273,8 +6273,21 @@ bool CombinerHelper::matchShiftsTooBig(MachineInstr &MI) {
62736273
}
62746274

62756275
bool CombinerHelper::matchCommuteConstantToRHS(MachineInstr &MI) {
6276-
Register LHS = MI.getOperand(1).getReg();
6277-
Register RHS = MI.getOperand(2).getReg();
6276+
unsigned LHSOpndIdx = 1;
6277+
unsigned RHSOpndIdx = 2;
6278+
switch (MI.getOpcode()) {
6279+
case TargetOpcode::G_UADDO:
6280+
case TargetOpcode::G_SADDO:
6281+
case TargetOpcode::G_UMULO:
6282+
case TargetOpcode::G_SMULO:
6283+
LHSOpndIdx = 2;
6284+
RHSOpndIdx = 3;
6285+
break;
6286+
default:
6287+
break;
6288+
}
6289+
Register LHS = MI.getOperand(LHSOpndIdx).getReg();
6290+
Register RHS = MI.getOperand(RHSOpndIdx).getReg();
62786291
if (!getIConstantVRegVal(LHS, MRI)) {
62796292
// Skip commuting if LHS is not a constant. But, LHS may be a
62806293
// G_CONSTANT_FOLD_BARRIER. If so we commute as long as we don't already
@@ -6300,10 +6313,23 @@ bool CombinerHelper::matchCommuteFPConstantToRHS(MachineInstr &MI) {
63006313

63016314
void CombinerHelper::applyCommuteBinOpOperands(MachineInstr &MI) {
63026315
Observer.changingInstr(MI);
6303-
Register LHSReg = MI.getOperand(1).getReg();
6304-
Register RHSReg = MI.getOperand(2).getReg();
6305-
MI.getOperand(1).setReg(RHSReg);
6306-
MI.getOperand(2).setReg(LHSReg);
6316+
unsigned LHSOpndIdx = 1;
6317+
unsigned RHSOpndIdx = 2;
6318+
switch (MI.getOpcode()) {
6319+
case TargetOpcode::G_UADDO:
6320+
case TargetOpcode::G_SADDO:
6321+
case TargetOpcode::G_UMULO:
6322+
case TargetOpcode::G_SMULO:
6323+
LHSOpndIdx = 2;
6324+
RHSOpndIdx = 3;
6325+
break;
6326+
default:
6327+
break;
6328+
}
6329+
Register LHSReg = MI.getOperand(LHSOpndIdx).getReg();
6330+
Register RHSReg = MI.getOperand(RHSOpndIdx).getReg();
6331+
MI.getOperand(LHSOpndIdx).setReg(RHSReg);
6332+
MI.getOperand(RHSOpndIdx).setReg(LHSReg);
63076333
Observer.changedInstr(MI);
63086334
}
63096335

llvm/test/CodeGen/AArch64/GlobalISel/combine-commute-fp-const-lhs.mir

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,129 @@ body: |
116116
$q0 = COPY %mul
117117
RET_ReallyLR
118118
...
119+
---
120+
name: fminnum
121+
tracksRegLiveness: true
122+
body: |
123+
bb.1:
124+
liveins: $s0
125+
126+
; CHECK-LABEL: name: fminnum
127+
; CHECK: liveins: $s0
128+
; CHECK-NEXT: {{ $}}
129+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $s0
130+
; CHECK-NEXT: %cst:_(s32) = G_FCONSTANT float 2.000000e+00
131+
; CHECK-NEXT: %min:_(s32) = G_FMINNUM [[COPY]], %cst
132+
; CHECK-NEXT: $s0 = COPY %min(s32)
133+
; CHECK-NEXT: RET_ReallyLR
134+
%0:_(s32) = COPY $s0
135+
%cst:_(s32) = G_FCONSTANT float 2.000000e+00
136+
%min:_(s32) = G_FMINNUM %cst, %0
137+
$s0 = COPY %min
138+
RET_ReallyLR
139+
...
140+
---
141+
name: fmaxnum
142+
tracksRegLiveness: true
143+
body: |
144+
bb.1:
145+
liveins: $s0
146+
147+
; CHECK-LABEL: name: fmaxnum
148+
; CHECK: liveins: $s0
149+
; CHECK-NEXT: {{ $}}
150+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $s0
151+
; CHECK-NEXT: %cst:_(s32) = G_FCONSTANT float 2.000000e+00
152+
; CHECK-NEXT: %max:_(s32) = G_FMAXNUM [[COPY]], %cst
153+
; CHECK-NEXT: $s0 = COPY %max(s32)
154+
; CHECK-NEXT: RET_ReallyLR
155+
%0:_(s32) = COPY $s0
156+
%cst:_(s32) = G_FCONSTANT float 2.000000e+00
157+
%max:_(s32) = G_FMAXNUM %cst, %0
158+
$s0 = COPY %max
159+
RET_ReallyLR
160+
...
161+
---
162+
name: fminnum_ieee
163+
tracksRegLiveness: true
164+
body: |
165+
bb.1:
166+
liveins: $s0
167+
168+
; CHECK-LABEL: name: fminnum_ieee
169+
; CHECK: liveins: $s0
170+
; CHECK-NEXT: {{ $}}
171+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $s0
172+
; CHECK-NEXT: %cst:_(s32) = G_FCONSTANT float 2.000000e+00
173+
; CHECK-NEXT: %min:_(s32) = G_FMINNUM_IEEE [[COPY]], %cst
174+
; CHECK-NEXT: $s0 = COPY %min(s32)
175+
; CHECK-NEXT: RET_ReallyLR
176+
%0:_(s32) = COPY $s0
177+
%cst:_(s32) = G_FCONSTANT float 2.000000e+00
178+
%min:_(s32) = G_FMINNUM_IEEE %cst, %0
179+
$s0 = COPY %min
180+
RET_ReallyLR
181+
...
182+
---
183+
name: fmaxnum_ieee
184+
tracksRegLiveness: true
185+
body: |
186+
bb.1:
187+
liveins: $s0
188+
189+
; CHECK-LABEL: name: fmaxnum_ieee
190+
; CHECK: liveins: $s0
191+
; CHECK-NEXT: {{ $}}
192+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $s0
193+
; CHECK-NEXT: %cst:_(s32) = G_FCONSTANT float 2.000000e+00
194+
; CHECK-NEXT: %max:_(s32) = G_FMAXNUM_IEEE [[COPY]], %cst
195+
; CHECK-NEXT: $s0 = COPY %max(s32)
196+
; CHECK-NEXT: RET_ReallyLR
197+
%0:_(s32) = COPY $s0
198+
%cst:_(s32) = G_FCONSTANT float 2.000000e+00
199+
%max:_(s32) = G_FMAXNUM_IEEE %cst, %0
200+
$s0 = COPY %max
201+
RET_ReallyLR
202+
...
203+
---
204+
name: fminimum
205+
tracksRegLiveness: true
206+
body: |
207+
bb.1:
208+
liveins: $s0
209+
210+
; CHECK-LABEL: name: fminimum
211+
; CHECK: liveins: $s0
212+
; CHECK-NEXT: {{ $}}
213+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $s0
214+
; CHECK-NEXT: %cst:_(s32) = G_FCONSTANT float 2.000000e+00
215+
; CHECK-NEXT: %min:_(s32) = G_FMINIMUM [[COPY]], %cst
216+
; CHECK-NEXT: $s0 = COPY %min(s32)
217+
; CHECK-NEXT: RET_ReallyLR
218+
%0:_(s32) = COPY $s0
219+
%cst:_(s32) = G_FCONSTANT float 2.000000e+00
220+
%min:_(s32) = G_FMINIMUM %cst, %0
221+
$s0 = COPY %min
222+
RET_ReallyLR
223+
...
224+
---
225+
name: fmaximum
226+
tracksRegLiveness: true
227+
body: |
228+
bb.1:
229+
liveins: $s0
230+
231+
; CHECK-LABEL: name: fmaximum
232+
; CHECK: liveins: $s0
233+
; CHECK-NEXT: {{ $}}
234+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $s0
235+
; CHECK-NEXT: %cst:_(s32) = G_FCONSTANT float 2.000000e+00
236+
; CHECK-NEXT: %max:_(s32) = G_FMAXIMUM [[COPY]], %cst
237+
; CHECK-NEXT: $s0 = COPY %max(s32)
238+
; CHECK-NEXT: RET_ReallyLR
239+
%0:_(s32) = COPY $s0
240+
%cst:_(s32) = G_FCONSTANT float 2.000000e+00
241+
%max:_(s32) = G_FMAXIMUM %cst, %0
242+
$s0 = COPY %max
243+
RET_ReallyLR
244+
...

0 commit comments

Comments
 (0)