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