blob: 917d5e0a1ef0a80e6857d5abe4b54cf104d406c3 [file] [log] [blame]
Michael Zolotukhin52b064f2018-04-09 23:37:20 +00001//===- SSAUpdaterBulk.cpp - Unstructured SSA Update Tool ------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael Zolotukhin52b064f2018-04-09 23:37:20 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SSAUpdaterBulk class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
14#include "llvm/Analysis/IteratedDominanceFrontier.h"
15#include "llvm/IR/BasicBlock.h"
16#include "llvm/IR/Dominators.h"
17#include "llvm/IR/IRBuilder.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/IR/Use.h"
20#include "llvm/IR/Value.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "ssaupdaterbulk"
25
Michael Zolotukhina6e7bd72018-04-17 04:45:40 +000026/// Helper function for finding a block which should have a value for the given
27/// user. For PHI-nodes this block is the corresponding predecessor, for other
28/// instructions it's their parent block.
29static BasicBlock *getUserBB(Use *U) {
30 auto *User = cast<Instruction>(U->getUser());
31
32 if (auto *UserPN = dyn_cast<PHINode>(User))
33 return UserPN->getIncomingBlock(*U);
34 else
35 return User->getParent();
36}
37
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000038/// Add a new variable to the SSA rewriter. This needs to be called before
39/// AddAvailableValue or AddUse calls.
Michael Zolotukhina2c9af02018-04-20 13:34:32 +000040unsigned SSAUpdaterBulk::AddVariable(StringRef Name, Type *Ty) {
41 unsigned Var = Rewrites.size();
Nicola Zaghend34e60c2018-05-14 12:53:11 +000042 LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": initialized with Ty = "
43 << *Ty << ", Name = " << Name << "\n");
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000044 RewriteInfo RI(Name, Ty);
Michael Zolotukhina2c9af02018-04-20 13:34:32 +000045 Rewrites.push_back(RI);
46 return Var;
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000047}
48
49/// Indicate that a rewritten value is available in the specified block with the
50/// specified value.
51void SSAUpdaterBulk::AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V) {
Michael Zolotukhina2c9af02018-04-20 13:34:32 +000052 assert(Var < Rewrites.size() && "Variable not found!");
Nicola Zaghend34e60c2018-05-14 12:53:11 +000053 LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var
54 << ": added new available value" << *V << " in "
55 << BB->getName() << "\n");
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000056 Rewrites[Var].Defines[BB] = V;
57}
58
59/// Record a use of the symbolic value. This use will be updated with a
60/// rewritten value when RewriteAllUses is called.
61void SSAUpdaterBulk::AddUse(unsigned Var, Use *U) {
Michael Zolotukhina2c9af02018-04-20 13:34:32 +000062 assert(Var < Rewrites.size() && "Variable not found!");
Nicola Zaghend34e60c2018-05-14 12:53:11 +000063 LLVM_DEBUG(dbgs() << "SSAUpdater: Var=" << Var << ": added a use" << *U->get()
64 << " in " << getUserBB(U)->getName() << "\n");
Michael Zolotukhin79cb54b2018-04-20 07:56:00 +000065 Rewrites[Var].Uses.push_back(U);
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000066}
67
68/// Return true if the SSAUpdater already has a value for the specified variable
69/// in the specified block.
70bool SSAUpdaterBulk::HasValueForBlock(unsigned Var, BasicBlock *BB) {
Michael Zolotukhina2c9af02018-04-20 13:34:32 +000071 return (Var < Rewrites.size()) ? Rewrites[Var].Defines.count(BB) : false;
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000072}
73
74// Compute value at the given block BB. We either should already know it, or we
75// should be able to recursively reach it going up dominator tree.
76Value *SSAUpdaterBulk::computeValueAt(BasicBlock *BB, RewriteInfo &R,
77 DominatorTree *DT) {
78 if (!R.Defines.count(BB)) {
Michael Zolotukhinaa786852018-04-10 02:16:29 +000079 if (DT->isReachableFromEntry(BB) && PredCache.get(BB).size()) {
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000080 BasicBlock *IDom = DT->getNode(BB)->getIDom()->getBlock();
Michael Zolotukhin4fbb9302018-04-11 23:37:37 +000081 Value *V = computeValueAt(IDom, R, DT);
82 R.Defines[BB] = V;
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000083 } else
84 R.Defines[BB] = UndefValue::get(R.Ty);
85 }
86 return R.Defines[BB];
87}
88
89/// Given sets of UsingBlocks and DefBlocks, compute the set of LiveInBlocks.
90/// This is basically a subgraph limited by DefBlocks and UsingBlocks.
91static void
92ComputeLiveInBlocks(const SmallPtrSetImpl<BasicBlock *> &UsingBlocks,
93 const SmallPtrSetImpl<BasicBlock *> &DefBlocks,
Michael Zolotukhinbc843212018-04-20 07:57:24 +000094 SmallPtrSetImpl<BasicBlock *> &LiveInBlocks,
95 PredIteratorCache &PredCache) {
Michael Zolotukhin52b064f2018-04-09 23:37:20 +000096 // To determine liveness, we must iterate through the predecessors of blocks
97 // where the def is live. Blocks are added to the worklist if we need to
98 // check their predecessors. Start with all the using blocks.
99 SmallVector<BasicBlock *, 64> LiveInBlockWorklist(UsingBlocks.begin(),
100 UsingBlocks.end());
101
102 // Now that we have a set of blocks where the phi is live-in, recursively add
103 // their predecessors until we find the full region the value is live.
104 while (!LiveInBlockWorklist.empty()) {
105 BasicBlock *BB = LiveInBlockWorklist.pop_back_val();
106
107 // The block really is live in here, insert it into the set. If already in
108 // the set, then it has already been processed.
109 if (!LiveInBlocks.insert(BB).second)
110 continue;
111
112 // Since the value is live into BB, it is either defined in a predecessor or
113 // live into it to. Add the preds to the worklist unless they are a
114 // defining block.
Michael Zolotukhinbc843212018-04-20 07:57:24 +0000115 for (BasicBlock *P : PredCache.get(BB)) {
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000116 // The value is not live into a predecessor if it defines the value.
117 if (DefBlocks.count(P))
118 continue;
119
120 // Otherwise it is, add to the worklist.
121 LiveInBlockWorklist.push_back(P);
122 }
123 }
124}
125
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000126/// Perform all the necessary updates, including new PHI-nodes insertion and the
127/// requested uses update.
128void SSAUpdaterBulk::RewriteAllUses(DominatorTree *DT,
129 SmallVectorImpl<PHINode *> *InsertedPHIs) {
Michael Zolotukhina2c9af02018-04-20 13:34:32 +0000130 for (auto &R : Rewrites) {
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000131 // Compute locations for new phi-nodes.
132 // For that we need to initialize DefBlocks from definitions in R.Defines,
133 // UsingBlocks from uses in R.Uses, then compute LiveInBlocks, and then use
134 // this set for computing iterated dominance frontier (IDF).
135 // The IDF blocks are the blocks where we need to insert new phi-nodes.
136 ForwardIDFCalculator IDF(*DT);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000137 LLVM_DEBUG(dbgs() << "SSAUpdater: rewriting " << R.Uses.size()
138 << " use(s)\n");
Michael Zolotukhina6e7bd72018-04-17 04:45:40 +0000139
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000140 SmallPtrSet<BasicBlock *, 2> DefBlocks;
Michael Zolotukhin0df1d482018-04-20 07:58:54 +0000141 for (auto &Def : R.Defines)
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000142 DefBlocks.insert(Def.first);
143 IDF.setDefiningBlocks(DefBlocks);
144
145 SmallPtrSet<BasicBlock *, 2> UsingBlocks;
Michael Zolotukhin0df1d482018-04-20 07:58:54 +0000146 for (Use *U : R.Uses)
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000147 UsingBlocks.insert(getUserBB(U));
148
149 SmallVector<BasicBlock *, 32> IDFBlocks;
150 SmallPtrSet<BasicBlock *, 32> LiveInBlocks;
Michael Zolotukhinbc843212018-04-20 07:57:24 +0000151 ComputeLiveInBlocks(UsingBlocks, DefBlocks, LiveInBlocks, PredCache);
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000152 IDF.resetLiveInBlocks();
153 IDF.setLiveInBlocks(LiveInBlocks);
154 IDF.calculate(IDFBlocks);
155
156 // We've computed IDF, now insert new phi-nodes there.
157 SmallVector<PHINode *, 4> InsertedPHIsForVar;
Michael Zolotukhin0df1d482018-04-20 07:58:54 +0000158 for (auto *FrontierBB : IDFBlocks) {
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000159 IRBuilder<> B(FrontierBB, FrontierBB->begin());
160 PHINode *PN = B.CreatePHI(R.Ty, 0, R.Name);
161 R.Defines[FrontierBB] = PN;
162 InsertedPHIsForVar.push_back(PN);
163 if (InsertedPHIs)
164 InsertedPHIs->push_back(PN);
165 }
166
167 // Fill in arguments of the inserted PHIs.
Michael Zolotukhina2c9af02018-04-20 13:34:32 +0000168 for (auto *PN : InsertedPHIsForVar) {
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000169 BasicBlock *PBB = PN->getParent();
170 for (BasicBlock *Pred : PredCache.get(PBB))
171 PN->addIncoming(computeValueAt(Pred, R, DT), Pred);
172 }
173
174 // Rewrite actual uses with the inserted definitions.
Michael Zolotukhin79cb54b2018-04-20 07:56:00 +0000175 SmallPtrSet<Use *, 4> ProcessedUses;
176 for (Use *U : R.Uses) {
177 if (!ProcessedUses.insert(U).second)
178 continue;
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000179 Value *V = computeValueAt(getUserBB(U), R, DT);
180 Value *OldVal = U->get();
Michael Zolotukhin26339b42018-04-20 07:59:57 +0000181 assert(OldVal && "Invalid use!");
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000182 // Notify that users of the existing value that it is being replaced.
183 if (OldVal != V && OldVal->hasValueHandle())
184 ValueHandleBase::ValueIsRAUWd(OldVal, V);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000185 LLVM_DEBUG(dbgs() << "SSAUpdater: replacing " << *OldVal << " with " << *V
186 << "\n");
Michael Zolotukhin52b064f2018-04-09 23:37:20 +0000187 U->set(V);
188 }
189 }
190}