blob: e46958527b3b22e0034628fc8b0c63bd8c20343a [file] [log] [blame]
Philip Reamesd16a9b12015-02-20 01:06:44 +00001//===- RewriteStatepointsForGC.cpp - Make GC relocations explicit ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Rewrite an existing set of gc.statepoints such that they make potential
11// relocations performed by the garbage collector explicit in the IR.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Pass.h"
16#include "llvm/Analysis/CFG.h"
Philip Reamesabcdc5e2015-08-27 01:02:28 +000017#include "llvm/Analysis/InstructionSimplify.h"
Igor Laevskye0317182015-05-19 15:59:05 +000018#include "llvm/Analysis/TargetTransformInfo.h"
Philip Reamesd16a9b12015-02-20 01:06:44 +000019#include "llvm/ADT/SetOperations.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/ADT/DenseSet.h"
Philip Reames4d80ede2015-04-10 23:11:26 +000022#include "llvm/ADT/SetVector.h"
Swaroop Sridhar665bc9c2015-05-20 01:07:23 +000023#include "llvm/ADT/StringRef.h"
Philip Reamesd16a9b12015-02-20 01:06:44 +000024#include "llvm/IR/BasicBlock.h"
25#include "llvm/IR/CallSite.h"
26#include "llvm/IR/Dominators.h"
27#include "llvm/IR/Function.h"
28#include "llvm/IR/IRBuilder.h"
29#include "llvm/IR/InstIterator.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/IntrinsicInst.h"
33#include "llvm/IR/Module.h"
Sanjoy Das353a19e2015-06-02 22:33:37 +000034#include "llvm/IR/MDBuilder.h"
Philip Reamesd16a9b12015-02-20 01:06:44 +000035#include "llvm/IR/Statepoint.h"
36#include "llvm/IR/Value.h"
37#include "llvm/IR/Verifier.h"
38#include "llvm/Support/Debug.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Transforms/Scalar.h"
41#include "llvm/Transforms/Utils/BasicBlockUtils.h"
42#include "llvm/Transforms/Utils/Cloning.h"
43#include "llvm/Transforms/Utils/Local.h"
44#include "llvm/Transforms/Utils/PromoteMemToReg.h"
45
46#define DEBUG_TYPE "rewrite-statepoints-for-gc"
47
48using namespace llvm;
49
Philip Reamesd16a9b12015-02-20 01:06:44 +000050// Print the liveset found at the insert location
51static cl::opt<bool> PrintLiveSet("spp-print-liveset", cl::Hidden,
52 cl::init(false));
Philip Reames704e78b2015-04-10 22:34:56 +000053static cl::opt<bool> PrintLiveSetSize("spp-print-liveset-size", cl::Hidden,
54 cl::init(false));
Philip Reamesd16a9b12015-02-20 01:06:44 +000055// Print out the base pointers for debugging
Philip Reames704e78b2015-04-10 22:34:56 +000056static cl::opt<bool> PrintBasePointers("spp-print-base-pointers", cl::Hidden,
57 cl::init(false));
Philip Reamesd16a9b12015-02-20 01:06:44 +000058
Igor Laevskye0317182015-05-19 15:59:05 +000059// Cost threshold measuring when it is profitable to rematerialize value instead
60// of relocating it
61static cl::opt<unsigned>
62RematerializationThreshold("spp-rematerialization-threshold", cl::Hidden,
63 cl::init(6));
64
Philip Reamese73300b2015-04-13 16:41:32 +000065#ifdef XDEBUG
66static bool ClobberNonLive = true;
67#else
68static bool ClobberNonLive = false;
69#endif
70static cl::opt<bool, true> ClobberNonLiveOverride("rs4gc-clobber-non-live",
71 cl::location(ClobberNonLive),
72 cl::Hidden);
73
Benjamin Kramer6f665452015-02-20 14:00:58 +000074namespace {
Sanjoy Dasea45f0e2015-06-02 22:33:34 +000075struct RewriteStatepointsForGC : public ModulePass {
Philip Reamesd16a9b12015-02-20 01:06:44 +000076 static char ID; // Pass identification, replacement for typeid
77
Sanjoy Dasea45f0e2015-06-02 22:33:34 +000078 RewriteStatepointsForGC() : ModulePass(ID) {
Philip Reamesd16a9b12015-02-20 01:06:44 +000079 initializeRewriteStatepointsForGCPass(*PassRegistry::getPassRegistry());
80 }
Sanjoy Dasea45f0e2015-06-02 22:33:34 +000081 bool runOnFunction(Function &F);
82 bool runOnModule(Module &M) override {
83 bool Changed = false;
84 for (Function &F : M)
85 Changed |= runOnFunction(F);
Sanjoy Das353a19e2015-06-02 22:33:37 +000086
87 if (Changed) {
88 // stripDereferenceabilityInfo asserts that shouldRewriteStatepointsIn
89 // returns true for at least one function in the module. Since at least
90 // one function changed, we know that the precondition is satisfied.
91 stripDereferenceabilityInfo(M);
92 }
93
Sanjoy Dasea45f0e2015-06-02 22:33:34 +000094 return Changed;
95 }
Philip Reamesd16a9b12015-02-20 01:06:44 +000096
97 void getAnalysisUsage(AnalysisUsage &AU) const override {
98 // We add and rewrite a bunch of instructions, but don't really do much
99 // else. We could in theory preserve a lot more analyses here.
100 AU.addRequired<DominatorTreeWrapperPass>();
Igor Laevskye0317182015-05-19 15:59:05 +0000101 AU.addRequired<TargetTransformInfoWrapperPass>();
Philip Reamesd16a9b12015-02-20 01:06:44 +0000102 }
Sanjoy Das353a19e2015-06-02 22:33:37 +0000103
104 /// The IR fed into RewriteStatepointsForGC may have had attributes implying
105 /// dereferenceability that are no longer valid/correct after
106 /// RewriteStatepointsForGC has run. This is because semantically, after
107 /// RewriteStatepointsForGC runs, all calls to gc.statepoint "free" the entire
108 /// heap. stripDereferenceabilityInfo (conservatively) restores correctness
109 /// by erasing all attributes in the module that externally imply
110 /// dereferenceability.
111 ///
112 void stripDereferenceabilityInfo(Module &M);
113
114 // Helpers for stripDereferenceabilityInfo
115 void stripDereferenceabilityInfoFromBody(Function &F);
116 void stripDereferenceabilityInfoFromPrototype(Function &F);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000117};
Benjamin Kramer6f665452015-02-20 14:00:58 +0000118} // namespace
Philip Reamesd16a9b12015-02-20 01:06:44 +0000119
120char RewriteStatepointsForGC::ID = 0;
121
Sanjoy Dasea45f0e2015-06-02 22:33:34 +0000122ModulePass *llvm::createRewriteStatepointsForGCPass() {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000123 return new RewriteStatepointsForGC();
124}
125
126INITIALIZE_PASS_BEGIN(RewriteStatepointsForGC, "rewrite-statepoints-for-gc",
127 "Make relocations explicit at statepoints", false, false)
128INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
129INITIALIZE_PASS_END(RewriteStatepointsForGC, "rewrite-statepoints-for-gc",
130 "Make relocations explicit at statepoints", false, false)
131
132namespace {
Philip Reamesdf1ef082015-04-10 22:53:14 +0000133struct GCPtrLivenessData {
134 /// Values defined in this block.
135 DenseMap<BasicBlock *, DenseSet<Value *>> KillSet;
136 /// Values used in this block (and thus live); does not included values
137 /// killed within this block.
138 DenseMap<BasicBlock *, DenseSet<Value *>> LiveSet;
139
140 /// Values live into this basic block (i.e. used by any
141 /// instruction in this basic block or ones reachable from here)
142 DenseMap<BasicBlock *, DenseSet<Value *>> LiveIn;
143
144 /// Values live out of this basic block (i.e. live into
145 /// any successor block)
146 DenseMap<BasicBlock *, DenseSet<Value *>> LiveOut;
147};
148
Philip Reamesd16a9b12015-02-20 01:06:44 +0000149// The type of the internal cache used inside the findBasePointers family
150// of functions. From the callers perspective, this is an opaque type and
151// should not be inspected.
152//
153// In the actual implementation this caches two relations:
154// - The base relation itself (i.e. this pointer is based on that one)
155// - The base defining value relation (i.e. before base_phi insertion)
156// Generally, after the execution of a full findBasePointer call, only the
157// base relation will remain. Internally, we add a mixture of the two
158// types, then update all the second type to the first type
Philip Reamese9c3b9b2015-02-20 22:48:20 +0000159typedef DenseMap<Value *, Value *> DefiningValueMapTy;
Philip Reames1f017542015-02-20 23:16:52 +0000160typedef DenseSet<llvm::Value *> StatepointLiveSetTy;
Igor Laevskye0317182015-05-19 15:59:05 +0000161typedef DenseMap<Instruction *, Value *> RematerializedValueMapTy;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000162
Philip Reamesd16a9b12015-02-20 01:06:44 +0000163struct PartiallyConstructedSafepointRecord {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000164 /// The set of values known to be live across this safepoint
Philip Reames860660e2015-02-20 22:05:18 +0000165 StatepointLiveSetTy liveset;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000166
167 /// Mapping from live pointers to a base-defining-value
Philip Reamesf2041322015-02-20 19:26:04 +0000168 DenseMap<llvm::Value *, llvm::Value *> PointerToBase;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000169
Philip Reames0a3240f2015-02-20 21:34:11 +0000170 /// The *new* gc.statepoint instruction itself. This produces the token
171 /// that normal path gc.relocates and the gc.result are tied to.
172 Instruction *StatepointToken;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000173
Philip Reamesf2041322015-02-20 19:26:04 +0000174 /// Instruction to which exceptional gc relocates are attached
175 /// Makes it easier to iterate through them during relocationViaAlloca.
176 Instruction *UnwindToken;
Igor Laevskye0317182015-05-19 15:59:05 +0000177
178 /// Record live values we are rematerialized instead of relocating.
179 /// They are not included into 'liveset' field.
180 /// Maps rematerialized copy to it's original value.
181 RematerializedValueMapTy RematerializedValues;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000182};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000183}
Philip Reamesd16a9b12015-02-20 01:06:44 +0000184
Philip Reamesdf1ef082015-04-10 22:53:14 +0000185/// Compute the live-in set for every basic block in the function
186static void computeLiveInValues(DominatorTree &DT, Function &F,
187 GCPtrLivenessData &Data);
188
189/// Given results from the dataflow liveness computation, find the set of live
190/// Values at a particular instruction.
191static void findLiveSetAtInst(Instruction *inst, GCPtrLivenessData &Data,
192 StatepointLiveSetTy &out);
193
Philip Reamesd16a9b12015-02-20 01:06:44 +0000194// TODO: Once we can get to the GCStrategy, this becomes
195// Optional<bool> isGCManagedPointer(const Value *V) const override {
196
Craig Toppere3dcce92015-08-01 22:20:21 +0000197static bool isGCPointerType(Type *T) {
198 if (auto *PT = dyn_cast<PointerType>(T))
Philip Reamesd16a9b12015-02-20 01:06:44 +0000199 // For the sake of this example GC, we arbitrarily pick addrspace(1) as our
200 // GC managed heap. We know that a pointer into this heap needs to be
201 // updated and that no other pointer does.
202 return (1 == PT->getAddressSpace());
203 return false;
204}
205
Philip Reames8531d8c2015-04-10 21:48:25 +0000206// Return true if this type is one which a) is a gc pointer or contains a GC
207// pointer and b) is of a type this code expects to encounter as a live value.
208// (The insertion code will assert that a type which matches (a) and not (b)
Philip Reames704e78b2015-04-10 22:34:56 +0000209// is not encountered.)
Philip Reames8531d8c2015-04-10 21:48:25 +0000210static bool isHandledGCPointerType(Type *T) {
211 // We fully support gc pointers
212 if (isGCPointerType(T))
213 return true;
214 // We partially support vectors of gc pointers. The code will assert if it
215 // can't handle something.
216 if (auto VT = dyn_cast<VectorType>(T))
217 if (isGCPointerType(VT->getElementType()))
218 return true;
219 return false;
220}
221
222#ifndef NDEBUG
223/// Returns true if this type contains a gc pointer whether we know how to
224/// handle that type or not.
225static bool containsGCPtrType(Type *Ty) {
Philip Reames704e78b2015-04-10 22:34:56 +0000226 if (isGCPointerType(Ty))
Philip Reames8531d8c2015-04-10 21:48:25 +0000227 return true;
228 if (VectorType *VT = dyn_cast<VectorType>(Ty))
229 return isGCPointerType(VT->getScalarType());
230 if (ArrayType *AT = dyn_cast<ArrayType>(Ty))
231 return containsGCPtrType(AT->getElementType());
232 if (StructType *ST = dyn_cast<StructType>(Ty))
Philip Reames704e78b2015-04-10 22:34:56 +0000233 return std::any_of(
234 ST->subtypes().begin(), ST->subtypes().end(),
235 [](Type *SubType) { return containsGCPtrType(SubType); });
Philip Reames8531d8c2015-04-10 21:48:25 +0000236 return false;
237}
238
239// Returns true if this is a type which a) is a gc pointer or contains a GC
240// pointer and b) is of a type which the code doesn't expect (i.e. first class
241// aggregates). Used to trip assertions.
242static bool isUnhandledGCPointerType(Type *Ty) {
243 return containsGCPtrType(Ty) && !isHandledGCPointerType(Ty);
244}
245#endif
246
Philip Reamesd16a9b12015-02-20 01:06:44 +0000247static bool order_by_name(llvm::Value *a, llvm::Value *b) {
248 if (a->hasName() && b->hasName()) {
249 return -1 == a->getName().compare(b->getName());
250 } else if (a->hasName() && !b->hasName()) {
251 return true;
252 } else if (!a->hasName() && b->hasName()) {
253 return false;
254 } else {
255 // Better than nothing, but not stable
256 return a < b;
257 }
258}
259
Philip Reamesdf1ef082015-04-10 22:53:14 +0000260// Conservatively identifies any definitions which might be live at the
261// given instruction. The analysis is performed immediately before the
262// given instruction. Values defined by that instruction are not considered
263// live. Values used by that instruction are considered live.
264static void analyzeParsePointLiveness(
265 DominatorTree &DT, GCPtrLivenessData &OriginalLivenessData,
266 const CallSite &CS, PartiallyConstructedSafepointRecord &result) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000267 Instruction *inst = CS.getInstruction();
268
Philip Reames1f017542015-02-20 23:16:52 +0000269 StatepointLiveSetTy liveset;
Philip Reamesdf1ef082015-04-10 22:53:14 +0000270 findLiveSetAtInst(inst, OriginalLivenessData, liveset);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000271
272 if (PrintLiveSet) {
273 // Note: This output is used by several of the test cases
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000274 // The order of elements in a set is not stable, put them in a vec and sort
Philip Reamesd16a9b12015-02-20 01:06:44 +0000275 // by name
Philip Reamesdab35f32015-09-02 21:11:44 +0000276 SmallVector<Value *, 64> Temp;
277 Temp.insert(Temp.end(), liveset.begin(), liveset.end());
278 std::sort(Temp.begin(), Temp.end(), order_by_name);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000279 errs() << "Live Variables:\n";
Philip Reamesdab35f32015-09-02 21:11:44 +0000280 for (Value *V : Temp)
281 dbgs() << " " << V->getName() << " " << *V << "\n";
Philip Reamesd16a9b12015-02-20 01:06:44 +0000282 }
283 if (PrintLiveSetSize) {
284 errs() << "Safepoint For: " << CS.getCalledValue()->getName() << "\n";
285 errs() << "Number live values: " << liveset.size() << "\n";
286 }
287 result.liveset = liveset;
288}
289
Philip Reames311f7102015-05-12 22:19:52 +0000290static Value *findBaseDefiningValue(Value *I);
291
Philip Reames8fe7f132015-06-26 22:47:37 +0000292/// Return a base defining value for the 'Index' element of the given vector
293/// instruction 'I'. If Index is null, returns a BDV for the entire vector
294/// 'I'. As an optimization, this method will try to determine when the
295/// element is known to already be a base pointer. If this can be established,
296/// the second value in the returned pair will be true. Note that either a
297/// vector or a pointer typed value can be returned. For the former, the
298/// vector returned is a BDV (and possibly a base) of the entire vector 'I'.
299/// If the later, the return pointer is a BDV (or possibly a base) for the
300/// particular element in 'I'.
301static std::pair<Value *, bool>
302findBaseDefiningValueOfVector(Value *I, Value *Index = nullptr) {
Philip Reames8531d8c2015-04-10 21:48:25 +0000303 assert(I->getType()->isVectorTy() &&
304 cast<VectorType>(I->getType())->getElementType()->isPointerTy() &&
305 "Illegal to ask for the base pointer of a non-pointer type");
306
307 // Each case parallels findBaseDefiningValue below, see that code for
308 // detailed motivation.
309
310 if (isa<Argument>(I))
311 // An incoming argument to the function is a base pointer
Philip Reames8fe7f132015-06-26 22:47:37 +0000312 return std::make_pair(I, true);
Philip Reames8531d8c2015-04-10 21:48:25 +0000313
314 // We shouldn't see the address of a global as a vector value?
315 assert(!isa<GlobalVariable>(I) &&
316 "unexpected global variable found in base of vector");
317
318 // inlining could possibly introduce phi node that contains
319 // undef if callee has multiple returns
320 if (isa<UndefValue>(I))
321 // utterly meaningless, but useful for dealing with partially optimized
322 // code.
Philip Reames8fe7f132015-06-26 22:47:37 +0000323 return std::make_pair(I, true);
Philip Reames8531d8c2015-04-10 21:48:25 +0000324
325 // Due to inheritance, this must be _after_ the global variable and undef
326 // checks
327 if (Constant *Con = dyn_cast<Constant>(I)) {
328 assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) &&
329 "order of checks wrong!");
330 assert(Con->isNullValue() && "null is the only case which makes sense");
Philip Reames8fe7f132015-06-26 22:47:37 +0000331 return std::make_pair(Con, true);
Philip Reames8531d8c2015-04-10 21:48:25 +0000332 }
Philip Reames8fe7f132015-06-26 22:47:37 +0000333
Philip Reames8531d8c2015-04-10 21:48:25 +0000334 if (isa<LoadInst>(I))
Philip Reames8fe7f132015-06-26 22:47:37 +0000335 return std::make_pair(I, true);
336
Philip Reames311f7102015-05-12 22:19:52 +0000337 // For an insert element, we might be able to look through it if we know
Philip Reames8fe7f132015-06-26 22:47:37 +0000338 // something about the indexes.
Philip Reames311f7102015-05-12 22:19:52 +0000339 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(I)) {
Philip Reames8fe7f132015-06-26 22:47:37 +0000340 if (Index) {
341 Value *InsertIndex = IEI->getOperand(2);
342 // This index is inserting the value, look for its BDV
343 if (InsertIndex == Index)
344 return std::make_pair(findBaseDefiningValue(IEI->getOperand(1)), false);
345 // Both constant, and can't be equal per above. This insert is definitely
346 // not relevant, look back at the rest of the vector and keep trying.
347 if (isa<ConstantInt>(Index) && isa<ConstantInt>(InsertIndex))
348 return findBaseDefiningValueOfVector(IEI->getOperand(0), Index);
349 }
350
351 // We don't know whether this vector contains entirely base pointers or
352 // not. To be conservatively correct, we treat it as a BDV and will
353 // duplicate code as needed to construct a parallel vector of bases.
354 return std::make_pair(IEI, false);
Philip Reames311f7102015-05-12 22:19:52 +0000355 }
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +0000356
Philip Reames8fe7f132015-06-26 22:47:37 +0000357 if (isa<ShuffleVectorInst>(I))
358 // We don't know whether this vector contains entirely base pointers or
359 // not. To be conservatively correct, we treat it as a BDV and will
360 // duplicate code as needed to construct a parallel vector of bases.
361 // TODO: There a number of local optimizations which could be applied here
362 // for particular sufflevector patterns.
363 return std::make_pair(I, false);
364
365 // A PHI or Select is a base defining value. The outer findBasePointer
366 // algorithm is responsible for constructing a base value for this BDV.
367 assert((isa<SelectInst>(I) || isa<PHINode>(I)) &&
368 "unknown vector instruction - no base found for vector element");
369 return std::make_pair(I, false);
Philip Reames8531d8c2015-04-10 21:48:25 +0000370}
371
Philip Reames8fe7f132015-06-26 22:47:37 +0000372static bool isKnownBaseResult(Value *V);
373
Philip Reamesd16a9b12015-02-20 01:06:44 +0000374/// Helper function for findBasePointer - Will return a value which either a)
Philip Reames9ac4e382015-08-12 21:00:20 +0000375/// defines the base pointer for the input, b) blocks the simple search
376/// (i.e. a PHI or Select of two derived pointers), or c) involves a change
377/// from pointer to vector type or back.
Philip Reamesd16a9b12015-02-20 01:06:44 +0000378static Value *findBaseDefiningValue(Value *I) {
Philip Reames8fe7f132015-06-26 22:47:37 +0000379 if (I->getType()->isVectorTy())
380 return findBaseDefiningValueOfVector(I).first;
381
Philip Reamesd16a9b12015-02-20 01:06:44 +0000382 assert(I->getType()->isPointerTy() &&
383 "Illegal to ask for the base pointer of a non-pointer type");
384
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000385 if (isa<Argument>(I))
Philip Reamesd16a9b12015-02-20 01:06:44 +0000386 // An incoming argument to the function is a base pointer
387 // We should have never reached here if this argument isn't an gc value
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000388 return I;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000389
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000390 if (isa<GlobalVariable>(I))
Philip Reamesd16a9b12015-02-20 01:06:44 +0000391 // base case
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000392 return I;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000393
394 // inlining could possibly introduce phi node that contains
395 // undef if callee has multiple returns
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000396 if (isa<UndefValue>(I))
397 // utterly meaningless, but useful for dealing with
398 // partially optimized code.
Philip Reames704e78b2015-04-10 22:34:56 +0000399 return I;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000400
401 // Due to inheritance, this must be _after_ the global variable and undef
402 // checks
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000403 if (Constant *Con = dyn_cast<Constant>(I)) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000404 assert(!isa<GlobalVariable>(I) && !isa<UndefValue>(I) &&
405 "order of checks wrong!");
406 // Note: Finding a constant base for something marked for relocation
407 // doesn't really make sense. The most likely case is either a) some
408 // screwed up the address space usage or b) your validating against
409 // compiled C++ code w/o the proper separation. The only real exception
410 // is a null pointer. You could have generic code written to index of
411 // off a potentially null value and have proven it null. We also use
412 // null pointers in dead paths of relocation phis (which we might later
413 // want to find a base pointer for).
Philip Reames24c6cd52015-03-27 05:47:00 +0000414 assert(isa<ConstantPointerNull>(Con) &&
415 "null is the only case which makes sense");
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000416 return Con;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000417 }
418
419 if (CastInst *CI = dyn_cast<CastInst>(I)) {
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000420 Value *Def = CI->stripPointerCasts();
David Blaikie82ad7872015-02-20 23:44:24 +0000421 // If we find a cast instruction here, it means we've found a cast which is
422 // not simply a pointer cast (i.e. an inttoptr). We don't know how to
423 // handle int->ptr conversion.
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000424 assert(!isa<CastInst>(Def) && "shouldn't find another cast here");
425 return findBaseDefiningValue(Def);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000426 }
427
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000428 if (isa<LoadInst>(I))
429 return I; // The value loaded is an gc base itself
Philip Reamesd16a9b12015-02-20 01:06:44 +0000430
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000431 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
432 // The base of this GEP is the base
433 return findBaseDefiningValue(GEP->getPointerOperand());
Philip Reamesd16a9b12015-02-20 01:06:44 +0000434
435 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
436 switch (II->getIntrinsicID()) {
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000437 case Intrinsic::experimental_gc_result_ptr:
Philip Reamesd16a9b12015-02-20 01:06:44 +0000438 default:
439 // fall through to general call handling
440 break;
441 case Intrinsic::experimental_gc_statepoint:
442 case Intrinsic::experimental_gc_result_float:
443 case Intrinsic::experimental_gc_result_int:
444 llvm_unreachable("these don't produce pointers");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000445 case Intrinsic::experimental_gc_relocate: {
446 // Rerunning safepoint insertion after safepoints are already
447 // inserted is not supported. It could probably be made to work,
448 // but why are you doing this? There's no good reason.
449 llvm_unreachable("repeat safepoint insertion is not supported");
450 }
451 case Intrinsic::gcroot:
452 // Currently, this mechanism hasn't been extended to work with gcroot.
453 // There's no reason it couldn't be, but I haven't thought about the
454 // implications much.
455 llvm_unreachable(
456 "interaction with the gcroot mechanism is not supported");
457 }
458 }
459 // We assume that functions in the source language only return base
460 // pointers. This should probably be generalized via attributes to support
461 // both source language and internal functions.
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000462 if (isa<CallInst>(I) || isa<InvokeInst>(I))
463 return I;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000464
465 // I have absolutely no idea how to implement this part yet. It's not
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000466 // necessarily hard, I just haven't really looked at it yet.
Philip Reamesd16a9b12015-02-20 01:06:44 +0000467 assert(!isa<LandingPadInst>(I) && "Landing Pad is unimplemented");
468
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000469 if (isa<AtomicCmpXchgInst>(I))
Philip Reamesd16a9b12015-02-20 01:06:44 +0000470 // A CAS is effectively a atomic store and load combined under a
471 // predicate. From the perspective of base pointers, we just treat it
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000472 // like a load.
473 return I;
Philip Reames704e78b2015-04-10 22:34:56 +0000474
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000475 assert(!isa<AtomicRMWInst>(I) && "Xchg handled above, all others are "
Philip Reames704e78b2015-04-10 22:34:56 +0000476 "binary ops which don't apply to pointers");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000477
478 // The aggregate ops. Aggregates can either be in the heap or on the
479 // stack, but in either case, this is simply a field load. As a result,
480 // this is a defining definition of the base just like a load is.
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000481 if (isa<ExtractValueInst>(I))
482 return I;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000483
484 // We should never see an insert vector since that would require we be
485 // tracing back a struct value not a pointer value.
486 assert(!isa<InsertValueInst>(I) &&
487 "Base pointer for a struct is meaningless");
488
Philip Reames9ac4e382015-08-12 21:00:20 +0000489 // An extractelement produces a base result exactly when it's input does.
490 // We may need to insert a parallel instruction to extract the appropriate
491 // element out of the base vector corresponding to the input. Given this,
492 // it's analogous to the phi and select case even though it's not a merge.
493 if (auto *EEI = dyn_cast<ExtractElementInst>(I)) {
494 Value *VectorOperand = EEI->getVectorOperand();
495 Value *Index = EEI->getIndexOperand();
496 std::pair<Value *, bool> pair =
497 findBaseDefiningValueOfVector(VectorOperand, Index);
498 Value *VectorBase = pair.first;
499 if (VectorBase->getType()->isPointerTy())
500 // We found a BDV for this specific element with the vector. This is an
501 // optimization, but in practice it covers most of the useful cases
502 // created via scalarization. Note: The peephole optimization here is
503 // currently needed for correctness since the general algorithm doesn't
504 // yet handle insertelements. That will change shortly.
505 return VectorBase;
506 else {
507 assert(VectorBase->getType()->isVectorTy());
508 // Otherwise, we have an instruction which potentially produces a
509 // derived pointer and we need findBasePointers to clone code for us
510 // such that we can create an instruction which produces the
511 // accompanying base pointer.
512 return EEI;
513 }
514 }
515
Philip Reamesd16a9b12015-02-20 01:06:44 +0000516 // The last two cases here don't return a base pointer. Instead, they
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000517 // return a value which dynamically selects from among several base
Philip Reamesd16a9b12015-02-20 01:06:44 +0000518 // derived pointers (each with it's own base potentially). It's the job of
519 // the caller to resolve these.
Philip Reames704e78b2015-04-10 22:34:56 +0000520 assert((isa<SelectInst>(I) || isa<PHINode>(I)) &&
Philip Reamesaa66dfa2015-03-27 05:34:44 +0000521 "missing instruction case in findBaseDefiningValing");
522 return I;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000523}
524
525/// Returns the base defining value for this value.
Philip Reames18d0feb2015-03-27 05:39:32 +0000526static Value *findBaseDefiningValueCached(Value *I, DefiningValueMapTy &Cache) {
527 Value *&Cached = Cache[I];
Benjamin Kramer6f665452015-02-20 14:00:58 +0000528 if (!Cached) {
529 Cached = findBaseDefiningValue(I);
Philip Reames2a892a62015-07-23 22:25:26 +0000530 DEBUG(dbgs() << "fBDV-cached: " << I->getName() << " -> "
531 << Cached->getName() << "\n");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000532 }
Philip Reames18d0feb2015-03-27 05:39:32 +0000533 assert(Cache[I] != nullptr);
Benjamin Kramer6f665452015-02-20 14:00:58 +0000534 return Cached;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000535}
536
537/// Return a base pointer for this value if known. Otherwise, return it's
538/// base defining value.
Philip Reames18d0feb2015-03-27 05:39:32 +0000539static Value *findBaseOrBDV(Value *I, DefiningValueMapTy &Cache) {
540 Value *Def = findBaseDefiningValueCached(I, Cache);
541 auto Found = Cache.find(Def);
542 if (Found != Cache.end()) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000543 // Either a base-of relation, or a self reference. Caller must check.
Benjamin Kramer6f665452015-02-20 14:00:58 +0000544 return Found->second;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000545 }
546 // Only a BDV available
Philip Reames18d0feb2015-03-27 05:39:32 +0000547 return Def;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000548}
549
550/// Given the result of a call to findBaseDefiningValue, or findBaseOrBDV,
551/// is it known to be a base pointer? Or do we need to continue searching.
Philip Reames18d0feb2015-03-27 05:39:32 +0000552static bool isKnownBaseResult(Value *V) {
Philip Reames9ac4e382015-08-12 21:00:20 +0000553 if (!isa<PHINode>(V) && !isa<SelectInst>(V) && !isa<ExtractElementInst>(V)) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000554 // no recursion possible
555 return true;
556 }
Philip Reames18d0feb2015-03-27 05:39:32 +0000557 if (isa<Instruction>(V) &&
558 cast<Instruction>(V)->getMetadata("is_base_value")) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000559 // This is a previously inserted base phi or select. We know
560 // that this is a base value.
561 return true;
562 }
563
564 // We need to keep searching
565 return false;
566}
567
Philip Reamesd16a9b12015-02-20 01:06:44 +0000568namespace {
Philip Reames9b141ed2015-07-23 22:49:14 +0000569/// Models the state of a single base defining value in the findBasePointer
570/// algorithm for determining where a new instruction is needed to propagate
571/// the base of this BDV.
572class BDVState {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000573public:
574 enum Status { Unknown, Base, Conflict };
575
Philip Reames9b141ed2015-07-23 22:49:14 +0000576 BDVState(Status s, Value *b = nullptr) : status(s), base(b) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000577 assert(status != Base || b);
578 }
Philip Reames9b141ed2015-07-23 22:49:14 +0000579 explicit BDVState(Value *b) : status(Base), base(b) {}
580 BDVState() : status(Unknown), base(nullptr) {}
Philip Reamesd16a9b12015-02-20 01:06:44 +0000581
582 Status getStatus() const { return status; }
583 Value *getBase() const { return base; }
584
585 bool isBase() const { return getStatus() == Base; }
586 bool isUnknown() const { return getStatus() == Unknown; }
587 bool isConflict() const { return getStatus() == Conflict; }
588
Philip Reames9b141ed2015-07-23 22:49:14 +0000589 bool operator==(const BDVState &other) const {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000590 return base == other.base && status == other.status;
591 }
592
Philip Reames9b141ed2015-07-23 22:49:14 +0000593 bool operator!=(const BDVState &other) const { return !(*this == other); }
Philip Reamesd16a9b12015-02-20 01:06:44 +0000594
Philip Reames2a892a62015-07-23 22:25:26 +0000595 LLVM_DUMP_METHOD
596 void dump() const { print(dbgs()); dbgs() << '\n'; }
597
598 void print(raw_ostream &OS) const {
Philip Reamesdab35f32015-09-02 21:11:44 +0000599 switch (status) {
600 case Unknown:
601 OS << "U";
602 break;
603 case Base:
604 OS << "B";
605 break;
606 case Conflict:
607 OS << "C";
608 break;
609 };
610 OS << " (" << base << " - "
Philip Reames2a892a62015-07-23 22:25:26 +0000611 << (base ? base->getName() : "nullptr") << "): ";
Philip Reamesd16a9b12015-02-20 01:06:44 +0000612 }
613
614private:
615 Status status;
616 Value *base; // non null only if status == base
617};
618
Philip Reames9b141ed2015-07-23 22:49:14 +0000619inline raw_ostream &operator<<(raw_ostream &OS, const BDVState &State) {
Philip Reames2a892a62015-07-23 22:25:26 +0000620 State.print(OS);
621 return OS;
622}
623
624
Philip Reames9b141ed2015-07-23 22:49:14 +0000625typedef DenseMap<Value *, BDVState> ConflictStateMapTy;
626// Values of type BDVState form a lattice, and this is a helper
Philip Reamesd16a9b12015-02-20 01:06:44 +0000627// class that implementes the meet operation. The meat of the meet
Philip Reames9b141ed2015-07-23 22:49:14 +0000628// operation is implemented in MeetBDVStates::pureMeet
629class MeetBDVStates {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000630public:
Philip Reames273e6bb2015-07-23 21:41:27 +0000631 /// Initializes the currentResult to the TOP state so that if can be met with
632 /// any other state to produce that state.
Philip Reames9b141ed2015-07-23 22:49:14 +0000633 MeetBDVStates() {}
Philip Reamesd16a9b12015-02-20 01:06:44 +0000634
Philip Reames9b141ed2015-07-23 22:49:14 +0000635 // Destructively meet the current result with the given BDVState
636 void meetWith(BDVState otherState) {
Philip Reames273e6bb2015-07-23 21:41:27 +0000637 currentResult = meet(otherState, currentResult);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000638 }
639
Philip Reames9b141ed2015-07-23 22:49:14 +0000640 BDVState getResult() const { return currentResult; }
Philip Reamesd16a9b12015-02-20 01:06:44 +0000641
642private:
Philip Reames9b141ed2015-07-23 22:49:14 +0000643 BDVState currentResult;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000644
Philip Reames9b141ed2015-07-23 22:49:14 +0000645 /// Perform a meet operation on two elements of the BDVState lattice.
646 static BDVState meet(BDVState LHS, BDVState RHS) {
Philip Reames273e6bb2015-07-23 21:41:27 +0000647 assert((pureMeet(LHS, RHS) == pureMeet(RHS, LHS)) &&
648 "math is wrong: meet does not commute!");
Philip Reames9b141ed2015-07-23 22:49:14 +0000649 BDVState Result = pureMeet(LHS, RHS);
Philip Reames2a892a62015-07-23 22:25:26 +0000650 DEBUG(dbgs() << "meet of " << LHS << " with " << RHS
651 << " produced " << Result << "\n");
652 return Result;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000653 }
654
Philip Reames9b141ed2015-07-23 22:49:14 +0000655 static BDVState pureMeet(const BDVState &stateA, const BDVState &stateB) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000656 switch (stateA.getStatus()) {
Philip Reames9b141ed2015-07-23 22:49:14 +0000657 case BDVState::Unknown:
Philip Reamesd16a9b12015-02-20 01:06:44 +0000658 return stateB;
659
Philip Reames9b141ed2015-07-23 22:49:14 +0000660 case BDVState::Base:
Philip Reamesd16a9b12015-02-20 01:06:44 +0000661 assert(stateA.getBase() && "can't be null");
David Blaikie82ad7872015-02-20 23:44:24 +0000662 if (stateB.isUnknown())
Philip Reamesd16a9b12015-02-20 01:06:44 +0000663 return stateA;
David Blaikie82ad7872015-02-20 23:44:24 +0000664
665 if (stateB.isBase()) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000666 if (stateA.getBase() == stateB.getBase()) {
667 assert(stateA == stateB && "equality broken!");
668 return stateA;
669 }
Philip Reames9b141ed2015-07-23 22:49:14 +0000670 return BDVState(BDVState::Conflict);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000671 }
David Blaikie82ad7872015-02-20 23:44:24 +0000672 assert(stateB.isConflict() && "only three states!");
Philip Reames9b141ed2015-07-23 22:49:14 +0000673 return BDVState(BDVState::Conflict);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000674
Philip Reames9b141ed2015-07-23 22:49:14 +0000675 case BDVState::Conflict:
Philip Reamesd16a9b12015-02-20 01:06:44 +0000676 return stateA;
677 }
Reid Klecknera070ee52015-02-20 19:46:02 +0000678 llvm_unreachable("only three states!");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000679 }
680};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000681}
Philip Reamesd16a9b12015-02-20 01:06:44 +0000682/// For a given value or instruction, figure out what base ptr it's derived
683/// from. For gc objects, this is simply itself. On success, returns a value
684/// which is the base pointer. (This is reliable and can be used for
685/// relocation.) On failure, returns nullptr.
Philip Reamesba198492015-04-14 00:41:34 +0000686static Value *findBasePointer(Value *I, DefiningValueMapTy &cache) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000687 Value *def = findBaseOrBDV(I, cache);
688
689 if (isKnownBaseResult(def)) {
690 return def;
691 }
692
693 // Here's the rough algorithm:
694 // - For every SSA value, construct a mapping to either an actual base
695 // pointer or a PHI which obscures the base pointer.
696 // - Construct a mapping from PHI to unknown TOP state. Use an
697 // optimistic algorithm to propagate base pointer information. Lattice
698 // looks like:
699 // UNKNOWN
700 // b1 b2 b3 b4
701 // CONFLICT
702 // When algorithm terminates, all PHIs will either have a single concrete
703 // base or be in a conflict state.
704 // - For every conflict, insert a dummy PHI node without arguments. Add
705 // these to the base[Instruction] = BasePtr mapping. For every
706 // non-conflict, add the actual base.
707 // - For every conflict, add arguments for the base[a] of each input
708 // arguments.
709 //
710 // Note: A simpler form of this would be to add the conflict form of all
711 // PHIs without running the optimistic algorithm. This would be
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000712 // analogous to pessimistic data flow and would likely lead to an
Philip Reamesd16a9b12015-02-20 01:06:44 +0000713 // overall worse solution.
714
Philip Reames29e9ae72015-07-24 00:42:55 +0000715#ifndef NDEBUG
Philip Reames88958b22015-07-24 00:02:11 +0000716 auto isExpectedBDVType = [](Value *BDV) {
Philip Reames9ac4e382015-08-12 21:00:20 +0000717 return isa<PHINode>(BDV) || isa<SelectInst>(BDV) || isa<ExtractElementInst>(BDV);
Philip Reames88958b22015-07-24 00:02:11 +0000718 };
Philip Reames29e9ae72015-07-24 00:42:55 +0000719#endif
Philip Reames88958b22015-07-24 00:02:11 +0000720
721 // Once populated, will contain a mapping from each potentially non-base BDV
722 // to a lattice value (described above) which corresponds to that BDV.
Philip Reames860660e2015-02-20 22:05:18 +0000723 ConflictStateMapTy states;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000724 // Recursively fill in all phis & selects reachable from the initial one
725 // for which we don't already know a definite base value for
Philip Reames88958b22015-07-24 00:02:11 +0000726 /* scope */ {
727 DenseSet<Value *> Visited;
728 SmallVector<Value*, 16> Worklist;
729 Worklist.push_back(def);
730 Visited.insert(def);
731 while (!Worklist.empty()) {
732 Value *Current = Worklist.pop_back_val();
733 assert(!isKnownBaseResult(Current) && "why did it get added?");
734
735 auto visitIncomingValue = [&](Value *InVal) {
736 Value *Base = findBaseOrBDV(InVal, cache);
737 if (isKnownBaseResult(Base))
738 // Known bases won't need new instructions introduced and can be
739 // ignored safely
740 return;
741 assert(isExpectedBDVType(Base) && "the only non-base values "
742 "we see should be base defining values");
743 if (Visited.insert(Base).second)
744 Worklist.push_back(Base);
745 };
746 if (PHINode *Phi = dyn_cast<PHINode>(Current)) {
747 for (Value *InVal : Phi->incoming_values())
748 visitIncomingValue(InVal);
Philip Reames9ac4e382015-08-12 21:00:20 +0000749 } else if (SelectInst *Sel = dyn_cast<SelectInst>(Current)) {
Philip Reames88958b22015-07-24 00:02:11 +0000750 visitIncomingValue(Sel->getTrueValue());
751 visitIncomingValue(Sel->getFalseValue());
Philip Reames9ac4e382015-08-12 21:00:20 +0000752 } else if (auto *EE = dyn_cast<ExtractElementInst>(Current)) {
753 visitIncomingValue(EE->getVectorOperand());
754 } else {
755 // There are two classes of instructions we know we don't handle.
756 assert(isa<ShuffleVectorInst>(Current) ||
757 isa<InsertElementInst>(Current));
758 llvm_unreachable("unimplemented instruction case");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000759 }
760 }
Philip Reames88958b22015-07-24 00:02:11 +0000761 // The frontier of visited instructions are the ones we might need to
762 // duplicate, so fill in the starting state for the optimistic algorithm
763 // that follows.
764 for (Value *BDV : Visited) {
765 states[BDV] = BDVState();
766 }
Philip Reamesd16a9b12015-02-20 01:06:44 +0000767 }
768
Philip Reamesdab35f32015-09-02 21:11:44 +0000769#ifndef NDEBUG
770 DEBUG(dbgs() << "States after initialization:\n");
771 for (auto Pair : states) {
772 DEBUG(dbgs() << " " << Pair.second << " for " << *Pair.first << "\n");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000773 }
Philip Reamesdab35f32015-09-02 21:11:44 +0000774#endif
Philip Reamesd16a9b12015-02-20 01:06:44 +0000775
776 // TODO: come back and revisit the state transitions around inputs which
777 // have reached conflict state. The current version seems too conservative.
778
Philip Reames273e6bb2015-07-23 21:41:27 +0000779 // Return a phi state for a base defining value. We'll generate a new
780 // base state for known bases and expect to find a cached state otherwise.
781 auto getStateForBDV = [&](Value *baseValue) {
782 if (isKnownBaseResult(baseValue))
Philip Reames9b141ed2015-07-23 22:49:14 +0000783 return BDVState(baseValue);
Philip Reames273e6bb2015-07-23 21:41:27 +0000784 auto I = states.find(baseValue);
785 assert(I != states.end() && "lookup failed!");
786 return I->second;
787 };
788
Philip Reamesd16a9b12015-02-20 01:06:44 +0000789 bool progress = true;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000790 while (progress) {
Yaron Keren42a7adf2015-02-28 13:11:24 +0000791#ifndef NDEBUG
792 size_t oldSize = states.size();
793#endif
Philip Reamesd16a9b12015-02-20 01:06:44 +0000794 progress = false;
Philip Reamesa226e612015-02-28 00:47:50 +0000795 // We're only changing keys in this loop, thus safe to keep iterators
Philip Reamesd16a9b12015-02-20 01:06:44 +0000796 for (auto Pair : states) {
Philip Reamesd16a9b12015-02-20 01:06:44 +0000797 Value *v = Pair.first;
798 assert(!isKnownBaseResult(v) && "why did it get added?");
Philip Reames273e6bb2015-07-23 21:41:27 +0000799
Philip Reames9b141ed2015-07-23 22:49:14 +0000800 // Given an input value for the current instruction, return a BDVState
Philip Reames273e6bb2015-07-23 21:41:27 +0000801 // instance which represents the BDV of that value.
802 auto getStateForInput = [&](Value *V) mutable {
803 Value *BDV = findBaseOrBDV(V, cache);
804 return getStateForBDV(BDV);
805 };
806
Philip Reames9b141ed2015-07-23 22:49:14 +0000807 MeetBDVStates calculateMeet;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000808 if (SelectInst *select = dyn_cast<SelectInst>(v)) {
Philip Reames273e6bb2015-07-23 21:41:27 +0000809 calculateMeet.meetWith(getStateForInput(select->getTrueValue()));
810 calculateMeet.meetWith(getStateForInput(select->getFalseValue()));
Philip Reames9ac4e382015-08-12 21:00:20 +0000811 } else if (PHINode *Phi = dyn_cast<PHINode>(v)) {
812 for (Value *Val : Phi->incoming_values())
Philip Reames273e6bb2015-07-23 21:41:27 +0000813 calculateMeet.meetWith(getStateForInput(Val));
Philip Reames9ac4e382015-08-12 21:00:20 +0000814 } else {
815 // The 'meet' for an extractelement is slightly trivial, but it's still
816 // useful in that it drives us to conflict if our input is.
817 auto *EE = cast<ExtractElementInst>(v);
818 calculateMeet.meetWith(getStateForInput(EE->getVectorOperand()));
819 }
820
Philip Reamesd16a9b12015-02-20 01:06:44 +0000821
Philip Reames9b141ed2015-07-23 22:49:14 +0000822 BDVState oldState = states[v];
823 BDVState newState = calculateMeet.getResult();
Philip Reamesd16a9b12015-02-20 01:06:44 +0000824 if (oldState != newState) {
825 progress = true;
826 states[v] = newState;
827 }
828 }
829
830 assert(oldSize <= states.size());
831 assert(oldSize == states.size() || progress);
832 }
833
Philip Reamesdab35f32015-09-02 21:11:44 +0000834#ifndef NDEBUG
835 DEBUG(dbgs() << "States after meet iteration:\n");
836 for (auto Pair : states) {
837 DEBUG(dbgs() << " " << Pair.second << " for " << *Pair.first << "\n");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000838 }
Philip Reamesdab35f32015-09-02 21:11:44 +0000839#endif
840
Philip Reamesd16a9b12015-02-20 01:06:44 +0000841 // Insert Phis for all conflicts
Philip Reames2e5bcbe2015-02-28 01:52:09 +0000842 // We want to keep naming deterministic in the loop that follows, so
843 // sort the keys before iteration. This is useful in allowing us to
844 // write stable tests. Note that there is no invalidation issue here.
Philip Reames704e78b2015-04-10 22:34:56 +0000845 SmallVector<Value *, 16> Keys;
Philip Reames2e5bcbe2015-02-28 01:52:09 +0000846 Keys.reserve(states.size());
Philip Reamesd16a9b12015-02-20 01:06:44 +0000847 for (auto Pair : states) {
Philip Reames2e5bcbe2015-02-28 01:52:09 +0000848 Value *V = Pair.first;
849 Keys.push_back(V);
850 }
851 std::sort(Keys.begin(), Keys.end(), order_by_name);
852 // TODO: adjust naming patterns to avoid this order of iteration dependency
853 for (Value *V : Keys) {
Philip Reames6ff1a1e32015-07-21 19:04:38 +0000854 Instruction *I = cast<Instruction>(V);
Philip Reames9b141ed2015-07-23 22:49:14 +0000855 BDVState State = states[I];
Philip Reames6ff1a1e32015-07-21 19:04:38 +0000856 assert(!isKnownBaseResult(I) && "why did it get added?");
857 assert(!State.isUnknown() && "Optimistic algorithm didn't complete!");
Philip Reames9ac4e382015-08-12 21:00:20 +0000858
859 // extractelement instructions are a bit special in that we may need to
860 // insert an extract even when we know an exact base for the instruction.
861 // The problem is that we need to convert from a vector base to a scalar
862 // base for the particular indice we're interested in.
863 if (State.isBase() && isa<ExtractElementInst>(I) &&
864 isa<VectorType>(State.getBase()->getType())) {
865 auto *EE = cast<ExtractElementInst>(I);
866 // TODO: In many cases, the new instruction is just EE itself. We should
867 // exploit this, but can't do it here since it would break the invariant
868 // about the BDV not being known to be a base.
869 auto *BaseInst = ExtractElementInst::Create(State.getBase(),
870 EE->getIndexOperand(),
871 "base_ee", EE);
872 BaseInst->setMetadata("is_base_value", MDNode::get(I->getContext(), {}));
873 states[I] = BDVState(BDVState::Base, BaseInst);
874 }
875
Philip Reames6ff1a1e32015-07-21 19:04:38 +0000876 if (!State.isConflict())
Philip Reamesf986d682015-02-28 00:54:41 +0000877 continue;
Philip Reames704e78b2015-04-10 22:34:56 +0000878
Philip Reames6ff1a1e32015-07-21 19:04:38 +0000879 /// Create and insert a new instruction which will represent the base of
880 /// the given instruction 'I'.
881 auto MakeBaseInstPlaceholder = [](Instruction *I) -> Instruction* {
882 if (isa<PHINode>(I)) {
883 BasicBlock *BB = I->getParent();
884 int NumPreds = std::distance(pred_begin(BB), pred_end(BB));
885 assert(NumPreds > 0 && "how did we reach here");
Philip Reamesfa2c6302015-07-24 19:01:39 +0000886 std::string Name = I->hasName() ?
887 (I->getName() + ".base").str() : "base_phi";
888 return PHINode::Create(I->getType(), NumPreds, Name, I);
Philip Reames9ac4e382015-08-12 21:00:20 +0000889 } else if (SelectInst *Sel = dyn_cast<SelectInst>(I)) {
890 // The undef will be replaced later
891 UndefValue *Undef = UndefValue::get(Sel->getType());
892 std::string Name = I->hasName() ?
893 (I->getName() + ".base").str() : "base_select";
894 return SelectInst::Create(Sel->getCondition(), Undef,
895 Undef, Name, Sel);
896 } else {
897 auto *EE = cast<ExtractElementInst>(I);
898 UndefValue *Undef = UndefValue::get(EE->getVectorOperand()->getType());
899 std::string Name = I->hasName() ?
900 (I->getName() + ".base").str() : "base_ee";
901 return ExtractElementInst::Create(Undef, EE->getIndexOperand(), Name,
902 EE);
Philip Reames6ff1a1e32015-07-21 19:04:38 +0000903 }
Philip Reames6ff1a1e32015-07-21 19:04:38 +0000904 };
905 Instruction *BaseInst = MakeBaseInstPlaceholder(I);
906 // Add metadata marking this as a base value
907 BaseInst->setMetadata("is_base_value", MDNode::get(I->getContext(), {}));
Philip Reames9b141ed2015-07-23 22:49:14 +0000908 states[I] = BDVState(BDVState::Conflict, BaseInst);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000909 }
910
911 // Fixup all the inputs of the new PHIs
912 for (auto Pair : states) {
913 Instruction *v = cast<Instruction>(Pair.first);
Philip Reames9b141ed2015-07-23 22:49:14 +0000914 BDVState state = Pair.second;
Philip Reamesd16a9b12015-02-20 01:06:44 +0000915
916 assert(!isKnownBaseResult(v) && "why did it get added?");
917 assert(!state.isUnknown() && "Optimistic algorithm didn't complete!");
Philip Reames28e61ce2015-02-28 01:57:44 +0000918 if (!state.isConflict())
919 continue;
Philip Reames704e78b2015-04-10 22:34:56 +0000920
Philip Reames28e61ce2015-02-28 01:57:44 +0000921 if (PHINode *basephi = dyn_cast<PHINode>(state.getBase())) {
922 PHINode *phi = cast<PHINode>(v);
923 unsigned NumPHIValues = phi->getNumIncomingValues();
924 for (unsigned i = 0; i < NumPHIValues; i++) {
925 Value *InVal = phi->getIncomingValue(i);
926 BasicBlock *InBB = phi->getIncomingBlock(i);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000927
Philip Reames28e61ce2015-02-28 01:57:44 +0000928 // If we've already seen InBB, add the same incoming value
929 // we added for it earlier. The IR verifier requires phi
930 // nodes with multiple entries from the same basic block
931 // to have the same incoming value for each of those
932 // entries. If we don't do this check here and basephi
933 // has a different type than base, we'll end up adding two
934 // bitcasts (and hence two distinct values) as incoming
935 // values for the same basic block.
Philip Reamesd16a9b12015-02-20 01:06:44 +0000936
Philip Reames28e61ce2015-02-28 01:57:44 +0000937 int blockIndex = basephi->getBasicBlockIndex(InBB);
938 if (blockIndex != -1) {
939 Value *oldBase = basephi->getIncomingValue(blockIndex);
940 basephi->addIncoming(oldBase, InBB);
Philip Reamesd16a9b12015-02-20 01:06:44 +0000941#ifndef NDEBUG
Philip Reames28e61ce2015-02-28 01:57:44 +0000942 Value *base = findBaseOrBDV(InVal, cache);
943 if (!isKnownBaseResult(base)) {
944 // Either conflict or base.
945 assert(states.count(base));
946 base = states[base].getBase();
Philip Reames9b141ed2015-07-23 22:49:14 +0000947 assert(base != nullptr && "unknown BDVState!");
Philip Reames28e61ce2015-02-28 01:57:44 +0000948 }
Philip Reamesd16a9b12015-02-20 01:06:44 +0000949
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000950 // In essence this assert states: the only way two
Philip Reames28e61ce2015-02-28 01:57:44 +0000951 // values incoming from the same basic block may be
952 // different is by being different bitcasts of the same
953 // value. A cleanup that remains TODO is changing
954 // findBaseOrBDV to return an llvm::Value of the correct
955 // type (and still remain pure). This will remove the
956 // need to add bitcasts.
957 assert(base->stripPointerCasts() == oldBase->stripPointerCasts() &&
958 "sanity -- findBaseOrBDV should be pure!");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000959#endif
Philip Reames28e61ce2015-02-28 01:57:44 +0000960 continue;
961 }
Philip Reamesd16a9b12015-02-20 01:06:44 +0000962
Philip Reames28e61ce2015-02-28 01:57:44 +0000963 // Find either the defining value for the PHI or the normal base for
964 // a non-phi node
965 Value *base = findBaseOrBDV(InVal, cache);
966 if (!isKnownBaseResult(base)) {
967 // Either conflict or base.
968 assert(states.count(base));
969 base = states[base].getBase();
Philip Reames9b141ed2015-07-23 22:49:14 +0000970 assert(base != nullptr && "unknown BDVState!");
Philip Reamesd16a9b12015-02-20 01:06:44 +0000971 }
Philip Reames28e61ce2015-02-28 01:57:44 +0000972 assert(base && "can't be null");
973 // Must use original input BB since base may not be Instruction
974 // The cast is needed since base traversal may strip away bitcasts
975 if (base->getType() != basephi->getType()) {
976 base = new BitCastInst(base, basephi->getType(), "cast",
977 InBB->getTerminator());
Philip Reamesd16a9b12015-02-20 01:06:44 +0000978 }
Philip Reames28e61ce2015-02-28 01:57:44 +0000979 basephi->addIncoming(base, InBB);
980 }
981 assert(basephi->getNumIncomingValues() == NumPHIValues);
Philip Reames9ac4e382015-08-12 21:00:20 +0000982 } else if (SelectInst *basesel = dyn_cast<SelectInst>(state.getBase())) {
Philip Reames28e61ce2015-02-28 01:57:44 +0000983 SelectInst *sel = cast<SelectInst>(v);
984 // Operand 1 & 2 are true, false path respectively. TODO: refactor to
985 // something more safe and less hacky.
986 for (int i = 1; i <= 2; i++) {
987 Value *InVal = sel->getOperand(i);
988 // Find either the defining value for the PHI or the normal base for
989 // a non-phi node
990 Value *base = findBaseOrBDV(InVal, cache);
991 if (!isKnownBaseResult(base)) {
992 // Either conflict or base.
993 assert(states.count(base));
994 base = states[base].getBase();
Philip Reames9b141ed2015-07-23 22:49:14 +0000995 assert(base != nullptr && "unknown BDVState!");
Philip Reames28e61ce2015-02-28 01:57:44 +0000996 }
997 assert(base && "can't be null");
998 // Must use original input BB since base may not be Instruction
999 // The cast is needed since base traversal may strip away bitcasts
1000 if (base->getType() != basesel->getType()) {
1001 base = new BitCastInst(base, basesel->getType(), "cast", basesel);
Philip Reames28e61ce2015-02-28 01:57:44 +00001002 }
1003 basesel->setOperand(i, base);
1004 }
Philip Reames9ac4e382015-08-12 21:00:20 +00001005 } else {
1006 auto *BaseEE = cast<ExtractElementInst>(state.getBase());
1007 Value *InVal = cast<ExtractElementInst>(v)->getVectorOperand();
1008 Value *Base = findBaseOrBDV(InVal, cache);
1009 if (!isKnownBaseResult(Base)) {
1010 // Either conflict or base.
1011 assert(states.count(Base));
1012 Base = states[Base].getBase();
1013 assert(Base != nullptr && "unknown BDVState!");
1014 }
1015 assert(Base && "can't be null");
1016 BaseEE->setOperand(0, Base);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001017 }
1018 }
1019
Philip Reamesabcdc5e2015-08-27 01:02:28 +00001020 // Now that we're done with the algorithm, see if we can optimize the
1021 // results slightly by reducing the number of new instructions needed.
1022 // Arguably, this should be integrated into the algorithm above, but
1023 // doing as a post process step is easier to reason about for the moment.
1024 DenseMap<Value *, Value *> ReverseMap;
1025 SmallPtrSet<Instruction *, 16> NewInsts;
1026 SmallSetVector<Instruction *, 16> Worklist;
1027 for (auto Item : states) {
1028 Value *V = Item.first;
1029 Value *Base = Item.second.getBase();
1030 assert(V && Base);
1031 assert(!isKnownBaseResult(V) && "why did it get added?");
1032 assert(isKnownBaseResult(Base) &&
1033 "must be something we 'know' is a base pointer");
1034 if (!Item.second.isConflict())
1035 continue;
1036
1037 ReverseMap[Base] = V;
1038 if (auto *BaseI = dyn_cast<Instruction>(Base)) {
1039 NewInsts.insert(BaseI);
1040 Worklist.insert(BaseI);
1041 }
1042 }
1043 auto PushNewUsers = [&](Instruction *I) {
1044 for (User *U : I->users())
1045 if (auto *UI = dyn_cast<Instruction>(U))
1046 if (NewInsts.count(UI))
1047 Worklist.insert(UI);
1048 };
1049 const DataLayout &DL = cast<Instruction>(def)->getModule()->getDataLayout();
1050 while (!Worklist.empty()) {
1051 Instruction *BaseI = Worklist.pop_back_val();
Philip Reamesdab35f32015-09-02 21:11:44 +00001052 assert(NewInsts.count(BaseI));
Philip Reamesabcdc5e2015-08-27 01:02:28 +00001053 Value *Bdv = ReverseMap[BaseI];
1054 if (auto *BdvI = dyn_cast<Instruction>(Bdv))
1055 if (BaseI->isIdenticalTo(BdvI)) {
1056 DEBUG(dbgs() << "Identical Base: " << *BaseI << "\n");
1057 PushNewUsers(BaseI);
1058 BaseI->replaceAllUsesWith(Bdv);
1059 BaseI->eraseFromParent();
1060 states[Bdv] = BDVState(BDVState::Conflict, Bdv);
1061 NewInsts.erase(BaseI);
1062 ReverseMap.erase(BaseI);
1063 continue;
1064 }
1065 if (Value *V = SimplifyInstruction(BaseI, DL)) {
1066 DEBUG(dbgs() << "Base " << *BaseI << " simplified to " << *V << "\n");
1067 PushNewUsers(BaseI);
1068 BaseI->replaceAllUsesWith(V);
1069 BaseI->eraseFromParent();
1070 states[Bdv] = BDVState(BDVState::Conflict, V);
1071 NewInsts.erase(BaseI);
1072 ReverseMap.erase(BaseI);
1073 continue;
1074 }
1075 }
1076
Philip Reamesd16a9b12015-02-20 01:06:44 +00001077 // Cache all of our results so we can cheaply reuse them
1078 // NOTE: This is actually two caches: one of the base defining value
1079 // relation and one of the base pointer relation! FIXME
1080 for (auto item : states) {
1081 Value *v = item.first;
1082 Value *base = item.second.getBase();
1083 assert(v && base);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001084
Philip Reamesdab35f32015-09-02 21:11:44 +00001085 std::string fromstr =
1086 cache.count(v) ? (cache[v]->hasName() ? cache[v]->getName() : "")
1087 : "none";
1088 DEBUG(dbgs() << "Updating base value cache"
1089 << " for: " << (v->hasName() ? v->getName() : "")
1090 << " from: " << fromstr
1091 << " to: " << (base->hasName() ? base->getName() : "") << "\n");
Philip Reamesd16a9b12015-02-20 01:06:44 +00001092
Philip Reamesd16a9b12015-02-20 01:06:44 +00001093 if (cache.count(v)) {
1094 // Once we transition from the BDV relation being store in the cache to
1095 // the base relation being stored, it must be stable
1096 assert((!isKnownBaseResult(cache[v]) || cache[v] == base) &&
1097 "base relation should be stable");
1098 }
1099 cache[v] = base;
1100 }
1101 assert(cache.find(def) != cache.end());
1102 return cache[def];
1103}
1104
1105// For a set of live pointers (base and/or derived), identify the base
1106// pointer of the object which they are derived from. This routine will
1107// mutate the IR graph as needed to make the 'base' pointer live at the
1108// definition site of 'derived'. This ensures that any use of 'derived' can
1109// also use 'base'. This may involve the insertion of a number of
1110// additional PHI nodes.
1111//
1112// preconditions: live is a set of pointer type Values
1113//
1114// side effects: may insert PHI nodes into the existing CFG, will preserve
1115// CFG, will not remove or mutate any existing nodes
1116//
Philip Reamesf2041322015-02-20 19:26:04 +00001117// post condition: PointerToBase contains one (derived, base) pair for every
Philip Reamesd16a9b12015-02-20 01:06:44 +00001118// pointer in live. Note that derived can be equal to base if the original
1119// pointer was a base pointer.
Philip Reames704e78b2015-04-10 22:34:56 +00001120static void
1121findBasePointers(const StatepointLiveSetTy &live,
1122 DenseMap<llvm::Value *, llvm::Value *> &PointerToBase,
Philip Reamesba198492015-04-14 00:41:34 +00001123 DominatorTree *DT, DefiningValueMapTy &DVCache) {
Philip Reames2e5bcbe2015-02-28 01:52:09 +00001124 // For the naming of values inserted to be deterministic - which makes for
1125 // much cleaner and more stable tests - we need to assign an order to the
1126 // live values. DenseSets do not provide a deterministic order across runs.
Philip Reames704e78b2015-04-10 22:34:56 +00001127 SmallVector<Value *, 64> Temp;
Philip Reames2e5bcbe2015-02-28 01:52:09 +00001128 Temp.insert(Temp.end(), live.begin(), live.end());
1129 std::sort(Temp.begin(), Temp.end(), order_by_name);
1130 for (Value *ptr : Temp) {
Philip Reamesba198492015-04-14 00:41:34 +00001131 Value *base = findBasePointer(ptr, DVCache);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001132 assert(base && "failed to find base pointer");
Philip Reamesf2041322015-02-20 19:26:04 +00001133 PointerToBase[ptr] = base;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001134 assert((!isa<Instruction>(base) || !isa<Instruction>(ptr) ||
1135 DT->dominates(cast<Instruction>(base)->getParent(),
1136 cast<Instruction>(ptr)->getParent())) &&
1137 "The base we found better dominate the derived pointer");
1138
David Blaikie82ad7872015-02-20 23:44:24 +00001139 // If you see this trip and like to live really dangerously, the code should
1140 // be correct, just with idioms the verifier can't handle. You can try
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001141 // disabling the verifier at your own substantial risk.
Philip Reames704e78b2015-04-10 22:34:56 +00001142 assert(!isa<ConstantPointerNull>(base) &&
Philip Reames24c6cd52015-03-27 05:47:00 +00001143 "the relocation code needs adjustment to handle the relocation of "
1144 "a null pointer constant without causing false positives in the "
1145 "safepoint ir verifier.");
Philip Reamesd16a9b12015-02-20 01:06:44 +00001146 }
1147}
1148
1149/// Find the required based pointers (and adjust the live set) for the given
1150/// parse point.
1151static void findBasePointers(DominatorTree &DT, DefiningValueMapTy &DVCache,
1152 const CallSite &CS,
1153 PartiallyConstructedSafepointRecord &result) {
Philip Reamesf2041322015-02-20 19:26:04 +00001154 DenseMap<llvm::Value *, llvm::Value *> PointerToBase;
Philip Reamesba198492015-04-14 00:41:34 +00001155 findBasePointers(result.liveset, PointerToBase, &DT, DVCache);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001156
1157 if (PrintBasePointers) {
Philip Reamesa5aeaf42015-02-28 00:20:48 +00001158 // Note: Need to print these in a stable order since this is checked in
1159 // some tests.
Philip Reamesd16a9b12015-02-20 01:06:44 +00001160 errs() << "Base Pairs (w/o Relocation):\n";
Philip Reames704e78b2015-04-10 22:34:56 +00001161 SmallVector<Value *, 64> Temp;
Philip Reamesa5aeaf42015-02-28 00:20:48 +00001162 Temp.reserve(PointerToBase.size());
Philip Reamesf2041322015-02-20 19:26:04 +00001163 for (auto Pair : PointerToBase) {
Philip Reamesa5aeaf42015-02-28 00:20:48 +00001164 Temp.push_back(Pair.first);
1165 }
1166 std::sort(Temp.begin(), Temp.end(), order_by_name);
1167 for (Value *Ptr : Temp) {
1168 Value *Base = PointerToBase[Ptr];
Philip Reames704e78b2015-04-10 22:34:56 +00001169 errs() << " derived %" << Ptr->getName() << " base %" << Base->getName()
1170 << "\n";
Philip Reamesd16a9b12015-02-20 01:06:44 +00001171 }
1172 }
1173
Philip Reamesf2041322015-02-20 19:26:04 +00001174 result.PointerToBase = PointerToBase;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001175}
1176
Philip Reamesdf1ef082015-04-10 22:53:14 +00001177/// Given an updated version of the dataflow liveness results, update the
1178/// liveset and base pointer maps for the call site CS.
1179static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData,
1180 const CallSite &CS,
1181 PartiallyConstructedSafepointRecord &result);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001182
Philip Reamesdf1ef082015-04-10 22:53:14 +00001183static void recomputeLiveInValues(
1184 Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate,
Philip Reamesd2b66462015-02-20 22:39:41 +00001185 MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) {
Philip Reamesdf1ef082015-04-10 22:53:14 +00001186 // TODO-PERF: reuse the original liveness, then simply run the dataflow
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001187 // again. The old values are still live and will help it stabilize quickly.
Philip Reamesdf1ef082015-04-10 22:53:14 +00001188 GCPtrLivenessData RevisedLivenessData;
1189 computeLiveInValues(DT, F, RevisedLivenessData);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001190 for (size_t i = 0; i < records.size(); i++) {
1191 struct PartiallyConstructedSafepointRecord &info = records[i];
Philip Reamesd2b66462015-02-20 22:39:41 +00001192 const CallSite &CS = toUpdate[i];
Philip Reamesdf1ef082015-04-10 22:53:14 +00001193 recomputeLiveInValues(RevisedLivenessData, CS, info);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001194 }
1195}
1196
Philip Reames69e51ca2015-04-13 18:07:21 +00001197// When inserting gc.relocate calls, we need to ensure there are no uses
1198// of the original value between the gc.statepoint and the gc.relocate call.
1199// One case which can arise is a phi node starting one of the successor blocks.
1200// We also need to be able to insert the gc.relocates only on the path which
1201// goes through the statepoint. We might need to split an edge to make this
Philip Reamesf209a152015-04-13 20:00:30 +00001202// possible.
1203static BasicBlock *
Sanjoy Dasea45f0e2015-06-02 22:33:34 +00001204normalizeForInvokeSafepoint(BasicBlock *BB, BasicBlock *InvokeParent,
1205 DominatorTree &DT) {
Philip Reames69e51ca2015-04-13 18:07:21 +00001206 BasicBlock *Ret = BB;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001207 if (!BB->getUniquePredecessor()) {
Chandler Carruth96ada252015-07-22 09:52:54 +00001208 Ret = SplitBlockPredecessors(BB, InvokeParent, "", &DT);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001209 }
1210
Philip Reames69e51ca2015-04-13 18:07:21 +00001211 // Now that 'ret' has unique predecessor we can safely remove all phi nodes
1212 // from it
1213 FoldSingleEntryPHINodes(Ret);
1214 assert(!isa<PHINode>(Ret->begin()));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001215
Philip Reames69e51ca2015-04-13 18:07:21 +00001216 // At this point, we can safely insert a gc.relocate as the first instruction
1217 // in Ret if needed.
1218 return Ret;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001219}
1220
Philip Reamesd2b66462015-02-20 22:39:41 +00001221static int find_index(ArrayRef<Value *> livevec, Value *val) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001222 auto itr = std::find(livevec.begin(), livevec.end(), val);
1223 assert(livevec.end() != itr);
1224 size_t index = std::distance(livevec.begin(), itr);
1225 assert(index < livevec.size());
1226 return index;
1227}
1228
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001229// Create new attribute set containing only attributes which can be transferred
Philip Reamesd16a9b12015-02-20 01:06:44 +00001230// from original call to the safepoint.
1231static AttributeSet legalizeCallAttributes(AttributeSet AS) {
1232 AttributeSet ret;
1233
1234 for (unsigned Slot = 0; Slot < AS.getNumSlots(); Slot++) {
1235 unsigned index = AS.getSlotIndex(Slot);
1236
1237 if (index == AttributeSet::ReturnIndex ||
1238 index == AttributeSet::FunctionIndex) {
1239
1240 for (auto it = AS.begin(Slot), it_end = AS.end(Slot); it != it_end;
1241 ++it) {
1242 Attribute attr = *it;
1243
1244 // Do not allow certain attributes - just skip them
1245 // Safepoint can not be read only or read none.
1246 if (attr.hasAttribute(Attribute::ReadNone) ||
1247 attr.hasAttribute(Attribute::ReadOnly))
1248 continue;
1249
1250 ret = ret.addAttributes(
1251 AS.getContext(), index,
1252 AttributeSet::get(AS.getContext(), index, AttrBuilder(attr)));
1253 }
1254 }
1255
1256 // Just skip parameter attributes for now
1257 }
1258
1259 return ret;
1260}
1261
1262/// Helper function to place all gc relocates necessary for the given
1263/// statepoint.
1264/// Inputs:
1265/// liveVariables - list of variables to be relocated.
1266/// liveStart - index of the first live variable.
1267/// basePtrs - base pointers.
1268/// statepointToken - statepoint instruction to which relocates should be
1269/// bound.
1270/// Builder - Llvm IR builder to be used to construct new calls.
Sanjoy Das5665c992015-05-11 23:47:27 +00001271static void CreateGCRelocates(ArrayRef<llvm::Value *> LiveVariables,
1272 const int LiveStart,
1273 ArrayRef<llvm::Value *> BasePtrs,
1274 Instruction *StatepointToken,
Benjamin Kramerf044d3f2015-03-09 16:23:46 +00001275 IRBuilder<> Builder) {
Philip Reames94babb72015-07-21 17:18:03 +00001276 if (LiveVariables.empty())
1277 return;
1278
1279 // All gc_relocate are set to i8 addrspace(1)* type. We originally generated
1280 // unique declarations for each pointer type, but this proved problematic
1281 // because the intrinsic mangling code is incomplete and fragile. Since
1282 // we're moving towards a single unified pointer type anyways, we can just
1283 // cast everything to an i8* of the right address space. A bitcast is added
1284 // later to convert gc_relocate to the actual value's type.
Philip Reames74ce2e72015-07-21 16:51:17 +00001285 Module *M = StatepointToken->getModule();
Philip Reames94babb72015-07-21 17:18:03 +00001286 auto AS = cast<PointerType>(LiveVariables[0]->getType())->getAddressSpace();
1287 Type *Types[] = {Type::getInt8PtrTy(M->getContext(), AS)};
1288 Value *GCRelocateDecl =
1289 Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001290
Sanjoy Das5665c992015-05-11 23:47:27 +00001291 for (unsigned i = 0; i < LiveVariables.size(); i++) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001292 // Generate the gc.relocate call and save the result
Sanjoy Das5665c992015-05-11 23:47:27 +00001293 Value *BaseIdx =
Philip Reamesf3880502015-07-21 00:49:55 +00001294 Builder.getInt32(LiveStart + find_index(LiveVariables, BasePtrs[i]));
1295 Value *LiveIdx =
1296 Builder.getInt32(LiveStart + find_index(LiveVariables, LiveVariables[i]));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001297
1298 // only specify a debug name if we can give a useful one
Philip Reames74ce2e72015-07-21 16:51:17 +00001299 CallInst *Reloc = Builder.CreateCall(
David Blaikieff6409d2015-05-18 22:13:54 +00001300 GCRelocateDecl, {StatepointToken, BaseIdx, LiveIdx},
Sanjoy Das5665c992015-05-11 23:47:27 +00001301 LiveVariables[i]->hasName() ? LiveVariables[i]->getName() + ".relocated"
Philip Reamesd16a9b12015-02-20 01:06:44 +00001302 : "");
1303 // Trick CodeGen into thinking there are lots of free registers at this
1304 // fake call.
Philip Reames74ce2e72015-07-21 16:51:17 +00001305 Reloc->setCallingConv(CallingConv::Cold);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001306 }
Philip Reamesd16a9b12015-02-20 01:06:44 +00001307}
1308
1309static void
1310makeStatepointExplicitImpl(const CallSite &CS, /* to replace */
1311 const SmallVectorImpl<llvm::Value *> &basePtrs,
1312 const SmallVectorImpl<llvm::Value *> &liveVariables,
1313 Pass *P,
1314 PartiallyConstructedSafepointRecord &result) {
1315 assert(basePtrs.size() == liveVariables.size());
1316 assert(isStatepoint(CS) &&
1317 "This method expects to be rewriting a statepoint");
1318
1319 BasicBlock *BB = CS.getInstruction()->getParent();
1320 assert(BB);
1321 Function *F = BB->getParent();
1322 assert(F && "must be set");
1323 Module *M = F->getParent();
Nick Lewyckyeb3231e2015-02-20 07:14:02 +00001324 (void)M;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001325 assert(M && "must be set");
1326
1327 // We're not changing the function signature of the statepoint since the gc
1328 // arguments go into the var args section.
1329 Function *gc_statepoint_decl = CS.getCalledFunction();
1330
1331 // Then go ahead and use the builder do actually do the inserts. We insert
1332 // immediately before the previous instruction under the assumption that all
1333 // arguments will be available here. We can't insert afterwards since we may
1334 // be replacing a terminator.
1335 Instruction *insertBefore = CS.getInstruction();
1336 IRBuilder<> Builder(insertBefore);
1337 // Copy all of the arguments from the original statepoint - this includes the
1338 // target, call args, and deopt args
Philip Reamesd2b66462015-02-20 22:39:41 +00001339 SmallVector<llvm::Value *, 64> args;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001340 args.insert(args.end(), CS.arg_begin(), CS.arg_end());
1341 // TODO: Clear the 'needs rewrite' flag
1342
1343 // add all the pointers to be relocated (gc arguments)
1344 // Capture the start of the live variable list for use in the gc_relocates
1345 const int live_start = args.size();
1346 args.insert(args.end(), liveVariables.begin(), liveVariables.end());
1347
1348 // Create the statepoint given all the arguments
1349 Instruction *token = nullptr;
1350 AttributeSet return_attributes;
1351 if (CS.isCall()) {
1352 CallInst *toReplace = cast<CallInst>(CS.getInstruction());
1353 CallInst *call =
1354 Builder.CreateCall(gc_statepoint_decl, args, "safepoint_token");
1355 call->setTailCall(toReplace->isTailCall());
1356 call->setCallingConv(toReplace->getCallingConv());
1357
1358 // Currently we will fail on parameter attributes and on certain
1359 // function attributes.
1360 AttributeSet new_attrs = legalizeCallAttributes(toReplace->getAttributes());
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001361 // In case if we can handle this set of attributes - set up function attrs
Philip Reamesd16a9b12015-02-20 01:06:44 +00001362 // directly on statepoint and return attrs later for gc_result intrinsic.
1363 call->setAttributes(new_attrs.getFnAttributes());
1364 return_attributes = new_attrs.getRetAttributes();
1365
1366 token = call;
1367
1368 // Put the following gc_result and gc_relocate calls immediately after the
1369 // the old call (which we're about to delete)
1370 BasicBlock::iterator next(toReplace);
1371 assert(BB->end() != next && "not a terminator, must have next");
1372 next++;
1373 Instruction *IP = &*(next);
1374 Builder.SetInsertPoint(IP);
1375 Builder.SetCurrentDebugLocation(IP->getDebugLoc());
1376
David Blaikie82ad7872015-02-20 23:44:24 +00001377 } else {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001378 InvokeInst *toReplace = cast<InvokeInst>(CS.getInstruction());
1379
1380 // Insert the new invoke into the old block. We'll remove the old one in a
1381 // moment at which point this will become the new terminator for the
1382 // original block.
1383 InvokeInst *invoke = InvokeInst::Create(
1384 gc_statepoint_decl, toReplace->getNormalDest(),
Philip Reamesfa2c6302015-07-24 19:01:39 +00001385 toReplace->getUnwindDest(), args, "statepoint_token", toReplace->getParent());
Philip Reamesd16a9b12015-02-20 01:06:44 +00001386 invoke->setCallingConv(toReplace->getCallingConv());
1387
1388 // Currently we will fail on parameter attributes and on certain
1389 // function attributes.
1390 AttributeSet new_attrs = legalizeCallAttributes(toReplace->getAttributes());
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001391 // In case if we can handle this set of attributes - set up function attrs
Philip Reamesd16a9b12015-02-20 01:06:44 +00001392 // directly on statepoint and return attrs later for gc_result intrinsic.
1393 invoke->setAttributes(new_attrs.getFnAttributes());
1394 return_attributes = new_attrs.getRetAttributes();
1395
1396 token = invoke;
1397
1398 // Generate gc relocates in exceptional path
Philip Reames69e51ca2015-04-13 18:07:21 +00001399 BasicBlock *unwindBlock = toReplace->getUnwindDest();
1400 assert(!isa<PHINode>(unwindBlock->begin()) &&
1401 unwindBlock->getUniquePredecessor() &&
1402 "can't safely insert in this block!");
Philip Reamesd16a9b12015-02-20 01:06:44 +00001403
1404 Instruction *IP = &*(unwindBlock->getFirstInsertionPt());
1405 Builder.SetInsertPoint(IP);
1406 Builder.SetCurrentDebugLocation(toReplace->getDebugLoc());
1407
1408 // Extract second element from landingpad return value. We will attach
1409 // exceptional gc relocates to it.
1410 const unsigned idx = 1;
1411 Instruction *exceptional_token =
1412 cast<Instruction>(Builder.CreateExtractValue(
1413 unwindBlock->getLandingPadInst(), idx, "relocate_token"));
Philip Reamesf2041322015-02-20 19:26:04 +00001414 result.UnwindToken = exceptional_token;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001415
Philip Reames6ff1a1e32015-07-21 19:04:38 +00001416 CreateGCRelocates(liveVariables, live_start, basePtrs,
1417 exceptional_token, Builder);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001418
1419 // Generate gc relocates and returns for normal block
Philip Reames69e51ca2015-04-13 18:07:21 +00001420 BasicBlock *normalDest = toReplace->getNormalDest();
1421 assert(!isa<PHINode>(normalDest->begin()) &&
1422 normalDest->getUniquePredecessor() &&
1423 "can't safely insert in this block!");
Philip Reamesd16a9b12015-02-20 01:06:44 +00001424
1425 IP = &*(normalDest->getFirstInsertionPt());
1426 Builder.SetInsertPoint(IP);
1427
1428 // gc relocates will be generated later as if it were regular call
1429 // statepoint
Philip Reamesd16a9b12015-02-20 01:06:44 +00001430 }
1431 assert(token);
1432
1433 // Take the name of the original value call if it had one.
1434 token->takeName(CS.getInstruction());
1435
Philip Reames704e78b2015-04-10 22:34:56 +00001436// The GCResult is already inserted, we just need to find it
David Blaikie5e5d7842015-02-22 20:58:38 +00001437#ifndef NDEBUG
1438 Instruction *toReplace = CS.getInstruction();
1439 assert((toReplace->hasNUses(0) || toReplace->hasNUses(1)) &&
1440 "only valid use before rewrite is gc.result");
1441 assert(!toReplace->hasOneUse() ||
1442 isGCResult(cast<Instruction>(*toReplace->user_begin())));
1443#endif
Philip Reamesd16a9b12015-02-20 01:06:44 +00001444
1445 // Update the gc.result of the original statepoint (if any) to use the newly
1446 // inserted statepoint. This is safe to do here since the token can't be
1447 // considered a live reference.
1448 CS.getInstruction()->replaceAllUsesWith(token);
1449
Philip Reames0a3240f2015-02-20 21:34:11 +00001450 result.StatepointToken = token;
1451
Philip Reamesd16a9b12015-02-20 01:06:44 +00001452 // Second, create a gc.relocate for every live variable
Philip Reames0a3240f2015-02-20 21:34:11 +00001453 CreateGCRelocates(liveVariables, live_start, basePtrs, token, Builder);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001454}
1455
1456namespace {
1457struct name_ordering {
1458 Value *base;
1459 Value *derived;
1460 bool operator()(name_ordering const &a, name_ordering const &b) {
1461 return -1 == a.derived->getName().compare(b.derived->getName());
1462 }
1463};
1464}
1465static void stablize_order(SmallVectorImpl<Value *> &basevec,
1466 SmallVectorImpl<Value *> &livevec) {
1467 assert(basevec.size() == livevec.size());
1468
Philip Reames860660e2015-02-20 22:05:18 +00001469 SmallVector<name_ordering, 64> temp;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001470 for (size_t i = 0; i < basevec.size(); i++) {
1471 name_ordering v;
1472 v.base = basevec[i];
1473 v.derived = livevec[i];
1474 temp.push_back(v);
1475 }
1476 std::sort(temp.begin(), temp.end(), name_ordering());
1477 for (size_t i = 0; i < basevec.size(); i++) {
1478 basevec[i] = temp[i].base;
1479 livevec[i] = temp[i].derived;
1480 }
1481}
1482
1483// Replace an existing gc.statepoint with a new one and a set of gc.relocates
1484// which make the relocations happening at this safepoint explicit.
Philip Reames704e78b2015-04-10 22:34:56 +00001485//
Philip Reamesd16a9b12015-02-20 01:06:44 +00001486// WARNING: Does not do any fixup to adjust users of the original live
1487// values. That's the callers responsibility.
1488static void
1489makeStatepointExplicit(DominatorTree &DT, const CallSite &CS, Pass *P,
1490 PartiallyConstructedSafepointRecord &result) {
Philip Reamesf2041322015-02-20 19:26:04 +00001491 auto liveset = result.liveset;
1492 auto PointerToBase = result.PointerToBase;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001493
1494 // Convert to vector for efficient cross referencing.
1495 SmallVector<Value *, 64> basevec, livevec;
1496 livevec.reserve(liveset.size());
1497 basevec.reserve(liveset.size());
1498 for (Value *L : liveset) {
1499 livevec.push_back(L);
Philip Reames74ce2e72015-07-21 16:51:17 +00001500 assert(PointerToBase.count(L));
Philip Reamesf2041322015-02-20 19:26:04 +00001501 Value *base = PointerToBase[L];
Philip Reamesd16a9b12015-02-20 01:06:44 +00001502 basevec.push_back(base);
1503 }
1504 assert(livevec.size() == basevec.size());
1505
1506 // To make the output IR slightly more stable (for use in diffs), ensure a
1507 // fixed order of the values in the safepoint (by sorting the value name).
1508 // The order is otherwise meaningless.
1509 stablize_order(basevec, livevec);
1510
1511 // Do the actual rewriting and delete the old statepoint
1512 makeStatepointExplicitImpl(CS, basevec, livevec, P, result);
1513 CS.getInstruction()->eraseFromParent();
1514}
1515
1516// Helper function for the relocationViaAlloca.
1517// It receives iterator to the statepoint gc relocates and emits store to the
1518// assigned
1519// location (via allocaMap) for the each one of them.
1520// Add visited values into the visitedLiveValues set we will later use them
1521// for sanity check.
1522static void
Sanjoy Das5665c992015-05-11 23:47:27 +00001523insertRelocationStores(iterator_range<Value::user_iterator> GCRelocs,
1524 DenseMap<Value *, Value *> &AllocaMap,
1525 DenseSet<Value *> &VisitedLiveValues) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001526
Sanjoy Das5665c992015-05-11 23:47:27 +00001527 for (User *U : GCRelocs) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001528 if (!isa<IntrinsicInst>(U))
1529 continue;
1530
Sanjoy Das5665c992015-05-11 23:47:27 +00001531 IntrinsicInst *RelocatedValue = cast<IntrinsicInst>(U);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001532
1533 // We only care about relocates
Sanjoy Das5665c992015-05-11 23:47:27 +00001534 if (RelocatedValue->getIntrinsicID() !=
Philip Reamesd16a9b12015-02-20 01:06:44 +00001535 Intrinsic::experimental_gc_relocate) {
1536 continue;
1537 }
1538
Sanjoy Das5665c992015-05-11 23:47:27 +00001539 GCRelocateOperands RelocateOperands(RelocatedValue);
1540 Value *OriginalValue =
1541 const_cast<Value *>(RelocateOperands.getDerivedPtr());
1542 assert(AllocaMap.count(OriginalValue));
1543 Value *Alloca = AllocaMap[OriginalValue];
Philip Reamesd16a9b12015-02-20 01:06:44 +00001544
1545 // Emit store into the related alloca
Sanjoy Das89c54912015-05-11 18:49:34 +00001546 // All gc_relocate are i8 addrspace(1)* typed, and it must be bitcasted to
1547 // the correct type according to alloca.
Sanjoy Das5665c992015-05-11 23:47:27 +00001548 assert(RelocatedValue->getNextNode() && "Should always have one since it's not a terminator");
1549 IRBuilder<> Builder(RelocatedValue->getNextNode());
Sanjoy Das89c54912015-05-11 18:49:34 +00001550 Value *CastedRelocatedValue =
Sanjoy Das5665c992015-05-11 23:47:27 +00001551 Builder.CreateBitCast(RelocatedValue, cast<AllocaInst>(Alloca)->getAllocatedType(),
1552 RelocatedValue->hasName() ? RelocatedValue->getName() + ".casted" : "");
Sanjoy Das89c54912015-05-11 18:49:34 +00001553
Sanjoy Das5665c992015-05-11 23:47:27 +00001554 StoreInst *Store = new StoreInst(CastedRelocatedValue, Alloca);
1555 Store->insertAfter(cast<Instruction>(CastedRelocatedValue));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001556
1557#ifndef NDEBUG
Sanjoy Das5665c992015-05-11 23:47:27 +00001558 VisitedLiveValues.insert(OriginalValue);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001559#endif
1560 }
1561}
1562
Igor Laevskye0317182015-05-19 15:59:05 +00001563// Helper function for the "relocationViaAlloca". Similar to the
1564// "insertRelocationStores" but works for rematerialized values.
1565static void
1566insertRematerializationStores(
1567 RematerializedValueMapTy RematerializedValues,
1568 DenseMap<Value *, Value *> &AllocaMap,
1569 DenseSet<Value *> &VisitedLiveValues) {
1570
1571 for (auto RematerializedValuePair: RematerializedValues) {
1572 Instruction *RematerializedValue = RematerializedValuePair.first;
1573 Value *OriginalValue = RematerializedValuePair.second;
1574
1575 assert(AllocaMap.count(OriginalValue) &&
1576 "Can not find alloca for rematerialized value");
1577 Value *Alloca = AllocaMap[OriginalValue];
1578
1579 StoreInst *Store = new StoreInst(RematerializedValue, Alloca);
1580 Store->insertAfter(RematerializedValue);
1581
1582#ifndef NDEBUG
1583 VisitedLiveValues.insert(OriginalValue);
1584#endif
1585 }
1586}
1587
Philip Reamesd16a9b12015-02-20 01:06:44 +00001588/// do all the relocation update via allocas and mem2reg
1589static void relocationViaAlloca(
Igor Laevsky285fe842015-05-19 16:29:43 +00001590 Function &F, DominatorTree &DT, ArrayRef<Value *> Live,
1591 ArrayRef<struct PartiallyConstructedSafepointRecord> Records) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001592#ifndef NDEBUG
Philip Reamesa6ebf072015-03-27 05:53:16 +00001593 // record initial number of (static) allocas; we'll check we have the same
1594 // number when we get done.
1595 int InitialAllocaNum = 0;
Philip Reames704e78b2015-04-10 22:34:56 +00001596 for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E;
1597 I++)
Philip Reamesa6ebf072015-03-27 05:53:16 +00001598 if (isa<AllocaInst>(*I))
1599 InitialAllocaNum++;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001600#endif
1601
1602 // TODO-PERF: change data structures, reserve
Igor Laevsky285fe842015-05-19 16:29:43 +00001603 DenseMap<Value *, Value *> AllocaMap;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001604 SmallVector<AllocaInst *, 200> PromotableAllocas;
Igor Laevskye0317182015-05-19 15:59:05 +00001605 // Used later to chack that we have enough allocas to store all values
1606 std::size_t NumRematerializedValues = 0;
Igor Laevsky285fe842015-05-19 16:29:43 +00001607 PromotableAllocas.reserve(Live.size());
Philip Reamesd16a9b12015-02-20 01:06:44 +00001608
Igor Laevskye0317182015-05-19 15:59:05 +00001609 // Emit alloca for "LiveValue" and record it in "allocaMap" and
1610 // "PromotableAllocas"
1611 auto emitAllocaFor = [&](Value *LiveValue) {
1612 AllocaInst *Alloca = new AllocaInst(LiveValue->getType(), "",
1613 F.getEntryBlock().getFirstNonPHI());
Igor Laevsky285fe842015-05-19 16:29:43 +00001614 AllocaMap[LiveValue] = Alloca;
Igor Laevskye0317182015-05-19 15:59:05 +00001615 PromotableAllocas.push_back(Alloca);
1616 };
1617
Philip Reamesd16a9b12015-02-20 01:06:44 +00001618 // emit alloca for each live gc pointer
Igor Laevsky285fe842015-05-19 16:29:43 +00001619 for (unsigned i = 0; i < Live.size(); i++) {
1620 emitAllocaFor(Live[i]);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001621 }
1622
Igor Laevskye0317182015-05-19 15:59:05 +00001623 // emit allocas for rematerialized values
Igor Laevsky285fe842015-05-19 16:29:43 +00001624 for (size_t i = 0; i < Records.size(); i++) {
1625 const struct PartiallyConstructedSafepointRecord &Info = Records[i];
Igor Laevskye0317182015-05-19 15:59:05 +00001626
Igor Laevsky285fe842015-05-19 16:29:43 +00001627 for (auto RematerializedValuePair : Info.RematerializedValues) {
Igor Laevskye0317182015-05-19 15:59:05 +00001628 Value *OriginalValue = RematerializedValuePair.second;
Igor Laevsky285fe842015-05-19 16:29:43 +00001629 if (AllocaMap.count(OriginalValue) != 0)
Igor Laevskye0317182015-05-19 15:59:05 +00001630 continue;
1631
1632 emitAllocaFor(OriginalValue);
1633 ++NumRematerializedValues;
1634 }
1635 }
Igor Laevsky285fe842015-05-19 16:29:43 +00001636
Philip Reamesd16a9b12015-02-20 01:06:44 +00001637 // The next two loops are part of the same conceptual operation. We need to
1638 // insert a store to the alloca after the original def and at each
1639 // redefinition. We need to insert a load before each use. These are split
1640 // into distinct loops for performance reasons.
1641
1642 // update gc pointer after each statepoint
1643 // either store a relocated value or null (if no relocated value found for
1644 // this gc pointer and it is not a gc_result)
1645 // this must happen before we update the statepoint with load of alloca
1646 // otherwise we lose the link between statepoint and old def
Igor Laevsky285fe842015-05-19 16:29:43 +00001647 for (size_t i = 0; i < Records.size(); i++) {
1648 const struct PartiallyConstructedSafepointRecord &Info = Records[i];
1649 Value *Statepoint = Info.StatepointToken;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001650
1651 // This will be used for consistency check
Igor Laevsky285fe842015-05-19 16:29:43 +00001652 DenseSet<Value *> VisitedLiveValues;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001653
1654 // Insert stores for normal statepoint gc relocates
Igor Laevsky285fe842015-05-19 16:29:43 +00001655 insertRelocationStores(Statepoint->users(), AllocaMap, VisitedLiveValues);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001656
1657 // In case if it was invoke statepoint
1658 // we will insert stores for exceptional path gc relocates.
Philip Reames0a3240f2015-02-20 21:34:11 +00001659 if (isa<InvokeInst>(Statepoint)) {
Igor Laevsky285fe842015-05-19 16:29:43 +00001660 insertRelocationStores(Info.UnwindToken->users(), AllocaMap,
1661 VisitedLiveValues);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001662 }
1663
Igor Laevskye0317182015-05-19 15:59:05 +00001664 // Do similar thing with rematerialized values
Igor Laevsky285fe842015-05-19 16:29:43 +00001665 insertRematerializationStores(Info.RematerializedValues, AllocaMap,
1666 VisitedLiveValues);
Igor Laevskye0317182015-05-19 15:59:05 +00001667
Philip Reamese73300b2015-04-13 16:41:32 +00001668 if (ClobberNonLive) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001669 // As a debugging aid, pretend that an unrelocated pointer becomes null at
Philip Reamese73300b2015-04-13 16:41:32 +00001670 // the gc.statepoint. This will turn some subtle GC problems into
1671 // slightly easier to debug SEGVs. Note that on large IR files with
1672 // lots of gc.statepoints this is extremely costly both memory and time
1673 // wise.
1674 SmallVector<AllocaInst *, 64> ToClobber;
Igor Laevsky285fe842015-05-19 16:29:43 +00001675 for (auto Pair : AllocaMap) {
Philip Reamese73300b2015-04-13 16:41:32 +00001676 Value *Def = Pair.first;
1677 AllocaInst *Alloca = cast<AllocaInst>(Pair.second);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001678
Philip Reamese73300b2015-04-13 16:41:32 +00001679 // This value was relocated
Igor Laevsky285fe842015-05-19 16:29:43 +00001680 if (VisitedLiveValues.count(Def)) {
Philip Reamese73300b2015-04-13 16:41:32 +00001681 continue;
1682 }
1683 ToClobber.push_back(Alloca);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001684 }
Philip Reamesfa2fcf172015-02-20 19:51:56 +00001685
Philip Reamese73300b2015-04-13 16:41:32 +00001686 auto InsertClobbersAt = [&](Instruction *IP) {
1687 for (auto *AI : ToClobber) {
1688 auto AIType = cast<PointerType>(AI->getType());
1689 auto PT = cast<PointerType>(AIType->getElementType());
1690 Constant *CPN = ConstantPointerNull::get(PT);
Igor Laevsky285fe842015-05-19 16:29:43 +00001691 StoreInst *Store = new StoreInst(CPN, AI);
1692 Store->insertBefore(IP);
Philip Reamese73300b2015-04-13 16:41:32 +00001693 }
1694 };
1695
1696 // Insert the clobbering stores. These may get intermixed with the
1697 // gc.results and gc.relocates, but that's fine.
1698 if (auto II = dyn_cast<InvokeInst>(Statepoint)) {
1699 InsertClobbersAt(II->getNormalDest()->getFirstInsertionPt());
1700 InsertClobbersAt(II->getUnwindDest()->getFirstInsertionPt());
1701 } else {
1702 BasicBlock::iterator Next(cast<CallInst>(Statepoint));
1703 Next++;
1704 InsertClobbersAt(Next);
Philip Reamesfa2fcf172015-02-20 19:51:56 +00001705 }
David Blaikie82ad7872015-02-20 23:44:24 +00001706 }
Philip Reamesd16a9b12015-02-20 01:06:44 +00001707 }
1708 // update use with load allocas and add store for gc_relocated
Igor Laevsky285fe842015-05-19 16:29:43 +00001709 for (auto Pair : AllocaMap) {
1710 Value *Def = Pair.first;
1711 Value *Alloca = Pair.second;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001712
1713 // we pre-record the uses of allocas so that we dont have to worry about
1714 // later update
1715 // that change the user information.
Igor Laevsky285fe842015-05-19 16:29:43 +00001716 SmallVector<Instruction *, 20> Uses;
Philip Reamesd16a9b12015-02-20 01:06:44 +00001717 // PERF: trade a linear scan for repeated reallocation
Igor Laevsky285fe842015-05-19 16:29:43 +00001718 Uses.reserve(std::distance(Def->user_begin(), Def->user_end()));
1719 for (User *U : Def->users()) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00001720 if (!isa<ConstantExpr>(U)) {
1721 // If the def has a ConstantExpr use, then the def is either a
1722 // ConstantExpr use itself or null. In either case
1723 // (recursively in the first, directly in the second), the oop
1724 // it is ultimately dependent on is null and this particular
1725 // use does not need to be fixed up.
Igor Laevsky285fe842015-05-19 16:29:43 +00001726 Uses.push_back(cast<Instruction>(U));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001727 }
1728 }
1729
Igor Laevsky285fe842015-05-19 16:29:43 +00001730 std::sort(Uses.begin(), Uses.end());
1731 auto Last = std::unique(Uses.begin(), Uses.end());
1732 Uses.erase(Last, Uses.end());
Philip Reamesd16a9b12015-02-20 01:06:44 +00001733
Igor Laevsky285fe842015-05-19 16:29:43 +00001734 for (Instruction *Use : Uses) {
1735 if (isa<PHINode>(Use)) {
1736 PHINode *Phi = cast<PHINode>(Use);
1737 for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++) {
1738 if (Def == Phi->getIncomingValue(i)) {
1739 LoadInst *Load = new LoadInst(
1740 Alloca, "", Phi->getIncomingBlock(i)->getTerminator());
1741 Phi->setIncomingValue(i, Load);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001742 }
1743 }
1744 } else {
Igor Laevsky285fe842015-05-19 16:29:43 +00001745 LoadInst *Load = new LoadInst(Alloca, "", Use);
1746 Use->replaceUsesOfWith(Def, Load);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001747 }
1748 }
1749
1750 // emit store for the initial gc value
1751 // store must be inserted after load, otherwise store will be in alloca's
1752 // use list and an extra load will be inserted before it
Igor Laevsky285fe842015-05-19 16:29:43 +00001753 StoreInst *Store = new StoreInst(Def, Alloca);
1754 if (Instruction *Inst = dyn_cast<Instruction>(Def)) {
1755 if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Inst)) {
Philip Reames6da37852015-03-04 00:13:52 +00001756 // InvokeInst is a TerminatorInst so the store need to be inserted
1757 // into its normal destination block.
Igor Laevsky285fe842015-05-19 16:29:43 +00001758 BasicBlock *NormalDest = Invoke->getNormalDest();
1759 Store->insertBefore(NormalDest->getFirstNonPHI());
Philip Reames6da37852015-03-04 00:13:52 +00001760 } else {
Igor Laevsky285fe842015-05-19 16:29:43 +00001761 assert(!Inst->isTerminator() &&
Philip Reames6da37852015-03-04 00:13:52 +00001762 "The only TerminatorInst that can produce a value is "
1763 "InvokeInst which is handled above.");
Igor Laevsky285fe842015-05-19 16:29:43 +00001764 Store->insertAfter(Inst);
Philip Reames6da37852015-03-04 00:13:52 +00001765 }
Philip Reamesd16a9b12015-02-20 01:06:44 +00001766 } else {
Igor Laevsky285fe842015-05-19 16:29:43 +00001767 assert(isa<Argument>(Def));
1768 Store->insertAfter(cast<Instruction>(Alloca));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001769 }
1770 }
1771
Igor Laevsky285fe842015-05-19 16:29:43 +00001772 assert(PromotableAllocas.size() == Live.size() + NumRematerializedValues &&
Philip Reamesd16a9b12015-02-20 01:06:44 +00001773 "we must have the same allocas with lives");
1774 if (!PromotableAllocas.empty()) {
1775 // apply mem2reg to promote alloca to SSA
1776 PromoteMemToReg(PromotableAllocas, DT);
1777 }
1778
1779#ifndef NDEBUG
Philip Reames704e78b2015-04-10 22:34:56 +00001780 for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end(); I != E;
1781 I++)
Philip Reamesa6ebf072015-03-27 05:53:16 +00001782 if (isa<AllocaInst>(*I))
1783 InitialAllocaNum--;
1784 assert(InitialAllocaNum == 0 && "We must not introduce any extra allocas");
Philip Reamesd16a9b12015-02-20 01:06:44 +00001785#endif
1786}
1787
1788/// Implement a unique function which doesn't require we sort the input
1789/// vector. Doing so has the effect of changing the output of a couple of
1790/// tests in ways which make them less useful in testing fused safepoints.
Philip Reamesd2b66462015-02-20 22:39:41 +00001791template <typename T> static void unique_unsorted(SmallVectorImpl<T> &Vec) {
Benjamin Kramer258ea0d2015-06-13 19:50:38 +00001792 SmallSet<T, 8> Seen;
1793 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), [&](const T &V) {
1794 return !Seen.insert(V).second;
1795 }), Vec.end());
Philip Reamesd16a9b12015-02-20 01:06:44 +00001796}
1797
Philip Reamesd16a9b12015-02-20 01:06:44 +00001798/// Insert holders so that each Value is obviously live through the entire
Philip Reamesf209a152015-04-13 20:00:30 +00001799/// lifetime of the call.
Philip Reamesd16a9b12015-02-20 01:06:44 +00001800static void insertUseHolderAfter(CallSite &CS, const ArrayRef<Value *> Values,
Philip Reamesf209a152015-04-13 20:00:30 +00001801 SmallVectorImpl<CallInst *> &Holders) {
Philip Reames21142752015-04-13 19:07:47 +00001802 if (Values.empty())
1803 // No values to hold live, might as well not insert the empty holder
1804 return;
1805
Philip Reamesd16a9b12015-02-20 01:06:44 +00001806 Module *M = CS.getInstruction()->getParent()->getParent()->getParent();
Philip Reamesf209a152015-04-13 20:00:30 +00001807 // Use a dummy vararg function to actually hold the values live
1808 Function *Func = cast<Function>(M->getOrInsertFunction(
1809 "__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true)));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001810 if (CS.isCall()) {
1811 // For call safepoints insert dummy calls right after safepoint
Philip Reamesf209a152015-04-13 20:00:30 +00001812 BasicBlock::iterator Next(CS.getInstruction());
1813 Next++;
1814 Holders.push_back(CallInst::Create(Func, Values, "", Next));
1815 return;
1816 }
1817 // For invoke safepooints insert dummy calls both in normal and
1818 // exceptional destination blocks
1819 auto *II = cast<InvokeInst>(CS.getInstruction());
1820 Holders.push_back(CallInst::Create(
1821 Func, Values, "", II->getNormalDest()->getFirstInsertionPt()));
1822 Holders.push_back(CallInst::Create(
1823 Func, Values, "", II->getUnwindDest()->getFirstInsertionPt()));
Philip Reamesd16a9b12015-02-20 01:06:44 +00001824}
1825
1826static void findLiveReferences(
Philip Reamesd2b66462015-02-20 22:39:41 +00001827 Function &F, DominatorTree &DT, Pass *P, ArrayRef<CallSite> toUpdate,
1828 MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) {
Philip Reamesdf1ef082015-04-10 22:53:14 +00001829 GCPtrLivenessData OriginalLivenessData;
1830 computeLiveInValues(DT, F, OriginalLivenessData);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001831 for (size_t i = 0; i < records.size(); i++) {
1832 struct PartiallyConstructedSafepointRecord &info = records[i];
Philip Reamesd2b66462015-02-20 22:39:41 +00001833 const CallSite &CS = toUpdate[i];
Philip Reamesdf1ef082015-04-10 22:53:14 +00001834 analyzeParsePointLiveness(DT, OriginalLivenessData, CS, info);
Philip Reamesd16a9b12015-02-20 01:06:44 +00001835 }
1836}
1837
Philip Reames8531d8c2015-04-10 21:48:25 +00001838/// Remove any vector of pointers from the liveset by scalarizing them over the
1839/// statepoint instruction. Adds the scalarized pieces to the liveset. It
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001840/// would be preferable to include the vector in the statepoint itself, but
Philip Reames8531d8c2015-04-10 21:48:25 +00001841/// the lowering code currently does not handle that. Extending it would be
1842/// slightly non-trivial since it requires a format change. Given how rare
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001843/// such cases are (for the moment?) scalarizing is an acceptable compromise.
Philip Reames8531d8c2015-04-10 21:48:25 +00001844static void splitVectorValues(Instruction *StatepointInst,
Philip Reames8fe7f132015-06-26 22:47:37 +00001845 StatepointLiveSetTy &LiveSet,
1846 DenseMap<Value *, Value *>& PointerToBase,
1847 DominatorTree &DT) {
Philip Reames8531d8c2015-04-10 21:48:25 +00001848 SmallVector<Value *, 16> ToSplit;
1849 for (Value *V : LiveSet)
1850 if (isa<VectorType>(V->getType()))
1851 ToSplit.push_back(V);
1852
1853 if (ToSplit.empty())
1854 return;
1855
Philip Reames8fe7f132015-06-26 22:47:37 +00001856 DenseMap<Value *, SmallVector<Value *, 16>> ElementMapping;
1857
Philip Reames8531d8c2015-04-10 21:48:25 +00001858 Function &F = *(StatepointInst->getParent()->getParent());
1859
Philip Reames704e78b2015-04-10 22:34:56 +00001860 DenseMap<Value *, AllocaInst *> AllocaMap;
Philip Reames8531d8c2015-04-10 21:48:25 +00001861 // First is normal return, second is exceptional return (invoke only)
Philip Reames704e78b2015-04-10 22:34:56 +00001862 DenseMap<Value *, std::pair<Value *, Value *>> Replacements;
Philip Reames8531d8c2015-04-10 21:48:25 +00001863 for (Value *V : ToSplit) {
Philip Reames704e78b2015-04-10 22:34:56 +00001864 AllocaInst *Alloca =
1865 new AllocaInst(V->getType(), "", F.getEntryBlock().getFirstNonPHI());
Philip Reames8531d8c2015-04-10 21:48:25 +00001866 AllocaMap[V] = Alloca;
1867
1868 VectorType *VT = cast<VectorType>(V->getType());
1869 IRBuilder<> Builder(StatepointInst);
Philip Reames704e78b2015-04-10 22:34:56 +00001870 SmallVector<Value *, 16> Elements;
Philip Reames8531d8c2015-04-10 21:48:25 +00001871 for (unsigned i = 0; i < VT->getNumElements(); i++)
1872 Elements.push_back(Builder.CreateExtractElement(V, Builder.getInt32(i)));
Philip Reames8fe7f132015-06-26 22:47:37 +00001873 ElementMapping[V] = Elements;
Philip Reames8531d8c2015-04-10 21:48:25 +00001874
1875 auto InsertVectorReform = [&](Instruction *IP) {
1876 Builder.SetInsertPoint(IP);
1877 Builder.SetCurrentDebugLocation(IP->getDebugLoc());
1878 Value *ResultVec = UndefValue::get(VT);
1879 for (unsigned i = 0; i < VT->getNumElements(); i++)
1880 ResultVec = Builder.CreateInsertElement(ResultVec, Elements[i],
1881 Builder.getInt32(i));
1882 return ResultVec;
1883 };
1884
1885 if (isa<CallInst>(StatepointInst)) {
1886 BasicBlock::iterator Next(StatepointInst);
1887 Next++;
1888 Instruction *IP = &*(Next);
1889 Replacements[V].first = InsertVectorReform(IP);
1890 Replacements[V].second = nullptr;
1891 } else {
1892 InvokeInst *Invoke = cast<InvokeInst>(StatepointInst);
1893 // We've already normalized - check that we don't have shared destination
Philip Reames704e78b2015-04-10 22:34:56 +00001894 // blocks
Philip Reames8531d8c2015-04-10 21:48:25 +00001895 BasicBlock *NormalDest = Invoke->getNormalDest();
1896 assert(!isa<PHINode>(NormalDest->begin()));
1897 BasicBlock *UnwindDest = Invoke->getUnwindDest();
1898 assert(!isa<PHINode>(UnwindDest->begin()));
1899 // Insert insert element sequences in both successors
1900 Instruction *IP = &*(NormalDest->getFirstInsertionPt());
1901 Replacements[V].first = InsertVectorReform(IP);
1902 IP = &*(UnwindDest->getFirstInsertionPt());
1903 Replacements[V].second = InsertVectorReform(IP);
1904 }
1905 }
Philip Reames8fe7f132015-06-26 22:47:37 +00001906
Philip Reames8531d8c2015-04-10 21:48:25 +00001907 for (Value *V : ToSplit) {
1908 AllocaInst *Alloca = AllocaMap[V];
1909
1910 // Capture all users before we start mutating use lists
Philip Reames704e78b2015-04-10 22:34:56 +00001911 SmallVector<Instruction *, 16> Users;
Philip Reames8531d8c2015-04-10 21:48:25 +00001912 for (User *U : V->users())
1913 Users.push_back(cast<Instruction>(U));
1914
1915 for (Instruction *I : Users) {
1916 if (auto Phi = dyn_cast<PHINode>(I)) {
1917 for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++)
1918 if (V == Phi->getIncomingValue(i)) {
Philip Reames704e78b2015-04-10 22:34:56 +00001919 LoadInst *Load = new LoadInst(
1920 Alloca, "", Phi->getIncomingBlock(i)->getTerminator());
Philip Reames8531d8c2015-04-10 21:48:25 +00001921 Phi->setIncomingValue(i, Load);
1922 }
1923 } else {
1924 LoadInst *Load = new LoadInst(Alloca, "", I);
1925 I->replaceUsesOfWith(V, Load);
1926 }
1927 }
1928
1929 // Store the original value and the replacement value into the alloca
1930 StoreInst *Store = new StoreInst(V, Alloca);
1931 if (auto I = dyn_cast<Instruction>(V))
1932 Store->insertAfter(I);
1933 else
1934 Store->insertAfter(Alloca);
Philip Reames704e78b2015-04-10 22:34:56 +00001935
Philip Reames8531d8c2015-04-10 21:48:25 +00001936 // Normal return for invoke, or call return
1937 Instruction *Replacement = cast<Instruction>(Replacements[V].first);
1938 (new StoreInst(Replacement, Alloca))->insertAfter(Replacement);
1939 // Unwind return for invoke only
1940 Replacement = cast_or_null<Instruction>(Replacements[V].second);
1941 if (Replacement)
1942 (new StoreInst(Replacement, Alloca))->insertAfter(Replacement);
1943 }
1944
1945 // apply mem2reg to promote alloca to SSA
Philip Reames704e78b2015-04-10 22:34:56 +00001946 SmallVector<AllocaInst *, 16> Allocas;
Philip Reames8531d8c2015-04-10 21:48:25 +00001947 for (Value *V : ToSplit)
1948 Allocas.push_back(AllocaMap[V]);
1949 PromoteMemToReg(Allocas, DT);
Philip Reames8fe7f132015-06-26 22:47:37 +00001950
1951 // Update our tracking of live pointers and base mappings to account for the
1952 // changes we just made.
1953 for (Value *V : ToSplit) {
1954 auto &Elements = ElementMapping[V];
1955
1956 LiveSet.erase(V);
1957 LiveSet.insert(Elements.begin(), Elements.end());
1958 // We need to update the base mapping as well.
1959 assert(PointerToBase.count(V));
1960 Value *OldBase = PointerToBase[V];
1961 auto &BaseElements = ElementMapping[OldBase];
1962 PointerToBase.erase(V);
1963 assert(Elements.size() == BaseElements.size());
1964 for (unsigned i = 0; i < Elements.size(); i++) {
1965 Value *Elem = Elements[i];
1966 PointerToBase[Elem] = BaseElements[i];
1967 }
1968 }
Philip Reames8531d8c2015-04-10 21:48:25 +00001969}
1970
Igor Laevskye0317182015-05-19 15:59:05 +00001971// Helper function for the "rematerializeLiveValues". It walks use chain
1972// starting from the "CurrentValue" until it meets "BaseValue". Only "simple"
1973// values are visited (currently it is GEP's and casts). Returns true if it
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001974// successfully reached "BaseValue" and false otherwise.
Igor Laevskye0317182015-05-19 15:59:05 +00001975// Fills "ChainToBase" array with all visited values. "BaseValue" is not
1976// recorded.
1977static bool findRematerializableChainToBasePointer(
1978 SmallVectorImpl<Instruction*> &ChainToBase,
1979 Value *CurrentValue, Value *BaseValue) {
1980
1981 // We have found a base value
1982 if (CurrentValue == BaseValue) {
1983 return true;
1984 }
1985
1986 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurrentValue)) {
1987 ChainToBase.push_back(GEP);
1988 return findRematerializableChainToBasePointer(ChainToBase,
1989 GEP->getPointerOperand(),
1990 BaseValue);
1991 }
1992
1993 if (CastInst *CI = dyn_cast<CastInst>(CurrentValue)) {
1994 Value *Def = CI->stripPointerCasts();
1995
1996 // This two checks are basically similar. First one is here for the
1997 // consistency with findBasePointers logic.
1998 assert(!isa<CastInst>(Def) && "not a pointer cast found");
1999 if (!CI->isNoopCast(CI->getModule()->getDataLayout()))
2000 return false;
2001
2002 ChainToBase.push_back(CI);
2003 return findRematerializableChainToBasePointer(ChainToBase, Def, BaseValue);
2004 }
2005
2006 // Not supported instruction in the chain
2007 return false;
2008}
2009
2010// Helper function for the "rematerializeLiveValues". Compute cost of the use
2011// chain we are going to rematerialize.
2012static unsigned
2013chainToBasePointerCost(SmallVectorImpl<Instruction*> &Chain,
2014 TargetTransformInfo &TTI) {
2015 unsigned Cost = 0;
2016
2017 for (Instruction *Instr : Chain) {
2018 if (CastInst *CI = dyn_cast<CastInst>(Instr)) {
2019 assert(CI->isNoopCast(CI->getModule()->getDataLayout()) &&
2020 "non noop cast is found during rematerialization");
2021
2022 Type *SrcTy = CI->getOperand(0)->getType();
2023 Cost += TTI.getCastInstrCost(CI->getOpcode(), CI->getType(), SrcTy);
2024
2025 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Instr)) {
2026 // Cost of the address calculation
2027 Type *ValTy = GEP->getPointerOperandType()->getPointerElementType();
2028 Cost += TTI.getAddressComputationCost(ValTy);
2029
2030 // And cost of the GEP itself
2031 // TODO: Use TTI->getGEPCost here (it exists, but appears to be not
2032 // allowed for the external usage)
2033 if (!GEP->hasAllConstantIndices())
2034 Cost += 2;
2035
2036 } else {
2037 llvm_unreachable("unsupported instruciton type during rematerialization");
2038 }
2039 }
2040
2041 return Cost;
2042}
2043
2044// From the statepoint liveset pick values that are cheaper to recompute then to
2045// relocate. Remove this values from the liveset, rematerialize them after
2046// statepoint and record them in "Info" structure. Note that similar to
2047// relocated values we don't do any user adjustments here.
2048static void rematerializeLiveValues(CallSite CS,
2049 PartiallyConstructedSafepointRecord &Info,
2050 TargetTransformInfo &TTI) {
Aaron Ballmanff7d4fa2015-05-20 14:53:50 +00002051 const unsigned int ChainLengthThreshold = 10;
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +00002052
Igor Laevskye0317182015-05-19 15:59:05 +00002053 // Record values we are going to delete from this statepoint live set.
2054 // We can not di this in following loop due to iterator invalidation.
2055 SmallVector<Value *, 32> LiveValuesToBeDeleted;
2056
2057 for (Value *LiveValue: Info.liveset) {
2058 // For each live pointer find it's defining chain
2059 SmallVector<Instruction *, 3> ChainToBase;
Philip Reames74ce2e72015-07-21 16:51:17 +00002060 assert(Info.PointerToBase.count(LiveValue));
Igor Laevskye0317182015-05-19 15:59:05 +00002061 bool FoundChain =
2062 findRematerializableChainToBasePointer(ChainToBase,
2063 LiveValue,
2064 Info.PointerToBase[LiveValue]);
2065 // Nothing to do, or chain is too long
2066 if (!FoundChain ||
2067 ChainToBase.size() == 0 ||
2068 ChainToBase.size() > ChainLengthThreshold)
2069 continue;
2070
2071 // Compute cost of this chain
2072 unsigned Cost = chainToBasePointerCost(ChainToBase, TTI);
2073 // TODO: We can also account for cases when we will be able to remove some
2074 // of the rematerialized values by later optimization passes. I.e if
2075 // we rematerialized several intersecting chains. Or if original values
2076 // don't have any uses besides this statepoint.
2077
2078 // For invokes we need to rematerialize each chain twice - for normal and
2079 // for unwind basic blocks. Model this by multiplying cost by two.
2080 if (CS.isInvoke()) {
2081 Cost *= 2;
2082 }
2083 // If it's too expensive - skip it
2084 if (Cost >= RematerializationThreshold)
2085 continue;
2086
2087 // Remove value from the live set
2088 LiveValuesToBeDeleted.push_back(LiveValue);
2089
2090 // Clone instructions and record them inside "Info" structure
2091
2092 // Walk backwards to visit top-most instructions first
2093 std::reverse(ChainToBase.begin(), ChainToBase.end());
2094
2095 // Utility function which clones all instructions from "ChainToBase"
2096 // and inserts them before "InsertBefore". Returns rematerialized value
2097 // which should be used after statepoint.
2098 auto rematerializeChain = [&ChainToBase](Instruction *InsertBefore) {
2099 Instruction *LastClonedValue = nullptr;
2100 Instruction *LastValue = nullptr;
2101 for (Instruction *Instr: ChainToBase) {
2102 // Only GEP's and casts are suported as we need to be careful to not
2103 // introduce any new uses of pointers not in the liveset.
2104 // Note that it's fine to introduce new uses of pointers which were
2105 // otherwise not used after this statepoint.
2106 assert(isa<GetElementPtrInst>(Instr) || isa<CastInst>(Instr));
2107
2108 Instruction *ClonedValue = Instr->clone();
2109 ClonedValue->insertBefore(InsertBefore);
2110 ClonedValue->setName(Instr->getName() + ".remat");
2111
2112 // If it is not first instruction in the chain then it uses previously
2113 // cloned value. We should update it to use cloned value.
2114 if (LastClonedValue) {
2115 assert(LastValue);
2116 ClonedValue->replaceUsesOfWith(LastValue, LastClonedValue);
2117#ifndef NDEBUG
Igor Laevskyd83f6972015-05-21 13:02:14 +00002118 // Assert that cloned instruction does not use any instructions from
2119 // this chain other than LastClonedValue
2120 for (auto OpValue : ClonedValue->operand_values()) {
2121 assert(std::find(ChainToBase.begin(), ChainToBase.end(), OpValue) ==
2122 ChainToBase.end() &&
2123 "incorrect use in rematerialization chain");
Igor Laevskye0317182015-05-19 15:59:05 +00002124 }
2125#endif
2126 }
2127
2128 LastClonedValue = ClonedValue;
2129 LastValue = Instr;
2130 }
2131 assert(LastClonedValue);
2132 return LastClonedValue;
2133 };
2134
2135 // Different cases for calls and invokes. For invokes we need to clone
2136 // instructions both on normal and unwind path.
2137 if (CS.isCall()) {
2138 Instruction *InsertBefore = CS.getInstruction()->getNextNode();
2139 assert(InsertBefore);
2140 Instruction *RematerializedValue = rematerializeChain(InsertBefore);
2141 Info.RematerializedValues[RematerializedValue] = LiveValue;
2142 } else {
2143 InvokeInst *Invoke = cast<InvokeInst>(CS.getInstruction());
2144
2145 Instruction *NormalInsertBefore =
2146 Invoke->getNormalDest()->getFirstInsertionPt();
2147 Instruction *UnwindInsertBefore =
2148 Invoke->getUnwindDest()->getFirstInsertionPt();
2149
2150 Instruction *NormalRematerializedValue =
2151 rematerializeChain(NormalInsertBefore);
2152 Instruction *UnwindRematerializedValue =
2153 rematerializeChain(UnwindInsertBefore);
2154
2155 Info.RematerializedValues[NormalRematerializedValue] = LiveValue;
2156 Info.RematerializedValues[UnwindRematerializedValue] = LiveValue;
2157 }
2158 }
2159
2160 // Remove rematerializaed values from the live set
2161 for (auto LiveValue: LiveValuesToBeDeleted) {
2162 Info.liveset.erase(LiveValue);
2163 }
2164}
2165
Philip Reamesd16a9b12015-02-20 01:06:44 +00002166static bool insertParsePoints(Function &F, DominatorTree &DT, Pass *P,
Philip Reamesd2b66462015-02-20 22:39:41 +00002167 SmallVectorImpl<CallSite> &toUpdate) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00002168#ifndef NDEBUG
2169 // sanity check the input
2170 std::set<CallSite> uniqued;
2171 uniqued.insert(toUpdate.begin(), toUpdate.end());
2172 assert(uniqued.size() == toUpdate.size() && "no duplicates please!");
2173
2174 for (size_t i = 0; i < toUpdate.size(); i++) {
2175 CallSite &CS = toUpdate[i];
2176 assert(CS.getInstruction()->getParent()->getParent() == &F);
2177 assert(isStatepoint(CS) && "expected to already be a deopt statepoint");
2178 }
2179#endif
2180
Philip Reames69e51ca2015-04-13 18:07:21 +00002181 // When inserting gc.relocates for invokes, we need to be able to insert at
2182 // the top of the successor blocks. See the comment on
2183 // normalForInvokeSafepoint on exactly what is needed. Note that this step
Philip Reamesf209a152015-04-13 20:00:30 +00002184 // may restructure the CFG.
2185 for (CallSite CS : toUpdate) {
2186 if (!CS.isInvoke())
2187 continue;
2188 InvokeInst *invoke = cast<InvokeInst>(CS.getInstruction());
2189 normalizeForInvokeSafepoint(invoke->getNormalDest(), invoke->getParent(),
Sanjoy Dasea45f0e2015-06-02 22:33:34 +00002190 DT);
Philip Reamesf209a152015-04-13 20:00:30 +00002191 normalizeForInvokeSafepoint(invoke->getUnwindDest(), invoke->getParent(),
Sanjoy Dasea45f0e2015-06-02 22:33:34 +00002192 DT);
Philip Reamesf209a152015-04-13 20:00:30 +00002193 }
Philip Reames69e51ca2015-04-13 18:07:21 +00002194
Philip Reamesd16a9b12015-02-20 01:06:44 +00002195 // A list of dummy calls added to the IR to keep various values obviously
2196 // live in the IR. We'll remove all of these when done.
Philip Reamesd2b66462015-02-20 22:39:41 +00002197 SmallVector<CallInst *, 64> holders;
Philip Reamesd16a9b12015-02-20 01:06:44 +00002198
2199 // Insert a dummy call with all of the arguments to the vm_state we'll need
2200 // for the actual safepoint insertion. This ensures reference arguments in
2201 // the deopt argument list are considered live through the safepoint (and
2202 // thus makes sure they get relocated.)
2203 for (size_t i = 0; i < toUpdate.size(); i++) {
2204 CallSite &CS = toUpdate[i];
2205 Statepoint StatepointCS(CS);
2206
2207 SmallVector<Value *, 64> DeoptValues;
2208 for (Use &U : StatepointCS.vm_state_args()) {
2209 Value *Arg = cast<Value>(&U);
Philip Reames8531d8c2015-04-10 21:48:25 +00002210 assert(!isUnhandledGCPointerType(Arg->getType()) &&
2211 "support for FCA unimplemented");
2212 if (isHandledGCPointerType(Arg->getType()))
Philip Reamesd16a9b12015-02-20 01:06:44 +00002213 DeoptValues.push_back(Arg);
2214 }
2215 insertUseHolderAfter(CS, DeoptValues, holders);
2216 }
2217
Philip Reamesd2b66462015-02-20 22:39:41 +00002218 SmallVector<struct PartiallyConstructedSafepointRecord, 64> records;
Philip Reamesd16a9b12015-02-20 01:06:44 +00002219 records.reserve(toUpdate.size());
2220 for (size_t i = 0; i < toUpdate.size(); i++) {
2221 struct PartiallyConstructedSafepointRecord info;
2222 records.push_back(info);
2223 }
2224 assert(records.size() == toUpdate.size());
2225
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002226 // A) Identify all gc pointers which are statically live at the given call
Philip Reamesd16a9b12015-02-20 01:06:44 +00002227 // site.
2228 findLiveReferences(F, DT, P, toUpdate, records);
2229
2230 // B) Find the base pointers for each live pointer
2231 /* scope for caching */ {
2232 // Cache the 'defining value' relation used in the computation and
2233 // insertion of base phis and selects. This ensures that we don't insert
2234 // large numbers of duplicate base_phis.
2235 DefiningValueMapTy DVCache;
2236
2237 for (size_t i = 0; i < records.size(); i++) {
2238 struct PartiallyConstructedSafepointRecord &info = records[i];
2239 CallSite &CS = toUpdate[i];
2240 findBasePointers(DT, DVCache, CS, info);
2241 }
2242 } // end of cache scope
2243
2244 // The base phi insertion logic (for any safepoint) may have inserted new
2245 // instructions which are now live at some safepoint. The simplest such
2246 // example is:
2247 // loop:
2248 // phi a <-- will be a new base_phi here
2249 // safepoint 1 <-- that needs to be live here
2250 // gep a + 1
2251 // safepoint 2
2252 // br loop
Philip Reamesd16a9b12015-02-20 01:06:44 +00002253 // We insert some dummy calls after each safepoint to definitely hold live
2254 // the base pointers which were identified for that safepoint. We'll then
2255 // ask liveness for _every_ base inserted to see what is now live. Then we
2256 // remove the dummy calls.
2257 holders.reserve(holders.size() + records.size());
2258 for (size_t i = 0; i < records.size(); i++) {
2259 struct PartiallyConstructedSafepointRecord &info = records[i];
2260 CallSite &CS = toUpdate[i];
2261
2262 SmallVector<Value *, 128> Bases;
Philip Reamesf2041322015-02-20 19:26:04 +00002263 for (auto Pair : info.PointerToBase) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00002264 Bases.push_back(Pair.second);
2265 }
2266 insertUseHolderAfter(CS, Bases, holders);
2267 }
2268
Philip Reamesdf1ef082015-04-10 22:53:14 +00002269 // By selecting base pointers, we've effectively inserted new uses. Thus, we
2270 // need to rerun liveness. We may *also* have inserted new defs, but that's
2271 // not the key issue.
2272 recomputeLiveInValues(F, DT, P, toUpdate, records);
Philip Reamesd16a9b12015-02-20 01:06:44 +00002273
Philip Reamesd16a9b12015-02-20 01:06:44 +00002274 if (PrintBasePointers) {
2275 for (size_t i = 0; i < records.size(); i++) {
2276 struct PartiallyConstructedSafepointRecord &info = records[i];
2277 errs() << "Base Pairs: (w/Relocation)\n";
Philip Reamesf2041322015-02-20 19:26:04 +00002278 for (auto Pair : info.PointerToBase) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00002279 errs() << " derived %" << Pair.first->getName() << " base %"
2280 << Pair.second->getName() << "\n";
2281 }
2282 }
2283 }
2284 for (size_t i = 0; i < holders.size(); i++) {
2285 holders[i]->eraseFromParent();
2286 holders[i] = nullptr;
2287 }
2288 holders.clear();
2289
Philip Reames8fe7f132015-06-26 22:47:37 +00002290 // Do a limited scalarization of any live at safepoint vector values which
2291 // contain pointers. This enables this pass to run after vectorization at
2292 // the cost of some possible performance loss. TODO: it would be nice to
2293 // natively support vectors all the way through the backend so we don't need
2294 // to scalarize here.
2295 for (size_t i = 0; i < records.size(); i++) {
2296 struct PartiallyConstructedSafepointRecord &info = records[i];
2297 Instruction *statepoint = toUpdate[i].getInstruction();
2298 splitVectorValues(cast<Instruction>(statepoint), info.liveset,
2299 info.PointerToBase, DT);
2300 }
2301
Igor Laevskye0317182015-05-19 15:59:05 +00002302 // In order to reduce live set of statepoint we might choose to rematerialize
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002303 // some values instead of relocating them. This is purely an optimization and
Igor Laevskye0317182015-05-19 15:59:05 +00002304 // does not influence correctness.
2305 TargetTransformInfo &TTI =
2306 P->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
2307
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +00002308 for (size_t i = 0; i < records.size(); i++) {
Igor Laevskye0317182015-05-19 15:59:05 +00002309 struct PartiallyConstructedSafepointRecord &info = records[i];
2310 CallSite &CS = toUpdate[i];
2311
2312 rematerializeLiveValues(CS, info, TTI);
2313 }
2314
Philip Reamesd16a9b12015-02-20 01:06:44 +00002315 // Now run through and replace the existing statepoints with new ones with
2316 // the live variables listed. We do not yet update uses of the values being
2317 // relocated. We have references to live variables that need to
2318 // survive to the last iteration of this loop. (By construction, the
2319 // previous statepoint can not be a live variable, thus we can and remove
2320 // the old statepoint calls as we go.)
2321 for (size_t i = 0; i < records.size(); i++) {
2322 struct PartiallyConstructedSafepointRecord &info = records[i];
2323 CallSite &CS = toUpdate[i];
2324 makeStatepointExplicit(DT, CS, P, info);
2325 }
2326 toUpdate.clear(); // prevent accident use of invalid CallSites
2327
Philip Reamesd16a9b12015-02-20 01:06:44 +00002328 // Do all the fixups of the original live variables to their relocated selves
Philip Reamesd2b66462015-02-20 22:39:41 +00002329 SmallVector<Value *, 128> live;
Philip Reamesd16a9b12015-02-20 01:06:44 +00002330 for (size_t i = 0; i < records.size(); i++) {
2331 struct PartiallyConstructedSafepointRecord &info = records[i];
2332 // We can't simply save the live set from the original insertion. One of
2333 // the live values might be the result of a call which needs a safepoint.
2334 // That Value* no longer exists and we need to use the new gc_result.
2335 // Thankfully, the liveset is embedded in the statepoint (and updated), so
2336 // we just grab that.
Philip Reames0a3240f2015-02-20 21:34:11 +00002337 Statepoint statepoint(info.StatepointToken);
Philip Reamesd16a9b12015-02-20 01:06:44 +00002338 live.insert(live.end(), statepoint.gc_args_begin(),
2339 statepoint.gc_args_end());
Philip Reames9a2e01d2015-04-13 17:35:55 +00002340#ifndef NDEBUG
2341 // Do some basic sanity checks on our liveness results before performing
2342 // relocation. Relocation can and will turn mistakes in liveness results
2343 // into non-sensical code which is must harder to debug.
2344 // TODO: It would be nice to test consistency as well
2345 assert(DT.isReachableFromEntry(info.StatepointToken->getParent()) &&
2346 "statepoint must be reachable or liveness is meaningless");
2347 for (Value *V : statepoint.gc_args()) {
2348 if (!isa<Instruction>(V))
2349 // Non-instruction values trivial dominate all possible uses
2350 continue;
2351 auto LiveInst = cast<Instruction>(V);
2352 assert(DT.isReachableFromEntry(LiveInst->getParent()) &&
2353 "unreachable values should never be live");
2354 assert(DT.dominates(LiveInst, info.StatepointToken) &&
2355 "basic SSA liveness expectation violated by liveness analysis");
2356 }
2357#endif
Philip Reamesd16a9b12015-02-20 01:06:44 +00002358 }
2359 unique_unsorted(live);
2360
Nick Lewyckyeb3231e2015-02-20 07:14:02 +00002361#ifndef NDEBUG
Philip Reamesd16a9b12015-02-20 01:06:44 +00002362 // sanity check
2363 for (auto ptr : live) {
2364 assert(isGCPointerType(ptr->getType()) && "must be a gc pointer type");
2365 }
Nick Lewyckyeb3231e2015-02-20 07:14:02 +00002366#endif
Philip Reamesd16a9b12015-02-20 01:06:44 +00002367
2368 relocationViaAlloca(F, DT, live, records);
2369 return !records.empty();
2370}
2371
Sanjoy Das353a19e2015-06-02 22:33:37 +00002372// Handles both return values and arguments for Functions and CallSites.
2373template <typename AttrHolder>
2374static void RemoveDerefAttrAtIndex(LLVMContext &Ctx, AttrHolder &AH,
2375 unsigned Index) {
2376 AttrBuilder R;
2377 if (AH.getDereferenceableBytes(Index))
2378 R.addAttribute(Attribute::get(Ctx, Attribute::Dereferenceable,
2379 AH.getDereferenceableBytes(Index)));
2380 if (AH.getDereferenceableOrNullBytes(Index))
2381 R.addAttribute(Attribute::get(Ctx, Attribute::DereferenceableOrNull,
2382 AH.getDereferenceableOrNullBytes(Index)));
2383
2384 if (!R.empty())
2385 AH.setAttributes(AH.getAttributes().removeAttributes(
2386 Ctx, Index, AttributeSet::get(Ctx, Index, R)));
Vasileios Kalintiris9f77f612015-06-03 08:51:30 +00002387}
Sanjoy Das353a19e2015-06-02 22:33:37 +00002388
2389void
2390RewriteStatepointsForGC::stripDereferenceabilityInfoFromPrototype(Function &F) {
2391 LLVMContext &Ctx = F.getContext();
2392
2393 for (Argument &A : F.args())
2394 if (isa<PointerType>(A.getType()))
2395 RemoveDerefAttrAtIndex(Ctx, F, A.getArgNo() + 1);
2396
2397 if (isa<PointerType>(F.getReturnType()))
2398 RemoveDerefAttrAtIndex(Ctx, F, AttributeSet::ReturnIndex);
2399}
2400
2401void RewriteStatepointsForGC::stripDereferenceabilityInfoFromBody(Function &F) {
2402 if (F.empty())
2403 return;
2404
2405 LLVMContext &Ctx = F.getContext();
2406 MDBuilder Builder(Ctx);
2407
Nico Rieck78199512015-08-06 19:10:45 +00002408 for (Instruction &I : instructions(F)) {
Sanjoy Das353a19e2015-06-02 22:33:37 +00002409 if (const MDNode *MD = I.getMetadata(LLVMContext::MD_tbaa)) {
2410 assert(MD->getNumOperands() < 5 && "unrecognized metadata shape!");
2411 bool IsImmutableTBAA =
2412 MD->getNumOperands() == 4 &&
2413 mdconst::extract<ConstantInt>(MD->getOperand(3))->getValue() == 1;
2414
2415 if (!IsImmutableTBAA)
2416 continue; // no work to do, MD_tbaa is already marked mutable
2417
2418 MDNode *Base = cast<MDNode>(MD->getOperand(0));
2419 MDNode *Access = cast<MDNode>(MD->getOperand(1));
2420 uint64_t Offset =
2421 mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue();
2422
2423 MDNode *MutableTBAA =
2424 Builder.createTBAAStructTagNode(Base, Access, Offset);
2425 I.setMetadata(LLVMContext::MD_tbaa, MutableTBAA);
2426 }
2427
2428 if (CallSite CS = CallSite(&I)) {
2429 for (int i = 0, e = CS.arg_size(); i != e; i++)
2430 if (isa<PointerType>(CS.getArgument(i)->getType()))
2431 RemoveDerefAttrAtIndex(Ctx, CS, i + 1);
2432 if (isa<PointerType>(CS.getType()))
2433 RemoveDerefAttrAtIndex(Ctx, CS, AttributeSet::ReturnIndex);
2434 }
2435 }
2436}
2437
Philip Reamesd16a9b12015-02-20 01:06:44 +00002438/// Returns true if this function should be rewritten by this pass. The main
2439/// point of this function is as an extension point for custom logic.
2440static bool shouldRewriteStatepointsIn(Function &F) {
2441 // TODO: This should check the GCStrategy
Philip Reames2ef029c2015-02-20 18:56:14 +00002442 if (F.hasGC()) {
NAKAMURA Takumifb3bd712015-05-25 01:43:23 +00002443 const char *FunctionGCName = F.getGC();
2444 const StringRef StatepointExampleName("statepoint-example");
2445 const StringRef CoreCLRName("coreclr");
2446 return (StatepointExampleName == FunctionGCName) ||
NAKAMURA Takumi5582a6a2015-05-25 01:43:34 +00002447 (CoreCLRName == FunctionGCName);
2448 } else
Philip Reames2ef029c2015-02-20 18:56:14 +00002449 return false;
Philip Reamesd16a9b12015-02-20 01:06:44 +00002450}
2451
Sanjoy Das353a19e2015-06-02 22:33:37 +00002452void RewriteStatepointsForGC::stripDereferenceabilityInfo(Module &M) {
2453#ifndef NDEBUG
2454 assert(std::any_of(M.begin(), M.end(), shouldRewriteStatepointsIn) &&
2455 "precondition!");
2456#endif
2457
2458 for (Function &F : M)
2459 stripDereferenceabilityInfoFromPrototype(F);
2460
2461 for (Function &F : M)
2462 stripDereferenceabilityInfoFromBody(F);
2463}
2464
Philip Reamesd16a9b12015-02-20 01:06:44 +00002465bool RewriteStatepointsForGC::runOnFunction(Function &F) {
2466 // Nothing to do for declarations.
2467 if (F.isDeclaration() || F.empty())
2468 return false;
2469
2470 // Policy choice says not to rewrite - the most common reason is that we're
2471 // compiling code without a GCStrategy.
2472 if (!shouldRewriteStatepointsIn(F))
2473 return false;
2474
Sanjoy Dasea45f0e2015-06-02 22:33:34 +00002475 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
Philip Reames704e78b2015-04-10 22:34:56 +00002476
Philip Reames85b36a82015-04-10 22:07:04 +00002477 // Gather all the statepoints which need rewritten. Be careful to only
2478 // consider those in reachable code since we need to ask dominance queries
2479 // when rewriting. We'll delete the unreachable ones in a moment.
Philip Reamesd2b66462015-02-20 22:39:41 +00002480 SmallVector<CallSite, 64> ParsePointNeeded;
Philip Reamesf66d7372015-04-10 22:16:58 +00002481 bool HasUnreachableStatepoint = false;
Nico Rieck78199512015-08-06 19:10:45 +00002482 for (Instruction &I : instructions(F)) {
Philip Reamesd16a9b12015-02-20 01:06:44 +00002483 // TODO: only the ones with the flag set!
Philip Reames85b36a82015-04-10 22:07:04 +00002484 if (isStatepoint(I)) {
2485 if (DT.isReachableFromEntry(I.getParent()))
2486 ParsePointNeeded.push_back(CallSite(&I));
2487 else
Philip Reamesf66d7372015-04-10 22:16:58 +00002488 HasUnreachableStatepoint = true;
Philip Reames85b36a82015-04-10 22:07:04 +00002489 }
Philip Reamesd16a9b12015-02-20 01:06:44 +00002490 }
2491
Philip Reames85b36a82015-04-10 22:07:04 +00002492 bool MadeChange = false;
Philip Reames704e78b2015-04-10 22:34:56 +00002493
Philip Reames85b36a82015-04-10 22:07:04 +00002494 // Delete any unreachable statepoints so that we don't have unrewritten
2495 // statepoints surviving this pass. This makes testing easier and the
2496 // resulting IR less confusing to human readers. Rather than be fancy, we
2497 // just reuse a utility function which removes the unreachable blocks.
Philip Reamesf66d7372015-04-10 22:16:58 +00002498 if (HasUnreachableStatepoint)
Philip Reames85b36a82015-04-10 22:07:04 +00002499 MadeChange |= removeUnreachableBlocks(F);
2500
Philip Reamesd16a9b12015-02-20 01:06:44 +00002501 // Return early if no work to do.
2502 if (ParsePointNeeded.empty())
Philip Reames85b36a82015-04-10 22:07:04 +00002503 return MadeChange;
Philip Reamesd16a9b12015-02-20 01:06:44 +00002504
Philip Reames85b36a82015-04-10 22:07:04 +00002505 // As a prepass, go ahead and aggressively destroy single entry phi nodes.
2506 // These are created by LCSSA. They have the effect of increasing the size
2507 // of liveness sets for no good reason. It may be harder to do this post
2508 // insertion since relocations and base phis can confuse things.
2509 for (BasicBlock &BB : F)
2510 if (BB.getUniquePredecessor()) {
2511 MadeChange = true;
2512 FoldSingleEntryPHINodes(&BB);
2513 }
2514
Philip Reames971dc3a2015-08-12 22:11:45 +00002515 // Before we start introducing relocations, we want to tweak the IR a bit to
2516 // avoid unfortunate code generation effects. The main example is that we
2517 // want to try to make sure the comparison feeding a branch is after any
2518 // safepoints. Otherwise, we end up with a comparison of pre-relocation
2519 // values feeding a branch after relocation. This is semantically correct,
2520 // but results in extra register pressure since both the pre-relocation and
2521 // post-relocation copies must be available in registers. For code without
2522 // relocations this is handled elsewhere, but teaching the scheduler to
2523 // reverse the transform we're about to do would be slightly complex.
2524 // Note: This may extend the live range of the inputs to the icmp and thus
2525 // increase the liveset of any statepoint we move over. This is profitable
2526 // as long as all statepoints are in rare blocks. If we had in-register
2527 // lowering for live values this would be a much safer transform.
2528 auto getConditionInst = [](TerminatorInst *TI) -> Instruction* {
2529 if (auto *BI = dyn_cast<BranchInst>(TI))
2530 if (BI->isConditional())
2531 return dyn_cast<Instruction>(BI->getCondition());
2532 // TODO: Extend this to handle switches
2533 return nullptr;
2534 };
2535 for (BasicBlock &BB : F) {
2536 TerminatorInst *TI = BB.getTerminator();
2537 if (auto *Cond = getConditionInst(TI))
2538 // TODO: Handle more than just ICmps here. We should be able to move
2539 // most instructions without side effects or memory access.
2540 if (isa<ICmpInst>(Cond) && Cond->hasOneUse()) {
2541 MadeChange = true;
2542 Cond->moveBefore(TI);
2543 }
2544 }
2545
Philip Reames85b36a82015-04-10 22:07:04 +00002546 MadeChange |= insertParsePoints(F, DT, this, ParsePointNeeded);
2547 return MadeChange;
Philip Reamesd16a9b12015-02-20 01:06:44 +00002548}
Philip Reamesdf1ef082015-04-10 22:53:14 +00002549
2550// liveness computation via standard dataflow
2551// -------------------------------------------------------------------
2552
2553// TODO: Consider using bitvectors for liveness, the set of potentially
2554// interesting values should be small and easy to pre-compute.
2555
Philip Reamesdf1ef082015-04-10 22:53:14 +00002556/// Compute the live-in set for the location rbegin starting from
2557/// the live-out set of the basic block
2558static void computeLiveInValues(BasicBlock::reverse_iterator rbegin,
2559 BasicBlock::reverse_iterator rend,
2560 DenseSet<Value *> &LiveTmp) {
2561
2562 for (BasicBlock::reverse_iterator ritr = rbegin; ritr != rend; ritr++) {
2563 Instruction *I = &*ritr;
2564
2565 // KILL/Def - Remove this definition from LiveIn
2566 LiveTmp.erase(I);
2567
2568 // Don't consider *uses* in PHI nodes, we handle their contribution to
2569 // predecessor blocks when we seed the LiveOut sets
2570 if (isa<PHINode>(I))
2571 continue;
2572
2573 // USE - Add to the LiveIn set for this instruction
2574 for (Value *V : I->operands()) {
2575 assert(!isUnhandledGCPointerType(V->getType()) &&
2576 "support for FCA unimplemented");
Philip Reames63294cb2015-04-26 19:48:03 +00002577 if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) {
2578 // The choice to exclude all things constant here is slightly subtle.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002579 // There are two independent reasons:
Philip Reames63294cb2015-04-26 19:48:03 +00002580 // - We assume that things which are constant (from LLVM's definition)
2581 // do not move at runtime. For example, the address of a global
2582 // variable is fixed, even though it's contents may not be.
2583 // - Second, we can't disallow arbitrary inttoptr constants even
2584 // if the language frontend does. Optimization passes are free to
2585 // locally exploit facts without respect to global reachability. This
2586 // can create sections of code which are dynamically unreachable and
2587 // contain just about anything. (see constants.ll in tests)
Philip Reamesdf1ef082015-04-10 22:53:14 +00002588 LiveTmp.insert(V);
2589 }
2590 }
2591 }
2592}
2593
2594static void computeLiveOutSeed(BasicBlock *BB, DenseSet<Value *> &LiveTmp) {
2595
2596 for (BasicBlock *Succ : successors(BB)) {
2597 const BasicBlock::iterator E(Succ->getFirstNonPHI());
2598 for (BasicBlock::iterator I = Succ->begin(); I != E; I++) {
2599 PHINode *Phi = cast<PHINode>(&*I);
2600 Value *V = Phi->getIncomingValueForBlock(BB);
2601 assert(!isUnhandledGCPointerType(V->getType()) &&
2602 "support for FCA unimplemented");
Philip Reames63294cb2015-04-26 19:48:03 +00002603 if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) {
Philip Reamesdf1ef082015-04-10 22:53:14 +00002604 LiveTmp.insert(V);
2605 }
2606 }
2607 }
2608}
2609
2610static DenseSet<Value *> computeKillSet(BasicBlock *BB) {
2611 DenseSet<Value *> KillSet;
2612 for (Instruction &I : *BB)
2613 if (isHandledGCPointerType(I.getType()))
2614 KillSet.insert(&I);
2615 return KillSet;
2616}
2617
Philip Reames9638ff92015-04-11 00:06:47 +00002618#ifndef NDEBUG
Philip Reamesdf1ef082015-04-10 22:53:14 +00002619/// Check that the items in 'Live' dominate 'TI'. This is used as a basic
2620/// sanity check for the liveness computation.
2621static void checkBasicSSA(DominatorTree &DT, DenseSet<Value *> &Live,
2622 TerminatorInst *TI, bool TermOkay = false) {
Philip Reamesdf1ef082015-04-10 22:53:14 +00002623 for (Value *V : Live) {
2624 if (auto *I = dyn_cast<Instruction>(V)) {
2625 // The terminator can be a member of the LiveOut set. LLVM's definition
2626 // of instruction dominance states that V does not dominate itself. As
2627 // such, we need to special case this to allow it.
2628 if (TermOkay && TI == I)
2629 continue;
2630 assert(DT.dominates(I, TI) &&
2631 "basic SSA liveness expectation violated by liveness analysis");
2632 }
2633 }
Philip Reamesdf1ef082015-04-10 22:53:14 +00002634}
2635
2636/// Check that all the liveness sets used during the computation of liveness
2637/// obey basic SSA properties. This is useful for finding cases where we miss
2638/// a def.
2639static void checkBasicSSA(DominatorTree &DT, GCPtrLivenessData &Data,
2640 BasicBlock &BB) {
2641 checkBasicSSA(DT, Data.LiveSet[&BB], BB.getTerminator());
2642 checkBasicSSA(DT, Data.LiveOut[&BB], BB.getTerminator(), true);
2643 checkBasicSSA(DT, Data.LiveIn[&BB], BB.getTerminator());
2644}
Philip Reames9638ff92015-04-11 00:06:47 +00002645#endif
Philip Reamesdf1ef082015-04-10 22:53:14 +00002646
2647static void computeLiveInValues(DominatorTree &DT, Function &F,
2648 GCPtrLivenessData &Data) {
2649
Philip Reames4d80ede2015-04-10 23:11:26 +00002650 SmallSetVector<BasicBlock *, 200> Worklist;
Philip Reamesdf1ef082015-04-10 22:53:14 +00002651 auto AddPredsToWorklist = [&](BasicBlock *BB) {
Philip Reames4d80ede2015-04-10 23:11:26 +00002652 // We use a SetVector so that we don't have duplicates in the worklist.
2653 Worklist.insert(pred_begin(BB), pred_end(BB));
Philip Reamesdf1ef082015-04-10 22:53:14 +00002654 };
2655 auto NextItem = [&]() {
2656 BasicBlock *BB = Worklist.back();
2657 Worklist.pop_back();
Philip Reamesdf1ef082015-04-10 22:53:14 +00002658 return BB;
2659 };
2660
2661 // Seed the liveness for each individual block
2662 for (BasicBlock &BB : F) {
2663 Data.KillSet[&BB] = computeKillSet(&BB);
2664 Data.LiveSet[&BB].clear();
2665 computeLiveInValues(BB.rbegin(), BB.rend(), Data.LiveSet[&BB]);
2666
2667#ifndef NDEBUG
2668 for (Value *Kill : Data.KillSet[&BB])
2669 assert(!Data.LiveSet[&BB].count(Kill) && "live set contains kill");
2670#endif
2671
2672 Data.LiveOut[&BB] = DenseSet<Value *>();
2673 computeLiveOutSeed(&BB, Data.LiveOut[&BB]);
2674 Data.LiveIn[&BB] = Data.LiveSet[&BB];
2675 set_union(Data.LiveIn[&BB], Data.LiveOut[&BB]);
2676 set_subtract(Data.LiveIn[&BB], Data.KillSet[&BB]);
2677 if (!Data.LiveIn[&BB].empty())
2678 AddPredsToWorklist(&BB);
2679 }
2680
2681 // Propagate that liveness until stable
2682 while (!Worklist.empty()) {
2683 BasicBlock *BB = NextItem();
2684
2685 // Compute our new liveout set, then exit early if it hasn't changed
2686 // despite the contribution of our successor.
2687 DenseSet<Value *> LiveOut = Data.LiveOut[BB];
2688 const auto OldLiveOutSize = LiveOut.size();
2689 for (BasicBlock *Succ : successors(BB)) {
2690 assert(Data.LiveIn.count(Succ));
2691 set_union(LiveOut, Data.LiveIn[Succ]);
2692 }
2693 // assert OutLiveOut is a subset of LiveOut
2694 if (OldLiveOutSize == LiveOut.size()) {
2695 // If the sets are the same size, then we didn't actually add anything
2696 // when unioning our successors LiveIn Thus, the LiveIn of this block
2697 // hasn't changed.
2698 continue;
2699 }
2700 Data.LiveOut[BB] = LiveOut;
2701
2702 // Apply the effects of this basic block
2703 DenseSet<Value *> LiveTmp = LiveOut;
2704 set_union(LiveTmp, Data.LiveSet[BB]);
2705 set_subtract(LiveTmp, Data.KillSet[BB]);
2706
2707 assert(Data.LiveIn.count(BB));
2708 const DenseSet<Value *> &OldLiveIn = Data.LiveIn[BB];
2709 // assert: OldLiveIn is a subset of LiveTmp
2710 if (OldLiveIn.size() != LiveTmp.size()) {
2711 Data.LiveIn[BB] = LiveTmp;
2712 AddPredsToWorklist(BB);
2713 }
2714 } // while( !worklist.empty() )
2715
2716#ifndef NDEBUG
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002717 // Sanity check our output against SSA properties. This helps catch any
Philip Reamesdf1ef082015-04-10 22:53:14 +00002718 // missing kills during the above iteration.
2719 for (BasicBlock &BB : F) {
2720 checkBasicSSA(DT, Data, BB);
2721 }
2722#endif
2723}
2724
2725static void findLiveSetAtInst(Instruction *Inst, GCPtrLivenessData &Data,
2726 StatepointLiveSetTy &Out) {
2727
2728 BasicBlock *BB = Inst->getParent();
2729
2730 // Note: The copy is intentional and required
2731 assert(Data.LiveOut.count(BB));
2732 DenseSet<Value *> LiveOut = Data.LiveOut[BB];
2733
2734 // We want to handle the statepoint itself oddly. It's
2735 // call result is not live (normal), nor are it's arguments
2736 // (unless they're used again later). This adjustment is
2737 // specifically what we need to relocate
2738 BasicBlock::reverse_iterator rend(Inst);
2739 computeLiveInValues(BB->rbegin(), rend, LiveOut);
2740 LiveOut.erase(Inst);
2741 Out.insert(LiveOut.begin(), LiveOut.end());
2742}
2743
2744static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData,
2745 const CallSite &CS,
2746 PartiallyConstructedSafepointRecord &Info) {
2747 Instruction *Inst = CS.getInstruction();
2748 StatepointLiveSetTy Updated;
2749 findLiveSetAtInst(Inst, RevisedLivenessData, Updated);
2750
2751#ifndef NDEBUG
2752 DenseSet<Value *> Bases;
2753 for (auto KVPair : Info.PointerToBase) {
2754 Bases.insert(KVPair.second);
2755 }
2756#endif
2757 // We may have base pointers which are now live that weren't before. We need
2758 // to update the PointerToBase structure to reflect this.
2759 for (auto V : Updated)
2760 if (!Info.PointerToBase.count(V)) {
2761 assert(Bases.count(V) && "can't find base for unexpected live value");
2762 Info.PointerToBase[V] = V;
2763 continue;
2764 }
2765
2766#ifndef NDEBUG
2767 for (auto V : Updated) {
2768 assert(Info.PointerToBase.count(V) &&
2769 "must be able to find base for live value");
2770 }
2771#endif
2772
2773 // Remove any stale base mappings - this can happen since our liveness is
2774 // more precise then the one inherent in the base pointer analysis
2775 DenseSet<Value *> ToErase;
2776 for (auto KVPair : Info.PointerToBase)
2777 if (!Updated.count(KVPair.first))
2778 ToErase.insert(KVPair.first);
2779 for (auto V : ToErase)
2780 Info.PointerToBase.erase(V);
2781
2782#ifndef NDEBUG
2783 for (auto KVPair : Info.PointerToBase)
2784 assert(Updated.count(KVPair.first) && "record for non-live value");
2785#endif
2786
2787 Info.liveset = Updated;
2788}