[llvm] 6ba5f8c - [InstCombine] Fix incorrect inbounds on GEP of GEP (PR44425)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 1 13:13:50 PST 2020


Author: Nikita Popov
Date: 2020-01-01T22:10:55+01:00
New Revision: 6ba5f8c4acb494ecd99a472706075b2153ddccf3

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

LOG: [InstCombine] Fix incorrect inbounds on GEP of GEP (PR44425)

This fixes https://bugs.llvm.org/show_bug.cgi?id=44425. We need to
drop inbounds if one of the GEPs is not inbounds. This was already
done when creating a new GEP, but not when modifying in place.

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

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/getelementptr.ll
    llvm/test/Transforms/InstCombine/phi-equal-incoming-pointers.ll
    llvm/test/Transforms/InstCombine/pr26992.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 9037dd731e5d..f2b3f15f9704 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1922,6 +1922,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
 
       // Update the GEP in place if possible.
       if (Src->getNumOperands() == 2) {
+        GEP.setIsInBounds(GEP.isInBounds() && Src->isInBounds());
         GEP.setOperand(0, Src->getOperand(0));
         GEP.setOperand(1, Sum);
         return &GEP;

diff  --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index 92a143fa1e9a..0c8dcdd0f0e0 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -1201,7 +1201,7 @@ define i32* @test_nzgep_zgep([1 x i32]* %base, i64 %idx) {
 
 define i32* @test_gep_inbounds_of_gep(i32* %base) {
 ; CHECK-LABEL: @test_gep_inbounds_of_gep(
-; CHECK-NEXT:    [[PTR2:%.*]] = getelementptr inbounds i32, i32* [[BASE:%.*]], i64 8
+; CHECK-NEXT:    [[PTR2:%.*]] = getelementptr i32, i32* [[BASE:%.*]], i64 8
 ; CHECK-NEXT:    ret i32* [[PTR2]]
 ;
   %ptr1 = getelementptr i32, i32* %base, i64 4

diff  --git a/llvm/test/Transforms/InstCombine/phi-equal-incoming-pointers.ll b/llvm/test/Transforms/InstCombine/phi-equal-incoming-pointers.ll
index 4fe575eaaa9b..db5402bd78c1 100644
--- a/llvm/test/Transforms/InstCombine/phi-equal-incoming-pointers.ll
+++ b/llvm/test/Transforms/InstCombine/phi-equal-incoming-pointers.ll
@@ -528,7 +528,7 @@ define i32 @test_extra_uses_multiple_geps(i1 %cond, i1 %cond2) {
 ; ALL-NEXT:    call void @foo.i32(i32* nonnull [[PTR1_TYPED]])
 ; ALL-NEXT:    br label [[EXIT:%.*]]
 ; ALL:       bb2:
-; ALL-NEXT:    [[PTR2_1:%.*]] = getelementptr inbounds i8, i8* [[OBJ]], i64 16
+; ALL-NEXT:    [[PTR2_1:%.*]] = getelementptr i8, i8* [[OBJ]], i64 16
 ; ALL-NEXT:    [[PTR2_TYPED:%.*]] = bitcast i8* [[PTR2_1]] to i32*
 ; ALL-NEXT:    [[RES2:%.*]] = load i32, i32* [[PTR2_TYPED]], align 4
 ; ALL-NEXT:    call void @foo.i32(i32* nonnull [[PTR2_TYPED]])

diff  --git a/llvm/test/Transforms/InstCombine/pr26992.ll b/llvm/test/Transforms/InstCombine/pr26992.ll
index 3041bdfd7c4a..da54877805ac 100644
--- a/llvm/test/Transforms/InstCombine/pr26992.ll
+++ b/llvm/test/Transforms/InstCombine/pr26992.ll
@@ -9,7 +9,7 @@ define i1 @test1(i8* %p) personality i32 (...)* @__CxxFrameHandler3 {
 ; CHECK-NEXT:    invoke void @may_throw()
 ; CHECK-NEXT:    to label [[INVOKE_CONT:%.*]] unwind label [[CATCH_DISPATCH:%.*]]
 ; CHECK:       invoke.cont:
-; CHECK-NEXT:    [[B:%.*]] = getelementptr inbounds i8, i8* [[P]], i64 2
+; CHECK-NEXT:    [[B:%.*]] = getelementptr i8, i8* [[P]], i64 2
 ; CHECK-NEXT:    invoke void @may_throw()
 ; CHECK-NEXT:    to label [[EXIT:%.*]] unwind label [[CATCH_DISPATCH]]
 ; CHECK:       catch.dispatch:


        


More information about the llvm-commits mailing list