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