llvm.org GIT mirror llvm / 8915ae9
[Dominators] Reapply r306892, r306893, r306893. This reverts commit r306907 and reapplies the patches in the title. The patches used to make one of the CodeGen/ARM/2011-02-07-AntidepClobber.ll test to fail because of a missing null check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306919 91177308-0d34-0410-b5e6-96231b3b80d8 Jakub Kuderski 2 years ago
5 changed file(s) with 157 addition(s) and 77 deletion(s). Raw diff Collapse all Expand all
8585 private:
8686 DominatorTreeBase &DT;
8787 bool useLiveIn;
88 DenseMap DomLevels;
8988 const SmallPtrSetImpl *LiveInBlocks;
9089 const SmallPtrSetImpl *DefBlocks;
9190 };
6464
6565 NodeT *TheBB;
6666 DomTreeNodeBase *IDom;
67 unsigned Level;
6768 std::vector Children;
6869 mutable unsigned DFSNumIn = ~0;
6970 mutable unsigned DFSNumOut = ~0;
7071
7172 public:
72 DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom) : TheBB(BB), IDom(iDom) {}
73 DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
74 : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
7375
7476 using iterator = typename std::vector::iterator;
7577 using const_iterator =
8284
8385 NodeT *getBlock() const { return TheBB; }
8486 DomTreeNodeBase *getIDom() const { return IDom; }
87 unsigned getLevel() const { return Level; }
8588
8689 const std::vector &getChildren() const { return Children; }
8790
99102 if (getNumChildren() != Other->getNumChildren())
100103 return true;
101104
105 if (Level != Other->Level) return true;
106
102107 SmallPtrSet OtherChildren;
103108 for (const DomTreeNodeBase *I : *Other) {
104109 const NodeT *Nd = I->getBlock();
115120
116121 void setIDom(DomTreeNodeBase *NewIDom) {
117122 assert(IDom && "No immediate dominator?");
118 if (IDom != NewIDom) {
119 typename std::vector::iterator I =
120 find(IDom->Children, this);
121 assert(I != IDom->Children.end() &&
122 "Not in immediate dominator children set!");
123 // I am no longer your child...
124 IDom->Children.erase(I);
125
126 // Switch to new dominator
127 IDom = NewIDom;
128 IDom->Children.push_back(this);
129 }
123 if (IDom == NewIDom) return;
124
125 auto I = find(IDom->Children, this);
126 assert(I != IDom->Children.end() &&
127 "Not in immediate dominator children set!");
128 // I am no longer your child...
129 IDom->Children.erase(I);
130
131 // Switch to new dominator
132 IDom = NewIDom;
133 IDom->Children.push_back(this);
134
135 UpdateLevel();
130136 }
131137
132138 /// getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes
142148 return this->DFSNumIn >= other->DFSNumIn &&
143149 this->DFSNumOut <= other->DFSNumOut;
144150 }
151
152 void UpdateLevel() {
153 assert(IDom);
154 if (Level == IDom->Level + 1) return;
155
156 SmallVector WorkStack = {this};
157
158 while (!WorkStack.empty()) {
159 DomTreeNodeBase *Current = WorkStack.pop_back_val();
160 Current->Level = Current->IDom->Level + 1;
161
162 for (DomTreeNodeBase *C : *Current) {
163 assert(C->IDom);
164 if (C->Level != C->IDom->Level + 1) WorkStack.push_back(C);
165 }
166 }
167 }
145168 };
146169
147170 template
151174 else
152175 O << " <>";
153176
154 O << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}";
155
156 return O << "\n";
177 O << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "} ["
178 << Node->getLevel() << "]\n";
179
180 return O;
157181 }
158182
159183 template
344368 if (!isReachableFromEntry(A))
345369 return false;
346370
371 if (B->getIDom() == A) return true;
372
373 if (A->getIDom() == B) return false;
374
375 // A can only dominate B if it is higher in the tree.
376 if (A->getLevel() >= B->getLevel()) return false;
377
347378 // Compare the result of the tree walk and the dfs numbers, if expensive
348379 // checks are enabled.
349380 #ifdef EXPENSIVE_CHECKS
375406
376407 /// findNearestCommonDominator - Find nearest common dominator basic block
377408 /// for basic block A and B. If there is no such block then return NULL.
378 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) {
409 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) const {
379410 assert(A->getParent() == B->getParent() &&
380411 "Two blocks are not in same function");
381412
387418 return &Entry;
388419 }
389420
390 // If B dominates A then B is nearest common dominator.
391 if (dominates(B, A))
392 return B;
393
394 // If A dominates B then A is nearest common dominator.
395 if (dominates(A, B))
396 return A;
397
398421 DomTreeNodeBase *NodeA = getNode(A);
399422 DomTreeNodeBase *NodeB = getNode(B);
400423
401 // If we have DFS info, then we can avoid all allocations by just querying
402 // it from each IDom. Note that because we call 'dominates' twice above, we
403 // expect to call through this code at most 16 times in a row without
404 // building valid DFS information. This is important as below is a *very*
405 // slow tree walk.
406 if (DFSInfoValid) {
407 DomTreeNodeBase *IDomA = NodeA->getIDom();
408 while (IDomA) {
409 if (NodeB->DominatedBy(IDomA))
410 return IDomA->getBlock();
411 IDomA = IDomA->getIDom();
412 }
413 return nullptr;
414 }
415
416 // Collect NodeA dominators set.
417 SmallPtrSet *, 16> NodeADoms;
418 NodeADoms.insert(NodeA);
419 DomTreeNodeBase *IDomA = NodeA->getIDom();
420 while (IDomA) {
421 NodeADoms.insert(IDomA);
422 IDomA = IDomA->getIDom();
423 }
424
425 // Walk NodeB immediate dominators chain and find common dominator node.
426 DomTreeNodeBase *IDomB = NodeB->getIDom();
427 while (IDomB) {
428 if (NodeADoms.count(IDomB) != 0)
429 return IDomB->getBlock();
430
431 IDomB = IDomB->getIDom();
432 }
433
434 return nullptr;
435 }
436
437 const NodeT *findNearestCommonDominator(const NodeT *A, const NodeT *B) {
424 if (!NodeA || !NodeB) return nullptr;
425
426 // Use level information to go up the tree until the levels match. Then
427 // continue going up til we arrive at the same node.
428 while (NodeA && NodeA != NodeB) {
429 if (NodeA->getLevel() < NodeB->getLevel()) std::swap(NodeA, NodeB);
430
431 NodeA = NodeA->IDom;
432 }
433
434 return NodeA ? NodeA->getBlock() : nullptr;
435 }
436
437 const NodeT *findNearestCommonDominator(const NodeT *A,
438 const NodeT *B) const {
438439 // Cast away the const qualifiers here. This is ok since
439440 // const is re-introduced on the return type.
440441 return findNearestCommonDominator(const_cast(A),
480481 } else {
481482 assert(Roots.size() == 1);
482483 NodeT *OldRoot = Roots.front();
483 DomTreeNodes[OldRoot] =
484 NewNode->addChild(std::move(DomTreeNodes[OldRoot]));
484 auto &OldNode = DomTreeNodes[OldRoot];
485 OldNode = NewNode->addChild(std::move(DomTreeNodes[OldRoot]));
486 OldNode->IDom = NewNode;
487 OldNode->UpdateLevel();
485488 Roots[0] = BB;
486489 }
487490 return RootNode = NewNode;
279279 }
280280
281281 void doFullDFSWalk(const DomTreeT &DT) {
282 NumToNode.push_back(nullptr);
282283 unsigned Num = 0;
283284 for (auto *Root : DT.Roots)
284285 if (!DT.isPostDominator())
316317
317318 return true;
318319 }
320
321 // Check if for every parent with a level L in the tree all of its children
322 // have level L + 1.
323 static bool VerifyLevels(const DomTreeT &DT) {
324 for (auto &NodeToTN : DT.DomTreeNodes) {
325 const TreeNodePtr TN = NodeToTN.second.get();
326 const NodePtr BB = TN->getBlock();
327 if (!BB) continue;
328
329 const TreeNodePtr IDom = TN->getIDom();
330 if (!IDom && TN->getLevel() != 0) {
331 errs() << "Node without an IDom ";
332 PrintBlockOrNullptr(errs(), BB);
333 errs() << " has a nonzero level " << TN->getLevel() << "!\n";
334 errs().flush();
335
336 return false;
337 }
338
339 if (IDom && TN->getLevel() != IDom->getLevel() + 1) {
340 errs() << "Node ";
341 PrintBlockOrNullptr(errs(), BB);
342 errs() << " has level " << TN->getLevel() << " while it's IDom ";
343 PrintBlockOrNullptr(errs(), IDom->getBlock());
344 errs() << " has level " << IDom->getLevel() << "!\n";
345 errs().flush();
346
347 return false;
348 }
349 }
350
351 return true;
352 }
353
354 // Checks if for every edge From -> To in the graph
355 // NCD(From, To) == IDom(To) or To.
356 bool verifyNCD(const DomTreeT &DT) {
357 clear();
358 doFullDFSWalk(DT);
359
360 for (auto &BlockToInfo : NodeToInfo) {
361 auto &Info = BlockToInfo.second;
362
363 const NodePtr From = NumToNode[Info.Parent];
364 if (!From) continue;
365
366 const NodePtr To = BlockToInfo.first;
367 const TreeNodePtr ToTN = DT.getNode(To);
368 assert(ToTN);
369
370 const NodePtr NCD = DT.findNearestCommonDominator(From, To);
371 const TreeNodePtr NCDTN = NCD ? DT.getNode(NCD) : nullptr;
372 const TreeNodePtr ToIDom = ToTN->getIDom();
373 if (NCDTN != ToTN && NCDTN != ToIDom) {
374 errs() << "NearestCommonDominator verification failed:\n\tNCD(From:";
375 PrintBlockOrNullptr(errs(), From);
376 errs() << ", To:";
377 PrintBlockOrNullptr(errs(), To);
378 errs() << ") = ";
379 PrintBlockOrNullptr(errs(), NCD);
380 errs() << ",\t (should be To or IDom[To]: ";
381 PrintBlockOrNullptr(errs(), ToIDom ? ToIDom->getBlock() : nullptr);
382 errs() << ")\n";
383 errs().flush();
384
385 return false;
386 }
387 }
388
389 return true;
390 }
391
319392 // The below routines verify the correctness of the dominator tree relative to
320393 // the CFG it's coming from. A tree is a dominator tree iff it has two
321394 // properties, called the parent property and the sibling property. Tarjan
440513 "NodePtr should be a pointer type");
441514 SemiNCAInfo::type> SNCA;
442515
443 return SNCA.verifyReachability(DT) && SNCA.verifyParentProperty(DT) &&
516 return SNCA.verifyReachability(DT) && SNCA.VerifyLevels(DT) &&
517 SNCA.verifyNCD(DT) && SNCA.verifyParentProperty(DT) &&
444518 SNCA.verifySiblingProperty(DT);
445
446519 }
447520
448521 } // namespace DomTreeBuilder
1919 template
2020 void IDFCalculator::calculate(
2121 SmallVectorImpl &PHIBlocks) {
22 // If we haven't computed dominator tree levels, do so now.
23 if (DomLevels.empty()) {
24 for (auto DFI = df_begin(DT.getRootNode()), DFE = df_end(DT.getRootNode());
25 DFI != DFE; ++DFI) {
26 DomLevels[*DFI] = DFI.getPathLength() - 1;
27 }
28 }
29
3022 // Use a priority queue keyed on dominator tree level so that inserted nodes
3123 // are handled from the bottom of the dominator tree upwards.
3224 typedef std::pair DomTreeNodePair;
3628
3729 for (BasicBlock *BB : *DefBlocks) {
3830 if (DomTreeNode *Node = DT.getNode(BB))
39 PQ.push(std::make_pair(Node, DomLevels.lookup(Node)));
31 PQ.push({Node, Node->getLevel()});
4032 }
4133
4234 SmallVector Worklist;
7163 if (SuccNode->getIDom() == Node)
7264 continue;
7365
74 unsigned SuccLevel = DomLevels.lookup(SuccNode);
66 const unsigned SuccLevel = SuccNode->getLevel();
7567 if (SuccLevel > RootLevel)
7668 continue;
7769
229229 EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 3UL);
230230 EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 4UL);
231231
232 // Check levels before
233 EXPECT_EQ(DT->getNode(BB0)->getLevel(), 0U);
234 EXPECT_EQ(DT->getNode(BB1)->getLevel(), 1U);
235 EXPECT_EQ(DT->getNode(BB2)->getLevel(), 1U);
236 EXPECT_EQ(DT->getNode(BB4)->getLevel(), 1U);
237
232238 // Reattach block 3 to block 1 and recalculate
233239 BB1->getTerminator()->eraseFromParent();
234240 BranchInst::Create(BB4, BB3, ConstantInt::getTrue(F.getContext()), BB1);
246252 EXPECT_EQ(DT->getNode(BB3)->getDFSNumOut(), 3UL);
247253 EXPECT_EQ(DT->getNode(BB4)->getDFSNumIn(), 5UL);
248254 EXPECT_EQ(DT->getNode(BB4)->getDFSNumOut(), 6UL);
255
256 // Check levels after
257 EXPECT_EQ(DT->getNode(BB0)->getLevel(), 0U);
258 EXPECT_EQ(DT->getNode(BB1)->getLevel(), 1U);
259 EXPECT_EQ(DT->getNode(BB2)->getLevel(), 1U);
260 EXPECT_EQ(DT->getNode(BB3)->getLevel(), 2U);
261 EXPECT_EQ(DT->getNode(BB4)->getLevel(), 1U);
249262
250263 // Change root node
251264 DT->verifyDomTree();