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