[llvm] 30a12f3 - [InstCombine] Fix GEP with same index comparison with opaque pointers

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 28 00:23:59 PST 2021


Author: Nikita Popov
Date: 2021-12-28T09:23:28+01:00
New Revision: 30a12f3f6322399185fdceffe176152a58bb84ae

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

LOG: [InstCombine] Fix GEP with same index comparison with opaque pointers

We need to also check that the source element type is the same,
otherwise the indices may have different meaning. The added
addrspacecast demonstrates that we do still need to check the
pointer type.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/getelementptr.ll
    llvm/test/Transforms/InstCombine/opaque-ptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index ed53b88aed615..62a43b00773a7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -894,9 +894,10 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
     // If the base pointers are 
diff erent, but the indices are the same, just
     // compare the base pointer.
     if (PtrBase != GEPRHS->getOperand(0)) {
-      bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
-      IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
-                        GEPRHS->getOperand(0)->getType();
+      bool IndicesTheSame =
+          GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
+          GEPLHS->getType() == GEPRHS->getType() &&
+          GEPLHS->getSourceElementType() == GEPRHS->getSourceElementType();
       if (IndicesTheSame)
         for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
           if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {

diff  --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index e57eccc84b5f3..be3a7b1ac8040 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -151,11 +151,25 @@ define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
 ;
   %t1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
   %t3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1
-  ;; seteq x, y
   %t4 = icmp eq i32* %t1, %t3
   ret i1 %t4
 }
 
+define i1 @test10_addrspacecast({ i32, i32 }* %x, { i32, i32 } addrspace(3)* %y) {
+; CHECK-LABEL: @test10_addrspacecast(
+; CHECK-NEXT:    [[T1:%.*]] = getelementptr { i32, i32 }, { i32, i32 }* [[X:%.*]], i64 0, i32 1
+; CHECK-NEXT:    [[T3:%.*]] = getelementptr { i32, i32 }, { i32, i32 } addrspace(3)* [[Y:%.*]], i64 0, i32 1
+; CHECK-NEXT:    [[T3_C:%.*]] = addrspacecast i32 addrspace(3)* [[T3]] to i32*
+; CHECK-NEXT:    [[T4:%.*]] = icmp eq i32* [[T1]], [[T3_C]]
+; CHECK-NEXT:    ret i1 [[T4]]
+;
+  %t1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
+  %t3 = getelementptr { i32, i32 }, { i32, i32 } addrspace(3)* %y, i32 0, i32 1
+  %t3.c = addrspacecast i32 addrspace(3)* %t3 to i32*
+  %t4 = icmp eq i32* %t1, %t3.c
+  ret i1 %t4
+}
+
 define i1 @test11({ i32, i32 }* %X) {
 ; CHECK-LABEL: @test11(
 ; CHECK-NEXT:    [[Q:%.*]] = icmp eq { i32, i32 }* [[X:%.*]], null

diff  --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index ab2a109f6103b..4ebf68f918e17 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -182,3 +182,27 @@ define ptr @geps_not_combinable(ptr %a) {
   %a3 = getelementptr { i32, i32 }, ptr %a2, i32 0, i32 1
   ret ptr %a3
 }
+
+define i1 @compare_geps_same_indices(ptr %a, ptr %b, i64 %idx) {
+; CHECK-LABEL: @compare_geps_same_indices(
+; CHECK-NEXT:    [[C:%.*]] = icmp eq ptr [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    ret i1 [[C]]
+;
+  %a2 = getelementptr i32, ptr %a, i64 %idx
+  %b2 = getelementptr i32, ptr %b, i64 %idx
+  %c = icmp eq ptr %a2, %b2
+  ret i1 %c
+}
+
+define i1 @compare_geps_same_indices_
diff erent_types(ptr %a, ptr %b, i64 %idx) {
+; CHECK-LABEL: @compare_geps_same_indices_
diff erent_types(
+; CHECK-NEXT:    [[A2:%.*]] = getelementptr i32, ptr [[A:%.*]], i64 [[IDX:%.*]]
+; CHECK-NEXT:    [[B2:%.*]] = getelementptr i64, ptr [[B:%.*]], i64 [[IDX]]
+; CHECK-NEXT:    [[C:%.*]] = icmp eq ptr [[A2]], [[B2]]
+; CHECK-NEXT:    ret i1 [[C]]
+;
+  %a2 = getelementptr i32, ptr %a, i64 %idx
+  %b2 = getelementptr i64, ptr %b, i64 %idx
+  %c = icmp eq ptr %a2, %b2
+  ret i1 %c
+}


        


More information about the llvm-commits mailing list