Skip to content

Commit b25c73d

Browse files
nikicAlexisPerry
authored andcommitted
[InstCombine] Preserve all gep flags when emitting offset
1 parent cf00036 commit b25c73d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
206206
!GEP->getSourceElementType()->isIntegerTy(8)) {
207207
replaceInstUsesWith(
208208
*Inst, Builder.CreateGEP(Builder.getInt8Ty(), GEP->getPointerOperand(),
209-
Offset, "", GEP->isInBounds()));
209+
Offset, "", GEP->getNoWrapFlags()));
210210
eraseInstFromFunction(*Inst);
211211
}
212212
return Offset;

llvm/test/Transforms/InstCombine/sub.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,24 @@ define i64 @test60(ptr %foo, i64 %i, i64 %j) {
11601160
ret i64 %sub
11611161
}
11621162

1163+
define i64 @test60_nuw(ptr %foo, i64 %i, i64 %j) {
1164+
; CHECK-LABEL: @test60_nuw(
1165+
; CHECK-NEXT: [[GEP1_IDX:%.*]] = mul nuw i64 [[J:%.*]], 100
1166+
; CHECK-NEXT: [[GEP1_OFFS:%.*]] = add nuw i64 [[GEP1_IDX]], [[I:%.*]]
1167+
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr nuw i8, ptr [[FOO:%.*]], i64 [[GEP1_OFFS]]
1168+
; CHECK-NEXT: [[GEPDIFF:%.*]] = add i64 [[GEP1_OFFS]], -4200
1169+
; CHECK-NEXT: store ptr [[GEP1]], ptr @dummy_global1, align 8
1170+
; CHECK-NEXT: ret i64 [[GEPDIFF]]
1171+
;
1172+
%gep1 = getelementptr nuw [100 x [100 x i8]], ptr %foo, i64 0, i64 %j, i64 %i
1173+
%gep2 = getelementptr nuw [100 x [100 x i8]], ptr %foo, i64 0, i64 42, i64 0
1174+
%cast1 = ptrtoint ptr %gep1 to i64
1175+
%cast2 = ptrtoint ptr %gep2 to i64
1176+
%sub = sub i64 %cast1, %cast2
1177+
store ptr %gep1, ptr @dummy_global1
1178+
ret i64 %sub
1179+
}
1180+
11631181
define i64 @test61(ptr %foo, i64 %i, i64 %j) {
11641182
; CHECK-LABEL: @test61(
11651183
; CHECK-NEXT: [[GEP2_IDX:%.*]] = mul nsw i64 [[J:%.*]], 100

0 commit comments

Comments
 (0)