Michael Zolotukhin | 52b064f | 2018-04-09 23:37:20 +0000 | [diff] [blame] | 1 | //===- SSAUpdaterBulk.cpp - Unstructured SSA Update Tool ------------------===// |
| 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 implements the SSAUpdaterBulk class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/SSAUpdaterBulk.h" |
| 15 | #include "llvm/Analysis/IteratedDominanceFrontier.h" |
| 16 | #include "llvm/IR/BasicBlock.h" |
| 17 | #include "llvm/IR/Dominators.h" |
| 18 | #include "llvm/IR/IRBuilder.h" |
| 19 | #include "llvm/IR/Instructions.h" |
| 20 | #include "llvm/IR/Use.h" |
| 21 | #include "llvm/IR/Value.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #define DEBUG_TYPE "ssaupdaterbulk" |
| 26 | |
| 27 | /// Add a new variable to the SSA rewriter. This needs to be called before |
| 28 | /// AddAvailableValue or AddUse calls. |
| 29 | void SSAUpdaterBulk::AddVariable(unsigned Var, StringRef Name, Type *Ty) { |
| 30 | assert(Rewrites.find(Var) == Rewrites.end() && "Variable added twice!"); |
| 31 | RewriteInfo RI(Name, Ty); |
| 32 | Rewrites[Var] = RI; |
| 33 | } |
| 34 | |
| 35 | /// Indicate that a rewritten value is available in the specified block with the |
| 36 | /// specified value. |
| 37 | void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) { |
| 38 | assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!"); |
| 39 | Rewrites[Var].Defines[BB] = V; |
| 40 | } |
| 41 | |
| 42 | /// Record a use of the symbolic value. This use will be updated with a |
| 43 | /// rewritten value when RewriteAllUses is called. |
| 44 | void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) { |
| 45 | assert(Rewrites.find(Var) != Rewrites.end() && "Should add variable first!"); |
| 46 | Rewrites[Var].Uses.insert(U); |
| 47 | } |
| 48 | |
| 49 | /// Return true if the SSAUpdater already has a value for the specified variable |
| 50 | /// in the specified block. |
| 51 | bool SSAUpdaterBulk::HasValueForBlock(unsigned Var, BasicBlock *BB) { |
| 52 | return Rewrites.count(Var) ? Rewrites[Var].Defines.count(BB) : false; |
| 53 | } |
| 54 | |
| 55 | // Compute value at the given block BB. We either should already know it, or we |
| 56 | // should be able to recursively reach it going up dominator tree. |
| 57 | Value *SSAUpdaterBulk::computeValueAt(BasicBlock *BB, RewriteInfo &R, |
| 58 | DominatorTree *DT) { |
| 59 | if (!R.Defines.count(BB)) { |
Michael Zolotukhin | aa78685 | 2018-04-10 02:16:29 +0000 | [diff] [blame^] | 60 | if (DT->isReachableFromEntry(BB) && PredCache.get(BB).size()) { |
Michael Zolotukhin | 52b064f | 2018-04-09 23:37:20 +0000 | [diff] [blame] | 61 | BasicBlock *IDom = DT->getNode(BB)->getIDom()->getBlock(); |
| 62 | R.Defines[BB] = computeValueAt(IDom, R, DT); |
| 63 | } else |
| 64 | R.Defines[BB] = UndefValue::get(R.Ty); |
| 65 | } |
| 66 | return R.Defines[BB]; |
| 67 | } |
| 68 | |
| 69 | /// Given sets of UsingBlocks and DefBlocks, compute the set of LiveInBlocks. |
| 70 | /// This is basically a subgraph limited by DefBlocks and UsingBlocks. |
| 71 | static void |
| 72 | ComputeLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &UsingBlocks, |
| 73 | const SmallPtrSetImpl<BasicBlock *> &DefBlocks, |
| 74 | SmallPtrSetImpl<BasicBlock *> &LiveInBlocks) { |
| 75 | // To determine liveness, we must iterate through the predecessors of blocks |
| 76 | // where the def is live. Blocks are added to the worklist if we need to |
| 77 | // check their predecessors. Start with all the using blocks. |
| 78 | SmallVector<BasicBlock *, 64> LiveInBlockWorklist(UsingBlocks.begin(), |
| 79 | UsingBlocks.end()); |
| 80 | |
| 81 | // Now that we have a set of blocks where the phi is live-in, recursively add |
| 82 | // their predecessors until we find the full region the value is live. |
| 83 | while (!LiveInBlockWorklist.empty()) { |
| 84 | BasicBlock *BB = LiveInBlockWorklist.pop_back_val(); |
| 85 | |
| 86 | // The block really is live in here, insert it into the set. If already in |
| 87 | // the set, then it has already been processed. |
| 88 | if (!LiveInBlocks.insert(BB).second) |
| 89 | continue; |
| 90 | |
| 91 | // Since the value is live into BB, it is either defined in a predecessor or |
| 92 | // live into it to. Add the preds to the worklist unless they are a |
| 93 | // defining block. |
| 94 | for (BasicBlock *P : predecessors(BB)) { |
| 95 | // The value is not live into a predecessor if it defines the value. |
| 96 | if (DefBlocks.count(P)) |
| 97 | continue; |
| 98 | |
| 99 | // Otherwise it is, add to the worklist. |
| 100 | LiveInBlockWorklist.push_back(P); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Helper function for finding a block which should have a value for the given |
| 106 | /// user. For PHI-nodes this block is the corresponding predecessor, for other |
| 107 | /// instructions it's their parent block. |
| 108 | static BasicBlock *getUserBB(Use *U) { |
| 109 | auto *User = cast<Instruction>(U->getUser()); |
| 110 | |
| 111 | if (auto *UserPN = dyn_cast<PHINode>(User)) |
| 112 | return UserPN->getIncomingBlock(*U); |
| 113 | else |
| 114 | return User->getParent(); |
| 115 | } |
| 116 | |
| 117 | /// Perform all the necessary updates, including new PHI-nodes insertion and the |
| 118 | /// requested uses update. |
| 119 | void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT, |
| 120 | SmallVectorImpl<PHINode *> *InsertedPHIs) { |
| 121 | for (auto P : Rewrites) { |
| 122 | // Compute locations for new phi-nodes. |
| 123 | // For that we need to initialize DefBlocks from definitions in R.Defines, |
| 124 | // UsingBlocks from uses in R.Uses, then compute LiveInBlocks, and then use |
| 125 | // this set for computing iterated dominance frontier (IDF). |
| 126 | // The IDF blocks are the blocks where we need to insert new phi-nodes. |
| 127 | ForwardIDFCalculator IDF(*DT); |
| 128 | RewriteInfo &R = P.second; |
| 129 | SmallPtrSet<BasicBlock *, 2> DefBlocks; |
| 130 | for (auto Def : R.Defines) |
| 131 | DefBlocks.insert(Def.first); |
| 132 | IDF.setDefiningBlocks(DefBlocks); |
| 133 | |
| 134 | SmallPtrSet<BasicBlock *, 2> UsingBlocks; |
| 135 | for (auto U : R.Uses) |
| 136 | UsingBlocks.insert(getUserBB(U)); |
| 137 | |
| 138 | SmallVector<BasicBlock *, 32> IDFBlocks; |
| 139 | SmallPtrSet<BasicBlock *, 32> LiveInBlocks; |
| 140 | ComputeLiveInBlocks(UsingBlocks, DefBlocks, LiveInBlocks); |
| 141 | IDF.resetLiveInBlocks(); |
| 142 | IDF.setLiveInBlocks(LiveInBlocks); |
| 143 | IDF.calculate(IDFBlocks); |
| 144 | |
| 145 | // We've computed IDF, now insert new phi-nodes there. |
| 146 | SmallVector<PHINode *, 4> InsertedPHIsForVar; |
| 147 | for (auto FrontierBB : IDFBlocks) { |
| 148 | IRBuilder<> B(FrontierBB, FrontierBB->begin()); |
| 149 | PHINode *PN = B.CreatePHI(R.Ty, 0, R.Name); |
| 150 | R.Defines[FrontierBB] = PN; |
| 151 | InsertedPHIsForVar.push_back(PN); |
| 152 | if (InsertedPHIs) |
| 153 | InsertedPHIs->push_back(PN); |
| 154 | } |
| 155 | |
| 156 | // Fill in arguments of the inserted PHIs. |
| 157 | for (auto PN : InsertedPHIsForVar) { |
| 158 | BasicBlock *PBB = PN->getParent(); |
| 159 | for (BasicBlock *Pred : PredCache.get(PBB)) |
| 160 | PN->addIncoming(computeValueAt(Pred, R, DT), Pred); |
| 161 | } |
| 162 | |
| 163 | // Rewrite actual uses with the inserted definitions. |
| 164 | for (auto U : R.Uses) { |
| 165 | Value *V = computeValueAt(getUserBB(U), R, DT); |
| 166 | Value *OldVal = U->get(); |
| 167 | // Notify that users of the existing value that it is being replaced. |
| 168 | if (OldVal != V && OldVal->hasValueHandle()) |
| 169 | ValueHandleBase::ValueIsRAUWd(OldVal, V); |
| 170 | U->set(V); |
| 171 | } |
| 172 | } |
| 173 | } |