Skip to content

Commit 60984f5

Browse files
committed
[InstCombine] Preserve all gep flags in gep of exact div fold
1 parent 7811330 commit 60984f5

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,10 +2941,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
29412941
m_SpecificInt(countr_zero(TyAllocSize)))))) ||
29422942
match(GEP.getOperand(1),
29432943
m_Exact(m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
2944-
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
2945-
Builder.getInt8Ty(), GEP.getPointerOperand(), V);
2946-
NewGEP->setIsInBounds(GEP.isInBounds());
2947-
return NewGEP;
2944+
return GetElementPtrInst::Create(Builder.getInt8Ty(),
2945+
GEP.getPointerOperand(), V,
2946+
GEP.getNoWrapFlags());
29482947
}
29492948
}
29502949
}

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,16 @@ define ptr @gep_sdiv_inbounds(ptr %p, i64 %off) {
15661566
ret ptr %ptr
15671567
}
15681568

1569+
define ptr @gep_sdiv_nuw(ptr %p, i64 %off) {
1570+
; CHECK-LABEL: @gep_sdiv_nuw(
1571+
; CHECK-NEXT: [[PTR:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
1572+
; CHECK-NEXT: ret ptr [[PTR]]
1573+
;
1574+
%index = sdiv exact i64 %off, 7
1575+
%ptr = getelementptr nuw %struct.C, ptr %p, i64 %index
1576+
ret ptr %ptr
1577+
}
1578+
15691579
define ptr @gep_ashr(ptr %p, i64 %off) {
15701580
; CHECK-LABEL: @gep_ashr(
15711581
; CHECK-NEXT: [[PTR:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[OFF:%.*]]

0 commit comments

Comments
 (0)