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