blob: 06b642c73c3a6d924d3be5d06b3d9e5b4af12be6 [file] [log] [blame]
Philip Reames47cc6732015-02-04 00:37:33 +00001//===- PlaceSafepoints.cpp - Place GC Safepoints --------------------------===//
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// Place garbage collection safepoints at appropriate locations in the IR. This
11// does not make relocation semantics or variable liveness explicit. That's
12// done by RewriteStatepointsForGC.
13//
Philip Reamesd4a912f2015-02-09 22:44:03 +000014// Terminology:
15// - A call is said to be "parseable" if there is a stack map generated for the
16// return PC of the call. A runtime can determine where values listed in the
17// deopt arguments and (after RewriteStatepointsForGC) gc arguments are located
18// on the stack when the code is suspended inside such a call. Every parse
19// point is represented by a call wrapped in an gc.statepoint intrinsic.
20// - A "poll" is an explicit check in the generated code to determine if the
21// runtime needs the generated code to cooperate by calling a helper routine
22// and thus suspending its execution at a known state. The call to the helper
23// routine will be parseable. The (gc & runtime specific) logic of a poll is
24// assumed to be provided in a function of the name "gc.safepoint_poll".
25//
26// We aim to insert polls such that running code can quickly be brought to a
27// well defined state for inspection by the collector. In the current
28// implementation, this is done via the insertion of poll sites at method entry
29// and the backedge of most loops. We try to avoid inserting more polls than
30// are neccessary to ensure a finite period between poll sites. This is not
31// because the poll itself is expensive in the generated code; it's not. Polls
32// do tend to impact the optimizer itself in negative ways; we'd like to avoid
33// perturbing the optimization of the method as much as we can.
34//
35// We also need to make most call sites parseable. The callee might execute a
36// poll (or otherwise be inspected by the GC). If so, the entire stack
37// (including the suspended frame of the current method) must be parseable.
38//
Philip Reames47cc6732015-02-04 00:37:33 +000039// This pass will insert:
Philip Reamesd4a912f2015-02-09 22:44:03 +000040// - Call parse points ("call safepoints") for any call which may need to
41// reach a safepoint during the execution of the callee function.
42// - Backedge safepoint polls and entry safepoint polls to ensure that
43// executing code reaches a safepoint poll in a finite amount of time.
Philip Reames47cc6732015-02-04 00:37:33 +000044//
Philip Reamesd4a912f2015-02-09 22:44:03 +000045// We do not currently support return statepoints, but adding them would not
46// be hard. They are not required for correctness - entry safepoints are an
47// alternative - but some GCs may prefer them. Patches welcome.
Philip Reames47cc6732015-02-04 00:37:33 +000048//
49//===----------------------------------------------------------------------===//
50
51#include "llvm/Pass.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000052#include "llvm/IR/LegacyPassManager.h"
Philip Reames47cc6732015-02-04 00:37:33 +000053#include "llvm/ADT/SetOperations.h"
Philip Reames5708cca2015-05-12 20:43:48 +000054#include "llvm/ADT/SetVector.h"
Philip Reames47cc6732015-02-04 00:37:33 +000055#include "llvm/ADT/Statistic.h"
56#include "llvm/Analysis/LoopPass.h"
57#include "llvm/Analysis/LoopInfo.h"
58#include "llvm/Analysis/ScalarEvolution.h"
59#include "llvm/Analysis/ScalarEvolutionExpressions.h"
60#include "llvm/Analysis/CFG.h"
61#include "llvm/Analysis/InstructionSimplify.h"
62#include "llvm/IR/BasicBlock.h"
63#include "llvm/IR/CallSite.h"
64#include "llvm/IR/Dominators.h"
65#include "llvm/IR/Function.h"
66#include "llvm/IR/IRBuilder.h"
67#include "llvm/IR/InstIterator.h"
68#include "llvm/IR/Instructions.h"
69#include "llvm/IR/Intrinsics.h"
70#include "llvm/IR/IntrinsicInst.h"
71#include "llvm/IR/Module.h"
72#include "llvm/IR/Statepoint.h"
73#include "llvm/IR/Value.h"
74#include "llvm/IR/Verifier.h"
75#include "llvm/Support/Debug.h"
76#include "llvm/Support/CommandLine.h"
77#include "llvm/Support/raw_ostream.h"
78#include "llvm/Transforms/Scalar.h"
79#include "llvm/Transforms/Utils/BasicBlockUtils.h"
80#include "llvm/Transforms/Utils/Cloning.h"
81#include "llvm/Transforms/Utils/Local.h"
82
83#define DEBUG_TYPE "safepoint-placement"
84STATISTIC(NumEntrySafepoints, "Number of entry safepoints inserted");
85STATISTIC(NumCallSafepoints, "Number of call safepoints inserted");
86STATISTIC(NumBackedgeSafepoints, "Number of backedge safepoints inserted");
87
88STATISTIC(CallInLoop, "Number of loops w/o safepoints due to calls in loop");
89STATISTIC(FiniteExecution, "Number of loops w/o safepoints finite execution");
90
91using namespace llvm;
92
93// Ignore oppurtunities to avoid placing safepoints on backedges, useful for
94// validation
Philip Reames1f3e5c12015-02-20 23:32:03 +000095static cl::opt<bool> AllBackedges("spp-all-backedges", cl::Hidden,
96 cl::init(false));
Philip Reames47cc6732015-02-04 00:37:33 +000097
98/// If true, do not place backedge safepoints in counted loops.
Philip Reames1f3e5c12015-02-20 23:32:03 +000099static cl::opt<bool> SkipCounted("spp-counted", cl::Hidden, cl::init(true));
Philip Reames47cc6732015-02-04 00:37:33 +0000100
101// If true, split the backedge of a loop when placing the safepoint, otherwise
102// split the latch block itself. Both are useful to support for
103// experimentation, but in practice, it looks like splitting the backedge
104// optimizes better.
Philip Reames1f3e5c12015-02-20 23:32:03 +0000105static cl::opt<bool> SplitBackedge("spp-split-backedge", cl::Hidden,
106 cl::init(false));
Philip Reames47cc6732015-02-04 00:37:33 +0000107
108// Print tracing output
Philip Reames1f3e5c12015-02-20 23:32:03 +0000109static cl::opt<bool> TraceLSP("spp-trace", cl::Hidden, cl::init(false));
Philip Reames47cc6732015-02-04 00:37:33 +0000110
111namespace {
112
Philip Reames9f129042015-05-12 21:09:36 +0000113/// An analysis pass whose purpose is to identify each of the backedges in
114/// the function which require a safepoint poll to be inserted.
115struct PlaceBackedgeSafepointsImpl : public FunctionPass {
Philip Reames47cc6732015-02-04 00:37:33 +0000116 static char ID;
117
118 /// The output of the pass - gives a list of each backedge (described by
119 /// pointing at the branch) which need a poll inserted.
120 std::vector<TerminatorInst *> PollLocations;
121
122 /// True unless we're running spp-no-calls in which case we need to disable
123 /// the call dependend placement opts.
124 bool CallSafepointsEnabled;
Philip Reames9f129042015-05-12 21:09:36 +0000125
126 ScalarEvolution *SE = nullptr;
127 DominatorTree *DT = nullptr;
128 LoopInfo *LI = nullptr;
129
Philip Reames47cc6732015-02-04 00:37:33 +0000130 PlaceBackedgeSafepointsImpl(bool CallSafepoints = false)
Philip Reames9f129042015-05-12 21:09:36 +0000131 : FunctionPass(ID), CallSafepointsEnabled(CallSafepoints) {
Philip Reames5a9685d2015-02-04 00:39:57 +0000132 initializePlaceBackedgeSafepointsImplPass(*PassRegistry::getPassRegistry());
Philip Reames47cc6732015-02-04 00:37:33 +0000133 }
134
Philip Reames9f129042015-05-12 21:09:36 +0000135 bool runOnLoop(Loop *);
136 void runOnLoopAndSubLoops(Loop *L) {
137 // Visit all the subloops
138 for (auto I = L->begin(), E = L->end(); I != E; I++)
139 runOnLoopAndSubLoops(*I);
140 runOnLoop(L);
141 }
142
143 bool runOnFunction(Function &F) {
144 SE = &getAnalysis<ScalarEvolution>();
145 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
146 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
147 for (auto I = LI->begin(), E = LI->end(); I != E; I++) {
148 runOnLoopAndSubLoops(*I);
149 }
150 return false;
151 }
152
Philip Reames47cc6732015-02-04 00:37:33 +0000153 void getAnalysisUsage(AnalysisUsage &AU) const override {
Philip Reames57bdac92015-05-12 20:56:33 +0000154 AU.addRequired<DominatorTreeWrapperPass>();
Philip Reames47cc6732015-02-04 00:37:33 +0000155 AU.addRequired<ScalarEvolution>();
Philip Reames9f129042015-05-12 21:09:36 +0000156 AU.addRequired<LoopInfoWrapperPass>();
Philip Reames47cc6732015-02-04 00:37:33 +0000157 // We no longer modify the IR at all in this pass. Thus all
158 // analysis are preserved.
159 AU.setPreservesAll();
160 }
161};
162}
163
Philip Reames1f3e5c12015-02-20 23:32:03 +0000164static cl::opt<bool> NoEntry("spp-no-entry", cl::Hidden, cl::init(false));
165static cl::opt<bool> NoCall("spp-no-call", cl::Hidden, cl::init(false));
166static cl::opt<bool> NoBackedge("spp-no-backedge", cl::Hidden, cl::init(false));
Philip Reames47cc6732015-02-04 00:37:33 +0000167
168namespace {
169struct PlaceSafepoints : public ModulePass {
170 static char ID; // Pass identification, replacement for typeid
171
Philip Reames47cc6732015-02-04 00:37:33 +0000172 PlaceSafepoints() : ModulePass(ID) {
173 initializePlaceSafepointsPass(*PassRegistry::getPassRegistry());
Philip Reames47cc6732015-02-04 00:37:33 +0000174 }
175 bool runOnModule(Module &M) override {
176 bool modified = false;
177 for (Function &F : M) {
178 modified |= runOnFunction(F);
179 }
180 return modified;
181 }
182 bool runOnFunction(Function &F);
183
184 void getAnalysisUsage(AnalysisUsage &AU) const override {
185 // We modify the graph wholesale (inlining, block insertion, etc). We
186 // preserve nothing at the moment. We could potentially preserve dom tree
187 // if that was worth doing
188 }
189};
190}
191
192// Insert a safepoint poll immediately before the given instruction. Does
193// not handle the parsability of state at the runtime call, that's the
194// callers job.
Philip Reames5a9685d2015-02-04 00:39:57 +0000195static void
196InsertSafepointPoll(DominatorTree &DT, Instruction *after,
197 std::vector<CallSite> &ParsePointsNeeded /*rval*/);
Philip Reames47cc6732015-02-04 00:37:33 +0000198
199static bool isGCLeafFunction(const CallSite &CS);
200
201static bool needsStatepoint(const CallSite &CS) {
202 if (isGCLeafFunction(CS))
203 return false;
204 if (CS.isCall()) {
205 CallInst *call = cast<CallInst>(CS.getInstruction());
206 if (call->isInlineAsm())
207 return false;
208 }
209 if (isStatepoint(CS) || isGCRelocate(CS) || isGCResult(CS)) {
210 return false;
211 }
212 return true;
213}
214
Philip Reames5a9685d2015-02-04 00:39:57 +0000215static Value *ReplaceWithStatepoint(const CallSite &CS, Pass *P);
Philip Reames47cc6732015-02-04 00:37:33 +0000216
217/// Returns true if this loop is known to contain a call safepoint which
218/// must unconditionally execute on any iteration of the loop which returns
219/// to the loop header via an edge from Pred. Returns a conservative correct
220/// answer; i.e. false is always valid.
221static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header,
222 BasicBlock *Pred,
223 DominatorTree &DT) {
224 // In general, we're looking for any cut of the graph which ensures
225 // there's a call safepoint along every edge between Header and Pred.
226 // For the moment, we look only for the 'cuts' that consist of a single call
227 // instruction in a block which is dominated by the Header and dominates the
228 // loop latch (Pred) block. Somewhat surprisingly, walking the entire chain
229 // of such dominating blocks gets substaintially more occurences than just
230 // checking the Pred and Header blocks themselves. This may be due to the
231 // density of loop exit conditions caused by range and null checks.
232 // TODO: structure this as an analysis pass, cache the result for subloops,
233 // avoid dom tree recalculations
234 assert(DT.dominates(Header, Pred) && "loop latch not dominated by header?");
235
236 BasicBlock *Current = Pred;
237 while (true) {
238 for (Instruction &I : *Current) {
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000239 if (auto CS = CallSite(&I))
Philip Reames47cc6732015-02-04 00:37:33 +0000240 // Note: Technically, needing a safepoint isn't quite the right
241 // condition here. We should instead be checking if the target method
242 // has an
243 // unconditional poll. In practice, this is only a theoretical concern
244 // since we don't have any methods with conditional-only safepoint
245 // polls.
246 if (needsStatepoint(CS))
247 return true;
248 }
249
250 if (Current == Header)
251 break;
252 Current = DT.getNode(Current)->getIDom()->getBlock();
253 }
254
255 return false;
256}
257
258/// Returns true if this loop is known to terminate in a finite number of
259/// iterations. Note that this function may return false for a loop which
260/// does actual terminate in a finite constant number of iterations due to
261/// conservatism in the analysis.
262static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE,
Philip Reames5a9685d2015-02-04 00:39:57 +0000263 BasicBlock *Pred) {
Philip Reames47cc6732015-02-04 00:37:33 +0000264 // Only used when SkipCounted is off
265 const unsigned upperTripBound = 8192;
266
267 // A conservative bound on the loop as a whole.
268 const SCEV *MaxTrips = SE->getMaxBackedgeTakenCount(L);
269 if (MaxTrips != SE->getCouldNotCompute()) {
270 if (SE->getUnsignedRange(MaxTrips).getUnsignedMax().ult(upperTripBound))
271 return true;
272 if (SkipCounted &&
273 SE->getUnsignedRange(MaxTrips).getUnsignedMax().isIntN(32))
274 return true;
275 }
276
277 // If this is a conditional branch to the header with the alternate path
278 // being outside the loop, we can ask questions about the execution frequency
279 // of the exit block.
280 if (L->isLoopExiting(Pred)) {
281 // This returns an exact expression only. TODO: We really only need an
282 // upper bound here, but SE doesn't expose that.
283 const SCEV *MaxExec = SE->getExitCount(L, Pred);
284 if (MaxExec != SE->getCouldNotCompute()) {
285 if (SE->getUnsignedRange(MaxExec).getUnsignedMax().ult(upperTripBound))
286 return true;
287 if (SkipCounted &&
288 SE->getUnsignedRange(MaxExec).getUnsignedMax().isIntN(32))
289 return true;
290 }
291 }
292
293 return /* not finite */ false;
294}
295
296static void scanOneBB(Instruction *start, Instruction *end,
Philip Reames5a9685d2015-02-04 00:39:57 +0000297 std::vector<CallInst *> &calls,
298 std::set<BasicBlock *> &seen,
299 std::vector<BasicBlock *> &worklist) {
Philip Reames47cc6732015-02-04 00:37:33 +0000300 for (BasicBlock::iterator itr(start);
301 itr != start->getParent()->end() && itr != BasicBlock::iterator(end);
302 itr++) {
303 if (CallInst *CI = dyn_cast<CallInst>(&*itr)) {
304 calls.push_back(CI);
305 }
306 // FIXME: This code does not handle invokes
307 assert(!dyn_cast<InvokeInst>(&*itr) &&
308 "support for invokes in poll code needed");
309 // Only add the successor blocks if we reach the terminator instruction
310 // without encountering end first
311 if (itr->isTerminator()) {
312 BasicBlock *BB = itr->getParent();
Philip Reamesa29de872015-02-09 22:26:11 +0000313 for (BasicBlock *Succ : successors(BB)) {
Philip Reames47cc6732015-02-04 00:37:33 +0000314 if (seen.count(Succ) == 0) {
315 worklist.push_back(Succ);
316 seen.insert(Succ);
317 }
318 }
319 }
320 }
321}
322static void scanInlinedCode(Instruction *start, Instruction *end,
Philip Reames5a9685d2015-02-04 00:39:57 +0000323 std::vector<CallInst *> &calls,
324 std::set<BasicBlock *> &seen) {
Philip Reames47cc6732015-02-04 00:37:33 +0000325 calls.clear();
326 std::vector<BasicBlock *> worklist;
327 seen.insert(start->getParent());
328 scanOneBB(start, end, calls, seen, worklist);
329 while (!worklist.empty()) {
330 BasicBlock *BB = worklist.back();
331 worklist.pop_back();
332 scanOneBB(&*BB->begin(), end, calls, seen, worklist);
333 }
334}
335
Philip Reames9f129042015-05-12 21:09:36 +0000336bool PlaceBackedgeSafepointsImpl::runOnLoop(Loop *L) {
Philip Reames5708cca2015-05-12 20:43:48 +0000337 // Loop through all loop latches (branches controlling backedges). We need
338 // to place a safepoint on every backedge (potentially).
339 // Note: In common usage, there will be only one edge due to LoopSimplify
340 // having run sometime earlier in the pipeline, but this code must be correct
341 // w.r.t. loops with multiple backedges.
Philip Reames47cc6732015-02-04 00:37:33 +0000342 BasicBlock *header = L->getHeader();
Philip Reames5708cca2015-05-12 20:43:48 +0000343 SmallVector<BasicBlock*, 16> LoopLatches;
344 L->getLoopLatches(LoopLatches);
345 for (BasicBlock *pred : LoopLatches) {
346 assert(L->contains(pred));
Philip Reames47cc6732015-02-04 00:37:33 +0000347
348 // Make a policy decision about whether this loop needs a safepoint or
349 // not. Note that this is about unburdening the optimizer in loops, not
350 // avoiding the runtime cost of the actual safepoint.
351 if (!AllBackedges) {
352 if (mustBeFiniteCountedLoop(L, SE, pred)) {
353 if (TraceLSP)
354 errs() << "skipping safepoint placement in finite loop\n";
355 FiniteExecution++;
356 continue;
357 }
358 if (CallSafepointsEnabled &&
Philip Reames57bdac92015-05-12 20:56:33 +0000359 containsUnconditionalCallSafepoint(L, header, pred, *DT)) {
Philip Reames47cc6732015-02-04 00:37:33 +0000360 // Note: This is only semantically legal since we won't do any further
361 // IPO or inlining before the actual call insertion.. If we hadn't, we
362 // might latter loose this call safepoint.
363 if (TraceLSP)
364 errs() << "skipping safepoint placement due to unconditional call\n";
365 CallInLoop++;
366 continue;
367 }
368 }
369
370 // TODO: We can create an inner loop which runs a finite number of
371 // iterations with an outer loop which contains a safepoint. This would
372 // not help runtime performance that much, but it might help our ability to
373 // optimize the inner loop.
374
Philip Reames47cc6732015-02-04 00:37:33 +0000375 // Safepoint insertion would involve creating a new basic block (as the
376 // target of the current backedge) which does the safepoint (of all live
377 // variables) and branches to the true header
378 TerminatorInst *term = pred->getTerminator();
379
380 if (TraceLSP) {
381 errs() << "[LSP] terminator instruction: ";
382 term->dump();
383 }
384
385 PollLocations.push_back(term);
386 }
387
Philip Reames5708cca2015-05-12 20:43:48 +0000388 return false;
Philip Reames47cc6732015-02-04 00:37:33 +0000389}
390
391static Instruction *findLocationForEntrySafepoint(Function &F,
392 DominatorTree &DT) {
393
394 // Conceptually, this poll needs to be on method entry, but in
395 // practice, we place it as late in the entry block as possible. We
396 // can place it as late as we want as long as it dominates all calls
397 // that can grow the stack. This, combined with backedge polls,
398 // give us all the progress guarantees we need.
399
400 // Due to the way the frontend generates IR, we may have a couple of initial
401 // basic blocks before the first bytecode. These will be single-entry
402 // single-exit blocks which conceptually are just part of the first 'real
403 // basic block'. Since we don't have deopt state until the first bytecode,
404 // walk forward until we've found the first unconditional branch or merge.
405
406 // hasNextInstruction and nextInstruction are used to iterate
407 // through a "straight line" execution sequence.
408
409 auto hasNextInstruction = [](Instruction *I) {
410 if (!I->isTerminator()) {
411 return true;
412 }
413 BasicBlock *nextBB = I->getParent()->getUniqueSuccessor();
414 return nextBB && (nextBB->getUniquePredecessor() != nullptr);
415 };
416
417 auto nextInstruction = [&hasNextInstruction](Instruction *I) {
418 assert(hasNextInstruction(I) &&
419 "first check if there is a next instruction!");
420 if (I->isTerminator()) {
421 return I->getParent()->getUniqueSuccessor()->begin();
422 } else {
423 return std::next(BasicBlock::iterator(I));
424 }
425 };
426
427 Instruction *cursor = nullptr;
428 for (cursor = F.getEntryBlock().begin(); hasNextInstruction(cursor);
429 cursor = nextInstruction(cursor)) {
430
Philip Reames47cc6732015-02-04 00:37:33 +0000431 // We need to stop going forward as soon as we see a call that can
432 // grow the stack (i.e. the call target has a non-zero frame
433 // size).
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000434 if (CallSite(cursor)) {
Philip Reames47cc6732015-02-04 00:37:33 +0000435 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(cursor)) {
436 // llvm.assume(...) are not really calls.
437 if (II->getIntrinsicID() == Intrinsic::assume) {
438 continue;
439 }
Philip Reames2e78fa42015-04-26 19:41:23 +0000440 // llvm.frameescape() intrinsic is not a real call. The intrinsic can
441 // exist only in the entry block.
442 // Inserting a statepoint before llvm.frameescape() may split the
443 // entry block, and push the intrinsic out of the entry block.
444 if (II->getIntrinsicID() == Intrinsic::frameescape) {
445 continue;
446 }
Philip Reames47cc6732015-02-04 00:37:33 +0000447 }
448 break;
449 }
450 }
451
Philip Reames5a9685d2015-02-04 00:39:57 +0000452 assert((hasNextInstruction(cursor) || cursor->isTerminator()) &&
453 "either we stopped because of a call, or because of terminator");
Philip Reames47cc6732015-02-04 00:37:33 +0000454
455 if (cursor->isTerminator()) {
456 return cursor;
457 }
458
459 BasicBlock *BB = cursor->getParent();
460 SplitBlock(BB, cursor, nullptr);
461
462 // Note: SplitBlock modifies the DT. Simply passing a Pass (which is a
463 // module pass) is not enough.
464 DT.recalculate(F);
Adam Nemete340f852015-05-06 08:18:41 +0000465
Philip Reames47cc6732015-02-04 00:37:33 +0000466 // SplitBlock updates the DT
Adam Nemete340f852015-05-06 08:18:41 +0000467 DEBUG(DT.verifyDomTree());
Philip Reames47cc6732015-02-04 00:37:33 +0000468
469 return BB->getTerminator();
470}
471
472/// Identify the list of call sites which need to be have parseable state
473static void findCallSafepoints(Function &F,
474 std::vector<CallSite> &Found /*rval*/) {
475 assert(Found.empty() && "must be empty!");
Philip Reamesa29de872015-02-09 22:26:11 +0000476 for (Instruction &I : inst_range(F)) {
477 Instruction *inst = &I;
Philip Reames47cc6732015-02-04 00:37:33 +0000478 if (isa<CallInst>(inst) || isa<InvokeInst>(inst)) {
479 CallSite CS(inst);
480
481 // No safepoint needed or wanted
482 if (!needsStatepoint(CS)) {
483 continue;
484 }
485
486 Found.push_back(CS);
487 }
488 }
489}
490
491/// Implement a unique function which doesn't require we sort the input
492/// vector. Doing so has the effect of changing the output of a couple of
493/// tests in ways which make them less useful in testing fused safepoints.
494template <typename T> static void unique_unsorted(std::vector<T> &vec) {
495 std::set<T> seen;
496 std::vector<T> tmp;
497 vec.reserve(vec.size());
498 std::swap(tmp, vec);
499 for (auto V : tmp) {
500 if (seen.insert(V).second) {
501 vec.push_back(V);
502 }
503 }
504}
505
Philip Reamesb1ed02f2015-02-09 21:48:05 +0000506static std::string GCSafepointPollName("gc.safepoint_poll");
507
508static bool isGCSafepointPoll(Function &F) {
509 return F.getName().equals(GCSafepointPollName);
510}
511
Philip Reames0b1b3872015-02-21 00:09:09 +0000512/// Returns true if this function should be rewritten to include safepoint
513/// polls and parseable call sites. The main point of this function is to be
514/// an extension point for custom logic.
515static bool shouldRewriteFunction(Function &F) {
516 // TODO: This should check the GCStrategy
517 if (F.hasGC()) {
518 const std::string StatepointExampleName("statepoint-example");
519 return StatepointExampleName == F.getGC();
520 } else
521 return false;
522}
523
524// TODO: These should become properties of the GCStrategy, possibly with
525// command line overrides.
526static bool enableEntrySafepoints(Function &F) { return !NoEntry; }
527static bool enableBackedgeSafepoints(Function &F) { return !NoBackedge; }
528static bool enableCallSafepoints(Function &F) { return !NoCall; }
529
Igor Laevsky5e23e162015-05-08 11:59:09 +0000530// Normalize basic block to make it ready to be target of invoke statepoint.
531// Ensure that 'BB' does not have phi nodes. It may require spliting it.
532static BasicBlock *normalizeForInvokeSafepoint(BasicBlock *BB,
533 BasicBlock *InvokeParent) {
534 BasicBlock *ret = BB;
535
536 if (!BB->getUniquePredecessor()) {
537 ret = SplitBlockPredecessors(BB, InvokeParent, "");
538 }
539
540 // Now that 'ret' has unique predecessor we can safely remove all phi nodes
541 // from it
542 FoldSingleEntryPHINodes(ret);
543 assert(!isa<PHINode>(ret->begin()));
544
545 return ret;
546}
Philip Reames0b1b3872015-02-21 00:09:09 +0000547
Philip Reames47cc6732015-02-04 00:37:33 +0000548bool PlaceSafepoints::runOnFunction(Function &F) {
549 if (F.isDeclaration() || F.empty()) {
550 // This is a declaration, nothing to do. Must exit early to avoid crash in
551 // dom tree calculation
552 return false;
553 }
554
Philip Reames7e7dc3e2015-02-10 00:04:53 +0000555 if (isGCSafepointPoll(F)) {
556 // Given we're inlining this inside of safepoint poll insertion, this
557 // doesn't make any sense. Note that we do make any contained calls
558 // parseable after we inline a poll.
559 return false;
560 }
561
Philip Reames0b1b3872015-02-21 00:09:09 +0000562 if (!shouldRewriteFunction(F))
563 return false;
564
Philip Reames47cc6732015-02-04 00:37:33 +0000565 bool modified = false;
566
567 // In various bits below, we rely on the fact that uses are reachable from
568 // defs. When there are basic blocks unreachable from the entry, dominance
569 // and reachablity queries return non-sensical results. Thus, we preprocess
570 // the function to ensure these properties hold.
571 modified |= removeUnreachableBlocks(F);
572
573 // STEP 1 - Insert the safepoint polling locations. We do not need to
574 // actually insert parse points yet. That will be done for all polls and
575 // calls in a single pass.
576
577 // Note: With the migration, we need to recompute this for each 'pass'. Once
578 // we merge these, we'll do it once before the analysis
579 DominatorTree DT;
580
581 std::vector<CallSite> ParsePointNeeded;
582
Philip Reames0b1b3872015-02-21 00:09:09 +0000583 if (enableBackedgeSafepoints(F)) {
Philip Reames47cc6732015-02-04 00:37:33 +0000584 // Construct a pass manager to run the LoopPass backedge logic. We
585 // need the pass manager to handle scheduling all the loop passes
586 // appropriately. Doing this by hand is painful and just not worth messing
587 // with for the moment.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000588 legacy::FunctionPassManager FPM(F.getParent());
Philip Reames0b1b3872015-02-21 00:09:09 +0000589 bool CanAssumeCallSafepoints = enableCallSafepoints(F);
Philip Reames47cc6732015-02-04 00:37:33 +0000590 PlaceBackedgeSafepointsImpl *PBS =
Philip Reamesb1ed02f2015-02-09 21:48:05 +0000591 new PlaceBackedgeSafepointsImpl(CanAssumeCallSafepoints);
Philip Reames47cc6732015-02-04 00:37:33 +0000592 FPM.add(PBS);
Philip Reames47cc6732015-02-04 00:37:33 +0000593 FPM.run(F);
594
595 // We preserve dominance information when inserting the poll, otherwise
596 // we'd have to recalculate this on every insert
597 DT.recalculate(F);
598
Philip Reames5708cca2015-05-12 20:43:48 +0000599 auto &PollLocations = PBS->PollLocations;
600
601 auto OrderByBBName = [](Instruction *a, Instruction *b) {
602 return a->getParent()->getName() < b->getParent()->getName();
603 };
604 // We need the order of list to be stable so that naming ends up stable
605 // when we split edges. This makes test cases much easier to write.
606 std::sort(PollLocations.begin(), PollLocations.end(), OrderByBBName);
607
608 // We can sometimes end up with duplicate poll locations. This happens if
609 // a single loop is visited more than once. The fact this happens seems
610 // wrong, but it does happen for the split-backedge.ll test case.
611 PollLocations.erase(std::unique(PollLocations.begin(),
612 PollLocations.end()),
613 PollLocations.end());
614
Philip Reames47cc6732015-02-04 00:37:33 +0000615 // Insert a poll at each point the analysis pass identified
Philip Reames5708cca2015-05-12 20:43:48 +0000616 for (size_t i = 0; i < PollLocations.size(); i++) {
Philip Reames47cc6732015-02-04 00:37:33 +0000617 // We are inserting a poll, the function is modified
618 modified = true;
619
620 // The poll location must be the terminator of a loop latch block.
Philip Reames5708cca2015-05-12 20:43:48 +0000621 TerminatorInst *Term = PollLocations[i];
Philip Reames47cc6732015-02-04 00:37:33 +0000622
623 std::vector<CallSite> ParsePoints;
624 if (SplitBackedge) {
625 // Split the backedge of the loop and insert the poll within that new
626 // basic block. This creates a loop with two latches per original
627 // latch (which is non-ideal), but this appears to be easier to
628 // optimize in practice than inserting the poll immediately before the
629 // latch test.
630
631 // Since this is a latch, at least one of the successors must dominate
632 // it. Its possible that we have a) duplicate edges to the same header
633 // and b) edges to distinct loop headers. We need to insert pools on
Philip Reames5708cca2015-05-12 20:43:48 +0000634 // each.
635 SetVector<BasicBlock *> Headers;
Philip Reames47cc6732015-02-04 00:37:33 +0000636 for (unsigned i = 0; i < Term->getNumSuccessors(); i++) {
637 BasicBlock *Succ = Term->getSuccessor(i);
638 if (DT.dominates(Succ, Term->getParent())) {
639 Headers.insert(Succ);
640 }
641 }
642 assert(!Headers.empty() && "poll location is not a loop latch?");
643
644 // The split loop structure here is so that we only need to recalculate
645 // the dominator tree once. Alternatively, we could just keep it up to
646 // date and use a more natural merged loop.
Philip Reames5708cca2015-05-12 20:43:48 +0000647 SetVector<BasicBlock *> SplitBackedges;
Philip Reames47cc6732015-02-04 00:37:33 +0000648 for (BasicBlock *Header : Headers) {
649 BasicBlock *NewBB = SplitEdge(Term->getParent(), Header, nullptr);
650 SplitBackedges.insert(NewBB);
651 }
652 DT.recalculate(F);
653 for (BasicBlock *NewBB : SplitBackedges) {
Philip Reames5708cca2015-05-12 20:43:48 +0000654 std::vector<CallSite> RuntimeCalls;
655 InsertSafepointPoll(DT, NewBB->getTerminator(), RuntimeCalls);
Philip Reames47cc6732015-02-04 00:37:33 +0000656 NumBackedgeSafepoints++;
Philip Reames5708cca2015-05-12 20:43:48 +0000657 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
658 RuntimeCalls.end());
Philip Reames47cc6732015-02-04 00:37:33 +0000659 }
660
661 } else {
662 // Split the latch block itself, right before the terminator.
Philip Reames5708cca2015-05-12 20:43:48 +0000663 std::vector<CallSite> RuntimeCalls;
664 InsertSafepointPoll(DT, Term, RuntimeCalls);
Philip Reames47cc6732015-02-04 00:37:33 +0000665 NumBackedgeSafepoints++;
Philip Reames5708cca2015-05-12 20:43:48 +0000666 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
667 RuntimeCalls.end());
Philip Reames47cc6732015-02-04 00:37:33 +0000668 }
669
Philip Reames47cc6732015-02-04 00:37:33 +0000670 // Record the parse points for later use
671 ParsePointNeeded.insert(ParsePointNeeded.end(), ParsePoints.begin(),
672 ParsePoints.end());
673 }
674 }
675
Philip Reames0b1b3872015-02-21 00:09:09 +0000676 if (enableEntrySafepoints(F)) {
Philip Reames47cc6732015-02-04 00:37:33 +0000677 DT.recalculate(F);
678 Instruction *term = findLocationForEntrySafepoint(F, DT);
679 if (!term) {
680 // policy choice not to insert?
681 } else {
682 std::vector<CallSite> RuntimeCalls;
683 InsertSafepointPoll(DT, term, RuntimeCalls);
684 modified = true;
685 NumEntrySafepoints++;
686 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
687 RuntimeCalls.end());
688 }
689 }
690
Philip Reames0b1b3872015-02-21 00:09:09 +0000691 if (enableCallSafepoints(F)) {
Philip Reames47cc6732015-02-04 00:37:33 +0000692 DT.recalculate(F);
693 std::vector<CallSite> Calls;
694 findCallSafepoints(F, Calls);
695 NumCallSafepoints += Calls.size();
Philip Reames5a9685d2015-02-04 00:39:57 +0000696 ParsePointNeeded.insert(ParsePointNeeded.end(), Calls.begin(), Calls.end());
Philip Reames47cc6732015-02-04 00:37:33 +0000697 }
698
699 // Unique the vectors since we can end up with duplicates if we scan the call
700 // site for call safepoints after we add it for entry or backedge. The
701 // only reason we need tracking at all is that some functions might have
702 // polls but not call safepoints and thus we might miss marking the runtime
703 // calls for the polls. (This is useful in test cases!)
704 unique_unsorted(ParsePointNeeded);
705
706 // Any parse point (no matter what source) will be handled here
707 DT.recalculate(F); // Needed?
708
709 // We're about to start modifying the function
710 if (!ParsePointNeeded.empty())
711 modified = true;
712
713 // Now run through and insert the safepoints, but do _NOT_ update or remove
714 // any existing uses. We have references to live variables that need to
715 // survive to the last iteration of this loop.
716 std::vector<Value *> Results;
717 Results.reserve(ParsePointNeeded.size());
718 for (size_t i = 0; i < ParsePointNeeded.size(); i++) {
719 CallSite &CS = ParsePointNeeded[i];
Igor Laevsky5e23e162015-05-08 11:59:09 +0000720
721 // For invoke statepoints we need to remove all phi nodes at the normal
722 // destination block.
723 // Reason for this is that we can place gc_result only after last phi node
724 // in basic block. We will get malformed code after RAUW for the
725 // gc_result if one of this phi nodes uses result from the invoke.
726 if (InvokeInst *Invoke = dyn_cast<InvokeInst>(CS.getInstruction())) {
727 normalizeForInvokeSafepoint(Invoke->getNormalDest(),
728 Invoke->getParent());
729 }
730
Philip Reames47cc6732015-02-04 00:37:33 +0000731 Value *GCResult = ReplaceWithStatepoint(CS, nullptr);
732 Results.push_back(GCResult);
733 }
734 assert(Results.size() == ParsePointNeeded.size());
735
736 // Adjust all users of the old call sites to use the new ones instead
737 for (size_t i = 0; i < ParsePointNeeded.size(); i++) {
738 CallSite &CS = ParsePointNeeded[i];
739 Value *GCResult = Results[i];
740 if (GCResult) {
Igor Laevsky5e23e162015-05-08 11:59:09 +0000741 // Can not RAUW for the gc result in case of phi nodes preset.
742 assert(!isa<PHINode>(cast<Instruction>(GCResult)->getParent()->begin()));
Philip Reames47cc6732015-02-04 00:37:33 +0000743
744 // Replace all uses with the new call
745 CS.getInstruction()->replaceAllUsesWith(GCResult);
746 }
747
748 // Now that we've handled all uses, remove the original call itself
749 // Note: The insert point can't be the deleted instruction!
750 CS.getInstruction()->eraseFromParent();
751 }
752 return modified;
753}
754
755char PlaceBackedgeSafepointsImpl::ID = 0;
756char PlaceSafepoints::ID = 0;
757
Philip Reames5a9685d2015-02-04 00:39:57 +0000758ModulePass *llvm::createPlaceSafepointsPass() { return new PlaceSafepoints(); }
Philip Reames47cc6732015-02-04 00:37:33 +0000759
760INITIALIZE_PASS_BEGIN(PlaceBackedgeSafepointsImpl,
761 "place-backedge-safepoints-impl",
762 "Place Backedge Safepoints", false, false)
763INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
Philip Reames57bdac92015-05-12 20:56:33 +0000764INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Philip Reames9f129042015-05-12 21:09:36 +0000765INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Philip Reames47cc6732015-02-04 00:37:33 +0000766INITIALIZE_PASS_END(PlaceBackedgeSafepointsImpl,
767 "place-backedge-safepoints-impl",
768 "Place Backedge Safepoints", false, false)
769
770INITIALIZE_PASS_BEGIN(PlaceSafepoints, "place-safepoints", "Place Safepoints",
771 false, false)
772INITIALIZE_PASS_END(PlaceSafepoints, "place-safepoints", "Place Safepoints",
773 false, false)
774
775static bool isGCLeafFunction(const CallSite &CS) {
776 Instruction *inst = CS.getInstruction();
Aaron Ballman94d4d332015-02-05 13:52:42 +0000777 if (isa<IntrinsicInst>(inst)) {
Aaron Ballman1b072b32015-02-05 13:40:04 +0000778 // Most LLVM intrinsics are things which can never take a safepoint.
779 // As a result, we don't need to have the stack parsable at the
780 // callsite. This is a highly useful optimization since intrinsic
781 // calls are fairly prevelent, particularly in debug builds.
782 return true;
Philip Reames47cc6732015-02-04 00:37:33 +0000783 }
784
785 // If this function is marked explicitly as a leaf call, we don't need to
786 // place a safepoint of it. In fact, for correctness we *can't* in many
787 // cases. Note: Indirect calls return Null for the called function,
788 // these obviously aren't runtime functions with attributes
789 // TODO: Support attributes on the call site as well.
790 const Function *F = CS.getCalledFunction();
791 bool isLeaf =
792 F &&
793 F->getFnAttribute("gc-leaf-function").getValueAsString().equals("true");
794 if (isLeaf) {
795 return true;
796 }
797 return false;
798}
799
Philip Reames5a9685d2015-02-04 00:39:57 +0000800static void
801InsertSafepointPoll(DominatorTree &DT, Instruction *term,
802 std::vector<CallSite> &ParsePointsNeeded /*rval*/) {
Philip Reames47cc6732015-02-04 00:37:33 +0000803 Module *M = term->getParent()->getParent()->getParent();
804 assert(M);
805
806 // Inline the safepoint poll implementation - this will get all the branch,
807 // control flow, etc.. Most importantly, it will introduce the actual slow
808 // path call - where we need to insert a safepoint (parsepoint).
809 FunctionType *ftype =
810 FunctionType::get(Type::getVoidTy(M->getContext()), false);
811 assert(ftype && "null?");
812 // Note: This cast can fail if there's a function of the same name with a
813 // different type inserted previously
814 Function *F =
815 dyn_cast<Function>(M->getOrInsertFunction("gc.safepoint_poll", ftype));
Ramkumar Ramachandra3edf74f2015-02-09 23:02:10 +0000816 assert(F && "void @gc.safepoint_poll() must be defined");
817 assert(!F->empty() && "gc.safepoint_poll must be a non-empty function");
Philip Reames47cc6732015-02-04 00:37:33 +0000818 CallInst *poll = CallInst::Create(F, "", term);
819
820 // Record some information about the call site we're replacing
821 BasicBlock *OrigBB = term->getParent();
822 BasicBlock::iterator before(poll), after(poll);
823 bool isBegin(false);
824 if (before == term->getParent()->begin()) {
825 isBegin = true;
826 } else {
827 before--;
828 }
829 after++;
830 assert(after != poll->getParent()->end() && "must have successor");
831 assert(DT.dominates(before, after) && "trivially true");
832
833 // do the actual inlining
834 InlineFunctionInfo IFI;
835 bool inlineStatus = InlineFunction(poll, IFI);
836 assert(inlineStatus && "inline must succeed");
Philip Reames72634d62015-02-04 05:11:20 +0000837 (void)inlineStatus; // suppress warning in release-asserts
Philip Reames47cc6732015-02-04 00:37:33 +0000838
839 // Check post conditions
840 assert(IFI.StaticAllocas.empty() && "can't have allocs");
841
842 std::vector<CallInst *> calls; // new calls
843 std::set<BasicBlock *> BBs; // new BBs + insertee
844 // Include only the newly inserted instructions, Note: begin may not be valid
845 // if we inserted to the beginning of the basic block
846 BasicBlock::iterator start;
847 if (isBegin) {
848 start = OrigBB->begin();
849 } else {
850 start = before;
851 start++;
852 }
853
854 // If your poll function includes an unreachable at the end, that's not
855 // valid. Bugpoint likes to create this, so check for it.
856 assert(isPotentiallyReachable(&*start, &*after, nullptr, nullptr) &&
857 "malformed poll function");
858
859 scanInlinedCode(&*(start), &*(after), calls, BBs);
860
861 // Recompute since we've invalidated cached data. Conceptually we
862 // shouldn't need to do this, but implementation wise we appear to. Needed
863 // so we can insert safepoints correctly.
864 // TODO: update more cheaply
865 DT.recalculate(*after->getParent()->getParent());
866
867 assert(!calls.empty() && "slow path not found for safepoint poll");
868
869 // Record the fact we need a parsable state at the runtime call contained in
870 // the poll function. This is required so that the runtime knows how to
871 // parse the last frame when we actually take the safepoint (i.e. execute
872 // the slow path)
873 assert(ParsePointsNeeded.empty());
874 for (size_t i = 0; i < calls.size(); i++) {
875
876 // No safepoint needed or wanted
877 if (!needsStatepoint(calls[i])) {
878 continue;
879 }
880
881 // These are likely runtime calls. Should we assert that via calling
882 // convention or something?
883 ParsePointsNeeded.push_back(CallSite(calls[i]));
884 }
885 assert(ParsePointsNeeded.size() <= calls.size());
886}
887
Philip Reames47cc6732015-02-04 00:37:33 +0000888/// Replaces the given call site (Call or Invoke) with a gc.statepoint
889/// intrinsic with an empty deoptimization arguments list. This does
890/// NOT do explicit relocation for GC support.
891static Value *ReplaceWithStatepoint(const CallSite &CS, /* to replace */
892 Pass *P) {
NAKAMURA Takumi2a5bd542015-05-07 10:18:46 +0000893 assert(CS.getInstruction()->getParent()->getParent()->getParent() &&
894 "must be set");
Philip Reames47cc6732015-02-04 00:37:33 +0000895
896 // TODO: technically, a pass is not allowed to get functions from within a
897 // function pass since it might trigger a new function addition. Refactor
898 // this logic out to the initialization of the pass. Doesn't appear to
899 // matter in practice.
900
Philip Reames47cc6732015-02-04 00:37:33 +0000901 // Then go ahead and use the builder do actually do the inserts. We insert
902 // immediately before the previous instruction under the assumption that all
903 // arguments will be available here. We can't insert afterwards since we may
904 // be replacing a terminator.
Sanjoy Das93abd812015-05-06 23:53:19 +0000905 IRBuilder<> Builder(CS.getInstruction());
Philip Reamesb1ed02f2015-02-09 21:48:05 +0000906
907 // Note: The gc args are not filled in at this time, that's handled by
908 // RewriteStatepointsForGC (which is currently under review).
909
Philip Reames47cc6732015-02-04 00:37:33 +0000910 // Create the statepoint given all the arguments
Sanjoy Das93abd812015-05-06 23:53:19 +0000911 Instruction *Token = nullptr;
Sanjoy Dasabf15602015-05-06 23:53:21 +0000912 AttributeSet OriginalAttrs;
913
Philip Reames47cc6732015-02-04 00:37:33 +0000914 if (CS.isCall()) {
Sanjoy Das93abd812015-05-06 23:53:19 +0000915 CallInst *ToReplace = cast<CallInst>(CS.getInstruction());
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000916 CallInst *Call = Builder.CreateGCStatepointCall(
Ramkumar Ramachandra3408f3e2015-02-26 00:35:56 +0000917 CS.getCalledValue(), makeArrayRef(CS.arg_begin(), CS.arg_end()), None,
918 None, "safepoint_token");
Sanjoy Das93abd812015-05-06 23:53:19 +0000919 Call->setTailCall(ToReplace->isTailCall());
920 Call->setCallingConv(ToReplace->getCallingConv());
Philip Reames47cc6732015-02-04 00:37:33 +0000921
922 // Before we have to worry about GC semantics, all attributes are legal
Philip Reames47cc6732015-02-04 00:37:33 +0000923 // TODO: handle param attributes
Sanjoy Dasabf15602015-05-06 23:53:21 +0000924 OriginalAttrs = ToReplace->getAttributes();
925
926 // In case if we can handle this set of attributes - set up function
927 // attributes directly on statepoint and return attributes later for
928 // gc_result intrinsic.
929 Call->setAttributes(OriginalAttrs.getFnAttributes());
Philip Reames47cc6732015-02-04 00:37:33 +0000930
Sanjoy Das93abd812015-05-06 23:53:19 +0000931 Token = Call;
Philip Reames47cc6732015-02-04 00:37:33 +0000932
933 // Put the following gc_result and gc_relocate calls immediately after the
Sanjoy Dasabf15602015-05-06 23:53:21 +0000934 // the old call (which we're about to delete).
935 assert(ToReplace->getNextNode() && "not a terminator, must have next");
936 Builder.SetInsertPoint(ToReplace->getNextNode());
937 Builder.SetCurrentDebugLocation(ToReplace->getNextNode()->getDebugLoc());
Philip Reames47cc6732015-02-04 00:37:33 +0000938 } else if (CS.isInvoke()) {
Sanjoy Das93abd812015-05-06 23:53:19 +0000939 InvokeInst *ToReplace = cast<InvokeInst>(CS.getInstruction());
Philip Reames47cc6732015-02-04 00:37:33 +0000940
941 // Insert the new invoke into the old block. We'll remove the old one in a
942 // moment at which point this will become the new terminator for the
943 // original block.
Sanjoy Das93abd812015-05-06 23:53:19 +0000944 Builder.SetInsertPoint(ToReplace->getParent());
945 InvokeInst *Invoke = Builder.CreateGCStatepointInvoke(
946 CS.getCalledValue(), ToReplace->getNormalDest(),
947 ToReplace->getUnwindDest(), makeArrayRef(CS.arg_begin(), CS.arg_end()),
Sanjoy Dasabe1c682015-05-06 23:53:09 +0000948 Builder.getInt32(0), None, "safepoint_token");
Philip Reames47cc6732015-02-04 00:37:33 +0000949
950 // Currently we will fail on parameter attributes and on certain
951 // function attributes.
Sanjoy Dasabf15602015-05-06 23:53:21 +0000952 OriginalAttrs = ToReplace->getAttributes();
953
954 // In case if we can handle this set of attributes - set up function
955 // attributes directly on statepoint and return attributes later for
956 // gc_result intrinsic.
Sanjoy Das93abd812015-05-06 23:53:19 +0000957 Invoke->setAttributes(OriginalAttrs.getFnAttributes());
Philip Reames47cc6732015-02-04 00:37:33 +0000958
Sanjoy Das93abd812015-05-06 23:53:19 +0000959 Token = Invoke;
Philip Reames47cc6732015-02-04 00:37:33 +0000960
961 // We'll insert the gc.result into the normal block
Igor Laevsky5e23e162015-05-08 11:59:09 +0000962 BasicBlock *NormalDest = ToReplace->getNormalDest();
963 // Can not insert gc.result in case of phi nodes preset.
964 // Should have removed this cases prior to runnning this function
965 assert(!isa<PHINode>(NormalDest->begin()));
966 Instruction *IP = &*(NormalDest->getFirstInsertionPt());
967 Builder.SetInsertPoint(IP);
Philip Reames47cc6732015-02-04 00:37:33 +0000968 } else {
969 llvm_unreachable("unexpect type of CallSite");
970 }
Sanjoy Das93abd812015-05-06 23:53:19 +0000971 assert(Token);
Philip Reames47cc6732015-02-04 00:37:33 +0000972
973 // Handle the return value of the original call - update all uses to use a
974 // gc_result hanging off the statepoint node we just inserted
975
976 // Only add the gc_result iff there is actually a used result
977 if (!CS.getType()->isVoidTy() && !CS.getInstruction()->use_empty()) {
Sanjoy Das93abd812015-05-06 23:53:19 +0000978 std::string TakenName =
979 CS.getInstruction()->hasName() ? CS.getInstruction()->getName() : "";
980 CallInst *GCResult = Builder.CreateGCResult(Token, CS.getType(), TakenName);
Sanjoy Dasabf15602015-05-06 23:53:21 +0000981 GCResult->setAttributes(OriginalAttrs.getRetAttributes());
Sanjoy Das93abd812015-05-06 23:53:19 +0000982 return GCResult;
Philip Reames47cc6732015-02-04 00:37:33 +0000983 } else {
984 // No return value for the call.
985 return nullptr;
986 }
987}