[llvm] e6e0eb3 - [InstCombine] Strip bitcasts in GEP diff fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue May 24 07:12:10 PDT 2022


Author: Nikita Popov
Date: 2022-05-24T16:12:01+02:00
New Revision: e6e0eb3bc89d054889c1c663ccf600581ed77c7e

URL: https://github.com/llvm/llvm-project/commit/e6e0eb3bc89d054889c1c663ccf600581ed77c7e
DIFF: https://github.com/llvm/llvm-project/commit/e6e0eb3bc89d054889c1c663ccf600581ed77c7e.diff

LOG: [InstCombine] Strip bitcasts in GEP diff fold

Bitcasts were stripped in one case, but not the other. Of course,
this no longer really matters with opaque pointers, but as I went
through the trouble of tracking this down, we may as well remove
one typed vs opaque pointer optimization discrepancy.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/test/Transforms/InstCombine/getelementptr.ll
    llvm/test/Transforms/InstCombine/sub-gep.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 09d2e1726b72..486641009ecb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1688,7 +1688,8 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
   // Require at least one GEP with a common base pointer on both sides.
   if (auto *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
     // (gep X, ...) - X
-    if (LHSGEP->getOperand(0) == RHS) {
+    if (LHSGEP->getOperand(0)->stripPointerCasts() ==
+        RHS->stripPointerCasts()) {
       GEP1 = LHSGEP;
     } else if (auto *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
       // (gep X, ...) - (gep X, ...)

diff  --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index d5ffd9bd5769..b0b964c151eb 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -552,7 +552,7 @@ define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
 ; CHECK-NEXT:    [[T349:%.*]] = getelementptr [[STRUCT_SIGINFO_T:%.*]], %struct.siginfo_t* [[T344]], i64 0, i32 3, i32 0, i32 3, i32 0
 ; CHECK-NEXT:    [[T349350:%.*]] = bitcast i8** [[T349]] to i32*
 ; CHECK-NEXT:    [[T351:%.*]] = load i32, i32* [[T349350]], align 8
-; CHECK-NEXT:    [[T360:%.*]] = call i32 asm sideeffect "...", "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"(i32 [[T351]], %struct.__large_struct* elementtype(%struct.__large_struct) null, i32 -14, i32 0) #[[ATTR0:[0-9]+]]
+; CHECK-NEXT:    [[T360:%.*]] = call i32 asm sideeffect "...", "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"(i32 [[T351]], %struct.__large_struct* elementtype([[STRUCT___LARGE_STRUCT:%.*]]) null, i32 -14, i32 0) #[[ATTR0:[0-9]+]]
 ; CHECK-NEXT:    unreachable
 ;
 entry:
@@ -1300,9 +1300,8 @@ define i8* @gep_null_inbounds_
diff erent_type(i64 %idx1, i64 %idx2) {
 
 define i8* @D98588(i8* %c1, i64 %offset) {
 ; CHECK-LABEL: @D98588(
-; CHECK-NEXT:    [[C2:%.*]] = bitcast i8* [[C1:%.*]] to i64*
-; CHECK-NEXT:    [[C2_NEXT:%.*]] = getelementptr inbounds i64, i64* [[C2]], i64 [[OFFSET:%.*]]
-; CHECK-NEXT:    [[GEP:%.*]] = bitcast i64* [[C2_NEXT]] to i8*
+; CHECK-NEXT:    [[C2_NEXT_IDX:%.*]] = shl nsw i64 [[OFFSET:%.*]], 3
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds i8, i8* [[C1:%.*]], i64 [[C2_NEXT_IDX]]
 ; CHECK-NEXT:    ret i8* [[GEP]]
 ;
   %c2 = bitcast i8* %c1 to i64*

diff  --git a/llvm/test/Transforms/InstCombine/sub-gep.ll b/llvm/test/Transforms/InstCombine/sub-gep.ll
index b198e1423ccd..5cf5941e8974 100644
--- a/llvm/test/Transforms/InstCombine/sub-gep.ll
+++ b/llvm/test/Transforms/InstCombine/sub-gep.ll
@@ -372,13 +372,7 @@ define i64 @gep_
diff _second_inbounds(i8* %foo, i64 %i, i64 %j) {
 
 define i64 @gep_
diff _with_bitcast(i64* %p, i64 %idx) {
 ; CHECK-LABEL: @gep_
diff _with_bitcast(
-; CHECK-NEXT:    [[I0:%.*]] = bitcast i64* [[P:%.*]] to [4 x i64]*
-; CHECK-NEXT:    [[I1:%.*]] = getelementptr inbounds [4 x i64], [4 x i64]* [[I0]], i64 [[IDX:%.*]]
-; CHECK-NEXT:    [[I3:%.*]] = ptrtoint [4 x i64]* [[I1]] to i64
-; CHECK-NEXT:    [[I4:%.*]] = ptrtoint i64* [[P]] to i64
-; CHECK-NEXT:    [[I5:%.*]] = sub nuw i64 [[I3]], [[I4]]
-; CHECK-NEXT:    [[I6:%.*]] = lshr i64 [[I5]], 5
-; CHECK-NEXT:    ret i64 [[I6]]
+; CHECK-NEXT:    ret i64 [[IDX:%.*]]
 ;
   %i0 = bitcast i64* %p to [4 x i64]*
   %i1 = getelementptr inbounds [4 x i64], [4 x i64]* %i0, i64 %idx


        


More information about the llvm-commits mailing list