blob: 30adbfac058f0d21a84084fa1c4e7a463f07eccc [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Transforms/Utils/SSAUpdater.h"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000016#include "llvm/ADT/DenseMap.h"
Chris Lattner84063572011-07-18 01:43:58 +000017#include "llvm/ADT/TinyPtrVector.h"
Duncan Sandscdbd9922010-11-16 17:41:24 +000018#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/IntrinsicInst.h"
Bob Wilson84bd6b02010-04-17 03:08:24 +000022#include "llvm/Support/AlignOf.h"
23#include "llvm/Support/Allocator.h"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000024#include "llvm/Support/CFG.h"
25#include "llvm/Support/Debug.h"
Chris Lattner93f3bcf2009-10-10 09:04:27 +000026#include "llvm/Support/raw_ostream.h"
Devang Patel40348e82011-04-29 22:28:59 +000027#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Cameron Zwarichc8279392011-05-24 03:10:43 +000028#include "llvm/Transforms/Utils/Local.h"
Bob Wilson4aad88d2010-05-04 23:18:19 +000029#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
Devang Patel40348e82011-04-29 22:28:59 +000030
Chris Lattner93f3bcf2009-10-10 09:04:27 +000031using namespace llvm;
32
Bob Wilson84bd6b02010-04-17 03:08:24 +000033typedef DenseMap<BasicBlock*, Value*> AvailableValsTy;
Chris Lattner93f3bcf2009-10-10 09:04:27 +000034static AvailableValsTy &getAvailableVals(void *AV) {
35 return *static_cast<AvailableValsTy*>(AV);
36}
37
Chris Lattnerf5a1fb62009-10-10 23:15:24 +000038SSAUpdater::SSAUpdater(SmallVectorImpl<PHINode*> *NewPHI)
Duncan Sandsfc6e29d2010-09-02 08:14:03 +000039 : AV(0), ProtoType(0), ProtoName(), InsertedPHIs(NewPHI) {}
Chris Lattner93f3bcf2009-10-10 09:04:27 +000040
41SSAUpdater::~SSAUpdater() {
Richard Smith1cec7a02012-08-17 21:42:44 +000042 delete static_cast<AvailableValsTy*>(AV);
Chris Lattner93f3bcf2009-10-10 09:04:27 +000043}
44
Chris Lattnerdb125cf2011-07-18 04:54:35 +000045void SSAUpdater::Initialize(Type *Ty, StringRef Name) {
Chris Lattner93f3bcf2009-10-10 09:04:27 +000046 if (AV == 0)
47 AV = new AvailableValsTy();
48 else
49 getAvailableVals(AV).clear();
Duncan Sandsfc6e29d2010-09-02 08:14:03 +000050 ProtoType = Ty;
51 ProtoName = Name;
Chris Lattner93f3bcf2009-10-10 09:04:27 +000052}
53
Chris Lattner0bef5622009-10-10 23:41:48 +000054bool SSAUpdater::HasValueForBlock(BasicBlock *BB) const {
55 return getAvailableVals(AV).count(BB);
56}
57
Chris Lattner93f3bcf2009-10-10 09:04:27 +000058void 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 Wilson84bd6b02010-04-17 03:08:24 +000065static bool IsEquivalentPHI(PHINode *PHI,
Chris Lattner2a6cbba2013-10-14 16:05:55 +000066 SmallDenseMap<BasicBlock*, Value*, 8> &ValueMapping) {
Bob Wilsone98585e2010-01-27 22:01:02 +000067 unsigned PHINumValues = PHI->getNumIncomingValues();
68 if (PHINumValues != ValueMapping.size())
69 return false;
70
71 // Scan the phi to see if it matches.
72 for (unsigned i = 0, e = PHINumValues; i != e; ++i)
73 if (ValueMapping[PHI->getIncomingBlock(i)] !=
74 PHI->getIncomingValue(i)) {
75 return false;
76 }
77
78 return true;
79}
80
Chris Lattner5fb10722009-10-10 22:41:58 +000081Value *SSAUpdater::GetValueAtEndOfBlock(BasicBlock *BB) {
Chris Lattner5fb10722009-10-10 22:41:58 +000082 Value *Res = GetValueAtEndOfBlockInternal(BB);
Chris Lattner93f3bcf2009-10-10 09:04:27 +000083 return Res;
84}
85
Chris Lattner1a8d4de2009-10-10 23:00:11 +000086Value *SSAUpdater::GetValueInMiddleOfBlock(BasicBlock *BB) {
87 // If there is no definition of the renamed variable in this block, just use
88 // GetValueAtEndOfBlock to do our work.
Bob Wilson84bd6b02010-04-17 03:08:24 +000089 if (!HasValueForBlock(BB))
Chris Lattner1a8d4de2009-10-10 23:00:11 +000090 return GetValueAtEndOfBlock(BB);
Duncan Sandsed903422009-10-16 15:20:13 +000091
Chris Lattner1a8d4de2009-10-10 23:00:11 +000092 // Otherwise, we have the hard case. Get the live-in values for each
93 // predecessor.
94 SmallVector<std::pair<BasicBlock*, Value*>, 8> PredValues;
95 Value *SingularValue = 0;
Duncan Sandsed903422009-10-16 15:20:13 +000096
Chris Lattner1a8d4de2009-10-10 23:00:11 +000097 // We can get our predecessor info by walking the pred_iterator list, but it
98 // is relatively slow. If we already have PHI nodes in this block, walk one
99 // of them to get the predecessor list instead.
100 if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) {
101 for (unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) {
102 BasicBlock *PredBB = SomePhi->getIncomingBlock(i);
103 Value *PredVal = GetValueAtEndOfBlock(PredBB);
104 PredValues.push_back(std::make_pair(PredBB, PredVal));
Duncan Sandsed903422009-10-16 15:20:13 +0000105
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000106 // Compute SingularValue.
107 if (i == 0)
108 SingularValue = PredVal;
109 else if (PredVal != SingularValue)
110 SingularValue = 0;
111 }
112 } else {
113 bool isFirstPred = true;
114 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
115 BasicBlock *PredBB = *PI;
116 Value *PredVal = GetValueAtEndOfBlock(PredBB);
117 PredValues.push_back(std::make_pair(PredBB, PredVal));
Duncan Sandsed903422009-10-16 15:20:13 +0000118
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000119 // Compute SingularValue.
120 if (isFirstPred) {
121 SingularValue = PredVal;
122 isFirstPred = false;
123 } else if (PredVal != SingularValue)
124 SingularValue = 0;
125 }
126 }
Duncan Sandsed903422009-10-16 15:20:13 +0000127
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000128 // If there are no predecessors, just return undef.
129 if (PredValues.empty())
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000130 return UndefValue::get(ProtoType);
Duncan Sandsed903422009-10-16 15:20:13 +0000131
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000132 // Otherwise, if all the merged values are the same, just use it.
133 if (SingularValue != 0)
134 return SingularValue;
Duncan Sandsed903422009-10-16 15:20:13 +0000135
Bob Wilson84bd6b02010-04-17 03:08:24 +0000136 // Otherwise, we do need a PHI: check to see if we already have one available
137 // in this block that produces the right value.
138 if (isa<PHINode>(BB->begin())) {
Chris Lattner2a6cbba2013-10-14 16:05:55 +0000139 SmallDenseMap<BasicBlock*, Value*, 8> ValueMapping(PredValues.begin(),
140 PredValues.end());
Bob Wilson84bd6b02010-04-17 03:08:24 +0000141 PHINode *SomePHI;
142 for (BasicBlock::iterator It = BB->begin();
143 (SomePHI = dyn_cast<PHINode>(It)); ++It) {
144 if (IsEquivalentPHI(SomePHI, ValueMapping))
145 return SomePHI;
146 }
147 }
Bob Wilsone98585e2010-01-27 22:01:02 +0000148
Chris Lattner4c1e3da2009-12-21 07:16:11 +0000149 // Ok, we have no way out, insert a new one now.
Jay Foad3ecfc862011-03-30 11:28:46 +0000150 PHINode *InsertedPHI = PHINode::Create(ProtoType, PredValues.size(),
151 ProtoName, &BB->front());
Duncan Sandsed903422009-10-16 15:20:13 +0000152
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000153 // Fill in all the predecessors of the PHI.
154 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
155 InsertedPHI->addIncoming(PredValues[i].second, PredValues[i].first);
Duncan Sandsed903422009-10-16 15:20:13 +0000156
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000157 // See if the PHI node can be merged to a single value. This can happen in
158 // loop cases when we get a PHI of itself and one other value.
Duncan Sandscdbd9922010-11-16 17:41:24 +0000159 if (Value *V = SimplifyInstruction(InsertedPHI)) {
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000160 InsertedPHI->eraseFromParent();
Duncan Sandscdbd9922010-11-16 17:41:24 +0000161 return V;
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000162 }
Chris Lattnerf5a1fb62009-10-10 23:15:24 +0000163
Eli Benderskyccaddf42012-06-25 10:13:14 +0000164 // Set the DebugLoc of the inserted PHI, if available.
165 DebugLoc DL;
166 if (const Instruction *I = BB->getFirstNonPHI())
167 DL = I->getDebugLoc();
168 InsertedPHI->setDebugLoc(DL);
Devang Patel40348e82011-04-29 22:28:59 +0000169
Chris Lattnerf5a1fb62009-10-10 23:15:24 +0000170 // If the client wants to know about all new instructions, tell it.
171 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
Duncan Sandsed903422009-10-16 15:20:13 +0000172
David Greene1af40ca2010-01-05 01:26:49 +0000173 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Chris Lattner1a8d4de2009-10-10 23:00:11 +0000174 return InsertedPHI;
175}
176
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000177void SSAUpdater::RewriteUse(Use &U) {
178 Instruction *User = cast<Instruction>(U.getUser());
Bob Wilson84bd6b02010-04-17 03:08:24 +0000179
Chris Lattner88a86242009-10-20 20:27:49 +0000180 Value *V;
181 if (PHINode *UserPN = dyn_cast<PHINode>(User))
182 V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U));
183 else
184 V = GetValueInMiddleOfBlock(User->getParent());
Duncan Sandsed903422009-10-16 15:20:13 +0000185
Nadav Rotem7b6783a2012-08-13 23:06:54 +0000186 // Notify that users of the existing value that it is being replaced.
187 Value *OldVal = U.get();
188 if (OldVal != V && OldVal->hasValueHandle())
189 ValueHandleBase::ValueIsRAUWd(OldVal, V);
190
Torok Edwinf9933272009-10-20 15:42:00 +0000191 U.set(V);
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000192}
193
Chris Lattnerffd9bee2010-08-29 04:54:06 +0000194void SSAUpdater::RewriteUseAfterInsertions(Use &U) {
195 Instruction *User = cast<Instruction>(U.getUser());
196
197 Value *V;
198 if (PHINode *UserPN = dyn_cast<PHINode>(User))
199 V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U));
200 else
201 V = GetValueAtEndOfBlock(User->getParent());
202
203 U.set(V);
204}
205
Bob Wilson4aad88d2010-05-04 23:18:19 +0000206namespace llvm {
207template<>
208class SSAUpdaterTraits<SSAUpdater> {
209public:
210 typedef BasicBlock BlkT;
211 typedef Value *ValT;
212 typedef PHINode PhiT;
213
214 typedef succ_iterator BlkSucc_iterator;
215 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return succ_begin(BB); }
216 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return succ_end(BB); }
217
Chandler Carruthfdc2d0f2012-06-20 08:39:30 +0000218 class PHI_iterator {
219 private:
220 PHINode *PHI;
221 unsigned idx;
222
223 public:
224 explicit PHI_iterator(PHINode *P) // begin iterator
225 : PHI(P), idx(0) {}
226 PHI_iterator(PHINode *P, bool) // end iterator
227 : PHI(P), idx(PHI->getNumIncomingValues()) {}
228
229 PHI_iterator &operator++() { ++idx; return *this; }
230 bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
231 bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
232 Value *getIncomingValue() { return PHI->getIncomingValue(idx); }
233 BasicBlock *getIncomingBlock() { return PHI->getIncomingBlock(idx); }
234 };
235
236 static PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
237 static PHI_iterator PHI_end(PhiT *PHI) {
Bob Wilson4aad88d2010-05-04 23:18:19 +0000238 return PHI_iterator(PHI, true);
239 }
240
241 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
242 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
243 static void FindPredecessorBlocks(BasicBlock *BB,
244 SmallVectorImpl<BasicBlock*> *Preds) {
245 // We can get our predecessor info by walking the pred_iterator list,
246 // but it is relatively slow. If we already have PHI nodes in this
247 // block, walk one of them to get the predecessor list instead.
248 if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) {
249 for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI)
250 Preds->push_back(SomePhi->getIncomingBlock(PI));
251 } else {
252 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
253 Preds->push_back(*PI);
254 }
255 }
256
257 /// GetUndefVal - Get an undefined value of the same type as the value
258 /// being handled.
259 static Value *GetUndefVal(BasicBlock *BB, SSAUpdater *Updater) {
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000260 return UndefValue::get(Updater->ProtoType);
Bob Wilson4aad88d2010-05-04 23:18:19 +0000261 }
262
263 /// CreateEmptyPHI - Create a new PHI instruction in the specified block.
264 /// Reserve space for the operands but do not fill them in yet.
265 static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds,
266 SSAUpdater *Updater) {
Jay Foad3ecfc862011-03-30 11:28:46 +0000267 PHINode *PHI = PHINode::Create(Updater->ProtoType, NumPreds,
268 Updater->ProtoName, &BB->front());
Bob Wilson4aad88d2010-05-04 23:18:19 +0000269 return PHI;
270 }
271
272 /// AddPHIOperand - Add the specified value as an operand of the PHI for
273 /// the specified predecessor block.
274 static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred) {
275 PHI->addIncoming(Val, Pred);
276 }
277
278 /// InstrIsPHI - Check if an instruction is a PHI.
279 ///
280 static PHINode *InstrIsPHI(Instruction *I) {
281 return dyn_cast<PHINode>(I);
282 }
283
284 /// ValueIsPHI - Check if a value is a PHI.
285 ///
286 static PHINode *ValueIsPHI(Value *Val, SSAUpdater *Updater) {
287 return dyn_cast<PHINode>(Val);
288 }
289
290 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
291 /// operands, i.e., it was just added.
292 static PHINode *ValueIsNewPHI(Value *Val, SSAUpdater *Updater) {
293 PHINode *PHI = ValueIsPHI(Val, Updater);
294 if (PHI && PHI->getNumIncomingValues() == 0)
295 return PHI;
296 return 0;
297 }
298
299 /// GetPHIValue - For the specified PHI instruction, return the value
300 /// that it defines.
301 static Value *GetPHIValue(PHINode *PHI) {
302 return PHI;
303 }
304};
305
306} // End llvm namespace
307
Chandler Carruth064a6862013-07-28 22:00:33 +0000308/// Check to see if AvailableVals has an entry for the specified BB and if so,
309/// return it. If not, construct SSA form by first calculating the required
310/// placement of PHIs and then inserting new PHIs where needed.
Chris Lattner5fb10722009-10-10 22:41:58 +0000311Value *SSAUpdater::GetValueAtEndOfBlockInternal(BasicBlock *BB) {
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000312 AvailableValsTy &AvailableVals = getAvailableVals(AV);
Bob Wilson84bd6b02010-04-17 03:08:24 +0000313 if (Value *V = AvailableVals[BB])
314 return V;
Duncan Sandsed903422009-10-16 15:20:13 +0000315
Bob Wilson4aad88d2010-05-04 23:18:19 +0000316 SSAUpdaterImpl<SSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
317 return Impl.GetValue(BB);
Chris Lattner93f3bcf2009-10-10 09:04:27 +0000318}
Chris Lattnera2d845a2011-01-14 19:36:13 +0000319
320//===----------------------------------------------------------------------===//
321// LoadAndStorePromoter Implementation
322//===----------------------------------------------------------------------===//
323
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000324LoadAndStorePromoter::
325LoadAndStorePromoter(const SmallVectorImpl<Instruction*> &Insts,
Devang Patel231a5ab2011-07-06 21:09:55 +0000326 SSAUpdater &S, StringRef BaseName) : SSA(S) {
Chris Lattnera2d845a2011-01-14 19:36:13 +0000327 if (Insts.empty()) return;
328
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000329 Value *SomeVal;
Chris Lattnera2d845a2011-01-14 19:36:13 +0000330 if (LoadInst *LI = dyn_cast<LoadInst>(Insts[0]))
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000331 SomeVal = LI;
Chris Lattnera2d845a2011-01-14 19:36:13 +0000332 else
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000333 SomeVal = cast<StoreInst>(Insts[0])->getOperand(0);
334
335 if (BaseName.empty())
336 BaseName = SomeVal->getName();
337 SSA.Initialize(SomeVal->getType(), BaseName);
338}
339
340
341void LoadAndStorePromoter::
342run(const SmallVectorImpl<Instruction*> &Insts) const {
Chris Lattnera2d845a2011-01-14 19:36:13 +0000343
344 // First step: bucket up uses of the alloca by the block they occur in.
345 // This is important because we have to handle multiple defs/uses in a block
346 // ourselves: SSAUpdater is purely for cross-block references.
Chris Lattner84063572011-07-18 01:43:58 +0000347 DenseMap<BasicBlock*, TinyPtrVector<Instruction*> > UsesByBlock;
Chris Lattnera2d845a2011-01-14 19:36:13 +0000348
349 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
350 Instruction *User = Insts[i];
351 UsesByBlock[User->getParent()].push_back(User);
352 }
353
354 // Okay, now we can iterate over all the blocks in the function with uses,
355 // processing them. Keep track of which loads are loading a live-in value.
356 // Walk the uses in the use-list order to be determinstic.
357 SmallVector<LoadInst*, 32> LiveInLoads;
358 DenseMap<Value*, Value*> ReplacedLoads;
359
360 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
361 Instruction *User = Insts[i];
362 BasicBlock *BB = User->getParent();
Chris Lattner84063572011-07-18 01:43:58 +0000363 TinyPtrVector<Instruction*> &BlockUses = UsesByBlock[BB];
Chris Lattnera2d845a2011-01-14 19:36:13 +0000364
365 // If this block has already been processed, ignore this repeat use.
366 if (BlockUses.empty()) continue;
367
368 // Okay, this is the first use in the block. If this block just has a
369 // single user in it, we can rewrite it trivially.
370 if (BlockUses.size() == 1) {
371 // If it is a store, it is a trivial def of the value in the block.
Cameron Zwarichc8279392011-05-24 03:10:43 +0000372 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
Devang Patel231a5ab2011-07-06 21:09:55 +0000373 updateDebugInfo(SI);
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000374 SSA.AddAvailableValue(BB, SI->getOperand(0));
Cameron Zwarichc8279392011-05-24 03:10:43 +0000375 } else
Chris Lattnera2d845a2011-01-14 19:36:13 +0000376 // Otherwise it is a load, queue it to rewrite as a live-in load.
377 LiveInLoads.push_back(cast<LoadInst>(User));
378 BlockUses.clear();
379 continue;
380 }
381
382 // Otherwise, check to see if this block is all loads.
383 bool HasStore = false;
384 for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) {
385 if (isa<StoreInst>(BlockUses[i])) {
386 HasStore = true;
387 break;
388 }
389 }
390
391 // If so, we can queue them all as live in loads. We don't have an
392 // efficient way to tell which on is first in the block and don't want to
393 // scan large blocks, so just add all loads as live ins.
394 if (!HasStore) {
395 for (unsigned i = 0, e = BlockUses.size(); i != e; ++i)
396 LiveInLoads.push_back(cast<LoadInst>(BlockUses[i]));
397 BlockUses.clear();
398 continue;
399 }
400
401 // Otherwise, we have mixed loads and stores (or just a bunch of stores).
402 // Since SSAUpdater is purely for cross-block values, we need to determine
403 // the order of these instructions in the block. If the first use in the
404 // block is a load, then it uses the live in value. The last store defines
405 // the live out value. We handle this by doing a linear scan of the block.
406 Value *StoredValue = 0;
407 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
408 if (LoadInst *L = dyn_cast<LoadInst>(II)) {
409 // If this is a load from an unrelated pointer, ignore it.
410 if (!isInstInList(L, Insts)) continue;
411
412 // If we haven't seen a store yet, this is a live in use, otherwise
413 // use the stored value.
414 if (StoredValue) {
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000415 replaceLoadWithValue(L, StoredValue);
Chris Lattnera2d845a2011-01-14 19:36:13 +0000416 L->replaceAllUsesWith(StoredValue);
417 ReplacedLoads[L] = StoredValue;
418 } else {
419 LiveInLoads.push_back(L);
420 }
421 continue;
422 }
423
Cameron Zwarichc8279392011-05-24 03:10:43 +0000424 if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
Chris Lattnera2d845a2011-01-14 19:36:13 +0000425 // If this is a store to an unrelated pointer, ignore it.
Cameron Zwarichc8279392011-05-24 03:10:43 +0000426 if (!isInstInList(SI, Insts)) continue;
Devang Patel231a5ab2011-07-06 21:09:55 +0000427 updateDebugInfo(SI);
Cameron Zwarichc8279392011-05-24 03:10:43 +0000428
Chris Lattnera2d845a2011-01-14 19:36:13 +0000429 // Remember that this is the active value in the block.
Cameron Zwarichc8279392011-05-24 03:10:43 +0000430 StoredValue = SI->getOperand(0);
Chris Lattnera2d845a2011-01-14 19:36:13 +0000431 }
432 }
433
434 // The last stored value that happened is the live-out for the block.
435 assert(StoredValue && "Already checked that there is a store in block");
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000436 SSA.AddAvailableValue(BB, StoredValue);
Chris Lattnera2d845a2011-01-14 19:36:13 +0000437 BlockUses.clear();
438 }
439
440 // Okay, now we rewrite all loads that use live-in values in the loop,
441 // inserting PHI nodes as necessary.
442 for (unsigned i = 0, e = LiveInLoads.size(); i != e; ++i) {
443 LoadInst *ALoad = LiveInLoads[i];
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000444 Value *NewVal = SSA.GetValueInMiddleOfBlock(ALoad->getParent());
445 replaceLoadWithValue(ALoad, NewVal);
Chris Lattner867be592011-01-24 03:29:07 +0000446
447 // Avoid assertions in unreachable code.
448 if (NewVal == ALoad) NewVal = UndefValue::get(NewVal->getType());
Chris Lattnera2d845a2011-01-14 19:36:13 +0000449 ALoad->replaceAllUsesWith(NewVal);
450 ReplacedLoads[ALoad] = NewVal;
451 }
452
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000453 // Allow the client to do stuff before we start nuking things.
454 doExtraRewritesBeforeFinalDeletion();
455
Chris Lattnera2d845a2011-01-14 19:36:13 +0000456 // Now that everything is rewritten, delete the old instructions from the
457 // function. They should all be dead now.
458 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
459 Instruction *User = Insts[i];
460
461 // If this is a load that still has uses, then the load must have been added
462 // as a live value in the SSAUpdate data structure for a block (e.g. because
463 // the loaded value was stored later). In this case, we need to recursively
464 // propagate the updates until we get to the real value.
465 if (!User->use_empty()) {
466 Value *NewVal = ReplacedLoads[User];
467 assert(NewVal && "not a replaced load?");
468
469 // Propagate down to the ultimate replacee. The intermediately loads
470 // could theoretically already have been deleted, so we don't want to
471 // dereference the Value*'s.
472 DenseMap<Value*, Value*>::iterator RLI = ReplacedLoads.find(NewVal);
473 while (RLI != ReplacedLoads.end()) {
474 NewVal = RLI->second;
475 RLI = ReplacedLoads.find(NewVal);
476 }
477
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000478 replaceLoadWithValue(cast<LoadInst>(User), NewVal);
Chris Lattnera2d845a2011-01-14 19:36:13 +0000479 User->replaceAllUsesWith(NewVal);
480 }
481
Chris Lattnerdeaf55f2011-01-15 00:12:35 +0000482 instructionDeleted(User);
Chris Lattnera2d845a2011-01-14 19:36:13 +0000483 User->eraseFromParent();
484 }
485}
Benjamin Krameraa5354c2011-11-14 17:22:45 +0000486
487bool
488LoadAndStorePromoter::isInstInList(Instruction *I,
489 const SmallVectorImpl<Instruction*> &Insts)
490 const {
491 return std::find(Insts.begin(), Insts.end(), I) != Insts.end();
492}