Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1 | //===- MemorySSA.cpp - Memory SSA Builder ---------------------------------===// |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 6 | // |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements the MemorySSA class. |
| 10 | // |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Daniel Berlin | 554dcd8 | 2017-04-11 20:06:36 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/MemorySSA.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMapInfo.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
| 17 | #include "llvm/ADT/DepthFirstIterator.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Hashing.h" |
| 19 | #include "llvm/ADT/None.h" |
| 20 | #include "llvm/ADT/Optional.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
| 22 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
| 24 | #include "llvm/ADT/iterator.h" |
| 25 | #include "llvm/ADT/iterator_range.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AliasAnalysis.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/IteratedDominanceFrontier.h" |
| 28 | #include "llvm/Analysis/MemoryLocation.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 29 | #include "llvm/Config/llvm-config.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 30 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 31 | #include "llvm/IR/BasicBlock.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Function.h" |
| 34 | #include "llvm/IR/Instruction.h" |
| 35 | #include "llvm/IR/Instructions.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 36 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Intrinsics.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 38 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 39 | #include "llvm/IR/PassManager.h" |
| 40 | #include "llvm/IR/Use.h" |
| 41 | #include "llvm/Pass.h" |
| 42 | #include "llvm/Support/AtomicOrdering.h" |
| 43 | #include "llvm/Support/Casting.h" |
| 44 | #include "llvm/Support/CommandLine.h" |
| 45 | #include "llvm/Support/Compiler.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ErrorHandling.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 48 | #include "llvm/Support/FormattedStream.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 51 | #include <cassert> |
Alina Sbirlea | 63e97fa | 2019-07-31 17:41:04 +0000 | [diff] [blame^] | 52 | #include <cstdlib> |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 53 | #include <iterator> |
| 54 | #include <memory> |
| 55 | #include <utility> |
| 56 | |
| 57 | using namespace llvm; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 58 | |
| 59 | #define DEBUG_TYPE "memoryssa" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 60 | |
Geoff Berry | efb0dd1 | 2016-06-14 21:19:40 +0000 | [diff] [blame] | 61 | INITIALIZE_PASS_BEGIN(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false, |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 62 | true) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 64 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Geoff Berry | efb0dd1 | 2016-06-14 21:19:40 +0000 | [diff] [blame] | 65 | INITIALIZE_PASS_END(MemorySSAWrapperPass, "memoryssa", "Memory SSA", false, |
| 66 | true) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 67 | |
Chad Rosier | 232e29e | 2016-07-06 21:20:47 +0000 | [diff] [blame] | 68 | INITIALIZE_PASS_BEGIN(MemorySSAPrinterLegacyPass, "print-memoryssa", |
| 69 | "Memory SSA Printer", false, false) |
| 70 | INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) |
| 71 | INITIALIZE_PASS_END(MemorySSAPrinterLegacyPass, "print-memoryssa", |
| 72 | "Memory SSA Printer", false, false) |
| 73 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 74 | static cl::opt<unsigned> MaxCheckLimit( |
| 75 | "memssa-check-limit", cl::Hidden, cl::init(100), |
| 76 | cl::desc("The maximum number of stores/phis MemorySSA" |
| 77 | "will consider trying to walk past (default = 100)")); |
| 78 | |
Alina Sbirlea | cc2e8cc | 2018-08-15 17:34:55 +0000 | [diff] [blame] | 79 | // Always verify MemorySSA if expensive checking is enabled. |
| 80 | #ifdef EXPENSIVE_CHECKS |
| 81 | bool llvm::VerifyMemorySSA = true; |
| 82 | #else |
| 83 | bool llvm::VerifyMemorySSA = false; |
| 84 | #endif |
Alina Sbirlea | 4fd1f26 | 2019-04-23 20:59:44 +0000 | [diff] [blame] | 85 | /// Enables memory ssa as a dependency for loop passes in legacy pass manager. |
| 86 | cl::opt<bool> llvm::EnableMSSALoopDependency( |
| 87 | "enable-mssa-loop-dependency", cl::Hidden, cl::init(false), |
| 88 | cl::desc("Enable MemorySSA dependency for loop pass manager")); |
| 89 | |
Alina Sbirlea | cc2e8cc | 2018-08-15 17:34:55 +0000 | [diff] [blame] | 90 | static cl::opt<bool, true> |
| 91 | VerifyMemorySSAX("verify-memoryssa", cl::location(VerifyMemorySSA), |
| 92 | cl::Hidden, cl::desc("Enable verification of MemorySSA.")); |
Chad Rosier | 232e29e | 2016-07-06 21:20:47 +0000 | [diff] [blame] | 93 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 94 | namespace llvm { |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 95 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 96 | /// An assembly annotator class to print Memory SSA information in |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 97 | /// comments. |
| 98 | class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter { |
| 99 | friend class MemorySSA; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 100 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 101 | const MemorySSA *MSSA; |
| 102 | |
| 103 | public: |
| 104 | MemorySSAAnnotatedWriter(const MemorySSA *M) : MSSA(M) {} |
| 105 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 106 | void emitBasicBlockStartAnnot(const BasicBlock *BB, |
| 107 | formatted_raw_ostream &OS) override { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 108 | if (MemoryAccess *MA = MSSA->getMemoryAccess(BB)) |
| 109 | OS << "; " << *MA << "\n"; |
| 110 | } |
| 111 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 112 | void emitInstructionAnnot(const Instruction *I, |
| 113 | formatted_raw_ostream &OS) override { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 114 | if (MemoryAccess *MA = MSSA->getMemoryAccess(I)) |
| 115 | OS << "; " << *MA << "\n"; |
| 116 | } |
| 117 | }; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 118 | |
| 119 | } // end namespace llvm |
George Burgess IV | fd1f2f8 | 2016-06-24 21:02:12 +0000 | [diff] [blame] | 120 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 121 | namespace { |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 122 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 123 | /// Our current alias analysis API differentiates heavily between calls and |
| 124 | /// non-calls, and functions called on one usually assert on the other. |
| 125 | /// This class encapsulates the distinction to simplify other code that wants |
| 126 | /// "Memory affecting instructions and related data" to use as a key. |
| 127 | /// For example, this class is used as a densemap key in the use optimizer. |
| 128 | class MemoryLocOrCall { |
| 129 | public: |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 130 | bool IsCall = false; |
| 131 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 132 | MemoryLocOrCall(MemoryUseOrDef *MUD) |
| 133 | : MemoryLocOrCall(MUD->getMemoryInst()) {} |
Sebastian Pop | 5068d7a | 2016-10-13 03:23:33 +0000 | [diff] [blame] | 134 | MemoryLocOrCall(const MemoryUseOrDef *MUD) |
| 135 | : MemoryLocOrCall(MUD->getMemoryInst()) {} |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 136 | |
| 137 | MemoryLocOrCall(Instruction *Inst) { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 138 | if (auto *C = dyn_cast<CallBase>(Inst)) { |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 139 | IsCall = true; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 140 | Call = C; |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 141 | } else { |
| 142 | IsCall = false; |
| 143 | // There is no such thing as a memorylocation for a fence inst, and it is |
| 144 | // unique in that regard. |
| 145 | if (!isa<FenceInst>(Inst)) |
| 146 | Loc = MemoryLocation::get(Inst); |
| 147 | } |
| 148 | } |
| 149 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 150 | explicit MemoryLocOrCall(const MemoryLocation &Loc) : Loc(Loc) {} |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 152 | const CallBase *getCall() const { |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 153 | assert(IsCall); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 154 | return Call; |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 155 | } |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 156 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 157 | MemoryLocation getLoc() const { |
| 158 | assert(!IsCall); |
| 159 | return Loc; |
| 160 | } |
| 161 | |
| 162 | bool operator==(const MemoryLocOrCall &Other) const { |
| 163 | if (IsCall != Other.IsCall) |
| 164 | return false; |
| 165 | |
George Burgess IV | 3588fd4 | 2018-03-29 00:54:39 +0000 | [diff] [blame] | 166 | if (!IsCall) |
| 167 | return Loc == Other.Loc; |
| 168 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 169 | if (Call->getCalledValue() != Other.Call->getCalledValue()) |
George Burgess IV | 3588fd4 | 2018-03-29 00:54:39 +0000 | [diff] [blame] | 170 | return false; |
| 171 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 172 | return Call->arg_size() == Other.Call->arg_size() && |
| 173 | std::equal(Call->arg_begin(), Call->arg_end(), |
| 174 | Other.Call->arg_begin()); |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
Daniel Berlin | f536113 | 2016-10-22 04:15:41 +0000 | [diff] [blame] | 178 | union { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 179 | const CallBase *Call; |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 180 | MemoryLocation Loc; |
Daniel Berlin | f536113 | 2016-10-22 04:15:41 +0000 | [diff] [blame] | 181 | }; |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 182 | }; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 183 | |
| 184 | } // end anonymous namespace |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 185 | |
| 186 | namespace llvm { |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 187 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 188 | template <> struct DenseMapInfo<MemoryLocOrCall> { |
| 189 | static inline MemoryLocOrCall getEmptyKey() { |
| 190 | return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getEmptyKey()); |
| 191 | } |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 192 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 193 | static inline MemoryLocOrCall getTombstoneKey() { |
| 194 | return MemoryLocOrCall(DenseMapInfo<MemoryLocation>::getTombstoneKey()); |
| 195 | } |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 196 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 197 | static unsigned getHashValue(const MemoryLocOrCall &MLOC) { |
George Burgess IV | 3588fd4 | 2018-03-29 00:54:39 +0000 | [diff] [blame] | 198 | if (!MLOC.IsCall) |
| 199 | return hash_combine( |
| 200 | MLOC.IsCall, |
| 201 | DenseMapInfo<MemoryLocation>::getHashValue(MLOC.getLoc())); |
| 202 | |
| 203 | hash_code hash = |
| 204 | hash_combine(MLOC.IsCall, DenseMapInfo<const Value *>::getHashValue( |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 205 | MLOC.getCall()->getCalledValue())); |
George Burgess IV | 3588fd4 | 2018-03-29 00:54:39 +0000 | [diff] [blame] | 206 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 207 | for (const Value *Arg : MLOC.getCall()->args()) |
George Burgess IV | 3588fd4 | 2018-03-29 00:54:39 +0000 | [diff] [blame] | 208 | hash = hash_combine(hash, DenseMapInfo<const Value *>::getHashValue(Arg)); |
| 209 | return hash; |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 210 | } |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 211 | |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 212 | static bool isEqual(const MemoryLocOrCall &LHS, const MemoryLocOrCall &RHS) { |
| 213 | return LHS == RHS; |
| 214 | } |
| 215 | }; |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 216 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 217 | } // end namespace llvm |
| 218 | |
George Burgess IV | 82e355c | 2016-08-03 19:39:54 +0000 | [diff] [blame] | 219 | /// This does one-way checks to see if Use could theoretically be hoisted above |
| 220 | /// MayClobber. This will not check the other way around. |
| 221 | /// |
| 222 | /// This assumes that, for the purposes of MemorySSA, Use comes directly after |
| 223 | /// MayClobber, with no potentially clobbering operations in between them. |
| 224 | /// (Where potentially clobbering ops are memory barriers, aliased stores, etc.) |
Alina Sbirlea | ca741a8 | 2017-12-22 19:54:03 +0000 | [diff] [blame] | 225 | static bool areLoadsReorderable(const LoadInst *Use, |
| 226 | const LoadInst *MayClobber) { |
George Burgess IV | 82e355c | 2016-08-03 19:39:54 +0000 | [diff] [blame] | 227 | bool VolatileUse = Use->isVolatile(); |
| 228 | bool VolatileClobber = MayClobber->isVolatile(); |
| 229 | // Volatile operations may never be reordered with other volatile operations. |
| 230 | if (VolatileUse && VolatileClobber) |
Alina Sbirlea | ca741a8 | 2017-12-22 19:54:03 +0000 | [diff] [blame] | 231 | return false; |
| 232 | // Otherwise, volatile doesn't matter here. From the language reference: |
| 233 | // 'optimizers may change the order of volatile operations relative to |
| 234 | // non-volatile operations.'" |
George Burgess IV | 82e355c | 2016-08-03 19:39:54 +0000 | [diff] [blame] | 235 | |
| 236 | // If a load is seq_cst, it cannot be moved above other loads. If its ordering |
| 237 | // is weaker, it can be moved above other loads. We just need to be sure that |
| 238 | // MayClobber isn't an acquire load, because loads can't be moved above |
| 239 | // acquire loads. |
| 240 | // |
| 241 | // Note that this explicitly *does* allow the free reordering of monotonic (or |
| 242 | // weaker) loads of the same address. |
| 243 | bool SeqCstUse = Use->getOrdering() == AtomicOrdering::SequentiallyConsistent; |
| 244 | bool MayClobberIsAcquire = isAtLeastOrStrongerThan(MayClobber->getOrdering(), |
| 245 | AtomicOrdering::Acquire); |
Alina Sbirlea | ca741a8 | 2017-12-22 19:54:03 +0000 | [diff] [blame] | 246 | return !(SeqCstUse || MayClobberIsAcquire); |
George Burgess IV | 82e355c | 2016-08-03 19:39:54 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 249 | namespace { |
| 250 | |
| 251 | struct ClobberAlias { |
| 252 | bool IsClobber; |
| 253 | Optional<AliasResult> AR; |
| 254 | }; |
| 255 | |
| 256 | } // end anonymous namespace |
| 257 | |
| 258 | // Return a pair of {IsClobber (bool), AR (AliasResult)}. It relies on AR being |
| 259 | // ignored if IsClobber = false. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 260 | template <typename AliasAnalysisType> |
| 261 | static ClobberAlias |
| 262 | instructionClobbersQuery(const MemoryDef *MD, const MemoryLocation &UseLoc, |
| 263 | const Instruction *UseInst, AliasAnalysisType &AA) { |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 264 | Instruction *DefInst = MD->getMemoryInst(); |
| 265 | assert(DefInst && "Defining instruction not actually an instruction"); |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 266 | const auto *UseCall = dyn_cast<CallBase>(UseInst); |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 267 | Optional<AliasResult> AR; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 268 | |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 269 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) { |
| 270 | // These intrinsics will show up as affecting memory, but they are just |
George Burgess IV | ff08c80 | 2018-08-10 05:14:43 +0000 | [diff] [blame] | 271 | // markers, mostly. |
| 272 | // |
| 273 | // FIXME: We probably don't actually want MemorySSA to model these at all |
| 274 | // (including creating MemoryAccesses for them): we just end up inventing |
| 275 | // clobbers where they don't really exist at all. Please see D43269 for |
| 276 | // context. |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 277 | switch (II->getIntrinsicID()) { |
| 278 | case Intrinsic::lifetime_start: |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 279 | if (UseCall) |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 280 | return {false, NoAlias}; |
| 281 | AR = AA.alias(MemoryLocation(II->getArgOperand(1)), UseLoc); |
George Burgess IV | ff08c80 | 2018-08-10 05:14:43 +0000 | [diff] [blame] | 282 | return {AR != NoAlias, AR}; |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 283 | case Intrinsic::lifetime_end: |
| 284 | case Intrinsic::invariant_start: |
| 285 | case Intrinsic::invariant_end: |
| 286 | case Intrinsic::assume: |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 287 | return {false, NoAlias}; |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 293 | if (UseCall) { |
| 294 | ModRefInfo I = AA.getModRefInfo(DefInst, UseCall); |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 295 | AR = isMustSet(I) ? MustAlias : MayAlias; |
| 296 | return {isModOrRefSet(I), AR}; |
Hans Wennborg | 70e22d1 | 2017-11-21 18:00:01 +0000 | [diff] [blame] | 297 | } |
George Burgess IV | 82e355c | 2016-08-03 19:39:54 +0000 | [diff] [blame] | 298 | |
Alina Sbirlea | ca741a8 | 2017-12-22 19:54:03 +0000 | [diff] [blame] | 299 | if (auto *DefLoad = dyn_cast<LoadInst>(DefInst)) |
| 300 | if (auto *UseLoad = dyn_cast<LoadInst>(UseInst)) |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 301 | return {!areLoadsReorderable(UseLoad, DefLoad), MayAlias}; |
George Burgess IV | 82e355c | 2016-08-03 19:39:54 +0000 | [diff] [blame] | 302 | |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 303 | ModRefInfo I = AA.getModRefInfo(DefInst, UseLoc); |
| 304 | AR = isMustSet(I) ? MustAlias : MayAlias; |
| 305 | return {isModSet(I), AR}; |
Daniel Berlin | dff31de | 2016-08-02 21:57:52 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 308 | template <typename AliasAnalysisType> |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 309 | static ClobberAlias instructionClobbersQuery(MemoryDef *MD, |
| 310 | const MemoryUseOrDef *MU, |
| 311 | const MemoryLocOrCall &UseMLOC, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 312 | AliasAnalysisType &AA) { |
Sebastian Pop | 5068d7a | 2016-10-13 03:23:33 +0000 | [diff] [blame] | 313 | // FIXME: This is a temporary hack to allow a single instructionClobbersQuery |
| 314 | // to exist while MemoryLocOrCall is pushed through places. |
| 315 | if (UseMLOC.IsCall) |
| 316 | return instructionClobbersQuery(MD, MemoryLocation(), MU->getMemoryInst(), |
| 317 | AA); |
| 318 | return instructionClobbersQuery(MD, UseMLOC.getLoc(), MU->getMemoryInst(), |
| 319 | AA); |
| 320 | } |
| 321 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 322 | // Return true when MD may alias MU, return false otherwise. |
Daniel Berlin | dcb004f | 2017-03-02 23:06:46 +0000 | [diff] [blame] | 323 | bool MemorySSAUtil::defClobbersUseOrDef(MemoryDef *MD, const MemoryUseOrDef *MU, |
| 324 | AliasAnalysis &AA) { |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 325 | return instructionClobbersQuery(MD, MU, MemoryLocOrCall(MU), AA).IsClobber; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 326 | } |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 327 | |
| 328 | namespace { |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 329 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 330 | struct UpwardsMemoryQuery { |
| 331 | // True if our original query started off as a call |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 332 | bool IsCall = false; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 333 | // The pointer location we started the query with. This will be empty if |
| 334 | // IsCall is true. |
| 335 | MemoryLocation StartingLoc; |
| 336 | // This is the instruction we were querying about. |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 337 | const Instruction *Inst = nullptr; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 338 | // The MemoryAccess we actually got called with, used to test local domination |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 339 | const MemoryAccess *OriginalAccess = nullptr; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 340 | Optional<AliasResult> AR = MayAlias; |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 341 | bool SkipSelfAccess = false; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 342 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 343 | UpwardsMemoryQuery() = default; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 344 | |
| 345 | UpwardsMemoryQuery(const Instruction *Inst, const MemoryAccess *Access) |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 346 | : IsCall(isa<CallBase>(Inst)), Inst(Inst), OriginalAccess(Access) { |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 347 | if (!IsCall) |
| 348 | StartingLoc = MemoryLocation::get(Inst); |
| 349 | } |
| 350 | }; |
| 351 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 352 | } // end anonymous namespace |
| 353 | |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 354 | static bool lifetimeEndsAt(MemoryDef *MD, const MemoryLocation &Loc, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 355 | BatchAAResults &AA) { |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 356 | Instruction *Inst = MD->getMemoryInst(); |
| 357 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
| 358 | switch (II->getIntrinsicID()) { |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 359 | case Intrinsic::lifetime_end: |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 360 | return AA.alias(MemoryLocation(II->getArgOperand(1)), Loc) == MustAlias; |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 361 | default: |
| 362 | return false; |
| 363 | } |
| 364 | } |
| 365 | return false; |
| 366 | } |
| 367 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 368 | template <typename AliasAnalysisType> |
| 369 | static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysisType &AA, |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 370 | const Instruction *I) { |
| 371 | // If the memory can't be changed, then loads of the memory can't be |
| 372 | // clobbered. |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 373 | return isa<LoadInst>(I) && (I->getMetadata(LLVMContext::MD_invariant_load) || |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 374 | AA.pointsToConstantMemory(MemoryLocation( |
| 375 | cast<LoadInst>(I)->getPointerOperand()))); |
Sebastian Pop | 5ba9f24 | 2016-10-13 01:39:10 +0000 | [diff] [blame] | 376 | } |
| 377 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 378 | /// Verifies that `Start` is clobbered by `ClobberAt`, and that nothing |
| 379 | /// inbetween `Start` and `ClobberAt` can clobbers `Start`. |
| 380 | /// |
| 381 | /// This is meant to be as simple and self-contained as possible. Because it |
| 382 | /// uses no cache, etc., it can be relatively expensive. |
| 383 | /// |
| 384 | /// \param Start The MemoryAccess that we want to walk from. |
| 385 | /// \param ClobberAt A clobber for Start. |
| 386 | /// \param StartLoc The MemoryLocation for Start. |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 387 | /// \param MSSA The MemorySSA instance that Start and ClobberAt belong to. |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 388 | /// \param Query The UpwardsMemoryQuery we used for our search. |
| 389 | /// \param AA The AliasAnalysis we used for our search. |
Alina Sbirlea | 65f385d | 2018-09-07 23:51:41 +0000 | [diff] [blame] | 390 | /// \param AllowImpreciseClobber Always false, unless we do relaxed verify. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 391 | |
| 392 | template <typename AliasAnalysisType> |
Alina Sbirlea | d77edc0 | 2019-02-11 19:51:21 +0000 | [diff] [blame] | 393 | LLVM_ATTRIBUTE_UNUSED static void |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 394 | checkClobberSanity(const MemoryAccess *Start, MemoryAccess *ClobberAt, |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 395 | const MemoryLocation &StartLoc, const MemorySSA &MSSA, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 396 | const UpwardsMemoryQuery &Query, AliasAnalysisType &AA, |
Alina Sbirlea | 65f385d | 2018-09-07 23:51:41 +0000 | [diff] [blame] | 397 | bool AllowImpreciseClobber = false) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 398 | assert(MSSA.dominates(ClobberAt, Start) && "Clobber doesn't dominate start?"); |
| 399 | |
| 400 | if (MSSA.isLiveOnEntryDef(Start)) { |
| 401 | assert(MSSA.isLiveOnEntryDef(ClobberAt) && |
| 402 | "liveOnEntry must clobber itself"); |
| 403 | return; |
| 404 | } |
| 405 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 406 | bool FoundClobber = false; |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 407 | DenseSet<ConstMemoryAccessPair> VisitedPhis; |
| 408 | SmallVector<ConstMemoryAccessPair, 8> Worklist; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 409 | Worklist.emplace_back(Start, StartLoc); |
| 410 | // Walk all paths from Start to ClobberAt, while looking for clobbers. If one |
| 411 | // is found, complain. |
| 412 | while (!Worklist.empty()) { |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 413 | auto MAP = Worklist.pop_back_val(); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 414 | // All we care about is that nothing from Start to ClobberAt clobbers Start. |
| 415 | // We learn nothing from revisiting nodes. |
| 416 | if (!VisitedPhis.insert(MAP).second) |
| 417 | continue; |
| 418 | |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 419 | for (const auto *MA : def_chain(MAP.first)) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 420 | if (MA == ClobberAt) { |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 421 | if (const auto *MD = dyn_cast<MemoryDef>(MA)) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 422 | // instructionClobbersQuery isn't essentially free, so don't use `|=`, |
| 423 | // since it won't let us short-circuit. |
| 424 | // |
| 425 | // Also, note that this can't be hoisted out of the `Worklist` loop, |
| 426 | // since MD may only act as a clobber for 1 of N MemoryLocations. |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 427 | FoundClobber = FoundClobber || MSSA.isLiveOnEntryDef(MD); |
| 428 | if (!FoundClobber) { |
| 429 | ClobberAlias CA = |
| 430 | instructionClobbersQuery(MD, MAP.second, Query.Inst, AA); |
| 431 | if (CA.IsClobber) { |
| 432 | FoundClobber = true; |
| 433 | // Not used: CA.AR; |
| 434 | } |
| 435 | } |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 436 | } |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | // We should never hit liveOnEntry, unless it's the clobber. |
| 441 | assert(!MSSA.isLiveOnEntryDef(MA) && "Hit liveOnEntry before clobber?"); |
| 442 | |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 443 | if (const auto *MD = dyn_cast<MemoryDef>(MA)) { |
Alina Sbirlea | 5bce4d5 | 2018-08-29 22:38:51 +0000 | [diff] [blame] | 444 | // If Start is a Def, skip self. |
| 445 | if (MD == Start) |
| 446 | continue; |
| 447 | |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 448 | assert(!instructionClobbersQuery(MD, MAP.second, Query.Inst, AA) |
| 449 | .IsClobber && |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 450 | "Found clobber before reaching ClobberAt!"); |
| 451 | continue; |
| 452 | } |
| 453 | |
Alina Sbirlea | 5bce4d5 | 2018-08-29 22:38:51 +0000 | [diff] [blame] | 454 | if (const auto *MU = dyn_cast<MemoryUse>(MA)) { |
Alina Sbirlea | 6edcc9e | 2018-08-29 23:20:29 +0000 | [diff] [blame] | 455 | (void)MU; |
Alina Sbirlea | 5bce4d5 | 2018-08-29 22:38:51 +0000 | [diff] [blame] | 456 | assert (MU == Start && |
| 457 | "Can only find use in def chain if Start is a use"); |
| 458 | continue; |
| 459 | } |
| 460 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 461 | assert(isa<MemoryPhi>(MA)); |
Alina Sbirlea | f5403d8 | 2018-08-29 18:26:04 +0000 | [diff] [blame] | 462 | Worklist.append( |
| 463 | upward_defs_begin({const_cast<MemoryAccess *>(MA), MAP.second}), |
| 464 | upward_defs_end()); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Alina Sbirlea | 65f385d | 2018-09-07 23:51:41 +0000 | [diff] [blame] | 468 | // If the verify is done following an optimization, it's possible that |
| 469 | // ClobberAt was a conservative clobbering, that we can now infer is not a |
| 470 | // true clobbering access. Don't fail the verify if that's the case. |
| 471 | // We do have accesses that claim they're optimized, but could be optimized |
| 472 | // further. Updating all these can be expensive, so allow it for now (FIXME). |
| 473 | if (AllowImpreciseClobber) |
| 474 | return; |
| 475 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 476 | // If ClobberAt is a MemoryPhi, we can assume something above it acted as a |
| 477 | // clobber. Otherwise, `ClobberAt` should've acted as a clobber at some point. |
| 478 | assert((isa<MemoryPhi>(ClobberAt) || FoundClobber) && |
| 479 | "ClobberAt never acted as a clobber"); |
| 480 | } |
| 481 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 482 | namespace { |
| 483 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 484 | /// Our algorithm for walking (and trying to optimize) clobbers, all wrapped up |
| 485 | /// in one class. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 486 | template <class AliasAnalysisType> class ClobberWalker { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 487 | /// Save a few bytes by using unsigned instead of size_t. |
| 488 | using ListIndex = unsigned; |
| 489 | |
| 490 | /// Represents a span of contiguous MemoryDefs, potentially ending in a |
| 491 | /// MemoryPhi. |
| 492 | struct DefPath { |
| 493 | MemoryLocation Loc; |
| 494 | // Note that, because we always walk in reverse, Last will always dominate |
| 495 | // First. Also note that First and Last are inclusive. |
| 496 | MemoryAccess *First; |
| 497 | MemoryAccess *Last; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 498 | Optional<ListIndex> Previous; |
| 499 | |
| 500 | DefPath(const MemoryLocation &Loc, MemoryAccess *First, MemoryAccess *Last, |
| 501 | Optional<ListIndex> Previous) |
| 502 | : Loc(Loc), First(First), Last(Last), Previous(Previous) {} |
| 503 | |
| 504 | DefPath(const MemoryLocation &Loc, MemoryAccess *Init, |
| 505 | Optional<ListIndex> Previous) |
| 506 | : DefPath(Loc, Init, Init, Previous) {} |
| 507 | }; |
| 508 | |
| 509 | const MemorySSA &MSSA; |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 510 | AliasAnalysisType &AA; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 511 | DominatorTree &DT; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 512 | UpwardsMemoryQuery *Query; |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 513 | unsigned *UpwardWalkLimit; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 514 | |
| 515 | // Phi optimization bookkeeping |
| 516 | SmallVector<DefPath, 32> Paths; |
| 517 | DenseSet<ConstMemoryAccessPair> VisitedPhis; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 518 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 519 | /// Find the nearest def or phi that `From` can legally be optimized to. |
Daniel Berlin | d042031 | 2017-04-01 09:01:12 +0000 | [diff] [blame] | 520 | const MemoryAccess *getWalkTarget(const MemoryPhi *From) const { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 521 | assert(From->getNumOperands() && "Phi with no operands?"); |
| 522 | |
| 523 | BasicBlock *BB = From->getBlock(); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 524 | MemoryAccess *Result = MSSA.getLiveOnEntryDef(); |
| 525 | DomTreeNode *Node = DT.getNode(BB); |
| 526 | while ((Node = Node->getIDom())) { |
Daniel Berlin | 7500c56 | 2017-04-01 08:59:45 +0000 | [diff] [blame] | 527 | auto *Defs = MSSA.getBlockDefs(Node->getBlock()); |
| 528 | if (Defs) |
Daniel Berlin | d042031 | 2017-04-01 09:01:12 +0000 | [diff] [blame] | 529 | return &*Defs->rbegin(); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 530 | } |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 531 | return Result; |
| 532 | } |
| 533 | |
| 534 | /// Result of calling walkToPhiOrClobber. |
| 535 | struct UpwardsWalkResult { |
| 536 | /// The "Result" of the walk. Either a clobber, the last thing we walked, or |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 537 | /// both. Include alias info when clobber found. |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 538 | MemoryAccess *Result; |
| 539 | bool IsKnownClobber; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 540 | Optional<AliasResult> AR; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
| 543 | /// Walk to the next Phi or Clobber in the def chain starting at Desc.Last. |
| 544 | /// This will update Desc.Last as it walks. It will (optionally) also stop at |
| 545 | /// StopAt. |
| 546 | /// |
| 547 | /// This does not test for whether StopAt is a clobber |
Daniel Berlin | d042031 | 2017-04-01 09:01:12 +0000 | [diff] [blame] | 548 | UpwardsWalkResult |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 549 | walkToPhiOrClobber(DefPath &Desc, const MemoryAccess *StopAt = nullptr, |
| 550 | const MemoryAccess *SkipStopAt = nullptr) const { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 551 | assert(!isa<MemoryUse>(Desc.Last) && "Uses don't exist in my world"); |
Alina Sbirlea | c8d6e04 | 2019-03-29 22:55:59 +0000 | [diff] [blame] | 552 | assert(UpwardWalkLimit && "Need a valid walk limit"); |
Alina Sbirlea | 5776938 | 2019-04-12 18:48:46 +0000 | [diff] [blame] | 553 | bool LimitAlreadyReached = false; |
| 554 | // (*UpwardWalkLimit) may be 0 here, due to the loop in tryOptimizePhi. Set |
| 555 | // it to 1. This will not do any alias() calls. It either returns in the |
| 556 | // first iteration in the loop below, or is set back to 0 if all def chains |
| 557 | // are free of MemoryDefs. |
| 558 | if (!*UpwardWalkLimit) { |
| 559 | *UpwardWalkLimit = 1; |
| 560 | LimitAlreadyReached = true; |
| 561 | } |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 562 | |
| 563 | for (MemoryAccess *Current : def_chain(Desc.Last)) { |
| 564 | Desc.Last = Current; |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 565 | if (Current == StopAt || Current == SkipStopAt) |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 566 | return {Current, false, MayAlias}; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 567 | |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 568 | if (auto *MD = dyn_cast<MemoryDef>(Current)) { |
| 569 | if (MSSA.isLiveOnEntryDef(MD)) |
| 570 | return {MD, true, MustAlias}; |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 571 | |
| 572 | if (!--*UpwardWalkLimit) |
| 573 | return {Current, true, MayAlias}; |
| 574 | |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 575 | ClobberAlias CA = |
| 576 | instructionClobbersQuery(MD, Desc.Loc, Query->Inst, AA); |
| 577 | if (CA.IsClobber) |
| 578 | return {MD, true, CA.AR}; |
| 579 | } |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Alina Sbirlea | 5776938 | 2019-04-12 18:48:46 +0000 | [diff] [blame] | 582 | if (LimitAlreadyReached) |
| 583 | *UpwardWalkLimit = 0; |
| 584 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 585 | assert(isa<MemoryPhi>(Desc.Last) && |
| 586 | "Ended at a non-clobber that's not a phi?"); |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 587 | return {Desc.Last, false, MayAlias}; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void addSearches(MemoryPhi *Phi, SmallVectorImpl<ListIndex> &PausedSearches, |
| 591 | ListIndex PriorNode) { |
| 592 | auto UpwardDefs = make_range(upward_defs_begin({Phi, Paths[PriorNode].Loc}), |
| 593 | upward_defs_end()); |
| 594 | for (const MemoryAccessPair &P : UpwardDefs) { |
| 595 | PausedSearches.push_back(Paths.size()); |
| 596 | Paths.emplace_back(P.second, P.first, PriorNode); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /// Represents a search that terminated after finding a clobber. This clobber |
| 601 | /// may or may not be present in the path of defs from LastNode..SearchStart, |
| 602 | /// since it may have been retrieved from cache. |
| 603 | struct TerminatedPath { |
| 604 | MemoryAccess *Clobber; |
| 605 | ListIndex LastNode; |
| 606 | }; |
| 607 | |
| 608 | /// Get an access that keeps us from optimizing to the given phi. |
| 609 | /// |
| 610 | /// PausedSearches is an array of indices into the Paths array. Its incoming |
| 611 | /// value is the indices of searches that stopped at the last phi optimization |
| 612 | /// target. It's left in an unspecified state. |
| 613 | /// |
| 614 | /// If this returns None, NewPaused is a vector of searches that terminated |
| 615 | /// at StopWhere. Otherwise, NewPaused is left in an unspecified state. |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 616 | Optional<TerminatedPath> |
Daniel Berlin | d042031 | 2017-04-01 09:01:12 +0000 | [diff] [blame] | 617 | getBlockingAccess(const MemoryAccess *StopWhere, |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 618 | SmallVectorImpl<ListIndex> &PausedSearches, |
| 619 | SmallVectorImpl<ListIndex> &NewPaused, |
| 620 | SmallVectorImpl<TerminatedPath> &Terminated) { |
| 621 | assert(!PausedSearches.empty() && "No searches to continue?"); |
| 622 | |
| 623 | // BFS vs DFS really doesn't make a difference here, so just do a DFS with |
| 624 | // PausedSearches as our stack. |
| 625 | while (!PausedSearches.empty()) { |
| 626 | ListIndex PathIndex = PausedSearches.pop_back_val(); |
| 627 | DefPath &Node = Paths[PathIndex]; |
| 628 | |
| 629 | // If we've already visited this path with this MemoryLocation, we don't |
| 630 | // need to do so again. |
| 631 | // |
| 632 | // NOTE: That we just drop these paths on the ground makes caching |
| 633 | // behavior sporadic. e.g. given a diamond: |
| 634 | // A |
| 635 | // B C |
| 636 | // D |
| 637 | // |
| 638 | // ...If we walk D, B, A, C, we'll only cache the result of phi |
| 639 | // optimization for A, B, and D; C will be skipped because it dies here. |
| 640 | // This arguably isn't the worst thing ever, since: |
| 641 | // - We generally query things in a top-down order, so if we got below D |
| 642 | // without needing cache entries for {C, MemLoc}, then chances are |
| 643 | // that those cache entries would end up ultimately unused. |
| 644 | // - We still cache things for A, so C only needs to walk up a bit. |
| 645 | // If this behavior becomes problematic, we can fix without a ton of extra |
| 646 | // work. |
| 647 | if (!VisitedPhis.insert({Node.Last, Node.Loc}).second) |
| 648 | continue; |
| 649 | |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 650 | const MemoryAccess *SkipStopWhere = nullptr; |
| 651 | if (Query->SkipSelfAccess && Node.Loc == Query->StartingLoc) { |
| 652 | assert(isa<MemoryDef>(Query->OriginalAccess)); |
| 653 | SkipStopWhere = Query->OriginalAccess; |
| 654 | } |
| 655 | |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 656 | UpwardsWalkResult Res = walkToPhiOrClobber(Node, |
| 657 | /*StopAt=*/StopWhere, |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 658 | /*SkipStopAt=*/SkipStopWhere); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 659 | if (Res.IsKnownClobber) { |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 660 | assert(Res.Result != StopWhere && Res.Result != SkipStopWhere); |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 661 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 662 | // If this wasn't a cache hit, we hit a clobber when walking. That's a |
| 663 | // failure. |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 664 | TerminatedPath Term{Res.Result, PathIndex}; |
Daniel Berlin | d7a7ae0 | 2017-04-05 19:01:58 +0000 | [diff] [blame] | 665 | if (!MSSA.dominates(Res.Result, StopWhere)) |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 666 | return Term; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 667 | |
| 668 | // Otherwise, it's a valid thing to potentially optimize to. |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 669 | Terminated.push_back(Term); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 670 | continue; |
| 671 | } |
| 672 | |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 673 | if (Res.Result == StopWhere || Res.Result == SkipStopWhere) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 674 | // We've hit our target. Save this path off for if we want to continue |
Alina Sbirlea | f723020 | 2019-01-07 18:40:27 +0000 | [diff] [blame] | 675 | // walking. If we are in the mode of skipping the OriginalAccess, and |
| 676 | // we've reached back to the OriginalAccess, do not save path, we've |
| 677 | // just looped back to self. |
| 678 | if (Res.Result != SkipStopWhere) |
| 679 | NewPaused.push_back(PathIndex); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 680 | continue; |
| 681 | } |
| 682 | |
| 683 | assert(!MSSA.isLiveOnEntryDef(Res.Result) && "liveOnEntry is a clobber"); |
| 684 | addSearches(cast<MemoryPhi>(Res.Result), PausedSearches, PathIndex); |
| 685 | } |
| 686 | |
| 687 | return None; |
| 688 | } |
| 689 | |
| 690 | template <typename T, typename Walker> |
| 691 | struct generic_def_path_iterator |
| 692 | : public iterator_facade_base<generic_def_path_iterator<T, Walker>, |
| 693 | std::forward_iterator_tag, T *> { |
Hans Wennborg | 5519cb2 | 2019-03-25 09:27:42 +0000 | [diff] [blame] | 694 | generic_def_path_iterator() {} |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 695 | generic_def_path_iterator(Walker *W, ListIndex N) : W(W), N(N) {} |
| 696 | |
| 697 | T &operator*() const { return curNode(); } |
| 698 | |
| 699 | generic_def_path_iterator &operator++() { |
| 700 | N = curNode().Previous; |
| 701 | return *this; |
| 702 | } |
| 703 | |
| 704 | bool operator==(const generic_def_path_iterator &O) const { |
| 705 | if (N.hasValue() != O.N.hasValue()) |
| 706 | return false; |
| 707 | return !N.hasValue() || *N == *O.N; |
| 708 | } |
| 709 | |
| 710 | private: |
| 711 | T &curNode() const { return W->Paths[*N]; } |
| 712 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 713 | Walker *W = nullptr; |
| 714 | Optional<ListIndex> N = None; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
| 717 | using def_path_iterator = generic_def_path_iterator<DefPath, ClobberWalker>; |
| 718 | using const_def_path_iterator = |
| 719 | generic_def_path_iterator<const DefPath, const ClobberWalker>; |
| 720 | |
| 721 | iterator_range<def_path_iterator> def_path(ListIndex From) { |
| 722 | return make_range(def_path_iterator(this, From), def_path_iterator()); |
| 723 | } |
| 724 | |
| 725 | iterator_range<const_def_path_iterator> const_def_path(ListIndex From) const { |
| 726 | return make_range(const_def_path_iterator(this, From), |
| 727 | const_def_path_iterator()); |
| 728 | } |
| 729 | |
| 730 | struct OptznResult { |
| 731 | /// The path that contains our result. |
| 732 | TerminatedPath PrimaryClobber; |
| 733 | /// The paths that we can legally cache back from, but that aren't |
| 734 | /// necessarily the result of the Phi optimization. |
| 735 | SmallVector<TerminatedPath, 4> OtherClobbers; |
| 736 | }; |
| 737 | |
| 738 | ListIndex defPathIndex(const DefPath &N) const { |
| 739 | // The assert looks nicer if we don't need to do &N |
| 740 | const DefPath *NP = &N; |
| 741 | assert(!Paths.empty() && NP >= &Paths.front() && NP <= &Paths.back() && |
| 742 | "Out of bounds DefPath!"); |
| 743 | return NP - &Paths.front(); |
| 744 | } |
| 745 | |
| 746 | /// Try to optimize a phi as best as we can. Returns a SmallVector of Paths |
| 747 | /// that act as legal clobbers. Note that this won't return *all* clobbers. |
| 748 | /// |
| 749 | /// Phi optimization algorithm tl;dr: |
| 750 | /// - Find the earliest def/phi, A, we can optimize to |
| 751 | /// - Find if all paths from the starting memory access ultimately reach A |
| 752 | /// - If not, optimization isn't possible. |
| 753 | /// - Otherwise, walk from A to another clobber or phi, A'. |
| 754 | /// - If A' is a def, we're done. |
| 755 | /// - If A' is a phi, try to optimize it. |
| 756 | /// |
| 757 | /// A path is a series of {MemoryAccess, MemoryLocation} pairs. A path |
| 758 | /// terminates when a MemoryAccess that clobbers said MemoryLocation is found. |
| 759 | OptznResult tryOptimizePhi(MemoryPhi *Phi, MemoryAccess *Start, |
| 760 | const MemoryLocation &Loc) { |
| 761 | assert(Paths.empty() && VisitedPhis.empty() && |
| 762 | "Reset the optimization state."); |
| 763 | |
| 764 | Paths.emplace_back(Loc, Start, Phi, None); |
| 765 | // Stores how many "valid" optimization nodes we had prior to calling |
| 766 | // addSearches/getBlockingAccess. Necessary for caching if we had a blocker. |
| 767 | auto PriorPathsSize = Paths.size(); |
| 768 | |
| 769 | SmallVector<ListIndex, 16> PausedSearches; |
| 770 | SmallVector<ListIndex, 8> NewPaused; |
| 771 | SmallVector<TerminatedPath, 4> TerminatedPaths; |
| 772 | |
| 773 | addSearches(Phi, PausedSearches, 0); |
| 774 | |
| 775 | // Moves the TerminatedPath with the "most dominated" Clobber to the end of |
| 776 | // Paths. |
| 777 | auto MoveDominatedPathToEnd = [&](SmallVectorImpl<TerminatedPath> &Paths) { |
| 778 | assert(!Paths.empty() && "Need a path to move"); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 779 | auto Dom = Paths.begin(); |
| 780 | for (auto I = std::next(Dom), E = Paths.end(); I != E; ++I) |
| 781 | if (!MSSA.dominates(I->Clobber, Dom->Clobber)) |
| 782 | Dom = I; |
| 783 | auto Last = Paths.end() - 1; |
| 784 | if (Last != Dom) |
| 785 | std::iter_swap(Last, Dom); |
| 786 | }; |
| 787 | |
| 788 | MemoryPhi *Current = Phi; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 789 | while (true) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 790 | assert(!MSSA.isLiveOnEntryDef(Current) && |
| 791 | "liveOnEntry wasn't treated as a clobber?"); |
| 792 | |
Daniel Berlin | d042031 | 2017-04-01 09:01:12 +0000 | [diff] [blame] | 793 | const auto *Target = getWalkTarget(Current); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 794 | // If a TerminatedPath doesn't dominate Target, then it wasn't a legal |
| 795 | // optimization for the prior phi. |
| 796 | assert(all_of(TerminatedPaths, [&](const TerminatedPath &P) { |
| 797 | return MSSA.dominates(P.Clobber, Target); |
| 798 | })); |
| 799 | |
| 800 | // FIXME: This is broken, because the Blocker may be reported to be |
| 801 | // liveOnEntry, and we'll happily wait for that to disappear (read: never) |
George Burgess IV | 7f414b9 | 2016-08-22 23:40:01 +0000 | [diff] [blame] | 802 | // For the moment, this is fine, since we do nothing with blocker info. |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 803 | if (Optional<TerminatedPath> Blocker = getBlockingAccess( |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 804 | Target, PausedSearches, NewPaused, TerminatedPaths)) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 805 | |
| 806 | // Find the node we started at. We can't search based on N->Last, since |
| 807 | // we may have gone around a loop with a different MemoryLocation. |
George Burgess IV | 14633b5 | 2016-08-03 01:22:19 +0000 | [diff] [blame] | 808 | auto Iter = find_if(def_path(Blocker->LastNode), [&](const DefPath &N) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 809 | return defPathIndex(N) < PriorPathsSize; |
| 810 | }); |
| 811 | assert(Iter != def_path_iterator()); |
| 812 | |
| 813 | DefPath &CurNode = *Iter; |
| 814 | assert(CurNode.Last == Current); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 815 | |
| 816 | // Two things: |
| 817 | // A. We can't reliably cache all of NewPaused back. Consider a case |
| 818 | // where we have two paths in NewPaused; one of which can't optimize |
| 819 | // above this phi, whereas the other can. If we cache the second path |
| 820 | // back, we'll end up with suboptimal cache entries. We can handle |
| 821 | // cases like this a bit better when we either try to find all |
| 822 | // clobbers that block phi optimization, or when our cache starts |
| 823 | // supporting unfinished searches. |
| 824 | // B. We can't reliably cache TerminatedPaths back here without doing |
| 825 | // extra checks; consider a case like: |
| 826 | // T |
| 827 | // / \ |
| 828 | // D C |
| 829 | // \ / |
| 830 | // S |
| 831 | // Where T is our target, C is a node with a clobber on it, D is a |
| 832 | // diamond (with a clobber *only* on the left or right node, N), and |
| 833 | // S is our start. Say we walk to D, through the node opposite N |
| 834 | // (read: ignoring the clobber), and see a cache entry in the top |
| 835 | // node of D. That cache entry gets put into TerminatedPaths. We then |
| 836 | // walk up to C (N is later in our worklist), find the clobber, and |
| 837 | // quit. If we append TerminatedPaths to OtherClobbers, we'll cache |
| 838 | // the bottom part of D to the cached clobber, ignoring the clobber |
| 839 | // in N. Again, this problem goes away if we start tracking all |
| 840 | // blockers for a given phi optimization. |
| 841 | TerminatedPath Result{CurNode.Last, defPathIndex(CurNode)}; |
| 842 | return {Result, {}}; |
| 843 | } |
| 844 | |
| 845 | // If there's nothing left to search, then all paths led to valid clobbers |
| 846 | // that we got from our cache; pick the nearest to the start, and allow |
| 847 | // the rest to be cached back. |
| 848 | if (NewPaused.empty()) { |
| 849 | MoveDominatedPathToEnd(TerminatedPaths); |
| 850 | TerminatedPath Result = TerminatedPaths.pop_back_val(); |
| 851 | return {Result, std::move(TerminatedPaths)}; |
| 852 | } |
| 853 | |
| 854 | MemoryAccess *DefChainEnd = nullptr; |
| 855 | SmallVector<TerminatedPath, 4> Clobbers; |
| 856 | for (ListIndex Paused : NewPaused) { |
| 857 | UpwardsWalkResult WR = walkToPhiOrClobber(Paths[Paused]); |
| 858 | if (WR.IsKnownClobber) |
| 859 | Clobbers.push_back({WR.Result, Paused}); |
| 860 | else |
| 861 | // Micro-opt: If we hit the end of the chain, save it. |
| 862 | DefChainEnd = WR.Result; |
| 863 | } |
| 864 | |
| 865 | if (!TerminatedPaths.empty()) { |
| 866 | // If we couldn't find the dominating phi/liveOnEntry in the above loop, |
| 867 | // do it now. |
| 868 | if (!DefChainEnd) |
Daniel Berlin | d042031 | 2017-04-01 09:01:12 +0000 | [diff] [blame] | 869 | for (auto *MA : def_chain(const_cast<MemoryAccess *>(Target))) |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 870 | DefChainEnd = MA; |
| 871 | |
| 872 | // If any of the terminated paths don't dominate the phi we'll try to |
| 873 | // optimize, we need to figure out what they are and quit. |
| 874 | const BasicBlock *ChainBB = DefChainEnd->getBlock(); |
| 875 | for (const TerminatedPath &TP : TerminatedPaths) { |
| 876 | // Because we know that DefChainEnd is as "high" as we can go, we |
| 877 | // don't need local dominance checks; BB dominance is sufficient. |
| 878 | if (DT.dominates(ChainBB, TP.Clobber->getBlock())) |
| 879 | Clobbers.push_back(TP); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // If we have clobbers in the def chain, find the one closest to Current |
| 884 | // and quit. |
| 885 | if (!Clobbers.empty()) { |
| 886 | MoveDominatedPathToEnd(Clobbers); |
| 887 | TerminatedPath Result = Clobbers.pop_back_val(); |
| 888 | return {Result, std::move(Clobbers)}; |
| 889 | } |
| 890 | |
| 891 | assert(all_of(NewPaused, |
| 892 | [&](ListIndex I) { return Paths[I].Last == DefChainEnd; })); |
| 893 | |
| 894 | // Because liveOnEntry is a clobber, this must be a phi. |
| 895 | auto *DefChainPhi = cast<MemoryPhi>(DefChainEnd); |
| 896 | |
| 897 | PriorPathsSize = Paths.size(); |
| 898 | PausedSearches.clear(); |
| 899 | for (ListIndex I : NewPaused) |
| 900 | addSearches(DefChainPhi, PausedSearches, I); |
| 901 | NewPaused.clear(); |
| 902 | |
| 903 | Current = DefChainPhi; |
| 904 | } |
| 905 | } |
| 906 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 907 | void verifyOptResult(const OptznResult &R) const { |
| 908 | assert(all_of(R.OtherClobbers, [&](const TerminatedPath &P) { |
| 909 | return MSSA.dominates(P.Clobber, R.PrimaryClobber.Clobber); |
| 910 | })); |
| 911 | } |
| 912 | |
| 913 | void resetPhiOptznState() { |
| 914 | Paths.clear(); |
| 915 | VisitedPhis.clear(); |
| 916 | } |
| 917 | |
| 918 | public: |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 919 | ClobberWalker(const MemorySSA &MSSA, AliasAnalysisType &AA, DominatorTree &DT) |
Daniel Berlin | d7a7ae0 | 2017-04-05 19:01:58 +0000 | [diff] [blame] | 920 | : MSSA(MSSA), AA(AA), DT(DT) {} |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 921 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 922 | AliasAnalysisType *getAA() { return &AA; } |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 923 | /// Finds the nearest clobber for the given query, optimizing phis if |
| 924 | /// possible. |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 925 | MemoryAccess *findClobber(MemoryAccess *Start, UpwardsMemoryQuery &Q, |
| 926 | unsigned &UpWalkLimit) { |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 927 | Query = &Q; |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 928 | UpwardWalkLimit = &UpWalkLimit; |
| 929 | // Starting limit must be > 0. |
| 930 | if (!UpWalkLimit) |
| 931 | UpWalkLimit++; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 932 | |
| 933 | MemoryAccess *Current = Start; |
| 934 | // This walker pretends uses don't exist. If we're handed one, silently grab |
| 935 | // its def. (This has the nice side-effect of ensuring we never cache uses) |
| 936 | if (auto *MU = dyn_cast<MemoryUse>(Start)) |
| 937 | Current = MU->getDefiningAccess(); |
| 938 | |
| 939 | DefPath FirstDesc(Q.StartingLoc, Current, Current, None); |
| 940 | // Fast path for the overly-common case (no crazy phi optimization |
| 941 | // necessary) |
| 942 | UpwardsWalkResult WalkResult = walkToPhiOrClobber(FirstDesc); |
George Burgess IV | 93ea19b | 2016-07-24 07:03:49 +0000 | [diff] [blame] | 943 | MemoryAccess *Result; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 944 | if (WalkResult.IsKnownClobber) { |
George Burgess IV | 93ea19b | 2016-07-24 07:03:49 +0000 | [diff] [blame] | 945 | Result = WalkResult.Result; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 946 | Q.AR = WalkResult.AR; |
George Burgess IV | 93ea19b | 2016-07-24 07:03:49 +0000 | [diff] [blame] | 947 | } else { |
| 948 | OptznResult OptRes = tryOptimizePhi(cast<MemoryPhi>(FirstDesc.Last), |
| 949 | Current, Q.StartingLoc); |
| 950 | verifyOptResult(OptRes); |
George Burgess IV | 93ea19b | 2016-07-24 07:03:49 +0000 | [diff] [blame] | 951 | resetPhiOptznState(); |
| 952 | Result = OptRes.PrimaryClobber.Clobber; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 953 | } |
| 954 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 955 | #ifdef EXPENSIVE_CHECKS |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 956 | if (!Q.SkipSelfAccess && *UpwardWalkLimit > 0) |
Alina Sbirlea | e41f4b3 | 2019-01-10 21:47:15 +0000 | [diff] [blame] | 957 | checkClobberSanity(Current, Result, Q.StartingLoc, MSSA, Q, AA); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 958 | #endif |
George Burgess IV | 93ea19b | 2016-07-24 07:03:49 +0000 | [diff] [blame] | 959 | return Result; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 960 | } |
| 961 | }; |
| 962 | |
| 963 | struct RenamePassData { |
| 964 | DomTreeNode *DTN; |
| 965 | DomTreeNode::const_iterator ChildIt; |
| 966 | MemoryAccess *IncomingVal; |
| 967 | |
| 968 | RenamePassData(DomTreeNode *D, DomTreeNode::const_iterator It, |
| 969 | MemoryAccess *M) |
| 970 | : DTN(D), ChildIt(It), IncomingVal(M) {} |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 971 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 972 | void swap(RenamePassData &RHS) { |
| 973 | std::swap(DTN, RHS.DTN); |
| 974 | std::swap(ChildIt, RHS.ChildIt); |
| 975 | std::swap(IncomingVal, RHS.IncomingVal); |
| 976 | } |
| 977 | }; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 978 | |
| 979 | } // end anonymous namespace |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 980 | |
| 981 | namespace llvm { |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 982 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 983 | template <class AliasAnalysisType> class MemorySSA::ClobberWalkerBase { |
| 984 | ClobberWalker<AliasAnalysisType> Walker; |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 985 | MemorySSA *MSSA; |
| 986 | |
| 987 | public: |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 988 | ClobberWalkerBase(MemorySSA *M, AliasAnalysisType *A, DominatorTree *D) |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 989 | : Walker(*M, *A, *D), MSSA(M) {} |
| 990 | |
| 991 | MemoryAccess *getClobberingMemoryAccessBase(MemoryAccess *, |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 992 | const MemoryLocation &, |
| 993 | unsigned &); |
| 994 | // Third argument (bool), defines whether the clobber search should skip the |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 995 | // original queried access. If true, there will be a follow-up query searching |
| 996 | // for a clobber access past "self". Note that the Optimized access is not |
| 997 | // updated if a new clobber is found by this SkipSelf search. If this |
| 998 | // additional query becomes heavily used we may decide to cache the result. |
| 999 | // Walker instantiations will decide how to set the SkipSelf bool. |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1000 | MemoryAccess *getClobberingMemoryAccessBase(MemoryAccess *, unsigned &, bool); |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 1001 | }; |
| 1002 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1003 | /// A MemorySSAWalker that does AA walks to disambiguate accesses. It no |
George Burgess IV | 45f263d | 2018-05-26 02:28:55 +0000 | [diff] [blame] | 1004 | /// longer does caching on its own, but the name has been retained for the |
| 1005 | /// moment. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1006 | template <class AliasAnalysisType> |
George Burgess IV | fd1f2f8 | 2016-06-24 21:02:12 +0000 | [diff] [blame] | 1007 | class MemorySSA::CachingWalker final : public MemorySSAWalker { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1008 | ClobberWalkerBase<AliasAnalysisType> *Walker; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 1009 | |
George Burgess IV | fd1f2f8 | 2016-06-24 21:02:12 +0000 | [diff] [blame] | 1010 | public: |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1011 | CachingWalker(MemorySSA *M, ClobberWalkerBase<AliasAnalysisType> *W) |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 1012 | : MemorySSAWalker(M), Walker(W) {} |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1013 | ~CachingWalker() override = default; |
George Burgess IV | fd1f2f8 | 2016-06-24 21:02:12 +0000 | [diff] [blame] | 1014 | |
George Burgess IV | 400ae40 | 2016-07-20 19:51:34 +0000 | [diff] [blame] | 1015 | using MemorySSAWalker::getClobberingMemoryAccess; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1016 | |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1017 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, unsigned &UWL) { |
| 1018 | return Walker->getClobberingMemoryAccessBase(MA, UWL, false); |
| 1019 | } |
| 1020 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, |
| 1021 | const MemoryLocation &Loc, |
| 1022 | unsigned &UWL) { |
| 1023 | return Walker->getClobberingMemoryAccessBase(MA, Loc, UWL); |
| 1024 | } |
| 1025 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1026 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA) override { |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1027 | unsigned UpwardWalkLimit = MaxCheckLimit; |
| 1028 | return getClobberingMemoryAccess(MA, UpwardWalkLimit); |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1029 | } |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 1030 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1031 | const MemoryLocation &Loc) override { |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1032 | unsigned UpwardWalkLimit = MaxCheckLimit; |
| 1033 | return getClobberingMemoryAccess(MA, Loc, UpwardWalkLimit); |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1034 | } |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 1035 | |
| 1036 | void invalidateInfo(MemoryAccess *MA) override { |
| 1037 | if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) |
| 1038 | MUD->resetOptimized(); |
| 1039 | } |
George Burgess IV | fd1f2f8 | 2016-06-24 21:02:12 +0000 | [diff] [blame] | 1040 | }; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1041 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1042 | template <class AliasAnalysisType> |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1043 | class MemorySSA::SkipSelfWalker final : public MemorySSAWalker { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1044 | ClobberWalkerBase<AliasAnalysisType> *Walker; |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1045 | |
| 1046 | public: |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1047 | SkipSelfWalker(MemorySSA *M, ClobberWalkerBase<AliasAnalysisType> *W) |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1048 | : MemorySSAWalker(M), Walker(W) {} |
| 1049 | ~SkipSelfWalker() override = default; |
| 1050 | |
| 1051 | using MemorySSAWalker::getClobberingMemoryAccess; |
| 1052 | |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1053 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, unsigned &UWL) { |
| 1054 | return Walker->getClobberingMemoryAccessBase(MA, UWL, true); |
| 1055 | } |
| 1056 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, |
| 1057 | const MemoryLocation &Loc, |
| 1058 | unsigned &UWL) { |
| 1059 | return Walker->getClobberingMemoryAccessBase(MA, Loc, UWL); |
| 1060 | } |
| 1061 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1062 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA) override { |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1063 | unsigned UpwardWalkLimit = MaxCheckLimit; |
| 1064 | return getClobberingMemoryAccess(MA, UpwardWalkLimit); |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1065 | } |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1066 | MemoryAccess *getClobberingMemoryAccess(MemoryAccess *MA, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1067 | const MemoryLocation &Loc) override { |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1068 | unsigned UpwardWalkLimit = MaxCheckLimit; |
| 1069 | return getClobberingMemoryAccess(MA, Loc, UpwardWalkLimit); |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1070 | } |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1071 | |
| 1072 | void invalidateInfo(MemoryAccess *MA) override { |
| 1073 | if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) |
| 1074 | MUD->resetOptimized(); |
| 1075 | } |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1076 | }; |
| 1077 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1078 | } // end namespace llvm |
| 1079 | |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1080 | void MemorySSA::renameSuccessorPhis(BasicBlock *BB, MemoryAccess *IncomingVal, |
| 1081 | bool RenameAllUses) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1082 | // Pass through values to our successors |
| 1083 | for (const BasicBlock *S : successors(BB)) { |
| 1084 | auto It = PerBlockAccesses.find(S); |
| 1085 | // Rename the phi nodes in our successor block |
| 1086 | if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front())) |
| 1087 | continue; |
Daniel Berlin | ada263d | 2016-06-20 20:21:33 +0000 | [diff] [blame] | 1088 | AccessList *Accesses = It->second.get(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1089 | auto *Phi = cast<MemoryPhi>(&Accesses->front()); |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1090 | if (RenameAllUses) { |
| 1091 | int PhiIndex = Phi->getBasicBlockIndex(BB); |
| 1092 | assert(PhiIndex != -1 && "Incomplete phi during partial rename"); |
| 1093 | Phi->setIncomingValue(PhiIndex, IncomingVal); |
| 1094 | } else |
| 1095 | Phi->addIncoming(IncomingVal, BB); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1096 | } |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1097 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1098 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1099 | /// Rename a single basic block into MemorySSA form. |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1100 | /// Uses the standard SSA renaming algorithm. |
| 1101 | /// \returns The new incoming value. |
| 1102 | MemoryAccess *MemorySSA::renameBlock(BasicBlock *BB, MemoryAccess *IncomingVal, |
| 1103 | bool RenameAllUses) { |
| 1104 | auto It = PerBlockAccesses.find(BB); |
| 1105 | // Skip most processing if the list is empty. |
| 1106 | if (It != PerBlockAccesses.end()) { |
| 1107 | AccessList *Accesses = It->second.get(); |
| 1108 | for (MemoryAccess &L : *Accesses) { |
| 1109 | if (MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&L)) { |
| 1110 | if (MUD->getDefiningAccess() == nullptr || RenameAllUses) |
| 1111 | MUD->setDefiningAccess(IncomingVal); |
| 1112 | if (isa<MemoryDef>(&L)) |
| 1113 | IncomingVal = &L; |
| 1114 | } else { |
| 1115 | IncomingVal = &L; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1119 | return IncomingVal; |
| 1120 | } |
| 1121 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1122 | /// This is the standard SSA renaming algorithm. |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1123 | /// |
| 1124 | /// We walk the dominator tree in preorder, renaming accesses, and then filling |
| 1125 | /// in phi nodes in our successors. |
| 1126 | void MemorySSA::renamePass(DomTreeNode *Root, MemoryAccess *IncomingVal, |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1127 | SmallPtrSetImpl<BasicBlock *> &Visited, |
| 1128 | bool SkipVisited, bool RenameAllUses) { |
Alina Sbirlea | 0363c3b | 2019-05-02 23:41:58 +0000 | [diff] [blame] | 1129 | assert(Root && "Trying to rename accesses in an unreachable block"); |
| 1130 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1131 | SmallVector<RenamePassData, 32> WorkStack; |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1132 | // Skip everything if we already renamed this block and we are skipping. |
| 1133 | // Note: You can't sink this into the if, because we need it to occur |
| 1134 | // regardless of whether we skip blocks or not. |
| 1135 | bool AlreadyVisited = !Visited.insert(Root->getBlock()).second; |
| 1136 | if (SkipVisited && AlreadyVisited) |
| 1137 | return; |
| 1138 | |
| 1139 | IncomingVal = renameBlock(Root->getBlock(), IncomingVal, RenameAllUses); |
| 1140 | renameSuccessorPhis(Root->getBlock(), IncomingVal, RenameAllUses); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1141 | WorkStack.push_back({Root, Root->begin(), IncomingVal}); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1142 | |
| 1143 | while (!WorkStack.empty()) { |
| 1144 | DomTreeNode *Node = WorkStack.back().DTN; |
| 1145 | DomTreeNode::const_iterator ChildIt = WorkStack.back().ChildIt; |
| 1146 | IncomingVal = WorkStack.back().IncomingVal; |
| 1147 | |
| 1148 | if (ChildIt == Node->end()) { |
| 1149 | WorkStack.pop_back(); |
| 1150 | } else { |
| 1151 | DomTreeNode *Child = *ChildIt; |
| 1152 | ++WorkStack.back().ChildIt; |
| 1153 | BasicBlock *BB = Child->getBlock(); |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1154 | // Note: You can't sink this into the if, because we need it to occur |
| 1155 | // regardless of whether we skip blocks or not. |
| 1156 | AlreadyVisited = !Visited.insert(BB).second; |
| 1157 | if (SkipVisited && AlreadyVisited) { |
| 1158 | // We already visited this during our renaming, which can happen when |
| 1159 | // being asked to rename multiple blocks. Figure out the incoming val, |
| 1160 | // which is the last def. |
| 1161 | // Incoming value can only change if there is a block def, and in that |
| 1162 | // case, it's the last block def in the list. |
| 1163 | if (auto *BlockDefs = getWritableBlockDefs(BB)) |
| 1164 | IncomingVal = &*BlockDefs->rbegin(); |
| 1165 | } else |
| 1166 | IncomingVal = renameBlock(BB, IncomingVal, RenameAllUses); |
| 1167 | renameSuccessorPhis(BB, IncomingVal, RenameAllUses); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1168 | WorkStack.push_back({Child, Child->begin(), IncomingVal}); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1173 | /// This handles unreachable block accesses by deleting phi nodes in |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1174 | /// unreachable blocks, and marking all other unreachable MemoryAccess's as |
| 1175 | /// being uses of the live on entry definition. |
| 1176 | void MemorySSA::markUnreachableAsLiveOnEntry(BasicBlock *BB) { |
| 1177 | assert(!DT->isReachableFromEntry(BB) && |
| 1178 | "Reachable block found while handling unreachable blocks"); |
| 1179 | |
Daniel Berlin | fc7e651 | 2016-07-06 05:32:05 +0000 | [diff] [blame] | 1180 | // Make sure phi nodes in our reachable successors end up with a |
| 1181 | // LiveOnEntryDef for our incoming edge, even though our block is forward |
| 1182 | // unreachable. We could just disconnect these blocks from the CFG fully, |
| 1183 | // but we do not right now. |
| 1184 | for (const BasicBlock *S : successors(BB)) { |
| 1185 | if (!DT->isReachableFromEntry(S)) |
| 1186 | continue; |
| 1187 | auto It = PerBlockAccesses.find(S); |
| 1188 | // Rename the phi nodes in our successor block |
| 1189 | if (It == PerBlockAccesses.end() || !isa<MemoryPhi>(It->second->front())) |
| 1190 | continue; |
| 1191 | AccessList *Accesses = It->second.get(); |
| 1192 | auto *Phi = cast<MemoryPhi>(&Accesses->front()); |
| 1193 | Phi->addIncoming(LiveOnEntryDef.get(), BB); |
| 1194 | } |
| 1195 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1196 | auto It = PerBlockAccesses.find(BB); |
| 1197 | if (It == PerBlockAccesses.end()) |
| 1198 | return; |
| 1199 | |
| 1200 | auto &Accesses = It->second; |
| 1201 | for (auto AI = Accesses->begin(), AE = Accesses->end(); AI != AE;) { |
| 1202 | auto Next = std::next(AI); |
| 1203 | // If we have a phi, just remove it. We are going to replace all |
| 1204 | // users with live on entry. |
| 1205 | if (auto *UseOrDef = dyn_cast<MemoryUseOrDef>(AI)) |
| 1206 | UseOrDef->setDefiningAccess(LiveOnEntryDef.get()); |
| 1207 | else |
| 1208 | Accesses->erase(AI); |
| 1209 | AI = Next; |
| 1210 | } |
| 1211 | } |
| 1212 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 1213 | MemorySSA::MemorySSA(Function &Func, AliasAnalysis *AA, DominatorTree *DT) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1214 | : AA(nullptr), DT(DT), F(Func), LiveOnEntryDef(nullptr), Walker(nullptr), |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1215 | SkipWalker(nullptr), NextID(0) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1216 | // Build MemorySSA using a batch alias analysis. This reuses the internal |
| 1217 | // state that AA collects during an alias()/getModRefInfo() call. This is |
| 1218 | // safe because there are no CFG changes while building MemorySSA and can |
| 1219 | // significantly reduce the time spent by the compiler in AA, because we will |
| 1220 | // make queries about all the instructions in the Function. |
| 1221 | BatchAAResults BatchAA(*AA); |
| 1222 | buildMemorySSA(BatchAA); |
| 1223 | // Intentionally leave AA to nullptr while building so we don't accidently |
| 1224 | // use non-batch AliasAnalysis. |
| 1225 | this->AA = AA; |
| 1226 | // Also create the walker here. |
| 1227 | getWalker(); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1230 | MemorySSA::~MemorySSA() { |
| 1231 | // Drop all our references |
| 1232 | for (const auto &Pair : PerBlockAccesses) |
| 1233 | for (MemoryAccess &MA : *Pair.second) |
| 1234 | MA.dropAllReferences(); |
| 1235 | } |
| 1236 | |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1237 | MemorySSA::AccessList *MemorySSA::getOrCreateAccessList(const BasicBlock *BB) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1238 | auto Res = PerBlockAccesses.insert(std::make_pair(BB, nullptr)); |
| 1239 | |
| 1240 | if (Res.second) |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1241 | Res.first->second = llvm::make_unique<AccessList>(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1242 | return Res.first->second.get(); |
| 1243 | } |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1244 | |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1245 | MemorySSA::DefsList *MemorySSA::getOrCreateDefsList(const BasicBlock *BB) { |
| 1246 | auto Res = PerBlockDefs.insert(std::make_pair(BB, nullptr)); |
| 1247 | |
| 1248 | if (Res.second) |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1249 | Res.first->second = llvm::make_unique<DefsList>(); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1250 | return Res.first->second.get(); |
| 1251 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1252 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1253 | namespace llvm { |
| 1254 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1255 | /// This class is a batch walker of all MemoryUse's in the program, and points |
| 1256 | /// their defining access at the thing that actually clobbers them. Because it |
| 1257 | /// is a batch walker that touches everything, it does not operate like the |
| 1258 | /// other walkers. This walker is basically performing a top-down SSA renaming |
| 1259 | /// pass, where the version stack is used as the cache. This enables it to be |
| 1260 | /// significantly more time and memory efficient than using the regular walker, |
| 1261 | /// which is walking bottom-up. |
| 1262 | class MemorySSA::OptimizeUses { |
| 1263 | public: |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1264 | OptimizeUses(MemorySSA *MSSA, CachingWalker<BatchAAResults> *Walker, |
| 1265 | BatchAAResults *BAA, DominatorTree *DT) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1266 | : MSSA(MSSA), Walker(Walker), AA(BAA), DT(DT) {} |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1267 | |
| 1268 | void optimizeUses(); |
| 1269 | |
| 1270 | private: |
| 1271 | /// This represents where a given memorylocation is in the stack. |
| 1272 | struct MemlocStackInfo { |
| 1273 | // This essentially is keeping track of versions of the stack. Whenever |
| 1274 | // the stack changes due to pushes or pops, these versions increase. |
| 1275 | unsigned long StackEpoch; |
| 1276 | unsigned long PopEpoch; |
| 1277 | // This is the lower bound of places on the stack to check. It is equal to |
| 1278 | // the place the last stack walk ended. |
| 1279 | // Note: Correctness depends on this being initialized to 0, which densemap |
| 1280 | // does |
| 1281 | unsigned long LowerBound; |
Daniel Berlin | 4b4c722 | 2016-08-08 04:44:53 +0000 | [diff] [blame] | 1282 | const BasicBlock *LowerBoundBlock; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1283 | // This is where the last walk for this memory location ended. |
| 1284 | unsigned long LastKill; |
| 1285 | bool LastKillValid; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1286 | Optional<AliasResult> AR; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1287 | }; |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1288 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1289 | void optimizeUsesInBlock(const BasicBlock *, unsigned long &, unsigned long &, |
| 1290 | SmallVectorImpl<MemoryAccess *> &, |
| 1291 | DenseMap<MemoryLocOrCall, MemlocStackInfo> &); |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1292 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1293 | MemorySSA *MSSA; |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1294 | CachingWalker<BatchAAResults> *Walker; |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1295 | BatchAAResults *AA; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1296 | DominatorTree *DT; |
| 1297 | }; |
| 1298 | |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1299 | } // end namespace llvm |
| 1300 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1301 | /// Optimize the uses in a given block This is basically the SSA renaming |
| 1302 | /// algorithm, with one caveat: We are able to use a single stack for all |
| 1303 | /// MemoryUses. This is because the set of *possible* reaching MemoryDefs is |
| 1304 | /// the same for every MemoryUse. The *actual* clobbering MemoryDef is just |
| 1305 | /// going to be some position in that stack of possible ones. |
| 1306 | /// |
| 1307 | /// We track the stack positions that each MemoryLocation needs |
| 1308 | /// to check, and last ended at. This is because we only want to check the |
| 1309 | /// things that changed since last time. The same MemoryLocation should |
| 1310 | /// get clobbered by the same store (getModRefInfo does not use invariantness or |
| 1311 | /// things like this, and if they start, we can modify MemoryLocOrCall to |
| 1312 | /// include relevant data) |
| 1313 | void MemorySSA::OptimizeUses::optimizeUsesInBlock( |
| 1314 | const BasicBlock *BB, unsigned long &StackEpoch, unsigned long &PopEpoch, |
| 1315 | SmallVectorImpl<MemoryAccess *> &VersionStack, |
| 1316 | DenseMap<MemoryLocOrCall, MemlocStackInfo> &LocStackInfo) { |
| 1317 | |
| 1318 | /// If no accesses, nothing to do. |
| 1319 | MemorySSA::AccessList *Accesses = MSSA->getWritableBlockAccesses(BB); |
| 1320 | if (Accesses == nullptr) |
| 1321 | return; |
| 1322 | |
| 1323 | // Pop everything that doesn't dominate the current block off the stack, |
| 1324 | // increment the PopEpoch to account for this. |
Piotr Padlewski | cc5868c1 | 2017-02-18 20:34:36 +0000 | [diff] [blame] | 1325 | while (true) { |
| 1326 | assert( |
| 1327 | !VersionStack.empty() && |
| 1328 | "Version stack should have liveOnEntry sentinel dominating everything"); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1329 | BasicBlock *BackBlock = VersionStack.back()->getBlock(); |
| 1330 | if (DT->dominates(BackBlock, BB)) |
| 1331 | break; |
| 1332 | while (VersionStack.back()->getBlock() == BackBlock) |
| 1333 | VersionStack.pop_back(); |
| 1334 | ++PopEpoch; |
| 1335 | } |
Piotr Padlewski | cc5868c1 | 2017-02-18 20:34:36 +0000 | [diff] [blame] | 1336 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1337 | for (MemoryAccess &MA : *Accesses) { |
| 1338 | auto *MU = dyn_cast<MemoryUse>(&MA); |
| 1339 | if (!MU) { |
| 1340 | VersionStack.push_back(&MA); |
| 1341 | ++StackEpoch; |
| 1342 | continue; |
| 1343 | } |
| 1344 | |
George Burgess IV | 024f3d2 | 2016-08-03 19:57:02 +0000 | [diff] [blame] | 1345 | if (isUseTriviallyOptimizableToLiveOnEntry(*AA, MU->getMemoryInst())) { |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1346 | MU->setDefiningAccess(MSSA->getLiveOnEntryDef(), true, None); |
George Burgess IV | 024f3d2 | 2016-08-03 19:57:02 +0000 | [diff] [blame] | 1347 | continue; |
| 1348 | } |
| 1349 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1350 | MemoryLocOrCall UseMLOC(MU); |
| 1351 | auto &LocInfo = LocStackInfo[UseMLOC]; |
Daniel Berlin | 26fcea9 | 2016-08-02 20:02:21 +0000 | [diff] [blame] | 1352 | // If the pop epoch changed, it means we've removed stuff from top of |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1353 | // stack due to changing blocks. We may have to reset the lower bound or |
| 1354 | // last kill info. |
| 1355 | if (LocInfo.PopEpoch != PopEpoch) { |
| 1356 | LocInfo.PopEpoch = PopEpoch; |
| 1357 | LocInfo.StackEpoch = StackEpoch; |
Daniel Berlin | 4b4c722 | 2016-08-08 04:44:53 +0000 | [diff] [blame] | 1358 | // If the lower bound was in something that no longer dominates us, we |
| 1359 | // have to reset it. |
| 1360 | // We can't simply track stack size, because the stack may have had |
| 1361 | // pushes/pops in the meantime. |
| 1362 | // XXX: This is non-optimal, but only is slower cases with heavily |
| 1363 | // branching dominator trees. To get the optimal number of queries would |
| 1364 | // be to make lowerbound and lastkill a per-loc stack, and pop it until |
| 1365 | // the top of that stack dominates us. This does not seem worth it ATM. |
| 1366 | // A much cheaper optimization would be to always explore the deepest |
| 1367 | // branch of the dominator tree first. This will guarantee this resets on |
| 1368 | // the smallest set of blocks. |
| 1369 | if (LocInfo.LowerBoundBlock && LocInfo.LowerBoundBlock != BB && |
Daniel Berlin | 1e98c04 | 2016-09-26 17:22:54 +0000 | [diff] [blame] | 1370 | !DT->dominates(LocInfo.LowerBoundBlock, BB)) { |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1371 | // Reset the lower bound of things to check. |
| 1372 | // TODO: Some day we should be able to reset to last kill, rather than |
| 1373 | // 0. |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1374 | LocInfo.LowerBound = 0; |
Daniel Berlin | 4b4c722 | 2016-08-08 04:44:53 +0000 | [diff] [blame] | 1375 | LocInfo.LowerBoundBlock = VersionStack[0]->getBlock(); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1376 | LocInfo.LastKillValid = false; |
| 1377 | } |
| 1378 | } else if (LocInfo.StackEpoch != StackEpoch) { |
| 1379 | // If all that has changed is the StackEpoch, we only have to check the |
| 1380 | // new things on the stack, because we've checked everything before. In |
| 1381 | // this case, the lower bound of things to check remains the same. |
| 1382 | LocInfo.PopEpoch = PopEpoch; |
| 1383 | LocInfo.StackEpoch = StackEpoch; |
| 1384 | } |
| 1385 | if (!LocInfo.LastKillValid) { |
| 1386 | LocInfo.LastKill = VersionStack.size() - 1; |
| 1387 | LocInfo.LastKillValid = true; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1388 | LocInfo.AR = MayAlias; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | // At this point, we should have corrected last kill and LowerBound to be |
| 1392 | // in bounds. |
| 1393 | assert(LocInfo.LowerBound < VersionStack.size() && |
| 1394 | "Lower bound out of range"); |
| 1395 | assert(LocInfo.LastKill < VersionStack.size() && |
| 1396 | "Last kill info out of range"); |
| 1397 | // In any case, the new upper bound is the top of the stack. |
| 1398 | unsigned long UpperBound = VersionStack.size() - 1; |
| 1399 | |
| 1400 | if (UpperBound - LocInfo.LowerBound > MaxCheckLimit) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1401 | LLVM_DEBUG(dbgs() << "MemorySSA skipping optimization of " << *MU << " (" |
| 1402 | << *(MU->getMemoryInst()) << ")" |
| 1403 | << " because there are " |
| 1404 | << UpperBound - LocInfo.LowerBound |
| 1405 | << " stores to disambiguate\n"); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1406 | // Because we did not walk, LastKill is no longer valid, as this may |
| 1407 | // have been a kill. |
| 1408 | LocInfo.LastKillValid = false; |
| 1409 | continue; |
| 1410 | } |
| 1411 | bool FoundClobberResult = false; |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1412 | unsigned UpwardWalkLimit = MaxCheckLimit; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1413 | while (UpperBound > LocInfo.LowerBound) { |
| 1414 | if (isa<MemoryPhi>(VersionStack[UpperBound])) { |
| 1415 | // For phis, use the walker, see where we ended up, go there |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 1416 | MemoryAccess *Result = |
| 1417 | Walker->getClobberingMemoryAccess(MU, UpwardWalkLimit); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1418 | // We are guaranteed to find it or something is wrong |
| 1419 | while (VersionStack[UpperBound] != Result) { |
| 1420 | assert(UpperBound != 0); |
| 1421 | --UpperBound; |
| 1422 | } |
| 1423 | FoundClobberResult = true; |
| 1424 | break; |
| 1425 | } |
| 1426 | |
| 1427 | MemoryDef *MD = cast<MemoryDef>(VersionStack[UpperBound]); |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 1428 | // If the lifetime of the pointer ends at this instruction, it's live on |
| 1429 | // entry. |
| 1430 | if (!UseMLOC.IsCall && lifetimeEndsAt(MD, UseMLOC.getLoc(), *AA)) { |
| 1431 | // Reset UpperBound to liveOnEntryDef's place in the stack |
| 1432 | UpperBound = 0; |
| 1433 | FoundClobberResult = true; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1434 | LocInfo.AR = MustAlias; |
Daniel Berlin | df10119 | 2016-08-03 00:01:46 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | } |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1437 | ClobberAlias CA = instructionClobbersQuery(MD, MU, UseMLOC, *AA); |
| 1438 | if (CA.IsClobber) { |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1439 | FoundClobberResult = true; |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1440 | LocInfo.AR = CA.AR; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | --UpperBound; |
| 1444 | } |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Note: Phis always have AliasResult AR set to MayAlias ATM. |
| 1447 | |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1448 | // At the end of this loop, UpperBound is either a clobber, or lower bound |
| 1449 | // PHI walking may cause it to be < LowerBound, and in fact, < LastKill. |
| 1450 | if (FoundClobberResult || UpperBound < LocInfo.LastKill) { |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1451 | // We were last killed now by where we got to |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1452 | if (MSSA->isLiveOnEntryDef(VersionStack[UpperBound])) |
| 1453 | LocInfo.AR = None; |
| 1454 | MU->setDefiningAccess(VersionStack[UpperBound], true, LocInfo.AR); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1455 | LocInfo.LastKill = UpperBound; |
| 1456 | } else { |
| 1457 | // Otherwise, we checked all the new ones, and now we know we can get to |
| 1458 | // LastKill. |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 1459 | MU->setDefiningAccess(VersionStack[LocInfo.LastKill], true, LocInfo.AR); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1460 | } |
| 1461 | LocInfo.LowerBound = VersionStack.size() - 1; |
Daniel Berlin | 4b4c722 | 2016-08-08 04:44:53 +0000 | [diff] [blame] | 1462 | LocInfo.LowerBoundBlock = BB; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | /// Optimize uses to point to their actual clobbering definitions. |
| 1467 | void MemorySSA::OptimizeUses::optimizeUses() { |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1468 | SmallVector<MemoryAccess *, 16> VersionStack; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1469 | DenseMap<MemoryLocOrCall, MemlocStackInfo> LocStackInfo; |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1470 | VersionStack.push_back(MSSA->getLiveOnEntryDef()); |
| 1471 | |
| 1472 | unsigned long StackEpoch = 1; |
| 1473 | unsigned long PopEpoch = 1; |
Piotr Padlewski | cc5868c1 | 2017-02-18 20:34:36 +0000 | [diff] [blame] | 1474 | // We perform a non-recursive top-down dominator tree walk. |
Daniel Berlin | 7ac3d74 | 2016-08-05 22:09:14 +0000 | [diff] [blame] | 1475 | for (const auto *DomNode : depth_first(DT->getRootNode())) |
| 1476 | optimizeUsesInBlock(DomNode->getBlock(), StackEpoch, PopEpoch, VersionStack, |
| 1477 | LocStackInfo); |
Daniel Berlin | c43aa5a | 2016-08-02 16:24:03 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Daniel Berlin | 3d512a2 | 2016-08-22 19:14:30 +0000 | [diff] [blame] | 1480 | void MemorySSA::placePHINodes( |
Michael Zolotukhin | 67cfbaa | 2018-05-15 18:40:29 +0000 | [diff] [blame] | 1481 | const SmallPtrSetImpl<BasicBlock *> &DefiningBlocks) { |
Daniel Berlin | 3d512a2 | 2016-08-22 19:14:30 +0000 | [diff] [blame] | 1482 | // Determine where our MemoryPhi's should go |
| 1483 | ForwardIDFCalculator IDFs(*DT); |
| 1484 | IDFs.setDefiningBlocks(DefiningBlocks); |
Daniel Berlin | 3d512a2 | 2016-08-22 19:14:30 +0000 | [diff] [blame] | 1485 | SmallVector<BasicBlock *, 32> IDFBlocks; |
| 1486 | IDFs.calculate(IDFBlocks); |
| 1487 | |
| 1488 | // Now place MemoryPhi nodes. |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1489 | for (auto &BB : IDFBlocks) |
| 1490 | createMemoryPhi(BB); |
Daniel Berlin | 3d512a2 | 2016-08-22 19:14:30 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1493 | void MemorySSA::buildMemorySSA(BatchAAResults &BAA) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1494 | // We create an access to represent "live on entry", for things like |
| 1495 | // arguments or users of globals, where the memory they use is defined before |
| 1496 | // the beginning of the function. We do not actually insert it into the IR. |
| 1497 | // We do not define a live on exit for the immediate uses, and thus our |
| 1498 | // semantics do *not* imply that something with no immediate uses can simply |
| 1499 | // be removed. |
| 1500 | BasicBlock &StartingPoint = F.getEntryBlock(); |
George Burgess IV | 612cf21 | 2018-02-27 06:43:19 +0000 | [diff] [blame] | 1501 | LiveOnEntryDef.reset(new MemoryDef(F.getContext(), nullptr, nullptr, |
| 1502 | &StartingPoint, NextID++)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1503 | |
| 1504 | // We maintain lists of memory accesses per-block, trading memory for time. We |
| 1505 | // could just look up the memory access for every possible instruction in the |
| 1506 | // stream. |
| 1507 | SmallPtrSet<BasicBlock *, 32> DefiningBlocks; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1508 | // Go through each block, figure out where defs occur, and chain together all |
| 1509 | // the accesses. |
| 1510 | for (BasicBlock &B : F) { |
Daniel Berlin | 7898ca6 | 2016-02-07 01:52:15 +0000 | [diff] [blame] | 1511 | bool InsertIntoDef = false; |
Daniel Berlin | ada263d | 2016-06-20 20:21:33 +0000 | [diff] [blame] | 1512 | AccessList *Accesses = nullptr; |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1513 | DefsList *Defs = nullptr; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1514 | for (Instruction &I : B) { |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1515 | MemoryUseOrDef *MUD = createNewAccess(&I, &BAA); |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 1516 | if (!MUD) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1517 | continue; |
Daniel Berlin | 1b51a29 | 2016-02-07 01:52:19 +0000 | [diff] [blame] | 1518 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1519 | if (!Accesses) |
| 1520 | Accesses = getOrCreateAccessList(&B); |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 1521 | Accesses->push_back(MUD); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1522 | if (isa<MemoryDef>(MUD)) { |
| 1523 | InsertIntoDef = true; |
| 1524 | if (!Defs) |
| 1525 | Defs = getOrCreateDefsList(&B); |
| 1526 | Defs->push_back(*MUD); |
| 1527 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1528 | } |
Daniel Berlin | 7898ca6 | 2016-02-07 01:52:15 +0000 | [diff] [blame] | 1529 | if (InsertIntoDef) |
| 1530 | DefiningBlocks.insert(&B); |
Daniel Berlin | 1b51a29 | 2016-02-07 01:52:19 +0000 | [diff] [blame] | 1531 | } |
Michael Zolotukhin | 67cfbaa | 2018-05-15 18:40:29 +0000 | [diff] [blame] | 1532 | placePHINodes(DefiningBlocks); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1533 | |
| 1534 | // Now do regular SSA renaming on the MemoryDef/MemoryUse. Visited will get |
| 1535 | // filled in with all blocks. |
| 1536 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 1537 | renamePass(DT->getRootNode(), LiveOnEntryDef.get(), Visited); |
| 1538 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1539 | ClobberWalkerBase<BatchAAResults> WalkerBase(this, &BAA, DT); |
| 1540 | CachingWalker<BatchAAResults> WalkerLocal(this, &WalkerBase); |
| 1541 | OptimizeUses(this, &WalkerLocal, &BAA, DT).optimizeUses(); |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 1542 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1543 | // Mark the uses in unreachable blocks as live on entry, so that they go |
| 1544 | // somewhere. |
| 1545 | for (auto &BB : F) |
| 1546 | if (!Visited.count(&BB)) |
| 1547 | markUnreachableAsLiveOnEntry(&BB); |
Daniel Berlin | 16ed57c | 2016-06-27 18:22:27 +0000 | [diff] [blame] | 1548 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1549 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 1550 | MemorySSAWalker *MemorySSA::getWalker() { return getWalkerImpl(); } |
| 1551 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1552 | MemorySSA::CachingWalker<AliasAnalysis> *MemorySSA::getWalkerImpl() { |
Daniel Berlin | 16ed57c | 2016-06-27 18:22:27 +0000 | [diff] [blame] | 1553 | if (Walker) |
| 1554 | return Walker.get(); |
| 1555 | |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 1556 | if (!WalkerBase) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1557 | WalkerBase = |
| 1558 | llvm::make_unique<ClobberWalkerBase<AliasAnalysis>>(this, AA, DT); |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 1559 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1560 | Walker = |
| 1561 | llvm::make_unique<CachingWalker<AliasAnalysis>>(this, WalkerBase.get()); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 1562 | return Walker.get(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1565 | MemorySSAWalker *MemorySSA::getSkipSelfWalker() { |
| 1566 | if (SkipWalker) |
| 1567 | return SkipWalker.get(); |
| 1568 | |
| 1569 | if (!WalkerBase) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1570 | WalkerBase = |
| 1571 | llvm::make_unique<ClobberWalkerBase<AliasAnalysis>>(this, AA, DT); |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1572 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1573 | SkipWalker = |
| 1574 | llvm::make_unique<SkipSelfWalker<AliasAnalysis>>(this, WalkerBase.get()); |
Alina Sbirlea | 12bbb4f | 2019-01-07 19:38:47 +0000 | [diff] [blame] | 1575 | return SkipWalker.get(); |
| 1576 | } |
| 1577 | |
| 1578 | |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1579 | // This is a helper function used by the creation routines. It places NewAccess |
| 1580 | // into the access and defs lists for a given basic block, at the given |
| 1581 | // insertion point. |
| 1582 | void MemorySSA::insertIntoListsForBlock(MemoryAccess *NewAccess, |
| 1583 | const BasicBlock *BB, |
| 1584 | InsertionPlace Point) { |
| 1585 | auto *Accesses = getOrCreateAccessList(BB); |
| 1586 | if (Point == Beginning) { |
| 1587 | // If it's a phi node, it goes first, otherwise, it goes after any phi |
| 1588 | // nodes. |
| 1589 | if (isa<MemoryPhi>(NewAccess)) { |
| 1590 | Accesses->push_front(NewAccess); |
| 1591 | auto *Defs = getOrCreateDefsList(BB); |
| 1592 | Defs->push_front(*NewAccess); |
| 1593 | } else { |
| 1594 | auto AI = find_if_not( |
| 1595 | *Accesses, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); }); |
| 1596 | Accesses->insert(AI, NewAccess); |
| 1597 | if (!isa<MemoryUse>(NewAccess)) { |
| 1598 | auto *Defs = getOrCreateDefsList(BB); |
| 1599 | auto DI = find_if_not( |
| 1600 | *Defs, [](const MemoryAccess &MA) { return isa<MemoryPhi>(MA); }); |
| 1601 | Defs->insert(DI, *NewAccess); |
| 1602 | } |
| 1603 | } |
| 1604 | } else { |
| 1605 | Accesses->push_back(NewAccess); |
| 1606 | if (!isa<MemoryUse>(NewAccess)) { |
| 1607 | auto *Defs = getOrCreateDefsList(BB); |
| 1608 | Defs->push_back(*NewAccess); |
| 1609 | } |
| 1610 | } |
Daniel Berlin | 9d8a335 | 2017-01-30 11:35:39 +0000 | [diff] [blame] | 1611 | BlockNumberingValid.erase(BB); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | void MemorySSA::insertIntoListsBefore(MemoryAccess *What, const BasicBlock *BB, |
| 1615 | AccessList::iterator InsertPt) { |
| 1616 | auto *Accesses = getWritableBlockAccesses(BB); |
| 1617 | bool WasEnd = InsertPt == Accesses->end(); |
| 1618 | Accesses->insert(AccessList::iterator(InsertPt), What); |
| 1619 | if (!isa<MemoryUse>(What)) { |
| 1620 | auto *Defs = getOrCreateDefsList(BB); |
| 1621 | // If we got asked to insert at the end, we have an easy job, just shove it |
| 1622 | // at the end. If we got asked to insert before an existing def, we also get |
Zhaoshi Zheng | a5531f2 | 2018-04-04 21:08:11 +0000 | [diff] [blame] | 1623 | // an iterator. If we got asked to insert before a use, we have to hunt for |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1624 | // the next def. |
| 1625 | if (WasEnd) { |
| 1626 | Defs->push_back(*What); |
| 1627 | } else if (isa<MemoryDef>(InsertPt)) { |
| 1628 | Defs->insert(InsertPt->getDefsIterator(), *What); |
| 1629 | } else { |
| 1630 | while (InsertPt != Accesses->end() && !isa<MemoryDef>(InsertPt)) |
| 1631 | ++InsertPt; |
| 1632 | // Either we found a def, or we are inserting at the end |
| 1633 | if (InsertPt == Accesses->end()) |
| 1634 | Defs->push_back(*What); |
| 1635 | else |
| 1636 | Defs->insert(InsertPt->getDefsIterator(), *What); |
| 1637 | } |
| 1638 | } |
Daniel Berlin | 9d8a335 | 2017-01-30 11:35:39 +0000 | [diff] [blame] | 1639 | BlockNumberingValid.erase(BB); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
George Burgess IV | 5676a5d | 2018-08-22 22:34:38 +0000 | [diff] [blame] | 1642 | void MemorySSA::prepareForMoveTo(MemoryAccess *What, BasicBlock *BB) { |
| 1643 | // Keep it in the lookup tables, remove from the lists |
| 1644 | removeFromLists(What, false); |
| 1645 | |
| 1646 | // Note that moving should implicitly invalidate the optimized state of a |
| 1647 | // MemoryUse (and Phis can't be optimized). However, it doesn't do so for a |
| 1648 | // MemoryDef. |
| 1649 | if (auto *MD = dyn_cast<MemoryDef>(What)) |
| 1650 | MD->resetOptimized(); |
| 1651 | What->setBlock(BB); |
| 1652 | } |
| 1653 | |
Zhaoshi Zheng | a5531f2 | 2018-04-04 21:08:11 +0000 | [diff] [blame] | 1654 | // Move What before Where in the IR. The end result is that What will belong to |
Daniel Berlin | 60ead05 | 2017-01-28 01:23:13 +0000 | [diff] [blame] | 1655 | // the right lists and have the right Block set, but will not otherwise be |
| 1656 | // correct. It will not have the right defining access, and if it is a def, |
| 1657 | // things below it will not properly be updated. |
| 1658 | void MemorySSA::moveTo(MemoryUseOrDef *What, BasicBlock *BB, |
| 1659 | AccessList::iterator Where) { |
George Burgess IV | 5676a5d | 2018-08-22 22:34:38 +0000 | [diff] [blame] | 1660 | prepareForMoveTo(What, BB); |
Daniel Berlin | 60ead05 | 2017-01-28 01:23:13 +0000 | [diff] [blame] | 1661 | insertIntoListsBefore(What, BB, Where); |
| 1662 | } |
| 1663 | |
Alina Sbirlea | 0f53355 | 2018-07-11 22:11:46 +0000 | [diff] [blame] | 1664 | void MemorySSA::moveTo(MemoryAccess *What, BasicBlock *BB, |
Daniel Berlin | 9d8a335 | 2017-01-30 11:35:39 +0000 | [diff] [blame] | 1665 | InsertionPlace Point) { |
Alina Sbirlea | 0f53355 | 2018-07-11 22:11:46 +0000 | [diff] [blame] | 1666 | if (isa<MemoryPhi>(What)) { |
| 1667 | assert(Point == Beginning && |
| 1668 | "Can only move a Phi at the beginning of the block"); |
| 1669 | // Update lookup table entry |
| 1670 | ValueToMemoryAccess.erase(What->getBlock()); |
| 1671 | bool Inserted = ValueToMemoryAccess.insert({BB, What}).second; |
| 1672 | (void)Inserted; |
| 1673 | assert(Inserted && "Cannot move a Phi to a block that already has one"); |
| 1674 | } |
| 1675 | |
George Burgess IV | 5676a5d | 2018-08-22 22:34:38 +0000 | [diff] [blame] | 1676 | prepareForMoveTo(What, BB); |
Daniel Berlin | 9d8a335 | 2017-01-30 11:35:39 +0000 | [diff] [blame] | 1677 | insertIntoListsForBlock(What, BB, Point); |
| 1678 | } |
| 1679 | |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1680 | MemoryPhi *MemorySSA::createMemoryPhi(BasicBlock *BB) { |
| 1681 | assert(!getMemoryAccess(BB) && "MemoryPhi already exists for this BB"); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1682 | MemoryPhi *Phi = new MemoryPhi(BB->getContext(), BB, NextID++); |
Daniel Berlin | 9d8a335 | 2017-01-30 11:35:39 +0000 | [diff] [blame] | 1683 | // Phi's always are placed at the front of the block. |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1684 | insertIntoListsForBlock(Phi, BB, Beginning); |
Daniel Berlin | 5130cc8 | 2016-07-31 21:08:20 +0000 | [diff] [blame] | 1685 | ValueToMemoryAccess[BB] = Phi; |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1686 | return Phi; |
| 1687 | } |
| 1688 | |
| 1689 | MemoryUseOrDef *MemorySSA::createDefinedAccess(Instruction *I, |
Alina Sbirlea | 7980099 | 2018-09-10 20:13:01 +0000 | [diff] [blame] | 1690 | MemoryAccess *Definition, |
Alina Sbirlea | 4bc625c | 2019-07-30 20:10:33 +0000 | [diff] [blame] | 1691 | const MemoryUseOrDef *Template, |
| 1692 | bool CreationMustSucceed) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1693 | assert(!isa<PHINode>(I) && "Cannot create a defined access for a PHI"); |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1694 | MemoryUseOrDef *NewAccess = createNewAccess(I, AA, Template); |
Alina Sbirlea | 4bc625c | 2019-07-30 20:10:33 +0000 | [diff] [blame] | 1695 | if (CreationMustSucceed) |
| 1696 | assert(NewAccess != nullptr && "Tried to create a memory access for a " |
| 1697 | "non-memory touching instruction"); |
| 1698 | if (NewAccess) |
| 1699 | NewAccess->setDefiningAccess(Definition); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1700 | return NewAccess; |
| 1701 | } |
| 1702 | |
Daniel Berlin | d952cea | 2017-04-07 01:28:36 +0000 | [diff] [blame] | 1703 | // Return true if the instruction has ordering constraints. |
| 1704 | // Note specifically that this only considers stores and loads |
| 1705 | // because others are still considered ModRef by getModRefInfo. |
| 1706 | static inline bool isOrdered(const Instruction *I) { |
| 1707 | if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 1708 | if (!SI->isUnordered()) |
| 1709 | return true; |
| 1710 | } else if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 1711 | if (!LI->isUnordered()) |
| 1712 | return true; |
| 1713 | } |
| 1714 | return false; |
| 1715 | } |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1716 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1717 | /// Helper function to create new memory accesses |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1718 | template <typename AliasAnalysisType> |
Alina Sbirlea | 7980099 | 2018-09-10 20:13:01 +0000 | [diff] [blame] | 1719 | MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1720 | AliasAnalysisType *AAP, |
Alina Sbirlea | 7980099 | 2018-09-10 20:13:01 +0000 | [diff] [blame] | 1721 | const MemoryUseOrDef *Template) { |
Peter Collingbourne | b9aa1f4 | 2016-05-26 04:58:46 +0000 | [diff] [blame] | 1722 | // The assume intrinsic has a control dependency which we model by claiming |
| 1723 | // that it writes arbitrarily. Ignore that fake memory dependency here. |
| 1724 | // FIXME: Replace this special casing with a more accurate modelling of |
| 1725 | // assume's control dependency. |
| 1726 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 1727 | if (II->getIntrinsicID() == Intrinsic::assume) |
| 1728 | return nullptr; |
| 1729 | |
Alina Sbirlea | 7980099 | 2018-09-10 20:13:01 +0000 | [diff] [blame] | 1730 | bool Def, Use; |
| 1731 | if (Template) { |
| 1732 | Def = dyn_cast_or_null<MemoryDef>(Template) != nullptr; |
| 1733 | Use = dyn_cast_or_null<MemoryUse>(Template) != nullptr; |
| 1734 | #if !defined(NDEBUG) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1735 | ModRefInfo ModRef = AAP->getModRefInfo(I, None); |
Alina Sbirlea | 7980099 | 2018-09-10 20:13:01 +0000 | [diff] [blame] | 1736 | bool DefCheck, UseCheck; |
| 1737 | DefCheck = isModSet(ModRef) || isOrdered(I); |
| 1738 | UseCheck = isRefSet(ModRef); |
| 1739 | assert(Def == DefCheck && (Def || Use == UseCheck) && "Invalid template"); |
| 1740 | #endif |
| 1741 | } else { |
| 1742 | // Find out what affect this instruction has on memory. |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1743 | ModRefInfo ModRef = AAP->getModRefInfo(I, None); |
Alina Sbirlea | 7980099 | 2018-09-10 20:13:01 +0000 | [diff] [blame] | 1744 | // The isOrdered check is used to ensure that volatiles end up as defs |
| 1745 | // (atomics end up as ModRef right now anyway). Until we separate the |
| 1746 | // ordering chain from the memory chain, this enables people to see at least |
| 1747 | // some relative ordering to volatiles. Note that getClobberingMemoryAccess |
| 1748 | // will still give an answer that bypasses other volatile loads. TODO: |
| 1749 | // Separate memory aliasing and ordering into two different chains so that |
| 1750 | // we can precisely represent both "what memory will this read/write/is |
| 1751 | // clobbered by" and "what instructions can I move this past". |
| 1752 | Def = isModSet(ModRef) || isOrdered(I); |
| 1753 | Use = isRefSet(ModRef); |
| 1754 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1755 | |
| 1756 | // It's possible for an instruction to not modify memory at all. During |
| 1757 | // construction, we ignore them. |
Peter Collingbourne | ffecb14 | 2016-05-26 01:19:17 +0000 | [diff] [blame] | 1758 | if (!Def && !Use) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1759 | return nullptr; |
| 1760 | |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 1761 | MemoryUseOrDef *MUD; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1762 | if (Def) |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 1763 | MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1764 | else |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 1765 | MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent()); |
Daniel Berlin | 5130cc8 | 2016-07-31 21:08:20 +0000 | [diff] [blame] | 1766 | ValueToMemoryAccess[I] = MUD; |
George Burgess IV | b42b762 | 2016-03-11 19:34:03 +0000 | [diff] [blame] | 1767 | return MUD; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1770 | /// Returns true if \p Replacer dominates \p Replacee . |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1771 | bool MemorySSA::dominatesUse(const MemoryAccess *Replacer, |
| 1772 | const MemoryAccess *Replacee) const { |
| 1773 | if (isa<MemoryUseOrDef>(Replacee)) |
| 1774 | return DT->dominates(Replacer->getBlock(), Replacee->getBlock()); |
| 1775 | const auto *MP = cast<MemoryPhi>(Replacee); |
| 1776 | // For a phi node, the use occurs in the predecessor block of the phi node. |
| 1777 | // Since we may occur multiple times in the phi node, we have to check each |
| 1778 | // operand to ensure Replacer dominates each operand where Replacee occurs. |
| 1779 | for (const Use &Arg : MP->operands()) { |
George Burgess IV | b5a229f | 2016-02-02 23:15:26 +0000 | [diff] [blame] | 1780 | if (Arg.get() != Replacee && |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1781 | !DT->dominates(Replacer->getBlock(), MP->getIncomingBlock(Arg))) |
| 1782 | return false; |
| 1783 | } |
| 1784 | return true; |
| 1785 | } |
| 1786 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1787 | /// Properly remove \p MA from all of MemorySSA's lookup tables. |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1788 | void MemorySSA::removeFromLookups(MemoryAccess *MA) { |
| 1789 | assert(MA->use_empty() && |
| 1790 | "Trying to remove memory access that still has uses"); |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 1791 | BlockNumbering.erase(MA); |
George Burgess IV | 2cbf973 | 2018-06-22 22:34:07 +0000 | [diff] [blame] | 1792 | if (auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1793 | MUD->setDefiningAccess(nullptr); |
| 1794 | // Invalidate our walker's cache if necessary |
| 1795 | if (!isa<MemoryUse>(MA)) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 1796 | getWalker()->invalidateInfo(MA); |
George Burgess IV | 2cbf973 | 2018-06-22 22:34:07 +0000 | [diff] [blame] | 1797 | |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1798 | Value *MemoryInst; |
George Burgess IV | 2cbf973 | 2018-06-22 22:34:07 +0000 | [diff] [blame] | 1799 | if (const auto *MUD = dyn_cast<MemoryUseOrDef>(MA)) |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1800 | MemoryInst = MUD->getMemoryInst(); |
George Burgess IV | 2cbf973 | 2018-06-22 22:34:07 +0000 | [diff] [blame] | 1801 | else |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1802 | MemoryInst = MA->getBlock(); |
George Burgess IV | 2cbf973 | 2018-06-22 22:34:07 +0000 | [diff] [blame] | 1803 | |
Daniel Berlin | 5130cc8 | 2016-07-31 21:08:20 +0000 | [diff] [blame] | 1804 | auto VMA = ValueToMemoryAccess.find(MemoryInst); |
| 1805 | if (VMA->second == MA) |
| 1806 | ValueToMemoryAccess.erase(VMA); |
Daniel Berlin | 60ead05 | 2017-01-28 01:23:13 +0000 | [diff] [blame] | 1807 | } |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1808 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1809 | /// Properly remove \p MA from all of MemorySSA's lists. |
Daniel Berlin | 60ead05 | 2017-01-28 01:23:13 +0000 | [diff] [blame] | 1810 | /// |
| 1811 | /// Because of the way the intrusive list and use lists work, it is important to |
| 1812 | /// do removal in the right order. |
| 1813 | /// ShouldDelete defaults to true, and will cause the memory access to also be |
| 1814 | /// deleted, not just removed. |
| 1815 | void MemorySSA::removeFromLists(MemoryAccess *MA, bool ShouldDelete) { |
Alina Sbirlea | da1e80f | 2018-06-29 20:46:16 +0000 | [diff] [blame] | 1816 | BasicBlock *BB = MA->getBlock(); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1817 | // The access list owns the reference, so we erase it from the non-owning list |
| 1818 | // first. |
| 1819 | if (!isa<MemoryUse>(MA)) { |
Alina Sbirlea | da1e80f | 2018-06-29 20:46:16 +0000 | [diff] [blame] | 1820 | auto DefsIt = PerBlockDefs.find(BB); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1821 | std::unique_ptr<DefsList> &Defs = DefsIt->second; |
| 1822 | Defs->remove(*MA); |
| 1823 | if (Defs->empty()) |
| 1824 | PerBlockDefs.erase(DefsIt); |
| 1825 | } |
| 1826 | |
Daniel Berlin | 60ead05 | 2017-01-28 01:23:13 +0000 | [diff] [blame] | 1827 | // The erase call here will delete it. If we don't want it deleted, we call |
| 1828 | // remove instead. |
Alina Sbirlea | da1e80f | 2018-06-29 20:46:16 +0000 | [diff] [blame] | 1829 | auto AccessIt = PerBlockAccesses.find(BB); |
Daniel Berlin | ada263d | 2016-06-20 20:21:33 +0000 | [diff] [blame] | 1830 | std::unique_ptr<AccessList> &Accesses = AccessIt->second; |
Daniel Berlin | 60ead05 | 2017-01-28 01:23:13 +0000 | [diff] [blame] | 1831 | if (ShouldDelete) |
| 1832 | Accesses->erase(MA); |
| 1833 | else |
| 1834 | Accesses->remove(MA); |
| 1835 | |
Alina Sbirlea | da1e80f | 2018-06-29 20:46:16 +0000 | [diff] [blame] | 1836 | if (Accesses->empty()) { |
George Burgess IV | e0e6e48 | 2016-03-02 02:35:04 +0000 | [diff] [blame] | 1837 | PerBlockAccesses.erase(AccessIt); |
Alina Sbirlea | da1e80f | 2018-06-29 20:46:16 +0000 | [diff] [blame] | 1838 | BlockNumberingValid.erase(BB); |
| 1839 | } |
Daniel Berlin | 83fc77b | 2016-03-01 18:46:54 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1842 | void MemorySSA::print(raw_ostream &OS) const { |
| 1843 | MemorySSAAnnotatedWriter Writer(this); |
| 1844 | F.print(OS, &Writer); |
| 1845 | } |
| 1846 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1847 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 1848 | LLVM_DUMP_METHOD void MemorySSA::dump() const { print(dbgs()); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1849 | #endif |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 1850 | |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 1851 | void MemorySSA::verifyMemorySSA() const { |
| 1852 | verifyDefUses(F); |
| 1853 | verifyDomination(F); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1854 | verifyOrdering(F); |
George Burgess IV | 97ec624 | 2018-06-25 05:30:36 +0000 | [diff] [blame] | 1855 | verifyDominationNumbers(F); |
Alina Sbirlea | 63e97fa | 2019-07-31 17:41:04 +0000 | [diff] [blame^] | 1856 | verifyPrevDefInPhis(F); |
Alina Sbirlea | d77edc0 | 2019-02-11 19:51:21 +0000 | [diff] [blame] | 1857 | // Previously, the verification used to also verify that the clobberingAccess |
| 1858 | // cached by MemorySSA is the same as the clobberingAccess found at a later |
| 1859 | // query to AA. This does not hold true in general due to the current fragility |
| 1860 | // of BasicAA which has arbitrary caps on the things it analyzes before giving |
| 1861 | // up. As a result, transformations that are correct, will lead to BasicAA |
| 1862 | // returning different Alias answers before and after that transformation. |
| 1863 | // Invalidating MemorySSA is not an option, as the results in BasicAA can be so |
| 1864 | // random, in the worst case we'd need to rebuild MemorySSA from scratch after |
| 1865 | // every transformation, which defeats the purpose of using it. For such an |
| 1866 | // example, see test4 added in D51960. |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Alina Sbirlea | 63e97fa | 2019-07-31 17:41:04 +0000 | [diff] [blame^] | 1869 | void MemorySSA::verifyPrevDefInPhis(Function &F) const { |
| 1870 | #ifndef NDEBUG |
| 1871 | for (const BasicBlock &BB : F) { |
| 1872 | if (MemoryPhi *Phi = getMemoryAccess(&BB)) { |
| 1873 | for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) { |
| 1874 | auto *Pred = Phi->getIncomingBlock(I); |
| 1875 | auto *IncAcc = Phi->getIncomingValue(I); |
| 1876 | // If Pred has no unreachable predecessors, get last def looking at |
| 1877 | // IDoms. If, while walkings IDoms, any of these has an unreachable |
| 1878 | // predecessor, then the expected incoming def is LoE. |
| 1879 | if (auto *DTNode = DT->getNode(Pred)) { |
| 1880 | while (DTNode) { |
| 1881 | if (auto *DefList = getBlockDefs(DTNode->getBlock())) { |
| 1882 | auto *LastAcc = &*(--DefList->end()); |
| 1883 | assert(LastAcc == IncAcc && |
| 1884 | "Incorrect incoming access into phi."); |
| 1885 | break; |
| 1886 | } |
| 1887 | DTNode = DTNode->getIDom(); |
| 1888 | } |
| 1889 | assert((DTNode || IncAcc == getLiveOnEntryDef()) && |
| 1890 | "Expected LoE inc"); |
| 1891 | } else if (auto *DefList = getBlockDefs(Pred)) { |
| 1892 | // If Pred has unreachable predecessors, but has at least a Def, the |
| 1893 | // incoming access can be the last Def in Pred, or it could have been |
| 1894 | // optimized to LoE. |
| 1895 | auto *LastAcc = &*(--DefList->end()); |
| 1896 | assert((LastAcc == IncAcc || IncAcc == getLiveOnEntryDef()) && |
| 1897 | "Incorrect incoming access into phi."); |
| 1898 | } else { |
| 1899 | // If Pred has unreachable predecessors and no Defs, incoming access |
| 1900 | // should be LoE. |
| 1901 | assert(IncAcc == getLiveOnEntryDef() && "Expected LoE inc"); |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | #endif |
| 1907 | } |
| 1908 | |
George Burgess IV | 97ec624 | 2018-06-25 05:30:36 +0000 | [diff] [blame] | 1909 | /// Verify that all of the blocks we believe to have valid domination numbers |
| 1910 | /// actually have valid domination numbers. |
| 1911 | void MemorySSA::verifyDominationNumbers(const Function &F) const { |
| 1912 | #ifndef NDEBUG |
| 1913 | if (BlockNumberingValid.empty()) |
| 1914 | return; |
| 1915 | |
| 1916 | SmallPtrSet<const BasicBlock *, 16> ValidBlocks = BlockNumberingValid; |
| 1917 | for (const BasicBlock &BB : F) { |
| 1918 | if (!ValidBlocks.count(&BB)) |
| 1919 | continue; |
| 1920 | |
| 1921 | ValidBlocks.erase(&BB); |
| 1922 | |
| 1923 | const AccessList *Accesses = getBlockAccesses(&BB); |
| 1924 | // It's correct to say an empty block has valid numbering. |
| 1925 | if (!Accesses) |
| 1926 | continue; |
| 1927 | |
| 1928 | // Block numbering starts at 1. |
| 1929 | unsigned long LastNumber = 0; |
| 1930 | for (const MemoryAccess &MA : *Accesses) { |
| 1931 | auto ThisNumberIter = BlockNumbering.find(&MA); |
| 1932 | assert(ThisNumberIter != BlockNumbering.end() && |
| 1933 | "MemoryAccess has no domination number in a valid block!"); |
| 1934 | |
| 1935 | unsigned long ThisNumber = ThisNumberIter->second; |
| 1936 | assert(ThisNumber > LastNumber && |
| 1937 | "Domination numbers should be strictly increasing!"); |
| 1938 | LastNumber = ThisNumber; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | assert(ValidBlocks.empty() && |
| 1943 | "All valid BasicBlocks should exist in F -- dangling pointers?"); |
| 1944 | #endif |
| 1945 | } |
| 1946 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1947 | /// Verify that the order and existence of MemoryAccesses matches the |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1948 | /// order and existence of memory affecting instructions. |
| 1949 | void MemorySSA::verifyOrdering(Function &F) const { |
George Burgess IV | 6a9aa02 | 2018-08-28 00:32:32 +0000 | [diff] [blame] | 1950 | #ifndef NDEBUG |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1951 | // Walk all the blocks, comparing what the lookups think and what the access |
| 1952 | // lists think, as well as the order in the blocks vs the order in the access |
| 1953 | // lists. |
| 1954 | SmallVector<MemoryAccess *, 32> ActualAccesses; |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1955 | SmallVector<MemoryAccess *, 32> ActualDefs; |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1956 | for (BasicBlock &B : F) { |
| 1957 | const AccessList *AL = getBlockAccesses(&B); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1958 | const auto *DL = getBlockDefs(&B); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1959 | MemoryAccess *Phi = getMemoryAccess(&B); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1960 | if (Phi) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1961 | ActualAccesses.push_back(Phi); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1962 | ActualDefs.push_back(Phi); |
| 1963 | } |
| 1964 | |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1965 | for (Instruction &I : B) { |
| 1966 | MemoryAccess *MA = getMemoryAccess(&I); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1967 | assert((!MA || (AL && (isa<MemoryUse>(MA) || DL))) && |
| 1968 | "We have memory affecting instructions " |
| 1969 | "in this block but they are not in the " |
| 1970 | "access list or defs list"); |
| 1971 | if (MA) { |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1972 | ActualAccesses.push_back(MA); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1973 | if (isa<MemoryDef>(MA)) |
| 1974 | ActualDefs.push_back(MA); |
| 1975 | } |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1976 | } |
| 1977 | // Either we hit the assert, really have no accesses, or we have both |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1978 | // accesses and an access list. |
| 1979 | // Same with defs. |
| 1980 | if (!AL && !DL) |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1981 | continue; |
| 1982 | assert(AL->size() == ActualAccesses.size() && |
| 1983 | "We don't have the same number of accesses in the block as on the " |
| 1984 | "access list"); |
Davide Italiano | 6c77de0 | 2017-01-30 03:16:43 +0000 | [diff] [blame] | 1985 | assert((DL || ActualDefs.size() == 0) && |
| 1986 | "Either we should have a defs list, or we should have no defs"); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1987 | assert((!DL || DL->size() == ActualDefs.size()) && |
| 1988 | "We don't have the same number of defs in the block as on the " |
| 1989 | "def list"); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 1990 | auto ALI = AL->begin(); |
| 1991 | auto AAI = ActualAccesses.begin(); |
| 1992 | while (ALI != AL->end() && AAI != ActualAccesses.end()) { |
| 1993 | assert(&*ALI == *AAI && "Not the same accesses in the same order"); |
| 1994 | ++ALI; |
| 1995 | ++AAI; |
| 1996 | } |
| 1997 | ActualAccesses.clear(); |
Daniel Berlin | d602e04 | 2017-01-25 20:56:19 +0000 | [diff] [blame] | 1998 | if (DL) { |
| 1999 | auto DLI = DL->begin(); |
| 2000 | auto ADI = ActualDefs.begin(); |
| 2001 | while (DLI != DL->end() && ADI != ActualDefs.end()) { |
| 2002 | assert(&*DLI == *ADI && "Not the same defs in the same order"); |
| 2003 | ++DLI; |
| 2004 | ++ADI; |
| 2005 | } |
| 2006 | } |
| 2007 | ActualDefs.clear(); |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 2008 | } |
George Burgess IV | 6a9aa02 | 2018-08-28 00:32:32 +0000 | [diff] [blame] | 2009 | #endif |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2012 | /// Verify the domination properties of MemorySSA by checking that each |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2013 | /// definition dominates all of its uses. |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 2014 | void MemorySSA::verifyDomination(Function &F) const { |
Daniel Berlin | 7af9587 | 2016-08-05 21:47:20 +0000 | [diff] [blame] | 2015 | #ifndef NDEBUG |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2016 | for (BasicBlock &B : F) { |
| 2017 | // Phi nodes are attached to basic blocks |
Daniel Berlin | 2919b1c | 2016-08-05 21:46:52 +0000 | [diff] [blame] | 2018 | if (MemoryPhi *MP = getMemoryAccess(&B)) |
| 2019 | for (const Use &U : MP->uses()) |
| 2020 | assert(dominates(MP, U) && "Memory PHI does not dominate it's uses"); |
Daniel Berlin | 7af9587 | 2016-08-05 21:47:20 +0000 | [diff] [blame] | 2021 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2022 | for (Instruction &I : B) { |
| 2023 | MemoryAccess *MD = dyn_cast_or_null<MemoryDef>(getMemoryAccess(&I)); |
| 2024 | if (!MD) |
| 2025 | continue; |
| 2026 | |
Daniel Berlin | 2919b1c | 2016-08-05 21:46:52 +0000 | [diff] [blame] | 2027 | for (const Use &U : MD->uses()) |
| 2028 | assert(dominates(MD, U) && "Memory Def does not dominate it's uses"); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2029 | } |
| 2030 | } |
Daniel Berlin | 7af9587 | 2016-08-05 21:47:20 +0000 | [diff] [blame] | 2031 | #endif |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2034 | /// Verify the def-use lists in MemorySSA, by verifying that \p Use |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2035 | /// appears in the use list of \p Def. |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 2036 | void MemorySSA::verifyUseInDefs(MemoryAccess *Def, MemoryAccess *Use) const { |
Daniel Berlin | 7af9587 | 2016-08-05 21:47:20 +0000 | [diff] [blame] | 2037 | #ifndef NDEBUG |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2038 | // The live on entry use may cause us to get a NULL def here |
Daniel Berlin | 7af9587 | 2016-08-05 21:47:20 +0000 | [diff] [blame] | 2039 | if (!Def) |
| 2040 | assert(isLiveOnEntryDef(Use) && |
| 2041 | "Null def but use not point to live on entry def"); |
| 2042 | else |
Daniel Berlin | da2f38e | 2016-08-11 21:26:50 +0000 | [diff] [blame] | 2043 | assert(is_contained(Def->users(), Use) && |
Daniel Berlin | 7af9587 | 2016-08-05 21:47:20 +0000 | [diff] [blame] | 2044 | "Did not find use in def's use list"); |
| 2045 | #endif |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2048 | /// Verify the immediate use information, by walking all the memory |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2049 | /// accesses and verifying that, for each use, it appears in the |
| 2050 | /// appropriate def's use list |
Daniel Berlin | 932b4cb | 2016-02-10 17:39:43 +0000 | [diff] [blame] | 2051 | void MemorySSA::verifyDefUses(Function &F) const { |
George Burgess IV | 6a9aa02 | 2018-08-28 00:32:32 +0000 | [diff] [blame] | 2052 | #ifndef NDEBUG |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2053 | for (BasicBlock &B : F) { |
| 2054 | // Phi nodes are attached to basic blocks |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 2055 | if (MemoryPhi *Phi = getMemoryAccess(&B)) { |
David Majnemer | 580e754 | 2016-06-25 00:04:06 +0000 | [diff] [blame] | 2056 | assert(Phi->getNumOperands() == static_cast<unsigned>(std::distance( |
| 2057 | pred_begin(&B), pred_end(&B))) && |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 2058 | "Incomplete MemoryPhi Node"); |
Alina Sbirlea | 201d02c | 2018-06-20 21:06:13 +0000 | [diff] [blame] | 2059 | for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2060 | verifyUseInDefs(Phi->getIncomingValue(I), Phi); |
Alina Sbirlea | 201d02c | 2018-06-20 21:06:13 +0000 | [diff] [blame] | 2061 | assert(find(predecessors(&B), Phi->getIncomingBlock(I)) != |
| 2062 | pred_end(&B) && |
| 2063 | "Incoming phi block not a block predecessor"); |
| 2064 | } |
Daniel Berlin | 1430026 | 2016-06-21 18:39:20 +0000 | [diff] [blame] | 2065 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2066 | |
| 2067 | for (Instruction &I : B) { |
George Burgess IV | 66837ab | 2016-11-01 21:17:46 +0000 | [diff] [blame] | 2068 | if (MemoryUseOrDef *MA = getMemoryAccess(&I)) { |
| 2069 | verifyUseInDefs(MA->getDefiningAccess(), MA); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | } |
George Burgess IV | 6a9aa02 | 2018-08-28 00:32:32 +0000 | [diff] [blame] | 2073 | #endif |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 2076 | /// Perform a local numbering on blocks so that instruction ordering can be |
| 2077 | /// determined in constant time. |
| 2078 | /// TODO: We currently just number in order. If we numbered by N, we could |
| 2079 | /// allow at least N-1 sequences of insertBefore or insertAfter (and at least |
| 2080 | /// log2(N) sequences of mixed before and after) without needing to invalidate |
| 2081 | /// the numbering. |
| 2082 | void MemorySSA::renumberBlock(const BasicBlock *B) const { |
| 2083 | // The pre-increment ensures the numbers really start at 1. |
| 2084 | unsigned long CurrentNumber = 0; |
| 2085 | const AccessList *AL = getBlockAccesses(B); |
| 2086 | assert(AL != nullptr && "Asking to renumber an empty block"); |
| 2087 | for (const auto &I : *AL) |
| 2088 | BlockNumbering[&I] = ++CurrentNumber; |
| 2089 | BlockNumberingValid.insert(B); |
| 2090 | } |
| 2091 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2092 | /// Determine, for two memory accesses in the same block, |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2093 | /// whether \p Dominator dominates \p Dominatee. |
| 2094 | /// \returns True if \p Dominator dominates \p Dominatee. |
| 2095 | bool MemorySSA::locallyDominates(const MemoryAccess *Dominator, |
| 2096 | const MemoryAccess *Dominatee) const { |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 2097 | const BasicBlock *DominatorBlock = Dominator->getBlock(); |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 2098 | |
Daniel Berlin | 1986030 | 2016-07-19 23:08:08 +0000 | [diff] [blame] | 2099 | assert((DominatorBlock == Dominatee->getBlock()) && |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 2100 | "Asking for local domination when accesses are in different blocks!"); |
Sebastian Pop | e1f60b1 | 2016-06-10 21:36:41 +0000 | [diff] [blame] | 2101 | // A node dominates itself. |
| 2102 | if (Dominatee == Dominator) |
| 2103 | return true; |
| 2104 | |
| 2105 | // When Dominatee is defined on function entry, it is not dominated by another |
| 2106 | // memory access. |
| 2107 | if (isLiveOnEntryDef(Dominatee)) |
| 2108 | return false; |
| 2109 | |
| 2110 | // When Dominator is defined on function entry, it dominates the other memory |
| 2111 | // access. |
| 2112 | if (isLiveOnEntryDef(Dominator)) |
| 2113 | return true; |
| 2114 | |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 2115 | if (!BlockNumberingValid.count(DominatorBlock)) |
| 2116 | renumberBlock(DominatorBlock); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2117 | |
Daniel Berlin | 5c46b94 | 2016-07-19 22:49:43 +0000 | [diff] [blame] | 2118 | unsigned long DominatorNum = BlockNumbering.lookup(Dominator); |
| 2119 | // All numbers start with 1 |
| 2120 | assert(DominatorNum != 0 && "Block was not numbered properly"); |
| 2121 | unsigned long DominateeNum = BlockNumbering.lookup(Dominatee); |
| 2122 | assert(DominateeNum != 0 && "Block was not numbered properly"); |
| 2123 | return DominatorNum < DominateeNum; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 2126 | bool MemorySSA::dominates(const MemoryAccess *Dominator, |
| 2127 | const MemoryAccess *Dominatee) const { |
| 2128 | if (Dominator == Dominatee) |
| 2129 | return true; |
| 2130 | |
| 2131 | if (isLiveOnEntryDef(Dominatee)) |
| 2132 | return false; |
| 2133 | |
| 2134 | if (Dominator->getBlock() != Dominatee->getBlock()) |
| 2135 | return DT->dominates(Dominator->getBlock(), Dominatee->getBlock()); |
| 2136 | return locallyDominates(Dominator, Dominatee); |
| 2137 | } |
| 2138 | |
Daniel Berlin | 2919b1c | 2016-08-05 21:46:52 +0000 | [diff] [blame] | 2139 | bool MemorySSA::dominates(const MemoryAccess *Dominator, |
| 2140 | const Use &Dominatee) const { |
| 2141 | if (MemoryPhi *MP = dyn_cast<MemoryPhi>(Dominatee.getUser())) { |
| 2142 | BasicBlock *UseBB = MP->getIncomingBlock(Dominatee); |
| 2143 | // The def must dominate the incoming block of the phi. |
| 2144 | if (UseBB != Dominator->getBlock()) |
| 2145 | return DT->dominates(Dominator->getBlock(), UseBB); |
| 2146 | // If the UseBB and the DefBB are the same, compare locally. |
| 2147 | return locallyDominates(Dominator, cast<MemoryAccess>(Dominatee)); |
| 2148 | } |
| 2149 | // If it's not a PHI node use, the normal dominates can already handle it. |
| 2150 | return dominates(Dominator, cast<MemoryAccess>(Dominatee.getUser())); |
| 2151 | } |
| 2152 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2153 | const static char LiveOnEntryStr[] = "liveOnEntry"; |
| 2154 | |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 2155 | void MemoryAccess::print(raw_ostream &OS) const { |
| 2156 | switch (getValueID()) { |
| 2157 | case MemoryPhiVal: return static_cast<const MemoryPhi *>(this)->print(OS); |
| 2158 | case MemoryDefVal: return static_cast<const MemoryDef *>(this)->print(OS); |
| 2159 | case MemoryUseVal: return static_cast<const MemoryUse *>(this)->print(OS); |
| 2160 | } |
| 2161 | llvm_unreachable("invalid value id"); |
| 2162 | } |
| 2163 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2164 | void MemoryDef::print(raw_ostream &OS) const { |
| 2165 | MemoryAccess *UO = getDefiningAccess(); |
| 2166 | |
George Burgess IV | aa283d8 | 2018-06-14 19:55:53 +0000 | [diff] [blame] | 2167 | auto printID = [&OS](MemoryAccess *A) { |
| 2168 | if (A && A->getID()) |
| 2169 | OS << A->getID(); |
| 2170 | else |
| 2171 | OS << LiveOnEntryStr; |
| 2172 | }; |
| 2173 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2174 | OS << getID() << " = MemoryDef("; |
George Burgess IV | aa283d8 | 2018-06-14 19:55:53 +0000 | [diff] [blame] | 2175 | printID(UO); |
| 2176 | OS << ")"; |
| 2177 | |
| 2178 | if (isOptimized()) { |
| 2179 | OS << "->"; |
| 2180 | printID(getOptimized()); |
| 2181 | |
| 2182 | if (Optional<AliasResult> AR = getOptimizedAccessType()) |
| 2183 | OS << " " << *AR; |
| 2184 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | void MemoryPhi::print(raw_ostream &OS) const { |
| 2188 | bool First = true; |
| 2189 | OS << getID() << " = MemoryPhi("; |
| 2190 | for (const auto &Op : operands()) { |
| 2191 | BasicBlock *BB = getIncomingBlock(Op); |
| 2192 | MemoryAccess *MA = cast<MemoryAccess>(Op); |
| 2193 | if (!First) |
| 2194 | OS << ','; |
| 2195 | else |
| 2196 | First = false; |
| 2197 | |
| 2198 | OS << '{'; |
| 2199 | if (BB->hasName()) |
| 2200 | OS << BB->getName(); |
| 2201 | else |
| 2202 | BB->printAsOperand(OS, false); |
| 2203 | OS << ','; |
| 2204 | if (unsigned ID = MA->getID()) |
| 2205 | OS << ID; |
| 2206 | else |
| 2207 | OS << LiveOnEntryStr; |
| 2208 | OS << '}'; |
| 2209 | } |
| 2210 | OS << ')'; |
| 2211 | } |
| 2212 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2213 | void MemoryUse::print(raw_ostream &OS) const { |
| 2214 | MemoryAccess *UO = getDefiningAccess(); |
| 2215 | OS << "MemoryUse("; |
| 2216 | if (UO && UO->getID()) |
| 2217 | OS << UO->getID(); |
| 2218 | else |
| 2219 | OS << LiveOnEntryStr; |
| 2220 | OS << ')'; |
George Burgess IV | aa283d8 | 2018-06-14 19:55:53 +0000 | [diff] [blame] | 2221 | |
| 2222 | if (Optional<AliasResult> AR = getOptimizedAccessType()) |
| 2223 | OS << " " << *AR; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | void MemoryAccess::dump() const { |
Daniel Berlin | 78cbd28 | 2017-02-20 22:26:03 +0000 | [diff] [blame] | 2227 | // Cannot completely remove virtual function even in release mode. |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 2228 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2229 | print(dbgs()); |
| 2230 | dbgs() << "\n"; |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 2231 | #endif |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Chad Rosier | 232e29e | 2016-07-06 21:20:47 +0000 | [diff] [blame] | 2234 | char MemorySSAPrinterLegacyPass::ID = 0; |
| 2235 | |
| 2236 | MemorySSAPrinterLegacyPass::MemorySSAPrinterLegacyPass() : FunctionPass(ID) { |
| 2237 | initializeMemorySSAPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 2238 | } |
| 2239 | |
| 2240 | void MemorySSAPrinterLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 2241 | AU.setPreservesAll(); |
| 2242 | AU.addRequired<MemorySSAWrapperPass>(); |
Chad Rosier | 232e29e | 2016-07-06 21:20:47 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | bool MemorySSAPrinterLegacyPass::runOnFunction(Function &F) { |
| 2246 | auto &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA(); |
| 2247 | MSSA.print(dbgs()); |
| 2248 | if (VerifyMemorySSA) |
| 2249 | MSSA.verifyMemorySSA(); |
| 2250 | return false; |
| 2251 | } |
| 2252 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 2253 | AnalysisKey MemorySSAAnalysis::Key; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2254 | |
Daniel Berlin | 1e98c04 | 2016-09-26 17:22:54 +0000 | [diff] [blame] | 2255 | MemorySSAAnalysis::Result MemorySSAAnalysis::run(Function &F, |
| 2256 | FunctionAnalysisManager &AM) { |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2257 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 2258 | auto &AA = AM.getResult<AAManager>(F); |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 2259 | return MemorySSAAnalysis::Result(llvm::make_unique<MemorySSA>(F, &AA, &DT)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Alina Sbirlea | b468320 | 2019-04-30 22:43:55 +0000 | [diff] [blame] | 2262 | bool MemorySSAAnalysis::Result::invalidate( |
| 2263 | Function &F, const PreservedAnalyses &PA, |
| 2264 | FunctionAnalysisManager::Invalidator &Inv) { |
| 2265 | auto PAC = PA.getChecker<MemorySSAAnalysis>(); |
| 2266 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 2267 | Inv.invalidate<AAManager>(F, PA) || |
| 2268 | Inv.invalidate<DominatorTreeAnalysis>(F, PA); |
| 2269 | } |
| 2270 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2271 | PreservedAnalyses MemorySSAPrinterPass::run(Function &F, |
| 2272 | FunctionAnalysisManager &AM) { |
| 2273 | OS << "MemorySSA for function: " << F.getName() << "\n"; |
Geoff Berry | 290a13e | 2016-08-08 18:27:22 +0000 | [diff] [blame] | 2274 | AM.getResult<MemorySSAAnalysis>(F).getMSSA().print(OS); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2275 | |
| 2276 | return PreservedAnalyses::all(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2279 | PreservedAnalyses MemorySSAVerifierPass::run(Function &F, |
| 2280 | FunctionAnalysisManager &AM) { |
Geoff Berry | 290a13e | 2016-08-08 18:27:22 +0000 | [diff] [blame] | 2281 | AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA(); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2282 | |
| 2283 | return PreservedAnalyses::all(); |
| 2284 | } |
| 2285 | |
| 2286 | char MemorySSAWrapperPass::ID = 0; |
| 2287 | |
| 2288 | MemorySSAWrapperPass::MemorySSAWrapperPass() : FunctionPass(ID) { |
| 2289 | initializeMemorySSAWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 2290 | } |
| 2291 | |
| 2292 | void MemorySSAWrapperPass::releaseMemory() { MSSA.reset(); } |
| 2293 | |
| 2294 | void MemorySSAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2295 | AU.setPreservesAll(); |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2296 | AU.addRequiredTransitive<DominatorTreeWrapperPass>(); |
| 2297 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2300 | bool MemorySSAWrapperPass::runOnFunction(Function &F) { |
| 2301 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 2302 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 2303 | MSSA.reset(new MemorySSA(F, &AA, &DT)); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2304 | return false; |
| 2305 | } |
| 2306 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2307 | void MemorySSAWrapperPass::verifyAnalysis() const { MSSA->verifyMemorySSA(); } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2308 | |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 2309 | void MemorySSAWrapperPass::print(raw_ostream &OS, const Module *M) const { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2310 | MSSA->print(OS); |
| 2311 | } |
| 2312 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2313 | MemorySSAWalker::MemorySSAWalker(MemorySSA *M) : MSSA(M) {} |
| 2314 | |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2315 | /// Walk the use-def chains starting at \p StartingAccess and find |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2316 | /// the MemoryAccess that actually clobbers Loc. |
| 2317 | /// |
| 2318 | /// \returns our clobbering memory access |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 2319 | template <typename AliasAnalysisType> |
| 2320 | MemoryAccess * |
| 2321 | MemorySSA::ClobberWalkerBase<AliasAnalysisType>::getClobberingMemoryAccessBase( |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 2322 | MemoryAccess *StartingAccess, const MemoryLocation &Loc, |
| 2323 | unsigned &UpwardWalkLimit) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2324 | if (isa<MemoryPhi>(StartingAccess)) |
| 2325 | return StartingAccess; |
| 2326 | |
| 2327 | auto *StartingUseOrDef = cast<MemoryUseOrDef>(StartingAccess); |
| 2328 | if (MSSA->isLiveOnEntryDef(StartingUseOrDef)) |
| 2329 | return StartingUseOrDef; |
| 2330 | |
| 2331 | Instruction *I = StartingUseOrDef->getMemoryInst(); |
| 2332 | |
| 2333 | // Conservatively, fences are always clobbers, so don't perform the walk if we |
| 2334 | // hit a fence. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 2335 | if (!isa<CallBase>(I) && I->isFenceLike()) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2336 | return StartingUseOrDef; |
| 2337 | |
| 2338 | UpwardsMemoryQuery Q; |
| 2339 | Q.OriginalAccess = StartingUseOrDef; |
| 2340 | Q.StartingLoc = Loc; |
George Burgess IV | 5f30897 | 2016-07-19 01:29:15 +0000 | [diff] [blame] | 2341 | Q.Inst = I; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2342 | Q.IsCall = false; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2343 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2344 | // Unlike the other function, do not walk to the def of a def, because we are |
| 2345 | // handed something we already believe is the clobbering access. |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2346 | // We never set SkipSelf to true in Q in this method. |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2347 | MemoryAccess *DefiningAccess = isa<MemoryUse>(StartingUseOrDef) |
| 2348 | ? StartingUseOrDef->getDefiningAccess() |
| 2349 | : StartingUseOrDef; |
| 2350 | |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 2351 | MemoryAccess *Clobber = |
| 2352 | Walker.findClobber(DefiningAccess, Q, UpwardWalkLimit); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2353 | LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is "); |
| 2354 | LLVM_DEBUG(dbgs() << *StartingUseOrDef << "\n"); |
| 2355 | LLVM_DEBUG(dbgs() << "Final Memory SSA clobber for " << *I << " is "); |
| 2356 | LLVM_DEBUG(dbgs() << *Clobber << "\n"); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2357 | return Clobber; |
| 2358 | } |
| 2359 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 2360 | template <typename AliasAnalysisType> |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2361 | MemoryAccess * |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 2362 | MemorySSA::ClobberWalkerBase<AliasAnalysisType>::getClobberingMemoryAccessBase( |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 2363 | MemoryAccess *MA, unsigned &UpwardWalkLimit, bool SkipSelf) { |
George Burgess IV | 400ae40 | 2016-07-20 19:51:34 +0000 | [diff] [blame] | 2364 | auto *StartingAccess = dyn_cast<MemoryUseOrDef>(MA); |
| 2365 | // If this is a MemoryPhi, we can't do anything. |
| 2366 | if (!StartingAccess) |
| 2367 | return MA; |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2368 | |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2369 | bool IsOptimized = false; |
| 2370 | |
Daniel Berlin | cd2deac | 2016-10-20 20:13:45 +0000 | [diff] [blame] | 2371 | // If this is an already optimized use or def, return the optimized result. |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 2372 | // Note: Currently, we store the optimized def result in a separate field, |
| 2373 | // since we can't use the defining access. |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2374 | if (StartingAccess->isOptimized()) { |
| 2375 | if (!SkipSelf || !isa<MemoryDef>(StartingAccess)) |
| 2376 | return StartingAccess->getOptimized(); |
| 2377 | IsOptimized = true; |
| 2378 | } |
Daniel Berlin | cd2deac | 2016-10-20 20:13:45 +0000 | [diff] [blame] | 2379 | |
George Burgess IV | 400ae40 | 2016-07-20 19:51:34 +0000 | [diff] [blame] | 2380 | const Instruction *I = StartingAccess->getMemoryInst(); |
George Burgess IV | 44477c6 | 2018-03-11 04:16:12 +0000 | [diff] [blame] | 2381 | // We can't sanely do anything with a fence, since they conservatively clobber |
| 2382 | // all memory, and have no locations to get pointers from to try to |
| 2383 | // disambiguate. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 2384 | if (!isa<CallBase>(I) && I->isFenceLike()) |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2385 | return StartingAccess; |
| 2386 | |
Alina Sbirlea | b4d088d | 2018-11-13 21:12:49 +0000 | [diff] [blame] | 2387 | UpwardsMemoryQuery Q(I, StartingAccess); |
| 2388 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 2389 | if (isUseTriviallyOptimizableToLiveOnEntry(*Walker.getAA(), I)) { |
George Burgess IV | 024f3d2 | 2016-08-03 19:57:02 +0000 | [diff] [blame] | 2390 | MemoryAccess *LiveOnEntry = MSSA->getLiveOnEntryDef(); |
George Burgess IV | 44477c6 | 2018-03-11 04:16:12 +0000 | [diff] [blame] | 2391 | StartingAccess->setOptimized(LiveOnEntry); |
| 2392 | StartingAccess->setOptimizedAccessType(None); |
George Burgess IV | 024f3d2 | 2016-08-03 19:57:02 +0000 | [diff] [blame] | 2393 | return LiveOnEntry; |
| 2394 | } |
| 2395 | |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2396 | MemoryAccess *OptimizedAccess; |
| 2397 | if (!IsOptimized) { |
| 2398 | // Start with the thing we already think clobbers this location |
| 2399 | MemoryAccess *DefiningAccess = StartingAccess->getDefiningAccess(); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2400 | |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2401 | // At this point, DefiningAccess may be the live on entry def. |
| 2402 | // If it is, we will not get a better result. |
| 2403 | if (MSSA->isLiveOnEntryDef(DefiningAccess)) { |
| 2404 | StartingAccess->setOptimized(DefiningAccess); |
| 2405 | StartingAccess->setOptimizedAccessType(None); |
| 2406 | return DefiningAccess; |
| 2407 | } |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2408 | |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 2409 | OptimizedAccess = Walker.findClobber(DefiningAccess, Q, UpwardWalkLimit); |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2410 | StartingAccess->setOptimized(OptimizedAccess); |
| 2411 | if (MSSA->isLiveOnEntryDef(OptimizedAccess)) |
| 2412 | StartingAccess->setOptimizedAccessType(None); |
| 2413 | else if (Q.AR == MustAlias) |
| 2414 | StartingAccess->setOptimizedAccessType(MustAlias); |
| 2415 | } else |
| 2416 | OptimizedAccess = StartingAccess->getOptimized(); |
| 2417 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2418 | LLVM_DEBUG(dbgs() << "Starting Memory SSA clobber for " << *I << " is "); |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2419 | LLVM_DEBUG(dbgs() << *StartingAccess << "\n"); |
| 2420 | LLVM_DEBUG(dbgs() << "Optimized Memory SSA clobber for " << *I << " is "); |
| 2421 | LLVM_DEBUG(dbgs() << *OptimizedAccess << "\n"); |
Alina Sbirlea | d90c9f4 | 2018-03-08 18:03:14 +0000 | [diff] [blame] | 2422 | |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2423 | MemoryAccess *Result; |
| 2424 | if (SkipSelf && isa<MemoryPhi>(OptimizedAccess) && |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 2425 | isa<MemoryDef>(StartingAccess) && UpwardWalkLimit) { |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2426 | assert(isa<MemoryDef>(Q.OriginalAccess)); |
| 2427 | Q.SkipSelfAccess = true; |
Alina Sbirlea | f085cc5 | 2019-03-29 21:56:09 +0000 | [diff] [blame] | 2428 | Result = Walker.findClobber(OptimizedAccess, Q, UpwardWalkLimit); |
Alina Sbirlea | bc8aa24 | 2019-01-07 19:22:37 +0000 | [diff] [blame] | 2429 | } else |
| 2430 | Result = OptimizedAccess; |
| 2431 | |
| 2432 | LLVM_DEBUG(dbgs() << "Result Memory SSA clobber [SkipSelf = " << SkipSelf); |
| 2433 | LLVM_DEBUG(dbgs() << "] for " << *I << " is " << *Result << "\n"); |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2434 | |
| 2435 | return Result; |
| 2436 | } |
| 2437 | |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2438 | MemoryAccess * |
George Burgess IV | 400ae40 | 2016-07-20 19:51:34 +0000 | [diff] [blame] | 2439 | DoNothingMemorySSAWalker::getClobberingMemoryAccess(MemoryAccess *MA) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2440 | if (auto *Use = dyn_cast<MemoryUseOrDef>(MA)) |
| 2441 | return Use->getDefiningAccess(); |
| 2442 | return MA; |
| 2443 | } |
| 2444 | |
| 2445 | MemoryAccess *DoNothingMemorySSAWalker::getClobberingMemoryAccess( |
George Burgess IV | 013fd73 | 2016-10-28 19:22:46 +0000 | [diff] [blame] | 2446 | MemoryAccess *StartingAccess, const MemoryLocation &) { |
George Burgess IV | e1100f5 | 2016-02-02 22:46:49 +0000 | [diff] [blame] | 2447 | if (auto *Use = dyn_cast<MemoryUseOrDef>(StartingAccess)) |
| 2448 | return Use->getDefiningAccess(); |
| 2449 | return StartingAccess; |
| 2450 | } |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 2451 | |
| 2452 | void MemoryPhi::deleteMe(DerivedUser *Self) { |
| 2453 | delete static_cast<MemoryPhi *>(Self); |
| 2454 | } |
| 2455 | |
| 2456 | void MemoryDef::deleteMe(DerivedUser *Self) { |
| 2457 | delete static_cast<MemoryDef *>(Self); |
| 2458 | } |
| 2459 | |
| 2460 | void MemoryUse::deleteMe(DerivedUser *Self) { |
| 2461 | delete static_cast<MemoryUse *>(Self); |
| 2462 | } |