Owen Anderson | 9b8f34f | 2007-10-31 03:30:14 +0000 | [diff] [blame] | 1 | //===- MachineDominators.cpp - Machine Dominator Calculation --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 9b8f34f | 2007-10-31 03:30:14 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements simple dominator construction algorithms for finding |
| 11 | // forward dominators on machine functions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/MachineDominators.h" |
Bill Wendling | 0c20943 | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Benjamin Kramer | 012b151 | 2015-02-27 23:13:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallBitVector.h" |
Chad Rosier | fd342808 | 2016-06-24 13:32:22 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Owen Anderson | 9b8f34f | 2007-10-31 03:30:14 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
Chad Rosier | e2185fd | 2016-06-24 17:15:04 +0000 | [diff] [blame] | 22 | // Always verify dominfo if expensive checking is enabled. |
Chad Rosier | fd342808 | 2016-06-24 13:32:22 +0000 | [diff] [blame] | 23 | #ifdef EXPENSIVE_CHECKS |
Chad Rosier | e2185fd | 2016-06-24 17:15:04 +0000 | [diff] [blame] | 24 | static bool VerifyMachineDomInfo = true; |
Chad Rosier | fd342808 | 2016-06-24 13:32:22 +0000 | [diff] [blame] | 25 | #else |
| 26 | static bool VerifyMachineDomInfo = false; |
| 27 | #endif |
| 28 | static cl::opt<bool, true> VerifyMachineDomInfoX( |
| 29 | "verify-machine-dom-info", cl::location(VerifyMachineDomInfo), |
| 30 | cl::desc("Verify machine dominator info (time consuming)")); |
| 31 | |
John McCall | 323c30c | 2009-12-16 00:13:24 +0000 | [diff] [blame] | 32 | namespace llvm { |
Benjamin Kramer | a667d1a | 2015-07-13 17:21:31 +0000 | [diff] [blame] | 33 | template class DomTreeNodeBase<MachineBasicBlock>; |
| 34 | template class DominatorTreeBase<MachineBasicBlock>; |
John McCall | 323c30c | 2009-12-16 00:13:24 +0000 | [diff] [blame] | 35 | } |
Owen Anderson | 9b8f34f | 2007-10-31 03:30:14 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 647e61a | 2008-01-05 20:15:42 +0000 | [diff] [blame] | 37 | char MachineDominatorTree::ID = 0; |
| 38 | |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 39 | INITIALIZE_PASS(MachineDominatorTree, "machinedomtree", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 40 | "MachineDominator Tree Construction", true, true) |
Bill Wendling | 0c20943 | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 41 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 42 | char &llvm::MachineDominatorsID = MachineDominatorTree::ID; |
Dan Gohman | 906152a | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 43 | |
| 44 | void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const { |
| 45 | AU.setPreservesAll(); |
| 46 | MachineFunctionPass::getAnalysisUsage(AU); |
| 47 | } |
| 48 | |
| 49 | bool MachineDominatorTree::runOnMachineFunction(MachineFunction &F) { |
Quentin Colombet | abea99f | 2014-08-13 21:00:07 +0000 | [diff] [blame] | 50 | CriticalEdgesToSplit.clear(); |
| 51 | NewBBs.clear(); |
Dan Gohman | 906152a | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 52 | DT->recalculate(F); |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | MachineDominatorTree::MachineDominatorTree() |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 58 | : MachineFunctionPass(ID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 59 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
Dan Gohman | 906152a | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 60 | DT = new DominatorTreeBase<MachineBasicBlock>(false); |
| 61 | } |
| 62 | |
| 63 | MachineDominatorTree::~MachineDominatorTree() { |
Dan Gohman | 906152a | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 64 | delete DT; |
| 65 | } |
| 66 | |
| 67 | void MachineDominatorTree::releaseMemory() { |
| 68 | DT->releaseMemory(); |
| 69 | } |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 70 | |
Chad Rosier | fd342808 | 2016-06-24 13:32:22 +0000 | [diff] [blame] | 71 | void MachineDominatorTree::verifyAnalysis() const { |
Serge Pavlov | ed5eb93 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 72 | if (VerifyMachineDomInfo) |
Chad Rosier | fd342808 | 2016-06-24 13:32:22 +0000 | [diff] [blame] | 73 | verifyDomTree(); |
| 74 | } |
| 75 | |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 76 | void MachineDominatorTree::print(raw_ostream &OS, const Module*) const { |
| 77 | DT->print(OS); |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 78 | } |
Benjamin Kramer | 012b151 | 2015-02-27 23:13:13 +0000 | [diff] [blame] | 79 | |
| 80 | void MachineDominatorTree::applySplitCriticalEdges() const { |
| 81 | // Bail out early if there is nothing to do. |
| 82 | if (CriticalEdgesToSplit.empty()) |
| 83 | return; |
| 84 | |
| 85 | // For each element in CriticalEdgesToSplit, remember whether or not element |
| 86 | // is the new immediate domminator of its successor. The mapping is done by |
| 87 | // index, i.e., the information for the ith element of CriticalEdgesToSplit is |
| 88 | // the ith element of IsNewIDom. |
| 89 | SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true); |
| 90 | size_t Idx = 0; |
| 91 | |
| 92 | // Collect all the dominance properties info, before invalidating |
| 93 | // the underlying DT. |
| 94 | for (CriticalEdge &Edge : CriticalEdgesToSplit) { |
| 95 | // Update dominator information. |
| 96 | MachineBasicBlock *Succ = Edge.ToBB; |
| 97 | MachineDomTreeNode *SuccDTNode = DT->getNode(Succ); |
| 98 | |
| 99 | for (MachineBasicBlock *PredBB : Succ->predecessors()) { |
| 100 | if (PredBB == Edge.NewBB) |
| 101 | continue; |
| 102 | // If we are in this situation: |
| 103 | // FromBB1 FromBB2 |
| 104 | // + + |
| 105 | // + + + + |
| 106 | // + + + + |
| 107 | // ... Split1 Split2 ... |
| 108 | // + + |
| 109 | // + + |
| 110 | // + |
| 111 | // Succ |
| 112 | // Instead of checking the domiance property with Split2, we check it with |
| 113 | // FromBB2 since Split2 is still unknown of the underlying DT structure. |
| 114 | if (NewBBs.count(PredBB)) { |
| 115 | assert(PredBB->pred_size() == 1 && "A basic block resulting from a " |
| 116 | "critical edge split has more " |
| 117 | "than one predecessor!"); |
| 118 | PredBB = *PredBB->pred_begin(); |
| 119 | } |
| 120 | if (!DT->dominates(SuccDTNode, DT->getNode(PredBB))) { |
| 121 | IsNewIDom[Idx] = false; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | ++Idx; |
| 126 | } |
| 127 | |
| 128 | // Now, update DT with the collected dominance properties info. |
| 129 | Idx = 0; |
| 130 | for (CriticalEdge &Edge : CriticalEdgesToSplit) { |
| 131 | // We know FromBB dominates NewBB. |
| 132 | MachineDomTreeNode *NewDTNode = DT->addNewBlock(Edge.NewBB, Edge.FromBB); |
| 133 | |
| 134 | // If all the other predecessors of "Succ" are dominated by "Succ" itself |
| 135 | // then the new block is the new immediate dominator of "Succ". Otherwise, |
| 136 | // the new block doesn't dominate anything. |
| 137 | if (IsNewIDom[Idx]) |
| 138 | DT->changeImmediateDominator(DT->getNode(Edge.ToBB), NewDTNode); |
| 139 | ++Idx; |
| 140 | } |
| 141 | NewBBs.clear(); |
| 142 | CriticalEdgesToSplit.clear(); |
| 143 | } |
Chad Rosier | fd342808 | 2016-06-24 13:32:22 +0000 | [diff] [blame] | 144 | |
| 145 | void MachineDominatorTree::verifyDomTree() const { |
| 146 | MachineFunction &F = *getRoot()->getParent(); |
| 147 | |
| 148 | MachineDominatorTree OtherDT; |
| 149 | OtherDT.DT->recalculate(F); |
| 150 | if (compare(OtherDT)) { |
| 151 | errs() << "MachineDominatorTree is not up to date!\nComputed:\n"; |
| 152 | print(errs(), nullptr); |
| 153 | errs() << "\nActual:\n"; |
| 154 | OtherDT.print(errs(), nullptr); |
| 155 | abort(); |
| 156 | } |
| 157 | } |