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