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