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