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, SameBlockNoLoop) {
152 "define void @test() {\n"
154 " br label %middle\n"
156 " %B = bitcast i8 undef to i8\n"
157 " bitcast i8 undef to i8\n"
158 " bitcast i8 undef to i8\n"
159 " %A = bitcast i8 undef to i8\n"
160 " br label %nextblock\n"
167 TEST_F(IsPotentiallyReachableTest, StraightNoPath) {
169 "define void @test() {\n"
171 " %B = bitcast i8 undef to i8\n"
174 " %A = bitcast i8 undef to i8\n"
180 TEST_F(IsPotentiallyReachableTest, StraightPath) {
182 "define void @test() {\n"
184 " %A = bitcast i8 undef to i8\n"
187 " %B = bitcast i8 undef to i8\n"
193 TEST_F(IsPotentiallyReachableTest, DestUnreachable) {
195 "define void @test() {\n"
197 " br label %midblock\n"
199 " %A = bitcast i8 undef to i8\n"
202 " %B = bitcast i8 undef to i8\n"
203 " br label %midblock\n"
208 TEST_F(IsPotentiallyReachableTest, BranchToReturn) {
210 "define void @test(i1 %x) {\n"
212 " %A = bitcast i8 undef to i8\n"
213 " br i1 %x, label %block1, label %block2\n"
217 " %B = bitcast i8 undef to i8\n"
223 TEST_F(IsPotentiallyReachableTest, SimpleLoop1) {
225 "declare i1 @switch()\n"
227 "define void @test() {\n"
231 " %B = bitcast i8 undef to i8\n"
232 " %A = bitcast i8 undef to i8\n"
233 " %x = call i1 @switch()\n"
234 " br i1 %x, label %loop, label %exit\n"
241 TEST_F(IsPotentiallyReachableTest, SimpleLoop2) {
243 "declare i1 @switch()\n"
245 "define void @test() {\n"
247 " %B = bitcast i8 undef to i8\n"
250 " %A = bitcast i8 undef to i8\n"
251 " %x = call i1 @switch()\n"
252 " br i1 %x, label %loop, label %exit\n"
259 TEST_F(IsPotentiallyReachableTest, SimpleLoop3) {
261 "declare i1 @switch()\n"
263 "define void @test() {\n"
267 " %B = bitcast i8 undef to i8\n"
268 " %x = call i1 @switch()\n"
269 " br i1 %x, label %loop, label %exit\n"
271 " %A = bitcast i8 undef to i8\n"
278 TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther1) {
280 "declare i1 @switch()\n"
282 "define void @test() {\n"
286 " %A = bitcast i8 undef to i8\n"
287 " %x = call i1 @switch()\n"
288 " br i1 %x, label %loop1, label %loop1exit\n"
292 " %B = bitcast i8 undef to i8\n"
293 " %y = call i1 @switch()\n"
294 " br i1 %x, label %loop2, label %loop2exit\n"
301 TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther2) {
303 "declare i1 @switch()\n"
305 "define void @test() {\n"
309 " %B = bitcast i8 undef to i8\n"
310 " %x = call i1 @switch()\n"
311 " br i1 %x, label %loop1, label %loop1exit\n"
315 " %A = bitcast i8 undef to i8\n"
316 " %y = call i1 @switch()\n"
317 " br i1 %x, label %loop2, label %loop2exit\n"
324 TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOtherInsideAThirdLoop) {
326 "declare i1 @switch()\n"
328 "define void @test() {\n"
330 " br label %outerloop3\n"
332 " br label %innerloop1\n"
334 " %B = bitcast i8 undef to i8\n"
335 " %x = call i1 @switch()\n"
336 " br i1 %x, label %innerloop1, label %innerloop1exit\n"
338 " br label %innerloop2\n"
340 " %A = bitcast i8 undef to i8\n"
341 " %y = call i1 @switch()\n"
342 " br i1 %x, label %innerloop2, label %innerloop2exit\n"
344 " ;; In outer loop3 now.\n"
345 " %z = call i1 @switch()\n"
346 " br i1 %z, label %outerloop3, label %exit\n"
353 TEST_F(IsPotentiallyReachableTest, BranchInsideLoop) {
355 "declare i1 @switch()\n"
357 "define void @test() {\n"
361 " %x = call i1 @switch()\n"
362 " br i1 %x, label %nextloopblock, label %exit\n"
364 " %y = call i1 @switch()\n"
365 " br i1 %y, label %left, label %right\n"
367 " %A = bitcast i8 undef to i8\n"
370 " %B = bitcast i8 undef to i8\n"