Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //==- llvm/CodeGen/MachineDominators.h - Machine Dom Calculation -*- C++ -*-==// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines classes mirroring those in llvm/Analysis/Dominators.h, |
| 10 | // but for target-specific code rather than target-independent IR. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H |
| 15 | #define LLVM_CODEGEN_MACHINEDOMINATORS_H |
| 16 | |
| 17 | #include "llvm/ADT/SmallSet.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | #include "llvm/CodeGen/MachineInstr.h" |
| 22 | #include "llvm/Support/GenericDomTree.h" |
| 23 | #include "llvm/Support/GenericDomTreeConstruction.h" |
| 24 | #include <cassert> |
| 25 | #include <memory> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | template <> |
| 30 | inline void DominatorTreeBase<MachineBasicBlock, false>::addRoot( |
| 31 | MachineBasicBlock *MBB) { |
| 32 | this->Roots.push_back(MBB); |
| 33 | } |
| 34 | |
| 35 | extern template class DomTreeNodeBase<MachineBasicBlock>; |
| 36 | extern template class DominatorTreeBase<MachineBasicBlock, false>; // DomTree |
| 37 | extern template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTree |
| 38 | |
| 39 | using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>; |
| 40 | |
| 41 | //===------------------------------------- |
| 42 | /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to |
| 43 | /// compute a normal dominator tree. |
| 44 | /// |
| 45 | class MachineDominatorTree : public MachineFunctionPass { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 46 | using DomTreeT = DomTreeBase<MachineBasicBlock>; |
| 47 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | /// Helper structure used to hold all the basic blocks |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | /// involved in the split of a critical edge. |
| 50 | struct CriticalEdge { |
| 51 | MachineBasicBlock *FromBB; |
| 52 | MachineBasicBlock *ToBB; |
| 53 | MachineBasicBlock *NewBB; |
| 54 | }; |
| 55 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 56 | /// Pile up all the critical edges to be split. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | /// The splitting of a critical edge is local and thus, it is possible |
| 58 | /// to apply several of those changes at the same time. |
| 59 | mutable SmallVector<CriticalEdge, 32> CriticalEdgesToSplit; |
| 60 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 61 | /// Remember all the basic blocks that are inserted during |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 62 | /// edge splitting. |
| 63 | /// Invariant: NewBBs == all the basic blocks contained in the NewBB |
| 64 | /// field of all the elements of CriticalEdgesToSplit. |
| 65 | /// I.e., forall elt in CriticalEdgesToSplit, it exists BB in NewBBs |
| 66 | /// such as BB == elt.NewBB. |
| 67 | mutable SmallSet<MachineBasicBlock *, 32> NewBBs; |
| 68 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 69 | /// The DominatorTreeBase that is used to compute a normal dominator tree. |
| 70 | std::unique_ptr<DomTreeT> DT; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 72 | /// Apply all the recorded critical edges to the DT. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | /// This updates the underlying DT information in a way that uses |
| 74 | /// the fast query path of DT as much as possible. |
| 75 | /// |
| 76 | /// \post CriticalEdgesToSplit.empty(). |
| 77 | void applySplitCriticalEdges() const; |
| 78 | |
| 79 | public: |
| 80 | static char ID; // Pass ID, replacement for typeid |
| 81 | |
| 82 | MachineDominatorTree(); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 83 | explicit MachineDominatorTree(MachineFunction &MF) : MachineFunctionPass(ID) { |
| 84 | calculate(MF); |
| 85 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 86 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 87 | DomTreeT &getBase() { |
| 88 | if (!DT) DT.reset(new DomTreeT()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | applySplitCriticalEdges(); |
| 90 | return *DT; |
| 91 | } |
| 92 | |
| 93 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 94 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 95 | MachineBasicBlock *getRoot() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 96 | applySplitCriticalEdges(); |
| 97 | return DT->getRoot(); |
| 98 | } |
| 99 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 100 | MachineDomTreeNode *getRootNode() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 101 | applySplitCriticalEdges(); |
| 102 | return DT->getRootNode(); |
| 103 | } |
| 104 | |
| 105 | bool runOnMachineFunction(MachineFunction &F) override; |
| 106 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 107 | void calculate(MachineFunction &F); |
| 108 | |
| 109 | bool dominates(const MachineDomTreeNode *A, |
| 110 | const MachineDomTreeNode *B) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 111 | applySplitCriticalEdges(); |
| 112 | return DT->dominates(A, B); |
| 113 | } |
| 114 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 115 | bool dominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 116 | applySplitCriticalEdges(); |
| 117 | return DT->dominates(A, B); |
| 118 | } |
| 119 | |
| 120 | // dominates - Return true if A dominates B. This performs the |
| 121 | // special checks necessary if A and B are in the same basic block. |
| 122 | bool dominates(const MachineInstr *A, const MachineInstr *B) const { |
| 123 | applySplitCriticalEdges(); |
| 124 | const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent(); |
| 125 | if (BBA != BBB) return DT->dominates(BBA, BBB); |
| 126 | |
| 127 | // Loop through the basic block until we find A or B. |
| 128 | MachineBasicBlock::const_iterator I = BBA->begin(); |
| 129 | for (; &*I != A && &*I != B; ++I) |
| 130 | /*empty*/ ; |
| 131 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 132 | return &*I == A; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 135 | bool properlyDominates(const MachineDomTreeNode *A, |
| 136 | const MachineDomTreeNode *B) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 137 | applySplitCriticalEdges(); |
| 138 | return DT->properlyDominates(A, B); |
| 139 | } |
| 140 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 141 | bool properlyDominates(const MachineBasicBlock *A, |
| 142 | const MachineBasicBlock *B) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 143 | applySplitCriticalEdges(); |
| 144 | return DT->properlyDominates(A, B); |
| 145 | } |
| 146 | |
| 147 | /// findNearestCommonDominator - Find nearest common dominator basic block |
| 148 | /// for basic block A and B. If there is no such block then return NULL. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 149 | MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A, |
| 150 | MachineBasicBlock *B) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 151 | applySplitCriticalEdges(); |
| 152 | return DT->findNearestCommonDominator(A, B); |
| 153 | } |
| 154 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 155 | MachineDomTreeNode *operator[](MachineBasicBlock *BB) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 156 | applySplitCriticalEdges(); |
| 157 | return DT->getNode(BB); |
| 158 | } |
| 159 | |
| 160 | /// getNode - return the (Post)DominatorTree node for the specified basic |
| 161 | /// block. This is the same as using operator[] on this class. |
| 162 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 163 | MachineDomTreeNode *getNode(MachineBasicBlock *BB) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 164 | applySplitCriticalEdges(); |
| 165 | return DT->getNode(BB); |
| 166 | } |
| 167 | |
| 168 | /// addNewBlock - Add a new node to the dominator tree information. This |
| 169 | /// creates a new node as a child of DomBB dominator node,linking it into |
| 170 | /// the children list of the immediate dominator. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 171 | MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB, |
| 172 | MachineBasicBlock *DomBB) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 173 | applySplitCriticalEdges(); |
| 174 | return DT->addNewBlock(BB, DomBB); |
| 175 | } |
| 176 | |
| 177 | /// changeImmediateDominator - This method is used to update the dominator |
| 178 | /// tree information when a node's immediate dominator changes. |
| 179 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 180 | void changeImmediateDominator(MachineBasicBlock *N, |
| 181 | MachineBasicBlock *NewIDom) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 182 | applySplitCriticalEdges(); |
| 183 | DT->changeImmediateDominator(N, NewIDom); |
| 184 | } |
| 185 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 186 | void changeImmediateDominator(MachineDomTreeNode *N, |
| 187 | MachineDomTreeNode *NewIDom) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 188 | applySplitCriticalEdges(); |
| 189 | DT->changeImmediateDominator(N, NewIDom); |
| 190 | } |
| 191 | |
| 192 | /// eraseNode - Removes a node from the dominator tree. Block must not |
| 193 | /// dominate any other blocks. Removes node from its immediate dominator's |
| 194 | /// children list. Deletes dominator node associated with basic block BB. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 195 | void eraseNode(MachineBasicBlock *BB) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 196 | applySplitCriticalEdges(); |
| 197 | DT->eraseNode(BB); |
| 198 | } |
| 199 | |
| 200 | /// splitBlock - BB is split and now it has one successor. Update dominator |
| 201 | /// tree to reflect this change. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 202 | void splitBlock(MachineBasicBlock* NewBB) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 203 | applySplitCriticalEdges(); |
| 204 | DT->splitBlock(NewBB); |
| 205 | } |
| 206 | |
| 207 | /// isReachableFromEntry - Return true if A is dominated by the entry |
| 208 | /// block of the function containing it. |
| 209 | bool isReachableFromEntry(const MachineBasicBlock *A) { |
| 210 | applySplitCriticalEdges(); |
| 211 | return DT->isReachableFromEntry(A); |
| 212 | } |
| 213 | |
| 214 | void releaseMemory() override; |
| 215 | |
| 216 | void verifyAnalysis() const override; |
| 217 | |
| 218 | void print(raw_ostream &OS, const Module*) const override; |
| 219 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 220 | /// Record that the critical edge (FromBB, ToBB) has been |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 221 | /// split with NewBB. |
| 222 | /// This is best to use this method instead of directly update the |
| 223 | /// underlying information, because this helps mitigating the |
| 224 | /// number of time the DT information is invalidated. |
| 225 | /// |
| 226 | /// \note Do not use this method with regular edges. |
| 227 | /// |
| 228 | /// \note To benefit from the compile time improvement incurred by this |
| 229 | /// method, the users of this method have to limit the queries to the DT |
| 230 | /// interface between two edges splitting. In other words, they have to |
| 231 | /// pack the splitting of critical edges as much as possible. |
| 232 | void recordSplitCriticalEdge(MachineBasicBlock *FromBB, |
| 233 | MachineBasicBlock *ToBB, |
| 234 | MachineBasicBlock *NewBB) { |
| 235 | bool Inserted = NewBBs.insert(NewBB).second; |
| 236 | (void)Inserted; |
| 237 | assert(Inserted && |
| 238 | "A basic block inserted via edge splitting cannot appear twice"); |
| 239 | CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB}); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | //===------------------------------------- |
| 244 | /// DominatorTree GraphTraits specialization so the DominatorTree can be |
| 245 | /// iterable by generic graph iterators. |
| 246 | /// |
| 247 | |
| 248 | template <class Node, class ChildIterator> |
| 249 | struct MachineDomTreeGraphTraitsBase { |
| 250 | using NodeRef = Node *; |
| 251 | using ChildIteratorType = ChildIterator; |
| 252 | |
| 253 | static NodeRef getEntryNode(NodeRef N) { return N; } |
| 254 | static ChildIteratorType child_begin(NodeRef N) { return N->begin(); } |
| 255 | static ChildIteratorType child_end(NodeRef N) { return N->end(); } |
| 256 | }; |
| 257 | |
| 258 | template <class T> struct GraphTraits; |
| 259 | |
| 260 | template <> |
| 261 | struct GraphTraits<MachineDomTreeNode *> |
| 262 | : public MachineDomTreeGraphTraitsBase<MachineDomTreeNode, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 263 | MachineDomTreeNode::const_iterator> { |
| 264 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 265 | |
| 266 | template <> |
| 267 | struct GraphTraits<const MachineDomTreeNode *> |
| 268 | : public MachineDomTreeGraphTraitsBase<const MachineDomTreeNode, |
| 269 | MachineDomTreeNode::const_iterator> { |
| 270 | }; |
| 271 | |
| 272 | template <> struct GraphTraits<MachineDominatorTree*> |
| 273 | : public GraphTraits<MachineDomTreeNode *> { |
| 274 | static NodeRef getEntryNode(MachineDominatorTree *DT) { |
| 275 | return DT->getRootNode(); |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | } // end namespace llvm |
| 280 | |
| 281 | #endif // LLVM_CODEGEN_MACHINEDOMINATORS_H |