George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1 | //===-- MemorySSA.cpp - Memory SSA Builder---------------------------===// |
| 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 MemorySSA class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------===// |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/Utils/MemorySSA.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "llvm/ADT/DenseSet.h" |
| 16 | #include "llvm/ADT/DepthFirstIterator.h" |
| 17 | #include "llvm/ADT/GraphTraits.h" |
| 18 | #include "llvm/ADT/PostOrderIterator.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/SmallSet.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
| 23 | #include "llvm/Analysis/AliasAnalysis.h" |
| 24 | #include "llvm/Analysis/CFG.h" |
| 25 | #include "llvm/Analysis/GlobalsModRef.h" |
| 26 | #include "llvm/Analysis/IteratedDominanceFrontier.h" |
| 27 | #include "llvm/Analysis/MemoryLocation.h" |
| 28 | #include "llvm/Analysis/PHITransAddr.h" |
| 29 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
| 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/Dominators.h" |
| 32 | #include "llvm/IR/GlobalVariable.h" |
| 33 | #include "llvm/IR/IRBuilder.h" |
| 34 | #include "llvm/IR/IntrinsicInst.h" |
| 35 | #include "llvm/IR/LLVMContext.h" |
| 36 | #include "llvm/IR/Metadata.h" |
| 37 | #include "llvm/IR/Module.h" |
| 38 | #include "llvm/IR/PatternMatch.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
| 40 | #include "llvm/Support/FormattedStream.h" |
| 41 | #include "llvm/Transforms/Scalar.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 42 | #include <algorithm> |
| 43 | |
| 44 | #define DEBUG_TYPE "memoryssa" |
| 45 | using namespace llvm; |
| 46 | STATISTIC(NumClobberCacheLookups, "Number of Memory SSA version cache lookups"); |
| 47 | STATISTIC(NumClobberCacheHits, "Number of Memory SSA version cache hits"); |
| 48 | STATISTIC(NumClobberCacheInserts, "Number of MemorySSA version cache inserts"); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 49 | |
| 50 | INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", true, |
| 51 | true) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 52 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 53 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS_END(MemorySSAWrapperPass, "memoryssa", "Memory SSA", true, true) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 55 | |
| 56 | namespace llvm { |
| 57 | |
| 58 | /// \brief An assembly annotator class to print Memory SSA information in |
| 59 | /// comments. |
| 60 | class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter { |
| 61 | friend class MemorySSA; |
| 62 | const MemorySSA *MSSA; |
| 63 | |
| 64 | public: |
| 65 | MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {} |
| 66 | |
| 67 | virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, |
| 68 | formatted_raw_ostream &OS) { |
| 69 | if (MemoryAccess *MA = MSSA->getMemoryAccess(BB)) |
| 70 | OS << "; " << *MA << "\n"; |
| 71 | } |
| 72 | |
| 73 | virtual void emitInstructionAnnot(const Instruction *I, |
| 74 | formatted_raw_ostream &OS) { |
| 75 | if (MemoryAccess *MA = MSSA->getMemoryAccess(I)) |
| 76 | OS << "; " << *MA << "\n"; |
| 77 | } |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | namespace { |
| 82 | struct RenamePassData { |
| 83 | DomTreeNode *DTN; |
| 84 | DomTreeNode::const_iterator ChildIt; |
| 85 | MemoryAccess *IncomingVal; |
| 86 | |
| 87 | RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It, |
| 88 | MemoryAccess *M) |
| 89 | : DTN(D), ChildIt(It), IncomingVal(M) {} |
| 90 | void swap(RenamePassData &RHS) { |
| 91 | std::swap(DTN, RHS.DTN); |
| 92 | std::swap(ChildIt, RHS.ChildIt); |
| 93 | std::swap(IncomingVal, RHS.IncomingVal); |
| 94 | } |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | namespace llvm { |
| 99 | /// \brief Rename a single basic block into MemorySSA form. |
| 100 | /// Uses the standard SSA renaming algorithm. |
| 101 | /// \returns The new incoming value. |
| 102 | MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB, |
| 103 | MemoryAccess *IncomingVal) { |
| 104 | auto It = PerBlockAccesses.find(BB); |
| 105 | // Skip most processing if the list is empty. |
| 106 | if (It != PerBlockAccesses.end()) { |
| 107 | AccessListType *Accesses = It->second.get(); |
| 108 | for (MemoryAccess &L : *Accesses) { |
| 109 | switch (L.getValueID()) { |
| 110 | case Value::MemoryUseVal: |
| 111 | cast<MemoryUse>(&L)->setDefiningAccess(IncomingVal); |
| 112 | break; |
| 113 | case Value::MemoryDefVal: |
| 114 | // We can't legally optimize defs, because we only allow single |
| 115 | // memory phis/uses on operations, and if we optimize these, we can |
| 116 | // end up with multiple reaching defs. Uses do not have this |
| 117 | // problem, since they do not produce a value |
| 118 | cast<MemoryDef>(&L)->setDefiningAccess(IncomingVal); |
| 119 | IncomingVal = &L; |
| 120 | break; |
| 121 | case Value::MemoryPhiVal: |
| 122 | IncomingVal = &L; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Pass through values to our successors |
| 129 | for (const BasicBlock *S : successors(BB)) { |
| 130 | auto It = PerBlockAccesses.find(S); |
| 131 | // Rename the phi nodes in our successor block |
| 132 | if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front())) |
| 133 | continue; |
| 134 | AccessListType *Accesses = It->second.get(); |
| 135 | auto *Phi = cast<MemoryPhi>(&Accesses->front()); |
| 136 | assert(std::find(succ_begin(BB), succ_end(BB), S) != succ_end(BB) && |
| 137 | "Must be at least one edge from Succ to BB!"); |
| 138 | Phi->addIncoming(IncomingVal, BB); |
| 139 | } |
| 140 | |
| 141 | return IncomingVal; |
| 142 | } |
| 143 | |
| 144 | /// \brief This is the standard SSA renaming algorithm. |
| 145 | /// |
| 146 | /// We walk the dominator tree in preorder, renaming accesses, and then filling |
| 147 | /// in phi nodes in our successors. |
| 148 | void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal, |
| 149 | SmallPtrSet<BasicBlock *, 16> &Visited) { |
| 150 | SmallVector<RenamePassData, 32> WorkStack; |
| 151 | IncomingVal = renameBlock(Root->getBlock(), IncomingVal); |
| 152 | WorkStack.push_back({Root, Root->begin(), IncomingVal}); |
| 153 | Visited.insert(Root->getBlock()); |
| 154 | |
| 155 | while (!WorkStack.empty()) { |
| 156 | DomTreeNode *Node = WorkStack.back().DTN; |
| 157 | DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt; |
| 158 | IncomingVal = WorkStack.back().IncomingVal; |
| 159 | |
| 160 | if (ChildIt == Node->end()) { |
| 161 | WorkStack.pop_back(); |
| 162 | } else { |
| 163 | DomTreeNode *Child = *ChildIt; |
| 164 | ++WorkStack.back().ChildIt; |
| 165 | BasicBlock *BB = Child->getBlock(); |
| 166 | Visited.insert(BB); |
| 167 | IncomingVal = renameBlock(BB, IncomingVal); |
| 168 | WorkStack.push_back({Child, Child->begin(), IncomingVal}); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// \brief Compute dominator levels, used by the phi insertion algorithm above. |
| 174 | void MemorySSA::computeDomLevels(DenseMap<DomTreeNode *, unsigned> &DomLevels) { |
| 175 | for (auto DFI = df_begin(DT->getRootNode()), DFE = df_end(DT->getRootNode()); |
| 176 | DFI != DFE; ++DFI) |
| 177 | DomLevels[*DFI] = DFI.getPathLength() - 1; |
| 178 | } |
| 179 | |
| 180 | /// \brief This handles unreachable block acccesses by deleting phi nodes in |
| 181 | /// unreachable blocks, and marking all other unreachable MemoryAccess's as |
| 182 | /// being uses of the live on entry definition. |
| 183 | void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) { |
| 184 | assert(!DT->isReachableFromEntry(BB) && |
| 185 | "Reachable block found while handling unreachable blocks"); |
| 186 | |
| 187 | auto It = PerBlockAccesses.find(BB); |
| 188 | if (It == PerBlockAccesses.end()) |
| 189 | return; |
| 190 | |
| 191 | auto &Accesses = It->second; |
| 192 | for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) { |
| 193 | auto Next = std::next(AI); |
| 194 | // If we have a phi, just remove it. We are going to replace all |
| 195 | // users with live on entry. |
| 196 | if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI)) |
| 197 | UseOrDef->setDefiningAccess(LiveOnEntryDef.get()); |
| 198 | else |
| 199 | Accesses->erase(AI); |
| 200 | AI = Next; |
| 201 | } |
| 202 | } |
| 203 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 204 | MemorySSA::MemorySSA(Function &Func, AliasAnalysis *AA, DominatorTree *DT) |
| 205 | : AA(AA), DT(DT), F(Func), LiveOnEntryDef(nullptr), Walker(nullptr), |
| 206 | NextID(0) { |
| 207 | getWalker(); // Ensure MemorySSA has been built. |
| 208 | } |
| 209 | |
| 210 | MemorySSA::MemorySSA(MemorySSA &&MSSA) |
| 211 | : AA(MSSA.AA), DT(MSSA.DT), F(MSSA.F), |
| 212 | ValueToMemoryAccess(std::move(MSSA.ValueToMemoryAccess)), |
| 213 | PerBlockAccesses(std::move(MSSA.PerBlockAccesses)), |
| 214 | LiveOnEntryDef(std::move(MSSA.LiveOnEntryDef)), |
| 215 | Walker(std::move(MSSA.Walker)), NextID(MSSA.NextID) { |
| 216 | // Update the Walker MSSA pointer so it doesn't point to the moved-from MSSA |
| 217 | // object any more. |
| 218 | Walker->MSSA = this; |
| 219 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 220 | |
| 221 | MemorySSA::~MemorySSA() { |
| 222 | // Drop all our references |
| 223 | for (const auto &Pair : PerBlockAccesses) |
| 224 | for (MemoryAccess &MA : *Pair.second) |
| 225 | MA.dropAllReferences(); |
| 226 | } |
| 227 | |
| 228 | MemorySSA::AccessListType *MemorySSA::getOrCreateAccessList(BasicBlock *BB) { |
| 229 | auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr)); |
| 230 | |
| 231 | if (Res.second) |
| 232 | Res.first->second = make_unique<AccessListType>(); |
| 233 | return Res.first->second.get(); |
| 234 | } |
| 235 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 236 | MemorySSAWalker *MemorySSA::getWalker() { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 237 | if (Walker) |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 238 | return Walker.get(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 239 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 240 | Walker = make_unique<CachingMemorySSAWalker>(this, AA, DT); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 241 | |
| 242 | // We create an access to represent "live on entry", for things like |
| 243 | // arguments or users of globals, where the memory they use is defined before |
| 244 | // the beginning of the function. We do not actually insert it into the IR. |
| 245 | // We do not define a live on exit for the immediate uses, and thus our |
| 246 | // semantics do *not* imply that something with no immediate uses can simply |
| 247 | // be removed. |
| 248 | BasicBlock &StartingPoint = F.getEntryBlock(); |
| 249 | LiveOnEntryDef = make_unique<MemoryDef>(F.getContext(), nullptr, nullptr, |
| 250 | &StartingPoint, NextID++); |
| 251 | |
| 252 | // We maintain lists of memory accesses per-block, trading memory for time. We |
| 253 | // could just look up the memory access for every possible instruction in the |
| 254 | // stream. |
| 255 | SmallPtrSet<BasicBlock *, 32> DefiningBlocks; |
Daniel Berlin | 1b51a29 | 2016-02-07 01:52:19 +0000 | [diff] [blame] | 256 | SmallPtrSet<BasicBlock *, 32> DefUseBlocks; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 257 | // Go through each block, figure out where defs occur, and chain together all |
| 258 | // the accesses. |
| 259 | for (BasicBlock &B : F) { |
Daniel Berlin | 7898ca6 | 2016-02-07 01:52:15 +0000 | [diff] [blame] | 260 | bool InsertIntoDef = false; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 261 | AccessListType *Accesses = nullptr; |
| 262 | for (Instruction &I : B) { |
Peter Collingbourne | ffecb14 | 2016-05-26 01:19:17 +0000 | [diff] [blame] | 263 | MemoryUseOrDef *MUD = createNewAccess(&I); |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 264 | if (!MUD) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 265 | continue; |
George Burgess IV | 3887a41 | 2016-03-21 21:25:39 +0000 | [diff] [blame] | 266 | InsertIntoDef |= isa<MemoryDef>(MUD); |
Daniel Berlin | 1b51a29 | 2016-02-07 01:52:19 +0000 | [diff] [blame] | 267 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 268 | if (!Accesses) |
| 269 | Accesses = getOrCreateAccessList(&B); |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 270 | Accesses->push_back(MUD); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 271 | } |
Daniel Berlin | 7898ca6 | 2016-02-07 01:52:15 +0000 | [diff] [blame] | 272 | if (InsertIntoDef) |
| 273 | DefiningBlocks.insert(&B); |
George Burgess IV | 3887a41 | 2016-03-21 21:25:39 +0000 | [diff] [blame] | 274 | if (Accesses) |
Daniel Berlin | 1b51a29 | 2016-02-07 01:52:19 +0000 | [diff] [blame] | 275 | DefUseBlocks.insert(&B); |
| 276 | } |
| 277 | |
| 278 | // Compute live-in. |
| 279 | // Live in is normally defined as "all the blocks on the path from each def to |
| 280 | // each of it's uses". |
| 281 | // MemoryDef's are implicit uses of previous state, so they are also uses. |
| 282 | // This means we don't really have def-only instructions. The only |
| 283 | // MemoryDef's that are not really uses are those that are of the LiveOnEntry |
| 284 | // variable (because LiveOnEntry can reach anywhere, and every def is a |
| 285 | // must-kill of LiveOnEntry). |
| 286 | // In theory, you could precisely compute live-in by using alias-analysis to |
| 287 | // disambiguate defs and uses to see which really pair up with which. |
| 288 | // In practice, this would be really expensive and difficult. So we simply |
| 289 | // assume all defs are also uses that need to be kept live. |
| 290 | // Because of this, the end result of this live-in computation will be "the |
| 291 | // entire set of basic blocks that reach any use". |
| 292 | |
| 293 | SmallPtrSet<BasicBlock *, 32> LiveInBlocks; |
| 294 | SmallVector<BasicBlock *, 64> LiveInBlockWorklist(DefUseBlocks.begin(), |
| 295 | DefUseBlocks.end()); |
| 296 | // Now that we have a set of blocks where a value is live-in, recursively add |
| 297 | // predecessors until we find the full region the value is live. |
| 298 | while (!LiveInBlockWorklist.empty()) { |
| 299 | BasicBlock *BB = LiveInBlockWorklist.pop_back_val(); |
| 300 | |
| 301 | // The block really is live in here, insert it into the set. If already in |
| 302 | // the set, then it has already been processed. |
| 303 | if (!LiveInBlocks.insert(BB).second) |
| 304 | continue; |
| 305 | |
| 306 | // Since the value is live into BB, it is either defined in a predecessor or |
| 307 | // live into it to. |
| 308 | LiveInBlockWorklist.append(pred_begin(BB), pred_end(BB)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Determine where our MemoryPhi's should go |
Daniel Berlin | 77fa84e | 2016-04-19 06:13:28 +0000 | [diff] [blame] | 312 | ForwardIDFCalculator IDFs(*DT); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 313 | IDFs.setDefiningBlocks(DefiningBlocks); |
Daniel Berlin | 1b51a29 | 2016-02-07 01:52:19 +0000 | [diff] [blame] | 314 | IDFs.setLiveInBlocks(LiveInBlocks); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 315 | SmallVector<BasicBlock *, 32> IDFBlocks; |
| 316 | IDFs.calculate(IDFBlocks); |
| 317 | |
| 318 | // Now place MemoryPhi nodes. |
| 319 | for (auto &BB : IDFBlocks) { |
| 320 | // Insert phi node |
| 321 | AccessListType *Accesses = getOrCreateAccessList(BB); |
| 322 | MemoryPhi *Phi = new MemoryPhi(F.getContext(), BB, NextID++); |
Daniel Berlin | f6c9ae9 | 2016-02-10 17:41:25 +0000 | [diff] [blame] | 323 | ValueToMemoryAccess.insert(std::make_pair(BB, Phi)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 324 | // Phi's always are placed at the front of the block. |
| 325 | Accesses->push_front(Phi); |
| 326 | } |
| 327 | |
| 328 | // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get |
| 329 | // filled in with all blocks. |
| 330 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 331 | renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited); |
| 332 | |
| 333 | // Now optimize the MemoryUse's defining access to point to the nearest |
| 334 | // dominating clobbering def. |
| 335 | // This ensures that MemoryUse's that are killed by the same store are |
| 336 | // immediate users of that store, one of the invariants we guarantee. |
| 337 | for (auto DomNode : depth_first(DT)) { |
| 338 | BasicBlock *BB = DomNode->getBlock(); |
| 339 | auto AI = PerBlockAccesses.find(BB); |
| 340 | if (AI == PerBlockAccesses.end()) |
| 341 | continue; |
| 342 | AccessListType *Accesses = AI->second.get(); |
| 343 | for (auto &MA : *Accesses) { |
| 344 | if (auto *MU = dyn_cast<MemoryUse>(&MA)) { |
| 345 | Instruction *Inst = MU->getMemoryInst(); |
Daniel Berlin | 6412002 | 2016-03-02 21:16:28 +0000 | [diff] [blame] | 346 | MU->setDefiningAccess(Walker->getClobberingMemoryAccess(Inst)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Mark the uses in unreachable blocks as live on entry, so that they go |
| 352 | // somewhere. |
| 353 | for (auto &BB : F) |
| 354 | if (!Visited.count(&BB)) |
| 355 | markUnreachableAsLiveOnEntry(&BB); |
| 356 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 357 | return Walker.get(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /// \brief Helper function to create new memory accesses |
Peter Collingbourne | ffecb14 | 2016-05-26 01:19:17 +0000 | [diff] [blame] | 361 | MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I) { |
Peter Collingbourne | b9aa1f4 | 2016-05-26 04:58:46 +0000 | [diff] [blame] | 362 | // The assume intrinsic has a control dependency which we model by claiming |
| 363 | // that it writes arbitrarily. Ignore that fake memory dependency here. |
| 364 | // FIXME: Replace this special casing with a more accurate modelling of |
| 365 | // assume's control dependency. |
| 366 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 367 | if (II->getIntrinsicID() == Intrinsic::assume) |
| 368 | return nullptr; |
| 369 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 370 | // Find out what affect this instruction has on memory. |
| 371 | ModRefInfo ModRef = AA->getModRefInfo(I); |
| 372 | bool Def = bool(ModRef & MRI_Mod); |
| 373 | bool Use = bool(ModRef & MRI_Ref); |
| 374 | |
| 375 | // It's possible for an instruction to not modify memory at all. During |
| 376 | // construction, we ignore them. |
Peter Collingbourne | ffecb14 | 2016-05-26 01:19:17 +0000 | [diff] [blame] | 377 | if (!Def && !Use) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 378 | return nullptr; |
| 379 | |
| 380 | assert((Def || Use) && |
| 381 | "Trying to create a memory access with a non-memory instruction"); |
| 382 | |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 383 | MemoryUseOrDef *MUD; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 384 | if (Def) |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 385 | MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 386 | else |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 387 | MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent()); |
| 388 | ValueToMemoryAccess.insert(std::make_pair(I, MUD)); |
| 389 | return MUD; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | MemoryAccess *MemorySSA::findDominatingDef(BasicBlock *UseBlock, |
| 393 | enum InsertionPlace Where) { |
| 394 | // Handle the initial case |
| 395 | if (Where == Beginning) |
| 396 | // The only thing that could define us at the beginning is a phi node |
| 397 | if (MemoryPhi *Phi = getMemoryAccess(UseBlock)) |
| 398 | return Phi; |
| 399 | |
| 400 | DomTreeNode *CurrNode = DT->getNode(UseBlock); |
| 401 | // Need to be defined by our dominator |
| 402 | if (Where == Beginning) |
| 403 | CurrNode = CurrNode->getIDom(); |
| 404 | Where = End; |
| 405 | while (CurrNode) { |
| 406 | auto It = PerBlockAccesses.find(CurrNode->getBlock()); |
| 407 | if (It != PerBlockAccesses.end()) { |
| 408 | auto &Accesses = It->second; |
| 409 | for (auto RAI = Accesses->rbegin(), RAE = Accesses->rend(); RAI != RAE; |
| 410 | ++RAI) { |
| 411 | if (isa<MemoryDef>(*RAI) || isa<MemoryPhi>(*RAI)) |
| 412 | return &*RAI; |
| 413 | } |
| 414 | } |
| 415 | CurrNode = CurrNode->getIDom(); |
| 416 | } |
| 417 | return LiveOnEntryDef.get(); |
| 418 | } |
| 419 | |
| 420 | /// \brief Returns true if \p Replacer dominates \p Replacee . |
| 421 | bool MemorySSA::dominatesUse(const MemoryAccess *Replacer, |
| 422 | const MemoryAccess *Replacee) const { |
| 423 | if (isa<MemoryUseOrDef>(Replacee)) |
| 424 | return DT->dominates(Replacer->getBlock(), Replacee->getBlock()); |
| 425 | const auto *MP = cast<MemoryPhi>(Replacee); |
| 426 | // For a phi node, the use occurs in the predecessor block of the phi node. |
| 427 | // Since we may occur multiple times in the phi node, we have to check each |
| 428 | // operand to ensure Replacer dominates each operand where Replacee occurs. |
| 429 | for (const Use &Arg : MP->operands()) { |
George Burgess IV | b5a229f | 2016-02-02 23:15:26 +0000 | [diff] [blame] | 430 | if (Arg.get() != Replacee && |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 431 | !DT->dominates(Replacer->getBlock(), MP->getIncomingBlock(Arg))) |
| 432 | return false; |
| 433 | } |
| 434 | return true; |
| 435 | } |
| 436 | |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 437 | /// \brief If all arguments of a MemoryPHI are defined by the same incoming |
| 438 | /// argument, return that argument. |
| 439 | static MemoryAccess *onlySingleValue(MemoryPhi *MP) { |
| 440 | MemoryAccess *MA = nullptr; |
| 441 | |
| 442 | for (auto &Arg : MP->operands()) { |
| 443 | if (!MA) |
| 444 | MA = cast<MemoryAccess>(Arg); |
| 445 | else if (MA != Arg) |
| 446 | return nullptr; |
| 447 | } |
| 448 | return MA; |
| 449 | } |
| 450 | |
| 451 | /// \brief Properly remove \p MA from all of MemorySSA's lookup tables. |
| 452 | /// |
| 453 | /// Because of the way the intrusive list and use lists work, it is important to |
| 454 | /// do removal in the right order. |
| 455 | void MemorySSA::removeFromLookups(MemoryAccess *MA) { |
| 456 | assert(MA->use_empty() && |
| 457 | "Trying to remove memory access that still has uses"); |
| 458 | if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(MA)) |
| 459 | MUD->setDefiningAccess(nullptr); |
| 460 | // Invalidate our walker's cache if necessary |
| 461 | if (!isa<MemoryUse>(MA)) |
| 462 | Walker->invalidateInfo(MA); |
| 463 | // The call below to erase will destroy MA, so we can't change the order we |
| 464 | // are doing things here |
| 465 | Value *MemoryInst; |
| 466 | if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(MA)) { |
| 467 | MemoryInst = MUD->getMemoryInst(); |
| 468 | } else { |
| 469 | MemoryInst = MA->getBlock(); |
| 470 | } |
| 471 | ValueToMemoryAccess.erase(MemoryInst); |
| 472 | |
George Burgess IV | e0e6e48 | 2016-03-02 02:35:04 +0000 | [diff] [blame] | 473 | auto AccessIt = PerBlockAccesses.find(MA->getBlock()); |
| 474 | std::unique_ptr<AccessListType> &Accesses = AccessIt->second; |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 475 | Accesses->erase(MA); |
George Burgess IV | e0e6e48 | 2016-03-02 02:35:04 +0000 | [diff] [blame] | 476 | if (Accesses->empty()) |
| 477 | PerBlockAccesses.erase(AccessIt); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void MemorySSA::removeMemoryAccess(MemoryAccess *MA) { |
| 481 | assert(!isLiveOnEntryDef(MA) && "Trying to remove the live on entry def"); |
| 482 | // We can only delete phi nodes if they have no uses, or we can replace all |
| 483 | // uses with a single definition. |
| 484 | MemoryAccess *NewDefTarget = nullptr; |
| 485 | if (MemoryPhi *MP = dyn_cast<MemoryPhi>(MA)) { |
| 486 | // Note that it is sufficient to know that all edges of the phi node have |
| 487 | // the same argument. If they do, by the definition of dominance frontiers |
| 488 | // (which we used to place this phi), that argument must dominate this phi, |
| 489 | // and thus, must dominate the phi's uses, and so we will not hit the assert |
| 490 | // below. |
| 491 | NewDefTarget = onlySingleValue(MP); |
| 492 | assert((NewDefTarget || MP->use_empty()) && |
| 493 | "We can't delete this memory phi"); |
| 494 | } else { |
| 495 | NewDefTarget = cast<MemoryUseOrDef>(MA)->getDefiningAccess(); |
| 496 | } |
| 497 | |
| 498 | // Re-point the uses at our defining access |
| 499 | if (!MA->use_empty()) |
| 500 | MA->replaceAllUsesWith(NewDefTarget); |
| 501 | |
| 502 | // The call below to erase will destroy MA, so we can't change the order we |
| 503 | // are doing things here |
| 504 | removeFromLookups(MA); |
| 505 | } |
| 506 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 507 | void MemorySSA::print(raw_ostream &OS) const { |
| 508 | MemorySSAAnnotatedWriter Writer(this); |
| 509 | F.print(OS, &Writer); |
| 510 | } |
| 511 | |
| 512 | void MemorySSA::dump() const { |
| 513 | MemorySSAAnnotatedWriter Writer(this); |
| 514 | F.print(dbgs(), &Writer); |
| 515 | } |
| 516 | |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 517 | void MemorySSA::verifyMemorySSA() const { |
| 518 | verifyDefUses(F); |
| 519 | verifyDomination(F); |
| 520 | } |
| 521 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 522 | /// \brief Verify the domination properties of MemorySSA by checking that each |
| 523 | /// definition dominates all of its uses. |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 524 | void MemorySSA::verifyDomination(Function &F) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 525 | for (BasicBlock &B : F) { |
| 526 | // Phi nodes are attached to basic blocks |
| 527 | if (MemoryPhi *MP = getMemoryAccess(&B)) { |
| 528 | for (User *U : MP->users()) { |
| 529 | BasicBlock *UseBlock; |
| 530 | // Phi operands are used on edges, we simulate the right domination by |
| 531 | // acting as if the use occurred at the end of the predecessor block. |
| 532 | if (MemoryPhi *P = dyn_cast<MemoryPhi>(U)) { |
| 533 | for (const auto &Arg : P->operands()) { |
| 534 | if (Arg == MP) { |
| 535 | UseBlock = P->getIncomingBlock(Arg); |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | } else { |
| 540 | UseBlock = cast<MemoryAccess>(U)->getBlock(); |
| 541 | } |
George Burgess IV | 60adac4 | 2016-02-02 23:26:01 +0000 | [diff] [blame] | 542 | (void)UseBlock; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 543 | assert(DT->dominates(MP->getBlock(), UseBlock) && |
| 544 | "Memory PHI does not dominate it's uses"); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | for (Instruction &I : B) { |
| 549 | MemoryAccess *MD = dyn_cast_or_null<MemoryDef>(getMemoryAccess(&I)); |
| 550 | if (!MD) |
| 551 | continue; |
| 552 | |
Benjamin Kramer | 451f54c | 2016-02-22 13:11:58 +0000 | [diff] [blame] | 553 | for (User *U : MD->users()) { |
Mehdi Amini | 89038a1 | 2016-04-02 05:34:19 +0000 | [diff] [blame] | 554 | BasicBlock *UseBlock; (void)UseBlock; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 555 | // Things are allowed to flow to phi nodes over their predecessor edge. |
| 556 | if (auto *P = dyn_cast<MemoryPhi>(U)) { |
| 557 | for (const auto &Arg : P->operands()) { |
| 558 | if (Arg == MD) { |
| 559 | UseBlock = P->getIncomingBlock(Arg); |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | } else { |
| 564 | UseBlock = cast<MemoryAccess>(U)->getBlock(); |
| 565 | } |
| 566 | assert(DT->dominates(MD->getBlock(), UseBlock) && |
| 567 | "Memory Def does not dominate it's uses"); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /// \brief Verify the def-use lists in MemorySSA, by verifying that \p Use |
| 574 | /// appears in the use list of \p Def. |
| 575 | /// |
| 576 | /// llvm_unreachable is used instead of asserts because this may be called in |
| 577 | /// a build without asserts. In that case, we don't want this to turn into a |
| 578 | /// nop. |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 579 | void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 580 | // The live on entry use may cause us to get a NULL def here |
| 581 | if (!Def) { |
| 582 | if (!isLiveOnEntryDef(Use)) |
| 583 | llvm_unreachable("Null def but use not point to live on entry def"); |
| 584 | } else if (std::find(Def->user_begin(), Def->user_end(), Use) == |
| 585 | Def->user_end()) { |
| 586 | llvm_unreachable("Did not find use in def's use list"); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /// \brief Verify the immediate use information, by walking all the memory |
| 591 | /// accesses and verifying that, for each use, it appears in the |
| 592 | /// appropriate def's use list |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 593 | void MemorySSA::verifyDefUses(Function &F) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 594 | for (BasicBlock &B : F) { |
| 595 | // Phi nodes are attached to basic blocks |
| 596 | if (MemoryPhi *Phi = getMemoryAccess(&B)) |
| 597 | for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) |
| 598 | verifyUseInDefs(Phi->getIncomingValue(I), Phi); |
| 599 | |
| 600 | for (Instruction &I : B) { |
| 601 | if (MemoryAccess *MA = getMemoryAccess(&I)) { |
| 602 | assert(isa<MemoryUseOrDef>(MA) && |
| 603 | "Found a phi node not attached to a bb"); |
| 604 | verifyUseInDefs(cast<MemoryUseOrDef>(MA)->getDefiningAccess(), MA); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | MemoryAccess *MemorySSA::getMemoryAccess(const Value *I) const { |
Daniel Berlin | f6c9ae9 | 2016-02-10 17:41:25 +0000 | [diff] [blame] | 611 | return ValueToMemoryAccess.lookup(I); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | MemoryPhi *MemorySSA::getMemoryAccess(const BasicBlock *BB) const { |
| 615 | return cast_or_null<MemoryPhi>(getMemoryAccess((const Value *)BB)); |
| 616 | } |
| 617 | |
| 618 | /// \brief Determine, for two memory accesses in the same block, |
| 619 | /// whether \p Dominator dominates \p Dominatee. |
| 620 | /// \returns True if \p Dominator dominates \p Dominatee. |
| 621 | bool MemorySSA::locallyDominates(const MemoryAccess *Dominator, |
| 622 | const MemoryAccess *Dominatee) const { |
| 623 | |
| 624 | assert((Dominator->getBlock() == Dominatee->getBlock()) && |
| 625 | "Asking for local domination when accesses are in different blocks!"); |
Sebastian Pop | e1f60b1 | 2016-06-10 21:36:41 +0000 | [diff] [blame^] | 626 | |
| 627 | // A node dominates itself. |
| 628 | if (Dominatee == Dominator) |
| 629 | return true; |
| 630 | |
| 631 | // When Dominatee is defined on function entry, it is not dominated by another |
| 632 | // memory access. |
| 633 | if (isLiveOnEntryDef(Dominatee)) |
| 634 | return false; |
| 635 | |
| 636 | // When Dominator is defined on function entry, it dominates the other memory |
| 637 | // access. |
| 638 | if (isLiveOnEntryDef(Dominator)) |
| 639 | return true; |
| 640 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 641 | // Get the access list for the block |
| 642 | const AccessListType *AccessList = getBlockAccesses(Dominator->getBlock()); |
| 643 | AccessListType::const_reverse_iterator It(Dominator->getIterator()); |
| 644 | |
| 645 | // If we hit the beginning of the access list before we hit dominatee, we must |
| 646 | // dominate it |
| 647 | return std::none_of(It, AccessList->rend(), |
| 648 | [&](const MemoryAccess &MA) { return &MA == Dominatee; }); |
| 649 | } |
| 650 | |
| 651 | const static char LiveOnEntryStr[] = "liveOnEntry"; |
| 652 | |
| 653 | void MemoryDef::print(raw_ostream &OS) const { |
| 654 | MemoryAccess *UO = getDefiningAccess(); |
| 655 | |
| 656 | OS << getID() << " = MemoryDef("; |
| 657 | if (UO && UO->getID()) |
| 658 | OS << UO->getID(); |
| 659 | else |
| 660 | OS << LiveOnEntryStr; |
| 661 | OS << ')'; |
| 662 | } |
| 663 | |
| 664 | void MemoryPhi::print(raw_ostream &OS) const { |
| 665 | bool First = true; |
| 666 | OS << getID() << " = MemoryPhi("; |
| 667 | for (const auto &Op : operands()) { |
| 668 | BasicBlock *BB = getIncomingBlock(Op); |
| 669 | MemoryAccess *MA = cast<MemoryAccess>(Op); |
| 670 | if (!First) |
| 671 | OS << ','; |
| 672 | else |
| 673 | First = false; |
| 674 | |
| 675 | OS << '{'; |
| 676 | if (BB->hasName()) |
| 677 | OS << BB->getName(); |
| 678 | else |
| 679 | BB->printAsOperand(OS, false); |
| 680 | OS << ','; |
| 681 | if (unsigned ID = MA->getID()) |
| 682 | OS << ID; |
| 683 | else |
| 684 | OS << LiveOnEntryStr; |
| 685 | OS << '}'; |
| 686 | } |
| 687 | OS << ')'; |
| 688 | } |
| 689 | |
| 690 | MemoryAccess::~MemoryAccess() {} |
| 691 | |
| 692 | void MemoryUse::print(raw_ostream &OS) const { |
| 693 | MemoryAccess *UO = getDefiningAccess(); |
| 694 | OS << "MemoryUse("; |
| 695 | if (UO && UO->getID()) |
| 696 | OS << UO->getID(); |
| 697 | else |
| 698 | OS << LiveOnEntryStr; |
| 699 | OS << ')'; |
| 700 | } |
| 701 | |
| 702 | void MemoryAccess::dump() const { |
| 703 | print(dbgs()); |
| 704 | dbgs() << "\n"; |
| 705 | } |
| 706 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 707 | char MemorySSAAnalysis::PassID; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 708 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 709 | MemorySSA MemorySSAAnalysis::run(Function &F, AnalysisManager<Function> &AM) { |
| 710 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 711 | auto &AA = AM.getResult<AAManager>(F); |
| 712 | return MemorySSA(F, &AA, &DT); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 715 | PreservedAnalyses MemorySSAPrinterPass::run(Function &F, |
| 716 | FunctionAnalysisManager &AM) { |
| 717 | OS << "MemorySSA for function: " << F.getName() << "\n"; |
| 718 | AM.getResult<MemorySSAAnalysis>(F).print(OS); |
| 719 | |
| 720 | return PreservedAnalyses::all(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 723 | PreservedAnalyses MemorySSAVerifierPass::run(Function &F, |
| 724 | FunctionAnalysisManager &AM) { |
| 725 | AM.getResult<MemorySSAAnalysis>(F).verifyMemorySSA(); |
| 726 | |
| 727 | return PreservedAnalyses::all(); |
| 728 | } |
| 729 | |
| 730 | char MemorySSAWrapperPass::ID = 0; |
| 731 | |
| 732 | MemorySSAWrapperPass::MemorySSAWrapperPass() : FunctionPass(ID) { |
| 733 | initializeMemorySSAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 734 | } |
| 735 | |
| 736 | void MemorySSAWrapperPass::releaseMemory() { MSSA.reset(); } |
| 737 | |
| 738 | void MemorySSAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 739 | AU.setPreservesAll(); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 740 | AU.addRequiredTransitive<DominatorTreeWrapperPass>(); |
| 741 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 744 | bool MemorySSAWrapperPass::runOnFunction(Function &F) { |
| 745 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 746 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 747 | MSSA.reset(new MemorySSA(F, &AA, &DT)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 748 | return false; |
| 749 | } |
| 750 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 751 | void MemorySSAWrapperPass::verifyAnalysis() const { MSSA->verifyMemorySSA(); } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 752 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 753 | void MemorySSAWrapperPass::print(raw_ostream &OS, const Module *M) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 754 | MSSA->print(OS); |
| 755 | } |
| 756 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 757 | MemorySSAWalker::MemorySSAWalker(MemorySSA *M) : MSSA(M) {} |
| 758 | |
| 759 | CachingMemorySSAWalker::CachingMemorySSAWalker(MemorySSA *M, AliasAnalysis *A, |
| 760 | DominatorTree *D) |
| 761 | : MemorySSAWalker(M), AA(A), DT(D) {} |
| 762 | |
| 763 | CachingMemorySSAWalker::~CachingMemorySSAWalker() {} |
| 764 | |
| 765 | struct CachingMemorySSAWalker::UpwardsMemoryQuery { |
| 766 | // True if we saw a phi whose predecessor was a backedge |
| 767 | bool SawBackedgePhi; |
| 768 | // True if our original query started off as a call |
| 769 | bool IsCall; |
| 770 | // The pointer location we started the query with. This will be empty if |
| 771 | // IsCall is true. |
| 772 | MemoryLocation StartingLoc; |
| 773 | // This is the instruction we were querying about. |
| 774 | const Instruction *Inst; |
| 775 | // Set of visited Instructions for this query. |
| 776 | DenseSet<MemoryAccessPair> Visited; |
George Burgess IV | 49cad7d | 2016-03-30 03:12:08 +0000 | [diff] [blame] | 777 | // Vector of visited call accesses for this query. This is separated out |
| 778 | // because you can always cache and lookup the result of call queries (IE when |
| 779 | // IsCall == true) for every call in the chain. The calls have no AA location |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 780 | // associated with them with them, and thus, no context dependence. |
George Burgess IV | 49cad7d | 2016-03-30 03:12:08 +0000 | [diff] [blame] | 781 | SmallVector<const MemoryAccess *, 32> VisitedCalls; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 782 | // The MemoryAccess we actually got called with, used to test local domination |
| 783 | const MemoryAccess *OriginalAccess; |
| 784 | // The Datalayout for the module we started in |
| 785 | const DataLayout *DL; |
| 786 | |
| 787 | UpwardsMemoryQuery() |
| 788 | : SawBackedgePhi(false), IsCall(false), Inst(nullptr), |
| 789 | OriginalAccess(nullptr), DL(nullptr) {} |
| 790 | }; |
| 791 | |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 792 | void CachingMemorySSAWalker::invalidateInfo(MemoryAccess *MA) { |
| 793 | |
| 794 | // TODO: We can do much better cache invalidation with differently stored |
| 795 | // caches. For now, for MemoryUses, we simply remove them |
| 796 | // from the cache, and kill the entire call/non-call cache for everything |
| 797 | // else. The problem is for phis or defs, currently we'd need to follow use |
| 798 | // chains down and invalidate anything below us in the chain that currently |
| 799 | // terminates at this access. |
| 800 | |
| 801 | // See if this is a MemoryUse, if so, just remove the cached info. MemoryUse |
| 802 | // is by definition never a barrier, so nothing in the cache could point to |
| 803 | // this use. In that case, we only need invalidate the info for the use |
| 804 | // itself. |
| 805 | |
| 806 | if (MemoryUse *MU = dyn_cast<MemoryUse>(MA)) { |
| 807 | UpwardsMemoryQuery Q; |
| 808 | Instruction *I = MU->getMemoryInst(); |
| 809 | Q.IsCall = bool(ImmutableCallSite(I)); |
| 810 | Q.Inst = I; |
| 811 | if (!Q.IsCall) |
| 812 | Q.StartingLoc = MemoryLocation::get(I); |
| 813 | doCacheRemove(MA, Q, Q.StartingLoc); |
Geoff Berry | 9fe26e6 | 2016-04-22 14:44:10 +0000 | [diff] [blame] | 814 | } else { |
| 815 | // If it is not a use, the best we can do right now is destroy the cache. |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 816 | CachedUpwardsClobberingCall.clear(); |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 817 | CachedUpwardsClobberingAccess.clear(); |
Geoff Berry | 9fe26e6 | 2016-04-22 14:44:10 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Filipe Cabecinhas | 0da9937 | 2016-04-29 15:22:48 +0000 | [diff] [blame] | 820 | #ifdef EXPENSIVE_CHECKS |
Geoff Berry | 9fe26e6 | 2016-04-22 14:44:10 +0000 | [diff] [blame] | 821 | // Run this only when expensive checks are enabled. |
| 822 | verifyRemoved(MA); |
| 823 | #endif |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 824 | } |
| 825 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 826 | void CachingMemorySSAWalker::doCacheRemove(const MemoryAccess *M, |
| 827 | const UpwardsMemoryQuery &Q, |
| 828 | const MemoryLocation &Loc) { |
| 829 | if (Q.IsCall) |
| 830 | CachedUpwardsClobberingCall.erase(M); |
| 831 | else |
| 832 | CachedUpwardsClobberingAccess.erase({M, Loc}); |
| 833 | } |
| 834 | |
| 835 | void CachingMemorySSAWalker::doCacheInsert(const MemoryAccess *M, |
| 836 | MemoryAccess *Result, |
| 837 | const UpwardsMemoryQuery &Q, |
| 838 | const MemoryLocation &Loc) { |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 839 | // This is fine for Phis, since there are times where we can't optimize them. |
| 840 | // Making a def its own clobber is never correct, though. |
| 841 | assert((Result != M || isa<MemoryPhi>(M)) && |
| 842 | "Something can't clobber itself!"); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 843 | ++NumClobberCacheInserts; |
| 844 | if (Q.IsCall) |
| 845 | CachedUpwardsClobberingCall[M] = Result; |
| 846 | else |
| 847 | CachedUpwardsClobberingAccess[{M, Loc}] = Result; |
| 848 | } |
| 849 | |
| 850 | MemoryAccess *CachingMemorySSAWalker::doCacheLookup(const MemoryAccess *M, |
| 851 | const UpwardsMemoryQuery &Q, |
| 852 | const MemoryLocation &Loc) { |
| 853 | ++NumClobberCacheLookups; |
| 854 | MemoryAccess *Result = nullptr; |
| 855 | |
| 856 | if (Q.IsCall) |
| 857 | Result = CachedUpwardsClobberingCall.lookup(M); |
| 858 | else |
| 859 | Result = CachedUpwardsClobberingAccess.lookup({M, Loc}); |
| 860 | |
| 861 | if (Result) |
| 862 | ++NumClobberCacheHits; |
| 863 | return Result; |
| 864 | } |
| 865 | |
| 866 | bool CachingMemorySSAWalker::instructionClobbersQuery( |
| 867 | const MemoryDef *MD, UpwardsMemoryQuery &Q, |
| 868 | const MemoryLocation &Loc) const { |
| 869 | Instruction *DefMemoryInst = MD->getMemoryInst(); |
| 870 | assert(DefMemoryInst && "Defining instruction not actually an instruction"); |
| 871 | |
| 872 | if (!Q.IsCall) |
| 873 | return AA->getModRefInfo(DefMemoryInst, Loc) & MRI_Mod; |
| 874 | |
| 875 | // If this is a call, mark it for caching |
| 876 | if (ImmutableCallSite(DefMemoryInst)) |
George Burgess IV | 49cad7d | 2016-03-30 03:12:08 +0000 | [diff] [blame] | 877 | Q.VisitedCalls.push_back(MD); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 878 | ModRefInfo I = AA->getModRefInfo(DefMemoryInst, ImmutableCallSite(Q.Inst)); |
| 879 | return I != MRI_NoModRef; |
| 880 | } |
| 881 | |
| 882 | MemoryAccessPair CachingMemorySSAWalker::UpwardsDFSWalk( |
| 883 | MemoryAccess *StartingAccess, const MemoryLocation &Loc, |
| 884 | UpwardsMemoryQuery &Q, bool FollowingBackedge) { |
| 885 | MemoryAccess *ModifyingAccess = nullptr; |
| 886 | |
| 887 | auto DFI = df_begin(StartingAccess); |
| 888 | for (auto DFE = df_end(StartingAccess); DFI != DFE;) { |
| 889 | MemoryAccess *CurrAccess = *DFI; |
| 890 | if (MSSA->isLiveOnEntryDef(CurrAccess)) |
| 891 | return {CurrAccess, Loc}; |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 892 | // If this is a MemoryDef, check whether it clobbers our current query. This |
| 893 | // needs to be done before consulting the cache, because the cache reports |
| 894 | // the clobber for CurrAccess. If CurrAccess is a clobber for this query, |
| 895 | // and we ask the cache for information first, then we might skip this |
| 896 | // clobber, which is bad. |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 897 | if (auto *MD = dyn_cast<MemoryDef>(CurrAccess)) { |
| 898 | // If we hit the top, stop following this path. |
| 899 | // While we can do lookups, we can't sanely do inserts here unless we were |
| 900 | // to track everything we saw along the way, since we don't know where we |
| 901 | // will stop. |
| 902 | if (instructionClobbersQuery(MD, Q, Loc)) { |
| 903 | ModifyingAccess = CurrAccess; |
| 904 | break; |
| 905 | } |
| 906 | } |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 907 | if (auto CacheResult = doCacheLookup(CurrAccess, Q, Loc)) |
| 908 | return {CacheResult, Loc}; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 909 | |
| 910 | // We need to know whether it is a phi so we can track backedges. |
| 911 | // Otherwise, walk all upward defs. |
| 912 | if (!isa<MemoryPhi>(CurrAccess)) { |
| 913 | ++DFI; |
| 914 | continue; |
| 915 | } |
| 916 | |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 917 | #ifndef NDEBUG |
| 918 | // The loop below visits the phi's children for us. Because phis are the |
| 919 | // only things with multiple edges, skipping the children should always lead |
| 920 | // us to the end of the loop. |
| 921 | // |
| 922 | // Use a copy of DFI because skipChildren would kill our search stack, which |
| 923 | // would make caching anything on the way back impossible. |
| 924 | auto DFICopy = DFI; |
| 925 | assert(DFICopy.skipChildren() == DFE && |
| 926 | "Skipping phi's children doesn't end the DFS?"); |
| 927 | #endif |
| 928 | |
George Burgess IV | 82ee942 | 2016-03-30 00:26:26 +0000 | [diff] [blame] | 929 | const MemoryAccessPair PHIPair(CurrAccess, Loc); |
| 930 | |
| 931 | // Don't try to optimize this phi again if we've already tried to do so. |
| 932 | if (!Q.Visited.insert(PHIPair).second) { |
| 933 | ModifyingAccess = CurrAccess; |
| 934 | break; |
| 935 | } |
| 936 | |
George Burgess IV | 49cad7d | 2016-03-30 03:12:08 +0000 | [diff] [blame] | 937 | std::size_t InitialVisitedCallSize = Q.VisitedCalls.size(); |
| 938 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 939 | // Recurse on PHI nodes, since we need to change locations. |
| 940 | // TODO: Allow graphtraits on pairs, which would turn this whole function |
| 941 | // into a normal single depth first walk. |
| 942 | MemoryAccess *FirstDef = nullptr; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 943 | for (auto MPI = upward_defs_begin(PHIPair), MPE = upward_defs_end(); |
| 944 | MPI != MPE; ++MPI) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 945 | bool Backedge = |
| 946 | !FollowingBackedge && |
| 947 | DT->dominates(CurrAccess->getBlock(), MPI.getPhiArgBlock()); |
| 948 | |
| 949 | MemoryAccessPair CurrentPair = |
| 950 | UpwardsDFSWalk(MPI->first, MPI->second, Q, Backedge); |
| 951 | // All the phi arguments should reach the same point if we can bypass |
| 952 | // this phi. The alternative is that they hit this phi node, which |
| 953 | // means we can skip this argument. |
| 954 | if (FirstDef && CurrentPair.first != PHIPair.first && |
| 955 | CurrentPair.first != FirstDef) { |
| 956 | ModifyingAccess = CurrAccess; |
| 957 | break; |
| 958 | } |
| 959 | |
| 960 | if (!FirstDef) |
| 961 | FirstDef = CurrentPair.first; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 962 | } |
| 963 | |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 964 | // If we exited the loop early, go with the result it gave us. |
| 965 | if (!ModifyingAccess) { |
George Burgess IV | 82ee942 | 2016-03-30 00:26:26 +0000 | [diff] [blame] | 966 | assert(FirstDef && "Found a Phi with no upward defs?"); |
| 967 | ModifyingAccess = FirstDef; |
George Burgess IV | 49cad7d | 2016-03-30 03:12:08 +0000 | [diff] [blame] | 968 | } else { |
| 969 | // If we can't optimize this Phi, then we can't safely cache any of the |
| 970 | // calls we visited when trying to optimize it. Wipe them out now. |
| 971 | Q.VisitedCalls.resize(InitialVisitedCallSize); |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 972 | } |
| 973 | break; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | if (!ModifyingAccess) |
| 977 | return {MSSA->getLiveOnEntryDef(), Q.StartingLoc}; |
| 978 | |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 979 | const BasicBlock *OriginalBlock = StartingAccess->getBlock(); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 980 | assert(DFI.getPathLength() > 0 && "We dropped our path?"); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 981 | unsigned N = DFI.getPathLength(); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 982 | // If we found a clobbering def, the last element in the path will be our |
| 983 | // clobber, so we don't want to cache that to itself. OTOH, if we optimized a |
| 984 | // phi, we can add the last thing in the path to the cache, since that won't |
| 985 | // be the result. |
| 986 | if (DFI.getPath(N - 1) == ModifyingAccess) |
| 987 | --N; |
| 988 | for (; N > 1; --N) { |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 989 | MemoryAccess *CacheAccess = DFI.getPath(N - 1); |
| 990 | BasicBlock *CurrBlock = CacheAccess->getBlock(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 991 | if (!FollowingBackedge) |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 992 | doCacheInsert(CacheAccess, ModifyingAccess, Q, Loc); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 993 | if (DT->dominates(CurrBlock, OriginalBlock) && |
| 994 | (CurrBlock != OriginalBlock || !FollowingBackedge || |
George Burgess IV | 0e48986 | 2016-03-23 18:31:55 +0000 | [diff] [blame] | 995 | MSSA->locallyDominates(CacheAccess, StartingAccess))) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | |
| 999 | // Cache everything else on the way back. The caller should cache |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 1000 | // StartingAccess for us. |
| 1001 | for (; N > 1; --N) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1002 | MemoryAccess *CacheAccess = DFI.getPath(N - 1); |
| 1003 | doCacheInsert(CacheAccess, ModifyingAccess, Q, Loc); |
| 1004 | } |
| 1005 | assert(Q.Visited.size() < 1000 && "Visited too much"); |
| 1006 | |
| 1007 | return {ModifyingAccess, Loc}; |
| 1008 | } |
| 1009 | |
| 1010 | /// \brief Walk the use-def chains starting at \p MA and find |
| 1011 | /// the MemoryAccess that actually clobbers Loc. |
| 1012 | /// |
| 1013 | /// \returns our clobbering memory access |
| 1014 | MemoryAccess * |
| 1015 | CachingMemorySSAWalker::getClobberingMemoryAccess(MemoryAccess *StartingAccess, |
| 1016 | UpwardsMemoryQuery &Q) { |
| 1017 | return UpwardsDFSWalk(StartingAccess, Q.StartingLoc, Q, false).first; |
| 1018 | } |
| 1019 | |
| 1020 | MemoryAccess * |
| 1021 | CachingMemorySSAWalker::getClobberingMemoryAccess(MemoryAccess *StartingAccess, |
| 1022 | MemoryLocation &Loc) { |
| 1023 | if (isa<MemoryPhi>(StartingAccess)) |
| 1024 | return StartingAccess; |
| 1025 | |
| 1026 | auto *StartingUseOrDef = cast<MemoryUseOrDef>(StartingAccess); |
| 1027 | if (MSSA->isLiveOnEntryDef(StartingUseOrDef)) |
| 1028 | return StartingUseOrDef; |
| 1029 | |
| 1030 | Instruction *I = StartingUseOrDef->getMemoryInst(); |
| 1031 | |
| 1032 | // Conservatively, fences are always clobbers, so don't perform the walk if we |
| 1033 | // hit a fence. |
| 1034 | if (isa<FenceInst>(I)) |
| 1035 | return StartingUseOrDef; |
| 1036 | |
| 1037 | UpwardsMemoryQuery Q; |
| 1038 | Q.OriginalAccess = StartingUseOrDef; |
| 1039 | Q.StartingLoc = Loc; |
| 1040 | Q.Inst = StartingUseOrDef->getMemoryInst(); |
| 1041 | Q.IsCall = false; |
| 1042 | Q.DL = &Q.Inst->getModule()->getDataLayout(); |
| 1043 | |
| 1044 | if (auto CacheResult = doCacheLookup(StartingUseOrDef, Q, Q.StartingLoc)) |
| 1045 | return CacheResult; |
| 1046 | |
| 1047 | // Unlike the other function, do not walk to the def of a def, because we are |
| 1048 | // handed something we already believe is the clobbering access. |
| 1049 | MemoryAccess *DefiningAccess = isa<MemoryUse>(StartingUseOrDef) |
| 1050 | ? StartingUseOrDef->getDefiningAccess() |
| 1051 | : StartingUseOrDef; |
| 1052 | |
| 1053 | MemoryAccess *Clobber = getClobberingMemoryAccess(DefiningAccess, Q); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 1054 | // Only cache this if it wouldn't make Clobber point to itself. |
| 1055 | if (Clobber != StartingAccess) |
| 1056 | doCacheInsert(Q.OriginalAccess, Clobber, Q, Q.StartingLoc); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1057 | DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is "); |
| 1058 | DEBUG(dbgs() << *StartingUseOrDef << "\n"); |
| 1059 | DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is "); |
| 1060 | DEBUG(dbgs() << *Clobber << "\n"); |
| 1061 | return Clobber; |
| 1062 | } |
| 1063 | |
| 1064 | MemoryAccess * |
| 1065 | CachingMemorySSAWalker::getClobberingMemoryAccess(const Instruction *I) { |
| 1066 | // There should be no way to lookup an instruction and get a phi as the |
| 1067 | // access, since we only map BB's to PHI's. So, this must be a use or def. |
| 1068 | auto *StartingAccess = cast<MemoryUseOrDef>(MSSA->getMemoryAccess(I)); |
| 1069 | |
| 1070 | // We can't sanely do anything with a FenceInst, they conservatively |
| 1071 | // clobber all memory, and have no locations to get pointers from to |
| 1072 | // try to disambiguate |
| 1073 | if (isa<FenceInst>(I)) |
| 1074 | return StartingAccess; |
| 1075 | |
| 1076 | UpwardsMemoryQuery Q; |
| 1077 | Q.OriginalAccess = StartingAccess; |
| 1078 | Q.IsCall = bool(ImmutableCallSite(I)); |
| 1079 | if (!Q.IsCall) |
| 1080 | Q.StartingLoc = MemoryLocation::get(I); |
| 1081 | Q.Inst = I; |
| 1082 | Q.DL = &Q.Inst->getModule()->getDataLayout(); |
| 1083 | if (auto CacheResult = doCacheLookup(StartingAccess, Q, Q.StartingLoc)) |
| 1084 | return CacheResult; |
| 1085 | |
| 1086 | // Start with the thing we already think clobbers this location |
| 1087 | MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess(); |
| 1088 | |
| 1089 | // At this point, DefiningAccess may be the live on entry def. |
| 1090 | // If it is, we will not get a better result. |
| 1091 | if (MSSA->isLiveOnEntryDef(DefiningAccess)) |
| 1092 | return DefiningAccess; |
| 1093 | |
| 1094 | MemoryAccess *Result = getClobberingMemoryAccess(DefiningAccess, Q); |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 1095 | // DFS won't cache a result for DefiningAccess. So, if DefiningAccess isn't |
| 1096 | // our clobber, be sure that it gets a cache entry, too. |
| 1097 | if (Result != DefiningAccess) |
| 1098 | doCacheInsert(DefiningAccess, Result, Q, Q.StartingLoc); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1099 | doCacheInsert(Q.OriginalAccess, Result, Q, Q.StartingLoc); |
| 1100 | // TODO: When this implementation is more mature, we may want to figure out |
| 1101 | // what this additional caching buys us. It's most likely A Good Thing. |
| 1102 | if (Q.IsCall) |
| 1103 | for (const MemoryAccess *MA : Q.VisitedCalls) |
George Burgess IV | 1b1fef3 | 2016-04-29 18:42:55 +0000 | [diff] [blame] | 1104 | if (MA != Result) |
| 1105 | doCacheInsert(MA, Result, Q, Q.StartingLoc); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1106 | |
| 1107 | DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is "); |
| 1108 | DEBUG(dbgs() << *DefiningAccess << "\n"); |
| 1109 | DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is "); |
| 1110 | DEBUG(dbgs() << *Result << "\n"); |
| 1111 | |
| 1112 | return Result; |
| 1113 | } |
| 1114 | |
Geoff Berry | 9fe26e6 | 2016-04-22 14:44:10 +0000 | [diff] [blame] | 1115 | // Verify that MA doesn't exist in any of the caches. |
| 1116 | void CachingMemorySSAWalker::verifyRemoved(MemoryAccess *MA) { |
| 1117 | #ifndef NDEBUG |
| 1118 | for (auto &P : CachedUpwardsClobberingAccess) |
| 1119 | assert(P.first.first != MA && P.second != MA && |
| 1120 | "Found removed MemoryAccess in cache."); |
| 1121 | for (auto &P : CachedUpwardsClobberingCall) |
| 1122 | assert(P.first != MA && P.second != MA && |
| 1123 | "Found removed MemoryAccess in cache."); |
| 1124 | #endif // !NDEBUG |
| 1125 | } |
| 1126 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1127 | MemoryAccess * |
| 1128 | DoNothingMemorySSAWalker::getClobberingMemoryAccess(const Instruction *I) { |
| 1129 | MemoryAccess *MA = MSSA->getMemoryAccess(I); |
| 1130 | if (auto *Use = dyn_cast<MemoryUseOrDef>(MA)) |
| 1131 | return Use->getDefiningAccess(); |
| 1132 | return MA; |
| 1133 | } |
| 1134 | |
| 1135 | MemoryAccess *DoNothingMemorySSAWalker::getClobberingMemoryAccess( |
| 1136 | MemoryAccess *StartingAccess, MemoryLocation &) { |
| 1137 | if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess)) |
| 1138 | return Use->getDefiningAccess(); |
| 1139 | return StartingAccess; |
| 1140 | } |
| 1141 | } |