llvm.org GIT mirror llvm / b13ba2b
[LOOPINFO] Extend Loop object to add utilities to get the loop bounds, step, induction variable, and guard branch. Summary: This PR extends the loop object with more utilities to get loop bounds, step, induction variable, and guard branch. There already exists passes which try to obtain the loop induction variable in their own pass, e.g. loop interchange. It would be useful to have a common area to get these information. Moreover, loop fusion (https://reviews.llvm.org/D55851) is planning to use getGuard() to extend the kind of loops it is able to fuse, e.g. rotated loop with non-constant upper bound, which would have a loop guard. /// Example: /// for (int i = lb; i < ub; i+=step) /// <loop body> /// --- pseudo LLVMIR --- /// beforeloop: /// guardcmp = (lb < ub) /// if (guardcmp) goto preheader; else goto afterloop /// preheader: /// loop: /// i1 = phi[{lb, preheader}, {i2, latch}] /// <loop body> /// i2 = i1 + step /// latch: /// cmp = (i2 < ub) /// if (cmp) goto loop /// exit: /// afterloop: /// /// getBounds /// getInitialIVValue --> lb /// getStepInst --> i2 = i1 + step /// getStepValue --> step /// getFinalIVValue --> ub /// getCanonicalPredicate --> '<' /// getDirection --> Increasing /// getGuard --> if (guardcmp) goto loop; else goto afterloop /// getInductionVariable --> i1 /// getAuxiliaryInductionVariable --> {i1} /// isCanonical --> false Committed on behalf of @Whitney (Whitney Tsang). Reviewers: kbarton, hfinkel, dmgreen, Meinersbur, jdoerfert, syzaara, fhahn Reviewed By: kbarton Subscribers: tvvikram, bmahjour, etiotto, fhahn, jsji, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D60565 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361517 91177308-0d34-0410-b5e6-96231b3b80d8 Kit Barton 2 months ago
4 changed file(s) with 1309 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
5353 class DominatorTree;
5454 class LoopInfo;
5555 class Loop;
56 class InductionDescriptor;
5657 class MDNode;
5758 class MemorySSAUpdater;
5859 class PHINode;
60 class PostDominatorTree;
61 class ScalarEvolution;
5962 class raw_ostream;
6063 template class DominatorTreeBase;
6164 template class LoopInfoBase;
527530 /// non-unique or the loop is dead; otherwise, return true.
528531 bool getIncomingAndBackEdge(BasicBlock *&Incoming,
529532 BasicBlock *&Backedge) const;
533
534 /// Below are some utilities to get loop bounds and induction variable, and
535 /// check if a given phinode is an auxiliary induction variable, as well as
536 /// checking if the loop is canonical.
537 ///
538 /// Here is an example:
539 /// \code
540 /// for (int i = lb; i < ub; i+=step)
541 ///
542 /// --- pseudo LLVMIR ---
543 /// beforeloop:
544 /// guardcmp = (lb < ub)
545 /// if (guardcmp) goto preheader; else goto afterloop
546 /// preheader:
547 /// loop:
548 /// i_1 = phi[{lb, preheader}, {i_2, latch}]
549 ///
550 /// i_2 = i_1 + step
551 /// latch:
552 /// cmp = (i_2 < ub)
553 /// if (cmp) goto loop
554 /// exit:
555 /// afterloop:
556 /// \endcode
557 ///
558 /// - getBounds
559 /// - getInitialIVValue --> lb
560 /// - getStepInst --> i_2 = i_1 + step
561 /// - getStepValue --> step
562 /// - getFinalIVValue --> ub
563 /// - getCanonicalPredicate --> '<'
564 /// - getDirection --> Increasing
565 ///
566 /// - getInductionVariable --> i_1
567 /// - isAuxiliaryInductionVariable(x) --> true if x == i_1
568 /// - isCanonical --> false
569 struct LoopBounds {
570 /// Return the LoopBounds object if
571 /// - the given \p IndVar is an induction variable
572 /// - the initial value of the induction variable can be found
573 /// - the step instruction of the induction variable can be found
574 /// - the final value of the induction variable can be found
575 ///
576 /// Else None.
577 static Optional getBounds(const Loop &L, PHINode &IndVar,
578 ScalarEvolution &SE);
579
580 /// Get the initial value of the loop induction variable.
581 Value &getInitialIVValue() const { return InitialIVValue; }
582
583 /// Get the instruction that updates the loop induction variable.
584 Instruction &getStepInst() const { return StepInst; }
585
586 /// Get the step that the loop induction variable gets updated by in each
587 /// loop iteration. Return nullptr if not found.
588 Value *getStepValue() const { return StepValue; }
589
590 /// Get the final value of the loop induction variable.
591 Value &getFinalIVValue() const { return FinalIVValue; }
592
593 /// Return the canonical predicate for the latch compare instruction, if
594 /// able to be calcuated. Else BAD_ICMP_PREDICATE.
595 ///
596 /// A predicate is considered as canonical if requirements below are all
597 /// satisfied:
598 /// 1. The first successor of the latch branch is the loop header
599 /// If not, inverse the predicate.
600 /// 2. One of the operands of the latch comparison is StepInst
601 /// If not, and
602 /// - if the current calcuated predicate is not ne or eq, flip the
603 /// predicate.
604 /// - else if the loop is increasing, return slt
605 /// (notice that it is safe to change from ne or eq to sign compare)
606 /// - else if the loop is decreasing, return sgt
607 /// (notice that it is safe to change from ne or eq to sign compare)
608 ///
609 /// Here is an example when both (1) and (2) are not satisfied:
610 /// \code
611 /// loop.header:
612 /// %iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
613 /// %inc = add %iv, %step
614 /// %cmp = slt %iv, %finaliv
615 /// br %cmp, %loop.exit, %loop.header
616 /// loop.exit:
617 /// \endcode
618 /// - The second successor of the latch branch is the loop header instead
619 /// of the first successor (slt -> sge)
620 /// - The first operand of the latch comparison (%cmp) is the IndVar (%iv)
621 /// instead of the StepInst (%inc) (sge -> sgt)
622 ///
623 /// The predicate would be sgt if both (1) and (2) are satisfied.
624 /// getCanonicalPredicate() returns sgt for this example.
625 /// Note: The IR is not changed.
626 ICmpInst::Predicate getCanonicalPredicate() const;
627
628 /// An enum for the direction of the loop
629 /// - for (int i = 0; i < ub; ++i) --> Increasing
630 /// - for (int i = ub; i > 0; --i) --> Descresing
631 /// - for (int i = x; i != y; i+=z) --> Unknown
632 enum class Direction { Increasing, Decreasing, Unknown };
633
634 /// Get the direction of the loop.
635 Direction getDirection() const;
636
637 private:
638 LoopBounds(const Loop &Loop, Value &I, Instruction &SI, Value *SV, Value &F,
639 ScalarEvolution &SE)
640 : L(Loop), InitialIVValue(I), StepInst(SI), StepValue(SV),
641 FinalIVValue(F), SE(SE) {}
642
643 const Loop &L;
644
645 // The initial value of the loop induction variable
646 Value &InitialIVValue;
647
648 // The instruction that updates the loop induction variable
649 Instruction &StepInst;
650
651 // The value that the loop induction variable gets updated by in each loop
652 // iteration
653 Value *StepValue;
654
655 // The final value of the loop induction variable
656 Value &FinalIVValue;
657
658 ScalarEvolution &SE;
659 };
660
661 /// Return the struct LoopBounds collected if all struct members are found,
662 /// else None.
663 Optional getBounds(ScalarEvolution &SE) const;
664
665 /// Return the loop induction variable if found, else return nullptr.
666 /// An instruction is considered as the loop induction variable if
667 /// - it is an induction variable of the loop; and
668 /// - it is used to determine the condition of the branch in the loop latch
669 ///
670 /// Note: the induction variable doesn't need to be canonical, i.e. starts at
671 /// zero and increments by one each time through the loop (but it can be).
672 PHINode *getInductionVariable(ScalarEvolution &SE) const;
673
674 /// Get the loop induction descriptor for the loop induction variable. Return
675 /// true if the loop induction variable is found.
676 bool getInductionDescriptor(ScalarEvolution &SE,
677 InductionDescriptor &IndDesc) const;
678
679 /// Return true if the given PHINode \p AuxIndVar is
680 /// - in the loop header
681 /// - not used outside of the loop
682 /// - incremented by a loop invariant step for each loop iteration
683 /// - step instruction opcode should be add or sub
684 /// Note: auxiliary induction variable is not required to be used in the
685 /// conditional branch in the loop latch. (but it can be)
686 bool isAuxiliaryInductionVariable(PHINode &AuxIndVar,
687 ScalarEvolution &SE) const;
688
689 /// Return true if the loop induction variable starts at zero and increments
690 /// by one each time through the loop.
691 bool isCanonical(ScalarEvolution &SE) const;
530692
531693 /// Return true if the Loop is in LCSSA form.
532694 bool isLCSSAForm(DominatorTree &DT) const;
1616 #include "llvm/ADT/DepthFirstIterator.h"
1717 #include "llvm/ADT/ScopeExit.h"
1818 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Analysis/IVDescriptors.h"
1920 #include "llvm/Analysis/LoopInfoImpl.h"
2021 #include "llvm/Analysis/LoopIterator.h"
2122 #include "llvm/Analysis/MemorySSA.h"
2223 #include "llvm/Analysis/MemorySSAUpdater.h"
24 #include "llvm/Analysis/PostDominators.h"
25 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2326 #include "llvm/Analysis/ValueTracking.h"
2427 #include "llvm/Config/llvm-config.h"
2528 #include "llvm/IR/CFG.h"
163166 return nullptr;
164167 }
165168
169 /// Return true if V1 and V2 have the same value ignoring bit width.
170 static bool isEqualIgnoreBitwidth(Value &V1, Value &V2, ScalarEvolution &SE) {
171 const SCEV *S1 = SE.getSCEV(&V1);
172 const SCEV *S2 = SE.getSCEV(&V2);
173 Type *WiderType = SE.getWiderType(S1->getType(), S2->getType());
174 S1 = SE.getNoopOrAnyExtend(S1, WiderType);
175 S2 = SE.getNoopOrAnyExtend(S2, WiderType);
176 return SE.getMinusSCEV(S1, S2)->isZero();
177 }
178
179 /// Get the latch condition instruction.
180 static ICmpInst *getLatchCmpInst(const Loop &L) {
181 if (BasicBlock *Latch = L.getLoopLatch())
182 if (BranchInst *BI = dyn_cast_or_null(Latch->getTerminator()))
183 if (BI->isConditional())
184 return dyn_cast(BI->getCondition());
185
186 return nullptr;
187 }
188
189 /// Return the final value of the loop induction variable if found.
190 static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
191 const Instruction &StepInst) {
192 ICmpInst *LatchCmpInst = getLatchCmpInst(L);
193 if (!LatchCmpInst)
194 return nullptr;
195
196 Value *Op0 = LatchCmpInst->getOperand(0);
197 Value *Op1 = LatchCmpInst->getOperand(1);
198 if (Op0 == &IndVar || Op0 == &StepInst)
199 return Op1;
200
201 if (Op1 == &IndVar || Op1 == &StepInst)
202 return Op0;
203
204 return nullptr;
205 }
206
207 Optional Loop::LoopBounds::getBounds(const Loop &L,
208 PHINode &IndVar,
209 ScalarEvolution &SE) {
210 InductionDescriptor IndDesc;
211 if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc))
212 return None;
213
214 Value *InitialIVValue = IndDesc.getStartValue();
215 Instruction *StepInst = IndDesc.getInductionBinOp();
216 if (!InitialIVValue || !StepInst)
217 return None;
218
219 const SCEV *Step = IndDesc.getStep();
220 Value *StepInstOp1 = StepInst->getOperand(1);
221 Value *StepInstOp0 = StepInst->getOperand(0);
222 Value *StepValue = nullptr;
223 if (SE.getSCEV(StepInstOp1) == Step)
224 StepValue = StepInstOp1;
225 else if (SE.getSCEV(StepInstOp0) == Step)
226 StepValue = StepInstOp0;
227
228 Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst);
229 if (!FinalIVValue)
230 return None;
231
232 return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue,
233 SE);
234 }
235
236 using Direction = Loop::LoopBounds::Direction;
237
238 ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const {
239 BasicBlock *Latch = L.getLoopLatch();
240 assert(Latch && "Expecting valid latch");
241
242 BranchInst *BI = dyn_cast_or_null(Latch->getTerminator());
243 assert(BI && BI->isConditional() && "Expecting conditional latch branch");
244
245 ICmpInst *LatchCmpInst = dyn_cast(BI->getCondition());
246 assert(LatchCmpInst &&
247 "Expecting the latch compare instruction to be a CmpInst");
248
249 // Need to inverse the predicate when first successor is not the loop
250 // header
251 ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader())
252 ? LatchCmpInst->getPredicate()
253 : LatchCmpInst->getInversePredicate();
254
255 if (LatchCmpInst->getOperand(0) == &getFinalIVValue())
256 Pred = ICmpInst::getSwappedPredicate(Pred);
257
258 // Need to flip strictness of the predicate when the latch compare instruction
259 // is not using StepInst
260 if (LatchCmpInst->getOperand(0) == &getStepInst() ||
261 LatchCmpInst->getOperand(1) == &getStepInst())
262 return Pred;
263
264 // Cannot flip strictness of NE and EQ
265 if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
266 return ICmpInst::getFlippedStrictnessPredicate(Pred);
267
268 Direction D = getDirection();
269 if (D == Direction::Increasing)
270 return ICmpInst::ICMP_SLT;
271
272 if (D == Direction::Decreasing)
273 return ICmpInst::ICMP_SGT;
274
275 // If cannot determine the direction, then unable to find the canonical
276 // predicate
277 return ICmpInst::BAD_ICMP_PREDICATE;
278 }
279
280 Direction Loop::LoopBounds::getDirection() const {
281 if (const SCEVAddRecExpr *StepAddRecExpr =
282 dyn_cast(SE.getSCEV(&getStepInst())))
283 if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) {
284 if (SE.isKnownPositive(StepRecur))
285 return Direction::Increasing;
286 if (SE.isKnownNegative(StepRecur))
287 return Direction::Decreasing;
288 }
289
290 return Direction::Unknown;
291 }
292
293 Optional Loop::getBounds(ScalarEvolution &SE) const {
294 if (PHINode *IndVar = getInductionVariable(SE))
295 return LoopBounds::getBounds(*this, *IndVar, SE);
296
297 return None;
298 }
299
300 PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
301 if (!isLoopSimplifyForm())
302 return nullptr;
303
304 BasicBlock *Header = getHeader();
305 assert(Header && "Expected a valid loop header");
306 BasicBlock *Latch = getLoopLatch();
307 assert(Latch && "Expected a valid loop latch");
308 ICmpInst *CmpInst = getLatchCmpInst(*this);
309 if (!CmpInst)
310 return nullptr;
311
312 // case 1:
313 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
314 // StepInst = IndVar + step
315 // cmp = StepInst < FinalValue
316 Instruction *LatchCmpOp0 = dyn_cast(CmpInst->getOperand(0));
317 Instruction *LatchCmpOp1 = dyn_cast(CmpInst->getOperand(1));
318 // Loop over all of the PHI nodes in loop header, store the PHI node that has
319 // incoming value from latch equals to the StepInst
320 BinaryOperator *StepInst = nullptr;
321 PHINode *IndVar = nullptr;
322 for (PHINode &PN : Header->phis()) {
323 Value *IncomingValue = PN.getIncomingValueForBlock(Latch);
324 assert(IncomingValue && "Expecting valid incoming value from latch");
325 if (IncomingValue == LatchCmpOp0 || IncomingValue == LatchCmpOp1) {
326 IndVar = &PN;
327 StepInst = dyn_cast(IncomingValue);
328 if (StepInst)
329 if (isEqualIgnoreBitwidth(*StepInst->getOperand(0), *IndVar, SE) ||
330 isEqualIgnoreBitwidth(*StepInst->getOperand(1), *IndVar, SE))
331 return IndVar;
332 }
333 }
334
335 // case 2:
336 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
337 // StepInst = IndVar + step
338 // cmp = IndVar < FinalValue
339 for (Value *Op : CmpInst->operands()) {
340 PHINode *IndVar = dyn_cast(Op);
341 if (!IndVar)
342 continue;
343
344 if (IndVar->getParent() != Header)
345 continue;
346
347 Value *IncomingValue = IndVar->getIncomingValueForBlock(Latch);
348 assert(IncomingValue && "Expecting valid incoming value from latch");
349 StepInst = dyn_cast(IncomingValue);
350 if (StepInst)
351 if (StepInst->getOperand(0) == IndVar ||
352 StepInst->getOperand(1) == IndVar)
353 return IndVar;
354 }
355
356 return nullptr;
357 }
358
359 bool Loop::getInductionDescriptor(ScalarEvolution &SE,
360 InductionDescriptor &IndDesc) const {
361 if (PHINode *IndVar = getInductionVariable(SE))
362 return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
363
364 return false;
365 }
366
367 bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar,
368 ScalarEvolution &SE) const {
369 // Located in the loop header
370 BasicBlock *Header = getHeader();
371 if (AuxIndVar.getParent() != Header)
372 return false;
373
374 // No uses outside of the loop
375 for (User *U : AuxIndVar.users())
376 if (const Instruction *I = dyn_cast(U))
377 if (!contains(I))
378 return false;
379
380 InductionDescriptor IndDesc;
381 if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc))
382 return false;
383
384 // The step instruction opcode should be add or sub.
385 if (IndDesc.getInductionOpcode() != Instruction::Add &&
386 IndDesc.getInductionOpcode() != Instruction::Sub)
387 return false;
388
389 // Incremented by a loop invariant step for each loop iteration
390 return SE.isLoopInvariant(IndDesc.getStep(), this);
391 }
392
393 bool Loop::isCanonical(ScalarEvolution &SE) const {
394 InductionDescriptor IndDesc;
395 if (!getInductionDescriptor(SE, IndDesc))
396 return false;
397
398 ConstantInt *Init = dyn_cast_or_null(IndDesc.getStartValue());
399 if (!Init || !Init->isZero())
400 return false;
401
402 if (IndDesc.getInductionOpcode() != Instruction::Add)
403 return false;
404
405 ConstantInt *Step = IndDesc.getConstIntStepValue();
406 if (!Step || !Step->isOne())
407 return false;
408
409 return true;
410 }
411
166412 // Check that 'BB' doesn't have any uses outside of the 'L'
167413 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
168414 DominatorTree &DT) {
291291 return LoopList;
292292 }
293293
294 static PHINode *getInductionVariable(Loop *L, ScalarEvolution *SE) {
295 PHINode *InnerIndexVar = L->getCanonicalInductionVariable();
296 if (InnerIndexVar)
297 return InnerIndexVar;
298 if (L->getLoopLatch() == nullptr || L->getLoopPredecessor() == nullptr)
299 return nullptr;
300 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ++I) {
301 PHINode *PhiVar = cast(I);
302 Type *PhiTy = PhiVar->getType();
303 if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
304 !PhiTy->isPointerTy())
305 return nullptr;
306 const SCEVAddRecExpr *AddRec =
307 dyn_cast(SE->getSCEV(PhiVar));
308 if (!AddRec || !AddRec->isAffine())
309 continue;
310 const SCEV *Step = AddRec->getStepRecurrence(*SE);
311 if (!isa(Step))
312 continue;
313 // Found the induction variable.
314 // FIXME: Handle loops with more than one induction variable. Note that,
315 // currently, legality makes sure we have only one induction variable.
316 return PhiVar;
317 }
318 return nullptr;
319 }
320
321294 namespace {
322295
323296 /// LoopInterchangeLegality checks if it is legal to interchange the loop.
12261199 if (InnerLoop->getSubLoops().empty()) {
12271200 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
12281201 LLVM_DEBUG(dbgs() << "Calling Split Inner Loop\n");
1229 PHINode *InductionPHI = getInductionVariable(InnerLoop, SE);
1202 PHINode *InductionPHI = InnerLoop->getInductionVariable(*SE);
12301203 if (!InductionPHI) {
12311204 LLVM_DEBUG(dbgs() << "Failed to find the point to split loop latch \n");
12321205 return false;
66 //===----------------------------------------------------------------------===//
77
88 #include "llvm/Analysis/LoopInfo.h"
9 #include "llvm/Analysis/AssumptionCache.h"
10 #include "llvm/Analysis/PostDominators.h"
11 #include "llvm/Analysis/ScalarEvolution.h"
12 #include "llvm/Analysis/TargetLibraryInfo.h"
913 #include "llvm/AsmParser/Parser.h"
1014 #include "llvm/IR/Dominators.h"
1115 #include "llvm/Support/SourceMgr.h"
2327 DominatorTree DT(*F);
2428 LoopInfo LI(DT);
2529 Test(*F, LI);
30 }
31
32 /// Build the loop info and scalar evolution for the function and run the Test.
33 static void runWithLoopInfoPlus(
34 Module &M, StringRef FuncName,
35 function_ref
36 PostDominatorTree &PDT)>
37 Test) {
38 auto *F = M.getFunction(FuncName);
39 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
40
41 TargetLibraryInfoImpl TLII;
42 TargetLibraryInfo TLI(TLII);
43 AssumptionCache AC(*F);
44 DominatorTree DT(*F);
45 LoopInfo LI(DT);
46 ScalarEvolution SE(*F, TLI, AC, DT, LI);
47
48 PostDominatorTree PDT(*F);
49 Test(*F, LI, SE, PDT);
2650 }
2751
2852 static std::unique_ptr makeLLVMModule(LLVMContext &Context,
209233 EXPECT_EQ(&L_0_1, ReverseSiblingPreorder[6]);
210234 EXPECT_EQ(&L_0_0, ReverseSiblingPreorder[7]);
211235 }
236
237 TEST(LoopInfoTest, CanonicalLoop) {
238 const char *ModuleStr =
239 "define void @foo(i32* %A, i32 %ub) {\n"
240 "entry:\n"
241 " %guardcmp = icmp slt i32 0, %ub\n"
242 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
243 "for.preheader:\n"
244 " br label %for.body\n"
245 "for.body:\n"
246 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
247 " %idxprom = sext i32 %i to i64\n"
248 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
249 " store i32 %i, i32* %arrayidx, align 4\n"
250 " %inc = add nsw i32 %i, 1\n"
251 " %cmp = icmp slt i32 %inc, %ub\n"
252 " br i1 %cmp, label %for.body, label %for.exit\n"
253 "for.exit:\n"
254 " br label %for.end\n"
255 "for.end:\n"
256 " ret void\n"
257 "}\n";
258
259 // Parse the module.
260 LLVMContext Context;
261 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
262
263 runWithLoopInfoPlus(
264 *M, "foo",
265 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
266 PostDominatorTree &PDT) {
267 Function::iterator FI = F.begin();
268 // First two basic block are entry and for.preheader - skip them.
269 ++FI;
270 BasicBlock *Header = &*(++FI);
271 assert(Header->getName() == "for.body");
272 Loop *L = LI.getLoopFor(Header);
273 EXPECT_NE(L, nullptr);
274
275 Optional Bounds = L->getBounds(SE);
276 EXPECT_NE(Bounds, None);
277 ConstantInt *InitialIVValue =
278 dyn_cast(&Bounds->getInitialIVValue());
279 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
280 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
281 ConstantInt *StepValue =
282 dyn_cast_or_null(Bounds->getStepValue());
283 EXPECT_TRUE(StepValue && StepValue->isOne());
284 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
285 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
286 EXPECT_EQ(Bounds->getDirection(),
287 Loop::LoopBounds::Direction::Increasing);
288 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
289 });
290 }
291
292 TEST(LoopInfoTest, LoopWithInverseGuardSuccs) {
293 const char *ModuleStr =
294 "define void @foo(i32* %A, i32 %ub) {\n"
295 "entry:\n"
296 " %guardcmp = icmp sge i32 0, %ub\n"
297 " br i1 %guardcmp, label %for.end, label %for.preheader\n"
298 "for.preheader:\n"
299 " br label %for.body\n"
300 "for.body:\n"
301 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
302 " %idxprom = sext i32 %i to i64\n"
303 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
304 " store i32 %i, i32* %arrayidx, align 4\n"
305 " %inc = add nsw i32 %i, 1\n"
306 " %cmp = icmp slt i32 %inc, %ub\n"
307 " br i1 %cmp, label %for.body, label %for.exit\n"
308 "for.exit:\n"
309 " br label %for.end\n"
310 "for.end:\n"
311 " ret void\n"
312 "}\n";
313
314 // Parse the module.
315 LLVMContext Context;
316 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
317
318 runWithLoopInfoPlus(
319 *M, "foo",
320 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
321 PostDominatorTree &PDT) {
322 Function::iterator FI = F.begin();
323 // First two basic block are entry and for.preheader - skip them.
324 ++FI;
325 BasicBlock *Header = &*(++FI);
326 assert(Header->getName() == "for.body");
327 Loop *L = LI.getLoopFor(Header);
328 EXPECT_NE(L, nullptr);
329
330 Optional Bounds = L->getBounds(SE);
331 EXPECT_NE(Bounds, None);
332 ConstantInt *InitialIVValue =
333 dyn_cast(&Bounds->getInitialIVValue());
334 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
335 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
336 ConstantInt *StepValue =
337 dyn_cast_or_null(Bounds->getStepValue());
338 EXPECT_TRUE(StepValue && StepValue->isOne());
339 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
340 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
341 EXPECT_EQ(Bounds->getDirection(),
342 Loop::LoopBounds::Direction::Increasing);
343 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
344 });
345 }
346
347 TEST(LoopInfoTest, LoopWithSwappedGuardCmp) {
348 const char *ModuleStr =
349 "define void @foo(i32* %A, i32 %ub) {\n"
350 "entry:\n"
351 " %guardcmp = icmp sgt i32 %ub, 0\n"
352 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
353 "for.preheader:\n"
354 " br label %for.body\n"
355 "for.body:\n"
356 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
357 " %idxprom = sext i32 %i to i64\n"
358 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
359 " store i32 %i, i32* %arrayidx, align 4\n"
360 " %inc = add nsw i32 %i, 1\n"
361 " %cmp = icmp sge i32 %inc, %ub\n"
362 " br i1 %cmp, label %for.exit, label %for.body\n"
363 "for.exit:\n"
364 " br label %for.end\n"
365 "for.end:\n"
366 " ret void\n"
367 "}\n";
368
369 // Parse the module.
370 LLVMContext Context;
371 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
372
373 runWithLoopInfoPlus(
374 *M, "foo",
375 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
376 PostDominatorTree &PDT) {
377 Function::iterator FI = F.begin();
378 // First two basic block are entry and for.preheader - skip them.
379 ++FI;
380 BasicBlock *Header = &*(++FI);
381 assert(Header->getName() == "for.body");
382 Loop *L = LI.getLoopFor(Header);
383 EXPECT_NE(L, nullptr);
384
385 Optional Bounds = L->getBounds(SE);
386 EXPECT_NE(Bounds, None);
387 ConstantInt *InitialIVValue =
388 dyn_cast(&Bounds->getInitialIVValue());
389 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
390 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
391 ConstantInt *StepValue =
392 dyn_cast_or_null(Bounds->getStepValue());
393 EXPECT_TRUE(StepValue && StepValue->isOne());
394 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
395 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
396 EXPECT_EQ(Bounds->getDirection(),
397 Loop::LoopBounds::Direction::Increasing);
398 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
399 });
400 }
401
402 TEST(LoopInfoTest, LoopWithInverseLatchSuccs) {
403 const char *ModuleStr =
404 "define void @foo(i32* %A, i32 %ub) {\n"
405 "entry:\n"
406 " %guardcmp = icmp slt i32 0, %ub\n"
407 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
408 "for.preheader:\n"
409 " br label %for.body\n"
410 "for.body:\n"
411 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
412 " %idxprom = sext i32 %i to i64\n"
413 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
414 " store i32 %i, i32* %arrayidx, align 4\n"
415 " %inc = add nsw i32 %i, 1\n"
416 " %cmp = icmp sge i32 %inc, %ub\n"
417 " br i1 %cmp, label %for.exit, label %for.body\n"
418 "for.exit:\n"
419 " br label %for.end\n"
420 "for.end:\n"
421 " ret void\n"
422 "}\n";
423
424 // Parse the module.
425 LLVMContext Context;
426 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
427
428 runWithLoopInfoPlus(
429 *M, "foo",
430 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
431 PostDominatorTree &PDT) {
432 Function::iterator FI = F.begin();
433 // First two basic block are entry and for.preheader - skip them.
434 ++FI;
435 BasicBlock *Header = &*(++FI);
436 assert(Header->getName() == "for.body");
437 Loop *L = LI.getLoopFor(Header);
438 EXPECT_NE(L, nullptr);
439
440 Optional Bounds = L->getBounds(SE);
441 EXPECT_NE(Bounds, None);
442 ConstantInt *InitialIVValue =
443 dyn_cast(&Bounds->getInitialIVValue());
444 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
445 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
446 ConstantInt *StepValue =
447 dyn_cast_or_null(Bounds->getStepValue());
448 EXPECT_TRUE(StepValue && StepValue->isOne());
449 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
450 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
451 EXPECT_EQ(Bounds->getDirection(),
452 Loop::LoopBounds::Direction::Increasing);
453 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
454 });
455 }
456
457 TEST(LoopInfoTest, LoopWithLatchCmpNE) {
458 const char *ModuleStr =
459 "define void @foo(i32* %A, i32 %ub) {\n"
460 "entry:\n"
461 " %guardcmp = icmp slt i32 0, %ub\n"
462 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
463 "for.preheader:\n"
464 " br label %for.body\n"
465 "for.body:\n"
466 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
467 " %idxprom = sext i32 %i to i64\n"
468 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
469 " store i32 %i, i32* %arrayidx, align 4\n"
470 " %inc = add nsw i32 %i, 1\n"
471 " %cmp = icmp ne i32 %i, %ub\n"
472 " br i1 %cmp, label %for.body, label %for.exit\n"
473 "for.exit:\n"
474 " br label %for.end\n"
475 "for.end:\n"
476 " ret void\n"
477 "}\n";
478
479 // Parse the module.
480 LLVMContext Context;
481 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
482
483 runWithLoopInfoPlus(
484 *M, "foo",
485 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
486 PostDominatorTree &PDT) {
487 Function::iterator FI = F.begin();
488 // First two basic block are entry and for.preheader - skip them.
489 ++FI;
490 BasicBlock *Header = &*(++FI);
491 assert(Header->getName() == "for.body");
492 Loop *L = LI.getLoopFor(Header);
493 EXPECT_NE(L, nullptr);
494
495 Optional Bounds = L->getBounds(SE);
496 EXPECT_NE(Bounds, None);
497 ConstantInt *InitialIVValue =
498 dyn_cast(&Bounds->getInitialIVValue());
499 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
500 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
501 ConstantInt *StepValue =
502 dyn_cast_or_null(Bounds->getStepValue());
503 EXPECT_TRUE(StepValue && StepValue->isOne());
504 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
505 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
506 EXPECT_EQ(Bounds->getDirection(),
507 Loop::LoopBounds::Direction::Increasing);
508 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
509 });
510 }
511
512 TEST(LoopInfoTest, LoopWithGuardCmpSLE) {
513 const char *ModuleStr =
514 "define void @foo(i32* %A, i32 %ub) {\n"
515 "entry:\n"
516 " %ubPlusOne = add i32 %ub, 1\n"
517 " %guardcmp = icmp sle i32 0, %ub\n"
518 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
519 "for.preheader:\n"
520 " br label %for.body\n"
521 "for.body:\n"
522 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
523 " %idxprom = sext i32 %i to i64\n"
524 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
525 " store i32 %i, i32* %arrayidx, align 4\n"
526 " %inc = add nsw i32 %i, 1\n"
527 " %cmp = icmp ne i32 %i, %ubPlusOne\n"
528 " br i1 %cmp, label %for.body, label %for.exit\n"
529 "for.exit:\n"
530 " br label %for.end\n"
531 "for.end:\n"
532 " ret void\n"
533 "}\n";
534
535 // Parse the module.
536 LLVMContext Context;
537 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
538
539 runWithLoopInfoPlus(
540 *M, "foo",
541 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
542 PostDominatorTree &PDT) {
543 Function::iterator FI = F.begin();
544 // First two basic block are entry and for.preheader - skip them.
545 ++FI;
546 BasicBlock *Header = &*(++FI);
547 assert(Header->getName() == "for.body");
548 Loop *L = LI.getLoopFor(Header);
549 EXPECT_NE(L, nullptr);
550
551 Optional Bounds = L->getBounds(SE);
552 EXPECT_NE(Bounds, None);
553 ConstantInt *InitialIVValue =
554 dyn_cast(&Bounds->getInitialIVValue());
555 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
556 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
557 ConstantInt *StepValue =
558 dyn_cast_or_null(Bounds->getStepValue());
559 EXPECT_TRUE(StepValue && StepValue->isOne());
560 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ubPlusOne");
561 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
562 EXPECT_EQ(Bounds->getDirection(),
563 Loop::LoopBounds::Direction::Increasing);
564 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
565 });
566 }
567
568 TEST(LoopInfoTest, LoopNonConstantStep) {
569 const char *ModuleStr =
570 "define void @foo(i32* %A, i32 %ub, i32 %step) {\n"
571 "entry:\n"
572 " %guardcmp = icmp slt i32 0, %ub\n"
573 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
574 "for.preheader:\n"
575 " br label %for.body\n"
576 "for.body:\n"
577 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
578 " %idxprom = zext i32 %i to i64\n"
579 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
580 " store i32 %i, i32* %arrayidx, align 4\n"
581 " %inc = add nsw i32 %i, %step\n"
582 " %cmp = icmp slt i32 %inc, %ub\n"
583 " br i1 %cmp, label %for.body, label %for.exit\n"
584 "for.exit:\n"
585 " br label %for.end\n"
586 "for.end:\n"
587 " ret void\n"
588 "}\n";
589
590 // Parse the module.
591 LLVMContext Context;
592 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
593
594 runWithLoopInfoPlus(
595 *M, "foo",
596 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
597 PostDominatorTree &PDT) {
598 Function::iterator FI = F.begin();
599 // First two basic block are entry and for.preheader - skip them.
600 ++FI;
601 BasicBlock *Header = &*(++FI);
602 assert(Header->getName() == "for.body");
603 Loop *L = LI.getLoopFor(Header);
604 EXPECT_NE(L, nullptr);
605
606 Optional Bounds = L->getBounds(SE);
607 EXPECT_NE(Bounds, None);
608 ConstantInt *InitialIVValue =
609 dyn_cast(&Bounds->getInitialIVValue());
610 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
611 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
612 EXPECT_EQ(Bounds->getStepValue()->getName(), "step");
613 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
614 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
615 EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Unknown);
616 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
617 });
618 }
619
620 TEST(LoopInfoTest, LoopUnsignedBounds) {
621 const char *ModuleStr =
622 "define void @foo(i32* %A, i32 %ub) {\n"
623 "entry:\n"
624 " %guardcmp = icmp ult i32 0, %ub\n"
625 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
626 "for.preheader:\n"
627 " br label %for.body\n"
628 "for.body:\n"
629 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
630 " %idxprom = zext i32 %i to i64\n"
631 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
632 " store i32 %i, i32* %arrayidx, align 4\n"
633 " %inc = add i32 %i, 1\n"
634 " %cmp = icmp ult i32 %inc, %ub\n"
635 " br i1 %cmp, label %for.body, label %for.exit\n"
636 "for.exit:\n"
637 " br label %for.end\n"
638 "for.end:\n"
639 " ret void\n"
640 "}\n";
641
642 // Parse the module.
643 LLVMContext Context;
644 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
645
646 runWithLoopInfoPlus(
647 *M, "foo",
648 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
649 PostDominatorTree &PDT) {
650 Function::iterator FI = F.begin();
651 // First two basic block are entry and for.preheader - skip them.
652 ++FI;
653 BasicBlock *Header = &*(++FI);
654 assert(Header->getName() == "for.body");
655 Loop *L = LI.getLoopFor(Header);
656 EXPECT_NE(L, nullptr);
657
658 Optional Bounds = L->getBounds(SE);
659 EXPECT_NE(Bounds, None);
660 ConstantInt *InitialIVValue =
661 dyn_cast(&Bounds->getInitialIVValue());
662 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
663 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
664 ConstantInt *StepValue =
665 dyn_cast_or_null(Bounds->getStepValue());
666 EXPECT_TRUE(StepValue && StepValue->isOne());
667 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
668 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_ULT);
669 EXPECT_EQ(Bounds->getDirection(),
670 Loop::LoopBounds::Direction::Increasing);
671 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
672 });
673 }
674
675 TEST(LoopInfoTest, DecreasingLoop) {
676 const char *ModuleStr =
677 "define void @foo(i32* %A, i32 %ub) {\n"
678 "entry:\n"
679 " %guardcmp = icmp slt i32 0, %ub\n"
680 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
681 "for.preheader:\n"
682 " br label %for.body\n"
683 "for.body:\n"
684 " %i = phi i32 [ %ub, %for.preheader ], [ %inc, %for.body ]\n"
685 " %idxprom = sext i32 %i to i64\n"
686 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
687 " store i32 %i, i32* %arrayidx, align 4\n"
688 " %inc = sub nsw i32 %i, 1\n"
689 " %cmp = icmp sgt i32 %inc, 0\n"
690 " br i1 %cmp, label %for.body, label %for.exit\n"
691 "for.exit:\n"
692 " br label %for.end\n"
693 "for.end:\n"
694 " ret void\n"
695 "}\n";
696
697 // Parse the module.
698 LLVMContext Context;
699 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
700
701 runWithLoopInfoPlus(
702 *M, "foo",
703 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
704 PostDominatorTree &PDT) {
705 Function::iterator FI = F.begin();
706 // First two basic block are entry and for.preheader - skip them.
707 ++FI;
708 BasicBlock *Header = &*(++FI);
709 assert(Header->getName() == "for.body");
710 Loop *L = LI.getLoopFor(Header);
711 EXPECT_NE(L, nullptr);
712
713 Optional Bounds = L->getBounds(SE);
714 EXPECT_NE(Bounds, None);
715 EXPECT_EQ(Bounds->getInitialIVValue().getName(), "ub");
716 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
717 ConstantInt *StepValue =
718 dyn_cast_or_null(Bounds->getStepValue());
719 EXPECT_EQ(StepValue, nullptr);
720 ConstantInt *FinalIVValue =
721 dyn_cast(&Bounds->getFinalIVValue());
722 EXPECT_TRUE(FinalIVValue && FinalIVValue->isZero());
723 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SGT);
724 EXPECT_EQ(Bounds->getDirection(),
725 Loop::LoopBounds::Direction::Decreasing);
726 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
727 });
728 }
729
730 TEST(LoopInfoTest, CannotFindDirection) {
731 const char *ModuleStr =
732 "define void @foo(i32* %A, i32 %ub, i32 %step) {\n"
733 "entry:\n"
734 " %guardcmp = icmp slt i32 0, %ub\n"
735 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
736 "for.preheader:\n"
737 " br label %for.body\n"
738 "for.body:\n"
739 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
740 " %idxprom = sext i32 %i to i64\n"
741 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
742 " store i32 %i, i32* %arrayidx, align 4\n"
743 " %inc = add nsw i32 %i, %step\n"
744 " %cmp = icmp ne i32 %i, %ub\n"
745 " br i1 %cmp, label %for.body, label %for.exit\n"
746 "for.exit:\n"
747 " br label %for.end\n"
748 "for.end:\n"
749 " ret void\n"
750 "}\n";
751
752 // Parse the module.
753 LLVMContext Context;
754 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
755
756 runWithLoopInfoPlus(*M, "foo",
757 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
758 PostDominatorTree &PDT) {
759 Function::iterator FI = F.begin();
760 // First two basic block are entry and for.preheader
761 // - skip them.
762 ++FI;
763 BasicBlock *Header = &*(++FI);
764 assert(Header->getName() == "for.body");
765 Loop *L = LI.getLoopFor(Header);
766 EXPECT_NE(L, nullptr);
767
768 Optional Bounds = L->getBounds(SE);
769 EXPECT_NE(Bounds, None);
770 ConstantInt *InitialIVValue =
771 dyn_cast(&Bounds->getInitialIVValue());
772 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
773 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
774 EXPECT_EQ(Bounds->getStepValue()->getName(), "step");
775 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
776 EXPECT_EQ(Bounds->getCanonicalPredicate(),
777 ICmpInst::BAD_ICMP_PREDICATE);
778 EXPECT_EQ(Bounds->getDirection(),
779 Loop::LoopBounds::Direction::Unknown);
780 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
781 });
782 }
783
784 TEST(LoopInfoTest, ZextIndVar) {
785 const char *ModuleStr =
786 "define void @foo(i32* %A, i32 %ub) {\n"
787 "entry:\n"
788 " %guardcmp = icmp slt i32 0, %ub\n"
789 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
790 "for.preheader:\n"
791 " br label %for.body\n"
792 "for.body:\n"
793 " %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %for.body ]\n"
794 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
795 " %idxprom = sext i32 %i to i64\n"
796 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
797 " store i32 %i, i32* %arrayidx, align 4\n"
798 " %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1\n"
799 " %inc = add nsw i32 %i, 1\n"
800 " %wide.trip.count = zext i32 %ub to i64\n"
801 " %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count\n"
802 " br i1 %exitcond, label %for.body, label %for.exit\n"
803 "for.exit:\n"
804 " br label %for.end\n"
805 "for.end:\n"
806 " ret void\n"
807 "}\n";
808
809 // Parse the module.
810 LLVMContext Context;
811 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
812
813 runWithLoopInfoPlus(
814 *M, "foo",
815 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
816 PostDominatorTree &PDT) {
817 Function::iterator FI = F.begin();
818 // First two basic block are entry and for.preheader - skip them.
819 ++FI;
820 BasicBlock *Header = &*(++FI);
821 assert(Header->getName() == "for.body");
822 Loop *L = LI.getLoopFor(Header);
823 EXPECT_NE(L, nullptr);
824
825 Optional Bounds = L->getBounds(SE);
826 EXPECT_NE(Bounds, None);
827 ConstantInt *InitialIVValue =
828 dyn_cast(&Bounds->getInitialIVValue());
829 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
830 EXPECT_EQ(Bounds->getStepInst().getName(), "indvars.iv.next");
831 ConstantInt *StepValue =
832 dyn_cast_or_null(Bounds->getStepValue());
833 EXPECT_TRUE(StepValue && StepValue->isOne());
834 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "wide.trip.count");
835 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_NE);
836 EXPECT_EQ(Bounds->getDirection(),
837 Loop::LoopBounds::Direction::Increasing);
838 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "indvars.iv");
839 });
840 }
841
842 TEST(LoopInfoTest, UnguardedLoop) {
843 const char *ModuleStr =
844 "define void @foo(i32* %A, i32 %ub) {\n"
845 "entry:\n"
846 " br label %for.body\n"
847 "for.body:\n"
848 " %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]\n"
849 " %idxprom = sext i32 %i to i64\n"
850 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
851 " store i32 %i, i32* %arrayidx, align 4\n"
852 " %inc = add nsw i32 %i, 1\n"
853 " %cmp = icmp slt i32 %inc, %ub\n"
854 " br i1 %cmp, label %for.body, label %for.exit\n"
855 "for.exit:\n"
856 " br label %for.end\n"
857 "for.end:\n"
858 " ret void\n"
859 "}\n";
860
861 // Parse the module.
862 LLVMContext Context;
863 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
864
865 runWithLoopInfoPlus(
866 *M, "foo",
867 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
868 PostDominatorTree &PDT) {
869 Function::iterator FI = F.begin();
870 // First basic block is entry - skip it.
871 BasicBlock *Header = &*(++FI);
872 assert(Header->getName() == "for.body");
873 Loop *L = LI.getLoopFor(Header);
874 EXPECT_NE(L, nullptr);
875
876 Optional Bounds = L->getBounds(SE);
877 EXPECT_NE(Bounds, None);
878 ConstantInt *InitialIVValue =
879 dyn_cast(&Bounds->getInitialIVValue());
880 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
881 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
882 ConstantInt *StepValue =
883 dyn_cast_or_null(Bounds->getStepValue());
884 EXPECT_TRUE(StepValue && StepValue->isOne());
885 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
886 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
887 EXPECT_EQ(Bounds->getDirection(),
888 Loop::LoopBounds::Direction::Increasing);
889 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
890 });
891 }
892
893 TEST(LoopInfoTest, UnguardedLoopWithControlFlow) {
894 const char *ModuleStr =
895 "define void @foo(i32* %A, i32 %ub, i1 %cond) {\n"
896 "entry:\n"
897 " br i1 %cond, label %for.preheader, label %for.end\n"
898 "for.preheader:\n"
899 " br label %for.body\n"
900 "for.body:\n"
901 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
902 " %idxprom = sext i32 %i to i64\n"
903 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
904 " store i32 %i, i32* %arrayidx, align 4\n"
905 " %inc = add nsw i32 %i, 1\n"
906 " %cmp = icmp slt i32 %inc, %ub\n"
907 " br i1 %cmp, label %for.body, label %for.exit\n"
908 "for.exit:\n"
909 " br label %for.end\n"
910 "for.end:\n"
911 " ret void\n"
912 "}\n";
913
914 // Parse the module.
915 LLVMContext Context;
916 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
917
918 runWithLoopInfoPlus(
919 *M, "foo",
920 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
921 PostDominatorTree &PDT) {
922 Function::iterator FI = F.begin();
923 // First two basic block are entry and for.preheader - skip them.
924 ++FI;
925 BasicBlock *Header = &*(++FI);
926 assert(Header->getName() == "for.body");
927 Loop *L = LI.getLoopFor(Header);
928 EXPECT_NE(L, nullptr);
929
930 Optional Bounds = L->getBounds(SE);
931 EXPECT_NE(Bounds, None);
932 ConstantInt *InitialIVValue =
933 dyn_cast(&Bounds->getInitialIVValue());
934 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
935 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
936 ConstantInt *StepValue =
937 dyn_cast_or_null(Bounds->getStepValue());
938 EXPECT_TRUE(StepValue && StepValue->isOne());
939 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
940 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
941 EXPECT_EQ(Bounds->getDirection(),
942 Loop::LoopBounds::Direction::Increasing);
943 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
944 });
945 }
946
947 TEST(LoopInfoTest, LoopNest) {
948 const char *ModuleStr =
949 "define void @foo(i32* %A, i32 %ub) {\n"
950 "entry:\n"
951 " %guardcmp = icmp slt i32 0, %ub\n"
952 " br i1 %guardcmp, label %for.outer.preheader, label %for.end\n"
953 "for.outer.preheader:\n"
954 " br label %for.outer\n"
955 "for.outer:\n"
956 " %j = phi i32 [ 0, %for.outer.preheader ], [ %inc.outer, %for.outer.latch ]\n"
957 " br i1 %guardcmp, label %for.inner.preheader, label %for.outer.latch\n"
958 "for.inner.preheader:\n"
959 " br label %for.inner\n"
960 "for.inner:\n"
961 " %i = phi i32 [ 0, %for.inner.preheader ], [ %inc, %for.inner ]\n"
962 " %idxprom = sext i32 %i to i64\n"
963 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
964 " store i32 %i, i32* %arrayidx, align 4\n"
965 " %inc = add nsw i32 %i, 1\n"
966 " %cmp = icmp slt i32 %inc, %ub\n"
967 " br i1 %cmp, label %for.inner, label %for.inner.exit\n"
968 "for.inner.exit:\n"
969 " br label %for.outer.latch\n"
970 "for.outer.latch:\n"
971 " %inc.outer = add nsw i32 %j, 1\n"
972 " %cmp.outer = icmp slt i32 %inc.outer, %ub\n"
973 " br i1 %cmp.outer, label %for.outer, label %for.outer.exit\n"
974 "for.outer.exit:\n"
975 " br label %for.end\n"
976 "for.end:\n"
977 " ret void\n"
978 "}\n";
979
980 // Parse the module.
981 LLVMContext Context;
982 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
983
984 runWithLoopInfoPlus(
985 *M, "foo",
986 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
987 PostDominatorTree &PDT) {
988 Function::iterator FI = F.begin();
989 // First two basic block are entry and for.outer.preheader - skip them.
990 ++FI;
991 BasicBlock *Header = &*(++FI);
992 assert(Header->getName() == "for.outer");
993 Loop *L = LI.getLoopFor(Header);
994 EXPECT_NE(L, nullptr);
995
996 Optional Bounds = L->getBounds(SE);
997 EXPECT_NE(Bounds, None);
998 ConstantInt *InitialIVValue =
999 dyn_cast(&Bounds->getInitialIVValue());
1000 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
1001 EXPECT_EQ(Bounds->getStepInst().getName(), "inc.outer");
1002 ConstantInt *StepValue =
1003 dyn_cast_or_null(Bounds->getStepValue());
1004 EXPECT_TRUE(StepValue && StepValue->isOne());
1005 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
1006 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
1007 EXPECT_EQ(Bounds->getDirection(),
1008 Loop::LoopBounds::Direction::Increasing);
1009 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "j");
1010
1011 // Next two basic blocks are for.outer and for.inner.preheader - skip
1012 // them.
1013 ++FI;
1014 Header = &*(++FI);
1015 assert(Header->getName() == "for.inner");
1016 L = LI.getLoopFor(Header);
1017 EXPECT_NE(L, nullptr);
1018
1019 Optional InnerBounds = L->getBounds(SE);
1020 EXPECT_NE(InnerBounds, None);
1021 InitialIVValue =
1022 dyn_cast(&InnerBounds->getInitialIVValue());
1023 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
1024 EXPECT_EQ(InnerBounds->getStepInst().getName(), "inc");
1025 StepValue = dyn_cast_or_null(InnerBounds->getStepValue());
1026 EXPECT_TRUE(StepValue && StepValue->isOne());
1027 EXPECT_EQ(InnerBounds->getFinalIVValue().getName(), "ub");
1028 EXPECT_EQ(InnerBounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
1029 EXPECT_EQ(InnerBounds->getDirection(),
1030 Loop::LoopBounds::Direction::Increasing);
1031 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
1032 });
1033 }
1034
1035 TEST(LoopInfoTest, AuxiliaryIV) {
1036 const char *ModuleStr =
1037 "define void @foo(i32* %A, i32 %ub) {\n"
1038 "entry:\n"
1039 " %guardcmp = icmp slt i32 0, %ub\n"
1040 " br i1 %guardcmp, label %for.preheader, label %for.end\n"
1041 "for.preheader:\n"
1042 " br label %for.body\n"
1043 "for.body:\n"
1044 " %i = phi i32 [ 0, %for.preheader ], [ %inc, %for.body ]\n"
1045 " %aux = phi i32 [ 0, %for.preheader ], [ %auxinc, %for.body ]\n"
1046 " %loopvariant = phi i32 [ 0, %for.preheader ], [ %loopvariantinc, %for.body ]\n"
1047 " %usedoutside = phi i32 [ 0, %for.preheader ], [ %usedoutsideinc, %for.body ]\n"
1048 " %mulopcode = phi i32 [ 0, %for.preheader ], [ %mulopcodeinc, %for.body ]\n"
1049 " %idxprom = sext i32 %i to i64\n"
1050 " %arrayidx = getelementptr inbounds i32, i32* %A, i64 %idxprom\n"
1051 " store i32 %i, i32* %arrayidx, align 4\n"
1052 " %mulopcodeinc = mul nsw i32 %mulopcode, 5\n"
1053 " %usedoutsideinc = add nsw i32 %usedoutside, 5\n"
1054 " %loopvariantinc = add nsw i32 %loopvariant, %i\n"
1055 " %auxinc = add nsw i32 %aux, 5\n"
1056 " %inc = add nsw i32 %i, 1\n"
1057 " %cmp = icmp slt i32 %inc, %ub\n"
1058 " br i1 %cmp, label %for.body, label %for.exit\n"
1059 "for.exit:\n"
1060 " %lcssa = phi i32 [ %usedoutside, %for.body ]\n"
1061 " br label %for.end\n"
1062 "for.end:\n"
1063 " ret void\n"
1064 "}\n";
1065
1066 // Parse the module.
1067 LLVMContext Context;
1068 std::unique_ptr M = makeLLVMModule(Context, ModuleStr);
1069
1070 runWithLoopInfoPlus(
1071 *M, "foo",
1072 [&](Function &F, LoopInfo &LI, ScalarEvolution &SE,
1073 PostDominatorTree &PDT) {
1074 Function::iterator FI = F.begin();
1075 // First two basic block are entry and for.preheader - skip them.
1076 ++FI;
1077 BasicBlock *Header = &*(++FI);
1078 assert(Header->getName() == "for.body");
1079 Loop *L = LI.getLoopFor(Header);
1080 EXPECT_NE(L, nullptr);
1081
1082 Optional Bounds = L->getBounds(SE);
1083 EXPECT_NE(Bounds, None);
1084 ConstantInt *InitialIVValue =
1085 dyn_cast(&Bounds->getInitialIVValue());
1086 EXPECT_TRUE(InitialIVValue && InitialIVValue->isZero());
1087 EXPECT_EQ(Bounds->getStepInst().getName(), "inc");
1088 ConstantInt *StepValue =
1089 dyn_cast_or_null(Bounds->getStepValue());
1090 EXPECT_TRUE(StepValue && StepValue->isOne());
1091 EXPECT_EQ(Bounds->getFinalIVValue().getName(), "ub");
1092 EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT);
1093 EXPECT_EQ(Bounds->getDirection(),
1094 Loop::LoopBounds::Direction::Increasing);
1095 EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i");
1096 BasicBlock::iterator II = Header->begin();
1097 PHINode &Instruction_i = cast(*(II));
1098 EXPECT_TRUE(L->isAuxiliaryInductionVariable(Instruction_i, SE));
1099 PHINode &Instruction_aux = cast(*(++II));
1100 EXPECT_TRUE(L->isAuxiliaryInductionVariable(Instruction_aux, SE));
1101 PHINode &Instruction_loopvariant = cast(*(++II));
1102 EXPECT_FALSE(
1103 L->isAuxiliaryInductionVariable(Instruction_loopvariant, SE));
1104 PHINode &Instruction_usedoutside = cast(*(++II));
1105 EXPECT_FALSE(
1106 L->isAuxiliaryInductionVariable(Instruction_usedoutside, SE));
1107 PHINode &Instruction_mulopcode = cast(*(++II));
1108 EXPECT_FALSE(
1109 L->isAuxiliaryInductionVariable(Instruction_mulopcode, SE));
1110 });
1111 }