[llvm] 8dd9a13 - [InstCombine] Preserve inbounds when merging with zero-index GEP (PR44423)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 1 14:08:37 PST 2020


Author: Nikita Popov
Date: 2020-01-01T23:04:28+01:00
New Revision: 8dd9a1361958f0cc53d100124e158cbe691c4628

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

LOG: [InstCombine] Preserve inbounds when merging with zero-index GEP (PR44423)

This addresses https://bugs.llvm.org/show_bug.cgi?id=44423.
If one of the GEPs is inbounds and the other is zero-index,
we can also preserve inbounds.

Differential Revision: https://reviews.llvm.org/D72060

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/getelementptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index f2b3f15f9704..8e61e3d9fdb2 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1649,6 +1649,15 @@ Instruction *InstCombiner::narrowMathIfNoOverflow(BinaryOperator &BO) {
   return CastInst::Create(CastOpc, NarrowBO, BO.getType());
 }
 
+bool isMergedGEPInBounds(GEPOperator &GEP1, GEPOperator &GEP2) {
+  // At least one GEP must be inbounds.
+  if (!GEP1.isInBounds() && !GEP2.isInBounds())
+    return false;
+
+  return (GEP1.isInBounds() || GEP1.hasAllZeroIndices()) &&
+         (GEP2.isInBounds() || GEP2.hasAllZeroIndices());
+}
+
 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
   SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
   Type *GEPType = GEP.getType();
@@ -1922,7 +1931,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
 
       // Update the GEP in place if possible.
       if (Src->getNumOperands() == 2) {
-        GEP.setIsInBounds(GEP.isInBounds() && Src->isInBounds());
+        GEP.setIsInBounds(isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP)));
         GEP.setOperand(0, Src->getOperand(0));
         GEP.setOperand(1, Sum);
         return &GEP;
@@ -1939,7 +1948,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
     }
 
     if (!Indices.empty())
-      return GEP.isInBounds() && Src->isInBounds()
+      return isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP))
                  ? GetElementPtrInst::CreateInBounds(
                        Src->getSourceElementType(), Src->getOperand(0), Indices,
                        GEP.getName())

diff  --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index 0c8dcdd0f0e0..36e09cd0ad76 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -1171,7 +1171,7 @@ define <2 x i32*> @PR32414(i32** %ptr) {
 
 define i32* @test_bitcast_nzgep([1 x i32]* %base, i64 %idx) {
 ; CHECK-LABEL: @test_bitcast_nzgep(
-; CHECK-NEXT:    [[PTR:%.*]] = getelementptr [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
+; CHECK-NEXT:    [[PTR:%.*]] = getelementptr inbounds [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
 ; CHECK-NEXT:    ret i32* [[PTR]]
 ;
   %base2 = bitcast [1 x i32]* %base to i32*
@@ -1181,7 +1181,7 @@ define i32* @test_bitcast_nzgep([1 x i32]* %base, i64 %idx) {
 
 define i32* @test_zgep_nzgep([1 x i32]* %base, i64 %idx) {
 ; CHECK-LABEL: @test_zgep_nzgep(
-; CHECK-NEXT:    [[PTR:%.*]] = getelementptr [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
+; CHECK-NEXT:    [[PTR:%.*]] = getelementptr inbounds [1 x i32], [1 x i32]* [[BASE:%.*]], i64 0, i64 [[IDX:%.*]]
 ; CHECK-NEXT:    ret i32* [[PTR]]
 ;
   %base2 = getelementptr [1 x i32], [1 x i32]* %base, i64 0, i64 0
@@ -1191,7 +1191,7 @@ define i32* @test_zgep_nzgep([1 x i32]* %base, i64 %idx) {
 
 define i32* @test_nzgep_zgep([1 x i32]* %base, i64 %idx) {
 ; CHECK-LABEL: @test_nzgep_zgep(
-; CHECK-NEXT:    [[PTR:%.*]] = getelementptr [1 x i32], [1 x i32]* [[BASE:%.*]], i64 [[IDX:%.*]], i64 0
+; CHECK-NEXT:    [[PTR:%.*]] = getelementptr inbounds [1 x i32], [1 x i32]* [[BASE:%.*]], i64 [[IDX:%.*]], i64 0
 ; CHECK-NEXT:    ret i32* [[PTR]]
 ;
   %base2 = getelementptr inbounds [1 x i32], [1 x i32]* %base, i64 %idx


        


More information about the llvm-commits mailing list