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