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