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