Skip to content

Commit 9cae598

Browse files
clin111LebedevRI
authored andcommitted
[InstCombine] Avoid folding GEPs across loop boundaries
Folding a GEP from outside to inside a loop will materialize an add where there wasn't an equivalent operation before. Check the containing loops before making this fold. Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D107935
1 parent 0f09056 commit 9cae598

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -2132,8 +2132,17 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
21322132
}
21332133
}
21342134

2135+
// Guard the gep(gep) fold so we don't create an add inside a loop
2136+
// when there wasn't an equivalent instruction there before.
2137+
bool DifferentLoops = false;
2138+
if (LI)
2139+
if (auto *GEPLoop = LI->getLoopFor(GEP.getParent()))
2140+
if (auto *SrcOpI = dyn_cast<Instruction>(Src))
2141+
if (LI->getLoopFor(SrcOpI->getParent()) != GEPLoop)
2142+
DifferentLoops = true;
2143+
21352144
// Fold (gep(gep(Ptr,Idx0),Idx1) -> gep(Ptr,add(Idx0,Idx1))
2136-
if (GO1->getType() == SO1->getType()) {
2145+
if (!DifferentLoops && GO1->getType() == SO1->getType()) {
21372146
bool NewInBounds = GEP.isInBounds() && Src->isInBounds();
21382147
auto *NewIdx =
21392148
Builder.CreateAdd(GO1, SO1, GEP.getName() + ".idx",

llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ define float @gep_cross_loop(i64* %_arg_, float* %_arg_3, float %_arg_8)
216216
; CHECK-LABEL: @gep_cross_loop(
217217
; CHECK-NEXT: entry:
218218
; CHECK-NEXT: [[TMP0:%.*]] = load i64, i64* [[_ARG_:%.*]], align 8
219+
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds float, float* [[_ARG_3:%.*]], i64 [[TMP0]]
219220
; CHECK-NEXT: br label [[FOR_COND_I:%.*]]
220221
; CHECK: for.cond.i:
221222
; CHECK-NEXT: [[IDX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[ADD11_I:%.*]], [[FOR_BODY_I:%.*]] ]
@@ -225,8 +226,7 @@ define float @gep_cross_loop(i64* %_arg_, float* %_arg_3, float %_arg_8)
225226
; CHECK: for.cond.i.i.i.preheader:
226227
; CHECK-NEXT: ret float [[SUM]]
227228
; CHECK: for.body.i:
228-
; CHECK-NEXT: [[ARRAYIDX_I84_I_IDX:%.*]] = add nsw i64 [[IDX]], [[TMP0]]
229-
; CHECK-NEXT: [[ARRAYIDX_I84_I:%.*]] = getelementptr inbounds float, float* [[_ARG_3:%.*]], i64 [[ARRAYIDX_I84_I_IDX]]
229+
; CHECK-NEXT: [[ARRAYIDX_I84_I:%.*]] = getelementptr inbounds float, float* [[ADD_PTR]], i64 [[IDX]]
230230
; CHECK-NEXT: [[TMP1:%.*]] = load float, float* [[ARRAYIDX_I84_I]], align 4
231231
; CHECK-NEXT: [[ADD_I]] = fadd fast float [[SUM]], [[TMP1]]
232232
; CHECK-NEXT: [[ADD11_I]] = add nuw nsw i64 [[IDX]], 1

0 commit comments

Comments
 (0)