Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1 | //===- RewriteStatepointsForGC.cpp - Make GC relocations explicit ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Rewrite an existing set of gc.statepoints such that they make potential |
| 11 | // relocations performed by the garbage collector explicit in the IR. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Pass.h" |
| 16 | #include "llvm/Analysis/CFG.h" |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetTransformInfo.h" |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SetOperations.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
| 21 | #include "llvm/ADT/DenseSet.h" |
Philip Reames | 4d80ede | 2015-04-10 23:11:26 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SetVector.h" |
Swaroop Sridhar | 665bc9c | 2015-05-20 01:07:23 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/MapVector.h" |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 25 | #include "llvm/IR/BasicBlock.h" |
| 26 | #include "llvm/IR/CallSite.h" |
| 27 | #include "llvm/IR/Dominators.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/IRBuilder.h" |
| 30 | #include "llvm/IR/InstIterator.h" |
| 31 | #include "llvm/IR/Instructions.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/Module.h" |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 35 | #include "llvm/IR/MDBuilder.h" |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Statepoint.h" |
| 37 | #include "llvm/IR/Value.h" |
| 38 | #include "llvm/IR/Verifier.h" |
| 39 | #include "llvm/Support/Debug.h" |
| 40 | #include "llvm/Support/CommandLine.h" |
| 41 | #include "llvm/Transforms/Scalar.h" |
| 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 43 | #include "llvm/Transforms/Utils/Cloning.h" |
| 44 | #include "llvm/Transforms/Utils/Local.h" |
| 45 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 46 | |
| 47 | #define DEBUG_TYPE "rewrite-statepoints-for-gc" |
| 48 | |
| 49 | using namespace llvm; |
| 50 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 51 | // Print the liveset found at the insert location |
| 52 | static cl::opt<bool> PrintLiveSet("spp-print-liveset", cl::Hidden, |
| 53 | cl::init(false)); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 54 | static cl::opt<bool> PrintLiveSetSize("spp-print-liveset-size", cl::Hidden, |
| 55 | cl::init(false)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 56 | // Print out the base pointers for debugging |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 57 | static cl::opt<bool> PrintBasePointers("spp-print-base-pointers", cl::Hidden, |
| 58 | cl::init(false)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 59 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 60 | // Cost threshold measuring when it is profitable to rematerialize value instead |
| 61 | // of relocating it |
| 62 | static cl::opt<unsigned> |
| 63 | RematerializationThreshold("spp-rematerialization-threshold", cl::Hidden, |
| 64 | cl::init(6)); |
| 65 | |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 66 | #ifdef XDEBUG |
| 67 | static bool ClobberNonLive = true; |
| 68 | #else |
| 69 | static bool ClobberNonLive = false; |
| 70 | #endif |
| 71 | static cl::opt<bool, true> ClobberNonLiveOverride("rs4gc-clobber-non-live", |
| 72 | cl::location(ClobberNonLive), |
| 73 | cl::Hidden); |
| 74 | |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 75 | namespace { |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 76 | struct RewriteStatepointsForGC : public ModulePass { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 77 | static char ID; // Pass identification, replacement for typeid |
| 78 | |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 79 | RewriteStatepointsForGC() : ModulePass(ID) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 80 | initializeRewriteStatepointsForGCPass(*PassRegistry::getPassRegistry()); |
| 81 | } |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 82 | bool runOnFunction(Function &F); |
| 83 | bool runOnModule(Module &M) override { |
| 84 | bool Changed = false; |
| 85 | for (Function &F : M) |
| 86 | Changed |= runOnFunction(F); |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 87 | |
| 88 | if (Changed) { |
| 89 | // stripDereferenceabilityInfo asserts that shouldRewriteStatepointsIn |
| 90 | // returns true for at least one function in the module. Since at least |
| 91 | // one function changed, we know that the precondition is satisfied. |
| 92 | stripDereferenceabilityInfo(M); |
| 93 | } |
| 94 | |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 95 | return Changed; |
| 96 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 97 | |
| 98 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 99 | // We add and rewrite a bunch of instructions, but don't really do much |
| 100 | // else. We could in theory preserve a lot more analyses here. |
| 101 | AU.addRequired<DominatorTreeWrapperPass>(); |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 102 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 103 | } |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 104 | |
| 105 | /// The IR fed into RewriteStatepointsForGC may have had attributes implying |
| 106 | /// dereferenceability that are no longer valid/correct after |
| 107 | /// RewriteStatepointsForGC has run. This is because semantically, after |
| 108 | /// RewriteStatepointsForGC runs, all calls to gc.statepoint "free" the entire |
| 109 | /// heap. stripDereferenceabilityInfo (conservatively) restores correctness |
| 110 | /// by erasing all attributes in the module that externally imply |
| 111 | /// dereferenceability. |
| 112 | /// |
| 113 | void stripDereferenceabilityInfo(Module &M); |
| 114 | |
| 115 | // Helpers for stripDereferenceabilityInfo |
| 116 | void stripDereferenceabilityInfoFromBody(Function &F); |
| 117 | void stripDereferenceabilityInfoFromPrototype(Function &F); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 118 | }; |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 119 | } // namespace |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 120 | |
| 121 | char RewriteStatepointsForGC::ID = 0; |
| 122 | |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 123 | ModulePass *llvm::createRewriteStatepointsForGCPass() { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 124 | return new RewriteStatepointsForGC(); |
| 125 | } |
| 126 | |
| 127 | INITIALIZE_PASS_BEGIN(RewriteStatepointsForGC, "rewrite-statepoints-for-gc", |
| 128 | "Make relocations explicit at statepoints", false, false) |
| 129 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 130 | INITIALIZE_PASS_END(RewriteStatepointsForGC, "rewrite-statepoints-for-gc", |
| 131 | "Make relocations explicit at statepoints", false, false) |
| 132 | |
| 133 | namespace { |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 134 | struct GCPtrLivenessData { |
| 135 | /// Values defined in this block. |
| 136 | DenseMap<BasicBlock *, DenseSet<Value *>> KillSet; |
| 137 | /// Values used in this block (and thus live); does not included values |
| 138 | /// killed within this block. |
| 139 | DenseMap<BasicBlock *, DenseSet<Value *>> LiveSet; |
| 140 | |
| 141 | /// Values live into this basic block (i.e. used by any |
| 142 | /// instruction in this basic block or ones reachable from here) |
| 143 | DenseMap<BasicBlock *, DenseSet<Value *>> LiveIn; |
| 144 | |
| 145 | /// Values live out of this basic block (i.e. live into |
| 146 | /// any successor block) |
| 147 | DenseMap<BasicBlock *, DenseSet<Value *>> LiveOut; |
| 148 | }; |
| 149 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 150 | // The type of the internal cache used inside the findBasePointers family |
| 151 | // of functions. From the callers perspective, this is an opaque type and |
| 152 | // should not be inspected. |
| 153 | // |
| 154 | // In the actual implementation this caches two relations: |
| 155 | // - The base relation itself (i.e. this pointer is based on that one) |
| 156 | // - The base defining value relation (i.e. before base_phi insertion) |
| 157 | // Generally, after the execution of a full findBasePointer call, only the |
| 158 | // base relation will remain. Internally, we add a mixture of the two |
| 159 | // types, then update all the second type to the first type |
Philip Reames | e9c3b9b | 2015-02-20 22:48:20 +0000 | [diff] [blame] | 160 | typedef DenseMap<Value *, Value *> DefiningValueMapTy; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 161 | typedef DenseSet<Value *> StatepointLiveSetTy; |
Sanjoy Das | 40bdd04 | 2015-10-07 21:32:35 +0000 | [diff] [blame] | 162 | typedef DenseMap<AssertingVH<Instruction>, AssertingVH<Value>> |
| 163 | RematerializedValueMapTy; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 164 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 165 | struct PartiallyConstructedSafepointRecord { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 166 | /// The set of values known to be live across this safepoint |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 167 | StatepointLiveSetTy LiveSet; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 168 | |
| 169 | /// Mapping from live pointers to a base-defining-value |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 170 | DenseMap<Value *, Value *> PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 171 | |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 172 | /// The *new* gc.statepoint instruction itself. This produces the token |
| 173 | /// that normal path gc.relocates and the gc.result are tied to. |
| 174 | Instruction *StatepointToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 175 | |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 176 | /// Instruction to which exceptional gc relocates are attached |
| 177 | /// Makes it easier to iterate through them during relocationViaAlloca. |
| 178 | Instruction *UnwindToken; |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 179 | |
| 180 | /// Record live values we are rematerialized instead of relocating. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 181 | /// They are not included into 'LiveSet' field. |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 182 | /// Maps rematerialized copy to it's original value. |
| 183 | RematerializedValueMapTy RematerializedValues; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 184 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 185 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 186 | |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 187 | /// Compute the live-in set for every basic block in the function |
| 188 | static void computeLiveInValues(DominatorTree &DT, Function &F, |
| 189 | GCPtrLivenessData &Data); |
| 190 | |
| 191 | /// Given results from the dataflow liveness computation, find the set of live |
| 192 | /// Values at a particular instruction. |
| 193 | static void findLiveSetAtInst(Instruction *inst, GCPtrLivenessData &Data, |
| 194 | StatepointLiveSetTy &out); |
| 195 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 196 | // TODO: Once we can get to the GCStrategy, this becomes |
| 197 | // Optional<bool> isGCManagedPointer(const Value *V) const override { |
| 198 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 199 | static bool isGCPointerType(Type *T) { |
| 200 | if (auto *PT = dyn_cast<PointerType>(T)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 201 | // For the sake of this example GC, we arbitrarily pick addrspace(1) as our |
| 202 | // GC managed heap. We know that a pointer into this heap needs to be |
| 203 | // updated and that no other pointer does. |
| 204 | return (1 == PT->getAddressSpace()); |
| 205 | return false; |
| 206 | } |
| 207 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 208 | // Return true if this type is one which a) is a gc pointer or contains a GC |
| 209 | // pointer and b) is of a type this code expects to encounter as a live value. |
| 210 | // (The insertion code will assert that a type which matches (a) and not (b) |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 211 | // is not encountered.) |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 212 | static bool isHandledGCPointerType(Type *T) { |
| 213 | // We fully support gc pointers |
| 214 | if (isGCPointerType(T)) |
| 215 | return true; |
| 216 | // We partially support vectors of gc pointers. The code will assert if it |
| 217 | // can't handle something. |
| 218 | if (auto VT = dyn_cast<VectorType>(T)) |
| 219 | if (isGCPointerType(VT->getElementType())) |
| 220 | return true; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | #ifndef NDEBUG |
| 225 | /// Returns true if this type contains a gc pointer whether we know how to |
| 226 | /// handle that type or not. |
| 227 | static bool containsGCPtrType(Type *Ty) { |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 228 | if (isGCPointerType(Ty)) |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 229 | return true; |
| 230 | if (VectorType *VT = dyn_cast<VectorType>(Ty)) |
| 231 | return isGCPointerType(VT->getScalarType()); |
| 232 | if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) |
| 233 | return containsGCPtrType(AT->getElementType()); |
| 234 | if (StructType *ST = dyn_cast<StructType>(Ty)) |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 235 | return std::any_of( |
| 236 | ST->subtypes().begin(), ST->subtypes().end(), |
| 237 | [](Type *SubType) { return containsGCPtrType(SubType); }); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // Returns true if this is a type which a) is a gc pointer or contains a GC |
| 242 | // pointer and b) is of a type which the code doesn't expect (i.e. first class |
| 243 | // aggregates). Used to trip assertions. |
| 244 | static bool isUnhandledGCPointerType(Type *Ty) { |
| 245 | return containsGCPtrType(Ty) && !isHandledGCPointerType(Ty); |
| 246 | } |
| 247 | #endif |
| 248 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 249 | static bool order_by_name(Value *a, Value *b) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 250 | if (a->hasName() && b->hasName()) { |
| 251 | return -1 == a->getName().compare(b->getName()); |
| 252 | } else if (a->hasName() && !b->hasName()) { |
| 253 | return true; |
| 254 | } else if (!a->hasName() && b->hasName()) { |
| 255 | return false; |
| 256 | } else { |
| 257 | // Better than nothing, but not stable |
| 258 | return a < b; |
| 259 | } |
| 260 | } |
| 261 | |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 262 | // Return the name of the value suffixed with the provided value, or if the |
| 263 | // value didn't have a name, the default value specified. |
| 264 | static std::string suffixed_name_or(Value *V, StringRef Suffix, |
| 265 | StringRef DefaultName) { |
| 266 | return V->hasName() ? (V->getName() + Suffix).str() : DefaultName.str(); |
| 267 | } |
| 268 | |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 269 | // Conservatively identifies any definitions which might be live at the |
| 270 | // given instruction. The analysis is performed immediately before the |
| 271 | // given instruction. Values defined by that instruction are not considered |
| 272 | // live. Values used by that instruction are considered live. |
| 273 | static void analyzeParsePointLiveness( |
| 274 | DominatorTree &DT, GCPtrLivenessData &OriginalLivenessData, |
| 275 | const CallSite &CS, PartiallyConstructedSafepointRecord &result) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 276 | Instruction *inst = CS.getInstruction(); |
| 277 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 278 | StatepointLiveSetTy LiveSet; |
| 279 | findLiveSetAtInst(inst, OriginalLivenessData, LiveSet); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 280 | |
| 281 | if (PrintLiveSet) { |
| 282 | // Note: This output is used by several of the test cases |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 283 | // The order of elements in a set is not stable, put them in a vec and sort |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 284 | // by name |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 285 | SmallVector<Value *, 64> Temp; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 286 | Temp.insert(Temp.end(), LiveSet.begin(), LiveSet.end()); |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 287 | std::sort(Temp.begin(), Temp.end(), order_by_name); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 288 | errs() << "Live Variables:\n"; |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 289 | for (Value *V : Temp) |
| 290 | dbgs() << " " << V->getName() << " " << *V << "\n"; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 291 | } |
| 292 | if (PrintLiveSetSize) { |
| 293 | errs() << "Safepoint For: " << CS.getCalledValue()->getName() << "\n"; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 294 | errs() << "Number live values: " << LiveSet.size() << "\n"; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 295 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 296 | result.LiveSet = LiveSet; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 299 | static bool isKnownBaseResult(Value *V); |
| 300 | namespace { |
| 301 | /// A single base defining value - An immediate base defining value for an |
| 302 | /// instruction 'Def' is an input to 'Def' whose base is also a base of 'Def'. |
| 303 | /// For instructions which have multiple pointer [vector] inputs or that |
| 304 | /// transition between vector and scalar types, there is no immediate base |
| 305 | /// defining value. The 'base defining value' for 'Def' is the transitive |
| 306 | /// closure of this relation stopping at the first instruction which has no |
| 307 | /// immediate base defining value. The b.d.v. might itself be a base pointer, |
| 308 | /// but it can also be an arbitrary derived pointer. |
| 309 | struct BaseDefiningValueResult { |
| 310 | /// Contains the value which is the base defining value. |
| 311 | Value * const BDV; |
| 312 | /// True if the base defining value is also known to be an actual base |
| 313 | /// pointer. |
| 314 | const bool IsKnownBase; |
| 315 | BaseDefiningValueResult(Value *BDV, bool IsKnownBase) |
| 316 | : BDV(BDV), IsKnownBase(IsKnownBase) { |
| 317 | #ifndef NDEBUG |
| 318 | // Check consistency between new and old means of checking whether a BDV is |
| 319 | // a base. |
| 320 | bool MustBeBase = isKnownBaseResult(BDV); |
| 321 | assert(!MustBeBase || MustBeBase == IsKnownBase); |
| 322 | #endif |
| 323 | } |
| 324 | }; |
| 325 | } |
| 326 | |
| 327 | static BaseDefiningValueResult findBaseDefiningValue(Value *I); |
Philip Reames | 311f710 | 2015-05-12 22:19:52 +0000 | [diff] [blame] | 328 | |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 329 | /// Return a base defining value for the 'Index' element of the given vector |
| 330 | /// instruction 'I'. If Index is null, returns a BDV for the entire vector |
| 331 | /// 'I'. As an optimization, this method will try to determine when the |
| 332 | /// element is known to already be a base pointer. If this can be established, |
| 333 | /// the second value in the returned pair will be true. Note that either a |
| 334 | /// vector or a pointer typed value can be returned. For the former, the |
| 335 | /// vector returned is a BDV (and possibly a base) of the entire vector 'I'. |
| 336 | /// If the later, the return pointer is a BDV (or possibly a base) for the |
| 337 | /// particular element in 'I'. |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 338 | static BaseDefiningValueResult |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 339 | findBaseDefiningValueOfVector(Value *I) { |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 340 | assert(I->getType()->isVectorTy() && |
| 341 | cast<VectorType>(I->getType())->getElementType()->isPointerTy() && |
| 342 | "Illegal to ask for the base pointer of a non-pointer type"); |
| 343 | |
| 344 | // Each case parallels findBaseDefiningValue below, see that code for |
| 345 | // detailed motivation. |
| 346 | |
| 347 | if (isa<Argument>(I)) |
| 348 | // An incoming argument to the function is a base pointer |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 349 | return BaseDefiningValueResult(I, true); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 350 | |
| 351 | // We shouldn't see the address of a global as a vector value? |
| 352 | assert(!isa<GlobalVariable>(I) && |
| 353 | "unexpected global variable found in base of vector"); |
| 354 | |
| 355 | // inlining could possibly introduce phi node that contains |
| 356 | // undef if callee has multiple returns |
| 357 | if (isa<UndefValue>(I)) |
| 358 | // utterly meaningless, but useful for dealing with partially optimized |
| 359 | // code. |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 360 | return BaseDefiningValueResult(I, true); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 361 | |
| 362 | // Due to inheritance, this must be _after_ the global variable and undef |
| 363 | // checks |
| 364 | if (Constant *Con = dyn_cast<Constant>(I)) { |
| 365 | assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) && |
| 366 | "order of checks wrong!"); |
| 367 | assert(Con->isNullValue() && "null is the only case which makes sense"); |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 368 | return BaseDefiningValueResult(Con, true); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 369 | } |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 370 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 371 | if (isa<LoadInst>(I)) |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 372 | return BaseDefiningValueResult(I, true); |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 373 | |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 374 | if (isa<InsertElementInst>(I)) |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 375 | // We don't know whether this vector contains entirely base pointers or |
| 376 | // not. To be conservatively correct, we treat it as a BDV and will |
| 377 | // duplicate code as needed to construct a parallel vector of bases. |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 378 | return BaseDefiningValueResult(I, false); |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 379 | |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 380 | if (isa<ShuffleVectorInst>(I)) |
| 381 | // We don't know whether this vector contains entirely base pointers or |
| 382 | // not. To be conservatively correct, we treat it as a BDV and will |
| 383 | // duplicate code as needed to construct a parallel vector of bases. |
| 384 | // TODO: There a number of local optimizations which could be applied here |
| 385 | // for particular sufflevector patterns. |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 386 | return BaseDefiningValueResult(I, false); |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 387 | |
| 388 | // A PHI or Select is a base defining value. The outer findBasePointer |
| 389 | // algorithm is responsible for constructing a base value for this BDV. |
| 390 | assert((isa<SelectInst>(I) || isa<PHINode>(I)) && |
| 391 | "unknown vector instruction - no base found for vector element"); |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 392 | return BaseDefiningValueResult(I, false); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 395 | /// Helper function for findBasePointer - Will return a value which either a) |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 396 | /// defines the base pointer for the input, b) blocks the simple search |
| 397 | /// (i.e. a PHI or Select of two derived pointers), or c) involves a change |
| 398 | /// from pointer to vector type or back. |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 399 | static BaseDefiningValueResult findBaseDefiningValue(Value *I) { |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 400 | if (I->getType()->isVectorTy()) |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 401 | return findBaseDefiningValueOfVector(I); |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 402 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 403 | assert(I->getType()->isPointerTy() && |
| 404 | "Illegal to ask for the base pointer of a non-pointer type"); |
| 405 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 406 | if (isa<Argument>(I)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 407 | // An incoming argument to the function is a base pointer |
| 408 | // We should have never reached here if this argument isn't an gc value |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 409 | return BaseDefiningValueResult(I, true); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 410 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 411 | if (isa<GlobalVariable>(I)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 412 | // base case |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 413 | return BaseDefiningValueResult(I, true); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 414 | |
| 415 | // inlining could possibly introduce phi node that contains |
| 416 | // undef if callee has multiple returns |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 417 | if (isa<UndefValue>(I)) |
| 418 | // utterly meaningless, but useful for dealing with |
| 419 | // partially optimized code. |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 420 | return BaseDefiningValueResult(I, true); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 421 | |
| 422 | // Due to inheritance, this must be _after_ the global variable and undef |
| 423 | // checks |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 424 | if (isa<Constant>(I)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 425 | assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) && |
| 426 | "order of checks wrong!"); |
| 427 | // Note: Finding a constant base for something marked for relocation |
| 428 | // doesn't really make sense. The most likely case is either a) some |
| 429 | // screwed up the address space usage or b) your validating against |
| 430 | // compiled C++ code w/o the proper separation. The only real exception |
| 431 | // is a null pointer. You could have generic code written to index of |
| 432 | // off a potentially null value and have proven it null. We also use |
| 433 | // null pointers in dead paths of relocation phis (which we might later |
| 434 | // want to find a base pointer for). |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 435 | assert(isa<ConstantPointerNull>(I) && |
Philip Reames | 24c6cd5 | 2015-03-27 05:47:00 +0000 | [diff] [blame] | 436 | "null is the only case which makes sense"); |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 437 | return BaseDefiningValueResult(I, true); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 441 | Value *Def = CI->stripPointerCasts(); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 442 | // If we find a cast instruction here, it means we've found a cast which is |
| 443 | // not simply a pointer cast (i.e. an inttoptr). We don't know how to |
| 444 | // handle int->ptr conversion. |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 445 | assert(!isa<CastInst>(Def) && "shouldn't find another cast here"); |
| 446 | return findBaseDefiningValue(Def); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 449 | if (isa<LoadInst>(I)) |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 450 | // The value loaded is an gc base itself |
| 451 | return BaseDefiningValueResult(I, true); |
| 452 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 453 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 454 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) |
| 455 | // The base of this GEP is the base |
| 456 | return findBaseDefiningValue(GEP->getPointerOperand()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 457 | |
| 458 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 459 | switch (II->getIntrinsicID()) { |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 460 | case Intrinsic::experimental_gc_result_ptr: |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 461 | default: |
| 462 | // fall through to general call handling |
| 463 | break; |
| 464 | case Intrinsic::experimental_gc_statepoint: |
| 465 | case Intrinsic::experimental_gc_result_float: |
| 466 | case Intrinsic::experimental_gc_result_int: |
| 467 | llvm_unreachable("these don't produce pointers"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 468 | case Intrinsic::experimental_gc_relocate: { |
| 469 | // Rerunning safepoint insertion after safepoints are already |
| 470 | // inserted is not supported. It could probably be made to work, |
| 471 | // but why are you doing this? There's no good reason. |
| 472 | llvm_unreachable("repeat safepoint insertion is not supported"); |
| 473 | } |
| 474 | case Intrinsic::gcroot: |
| 475 | // Currently, this mechanism hasn't been extended to work with gcroot. |
| 476 | // There's no reason it couldn't be, but I haven't thought about the |
| 477 | // implications much. |
| 478 | llvm_unreachable( |
| 479 | "interaction with the gcroot mechanism is not supported"); |
| 480 | } |
| 481 | } |
| 482 | // We assume that functions in the source language only return base |
| 483 | // pointers. This should probably be generalized via attributes to support |
| 484 | // both source language and internal functions. |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 485 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 486 | return BaseDefiningValueResult(I, true); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 487 | |
| 488 | // I have absolutely no idea how to implement this part yet. It's not |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 489 | // necessarily hard, I just haven't really looked at it yet. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 490 | assert(!isa<LandingPadInst>(I) && "Landing Pad is unimplemented"); |
| 491 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 492 | if (isa<AtomicCmpXchgInst>(I)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 493 | // A CAS is effectively a atomic store and load combined under a |
| 494 | // predicate. From the perspective of base pointers, we just treat it |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 495 | // like a load. |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 496 | return BaseDefiningValueResult(I, true); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 497 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 498 | assert(!isa<AtomicRMWInst>(I) && "Xchg handled above, all others are " |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 499 | "binary ops which don't apply to pointers"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 500 | |
| 501 | // The aggregate ops. Aggregates can either be in the heap or on the |
| 502 | // stack, but in either case, this is simply a field load. As a result, |
| 503 | // this is a defining definition of the base just like a load is. |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 504 | if (isa<ExtractValueInst>(I)) |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 505 | return BaseDefiningValueResult(I, true); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 506 | |
| 507 | // We should never see an insert vector since that would require we be |
| 508 | // tracing back a struct value not a pointer value. |
| 509 | assert(!isa<InsertValueInst>(I) && |
| 510 | "Base pointer for a struct is meaningless"); |
| 511 | |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 512 | // An extractelement produces a base result exactly when it's input does. |
| 513 | // We may need to insert a parallel instruction to extract the appropriate |
| 514 | // element out of the base vector corresponding to the input. Given this, |
| 515 | // it's analogous to the phi and select case even though it's not a merge. |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 516 | if (isa<ExtractElementInst>(I)) |
| 517 | // Note: There a lot of obvious peephole cases here. This are deliberately |
| 518 | // handled after the main base pointer inference algorithm to make writing |
| 519 | // test cases to exercise that code easier. |
| 520 | return BaseDefiningValueResult(I, false); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 521 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 522 | // The last two cases here don't return a base pointer. Instead, they |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 523 | // return a value which dynamically selects from among several base |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 524 | // derived pointers (each with it's own base potentially). It's the job of |
| 525 | // the caller to resolve these. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 526 | assert((isa<SelectInst>(I) || isa<PHINode>(I)) && |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 527 | "missing instruction case in findBaseDefiningValing"); |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 528 | return BaseDefiningValueResult(I, false); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /// Returns the base defining value for this value. |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 532 | static Value *findBaseDefiningValueCached(Value *I, DefiningValueMapTy &Cache) { |
| 533 | Value *&Cached = Cache[I]; |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 534 | if (!Cached) { |
Philip Reames | f5b8e47 | 2015-09-03 21:34:30 +0000 | [diff] [blame] | 535 | Cached = findBaseDefiningValue(I).BDV; |
Philip Reames | 2a892a6 | 2015-07-23 22:25:26 +0000 | [diff] [blame] | 536 | DEBUG(dbgs() << "fBDV-cached: " << I->getName() << " -> " |
| 537 | << Cached->getName() << "\n"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 538 | } |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 539 | assert(Cache[I] != nullptr); |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 540 | return Cached; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /// Return a base pointer for this value if known. Otherwise, return it's |
| 544 | /// base defining value. |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 545 | static Value *findBaseOrBDV(Value *I, DefiningValueMapTy &Cache) { |
| 546 | Value *Def = findBaseDefiningValueCached(I, Cache); |
| 547 | auto Found = Cache.find(Def); |
| 548 | if (Found != Cache.end()) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 549 | // Either a base-of relation, or a self reference. Caller must check. |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 550 | return Found->second; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 551 | } |
| 552 | // Only a BDV available |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 553 | return Def; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /// Given the result of a call to findBaseDefiningValue, or findBaseOrBDV, |
| 557 | /// is it known to be a base pointer? Or do we need to continue searching. |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 558 | static bool isKnownBaseResult(Value *V) { |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 559 | if (!isa<PHINode>(V) && !isa<SelectInst>(V) && |
| 560 | !isa<ExtractElementInst>(V) && !isa<InsertElementInst>(V) && |
| 561 | !isa<ShuffleVectorInst>(V)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 562 | // no recursion possible |
| 563 | return true; |
| 564 | } |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 565 | if (isa<Instruction>(V) && |
| 566 | cast<Instruction>(V)->getMetadata("is_base_value")) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 567 | // This is a previously inserted base phi or select. We know |
| 568 | // that this is a base value. |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | // We need to keep searching |
| 573 | return false; |
| 574 | } |
| 575 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 576 | namespace { |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 577 | /// Models the state of a single base defining value in the findBasePointer |
| 578 | /// algorithm for determining where a new instruction is needed to propagate |
| 579 | /// the base of this BDV. |
| 580 | class BDVState { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 581 | public: |
| 582 | enum Status { Unknown, Base, Conflict }; |
| 583 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 584 | BDVState(Status s, Value *b = nullptr) : status(s), base(b) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 585 | assert(status != Base || b); |
| 586 | } |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 587 | explicit BDVState(Value *b) : status(Base), base(b) {} |
| 588 | BDVState() : status(Unknown), base(nullptr) {} |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 589 | |
| 590 | Status getStatus() const { return status; } |
| 591 | Value *getBase() const { return base; } |
| 592 | |
| 593 | bool isBase() const { return getStatus() == Base; } |
| 594 | bool isUnknown() const { return getStatus() == Unknown; } |
| 595 | bool isConflict() const { return getStatus() == Conflict; } |
| 596 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 597 | bool operator==(const BDVState &other) const { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 598 | return base == other.base && status == other.status; |
| 599 | } |
| 600 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 601 | bool operator!=(const BDVState &other) const { return !(*this == other); } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 602 | |
Philip Reames | 2a892a6 | 2015-07-23 22:25:26 +0000 | [diff] [blame] | 603 | LLVM_DUMP_METHOD |
| 604 | void dump() const { print(dbgs()); dbgs() << '\n'; } |
| 605 | |
| 606 | void print(raw_ostream &OS) const { |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 607 | switch (status) { |
| 608 | case Unknown: |
| 609 | OS << "U"; |
| 610 | break; |
| 611 | case Base: |
| 612 | OS << "B"; |
| 613 | break; |
| 614 | case Conflict: |
| 615 | OS << "C"; |
| 616 | break; |
| 617 | }; |
| 618 | OS << " (" << base << " - " |
Philip Reames | 2a892a6 | 2015-07-23 22:25:26 +0000 | [diff] [blame] | 619 | << (base ? base->getName() : "nullptr") << "): "; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | private: |
| 623 | Status status; |
| 624 | Value *base; // non null only if status == base |
| 625 | }; |
Philip Reames | b3967cd | 2015-09-02 22:30:53 +0000 | [diff] [blame] | 626 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 627 | |
Philip Reames | 6906e92 | 2015-09-02 21:57:17 +0000 | [diff] [blame] | 628 | #ifndef NDEBUG |
Philip Reames | b3967cd | 2015-09-02 22:30:53 +0000 | [diff] [blame] | 629 | static raw_ostream &operator<<(raw_ostream &OS, const BDVState &State) { |
Philip Reames | 2a892a6 | 2015-07-23 22:25:26 +0000 | [diff] [blame] | 630 | State.print(OS); |
| 631 | return OS; |
| 632 | } |
Philip Reames | 6906e92 | 2015-09-02 21:57:17 +0000 | [diff] [blame] | 633 | #endif |
Philip Reames | 2a892a6 | 2015-07-23 22:25:26 +0000 | [diff] [blame] | 634 | |
Philip Reames | b3967cd | 2015-09-02 22:30:53 +0000 | [diff] [blame] | 635 | namespace { |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 636 | // Values of type BDVState form a lattice, and this is a helper |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 637 | // class that implementes the meet operation. The meat of the meet |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 638 | // operation is implemented in MeetBDVStates::pureMeet |
| 639 | class MeetBDVStates { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 640 | public: |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 641 | /// Initializes the currentResult to the TOP state so that if can be met with |
| 642 | /// any other state to produce that state. |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 643 | MeetBDVStates() {} |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 644 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 645 | // Destructively meet the current result with the given BDVState |
| 646 | void meetWith(BDVState otherState) { |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 647 | currentResult = meet(otherState, currentResult); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 650 | BDVState getResult() const { return currentResult; } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 651 | |
| 652 | private: |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 653 | BDVState currentResult; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 654 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 655 | /// Perform a meet operation on two elements of the BDVState lattice. |
| 656 | static BDVState meet(BDVState LHS, BDVState RHS) { |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 657 | assert((pureMeet(LHS, RHS) == pureMeet(RHS, LHS)) && |
| 658 | "math is wrong: meet does not commute!"); |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 659 | BDVState Result = pureMeet(LHS, RHS); |
Philip Reames | 2a892a6 | 2015-07-23 22:25:26 +0000 | [diff] [blame] | 660 | DEBUG(dbgs() << "meet of " << LHS << " with " << RHS |
| 661 | << " produced " << Result << "\n"); |
| 662 | return Result; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 665 | static BDVState pureMeet(const BDVState &stateA, const BDVState &stateB) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 666 | switch (stateA.getStatus()) { |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 667 | case BDVState::Unknown: |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 668 | return stateB; |
| 669 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 670 | case BDVState::Base: |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 671 | assert(stateA.getBase() && "can't be null"); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 672 | if (stateB.isUnknown()) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 673 | return stateA; |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 674 | |
| 675 | if (stateB.isBase()) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 676 | if (stateA.getBase() == stateB.getBase()) { |
| 677 | assert(stateA == stateB && "equality broken!"); |
| 678 | return stateA; |
| 679 | } |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 680 | return BDVState(BDVState::Conflict); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 681 | } |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 682 | assert(stateB.isConflict() && "only three states!"); |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 683 | return BDVState(BDVState::Conflict); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 684 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 685 | case BDVState::Conflict: |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 686 | return stateA; |
| 687 | } |
Reid Kleckner | a070ee5 | 2015-02-20 19:46:02 +0000 | [diff] [blame] | 688 | llvm_unreachable("only three states!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 689 | } |
| 690 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 691 | } |
Philip Reames | b3967cd | 2015-09-02 22:30:53 +0000 | [diff] [blame] | 692 | |
| 693 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 694 | /// For a given value or instruction, figure out what base ptr it's derived |
| 695 | /// from. For gc objects, this is simply itself. On success, returns a value |
| 696 | /// which is the base pointer. (This is reliable and can be used for |
| 697 | /// relocation.) On failure, returns nullptr. |
Philip Reames | ba19849 | 2015-04-14 00:41:34 +0000 | [diff] [blame] | 698 | static Value *findBasePointer(Value *I, DefiningValueMapTy &cache) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 699 | Value *def = findBaseOrBDV(I, cache); |
| 700 | |
| 701 | if (isKnownBaseResult(def)) { |
| 702 | return def; |
| 703 | } |
| 704 | |
| 705 | // Here's the rough algorithm: |
| 706 | // - For every SSA value, construct a mapping to either an actual base |
| 707 | // pointer or a PHI which obscures the base pointer. |
| 708 | // - Construct a mapping from PHI to unknown TOP state. Use an |
| 709 | // optimistic algorithm to propagate base pointer information. Lattice |
| 710 | // looks like: |
| 711 | // UNKNOWN |
| 712 | // b1 b2 b3 b4 |
| 713 | // CONFLICT |
| 714 | // When algorithm terminates, all PHIs will either have a single concrete |
| 715 | // base or be in a conflict state. |
| 716 | // - For every conflict, insert a dummy PHI node without arguments. Add |
| 717 | // these to the base[Instruction] = BasePtr mapping. For every |
| 718 | // non-conflict, add the actual base. |
| 719 | // - For every conflict, add arguments for the base[a] of each input |
| 720 | // arguments. |
| 721 | // |
| 722 | // Note: A simpler form of this would be to add the conflict form of all |
| 723 | // PHIs without running the optimistic algorithm. This would be |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 724 | // analogous to pessimistic data flow and would likely lead to an |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 725 | // overall worse solution. |
| 726 | |
Philip Reames | 29e9ae7 | 2015-07-24 00:42:55 +0000 | [diff] [blame] | 727 | #ifndef NDEBUG |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 728 | auto isExpectedBDVType = [](Value *BDV) { |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 729 | return isa<PHINode>(BDV) || isa<SelectInst>(BDV) || |
| 730 | isa<ExtractElementInst>(BDV) || isa<InsertElementInst>(BDV); |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 731 | }; |
Philip Reames | 29e9ae7 | 2015-07-24 00:42:55 +0000 | [diff] [blame] | 732 | #endif |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 733 | |
| 734 | // Once populated, will contain a mapping from each potentially non-base BDV |
| 735 | // to a lattice value (described above) which corresponds to that BDV. |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 736 | // We use the order of insertion (DFS over the def/use graph) to provide a |
| 737 | // stable deterministic ordering for visiting DenseMaps (which are unordered) |
| 738 | // below. This is important for deterministic compilation. |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 739 | MapVector<Value *, BDVState> States; |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 740 | |
| 741 | // Recursively fill in all base defining values reachable from the initial |
| 742 | // one for which we don't already know a definite base value for |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 743 | /* scope */ { |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 744 | SmallVector<Value*, 16> Worklist; |
| 745 | Worklist.push_back(def); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 746 | States.insert(std::make_pair(def, BDVState())); |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 747 | while (!Worklist.empty()) { |
| 748 | Value *Current = Worklist.pop_back_val(); |
| 749 | assert(!isKnownBaseResult(Current) && "why did it get added?"); |
| 750 | |
| 751 | auto visitIncomingValue = [&](Value *InVal) { |
| 752 | Value *Base = findBaseOrBDV(InVal, cache); |
| 753 | if (isKnownBaseResult(Base)) |
| 754 | // Known bases won't need new instructions introduced and can be |
| 755 | // ignored safely |
| 756 | return; |
| 757 | assert(isExpectedBDVType(Base) && "the only non-base values " |
| 758 | "we see should be base defining values"); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 759 | if (States.insert(std::make_pair(Base, BDVState())).second) |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 760 | Worklist.push_back(Base); |
| 761 | }; |
| 762 | if (PHINode *Phi = dyn_cast<PHINode>(Current)) { |
| 763 | for (Value *InVal : Phi->incoming_values()) |
| 764 | visitIncomingValue(InVal); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 765 | } else if (SelectInst *Sel = dyn_cast<SelectInst>(Current)) { |
Philip Reames | 88958b2 | 2015-07-24 00:02:11 +0000 | [diff] [blame] | 766 | visitIncomingValue(Sel->getTrueValue()); |
| 767 | visitIncomingValue(Sel->getFalseValue()); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 768 | } else if (auto *EE = dyn_cast<ExtractElementInst>(Current)) { |
| 769 | visitIncomingValue(EE->getVectorOperand()); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 770 | } else if (auto *IE = dyn_cast<InsertElementInst>(Current)) { |
| 771 | visitIncomingValue(IE->getOperand(0)); // vector operand |
| 772 | visitIncomingValue(IE->getOperand(1)); // scalar operand |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 773 | } else { |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 774 | // There is one known class of instructions we know we don't handle. |
| 775 | assert(isa<ShuffleVectorInst>(Current)); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 776 | llvm_unreachable("unimplemented instruction case"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 781 | #ifndef NDEBUG |
| 782 | DEBUG(dbgs() << "States after initialization:\n"); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 783 | for (auto Pair : States) { |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 784 | DEBUG(dbgs() << " " << Pair.second << " for " << *Pair.first << "\n"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 785 | } |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 786 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 787 | |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 788 | // Return a phi state for a base defining value. We'll generate a new |
| 789 | // base state for known bases and expect to find a cached state otherwise. |
| 790 | auto getStateForBDV = [&](Value *baseValue) { |
| 791 | if (isKnownBaseResult(baseValue)) |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 792 | return BDVState(baseValue); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 793 | auto I = States.find(baseValue); |
| 794 | assert(I != States.end() && "lookup failed!"); |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 795 | return I->second; |
| 796 | }; |
| 797 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 798 | bool progress = true; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 799 | while (progress) { |
Yaron Keren | 42a7adf | 2015-02-28 13:11:24 +0000 | [diff] [blame] | 800 | #ifndef NDEBUG |
Philip Reames | b4e55f3 | 2015-09-10 00:32:56 +0000 | [diff] [blame] | 801 | const size_t oldSize = States.size(); |
Yaron Keren | 42a7adf | 2015-02-28 13:11:24 +0000 | [diff] [blame] | 802 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 803 | progress = false; |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 804 | // We're only changing values in this loop, thus safe to keep iterators. |
| 805 | // Since this is computing a fixed point, the order of visit does not |
| 806 | // effect the result. TODO: We could use a worklist here and make this run |
| 807 | // much faster. |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 808 | for (auto Pair : States) { |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 809 | Value *BDV = Pair.first; |
| 810 | assert(!isKnownBaseResult(BDV) && "why did it get added?"); |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 811 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 812 | // Given an input value for the current instruction, return a BDVState |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 813 | // instance which represents the BDV of that value. |
| 814 | auto getStateForInput = [&](Value *V) mutable { |
| 815 | Value *BDV = findBaseOrBDV(V, cache); |
| 816 | return getStateForBDV(BDV); |
| 817 | }; |
| 818 | |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 819 | MeetBDVStates calculateMeet; |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 820 | if (SelectInst *select = dyn_cast<SelectInst>(BDV)) { |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 821 | calculateMeet.meetWith(getStateForInput(select->getTrueValue())); |
| 822 | calculateMeet.meetWith(getStateForInput(select->getFalseValue())); |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 823 | } else if (PHINode *Phi = dyn_cast<PHINode>(BDV)) { |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 824 | for (Value *Val : Phi->incoming_values()) |
Philip Reames | 273e6bb | 2015-07-23 21:41:27 +0000 | [diff] [blame] | 825 | calculateMeet.meetWith(getStateForInput(Val)); |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 826 | } else if (auto *EE = dyn_cast<ExtractElementInst>(BDV)) { |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 827 | // The 'meet' for an extractelement is slightly trivial, but it's still |
| 828 | // useful in that it drives us to conflict if our input is. |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 829 | calculateMeet.meetWith(getStateForInput(EE->getVectorOperand())); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 830 | } else { |
| 831 | // Given there's a inherent type mismatch between the operands, will |
| 832 | // *always* produce Conflict. |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 833 | auto *IE = cast<InsertElementInst>(BDV); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 834 | calculateMeet.meetWith(getStateForInput(IE->getOperand(0))); |
| 835 | calculateMeet.meetWith(getStateForInput(IE->getOperand(1))); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 838 | BDVState oldState = States[BDV]; |
Philip Reames | 9b141ed | 2015-07-23 22:49:14 +0000 | [diff] [blame] | 839 | BDVState newState = calculateMeet.getResult(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 840 | if (oldState != newState) { |
| 841 | progress = true; |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 842 | States[BDV] = newState; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Philip Reames | b4e55f3 | 2015-09-10 00:32:56 +0000 | [diff] [blame] | 846 | assert(oldSize == States.size() && |
| 847 | "fixed point shouldn't be adding any new nodes to state"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 850 | #ifndef NDEBUG |
| 851 | DEBUG(dbgs() << "States after meet iteration:\n"); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 852 | for (auto Pair : States) { |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 853 | DEBUG(dbgs() << " " << Pair.second << " for " << *Pair.first << "\n"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 854 | } |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 855 | #endif |
| 856 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 857 | // Insert Phis for all conflicts |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 858 | // TODO: adjust naming patterns to avoid this order of iteration dependency |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 859 | for (auto Pair : States) { |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 860 | Instruction *I = cast<Instruction>(Pair.first); |
| 861 | BDVState State = Pair.second; |
Philip Reames | 6ff1a1e3 | 2015-07-21 19:04:38 +0000 | [diff] [blame] | 862 | assert(!isKnownBaseResult(I) && "why did it get added?"); |
| 863 | assert(!State.isUnknown() && "Optimistic algorithm didn't complete!"); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 864 | |
| 865 | // extractelement instructions are a bit special in that we may need to |
| 866 | // insert an extract even when we know an exact base for the instruction. |
| 867 | // The problem is that we need to convert from a vector base to a scalar |
| 868 | // base for the particular indice we're interested in. |
| 869 | if (State.isBase() && isa<ExtractElementInst>(I) && |
| 870 | isa<VectorType>(State.getBase()->getType())) { |
| 871 | auto *EE = cast<ExtractElementInst>(I); |
| 872 | // TODO: In many cases, the new instruction is just EE itself. We should |
| 873 | // exploit this, but can't do it here since it would break the invariant |
| 874 | // about the BDV not being known to be a base. |
| 875 | auto *BaseInst = ExtractElementInst::Create(State.getBase(), |
| 876 | EE->getIndexOperand(), |
| 877 | "base_ee", EE); |
| 878 | BaseInst->setMetadata("is_base_value", MDNode::get(I->getContext(), {})); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 879 | States[I] = BDVState(BDVState::Base, BaseInst); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 880 | } |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 881 | |
| 882 | // Since we're joining a vector and scalar base, they can never be the |
| 883 | // same. As a result, we should always see insert element having reached |
| 884 | // the conflict state. |
| 885 | if (isa<InsertElementInst>(I)) { |
| 886 | assert(State.isConflict()); |
| 887 | } |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 888 | |
Philip Reames | 6ff1a1e3 | 2015-07-21 19:04:38 +0000 | [diff] [blame] | 889 | if (!State.isConflict()) |
Philip Reames | f986d68 | 2015-02-28 00:54:41 +0000 | [diff] [blame] | 890 | continue; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 891 | |
Philip Reames | 6ff1a1e3 | 2015-07-21 19:04:38 +0000 | [diff] [blame] | 892 | /// Create and insert a new instruction which will represent the base of |
| 893 | /// the given instruction 'I'. |
| 894 | auto MakeBaseInstPlaceholder = [](Instruction *I) -> Instruction* { |
| 895 | if (isa<PHINode>(I)) { |
| 896 | BasicBlock *BB = I->getParent(); |
| 897 | int NumPreds = std::distance(pred_begin(BB), pred_end(BB)); |
| 898 | assert(NumPreds > 0 && "how did we reach here"); |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 899 | std::string Name = suffixed_name_or(I, ".base", "base_phi"); |
Philip Reames | fa2c630 | 2015-07-24 19:01:39 +0000 | [diff] [blame] | 900 | return PHINode::Create(I->getType(), NumPreds, Name, I); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 901 | } else if (SelectInst *Sel = dyn_cast<SelectInst>(I)) { |
| 902 | // The undef will be replaced later |
| 903 | UndefValue *Undef = UndefValue::get(Sel->getType()); |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 904 | std::string Name = suffixed_name_or(I, ".base", "base_select"); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 905 | return SelectInst::Create(Sel->getCondition(), Undef, |
| 906 | Undef, Name, Sel); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 907 | } else if (auto *EE = dyn_cast<ExtractElementInst>(I)) { |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 908 | UndefValue *Undef = UndefValue::get(EE->getVectorOperand()->getType()); |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 909 | std::string Name = suffixed_name_or(I, ".base", "base_ee"); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 910 | return ExtractElementInst::Create(Undef, EE->getIndexOperand(), Name, |
| 911 | EE); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 912 | } else { |
| 913 | auto *IE = cast<InsertElementInst>(I); |
| 914 | UndefValue *VecUndef = UndefValue::get(IE->getOperand(0)->getType()); |
| 915 | UndefValue *ScalarUndef = UndefValue::get(IE->getOperand(1)->getType()); |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 916 | std::string Name = suffixed_name_or(I, ".base", "base_ie"); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 917 | return InsertElementInst::Create(VecUndef, ScalarUndef, |
| 918 | IE->getOperand(2), Name, IE); |
Philip Reames | 6ff1a1e3 | 2015-07-21 19:04:38 +0000 | [diff] [blame] | 919 | } |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 920 | |
Philip Reames | 6ff1a1e3 | 2015-07-21 19:04:38 +0000 | [diff] [blame] | 921 | }; |
| 922 | Instruction *BaseInst = MakeBaseInstPlaceholder(I); |
| 923 | // Add metadata marking this as a base value |
| 924 | BaseInst->setMetadata("is_base_value", MDNode::get(I->getContext(), {})); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 925 | States[I] = BDVState(BDVState::Conflict, BaseInst); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 928 | // Returns a instruction which produces the base pointer for a given |
| 929 | // instruction. The instruction is assumed to be an input to one of the BDVs |
| 930 | // seen in the inference algorithm above. As such, we must either already |
| 931 | // know it's base defining value is a base, or have inserted a new |
| 932 | // instruction to propagate the base of it's BDV and have entered that newly |
| 933 | // introduced instruction into the state table. In either case, we are |
| 934 | // assured to be able to determine an instruction which produces it's base |
| 935 | // pointer. |
| 936 | auto getBaseForInput = [&](Value *Input, Instruction *InsertPt) { |
| 937 | Value *BDV = findBaseOrBDV(Input, cache); |
| 938 | Value *Base = nullptr; |
| 939 | if (isKnownBaseResult(BDV)) { |
| 940 | Base = BDV; |
| 941 | } else { |
| 942 | // Either conflict or base. |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 943 | assert(States.count(BDV)); |
| 944 | Base = States[BDV].getBase(); |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 945 | } |
| 946 | assert(Base && "can't be null"); |
| 947 | // The cast is needed since base traversal may strip away bitcasts |
| 948 | if (Base->getType() != Input->getType() && |
| 949 | InsertPt) { |
| 950 | Base = new BitCastInst(Base, Input->getType(), "cast", |
| 951 | InsertPt); |
| 952 | } |
| 953 | return Base; |
| 954 | }; |
| 955 | |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 956 | // Fixup all the inputs of the new PHIs. Visit order needs to be |
| 957 | // deterministic and predictable because we're naming newly created |
| 958 | // instructions. |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 959 | for (auto Pair : States) { |
Philip Reames | 7540e3a | 2015-09-10 00:01:53 +0000 | [diff] [blame] | 960 | Instruction *BDV = cast<Instruction>(Pair.first); |
Philip Reames | c8ded46 | 2015-09-10 00:27:50 +0000 | [diff] [blame] | 961 | BDVState State = Pair.second; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 962 | |
Philip Reames | 7540e3a | 2015-09-10 00:01:53 +0000 | [diff] [blame] | 963 | assert(!isKnownBaseResult(BDV) && "why did it get added?"); |
Philip Reames | c8ded46 | 2015-09-10 00:27:50 +0000 | [diff] [blame] | 964 | assert(!State.isUnknown() && "Optimistic algorithm didn't complete!"); |
| 965 | if (!State.isConflict()) |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 966 | continue; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 967 | |
Philip Reames | c8ded46 | 2015-09-10 00:27:50 +0000 | [diff] [blame] | 968 | if (PHINode *basephi = dyn_cast<PHINode>(State.getBase())) { |
Philip Reames | 7540e3a | 2015-09-10 00:01:53 +0000 | [diff] [blame] | 969 | PHINode *phi = cast<PHINode>(BDV); |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 970 | unsigned NumPHIValues = phi->getNumIncomingValues(); |
| 971 | for (unsigned i = 0; i < NumPHIValues; i++) { |
| 972 | Value *InVal = phi->getIncomingValue(i); |
| 973 | BasicBlock *InBB = phi->getIncomingBlock(i); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 974 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 975 | // If we've already seen InBB, add the same incoming value |
| 976 | // we added for it earlier. The IR verifier requires phi |
| 977 | // nodes with multiple entries from the same basic block |
| 978 | // to have the same incoming value for each of those |
| 979 | // entries. If we don't do this check here and basephi |
| 980 | // has a different type than base, we'll end up adding two |
| 981 | // bitcasts (and hence two distinct values) as incoming |
| 982 | // values for the same basic block. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 983 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 984 | int blockIndex = basephi->getBasicBlockIndex(InBB); |
| 985 | if (blockIndex != -1) { |
| 986 | Value *oldBase = basephi->getIncomingValue(blockIndex); |
| 987 | basephi->addIncoming(oldBase, InBB); |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 988 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 989 | #ifndef NDEBUG |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 990 | Value *Base = getBaseForInput(InVal, nullptr); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 991 | // In essence this assert states: the only way two |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 992 | // values incoming from the same basic block may be |
| 993 | // different is by being different bitcasts of the same |
| 994 | // value. A cleanup that remains TODO is changing |
| 995 | // findBaseOrBDV to return an llvm::Value of the correct |
| 996 | // type (and still remain pure). This will remove the |
| 997 | // need to add bitcasts. |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 998 | assert(Base->stripPointerCasts() == oldBase->stripPointerCasts() && |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 999 | "sanity -- findBaseOrBDV should be pure!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1000 | #endif |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 1001 | continue; |
| 1002 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1003 | |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 1004 | // Find the instruction which produces the base for each input. We may |
| 1005 | // need to insert a bitcast in the incoming block. |
| 1006 | // TODO: Need to split critical edges if insertion is needed |
| 1007 | Value *Base = getBaseForInput(InVal, InBB->getTerminator()); |
| 1008 | basephi->addIncoming(Base, InBB); |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 1009 | } |
| 1010 | assert(basephi->getNumIncomingValues() == NumPHIValues); |
Philip Reames | c8ded46 | 2015-09-10 00:27:50 +0000 | [diff] [blame] | 1011 | } else if (SelectInst *BaseSel = dyn_cast<SelectInst>(State.getBase())) { |
Philip Reames | 7540e3a | 2015-09-10 00:01:53 +0000 | [diff] [blame] | 1012 | SelectInst *Sel = cast<SelectInst>(BDV); |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 1013 | // Operand 1 & 2 are true, false path respectively. TODO: refactor to |
| 1014 | // something more safe and less hacky. |
| 1015 | for (int i = 1; i <= 2; i++) { |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 1016 | Value *InVal = Sel->getOperand(i); |
| 1017 | // Find the instruction which produces the base for each input. We may |
| 1018 | // need to insert a bitcast. |
| 1019 | Value *Base = getBaseForInput(InVal, BaseSel); |
| 1020 | BaseSel->setOperand(i, Base); |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 1021 | } |
Philip Reames | c8ded46 | 2015-09-10 00:27:50 +0000 | [diff] [blame] | 1022 | } else if (auto *BaseEE = dyn_cast<ExtractElementInst>(State.getBase())) { |
Philip Reames | 7540e3a | 2015-09-10 00:01:53 +0000 | [diff] [blame] | 1023 | Value *InVal = cast<ExtractElementInst>(BDV)->getVectorOperand(); |
Philip Reames | 3ea1589 | 2015-09-03 21:57:40 +0000 | [diff] [blame] | 1024 | // Find the instruction which produces the base for each input. We may |
| 1025 | // need to insert a bitcast. |
| 1026 | Value *Base = getBaseForInput(InVal, BaseEE); |
Philip Reames | 9ac4e38 | 2015-08-12 21:00:20 +0000 | [diff] [blame] | 1027 | BaseEE->setOperand(0, Base); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 1028 | } else { |
Philip Reames | c8ded46 | 2015-09-10 00:27:50 +0000 | [diff] [blame] | 1029 | auto *BaseIE = cast<InsertElementInst>(State.getBase()); |
Philip Reames | 7540e3a | 2015-09-10 00:01:53 +0000 | [diff] [blame] | 1030 | auto *BdvIE = cast<InsertElementInst>(BDV); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 1031 | auto UpdateOperand = [&](int OperandIdx) { |
| 1032 | Value *InVal = BdvIE->getOperand(OperandIdx); |
Philip Reames | 953817b | 2015-09-10 00:44:10 +0000 | [diff] [blame] | 1033 | Value *Base = getBaseForInput(InVal, BaseIE); |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 1034 | BaseIE->setOperand(OperandIdx, Base); |
| 1035 | }; |
| 1036 | UpdateOperand(0); // vector operand |
| 1037 | UpdateOperand(1); // scalar operand |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1038 | } |
Philip Reames | 6628713 | 2015-09-09 23:40:12 +0000 | [diff] [blame] | 1039 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1042 | // Now that we're done with the algorithm, see if we can optimize the |
| 1043 | // results slightly by reducing the number of new instructions needed. |
| 1044 | // Arguably, this should be integrated into the algorithm above, but |
| 1045 | // doing as a post process step is easier to reason about for the moment. |
| 1046 | DenseMap<Value *, Value *> ReverseMap; |
| 1047 | SmallPtrSet<Instruction *, 16> NewInsts; |
Philip Reames | 9546f36 | 2015-09-02 22:25:07 +0000 | [diff] [blame] | 1048 | SmallSetVector<AssertingVH<Instruction>, 16> Worklist; |
Philip Reames | 246e618 | 2015-09-03 20:24:29 +0000 | [diff] [blame] | 1049 | // Note: We need to visit the states in a deterministic order. We uses the |
| 1050 | // Keys we sorted above for this purpose. Note that we are papering over a |
| 1051 | // bigger problem with the algorithm above - it's visit order is not |
| 1052 | // deterministic. A larger change is needed to fix this. |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 1053 | for (auto Pair : States) { |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1054 | auto *BDV = Pair.first; |
| 1055 | auto State = Pair.second; |
Philip Reames | 246e618 | 2015-09-03 20:24:29 +0000 | [diff] [blame] | 1056 | Value *Base = State.getBase(); |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1057 | assert(BDV && Base); |
| 1058 | assert(!isKnownBaseResult(BDV) && "why did it get added?"); |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1059 | assert(isKnownBaseResult(Base) && |
| 1060 | "must be something we 'know' is a base pointer"); |
Philip Reames | 246e618 | 2015-09-03 20:24:29 +0000 | [diff] [blame] | 1061 | if (!State.isConflict()) |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1062 | continue; |
| 1063 | |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1064 | ReverseMap[Base] = BDV; |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1065 | if (auto *BaseI = dyn_cast<Instruction>(Base)) { |
| 1066 | NewInsts.insert(BaseI); |
| 1067 | Worklist.insert(BaseI); |
| 1068 | } |
| 1069 | } |
Philip Reames | 9546f36 | 2015-09-02 22:25:07 +0000 | [diff] [blame] | 1070 | auto ReplaceBaseInstWith = [&](Value *BDV, Instruction *BaseI, |
| 1071 | Value *Replacement) { |
| 1072 | // Add users which are new instructions (excluding self references) |
| 1073 | for (User *U : BaseI->users()) |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1074 | if (auto *UI = dyn_cast<Instruction>(U)) |
Philip Reames | 9546f36 | 2015-09-02 22:25:07 +0000 | [diff] [blame] | 1075 | if (NewInsts.count(UI) && UI != BaseI) |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1076 | Worklist.insert(UI); |
Philip Reames | 9546f36 | 2015-09-02 22:25:07 +0000 | [diff] [blame] | 1077 | // Then do the actual replacement |
| 1078 | NewInsts.erase(BaseI); |
| 1079 | ReverseMap.erase(BaseI); |
| 1080 | BaseI->replaceAllUsesWith(Replacement); |
| 1081 | BaseI->eraseFromParent(); |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 1082 | assert(States.count(BDV)); |
| 1083 | assert(States[BDV].isConflict() && States[BDV].getBase() == BaseI); |
| 1084 | States[BDV] = BDVState(BDVState::Conflict, Replacement); |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1085 | }; |
| 1086 | const DataLayout &DL = cast<Instruction>(def)->getModule()->getDataLayout(); |
| 1087 | while (!Worklist.empty()) { |
| 1088 | Instruction *BaseI = Worklist.pop_back_val(); |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 1089 | assert(NewInsts.count(BaseI)); |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1090 | Value *Bdv = ReverseMap[BaseI]; |
| 1091 | if (auto *BdvI = dyn_cast<Instruction>(Bdv)) |
| 1092 | if (BaseI->isIdenticalTo(BdvI)) { |
| 1093 | DEBUG(dbgs() << "Identical Base: " << *BaseI << "\n"); |
Philip Reames | 9546f36 | 2015-09-02 22:25:07 +0000 | [diff] [blame] | 1094 | ReplaceBaseInstWith(Bdv, BaseI, Bdv); |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1095 | continue; |
| 1096 | } |
| 1097 | if (Value *V = SimplifyInstruction(BaseI, DL)) { |
| 1098 | DEBUG(dbgs() << "Base " << *BaseI << " simplified to " << *V << "\n"); |
Philip Reames | 9546f36 | 2015-09-02 22:25:07 +0000 | [diff] [blame] | 1099 | ReplaceBaseInstWith(Bdv, BaseI, V); |
Philip Reames | abcdc5e | 2015-08-27 01:02:28 +0000 | [diff] [blame] | 1100 | continue; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1104 | // Cache all of our results so we can cheaply reuse them |
| 1105 | // NOTE: This is actually two caches: one of the base defining value |
| 1106 | // relation and one of the base pointer relation! FIXME |
Philip Reames | 34d7a74 | 2015-09-10 00:22:49 +0000 | [diff] [blame] | 1107 | for (auto Pair : States) { |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1108 | auto *BDV = Pair.first; |
| 1109 | Value *base = Pair.second.getBase(); |
| 1110 | assert(BDV && base); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1111 | |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 1112 | std::string fromstr = cache.count(BDV) ? cache[BDV]->getName() : "none"; |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 1113 | DEBUG(dbgs() << "Updating base value cache" |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 1114 | << " for: " << BDV->getName() |
Philip Reames | dab35f3 | 2015-09-02 21:11:44 +0000 | [diff] [blame] | 1115 | << " from: " << fromstr |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 1116 | << " to: " << base->getName() << "\n"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1117 | |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1118 | if (cache.count(BDV)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1119 | // Once we transition from the BDV relation being store in the cache to |
| 1120 | // the base relation being stored, it must be stable |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1121 | assert((!isKnownBaseResult(cache[BDV]) || cache[BDV] == base) && |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1122 | "base relation should be stable"); |
| 1123 | } |
Philip Reames | 15d5563 | 2015-09-09 23:26:08 +0000 | [diff] [blame] | 1124 | cache[BDV] = base; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1125 | } |
| 1126 | assert(cache.find(def) != cache.end()); |
| 1127 | return cache[def]; |
| 1128 | } |
| 1129 | |
| 1130 | // For a set of live pointers (base and/or derived), identify the base |
| 1131 | // pointer of the object which they are derived from. This routine will |
| 1132 | // mutate the IR graph as needed to make the 'base' pointer live at the |
| 1133 | // definition site of 'derived'. This ensures that any use of 'derived' can |
| 1134 | // also use 'base'. This may involve the insertion of a number of |
| 1135 | // additional PHI nodes. |
| 1136 | // |
| 1137 | // preconditions: live is a set of pointer type Values |
| 1138 | // |
| 1139 | // side effects: may insert PHI nodes into the existing CFG, will preserve |
| 1140 | // CFG, will not remove or mutate any existing nodes |
| 1141 | // |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1142 | // post condition: PointerToBase contains one (derived, base) pair for every |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1143 | // pointer in live. Note that derived can be equal to base if the original |
| 1144 | // pointer was a base pointer. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1145 | static void |
| 1146 | findBasePointers(const StatepointLiveSetTy &live, |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1147 | DenseMap<Value *, Value *> &PointerToBase, |
Philip Reames | ba19849 | 2015-04-14 00:41:34 +0000 | [diff] [blame] | 1148 | DominatorTree *DT, DefiningValueMapTy &DVCache) { |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 1149 | // For the naming of values inserted to be deterministic - which makes for |
| 1150 | // much cleaner and more stable tests - we need to assign an order to the |
| 1151 | // live values. DenseSets do not provide a deterministic order across runs. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1152 | SmallVector<Value *, 64> Temp; |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 1153 | Temp.insert(Temp.end(), live.begin(), live.end()); |
| 1154 | std::sort(Temp.begin(), Temp.end(), order_by_name); |
| 1155 | for (Value *ptr : Temp) { |
Philip Reames | ba19849 | 2015-04-14 00:41:34 +0000 | [diff] [blame] | 1156 | Value *base = findBasePointer(ptr, DVCache); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1157 | assert(base && "failed to find base pointer"); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1158 | PointerToBase[ptr] = base; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1159 | assert((!isa<Instruction>(base) || !isa<Instruction>(ptr) || |
| 1160 | DT->dominates(cast<Instruction>(base)->getParent(), |
| 1161 | cast<Instruction>(ptr)->getParent())) && |
| 1162 | "The base we found better dominate the derived pointer"); |
| 1163 | |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1164 | // If you see this trip and like to live really dangerously, the code should |
| 1165 | // be correct, just with idioms the verifier can't handle. You can try |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1166 | // disabling the verifier at your own substantial risk. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1167 | assert(!isa<ConstantPointerNull>(base) && |
Philip Reames | 24c6cd5 | 2015-03-27 05:47:00 +0000 | [diff] [blame] | 1168 | "the relocation code needs adjustment to handle the relocation of " |
| 1169 | "a null pointer constant without causing false positives in the " |
| 1170 | "safepoint ir verifier."); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /// Find the required based pointers (and adjust the live set) for the given |
| 1175 | /// parse point. |
| 1176 | static void findBasePointers(DominatorTree &DT, DefiningValueMapTy &DVCache, |
| 1177 | const CallSite &CS, |
| 1178 | PartiallyConstructedSafepointRecord &result) { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1179 | DenseMap<Value *, Value *> PointerToBase; |
| 1180 | findBasePointers(result.LiveSet, PointerToBase, &DT, DVCache); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (PrintBasePointers) { |
Philip Reames | a5aeaf4 | 2015-02-28 00:20:48 +0000 | [diff] [blame] | 1183 | // Note: Need to print these in a stable order since this is checked in |
| 1184 | // some tests. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1185 | errs() << "Base Pairs (w/o Relocation):\n"; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1186 | SmallVector<Value *, 64> Temp; |
Philip Reames | a5aeaf4 | 2015-02-28 00:20:48 +0000 | [diff] [blame] | 1187 | Temp.reserve(PointerToBase.size()); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1188 | for (auto Pair : PointerToBase) { |
Philip Reames | a5aeaf4 | 2015-02-28 00:20:48 +0000 | [diff] [blame] | 1189 | Temp.push_back(Pair.first); |
| 1190 | } |
| 1191 | std::sort(Temp.begin(), Temp.end(), order_by_name); |
| 1192 | for (Value *Ptr : Temp) { |
| 1193 | Value *Base = PointerToBase[Ptr]; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1194 | errs() << " derived %" << Ptr->getName() << " base %" << Base->getName() |
| 1195 | << "\n"; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1199 | result.PointerToBase = PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1202 | /// Given an updated version of the dataflow liveness results, update the |
| 1203 | /// liveset and base pointer maps for the call site CS. |
| 1204 | static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData, |
| 1205 | const CallSite &CS, |
| 1206 | PartiallyConstructedSafepointRecord &result); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1207 | |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1208 | static void recomputeLiveInValues( |
| 1209 | Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate, |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1210 | MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) { |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1211 | // TODO-PERF: reuse the original liveness, then simply run the dataflow |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1212 | // again. The old values are still live and will help it stabilize quickly. |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1213 | GCPtrLivenessData RevisedLivenessData; |
| 1214 | computeLiveInValues(DT, F, RevisedLivenessData); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1215 | for (size_t i = 0; i < records.size(); i++) { |
| 1216 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1217 | const CallSite &CS = toUpdate[i]; |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1218 | recomputeLiveInValues(RevisedLivenessData, CS, info); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 1222 | // When inserting gc.relocate calls, we need to ensure there are no uses |
| 1223 | // of the original value between the gc.statepoint and the gc.relocate call. |
| 1224 | // One case which can arise is a phi node starting one of the successor blocks. |
| 1225 | // We also need to be able to insert the gc.relocates only on the path which |
| 1226 | // goes through the statepoint. We might need to split an edge to make this |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 1227 | // possible. |
| 1228 | static BasicBlock * |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 1229 | normalizeForInvokeSafepoint(BasicBlock *BB, BasicBlock *InvokeParent, |
| 1230 | DominatorTree &DT) { |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 1231 | BasicBlock *Ret = BB; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1232 | if (!BB->getUniquePredecessor()) { |
Chandler Carruth | 96ada25 | 2015-07-22 09:52:54 +0000 | [diff] [blame] | 1233 | Ret = SplitBlockPredecessors(BB, InvokeParent, "", &DT); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 1236 | // Now that 'ret' has unique predecessor we can safely remove all phi nodes |
| 1237 | // from it |
| 1238 | FoldSingleEntryPHINodes(Ret); |
| 1239 | assert(!isa<PHINode>(Ret->begin())); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1240 | |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 1241 | // At this point, we can safely insert a gc.relocate as the first instruction |
| 1242 | // in Ret if needed. |
| 1243 | return Ret; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1246 | static int find_index(ArrayRef<Value *> livevec, Value *val) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1247 | auto itr = std::find(livevec.begin(), livevec.end(), val); |
| 1248 | assert(livevec.end() != itr); |
| 1249 | size_t index = std::distance(livevec.begin(), itr); |
| 1250 | assert(index < livevec.size()); |
| 1251 | return index; |
| 1252 | } |
| 1253 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1254 | // Create new attribute set containing only attributes which can be transferred |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1255 | // from original call to the safepoint. |
| 1256 | static AttributeSet legalizeCallAttributes(AttributeSet AS) { |
| 1257 | AttributeSet ret; |
| 1258 | |
| 1259 | for (unsigned Slot = 0; Slot < AS.getNumSlots(); Slot++) { |
| 1260 | unsigned index = AS.getSlotIndex(Slot); |
| 1261 | |
| 1262 | if (index == AttributeSet::ReturnIndex || |
| 1263 | index == AttributeSet::FunctionIndex) { |
| 1264 | |
| 1265 | for (auto it = AS.begin(Slot), it_end = AS.end(Slot); it != it_end; |
| 1266 | ++it) { |
| 1267 | Attribute attr = *it; |
| 1268 | |
| 1269 | // Do not allow certain attributes - just skip them |
| 1270 | // Safepoint can not be read only or read none. |
| 1271 | if (attr.hasAttribute(Attribute::ReadNone) || |
| 1272 | attr.hasAttribute(Attribute::ReadOnly)) |
| 1273 | continue; |
| 1274 | |
| 1275 | ret = ret.addAttributes( |
| 1276 | AS.getContext(), index, |
| 1277 | AttributeSet::get(AS.getContext(), index, AttrBuilder(attr))); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | // Just skip parameter attributes for now |
| 1282 | } |
| 1283 | |
| 1284 | return ret; |
| 1285 | } |
| 1286 | |
| 1287 | /// Helper function to place all gc relocates necessary for the given |
| 1288 | /// statepoint. |
| 1289 | /// Inputs: |
| 1290 | /// liveVariables - list of variables to be relocated. |
| 1291 | /// liveStart - index of the first live variable. |
| 1292 | /// basePtrs - base pointers. |
| 1293 | /// statepointToken - statepoint instruction to which relocates should be |
| 1294 | /// bound. |
| 1295 | /// Builder - Llvm IR builder to be used to construct new calls. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1296 | static void CreateGCRelocates(ArrayRef<Value *> LiveVariables, |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1297 | const int LiveStart, |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1298 | ArrayRef<Value *> BasePtrs, |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1299 | Instruction *StatepointToken, |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 1300 | IRBuilder<> Builder) { |
Philip Reames | 94babb7 | 2015-07-21 17:18:03 +0000 | [diff] [blame] | 1301 | if (LiveVariables.empty()) |
| 1302 | return; |
| 1303 | |
| 1304 | // All gc_relocate are set to i8 addrspace(1)* type. We originally generated |
| 1305 | // unique declarations for each pointer type, but this proved problematic |
| 1306 | // because the intrinsic mangling code is incomplete and fragile. Since |
| 1307 | // we're moving towards a single unified pointer type anyways, we can just |
| 1308 | // cast everything to an i8* of the right address space. A bitcast is added |
| 1309 | // later to convert gc_relocate to the actual value's type. |
Philip Reames | 74ce2e7 | 2015-07-21 16:51:17 +0000 | [diff] [blame] | 1310 | Module *M = StatepointToken->getModule(); |
Philip Reames | 94babb7 | 2015-07-21 17:18:03 +0000 | [diff] [blame] | 1311 | auto AS = cast<PointerType>(LiveVariables[0]->getType())->getAddressSpace(); |
| 1312 | Type *Types[] = {Type::getInt8PtrTy(M->getContext(), AS)}; |
| 1313 | Value *GCRelocateDecl = |
| 1314 | Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1315 | |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1316 | for (unsigned i = 0; i < LiveVariables.size(); i++) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1317 | // Generate the gc.relocate call and save the result |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1318 | Value *BaseIdx = |
Philip Reames | f388050 | 2015-07-21 00:49:55 +0000 | [diff] [blame] | 1319 | Builder.getInt32(LiveStart + find_index(LiveVariables, BasePtrs[i])); |
| 1320 | Value *LiveIdx = |
| 1321 | Builder.getInt32(LiveStart + find_index(LiveVariables, LiveVariables[i])); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1322 | |
| 1323 | // only specify a debug name if we can give a useful one |
Philip Reames | 74ce2e7 | 2015-07-21 16:51:17 +0000 | [diff] [blame] | 1324 | CallInst *Reloc = Builder.CreateCall( |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1325 | GCRelocateDecl, {StatepointToken, BaseIdx, LiveIdx}, |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 1326 | suffixed_name_or(LiveVariables[i], ".relocated", "")); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1327 | // Trick CodeGen into thinking there are lots of free registers at this |
| 1328 | // fake call. |
Philip Reames | 74ce2e7 | 2015-07-21 16:51:17 +0000 | [diff] [blame] | 1329 | Reloc->setCallingConv(CallingConv::Cold); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1330 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static void |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1334 | makeStatepointExplicitImpl(const CallSite CS, /* to replace */ |
| 1335 | const SmallVectorImpl<Value *> &BasePtrs, |
| 1336 | const SmallVectorImpl<Value *> &LiveVariables, |
| 1337 | PartiallyConstructedSafepointRecord &Result) { |
| 1338 | assert(BasePtrs.size() == LiveVariables.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1339 | assert(isStatepoint(CS) && |
| 1340 | "This method expects to be rewriting a statepoint"); |
| 1341 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1342 | // Then go ahead and use the builder do actually do the inserts. We insert |
| 1343 | // immediately before the previous instruction under the assumption that all |
| 1344 | // arguments will be available here. We can't insert afterwards since we may |
| 1345 | // be replacing a terminator. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1346 | Instruction *InsertBefore = CS.getInstruction(); |
| 1347 | IRBuilder<> Builder(InsertBefore); |
| 1348 | |
Sanjoy Das | 3c520a1 | 2015-10-08 23:18:38 +0000 | [diff] [blame] | 1349 | Statepoint OldSP(CS); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1350 | |
Sanjoy Das | 3c520a1 | 2015-10-08 23:18:38 +0000 | [diff] [blame] | 1351 | ArrayRef<Value *> GCArgs(LiveVariables); |
| 1352 | uint64_t StatepointID = OldSP.getID(); |
| 1353 | uint32_t NumPatchBytes = OldSP.getNumPatchBytes(); |
| 1354 | uint32_t Flags = OldSP.getFlags(); |
| 1355 | |
| 1356 | ArrayRef<Use> CallArgs(OldSP.arg_begin(), OldSP.arg_end()); |
| 1357 | ArrayRef<Use> DeoptArgs(OldSP.vm_state_begin(), OldSP.vm_state_end()); |
| 1358 | ArrayRef<Use> TransitionArgs(OldSP.gc_transition_args_begin(), |
| 1359 | OldSP.gc_transition_args_end()); |
| 1360 | Value *CallTarget = OldSP.getCalledValue(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1361 | |
| 1362 | // Create the statepoint given all the arguments |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1363 | Instruction *Token = nullptr; |
| 1364 | AttributeSet ReturnAttrs; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1365 | if (CS.isCall()) { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1366 | CallInst *ToReplace = cast<CallInst>(CS.getInstruction()); |
Sanjoy Das | 3c520a1 | 2015-10-08 23:18:38 +0000 | [diff] [blame] | 1367 | CallInst *Call = Builder.CreateGCStatepointCall( |
| 1368 | StatepointID, NumPatchBytes, CallTarget, Flags, CallArgs, |
| 1369 | TransitionArgs, DeoptArgs, GCArgs, "safepoint_token"); |
| 1370 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1371 | Call->setTailCall(ToReplace->isTailCall()); |
| 1372 | Call->setCallingConv(ToReplace->getCallingConv()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Currently we will fail on parameter attributes and on certain |
| 1375 | // function attributes. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1376 | AttributeSet NewAttrs = legalizeCallAttributes(ToReplace->getAttributes()); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1377 | // In case if we can handle this set of attributes - set up function attrs |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1378 | // directly on statepoint and return attrs later for gc_result intrinsic. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1379 | Call->setAttributes(NewAttrs.getFnAttributes()); |
| 1380 | ReturnAttrs = NewAttrs.getRetAttributes(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1381 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1382 | Token = Call; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1383 | |
| 1384 | // Put the following gc_result and gc_relocate calls immediately after the |
| 1385 | // the old call (which we're about to delete) |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1386 | assert(ToReplace->getNextNode() && "Not a terminator, must have next!"); |
| 1387 | Builder.SetInsertPoint(ToReplace->getNextNode()); |
| 1388 | Builder.SetCurrentDebugLocation(ToReplace->getNextNode()->getDebugLoc()); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1389 | } else { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1390 | InvokeInst *ToReplace = cast<InvokeInst>(CS.getInstruction()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Insert the new invoke into the old block. We'll remove the old one in a |
| 1393 | // moment at which point this will become the new terminator for the |
| 1394 | // original block. |
Sanjoy Das | 3c520a1 | 2015-10-08 23:18:38 +0000 | [diff] [blame] | 1395 | InvokeInst *Invoke = Builder.CreateGCStatepointInvoke( |
| 1396 | StatepointID, NumPatchBytes, CallTarget, ToReplace->getNormalDest(), |
| 1397 | ToReplace->getUnwindDest(), Flags, CallArgs, TransitionArgs, DeoptArgs, |
| 1398 | GCArgs, "statepoint_token"); |
| 1399 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1400 | Invoke->setCallingConv(ToReplace->getCallingConv()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Currently we will fail on parameter attributes and on certain |
| 1403 | // function attributes. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1404 | AttributeSet NewAttrs = legalizeCallAttributes(ToReplace->getAttributes()); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1405 | // In case if we can handle this set of attributes - set up function attrs |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1406 | // directly on statepoint and return attrs later for gc_result intrinsic. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1407 | Invoke->setAttributes(NewAttrs.getFnAttributes()); |
| 1408 | ReturnAttrs = NewAttrs.getRetAttributes(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1409 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1410 | Token = Invoke; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1411 | |
| 1412 | // Generate gc relocates in exceptional path |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1413 | BasicBlock *UnwindBlock = ToReplace->getUnwindDest(); |
| 1414 | assert(!isa<PHINode>(UnwindBlock->begin()) && |
| 1415 | UnwindBlock->getUniquePredecessor() && |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 1416 | "can't safely insert in this block!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1417 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1418 | Builder.SetInsertPoint(UnwindBlock->getFirstInsertionPt()); |
| 1419 | Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1420 | |
| 1421 | // Extract second element from landingpad return value. We will attach |
| 1422 | // exceptional gc relocates to it. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1423 | Instruction *ExceptionalToken = |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1424 | cast<Instruction>(Builder.CreateExtractValue( |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1425 | UnwindBlock->getLandingPadInst(), 1, "relocate_token")); |
| 1426 | Result.UnwindToken = ExceptionalToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1427 | |
Sanjoy Das | 3c520a1 | 2015-10-08 23:18:38 +0000 | [diff] [blame] | 1428 | const unsigned LiveStartIdx = Statepoint(Token).gcArgsStartIdx(); |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1429 | CreateGCRelocates(LiveVariables, LiveStartIdx, BasePtrs, ExceptionalToken, |
| 1430 | Builder); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Generate gc relocates and returns for normal block |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1433 | BasicBlock *NormalDest = ToReplace->getNormalDest(); |
| 1434 | assert(!isa<PHINode>(NormalDest->begin()) && |
| 1435 | NormalDest->getUniquePredecessor() && |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 1436 | "can't safely insert in this block!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1437 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1438 | Builder.SetInsertPoint(NormalDest->getFirstInsertionPt()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1439 | |
| 1440 | // gc relocates will be generated later as if it were regular call |
| 1441 | // statepoint |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1442 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1443 | assert(Token && "Should be set in one of the above branches!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1444 | |
| 1445 | // Take the name of the original value call if it had one. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1446 | Token->takeName(CS.getInstruction()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1447 | |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1448 | // The GCResult is already inserted, we just need to find it |
David Blaikie | 5e5d784 | 2015-02-22 20:58:38 +0000 | [diff] [blame] | 1449 | #ifndef NDEBUG |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1450 | Instruction *ToReplace = CS.getInstruction(); |
| 1451 | assert(!ToReplace->hasNUsesOrMore(2) && |
David Blaikie | 5e5d784 | 2015-02-22 20:58:38 +0000 | [diff] [blame] | 1452 | "only valid use before rewrite is gc.result"); |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1453 | assert(!ToReplace->hasOneUse() || |
| 1454 | isGCResult(cast<Instruction>(*ToReplace->user_begin()))); |
David Blaikie | 5e5d784 | 2015-02-22 20:58:38 +0000 | [diff] [blame] | 1455 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1456 | |
| 1457 | // Update the gc.result of the original statepoint (if any) to use the newly |
| 1458 | // inserted statepoint. This is safe to do here since the token can't be |
| 1459 | // considered a live reference. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1460 | CS.getInstruction()->replaceAllUsesWith(Token); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1461 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1462 | Result.StatepointToken = Token; |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1463 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1464 | // Second, create a gc.relocate for every live variable |
Sanjoy Das | 3c520a1 | 2015-10-08 23:18:38 +0000 | [diff] [blame] | 1465 | const unsigned LiveStartIdx = Statepoint(Token).gcArgsStartIdx(); |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1466 | CreateGCRelocates(LiveVariables, LiveStartIdx, BasePtrs, Token, Builder); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | namespace { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1470 | struct NameOrdering { |
| 1471 | Value *Base; |
| 1472 | Value *Derived; |
| 1473 | |
| 1474 | bool operator()(NameOrdering const &a, NameOrdering const &b) { |
| 1475 | return -1 == a.Derived->getName().compare(b.Derived->getName()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1476 | } |
| 1477 | }; |
| 1478 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1479 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1480 | static void StabilizeOrder(SmallVectorImpl<Value *> &BaseVec, |
| 1481 | SmallVectorImpl<Value *> &LiveVec) { |
| 1482 | assert(BaseVec.size() == LiveVec.size()); |
| 1483 | |
| 1484 | SmallVector<NameOrdering, 64> Temp; |
| 1485 | for (size_t i = 0; i < BaseVec.size(); i++) { |
| 1486 | NameOrdering v; |
| 1487 | v.Base = BaseVec[i]; |
| 1488 | v.Derived = LiveVec[i]; |
| 1489 | Temp.push_back(v); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1490 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1491 | |
| 1492 | std::sort(Temp.begin(), Temp.end(), NameOrdering()); |
| 1493 | for (size_t i = 0; i < BaseVec.size(); i++) { |
| 1494 | BaseVec[i] = Temp[i].Base; |
| 1495 | LiveVec[i] = Temp[i].Derived; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | // Replace an existing gc.statepoint with a new one and a set of gc.relocates |
| 1500 | // which make the relocations happening at this safepoint explicit. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1501 | // |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1502 | // WARNING: Does not do any fixup to adjust users of the original live |
| 1503 | // values. That's the callers responsibility. |
| 1504 | static void |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1505 | makeStatepointExplicit(DominatorTree &DT, const CallSite &CS, |
| 1506 | PartiallyConstructedSafepointRecord &Result) { |
Sanjoy Das | 1ede536 | 2015-10-08 23:18:22 +0000 | [diff] [blame] | 1507 | const auto &LiveSet = Result.LiveSet; |
| 1508 | const auto &PointerToBase = Result.PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Convert to vector for efficient cross referencing. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1511 | SmallVector<Value *, 64> BaseVec, LiveVec; |
| 1512 | LiveVec.reserve(LiveSet.size()); |
| 1513 | BaseVec.reserve(LiveSet.size()); |
| 1514 | for (Value *L : LiveSet) { |
| 1515 | LiveVec.push_back(L); |
Philip Reames | 74ce2e7 | 2015-07-21 16:51:17 +0000 | [diff] [blame] | 1516 | assert(PointerToBase.count(L)); |
Sanjoy Das | 1ede536 | 2015-10-08 23:18:22 +0000 | [diff] [blame] | 1517 | Value *Base = PointerToBase.find(L)->second; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1518 | BaseVec.push_back(Base); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1519 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1520 | assert(LiveVec.size() == BaseVec.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1521 | |
| 1522 | // To make the output IR slightly more stable (for use in diffs), ensure a |
| 1523 | // fixed order of the values in the safepoint (by sorting the value name). |
| 1524 | // The order is otherwise meaningless. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1525 | StabilizeOrder(BaseVec, LiveVec); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Do the actual rewriting and delete the old statepoint |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1528 | makeStatepointExplicitImpl(CS, BaseVec, LiveVec, Result); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1529 | CS.getInstruction()->eraseFromParent(); |
| 1530 | } |
| 1531 | |
| 1532 | // Helper function for the relocationViaAlloca. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1533 | // |
| 1534 | // It receives iterator to the statepoint gc relocates and emits a store to the |
| 1535 | // assigned location (via allocaMap) for the each one of them. It adds the |
| 1536 | // visited values into the visitedLiveValues set, which we will later use them |
| 1537 | // for sanity checking. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1538 | static void |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1539 | insertRelocationStores(iterator_range<Value::user_iterator> GCRelocs, |
| 1540 | DenseMap<Value *, Value *> &AllocaMap, |
| 1541 | DenseSet<Value *> &VisitedLiveValues) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1542 | |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1543 | for (User *U : GCRelocs) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1544 | if (!isa<IntrinsicInst>(U)) |
| 1545 | continue; |
| 1546 | |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1547 | IntrinsicInst *RelocatedValue = cast<IntrinsicInst>(U); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1548 | |
| 1549 | // We only care about relocates |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1550 | if (RelocatedValue->getIntrinsicID() != |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1551 | Intrinsic::experimental_gc_relocate) { |
| 1552 | continue; |
| 1553 | } |
| 1554 | |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1555 | GCRelocateOperands RelocateOperands(RelocatedValue); |
| 1556 | Value *OriginalValue = |
| 1557 | const_cast<Value *>(RelocateOperands.getDerivedPtr()); |
| 1558 | assert(AllocaMap.count(OriginalValue)); |
| 1559 | Value *Alloca = AllocaMap[OriginalValue]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Emit store into the related alloca |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1562 | // All gc_relocates are i8 addrspace(1)* typed, and it must be bitcasted to |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 1563 | // the correct type according to alloca. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1564 | assert(RelocatedValue->getNextNode() && |
| 1565 | "Should always have one since it's not a terminator"); |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1566 | IRBuilder<> Builder(RelocatedValue->getNextNode()); |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 1567 | Value *CastedRelocatedValue = |
Philip Reames | ece70b8 | 2015-09-09 23:57:18 +0000 | [diff] [blame] | 1568 | Builder.CreateBitCast(RelocatedValue, |
| 1569 | cast<AllocaInst>(Alloca)->getAllocatedType(), |
| 1570 | suffixed_name_or(RelocatedValue, ".casted", "")); |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 1571 | |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1572 | StoreInst *Store = new StoreInst(CastedRelocatedValue, Alloca); |
| 1573 | Store->insertAfter(cast<Instruction>(CastedRelocatedValue)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1574 | |
| 1575 | #ifndef NDEBUG |
Sanjoy Das | 5665c99 | 2015-05-11 23:47:27 +0000 | [diff] [blame] | 1576 | VisitedLiveValues.insert(OriginalValue); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1577 | #endif |
| 1578 | } |
| 1579 | } |
| 1580 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1581 | // Helper function for the "relocationViaAlloca". Similar to the |
| 1582 | // "insertRelocationStores" but works for rematerialized values. |
| 1583 | static void |
| 1584 | insertRematerializationStores( |
| 1585 | RematerializedValueMapTy RematerializedValues, |
| 1586 | DenseMap<Value *, Value *> &AllocaMap, |
| 1587 | DenseSet<Value *> &VisitedLiveValues) { |
| 1588 | |
| 1589 | for (auto RematerializedValuePair: RematerializedValues) { |
| 1590 | Instruction *RematerializedValue = RematerializedValuePair.first; |
| 1591 | Value *OriginalValue = RematerializedValuePair.second; |
| 1592 | |
| 1593 | assert(AllocaMap.count(OriginalValue) && |
| 1594 | "Can not find alloca for rematerialized value"); |
| 1595 | Value *Alloca = AllocaMap[OriginalValue]; |
| 1596 | |
| 1597 | StoreInst *Store = new StoreInst(RematerializedValue, Alloca); |
| 1598 | Store->insertAfter(RematerializedValue); |
| 1599 | |
| 1600 | #ifndef NDEBUG |
| 1601 | VisitedLiveValues.insert(OriginalValue); |
| 1602 | #endif |
| 1603 | } |
| 1604 | } |
| 1605 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1606 | /// Do all the relocation update via allocas and mem2reg |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1607 | static void relocationViaAlloca( |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1608 | Function &F, DominatorTree &DT, ArrayRef<Value *> Live, |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1609 | ArrayRef<PartiallyConstructedSafepointRecord> Records) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1610 | #ifndef NDEBUG |
Philip Reames | a6ebf07 | 2015-03-27 05:53:16 +0000 | [diff] [blame] | 1611 | // record initial number of (static) allocas; we'll check we have the same |
| 1612 | // number when we get done. |
| 1613 | int InitialAllocaNum = 0; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1614 | for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E; |
| 1615 | I++) |
Philip Reames | a6ebf07 | 2015-03-27 05:53:16 +0000 | [diff] [blame] | 1616 | if (isa<AllocaInst>(*I)) |
| 1617 | InitialAllocaNum++; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1618 | #endif |
| 1619 | |
| 1620 | // TODO-PERF: change data structures, reserve |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1621 | DenseMap<Value *, Value *> AllocaMap; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1622 | SmallVector<AllocaInst *, 200> PromotableAllocas; |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1623 | // Used later to chack that we have enough allocas to store all values |
| 1624 | std::size_t NumRematerializedValues = 0; |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1625 | PromotableAllocas.reserve(Live.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1626 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1627 | // Emit alloca for "LiveValue" and record it in "allocaMap" and |
| 1628 | // "PromotableAllocas" |
| 1629 | auto emitAllocaFor = [&](Value *LiveValue) { |
| 1630 | AllocaInst *Alloca = new AllocaInst(LiveValue->getType(), "", |
| 1631 | F.getEntryBlock().getFirstNonPHI()); |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1632 | AllocaMap[LiveValue] = Alloca; |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1633 | PromotableAllocas.push_back(Alloca); |
| 1634 | }; |
| 1635 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1636 | // Emit alloca for each live gc pointer |
| 1637 | for (Value *V : Live) |
| 1638 | emitAllocaFor(V); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1639 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1640 | // Emit allocas for rematerialized values |
| 1641 | for (const auto &Info : Records) |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1642 | for (auto RematerializedValuePair : Info.RematerializedValues) { |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1643 | Value *OriginalValue = RematerializedValuePair.second; |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1644 | if (AllocaMap.count(OriginalValue) != 0) |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1645 | continue; |
| 1646 | |
| 1647 | emitAllocaFor(OriginalValue); |
| 1648 | ++NumRematerializedValues; |
| 1649 | } |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1650 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1651 | // The next two loops are part of the same conceptual operation. We need to |
| 1652 | // insert a store to the alloca after the original def and at each |
| 1653 | // redefinition. We need to insert a load before each use. These are split |
| 1654 | // into distinct loops for performance reasons. |
| 1655 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1656 | // Update gc pointer after each statepoint: either store a relocated value or |
| 1657 | // null (if no relocated value was found for this gc pointer and it is not a |
| 1658 | // gc_result). This must happen before we update the statepoint with load of |
| 1659 | // alloca otherwise we lose the link between statepoint and old def. |
| 1660 | for (const auto &Info : Records) { |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1661 | Value *Statepoint = Info.StatepointToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1662 | |
| 1663 | // This will be used for consistency check |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1664 | DenseSet<Value *> VisitedLiveValues; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1665 | |
| 1666 | // Insert stores for normal statepoint gc relocates |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1667 | insertRelocationStores(Statepoint->users(), AllocaMap, VisitedLiveValues); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1668 | |
| 1669 | // In case if it was invoke statepoint |
| 1670 | // we will insert stores for exceptional path gc relocates. |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1671 | if (isa<InvokeInst>(Statepoint)) { |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1672 | insertRelocationStores(Info.UnwindToken->users(), AllocaMap, |
| 1673 | VisitedLiveValues); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1676 | // Do similar thing with rematerialized values |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1677 | insertRematerializationStores(Info.RematerializedValues, AllocaMap, |
| 1678 | VisitedLiveValues); |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1679 | |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1680 | if (ClobberNonLive) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1681 | // As a debugging aid, pretend that an unrelocated pointer becomes null at |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1682 | // the gc.statepoint. This will turn some subtle GC problems into |
| 1683 | // slightly easier to debug SEGVs. Note that on large IR files with |
| 1684 | // lots of gc.statepoints this is extremely costly both memory and time |
| 1685 | // wise. |
| 1686 | SmallVector<AllocaInst *, 64> ToClobber; |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1687 | for (auto Pair : AllocaMap) { |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1688 | Value *Def = Pair.first; |
| 1689 | AllocaInst *Alloca = cast<AllocaInst>(Pair.second); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1690 | |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1691 | // This value was relocated |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1692 | if (VisitedLiveValues.count(Def)) { |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1693 | continue; |
| 1694 | } |
| 1695 | ToClobber.push_back(Alloca); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1696 | } |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1697 | |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1698 | auto InsertClobbersAt = [&](Instruction *IP) { |
| 1699 | for (auto *AI : ToClobber) { |
| 1700 | auto AIType = cast<PointerType>(AI->getType()); |
| 1701 | auto PT = cast<PointerType>(AIType->getElementType()); |
| 1702 | Constant *CPN = ConstantPointerNull::get(PT); |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1703 | StoreInst *Store = new StoreInst(CPN, AI); |
| 1704 | Store->insertBefore(IP); |
Philip Reames | e73300b | 2015-04-13 16:41:32 +0000 | [diff] [blame] | 1705 | } |
| 1706 | }; |
| 1707 | |
| 1708 | // Insert the clobbering stores. These may get intermixed with the |
| 1709 | // gc.results and gc.relocates, but that's fine. |
| 1710 | if (auto II = dyn_cast<InvokeInst>(Statepoint)) { |
| 1711 | InsertClobbersAt(II->getNormalDest()->getFirstInsertionPt()); |
| 1712 | InsertClobbersAt(II->getUnwindDest()->getFirstInsertionPt()); |
| 1713 | } else { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1714 | InsertClobbersAt(cast<Instruction>(Statepoint)->getNextNode()); |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1715 | } |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1716 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1717 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1718 | |
| 1719 | // Update use with load allocas and add store for gc_relocated. |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1720 | for (auto Pair : AllocaMap) { |
| 1721 | Value *Def = Pair.first; |
| 1722 | Value *Alloca = Pair.second; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1723 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1724 | // We pre-record the uses of allocas so that we dont have to worry about |
| 1725 | // later update that changes the user information.. |
| 1726 | |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1727 | SmallVector<Instruction *, 20> Uses; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1728 | // PERF: trade a linear scan for repeated reallocation |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1729 | Uses.reserve(std::distance(Def->user_begin(), Def->user_end())); |
| 1730 | for (User *U : Def->users()) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1731 | if (!isa<ConstantExpr>(U)) { |
| 1732 | // If the def has a ConstantExpr use, then the def is either a |
| 1733 | // ConstantExpr use itself or null. In either case |
| 1734 | // (recursively in the first, directly in the second), the oop |
| 1735 | // it is ultimately dependent on is null and this particular |
| 1736 | // use does not need to be fixed up. |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1737 | Uses.push_back(cast<Instruction>(U)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1741 | std::sort(Uses.begin(), Uses.end()); |
| 1742 | auto Last = std::unique(Uses.begin(), Uses.end()); |
| 1743 | Uses.erase(Last, Uses.end()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1744 | |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1745 | for (Instruction *Use : Uses) { |
| 1746 | if (isa<PHINode>(Use)) { |
| 1747 | PHINode *Phi = cast<PHINode>(Use); |
| 1748 | for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++) { |
| 1749 | if (Def == Phi->getIncomingValue(i)) { |
| 1750 | LoadInst *Load = new LoadInst( |
| 1751 | Alloca, "", Phi->getIncomingBlock(i)->getTerminator()); |
| 1752 | Phi->setIncomingValue(i, Load); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | } else { |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1756 | LoadInst *Load = new LoadInst(Alloca, "", Use); |
| 1757 | Use->replaceUsesOfWith(Def, Load); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1761 | // Emit store for the initial gc value. Store must be inserted after load, |
| 1762 | // otherwise store will be in alloca's use list and an extra load will be |
| 1763 | // inserted before it. |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1764 | StoreInst *Store = new StoreInst(Def, Alloca); |
| 1765 | if (Instruction *Inst = dyn_cast<Instruction>(Def)) { |
| 1766 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Inst)) { |
Philip Reames | 6da3785 | 2015-03-04 00:13:52 +0000 | [diff] [blame] | 1767 | // InvokeInst is a TerminatorInst so the store need to be inserted |
| 1768 | // into its normal destination block. |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1769 | BasicBlock *NormalDest = Invoke->getNormalDest(); |
| 1770 | Store->insertBefore(NormalDest->getFirstNonPHI()); |
Philip Reames | 6da3785 | 2015-03-04 00:13:52 +0000 | [diff] [blame] | 1771 | } else { |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1772 | assert(!Inst->isTerminator() && |
Philip Reames | 6da3785 | 2015-03-04 00:13:52 +0000 | [diff] [blame] | 1773 | "The only TerminatorInst that can produce a value is " |
| 1774 | "InvokeInst which is handled above."); |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1775 | Store->insertAfter(Inst); |
Philip Reames | 6da3785 | 2015-03-04 00:13:52 +0000 | [diff] [blame] | 1776 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1777 | } else { |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1778 | assert(isa<Argument>(Def)); |
| 1779 | Store->insertAfter(cast<Instruction>(Alloca)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
Igor Laevsky | 285fe84 | 2015-05-19 16:29:43 +0000 | [diff] [blame] | 1783 | assert(PromotableAllocas.size() == Live.size() + NumRematerializedValues && |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1784 | "we must have the same allocas with lives"); |
| 1785 | if (!PromotableAllocas.empty()) { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1786 | // Apply mem2reg to promote alloca to SSA |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1787 | PromoteMemToReg(PromotableAllocas, DT); |
| 1788 | } |
| 1789 | |
| 1790 | #ifndef NDEBUG |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1791 | for (auto &I : F.getEntryBlock()) |
| 1792 | if (isa<AllocaInst>(I)) |
Philip Reames | a6ebf07 | 2015-03-27 05:53:16 +0000 | [diff] [blame] | 1793 | InitialAllocaNum--; |
| 1794 | assert(InitialAllocaNum == 0 && "We must not introduce any extra allocas"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1795 | #endif |
| 1796 | } |
| 1797 | |
| 1798 | /// Implement a unique function which doesn't require we sort the input |
| 1799 | /// vector. Doing so has the effect of changing the output of a couple of |
| 1800 | /// tests in ways which make them less useful in testing fused safepoints. |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1801 | template <typename T> static void unique_unsorted(SmallVectorImpl<T> &Vec) { |
Benjamin Kramer | 258ea0d | 2015-06-13 19:50:38 +0000 | [diff] [blame] | 1802 | SmallSet<T, 8> Seen; |
| 1803 | Vec.erase(std::remove_if(Vec.begin(), Vec.end(), [&](const T &V) { |
| 1804 | return !Seen.insert(V).second; |
| 1805 | }), Vec.end()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1808 | /// Insert holders so that each Value is obviously live through the entire |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 1809 | /// lifetime of the call. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1810 | static void insertUseHolderAfter(CallSite &CS, const ArrayRef<Value *> Values, |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 1811 | SmallVectorImpl<CallInst *> &Holders) { |
Philip Reames | 2114275 | 2015-04-13 19:07:47 +0000 | [diff] [blame] | 1812 | if (Values.empty()) |
| 1813 | // No values to hold live, might as well not insert the empty holder |
| 1814 | return; |
| 1815 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1816 | Module *M = CS.getInstruction()->getParent()->getParent()->getParent(); |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 1817 | // Use a dummy vararg function to actually hold the values live |
| 1818 | Function *Func = cast<Function>(M->getOrInsertFunction( |
| 1819 | "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true))); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1820 | if (CS.isCall()) { |
| 1821 | // For call safepoints insert dummy calls right after safepoint |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 1822 | BasicBlock::iterator Next(CS.getInstruction()); |
| 1823 | Next++; |
| 1824 | Holders.push_back(CallInst::Create(Func, Values, "", Next)); |
| 1825 | return; |
| 1826 | } |
| 1827 | // For invoke safepooints insert dummy calls both in normal and |
| 1828 | // exceptional destination blocks |
| 1829 | auto *II = cast<InvokeInst>(CS.getInstruction()); |
| 1830 | Holders.push_back(CallInst::Create( |
| 1831 | Func, Values, "", II->getNormalDest()->getFirstInsertionPt())); |
| 1832 | Holders.push_back(CallInst::Create( |
| 1833 | Func, Values, "", II->getUnwindDest()->getFirstInsertionPt())); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static void findLiveReferences( |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1837 | Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate, |
| 1838 | MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) { |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1839 | GCPtrLivenessData OriginalLivenessData; |
| 1840 | computeLiveInValues(DT, F, OriginalLivenessData); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1841 | for (size_t i = 0; i < records.size(); i++) { |
| 1842 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1843 | const CallSite &CS = toUpdate[i]; |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 1844 | analyzeParsePointLiveness(DT, OriginalLivenessData, CS, info); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 1848 | /// Remove any vector of pointers from the live set by scalarizing them over the |
| 1849 | /// statepoint instruction. Adds the scalarized pieces to the live set. It |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1850 | /// would be preferable to include the vector in the statepoint itself, but |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1851 | /// the lowering code currently does not handle that. Extending it would be |
| 1852 | /// slightly non-trivial since it requires a format change. Given how rare |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1853 | /// such cases are (for the moment?) scalarizing is an acceptable compromise. |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1854 | static void splitVectorValues(Instruction *StatepointInst, |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 1855 | StatepointLiveSetTy &LiveSet, |
| 1856 | DenseMap<Value *, Value *>& PointerToBase, |
| 1857 | DominatorTree &DT) { |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1858 | SmallVector<Value *, 16> ToSplit; |
| 1859 | for (Value *V : LiveSet) |
| 1860 | if (isa<VectorType>(V->getType())) |
| 1861 | ToSplit.push_back(V); |
| 1862 | |
| 1863 | if (ToSplit.empty()) |
| 1864 | return; |
| 1865 | |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 1866 | DenseMap<Value *, SmallVector<Value *, 16>> ElementMapping; |
| 1867 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1868 | Function &F = *(StatepointInst->getParent()->getParent()); |
| 1869 | |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1870 | DenseMap<Value *, AllocaInst *> AllocaMap; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1871 | // First is normal return, second is exceptional return (invoke only) |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1872 | DenseMap<Value *, std::pair<Value *, Value *>> Replacements; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1873 | for (Value *V : ToSplit) { |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1874 | AllocaInst *Alloca = |
| 1875 | new AllocaInst(V->getType(), "", F.getEntryBlock().getFirstNonPHI()); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1876 | AllocaMap[V] = Alloca; |
| 1877 | |
| 1878 | VectorType *VT = cast<VectorType>(V->getType()); |
| 1879 | IRBuilder<> Builder(StatepointInst); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1880 | SmallVector<Value *, 16> Elements; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1881 | for (unsigned i = 0; i < VT->getNumElements(); i++) |
| 1882 | Elements.push_back(Builder.CreateExtractElement(V, Builder.getInt32(i))); |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 1883 | ElementMapping[V] = Elements; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1884 | |
| 1885 | auto InsertVectorReform = [&](Instruction *IP) { |
| 1886 | Builder.SetInsertPoint(IP); |
| 1887 | Builder.SetCurrentDebugLocation(IP->getDebugLoc()); |
| 1888 | Value *ResultVec = UndefValue::get(VT); |
| 1889 | for (unsigned i = 0; i < VT->getNumElements(); i++) |
| 1890 | ResultVec = Builder.CreateInsertElement(ResultVec, Elements[i], |
| 1891 | Builder.getInt32(i)); |
| 1892 | return ResultVec; |
| 1893 | }; |
| 1894 | |
| 1895 | if (isa<CallInst>(StatepointInst)) { |
| 1896 | BasicBlock::iterator Next(StatepointInst); |
| 1897 | Next++; |
| 1898 | Instruction *IP = &*(Next); |
| 1899 | Replacements[V].first = InsertVectorReform(IP); |
| 1900 | Replacements[V].second = nullptr; |
| 1901 | } else { |
| 1902 | InvokeInst *Invoke = cast<InvokeInst>(StatepointInst); |
| 1903 | // We've already normalized - check that we don't have shared destination |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1904 | // blocks |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1905 | BasicBlock *NormalDest = Invoke->getNormalDest(); |
| 1906 | assert(!isa<PHINode>(NormalDest->begin())); |
| 1907 | BasicBlock *UnwindDest = Invoke->getUnwindDest(); |
| 1908 | assert(!isa<PHINode>(UnwindDest->begin())); |
| 1909 | // Insert insert element sequences in both successors |
| 1910 | Instruction *IP = &*(NormalDest->getFirstInsertionPt()); |
| 1911 | Replacements[V].first = InsertVectorReform(IP); |
| 1912 | IP = &*(UnwindDest->getFirstInsertionPt()); |
| 1913 | Replacements[V].second = InsertVectorReform(IP); |
| 1914 | } |
| 1915 | } |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 1916 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1917 | for (Value *V : ToSplit) { |
| 1918 | AllocaInst *Alloca = AllocaMap[V]; |
| 1919 | |
| 1920 | // Capture all users before we start mutating use lists |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1921 | SmallVector<Instruction *, 16> Users; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1922 | for (User *U : V->users()) |
| 1923 | Users.push_back(cast<Instruction>(U)); |
| 1924 | |
| 1925 | for (Instruction *I : Users) { |
| 1926 | if (auto Phi = dyn_cast<PHINode>(I)) { |
| 1927 | for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++) |
| 1928 | if (V == Phi->getIncomingValue(i)) { |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1929 | LoadInst *Load = new LoadInst( |
| 1930 | Alloca, "", Phi->getIncomingBlock(i)->getTerminator()); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1931 | Phi->setIncomingValue(i, Load); |
| 1932 | } |
| 1933 | } else { |
| 1934 | LoadInst *Load = new LoadInst(Alloca, "", I); |
| 1935 | I->replaceUsesOfWith(V, Load); |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | // Store the original value and the replacement value into the alloca |
| 1940 | StoreInst *Store = new StoreInst(V, Alloca); |
| 1941 | if (auto I = dyn_cast<Instruction>(V)) |
| 1942 | Store->insertAfter(I); |
| 1943 | else |
| 1944 | Store->insertAfter(Alloca); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1945 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1946 | // Normal return for invoke, or call return |
| 1947 | Instruction *Replacement = cast<Instruction>(Replacements[V].first); |
| 1948 | (new StoreInst(Replacement, Alloca))->insertAfter(Replacement); |
| 1949 | // Unwind return for invoke only |
| 1950 | Replacement = cast_or_null<Instruction>(Replacements[V].second); |
| 1951 | if (Replacement) |
| 1952 | (new StoreInst(Replacement, Alloca))->insertAfter(Replacement); |
| 1953 | } |
| 1954 | |
| 1955 | // apply mem2reg to promote alloca to SSA |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 1956 | SmallVector<AllocaInst *, 16> Allocas; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1957 | for (Value *V : ToSplit) |
| 1958 | Allocas.push_back(AllocaMap[V]); |
| 1959 | PromoteMemToReg(Allocas, DT); |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Update our tracking of live pointers and base mappings to account for the |
| 1962 | // changes we just made. |
| 1963 | for (Value *V : ToSplit) { |
| 1964 | auto &Elements = ElementMapping[V]; |
| 1965 | |
| 1966 | LiveSet.erase(V); |
| 1967 | LiveSet.insert(Elements.begin(), Elements.end()); |
| 1968 | // We need to update the base mapping as well. |
| 1969 | assert(PointerToBase.count(V)); |
| 1970 | Value *OldBase = PointerToBase[V]; |
| 1971 | auto &BaseElements = ElementMapping[OldBase]; |
| 1972 | PointerToBase.erase(V); |
| 1973 | assert(Elements.size() == BaseElements.size()); |
| 1974 | for (unsigned i = 0; i < Elements.size(); i++) { |
| 1975 | Value *Elem = Elements[i]; |
| 1976 | PointerToBase[Elem] = BaseElements[i]; |
| 1977 | } |
| 1978 | } |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1981 | // Helper function for the "rematerializeLiveValues". It walks use chain |
| 1982 | // starting from the "CurrentValue" until it meets "BaseValue". Only "simple" |
| 1983 | // values are visited (currently it is GEP's and casts). Returns true if it |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1984 | // successfully reached "BaseValue" and false otherwise. |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 1985 | // Fills "ChainToBase" array with all visited values. "BaseValue" is not |
| 1986 | // recorded. |
| 1987 | static bool findRematerializableChainToBasePointer( |
| 1988 | SmallVectorImpl<Instruction*> &ChainToBase, |
| 1989 | Value *CurrentValue, Value *BaseValue) { |
| 1990 | |
| 1991 | // We have found a base value |
| 1992 | if (CurrentValue == BaseValue) { |
| 1993 | return true; |
| 1994 | } |
| 1995 | |
| 1996 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurrentValue)) { |
| 1997 | ChainToBase.push_back(GEP); |
| 1998 | return findRematerializableChainToBasePointer(ChainToBase, |
| 1999 | GEP->getPointerOperand(), |
| 2000 | BaseValue); |
| 2001 | } |
| 2002 | |
| 2003 | if (CastInst *CI = dyn_cast<CastInst>(CurrentValue)) { |
| 2004 | Value *Def = CI->stripPointerCasts(); |
| 2005 | |
| 2006 | // This two checks are basically similar. First one is here for the |
| 2007 | // consistency with findBasePointers logic. |
| 2008 | assert(!isa<CastInst>(Def) && "not a pointer cast found"); |
| 2009 | if (!CI->isNoopCast(CI->getModule()->getDataLayout())) |
| 2010 | return false; |
| 2011 | |
| 2012 | ChainToBase.push_back(CI); |
| 2013 | return findRematerializableChainToBasePointer(ChainToBase, Def, BaseValue); |
| 2014 | } |
| 2015 | |
| 2016 | // Not supported instruction in the chain |
| 2017 | return false; |
| 2018 | } |
| 2019 | |
| 2020 | // Helper function for the "rematerializeLiveValues". Compute cost of the use |
| 2021 | // chain we are going to rematerialize. |
| 2022 | static unsigned |
| 2023 | chainToBasePointerCost(SmallVectorImpl<Instruction*> &Chain, |
| 2024 | TargetTransformInfo &TTI) { |
| 2025 | unsigned Cost = 0; |
| 2026 | |
| 2027 | for (Instruction *Instr : Chain) { |
| 2028 | if (CastInst *CI = dyn_cast<CastInst>(Instr)) { |
| 2029 | assert(CI->isNoopCast(CI->getModule()->getDataLayout()) && |
| 2030 | "non noop cast is found during rematerialization"); |
| 2031 | |
| 2032 | Type *SrcTy = CI->getOperand(0)->getType(); |
| 2033 | Cost += TTI.getCastInstrCost(CI->getOpcode(), CI->getType(), SrcTy); |
| 2034 | |
| 2035 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Instr)) { |
| 2036 | // Cost of the address calculation |
| 2037 | Type *ValTy = GEP->getPointerOperandType()->getPointerElementType(); |
| 2038 | Cost += TTI.getAddressComputationCost(ValTy); |
| 2039 | |
| 2040 | // And cost of the GEP itself |
| 2041 | // TODO: Use TTI->getGEPCost here (it exists, but appears to be not |
| 2042 | // allowed for the external usage) |
| 2043 | if (!GEP->hasAllConstantIndices()) |
| 2044 | Cost += 2; |
| 2045 | |
| 2046 | } else { |
| 2047 | llvm_unreachable("unsupported instruciton type during rematerialization"); |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | return Cost; |
| 2052 | } |
| 2053 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2054 | // From the statepoint live set pick values that are cheaper to recompute then |
| 2055 | // to relocate. Remove this values from the live set, rematerialize them after |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2056 | // statepoint and record them in "Info" structure. Note that similar to |
| 2057 | // relocated values we don't do any user adjustments here. |
| 2058 | static void rematerializeLiveValues(CallSite CS, |
| 2059 | PartiallyConstructedSafepointRecord &Info, |
| 2060 | TargetTransformInfo &TTI) { |
Aaron Ballman | ff7d4fa | 2015-05-20 14:53:50 +0000 | [diff] [blame] | 2061 | const unsigned int ChainLengthThreshold = 10; |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 2062 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2063 | // Record values we are going to delete from this statepoint live set. |
| 2064 | // We can not di this in following loop due to iterator invalidation. |
| 2065 | SmallVector<Value *, 32> LiveValuesToBeDeleted; |
| 2066 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2067 | for (Value *LiveValue: Info.LiveSet) { |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2068 | // For each live pointer find it's defining chain |
| 2069 | SmallVector<Instruction *, 3> ChainToBase; |
Philip Reames | 74ce2e7 | 2015-07-21 16:51:17 +0000 | [diff] [blame] | 2070 | assert(Info.PointerToBase.count(LiveValue)); |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2071 | bool FoundChain = |
| 2072 | findRematerializableChainToBasePointer(ChainToBase, |
| 2073 | LiveValue, |
| 2074 | Info.PointerToBase[LiveValue]); |
| 2075 | // Nothing to do, or chain is too long |
| 2076 | if (!FoundChain || |
| 2077 | ChainToBase.size() == 0 || |
| 2078 | ChainToBase.size() > ChainLengthThreshold) |
| 2079 | continue; |
| 2080 | |
| 2081 | // Compute cost of this chain |
| 2082 | unsigned Cost = chainToBasePointerCost(ChainToBase, TTI); |
| 2083 | // TODO: We can also account for cases when we will be able to remove some |
| 2084 | // of the rematerialized values by later optimization passes. I.e if |
| 2085 | // we rematerialized several intersecting chains. Or if original values |
| 2086 | // don't have any uses besides this statepoint. |
| 2087 | |
| 2088 | // For invokes we need to rematerialize each chain twice - for normal and |
| 2089 | // for unwind basic blocks. Model this by multiplying cost by two. |
| 2090 | if (CS.isInvoke()) { |
| 2091 | Cost *= 2; |
| 2092 | } |
| 2093 | // If it's too expensive - skip it |
| 2094 | if (Cost >= RematerializationThreshold) |
| 2095 | continue; |
| 2096 | |
| 2097 | // Remove value from the live set |
| 2098 | LiveValuesToBeDeleted.push_back(LiveValue); |
| 2099 | |
| 2100 | // Clone instructions and record them inside "Info" structure |
| 2101 | |
| 2102 | // Walk backwards to visit top-most instructions first |
| 2103 | std::reverse(ChainToBase.begin(), ChainToBase.end()); |
| 2104 | |
| 2105 | // Utility function which clones all instructions from "ChainToBase" |
| 2106 | // and inserts them before "InsertBefore". Returns rematerialized value |
| 2107 | // which should be used after statepoint. |
| 2108 | auto rematerializeChain = [&ChainToBase](Instruction *InsertBefore) { |
| 2109 | Instruction *LastClonedValue = nullptr; |
| 2110 | Instruction *LastValue = nullptr; |
| 2111 | for (Instruction *Instr: ChainToBase) { |
| 2112 | // Only GEP's and casts are suported as we need to be careful to not |
| 2113 | // introduce any new uses of pointers not in the liveset. |
| 2114 | // Note that it's fine to introduce new uses of pointers which were |
| 2115 | // otherwise not used after this statepoint. |
| 2116 | assert(isa<GetElementPtrInst>(Instr) || isa<CastInst>(Instr)); |
| 2117 | |
| 2118 | Instruction *ClonedValue = Instr->clone(); |
| 2119 | ClonedValue->insertBefore(InsertBefore); |
| 2120 | ClonedValue->setName(Instr->getName() + ".remat"); |
| 2121 | |
| 2122 | // If it is not first instruction in the chain then it uses previously |
| 2123 | // cloned value. We should update it to use cloned value. |
| 2124 | if (LastClonedValue) { |
| 2125 | assert(LastValue); |
| 2126 | ClonedValue->replaceUsesOfWith(LastValue, LastClonedValue); |
| 2127 | #ifndef NDEBUG |
Igor Laevsky | d83f697 | 2015-05-21 13:02:14 +0000 | [diff] [blame] | 2128 | // Assert that cloned instruction does not use any instructions from |
| 2129 | // this chain other than LastClonedValue |
| 2130 | for (auto OpValue : ClonedValue->operand_values()) { |
| 2131 | assert(std::find(ChainToBase.begin(), ChainToBase.end(), OpValue) == |
| 2132 | ChainToBase.end() && |
| 2133 | "incorrect use in rematerialization chain"); |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2134 | } |
| 2135 | #endif |
| 2136 | } |
| 2137 | |
| 2138 | LastClonedValue = ClonedValue; |
| 2139 | LastValue = Instr; |
| 2140 | } |
| 2141 | assert(LastClonedValue); |
| 2142 | return LastClonedValue; |
| 2143 | }; |
| 2144 | |
| 2145 | // Different cases for calls and invokes. For invokes we need to clone |
| 2146 | // instructions both on normal and unwind path. |
| 2147 | if (CS.isCall()) { |
| 2148 | Instruction *InsertBefore = CS.getInstruction()->getNextNode(); |
| 2149 | assert(InsertBefore); |
| 2150 | Instruction *RematerializedValue = rematerializeChain(InsertBefore); |
| 2151 | Info.RematerializedValues[RematerializedValue] = LiveValue; |
| 2152 | } else { |
| 2153 | InvokeInst *Invoke = cast<InvokeInst>(CS.getInstruction()); |
| 2154 | |
| 2155 | Instruction *NormalInsertBefore = |
| 2156 | Invoke->getNormalDest()->getFirstInsertionPt(); |
| 2157 | Instruction *UnwindInsertBefore = |
| 2158 | Invoke->getUnwindDest()->getFirstInsertionPt(); |
| 2159 | |
| 2160 | Instruction *NormalRematerializedValue = |
| 2161 | rematerializeChain(NormalInsertBefore); |
| 2162 | Instruction *UnwindRematerializedValue = |
| 2163 | rematerializeChain(UnwindInsertBefore); |
| 2164 | |
| 2165 | Info.RematerializedValues[NormalRematerializedValue] = LiveValue; |
| 2166 | Info.RematerializedValues[UnwindRematerializedValue] = LiveValue; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | // Remove rematerializaed values from the live set |
| 2171 | for (auto LiveValue: LiveValuesToBeDeleted) { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2172 | Info.LiveSet.erase(LiveValue); |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2176 | static bool insertParsePoints(Function &F, DominatorTree &DT, Pass *P, |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2177 | SmallVectorImpl<CallSite> &ToUpdate) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2178 | #ifndef NDEBUG |
| 2179 | // sanity check the input |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2180 | std::set<CallSite> Uniqued; |
| 2181 | Uniqued.insert(ToUpdate.begin(), ToUpdate.end()); |
| 2182 | assert(Uniqued.size() == ToUpdate.size() && "no duplicates please!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2183 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2184 | for (CallSite CS : ToUpdate) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2185 | assert(CS.getInstruction()->getParent()->getParent() == &F); |
| 2186 | assert(isStatepoint(CS) && "expected to already be a deopt statepoint"); |
| 2187 | } |
| 2188 | #endif |
| 2189 | |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 2190 | // When inserting gc.relocates for invokes, we need to be able to insert at |
| 2191 | // the top of the successor blocks. See the comment on |
| 2192 | // normalForInvokeSafepoint on exactly what is needed. Note that this step |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 2193 | // may restructure the CFG. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2194 | for (CallSite CS : ToUpdate) { |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 2195 | if (!CS.isInvoke()) |
| 2196 | continue; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2197 | auto *II = cast<InvokeInst>(CS.getInstruction()); |
| 2198 | normalizeForInvokeSafepoint(II->getNormalDest(), II->getParent(), DT); |
| 2199 | normalizeForInvokeSafepoint(II->getUnwindDest(), II->getParent(), DT); |
Philip Reames | f209a15 | 2015-04-13 20:00:30 +0000 | [diff] [blame] | 2200 | } |
Philip Reames | 69e51ca | 2015-04-13 18:07:21 +0000 | [diff] [blame] | 2201 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2202 | // A list of dummy calls added to the IR to keep various values obviously |
| 2203 | // live in the IR. We'll remove all of these when done. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2204 | SmallVector<CallInst *, 64> Holders; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2205 | |
| 2206 | // Insert a dummy call with all of the arguments to the vm_state we'll need |
| 2207 | // for the actual safepoint insertion. This ensures reference arguments in |
| 2208 | // the deopt argument list are considered live through the safepoint (and |
| 2209 | // thus makes sure they get relocated.) |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2210 | for (CallSite CS : ToUpdate) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2211 | Statepoint StatepointCS(CS); |
| 2212 | |
| 2213 | SmallVector<Value *, 64> DeoptValues; |
| 2214 | for (Use &U : StatepointCS.vm_state_args()) { |
| 2215 | Value *Arg = cast<Value>(&U); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 2216 | assert(!isUnhandledGCPointerType(Arg->getType()) && |
| 2217 | "support for FCA unimplemented"); |
| 2218 | if (isHandledGCPointerType(Arg->getType())) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2219 | DeoptValues.push_back(Arg); |
| 2220 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2221 | insertUseHolderAfter(CS, DeoptValues, Holders); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2224 | SmallVector<PartiallyConstructedSafepointRecord, 64> Records(ToUpdate.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2225 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2226 | // A) Identify all gc pointers which are statically live at the given call |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2227 | // site. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2228 | findLiveReferences(F, DT, P, ToUpdate, Records); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2229 | |
| 2230 | // B) Find the base pointers for each live pointer |
| 2231 | /* scope for caching */ { |
| 2232 | // Cache the 'defining value' relation used in the computation and |
| 2233 | // insertion of base phis and selects. This ensures that we don't insert |
| 2234 | // large numbers of duplicate base_phis. |
| 2235 | DefiningValueMapTy DVCache; |
| 2236 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2237 | for (size_t i = 0; i < Records.size(); i++) { |
| 2238 | PartiallyConstructedSafepointRecord &info = Records[i]; |
| 2239 | findBasePointers(DT, DVCache, ToUpdate[i], info); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2240 | } |
| 2241 | } // end of cache scope |
| 2242 | |
| 2243 | // The base phi insertion logic (for any safepoint) may have inserted new |
| 2244 | // instructions which are now live at some safepoint. The simplest such |
| 2245 | // example is: |
| 2246 | // loop: |
| 2247 | // phi a <-- will be a new base_phi here |
| 2248 | // safepoint 1 <-- that needs to be live here |
| 2249 | // gep a + 1 |
| 2250 | // safepoint 2 |
| 2251 | // br loop |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2252 | // We insert some dummy calls after each safepoint to definitely hold live |
| 2253 | // the base pointers which were identified for that safepoint. We'll then |
| 2254 | // ask liveness for _every_ base inserted to see what is now live. Then we |
| 2255 | // remove the dummy calls. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2256 | Holders.reserve(Holders.size() + Records.size()); |
| 2257 | for (size_t i = 0; i < Records.size(); i++) { |
| 2258 | PartiallyConstructedSafepointRecord &Info = Records[i]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2259 | |
| 2260 | SmallVector<Value *, 128> Bases; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2261 | for (auto Pair : Info.PointerToBase) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2262 | Bases.push_back(Pair.second); |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2263 | |
| 2264 | insertUseHolderAfter(ToUpdate[i], Bases, Holders); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2267 | // By selecting base pointers, we've effectively inserted new uses. Thus, we |
| 2268 | // need to rerun liveness. We may *also* have inserted new defs, but that's |
| 2269 | // not the key issue. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2270 | recomputeLiveInValues(F, DT, P, ToUpdate, Records); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2271 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2272 | if (PrintBasePointers) { |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2273 | for (auto &Info : Records) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2274 | errs() << "Base Pairs: (w/Relocation)\n"; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2275 | for (auto Pair : Info.PointerToBase) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2276 | errs() << " derived %" << Pair.first->getName() << " base %" |
| 2277 | << Pair.second->getName() << "\n"; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2280 | |
| 2281 | for (CallInst *CI : Holders) |
| 2282 | CI->eraseFromParent(); |
| 2283 | |
| 2284 | Holders.clear(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2285 | |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 2286 | // Do a limited scalarization of any live at safepoint vector values which |
| 2287 | // contain pointers. This enables this pass to run after vectorization at |
| 2288 | // the cost of some possible performance loss. TODO: it would be nice to |
| 2289 | // natively support vectors all the way through the backend so we don't need |
| 2290 | // to scalarize here. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2291 | for (size_t i = 0; i < Records.size(); i++) { |
| 2292 | PartiallyConstructedSafepointRecord &Info = Records[i]; |
| 2293 | Instruction *Statepoint = ToUpdate[i].getInstruction(); |
| 2294 | splitVectorValues(cast<Instruction>(Statepoint), Info.LiveSet, |
| 2295 | Info.PointerToBase, DT); |
Philip Reames | 8fe7f13 | 2015-06-26 22:47:37 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2298 | // In order to reduce live set of statepoint we might choose to rematerialize |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2299 | // some values instead of relocating them. This is purely an optimization and |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2300 | // does not influence correctness. |
| 2301 | TargetTransformInfo &TTI = |
| 2302 | P->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 2303 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2304 | for (size_t i = 0; i < Records.size(); i++) |
| 2305 | rematerializeLiveValues(ToUpdate[i], Records[i], TTI); |
Igor Laevsky | e031718 | 2015-05-19 15:59:05 +0000 | [diff] [blame] | 2306 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2307 | // Now run through and replace the existing statepoints with new ones with |
| 2308 | // the live variables listed. We do not yet update uses of the values being |
| 2309 | // relocated. We have references to live variables that need to |
| 2310 | // survive to the last iteration of this loop. (By construction, the |
| 2311 | // previous statepoint can not be a live variable, thus we can and remove |
| 2312 | // the old statepoint calls as we go.) |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2313 | for (size_t i = 0; i < Records.size(); i++) |
| 2314 | makeStatepointExplicit(DT, ToUpdate[i], Records[i]); |
| 2315 | |
| 2316 | ToUpdate.clear(); // prevent accident use of invalid CallSites |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2317 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2318 | // Do all the fixups of the original live variables to their relocated selves |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2319 | SmallVector<Value *, 128> Live; |
| 2320 | for (size_t i = 0; i < Records.size(); i++) { |
| 2321 | PartiallyConstructedSafepointRecord &Info = Records[i]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2322 | // We can't simply save the live set from the original insertion. One of |
| 2323 | // the live values might be the result of a call which needs a safepoint. |
| 2324 | // That Value* no longer exists and we need to use the new gc_result. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2325 | // Thankfully, the live set is embedded in the statepoint (and updated), so |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2326 | // we just grab that. |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2327 | Statepoint Statepoint(Info.StatepointToken); |
| 2328 | Live.insert(Live.end(), Statepoint.gc_args_begin(), |
| 2329 | Statepoint.gc_args_end()); |
Philip Reames | 9a2e01d | 2015-04-13 17:35:55 +0000 | [diff] [blame] | 2330 | #ifndef NDEBUG |
| 2331 | // Do some basic sanity checks on our liveness results before performing |
| 2332 | // relocation. Relocation can and will turn mistakes in liveness results |
| 2333 | // into non-sensical code which is must harder to debug. |
| 2334 | // TODO: It would be nice to test consistency as well |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2335 | assert(DT.isReachableFromEntry(Info.StatepointToken->getParent()) && |
Philip Reames | 9a2e01d | 2015-04-13 17:35:55 +0000 | [diff] [blame] | 2336 | "statepoint must be reachable or liveness is meaningless"); |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2337 | for (Value *V : Statepoint.gc_args()) { |
Philip Reames | 9a2e01d | 2015-04-13 17:35:55 +0000 | [diff] [blame] | 2338 | if (!isa<Instruction>(V)) |
| 2339 | // Non-instruction values trivial dominate all possible uses |
| 2340 | continue; |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2341 | auto *LiveInst = cast<Instruction>(V); |
Philip Reames | 9a2e01d | 2015-04-13 17:35:55 +0000 | [diff] [blame] | 2342 | assert(DT.isReachableFromEntry(LiveInst->getParent()) && |
| 2343 | "unreachable values should never be live"); |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2344 | assert(DT.dominates(LiveInst, Info.StatepointToken) && |
Philip Reames | 9a2e01d | 2015-04-13 17:35:55 +0000 | [diff] [blame] | 2345 | "basic SSA liveness expectation violated by liveness analysis"); |
| 2346 | } |
| 2347 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2348 | } |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2349 | unique_unsorted(Live); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2350 | |
Nick Lewycky | eb3231e | 2015-02-20 07:14:02 +0000 | [diff] [blame] | 2351 | #ifndef NDEBUG |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2352 | // sanity check |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2353 | for (auto *Ptr : Live) |
| 2354 | assert(isGCPointerType(Ptr->getType()) && "must be a gc pointer type"); |
Nick Lewycky | eb3231e | 2015-02-20 07:14:02 +0000 | [diff] [blame] | 2355 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2356 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2357 | relocationViaAlloca(F, DT, Live, Records); |
| 2358 | return !Records.empty(); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 2361 | // Handles both return values and arguments for Functions and CallSites. |
| 2362 | template <typename AttrHolder> |
| 2363 | static void RemoveDerefAttrAtIndex(LLVMContext &Ctx, AttrHolder &AH, |
| 2364 | unsigned Index) { |
| 2365 | AttrBuilder R; |
| 2366 | if (AH.getDereferenceableBytes(Index)) |
| 2367 | R.addAttribute(Attribute::get(Ctx, Attribute::Dereferenceable, |
| 2368 | AH.getDereferenceableBytes(Index))); |
| 2369 | if (AH.getDereferenceableOrNullBytes(Index)) |
| 2370 | R.addAttribute(Attribute::get(Ctx, Attribute::DereferenceableOrNull, |
| 2371 | AH.getDereferenceableOrNullBytes(Index))); |
| 2372 | |
| 2373 | if (!R.empty()) |
| 2374 | AH.setAttributes(AH.getAttributes().removeAttributes( |
| 2375 | Ctx, Index, AttributeSet::get(Ctx, Index, R))); |
Vasileios Kalintiris | 9f77f61 | 2015-06-03 08:51:30 +0000 | [diff] [blame] | 2376 | } |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 2377 | |
| 2378 | void |
| 2379 | RewriteStatepointsForGC::stripDereferenceabilityInfoFromPrototype(Function &F) { |
| 2380 | LLVMContext &Ctx = F.getContext(); |
| 2381 | |
| 2382 | for (Argument &A : F.args()) |
| 2383 | if (isa<PointerType>(A.getType())) |
| 2384 | RemoveDerefAttrAtIndex(Ctx, F, A.getArgNo() + 1); |
| 2385 | |
| 2386 | if (isa<PointerType>(F.getReturnType())) |
| 2387 | RemoveDerefAttrAtIndex(Ctx, F, AttributeSet::ReturnIndex); |
| 2388 | } |
| 2389 | |
| 2390 | void RewriteStatepointsForGC::stripDereferenceabilityInfoFromBody(Function &F) { |
| 2391 | if (F.empty()) |
| 2392 | return; |
| 2393 | |
| 2394 | LLVMContext &Ctx = F.getContext(); |
| 2395 | MDBuilder Builder(Ctx); |
| 2396 | |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 2397 | for (Instruction &I : instructions(F)) { |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 2398 | if (const MDNode *MD = I.getMetadata(LLVMContext::MD_tbaa)) { |
| 2399 | assert(MD->getNumOperands() < 5 && "unrecognized metadata shape!"); |
| 2400 | bool IsImmutableTBAA = |
| 2401 | MD->getNumOperands() == 4 && |
| 2402 | mdconst::extract<ConstantInt>(MD->getOperand(3))->getValue() == 1; |
| 2403 | |
| 2404 | if (!IsImmutableTBAA) |
| 2405 | continue; // no work to do, MD_tbaa is already marked mutable |
| 2406 | |
| 2407 | MDNode *Base = cast<MDNode>(MD->getOperand(0)); |
| 2408 | MDNode *Access = cast<MDNode>(MD->getOperand(1)); |
| 2409 | uint64_t Offset = |
| 2410 | mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue(); |
| 2411 | |
| 2412 | MDNode *MutableTBAA = |
| 2413 | Builder.createTBAAStructTagNode(Base, Access, Offset); |
| 2414 | I.setMetadata(LLVMContext::MD_tbaa, MutableTBAA); |
| 2415 | } |
| 2416 | |
| 2417 | if (CallSite CS = CallSite(&I)) { |
| 2418 | for (int i = 0, e = CS.arg_size(); i != e; i++) |
| 2419 | if (isa<PointerType>(CS.getArgument(i)->getType())) |
| 2420 | RemoveDerefAttrAtIndex(Ctx, CS, i + 1); |
| 2421 | if (isa<PointerType>(CS.getType())) |
| 2422 | RemoveDerefAttrAtIndex(Ctx, CS, AttributeSet::ReturnIndex); |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2427 | /// Returns true if this function should be rewritten by this pass. The main |
| 2428 | /// point of this function is as an extension point for custom logic. |
| 2429 | static bool shouldRewriteStatepointsIn(Function &F) { |
| 2430 | // TODO: This should check the GCStrategy |
Philip Reames | 2ef029c | 2015-02-20 18:56:14 +0000 | [diff] [blame] | 2431 | if (F.hasGC()) { |
NAKAMURA Takumi | fb3bd71 | 2015-05-25 01:43:23 +0000 | [diff] [blame] | 2432 | const char *FunctionGCName = F.getGC(); |
| 2433 | const StringRef StatepointExampleName("statepoint-example"); |
| 2434 | const StringRef CoreCLRName("coreclr"); |
| 2435 | return (StatepointExampleName == FunctionGCName) || |
NAKAMURA Takumi | 5582a6a | 2015-05-25 01:43:34 +0000 | [diff] [blame] | 2436 | (CoreCLRName == FunctionGCName); |
| 2437 | } else |
Philip Reames | 2ef029c | 2015-02-20 18:56:14 +0000 | [diff] [blame] | 2438 | return false; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Sanjoy Das | 353a19e | 2015-06-02 22:33:37 +0000 | [diff] [blame] | 2441 | void RewriteStatepointsForGC::stripDereferenceabilityInfo(Module &M) { |
| 2442 | #ifndef NDEBUG |
| 2443 | assert(std::any_of(M.begin(), M.end(), shouldRewriteStatepointsIn) && |
| 2444 | "precondition!"); |
| 2445 | #endif |
| 2446 | |
| 2447 | for (Function &F : M) |
| 2448 | stripDereferenceabilityInfoFromPrototype(F); |
| 2449 | |
| 2450 | for (Function &F : M) |
| 2451 | stripDereferenceabilityInfoFromBody(F); |
| 2452 | } |
| 2453 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2454 | bool RewriteStatepointsForGC::runOnFunction(Function &F) { |
| 2455 | // Nothing to do for declarations. |
| 2456 | if (F.isDeclaration() || F.empty()) |
| 2457 | return false; |
| 2458 | |
| 2459 | // Policy choice says not to rewrite - the most common reason is that we're |
| 2460 | // compiling code without a GCStrategy. |
| 2461 | if (!shouldRewriteStatepointsIn(F)) |
| 2462 | return false; |
| 2463 | |
Sanjoy Das | ea45f0e | 2015-06-02 22:33:34 +0000 | [diff] [blame] | 2464 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 2465 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2466 | // Gather all the statepoints which need rewritten. Be careful to only |
| 2467 | // consider those in reachable code since we need to ask dominance queries |
| 2468 | // when rewriting. We'll delete the unreachable ones in a moment. |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 2469 | SmallVector<CallSite, 64> ParsePointNeeded; |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 2470 | bool HasUnreachableStatepoint = false; |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 2471 | for (Instruction &I : instructions(F)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2472 | // TODO: only the ones with the flag set! |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2473 | if (isStatepoint(I)) { |
| 2474 | if (DT.isReachableFromEntry(I.getParent())) |
| 2475 | ParsePointNeeded.push_back(CallSite(&I)); |
| 2476 | else |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 2477 | HasUnreachableStatepoint = true; |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2478 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2481 | bool MadeChange = false; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame] | 2482 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2483 | // Delete any unreachable statepoints so that we don't have unrewritten |
| 2484 | // statepoints surviving this pass. This makes testing easier and the |
| 2485 | // resulting IR less confusing to human readers. Rather than be fancy, we |
| 2486 | // just reuse a utility function which removes the unreachable blocks. |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 2487 | if (HasUnreachableStatepoint) |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2488 | MadeChange |= removeUnreachableBlocks(F); |
| 2489 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2490 | // Return early if no work to do. |
| 2491 | if (ParsePointNeeded.empty()) |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2492 | return MadeChange; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2493 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2494 | // As a prepass, go ahead and aggressively destroy single entry phi nodes. |
| 2495 | // These are created by LCSSA. They have the effect of increasing the size |
| 2496 | // of liveness sets for no good reason. It may be harder to do this post |
| 2497 | // insertion since relocations and base phis can confuse things. |
| 2498 | for (BasicBlock &BB : F) |
| 2499 | if (BB.getUniquePredecessor()) { |
| 2500 | MadeChange = true; |
| 2501 | FoldSingleEntryPHINodes(&BB); |
| 2502 | } |
| 2503 | |
Philip Reames | 971dc3a | 2015-08-12 22:11:45 +0000 | [diff] [blame] | 2504 | // Before we start introducing relocations, we want to tweak the IR a bit to |
| 2505 | // avoid unfortunate code generation effects. The main example is that we |
| 2506 | // want to try to make sure the comparison feeding a branch is after any |
| 2507 | // safepoints. Otherwise, we end up with a comparison of pre-relocation |
| 2508 | // values feeding a branch after relocation. This is semantically correct, |
| 2509 | // but results in extra register pressure since both the pre-relocation and |
| 2510 | // post-relocation copies must be available in registers. For code without |
| 2511 | // relocations this is handled elsewhere, but teaching the scheduler to |
| 2512 | // reverse the transform we're about to do would be slightly complex. |
| 2513 | // Note: This may extend the live range of the inputs to the icmp and thus |
| 2514 | // increase the liveset of any statepoint we move over. This is profitable |
| 2515 | // as long as all statepoints are in rare blocks. If we had in-register |
| 2516 | // lowering for live values this would be a much safer transform. |
| 2517 | auto getConditionInst = [](TerminatorInst *TI) -> Instruction* { |
| 2518 | if (auto *BI = dyn_cast<BranchInst>(TI)) |
| 2519 | if (BI->isConditional()) |
| 2520 | return dyn_cast<Instruction>(BI->getCondition()); |
| 2521 | // TODO: Extend this to handle switches |
| 2522 | return nullptr; |
| 2523 | }; |
| 2524 | for (BasicBlock &BB : F) { |
| 2525 | TerminatorInst *TI = BB.getTerminator(); |
| 2526 | if (auto *Cond = getConditionInst(TI)) |
| 2527 | // TODO: Handle more than just ICmps here. We should be able to move |
| 2528 | // most instructions without side effects or memory access. |
| 2529 | if (isa<ICmpInst>(Cond) && Cond->hasOneUse()) { |
| 2530 | MadeChange = true; |
| 2531 | Cond->moveBefore(TI); |
| 2532 | } |
| 2533 | } |
| 2534 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2535 | MadeChange |= insertParsePoints(F, DT, this, ParsePointNeeded); |
| 2536 | return MadeChange; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2537 | } |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2538 | |
| 2539 | // liveness computation via standard dataflow |
| 2540 | // ------------------------------------------------------------------- |
| 2541 | |
| 2542 | // TODO: Consider using bitvectors for liveness, the set of potentially |
| 2543 | // interesting values should be small and easy to pre-compute. |
| 2544 | |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2545 | /// Compute the live-in set for the location rbegin starting from |
| 2546 | /// the live-out set of the basic block |
| 2547 | static void computeLiveInValues(BasicBlock::reverse_iterator rbegin, |
| 2548 | BasicBlock::reverse_iterator rend, |
| 2549 | DenseSet<Value *> &LiveTmp) { |
| 2550 | |
| 2551 | for (BasicBlock::reverse_iterator ritr = rbegin; ritr != rend; ritr++) { |
| 2552 | Instruction *I = &*ritr; |
| 2553 | |
| 2554 | // KILL/Def - Remove this definition from LiveIn |
| 2555 | LiveTmp.erase(I); |
| 2556 | |
| 2557 | // Don't consider *uses* in PHI nodes, we handle their contribution to |
| 2558 | // predecessor blocks when we seed the LiveOut sets |
| 2559 | if (isa<PHINode>(I)) |
| 2560 | continue; |
| 2561 | |
| 2562 | // USE - Add to the LiveIn set for this instruction |
| 2563 | for (Value *V : I->operands()) { |
| 2564 | assert(!isUnhandledGCPointerType(V->getType()) && |
| 2565 | "support for FCA unimplemented"); |
Philip Reames | 63294cb | 2015-04-26 19:48:03 +0000 | [diff] [blame] | 2566 | if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) { |
| 2567 | // The choice to exclude all things constant here is slightly subtle. |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2568 | // There are two independent reasons: |
Philip Reames | 63294cb | 2015-04-26 19:48:03 +0000 | [diff] [blame] | 2569 | // - We assume that things which are constant (from LLVM's definition) |
| 2570 | // do not move at runtime. For example, the address of a global |
| 2571 | // variable is fixed, even though it's contents may not be. |
| 2572 | // - Second, we can't disallow arbitrary inttoptr constants even |
| 2573 | // if the language frontend does. Optimization passes are free to |
| 2574 | // locally exploit facts without respect to global reachability. This |
| 2575 | // can create sections of code which are dynamically unreachable and |
| 2576 | // contain just about anything. (see constants.ll in tests) |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2577 | LiveTmp.insert(V); |
| 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | static void computeLiveOutSeed(BasicBlock *BB, DenseSet<Value *> &LiveTmp) { |
| 2584 | |
| 2585 | for (BasicBlock *Succ : successors(BB)) { |
| 2586 | const BasicBlock::iterator E(Succ->getFirstNonPHI()); |
| 2587 | for (BasicBlock::iterator I = Succ->begin(); I != E; I++) { |
| 2588 | PHINode *Phi = cast<PHINode>(&*I); |
| 2589 | Value *V = Phi->getIncomingValueForBlock(BB); |
| 2590 | assert(!isUnhandledGCPointerType(V->getType()) && |
| 2591 | "support for FCA unimplemented"); |
Philip Reames | 63294cb | 2015-04-26 19:48:03 +0000 | [diff] [blame] | 2592 | if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) { |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2593 | LiveTmp.insert(V); |
| 2594 | } |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | static DenseSet<Value *> computeKillSet(BasicBlock *BB) { |
| 2600 | DenseSet<Value *> KillSet; |
| 2601 | for (Instruction &I : *BB) |
| 2602 | if (isHandledGCPointerType(I.getType())) |
| 2603 | KillSet.insert(&I); |
| 2604 | return KillSet; |
| 2605 | } |
| 2606 | |
Philip Reames | 9638ff9 | 2015-04-11 00:06:47 +0000 | [diff] [blame] | 2607 | #ifndef NDEBUG |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2608 | /// Check that the items in 'Live' dominate 'TI'. This is used as a basic |
| 2609 | /// sanity check for the liveness computation. |
| 2610 | static void checkBasicSSA(DominatorTree &DT, DenseSet<Value *> &Live, |
| 2611 | TerminatorInst *TI, bool TermOkay = false) { |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2612 | for (Value *V : Live) { |
| 2613 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 2614 | // The terminator can be a member of the LiveOut set. LLVM's definition |
| 2615 | // of instruction dominance states that V does not dominate itself. As |
| 2616 | // such, we need to special case this to allow it. |
| 2617 | if (TermOkay && TI == I) |
| 2618 | continue; |
| 2619 | assert(DT.dominates(I, TI) && |
| 2620 | "basic SSA liveness expectation violated by liveness analysis"); |
| 2621 | } |
| 2622 | } |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /// Check that all the liveness sets used during the computation of liveness |
| 2626 | /// obey basic SSA properties. This is useful for finding cases where we miss |
| 2627 | /// a def. |
| 2628 | static void checkBasicSSA(DominatorTree &DT, GCPtrLivenessData &Data, |
| 2629 | BasicBlock &BB) { |
| 2630 | checkBasicSSA(DT, Data.LiveSet[&BB], BB.getTerminator()); |
| 2631 | checkBasicSSA(DT, Data.LiveOut[&BB], BB.getTerminator(), true); |
| 2632 | checkBasicSSA(DT, Data.LiveIn[&BB], BB.getTerminator()); |
| 2633 | } |
Philip Reames | 9638ff9 | 2015-04-11 00:06:47 +0000 | [diff] [blame] | 2634 | #endif |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2635 | |
| 2636 | static void computeLiveInValues(DominatorTree &DT, Function &F, |
| 2637 | GCPtrLivenessData &Data) { |
| 2638 | |
Philip Reames | 4d80ede | 2015-04-10 23:11:26 +0000 | [diff] [blame] | 2639 | SmallSetVector<BasicBlock *, 200> Worklist; |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2640 | auto AddPredsToWorklist = [&](BasicBlock *BB) { |
Philip Reames | 4d80ede | 2015-04-10 23:11:26 +0000 | [diff] [blame] | 2641 | // We use a SetVector so that we don't have duplicates in the worklist. |
| 2642 | Worklist.insert(pred_begin(BB), pred_end(BB)); |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2643 | }; |
| 2644 | auto NextItem = [&]() { |
| 2645 | BasicBlock *BB = Worklist.back(); |
| 2646 | Worklist.pop_back(); |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2647 | return BB; |
| 2648 | }; |
| 2649 | |
| 2650 | // Seed the liveness for each individual block |
| 2651 | for (BasicBlock &BB : F) { |
| 2652 | Data.KillSet[&BB] = computeKillSet(&BB); |
| 2653 | Data.LiveSet[&BB].clear(); |
| 2654 | computeLiveInValues(BB.rbegin(), BB.rend(), Data.LiveSet[&BB]); |
| 2655 | |
| 2656 | #ifndef NDEBUG |
| 2657 | for (Value *Kill : Data.KillSet[&BB]) |
| 2658 | assert(!Data.LiveSet[&BB].count(Kill) && "live set contains kill"); |
| 2659 | #endif |
| 2660 | |
| 2661 | Data.LiveOut[&BB] = DenseSet<Value *>(); |
| 2662 | computeLiveOutSeed(&BB, Data.LiveOut[&BB]); |
| 2663 | Data.LiveIn[&BB] = Data.LiveSet[&BB]; |
| 2664 | set_union(Data.LiveIn[&BB], Data.LiveOut[&BB]); |
| 2665 | set_subtract(Data.LiveIn[&BB], Data.KillSet[&BB]); |
| 2666 | if (!Data.LiveIn[&BB].empty()) |
| 2667 | AddPredsToWorklist(&BB); |
| 2668 | } |
| 2669 | |
| 2670 | // Propagate that liveness until stable |
| 2671 | while (!Worklist.empty()) { |
| 2672 | BasicBlock *BB = NextItem(); |
| 2673 | |
| 2674 | // Compute our new liveout set, then exit early if it hasn't changed |
| 2675 | // despite the contribution of our successor. |
| 2676 | DenseSet<Value *> LiveOut = Data.LiveOut[BB]; |
| 2677 | const auto OldLiveOutSize = LiveOut.size(); |
| 2678 | for (BasicBlock *Succ : successors(BB)) { |
| 2679 | assert(Data.LiveIn.count(Succ)); |
| 2680 | set_union(LiveOut, Data.LiveIn[Succ]); |
| 2681 | } |
| 2682 | // assert OutLiveOut is a subset of LiveOut |
| 2683 | if (OldLiveOutSize == LiveOut.size()) { |
| 2684 | // If the sets are the same size, then we didn't actually add anything |
| 2685 | // when unioning our successors LiveIn Thus, the LiveIn of this block |
| 2686 | // hasn't changed. |
| 2687 | continue; |
| 2688 | } |
| 2689 | Data.LiveOut[BB] = LiveOut; |
| 2690 | |
| 2691 | // Apply the effects of this basic block |
| 2692 | DenseSet<Value *> LiveTmp = LiveOut; |
| 2693 | set_union(LiveTmp, Data.LiveSet[BB]); |
| 2694 | set_subtract(LiveTmp, Data.KillSet[BB]); |
| 2695 | |
| 2696 | assert(Data.LiveIn.count(BB)); |
| 2697 | const DenseSet<Value *> &OldLiveIn = Data.LiveIn[BB]; |
| 2698 | // assert: OldLiveIn is a subset of LiveTmp |
| 2699 | if (OldLiveIn.size() != LiveTmp.size()) { |
| 2700 | Data.LiveIn[BB] = LiveTmp; |
| 2701 | AddPredsToWorklist(BB); |
| 2702 | } |
| 2703 | } // while( !worklist.empty() ) |
| 2704 | |
| 2705 | #ifndef NDEBUG |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2706 | // Sanity check our output against SSA properties. This helps catch any |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2707 | // missing kills during the above iteration. |
| 2708 | for (BasicBlock &BB : F) { |
| 2709 | checkBasicSSA(DT, Data, BB); |
| 2710 | } |
| 2711 | #endif |
| 2712 | } |
| 2713 | |
| 2714 | static void findLiveSetAtInst(Instruction *Inst, GCPtrLivenessData &Data, |
| 2715 | StatepointLiveSetTy &Out) { |
| 2716 | |
| 2717 | BasicBlock *BB = Inst->getParent(); |
| 2718 | |
| 2719 | // Note: The copy is intentional and required |
| 2720 | assert(Data.LiveOut.count(BB)); |
| 2721 | DenseSet<Value *> LiveOut = Data.LiveOut[BB]; |
| 2722 | |
| 2723 | // We want to handle the statepoint itself oddly. It's |
| 2724 | // call result is not live (normal), nor are it's arguments |
| 2725 | // (unless they're used again later). This adjustment is |
| 2726 | // specifically what we need to relocate |
| 2727 | BasicBlock::reverse_iterator rend(Inst); |
| 2728 | computeLiveInValues(BB->rbegin(), rend, LiveOut); |
| 2729 | LiveOut.erase(Inst); |
| 2730 | Out.insert(LiveOut.begin(), LiveOut.end()); |
| 2731 | } |
| 2732 | |
| 2733 | static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData, |
| 2734 | const CallSite &CS, |
| 2735 | PartiallyConstructedSafepointRecord &Info) { |
| 2736 | Instruction *Inst = CS.getInstruction(); |
| 2737 | StatepointLiveSetTy Updated; |
| 2738 | findLiveSetAtInst(Inst, RevisedLivenessData, Updated); |
| 2739 | |
| 2740 | #ifndef NDEBUG |
| 2741 | DenseSet<Value *> Bases; |
| 2742 | for (auto KVPair : Info.PointerToBase) { |
| 2743 | Bases.insert(KVPair.second); |
| 2744 | } |
| 2745 | #endif |
| 2746 | // We may have base pointers which are now live that weren't before. We need |
| 2747 | // to update the PointerToBase structure to reflect this. |
| 2748 | for (auto V : Updated) |
| 2749 | if (!Info.PointerToBase.count(V)) { |
| 2750 | assert(Bases.count(V) && "can't find base for unexpected live value"); |
| 2751 | Info.PointerToBase[V] = V; |
| 2752 | continue; |
| 2753 | } |
| 2754 | |
| 2755 | #ifndef NDEBUG |
| 2756 | for (auto V : Updated) { |
| 2757 | assert(Info.PointerToBase.count(V) && |
| 2758 | "must be able to find base for live value"); |
| 2759 | } |
| 2760 | #endif |
| 2761 | |
| 2762 | // Remove any stale base mappings - this can happen since our liveness is |
| 2763 | // more precise then the one inherent in the base pointer analysis |
| 2764 | DenseSet<Value *> ToErase; |
| 2765 | for (auto KVPair : Info.PointerToBase) |
| 2766 | if (!Updated.count(KVPair.first)) |
| 2767 | ToErase.insert(KVPair.first); |
| 2768 | for (auto V : ToErase) |
| 2769 | Info.PointerToBase.erase(V); |
| 2770 | |
| 2771 | #ifndef NDEBUG |
| 2772 | for (auto KVPair : Info.PointerToBase) |
| 2773 | assert(Updated.count(KVPair.first) && "record for non-live value"); |
| 2774 | #endif |
| 2775 | |
Sanjoy Das | b40bd1a | 2015-10-07 02:39:18 +0000 | [diff] [blame] | 2776 | Info.LiveSet = Updated; |
Philip Reames | df1ef08 | 2015-04-10 22:53:14 +0000 | [diff] [blame] | 2777 | } |