Skip to content

Commit 778826f

Browse files
authored
[GlobalIsel] Combine select to integer min max more (llvm#92570)
1 parent f7b0b99 commit 778826f

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ def select_to_minmax: GICombineRule<
13091309

13101310
def select_to_iminmax: GICombineRule<
13111311
(defs root:$root, build_fn_matchinfo:$info),
1312-
(match (G_ICMP $tst, $tst1, $x, $y),
1312+
(match (G_ICMP $tst, $tst1, $a, $b),
13131313
(G_SELECT $root, $tst, $x, $y),
13141314
[{ return Helper.matchSelectIMinMax(${root}, ${info}); }]),
13151315
(apply [{ Helper.applyBuildFnMO(${root}, ${info}); }])>;

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6772,13 +6772,20 @@ bool CombinerHelper::matchSelectIMinMax(const MachineOperand &MO,
67726772
if (CmpInst::isEquality(Pred))
67736773
return false;
67746774

6775-
[[maybe_unused]] Register CmpLHS = Cmp->getLHSReg();
6776-
[[maybe_unused]] Register CmpRHS = Cmp->getRHSReg();
6775+
Register CmpLHS = Cmp->getLHSReg();
6776+
Register CmpRHS = Cmp->getRHSReg();
6777+
6778+
// We can swap CmpLHS and CmpRHS for higher hitrate.
6779+
if (True == CmpRHS && False == CmpLHS) {
6780+
std::swap(CmpLHS, CmpRHS);
6781+
Pred = CmpInst::getSwappedPredicate(Pred);
6782+
}
67776783

67786784
// (icmp X, Y) ? X : Y -> integer minmax.
67796785
// see matchSelectPattern in ValueTracking.
67806786
// Legality between G_SELECT and integer minmax can differ.
6781-
assert(True == CmpLHS && False == CmpRHS && "unexpected MIR pattern");
6787+
if (True != CmpLHS || False != CmpRHS)
6788+
return false;
67826789

67836790
switch (Pred) {
67846791
case ICmpInst::ICMP_UGT:

llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,7 @@ body: |
911911
; CHECK-NEXT: %f1:_(s32) = G_TRUNC [[COPY1]](s64)
912912
; CHECK-NEXT: %t:_(<4 x s32>) = G_BUILD_VECTOR %t1(s32), %t1(s32), %t1(s32), %t1(s32)
913913
; CHECK-NEXT: %f:_(<4 x s32>) = G_BUILD_VECTOR %f1(s32), %f1(s32), %f1(s32), %f1(s32)
914-
; CHECK-NEXT: %c:_(<4 x s32>) = G_ICMP intpred(sle), %f(<4 x s32>), %t
915-
; CHECK-NEXT: %sel:_(<4 x s32>) = exact G_SELECT %c(<4 x s32>), %t, %f
914+
; CHECK-NEXT: %sel:_(<4 x s32>) = G_SMAX %t, %f
916915
; CHECK-NEXT: $q0 = COPY %sel(<4 x s32>)
917916
%0:_(s64) = COPY $x0
918917
%1:_(s64) = COPY $x1

0 commit comments

Comments
 (0)