[llvm] 2a11690 - [InstCombine] Preserve all gep flags when emitting offset

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 03:37:22 PDT 2024


Author: Nikita Popov
Date: 2024-06-19T12:36:36+02:00
New Revision: 2a1169088cd993bc123505ad8ff1b5fbb5ff6618

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

LOG: [InstCombine] Preserve all gep flags when emitting offset

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index d1b1ec8215a33..d926b1f3f568f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -206,7 +206,7 @@ Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
       !GEP->getSourceElementType()->isIntegerTy(8)) {
     replaceInstUsesWith(
         *Inst, Builder.CreateGEP(Builder.getInt8Ty(), GEP->getPointerOperand(),
-                                 Offset, "", GEP->isInBounds()));
+                                 Offset, "", GEP->getNoWrapFlags()));
     eraseInstFromFunction(*Inst);
   }
   return Offset;

diff  --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 32ed4a787e926..49770e8d9e43f 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -1160,6 +1160,24 @@ define i64 @test60(ptr %foo, i64 %i, i64 %j) {
   ret i64 %sub
 }
 
+define i64 @test60_nuw(ptr %foo, i64 %i, i64 %j) {
+; CHECK-LABEL: @test60_nuw(
+; CHECK-NEXT:    [[GEP1_IDX:%.*]] = mul nuw i64 [[J:%.*]], 100
+; CHECK-NEXT:    [[GEP1_OFFS:%.*]] = add nuw i64 [[GEP1_IDX]], [[I:%.*]]
+; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr nuw i8, ptr [[FOO:%.*]], i64 [[GEP1_OFFS]]
+; CHECK-NEXT:    [[GEPDIFF:%.*]] = add i64 [[GEP1_OFFS]], -4200
+; CHECK-NEXT:    store ptr [[GEP1]], ptr @dummy_global1, align 8
+; CHECK-NEXT:    ret i64 [[GEPDIFF]]
+;
+  %gep1 = getelementptr nuw [100 x [100 x i8]], ptr %foo, i64 0, i64 %j, i64 %i
+  %gep2 = getelementptr nuw [100 x [100 x i8]], ptr %foo, i64 0, i64 42, i64 0
+  %cast1 = ptrtoint ptr %gep1 to i64
+  %cast2 = ptrtoint ptr %gep2 to i64
+  %sub = sub i64 %cast1, %cast2
+  store ptr %gep1, ptr @dummy_global1
+  ret i64 %sub
+}
+
 define i64 @test61(ptr %foo, i64 %i, i64 %j) {
 ; CHECK-LABEL: @test61(
 ; CHECK-NEXT:    [[GEP2_IDX:%.*]] = mul nsw i64 [[J:%.*]], 100


        


More information about the llvm-commits mailing list