|
| 1 | +//===- DomTreeUpdater.h - DomTree/Post DomTree Updater ----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file defines the DomTreeUpdater class, which provides a uniform way to |
| 11 | +// update dominator tree related data structures. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef LLVM_DOMTREEUPDATER_H |
| 16 | +#define LLVM_DOMTREEUPDATER_H |
| 17 | + |
| 18 | +#include "llvm/Analysis/PostDominators.h" |
| 19 | +#include "llvm/IR/Dominators.h" |
| 20 | +#include "llvm/IR/Instructions.h" |
| 21 | +#include "llvm/IR/ValueHandle.h" |
| 22 | +#include "llvm/Support/GenericDomTree.h" |
| 23 | +#include <functional> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace llvm { |
| 27 | +class DomTreeUpdater { |
| 28 | +public: |
| 29 | + enum class UpdateStrategy : unsigned char { Eager = 0, Lazy = 1 }; |
| 30 | + |
| 31 | + explicit DomTreeUpdater(UpdateStrategy Strategy_) : Strategy(Strategy_) {} |
| 32 | + DomTreeUpdater(DominatorTree &DT_, UpdateStrategy Strategy_) |
| 33 | + : DT(&DT_), Strategy(Strategy_) {} |
| 34 | + DomTreeUpdater(PostDominatorTree &PDT_, UpdateStrategy Strategy_) |
| 35 | + : PDT(&PDT_), Strategy(Strategy_) {} |
| 36 | + DomTreeUpdater(DominatorTree &DT_, PostDominatorTree &PDT_, |
| 37 | + UpdateStrategy Strategy_) |
| 38 | + : DT(&DT_), PDT(&PDT_), Strategy(Strategy_) {} |
| 39 | + |
| 40 | + ~DomTreeUpdater() { flush(); } |
| 41 | + |
| 42 | + /// Returns the UpdateStrategy of the class instance. |
| 43 | + UpdateStrategy getUpdateStrategy() const { return Strategy; }; |
| 44 | + |
| 45 | + /// Returns true if it holds a DominatorTree. |
| 46 | + bool hasDomTree() const { return DT != nullptr; } |
| 47 | + |
| 48 | + /// Returns true if it holds a PostDominatorTree. |
| 49 | + bool hasPostDomTree() const { return PDT != nullptr; } |
| 50 | + |
| 51 | + /// Returns true if there is BasicBlock awaiting deletion. |
| 52 | + /// The deletion will only happen until a flush event and |
| 53 | + /// all available trees are up-to-date. |
| 54 | + /// Returns false under Eager UpdateStrategy. |
| 55 | + bool hasPendingDeletedBB() const { return !DeletedBBs.empty(); } |
| 56 | + |
| 57 | + /// Returns true if DelBB is awaiting deletion. |
| 58 | + /// Returns false under Eager UpdateStrategy. |
| 59 | + bool isBBPendingDeletion(BasicBlock *DelBB) const; |
| 60 | + |
| 61 | + /// Returns true if either of DT or PDT is valid and the tree has at |
| 62 | + /// least one update pending. If DT or PDT is nullptr it is treated |
| 63 | + /// as having no pending updates. This function does not check |
| 64 | + /// whether there is BasicBlock awaiting deletion. |
| 65 | + /// Returns false under Eager UpdateStrategy. |
| 66 | + bool hasPendingUpdates() const; |
| 67 | + |
| 68 | + /// Returns true if there are DominatorTree updates queued. |
| 69 | + /// Returns false under Eager UpdateStrategy. |
| 70 | + bool hasPendingDomTreeUpdates() const; |
| 71 | + |
| 72 | + /// Returns true if there are PostDominatorTree updates queued. |
| 73 | + /// Returns false under Eager UpdateStrategy. |
| 74 | + bool hasPendingPostDomTreeUpdates() const; |
| 75 | + |
| 76 | + /// Apply updates on all available trees. Under Eager UpdateStrategy with |
| 77 | + /// ForceRemoveDuplicates enabled or under Lazy UpdateStrategy, it will |
| 78 | + /// discard duplicated updates and self-dominance updates. The Eager |
| 79 | + /// Strategy applies the updates immediately while the Lazy Strategy |
| 80 | + /// queues the updates. It is required for the state of |
| 81 | + /// the LLVM IR to be updated *before* applying the Updates because the |
| 82 | + /// internal update routine will analyze the current state of the relationship |
| 83 | + /// between a pair of (From, To) BasicBlocks to determine whether a single |
| 84 | + /// update needs to be discarded. |
| 85 | + void applyUpdates(ArrayRef<DominatorTree::UpdateType> Updates, |
| 86 | + bool ForceRemoveDuplicates = false); |
| 87 | + |
| 88 | + /// Notify all available trees on an edge insertion. Under either Strategy, |
| 89 | + /// self-dominance update will be removed. The Eager Strategy applies |
| 90 | + /// the update immediately while the Lazy Strategy queues the update. |
| 91 | + /// It is recommended to only use this method when you have exactly one |
| 92 | + /// insertion (and no deletions). It is recommended to use applyUpdates() in |
| 93 | + /// all other cases. This function has to be called *after* making the update |
| 94 | + /// on the actual CFG. An internal functions checks if the edge exists in the |
| 95 | + /// CFG in DEBUG mode. |
| 96 | + void insertEdge(BasicBlock *From, BasicBlock *To); |
| 97 | + |
| 98 | + /// Notify all available trees on an edge insertion. |
| 99 | + /// Under either Strategy, these updates will be discard silently in the |
| 100 | + /// following sequence |
| 101 | + /// 1. Invalid - Inserting an edge that does not exist in the CFG. |
| 102 | + /// 2. Self-dominance update. |
| 103 | + /// The Eager Strategy applies the update immediately while the Lazy Strategy |
| 104 | + /// queues the update. It is recommended to only use this method when you have |
| 105 | + /// exactly one insertion (and no deletions) and want to discard an invalid |
| 106 | + /// update. Returns true if the update is valid. |
| 107 | + bool insertEdgeRelaxed(BasicBlock *From, BasicBlock *To); |
| 108 | + |
| 109 | + /// Notify all available trees on an edge deletion. Under either Strategy, |
| 110 | + /// self-dominance update will be removed. The Eager Strategy applies |
| 111 | + /// the update immediately while the Lazy Strategy queues the update. |
| 112 | + /// It is recommended to only use this method when you have exactly one |
| 113 | + /// deletion (and no insertions). It is recommended to use applyUpdates() in |
| 114 | + /// all other cases. This function has to be called *after* making the update |
| 115 | + /// on the actual CFG. An internal functions checks if the edge doesn't exist |
| 116 | + /// in the CFG in DEBUG mode. |
| 117 | + void deleteEdge(BasicBlock *From, BasicBlock *To); |
| 118 | + |
| 119 | + /// Notify all available trees on an edge deletion. |
| 120 | + /// Under either Strategy, these updates will be discard silently in the |
| 121 | + /// following sequence: |
| 122 | + /// 1. Invalid - Deleting an edge that still exists in the CFG. |
| 123 | + /// 2. Self-dominance update. |
| 124 | + /// The Eager Strategy applies the update immediately while the Lazy Strategy |
| 125 | + /// queues the update. It is recommended to only use this method when you have |
| 126 | + /// exactly one deletion (and no insertions) and want to discard an invalid |
| 127 | + /// update. Returns true if the update is valid. |
| 128 | + bool deleteEdgeRelaxed(BasicBlock *From, BasicBlock *To); |
| 129 | + |
| 130 | + /// Delete DelBB. DelBB will be removed from its Parent and |
| 131 | + /// erased from available trees if it exists and finally get deleted. |
| 132 | + /// Under Eager UpdateStrategy, DelBB will be processed immediately. |
| 133 | + /// Under Lazy UpdateStrategy, DelBB will be queued until a flush event and |
| 134 | + /// all available trees are up-to-date. |
| 135 | + void deleteBB(BasicBlock *DelBB); |
| 136 | + |
| 137 | + /// Delete DelBB. DelBB will be removed from its Parent and |
| 138 | + /// erased from available trees if it exists. Then the callback will |
| 139 | + /// be called. Finally, DelBB will be deleted. |
| 140 | + /// Under Eager UpdateStrategy, DelBB will be processed immediately. |
| 141 | + /// Under Lazy UpdateStrategy, DelBB will be queued until a flush event and |
| 142 | + /// all available trees are up-to-date. |
| 143 | + /// Multiple callbacks can be queued for one DelBB under Lazy UpdateStrategy. |
| 144 | + void callbackDeleteBB(BasicBlock *DelBB, |
| 145 | + std::function<void(BasicBlock *)> Callback); |
| 146 | + |
| 147 | + /// Recalculate all available trees. |
| 148 | + /// Under Lazy Strategy, available trees will only be recalculated if there |
| 149 | + /// are pending updates or there is BasicBlock awaiting deletion. Returns true |
| 150 | + /// if at least one tree is recalculated. |
| 151 | + bool recalculate(Function &F); |
| 152 | + |
| 153 | + /// Flush DomTree updates and return DomTree. |
| 154 | + /// It also flush out of date updates applied by all available trees |
| 155 | + /// and flush Deleted BBs if both trees are up-to-date. |
| 156 | + /// It must only be called when it has a DomTree. |
| 157 | + DominatorTree &getDomTree(); |
| 158 | + |
| 159 | + /// Flush PostDomTree updates and return PostDomTree. |
| 160 | + /// It also flush out of date updates applied by all available trees |
| 161 | + /// and flush Deleted BBs if both trees are up-to-date. |
| 162 | + /// It must only be called when it has a PostDomTree. |
| 163 | + PostDominatorTree &getPostDomTree(); |
| 164 | + |
| 165 | + /// Apply all pending updates to available trees and flush all BasicBlocks |
| 166 | + /// awaiting deletion. |
| 167 | + /// Does nothing under Eager UpdateStrategy. |
| 168 | + void flush(); |
| 169 | + |
| 170 | + /// Debug method to help view the internal state of this class. |
| 171 | + LLVM_DUMP_METHOD void dump() const; |
| 172 | + |
| 173 | +private: |
| 174 | + class CallBackOnDeletion final : public CallbackVH { |
| 175 | + public: |
| 176 | + CallBackOnDeletion(BasicBlock *V, |
| 177 | + std::function<void(BasicBlock *)> Callback) |
| 178 | + : CallbackVH(V), DelBB(V), Callback_(Callback) {} |
| 179 | + |
| 180 | + private: |
| 181 | + BasicBlock *DelBB = nullptr; |
| 182 | + std::function<void(BasicBlock *)> Callback_; |
| 183 | + |
| 184 | + void deleted() override { |
| 185 | + Callback_(DelBB); |
| 186 | + CallbackVH::deleted(); |
| 187 | + } |
| 188 | + }; |
| 189 | + |
| 190 | + SmallVector<DominatorTree::UpdateType, 16> PendUpdates; |
| 191 | + size_t PendDTUpdateIndex = 0; |
| 192 | + size_t PendPDTUpdateIndex = 0; |
| 193 | + DominatorTree *DT = nullptr; |
| 194 | + PostDominatorTree *PDT = nullptr; |
| 195 | + const UpdateStrategy Strategy; |
| 196 | + SmallPtrSet<BasicBlock *, 8> DeletedBBs; |
| 197 | + std::vector<CallBackOnDeletion> Callbacks; |
| 198 | + bool IsRecalculatingDomTree = false; |
| 199 | + bool IsRecalculatingPostDomTree = false; |
| 200 | + |
| 201 | + /// First remove all the instructions of DelBB and then make sure DelBB has a |
| 202 | + /// valid terminator instruction which is necessary to have when DelBB still |
| 203 | + /// has to be inside of its parent Function while awaiting deletion under Lazy |
| 204 | + /// UpdateStrategy to prevent other routines from asserting the state of the |
| 205 | + /// IR is inconsistent. Assert if DelBB is nullptr or has predecessors. |
| 206 | + void validateDeleteBB(BasicBlock *DelBB); |
| 207 | + |
| 208 | + /// Returns true if at least one BasicBlock is deleted. |
| 209 | + bool forceFlushDeletedBB(); |
| 210 | + |
| 211 | + /// Deduplicate and remove unnecessary updates (no-ops) when using Lazy |
| 212 | + /// UpdateStrategy. Returns true if the update is queued for update. |
| 213 | + bool applyLazyUpdate(DominatorTree::UpdateKind Kind, BasicBlock *From, |
| 214 | + BasicBlock *To); |
| 215 | + |
| 216 | + /// Helper function to apply all pending DomTree updates. |
| 217 | + void applyDomTreeUpdates(); |
| 218 | + |
| 219 | + /// Helper function to apply all pending PostDomTree updates. |
| 220 | + void applyPostDomTreeUpdates(); |
| 221 | + |
| 222 | + /// Helper function to flush deleted BasicBlocks if all available |
| 223 | + /// trees are up-to-date. |
| 224 | + void tryFlushDeletedBB(); |
| 225 | + |
| 226 | + /// Drop all updates applied by all available trees and delete BasicBlocks if |
| 227 | + /// all available trees are up-to-date. |
| 228 | + void dropOutOfDateUpdates(); |
| 229 | + |
| 230 | + /// Erase Basic Block node that has been unlinked from Function |
| 231 | + /// in the DomTree and PostDomTree. |
| 232 | + void eraseDelBBNode(BasicBlock *DelBB); |
| 233 | + |
| 234 | + /// Returns true if the update appears in the LLVM IR. |
| 235 | + /// It is used to check whether an update is valid in |
| 236 | + /// insertEdge/deleteEdge or is unnecessary in the batch update. |
| 237 | + bool isUpdateValid(DominatorTree::UpdateType Update) const; |
| 238 | + |
| 239 | + /// Returns true if the update is self dominance. |
| 240 | + bool isSelfDominance(DominatorTree::UpdateType Update) const; |
| 241 | +}; |
| 242 | +} // namespace llvm |
| 243 | + |
| 244 | +#endif // LLVM_DOMTREEUPDATER_H |
0 commit comments