blob: 2ecae356ff96fbdf83288c8e0b8fa30084ea1792 [file] [log] [blame]
Chris Lattner93f3bcf2009-10-10 09:04:27 +00001//===- SSAUpdater.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 SSAUpdater class.
11//
12//===----------------------------------------------------------------------===//
13
Bob Wilson84bd6b02010-04-17 03:08:24 +000014#define DEBUG_TYPE "ssaupdater"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000015#include "llvm/Instructions.h"
16#include "llvm/ADT/DenseMap.h"
Duncan Sandscdbd9922010-11-16 17:41:24 +000017#include "llvm/Analysis/InstructionSimplify.h"
Bob Wilson84bd6b02010-04-17 03:08:24 +000018#include "llvm/Support/AlignOf.h"
19#include "llvm/Support/Allocator.h"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000020#include "llvm/Support/CFG.h"
21#include "llvm/Support/Debug.h"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000022#include "llvm/Support/raw_ostream.h"
Bob Wilson4aad88d2010-05-04 23:18:19 +000023#include "llvm/Transforms/Utils/SSAUpdater.h"
24#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000025using namespace llvm;
26
Bob Wilson84bd6b02010-04-17 03:08:24 +000027typedef DenseMap<BasicBlock*, Value*> AvailableValsTy;
Chris Lattner93f3bcf2009-10-10 09:04:27 +000028static AvailableValsTy &getAvailableVals(void *AV) {
29 return *static_cast<AvailableValsTy*>(AV);
30}
31
Chris Lattnerf5a1fb62009-10-10 23:15:24 +000032SSAUpdater::SSAUpdater(SmallVectorImpl<PHINode*> *NewPHI)
Duncan Sandsfc6e29d2010-09-02 08:14:03 +000033 : AV(0), ProtoType(0), ProtoName(), InsertedPHIs(NewPHI) {}
Chris Lattner93f3bcf2009-10-10 09:04:27 +000034
35SSAUpdater::~SSAUpdater() {
36 delete &getAvailableVals(AV);
Chris Lattner93f3bcf2009-10-10 09:04:27 +000037}
38
39/// Initialize - Reset this object to get ready for a new set of SSA
Duncan Sandsfc6e29d2010-09-02 08:14:03 +000040/// updates with type 'Ty'. PHI nodes get a name based on 'Name'.
41void SSAUpdater::Initialize(const Type *Ty, StringRef Name) {
Chris Lattner93f3bcf2009-10-10 09:04:27 +000042 if (AV == 0)
43 AV = new AvailableValsTy();
44 else
45 getAvailableVals(AV).clear();
Duncan Sandsfc6e29d2010-09-02 08:14:03 +000046 ProtoType = Ty;
47 ProtoName = Name;
Chris Lattner93f3bcf2009-10-10 09:04:27 +000048}
49
Chris Lattner0bef5622009-10-10 23:41:48 +000050/// HasValueForBlock - Return true if the SSAUpdater already has a value for
51/// the specified block.
52bool SSAUpdater::HasValueForBlock(BasicBlock *BB) const {
53 return getAvailableVals(AV).count(BB);
54}
55
Chris Lattner93f3bcf2009-10-10 09:04:27 +000056/// AddAvailableValue - Indicate that a rewritten value is available in the
57/// specified block with the specified value.
58void SSAUpdater::AddAvailableValue(BasicBlock *BB, Value *V) {
Duncan Sandsfc6e29d2010-09-02 08:14:03 +000059 assert(ProtoType != 0 && "Need to initialize SSAUpdater");
60 assert(ProtoType == V->getType() &&
Chris Lattner93f3bcf2009-10-10 09:04:27 +000061 "All rewritten values must have the same type");
62 getAvailableVals(AV)[BB] = V;
63}
64
Bob Wilsone98585e2010-01-27 22:01:02 +000065/// IsEquivalentPHI - Check if PHI has the same incoming value as specified
66/// in ValueMapping for each predecessor block.
Bob Wilson84bd6b02010-04-17 03:08:24 +000067static bool IsEquivalentPHI(PHINode *PHI,
Bob Wilsone98585e2010-01-27 22:01:02 +000068 DenseMap<BasicBlock*, Value*> &ValueMapping) {
69 unsigned PHINumValues = PHI->getNumIncomingValues();
70 if (PHINumValues != ValueMapping.size())
71 return false;
72
73 // Scan the phi to see if it matches.
74 for (unsigned i = 0, e = PHINumValues; i != e; ++i)
75 if (ValueMapping[PHI->getIncomingBlock(i)] !=
76 PHI->getIncomingValue(i)) {
77 return false;
78 }
79
80 return true;
81}
82
Chris Lattner1a8d4de2009-10-10 23:00:11 +000083/// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
84/// live at the end of the specified block.
Chris Lattner5fb10722009-10-10 22:41:58 +000085Value *SSAUpdater::GetValueAtEndOfBlock(BasicBlock *BB) {
Chris Lattner5fb10722009-10-10 22:41:58 +000086 Value *Res = GetValueAtEndOfBlockInternal(BB);
Chris Lattner93f3bcf2009-10-10 09:04:27 +000087 return Res;
88}
89
Chris Lattner1a8d4de2009-10-10 23:00:11 +000090/// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
91/// is live in the middle of the specified block.
92///
93/// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
94/// important case: if there is a definition of the rewritten value after the
95/// 'use' in BB. Consider code like this:
96///
97/// X1 = ...
98/// SomeBB:
99/// use(X)
100/// X2 = ...
101/// br Cond, SomeBB, OutBB
102///
103/// In this case, there are two values (X1 and X2) added to the AvailableVals
104/// set by the client of the rewriter, and those values are both live out of
105/// their respective blocks. However, the use of X happens in the *middle* of
106/// a block. Because of this, we need to insert a new PHI node in SomeBB to
107/// merge the appropriate values, and this value isn't live out of the block.
108///
109Value *SSAUpdater::GetValueInMiddleOfBlock(BasicBlock *BB) {
110 // If there is no definition of the renamed variable in this block, just use
111 // GetValueAtEndOfBlock to do our work.
Bob Wilson84bd6b02010-04-17 03:08:24 +0000112 if (!HasValueForBlock(BB))
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000113 return GetValueAtEndOfBlock(BB);
Duncan Sandsed903422009-10-16 15:20:13 +0000114
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000115 // Otherwise, we have the hard case. Get the live-in values for each
116 // predecessor.
117 SmallVector<std::pair<BasicBlock*, Value*>, 8> PredValues;
118 Value *SingularValue = 0;
Duncan Sandsed903422009-10-16 15:20:13 +0000119
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000120 // We can get our predecessor info by walking the pred_iterator list, but it
121 // is relatively slow. If we already have PHI nodes in this block, walk one
122 // of them to get the predecessor list instead.
123 if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) {
124 for (unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) {
125 BasicBlock *PredBB = SomePhi->getIncomingBlock(i);
126 Value *PredVal = GetValueAtEndOfBlock(PredBB);
127 PredValues.push_back(std::make_pair(PredBB, PredVal));
Duncan Sandsed903422009-10-16 15:20:13 +0000128
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000129 // Compute SingularValue.
130 if (i == 0)
131 SingularValue = PredVal;
132 else if (PredVal != SingularValue)
133 SingularValue = 0;
134 }
135 } else {
136 bool isFirstPred = true;
137 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
138 BasicBlock *PredBB = *PI;
139 Value *PredVal = GetValueAtEndOfBlock(PredBB);
140 PredValues.push_back(std::make_pair(PredBB, PredVal));
Duncan Sandsed903422009-10-16 15:20:13 +0000141
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000142 // Compute SingularValue.
143 if (isFirstPred) {
144 SingularValue = PredVal;
145 isFirstPred = false;
146 } else if (PredVal != SingularValue)
147 SingularValue = 0;
148 }
149 }
Duncan Sandsed903422009-10-16 15:20:13 +0000150
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000151 // If there are no predecessors, just return undef.
152 if (PredValues.empty())
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000153 return UndefValue::get(ProtoType);
Duncan Sandsed903422009-10-16 15:20:13 +0000154
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000155 // Otherwise, if all the merged values are the same, just use it.
156 if (SingularValue != 0)
157 return SingularValue;
Duncan Sandsed903422009-10-16 15:20:13 +0000158
Bob Wilson84bd6b02010-04-17 03:08:24 +0000159 // Otherwise, we do need a PHI: check to see if we already have one available
160 // in this block that produces the right value.
161 if (isa<PHINode>(BB->begin())) {
162 DenseMap<BasicBlock*, Value*> ValueMapping(PredValues.begin(),
163 PredValues.end());
164 PHINode *SomePHI;
165 for (BasicBlock::iterator It = BB->begin();
166 (SomePHI = dyn_cast<PHINode>(It)); ++It) {
167 if (IsEquivalentPHI(SomePHI, ValueMapping))
168 return SomePHI;
169 }
170 }
Bob Wilsone98585e2010-01-27 22:01:02 +0000171
Chris Lattner4c1e3da2009-12-21 07:16:11 +0000172 // Ok, we have no way out, insert a new one now.
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000173 PHINode *InsertedPHI = PHINode::Create(ProtoType, ProtoName, &BB->front());
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000174 InsertedPHI->reserveOperandSpace(PredValues.size());
Duncan Sandsed903422009-10-16 15:20:13 +0000175
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000176 // Fill in all the predecessors of the PHI.
177 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
178 InsertedPHI->addIncoming(PredValues[i].second, PredValues[i].first);
Duncan Sandsed903422009-10-16 15:20:13 +0000179
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000180 // See if the PHI node can be merged to a single value. This can happen in
181 // loop cases when we get a PHI of itself and one other value.
Duncan Sandscdbd9922010-11-16 17:41:24 +0000182 if (Value *V = SimplifyInstruction(InsertedPHI)) {
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000183 InsertedPHI->eraseFromParent();
Duncan Sandscdbd9922010-11-16 17:41:24 +0000184 return V;
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000185 }
Chris Lattnerf5a1fb62009-10-10 23:15:24 +0000186
187 // If the client wants to know about all new instructions, tell it.
188 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
Duncan Sandsed903422009-10-16 15:20:13 +0000189
David Greene1af40ca2010-01-05 01:26:49 +0000190 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000191 return InsertedPHI;
192}
193
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000194/// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
195/// which use their value in the corresponding predecessor.
196void SSAUpdater::RewriteUse(Use &U) {
197 Instruction *User = cast<Instruction>(U.getUser());
Bob Wilson84bd6b02010-04-17 03:08:24 +0000198
Chris Lattner88a86242009-10-20 20:27:49 +0000199 Value *V;
200 if (PHINode *UserPN = dyn_cast<PHINode>(User))
201 V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U));
202 else
203 V = GetValueInMiddleOfBlock(User->getParent());
Duncan Sandsed903422009-10-16 15:20:13 +0000204
Torok Edwinf9933272009-10-20 15:42:00 +0000205 U.set(V);
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000206}
207
Chris Lattnerffd9bee2010-08-29 04:54:06 +0000208/// RewriteUseAfterInsertions - Rewrite a use, just like RewriteUse. However,
209/// this version of the method can rewrite uses in the same block as a
210/// definition, because it assumes that all uses of a value are below any
211/// inserted values.
212void SSAUpdater::RewriteUseAfterInsertions(Use &U) {
213 Instruction *User = cast<Instruction>(U.getUser());
214
215 Value *V;
216 if (PHINode *UserPN = dyn_cast<PHINode>(User))
217 V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U));
218 else
219 V = GetValueAtEndOfBlock(User->getParent());
220
221 U.set(V);
222}
223
Bob Wilson4aad88d2010-05-04 23:18:19 +0000224/// PHIiter - Iterator for PHI operands. This is used for the PHI_iterator
225/// in the SSAUpdaterImpl template.
226namespace {
227 class PHIiter {
228 private:
229 PHINode *PHI;
230 unsigned idx;
231
232 public:
233 explicit PHIiter(PHINode *P) // begin iterator
234 : PHI(P), idx(0) {}
235 PHIiter(PHINode *P, bool) // end iterator
236 : PHI(P), idx(PHI->getNumIncomingValues()) {}
237
238 PHIiter &operator++() { ++idx; return *this; }
239 bool operator==(const PHIiter& x) const { return idx == x.idx; }
240 bool operator!=(const PHIiter& x) const { return !operator==(x); }
241 Value *getIncomingValue() { return PHI->getIncomingValue(idx); }
242 BasicBlock *getIncomingBlock() { return PHI->getIncomingBlock(idx); }
243 };
244}
245
246/// SSAUpdaterTraits<SSAUpdater> - Traits for the SSAUpdaterImpl template,
247/// specialized for SSAUpdater.
248namespace llvm {
249template<>
250class SSAUpdaterTraits<SSAUpdater> {
251public:
252 typedef BasicBlock BlkT;
253 typedef Value *ValT;
254 typedef PHINode PhiT;
255
256 typedef succ_iterator BlkSucc_iterator;
257 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return succ_begin(BB); }
258 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return succ_end(BB); }
259
260 typedef PHIiter PHI_iterator;
261 static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
262 static inline PHI_iterator PHI_end(PhiT *PHI) {
263 return PHI_iterator(PHI, true);
264 }
265
266 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
267 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
268 static void FindPredecessorBlocks(BasicBlock *BB,
269 SmallVectorImpl<BasicBlock*> *Preds) {
270 // We can get our predecessor info by walking the pred_iterator list,
271 // but it is relatively slow. If we already have PHI nodes in this
272 // block, walk one of them to get the predecessor list instead.
273 if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) {
274 for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI)
275 Preds->push_back(SomePhi->getIncomingBlock(PI));
276 } else {
277 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
278 Preds->push_back(*PI);
279 }
280 }
281
282 /// GetUndefVal - Get an undefined value of the same type as the value
283 /// being handled.
284 static Value *GetUndefVal(BasicBlock *BB, SSAUpdater *Updater) {
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000285 return UndefValue::get(Updater->ProtoType);
Bob Wilson4aad88d2010-05-04 23:18:19 +0000286 }
287
288 /// CreateEmptyPHI - Create a new PHI instruction in the specified block.
289 /// Reserve space for the operands but do not fill them in yet.
290 static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds,
291 SSAUpdater *Updater) {
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000292 PHINode *PHI = PHINode::Create(Updater->ProtoType, Updater->ProtoName,
Bob Wilson4aad88d2010-05-04 23:18:19 +0000293 &BB->front());
294 PHI->reserveOperandSpace(NumPreds);
295 return PHI;
296 }
297
298 /// AddPHIOperand - Add the specified value as an operand of the PHI for
299 /// the specified predecessor block.
300 static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred) {
301 PHI->addIncoming(Val, Pred);
302 }
303
304 /// InstrIsPHI - Check if an instruction is a PHI.
305 ///
306 static PHINode *InstrIsPHI(Instruction *I) {
307 return dyn_cast<PHINode>(I);
308 }
309
310 /// ValueIsPHI - Check if a value is a PHI.
311 ///
312 static PHINode *ValueIsPHI(Value *Val, SSAUpdater *Updater) {
313 return dyn_cast<PHINode>(Val);
314 }
315
316 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
317 /// operands, i.e., it was just added.
318 static PHINode *ValueIsNewPHI(Value *Val, SSAUpdater *Updater) {
319 PHINode *PHI = ValueIsPHI(Val, Updater);
320 if (PHI && PHI->getNumIncomingValues() == 0)
321 return PHI;
322 return 0;
323 }
324
325 /// GetPHIValue - For the specified PHI instruction, return the value
326 /// that it defines.
327 static Value *GetPHIValue(PHINode *PHI) {
328 return PHI;
329 }
330};
331
332} // End llvm namespace
333
Chris Lattner5fb10722009-10-10 22:41:58 +0000334/// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
335/// for the specified BB and if so, return it. If not, construct SSA form by
Bob Wilson84bd6b02010-04-17 03:08:24 +0000336/// first calculating the required placement of PHIs and then inserting new
337/// PHIs where needed.
Chris Lattner5fb10722009-10-10 22:41:58 +0000338Value *SSAUpdater::GetValueAtEndOfBlockInternal(BasicBlock *BB) {
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000339 AvailableValsTy &AvailableVals = getAvailableVals(AV);
Bob Wilson84bd6b02010-04-17 03:08:24 +0000340 if (Value *V = AvailableVals[BB])
341 return V;
Duncan Sandsed903422009-10-16 15:20:13 +0000342
Bob Wilson4aad88d2010-05-04 23:18:19 +0000343 SSAUpdaterImpl<SSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
344 return Impl.GetValue(BB);
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000345}
Chris Lattnera2d845a2011-01-14 19:36:13 +0000346
347//===----------------------------------------------------------------------===//
348// LoadAndStorePromoter Implementation
349//===----------------------------------------------------------------------===//
350
351void LoadAndStorePromoter::run(StringRef BaseName,
352 const SmallVectorImpl<Instruction*> &Insts,
353 SSAUpdater *SSA) {
354 if (Insts.empty()) return;
355
356 // If no SSAUpdater was provided, use a default one. This allows the client
357 // to capture inserted PHI nodes etc if they want.
358 SSAUpdater DefaultSSA;
359 if (SSA == 0) SSA = &DefaultSSA;
360
361 const Type *ValTy;
362 if (LoadInst *LI = dyn_cast<LoadInst>(Insts[0]))
363 ValTy = LI->getType();
364 else
365 ValTy = cast<StoreInst>(Insts[0])->getOperand(0)->getType();
366
367 SSA->Initialize(ValTy, BaseName);
368
369 // First step: bucket up uses of the alloca by the block they occur in.
370 // This is important because we have to handle multiple defs/uses in a block
371 // ourselves: SSAUpdater is purely for cross-block references.
372 // FIXME: Want a TinyVector<Instruction*> since there is often 0/1 element.
373 DenseMap<BasicBlock*, std::vector<Instruction*> > UsesByBlock;
374
375 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
376 Instruction *User = Insts[i];
377 UsesByBlock[User->getParent()].push_back(User);
378 }
379
380 // Okay, now we can iterate over all the blocks in the function with uses,
381 // processing them. Keep track of which loads are loading a live-in value.
382 // Walk the uses in the use-list order to be determinstic.
383 SmallVector<LoadInst*, 32> LiveInLoads;
384 DenseMap<Value*, Value*> ReplacedLoads;
385
386 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
387 Instruction *User = Insts[i];
388 BasicBlock *BB = User->getParent();
389 std::vector<Instruction*> &BlockUses = UsesByBlock[BB];
390
391 // If this block has already been processed, ignore this repeat use.
392 if (BlockUses.empty()) continue;
393
394 // Okay, this is the first use in the block. If this block just has a
395 // single user in it, we can rewrite it trivially.
396 if (BlockUses.size() == 1) {
397 // If it is a store, it is a trivial def of the value in the block.
398 if (StoreInst *SI = dyn_cast<StoreInst>(User))
399 SSA->AddAvailableValue(BB, SI->getOperand(0));
400 else
401 // Otherwise it is a load, queue it to rewrite as a live-in load.
402 LiveInLoads.push_back(cast<LoadInst>(User));
403 BlockUses.clear();
404 continue;
405 }
406
407 // Otherwise, check to see if this block is all loads.
408 bool HasStore = false;
409 for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) {
410 if (isa<StoreInst>(BlockUses[i])) {
411 HasStore = true;
412 break;
413 }
414 }
415
416 // If so, we can queue them all as live in loads. We don't have an
417 // efficient way to tell which on is first in the block and don't want to
418 // scan large blocks, so just add all loads as live ins.
419 if (!HasStore) {
420 for (unsigned i = 0, e = BlockUses.size(); i != e; ++i)
421 LiveInLoads.push_back(cast<LoadInst>(BlockUses[i]));
422 BlockUses.clear();
423 continue;
424 }
425
426 // Otherwise, we have mixed loads and stores (or just a bunch of stores).
427 // Since SSAUpdater is purely for cross-block values, we need to determine
428 // the order of these instructions in the block. If the first use in the
429 // block is a load, then it uses the live in value. The last store defines
430 // the live out value. We handle this by doing a linear scan of the block.
431 Value *StoredValue = 0;
432 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
433 if (LoadInst *L = dyn_cast<LoadInst>(II)) {
434 // If this is a load from an unrelated pointer, ignore it.
435 if (!isInstInList(L, Insts)) continue;
436
437 // If we haven't seen a store yet, this is a live in use, otherwise
438 // use the stored value.
439 if (StoredValue) {
440 L->replaceAllUsesWith(StoredValue);
441 ReplacedLoads[L] = StoredValue;
442 } else {
443 LiveInLoads.push_back(L);
444 }
445 continue;
446 }
447
448 if (StoreInst *S = dyn_cast<StoreInst>(II)) {
449 // If this is a store to an unrelated pointer, ignore it.
450 if (!isInstInList(S, Insts)) continue;
451
452 // Remember that this is the active value in the block.
453 StoredValue = S->getOperand(0);
454 }
455 }
456
457 // The last stored value that happened is the live-out for the block.
458 assert(StoredValue && "Already checked that there is a store in block");
459 SSA->AddAvailableValue(BB, StoredValue);
460 BlockUses.clear();
461 }
462
463 // Okay, now we rewrite all loads that use live-in values in the loop,
464 // inserting PHI nodes as necessary.
465 for (unsigned i = 0, e = LiveInLoads.size(); i != e; ++i) {
466 LoadInst *ALoad = LiveInLoads[i];
467 Value *NewVal = SSA->GetValueInMiddleOfBlock(ALoad->getParent());
468 ALoad->replaceAllUsesWith(NewVal);
469 ReplacedLoads[ALoad] = NewVal;
470 }
471
472 // Now that everything is rewritten, delete the old instructions from the
473 // function. They should all be dead now.
474 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
475 Instruction *User = Insts[i];
476
477 // If this is a load that still has uses, then the load must have been added
478 // as a live value in the SSAUpdate data structure for a block (e.g. because
479 // the loaded value was stored later). In this case, we need to recursively
480 // propagate the updates until we get to the real value.
481 if (!User->use_empty()) {
482 Value *NewVal = ReplacedLoads[User];
483 assert(NewVal && "not a replaced load?");
484
485 // Propagate down to the ultimate replacee. The intermediately loads
486 // could theoretically already have been deleted, so we don't want to
487 // dereference the Value*'s.
488 DenseMap<Value*, Value*>::iterator RLI = ReplacedLoads.find(NewVal);
489 while (RLI != ReplacedLoads.end()) {
490 NewVal = RLI->second;
491 RLI = ReplacedLoads.find(NewVal);
492 }
493
494 User->replaceAllUsesWith(NewVal);
495 }
496
497 User->eraseFromParent();
498 }
499}