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" |
| 17 | #include "llvm/ADT/SetOperations.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/ADT/DenseSet.h" |
| 20 | #include "llvm/IR/BasicBlock.h" |
| 21 | #include "llvm/IR/CallSite.h" |
| 22 | #include "llvm/IR/Dominators.h" |
| 23 | #include "llvm/IR/Function.h" |
| 24 | #include "llvm/IR/IRBuilder.h" |
| 25 | #include "llvm/IR/InstIterator.h" |
| 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
| 28 | #include "llvm/IR/IntrinsicInst.h" |
| 29 | #include "llvm/IR/Module.h" |
| 30 | #include "llvm/IR/Statepoint.h" |
| 31 | #include "llvm/IR/Value.h" |
| 32 | #include "llvm/IR/Verifier.h" |
| 33 | #include "llvm/Support/Debug.h" |
| 34 | #include "llvm/Support/CommandLine.h" |
| 35 | #include "llvm/Transforms/Scalar.h" |
| 36 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 37 | #include "llvm/Transforms/Utils/Cloning.h" |
| 38 | #include "llvm/Transforms/Utils/Local.h" |
| 39 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 40 | |
| 41 | #define DEBUG_TYPE "rewrite-statepoints-for-gc" |
| 42 | |
| 43 | using namespace llvm; |
| 44 | |
| 45 | // Print tracing output |
| 46 | static cl::opt<bool> TraceLSP("trace-rewrite-statepoints", cl::Hidden, |
| 47 | cl::init(false)); |
| 48 | |
| 49 | // Print the liveset found at the insert location |
| 50 | static cl::opt<bool> PrintLiveSet("spp-print-liveset", cl::Hidden, |
| 51 | cl::init(false)); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 52 | static cl::opt<bool> PrintLiveSetSize("spp-print-liveset-size", cl::Hidden, |
| 53 | cl::init(false)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 54 | // Print out the base pointers for debugging |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 55 | static cl::opt<bool> PrintBasePointers("spp-print-base-pointers", cl::Hidden, |
| 56 | cl::init(false)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 57 | |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 58 | namespace { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 59 | struct RewriteStatepointsForGC : public FunctionPass { |
| 60 | static char ID; // Pass identification, replacement for typeid |
| 61 | |
| 62 | RewriteStatepointsForGC() : FunctionPass(ID) { |
| 63 | initializeRewriteStatepointsForGCPass(*PassRegistry::getPassRegistry()); |
| 64 | } |
| 65 | bool runOnFunction(Function &F) override; |
| 66 | |
| 67 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 68 | // We add and rewrite a bunch of instructions, but don't really do much |
| 69 | // else. We could in theory preserve a lot more analyses here. |
| 70 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 71 | } |
| 72 | }; |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 73 | } // namespace |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 74 | |
| 75 | char RewriteStatepointsForGC::ID = 0; |
| 76 | |
| 77 | FunctionPass *llvm::createRewriteStatepointsForGCPass() { |
| 78 | return new RewriteStatepointsForGC(); |
| 79 | } |
| 80 | |
| 81 | INITIALIZE_PASS_BEGIN(RewriteStatepointsForGC, "rewrite-statepoints-for-gc", |
| 82 | "Make relocations explicit at statepoints", false, false) |
| 83 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 84 | INITIALIZE_PASS_END(RewriteStatepointsForGC, "rewrite-statepoints-for-gc", |
| 85 | "Make relocations explicit at statepoints", false, false) |
| 86 | |
| 87 | namespace { |
| 88 | // The type of the internal cache used inside the findBasePointers family |
| 89 | // of functions. From the callers perspective, this is an opaque type and |
| 90 | // should not be inspected. |
| 91 | // |
| 92 | // In the actual implementation this caches two relations: |
| 93 | // - The base relation itself (i.e. this pointer is based on that one) |
| 94 | // - The base defining value relation (i.e. before base_phi insertion) |
| 95 | // Generally, after the execution of a full findBasePointer call, only the |
| 96 | // base relation will remain. Internally, we add a mixture of the two |
| 97 | // types, then update all the second type to the first type |
Philip Reames | e9c3b9b | 2015-02-20 22:48:20 +0000 | [diff] [blame] | 98 | typedef DenseMap<Value *, Value *> DefiningValueMapTy; |
Philip Reames | 1f01754 | 2015-02-20 23:16:52 +0000 | [diff] [blame] | 99 | typedef DenseSet<llvm::Value *> StatepointLiveSetTy; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 100 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 101 | struct PartiallyConstructedSafepointRecord { |
| 102 | /// The set of values known to be live accross this safepoint |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 103 | StatepointLiveSetTy liveset; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 104 | |
| 105 | /// Mapping from live pointers to a base-defining-value |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 106 | DenseMap<llvm::Value *, llvm::Value *> PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 107 | |
| 108 | /// Any new values which were added to the IR during base pointer analysis |
| 109 | /// for this safepoint |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 110 | DenseSet<llvm::Value *> NewInsertedDefs; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 111 | |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 112 | /// The *new* gc.statepoint instruction itself. This produces the token |
| 113 | /// that normal path gc.relocates and the gc.result are tied to. |
| 114 | Instruction *StatepointToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 115 | |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 116 | /// Instruction to which exceptional gc relocates are attached |
| 117 | /// Makes it easier to iterate through them during relocationViaAlloca. |
| 118 | Instruction *UnwindToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 119 | }; |
| 120 | } |
| 121 | |
| 122 | // TODO: Once we can get to the GCStrategy, this becomes |
| 123 | // Optional<bool> isGCManagedPointer(const Value *V) const override { |
| 124 | |
| 125 | static bool isGCPointerType(const Type *T) { |
| 126 | if (const PointerType *PT = dyn_cast<PointerType>(T)) |
| 127 | // For the sake of this example GC, we arbitrarily pick addrspace(1) as our |
| 128 | // GC managed heap. We know that a pointer into this heap needs to be |
| 129 | // updated and that no other pointer does. |
| 130 | return (1 == PT->getAddressSpace()); |
| 131 | return false; |
| 132 | } |
| 133 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 134 | // Return true if this type is one which a) is a gc pointer or contains a GC |
| 135 | // pointer and b) is of a type this code expects to encounter as a live value. |
| 136 | // (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^] | 137 | // is not encountered.) |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 138 | static bool isHandledGCPointerType(Type *T) { |
| 139 | // We fully support gc pointers |
| 140 | if (isGCPointerType(T)) |
| 141 | return true; |
| 142 | // We partially support vectors of gc pointers. The code will assert if it |
| 143 | // can't handle something. |
| 144 | if (auto VT = dyn_cast<VectorType>(T)) |
| 145 | if (isGCPointerType(VT->getElementType())) |
| 146 | return true; |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | #ifndef NDEBUG |
| 151 | /// Returns true if this type contains a gc pointer whether we know how to |
| 152 | /// handle that type or not. |
| 153 | static bool containsGCPtrType(Type *Ty) { |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 154 | if (isGCPointerType(Ty)) |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 155 | return true; |
| 156 | if (VectorType *VT = dyn_cast<VectorType>(Ty)) |
| 157 | return isGCPointerType(VT->getScalarType()); |
| 158 | if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) |
| 159 | return containsGCPtrType(AT->getElementType()); |
| 160 | if (StructType *ST = dyn_cast<StructType>(Ty)) |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 161 | return std::any_of( |
| 162 | ST->subtypes().begin(), ST->subtypes().end(), |
| 163 | [](Type *SubType) { return containsGCPtrType(SubType); }); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // Returns true if this is a type which a) is a gc pointer or contains a GC |
| 168 | // pointer and b) is of a type which the code doesn't expect (i.e. first class |
| 169 | // aggregates). Used to trip assertions. |
| 170 | static bool isUnhandledGCPointerType(Type *Ty) { |
| 171 | return containsGCPtrType(Ty) && !isHandledGCPointerType(Ty); |
| 172 | } |
| 173 | #endif |
| 174 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 175 | /// Return true if the Value is a gc reference type which is potentially used |
| 176 | /// after the instruction 'loc'. This is only used with the edge reachability |
| 177 | /// liveness code. Note: It is assumed the V dominates loc. |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 178 | static bool isLiveGCReferenceAt(Value &V, Instruction *Loc, DominatorTree &DT, |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 179 | LoopInfo *LI) { |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 180 | if (!isHandledGCPointerType(V.getType())) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 181 | return false; |
| 182 | |
| 183 | if (V.use_empty()) |
| 184 | return false; |
| 185 | |
| 186 | // Given assumption that V dominates loc, this may be live |
| 187 | return true; |
| 188 | } |
Benjamin Kramer | d4a3a55 | 2015-02-20 13:15:49 +0000 | [diff] [blame] | 189 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 190 | // Conservatively identifies any definitions which might be live at the |
| 191 | // given instruction. The analysis is performed immediately before the |
| 192 | // given instruction. Values defined by that instruction are not considered |
| 193 | // live. Values used by that instruction are considered live. |
| 194 | // |
| 195 | // preconditions: valid IR graph, term is either a terminator instruction or |
| 196 | // a call instruction, pred is the basic block of term, DT, LI are valid |
| 197 | // |
| 198 | // side effects: none, does not mutate IR |
| 199 | // |
| 200 | // postconditions: populates liveValues as discussed above |
| 201 | static void findLiveGCValuesAtInst(Instruction *term, BasicBlock *pred, |
| 202 | DominatorTree &DT, LoopInfo *LI, |
Philip Reames | 1f01754 | 2015-02-20 23:16:52 +0000 | [diff] [blame] | 203 | StatepointLiveSetTy &liveValues) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 204 | liveValues.clear(); |
| 205 | |
| 206 | assert(isa<CallInst>(term) || isa<InvokeInst>(term) || term->isTerminator()); |
| 207 | |
| 208 | Function *F = pred->getParent(); |
| 209 | |
| 210 | auto is_live_gc_reference = |
| 211 | [&](Value &V) { return isLiveGCReferenceAt(V, term, DT, LI); }; |
| 212 | |
| 213 | // Are there any gc pointer arguments live over this point? This needs to be |
| 214 | // special cased since arguments aren't defined in basic blocks. |
| 215 | for (Argument &arg : F->args()) { |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 216 | assert(!isUnhandledGCPointerType(arg.getType()) && |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 217 | "support for FCA unimplemented"); |
| 218 | |
| 219 | if (is_live_gc_reference(arg)) { |
| 220 | liveValues.insert(&arg); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Walk through all dominating blocks - the ones which can contain |
| 225 | // definitions used in this block - and check to see if any of the values |
| 226 | // they define are used in locations potentially reachable from the |
| 227 | // interesting instruction. |
| 228 | BasicBlock *BBI = pred; |
| 229 | while (true) { |
| 230 | if (TraceLSP) { |
| 231 | errs() << "[LSP] Looking at dominating block " << pred->getName() << "\n"; |
| 232 | } |
| 233 | assert(DT.dominates(BBI, pred)); |
| 234 | assert(isPotentiallyReachable(BBI, pred, &DT) && |
| 235 | "dominated block must be reachable"); |
| 236 | |
| 237 | // Walk through the instructions in dominating blocks and keep any |
| 238 | // that have a use potentially reachable from the block we're |
| 239 | // considering putting the safepoint in |
| 240 | for (Instruction &inst : *BBI) { |
| 241 | if (TraceLSP) { |
| 242 | errs() << "[LSP] Looking at instruction "; |
| 243 | inst.dump(); |
| 244 | } |
| 245 | |
| 246 | if (pred == BBI && (&inst) == term) { |
| 247 | if (TraceLSP) { |
| 248 | errs() << "[LSP] stopped because we encountered the safepoint " |
| 249 | "instruction.\n"; |
| 250 | } |
| 251 | |
| 252 | // If we're in the block which defines the interesting instruction, |
| 253 | // we don't want to include any values as live which are defined |
| 254 | // _after_ the interesting line or as part of the line itself |
| 255 | // i.e. "term" is the call instruction for a call safepoint, the |
| 256 | // results of the call should not be considered live in that stackmap |
| 257 | break; |
| 258 | } |
| 259 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 260 | assert(!isUnhandledGCPointerType(inst.getType()) && |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 261 | "support for FCA unimplemented"); |
| 262 | |
| 263 | if (is_live_gc_reference(inst)) { |
| 264 | if (TraceLSP) { |
| 265 | errs() << "[LSP] found live value for this safepoint "; |
| 266 | inst.dump(); |
| 267 | term->dump(); |
| 268 | } |
| 269 | liveValues.insert(&inst); |
| 270 | } |
| 271 | } |
| 272 | if (!DT.getNode(BBI)->getIDom()) { |
| 273 | assert(BBI == &F->getEntryBlock() && |
| 274 | "failed to find a dominator for something other than " |
| 275 | "the entry block"); |
| 276 | break; |
| 277 | } |
| 278 | BBI = DT.getNode(BBI)->getIDom()->getBlock(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | static bool order_by_name(llvm::Value *a, llvm::Value *b) { |
| 283 | if (a->hasName() && b->hasName()) { |
| 284 | return -1 == a->getName().compare(b->getName()); |
| 285 | } else if (a->hasName() && !b->hasName()) { |
| 286 | return true; |
| 287 | } else if (!a->hasName() && b->hasName()) { |
| 288 | return false; |
| 289 | } else { |
| 290 | // Better than nothing, but not stable |
| 291 | return a < b; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Find the initial live set. Note that due to base pointer |
| 296 | /// insertion, the live set may be incomplete. |
| 297 | static void |
| 298 | analyzeParsePointLiveness(DominatorTree &DT, const CallSite &CS, |
| 299 | PartiallyConstructedSafepointRecord &result) { |
| 300 | Instruction *inst = CS.getInstruction(); |
| 301 | |
| 302 | BasicBlock *BB = inst->getParent(); |
Philip Reames | 1f01754 | 2015-02-20 23:16:52 +0000 | [diff] [blame] | 303 | StatepointLiveSetTy liveset; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 304 | findLiveGCValuesAtInst(inst, BB, DT, nullptr, liveset); |
| 305 | |
| 306 | if (PrintLiveSet) { |
| 307 | // Note: This output is used by several of the test cases |
| 308 | // The order of elemtns in a set is not stable, put them in a vec and sort |
| 309 | // by name |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 310 | SmallVector<Value *, 64> temp; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 311 | temp.insert(temp.end(), liveset.begin(), liveset.end()); |
| 312 | std::sort(temp.begin(), temp.end(), order_by_name); |
| 313 | errs() << "Live Variables:\n"; |
| 314 | for (Value *V : temp) { |
| 315 | errs() << " " << V->getName(); // no newline |
| 316 | V->dump(); |
| 317 | } |
| 318 | } |
| 319 | if (PrintLiveSetSize) { |
| 320 | errs() << "Safepoint For: " << CS.getCalledValue()->getName() << "\n"; |
| 321 | errs() << "Number live values: " << liveset.size() << "\n"; |
| 322 | } |
| 323 | result.liveset = liveset; |
| 324 | } |
| 325 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 326 | /// If we can trivially determine that this vector contains only base pointers, |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 327 | /// return the base instruction. |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 328 | static Value *findBaseOfVector(Value *I) { |
| 329 | assert(I->getType()->isVectorTy() && |
| 330 | cast<VectorType>(I->getType())->getElementType()->isPointerTy() && |
| 331 | "Illegal to ask for the base pointer of a non-pointer type"); |
| 332 | |
| 333 | // Each case parallels findBaseDefiningValue below, see that code for |
| 334 | // detailed motivation. |
| 335 | |
| 336 | if (isa<Argument>(I)) |
| 337 | // An incoming argument to the function is a base pointer |
| 338 | return I; |
| 339 | |
| 340 | // We shouldn't see the address of a global as a vector value? |
| 341 | assert(!isa<GlobalVariable>(I) && |
| 342 | "unexpected global variable found in base of vector"); |
| 343 | |
| 344 | // inlining could possibly introduce phi node that contains |
| 345 | // undef if callee has multiple returns |
| 346 | if (isa<UndefValue>(I)) |
| 347 | // utterly meaningless, but useful for dealing with partially optimized |
| 348 | // code. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 349 | return I; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 350 | |
| 351 | // Due to inheritance, this must be _after_ the global variable and undef |
| 352 | // checks |
| 353 | if (Constant *Con = dyn_cast<Constant>(I)) { |
| 354 | assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) && |
| 355 | "order of checks wrong!"); |
| 356 | assert(Con->isNullValue() && "null is the only case which makes sense"); |
| 357 | return Con; |
| 358 | } |
| 359 | |
| 360 | if (isa<LoadInst>(I)) |
| 361 | return I; |
| 362 | |
| 363 | // Note: This code is currently rather incomplete. We are essentially only |
| 364 | // handling cases where the vector element is trivially a base pointer. We |
| 365 | // need to update the entire base pointer construction algorithm to know how |
| 366 | // to track vector elements and potentially scalarize, but the case which |
| 367 | // would motivate the work hasn't shown up in real workloads yet. |
| 368 | llvm_unreachable("no base found for vector element"); |
| 369 | } |
| 370 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 371 | /// Helper function for findBasePointer - Will return a value which either a) |
| 372 | /// defines the base pointer for the input or b) blocks the simple search |
| 373 | /// (i.e. a PHI or Select of two derived pointers) |
| 374 | static Value *findBaseDefiningValue(Value *I) { |
| 375 | assert(I->getType()->isPointerTy() && |
| 376 | "Illegal to ask for the base pointer of a non-pointer type"); |
| 377 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 378 | // This case is a bit of a hack - it only handles extracts from vectors which |
| 379 | // trivially contain only base pointers. See note inside the function for |
| 380 | // how to improve this. |
| 381 | if (auto *EEI = dyn_cast<ExtractElementInst>(I)) { |
| 382 | Value *VectorOperand = EEI->getVectorOperand(); |
| 383 | Value *VectorBase = findBaseOfVector(VectorOperand); |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 384 | (void)VectorBase; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 385 | assert(VectorBase && "extract element not known to be a trivial base"); |
| 386 | return EEI; |
| 387 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 388 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 389 | if (isa<Argument>(I)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 390 | // An incoming argument to the function is a base pointer |
| 391 | // 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] | 392 | return I; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 393 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 394 | if (isa<GlobalVariable>(I)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 395 | // base case |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 396 | return I; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 397 | |
| 398 | // inlining could possibly introduce phi node that contains |
| 399 | // undef if callee has multiple returns |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 400 | if (isa<UndefValue>(I)) |
| 401 | // utterly meaningless, but useful for dealing with |
| 402 | // partially optimized code. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 403 | return I; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 404 | |
| 405 | // Due to inheritance, this must be _after_ the global variable and undef |
| 406 | // checks |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 407 | if (Constant *Con = dyn_cast<Constant>(I)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 408 | assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) && |
| 409 | "order of checks wrong!"); |
| 410 | // Note: Finding a constant base for something marked for relocation |
| 411 | // doesn't really make sense. The most likely case is either a) some |
| 412 | // screwed up the address space usage or b) your validating against |
| 413 | // compiled C++ code w/o the proper separation. The only real exception |
| 414 | // is a null pointer. You could have generic code written to index of |
| 415 | // off a potentially null value and have proven it null. We also use |
| 416 | // null pointers in dead paths of relocation phis (which we might later |
| 417 | // want to find a base pointer for). |
Philip Reames | 24c6cd5 | 2015-03-27 05:47:00 +0000 | [diff] [blame] | 418 | assert(isa<ConstantPointerNull>(Con) && |
| 419 | "null is the only case which makes sense"); |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 420 | return Con; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 424 | Value *Def = CI->stripPointerCasts(); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 425 | // If we find a cast instruction here, it means we've found a cast which is |
| 426 | // not simply a pointer cast (i.e. an inttoptr). We don't know how to |
| 427 | // handle int->ptr conversion. |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 428 | assert(!isa<CastInst>(Def) && "shouldn't find another cast here"); |
| 429 | return findBaseDefiningValue(Def); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 432 | if (isa<LoadInst>(I)) |
| 433 | return I; // The value loaded is an gc base itself |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 434 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 435 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) |
| 436 | // The base of this GEP is the base |
| 437 | return findBaseDefiningValue(GEP->getPointerOperand()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 438 | |
| 439 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 440 | switch (II->getIntrinsicID()) { |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 441 | case Intrinsic::experimental_gc_result_ptr: |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 442 | default: |
| 443 | // fall through to general call handling |
| 444 | break; |
| 445 | case Intrinsic::experimental_gc_statepoint: |
| 446 | case Intrinsic::experimental_gc_result_float: |
| 447 | case Intrinsic::experimental_gc_result_int: |
| 448 | llvm_unreachable("these don't produce pointers"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 449 | case Intrinsic::experimental_gc_relocate: { |
| 450 | // Rerunning safepoint insertion after safepoints are already |
| 451 | // inserted is not supported. It could probably be made to work, |
| 452 | // but why are you doing this? There's no good reason. |
| 453 | llvm_unreachable("repeat safepoint insertion is not supported"); |
| 454 | } |
| 455 | case Intrinsic::gcroot: |
| 456 | // Currently, this mechanism hasn't been extended to work with gcroot. |
| 457 | // There's no reason it couldn't be, but I haven't thought about the |
| 458 | // implications much. |
| 459 | llvm_unreachable( |
| 460 | "interaction with the gcroot mechanism is not supported"); |
| 461 | } |
| 462 | } |
| 463 | // We assume that functions in the source language only return base |
| 464 | // pointers. This should probably be generalized via attributes to support |
| 465 | // both source language and internal functions. |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 466 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 467 | return I; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 468 | |
| 469 | // I have absolutely no idea how to implement this part yet. It's not |
| 470 | // neccessarily hard, I just haven't really looked at it yet. |
| 471 | assert(!isa<LandingPadInst>(I) && "Landing Pad is unimplemented"); |
| 472 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 473 | if (isa<AtomicCmpXchgInst>(I)) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 474 | // A CAS is effectively a atomic store and load combined under a |
| 475 | // predicate. From the perspective of base pointers, we just treat it |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 476 | // like a load. |
| 477 | return I; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 478 | |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 479 | assert(!isa<AtomicRMWInst>(I) && "Xchg handled above, all others are " |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 480 | "binary ops which don't apply to pointers"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 481 | |
| 482 | // The aggregate ops. Aggregates can either be in the heap or on the |
| 483 | // stack, but in either case, this is simply a field load. As a result, |
| 484 | // 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] | 485 | if (isa<ExtractValueInst>(I)) |
| 486 | return I; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 487 | |
| 488 | // We should never see an insert vector since that would require we be |
| 489 | // tracing back a struct value not a pointer value. |
| 490 | assert(!isa<InsertValueInst>(I) && |
| 491 | "Base pointer for a struct is meaningless"); |
| 492 | |
| 493 | // The last two cases here don't return a base pointer. Instead, they |
| 494 | // return a value which dynamically selects from amoung several base |
| 495 | // derived pointers (each with it's own base potentially). It's the job of |
| 496 | // the caller to resolve these. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 497 | assert((isa<SelectInst>(I) || isa<PHINode>(I)) && |
Philip Reames | aa66dfa | 2015-03-27 05:34:44 +0000 | [diff] [blame] | 498 | "missing instruction case in findBaseDefiningValing"); |
| 499 | return I; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /// Returns the base defining value for this value. |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 503 | static Value *findBaseDefiningValueCached(Value *I, DefiningValueMapTy &Cache) { |
| 504 | Value *&Cached = Cache[I]; |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 505 | if (!Cached) { |
| 506 | Cached = findBaseDefiningValue(I); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 507 | } |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 508 | assert(Cache[I] != nullptr); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 509 | |
| 510 | if (TraceLSP) { |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 511 | dbgs() << "fBDV-cached: " << I->getName() << " -> " << Cached->getName() |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 512 | << "\n"; |
| 513 | } |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 514 | return Cached; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /// Return a base pointer for this value if known. Otherwise, return it's |
| 518 | /// base defining value. |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 519 | static Value *findBaseOrBDV(Value *I, DefiningValueMapTy &Cache) { |
| 520 | Value *Def = findBaseDefiningValueCached(I, Cache); |
| 521 | auto Found = Cache.find(Def); |
| 522 | if (Found != Cache.end()) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 523 | // Either a base-of relation, or a self reference. Caller must check. |
Benjamin Kramer | 6f66545 | 2015-02-20 14:00:58 +0000 | [diff] [blame] | 524 | return Found->second; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 525 | } |
| 526 | // Only a BDV available |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 527 | return Def; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /// Given the result of a call to findBaseDefiningValue, or findBaseOrBDV, |
| 531 | /// 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] | 532 | static bool isKnownBaseResult(Value *V) { |
| 533 | if (!isa<PHINode>(V) && !isa<SelectInst>(V)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 534 | // no recursion possible |
| 535 | return true; |
| 536 | } |
Philip Reames | 18d0feb | 2015-03-27 05:39:32 +0000 | [diff] [blame] | 537 | if (isa<Instruction>(V) && |
| 538 | cast<Instruction>(V)->getMetadata("is_base_value")) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 539 | // This is a previously inserted base phi or select. We know |
| 540 | // that this is a base value. |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | // We need to keep searching |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | // TODO: find a better name for this |
| 549 | namespace { |
| 550 | class PhiState { |
| 551 | public: |
| 552 | enum Status { Unknown, Base, Conflict }; |
| 553 | |
| 554 | PhiState(Status s, Value *b = nullptr) : status(s), base(b) { |
| 555 | assert(status != Base || b); |
| 556 | } |
| 557 | PhiState(Value *b) : status(Base), base(b) {} |
| 558 | PhiState() : status(Unknown), base(nullptr) {} |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 559 | |
| 560 | Status getStatus() const { return status; } |
| 561 | Value *getBase() const { return base; } |
| 562 | |
| 563 | bool isBase() const { return getStatus() == Base; } |
| 564 | bool isUnknown() const { return getStatus() == Unknown; } |
| 565 | bool isConflict() const { return getStatus() == Conflict; } |
| 566 | |
| 567 | bool operator==(const PhiState &other) const { |
| 568 | return base == other.base && status == other.status; |
| 569 | } |
| 570 | |
| 571 | bool operator!=(const PhiState &other) const { return !(*this == other); } |
| 572 | |
| 573 | void dump() { |
| 574 | errs() << status << " (" << base << " - " |
| 575 | << (base ? base->getName() : "nullptr") << "): "; |
| 576 | } |
| 577 | |
| 578 | private: |
| 579 | Status status; |
| 580 | Value *base; // non null only if status == base |
| 581 | }; |
| 582 | |
Philip Reames | e9c3b9b | 2015-02-20 22:48:20 +0000 | [diff] [blame] | 583 | typedef DenseMap<Value *, PhiState> ConflictStateMapTy; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 584 | // Values of type PhiState form a lattice, and this is a helper |
| 585 | // class that implementes the meet operation. The meat of the meet |
| 586 | // operation is implemented in MeetPhiStates::pureMeet |
| 587 | class MeetPhiStates { |
| 588 | public: |
| 589 | // phiStates is a mapping from PHINodes and SelectInst's to PhiStates. |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 590 | explicit MeetPhiStates(const ConflictStateMapTy &phiStates) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 591 | : phiStates(phiStates) {} |
| 592 | |
| 593 | // Destructively meet the current result with the base V. V can |
| 594 | // either be a merge instruction (SelectInst / PHINode), in which |
| 595 | // case its status is looked up in the phiStates map; or a regular |
| 596 | // SSA value, in which case it is assumed to be a base. |
| 597 | void meetWith(Value *V) { |
| 598 | PhiState otherState = getStateForBDV(V); |
| 599 | assert((MeetPhiStates::pureMeet(otherState, currentResult) == |
| 600 | MeetPhiStates::pureMeet(currentResult, otherState)) && |
| 601 | "math is wrong: meet does not commute!"); |
| 602 | currentResult = MeetPhiStates::pureMeet(otherState, currentResult); |
| 603 | } |
| 604 | |
| 605 | PhiState getResult() const { return currentResult; } |
| 606 | |
| 607 | private: |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 608 | const ConflictStateMapTy &phiStates; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 609 | PhiState currentResult; |
| 610 | |
| 611 | /// Return a phi state for a base defining value. We'll generate a new |
| 612 | /// base state for known bases and expect to find a cached state otherwise |
| 613 | PhiState getStateForBDV(Value *baseValue) { |
| 614 | if (isKnownBaseResult(baseValue)) { |
| 615 | return PhiState(baseValue); |
| 616 | } else { |
| 617 | return lookupFromMap(baseValue); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | PhiState lookupFromMap(Value *V) { |
| 622 | auto I = phiStates.find(V); |
| 623 | assert(I != phiStates.end() && "lookup failed!"); |
| 624 | return I->second; |
| 625 | } |
| 626 | |
| 627 | static PhiState pureMeet(const PhiState &stateA, const PhiState &stateB) { |
| 628 | switch (stateA.getStatus()) { |
| 629 | case PhiState::Unknown: |
| 630 | return stateB; |
| 631 | |
| 632 | case PhiState::Base: |
| 633 | assert(stateA.getBase() && "can't be null"); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 634 | if (stateB.isUnknown()) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 635 | return stateA; |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 636 | |
| 637 | if (stateB.isBase()) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 638 | if (stateA.getBase() == stateB.getBase()) { |
| 639 | assert(stateA == stateB && "equality broken!"); |
| 640 | return stateA; |
| 641 | } |
| 642 | return PhiState(PhiState::Conflict); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 643 | } |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 644 | assert(stateB.isConflict() && "only three states!"); |
| 645 | return PhiState(PhiState::Conflict); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 646 | |
| 647 | case PhiState::Conflict: |
| 648 | return stateA; |
| 649 | } |
Reid Kleckner | a070ee5 | 2015-02-20 19:46:02 +0000 | [diff] [blame] | 650 | llvm_unreachable("only three states!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 651 | } |
| 652 | }; |
| 653 | } |
| 654 | /// For a given value or instruction, figure out what base ptr it's derived |
| 655 | /// from. For gc objects, this is simply itself. On success, returns a value |
| 656 | /// which is the base pointer. (This is reliable and can be used for |
| 657 | /// relocation.) On failure, returns nullptr. |
| 658 | static Value *findBasePointer(Value *I, DefiningValueMapTy &cache, |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 659 | DenseSet<llvm::Value *> &NewInsertedDefs) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 660 | Value *def = findBaseOrBDV(I, cache); |
| 661 | |
| 662 | if (isKnownBaseResult(def)) { |
| 663 | return def; |
| 664 | } |
| 665 | |
| 666 | // Here's the rough algorithm: |
| 667 | // - For every SSA value, construct a mapping to either an actual base |
| 668 | // pointer or a PHI which obscures the base pointer. |
| 669 | // - Construct a mapping from PHI to unknown TOP state. Use an |
| 670 | // optimistic algorithm to propagate base pointer information. Lattice |
| 671 | // looks like: |
| 672 | // UNKNOWN |
| 673 | // b1 b2 b3 b4 |
| 674 | // CONFLICT |
| 675 | // When algorithm terminates, all PHIs will either have a single concrete |
| 676 | // base or be in a conflict state. |
| 677 | // - For every conflict, insert a dummy PHI node without arguments. Add |
| 678 | // these to the base[Instruction] = BasePtr mapping. For every |
| 679 | // non-conflict, add the actual base. |
| 680 | // - For every conflict, add arguments for the base[a] of each input |
| 681 | // arguments. |
| 682 | // |
| 683 | // Note: A simpler form of this would be to add the conflict form of all |
| 684 | // PHIs without running the optimistic algorithm. This would be |
| 685 | // analougous to pessimistic data flow and would likely lead to an |
| 686 | // overall worse solution. |
| 687 | |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 688 | ConflictStateMapTy states; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 689 | states[def] = PhiState(); |
| 690 | // Recursively fill in all phis & selects reachable from the initial one |
| 691 | // for which we don't already know a definite base value for |
Philip Reames | a226e61 | 2015-02-28 00:47:50 +0000 | [diff] [blame] | 692 | // TODO: This should be rewritten with a worklist |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 693 | bool done = false; |
| 694 | while (!done) { |
| 695 | done = true; |
Philip Reames | a226e61 | 2015-02-28 00:47:50 +0000 | [diff] [blame] | 696 | // Since we're adding elements to 'states' as we run, we can't keep |
| 697 | // iterators into the set. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 698 | SmallVector<Value *, 16> Keys; |
Philip Reames | a226e61 | 2015-02-28 00:47:50 +0000 | [diff] [blame] | 699 | Keys.reserve(states.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 700 | for (auto Pair : states) { |
Philip Reames | a226e61 | 2015-02-28 00:47:50 +0000 | [diff] [blame] | 701 | Value *V = Pair.first; |
| 702 | Keys.push_back(V); |
| 703 | } |
| 704 | for (Value *v : Keys) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 705 | assert(!isKnownBaseResult(v) && "why did it get added?"); |
| 706 | if (PHINode *phi = dyn_cast<PHINode>(v)) { |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 707 | assert(phi->getNumIncomingValues() > 0 && |
| 708 | "zero input phis are illegal"); |
| 709 | for (Value *InVal : phi->incoming_values()) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 710 | Value *local = findBaseOrBDV(InVal, cache); |
| 711 | if (!isKnownBaseResult(local) && states.find(local) == states.end()) { |
| 712 | states[local] = PhiState(); |
| 713 | done = false; |
| 714 | } |
| 715 | } |
| 716 | } else if (SelectInst *sel = dyn_cast<SelectInst>(v)) { |
| 717 | Value *local = findBaseOrBDV(sel->getTrueValue(), cache); |
| 718 | if (!isKnownBaseResult(local) && states.find(local) == states.end()) { |
| 719 | states[local] = PhiState(); |
| 720 | done = false; |
| 721 | } |
| 722 | local = findBaseOrBDV(sel->getFalseValue(), cache); |
| 723 | if (!isKnownBaseResult(local) && states.find(local) == states.end()) { |
| 724 | states[local] = PhiState(); |
| 725 | done = false; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (TraceLSP) { |
| 732 | errs() << "States after initialization:\n"; |
| 733 | for (auto Pair : states) { |
| 734 | Instruction *v = cast<Instruction>(Pair.first); |
| 735 | PhiState state = Pair.second; |
| 736 | state.dump(); |
| 737 | v->dump(); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // TODO: come back and revisit the state transitions around inputs which |
| 742 | // have reached conflict state. The current version seems too conservative. |
| 743 | |
| 744 | bool progress = true; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 745 | while (progress) { |
Yaron Keren | 42a7adf | 2015-02-28 13:11:24 +0000 | [diff] [blame] | 746 | #ifndef NDEBUG |
| 747 | size_t oldSize = states.size(); |
| 748 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 749 | progress = false; |
Philip Reames | a226e61 | 2015-02-28 00:47:50 +0000 | [diff] [blame] | 750 | // 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] | 751 | for (auto Pair : states) { |
| 752 | MeetPhiStates calculateMeet(states); |
| 753 | Value *v = Pair.first; |
| 754 | assert(!isKnownBaseResult(v) && "why did it get added?"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 755 | if (SelectInst *select = dyn_cast<SelectInst>(v)) { |
| 756 | calculateMeet.meetWith(findBaseOrBDV(select->getTrueValue(), cache)); |
| 757 | calculateMeet.meetWith(findBaseOrBDV(select->getFalseValue(), cache)); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 758 | } else |
| 759 | for (Value *Val : cast<PHINode>(v)->incoming_values()) |
| 760 | calculateMeet.meetWith(findBaseOrBDV(Val, cache)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 761 | |
| 762 | PhiState oldState = states[v]; |
| 763 | PhiState newState = calculateMeet.getResult(); |
| 764 | if (oldState != newState) { |
| 765 | progress = true; |
| 766 | states[v] = newState; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | assert(oldSize <= states.size()); |
| 771 | assert(oldSize == states.size() || progress); |
| 772 | } |
| 773 | |
| 774 | if (TraceLSP) { |
| 775 | errs() << "States after meet iteration:\n"; |
| 776 | for (auto Pair : states) { |
| 777 | Instruction *v = cast<Instruction>(Pair.first); |
| 778 | PhiState state = Pair.second; |
| 779 | state.dump(); |
| 780 | v->dump(); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // Insert Phis for all conflicts |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 785 | // We want to keep naming deterministic in the loop that follows, so |
| 786 | // sort the keys before iteration. This is useful in allowing us to |
| 787 | // write stable tests. Note that there is no invalidation issue here. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 788 | SmallVector<Value *, 16> Keys; |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 789 | Keys.reserve(states.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 790 | for (auto Pair : states) { |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 791 | Value *V = Pair.first; |
| 792 | Keys.push_back(V); |
| 793 | } |
| 794 | std::sort(Keys.begin(), Keys.end(), order_by_name); |
| 795 | // TODO: adjust naming patterns to avoid this order of iteration dependency |
| 796 | for (Value *V : Keys) { |
| 797 | Instruction *v = cast<Instruction>(V); |
| 798 | PhiState state = states[V]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 799 | assert(!isKnownBaseResult(v) && "why did it get added?"); |
| 800 | assert(!state.isUnknown() && "Optimistic algorithm didn't complete!"); |
Philip Reames | f986d68 | 2015-02-28 00:54:41 +0000 | [diff] [blame] | 801 | if (!state.isConflict()) |
| 802 | continue; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 803 | |
Philip Reames | f986d68 | 2015-02-28 00:54:41 +0000 | [diff] [blame] | 804 | if (isa<PHINode>(v)) { |
| 805 | int num_preds = |
| 806 | std::distance(pred_begin(v->getParent()), pred_end(v->getParent())); |
| 807 | assert(num_preds > 0 && "how did we reach here"); |
| 808 | PHINode *phi = PHINode::Create(v->getType(), num_preds, "base_phi", v); |
| 809 | NewInsertedDefs.insert(phi); |
| 810 | // Add metadata marking this as a base value |
| 811 | auto *const_1 = ConstantInt::get( |
| 812 | Type::getInt32Ty( |
| 813 | v->getParent()->getParent()->getParent()->getContext()), |
| 814 | 1); |
| 815 | auto MDConst = ConstantAsMetadata::get(const_1); |
| 816 | MDNode *md = MDNode::get( |
| 817 | v->getParent()->getParent()->getParent()->getContext(), MDConst); |
| 818 | phi->setMetadata("is_base_value", md); |
| 819 | states[v] = PhiState(PhiState::Conflict, phi); |
| 820 | } else { |
| 821 | SelectInst *sel = cast<SelectInst>(v); |
| 822 | // The undef will be replaced later |
| 823 | UndefValue *undef = UndefValue::get(sel->getType()); |
| 824 | SelectInst *basesel = SelectInst::Create(sel->getCondition(), undef, |
| 825 | undef, "base_select", sel); |
| 826 | NewInsertedDefs.insert(basesel); |
| 827 | // Add metadata marking this as a base value |
| 828 | auto *const_1 = ConstantInt::get( |
| 829 | Type::getInt32Ty( |
| 830 | v->getParent()->getParent()->getParent()->getContext()), |
| 831 | 1); |
| 832 | auto MDConst = ConstantAsMetadata::get(const_1); |
| 833 | MDNode *md = MDNode::get( |
| 834 | v->getParent()->getParent()->getParent()->getContext(), MDConst); |
| 835 | basesel->setMetadata("is_base_value", md); |
| 836 | states[v] = PhiState(PhiState::Conflict, basesel); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | // Fixup all the inputs of the new PHIs |
| 841 | for (auto Pair : states) { |
| 842 | Instruction *v = cast<Instruction>(Pair.first); |
| 843 | PhiState state = Pair.second; |
| 844 | |
| 845 | assert(!isKnownBaseResult(v) && "why did it get added?"); |
| 846 | assert(!state.isUnknown() && "Optimistic algorithm didn't complete!"); |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 847 | if (!state.isConflict()) |
| 848 | continue; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 849 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 850 | if (PHINode *basephi = dyn_cast<PHINode>(state.getBase())) { |
| 851 | PHINode *phi = cast<PHINode>(v); |
| 852 | unsigned NumPHIValues = phi->getNumIncomingValues(); |
| 853 | for (unsigned i = 0; i < NumPHIValues; i++) { |
| 854 | Value *InVal = phi->getIncomingValue(i); |
| 855 | BasicBlock *InBB = phi->getIncomingBlock(i); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 856 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 857 | // If we've already seen InBB, add the same incoming value |
| 858 | // we added for it earlier. The IR verifier requires phi |
| 859 | // nodes with multiple entries from the same basic block |
| 860 | // to have the same incoming value for each of those |
| 861 | // entries. If we don't do this check here and basephi |
| 862 | // has a different type than base, we'll end up adding two |
| 863 | // bitcasts (and hence two distinct values) as incoming |
| 864 | // values for the same basic block. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 865 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 866 | int blockIndex = basephi->getBasicBlockIndex(InBB); |
| 867 | if (blockIndex != -1) { |
| 868 | Value *oldBase = basephi->getIncomingValue(blockIndex); |
| 869 | basephi->addIncoming(oldBase, InBB); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 870 | #ifndef NDEBUG |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 871 | Value *base = findBaseOrBDV(InVal, cache); |
| 872 | if (!isKnownBaseResult(base)) { |
| 873 | // Either conflict or base. |
| 874 | assert(states.count(base)); |
| 875 | base = states[base].getBase(); |
| 876 | assert(base != nullptr && "unknown PhiState!"); |
| 877 | assert(NewInsertedDefs.count(base) && |
| 878 | "should have already added this in a prev. iteration!"); |
| 879 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 880 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 881 | // In essense this assert states: the only way two |
| 882 | // values incoming from the same basic block may be |
| 883 | // different is by being different bitcasts of the same |
| 884 | // value. A cleanup that remains TODO is changing |
| 885 | // findBaseOrBDV to return an llvm::Value of the correct |
| 886 | // type (and still remain pure). This will remove the |
| 887 | // need to add bitcasts. |
| 888 | assert(base->stripPointerCasts() == oldBase->stripPointerCasts() && |
| 889 | "sanity -- findBaseOrBDV should be pure!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 890 | #endif |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 891 | continue; |
| 892 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 893 | |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 894 | // Find either the defining value for the PHI or the normal base for |
| 895 | // a non-phi node |
| 896 | Value *base = findBaseOrBDV(InVal, cache); |
| 897 | if (!isKnownBaseResult(base)) { |
| 898 | // Either conflict or base. |
| 899 | assert(states.count(base)); |
| 900 | base = states[base].getBase(); |
| 901 | assert(base != nullptr && "unknown PhiState!"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 902 | } |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 903 | assert(base && "can't be null"); |
| 904 | // Must use original input BB since base may not be Instruction |
| 905 | // The cast is needed since base traversal may strip away bitcasts |
| 906 | if (base->getType() != basephi->getType()) { |
| 907 | base = new BitCastInst(base, basephi->getType(), "cast", |
| 908 | InBB->getTerminator()); |
| 909 | NewInsertedDefs.insert(base); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 910 | } |
Philip Reames | 28e61ce | 2015-02-28 01:57:44 +0000 | [diff] [blame] | 911 | basephi->addIncoming(base, InBB); |
| 912 | } |
| 913 | assert(basephi->getNumIncomingValues() == NumPHIValues); |
| 914 | } else { |
| 915 | SelectInst *basesel = cast<SelectInst>(state.getBase()); |
| 916 | SelectInst *sel = cast<SelectInst>(v); |
| 917 | // Operand 1 & 2 are true, false path respectively. TODO: refactor to |
| 918 | // something more safe and less hacky. |
| 919 | for (int i = 1; i <= 2; i++) { |
| 920 | Value *InVal = sel->getOperand(i); |
| 921 | // Find either the defining value for the PHI or the normal base for |
| 922 | // a non-phi node |
| 923 | Value *base = findBaseOrBDV(InVal, cache); |
| 924 | if (!isKnownBaseResult(base)) { |
| 925 | // Either conflict or base. |
| 926 | assert(states.count(base)); |
| 927 | base = states[base].getBase(); |
| 928 | assert(base != nullptr && "unknown PhiState!"); |
| 929 | } |
| 930 | assert(base && "can't be null"); |
| 931 | // Must use original input BB since base may not be Instruction |
| 932 | // The cast is needed since base traversal may strip away bitcasts |
| 933 | if (base->getType() != basesel->getType()) { |
| 934 | base = new BitCastInst(base, basesel->getType(), "cast", basesel); |
| 935 | NewInsertedDefs.insert(base); |
| 936 | } |
| 937 | basesel->setOperand(i, base); |
| 938 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | // Cache all of our results so we can cheaply reuse them |
| 943 | // NOTE: This is actually two caches: one of the base defining value |
| 944 | // relation and one of the base pointer relation! FIXME |
| 945 | for (auto item : states) { |
| 946 | Value *v = item.first; |
| 947 | Value *base = item.second.getBase(); |
| 948 | assert(v && base); |
| 949 | assert(!isKnownBaseResult(v) && "why did it get added?"); |
| 950 | |
| 951 | if (TraceLSP) { |
| 952 | std::string fromstr = |
| 953 | cache.count(v) ? (cache[v]->hasName() ? cache[v]->getName() : "") |
| 954 | : "none"; |
| 955 | errs() << "Updating base value cache" |
| 956 | << " for: " << (v->hasName() ? v->getName() : "") |
| 957 | << " from: " << fromstr |
| 958 | << " to: " << (base->hasName() ? base->getName() : "") << "\n"; |
| 959 | } |
| 960 | |
| 961 | assert(isKnownBaseResult(base) && |
| 962 | "must be something we 'know' is a base pointer"); |
| 963 | if (cache.count(v)) { |
| 964 | // Once we transition from the BDV relation being store in the cache to |
| 965 | // the base relation being stored, it must be stable |
| 966 | assert((!isKnownBaseResult(cache[v]) || cache[v] == base) && |
| 967 | "base relation should be stable"); |
| 968 | } |
| 969 | cache[v] = base; |
| 970 | } |
| 971 | assert(cache.find(def) != cache.end()); |
| 972 | return cache[def]; |
| 973 | } |
| 974 | |
| 975 | // For a set of live pointers (base and/or derived), identify the base |
| 976 | // pointer of the object which they are derived from. This routine will |
| 977 | // mutate the IR graph as needed to make the 'base' pointer live at the |
| 978 | // definition site of 'derived'. This ensures that any use of 'derived' can |
| 979 | // also use 'base'. This may involve the insertion of a number of |
| 980 | // additional PHI nodes. |
| 981 | // |
| 982 | // preconditions: live is a set of pointer type Values |
| 983 | // |
| 984 | // side effects: may insert PHI nodes into the existing CFG, will preserve |
| 985 | // CFG, will not remove or mutate any existing nodes |
| 986 | // |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 987 | // post condition: PointerToBase contains one (derived, base) pair for every |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 988 | // pointer in live. Note that derived can be equal to base if the original |
| 989 | // pointer was a base pointer. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 990 | static void |
| 991 | findBasePointers(const StatepointLiveSetTy &live, |
| 992 | DenseMap<llvm::Value *, llvm::Value *> &PointerToBase, |
| 993 | DominatorTree *DT, DefiningValueMapTy &DVCache, |
| 994 | DenseSet<llvm::Value *> &NewInsertedDefs) { |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 995 | // For the naming of values inserted to be deterministic - which makes for |
| 996 | // much cleaner and more stable tests - we need to assign an order to the |
| 997 | // live values. DenseSets do not provide a deterministic order across runs. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 998 | SmallVector<Value *, 64> Temp; |
Philip Reames | 2e5bcbe | 2015-02-28 01:52:09 +0000 | [diff] [blame] | 999 | Temp.insert(Temp.end(), live.begin(), live.end()); |
| 1000 | std::sort(Temp.begin(), Temp.end(), order_by_name); |
| 1001 | for (Value *ptr : Temp) { |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1002 | Value *base = findBasePointer(ptr, DVCache, NewInsertedDefs); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1003 | assert(base && "failed to find base pointer"); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1004 | PointerToBase[ptr] = base; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1005 | assert((!isa<Instruction>(base) || !isa<Instruction>(ptr) || |
| 1006 | DT->dominates(cast<Instruction>(base)->getParent(), |
| 1007 | cast<Instruction>(ptr)->getParent())) && |
| 1008 | "The base we found better dominate the derived pointer"); |
| 1009 | |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1010 | // If you see this trip and like to live really dangerously, the code should |
| 1011 | // be correct, just with idioms the verifier can't handle. You can try |
| 1012 | // disabling the verifier at your own substaintial risk. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1013 | assert(!isa<ConstantPointerNull>(base) && |
Philip Reames | 24c6cd5 | 2015-03-27 05:47:00 +0000 | [diff] [blame] | 1014 | "the relocation code needs adjustment to handle the relocation of " |
| 1015 | "a null pointer constant without causing false positives in the " |
| 1016 | "safepoint ir verifier."); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | /// Find the required based pointers (and adjust the live set) for the given |
| 1021 | /// parse point. |
| 1022 | static void findBasePointers(DominatorTree &DT, DefiningValueMapTy &DVCache, |
| 1023 | const CallSite &CS, |
| 1024 | PartiallyConstructedSafepointRecord &result) { |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1025 | DenseMap<llvm::Value *, llvm::Value *> PointerToBase; |
| 1026 | DenseSet<llvm::Value *> NewInsertedDefs; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1027 | findBasePointers(result.liveset, PointerToBase, &DT, DVCache, |
| 1028 | NewInsertedDefs); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1029 | |
| 1030 | if (PrintBasePointers) { |
Philip Reames | a5aeaf4 | 2015-02-28 00:20:48 +0000 | [diff] [blame] | 1031 | // Note: Need to print these in a stable order since this is checked in |
| 1032 | // some tests. |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1033 | errs() << "Base Pairs (w/o Relocation):\n"; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1034 | SmallVector<Value *, 64> Temp; |
Philip Reames | a5aeaf4 | 2015-02-28 00:20:48 +0000 | [diff] [blame] | 1035 | Temp.reserve(PointerToBase.size()); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1036 | for (auto Pair : PointerToBase) { |
Philip Reames | a5aeaf4 | 2015-02-28 00:20:48 +0000 | [diff] [blame] | 1037 | Temp.push_back(Pair.first); |
| 1038 | } |
| 1039 | std::sort(Temp.begin(), Temp.end(), order_by_name); |
| 1040 | for (Value *Ptr : Temp) { |
| 1041 | Value *Base = PointerToBase[Ptr]; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1042 | errs() << " derived %" << Ptr->getName() << " base %" << Base->getName() |
| 1043 | << "\n"; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1047 | result.PointerToBase = PointerToBase; |
| 1048 | result.NewInsertedDefs = NewInsertedDefs; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /// Check for liveness of items in the insert defs and add them to the live |
| 1052 | /// and base pointer sets |
| 1053 | static void fixupLiveness(DominatorTree &DT, const CallSite &CS, |
Philip Reames | 1f01754 | 2015-02-20 23:16:52 +0000 | [diff] [blame] | 1054 | const DenseSet<Value *> &allInsertedDefs, |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1055 | PartiallyConstructedSafepointRecord &result) { |
| 1056 | Instruction *inst = CS.getInstruction(); |
| 1057 | |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1058 | auto liveset = result.liveset; |
| 1059 | auto PointerToBase = result.PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1060 | |
| 1061 | auto is_live_gc_reference = |
| 1062 | [&](Value &V) { return isLiveGCReferenceAt(V, inst, DT, nullptr); }; |
| 1063 | |
| 1064 | // For each new definition, check to see if a) the definition dominates the |
| 1065 | // instruction we're interested in, and b) one of the uses of that definition |
| 1066 | // is edge-reachable from the instruction we're interested in. This is the |
| 1067 | // same definition of liveness we used in the intial liveness analysis |
| 1068 | for (Value *newDef : allInsertedDefs) { |
| 1069 | if (liveset.count(newDef)) { |
| 1070 | // already live, no action needed |
| 1071 | continue; |
| 1072 | } |
| 1073 | |
| 1074 | // PERF: Use DT to check instruction domination might not be good for |
| 1075 | // compilation time, and we could change to optimal solution if this |
| 1076 | // turn to be a issue |
| 1077 | if (!DT.dominates(cast<Instruction>(newDef), inst)) { |
| 1078 | // can't possibly be live at inst |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | if (is_live_gc_reference(*newDef)) { |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1083 | // Add the live new defs into liveset and PointerToBase |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1084 | liveset.insert(newDef); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1085 | PointerToBase[newDef] = newDef; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | result.liveset = liveset; |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1090 | result.PointerToBase = PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | static void fixupLiveReferences( |
| 1094 | Function &F, DominatorTree &DT, Pass *P, |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1095 | const DenseSet<llvm::Value *> &allInsertedDefs, ArrayRef<CallSite> toUpdate, |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1096 | MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1097 | for (size_t i = 0; i < records.size(); i++) { |
| 1098 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1099 | const CallSite &CS = toUpdate[i]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1100 | fixupLiveness(DT, CS, allInsertedDefs, info); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | // Normalize basic block to make it ready to be target of invoke statepoint. |
| 1105 | // It means spliting it to have single predecessor. Return newly created BB |
| 1106 | // ready to be successor of invoke statepoint. |
| 1107 | static BasicBlock *normalizeBBForInvokeSafepoint(BasicBlock *BB, |
| 1108 | BasicBlock *InvokeParent, |
| 1109 | Pass *P) { |
| 1110 | BasicBlock *ret = BB; |
| 1111 | |
| 1112 | if (!BB->getUniquePredecessor()) { |
| 1113 | ret = SplitBlockPredecessors(BB, InvokeParent, ""); |
| 1114 | } |
| 1115 | |
| 1116 | // Another requirement for such basic blocks is to not have any phi nodes. |
| 1117 | // Since we just ensured that new BB will have single predecessor, |
| 1118 | // all phi nodes in it will have one value. Here it would be naturall place |
| 1119 | // to |
| 1120 | // remove them all. But we can not do this because we are risking to remove |
| 1121 | // one of the values stored in liveset of another statepoint. We will do it |
| 1122 | // later after placing all safepoints. |
| 1123 | |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1127 | static int find_index(ArrayRef<Value *> livevec, Value *val) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1128 | auto itr = std::find(livevec.begin(), livevec.end(), val); |
| 1129 | assert(livevec.end() != itr); |
| 1130 | size_t index = std::distance(livevec.begin(), itr); |
| 1131 | assert(index < livevec.size()); |
| 1132 | return index; |
| 1133 | } |
| 1134 | |
| 1135 | // Create new attribute set containing only attributes which can be transfered |
| 1136 | // from original call to the safepoint. |
| 1137 | static AttributeSet legalizeCallAttributes(AttributeSet AS) { |
| 1138 | AttributeSet ret; |
| 1139 | |
| 1140 | for (unsigned Slot = 0; Slot < AS.getNumSlots(); Slot++) { |
| 1141 | unsigned index = AS.getSlotIndex(Slot); |
| 1142 | |
| 1143 | if (index == AttributeSet::ReturnIndex || |
| 1144 | index == AttributeSet::FunctionIndex) { |
| 1145 | |
| 1146 | for (auto it = AS.begin(Slot), it_end = AS.end(Slot); it != it_end; |
| 1147 | ++it) { |
| 1148 | Attribute attr = *it; |
| 1149 | |
| 1150 | // Do not allow certain attributes - just skip them |
| 1151 | // Safepoint can not be read only or read none. |
| 1152 | if (attr.hasAttribute(Attribute::ReadNone) || |
| 1153 | attr.hasAttribute(Attribute::ReadOnly)) |
| 1154 | continue; |
| 1155 | |
| 1156 | ret = ret.addAttributes( |
| 1157 | AS.getContext(), index, |
| 1158 | AttributeSet::get(AS.getContext(), index, AttrBuilder(attr))); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | // Just skip parameter attributes for now |
| 1163 | } |
| 1164 | |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | /// Helper function to place all gc relocates necessary for the given |
| 1169 | /// statepoint. |
| 1170 | /// Inputs: |
| 1171 | /// liveVariables - list of variables to be relocated. |
| 1172 | /// liveStart - index of the first live variable. |
| 1173 | /// basePtrs - base pointers. |
| 1174 | /// statepointToken - statepoint instruction to which relocates should be |
| 1175 | /// bound. |
| 1176 | /// Builder - Llvm IR builder to be used to construct new calls. |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 1177 | static void CreateGCRelocates(ArrayRef<llvm::Value *> liveVariables, |
| 1178 | const int liveStart, |
| 1179 | ArrayRef<llvm::Value *> basePtrs, |
| 1180 | Instruction *statepointToken, |
| 1181 | IRBuilder<> Builder) { |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1182 | SmallVector<Instruction *, 64> NewDefs; |
| 1183 | NewDefs.reserve(liveVariables.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1184 | |
| 1185 | Module *M = statepointToken->getParent()->getParent()->getParent(); |
| 1186 | |
| 1187 | for (unsigned i = 0; i < liveVariables.size(); i++) { |
| 1188 | // We generate a (potentially) unique declaration for every pointer type |
| 1189 | // combination. This results is some blow up the function declarations in |
| 1190 | // the IR, but removes the need for argument bitcasts which shrinks the IR |
| 1191 | // greatly and makes it much more readable. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1192 | SmallVector<Type *, 1> types; // one per 'any' type |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1193 | types.push_back(liveVariables[i]->getType()); // result type |
| 1194 | Value *gc_relocate_decl = Intrinsic::getDeclaration( |
| 1195 | M, Intrinsic::experimental_gc_relocate, types); |
| 1196 | |
| 1197 | // Generate the gc.relocate call and save the result |
| 1198 | Value *baseIdx = |
| 1199 | ConstantInt::get(Type::getInt32Ty(M->getContext()), |
| 1200 | liveStart + find_index(liveVariables, basePtrs[i])); |
| 1201 | Value *liveIdx = ConstantInt::get( |
| 1202 | Type::getInt32Ty(M->getContext()), |
| 1203 | liveStart + find_index(liveVariables, liveVariables[i])); |
| 1204 | |
| 1205 | // only specify a debug name if we can give a useful one |
| 1206 | Value *reloc = Builder.CreateCall3( |
| 1207 | gc_relocate_decl, statepointToken, baseIdx, liveIdx, |
| 1208 | liveVariables[i]->hasName() ? liveVariables[i]->getName() + ".relocated" |
| 1209 | : ""); |
| 1210 | // Trick CodeGen into thinking there are lots of free registers at this |
| 1211 | // fake call. |
| 1212 | cast<CallInst>(reloc)->setCallingConv(CallingConv::Cold); |
| 1213 | |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1214 | NewDefs.push_back(cast<Instruction>(reloc)); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1215 | } |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1216 | assert(NewDefs.size() == liveVariables.size() && |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1217 | "missing or extra redefinition at safepoint"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | static void |
| 1221 | makeStatepointExplicitImpl(const CallSite &CS, /* to replace */ |
| 1222 | const SmallVectorImpl<llvm::Value *> &basePtrs, |
| 1223 | const SmallVectorImpl<llvm::Value *> &liveVariables, |
| 1224 | Pass *P, |
| 1225 | PartiallyConstructedSafepointRecord &result) { |
| 1226 | assert(basePtrs.size() == liveVariables.size()); |
| 1227 | assert(isStatepoint(CS) && |
| 1228 | "This method expects to be rewriting a statepoint"); |
| 1229 | |
| 1230 | BasicBlock *BB = CS.getInstruction()->getParent(); |
| 1231 | assert(BB); |
| 1232 | Function *F = BB->getParent(); |
| 1233 | assert(F && "must be set"); |
| 1234 | Module *M = F->getParent(); |
Nick Lewycky | eb3231e | 2015-02-20 07:14:02 +0000 | [diff] [blame] | 1235 | (void)M; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1236 | assert(M && "must be set"); |
| 1237 | |
| 1238 | // We're not changing the function signature of the statepoint since the gc |
| 1239 | // arguments go into the var args section. |
| 1240 | Function *gc_statepoint_decl = CS.getCalledFunction(); |
| 1241 | |
| 1242 | // Then go ahead and use the builder do actually do the inserts. We insert |
| 1243 | // immediately before the previous instruction under the assumption that all |
| 1244 | // arguments will be available here. We can't insert afterwards since we may |
| 1245 | // be replacing a terminator. |
| 1246 | Instruction *insertBefore = CS.getInstruction(); |
| 1247 | IRBuilder<> Builder(insertBefore); |
| 1248 | // Copy all of the arguments from the original statepoint - this includes the |
| 1249 | // target, call args, and deopt args |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1250 | SmallVector<llvm::Value *, 64> args; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1251 | args.insert(args.end(), CS.arg_begin(), CS.arg_end()); |
| 1252 | // TODO: Clear the 'needs rewrite' flag |
| 1253 | |
| 1254 | // add all the pointers to be relocated (gc arguments) |
| 1255 | // Capture the start of the live variable list for use in the gc_relocates |
| 1256 | const int live_start = args.size(); |
| 1257 | args.insert(args.end(), liveVariables.begin(), liveVariables.end()); |
| 1258 | |
| 1259 | // Create the statepoint given all the arguments |
| 1260 | Instruction *token = nullptr; |
| 1261 | AttributeSet return_attributes; |
| 1262 | if (CS.isCall()) { |
| 1263 | CallInst *toReplace = cast<CallInst>(CS.getInstruction()); |
| 1264 | CallInst *call = |
| 1265 | Builder.CreateCall(gc_statepoint_decl, args, "safepoint_token"); |
| 1266 | call->setTailCall(toReplace->isTailCall()); |
| 1267 | call->setCallingConv(toReplace->getCallingConv()); |
| 1268 | |
| 1269 | // Currently we will fail on parameter attributes and on certain |
| 1270 | // function attributes. |
| 1271 | AttributeSet new_attrs = legalizeCallAttributes(toReplace->getAttributes()); |
| 1272 | // In case if we can handle this set of sttributes - set up function attrs |
| 1273 | // directly on statepoint and return attrs later for gc_result intrinsic. |
| 1274 | call->setAttributes(new_attrs.getFnAttributes()); |
| 1275 | return_attributes = new_attrs.getRetAttributes(); |
| 1276 | |
| 1277 | token = call; |
| 1278 | |
| 1279 | // Put the following gc_result and gc_relocate calls immediately after the |
| 1280 | // the old call (which we're about to delete) |
| 1281 | BasicBlock::iterator next(toReplace); |
| 1282 | assert(BB->end() != next && "not a terminator, must have next"); |
| 1283 | next++; |
| 1284 | Instruction *IP = &*(next); |
| 1285 | Builder.SetInsertPoint(IP); |
| 1286 | Builder.SetCurrentDebugLocation(IP->getDebugLoc()); |
| 1287 | |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1288 | } else { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1289 | InvokeInst *toReplace = cast<InvokeInst>(CS.getInstruction()); |
| 1290 | |
| 1291 | // Insert the new invoke into the old block. We'll remove the old one in a |
| 1292 | // moment at which point this will become the new terminator for the |
| 1293 | // original block. |
| 1294 | InvokeInst *invoke = InvokeInst::Create( |
| 1295 | gc_statepoint_decl, toReplace->getNormalDest(), |
| 1296 | toReplace->getUnwindDest(), args, "", toReplace->getParent()); |
| 1297 | invoke->setCallingConv(toReplace->getCallingConv()); |
| 1298 | |
| 1299 | // Currently we will fail on parameter attributes and on certain |
| 1300 | // function attributes. |
| 1301 | AttributeSet new_attrs = legalizeCallAttributes(toReplace->getAttributes()); |
| 1302 | // In case if we can handle this set of sttributes - set up function attrs |
| 1303 | // directly on statepoint and return attrs later for gc_result intrinsic. |
| 1304 | invoke->setAttributes(new_attrs.getFnAttributes()); |
| 1305 | return_attributes = new_attrs.getRetAttributes(); |
| 1306 | |
| 1307 | token = invoke; |
| 1308 | |
| 1309 | // Generate gc relocates in exceptional path |
| 1310 | BasicBlock *unwindBlock = normalizeBBForInvokeSafepoint( |
| 1311 | toReplace->getUnwindDest(), invoke->getParent(), P); |
| 1312 | |
| 1313 | Instruction *IP = &*(unwindBlock->getFirstInsertionPt()); |
| 1314 | Builder.SetInsertPoint(IP); |
| 1315 | Builder.SetCurrentDebugLocation(toReplace->getDebugLoc()); |
| 1316 | |
| 1317 | // Extract second element from landingpad return value. We will attach |
| 1318 | // exceptional gc relocates to it. |
| 1319 | const unsigned idx = 1; |
| 1320 | Instruction *exceptional_token = |
| 1321 | cast<Instruction>(Builder.CreateExtractValue( |
| 1322 | unwindBlock->getLandingPadInst(), idx, "relocate_token")); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1323 | result.UnwindToken = exceptional_token; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1324 | |
| 1325 | // Just throw away return value. We will use the one we got for normal |
| 1326 | // block. |
| 1327 | (void)CreateGCRelocates(liveVariables, live_start, basePtrs, |
| 1328 | exceptional_token, Builder); |
| 1329 | |
| 1330 | // Generate gc relocates and returns for normal block |
| 1331 | BasicBlock *normalDest = normalizeBBForInvokeSafepoint( |
| 1332 | toReplace->getNormalDest(), invoke->getParent(), P); |
| 1333 | |
| 1334 | IP = &*(normalDest->getFirstInsertionPt()); |
| 1335 | Builder.SetInsertPoint(IP); |
| 1336 | |
| 1337 | // gc relocates will be generated later as if it were regular call |
| 1338 | // statepoint |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1339 | } |
| 1340 | assert(token); |
| 1341 | |
| 1342 | // Take the name of the original value call if it had one. |
| 1343 | token->takeName(CS.getInstruction()); |
| 1344 | |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1345 | // The GCResult is already inserted, we just need to find it |
David Blaikie | 5e5d784 | 2015-02-22 20:58:38 +0000 | [diff] [blame] | 1346 | #ifndef NDEBUG |
| 1347 | Instruction *toReplace = CS.getInstruction(); |
| 1348 | assert((toReplace->hasNUses(0) || toReplace->hasNUses(1)) && |
| 1349 | "only valid use before rewrite is gc.result"); |
| 1350 | assert(!toReplace->hasOneUse() || |
| 1351 | isGCResult(cast<Instruction>(*toReplace->user_begin()))); |
| 1352 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1353 | |
| 1354 | // Update the gc.result of the original statepoint (if any) to use the newly |
| 1355 | // inserted statepoint. This is safe to do here since the token can't be |
| 1356 | // considered a live reference. |
| 1357 | CS.getInstruction()->replaceAllUsesWith(token); |
| 1358 | |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1359 | result.StatepointToken = token; |
| 1360 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1361 | // Second, create a gc.relocate for every live variable |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1362 | CreateGCRelocates(liveVariables, live_start, basePtrs, token, Builder); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | namespace { |
| 1366 | struct name_ordering { |
| 1367 | Value *base; |
| 1368 | Value *derived; |
| 1369 | bool operator()(name_ordering const &a, name_ordering const &b) { |
| 1370 | return -1 == a.derived->getName().compare(b.derived->getName()); |
| 1371 | } |
| 1372 | }; |
| 1373 | } |
| 1374 | static void stablize_order(SmallVectorImpl<Value *> &basevec, |
| 1375 | SmallVectorImpl<Value *> &livevec) { |
| 1376 | assert(basevec.size() == livevec.size()); |
| 1377 | |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 1378 | SmallVector<name_ordering, 64> temp; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1379 | for (size_t i = 0; i < basevec.size(); i++) { |
| 1380 | name_ordering v; |
| 1381 | v.base = basevec[i]; |
| 1382 | v.derived = livevec[i]; |
| 1383 | temp.push_back(v); |
| 1384 | } |
| 1385 | std::sort(temp.begin(), temp.end(), name_ordering()); |
| 1386 | for (size_t i = 0; i < basevec.size(); i++) { |
| 1387 | basevec[i] = temp[i].base; |
| 1388 | livevec[i] = temp[i].derived; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | // Replace an existing gc.statepoint with a new one and a set of gc.relocates |
| 1393 | // which make the relocations happening at this safepoint explicit. |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1394 | // |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1395 | // WARNING: Does not do any fixup to adjust users of the original live |
| 1396 | // values. That's the callers responsibility. |
| 1397 | static void |
| 1398 | makeStatepointExplicit(DominatorTree &DT, const CallSite &CS, Pass *P, |
| 1399 | PartiallyConstructedSafepointRecord &result) { |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1400 | auto liveset = result.liveset; |
| 1401 | auto PointerToBase = result.PointerToBase; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1402 | |
| 1403 | // Convert to vector for efficient cross referencing. |
| 1404 | SmallVector<Value *, 64> basevec, livevec; |
| 1405 | livevec.reserve(liveset.size()); |
| 1406 | basevec.reserve(liveset.size()); |
| 1407 | for (Value *L : liveset) { |
| 1408 | livevec.push_back(L); |
| 1409 | |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1410 | assert(PointerToBase.find(L) != PointerToBase.end()); |
| 1411 | Value *base = PointerToBase[L]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1412 | basevec.push_back(base); |
| 1413 | } |
| 1414 | assert(livevec.size() == basevec.size()); |
| 1415 | |
| 1416 | // To make the output IR slightly more stable (for use in diffs), ensure a |
| 1417 | // fixed order of the values in the safepoint (by sorting the value name). |
| 1418 | // The order is otherwise meaningless. |
| 1419 | stablize_order(basevec, livevec); |
| 1420 | |
| 1421 | // Do the actual rewriting and delete the old statepoint |
| 1422 | makeStatepointExplicitImpl(CS, basevec, livevec, P, result); |
| 1423 | CS.getInstruction()->eraseFromParent(); |
| 1424 | } |
| 1425 | |
| 1426 | // Helper function for the relocationViaAlloca. |
| 1427 | // It receives iterator to the statepoint gc relocates and emits store to the |
| 1428 | // assigned |
| 1429 | // location (via allocaMap) for the each one of them. |
| 1430 | // Add visited values into the visitedLiveValues set we will later use them |
| 1431 | // for sanity check. |
| 1432 | static void |
| 1433 | insertRelocationStores(iterator_range<Value::user_iterator> gcRelocs, |
| 1434 | DenseMap<Value *, Value *> &allocaMap, |
| 1435 | DenseSet<Value *> &visitedLiveValues) { |
| 1436 | |
| 1437 | for (User *U : gcRelocs) { |
| 1438 | if (!isa<IntrinsicInst>(U)) |
| 1439 | continue; |
| 1440 | |
| 1441 | IntrinsicInst *relocatedValue = cast<IntrinsicInst>(U); |
| 1442 | |
| 1443 | // We only care about relocates |
| 1444 | if (relocatedValue->getIntrinsicID() != |
| 1445 | Intrinsic::experimental_gc_relocate) { |
| 1446 | continue; |
| 1447 | } |
| 1448 | |
| 1449 | GCRelocateOperands relocateOperands(relocatedValue); |
| 1450 | Value *originalValue = const_cast<Value *>(relocateOperands.derivedPtr()); |
| 1451 | assert(allocaMap.count(originalValue)); |
| 1452 | Value *alloca = allocaMap[originalValue]; |
| 1453 | |
| 1454 | // Emit store into the related alloca |
| 1455 | StoreInst *store = new StoreInst(relocatedValue, alloca); |
| 1456 | store->insertAfter(relocatedValue); |
| 1457 | |
| 1458 | #ifndef NDEBUG |
| 1459 | visitedLiveValues.insert(originalValue); |
| 1460 | #endif |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | /// do all the relocation update via allocas and mem2reg |
| 1465 | static void relocationViaAlloca( |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1466 | Function &F, DominatorTree &DT, ArrayRef<Value *> live, |
| 1467 | ArrayRef<struct PartiallyConstructedSafepointRecord> records) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1468 | #ifndef NDEBUG |
Philip Reames | a6ebf07 | 2015-03-27 05:53:16 +0000 | [diff] [blame] | 1469 | // record initial number of (static) allocas; we'll check we have the same |
| 1470 | // number when we get done. |
| 1471 | int InitialAllocaNum = 0; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1472 | for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E; |
| 1473 | I++) |
Philip Reames | a6ebf07 | 2015-03-27 05:53:16 +0000 | [diff] [blame] | 1474 | if (isa<AllocaInst>(*I)) |
| 1475 | InitialAllocaNum++; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1476 | #endif |
| 1477 | |
| 1478 | // TODO-PERF: change data structures, reserve |
| 1479 | DenseMap<Value *, Value *> allocaMap; |
| 1480 | SmallVector<AllocaInst *, 200> PromotableAllocas; |
| 1481 | PromotableAllocas.reserve(live.size()); |
| 1482 | |
| 1483 | // emit alloca for each live gc pointer |
| 1484 | for (unsigned i = 0; i < live.size(); i++) { |
| 1485 | Value *liveValue = live[i]; |
| 1486 | AllocaInst *alloca = new AllocaInst(liveValue->getType(), "", |
| 1487 | F.getEntryBlock().getFirstNonPHI()); |
| 1488 | allocaMap[liveValue] = alloca; |
| 1489 | PromotableAllocas.push_back(alloca); |
| 1490 | } |
| 1491 | |
| 1492 | // The next two loops are part of the same conceptual operation. We need to |
| 1493 | // insert a store to the alloca after the original def and at each |
| 1494 | // redefinition. We need to insert a load before each use. These are split |
| 1495 | // into distinct loops for performance reasons. |
| 1496 | |
| 1497 | // update gc pointer after each statepoint |
| 1498 | // either store a relocated value or null (if no relocated value found for |
| 1499 | // this gc pointer and it is not a gc_result) |
| 1500 | // this must happen before we update the statepoint with load of alloca |
| 1501 | // otherwise we lose the link between statepoint and old def |
| 1502 | for (size_t i = 0; i < records.size(); i++) { |
| 1503 | const struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1504 | Value *Statepoint = info.StatepointToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1505 | |
| 1506 | // This will be used for consistency check |
| 1507 | DenseSet<Value *> visitedLiveValues; |
| 1508 | |
| 1509 | // Insert stores for normal statepoint gc relocates |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1510 | insertRelocationStores(Statepoint->users(), allocaMap, visitedLiveValues); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1511 | |
| 1512 | // In case if it was invoke statepoint |
| 1513 | // we will insert stores for exceptional path gc relocates. |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1514 | if (isa<InvokeInst>(Statepoint)) { |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1515 | insertRelocationStores(info.UnwindToken->users(), allocaMap, |
| 1516 | visitedLiveValues); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | #ifndef NDEBUG |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1520 | // As a debuging aid, pretend that an unrelocated pointer becomes null at |
| 1521 | // the gc.statepoint. This will turn some subtle GC problems into slightly |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1522 | // easier to debug SEGVs |
| 1523 | SmallVector<AllocaInst *, 64> ToClobber; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1524 | for (auto Pair : allocaMap) { |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1525 | Value *Def = Pair.first; |
| 1526 | AllocaInst *Alloca = cast<AllocaInst>(Pair.second); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1527 | |
| 1528 | // This value was relocated |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1529 | if (visitedLiveValues.count(Def)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1530 | continue; |
| 1531 | } |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1532 | ToClobber.push_back(Alloca); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1533 | } |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1534 | |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1535 | auto InsertClobbersAt = [&](Instruction *IP) { |
| 1536 | for (auto *AI : ToClobber) { |
| 1537 | auto AIType = cast<PointerType>(AI->getType()); |
| 1538 | auto PT = cast<PointerType>(AIType->getElementType()); |
| 1539 | Constant *CPN = ConstantPointerNull::get(PT); |
| 1540 | StoreInst *store = new StoreInst(CPN, AI); |
| 1541 | store->insertBefore(IP); |
| 1542 | } |
| 1543 | }; |
| 1544 | |
| 1545 | // Insert the clobbering stores. These may get intermixed with the |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1546 | // gc.results and gc.relocates, but that's fine. |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1547 | if (auto II = dyn_cast<InvokeInst>(Statepoint)) { |
| 1548 | InsertClobbersAt(II->getNormalDest()->getFirstInsertionPt()); |
| 1549 | InsertClobbersAt(II->getUnwindDest()->getFirstInsertionPt()); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1550 | } else { |
| 1551 | BasicBlock::iterator Next(cast<CallInst>(Statepoint)); |
Philip Reames | fa2fcf17 | 2015-02-20 19:51:56 +0000 | [diff] [blame] | 1552 | Next++; |
| 1553 | InsertClobbersAt(Next); |
David Blaikie | 82ad787 | 2015-02-20 23:44:24 +0000 | [diff] [blame] | 1554 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1555 | #endif |
| 1556 | } |
| 1557 | // update use with load allocas and add store for gc_relocated |
| 1558 | for (auto Pair : allocaMap) { |
| 1559 | Value *def = Pair.first; |
| 1560 | Value *alloca = Pair.second; |
| 1561 | |
| 1562 | // we pre-record the uses of allocas so that we dont have to worry about |
| 1563 | // later update |
| 1564 | // that change the user information. |
| 1565 | SmallVector<Instruction *, 20> uses; |
| 1566 | // PERF: trade a linear scan for repeated reallocation |
| 1567 | uses.reserve(std::distance(def->user_begin(), def->user_end())); |
| 1568 | for (User *U : def->users()) { |
| 1569 | if (!isa<ConstantExpr>(U)) { |
| 1570 | // If the def has a ConstantExpr use, then the def is either a |
| 1571 | // ConstantExpr use itself or null. In either case |
| 1572 | // (recursively in the first, directly in the second), the oop |
| 1573 | // it is ultimately dependent on is null and this particular |
| 1574 | // use does not need to be fixed up. |
| 1575 | uses.push_back(cast<Instruction>(U)); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | std::sort(uses.begin(), uses.end()); |
| 1580 | auto last = std::unique(uses.begin(), uses.end()); |
| 1581 | uses.erase(last, uses.end()); |
| 1582 | |
| 1583 | for (Instruction *use : uses) { |
| 1584 | if (isa<PHINode>(use)) { |
| 1585 | PHINode *phi = cast<PHINode>(use); |
| 1586 | for (unsigned i = 0; i < phi->getNumIncomingValues(); i++) { |
| 1587 | if (def == phi->getIncomingValue(i)) { |
| 1588 | LoadInst *load = new LoadInst( |
| 1589 | alloca, "", phi->getIncomingBlock(i)->getTerminator()); |
| 1590 | phi->setIncomingValue(i, load); |
| 1591 | } |
| 1592 | } |
| 1593 | } else { |
| 1594 | LoadInst *load = new LoadInst(alloca, "", use); |
| 1595 | use->replaceUsesOfWith(def, load); |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | // emit store for the initial gc value |
| 1600 | // store must be inserted after load, otherwise store will be in alloca's |
| 1601 | // use list and an extra load will be inserted before it |
| 1602 | StoreInst *store = new StoreInst(def, alloca); |
Philip Reames | 6da3785 | 2015-03-04 00:13:52 +0000 | [diff] [blame] | 1603 | if (Instruction *inst = dyn_cast<Instruction>(def)) { |
| 1604 | if (InvokeInst *invoke = dyn_cast<InvokeInst>(inst)) { |
| 1605 | // InvokeInst is a TerminatorInst so the store need to be inserted |
| 1606 | // into its normal destination block. |
| 1607 | BasicBlock *normalDest = invoke->getNormalDest(); |
| 1608 | store->insertBefore(normalDest->getFirstNonPHI()); |
| 1609 | } else { |
| 1610 | assert(!inst->isTerminator() && |
| 1611 | "The only TerminatorInst that can produce a value is " |
| 1612 | "InvokeInst which is handled above."); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1613 | store->insertAfter(inst); |
Philip Reames | 6da3785 | 2015-03-04 00:13:52 +0000 | [diff] [blame] | 1614 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1615 | } else { |
| 1616 | assert((isa<Argument>(def) || isa<GlobalVariable>(def) || |
Philip Reames | 24c6cd5 | 2015-03-27 05:47:00 +0000 | [diff] [blame] | 1617 | isa<ConstantPointerNull>(def)) && |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1618 | "Must be argument or global"); |
| 1619 | store->insertAfter(cast<Instruction>(alloca)); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | assert(PromotableAllocas.size() == live.size() && |
| 1624 | "we must have the same allocas with lives"); |
| 1625 | if (!PromotableAllocas.empty()) { |
| 1626 | // apply mem2reg to promote alloca to SSA |
| 1627 | PromoteMemToReg(PromotableAllocas, DT); |
| 1628 | } |
| 1629 | |
| 1630 | #ifndef NDEBUG |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1631 | for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E; |
| 1632 | I++) |
Philip Reames | a6ebf07 | 2015-03-27 05:53:16 +0000 | [diff] [blame] | 1633 | if (isa<AllocaInst>(*I)) |
| 1634 | InitialAllocaNum--; |
| 1635 | assert(InitialAllocaNum == 0 && "We must not introduce any extra allocas"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1636 | #endif |
| 1637 | } |
| 1638 | |
| 1639 | /// Implement a unique function which doesn't require we sort the input |
| 1640 | /// vector. Doing so has the effect of changing the output of a couple of |
| 1641 | /// tests in ways which make them less useful in testing fused safepoints. |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1642 | template <typename T> static void unique_unsorted(SmallVectorImpl<T> &Vec) { |
| 1643 | DenseSet<T> Seen; |
| 1644 | SmallVector<T, 128> TempVec; |
| 1645 | TempVec.reserve(Vec.size()); |
| 1646 | for (auto Element : Vec) |
| 1647 | TempVec.push_back(Element); |
| 1648 | Vec.clear(); |
| 1649 | for (auto V : TempVec) { |
| 1650 | if (Seen.insert(V).second) { |
| 1651 | Vec.push_back(V); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | static Function *getUseHolder(Module &M) { |
| 1657 | FunctionType *ftype = |
| 1658 | FunctionType::get(Type::getVoidTy(M.getContext()), true); |
| 1659 | Function *Func = cast<Function>(M.getOrInsertFunction("__tmp_use", ftype)); |
| 1660 | return Func; |
| 1661 | } |
| 1662 | |
| 1663 | /// Insert holders so that each Value is obviously live through the entire |
| 1664 | /// liftetime of the call. |
| 1665 | static void insertUseHolderAfter(CallSite &CS, const ArrayRef<Value *> Values, |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1666 | SmallVectorImpl<CallInst *> &holders) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1667 | Module *M = CS.getInstruction()->getParent()->getParent()->getParent(); |
| 1668 | Function *Func = getUseHolder(*M); |
| 1669 | if (CS.isCall()) { |
| 1670 | // For call safepoints insert dummy calls right after safepoint |
| 1671 | BasicBlock::iterator next(CS.getInstruction()); |
| 1672 | next++; |
| 1673 | CallInst *base_holder = CallInst::Create(Func, Values, "", next); |
| 1674 | holders.push_back(base_holder); |
| 1675 | } else if (CS.isInvoke()) { |
| 1676 | // For invoke safepooints insert dummy calls both in normal and |
| 1677 | // exceptional destination blocks |
| 1678 | InvokeInst *invoke = cast<InvokeInst>(CS.getInstruction()); |
| 1679 | CallInst *normal_holder = CallInst::Create( |
| 1680 | Func, Values, "", invoke->getNormalDest()->getFirstInsertionPt()); |
| 1681 | CallInst *unwind_holder = CallInst::Create( |
| 1682 | Func, Values, "", invoke->getUnwindDest()->getFirstInsertionPt()); |
| 1683 | holders.push_back(normal_holder); |
| 1684 | holders.push_back(unwind_holder); |
Philip Reames | 860660e | 2015-02-20 22:05:18 +0000 | [diff] [blame] | 1685 | } else |
| 1686 | llvm_unreachable("unsupported call type"); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static void findLiveReferences( |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1690 | Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate, |
| 1691 | MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1692 | for (size_t i = 0; i < records.size(); i++) { |
| 1693 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1694 | const CallSite &CS = toUpdate[i]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1695 | analyzeParsePointLiveness(DT, CS, info); |
| 1696 | } |
| 1697 | } |
| 1698 | |
Philip Reames | 1f01754 | 2015-02-20 23:16:52 +0000 | [diff] [blame] | 1699 | static void addBasesAsLiveValues(StatepointLiveSetTy &liveset, |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1700 | DenseMap<Value *, Value *> &PointerToBase) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1701 | // Identify any base pointers which are used in this safepoint, but not |
| 1702 | // themselves relocated. We need to relocate them so that later inserted |
| 1703 | // safepoints can get the properly relocated base register. |
| 1704 | DenseSet<Value *> missing; |
| 1705 | for (Value *L : liveset) { |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1706 | assert(PointerToBase.find(L) != PointerToBase.end()); |
| 1707 | Value *base = PointerToBase[L]; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1708 | assert(base); |
| 1709 | if (liveset.find(base) == liveset.end()) { |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1710 | assert(PointerToBase.find(base) == PointerToBase.end()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1711 | // uniqued by set insert |
| 1712 | missing.insert(base); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | // Note that we want these at the end of the list, otherwise |
| 1717 | // register placement gets screwed up once we lower to STATEPOINT |
| 1718 | // instructions. This is an utter hack, but there doesn't seem to be a |
| 1719 | // better one. |
| 1720 | for (Value *base : missing) { |
| 1721 | assert(base); |
| 1722 | liveset.insert(base); |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1723 | PointerToBase[base] = base; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1724 | } |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1725 | assert(liveset.size() == PointerToBase.size()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1728 | /// Remove any vector of pointers from the liveset by scalarizing them over the |
| 1729 | /// statepoint instruction. Adds the scalarized pieces to the liveset. It |
| 1730 | /// would be preferrable to include the vector in the statepoint itself, but |
| 1731 | /// the lowering code currently does not handle that. Extending it would be |
| 1732 | /// slightly non-trivial since it requires a format change. Given how rare |
| 1733 | /// such cases are (for the moment?) scalarizing is an acceptable comprimise. |
| 1734 | static void splitVectorValues(Instruction *StatepointInst, |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1735 | StatepointLiveSetTy &LiveSet, DominatorTree &DT) { |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1736 | SmallVector<Value *, 16> ToSplit; |
| 1737 | for (Value *V : LiveSet) |
| 1738 | if (isa<VectorType>(V->getType())) |
| 1739 | ToSplit.push_back(V); |
| 1740 | |
| 1741 | if (ToSplit.empty()) |
| 1742 | return; |
| 1743 | |
| 1744 | Function &F = *(StatepointInst->getParent()->getParent()); |
| 1745 | |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1746 | DenseMap<Value *, AllocaInst *> AllocaMap; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1747 | // First is normal return, second is exceptional return (invoke only) |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1748 | DenseMap<Value *, std::pair<Value *, Value *>> Replacements; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1749 | for (Value *V : ToSplit) { |
| 1750 | LiveSet.erase(V); |
| 1751 | |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1752 | AllocaInst *Alloca = |
| 1753 | new AllocaInst(V->getType(), "", F.getEntryBlock().getFirstNonPHI()); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1754 | AllocaMap[V] = Alloca; |
| 1755 | |
| 1756 | VectorType *VT = cast<VectorType>(V->getType()); |
| 1757 | IRBuilder<> Builder(StatepointInst); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1758 | SmallVector<Value *, 16> Elements; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1759 | for (unsigned i = 0; i < VT->getNumElements(); i++) |
| 1760 | Elements.push_back(Builder.CreateExtractElement(V, Builder.getInt32(i))); |
| 1761 | LiveSet.insert(Elements.begin(), Elements.end()); |
| 1762 | |
| 1763 | auto InsertVectorReform = [&](Instruction *IP) { |
| 1764 | Builder.SetInsertPoint(IP); |
| 1765 | Builder.SetCurrentDebugLocation(IP->getDebugLoc()); |
| 1766 | Value *ResultVec = UndefValue::get(VT); |
| 1767 | for (unsigned i = 0; i < VT->getNumElements(); i++) |
| 1768 | ResultVec = Builder.CreateInsertElement(ResultVec, Elements[i], |
| 1769 | Builder.getInt32(i)); |
| 1770 | return ResultVec; |
| 1771 | }; |
| 1772 | |
| 1773 | if (isa<CallInst>(StatepointInst)) { |
| 1774 | BasicBlock::iterator Next(StatepointInst); |
| 1775 | Next++; |
| 1776 | Instruction *IP = &*(Next); |
| 1777 | Replacements[V].first = InsertVectorReform(IP); |
| 1778 | Replacements[V].second = nullptr; |
| 1779 | } else { |
| 1780 | InvokeInst *Invoke = cast<InvokeInst>(StatepointInst); |
| 1781 | // We've already normalized - check that we don't have shared destination |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1782 | // blocks |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1783 | BasicBlock *NormalDest = Invoke->getNormalDest(); |
| 1784 | assert(!isa<PHINode>(NormalDest->begin())); |
| 1785 | BasicBlock *UnwindDest = Invoke->getUnwindDest(); |
| 1786 | assert(!isa<PHINode>(UnwindDest->begin())); |
| 1787 | // Insert insert element sequences in both successors |
| 1788 | Instruction *IP = &*(NormalDest->getFirstInsertionPt()); |
| 1789 | Replacements[V].first = InsertVectorReform(IP); |
| 1790 | IP = &*(UnwindDest->getFirstInsertionPt()); |
| 1791 | Replacements[V].second = InsertVectorReform(IP); |
| 1792 | } |
| 1793 | } |
| 1794 | for (Value *V : ToSplit) { |
| 1795 | AllocaInst *Alloca = AllocaMap[V]; |
| 1796 | |
| 1797 | // Capture all users before we start mutating use lists |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1798 | SmallVector<Instruction *, 16> Users; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1799 | for (User *U : V->users()) |
| 1800 | Users.push_back(cast<Instruction>(U)); |
| 1801 | |
| 1802 | for (Instruction *I : Users) { |
| 1803 | if (auto Phi = dyn_cast<PHINode>(I)) { |
| 1804 | for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++) |
| 1805 | if (V == Phi->getIncomingValue(i)) { |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1806 | LoadInst *Load = new LoadInst( |
| 1807 | Alloca, "", Phi->getIncomingBlock(i)->getTerminator()); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1808 | Phi->setIncomingValue(i, Load); |
| 1809 | } |
| 1810 | } else { |
| 1811 | LoadInst *Load = new LoadInst(Alloca, "", I); |
| 1812 | I->replaceUsesOfWith(V, Load); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | // Store the original value and the replacement value into the alloca |
| 1817 | StoreInst *Store = new StoreInst(V, Alloca); |
| 1818 | if (auto I = dyn_cast<Instruction>(V)) |
| 1819 | Store->insertAfter(I); |
| 1820 | else |
| 1821 | Store->insertAfter(Alloca); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1822 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1823 | // Normal return for invoke, or call return |
| 1824 | Instruction *Replacement = cast<Instruction>(Replacements[V].first); |
| 1825 | (new StoreInst(Replacement, Alloca))->insertAfter(Replacement); |
| 1826 | // Unwind return for invoke only |
| 1827 | Replacement = cast_or_null<Instruction>(Replacements[V].second); |
| 1828 | if (Replacement) |
| 1829 | (new StoreInst(Replacement, Alloca))->insertAfter(Replacement); |
| 1830 | } |
| 1831 | |
| 1832 | // apply mem2reg to promote alloca to SSA |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 1833 | SmallVector<AllocaInst *, 16> Allocas; |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1834 | for (Value *V : ToSplit) |
| 1835 | Allocas.push_back(AllocaMap[V]); |
| 1836 | PromoteMemToReg(Allocas, DT); |
| 1837 | } |
| 1838 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1839 | static bool insertParsePoints(Function &F, DominatorTree &DT, Pass *P, |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1840 | SmallVectorImpl<CallSite> &toUpdate) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1841 | #ifndef NDEBUG |
| 1842 | // sanity check the input |
| 1843 | std::set<CallSite> uniqued; |
| 1844 | uniqued.insert(toUpdate.begin(), toUpdate.end()); |
| 1845 | assert(uniqued.size() == toUpdate.size() && "no duplicates please!"); |
| 1846 | |
| 1847 | for (size_t i = 0; i < toUpdate.size(); i++) { |
| 1848 | CallSite &CS = toUpdate[i]; |
| 1849 | assert(CS.getInstruction()->getParent()->getParent() == &F); |
| 1850 | assert(isStatepoint(CS) && "expected to already be a deopt statepoint"); |
| 1851 | } |
| 1852 | #endif |
| 1853 | |
| 1854 | // A list of dummy calls added to the IR to keep various values obviously |
| 1855 | // live in the IR. We'll remove all of these when done. |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1856 | SmallVector<CallInst *, 64> holders; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Insert a dummy call with all of the arguments to the vm_state we'll need |
| 1859 | // for the actual safepoint insertion. This ensures reference arguments in |
| 1860 | // the deopt argument list are considered live through the safepoint (and |
| 1861 | // thus makes sure they get relocated.) |
| 1862 | for (size_t i = 0; i < toUpdate.size(); i++) { |
| 1863 | CallSite &CS = toUpdate[i]; |
| 1864 | Statepoint StatepointCS(CS); |
| 1865 | |
| 1866 | SmallVector<Value *, 64> DeoptValues; |
| 1867 | for (Use &U : StatepointCS.vm_state_args()) { |
| 1868 | Value *Arg = cast<Value>(&U); |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1869 | assert(!isUnhandledGCPointerType(Arg->getType()) && |
| 1870 | "support for FCA unimplemented"); |
| 1871 | if (isHandledGCPointerType(Arg->getType())) |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1872 | DeoptValues.push_back(Arg); |
| 1873 | } |
| 1874 | insertUseHolderAfter(CS, DeoptValues, holders); |
| 1875 | } |
| 1876 | |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 1877 | SmallVector<struct PartiallyConstructedSafepointRecord, 64> records; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1878 | records.reserve(toUpdate.size()); |
| 1879 | for (size_t i = 0; i < toUpdate.size(); i++) { |
| 1880 | struct PartiallyConstructedSafepointRecord info; |
| 1881 | records.push_back(info); |
| 1882 | } |
| 1883 | assert(records.size() == toUpdate.size()); |
| 1884 | |
| 1885 | // A) Identify all gc pointers which are staticly live at the given call |
| 1886 | // site. |
| 1887 | findLiveReferences(F, DT, P, toUpdate, records); |
| 1888 | |
Philip Reames | 8531d8c | 2015-04-10 21:48:25 +0000 | [diff] [blame] | 1889 | // Do a limited scalarization of any live at safepoint vector values which |
| 1890 | // contain pointers. This enables this pass to run after vectorization at |
| 1891 | // the cost of some possible performance loss. TODO: it would be nice to |
| 1892 | // natively support vectors all the way through the backend so we don't need |
| 1893 | // to scalarize here. |
| 1894 | for (size_t i = 0; i < records.size(); i++) { |
| 1895 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
| 1896 | Instruction *statepoint = toUpdate[i].getInstruction(); |
| 1897 | splitVectorValues(cast<Instruction>(statepoint), info.liveset, DT); |
| 1898 | } |
| 1899 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1900 | // B) Find the base pointers for each live pointer |
| 1901 | /* scope for caching */ { |
| 1902 | // Cache the 'defining value' relation used in the computation and |
| 1903 | // insertion of base phis and selects. This ensures that we don't insert |
| 1904 | // large numbers of duplicate base_phis. |
| 1905 | DefiningValueMapTy DVCache; |
| 1906 | |
| 1907 | for (size_t i = 0; i < records.size(); i++) { |
| 1908 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
| 1909 | CallSite &CS = toUpdate[i]; |
| 1910 | findBasePointers(DT, DVCache, CS, info); |
| 1911 | } |
| 1912 | } // end of cache scope |
| 1913 | |
| 1914 | // The base phi insertion logic (for any safepoint) may have inserted new |
| 1915 | // instructions which are now live at some safepoint. The simplest such |
| 1916 | // example is: |
| 1917 | // loop: |
| 1918 | // phi a <-- will be a new base_phi here |
| 1919 | // safepoint 1 <-- that needs to be live here |
| 1920 | // gep a + 1 |
| 1921 | // safepoint 2 |
| 1922 | // br loop |
Philip Reames | 1f01754 | 2015-02-20 23:16:52 +0000 | [diff] [blame] | 1923 | DenseSet<llvm::Value *> allInsertedDefs; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1924 | for (size_t i = 0; i < records.size(); i++) { |
| 1925 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1926 | allInsertedDefs.insert(info.NewInsertedDefs.begin(), |
| 1927 | info.NewInsertedDefs.end()); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | // We insert some dummy calls after each safepoint to definitely hold live |
| 1931 | // the base pointers which were identified for that safepoint. We'll then |
| 1932 | // ask liveness for _every_ base inserted to see what is now live. Then we |
| 1933 | // remove the dummy calls. |
| 1934 | holders.reserve(holders.size() + records.size()); |
| 1935 | for (size_t i = 0; i < records.size(); i++) { |
| 1936 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
| 1937 | CallSite &CS = toUpdate[i]; |
| 1938 | |
| 1939 | SmallVector<Value *, 128> Bases; |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1940 | for (auto Pair : info.PointerToBase) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1941 | Bases.push_back(Pair.second); |
| 1942 | } |
| 1943 | insertUseHolderAfter(CS, Bases, holders); |
| 1944 | } |
| 1945 | |
| 1946 | // Add the bases explicitly to the live vector set. This may result in a few |
| 1947 | // extra relocations, but the base has to be available whenever a pointer |
| 1948 | // derived from it is used. Thus, we need it to be part of the statepoint's |
| 1949 | // gc arguments list. TODO: Introduce an explicit notion (in the following |
| 1950 | // code) of the GC argument list as seperate from the live Values at a |
| 1951 | // given statepoint. |
| 1952 | for (size_t i = 0; i < records.size(); i++) { |
| 1953 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1954 | addBasesAsLiveValues(info.liveset, info.PointerToBase); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | // If we inserted any new values, we need to adjust our notion of what is |
| 1958 | // live at a particular safepoint. |
| 1959 | if (!allInsertedDefs.empty()) { |
| 1960 | fixupLiveReferences(F, DT, P, allInsertedDefs, toUpdate, records); |
| 1961 | } |
| 1962 | if (PrintBasePointers) { |
| 1963 | for (size_t i = 0; i < records.size(); i++) { |
| 1964 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
| 1965 | errs() << "Base Pairs: (w/Relocation)\n"; |
Philip Reames | f204132 | 2015-02-20 19:26:04 +0000 | [diff] [blame] | 1966 | for (auto Pair : info.PointerToBase) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 1967 | errs() << " derived %" << Pair.first->getName() << " base %" |
| 1968 | << Pair.second->getName() << "\n"; |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | for (size_t i = 0; i < holders.size(); i++) { |
| 1973 | holders[i]->eraseFromParent(); |
| 1974 | holders[i] = nullptr; |
| 1975 | } |
| 1976 | holders.clear(); |
| 1977 | |
| 1978 | // Now run through and replace the existing statepoints with new ones with |
| 1979 | // the live variables listed. We do not yet update uses of the values being |
| 1980 | // relocated. We have references to live variables that need to |
| 1981 | // survive to the last iteration of this loop. (By construction, the |
| 1982 | // previous statepoint can not be a live variable, thus we can and remove |
| 1983 | // the old statepoint calls as we go.) |
| 1984 | for (size_t i = 0; i < records.size(); i++) { |
| 1985 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
| 1986 | CallSite &CS = toUpdate[i]; |
| 1987 | makeStatepointExplicit(DT, CS, P, info); |
| 1988 | } |
| 1989 | toUpdate.clear(); // prevent accident use of invalid CallSites |
| 1990 | |
| 1991 | // In case if we inserted relocates in a different basic block than the |
| 1992 | // original safepoint (this can happen for invokes). We need to be sure that |
| 1993 | // original values were not used in any of the phi nodes at the |
| 1994 | // beginning of basic block containing them. Because we know that all such |
| 1995 | // blocks will have single predecessor we can safely assume that all phi |
| 1996 | // nodes have single entry (because of normalizeBBForInvokeSafepoint). |
| 1997 | // Just remove them all here. |
| 1998 | for (size_t i = 0; i < records.size(); i++) { |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 1999 | Instruction *I = records[i].StatepointToken; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2000 | |
| 2001 | if (InvokeInst *invoke = dyn_cast<InvokeInst>(I)) { |
| 2002 | FoldSingleEntryPHINodes(invoke->getNormalDest()); |
| 2003 | assert(!isa<PHINode>(invoke->getNormalDest()->begin())); |
| 2004 | |
| 2005 | FoldSingleEntryPHINodes(invoke->getUnwindDest()); |
| 2006 | assert(!isa<PHINode>(invoke->getUnwindDest()->begin())); |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | // 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] | 2011 | SmallVector<Value *, 128> live; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2012 | for (size_t i = 0; i < records.size(); i++) { |
| 2013 | struct PartiallyConstructedSafepointRecord &info = records[i]; |
| 2014 | // We can't simply save the live set from the original insertion. One of |
| 2015 | // the live values might be the result of a call which needs a safepoint. |
| 2016 | // That Value* no longer exists and we need to use the new gc_result. |
| 2017 | // Thankfully, the liveset is embedded in the statepoint (and updated), so |
| 2018 | // we just grab that. |
Philip Reames | 0a3240f | 2015-02-20 21:34:11 +0000 | [diff] [blame] | 2019 | Statepoint statepoint(info.StatepointToken); |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2020 | live.insert(live.end(), statepoint.gc_args_begin(), |
| 2021 | statepoint.gc_args_end()); |
| 2022 | } |
| 2023 | unique_unsorted(live); |
| 2024 | |
Nick Lewycky | eb3231e | 2015-02-20 07:14:02 +0000 | [diff] [blame] | 2025 | #ifndef NDEBUG |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2026 | // sanity check |
| 2027 | for (auto ptr : live) { |
| 2028 | assert(isGCPointerType(ptr->getType()) && "must be a gc pointer type"); |
| 2029 | } |
Nick Lewycky | eb3231e | 2015-02-20 07:14:02 +0000 | [diff] [blame] | 2030 | #endif |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2031 | |
| 2032 | relocationViaAlloca(F, DT, live, records); |
| 2033 | return !records.empty(); |
| 2034 | } |
| 2035 | |
| 2036 | /// Returns true if this function should be rewritten by this pass. The main |
| 2037 | /// point of this function is as an extension point for custom logic. |
| 2038 | static bool shouldRewriteStatepointsIn(Function &F) { |
| 2039 | // TODO: This should check the GCStrategy |
Philip Reames | 2ef029c | 2015-02-20 18:56:14 +0000 | [diff] [blame] | 2040 | if (F.hasGC()) { |
| 2041 | const std::string StatepointExampleName("statepoint-example"); |
| 2042 | return StatepointExampleName == F.getGC(); |
| 2043 | } else |
| 2044 | return false; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | bool RewriteStatepointsForGC::runOnFunction(Function &F) { |
| 2048 | // Nothing to do for declarations. |
| 2049 | if (F.isDeclaration() || F.empty()) |
| 2050 | return false; |
| 2051 | |
| 2052 | // Policy choice says not to rewrite - the most common reason is that we're |
| 2053 | // compiling code without a GCStrategy. |
| 2054 | if (!shouldRewriteStatepointsIn(F)) |
| 2055 | return false; |
| 2056 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2057 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 2058 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2059 | // Gather all the statepoints which need rewritten. Be careful to only |
| 2060 | // consider those in reachable code since we need to ask dominance queries |
| 2061 | // when rewriting. We'll delete the unreachable ones in a moment. |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 2062 | SmallVector<CallSite, 64> ParsePointNeeded; |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 2063 | bool HasUnreachableStatepoint = false; |
Philip Reames | d2b6646 | 2015-02-20 22:39:41 +0000 | [diff] [blame] | 2064 | for (Instruction &I : inst_range(F)) { |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2065 | // TODO: only the ones with the flag set! |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2066 | if (isStatepoint(I)) { |
| 2067 | if (DT.isReachableFromEntry(I.getParent())) |
| 2068 | ParsePointNeeded.push_back(CallSite(&I)); |
| 2069 | else |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 2070 | HasUnreachableStatepoint = true; |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2071 | } |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2074 | bool MadeChange = false; |
Philip Reames | 704e78b | 2015-04-10 22:34:56 +0000 | [diff] [blame^] | 2075 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2076 | // Delete any unreachable statepoints so that we don't have unrewritten |
| 2077 | // statepoints surviving this pass. This makes testing easier and the |
| 2078 | // resulting IR less confusing to human readers. Rather than be fancy, we |
| 2079 | // just reuse a utility function which removes the unreachable blocks. |
Philip Reames | f66d737 | 2015-04-10 22:16:58 +0000 | [diff] [blame] | 2080 | if (HasUnreachableStatepoint) |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2081 | MadeChange |= removeUnreachableBlocks(F); |
| 2082 | |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2083 | // Return early if no work to do. |
| 2084 | if (ParsePointNeeded.empty()) |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2085 | return MadeChange; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2086 | |
Philip Reames | 85b36a8 | 2015-04-10 22:07:04 +0000 | [diff] [blame] | 2087 | // As a prepass, go ahead and aggressively destroy single entry phi nodes. |
| 2088 | // These are created by LCSSA. They have the effect of increasing the size |
| 2089 | // of liveness sets for no good reason. It may be harder to do this post |
| 2090 | // insertion since relocations and base phis can confuse things. |
| 2091 | for (BasicBlock &BB : F) |
| 2092 | if (BB.getUniquePredecessor()) { |
| 2093 | MadeChange = true; |
| 2094 | FoldSingleEntryPHINodes(&BB); |
| 2095 | } |
| 2096 | |
| 2097 | MadeChange |= insertParsePoints(F, DT, this, ParsePointNeeded); |
| 2098 | return MadeChange; |
Philip Reames | d16a9b1 | 2015-02-20 01:06:44 +0000 | [diff] [blame] | 2099 | } |