1 //===- CFGTest.cpp - CFG tests --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/CFG.h"
11 #include "llvm/ADT/OwningPtr.h"
12 #include "llvm/Analysis/Dominators.h"
13 #include "llvm/Analysis/LoopInfo.h"
14 #include "llvm/Assembly/Parser.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/InstIterator.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Pass.h"
22 #include "llvm/PassManager.h"
23 #include "gtest/gtest.h"
29 // This fixture assists in running the isPotentiallyReachable utility four ways
30 // and ensuring it produces the correct answer each time.
31 class IsPotentiallyReachableTest : public testing::Test {
33 void ParseAssembly(const char *Assembly) {
34 M.reset(new Module("Module", getGlobalContext()));
37 bool Parsed = ParseAssemblyString(Assembly, M.get(),
38 Error, M->getContext()) == M.get();
41 raw_string_ostream os(errMsg);
45 // A failure here means that the test itself is buggy.
46 report_fatal_error(os.str().c_str());
49 Function *F = M->getFunction("test");
51 report_fatal_error("Test must have a function named @test");
54 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
56 if (I->getName() == "A")
58 else if (I->getName() == "B")
63 report_fatal_error("@test must have an instruction %A");
65 report_fatal_error("@test must have an instruction %B");
68 void ExpectPath(bool ExpectedResult) {
70 class IsPotentiallyReachableTestPass : public FunctionPass {
72 IsPotentiallyReachableTestPass(bool ExpectedResult,
73 Instruction *A, Instruction *B)
74 : FunctionPass(ID), ExpectedResult(ExpectedResult), A(A), B(B) {}
76 static int initialize() {
77 PassInfo *PI = new PassInfo("isPotentiallyReachable testing pass",
78 "", &ID, 0, true, true);
79 PassRegistry::getPassRegistry()->registerPass(*PI, false);
80 initializeLoopInfoPass(*PassRegistry::getPassRegistry());
81 initializeDominatorTreePass(*PassRegistry::getPassRegistry());
85 void getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.addRequired<LoopInfo>();
88 AU.addRequired<DominatorTree>();
91 bool runOnFunction(Function &F) {
92 if (!F.hasName() || F.getName() != "test")
95 LoopInfo *LI = &getAnalysis<LoopInfo>();
96 DominatorTree *DT = &getAnalysis<DominatorTree>();
97 EXPECT_EQ(isPotentiallyReachable(A, B, 0, 0), ExpectedResult);
98 EXPECT_EQ(isPotentiallyReachable(A, B, DT, 0), ExpectedResult);
99 EXPECT_EQ(isPotentiallyReachable(A, B, 0, LI), ExpectedResult);
100 EXPECT_EQ(isPotentiallyReachable(A, B, DT, LI), ExpectedResult);
107 static int initialize = IsPotentiallyReachableTestPass::initialize();
110 IsPotentiallyReachableTestPass *P =
111 new IsPotentiallyReachableTestPass(ExpectedResult, A, B);
123 TEST_F(IsPotentiallyReachableTest, SameBlockNoPath) {
125 "define void @test() {\n"
127 " bitcast i8 undef to i8\n"
128 " %B = bitcast i8 undef to i8\n"
129 " bitcast i8 undef to i8\n"
130 " bitcast i8 undef to i8\n"
131 " %A = bitcast i8 undef to i8\n"
137 TEST_F(IsPotentiallyReachableTest, SameBlockPath) {
139 "define void @test() {\n"
141 " %A = bitcast i8 undef to i8\n"
142 " bitcast i8 undef to i8\n"
143 " bitcast i8 undef to i8\n"
144 " %B = bitcast i8 undef to i8\n"
150 TEST_F(IsPotentiallyReachableTest, StraightNoPath) {
152 "define void @test() {\n"
154 " %B = bitcast i8 undef to i8\n"
157 " %A = bitcast i8 undef to i8\n"
163 TEST_F(IsPotentiallyReachableTest, StraightPath) {
165 "define void @test() {\n"
167 " %A = bitcast i8 undef to i8\n"
170 " %B = bitcast i8 undef to i8\n"
176 TEST_F(IsPotentiallyReachableTest, DestUnreachable) {
178 "define void @test() {\n"
180 " br label %midblock\n"
182 " %A = bitcast i8 undef to i8\n"
185 " %B = bitcast i8 undef to i8\n"
186 " br label %midblock\n"
191 TEST_F(IsPotentiallyReachableTest, BranchToReturn) {
193 "define void @test(i1 %x) {\n"
195 " %A = bitcast i8 undef to i8\n"
196 " br i1 %x, label %block1, label %block2\n"
200 " %B = bitcast i8 undef to i8\n"
206 TEST_F(IsPotentiallyReachableTest, SimpleLoop1) {
208 "declare i1 @switch()\n"
210 "define void @test() {\n"
214 " %B = bitcast i8 undef to i8\n"
215 " %A = bitcast i8 undef to i8\n"
216 " %x = call i1 @switch()\n"
217 " br i1 %x, label %loop, label %exit\n"
224 TEST_F(IsPotentiallyReachableTest, SimpleLoop2) {
226 "declare i1 @switch()\n"
228 "define void @test() {\n"
230 " %B = bitcast i8 undef to i8\n"
233 " %A = bitcast i8 undef to i8\n"
234 " %x = call i1 @switch()\n"
235 " br i1 %x, label %loop, label %exit\n"
242 TEST_F(IsPotentiallyReachableTest, SimpleLoop3) {
244 "declare i1 @switch()\n"
246 "define void @test() {\n"
250 " %B = bitcast i8 undef to i8\n"
251 " %x = call i1 @switch()\n"
252 " br i1 %x, label %loop, label %exit\n"
254 " %A = bitcast i8 undef to i8\n"
261 TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther1) {
263 "declare i1 @switch()\n"
265 "define void @test() {\n"
269 " %A = bitcast i8 undef to i8\n"
270 " %x = call i1 @switch()\n"
271 " br i1 %x, label %loop1, label %loop1exit\n"
275 " %B = bitcast i8 undef to i8\n"
276 " %y = call i1 @switch()\n"
277 " br i1 %x, label %loop2, label %loop2exit\n"
284 TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther2) {
286 "declare i1 @switch()\n"
288 "define void @test() {\n"
292 " %B = bitcast i8 undef to i8\n"
293 " %x = call i1 @switch()\n"
294 " br i1 %x, label %loop1, label %loop1exit\n"
298 " %A = bitcast i8 undef to i8\n"
299 " %y = call i1 @switch()\n"
300 " br i1 %x, label %loop2, label %loop2exit\n"
307 TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOtherInsideAThirdLoop) {
309 "declare i1 @switch()\n"
311 "define void @test() {\n"
313 " br label %outerloop3\n"
315 " br label %innerloop1\n"
317 " %B = bitcast i8 undef to i8\n"
318 " %x = call i1 @switch()\n"
319 " br i1 %x, label %innerloop1, label %innerloop1exit\n"
321 " br label %innerloop2\n"
323 " %A = bitcast i8 undef to i8\n"
324 " %y = call i1 @switch()\n"
325 " br i1 %x, label %innerloop2, label %innerloop2exit\n"
327 " ;; In outer loop3 now.\n"
328 " %z = call i1 @switch()\n"
329 " br i1 %z, label %outerloop3, label %exit\n"
336 TEST_F(IsPotentiallyReachableTest, BranchInsideLoop) {
338 "declare i1 @switch()\n"
340 "define void @test() {\n"
344 " %x = call i1 @switch()\n"
345 " br i1 %x, label %nextloopblock, label %exit\n"
347 " %y = call i1 @switch()\n"
348 " br i1 %y, label %left, label %right\n"
350 " %A = bitcast i8 undef to i8\n"
353 " %B = bitcast i8 undef to i8\n"