1//===- SSAUpdater.cpp - Unstructured SSA Update Tool ----------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SSAUpdater class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Transforms/Utils/SSAUpdater.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/TinyPtrVector.h"
18#include "llvm/Analysis/InstructionSimplify.h"
19#include "llvm/IR/BasicBlock.h"
20#include "llvm/IR/CFG.h"
21#include "llvm/IR/Constants.h"
22#include "llvm/IR/DebugInfo.h"
23#include "llvm/IR/DebugLoc.h"
24#include "llvm/IR/Instruction.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Use.h"
27#include "llvm/IR/Value.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
32#include <cassert>
33#include <utility>
34
35using namespace llvm;
36
37#define DEBUG_TYPE "ssaupdater"
38
39using AvailableValsTy = DenseMap<BasicBlock *, Value *>;
40
41static AvailableValsTy &getAvailableVals(void *AV) {
42 return *static_cast<AvailableValsTy*>(AV);
43}
44
45SSAUpdater::SSAUpdater(SmallVectorImpl<PHINode *> *NewPHI)
46 : InsertedPHIs(NewPHI) {}
47
48SSAUpdater::~SSAUpdater() {
49 delete static_cast<AvailableValsTy*>(AV);
50}
51
52void SSAUpdater::Initialize(Type *Ty, StringRef Name) {
53 if (!AV)
54 AV = new AvailableValsTy();
55 else
56 getAvailableVals(AV).clear();
57 ProtoType = Ty;
58 ProtoName = std::string(Name);
59}
60
61bool SSAUpdater::HasValueForBlock(BasicBlock *BB) const {
62 return getAvailableVals(AV).count(Val: BB);
63}
64
65Value *SSAUpdater::FindValueForBlock(BasicBlock *BB) const {
66 return getAvailableVals(AV).lookup(Val: BB);
67}
68
69void SSAUpdater::AddAvailableValue(BasicBlock *BB, Value *V) {
70 assert(ProtoType && "Need to initialize SSAUpdater");
71 assert(ProtoType == V->getType() &&
72 "All rewritten values must have the same type");
73 getAvailableVals(AV)[BB] = V;
74}
75
76static bool IsEquivalentPHI(PHINode *PHI,
77 SmallDenseMap<BasicBlock *, Value *, 8> &ValueMapping) {
78 unsigned PHINumValues = PHI->getNumIncomingValues();
79 if (PHINumValues != ValueMapping.size())
80 return false;
81
82 // Scan the phi to see if it matches.
83 for (unsigned i = 0, e = PHINumValues; i != e; ++i)
84 if (ValueMapping[PHI->getIncomingBlock(i)] !=
85 PHI->getIncomingValue(i)) {
86 return false;
87 }
88
89 return true;
90}
91
92Value *SSAUpdater::GetValueAtEndOfBlock(BasicBlock *BB) {
93 Value *Res = GetValueAtEndOfBlockInternal(BB);
94 return Res;
95}
96
97Value *SSAUpdater::GetValueInMiddleOfBlock(BasicBlock *BB) {
98 // If there is no definition of the renamed variable in this block, just use
99 // GetValueAtEndOfBlock to do our work.
100 if (!HasValueForBlock(BB))
101 return GetValueAtEndOfBlock(BB);
102
103 // Otherwise, we have the hard case. Get the live-in values for each
104 // predecessor.
105 SmallVector<std::pair<BasicBlock *, Value *>, 8> PredValues;
106 Value *SingularValue = nullptr;
107
108 // We can get our predecessor info by walking the pred_iterator list, but it
109 // is relatively slow. If we already have PHI nodes in this block, walk one
110 // of them to get the predecessor list instead.
111 if (PHINode *SomePhi = dyn_cast<PHINode>(Val: BB->begin())) {
112 for (unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) {
113 BasicBlock *PredBB = SomePhi->getIncomingBlock(i);
114 Value *PredVal = GetValueAtEndOfBlock(BB: PredBB);
115 PredValues.push_back(Elt: std::make_pair(x&: PredBB, y&: PredVal));
116
117 // Compute SingularValue.
118 if (i == 0)
119 SingularValue = PredVal;
120 else if (PredVal != SingularValue)
121 SingularValue = nullptr;
122 }
123 } else {
124 bool isFirstPred = true;
125 for (BasicBlock *PredBB : predecessors(BB)) {
126 Value *PredVal = GetValueAtEndOfBlock(BB: PredBB);
127 PredValues.push_back(Elt: std::make_pair(x&: PredBB, y&: PredVal));
128
129 // Compute SingularValue.
130 if (isFirstPred) {
131 SingularValue = PredVal;
132 isFirstPred = false;
133 } else if (PredVal != SingularValue)
134 SingularValue = nullptr;
135 }
136 }
137
138 // If there are no predecessors, just return poison.
139 if (PredValues.empty())
140 return PoisonValue::get(T: ProtoType);
141
142 // Otherwise, if all the merged values are the same, just use it.
143 if (SingularValue)
144 return SingularValue;
145
146 // Otherwise, we do need a PHI: check to see if we already have one available
147 // in this block that produces the right value.
148 if (isa<PHINode>(Val: BB->begin())) {
149 SmallDenseMap<BasicBlock *, Value *, 8> ValueMapping(PredValues.begin(),
150 PredValues.end());
151 for (PHINode &SomePHI : BB->phis()) {
152 if (IsEquivalentPHI(PHI: &SomePHI, ValueMapping))
153 return &SomePHI;
154 }
155 }
156
157 // Ok, we have no way out, insert a new one now.
158 PHINode *InsertedPHI =
159 PHINode::Create(Ty: ProtoType, NumReservedValues: PredValues.size(), NameStr: ProtoName);
160 InsertedPHI->insertBefore(InsertPos: BB->begin());
161
162 // Fill in all the predecessors of the PHI.
163 for (const auto &PredValue : PredValues)
164 InsertedPHI->addIncoming(V: PredValue.second, BB: PredValue.first);
165
166 // See if the PHI node can be merged to a single value. This can happen in
167 // loop cases when we get a PHI of itself and one other value.
168 if (Value *V =
169 simplifyInstruction(I: InsertedPHI, Q: BB->getDataLayout())) {
170 InsertedPHI->eraseFromParent();
171 return V;
172 }
173
174 // Set the DebugLoc of the inserted PHI, if available.
175 DebugLoc DL;
176 if (BasicBlock::iterator It = BB->getFirstNonPHIIt(); It != BB->end())
177 DL = It->getDebugLoc();
178 InsertedPHI->setDebugLoc(DL);
179
180 // If the client wants to know about all new instructions, tell it.
181 if (InsertedPHIs) InsertedPHIs->push_back(Elt: InsertedPHI);
182
183 LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
184 return InsertedPHI;
185}
186
187void SSAUpdater::RewriteUse(Use &U) {
188 Instruction *User = cast<Instruction>(Val: U.getUser());
189
190 Value *V;
191 if (PHINode *UserPN = dyn_cast<PHINode>(Val: User))
192 V = GetValueAtEndOfBlock(BB: UserPN->getIncomingBlock(U));
193 else
194 V = GetValueInMiddleOfBlock(BB: User->getParent());
195
196 U.set(V);
197}
198
199void SSAUpdater::UpdateDebugValues(Instruction *I) {
200 SmallVector<DbgValueInst *, 4> DbgValues;
201 SmallVector<DbgVariableRecord *, 4> DbgVariableRecords;
202 llvm::findDbgValues(DbgValues, V: I, DbgVariableRecords: &DbgVariableRecords);
203 for (auto &DbgValue : DbgValues) {
204 if (DbgValue->getParent() == I->getParent())
205 continue;
206 UpdateDebugValue(I, DbgValue);
207 }
208 for (auto &DVR : DbgVariableRecords) {
209 if (DVR->getParent() == I->getParent())
210 continue;
211 UpdateDebugValue(I, DbgValue: DVR);
212 }
213}
214
215void SSAUpdater::UpdateDebugValues(Instruction *I,
216 SmallVectorImpl<DbgValueInst *> &DbgValues) {
217 for (auto &DbgValue : DbgValues) {
218 UpdateDebugValue(I, DbgValue);
219 }
220}
221
222void SSAUpdater::UpdateDebugValues(
223 Instruction *I, SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords) {
224 for (auto &DVR : DbgVariableRecords) {
225 UpdateDebugValue(I, DbgValue: DVR);
226 }
227}
228
229void SSAUpdater::UpdateDebugValue(Instruction *I, DbgValueInst *DbgValue) {
230 BasicBlock *UserBB = DbgValue->getParent();
231 if (HasValueForBlock(BB: UserBB)) {
232 Value *NewVal = GetValueAtEndOfBlock(BB: UserBB);
233 DbgValue->replaceVariableLocationOp(OldValue: I, NewValue: NewVal);
234 } else
235 DbgValue->setKillLocation();
236}
237
238void SSAUpdater::UpdateDebugValue(Instruction *I, DbgVariableRecord *DVR) {
239 BasicBlock *UserBB = DVR->getParent();
240 if (HasValueForBlock(BB: UserBB)) {
241 Value *NewVal = GetValueAtEndOfBlock(BB: UserBB);
242 DVR->replaceVariableLocationOp(OldValue: I, NewValue: NewVal);
243 } else
244 DVR->setKillLocation();
245}
246
247void SSAUpdater::RewriteUseAfterInsertions(Use &U) {
248 Instruction *User = cast<Instruction>(Val: U.getUser());
249
250 Value *V;
251 if (PHINode *UserPN = dyn_cast<PHINode>(Val: User))
252 V = GetValueAtEndOfBlock(BB: UserPN->getIncomingBlock(U));
253 else
254 V = GetValueAtEndOfBlock(BB: User->getParent());
255
256 U.set(V);
257}
258
259namespace llvm {
260
261template<>
262class SSAUpdaterTraits<SSAUpdater> {
263public:
264 using BlkT = BasicBlock;
265 using ValT = Value *;
266 using PhiT = PHINode;
267 using BlkSucc_iterator = succ_iterator;
268
269 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return succ_begin(BB); }
270 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return succ_end(BB); }
271
272 class PHI_iterator {
273 private:
274 PHINode *PHI;
275 unsigned idx;
276
277 public:
278 explicit PHI_iterator(PHINode *P) // begin iterator
279 : PHI(P), idx(0) {}
280 PHI_iterator(PHINode *P, bool) // end iterator
281 : PHI(P), idx(PHI->getNumIncomingValues()) {}
282
283 PHI_iterator &operator++() { ++idx; return *this; }
284 bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
285 bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
286
287 Value *getIncomingValue() { return PHI->getIncomingValue(i: idx); }
288 BasicBlock *getIncomingBlock() { return PHI->getIncomingBlock(i: idx); }
289 };
290
291 static PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
292 static PHI_iterator PHI_end(PhiT *PHI) {
293 return PHI_iterator(PHI, true);
294 }
295
296 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
297 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
298 static void FindPredecessorBlocks(BasicBlock *BB,
299 SmallVectorImpl<BasicBlock *> *Preds) {
300 // We can get our predecessor info by walking the pred_iterator list,
301 // but it is relatively slow. If we already have PHI nodes in this
302 // block, walk one of them to get the predecessor list instead.
303 if (PHINode *SomePhi = dyn_cast<PHINode>(Val: BB->begin()))
304 append_range(C&: *Preds, R: SomePhi->blocks());
305 else
306 append_range(C&: *Preds, R: predecessors(BB));
307 }
308
309 /// GetPoisonVal - Get a poison value of the same type as the value
310 /// being handled.
311 static Value *GetPoisonVal(BasicBlock *BB, SSAUpdater *Updater) {
312 return PoisonValue::get(T: Updater->ProtoType);
313 }
314
315 /// CreateEmptyPHI - Create a new PHI instruction in the specified block.
316 /// Reserve space for the operands but do not fill them in yet.
317 static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds,
318 SSAUpdater *Updater) {
319 PHINode *PHI =
320 PHINode::Create(Ty: Updater->ProtoType, NumReservedValues: NumPreds, NameStr: Updater->ProtoName);
321 // FIXME: Ordinarily we don't care about or try to assign DebugLocs to PHI
322 // nodes, but loop optimizations may try to use a PHI node as a DebugLoc
323 // source (e.g. if this is an induction variable), and it's not clear what
324 // location we could attach here, so mark this unknown for now.
325 PHI->setDebugLoc(DebugLoc::getUnknown());
326 PHI->insertBefore(InsertPos: BB->begin());
327 return PHI;
328 }
329
330 /// AddPHIOperand - Add the specified value as an operand of the PHI for
331 /// the specified predecessor block.
332 static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred) {
333 PHI->addIncoming(V: Val, BB: Pred);
334 }
335
336 /// ValueIsPHI - Check if a value is a PHI.
337 static PHINode *ValueIsPHI(Value *Val, SSAUpdater *Updater) {
338 return dyn_cast<PHINode>(Val);
339 }
340
341 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
342 /// operands, i.e., it was just added.
343 static PHINode *ValueIsNewPHI(Value *Val, SSAUpdater *Updater) {
344 PHINode *PHI = ValueIsPHI(Val, Updater);
345 if (PHI && PHI->getNumIncomingValues() == 0)
346 return PHI;
347 return nullptr;
348 }
349
350 /// GetPHIValue - For the specified PHI instruction, return the value
351 /// that it defines.
352 static Value *GetPHIValue(PHINode *PHI) {
353 return PHI;
354 }
355};
356
357} // end namespace llvm
358
359/// Check to see if AvailableVals has an entry for the specified BB and if so,
360/// return it. If not, construct SSA form by first calculating the required
361/// placement of PHIs and then inserting new PHIs where needed.
362Value *SSAUpdater::GetValueAtEndOfBlockInternal(BasicBlock *BB) {
363 AvailableValsTy &AvailableVals = getAvailableVals(AV);
364 if (Value *V = AvailableVals[BB])
365 return V;
366
367 SSAUpdaterImpl<SSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
368 return Impl.GetValue(BB);
369}
370
371//===----------------------------------------------------------------------===//
372// LoadAndStorePromoter Implementation
373//===----------------------------------------------------------------------===//
374
375LoadAndStorePromoter::
376LoadAndStorePromoter(ArrayRef<const Instruction *> Insts,
377 SSAUpdater &S, StringRef BaseName) : SSA(S) {
378 if (Insts.empty()) return;
379
380 const Value *SomeVal;
381 if (const LoadInst *LI = dyn_cast<LoadInst>(Val: Insts[0]))
382 SomeVal = LI;
383 else
384 SomeVal = cast<StoreInst>(Val: Insts[0])->getOperand(i_nocapture: 0);
385
386 if (BaseName.empty())
387 BaseName = SomeVal->getName();
388 SSA.Initialize(Ty: SomeVal->getType(), Name: BaseName);
389}
390
391void LoadAndStorePromoter::run(const SmallVectorImpl<Instruction *> &Insts) {
392 // First step: bucket up uses of the alloca by the block they occur in.
393 // This is important because we have to handle multiple defs/uses in a block
394 // ourselves: SSAUpdater is purely for cross-block references.
395 DenseMap<BasicBlock *, TinyPtrVector<Instruction *>> UsesByBlock;
396
397 for (Instruction *User : Insts)
398 UsesByBlock[User->getParent()].push_back(NewVal: User);
399
400 // Okay, now we can iterate over all the blocks in the function with uses,
401 // processing them. Keep track of which loads are loading a live-in value.
402 // Walk the uses in the use-list order to be determinstic.
403 SmallVector<LoadInst *, 32> LiveInLoads;
404 DenseMap<Value *, Value *> ReplacedLoads;
405
406 for (Instruction *User : Insts) {
407 BasicBlock *BB = User->getParent();
408 TinyPtrVector<Instruction *> &BlockUses = UsesByBlock[BB];
409
410 // If this block has already been processed, ignore this repeat use.
411 if (BlockUses.empty()) continue;
412
413 // Okay, this is the first use in the block. If this block just has a
414 // single user in it, we can rewrite it trivially.
415 if (BlockUses.size() == 1) {
416 // If it is a store, it is a trivial def of the value in the block.
417 if (StoreInst *SI = dyn_cast<StoreInst>(Val: User)) {
418 updateDebugInfo(I: SI);
419 SSA.AddAvailableValue(BB, V: SI->getOperand(i_nocapture: 0));
420 } else if (auto *AI = dyn_cast<AllocaInst>(Val: User)) {
421 // We treat AllocaInst as a store of an getValueToUseForAlloca value.
422 SSA.AddAvailableValue(BB, V: getValueToUseForAlloca(AI));
423 } else {
424 // Otherwise it is a load, queue it to rewrite as a live-in load.
425 LiveInLoads.push_back(Elt: cast<LoadInst>(Val: User));
426 }
427 BlockUses.clear();
428 continue;
429 }
430
431 // Otherwise, check to see if this block is all loads.
432 bool HasStore = false;
433 for (Instruction *I : BlockUses) {
434 if (isa<StoreInst>(Val: I) || isa<AllocaInst>(Val: I)) {
435 HasStore = true;
436 break;
437 }
438 }
439
440 // If so, we can queue them all as live in loads.
441 if (!HasStore) {
442 for (Instruction *I : BlockUses)
443 LiveInLoads.push_back(Elt: cast<LoadInst>(Val: I));
444 BlockUses.clear();
445 continue;
446 }
447
448 // Sort all of the interesting instructions in the block so that we don't
449 // have to scan a large block just to find a few instructions.
450 llvm::sort(
451 Start: BlockUses.begin(), End: BlockUses.end(),
452 Comp: [](Instruction *A, Instruction *B) { return A->comesBefore(Other: B); });
453
454 // Otherwise, we have mixed loads and stores (or just a bunch of stores).
455 // Since SSAUpdater is purely for cross-block values, we need to determine
456 // the order of these instructions in the block. If the first use in the
457 // block is a load, then it uses the live in value. The last store defines
458 // the live out value.
459 Value *StoredValue = nullptr;
460 for (Instruction *I : BlockUses) {
461 if (LoadInst *L = dyn_cast<LoadInst>(Val: I)) {
462 // If we haven't seen a store yet, this is a live in use, otherwise
463 // use the stored value.
464 if (StoredValue) {
465 replaceLoadWithValue(LI: L, V: StoredValue);
466 L->replaceAllUsesWith(V: StoredValue);
467 ReplacedLoads[L] = StoredValue;
468 } else {
469 LiveInLoads.push_back(Elt: L);
470 }
471 continue;
472 }
473
474 if (StoreInst *SI = dyn_cast<StoreInst>(Val: I)) {
475 updateDebugInfo(I: SI);
476
477 // Remember that this is the active value in the block.
478 StoredValue = SI->getOperand(i_nocapture: 0);
479 } else if (auto *AI = dyn_cast<AllocaInst>(Val: I)) {
480 // Check if this an alloca, in which case we treat it as a store of
481 // getValueToUseForAlloca.
482 StoredValue = getValueToUseForAlloca(AI);
483 }
484 }
485
486 // The last stored value that happened is the live-out for the block.
487 assert(StoredValue && "Already checked that there is a store in block");
488 SSA.AddAvailableValue(BB, V: StoredValue);
489 BlockUses.clear();
490 }
491
492 // Okay, now we rewrite all loads that use live-in values in the loop,
493 // inserting PHI nodes as necessary.
494 for (LoadInst *ALoad : LiveInLoads) {
495 Value *NewVal = SSA.GetValueInMiddleOfBlock(BB: ALoad->getParent());
496 replaceLoadWithValue(LI: ALoad, V: NewVal);
497
498 // Avoid assertions in unreachable code.
499 if (NewVal == ALoad) NewVal = PoisonValue::get(T: NewVal->getType());
500 ALoad->replaceAllUsesWith(V: NewVal);
501 ReplacedLoads[ALoad] = NewVal;
502 }
503
504 // Allow the client to do stuff before we start nuking things.
505 doExtraRewritesBeforeFinalDeletion();
506
507 // Now that everything is rewritten, delete the old instructions from the
508 // function. They should all be dead now.
509 for (Instruction *User : Insts) {
510 if (!shouldDelete(I: User))
511 continue;
512
513 // If this is a load that still has uses, then the load must have been added
514 // as a live value in the SSAUpdate data structure for a block (e.g. because
515 // the loaded value was stored later). In this case, we need to recursively
516 // propagate the updates until we get to the real value.
517 if (!User->use_empty()) {
518 Value *NewVal = ReplacedLoads[User];
519 assert(NewVal && "not a replaced load?");
520
521 // Propagate down to the ultimate replacee. The intermediately loads
522 // could theoretically already have been deleted, so we don't want to
523 // dereference the Value*'s.
524 DenseMap<Value*, Value*>::iterator RLI = ReplacedLoads.find(Val: NewVal);
525 while (RLI != ReplacedLoads.end()) {
526 NewVal = RLI->second;
527 RLI = ReplacedLoads.find(Val: NewVal);
528 }
529
530 replaceLoadWithValue(LI: cast<LoadInst>(Val: User), V: NewVal);
531 User->replaceAllUsesWith(V: NewVal);
532 }
533
534 instructionDeleted(I: User);
535 User->eraseFromParent();
536 }
537}
538