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