[llvm] 5189eff - [InstCombine] Canonicalize (icmp eq/ne X, rotate(X)) to always use rotate-left
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 10 12:39:07 PDT 2023
Author: Noah Goldstein
Date: 2023-06-10T14:38:46-05:00
New Revision: 5189eff3459c4e60e7fd772b208b187eeef7e115
URL: https://github.com/llvm/llvm-project/commit/5189eff3459c4e60e7fd772b208b187eeef7e115
DIFF: https://github.com/llvm/llvm-project/commit/5189eff3459c4e60e7fd772b208b187eeef7e115.diff
LOG: [InstCombine] Canonicalize (icmp eq/ne X, rotate(X)) to always use rotate-left
We canonicalize rotate-right -> rotate-left in other places. Makes
sense to do so here as well.
Proof: https://alive2.llvm.org/ce/z/HL3TpK
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D152349
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-equality-rotate.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 23c161f80a41f..8624ac92c0c5b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5045,6 +5045,19 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
return new ICmpInst(CmpInst::getInversePredicate(Pred), Op1,
ConstantInt::getNullValue(Op1->getType()));
+ // Canonicalize:
+ // icmp eq/ne X, OneUse(rotate-right(X))
+ // -> icmp eq/ne X, rotate-left(X)
+ // We generally try to convert rotate-right -> rotate-left, this just
+ // canonicalizes another case.
+ CmpInst::Predicate PredUnused = Pred;
+ if (match(&I, m_c_ICmp(PredUnused, m_Value(A),
+ m_OneUse(m_Intrinsic<Intrinsic::fshr>(
+ m_Deferred(A), m_Deferred(A), m_Value(B))))))
+ return new ICmpInst(
+ Pred, A,
+ Builder.CreateIntrinsic(Op0->getType(), Intrinsic::fshl, {A, A, B}));
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-equality-rotate.ll b/llvm/test/Transforms/InstCombine/icmp-equality-rotate.ll
index f87f786e004f8..30c97a7f25275 100644
--- a/llvm/test/Transforms/InstCombine/icmp-equality-rotate.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-equality-rotate.ll
@@ -6,8 +6,8 @@ declare i8 @llvm.fshr.i8(i8, i8, i8)
declare void @use.i8(i8)
define i1 @cmpeq_rorr_to_rorl(i8 %x, i8 %C) {
; CHECK-LABEL: @cmpeq_rorr_to_rorl(
-; CHECK-NEXT: [[X_RORR:%.*]] = call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[C:%.*]])
-; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[X_RORR]], [[X]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[X]], i8 [[C:%.*]])
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP1]], [[X]]
; CHECK-NEXT: ret i1 [[R]]
;
%x_rorr = call i8 @llvm.fshr.i8(i8 %x, i8 %x, i8 %C)
@@ -64,7 +64,7 @@ define i1 @cmpeq_rorr_to_rorl_multiuse_fail(i8 %x, i8 %C) {
define i1 @cmpne_rorr_rorr(i8 %x, i8 %C0, i8 %C1) {
; CHECK-LABEL: @cmpne_rorr_rorr(
; CHECK-NEXT: [[TMP1:%.*]] = sub i8 [[C0:%.*]], [[C1:%.*]]
-; CHECK-NEXT: [[TMP2:%.*]] = call i8 @llvm.fshr.i8(i8 [[X:%.*]], i8 [[X]], i8 [[TMP1]])
+; CHECK-NEXT: [[TMP2:%.*]] = call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[X]], i8 [[TMP1]])
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[TMP2]], [[X]]
; CHECK-NEXT: ret i1 [[R]]
;
More information about the llvm-commits
mailing list