blob: 2e31c282ae06d68ade07db739a0a79a7d6503931 [file] [log] [blame]
Philip Reames1a1bdb22014-12-02 18:50:36 +00001//===-- StatepointLowering.cpp - SDAGBuilder's statepoint code -----------===//
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// This file includes support code use by SelectionDAGBuilder when lowering a
11// statepoint sequence in SelectionDAG IR.
12//
13//===----------------------------------------------------------------------===//
14
15#include "StatepointLowering.h"
16#include "SelectionDAGBuilder.h"
17#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/CodeGen/FunctionLoweringInfo.h"
20#include "llvm/CodeGen/SelectionDAG.h"
21#include "llvm/CodeGen/StackMaps.h"
22#include "llvm/IR/CallingConv.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Intrinsics.h"
26#include "llvm/IR/Statepoint.h"
27#include "llvm/Target/TargetLowering.h"
28#include <algorithm>
29using namespace llvm;
30
31#define DEBUG_TYPE "statepoint-lowering"
32
33STATISTIC(NumSlotsAllocatedForStatepoints,
34 "Number of stack slots allocated for statepoints");
35STATISTIC(NumOfStatepoints, "Number of statepoint nodes encountered");
36STATISTIC(StatepointMaxSlotsRequired,
37 "Maximum number of stack slots required for a singe statepoint");
38
39void
40StatepointLoweringState::startNewStatepoint(SelectionDAGBuilder &Builder) {
41 // Consistency check
42 assert(PendingGCRelocateCalls.empty() &&
43 "Trying to visit statepoint before finished processing previous one");
44 Locations.clear();
45 RelocLocations.clear();
46 NextSlotToAllocate = 0;
47 // Need to resize this on each safepoint - we need the two to stay in
48 // sync and the clear patterns of a SelectionDAGBuilder have no relation
49 // to FunctionLoweringInfo.
50 AllocatedStackSlots.resize(Builder.FuncInfo.StatepointStackSlots.size());
51 for (size_t i = 0; i < AllocatedStackSlots.size(); i++) {
52 AllocatedStackSlots[i] = false;
53 }
54}
55void StatepointLoweringState::clear() {
56 Locations.clear();
57 RelocLocations.clear();
58 AllocatedStackSlots.clear();
59 assert(PendingGCRelocateCalls.empty() &&
60 "cleared before statepoint sequence completed");
61}
62
63SDValue
64StatepointLoweringState::allocateStackSlot(EVT ValueType,
65 SelectionDAGBuilder &Builder) {
66
67 NumSlotsAllocatedForStatepoints++;
68
69 // The basic scheme here is to first look for a previously created stack slot
70 // which is not in use (accounting for the fact arbitrary slots may already
71 // be reserved), or to create a new stack slot and use it.
72
73 // If this doesn't succeed in 40000 iterations, something is seriously wrong
74 for (int i = 0; i < 40000; i++) {
75 assert(Builder.FuncInfo.StatepointStackSlots.size() ==
76 AllocatedStackSlots.size() &&
77 "broken invariant");
78 const size_t NumSlots = AllocatedStackSlots.size();
79 assert(NextSlotToAllocate <= NumSlots && "broken invariant");
80
81 if (NextSlotToAllocate >= NumSlots) {
82 assert(NextSlotToAllocate == NumSlots);
83 // record stats
84 if (NumSlots + 1 > StatepointMaxSlotsRequired) {
85 StatepointMaxSlotsRequired = NumSlots + 1;
86 }
87
88 SDValue SpillSlot = Builder.DAG.CreateStackTemporary(ValueType);
89 const unsigned FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
90 Builder.FuncInfo.StatepointStackSlots.push_back(FI);
91 AllocatedStackSlots.push_back(true);
92 return SpillSlot;
93 }
94 if (!AllocatedStackSlots[NextSlotToAllocate]) {
95 const int FI = Builder.FuncInfo.StatepointStackSlots[NextSlotToAllocate];
96 AllocatedStackSlots[NextSlotToAllocate] = true;
97 return Builder.DAG.getFrameIndex(FI, ValueType);
98 }
99 // Note: We deliberately choose to advance this only on the failing path.
100 // Doing so on the suceeding path involes a bit of complexity that caused a
101 // minor bug previously. Unless performance shows this matters, please
102 // keep this code as simple as possible.
103 NextSlotToAllocate++;
104 }
105 llvm_unreachable("infinite loop?");
106}
107
108/// Try to find existing copies of the incoming values in stack slots used for
109/// statepoint spilling. If we can find a spill slot for the incoming value,
110/// mark that slot as allocated, and reuse the same slot for this safepoint.
111/// This helps to avoid series of loads and stores that only serve to resuffle
112/// values on the stack between calls.
113static void reservePreviousStackSlotForValue(SDValue Incoming,
114 SelectionDAGBuilder &Builder) {
115
116 if (isa<ConstantSDNode>(Incoming) || isa<FrameIndexSDNode>(Incoming)) {
117 // We won't need to spill this, so no need to check for previously
118 // allocated stack slots
119 return;
120 }
121
122 SDValue Loc = Builder.StatepointLowering.getLocation(Incoming);
123 if (Loc.getNode()) {
124 // duplicates in input
125 return;
126 }
127
128 // Search back for the load from a stack slot pattern to find the original
129 // slot we allocated for this value. We could extend this to deal with
130 // simple modification patterns, but simple dealing with trivial load/store
131 // sequences helps a lot already.
132 if (LoadSDNode *Load = dyn_cast<LoadSDNode>(Incoming)) {
133 if (auto *FI = dyn_cast<FrameIndexSDNode>(Load->getBasePtr())) {
134 const int Index = FI->getIndex();
135 auto Itr = std::find(Builder.FuncInfo.StatepointStackSlots.begin(),
136 Builder.FuncInfo.StatepointStackSlots.end(), Index);
137 if (Itr == Builder.FuncInfo.StatepointStackSlots.end()) {
138 // not one of the lowering stack slots, can't reuse!
139 // TODO: Actually, we probably could reuse the stack slot if the value
140 // hasn't changed at all, but we'd need to look for intervening writes
141 return;
142 } else {
143 // This is one of our dedicated lowering slots
144 const int Offset =
145 std::distance(Builder.FuncInfo.StatepointStackSlots.begin(), Itr);
146 if (Builder.StatepointLowering.isStackSlotAllocated(Offset)) {
147 // stack slot already assigned to someone else, can't use it!
148 // TODO: currently we reserve space for gc arguments after doing
149 // normal allocation for deopt arguments. We should reserve for
150 // _all_ deopt and gc arguments, then start allocating. This
151 // will prevent some moves being inserted when vm state changes,
152 // but gc state doesn't between two calls.
153 return;
154 }
155 // Reserve this stack slot
156 Builder.StatepointLowering.reserveStackSlot(Offset);
157 }
158
159 // Cache this slot so we find it when going through the normal
160 // assignment loop.
161 SDValue Loc =
162 Builder.DAG.getTargetFrameIndex(Index, Incoming.getValueType());
163
164 Builder.StatepointLowering.setLocation(Incoming, Loc);
165 }
166 }
167
168 // TODO: handle case where a reloaded value flows through a phi to
169 // another safepoint. e.g.
170 // bb1:
171 // a' = relocated...
172 // bb2: % pred: bb1, bb3, bb4, etc.
173 // a_phi = phi(a', ...)
174 // statepoint ... a_phi
175 // NOTE: This will require reasoning about cross basic block values. This is
176 // decidedly non trivial and this might not be the right place to do it. We
177 // don't really have the information we need here...
178
179 // TODO: handle simple updates. If a value is modified and the original
180 // value is no longer live, it would be nice to put the modified value in the
181 // same slot. This allows folding of the memory accesses for some
182 // instructions types (like an increment).
183 // statepoint (i)
184 // i1 = i+1
185 // statepoint (i1)
186}
187
188/// Remove any duplicate (as SDValues) from the derived pointer pairs. This
189/// is not required for correctness. It's purpose is to reduce the size of
190/// StackMap section. It has no effect on the number of spill slots required
191/// or the actual lowering.
192static void removeDuplicatesGCPtrs(SmallVectorImpl<const Value *> &Bases,
193 SmallVectorImpl<const Value *> &Ptrs,
194 SmallVectorImpl<const Value *> &Relocs,
195 SelectionDAGBuilder &Builder) {
196
197 // This is horribly ineffecient, but I don't care right now
198 SmallSet<SDValue, 64> Seen;
199
200 SmallVector<const Value *, 64> NewBases, NewPtrs, NewRelocs;
201 for (size_t i = 0; i < Ptrs.size(); i++) {
202 SDValue SD = Builder.getValue(Ptrs[i]);
203 // Only add non-duplicates
204 if (Seen.count(SD) == 0) {
205 NewBases.push_back(Bases[i]);
206 NewPtrs.push_back(Ptrs[i]);
207 NewRelocs.push_back(Relocs[i]);
208 }
209 Seen.insert(SD);
210 }
211 assert(Bases.size() >= NewBases.size());
212 assert(Ptrs.size() >= NewPtrs.size());
213 assert(Relocs.size() >= NewRelocs.size());
214 Bases = NewBases;
215 Ptrs = NewPtrs;
216 Relocs = NewRelocs;
217 assert(Ptrs.size() == Bases.size());
218 assert(Ptrs.size() == Relocs.size());
219}
220
221/// Extract call from statepoint, lower it and return pointer to the
222/// call node. Also update NodeMap so that getValue(statepoint) will
223/// reference lowered call result
224static SDNode *lowerCallFromStatepoint(const CallInst &CI,
225 SelectionDAGBuilder &Builder) {
226
227 assert(Intrinsic::experimental_gc_statepoint ==
228 dyn_cast<IntrinsicInst>(&CI)->getIntrinsicID() &&
229 "function called must be the statepoint function");
230
231 int NumCallArgs = dyn_cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue();
232 assert(NumCallArgs >= 0 && "non-negative");
233
234 ImmutableStatepoint StatepointOperands(&CI);
235
236 // Lower the actual call itself - This is a bit of a hack, but we want to
237 // avoid modifying the actual lowering code. This is similiar in intent to
238 // the LowerCallOperands mechanism used by PATCHPOINT, but is structured
239 // differently. Hopefully, this is slightly more robust w.r.t. calling
240 // convention, return values, and other function attributes.
241 Value *ActualCallee = const_cast<Value *>(StatepointOperands.actualCallee());
242#ifndef NDEBUG
243 StatepointOperands.verify();
244#endif
245
246 std::vector<Value *> Args;
247 CallInst::const_op_iterator arg_begin = StatepointOperands.call_args_begin();
248 CallInst::const_op_iterator arg_end = StatepointOperands.call_args_end();
249 Args.insert(Args.end(), arg_begin, arg_end);
250 // TODO: remove the creation of a new instruction! We should not be
251 // modifying the IR (even temporarily) at this point.
252 CallInst *Tmp = CallInst::Create(ActualCallee, Args);
253 Tmp->setTailCall(CI.isTailCall());
254 Tmp->setCallingConv(CI.getCallingConv());
255 Tmp->setAttributes(CI.getAttributes());
256 Builder.LowerCallTo(Tmp, Builder.getValue(ActualCallee), false);
257
258 // Handle the return value of the call iff any.
259 const bool HasDef = !Tmp->getType()->isVoidTy();
260 if (HasDef) {
261 // The value of the statepoint itself will be the value of call itself.
262 // We'll replace the actually call node shortly. gc_result will grab
263 // this value.
264 Builder.setValue(&CI, Builder.getValue(Tmp));
265 } else {
266 // The token value is never used from here on, just generate a poison value
267 Builder.setValue(&CI, Builder.DAG.getIntPtrConstant(-1));
268 }
269 // Remove the fake entry we created so we don't have a hanging reference
270 // after we delete this node.
271 Builder.removeValue(Tmp);
272 delete Tmp;
273 Tmp = nullptr;
274
275 // Search for the call node
276 // The following code is essentially reverse engineering X86's
277 // LowerCallTo.
278 SDNode *CallNode = nullptr;
279
280 // We just emitted a call, so it should be last thing generated
281 SDValue Chain = Builder.DAG.getRoot();
282
283 // Find closest CALLSEQ_END walking back through lowered nodes if needed
284 SDNode *CallEnd = Chain.getNode();
285 int Sanity = 0;
286 while (CallEnd->getOpcode() != ISD::CALLSEQ_END) {
287 CallEnd = CallEnd->getGluedNode();
288 assert(CallEnd && "Can not find call node");
289 assert(Sanity < 20 && "should have found call end already");
290 Sanity++;
291 }
292 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
293 "Expected a callseq node.");
294 assert(CallEnd->getGluedNode());
295
296 // Step back inside the CALLSEQ
297 CallNode = CallEnd->getGluedNode();
298 return CallNode;
299}
300
301/// Callect all gc pointers coming into statepoint intrinsic, clean them up,
302/// and return two arrays:
303/// Bases - base pointers incoming to this statepoint
304/// Ptrs - derived pointers incoming to this statepoint
305/// Relocs - the gc_relocate corresponding to each base/ptr pair
306/// Elements of this arrays should be in one-to-one correspondence with each
307/// other i.e Bases[i], Ptrs[i] are from the same gcrelocate call
308static void
309getIncomingStatepointGCValues(SmallVectorImpl<const Value *> &Bases,
310 SmallVectorImpl<const Value *> &Ptrs,
311 SmallVectorImpl<const Value *> &Relocs,
312 ImmutableCallSite Statepoint,
313 SelectionDAGBuilder &Builder) {
314 // Search for relocated pointers. Note that working backwards from the
315 // gc_relocates ensures that we only get pairs which are actually relocated
316 // and used after the statepoint.
317 // TODO: This logic should probably become a utility function in Statepoint.h
318 for (const User *U : cast<CallInst>(Statepoint.getInstruction())->users()) {
319 if (!isGCRelocate(U)) {
320 continue;
321 }
322 GCRelocateOperands relocateOpers(U);
323 Relocs.push_back(cast<Value>(U));
324 Bases.push_back(relocateOpers.basePtr());
325 Ptrs.push_back(relocateOpers.derivedPtr());
326 }
327
328 // Remove any redundant llvm::Values which map to the same SDValue as another
329 // input. Also has the effect of removing duplicates in the original
330 // llvm::Value input list as well. This is a useful optimization for
331 // reducing the size of the StackMap section. It has no other impact.
332 removeDuplicatesGCPtrs(Bases, Ptrs, Relocs, Builder);
333
334 assert(Bases.size() == Ptrs.size() && Ptrs.size() == Relocs.size());
335}
336
337/// Spill a value incoming to the statepoint. It might be either part of
338/// vmstate
339/// or gcstate. In both cases unconditionally spill it on the stack unless it
340/// is a null constant. Return pair with first element being frame index
341/// containing saved value and second element with outgoing chain from the
342/// emitted store
343static std::pair<SDValue, SDValue>
344spillIncomingStatepointValue(SDValue Incoming, SDValue Chain,
345 SelectionDAGBuilder &Builder) {
346 SDValue Loc = Builder.StatepointLowering.getLocation(Incoming);
347
348 // Emit new store if we didn't do it for this ptr before
349 if (!Loc.getNode()) {
350 Loc = Builder.StatepointLowering.allocateStackSlot(Incoming.getValueType(),
351 Builder);
352 assert(isa<FrameIndexSDNode>(Loc));
353 int Index = cast<FrameIndexSDNode>(Loc)->getIndex();
354 // We use TargetFrameIndex so that isel will not select it into LEA
355 Loc = Builder.DAG.getTargetFrameIndex(Index, Incoming.getValueType());
356
357 // TODO: We can create TokenFactor node instead of
358 // chaining stores one after another, this may allow
359 // a bit more optimal scheduling for them
360 Chain = Builder.DAG.getStore(Chain, Builder.getCurSDLoc(), Incoming, Loc,
361 MachinePointerInfo::getFixedStack(Index),
362 false, false, 0);
363
364 Builder.StatepointLowering.setLocation(Incoming, Loc);
365 }
366
367 assert(Loc.getNode());
368 return std::make_pair(Loc, Chain);
369}
370
371/// Lower a single value incoming to a statepoint node. This value can be
372/// either a deopt value or a gc value, the handling is the same. We special
373/// case constants and allocas, then fall back to spilling if required.
374static void lowerIncomingStatepointValue(SDValue Incoming,
375 SmallVectorImpl<SDValue> &Ops,
376 SelectionDAGBuilder &Builder) {
377 SDValue Chain = Builder.getRoot();
378
379 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Incoming)) {
380 // If the original value was a constant, make sure it gets recorded as
381 // such in the stackmap. This is required so that the consumer can
382 // parse any internal format to the deopt state. It also handles null
383 // pointers and other constant pointers in GC states
384 Ops.push_back(
385 Builder.DAG.getTargetConstant(StackMaps::ConstantOp, MVT::i64));
386 Ops.push_back(Builder.DAG.getTargetConstant(C->getSExtValue(), MVT::i64));
387 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) {
388 // This handles allocas as arguments to the statepoint
389 const TargetLowering &TLI = Builder.DAG.getTargetLoweringInfo();
390 Ops.push_back(
391 Builder.DAG.getTargetFrameIndex(FI->getIndex(), TLI.getPointerTy()));
392 } else {
393 // Otherwise, locate a spill slot and explicitly spill it so it
394 // can be found by the runtime later. We currently do not support
395 // tracking values through callee saved registers to their eventual
396 // spill location. This would be a useful optimization, but would
397 // need to be optional since it requires a lot of complexity on the
398 // runtime side which not all would support.
399 std::pair<SDValue, SDValue> Res =
400 spillIncomingStatepointValue(Incoming, Chain, Builder);
401 Ops.push_back(Res.first);
402 Chain = Res.second;
403 }
404
405 Builder.DAG.setRoot(Chain);
406}
407
408/// Lower deopt state and gc pointer arguments of the statepoint. The actual
409/// lowering is described in lowerIncomingStatepointValue. This function is
410/// responsible for lowering everything in the right position and playing some
411/// tricks to avoid redundant stack manipulation where possible. On
412/// completion, 'Ops' will contain ready to use operands for machine code
413/// statepoint. The chain nodes will have already been created and the DAG root
414/// will be set to the last value spilled (if any were).
415static void lowerStatepointMetaArgs(SmallVectorImpl<SDValue> &Ops,
416 ImmutableStatepoint Statepoint,
417 SelectionDAGBuilder &Builder) {
418
419 // Lower the deopt and gc arguments for this statepoint. Layout will
420 // be: deopt argument length, deopt arguments.., gc arguments...
421
422 SmallVector<const Value *, 64> Bases, Ptrs, Relocations;
423 getIncomingStatepointGCValues(Bases, Ptrs, Relocations,
424 Statepoint.getCallSite(), Builder);
425
426 // Before we actually start lowering (and allocating spill slots for values),
427 // reserve any stack slots which we judge to be profitable to reuse for a
428 // particular value. This is purely an optimization over the code below and
429 // doesn't change semantics at all. It is important for performance that we
430 // reserve slots for both deopt and gc values before lowering either.
431 for (auto I = Statepoint.vm_state_begin() + 1, E = Statepoint.vm_state_end();
432 I != E; ++I) {
433 Value *V = *I;
434 SDValue Incoming = Builder.getValue(V);
435 reservePreviousStackSlotForValue(Incoming, Builder);
436 }
437 for (unsigned i = 0; i < Bases.size() * 2; ++i) {
438 // Even elements will contain base, odd elements - derived ptr
439 const Value *V = i % 2 ? Bases[i / 2] : Ptrs[i / 2];
440 SDValue Incoming = Builder.getValue(V);
441 reservePreviousStackSlotForValue(Incoming, Builder);
442 }
443
444 // First, prefix the list with the number of unique values to be
445 // lowered. Note that this is the number of *Values* not the
446 // number of SDValues required to lower them.
447 const int NumVMSArgs = Statepoint.numTotalVMSArgs();
448 Ops.push_back(
449 Builder.DAG.getTargetConstant(StackMaps::ConstantOp, MVT::i64));
450 Ops.push_back(Builder.DAG.getTargetConstant(NumVMSArgs, MVT::i64));
451
452 assert(NumVMSArgs + 1 == std::distance(Statepoint.vm_state_begin(),
453 Statepoint.vm_state_end()));
454
455 // The vm state arguments are lowered in an opaque manner. We do
456 // not know what type of values are contained within. We skip the
457 // first one since that happens to be the total number we lowered
458 // explicitly just above. We could have left it in the loop and
459 // not done it explicitly, but it's far easier to understand this
460 // way.
461 for (auto I = Statepoint.vm_state_begin() + 1, E = Statepoint.vm_state_end();
462 I != E; ++I) {
463 const Value *V = *I;
464 SDValue Incoming = Builder.getValue(V);
465 lowerIncomingStatepointValue(Incoming, Ops, Builder);
466 }
467
468 // Finally, go ahead and lower all the gc arguments. There's no prefixed
469 // length for this one. After lowering, we'll have the base and pointer
470 // arrays interwoven with each (lowered) base pointer immediately followed by
471 // it's (lowered) derived pointer. i.e
472 // (base[0], ptr[0], base[1], ptr[1], ...)
473 for (unsigned i = 0; i < Bases.size() * 2; ++i) {
474 // Even elements will contain base, odd elements - derived ptr
475 const Value *V = i % 2 ? Bases[i / 2] : Ptrs[i / 2];
476 SDValue Incoming = Builder.getValue(V);
477 lowerIncomingStatepointValue(Incoming, Ops, Builder);
478 }
479}
480void SelectionDAGBuilder::visitStatepoint(const CallInst &CI) {
481 // The basic scheme here is that information about both the original call and
482 // the safepoint is encoded in the CallInst. We create a temporary call and
483 // lower it, then reverse engineer the calling sequence.
484
485 // Check some preconditions for sanity
486 assert(isStatepoint(&CI) &&
487 "function called must be the statepoint function");
488 NumOfStatepoints++;
489 // Clear state
490 StatepointLowering.startNewStatepoint(*this);
491
492#ifndef NDEBUG
493 // Consistency check
494 for (const User *U : CI.users()) {
495 const CallInst *Call = cast<CallInst>(U);
496 if (isGCRelocate(Call))
497 StatepointLowering.scheduleRelocCall(*Call);
498 }
499#endif
500
501 ImmutableStatepoint ISP(&CI);
502
503 // Lower statepoint vmstate and gcstate arguments
504 SmallVector<SDValue, 10> LoweredArgs;
505 lowerStatepointMetaArgs(LoweredArgs, ISP, *this);
506
507 // Get call node, we will replace it later with statepoint
508 SDNode *CallNode = lowerCallFromStatepoint(CI, *this);
509
510 // Construct the actual STATEPOINT node with all the appropriate arguments
511 // and return values.
512
513 // TODO: Currently, all of these operands are being marked as read/write in
514 // PrologEpilougeInserter.cpp, we should special case the VMState arguments
515 // and flags to be read-only.
516 SmallVector<SDValue, 40> Ops;
517
518 // Calculate and push starting position of vmstate arguments
519 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
520 SDValue Glue;
521 if (CallNode->getGluedNode()) {
522 // Glue is always last operand
523 Glue = CallNode->getOperand(CallNode->getNumOperands() - 1);
524 }
525 // Get number of arguments incoming directly into call node
526 unsigned NumCallRegArgs =
527 CallNode->getNumOperands() - (Glue.getNode() ? 4 : 3);
528 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, MVT::i32));
529
530 // Add call target
531 SDValue CallTarget = SDValue(CallNode->getOperand(1).getNode(), 0);
532 Ops.push_back(CallTarget);
533
534 // Add call arguments
535 // Get position of register mask in the call
536 SDNode::op_iterator RegMaskIt;
537 if (Glue.getNode())
538 RegMaskIt = CallNode->op_end() - 2;
539 else
540 RegMaskIt = CallNode->op_end() - 1;
541 Ops.insert(Ops.end(), CallNode->op_begin() + 2, RegMaskIt);
542
543 // Add a leading constant argument with the Flags and the calling convention
544 // masked together
545 CallingConv::ID CallConv = CI.getCallingConv();
546 int Flags = dyn_cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue();
547 assert(Flags == 0 && "not expected to be used");
548 Ops.push_back(DAG.getTargetConstant(StackMaps::ConstantOp, MVT::i64));
549 Ops.push_back(
550 DAG.getTargetConstant(Flags | ((unsigned)CallConv << 1), MVT::i64));
551
552 // Insert all vmstate and gcstate arguments
553 Ops.insert(Ops.end(), LoweredArgs.begin(), LoweredArgs.end());
554
555 // Add register mask from call node
556 Ops.push_back(*RegMaskIt);
557
558 // Add chain
559 Ops.push_back(CallNode->getOperand(0));
560
561 // Same for the glue, but we add it only if original call had it
562 if (Glue.getNode())
563 Ops.push_back(Glue);
564
565 // Compute return values
566 SmallVector<EVT, 21> ValueVTs;
567 ValueVTs.push_back(MVT::Other);
568 ValueVTs.push_back(MVT::Glue); // provide a glue output since we consume one
569 // as input. This allows someone else to chain
570 // off us as needed.
571 SDVTList NodeTys = DAG.getVTList(ValueVTs);
572
573 SDNode *StatepointMCNode = DAG.getMachineNode(TargetOpcode::STATEPOINT,
574 getCurSDLoc(), NodeTys, Ops);
575
576 // Replace original call
577 DAG.ReplaceAllUsesWith(CallNode, StatepointMCNode); // This may update Root
578 // Remove originall call node
579 DAG.DeleteNode(CallNode);
580
581 // DON'T set the root - under the assumption that it's already set past the
582 // inserted node we created.
583
584 // TODO: A better future implementation would be to emit a single variable
585 // argument, variable return value STATEPOINT node here and then hookup the
586 // return value of each gc.relocate to the respective output of the
587 // previously emitted STATEPOINT value. Unfortunately, this doesn't appear
588 // to actually be possible today.
589}
590
591void SelectionDAGBuilder::visitGCResult(const CallInst &CI) {
592 // The result value of the gc_result is simply the result of the actual
593 // call. We've already emitted this, so just grab the value.
594 Instruction *I = cast<Instruction>(CI.getArgOperand(0));
595 assert(isStatepoint(I) &&
596 "first argument must be a statepoint token");
597
598 setValue(&CI, getValue(I));
599}
600
601void SelectionDAGBuilder::visitGCRelocate(const CallInst &CI) {
602#ifndef NDEBUG
603 // Consistency check
604 StatepointLowering.relocCallVisited(CI);
605#endif
606
607 GCRelocateOperands relocateOpers(&CI);
608 SDValue SD = getValue(relocateOpers.derivedPtr());
609
610 if (isa<ConstantSDNode>(SD) || isa<FrameIndexSDNode>(SD)) {
611 // We didn't need to spill these special cases (constants and allocas).
612 // See the handling in spillIncomingValueForStatepoint for detail.
613 setValue(&CI, SD);
614 return;
615 }
616
617 SDValue Loc = StatepointLowering.getRelocLocation(SD);
618 // Emit new load if we did not emit it before
619 if (!Loc.getNode()) {
620 SDValue SpillSlot = StatepointLowering.getLocation(SD);
621 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
622
623 // Be conservative: flush all pending loads
624 // TODO: Probably we can be less restrictive on this,
625 // it may allow more scheduling opprtunities
626 SDValue Chain = getRoot();
627
628 Loc = DAG.getLoad(SpillSlot.getValueType(), getCurSDLoc(), Chain,
629 SpillSlot, MachinePointerInfo::getFixedStack(FI), false,
630 false, false, 0);
631
632 StatepointLowering.setRelocLocation(SD, Loc);
633
634 // Again, be conservative, don't emit pending loads
635 DAG.setRoot(Loc.getValue(1));
636 }
637
638 assert(Loc.getNode());
639 setValue(&CI, Loc);
640}