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