aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2024-06-19 12:36:36 +0200
committerNikita Popov <npopov@redhat.com>2024-06-19 12:36:36 +0200
commit2a1169088cd993bc123505ad8ff1b5fbb5ff6618 (patch)
treec994e3ee0c95917aa6464b607bc3ed5609347666
parent807222245e437aa693d0c8f8210cfe79a02cddb1 (diff)
downloadllvm-2a1169088cd993bc123505ad8ff1b5fbb5ff6618.zip
llvm-2a1169088cd993bc123505ad8ff1b5fbb5ff6618.tar.gz
llvm-2a1169088cd993bc123505ad8ff1b5fbb5ff6618.tar.bz2
[InstCombine] Preserve all gep flags when emitting offset
-rw-r--r--llvm/lib/Transforms/InstCombine/InstructionCombining.cpp2
-rw-r--r--llvm/test/Transforms/InstCombine/sub.ll18
2 files changed, 19 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index d1b1ec8..d926b1f 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 32ed4a7..49770e8 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