-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[InstCombine] Fold icmp pred (inttoptr X), (inttoptr Y) -> icmp pred X, Y
#77832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesAlive2 proofs are unavailable because Full diff: https://github.com/llvm/llvm-project/pull/77832.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 7c1aff445524de..da94bb1d2a8f36 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5703,6 +5703,15 @@ Instruction *InstCombinerImpl::foldICmpWithCastOp(ICmpInst &ICmp) {
return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1);
}
+ // Turn icmp pred (inttoptr x), (inttoptr y) into icmp pred x, y
+ if (CastOp0->getOpcode() == Instruction::IntToPtr &&
+ CompatibleSizes(DestTy, SrcTy)) {
+ Value *Op1Src;
+ if (match(ICmp.getOperand(1), m_IntToPtr(m_Value(Op1Src))) &&
+ Op1Src->getType() == SrcTy)
+ return new ICmpInst(ICmp.getPredicate(), Op0Src, Op1Src);
+ }
+
if (Instruction *R = foldICmpWithTrunc(ICmp))
return R;
diff --git a/llvm/test/Transforms/InstCombine/cast_ptr.ll b/llvm/test/Transforms/InstCombine/cast_ptr.ll
index 5c6c012064e05b..8865af0a34a401 100644
--- a/llvm/test/Transforms/InstCombine/cast_ptr.ll
+++ b/llvm/test/Transforms/InstCombine/cast_ptr.ll
@@ -113,6 +113,51 @@ define i1 @test4(i32 %A) {
ret i1 %C
}
+define i1 @test4_icmp_with_var(i32 %A1, i32 %A2) {
+; CHECK-LABEL: @test4_icmp_with_var(
+; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[A1:%.*]], [[A2:%.*]]
+; CHECK-NEXT: ret i1 [[C]]
+;
+ %B1 = inttoptr i32 %A1 to ptr
+ %B2 = inttoptr i32 %A2 to ptr
+ %C = icmp ugt ptr %B1, %B2
+ ret i1 %C
+}
+
+define i1 @test4_cmp_with_nonnull_constant(i32 %A) {
+; CHECK-LABEL: @test4_cmp_with_nonnull_constant(
+; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[A:%.*]], 1
+; CHECK-NEXT: ret i1 [[C]]
+;
+ %B = inttoptr i32 %A to ptr
+ %C = icmp eq ptr %B, inttoptr (i32 1 to ptr)
+ ret i1 %C
+}
+
+define i1 @test4_cmp_eq_0_or_1(i32 %x) {
+; CHECK-LABEL: @test4_cmp_eq_0_or_1(
+; CHECK-NEXT: [[OR:%.*]] = icmp ult i32 [[X:%.*]], 2
+; CHECK-NEXT: ret i1 [[OR]]
+;
+ %cast = inttoptr i32 %x to ptr
+ %tobool = icmp eq i32 %x, 0
+ %cmp = icmp eq ptr %cast, inttoptr (i32 1 to ptr)
+ %or = or i1 %tobool, %cmp
+ ret i1 %or
+}
+
+define i1 @test4_icmp_with_var_mismatched_type(i32 %A1, i64 %A2) {
+; CHECK-LABEL: @test4_icmp_with_var_mismatched_type(
+; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[A2:%.*]] to i32
+; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[TMP1]], [[A1:%.*]]
+; CHECK-NEXT: ret i1 [[C]]
+;
+ %B1 = inttoptr i32 %A1 to ptr
+ %B2 = inttoptr i64 %A2 to ptr
+ %C = icmp ugt ptr %B1, %B2
+ ret i1 %C
+}
+
define i1 @test4_as2(i16 %A) {
; CHECK-LABEL: @test4_as2(
; CHECK-NEXT: [[C:%.*]] = icmp eq i16 [[A:%.*]], 0
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Suspecting miscompile causing https://lab.llvm.org/buildbot/#/builders/236/builds/8673 |
Thank you for reporting this! I will investigate it. |
Waiting for the result of https://lab.llvm.org/buildbot/#/builders/239/builds/5285... |
We passed the CI with asan: https://lab.llvm.org/buildbot/#/builders/239/builds/5285. |
That's why I suspect miscompile. But so far it's just a suspicion. I am bisecting to be sure, that build is slow. |
Bisect is not finished, but it passed after this patch. |
…cmp pred X, Y`" (#78023) Reverts #77832 To fix https://lab.llvm.org/buildbot/#/builders/236/builds/8673 Also truncation to shorter type looks incorrect. Issue for tracking #78024 .
… X, Y` (llvm#77832) NOTE: Alive2 proofs are unavailable because `inttoptr` is unsupported.
…cmp pred X, Y`" (llvm#78023) Reverts llvm#77832 To fix https://lab.llvm.org/buildbot/#/builders/236/builds/8673 Also truncation to shorter type looks incorrect. Issue for tracking llvm#78024 .
Alive2 proofs are unavailable because
inttoptr
is unsupported.