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