Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 1 | //===- 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 Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "ssaupdater" |
Jay Foad | 562b84b | 2011-04-11 09:35:34 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
Duncan Sands | cdbd992 | 2010-11-16 17:41:24 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/InstructionSimplify.h" |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 19 | #include "llvm/Support/AlignOf.h" |
| 20 | #include "llvm/Support/Allocator.h" |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
| 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Devang Patel | 40348e8 | 2011-04-29 22:28:59 +0000 | [diff] [blame^] | 24 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Bob Wilson | 4aad88d | 2010-05-04 23:18:19 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 26 | #include "llvm/Transforms/Utils/SSAUpdaterImpl.h" |
Devang Patel | 40348e8 | 2011-04-29 22:28:59 +0000 | [diff] [blame^] | 27 | |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 30 | typedef DenseMap<BasicBlock*, Value*> AvailableValsTy; |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 31 | static AvailableValsTy &getAvailableVals(void *AV) { |
| 32 | return *static_cast<AvailableValsTy*>(AV); |
| 33 | } |
| 34 | |
Chris Lattner | f5a1fb6 | 2009-10-10 23:15:24 +0000 | [diff] [blame] | 35 | SSAUpdater::SSAUpdater(SmallVectorImpl<PHINode*> *NewPHI) |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 36 | : AV(0), ProtoType(0), ProtoName(), InsertedPHIs(NewPHI) {} |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 37 | |
| 38 | SSAUpdater::~SSAUpdater() { |
| 39 | delete &getAvailableVals(AV); |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /// Initialize - Reset this object to get ready for a new set of SSA |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 43 | /// updates with type 'Ty'. PHI nodes get a name based on 'Name'. |
| 44 | void SSAUpdater::Initialize(const Type *Ty, StringRef Name) { |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 45 | if (AV == 0) |
| 46 | AV = new AvailableValsTy(); |
| 47 | else |
| 48 | getAvailableVals(AV).clear(); |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 49 | ProtoType = Ty; |
| 50 | ProtoName = Name; |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Lattner | 0bef562 | 2009-10-10 23:41:48 +0000 | [diff] [blame] | 53 | /// HasValueForBlock - Return true if the SSAUpdater already has a value for |
| 54 | /// the specified block. |
| 55 | bool SSAUpdater::HasValueForBlock(BasicBlock *BB) const { |
| 56 | return getAvailableVals(AV).count(BB); |
| 57 | } |
| 58 | |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 59 | /// AddAvailableValue - Indicate that a rewritten value is available in the |
| 60 | /// specified block with the specified value. |
| 61 | void SSAUpdater::AddAvailableValue(BasicBlock *BB, Value *V) { |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 62 | assert(ProtoType != 0 && "Need to initialize SSAUpdater"); |
| 63 | assert(ProtoType == V->getType() && |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 64 | "All rewritten values must have the same type"); |
| 65 | getAvailableVals(AV)[BB] = V; |
| 66 | } |
| 67 | |
Bob Wilson | e98585e | 2010-01-27 22:01:02 +0000 | [diff] [blame] | 68 | /// IsEquivalentPHI - Check if PHI has the same incoming value as specified |
| 69 | /// in ValueMapping for each predecessor block. |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 70 | static bool IsEquivalentPHI(PHINode *PHI, |
Bob Wilson | e98585e | 2010-01-27 22:01:02 +0000 | [diff] [blame] | 71 | DenseMap<BasicBlock*, Value*> &ValueMapping) { |
| 72 | unsigned PHINumValues = PHI->getNumIncomingValues(); |
| 73 | if (PHINumValues != ValueMapping.size()) |
| 74 | return false; |
| 75 | |
| 76 | // Scan the phi to see if it matches. |
| 77 | for (unsigned i = 0, e = PHINumValues; i != e; ++i) |
| 78 | if (ValueMapping[PHI->getIncomingBlock(i)] != |
| 79 | PHI->getIncomingValue(i)) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 86 | /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is |
| 87 | /// live at the end of the specified block. |
Chris Lattner | 5fb1072 | 2009-10-10 22:41:58 +0000 | [diff] [blame] | 88 | Value *SSAUpdater::GetValueAtEndOfBlock(BasicBlock *BB) { |
Chris Lattner | 5fb1072 | 2009-10-10 22:41:58 +0000 | [diff] [blame] | 89 | Value *Res = GetValueAtEndOfBlockInternal(BB); |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 90 | return Res; |
| 91 | } |
| 92 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 93 | /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that |
| 94 | /// is live in the middle of the specified block. |
| 95 | /// |
| 96 | /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one |
| 97 | /// important case: if there is a definition of the rewritten value after the |
| 98 | /// 'use' in BB. Consider code like this: |
| 99 | /// |
| 100 | /// X1 = ... |
| 101 | /// SomeBB: |
| 102 | /// use(X) |
| 103 | /// X2 = ... |
| 104 | /// br Cond, SomeBB, OutBB |
| 105 | /// |
| 106 | /// In this case, there are two values (X1 and X2) added to the AvailableVals |
| 107 | /// set by the client of the rewriter, and those values are both live out of |
| 108 | /// their respective blocks. However, the use of X happens in the *middle* of |
| 109 | /// a block. Because of this, we need to insert a new PHI node in SomeBB to |
| 110 | /// merge the appropriate values, and this value isn't live out of the block. |
| 111 | /// |
| 112 | Value *SSAUpdater::GetValueInMiddleOfBlock(BasicBlock *BB) { |
| 113 | // If there is no definition of the renamed variable in this block, just use |
| 114 | // GetValueAtEndOfBlock to do our work. |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 115 | if (!HasValueForBlock(BB)) |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 116 | return GetValueAtEndOfBlock(BB); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 118 | // Otherwise, we have the hard case. Get the live-in values for each |
| 119 | // predecessor. |
| 120 | SmallVector<std::pair<BasicBlock*, Value*>, 8> PredValues; |
| 121 | Value *SingularValue = 0; |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 123 | // We can get our predecessor info by walking the pred_iterator list, but it |
| 124 | // is relatively slow. If we already have PHI nodes in this block, walk one |
| 125 | // of them to get the predecessor list instead. |
| 126 | if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) { |
| 127 | for (unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) { |
| 128 | BasicBlock *PredBB = SomePhi->getIncomingBlock(i); |
| 129 | Value *PredVal = GetValueAtEndOfBlock(PredBB); |
| 130 | PredValues.push_back(std::make_pair(PredBB, PredVal)); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 132 | // Compute SingularValue. |
| 133 | if (i == 0) |
| 134 | SingularValue = PredVal; |
| 135 | else if (PredVal != SingularValue) |
| 136 | SingularValue = 0; |
| 137 | } |
| 138 | } else { |
| 139 | bool isFirstPred = true; |
| 140 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 141 | BasicBlock *PredBB = *PI; |
| 142 | Value *PredVal = GetValueAtEndOfBlock(PredBB); |
| 143 | PredValues.push_back(std::make_pair(PredBB, PredVal)); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 145 | // Compute SingularValue. |
| 146 | if (isFirstPred) { |
| 147 | SingularValue = PredVal; |
| 148 | isFirstPred = false; |
| 149 | } else if (PredVal != SingularValue) |
| 150 | SingularValue = 0; |
| 151 | } |
| 152 | } |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 154 | // If there are no predecessors, just return undef. |
| 155 | if (PredValues.empty()) |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 156 | return UndefValue::get(ProtoType); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 158 | // Otherwise, if all the merged values are the same, just use it. |
| 159 | if (SingularValue != 0) |
| 160 | return SingularValue; |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 161 | |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 162 | // Otherwise, we do need a PHI: check to see if we already have one available |
| 163 | // in this block that produces the right value. |
| 164 | if (isa<PHINode>(BB->begin())) { |
| 165 | DenseMap<BasicBlock*, Value*> ValueMapping(PredValues.begin(), |
| 166 | PredValues.end()); |
| 167 | PHINode *SomePHI; |
| 168 | for (BasicBlock::iterator It = BB->begin(); |
| 169 | (SomePHI = dyn_cast<PHINode>(It)); ++It) { |
| 170 | if (IsEquivalentPHI(SomePHI, ValueMapping)) |
| 171 | return SomePHI; |
| 172 | } |
| 173 | } |
Bob Wilson | e98585e | 2010-01-27 22:01:02 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 4c1e3da | 2009-12-21 07:16:11 +0000 | [diff] [blame] | 175 | // Ok, we have no way out, insert a new one now. |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 176 | PHINode *InsertedPHI = PHINode::Create(ProtoType, PredValues.size(), |
| 177 | ProtoName, &BB->front()); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 179 | // Fill in all the predecessors of the PHI. |
| 180 | for (unsigned i = 0, e = PredValues.size(); i != e; ++i) |
| 181 | InsertedPHI->addIncoming(PredValues[i].second, PredValues[i].first); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 183 | // See if the PHI node can be merged to a single value. This can happen in |
| 184 | // loop cases when we get a PHI of itself and one other value. |
Duncan Sands | cdbd992 | 2010-11-16 17:41:24 +0000 | [diff] [blame] | 185 | if (Value *V = SimplifyInstruction(InsertedPHI)) { |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 186 | InsertedPHI->eraseFromParent(); |
Duncan Sands | cdbd992 | 2010-11-16 17:41:24 +0000 | [diff] [blame] | 187 | return V; |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | f5a1fb6 | 2009-10-10 23:15:24 +0000 | [diff] [blame] | 189 | |
Devang Patel | 40348e8 | 2011-04-29 22:28:59 +0000 | [diff] [blame^] | 190 | // Set DebugLoc. |
| 191 | InsertedPHI->setDebugLoc(GetFirstDebugLocInBasicBlock(BB)); |
| 192 | |
Chris Lattner | f5a1fb6 | 2009-10-10 23:15:24 +0000 | [diff] [blame] | 193 | // If the client wants to know about all new instructions, tell it. |
| 194 | if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 195 | |
David Greene | 1af40ca | 2010-01-05 01:26:49 +0000 | [diff] [blame] | 196 | DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n"); |
Chris Lattner | 1a8d4de | 2009-10-10 23:00:11 +0000 | [diff] [blame] | 197 | return InsertedPHI; |
| 198 | } |
| 199 | |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 200 | /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, |
| 201 | /// which use their value in the corresponding predecessor. |
| 202 | void SSAUpdater::RewriteUse(Use &U) { |
| 203 | Instruction *User = cast<Instruction>(U.getUser()); |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 88a8624 | 2009-10-20 20:27:49 +0000 | [diff] [blame] | 205 | Value *V; |
| 206 | if (PHINode *UserPN = dyn_cast<PHINode>(User)) |
| 207 | V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U)); |
| 208 | else |
| 209 | V = GetValueInMiddleOfBlock(User->getParent()); |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 210 | |
Torok Edwin | f993327 | 2009-10-20 15:42:00 +0000 | [diff] [blame] | 211 | U.set(V); |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | ffd9bee | 2010-08-29 04:54:06 +0000 | [diff] [blame] | 214 | /// RewriteUseAfterInsertions - Rewrite a use, just like RewriteUse. However, |
| 215 | /// this version of the method can rewrite uses in the same block as a |
| 216 | /// definition, because it assumes that all uses of a value are below any |
| 217 | /// inserted values. |
| 218 | void SSAUpdater::RewriteUseAfterInsertions(Use &U) { |
| 219 | Instruction *User = cast<Instruction>(U.getUser()); |
| 220 | |
| 221 | Value *V; |
| 222 | if (PHINode *UserPN = dyn_cast<PHINode>(User)) |
| 223 | V = GetValueAtEndOfBlock(UserPN->getIncomingBlock(U)); |
| 224 | else |
| 225 | V = GetValueAtEndOfBlock(User->getParent()); |
| 226 | |
| 227 | U.set(V); |
| 228 | } |
| 229 | |
Bob Wilson | 4aad88d | 2010-05-04 23:18:19 +0000 | [diff] [blame] | 230 | /// PHIiter - Iterator for PHI operands. This is used for the PHI_iterator |
| 231 | /// in the SSAUpdaterImpl template. |
| 232 | namespace { |
| 233 | class PHIiter { |
| 234 | private: |
| 235 | PHINode *PHI; |
| 236 | unsigned idx; |
| 237 | |
| 238 | public: |
| 239 | explicit PHIiter(PHINode *P) // begin iterator |
| 240 | : PHI(P), idx(0) {} |
| 241 | PHIiter(PHINode *P, bool) // end iterator |
| 242 | : PHI(P), idx(PHI->getNumIncomingValues()) {} |
| 243 | |
| 244 | PHIiter &operator++() { ++idx; return *this; } |
| 245 | bool operator==(const PHIiter& x) const { return idx == x.idx; } |
| 246 | bool operator!=(const PHIiter& x) const { return !operator==(x); } |
| 247 | Value *getIncomingValue() { return PHI->getIncomingValue(idx); } |
| 248 | BasicBlock *getIncomingBlock() { return PHI->getIncomingBlock(idx); } |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | /// SSAUpdaterTraits<SSAUpdater> - Traits for the SSAUpdaterImpl template, |
| 253 | /// specialized for SSAUpdater. |
| 254 | namespace llvm { |
| 255 | template<> |
| 256 | class SSAUpdaterTraits<SSAUpdater> { |
| 257 | public: |
| 258 | typedef BasicBlock BlkT; |
| 259 | typedef Value *ValT; |
| 260 | typedef PHINode PhiT; |
| 261 | |
| 262 | typedef succ_iterator BlkSucc_iterator; |
| 263 | static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return succ_begin(BB); } |
| 264 | static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return succ_end(BB); } |
| 265 | |
| 266 | typedef PHIiter PHI_iterator; |
| 267 | static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); } |
| 268 | static inline PHI_iterator PHI_end(PhiT *PHI) { |
| 269 | return PHI_iterator(PHI, true); |
| 270 | } |
| 271 | |
| 272 | /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds |
| 273 | /// vector, set Info->NumPreds, and allocate space in Info->Preds. |
| 274 | static void FindPredecessorBlocks(BasicBlock *BB, |
| 275 | SmallVectorImpl<BasicBlock*> *Preds) { |
| 276 | // We can get our predecessor info by walking the pred_iterator list, |
| 277 | // but it is relatively slow. If we already have PHI nodes in this |
| 278 | // block, walk one of them to get the predecessor list instead. |
| 279 | if (PHINode *SomePhi = dyn_cast<PHINode>(BB->begin())) { |
| 280 | for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI) |
| 281 | Preds->push_back(SomePhi->getIncomingBlock(PI)); |
| 282 | } else { |
| 283 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 284 | Preds->push_back(*PI); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /// GetUndefVal - Get an undefined value of the same type as the value |
| 289 | /// being handled. |
| 290 | static Value *GetUndefVal(BasicBlock *BB, SSAUpdater *Updater) { |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 291 | return UndefValue::get(Updater->ProtoType); |
Bob Wilson | 4aad88d | 2010-05-04 23:18:19 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// CreateEmptyPHI - Create a new PHI instruction in the specified block. |
| 295 | /// Reserve space for the operands but do not fill them in yet. |
| 296 | static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds, |
| 297 | SSAUpdater *Updater) { |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 298 | PHINode *PHI = PHINode::Create(Updater->ProtoType, NumPreds, |
| 299 | Updater->ProtoName, &BB->front()); |
Bob Wilson | 4aad88d | 2010-05-04 23:18:19 +0000 | [diff] [blame] | 300 | return PHI; |
| 301 | } |
| 302 | |
| 303 | /// AddPHIOperand - Add the specified value as an operand of the PHI for |
| 304 | /// the specified predecessor block. |
| 305 | static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred) { |
| 306 | PHI->addIncoming(Val, Pred); |
| 307 | } |
| 308 | |
| 309 | /// InstrIsPHI - Check if an instruction is a PHI. |
| 310 | /// |
| 311 | static PHINode *InstrIsPHI(Instruction *I) { |
| 312 | return dyn_cast<PHINode>(I); |
| 313 | } |
| 314 | |
| 315 | /// ValueIsPHI - Check if a value is a PHI. |
| 316 | /// |
| 317 | static PHINode *ValueIsPHI(Value *Val, SSAUpdater *Updater) { |
| 318 | return dyn_cast<PHINode>(Val); |
| 319 | } |
| 320 | |
| 321 | /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source |
| 322 | /// operands, i.e., it was just added. |
| 323 | static PHINode *ValueIsNewPHI(Value *Val, SSAUpdater *Updater) { |
| 324 | PHINode *PHI = ValueIsPHI(Val, Updater); |
| 325 | if (PHI && PHI->getNumIncomingValues() == 0) |
| 326 | return PHI; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /// GetPHIValue - For the specified PHI instruction, return the value |
| 331 | /// that it defines. |
| 332 | static Value *GetPHIValue(PHINode *PHI) { |
| 333 | return PHI; |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | } // End llvm namespace |
| 338 | |
Chris Lattner | 5fb1072 | 2009-10-10 22:41:58 +0000 | [diff] [blame] | 339 | /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry |
| 340 | /// for the specified BB and if so, return it. If not, construct SSA form by |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 341 | /// first calculating the required placement of PHIs and then inserting new |
| 342 | /// PHIs where needed. |
Chris Lattner | 5fb1072 | 2009-10-10 22:41:58 +0000 | [diff] [blame] | 343 | Value *SSAUpdater::GetValueAtEndOfBlockInternal(BasicBlock *BB) { |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 344 | AvailableValsTy &AvailableVals = getAvailableVals(AV); |
Bob Wilson | 84bd6b0 | 2010-04-17 03:08:24 +0000 | [diff] [blame] | 345 | if (Value *V = AvailableVals[BB]) |
| 346 | return V; |
Duncan Sands | ed90342 | 2009-10-16 15:20:13 +0000 | [diff] [blame] | 347 | |
Bob Wilson | 4aad88d | 2010-05-04 23:18:19 +0000 | [diff] [blame] | 348 | SSAUpdaterImpl<SSAUpdater> Impl(this, &AvailableVals, InsertedPHIs); |
| 349 | return Impl.GetValue(BB); |
Chris Lattner | 93f3bcf | 2009-10-10 09:04:27 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 351 | |
| 352 | //===----------------------------------------------------------------------===// |
| 353 | // LoadAndStorePromoter Implementation |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 356 | LoadAndStorePromoter:: |
| 357 | LoadAndStorePromoter(const SmallVectorImpl<Instruction*> &Insts, |
| 358 | SSAUpdater &S, StringRef BaseName) : SSA(S) { |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 359 | if (Insts.empty()) return; |
| 360 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 361 | Value *SomeVal; |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 362 | if (LoadInst *LI = dyn_cast<LoadInst>(Insts[0])) |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 363 | SomeVal = LI; |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 364 | else |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 365 | SomeVal = cast<StoreInst>(Insts[0])->getOperand(0); |
| 366 | |
| 367 | if (BaseName.empty()) |
| 368 | BaseName = SomeVal->getName(); |
| 369 | SSA.Initialize(SomeVal->getType(), BaseName); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | void LoadAndStorePromoter:: |
| 374 | run(const SmallVectorImpl<Instruction*> &Insts) const { |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 375 | |
| 376 | // First step: bucket up uses of the alloca by the block they occur in. |
| 377 | // This is important because we have to handle multiple defs/uses in a block |
| 378 | // ourselves: SSAUpdater is purely for cross-block references. |
| 379 | // FIXME: Want a TinyVector<Instruction*> since there is often 0/1 element. |
| 380 | DenseMap<BasicBlock*, std::vector<Instruction*> > UsesByBlock; |
| 381 | |
| 382 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 383 | Instruction *User = Insts[i]; |
| 384 | UsesByBlock[User->getParent()].push_back(User); |
| 385 | } |
| 386 | |
| 387 | // Okay, now we can iterate over all the blocks in the function with uses, |
| 388 | // processing them. Keep track of which loads are loading a live-in value. |
| 389 | // Walk the uses in the use-list order to be determinstic. |
| 390 | SmallVector<LoadInst*, 32> LiveInLoads; |
| 391 | DenseMap<Value*, Value*> ReplacedLoads; |
| 392 | |
| 393 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 394 | Instruction *User = Insts[i]; |
| 395 | BasicBlock *BB = User->getParent(); |
| 396 | std::vector<Instruction*> &BlockUses = UsesByBlock[BB]; |
| 397 | |
| 398 | // If this block has already been processed, ignore this repeat use. |
| 399 | if (BlockUses.empty()) continue; |
| 400 | |
| 401 | // Okay, this is the first use in the block. If this block just has a |
| 402 | // single user in it, we can rewrite it trivially. |
| 403 | if (BlockUses.size() == 1) { |
| 404 | // If it is a store, it is a trivial def of the value in the block. |
| 405 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 406 | SSA.AddAvailableValue(BB, SI->getOperand(0)); |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 407 | else |
| 408 | // Otherwise it is a load, queue it to rewrite as a live-in load. |
| 409 | LiveInLoads.push_back(cast<LoadInst>(User)); |
| 410 | BlockUses.clear(); |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | // Otherwise, check to see if this block is all loads. |
| 415 | bool HasStore = false; |
| 416 | for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) { |
| 417 | if (isa<StoreInst>(BlockUses[i])) { |
| 418 | HasStore = true; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // If so, we can queue them all as live in loads. We don't have an |
| 424 | // efficient way to tell which on is first in the block and don't want to |
| 425 | // scan large blocks, so just add all loads as live ins. |
| 426 | if (!HasStore) { |
| 427 | for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) |
| 428 | LiveInLoads.push_back(cast<LoadInst>(BlockUses[i])); |
| 429 | BlockUses.clear(); |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | // Otherwise, we have mixed loads and stores (or just a bunch of stores). |
| 434 | // Since SSAUpdater is purely for cross-block values, we need to determine |
| 435 | // the order of these instructions in the block. If the first use in the |
| 436 | // block is a load, then it uses the live in value. The last store defines |
| 437 | // the live out value. We handle this by doing a linear scan of the block. |
| 438 | Value *StoredValue = 0; |
| 439 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { |
| 440 | if (LoadInst *L = dyn_cast<LoadInst>(II)) { |
| 441 | // If this is a load from an unrelated pointer, ignore it. |
| 442 | if (!isInstInList(L, Insts)) continue; |
| 443 | |
| 444 | // If we haven't seen a store yet, this is a live in use, otherwise |
| 445 | // use the stored value. |
| 446 | if (StoredValue) { |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 447 | replaceLoadWithValue(L, StoredValue); |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 448 | L->replaceAllUsesWith(StoredValue); |
| 449 | ReplacedLoads[L] = StoredValue; |
| 450 | } else { |
| 451 | LiveInLoads.push_back(L); |
| 452 | } |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | if (StoreInst *S = dyn_cast<StoreInst>(II)) { |
| 457 | // If this is a store to an unrelated pointer, ignore it. |
| 458 | if (!isInstInList(S, Insts)) continue; |
| 459 | |
| 460 | // Remember that this is the active value in the block. |
| 461 | StoredValue = S->getOperand(0); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // The last stored value that happened is the live-out for the block. |
| 466 | assert(StoredValue && "Already checked that there is a store in block"); |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 467 | SSA.AddAvailableValue(BB, StoredValue); |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 468 | BlockUses.clear(); |
| 469 | } |
| 470 | |
| 471 | // Okay, now we rewrite all loads that use live-in values in the loop, |
| 472 | // inserting PHI nodes as necessary. |
| 473 | for (unsigned i = 0, e = LiveInLoads.size(); i != e; ++i) { |
| 474 | LoadInst *ALoad = LiveInLoads[i]; |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 475 | Value *NewVal = SSA.GetValueInMiddleOfBlock(ALoad->getParent()); |
| 476 | replaceLoadWithValue(ALoad, NewVal); |
Chris Lattner | 867be59 | 2011-01-24 03:29:07 +0000 | [diff] [blame] | 477 | |
| 478 | // Avoid assertions in unreachable code. |
| 479 | if (NewVal == ALoad) NewVal = UndefValue::get(NewVal->getType()); |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 480 | ALoad->replaceAllUsesWith(NewVal); |
| 481 | ReplacedLoads[ALoad] = NewVal; |
| 482 | } |
| 483 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 484 | // Allow the client to do stuff before we start nuking things. |
| 485 | doExtraRewritesBeforeFinalDeletion(); |
| 486 | |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 487 | // Now that everything is rewritten, delete the old instructions from the |
| 488 | // function. They should all be dead now. |
| 489 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 490 | Instruction *User = Insts[i]; |
| 491 | |
| 492 | // If this is a load that still has uses, then the load must have been added |
| 493 | // as a live value in the SSAUpdate data structure for a block (e.g. because |
| 494 | // the loaded value was stored later). In this case, we need to recursively |
| 495 | // propagate the updates until we get to the real value. |
| 496 | if (!User->use_empty()) { |
| 497 | Value *NewVal = ReplacedLoads[User]; |
| 498 | assert(NewVal && "not a replaced load?"); |
| 499 | |
| 500 | // Propagate down to the ultimate replacee. The intermediately loads |
| 501 | // could theoretically already have been deleted, so we don't want to |
| 502 | // dereference the Value*'s. |
| 503 | DenseMap<Value*, Value*>::iterator RLI = ReplacedLoads.find(NewVal); |
| 504 | while (RLI != ReplacedLoads.end()) { |
| 505 | NewVal = RLI->second; |
| 506 | RLI = ReplacedLoads.find(NewVal); |
| 507 | } |
| 508 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 509 | replaceLoadWithValue(cast<LoadInst>(User), NewVal); |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 510 | User->replaceAllUsesWith(NewVal); |
| 511 | } |
| 512 | |
Chris Lattner | deaf55f | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 513 | instructionDeleted(User); |
Chris Lattner | a2d845a | 2011-01-14 19:36:13 +0000 | [diff] [blame] | 514 | User->eraseFromParent(); |
| 515 | } |
| 516 | } |