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