1*ca02360fSXin Tong //===- LoopInfoTest.cpp - LoopInfo unit tests -----------------------------===//
2*ca02360fSXin Tong //
3*ca02360fSXin Tong //                     The LLVM Compiler Infrastructure
4*ca02360fSXin Tong //
5*ca02360fSXin Tong // This file is distributed under the University of Illinois Open Source
6*ca02360fSXin Tong // License. See LICENSE.TXT for details.
7*ca02360fSXin Tong //
8*ca02360fSXin Tong //===----------------------------------------------------------------------===//
9*ca02360fSXin Tong 
10*ca02360fSXin Tong #include "llvm/Analysis/LoopInfo.h"
11*ca02360fSXin Tong #include "llvm/AsmParser/Parser.h"
12*ca02360fSXin Tong #include "llvm/IR/Dominators.h"
13*ca02360fSXin Tong #include "llvm/Support/SourceMgr.h"
14*ca02360fSXin Tong #include "gtest/gtest.h"
15*ca02360fSXin Tong 
16*ca02360fSXin Tong using namespace llvm;
17*ca02360fSXin Tong 
18*ca02360fSXin Tong static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
19*ca02360fSXin Tong                                               const char *ModuleStr) {
20*ca02360fSXin Tong   SMDiagnostic Err;
21*ca02360fSXin Tong   return parseAssemblyString(ModuleStr, Err, Context);
22*ca02360fSXin Tong }
23*ca02360fSXin Tong 
24*ca02360fSXin Tong // This tests that for a loop with a single latch, we get the loop id from
25*ca02360fSXin Tong // its only latch, even in case the loop may not be in a simplified form.
26*ca02360fSXin Tong TEST(LoopInfoTest, LoopWithSingleLatch) {
27*ca02360fSXin Tong   const char *ModuleStr =
28*ca02360fSXin Tong          "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
29*ca02360fSXin Tong          "define void @foo(i32 %n) {\n"
30*ca02360fSXin Tong          "entry:\n"
31*ca02360fSXin Tong          "  br i1 undef, label %for.cond, label %for.end\n"
32*ca02360fSXin Tong          "for.cond:\n"
33*ca02360fSXin Tong          "  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
34*ca02360fSXin Tong          "  %cmp = icmp slt i32 %i.0, %n\n"
35*ca02360fSXin Tong          "  br i1 %cmp, label %for.inc, label %for.end\n"
36*ca02360fSXin Tong          "for.inc:\n"
37*ca02360fSXin Tong          "  %inc = add nsw i32 %i.0, 1\n"
38*ca02360fSXin Tong          "  br label %for.cond, !llvm.loop !0\n"
39*ca02360fSXin Tong          "for.end:\n"
40*ca02360fSXin Tong          "  ret void\n"
41*ca02360fSXin Tong          "}\n"
42*ca02360fSXin Tong          "!0 = distinct !{!0, !1}\n"
43*ca02360fSXin Tong          "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
44*ca02360fSXin Tong 
45*ca02360fSXin Tong   // Parse the module.
46*ca02360fSXin Tong   LLVMContext Context;
47*ca02360fSXin Tong   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
48*ca02360fSXin Tong 
49*ca02360fSXin Tong   // Build the dominator tree and loop info.
50*ca02360fSXin Tong   DominatorTree DT;
51*ca02360fSXin Tong   DT.recalculate(*M->begin());
52*ca02360fSXin Tong   LoopInfo LI;
53*ca02360fSXin Tong   LI.analyze(DT);
54*ca02360fSXin Tong 
55*ca02360fSXin Tong 
56*ca02360fSXin Tong   Function &F = *M->begin();
57*ca02360fSXin Tong   Function::iterator FI = F.begin();
58*ca02360fSXin Tong   FI++; // First basic block is entry - skip it.
59*ca02360fSXin Tong   BasicBlock *Header = &*FI++;
60*ca02360fSXin Tong   assert(Header->getName() == "for.cond");
61*ca02360fSXin Tong   Loop *L = LI.getLoopFor(Header);
62*ca02360fSXin Tong 
63*ca02360fSXin Tong   // This loop is not in simplified form.
64*ca02360fSXin Tong   EXPECT_FALSE(L->isLoopSimplifyForm());
65*ca02360fSXin Tong 
66*ca02360fSXin Tong   // Analyze the loop metadata id.
67*ca02360fSXin Tong   bool loopIDFoundAndSet = false;
68*ca02360fSXin Tong   // Try to get and set the metadata id for the loop.
69*ca02360fSXin Tong   if (MDNode *D = L->getLoopID()) {
70*ca02360fSXin Tong     L->setLoopID(D);
71*ca02360fSXin Tong     loopIDFoundAndSet = true;
72*ca02360fSXin Tong   }
73*ca02360fSXin Tong 
74*ca02360fSXin Tong   // We must have successfully found and set the loop id in the
75*ca02360fSXin Tong   // only latch the loop has.
76*ca02360fSXin Tong   EXPECT_TRUE(loopIDFoundAndSet);
77*ca02360fSXin Tong }
78