llvm.org GIT mirror llvm / ad7d2c0
Rearrange Dom unittest to accommodate multiple tests I've taken the approach from the LoopInfo test: * Rather than running in the pass manager just build the analyses manually * Split out the common parts (makeLLVMModule, runWithDomTree) into helpers Differential Revision: https://reviews.llvm.org/D33617 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304061 91177308-0d34-0410-b5e6-96231b3b80d8 Adam Nemet 2 years ago
1 changed file(s) with 215 addition(s) and 225 deletion(s). Raw diff Collapse all Expand all
1212 #include "llvm/IR/Dominators.h"
1313 #include "llvm/IR/Instructions.h"
1414 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/LegacyPassManager.h"
1615 #include "llvm/IR/Module.h"
1716 #include "llvm/Support/SourceMgr.h"
1817 #include "gtest/gtest.h"
1918
2019 using namespace llvm;
2120
22 namespace llvm {
23 void initializeDPassPass(PassRegistry &);
24
25 namespace {
26 struct DPass : public FunctionPass {
27 static char ID;
28 bool runOnFunction(Function &F) override {
29 DominatorTree *DT = &getAnalysis().getDomTree();
30 PostDominatorTree *PDT =
31 &getAnalysis().getPostDomTree();
32 Function::iterator FI = F.begin();
33
34 BasicBlock *BB0 = &*FI++;
35 BasicBlock::iterator BBI = BB0->begin();
36 Instruction *Y1 = &*BBI++;
37 Instruction *Y2 = &*BBI++;
38 Instruction *Y3 = &*BBI++;
39
40 BasicBlock *BB1 = &*FI++;
41 BBI = BB1->begin();
42 Instruction *Y4 = &*BBI++;
43
44 BasicBlock *BB2 = &*FI++;
45 BBI = BB2->begin();
46 Instruction *Y5 = &*BBI++;
47
48 BasicBlock *BB3 = &*FI++;
49 BBI = BB3->begin();
50 Instruction *Y6 = &*BBI++;
51 Instruction *Y7 = &*BBI++;
52
53 BasicBlock *BB4 = &*FI++;
54 BBI = BB4->begin();
55 Instruction *Y8 = &*BBI++;
56 Instruction *Y9 = &*BBI++;
57
58 // Reachability
59 EXPECT_TRUE(DT->isReachableFromEntry(BB0));
60 EXPECT_TRUE(DT->isReachableFromEntry(BB1));
61 EXPECT_TRUE(DT->isReachableFromEntry(BB2));
62 EXPECT_FALSE(DT->isReachableFromEntry(BB3));
63 EXPECT_TRUE(DT->isReachableFromEntry(BB4));
64
65 // BB dominance
66 EXPECT_TRUE(DT->dominates(BB0, BB0));
67 EXPECT_TRUE(DT->dominates(BB0, BB1));
68 EXPECT_TRUE(DT->dominates(BB0, BB2));
69 EXPECT_TRUE(DT->dominates(BB0, BB3));
70 EXPECT_TRUE(DT->dominates(BB0, BB4));
71
72 EXPECT_FALSE(DT->dominates(BB1, BB0));
73 EXPECT_TRUE(DT->dominates(BB1, BB1));
74 EXPECT_FALSE(DT->dominates(BB1, BB2));
75 EXPECT_TRUE(DT->dominates(BB1, BB3));
76 EXPECT_FALSE(DT->dominates(BB1, BB4));
77
78 EXPECT_FALSE(DT->dominates(BB2, BB0));
79 EXPECT_FALSE(DT->dominates(BB2, BB1));
80 EXPECT_TRUE(DT->dominates(BB2, BB2));
81 EXPECT_TRUE(DT->dominates(BB2, BB3));
82 EXPECT_FALSE(DT->dominates(BB2, BB4));
83
84 EXPECT_FALSE(DT->dominates(BB3, BB0));
85 EXPECT_FALSE(DT->dominates(BB3, BB1));
86 EXPECT_FALSE(DT->dominates(BB3, BB2));
87 EXPECT_TRUE(DT->dominates(BB3, BB3));
88 EXPECT_FALSE(DT->dominates(BB3, BB4));
89
90 // BB proper dominance
91 EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
92 EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
93 EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
94 EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
95
96 EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
97 EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
98 EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
99 EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
100
101 EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
102 EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
103 EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
104 EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
105
106 EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
107 EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
108 EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
109 EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
110
111 // Instruction dominance in the same reachable BB
112 EXPECT_FALSE(DT->dominates(Y1, Y1));
113 EXPECT_TRUE(DT->dominates(Y1, Y2));
114 EXPECT_FALSE(DT->dominates(Y2, Y1));
115 EXPECT_FALSE(DT->dominates(Y2, Y2));
116
117 // Instruction dominance in the same unreachable BB
118 EXPECT_TRUE(DT->dominates(Y6, Y6));
119 EXPECT_TRUE(DT->dominates(Y6, Y7));
120 EXPECT_TRUE(DT->dominates(Y7, Y6));
121 EXPECT_TRUE(DT->dominates(Y7, Y7));
122
123 // Invoke
124 EXPECT_TRUE(DT->dominates(Y3, Y4));
125 EXPECT_FALSE(DT->dominates(Y3, Y5));
126
127 // Phi
128 EXPECT_TRUE(DT->dominates(Y2, Y9));
129 EXPECT_FALSE(DT->dominates(Y3, Y9));
130 EXPECT_FALSE(DT->dominates(Y8, Y9));
131
132 // Anything dominates unreachable
133 EXPECT_TRUE(DT->dominates(Y1, Y6));
134 EXPECT_TRUE(DT->dominates(Y3, Y6));
135
136 // Unreachable doesn't dominate reachable
137 EXPECT_FALSE(DT->dominates(Y6, Y1));
138
139 // Instruction, BB dominance
140 EXPECT_FALSE(DT->dominates(Y1, BB0));
141 EXPECT_TRUE(DT->dominates(Y1, BB1));
142 EXPECT_TRUE(DT->dominates(Y1, BB2));
143 EXPECT_TRUE(DT->dominates(Y1, BB3));
144 EXPECT_TRUE(DT->dominates(Y1, BB4));
145
146 EXPECT_FALSE(DT->dominates(Y3, BB0));
147 EXPECT_TRUE(DT->dominates(Y3, BB1));
148 EXPECT_FALSE(DT->dominates(Y3, BB2));
149 EXPECT_TRUE(DT->dominates(Y3, BB3));
150 EXPECT_FALSE(DT->dominates(Y3, BB4));
151
152 EXPECT_TRUE(DT->dominates(Y6, BB3));
153
154 // Post dominance.
155 EXPECT_TRUE(PDT->dominates(BB0, BB0));
156 EXPECT_FALSE(PDT->dominates(BB1, BB0));
157 EXPECT_FALSE(PDT->dominates(BB2, BB0));
158 EXPECT_FALSE(PDT->dominates(BB3, BB0));
159 EXPECT_TRUE(PDT->dominates(BB4, BB1));
160
161 // Dominance descendants.
162 SmallVector DominatedBBs, PostDominatedBBs;
163
164 DT->getDescendants(BB0, DominatedBBs);
165 PDT->getDescendants(BB0, PostDominatedBBs);
166 EXPECT_EQ(DominatedBBs.size(), 4UL);
167 EXPECT_EQ(PostDominatedBBs.size(), 1UL);
168
169 // BB3 is unreachable. It should have no dominators nor postdominators.
170 DominatedBBs.clear();
171 PostDominatedBBs.clear();
172 DT->getDescendants(BB3, DominatedBBs);
173 DT->getDescendants(BB3, PostDominatedBBs);
174 EXPECT_EQ(DominatedBBs.size(), 0UL);
175 EXPECT_EQ(PostDominatedBBs.size(), 0UL);
176
177 // Check DFS Numbers before
178 EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
179 EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 7UL);
180 EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
181 EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 2UL);
182 EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 5UL);
183 EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 6UL);
184 EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 3UL);
185 EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 4UL);
186
187 // Reattach block 3 to block 1 and recalculate
188 BB1->getTerminator()->eraseFromParent();
189 BranchInst::Create(BB4, BB3, ConstantInt::getTrue(F.getContext()), BB1);
190 DT->recalculate(F);
191
192 // Check DFS Numbers after
193 EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
194 EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 9UL);
195 EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
196 EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 4UL);
197 EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 7UL);
198 EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 8UL);
199 EXPECT_EQ(DT->getNode(BB3)->getDFSNumIn(), 2UL);
200 EXPECT_EQ(DT->getNode(BB3)->getDFSNumOut(), 3UL);
201 EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 5UL);
202 EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 6UL);
203
204 // Change root node
205 DT->verifyDomTree();
206 BasicBlock *NewEntry =
207 BasicBlock::Create(F.getContext(), "new_entry", &F, BB0);
208 BranchInst::Create(BB0, NewEntry);
209 EXPECT_EQ(F.begin()->getName(), NewEntry->getName());
210 EXPECT_TRUE(&F.getEntryBlock() == NewEntry);
211 DT->setNewRoot(NewEntry);
212 DT->verifyDomTree();
213
214 return false;
215 }
216 void getAnalysisUsage(AnalysisUsage &AU) const override {
217 AU.addRequired();
218 AU.addRequired();
219 }
220 DPass() : FunctionPass(ID) {
221 initializeDPassPass(*PassRegistry::getPassRegistry());
222 }
223 };
224 char DPass::ID = 0;
225
226 std::unique_ptr makeLLVMModule(LLVMContext &Context, DPass *P) {
227 const char *ModuleString =
21 /// Build the dominator tree for the function and run the Test.
22 static void
23 runWithDomTree(Module &M, StringRef FuncName,
24 function_ref
25 DominatorTreeBase *PDT)>
26 Test) {
27 auto *F = M.getFunction(FuncName);
28 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
29 // Compute the dominator tree for the function.
30 DominatorTree DT(*F);
31 DominatorTreeBase PDT(/*isPostDom*/ true);
32 PDT.recalculate(*F);
33 Test(*F, &DT, &PDT);
34 }
35
36 static std::unique_ptr makeLLVMModule(LLVMContext &Context,
37 StringRef ModuleStr) {
38 SMDiagnostic Err;
39 std::unique_ptr M = parseAssemblyString(ModuleStr, Err, Context);
40 assert(M && "Bad assembly?");
41 return M;
42 }
43
44 TEST(DominatorTree, Unreachable) {
45 StringRef ModuleString =
22846 "declare i32 @g()\n"
22947 "define void @f(i32 %x) personality i32 ()* @g {\n"
23048 "bb0:\n"
24765 " %y9 = phi i32 [0, %bb2], [%y4, %bb1]\n"
24866 " ret void\n"
24967 "}\n";
250 SMDiagnostic Err;
251 return parseAssemblyString(ModuleString, Err, Context);
68
69 // Parse the module.
70 LLVMContext Context;
71 std::unique_ptr M = makeLLVMModule(Context, ModuleString);
72
73 runWithDomTree(
74 *M, "f",
75 [&](Function &F, DominatorTree *DT, DominatorTreeBase *PDT) {
76 Function::iterator FI = F.begin();
77
78 BasicBlock *BB0 = &*FI++;
79 BasicBlock::iterator BBI = BB0->begin();
80 Instruction *Y1 = &*BBI++;
81 Instruction *Y2 = &*BBI++;
82 Instruction *Y3 = &*BBI++;
83
84 BasicBlock *BB1 = &*FI++;
85 BBI = BB1->begin();
86 Instruction *Y4 = &*BBI++;
87
88 BasicBlock *BB2 = &*FI++;
89 BBI = BB2->begin();
90 Instruction *Y5 = &*BBI++;
91
92 BasicBlock *BB3 = &*FI++;
93 BBI = BB3->begin();
94 Instruction *Y6 = &*BBI++;
95 Instruction *Y7 = &*BBI++;
96
97 BasicBlock *BB4 = &*FI++;
98 BBI = BB4->begin();
99 Instruction *Y8 = &*BBI++;
100 Instruction *Y9 = &*BBI++;
101
102 // Reachability
103 EXPECT_TRUE(DT->isReachableFromEntry(BB0));
104 EXPECT_TRUE(DT->isReachableFromEntry(BB1));
105 EXPECT_TRUE(DT->isReachableFromEntry(BB2));
106 EXPECT_FALSE(DT->isReachableFromEntry(BB3));
107 EXPECT_TRUE(DT->isReachableFromEntry(BB4));
108
109 // BB dominance
110 EXPECT_TRUE(DT->dominates(BB0, BB0));
111 EXPECT_TRUE(DT->dominates(BB0, BB1));
112 EXPECT_TRUE(DT->dominates(BB0, BB2));
113 EXPECT_TRUE(DT->dominates(BB0, BB3));
114 EXPECT_TRUE(DT->dominates(BB0, BB4));
115
116 EXPECT_FALSE(DT->dominates(BB1, BB0));
117 EXPECT_TRUE(DT->dominates(BB1, BB1));
118 EXPECT_FALSE(DT->dominates(BB1, BB2));
119 EXPECT_TRUE(DT->dominates(BB1, BB3));
120 EXPECT_FALSE(DT->dominates(BB1, BB4));
121
122 EXPECT_FALSE(DT->dominates(BB2, BB0));
123 EXPECT_FALSE(DT->dominates(BB2, BB1));
124 EXPECT_TRUE(DT->dominates(BB2, BB2));
125 EXPECT_TRUE(DT->dominates(BB2, BB3));
126 EXPECT_FALSE(DT->dominates(BB2, BB4));
127
128 EXPECT_FALSE(DT->dominates(BB3, BB0));
129 EXPECT_FALSE(DT->dominates(BB3, BB1));
130 EXPECT_FALSE(DT->dominates(BB3, BB2));
131 EXPECT_TRUE(DT->dominates(BB3, BB3));
132 EXPECT_FALSE(DT->dominates(BB3, BB4));
133
134 // BB proper dominance
135 EXPECT_FALSE(DT->properlyDominates(BB0, BB0));
136 EXPECT_TRUE(DT->properlyDominates(BB0, BB1));
137 EXPECT_TRUE(DT->properlyDominates(BB0, BB2));
138 EXPECT_TRUE(DT->properlyDominates(BB0, BB3));
139
140 EXPECT_FALSE(DT->properlyDominates(BB1, BB0));
141 EXPECT_FALSE(DT->properlyDominates(BB1, BB1));
142 EXPECT_FALSE(DT->properlyDominates(BB1, BB2));
143 EXPECT_TRUE(DT->properlyDominates(BB1, BB3));
144
145 EXPECT_FALSE(DT->properlyDominates(BB2, BB0));
146 EXPECT_FALSE(DT->properlyDominates(BB2, BB1));
147 EXPECT_FALSE(DT->properlyDominates(BB2, BB2));
148 EXPECT_TRUE(DT->properlyDominates(BB2, BB3));
149
150 EXPECT_FALSE(DT->properlyDominates(BB3, BB0));
151 EXPECT_FALSE(DT->properlyDominates(BB3, BB1));
152 EXPECT_FALSE(DT->properlyDominates(BB3, BB2));
153 EXPECT_FALSE(DT->properlyDominates(BB3, BB3));
154
155 // Instruction dominance in the same reachable BB
156 EXPECT_FALSE(DT->dominates(Y1, Y1));
157 EXPECT_TRUE(DT->dominates(Y1, Y2));
158 EXPECT_FALSE(DT->dominates(Y2, Y1));
159 EXPECT_FALSE(DT->dominates(Y2, Y2));
160
161 // Instruction dominance in the same unreachable BB
162 EXPECT_TRUE(DT->dominates(Y6, Y6));
163 EXPECT_TRUE(DT->dominates(Y6, Y7));
164 EXPECT_TRUE(DT->dominates(Y7, Y6));
165 EXPECT_TRUE(DT->dominates(Y7, Y7));
166
167 // Invoke
168 EXPECT_TRUE(DT->dominates(Y3, Y4));
169 EXPECT_FALSE(DT->dominates(Y3, Y5));
170
171 // Phi
172 EXPECT_TRUE(DT->dominates(Y2, Y9));
173 EXPECT_FALSE(DT->dominates(Y3, Y9));
174 EXPECT_FALSE(DT->dominates(Y8, Y9));
175
176 // Anything dominates unreachable
177 EXPECT_TRUE(DT->dominates(Y1, Y6));
178 EXPECT_TRUE(DT->dominates(Y3, Y6));
179
180 // Unreachable doesn't dominate reachable
181 EXPECT_FALSE(DT->dominates(Y6, Y1));
182
183 // Instruction, BB dominance
184 EXPECT_FALSE(DT->dominates(Y1, BB0));
185 EXPECT_TRUE(DT->dominates(Y1, BB1));
186 EXPECT_TRUE(DT->dominates(Y1, BB2));
187 EXPECT_TRUE(DT->dominates(Y1, BB3));
188 EXPECT_TRUE(DT->dominates(Y1, BB4));
189
190 EXPECT_FALSE(DT->dominates(Y3, BB0));
191 EXPECT_TRUE(DT->dominates(Y3, BB1));
192 EXPECT_FALSE(DT->dominates(Y3, BB2));
193 EXPECT_TRUE(DT->dominates(Y3, BB3));
194 EXPECT_FALSE(DT->dominates(Y3, BB4));
195
196 EXPECT_TRUE(DT->dominates(Y6, BB3));
197
198 // Post dominance.
199 EXPECT_TRUE(PDT->dominates(BB0, BB0));
200 EXPECT_FALSE(PDT->dominates(BB1, BB0));
201 EXPECT_FALSE(PDT->dominates(BB2, BB0));
202 EXPECT_FALSE(PDT->dominates(BB3, BB0));
203 EXPECT_TRUE(PDT->dominates(BB4, BB1));
204
205 // Dominance descendants.
206 SmallVector DominatedBBs, PostDominatedBBs;
207
208 DT->getDescendants(BB0, DominatedBBs);
209 PDT->getDescendants(BB0, PostDominatedBBs);
210 EXPECT_EQ(DominatedBBs.size(), 4UL);
211 EXPECT_EQ(PostDominatedBBs.size(), 1UL);
212
213 // BB3 is unreachable. It should have no dominators nor postdominators.
214 DominatedBBs.clear();
215 PostDominatedBBs.clear();
216 DT->getDescendants(BB3, DominatedBBs);
217 DT->getDescendants(BB3, PostDominatedBBs);
218 EXPECT_EQ(DominatedBBs.size(), 0UL);
219 EXPECT_EQ(PostDominatedBBs.size(), 0UL);
220
221 // Check DFS Numbers before
222 EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
223 EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 7UL);
224 EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
225 EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 2UL);
226 EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 5UL);
227 EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 6UL);
228 EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 3UL);
229 EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 4UL);
230
231 // Reattach block 3 to block 1 and recalculate
232 BB1->getTerminator()->eraseFromParent();
233 BranchInst::Create(BB4, BB3, ConstantInt::getTrue(F.getContext()), BB1);
234 DT->recalculate(F);
235
236 // Check DFS Numbers after
237 EXPECT_EQ(DT->getNode(BB0)->getDFSNumIn(), 0UL);
238 EXPECT_EQ(DT->getNode(BB0)->getDFSNumOut(), 9UL);
239 EXPECT_EQ(DT->getNode(BB1)->getDFSNumIn(), 1UL);
240 EXPECT_EQ(DT->getNode(BB1)->getDFSNumOut(), 4UL);
241 EXPECT_EQ(DT->getNode(BB2)->getDFSNumIn(), 7UL);
242 EXPECT_EQ(DT->getNode(BB2)->getDFSNumOut(), 8UL);
243 EXPECT_EQ(DT->getNode(BB3)->getDFSNumIn(), 2UL);
244 EXPECT_EQ(DT->getNode(BB3)->getDFSNumOut(), 3UL);
245 EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 5UL);
246 EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 6UL);
247
248 // Change root node
249 DT->verifyDomTree();
250 BasicBlock *NewEntry =
251 BasicBlock::Create(F.getContext(), "new_entry", &F, BB0);
252 BranchInst::Create(BB0, NewEntry);
253 EXPECT_EQ(F.begin()->getName(), NewEntry->getName());
254 EXPECT_TRUE(&F.getEntryBlock() == NewEntry);
255 DT->setNewRoot(NewEntry);
256 DT->verifyDomTree();
257 });
252258 }
253
254 TEST(DominatorTree, Unreachable) {
255 DPass *P = new DPass();
256 LLVMContext Context;
257 std::unique_ptr M = makeLLVMModule(Context, P);
258 legacy::PassManager Passes;
259 Passes.add(P);
260 Passes.run(*M);
261 }
262 } // namespace
263 } // namespace llvm
264
265 INITIALIZE_PASS_BEGIN(DPass, "dpass", "dpass", false, false)
266 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
267 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
268 INITIALIZE_PASS_END(DPass, "dpass", "dpass", false, false)