Skip to content

Commit 99d8bc9

Browse files
committed
[InstCombine] Preserve all gep nowrap flags in ptradd canonicalization
1 parent e6bc72d commit 99d8bc9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,7 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
27992799
m_Shl(m_Value(), m_ConstantInt())))))) {
28002800
Value *Offset = EmitGEPOffset(cast<GEPOperator>(&GEP));
28012801
return replaceInstUsesWith(
2802-
GEP, Builder.CreatePtrAdd(PtrOp, Offset, "", GEP.isInBounds()));
2802+
GEP, Builder.CreatePtrAdd(PtrOp, Offset, "", GEP.getNoWrapFlags()));
28032803
}
28042804

28052805
// Check to see if the inputs to the PHI node are getelementptr instructions.

llvm/test/Transforms/InstCombine/canonicalize-gep-mul.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ entry:
1515
ret ptr %gep
1616
}
1717

18+
define ptr @mul4_nuw(ptr %p, i64 %x) {
19+
; CHECK-LABEL: define ptr @mul4_nuw(
20+
; CHECK-SAME: ptr [[P:%.*]], i64 [[X:%.*]]) {
21+
; CHECK-NEXT: entry:
22+
; CHECK-NEXT: [[GEP_IDX:%.*]] = shl i64 [[X]], 4
23+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i8, ptr [[P]], i64 [[GEP_IDX]]
24+
; CHECK-NEXT: ret ptr [[GEP]]
25+
;
26+
entry:
27+
%mul = mul i64 %x, 4
28+
%gep = getelementptr nuw i32, ptr %p, i64 %mul
29+
ret ptr %gep
30+
}
31+
1832
define ptr @mul5(ptr %p, i64 %x) {
1933
; CHECK-LABEL: define ptr @mul5(
2034
; CHECK-SAME: ptr [[P:%.*]], i64 [[X:%.*]]) {

0 commit comments

Comments
 (0)