llvm.org GIT mirror llvm / 65766ca
[IndVarSimplify] Simplify code using preheader assumption As noted in the nice block comment, the previous code didn't actually handle multi-entry loops correctly, it just assumed SCEV didn't analyze such loops. Given SCEV has comments to the contrary, that seems a bit suspect. More importantly, the pass actually requires loopsimplify form which ensures a loop-preheader is available. Remove the excessive generaility and shorten the code greatly. Note that we do successfully analyze many multi-entry loops, but we do so by converting them to single entry loops. See the added test case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316976 91177308-0d34-0410-b5e6-96231b3b80d8 Philip Reames 1 year, 11 months ago
2 changed file(s) with 28 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
181181 ICmpInst::Predicate InvariantPredicate;
182182 const SCEV *InvariantLHS, *InvariantRHS;
183183
184 if (!isa(IVOperand))
184 auto *PN = dyn_cast(IVOperand);
185 if (!PN)
185186 return false;
186187 if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
187188 InvariantLHS, InvariantRHS))
201202 NewRHS =
202203 ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx));
203204
204 auto *PN = cast(IVOperand);
205 for (unsigned i = 0, e = PN->getNumIncomingValues();
206 i != e && (!NewLHS || !NewRHS);
207 ++i) {
208
209 // If this is a value incoming from the backedge, then it cannot be a loop
210 // invariant value (since we know that IVOperand is an induction variable).
211 if (L->contains(PN->getIncomingBlock(i)))
212 continue;
213
214 // NB! This following assert does not fundamentally have to be true, but
215 // it is true today given how SCEV analyzes induction variables.
216 // Specifically, today SCEV will *not* recognize %iv as an induction
217 // variable in the following case:
218 //
219 // define void @f(i32 %k) {
220 // entry:
221 // br i1 undef, label %r, label %l
222 //
223 // l:
224 // %k.inc.l = add i32 %k, 1
225 // br label %loop
226 //
227 // r:
228 // %k.inc.r = add i32 %k, 1
229 // br label %loop
230 //
231 // loop:
232 // %iv = phi i32 [ %k.inc.l, %l ], [ %k.inc.r, %r ], [ %iv.inc, %loop ]
233 // %iv.inc = add i32 %iv, 1
234 // br label %loop
235 // }
236 //
237 // but if it starts to, at some point, then the assertion below will have
238 // to be changed to a runtime check.
239
240 Value *Incoming = PN->getIncomingValue(i);
241
242 #ifndef NDEBUG
243 if (auto *I = dyn_cast(Incoming))
244 assert(DT->dominates(I, ICmp) && "Should be a unique loop dominating value!");
245 #endif
246
205 // TODO: Support multiple entry loops? (We currently bail out of these in
206 // the IndVarSimplify pass)
207 if (auto *BB = L->getLoopPredecessor()) {
208 Value *Incoming = PN->getIncomingValue(PN->getBasicBlockIndex(BB));
247209 const SCEV *IncomingS = SE->getSCEV(Incoming);
248210
249211 if (!NewLHS && IncomingS == InvariantLHS)
272272 ret void
273273 }
274274
275 ; check to handle loops without preheaders, but invariant operands
276 ; (we handle this today by inserting a preheader)
277 define void @test9(i1 %cnd, i64 %start) {
278 ; CHECK-LABEL: @test9
279 ; CHECK-LABEL: loop.preheader:
280 entry:
281 br i1 %cnd, label %entry1, label %entry2
282 entry1:
283 br label %loop
284 entry2:
285 br label %loop
286 loop:
287 %indvars.iv = phi i64 [ %start, %entry1 ],[ %start, %entry2 ], [ %indvars.iv.next, %loop ]
288 %indvars.iv.next = add nsw i64 %indvars.iv, 1
289 ; CHECK: %cmp1 = icmp slt i64 %start, -1
290 %cmp1 = icmp slt i64 %indvars.iv, -1
291 br i1 %cmp1, label %for.end, label %loop
292
293 for.end: ; preds = %if.end, %entry
294 ret void
295 }
296
275297 !1 = !{i64 -1, i64 100}
276298
277299