[PATCH] D107935: [InstCombine] Avoid folding GEPs across loop boundaries
Chang Lin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 11 15:44:38 PDT 2021
clin1 created this revision.
clin1 added a reviewer: RKSimon.
Herald added a subscriber: hiraditya.
clin1 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D107935
Files:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll
Index: llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll
===================================================================
--- llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll
+++ llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll
@@ -186,3 +186,29 @@
call void @blackhole(<2 x i8*> %e6)
br label %loop
}
+
+; Avoid folding the GEP outside the loop to inside, and increasing loop
+; instruction count.
+define float @gep_cross_loop(i64* %_arg_, float* %_arg_3, float %_arg_8)
+{
+entry:
+ %0 = load i64, i64* %_arg_, align 8
+ %add.ptr = getelementptr inbounds float, float* %_arg_3, i64 %0
+ br label %for.cond.i
+
+for.cond.i: ; preds = %for.body.i, %entry
+ %idx = phi i64 [ 0, %entry ], [ %add11.i, %for.body.i ]
+ %sum = phi float [ 0.000000e+00, %entry ], [ %add.i, %for.body.i ]
+ %cmp = icmp ule i64 %idx, 16
+ br i1 %cmp, label %for.body.i, label %for.cond.i.i.i.preheader
+
+for.cond.i.i.i.preheader: ; preds = %for.cond.i
+ ret float %sum
+
+for.body.i: ; preds = %for.cond.i
+ %arrayidx.i84.i = getelementptr inbounds float, float * %add.ptr, i64 %idx
+ %1 = load float, float* %arrayidx.i84.i, align 4
+ %add.i = fadd fast float %sum, %1
+ %add11.i = add nsw i64 %idx, 1
+ br label %for.cond.i
+}
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2130,8 +2130,17 @@
}
}
+ // 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",
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107935.365867.patch
Type: text/x-patch
Size: 2424 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210811/91b3895b/attachment.bin>
More information about the llvm-commits
mailing list