diff options
author | Thorsten Schütt <schuett@gmail.com> | 2024-05-18 13:43:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-18 13:43:10 +0200 |
commit | 778826f0b8fb9f1a595ce8ab23faad6daa9466f5 (patch) | |
tree | 1af1086eab570b62ac780b2e1d65a14e41a9bcf6 | |
parent | f7b0b99c52ee36ed6ca8abcce74a752e483768d6 (diff) | |
download | llvm-778826f0b8fb9f1a595ce8ab23faad6daa9466f5.zip llvm-778826f0b8fb9f1a595ce8ab23faad6daa9466f5.tar.gz llvm-778826f0b8fb9f1a595ce8ab23faad6daa9466f5.tar.bz2 |
[GlobalIsel] Combine select to integer min max more (#92570)
-rw-r--r-- | llvm/include/llvm/Target/GlobalISel/Combine.td | 2 | ||||
-rw-r--r-- | llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp | 13 | ||||
-rw-r--r-- | llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir | 3 |
3 files changed, 12 insertions, 6 deletions
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td index a1d39d9..5d4b5a2 100644 --- a/llvm/include/llvm/Target/GlobalISel/Combine.td +++ b/llvm/include/llvm/Target/GlobalISel/Combine.td @@ -1309,7 +1309,7 @@ def select_to_minmax: GICombineRule< def select_to_iminmax: GICombineRule< (defs root:$root, build_fn_matchinfo:$info), - (match (G_ICMP $tst, $tst1, $x, $y), + (match (G_ICMP $tst, $tst1, $a, $b), (G_SELECT $root, $tst, $x, $y), [{ return Helper.matchSelectIMinMax(${root}, ${info}); }]), (apply [{ Helper.applyBuildFnMO(${root}, ${info}); }])>; diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp index a87a26d..22eb4a3 100644 --- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp @@ -6772,13 +6772,20 @@ bool CombinerHelper::matchSelectIMinMax(const MachineOperand &MO, if (CmpInst::isEquality(Pred)) return false; - [[maybe_unused]] Register CmpLHS = Cmp->getLHSReg(); - [[maybe_unused]] Register CmpRHS = Cmp->getRHSReg(); + Register CmpLHS = Cmp->getLHSReg(); + Register CmpRHS = Cmp->getRHSReg(); + + // We can swap CmpLHS and CmpRHS for higher hitrate. + if (True == CmpRHS && False == CmpLHS) { + std::swap(CmpLHS, CmpRHS); + Pred = CmpInst::getSwappedPredicate(Pred); + } // (icmp X, Y) ? X : Y -> integer minmax. // see matchSelectPattern in ValueTracking. // Legality between G_SELECT and integer minmax can differ. - assert(True == CmpLHS && False == CmpRHS && "unexpected MIR pattern"); + if (True != CmpLHS || False != CmpRHS) + return false; switch (Pred) { case ICmpInst::ICMP_UGT: diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir index 8d9ad8d..353c1550 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir +++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir @@ -911,8 +911,7 @@ body: | ; CHECK-NEXT: %f1:_(s32) = G_TRUNC [[COPY1]](s64) ; CHECK-NEXT: %t:_(<4 x s32>) = G_BUILD_VECTOR %t1(s32), %t1(s32), %t1(s32), %t1(s32) ; CHECK-NEXT: %f:_(<4 x s32>) = G_BUILD_VECTOR %f1(s32), %f1(s32), %f1(s32), %f1(s32) - ; CHECK-NEXT: %c:_(<4 x s32>) = G_ICMP intpred(sle), %f(<4 x s32>), %t - ; CHECK-NEXT: %sel:_(<4 x s32>) = exact G_SELECT %c(<4 x s32>), %t, %f + ; CHECK-NEXT: %sel:_(<4 x s32>) = G_SMAX %t, %f ; CHECK-NEXT: $q0 = COPY %sel(<4 x s32>) %0:_(s64) = COPY $x0 %1:_(s64) = COPY $x1 |