llvm.org GIT mirror llvm / 092c5cc
Handle unreachable code in the dominates functions. This changes users when needed for correctness, but still doesn't clean up code that now unnecessary checks for reachability. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153755 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 8 years ago
6 changed file(s) with 240 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
358358
359359 bool dominatedBySlowTreeWalk(const DomTreeNodeBase *A,
360360 const DomTreeNodeBase *B) const {
361 // A node trivially dominates itself.
362 if (B == A)
363 return true;
364
365 // An unreachable node is dominated by anything.
366 if (!isReachableFromEntry(B))
367 return true;
368
369 // And dominates nothing.
370 if (!isReachableFromEntry(A))
371 return false;
372
361373 const DomTreeNodeBase *IDom;
362 if (A == 0 || B == 0) return false;
363374 while ((IDom = B->getIDom()) != 0 && IDom != A && IDom != B)
364375 B = IDom; // Walk up the tree
365376 return IDom != 0;
368379
369380 /// isReachableFromEntry - Return true if A is dominated by the entry
370381 /// block of the function containing it.
371 bool isReachableFromEntry(const NodeT* A) {
382 bool isReachableFromEntry(const NodeT* A) const {
372383 assert(!this->isPostDominator() &&
373384 "This is not implemented for post dominators");
374 return dominates(&A->getParent()->front(), A);
385 return isReachableFromEntry(getNode(const_cast(A)));
386 }
387
388 inline bool isReachableFromEntry(const DomTreeNodeBase *A) const {
389 return A;
375390 }
376391
377392 /// dominates - Returns true iff A dominates B. Note that this is not a
379394 ///
380395 inline bool dominates(const DomTreeNodeBase *A,
381396 const DomTreeNodeBase *B) {
397 // A node trivially dominates itself.
382398 if (B == A)
383 return true; // A node trivially dominates itself.
384
385 if (A == 0 || B == 0)
399 return true;
400
401 // An unreachable node is dominated by anything.
402 if (!isReachableFromEntry(B))
403 return true;
404
405 // And dominates nothing.
406 if (!isReachableFromEntry(A))
386407 return false;
387408
388409 // Compare the result of the tree walk and the dfs numbers, if expensive
761761 InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
762762 I != E; ++I) {
763763 typename InvBlockTraits::NodeType *N = *I;
764 if (DT.dominates(BB, N)) // If BB dominates its predecessor...
764 // If BB dominates its predecessor...
765 if (DT.dominates(BB, N) && DT.isReachableFromEntry(N))
765766 TodoStack.push_back(N);
766767 }
767768
8787 const BasicBlock *UseBB = User->getParent();
8888 const BasicBlock *DefBB = Def->getParent();
8989
90 assert(isReachableFromEntry(DefBB) && isReachableFromEntry(UseBB) &&
91 "We only handle reachable blocks");
90 // Any unreachable use is dominated, even if Def == User.
91 if (!isReachableFromEntry(UseBB))
92 return true;
93
94 // Unreachable definitions don't dominate anything.
95 if (!isReachableFromEntry(DefBB))
96 return false;
9297
9398 // An instruction doesn't dominate a use in itself.
9499 if (Def == User)
118123 const BasicBlock *UseBB) const {
119124 const BasicBlock *DefBB = Def->getParent();
120125
121 assert(isReachableFromEntry(DefBB) && isReachableFromEntry(UseBB) &&
122 "We only handle reachable blocks");
126 // Any unreachable use is dominated, even if DefBB == UseBB.
127 if (!isReachableFromEntry(UseBB))
128 return true;
129
130 // Unreachable definitions don't dominate anything.
131 if (!isReachableFromEntry(DefBB))
132 return false;
123133
124134 if (DefBB == UseBB)
125135 return false;
138138 VMCore/PassManagerTest.cpp
139139 VMCore/ValueMapTest.cpp
140140 VMCore/VerifierTest.cpp
141 VMCore/DominatorTreeTest.cpp
141142 )
142143
143144 # MSVC9 and 8 cannot compile ValueMapTest.cpp due to their bug.
0 #include "llvm/Instructions.h"
1 #include "llvm/LLVMContext.h"
2 #include "llvm/Module.h"
3 #include "llvm/PassManager.h"
4 #include "llvm/Analysis/Dominators.h"
5 #include "llvm/Assembly/Parser.h"
6 #include "llvm/Support/SourceMgr.h"
7 #include "gtest/gtest.h"
8
9 using namespace llvm;
10
11 namespace llvm {
12 void initializeDPassPass(PassRegistry&);
13
14 namespace {
15 struct DPass : public FunctionPass {
16 static char ID;
17 virtual bool runOnFunction(Function &F) {
18 DominatorTree *DT = &getAnalysis();
19 Function::iterator FI = F.begin();
20
21 BasicBlock *BB0 = FI++;
22 BasicBlock::iterator BBI = BB0->begin();
23 Instruction *Y1 = BBI++;
24 Instruction *Y2 = BBI++;
25 Instruction *Y3 = BBI++;
26
27 BasicBlock *BB1 = FI++;
28 BBI = BB1->begin();
29 Instruction *Y4 = BBI++;
30
31 BasicBlock *BB2 = FI++;
32 BBI = BB2->begin();
33 Instruction *Y5 = BBI++;
34
35 BasicBlock *BB3 = FI++;
36 BBI = BB3->begin();
37 Instruction *Y6 = BBI++;
38 Instruction *Y7 = BBI++;
39
40 BasicBlock *BB4 = FI++;
41 BBI = BB4->begin();
42 Instruction *Y8 = BBI++;
43 Instruction *Y9 = BBI++;
44
45 // Reachability
46 EXPECT_TRUE(DT->isReachableFromEntry(BB0));
47 EXPECT_TRUE(DT->isReachableFromEntry(BB1));
48 EXPECT_TRUE(DT->isReachableFromEntry(BB2));
49 EXPECT_FALSE(DT->isReachableFromEntry(BB3));
50 EXPECT_TRUE(DT->isReachableFromEntry(BB4));
51
52 // BB dominance
53 EXPECT_TRUE(DT->dominates(BB0, BB0));
54 EXPECT_TRUE(DT->dominates(BB0, BB1));
55 EXPECT_TRUE(DT->dominates(BB0, BB2));
56 EXPECT_TRUE(DT->dominates(BB0, BB3));
57 EXPECT_TRUE(DT->dominates(BB0, BB4));
58
59 EXPECT_FALSE(DT->dominates(BB1, BB0));
60 EXPECT_TRUE(DT->dominates(BB1, BB1));
61 EXPECT_FALSE(DT->dominates(BB1, BB2));
62 EXPECT_TRUE(DT->dominates(BB1, BB3));
63 EXPECT_FALSE(DT->dominates(BB1, BB4));
64
65 EXPECT_FALSE(DT->dominates(BB2, BB0));
66 EXPECT_FALSE(DT->dominates(BB2, BB1));
67 EXPECT_TRUE(DT->dominates(BB2, BB2));
68 EXPECT_TRUE(DT->dominates(BB2, BB3));
69 EXPECT_FALSE(DT->dominates(BB2, BB4));
70
71 EXPECT_FALSE(DT->dominates(BB3, BB0));
72 EXPECT_FALSE(DT->dominates(BB3, BB1));
73 EXPECT_FALSE(DT->dominates(BB3, BB2));
74 EXPECT_TRUE(DT->dominates(BB3, BB3));
75 EXPECT_FALSE(DT->dominates(BB3, BB4));
76
77 // BB proper dominance
78 EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
79 EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
80 EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
81 EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
82
83 EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
84 EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
85 EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
86 EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
87
88 EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
89 EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
90 EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
91 EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
92
93 EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
94 EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
95 EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
96 EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
97
98 // Instruction dominance in the same reachable BB
99 EXPECT_FALSE(DT->dominates(Y1, Y1));
100 EXPECT_TRUE(DT->dominates(Y1, Y2));
101 EXPECT_FALSE(DT->dominates(Y2, Y1));
102 EXPECT_FALSE(DT->dominates(Y2, Y2));
103
104 // Instruction dominance in the same unreachable BB
105 EXPECT_TRUE(DT->dominates(Y6, Y6));
106 EXPECT_TRUE(DT->dominates(Y6, Y7));
107 EXPECT_TRUE(DT->dominates(Y7, Y6));
108 EXPECT_TRUE(DT->dominates(Y7, Y7));
109
110 // Invoke
111 EXPECT_TRUE(DT->dominates(Y3, Y4));
112 EXPECT_FALSE(DT->dominates(Y3, Y5));
113
114 // Phi
115 EXPECT_TRUE(DT->dominates(Y2, Y9));
116 EXPECT_FALSE(DT->dominates(Y3, Y9));
117 EXPECT_FALSE(DT->dominates(Y8, Y9));
118
119 // Anything dominates unreachable
120 EXPECT_TRUE(DT->dominates(Y1, Y6));
121 EXPECT_TRUE(DT->dominates(Y3, Y6));
122
123 // Unreachable doesn't dominate reachable
124 EXPECT_FALSE(DT->dominates(Y6, Y1));
125
126 // Instruction, BB dominance
127 EXPECT_FALSE(DT->dominates(Y1, BB0));
128 EXPECT_TRUE(DT->dominates(Y1, BB1));
129 EXPECT_TRUE(DT->dominates(Y1, BB2));
130 EXPECT_TRUE(DT->dominates(Y1, BB3));
131 EXPECT_TRUE(DT->dominates(Y1, BB4));
132
133 EXPECT_FALSE(DT->dominates(Y3, BB0));
134 EXPECT_TRUE(DT->dominates(Y3, BB1));
135 EXPECT_FALSE(DT->dominates(Y3, BB2));
136 EXPECT_TRUE(DT->dominates(Y3, BB3));
137 EXPECT_FALSE(DT->dominates(Y3, BB4));
138
139 EXPECT_TRUE(DT->dominates(Y6, BB3));
140
141 return false;
142 }
143 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
144 AU.addRequired();
145 }
146 DPass() : FunctionPass(ID) {
147 initializeDPassPass(*PassRegistry::getPassRegistry());
148 }
149 };
150 char DPass::ID = 0;
151
152
153 Module* makeLLVMModule(DPass *P) {
154 const char *ModuleStrig =
155 "declare i32 @g()\n" \
156 "define void @f(i32 %x) {\n" \
157 "bb0:\n" \
158 " %y1 = add i32 %x, 1\n" \
159 " %y2 = add i32 %x, 1\n" \
160 " %y3 = invoke i32 @g() to label %bb1 unwind label %bb2\n" \
161 "bb1:\n" \
162 " %y4 = add i32 %x, 1\n" \
163 " br label %bb4\n" \
164 "bb2:\n" \
165 " %y5 = landingpad i32 personality i32 ()* @g\n" \
166 " cleanup\n" \
167 " br label %bb4\n" \
168 "bb3:\n" \
169 " %y6 = add i32 %x, 1\n" \
170 " %y7 = add i32 %x, 1\n" \
171 " ret void\n" \
172 "bb4:\n" \
173 " %y8 = phi i32 [0, %bb2], [%y4, %bb1]\n"
174 " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n"
175 " ret void\n" \
176 "}\n";
177 LLVMContext &C = getGlobalContext();
178 SMDiagnostic Err;
179 return ParseAssemblyString(ModuleStrig, NULL, Err, C);
180 }
181
182 TEST(DominatorTree, Unreachable) {
183 DPass *P = new DPass();
184 Module *M = makeLLVMModule(P);
185 PassManager Passes;
186 Passes.add(P);
187 Passes.run(*M);
188 }
189 }
190 }
191
192 INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
193 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
194 INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)
88
99 LEVEL = ../..
1010 TESTNAME = VMCore
11 LINK_COMPONENTS := core support target ipa
11 LINK_COMPONENTS := core support target ipa asmparser
1212
1313 include $(LEVEL)/Makefile.config
1414 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest