Skip to content

Commit

Permalink
[InstCombine] Avoid folding GEPs across loop boundaries
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clin111 authored and LebedevRI committed Aug 19, 2021
1 parent 0f09056 commit 9cae598
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -2132,8 +2132,17 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
}
}

// Guard the gep(gep) fold so we don't create an add inside a loop
// when there wasn't an equivalent instruction there before.
bool DifferentLoops = false;
if (LI)
if (auto *GEPLoop = LI->getLoopFor(GEP.getParent()))
if (auto *SrcOpI = dyn_cast<Instruction>(Src))
if (LI->getLoopFor(SrcOpI->getParent()) != GEPLoop)
DifferentLoops = true;

// Fold (gep(gep(Ptr,Idx0),Idx1) -> gep(Ptr,add(Idx0,Idx1))
if (GO1->getType() == SO1->getType()) {
if (!DifferentLoops && GO1->getType() == SO1->getType()) {
bool NewInBounds = GEP.isInBounds() && Src->isInBounds();
auto *NewIdx =
Builder.CreateAdd(GO1, SO1, GEP.getName() + ".idx",
Expand Down
Expand Up @@ -216,6 +216,7 @@ define float @gep_cross_loop(i64* %_arg_, float* %_arg_3, float %_arg_8)
; CHECK-LABEL: @gep_cross_loop(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = load i64, i64* [[_ARG_:%.*]], align 8
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds float, float* [[_ARG_3:%.*]], i64 [[TMP0]]
; CHECK-NEXT: br label [[FOR_COND_I:%.*]]
; CHECK: for.cond.i:
; CHECK-NEXT: [[IDX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[ADD11_I:%.*]], [[FOR_BODY_I:%.*]] ]
Expand All @@ -225,8 +226,7 @@ define float @gep_cross_loop(i64* %_arg_, float* %_arg_3, float %_arg_8)
; CHECK: for.cond.i.i.i.preheader:
; CHECK-NEXT: ret float [[SUM]]
; CHECK: for.body.i:
; CHECK-NEXT: [[ARRAYIDX_I84_I_IDX:%.*]] = add nsw i64 [[IDX]], [[TMP0]]
; CHECK-NEXT: [[ARRAYIDX_I84_I:%.*]] = getelementptr inbounds float, float* [[_ARG_3:%.*]], i64 [[ARRAYIDX_I84_I_IDX]]
; CHECK-NEXT: [[ARRAYIDX_I84_I:%.*]] = getelementptr inbounds float, float* [[ADD_PTR]], i64 [[IDX]]
; CHECK-NEXT: [[TMP1:%.*]] = load float, float* [[ARRAYIDX_I84_I]], align 4
; CHECK-NEXT: [[ADD_I]] = fadd fast float [[SUM]], [[TMP1]]
; CHECK-NEXT: [[ADD11_I]] = add nuw nsw i64 [[IDX]], 1
Expand Down

0 comments on commit 9cae598

Please sign in to comment.