llvm.org GIT mirror llvm / caa6b75
Revert "Title: [LOOPINFO] Extend Loop object to add utilities to get the loop" This reverts commit d34797dfc26c61cea19f45669a13ea572172ba34. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362615 91177308-0d34-0410-b5e6-96231b3b80d8 Whitney Tsang a month ago
3 changed file(s) with 0 addition(s) and 1276 deletion(s). Raw diff Collapse all Expand all
5353 class DominatorTree;
5454 class LoopInfo;
5555 class Loop;
56 class InductionDescriptor;
5756 class MDNode;
5857 class MemorySSAUpdater;
5958 class PHINode;
60 class ScalarEvolution;
6159 class raw_ostream;
6260 template class DominatorTreeBase;
6361 template class LoopInfoBase;
529527 /// non-unique or the loop is dead; otherwise, return true.
530528 bool getIncomingAndBackEdge(BasicBlock *&Incoming,
531529 BasicBlock *&Backedge) const;
532
533 /// Below are some utilities to get loop bounds and induction variable, and
534 /// check if a given phinode is an auxiliary induction variable, as well as
535 /// checking if the loop is canonical.
536 ///
537 /// Here is an example:
538 /// \code
539 /// for (int i = lb; i < ub; i+=step)
540 ///
541 /// --- pseudo LLVMIR ---
542 /// beforeloop:
543 /// guardcmp = (lb < ub)
544 /// if (guardcmp) goto preheader; else goto afterloop
545 /// preheader:
546 /// loop:
547 /// i_1 = phi[{lb, preheader}, {i_2, latch}]
548 ///
549 /// i_2 = i_1 + step
550 /// latch:
551 /// cmp = (i_2 < ub)
552 /// if (cmp) goto loop
553 /// exit:
554 /// afterloop:
555 /// \endcode
556 ///
557 /// - getBounds
558 /// - getInitialIVValue --> lb
559 /// - getStepInst --> i_2 = i_1 + step
560 /// - getStepValue --> step
561 /// - getFinalIVValue --> ub
562 /// - getCanonicalPredicate --> '<'
563 /// - getDirection --> Increasing
564 ///
565 /// - getInductionVariable --> i_1
566 /// - isAuxiliaryInductionVariable(x) --> true if x == i_1
567 /// - isCanonical --> false
568 struct LoopBounds {
569 /// Return the LoopBounds object if
570 /// - the given \p IndVar is an induction variable
571 /// - the initial value of the induction variable can be found
572 /// - the step instruction of the induction variable can be found
573 /// - the final value of the induction variable can be found
574 ///
575 /// Else None.
576 static Optional getBounds(const Loop &L, PHINode &IndVar,
577 ScalarEvolution &SE);
578
579 /// Get the initial value of the loop induction variable.
580 Value &getInitialIVValue() const { return InitialIVValue; }
581
582 /// Get the instruction that updates the loop induction variable.
583 Instruction &getStepInst() const { return StepInst; }
584
585 /// Get the step that the loop induction variable gets updated by in each
586 /// loop iteration. Return nullptr if not found.
587 Value *getStepValue() const { return StepValue; }
588
589 /// Get the final value of the loop induction variable.
590 Value &getFinalIVValue() const { return FinalIVValue; }
591
592 /// Return the canonical predicate for the latch compare instruction, if
593 /// able to be calcuated. Else BAD_ICMP_PREDICATE.
594 ///
595 /// A predicate is considered as canonical if requirements below are all
596 /// satisfied:
597 /// 1. The first successor of the latch branch is the loop header
598 /// If not, inverse the predicate.
599 /// 2. One of the operands of the latch comparison is StepInst
600 /// If not, and
601 /// - if the current calcuated predicate is not ne or eq, flip the
602 /// predicate.
603 /// - else if the loop is increasing, return slt
604 /// (notice that it is safe to change from ne or eq to sign compare)
605 /// - else if the loop is decreasing, return sgt
606 /// (notice that it is safe to change from ne or eq to sign compare)
607 ///
608 /// Here is an example when both (1) and (2) are not satisfied:
609 /// \code
610 /// loop.header:
611 /// %iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
612 /// %inc = add %iv, %step
613 /// %cmp = slt %iv, %finaliv
614 /// br %cmp, %loop.exit, %loop.header
615 /// loop.exit:
616 /// \endcode
617 /// - The second successor of the latch branch is the loop header instead
618 /// of the first successor (slt -> sge)
619 /// - The first operand of the latch comparison (%cmp) is the IndVar (%iv)
620 /// instead of the StepInst (%inc) (sge -> sgt)
621 ///
622 /// The predicate would be sgt if both (1) and (2) are satisfied.
623 /// getCanonicalPredicate() returns sgt for this example.
624 /// Note: The IR is not changed.
625 ICmpInst::Predicate getCanonicalPredicate() const;
626
627 /// An enum for the direction of the loop
628 /// - for (int i = 0; i < ub; ++i) --> Increasing
629 /// - for (int i = ub; i > 0; --i) --> Descresing
630 /// - for (int i = x; i != y; i+=z) --> Unknown
631 enum class Direction { Increasing, Decreasing, Unknown };
632
633 /// Get the direction of the loop.
634 Direction getDirection() const;
635
636 private:
637 LoopBounds(const Loop &Loop, Value &I, Instruction &SI, Value *SV, Value &F,
638 ScalarEvolution &SE)
639 : L(Loop), InitialIVValue(I), StepInst(SI), StepValue(SV),
640 FinalIVValue(F), SE(SE) {}
641
642 const Loop &L;
643
644 // The initial value of the loop induction variable
645 Value &InitialIVValue;
646
647 // The instruction that updates the loop induction variable
648 Instruction &StepInst;
649
650 // The value that the loop induction variable gets updated by in each loop
651 // iteration
652 Value *StepValue;
653
654 // The final value of the loop induction variable
655 Value &FinalIVValue;
656
657 ScalarEvolution &SE;
658 };
659
660 /// Return the struct LoopBounds collected if all struct members are found,
661 /// else None.
662 Optional getBounds(ScalarEvolution &SE) const;
663
664 /// Return the loop induction variable if found, else return nullptr.
665 /// An instruction is considered as the loop induction variable if
666 /// - it is an induction variable of the loop; and
667 /// - it is used to determine the condition of the branch in the loop latch
668 ///
669 /// Note: the induction variable doesn't need to be canonical, i.e. starts at
670 /// zero and increments by one each time through the loop (but it can be).
671 PHINode *getInductionVariable(ScalarEvolution &SE) const;
672
673 /// Get the loop induction descriptor for the loop induction variable. Return
674 /// true if the loop induction variable is found.
675 bool getInductionDescriptor(ScalarEvolution &SE,
676 InductionDescriptor &IndDesc) const;
677
678 /// Return true if the given PHINode \p AuxIndVar is
679 /// - in the loop header
680 /// - not used outside of the loop
681 /// - incremented by a loop invariant step for each loop iteration
682 /// - step instruction opcode should be add or sub
683 /// Note: auxiliary induction variable is not required to be used in the
684 /// conditional branch in the loop latch. (but it can be)
685 bool isAuxiliaryInductionVariable(PHINode &AuxIndVar,
686 ScalarEvolution &SE) const;
687
688 /// Return true if the loop induction variable starts at zero and increments
689 /// by one each time through the loop.
690 bool isCanonical(ScalarEvolution &SE) const;
691530
692531 /// Return true if the Loop is in LCSSA form.
693532 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"
2019 #include "llvm/Analysis/LoopInfoImpl.h"
2120 #include "llvm/Analysis/LoopIterator.h"
2221 #include "llvm/Analysis/MemorySSA.h"
2322 #include "llvm/Analysis/MemorySSAUpdater.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2523 #include "llvm/Analysis/ValueTracking.h"
2624 #include "llvm/Config/llvm-config.h"
2725 #include "llvm/IR/CFG.h"
165163 return nullptr;
166164 }
167165
168 /// Get the latch condition instruction.
169 static ICmpInst *getLatchCmpInst(const Loop &L) {
170 if (BasicBlock *Latch = L.getLoopLatch())
171 if (BranchInst *BI = dyn_cast_or_null(Latch->getTerminator()))
172 if (BI->isConditional())
173 return dyn_cast(BI->getCondition());
174
175 return nullptr;
176 }
177
178 /// Return the final value of the loop induction variable if found.
179 static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
180 const Instruction &StepInst) {
181 ICmpInst *LatchCmpInst = getLatchCmpInst(L);
182 if (!LatchCmpInst)
183 return nullptr;
184
185 Value *Op0 = LatchCmpInst->getOperand(0);
186 Value *Op1 = LatchCmpInst->getOperand(1);
187 if (Op0 == &IndVar || Op0 == &StepInst)
188 return Op1;
189
190 if (Op1 == &IndVar || Op1 == &StepInst)
191 return Op0;
192
193 return nullptr;
194 }
195
196 Optional Loop::LoopBounds::getBounds(const Loop &L,
197 PHINode &IndVar,
198 ScalarEvolution &SE) {
199 InductionDescriptor IndDesc;
200 if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc))
201 return None;
202
203 Value *InitialIVValue = IndDesc.getStartValue();
204 Instruction *StepInst = IndDesc.getInductionBinOp();
205 if (!InitialIVValue || !StepInst)
206 return None;
207
208 const SCEV *Step = IndDesc.getStep();
209 Value *StepInstOp1 = StepInst->getOperand(1);
210 Value *StepInstOp0 = StepInst->getOperand(0);
211 Value *StepValue = nullptr;
212 if (SE.getSCEV(StepInstOp1) == Step)
213 StepValue = StepInstOp1;
214 else if (SE.getSCEV(StepInstOp0) == Step)
215 StepValue = StepInstOp0;
216
217 Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst);
218 if (!FinalIVValue)
219 return None;
220
221 return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue,
222 SE);
223 }
224
225 using Direction = Loop::LoopBounds::Direction;
226
227 ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const {
228 BasicBlock *Latch = L.getLoopLatch();
229 assert(Latch && "Expecting valid latch");
230
231 BranchInst *BI = dyn_cast_or_null(Latch->getTerminator());
232 assert(BI && BI->isConditional() && "Expecting conditional latch branch");
233
234 ICmpInst *LatchCmpInst = dyn_cast(BI->getCondition());
235 assert(LatchCmpInst &&
236 "Expecting the latch compare instruction to be a CmpInst");
237
238 // Need to inverse the predicate when first successor is not the loop
239 // header
240 ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader())
241 ? LatchCmpInst->getPredicate()
242 : LatchCmpInst->getInversePredicate();
243
244 if (LatchCmpInst->getOperand(0) == &getFinalIVValue())
245 Pred = ICmpInst::getSwappedPredicate(Pred);
246
247 // Need to flip strictness of the predicate when the latch compare instruction
248 // is not using StepInst
249 if (LatchCmpInst->getOperand(0) == &getStepInst() ||
250 LatchCmpInst->getOperand(1) == &getStepInst())
251 return Pred;
252
253 // Cannot flip strictness of NE and EQ
254 if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
255 return ICmpInst::getFlippedStrictnessPredicate(Pred);
256
257 Direction D = getDirection();
258 if (D == Direction::Increasing)
259 return ICmpInst::ICMP_SLT;
260
261 if (D == Direction::Decreasing)
262 return ICmpInst::ICMP_SGT;
263
264 // If cannot determine the direction, then unable to find the canonical
265 // predicate
266 return ICmpInst::BAD_ICMP_PREDICATE;
267 }
268
269 Direction Loop::LoopBounds::getDirection() const {
270 if (const SCEVAddRecExpr *StepAddRecExpr =
271 dyn_cast(SE.getSCEV(&getStepInst())))
272 if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) {
273 if (SE.isKnownPositive(StepRecur))
274 return Direction::Increasing;
275 if (SE.isKnownNegative(StepRecur))
276 return Direction::Decreasing;
277 }
278
279 return Direction::Unknown;
280 }
281
282 Optional Loop::getBounds(ScalarEvolution &SE) const {
283 if (PHINode *IndVar = getInductionVariable(SE))
284 return LoopBounds::getBounds(*this, *IndVar, SE);
285
286 return None;
287 }
288
289 PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
290 if (!isLoopSimplifyForm())
291 return nullptr;
292
293 BasicBlock *Header = getHeader();
294 assert(Header && "Expected a valid loop header");
295 assert(getLoopLatch() && "Expected a valid loop latch");
296 ICmpInst *CmpInst = getLatchCmpInst(*this);
297 if (!CmpInst)
298 return nullptr;
299
300 Instruction *LatchCmpOp0 = dyn_cast(CmpInst->getOperand(0));
301 Instruction *LatchCmpOp1 = dyn_cast(CmpInst->getOperand(1));
302
303 for (PHINode &IndVar : Header->phis()) {
304 InductionDescriptor IndDesc;
305 if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc))
306 continue;
307
308 Instruction *StepInst = IndDesc.getInductionBinOp();
309
310 // case 1:
311 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
312 // StepInst = IndVar + step
313 // cmp = StepInst < FinalValue
314 if (StepInst == LatchCmpOp0 || StepInst == LatchCmpOp1)
315 return &IndVar;
316
317 // case 2:
318 // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
319 // StepInst = IndVar + step
320 // cmp = IndVar < FinalValue
321 if (&IndVar == LatchCmpOp0 || &IndVar == LatchCmpOp1)
322 return &IndVar;
323 }
324
325 return nullptr;
326 }
327
328 bool Loop::getInductionDescriptor(ScalarEvolution &SE,
329 InductionDescriptor &IndDesc) const {
330 if (PHINode *IndVar = getInductionVariable(SE))
331 return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
332
333 return false;
334 }
335
336 bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar,
337 ScalarEvolution &SE) const {
338 // Located in the loop header
339 BasicBlock *Header = getHeader();
340 if (AuxIndVar.getParent() != Header)
341 return false;
342
343 // No uses outside of the loop
344 for (User *U : AuxIndVar.users())
345 if (const Instruction *I = dyn_cast(U))
346 if (!contains(I))
347 return false;
348
349 InductionDescriptor IndDesc;
350 if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc))
351 return false;
352
353 // The step instruction opcode should be add or sub.
354 if (IndDesc.getInductionOpcode() != Instruction::Add &&
355 IndDesc.getInductionOpcode() != Instruction::Sub)
356 return false;
357
358 // Incremented by a loop invariant step for each loop iteration
359 return SE.isLoopInvariant(IndDesc.getStep(), this);
360 }
361
362 bool Loop::isCanonical(ScalarEvolution &SE) const {
363 InductionDescriptor IndDesc;
364 if (!getInductionDescriptor(SE, IndDesc))
365 return false;
366
367 ConstantInt *Init = dyn_cast_or_null(IndDesc.getStartValue());
368 if (!Init || !Init->isZero())
369 return false;
370
371 if (IndDesc.getInductionOpcode() != Instruction::Add)
372 return false;
373
374 ConstantInt *Step = IndDesc.getConstIntStepValue();
375 if (!Step || !Step->isOne())
376 return false;
377
378 return true;
379 }
380
381166 // Check that 'BB' doesn't have any uses outside of the 'L'
382167 static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
383168 DominatorTree &DT) {
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"
139 #include "llvm/AsmParser/Parser.h"
1410 #include "llvm/IR/Dominators.h"
1511 #include "llvm/Support/SourceMgr.h"
2723 DominatorTree DT(*F);
2824 LoopInfo LI(DT);
2925 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);
5026 }
5127
5228 static std::unique_ptr makeLLVMModule(LLVMContext &Context,
233209 EXPECT_EQ(&L_0_1, ReverseSiblingPreorder[6]);
234210 EXPECT_EQ(&L_0_0, ReverseSiblingPreorder[7]);
235211 }
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 }