LLVM 19.0.0git
StatepointLowering.cpp
Go to the documentation of this file.
1//===- StatepointLowering.cpp - SDAGBuilder's statepoint code -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file includes support code use by SelectionDAGBuilder when lowering a
10// statepoint sequence in SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#include "StatepointLowering.h"
15#include "SelectionDAGBuilder.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SetVector.h"
20#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/Statistic.h"
36#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/GCStrategy.h"
39#include "llvm/IR/Instruction.h"
41#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/Statepoint.h"
43#include "llvm/IR/Type.h"
48#include <cassert>
49#include <cstddef>
50#include <cstdint>
51#include <iterator>
52#include <tuple>
53#include <utility>
54
55using namespace llvm;
56
57#define DEBUG_TYPE "statepoint-lowering"
58
59STATISTIC(NumSlotsAllocatedForStatepoints,
60 "Number of stack slots allocated for statepoints");
61STATISTIC(NumOfStatepoints, "Number of statepoint nodes encountered");
62STATISTIC(StatepointMaxSlotsRequired,
63 "Maximum number of stack slots required for a singe statepoint");
64
66 "use-registers-for-deopt-values", cl::Hidden, cl::init(false),
67 cl::desc("Allow using registers for non pointer deopt args"));
68
70 "use-registers-for-gc-values-in-landing-pad", cl::Hidden, cl::init(false),
71 cl::desc("Allow using registers for gc pointer in landing pad"));
72
74 "max-registers-for-gc-values", cl::Hidden, cl::init(0),
75 cl::desc("Max number of VRegs allowed to pass GC pointer meta args in"));
76
78
81 SDLoc L = Builder.getCurSDLoc();
82 Ops.push_back(Builder.DAG.getTargetConstant(StackMaps::ConstantOp, L,
83 MVT::i64));
84 Ops.push_back(Builder.DAG.getTargetConstant(Value, L, MVT::i64));
85}
86
88 // Consistency check
89 assert(PendingGCRelocateCalls.empty() &&
90 "Trying to visit statepoint before finished processing previous one");
91 Locations.clear();
92 NextSlotToAllocate = 0;
93 // Need to resize this on each safepoint - we need the two to stay in sync and
94 // the clear patterns of a SelectionDAGBuilder have no relation to
95 // FunctionLoweringInfo. Also need to ensure used bits get cleared.
96 AllocatedStackSlots.clear();
97 AllocatedStackSlots.resize(Builder.FuncInfo.StatepointStackSlots.size());
98}
99
101 Locations.clear();
102 AllocatedStackSlots.clear();
103 assert(PendingGCRelocateCalls.empty() &&
104 "cleared before statepoint sequence completed");
105}
106
109 SelectionDAGBuilder &Builder) {
110 NumSlotsAllocatedForStatepoints++;
112
113 unsigned SpillSize = ValueType.getStoreSize();
114 assert((SpillSize * 8) ==
115 (-8u & (7 + ValueType.getSizeInBits())) && // Round up modulo 8.
116 "Size not in bytes?");
117
118 // First look for a previously created stack slot which is not in
119 // use (accounting for the fact arbitrary slots may already be
120 // reserved), or to create a new stack slot and use it.
121
122 const size_t NumSlots = AllocatedStackSlots.size();
123 assert(NextSlotToAllocate <= NumSlots && "Broken invariant");
124
125 assert(AllocatedStackSlots.size() ==
127 "Broken invariant");
128
129 for (; NextSlotToAllocate < NumSlots; NextSlotToAllocate++) {
130 if (!AllocatedStackSlots.test(NextSlotToAllocate)) {
131 const int FI = Builder.FuncInfo.StatepointStackSlots[NextSlotToAllocate];
132 if (MFI.getObjectSize(FI) == SpillSize) {
133 AllocatedStackSlots.set(NextSlotToAllocate);
134 // TODO: Is ValueType the right thing to use here?
135 return Builder.DAG.getFrameIndex(FI, ValueType);
136 }
137 }
138 }
139
140 // Couldn't find a free slot, so create a new one:
141
142 SDValue SpillSlot = Builder.DAG.CreateStackTemporary(ValueType);
143 const unsigned FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
145
147 AllocatedStackSlots.resize(AllocatedStackSlots.size()+1, true);
148 assert(AllocatedStackSlots.size() ==
150 "Broken invariant");
151
152 StatepointMaxSlotsRequired.updateMax(
154
155 return SpillSlot;
156}
157
158/// Utility function for reservePreviousStackSlotForValue. Tries to find
159/// stack slot index to which we have spilled value for previous statepoints.
160/// LookUpDepth specifies maximum DFS depth this function is allowed to look.
161static std::optional<int> findPreviousSpillSlot(const Value *Val,
162 SelectionDAGBuilder &Builder,
163 int LookUpDepth) {
164 // Can not look any further - give up now
165 if (LookUpDepth <= 0)
166 return std::nullopt;
167
168 // Spill location is known for gc relocates
169 if (const auto *Relocate = dyn_cast<GCRelocateInst>(Val)) {
170 const Value *Statepoint = Relocate->getStatepoint();
171 assert((isa<GCStatepointInst>(Statepoint) || isa<UndefValue>(Statepoint)) &&
172 "GetStatepoint must return one of two types");
173 if (isa<UndefValue>(Statepoint))
174 return std::nullopt;
175
176 const auto &RelocationMap = Builder.FuncInfo.StatepointRelocationMaps
177 [cast<GCStatepointInst>(Statepoint)];
178
179 auto It = RelocationMap.find(Relocate);
180 if (It == RelocationMap.end())
181 return std::nullopt;
182
183 auto &Record = It->second;
184 if (Record.type != RecordType::Spill)
185 return std::nullopt;
186
187 return Record.payload.FI;
188 }
189
190 // Look through bitcast instructions.
191 if (const BitCastInst *Cast = dyn_cast<BitCastInst>(Val))
192 return findPreviousSpillSlot(Cast->getOperand(0), Builder, LookUpDepth - 1);
193
194 // Look through phi nodes
195 // All incoming values should have same known stack slot, otherwise result
196 // is unknown.
197 if (const PHINode *Phi = dyn_cast<PHINode>(Val)) {
198 std::optional<int> MergedResult;
199
200 for (const auto &IncomingValue : Phi->incoming_values()) {
201 std::optional<int> SpillSlot =
202 findPreviousSpillSlot(IncomingValue, Builder, LookUpDepth - 1);
203 if (!SpillSlot)
204 return std::nullopt;
205
206 if (MergedResult && *MergedResult != *SpillSlot)
207 return std::nullopt;
208
209 MergedResult = SpillSlot;
210 }
211 return MergedResult;
212 }
213
214 // TODO: We can do better for PHI nodes. In cases like this:
215 // ptr = phi(relocated_pointer, not_relocated_pointer)
216 // statepoint(ptr)
217 // We will return that stack slot for ptr is unknown. And later we might
218 // assign different stack slots for ptr and relocated_pointer. This limits
219 // llvm's ability to remove redundant stores.
220 // Unfortunately it's hard to accomplish in current infrastructure.
221 // We use this function to eliminate spill store completely, while
222 // in example we still need to emit store, but instead of any location
223 // we need to use special "preferred" location.
224
225 // TODO: handle simple updates. If a value is modified and the original
226 // value is no longer live, it would be nice to put the modified value in the
227 // same slot. This allows folding of the memory accesses for some
228 // instructions types (like an increment).
229 // statepoint (i)
230 // i1 = i+1
231 // statepoint (i1)
232 // However we need to be careful for cases like this:
233 // statepoint(i)
234 // i1 = i+1
235 // statepoint(i, i1)
236 // Here we want to reserve spill slot for 'i', but not for 'i+1'. If we just
237 // put handling of simple modifications in this function like it's done
238 // for bitcasts we might end up reserving i's slot for 'i+1' because order in
239 // which we visit values is unspecified.
240
241 // Don't know any information about this instruction
242 return std::nullopt;
243}
244
245/// Return true if-and-only-if the given SDValue can be lowered as either a
246/// constant argument or a stack reference. The key point is that the value
247/// doesn't need to be spilled or tracked as a vreg use.
249 // We are making an unchecked assumption that the frame size <= 2^16 as that
250 // is the largest offset which can be encoded in the stackmap format.
251 if (isa<FrameIndexSDNode>(Incoming))
252 return true;
253
254 // The largest constant describeable in the StackMap format is 64 bits.
255 // Potential Optimization: Constants values are sign extended by consumer,
256 // and thus there are many constants of static type > 64 bits whose value
257 // happens to be sext(Con64) and could thus be lowered directly.
258 if (Incoming.getValueType().getSizeInBits() > 64)
259 return false;
260
261 return isIntOrFPConstant(Incoming) || Incoming.isUndef();
262}
263
264/// Try to find existing copies of the incoming values in stack slots used for
265/// statepoint spilling. If we can find a spill slot for the incoming value,
266/// mark that slot as allocated, and reuse the same slot for this safepoint.
267/// This helps to avoid series of loads and stores that only serve to reshuffle
268/// values on the stack between calls.
269static void reservePreviousStackSlotForValue(const Value *IncomingValue,
270 SelectionDAGBuilder &Builder) {
271 SDValue Incoming = Builder.getValue(IncomingValue);
272
273 // If we won't spill this, we don't need to check for previously allocated
274 // stack slots.
276 return;
277
278 SDValue OldLocation = Builder.StatepointLowering.getLocation(Incoming);
279 if (OldLocation.getNode())
280 // Duplicates in input
281 return;
282
283 const int LookUpDepth = 6;
284 std::optional<int> Index =
285 findPreviousSpillSlot(IncomingValue, Builder, LookUpDepth);
286 if (!Index)
287 return;
288
289 const auto &StatepointSlots = Builder.FuncInfo.StatepointStackSlots;
290
291 auto SlotIt = find(StatepointSlots, *Index);
292 assert(SlotIt != StatepointSlots.end() &&
293 "Value spilled to the unknown stack slot");
294
295 // This is one of our dedicated lowering slots
296 const int Offset = std::distance(StatepointSlots.begin(), SlotIt);
298 // stack slot already assigned to someone else, can't use it!
299 // TODO: currently we reserve space for gc arguments after doing
300 // normal allocation for deopt arguments. We should reserve for
301 // _all_ deopt and gc arguments, then start allocating. This
302 // will prevent some moves being inserted when vm state changes,
303 // but gc state doesn't between two calls.
304 return;
305 }
306 // Reserve this stack slot
308
309 // Cache this slot so we find it when going through the normal
310 // assignment loop.
311 SDValue Loc =
312 Builder.DAG.getTargetFrameIndex(*Index, Builder.getFrameIndexTy());
314}
315
316/// Extract call from statepoint, lower it and return pointer to the
317/// call node. Also update NodeMap so that getValue(statepoint) will
318/// reference lowered call result
319static std::pair<SDValue, SDNode *> lowerCallFromStatepointLoweringInfo(
321 SelectionDAGBuilder &Builder) {
322 SDValue ReturnValue, CallEndVal;
323 std::tie(ReturnValue, CallEndVal) =
324 Builder.lowerInvokable(SI.CLI, SI.EHPadBB);
325 SDNode *CallEnd = CallEndVal.getNode();
326
327 // Get a call instruction from the call sequence chain. Tail calls are not
328 // allowed. The following code is essentially reverse engineering X86's
329 // LowerCallTo.
330 //
331 // We are expecting DAG to have the following form:
332 //
333 // ch = eh_label (only in case of invoke statepoint)
334 // ch, glue = callseq_start ch
335 // ch, glue = X86::Call ch, glue
336 // ch, glue = callseq_end ch, glue
337 // get_return_value ch, glue
338 //
339 // get_return_value can either be a sequence of CopyFromReg instructions
340 // to grab the return value from the return register(s), or it can be a LOAD
341 // to load a value returned by reference via a stack slot.
342
343 if (CallEnd->getOpcode() == ISD::EH_LABEL)
344 CallEnd = CallEnd->getOperand(0).getNode();
345
346 bool HasDef = !SI.CLI.RetTy->isVoidTy();
347 if (HasDef) {
348 if (CallEnd->getOpcode() == ISD::LOAD)
349 CallEnd = CallEnd->getOperand(0).getNode();
350 else
351 while (CallEnd->getOpcode() == ISD::CopyFromReg)
352 CallEnd = CallEnd->getOperand(0).getNode();
353 }
354
355 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END && "expected!");
356 return std::make_pair(ReturnValue, CallEnd->getOperand(0).getNode());
357}
358
360 FrameIndexSDNode &FI) {
361 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FI.getIndex());
362 auto MMOFlags = MachineMemOperand::MOStore |
364 auto &MFI = MF.getFrameInfo();
365 return MF.getMachineMemOperand(PtrInfo, MMOFlags,
366 MFI.getObjectSize(FI.getIndex()),
367 MFI.getObjectAlign(FI.getIndex()));
368}
369
370/// Spill a value incoming to the statepoint. It might be either part of
371/// vmstate
372/// or gcstate. In both cases unconditionally spill it on the stack unless it
373/// is a null constant. Return pair with first element being frame index
374/// containing saved value and second element with outgoing chain from the
375/// emitted store
376static std::tuple<SDValue, SDValue, MachineMemOperand*>
378 SelectionDAGBuilder &Builder) {
380 MachineMemOperand* MMO = nullptr;
381
382 // Emit new store if we didn't do it for this ptr before
383 if (!Loc.getNode()) {
384 Loc = Builder.StatepointLowering.allocateStackSlot(Incoming.getValueType(),
385 Builder);
386 int Index = cast<FrameIndexSDNode>(Loc)->getIndex();
387 // We use TargetFrameIndex so that isel will not select it into LEA
388 Loc = Builder.DAG.getTargetFrameIndex(Index, Builder.getFrameIndexTy());
389
390 // Right now we always allocate spill slots that are of the same
391 // size as the value we're about to spill (the size of spillee can
392 // vary since we spill vectors of pointers too). At some point we
393 // can consider allowing spills of smaller values to larger slots
394 // (i.e. change the '==' in the assert below to a '>=').
396 assert((MFI.getObjectSize(Index) * 8) ==
397 (-8 & (7 + // Round up modulo 8.
398 (int64_t)Incoming.getValueSizeInBits())) &&
399 "Bad spill: stack slot does not match!");
400
401 // Note: Using the alignment of the spill slot (rather than the abi or
402 // preferred alignment) is required for correctness when dealing with spill
403 // slots with preferred alignments larger than frame alignment..
404 auto &MF = Builder.DAG.getMachineFunction();
405 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, Index);
406 auto *StoreMMO = MF.getMachineMemOperand(
408 MFI.getObjectAlign(Index));
409 Chain = Builder.DAG.getStore(Chain, Builder.getCurSDLoc(), Incoming, Loc,
410 StoreMMO);
411
412 MMO = getMachineMemOperand(MF, *cast<FrameIndexSDNode>(Loc));
413
415 }
416
417 assert(Loc.getNode());
418 return std::make_tuple(Loc, Chain, MMO);
419}
420
421/// Lower a single value incoming to a statepoint node. This value can be
422/// either a deopt value or a gc value, the handling is the same. We special
423/// case constants and allocas, then fall back to spilling if required.
424static void
428 SelectionDAGBuilder &Builder) {
429
431 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) {
432 // This handles allocas as arguments to the statepoint (this is only
433 // really meaningful for a deopt value. For GC, we'd be trying to
434 // relocate the address of the alloca itself?)
435 assert(Incoming.getValueType() == Builder.getFrameIndexTy() &&
436 "Incoming value is a frame index!");
437 Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(),
438 Builder.getFrameIndexTy()));
439
440 auto &MF = Builder.DAG.getMachineFunction();
441 auto *MMO = getMachineMemOperand(MF, *FI);
442 MemRefs.push_back(MMO);
443 return;
444 }
445
446 assert(Incoming.getValueType().getSizeInBits() <= 64);
447
448 if (Incoming.isUndef()) {
449 // Put an easily recognized constant that's unlikely to be a valid
450 // value so that uses of undef by the consumer of the stackmap is
451 // easily recognized. This is legal since the compiler is always
452 // allowed to chose an arbitrary value for undef.
453 pushStackMapConstant(Ops, Builder, 0xFEFEFEFE);
454 return;
455 }
456
457 // If the original value was a constant, make sure it gets recorded as
458 // such in the stackmap. This is required so that the consumer can
459 // parse any internal format to the deopt state. It also handles null
460 // pointers and other constant pointers in GC states.
461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Incoming)) {
462 pushStackMapConstant(Ops, Builder, C->getSExtValue());
463 return;
464 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Incoming)) {
465 pushStackMapConstant(Ops, Builder,
466 C->getValueAPF().bitcastToAPInt().getZExtValue());
467 return;
468 }
469
470 llvm_unreachable("unhandled direct lowering case");
471 }
472
473
474
475 if (!RequireSpillSlot) {
476 // If this value is live in (not live-on-return, or live-through), we can
477 // treat it the same way patchpoint treats it's "live in" values. We'll
478 // end up folding some of these into stack references, but they'll be
479 // handled by the register allocator. Note that we do not have the notion
480 // of a late use so these values might be placed in registers which are
481 // clobbered by the call. This is fine for live-in. For live-through
482 // fix-up pass should be executed to force spilling of such registers.
483 Ops.push_back(Incoming);
484 } else {
485 // Otherwise, locate a spill slot and explicitly spill it so it can be
486 // found by the runtime later. Note: We know all of these spills are
487 // independent, but don't bother to exploit that chain wise. DAGCombine
488 // will happily do so as needed, so doing it here would be a small compile
489 // time win at most.
490 SDValue Chain = Builder.getRoot();
491 auto Res = spillIncomingStatepointValue(Incoming, Chain, Builder);
492 Ops.push_back(std::get<0>(Res));
493 if (auto *MMO = std::get<2>(Res))
494 MemRefs.push_back(MMO);
495 Chain = std::get<1>(Res);
496 Builder.DAG.setRoot(Chain);
497 }
498
499}
500
501/// Return true if value V represents the GC value. The behavior is conservative
502/// in case it is not sure that value is not GC the function returns true.
503static bool isGCValue(const Value *V, SelectionDAGBuilder &Builder) {
504 auto *Ty = V->getType();
505 if (!Ty->isPtrOrPtrVectorTy())
506 return false;
507 if (auto *GFI = Builder.GFI)
508 if (auto IsManaged = GFI->getStrategy().isGCManagedPointer(Ty))
509 return *IsManaged;
510 return true; // conservative
511}
512
513/// Lower deopt state and gc pointer arguments of the statepoint. The actual
514/// lowering is described in lowerIncomingStatepointValue. This function is
515/// responsible for lowering everything in the right position and playing some
516/// tricks to avoid redundant stack manipulation where possible. On
517/// completion, 'Ops' will contain ready to use operands for machine code
518/// statepoint. The chain nodes will have already been created and the DAG root
519/// will be set to the last value spilled (if any were).
520static void
524 DenseMap<SDValue, int> &LowerAsVReg,
526 SelectionDAGBuilder &Builder) {
527 // Lower the deopt and gc arguments for this statepoint. Layout will be:
528 // deopt argument length, deopt arguments.., gc arguments...
529
530 // Figure out what lowering strategy we're going to use for each part
531 // Note: It is conservatively correct to lower both "live-in" and "live-out"
532 // as "live-through". A "live-through" variable is one which is "live-in",
533 // "live-out", and live throughout the lifetime of the call (i.e. we can find
534 // it from any PC within the transitive callee of the statepoint). In
535 // particular, if the callee spills callee preserved registers we may not
536 // be able to find a value placed in that register during the call. This is
537 // fine for live-out, but not for live-through. If we were willing to make
538 // assumptions about the code generator producing the callee, we could
539 // potentially allow live-through values in callee saved registers.
540 const bool LiveInDeopt =
541 SI.StatepointFlags & (uint64_t)StatepointFlags::DeoptLiveIn;
542
543 // Decide which deriver pointers will go on VRegs
544 unsigned MaxVRegPtrs = MaxRegistersForGCPointers.getValue();
545
546 // Pointers used on exceptional path of invoke statepoint.
547 // We cannot assing them to VRegs.
548 SmallSet<SDValue, 8> LPadPointers;
550 if (const auto *StInvoke =
551 dyn_cast_or_null<InvokeInst>(SI.StatepointInstr)) {
552 LandingPadInst *LPI = StInvoke->getLandingPadInst();
553 for (const auto *Relocate : SI.GCRelocates)
554 if (Relocate->getOperand(0) == LPI) {
555 LPadPointers.insert(Builder.getValue(Relocate->getBasePtr()));
556 LPadPointers.insert(Builder.getValue(Relocate->getDerivedPtr()));
557 }
558 }
559
560 LLVM_DEBUG(dbgs() << "Deciding how to lower GC Pointers:\n");
561
562 // List of unique lowered GC Pointer values.
563 SmallSetVector<SDValue, 16> LoweredGCPtrs;
564 // Map lowered GC Pointer value to the index in above vector
565 DenseMap<SDValue, unsigned> GCPtrIndexMap;
566
567 unsigned CurNumVRegs = 0;
568
569 auto canPassGCPtrOnVReg = [&](SDValue SD) {
570 if (SD.getValueType().isVector())
571 return false;
572 if (LPadPointers.count(SD))
573 return false;
574 return !willLowerDirectly(SD);
575 };
576
577 auto processGCPtr = [&](const Value *V) {
578 SDValue PtrSD = Builder.getValue(V);
579 if (!LoweredGCPtrs.insert(PtrSD))
580 return; // skip duplicates
581 GCPtrIndexMap[PtrSD] = LoweredGCPtrs.size() - 1;
582
583 assert(!LowerAsVReg.count(PtrSD) && "must not have been seen");
584 if (LowerAsVReg.size() == MaxVRegPtrs)
585 return;
586 assert(V->getType()->isVectorTy() == PtrSD.getValueType().isVector() &&
587 "IR and SD types disagree");
588 if (!canPassGCPtrOnVReg(PtrSD)) {
589 LLVM_DEBUG(dbgs() << "direct/spill "; PtrSD.dump(&Builder.DAG));
590 return;
591 }
592 LLVM_DEBUG(dbgs() << "vreg "; PtrSD.dump(&Builder.DAG));
593 LowerAsVReg[PtrSD] = CurNumVRegs++;
594 };
595
596 // Process derived pointers first to give them more chance to go on VReg.
597 for (const Value *V : SI.Ptrs)
598 processGCPtr(V);
599 for (const Value *V : SI.Bases)
600 processGCPtr(V);
601
602 LLVM_DEBUG(dbgs() << LowerAsVReg.size() << " pointers will go in vregs\n");
603
604 auto requireSpillSlot = [&](const Value *V) {
606 Builder.getValue(V).getValueType()))
607 return true;
608 if (isGCValue(V, Builder))
609 return !LowerAsVReg.count(Builder.getValue(V));
610 return !(LiveInDeopt || UseRegistersForDeoptValues);
611 };
612
613 // Before we actually start lowering (and allocating spill slots for values),
614 // reserve any stack slots which we judge to be profitable to reuse for a
615 // particular value. This is purely an optimization over the code below and
616 // doesn't change semantics at all. It is important for performance that we
617 // reserve slots for both deopt and gc values before lowering either.
618 for (const Value *V : SI.DeoptState) {
619 if (requireSpillSlot(V))
621 }
622
623 for (const Value *V : SI.Ptrs) {
624 SDValue SDV = Builder.getValue(V);
625 if (!LowerAsVReg.count(SDV))
627 }
628
629 for (const Value *V : SI.Bases) {
630 SDValue SDV = Builder.getValue(V);
631 if (!LowerAsVReg.count(SDV))
633 }
634
635 // First, prefix the list with the number of unique values to be
636 // lowered. Note that this is the number of *Values* not the
637 // number of SDValues required to lower them.
638 const int NumVMSArgs = SI.DeoptState.size();
639 pushStackMapConstant(Ops, Builder, NumVMSArgs);
640
641 // The vm state arguments are lowered in an opaque manner. We do not know
642 // what type of values are contained within.
643 LLVM_DEBUG(dbgs() << "Lowering deopt state\n");
644 for (const Value *V : SI.DeoptState) {
646 // If this is a function argument at a static frame index, generate it as
647 // the frame index.
648 if (const Argument *Arg = dyn_cast<Argument>(V)) {
649 int FI = Builder.FuncInfo.getArgumentFrameIndex(Arg);
650 if (FI != INT_MAX)
651 Incoming = Builder.DAG.getFrameIndex(FI, Builder.getFrameIndexTy());
652 }
653 if (!Incoming.getNode())
654 Incoming = Builder.getValue(V);
655 LLVM_DEBUG(dbgs() << "Value " << *V
656 << " requireSpillSlot = " << requireSpillSlot(V) << "\n");
657 lowerIncomingStatepointValue(Incoming, requireSpillSlot(V), Ops, MemRefs,
658 Builder);
659 }
660
661 // Finally, go ahead and lower all the gc arguments.
662 pushStackMapConstant(Ops, Builder, LoweredGCPtrs.size());
663 for (SDValue SDV : LoweredGCPtrs)
664 lowerIncomingStatepointValue(SDV, !LowerAsVReg.count(SDV), Ops, MemRefs,
665 Builder);
666
667 // Copy to out vector. LoweredGCPtrs will be empty after this point.
668 GCPtrs = LoweredGCPtrs.takeVector();
669
670 // If there are any explicit spill slots passed to the statepoint, record
671 // them, but otherwise do not do anything special. These are user provided
672 // allocas and give control over placement to the consumer. In this case,
673 // it is the contents of the slot which may get updated, not the pointer to
674 // the alloca
676 for (Value *V : SI.GCArgs) {
677 SDValue Incoming = Builder.getValue(V);
678 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) {
679 // This handles allocas as arguments to the statepoint
680 assert(Incoming.getValueType() == Builder.getFrameIndexTy() &&
681 "Incoming value is a frame index!");
682 Allocas.push_back(Builder.DAG.getTargetFrameIndex(
683 FI->getIndex(), Builder.getFrameIndexTy()));
684
685 auto &MF = Builder.DAG.getMachineFunction();
686 auto *MMO = getMachineMemOperand(MF, *FI);
687 MemRefs.push_back(MMO);
688 }
689 }
690 pushStackMapConstant(Ops, Builder, Allocas.size());
691 Ops.append(Allocas.begin(), Allocas.end());
692
693 // Now construct GC base/derived map;
694 pushStackMapConstant(Ops, Builder, SI.Ptrs.size());
695 SDLoc L = Builder.getCurSDLoc();
696 for (unsigned i = 0; i < SI.Ptrs.size(); ++i) {
697 SDValue Base = Builder.getValue(SI.Bases[i]);
698 assert(GCPtrIndexMap.count(Base) && "base not found in index map");
699 Ops.push_back(
700 Builder.DAG.getTargetConstant(GCPtrIndexMap[Base], L, MVT::i64));
701 SDValue Derived = Builder.getValue(SI.Ptrs[i]);
702 assert(GCPtrIndexMap.count(Derived) && "derived not found in index map");
703 Ops.push_back(
704 Builder.DAG.getTargetConstant(GCPtrIndexMap[Derived], L, MVT::i64));
705 }
706}
707
710 // The basic scheme here is that information about both the original call and
711 // the safepoint is encoded in the CallInst. We create a temporary call and
712 // lower it, then reverse engineer the calling sequence.
713
714 NumOfStatepoints++;
715 // Clear state
717 assert(SI.Bases.size() == SI.Ptrs.size() && "Pointer without base!");
718 assert((GFI || SI.Bases.empty()) &&
719 "No gc specified, so cannot relocate pointers!");
720
721 LLVM_DEBUG(if (SI.StatepointInstr) dbgs()
722 << "Lowering statepoint " << *SI.StatepointInstr << "\n");
723#ifndef NDEBUG
724 for (const auto *Reloc : SI.GCRelocates)
725 if (Reloc->getParent() == SI.StatepointInstr->getParent())
727#endif
728
729 // Lower statepoint vmstate and gcstate arguments
730
731 // All lowered meta args.
732 SmallVector<SDValue, 10> LoweredMetaArgs;
733 // Lowered GC pointers (subset of above).
734 SmallVector<SDValue, 16> LoweredGCArgs;
736 // Maps derived pointer SDValue to statepoint result of relocated pointer.
737 DenseMap<SDValue, int> LowerAsVReg;
738 lowerStatepointMetaArgs(LoweredMetaArgs, MemRefs, LoweredGCArgs, LowerAsVReg,
739 SI, *this);
740
741 // Now that we've emitted the spills, we need to update the root so that the
742 // call sequence is ordered correctly.
743 SI.CLI.setChain(getRoot());
744
745 // Get call node, we will replace it later with statepoint
746 SDValue ReturnVal;
747 SDNode *CallNode;
748 std::tie(ReturnVal, CallNode) = lowerCallFromStatepointLoweringInfo(SI, *this);
749
750 // Construct the actual GC_TRANSITION_START, STATEPOINT, and GC_TRANSITION_END
751 // nodes with all the appropriate arguments and return values.
752
753 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
754 SDValue Chain = CallNode->getOperand(0);
755
756 SDValue Glue;
757 bool CallHasIncomingGlue = CallNode->getGluedNode();
758 if (CallHasIncomingGlue) {
759 // Glue is always last operand
760 Glue = CallNode->getOperand(CallNode->getNumOperands() - 1);
761 }
762
763 // Build the GC_TRANSITION_START node if necessary.
764 //
765 // The operands to the GC_TRANSITION_{START,END} nodes are laid out in the
766 // order in which they appear in the call to the statepoint intrinsic. If
767 // any of the operands is a pointer-typed, that operand is immediately
768 // followed by a SRCVALUE for the pointer that may be used during lowering
769 // (e.g. to form MachinePointerInfo values for loads/stores).
770 const bool IsGCTransition =
771 (SI.StatepointFlags & (uint64_t)StatepointFlags::GCTransition) ==
773 if (IsGCTransition) {
775
776 // Add chain
777 TSOps.push_back(Chain);
778
779 // Add GC transition arguments
780 for (const Value *V : SI.GCTransitionArgs) {
781 TSOps.push_back(getValue(V));
782 if (V->getType()->isPointerTy())
783 TSOps.push_back(DAG.getSrcValue(V));
784 }
785
786 // Add glue if necessary
787 if (CallHasIncomingGlue)
788 TSOps.push_back(Glue);
789
790 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
791
792 SDValue GCTransitionStart =
794
795 Chain = GCTransitionStart.getValue(0);
796 Glue = GCTransitionStart.getValue(1);
797 }
798
799 // TODO: Currently, all of these operands are being marked as read/write in
800 // PrologEpilougeInserter.cpp, we should special case the VMState arguments
801 // and flags to be read-only.
803
804 // Add the <id> and <numBytes> constants.
805 Ops.push_back(DAG.getTargetConstant(SI.ID, getCurSDLoc(), MVT::i64));
806 Ops.push_back(
807 DAG.getTargetConstant(SI.NumPatchBytes, getCurSDLoc(), MVT::i32));
808
809 // Calculate and push starting position of vmstate arguments
810 // Get number of arguments incoming directly into call node
811 unsigned NumCallRegArgs =
812 CallNode->getNumOperands() - (CallHasIncomingGlue ? 4 : 3);
813 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, getCurSDLoc(), MVT::i32));
814
815 // Add call target
816 SDValue CallTarget = SDValue(CallNode->getOperand(1).getNode(), 0);
817 Ops.push_back(CallTarget);
818
819 // Add call arguments
820 // Get position of register mask in the call
821 SDNode::op_iterator RegMaskIt;
822 if (CallHasIncomingGlue)
823 RegMaskIt = CallNode->op_end() - 2;
824 else
825 RegMaskIt = CallNode->op_end() - 1;
826 Ops.insert(Ops.end(), CallNode->op_begin() + 2, RegMaskIt);
827
828 // Add a constant argument for the calling convention
829 pushStackMapConstant(Ops, *this, SI.CLI.CallConv);
830
831 // Add a constant argument for the flags
832 uint64_t Flags = SI.StatepointFlags;
833 assert(((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0) &&
834 "Unknown flag used");
835 pushStackMapConstant(Ops, *this, Flags);
836
837 // Insert all vmstate and gcstate arguments
838 llvm::append_range(Ops, LoweredMetaArgs);
839
840 // Add register mask from call node
841 Ops.push_back(*RegMaskIt);
842
843 // Add chain
844 Ops.push_back(Chain);
845
846 // Same for the glue, but we add it only if original call had it
847 if (Glue.getNode())
848 Ops.push_back(Glue);
849
850 // Compute return values. Provide a glue output since we consume one as
851 // input. This allows someone else to chain off us as needed.
852 SmallVector<EVT, 8> NodeTys;
853 for (auto SD : LoweredGCArgs) {
854 if (!LowerAsVReg.count(SD))
855 continue;
856 NodeTys.push_back(SD.getValueType());
857 }
858 LLVM_DEBUG(dbgs() << "Statepoint has " << NodeTys.size() << " results\n");
859 assert(NodeTys.size() == LowerAsVReg.size() && "Inconsistent GC Ptr lowering");
860 NodeTys.push_back(MVT::Other);
861 NodeTys.push_back(MVT::Glue);
862
863 unsigned NumResults = NodeTys.size();
864 MachineSDNode *StatepointMCNode =
865 DAG.getMachineNode(TargetOpcode::STATEPOINT, getCurSDLoc(), NodeTys, Ops);
866 DAG.setNodeMemRefs(StatepointMCNode, MemRefs);
867
868 // For values lowered to tied-defs, create the virtual registers if used
869 // in other blocks. For local gc.relocate record appropriate statepoint
870 // result in StatepointLoweringState.
872 for (const auto *Relocate : SI.GCRelocates) {
873 Value *Derived = Relocate->getDerivedPtr();
874 SDValue SD = getValue(Derived);
875 if (!LowerAsVReg.count(SD))
876 continue;
877
878 SDValue Relocated = SDValue(StatepointMCNode, LowerAsVReg[SD]);
879
880 // Handle local relocate. Note that different relocates might
881 // map to the same SDValue.
882 if (SI.StatepointInstr->getParent() == Relocate->getParent()) {
884 if (Res)
885 assert(Res == Relocated);
886 else
887 StatepointLowering.setLocation(SD, Relocated);
888 continue;
889 }
890
891 // Handle multiple gc.relocates of the same input efficiently.
892 if (VirtRegs.count(SD))
893 continue;
894
895 auto *RetTy = Relocate->getType();
898 DAG.getDataLayout(), Reg, RetTy, std::nullopt);
899 SDValue Chain = DAG.getRoot();
900 RFV.getCopyToRegs(Relocated, DAG, getCurSDLoc(), Chain, nullptr);
901 PendingExports.push_back(Chain);
902
903 VirtRegs[SD] = Reg;
904 }
905
906 // Record for later use how each relocation was lowered. This is needed to
907 // allow later gc.relocates to mirror the lowering chosen.
908 const Instruction *StatepointInstr = SI.StatepointInstr;
909 auto &RelocationMap = FuncInfo.StatepointRelocationMaps[StatepointInstr];
910 for (const GCRelocateInst *Relocate : SI.GCRelocates) {
911 const Value *V = Relocate->getDerivedPtr();
912 SDValue SDV = getValue(V);
914
915 bool IsLocal = (Relocate->getParent() == StatepointInstr->getParent());
916
918 if (IsLocal && LowerAsVReg.count(SDV)) {
919 // Result is already stored in StatepointLowering
921 } else if (LowerAsVReg.count(SDV)) {
923 assert(VirtRegs.count(SDV));
924 Record.payload.Reg = VirtRegs[SDV];
925 } else if (Loc.getNode()) {
927 Record.payload.FI = cast<FrameIndexSDNode>(Loc)->getIndex();
928 } else {
930 // If we didn't relocate a value, we'll essentialy end up inserting an
931 // additional use of the original value when lowering the gc.relocate.
932 // We need to make sure the value is available at the new use, which
933 // might be in another block.
934 if (Relocate->getParent() != StatepointInstr->getParent())
936 }
937 RelocationMap[Relocate] = Record;
938 }
939
940
941
942 SDNode *SinkNode = StatepointMCNode;
943
944 // Build the GC_TRANSITION_END node if necessary.
945 //
946 // See the comment above regarding GC_TRANSITION_START for the layout of
947 // the operands to the GC_TRANSITION_END node.
948 if (IsGCTransition) {
950
951 // Add chain
952 TEOps.push_back(SDValue(StatepointMCNode, NumResults - 2));
953
954 // Add GC transition arguments
955 for (const Value *V : SI.GCTransitionArgs) {
956 TEOps.push_back(getValue(V));
957 if (V->getType()->isPointerTy())
958 TEOps.push_back(DAG.getSrcValue(V));
959 }
960
961 // Add glue
962 TEOps.push_back(SDValue(StatepointMCNode, NumResults - 1));
963
964 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
965
966 SDValue GCTransitionStart =
967 DAG.getNode(ISD::GC_TRANSITION_END, getCurSDLoc(), NodeTys, TEOps);
968
969 SinkNode = GCTransitionStart.getNode();
970 }
971
972 // Replace original call
973 // Call: ch,glue = CALL ...
974 // Statepoint: [gc relocates],ch,glue = STATEPOINT ...
975 unsigned NumSinkValues = SinkNode->getNumValues();
976 SDValue StatepointValues[2] = {SDValue(SinkNode, NumSinkValues - 2),
977 SDValue(SinkNode, NumSinkValues - 1)};
978 DAG.ReplaceAllUsesWith(CallNode, StatepointValues);
979 // Remove original call node
980 DAG.DeleteNode(CallNode);
981
982 // Since we always emit CopyToRegs (even for local relocates), we must
983 // update root, so that they are emitted before any local uses.
984 (void)getControlRoot();
985
986 // TODO: A better future implementation would be to emit a single variable
987 // argument, variable return value STATEPOINT node here and then hookup the
988 // return value of each gc.relocate to the respective output of the
989 // previously emitted STATEPOINT value. Unfortunately, this doesn't appear
990 // to actually be possible today.
991
992 return ReturnVal;
993}
994
995/// Return two gc.results if present. First result is a block local
996/// gc.result, second result is a non-block local gc.result. Corresponding
997/// entry will be nullptr if not present.
998static std::pair<const GCResultInst*, const GCResultInst*>
1000 std::pair<const GCResultInst *, const GCResultInst*> Res(nullptr, nullptr);
1001 for (const auto *U : S.users()) {
1002 auto *GRI = dyn_cast<GCResultInst>(U);
1003 if (!GRI)
1004 continue;
1005 if (GRI->getParent() == S.getParent())
1006 Res.first = GRI;
1007 else
1008 Res.second = GRI;
1009 }
1010 return Res;
1011}
1012
1013void
1015 const BasicBlock *EHPadBB /*= nullptr*/) {
1016 assert(I.getCallingConv() != CallingConv::AnyReg &&
1017 "anyregcc is not supported on statepoints!");
1018
1019#ifndef NDEBUG
1020 // Check that the associated GCStrategy expects to encounter statepoints.
1022 "GCStrategy does not expect to encounter statepoints");
1023#endif
1024
1025 SDValue ActualCallee;
1026 SDValue Callee = getValue(I.getActualCalledOperand());
1027
1028 if (I.getNumPatchBytes() > 0) {
1029 // If we've been asked to emit a nop sequence instead of a call instruction
1030 // for this statepoint then don't lower the call target, but use a constant
1031 // `undef` instead. Not lowering the call target lets statepoint clients
1032 // get away without providing a physical address for the symbolic call
1033 // target at link time.
1034 ActualCallee = DAG.getUNDEF(Callee.getValueType());
1035 } else {
1036 ActualCallee = Callee;
1037 }
1038
1039 const auto GCResultLocality = getGCResultLocality(I);
1040 AttributeSet retAttrs;
1041 if (GCResultLocality.first)
1042 retAttrs = GCResultLocality.first->getAttributes().getRetAttrs();
1043
1046 I.getNumCallArgs(), ActualCallee,
1047 I.getActualReturnType(), retAttrs,
1048 /*IsPatchPoint=*/false);
1049
1050 // There may be duplication in the gc.relocate list; such as two copies of
1051 // each relocation on normal and exceptional path for an invoke. We only
1052 // need to spill once and record one copy in the stackmap, but we need to
1053 // reload once per gc.relocate. (Dedupping gc.relocates is trickier and best
1054 // handled as a CSE problem elsewhere.)
1055 // TODO: There a couple of major stackmap size optimizations we could do
1056 // here if we wished.
1057 // 1) If we've encountered a derived pair {B, D}, we don't need to actually
1058 // record {B,B} if it's seen later.
1059 // 2) Due to rematerialization, actual derived pointers are somewhat rare;
1060 // given that, we could change the format to record base pointer relocations
1061 // separately with half the space. This would require a format rev and a
1062 // fairly major rework of the STATEPOINT node though.
1064 for (const GCRelocateInst *Relocate : I.getGCRelocates()) {
1065 SI.GCRelocates.push_back(Relocate);
1066
1067 SDValue DerivedSD = getValue(Relocate->getDerivedPtr());
1068 if (Seen.insert(DerivedSD).second) {
1069 SI.Bases.push_back(Relocate->getBasePtr());
1070 SI.Ptrs.push_back(Relocate->getDerivedPtr());
1071 }
1072 }
1073
1074 // If we find a deopt value which isn't explicitly added, we need to
1075 // ensure it gets lowered such that gc cycles occurring before the
1076 // deoptimization event during the lifetime of the call don't invalidate
1077 // the pointer we're deopting with. Note that we assume that all
1078 // pointers passed to deopt are base pointers; relaxing that assumption
1079 // would require relatively large changes to how we represent relocations.
1080 for (Value *V : I.deopt_operands()) {
1081 if (!isGCValue(V, *this))
1082 continue;
1083 if (Seen.insert(getValue(V)).second) {
1084 SI.Bases.push_back(V);
1085 SI.Ptrs.push_back(V);
1086 }
1087 }
1088
1089 SI.GCArgs = ArrayRef<const Use>(I.gc_args_begin(), I.gc_args_end());
1090 SI.StatepointInstr = &I;
1091 SI.ID = I.getID();
1092
1093 SI.DeoptState = ArrayRef<const Use>(I.deopt_begin(), I.deopt_end());
1094 SI.GCTransitionArgs = ArrayRef<const Use>(I.gc_transition_args_begin(),
1095 I.gc_transition_args_end());
1096
1097 SI.StatepointFlags = I.getFlags();
1098 SI.NumPatchBytes = I.getNumPatchBytes();
1099 SI.EHPadBB = EHPadBB;
1100
1101 SDValue ReturnValue = LowerAsSTATEPOINT(SI);
1102
1103 // Export the result value if needed
1104 if (!GCResultLocality.first && !GCResultLocality.second) {
1105 // The return value is not needed, just generate a poison value.
1106 // Note: This covers the void return case.
1108 return;
1109 }
1110
1111 if (GCResultLocality.first) {
1112 // Result value will be used in a same basic block. Don't export it or
1113 // perform any explicit register copies. The gc_result will simply grab
1114 // this value.
1115 setValue(&I, ReturnValue);
1116 }
1117
1118 if (!GCResultLocality.second)
1119 return;
1120 // Result value will be used in a different basic block so we need to export
1121 // it now. Default exporting mechanism will not work here because statepoint
1122 // call has a different type than the actual call. It means that by default
1123 // llvm will create export register of the wrong type (always i32 in our
1124 // case). So instead we need to create export register with correct type
1125 // manually.
1126 // TODO: To eliminate this problem we can remove gc.result intrinsics
1127 // completely and make statepoint call to return a tuple.
1128 Type *RetTy = GCResultLocality.second->getType();
1131 DAG.getDataLayout(), Reg, RetTy,
1132 I.getCallingConv());
1133 SDValue Chain = DAG.getEntryNode();
1134
1135 RFV.getCopyToRegs(ReturnValue, DAG, getCurSDLoc(), Chain, nullptr);
1136 PendingExports.push_back(Chain);
1137 FuncInfo.ValueMap[&I] = Reg;
1138}
1139
1141 const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB,
1142 bool VarArgDisallowed, bool ForceVoidReturnTy) {
1144 unsigned ArgBeginIndex = Call->arg_begin() - Call->op_begin();
1146 SI.CLI, Call, ArgBeginIndex, Call->arg_size(), Callee,
1147 ForceVoidReturnTy ? Type::getVoidTy(*DAG.getContext()) : Call->getType(),
1148 Call->getAttributes().getRetAttrs(), /*IsPatchPoint=*/false);
1149 if (!VarArgDisallowed)
1150 SI.CLI.IsVarArg = Call->getFunctionType()->isVarArg();
1151
1152 auto DeoptBundle = *Call->getOperandBundle(LLVMContext::OB_deopt);
1153
1155
1156 auto SD = parseStatepointDirectivesFromAttrs(Call->getAttributes());
1157 SI.ID = SD.StatepointID.value_or(DefaultID);
1158 SI.NumPatchBytes = SD.NumPatchBytes.value_or(0);
1159
1160 SI.DeoptState =
1161 ArrayRef<const Use>(DeoptBundle.Inputs.begin(), DeoptBundle.Inputs.end());
1162 SI.StatepointFlags = static_cast<uint64_t>(StatepointFlags::None);
1163 SI.EHPadBB = EHPadBB;
1164
1165 // NB! The GC arguments are deliberately left empty.
1166
1167 LLVM_DEBUG(dbgs() << "Lowering call with deopt bundle " << *Call << "\n");
1168 if (SDValue ReturnVal = LowerAsSTATEPOINT(SI)) {
1169 ReturnVal = lowerRangeToAssertZExt(DAG, *Call, ReturnVal);
1170 setValue(Call, ReturnVal);
1171 }
1172}
1173
1175 const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB) {
1176 LowerCallSiteWithDeoptBundleImpl(Call, Callee, EHPadBB,
1177 /* VarArgDisallowed = */ false,
1178 /* ForceVoidReturnTy = */ false);
1179}
1180
1181void SelectionDAGBuilder::visitGCResult(const GCResultInst &CI) {
1182 // The result value of the gc_result is simply the result of the actual
1183 // call. We've already emitted this, so just grab the value.
1184 const Value *SI = CI.getStatepoint();
1185 assert((isa<GCStatepointInst>(SI) || isa<UndefValue>(SI)) &&
1186 "GetStatepoint must return one of two types");
1187 if (isa<UndefValue>(SI))
1188 return;
1189
1190 if (cast<GCStatepointInst>(SI)->getParent() == CI.getParent()) {
1191 setValue(&CI, getValue(SI));
1192 return;
1193 }
1194 // Statepoint is in different basic block so we should have stored call
1195 // result in a virtual register.
1196 // We can not use default getValue() functionality to copy value from this
1197 // register because statepoint and actual call return types can be
1198 // different, and getValue() will use CopyFromReg of the wrong type,
1199 // which is always i32 in our case.
1200 Type *RetTy = CI.getType();
1201 SDValue CopyFromReg = getCopyFromRegs(SI, RetTy);
1202
1203 assert(CopyFromReg.getNode());
1204 setValue(&CI, CopyFromReg);
1205}
1206
1207void SelectionDAGBuilder::visitGCRelocate(const GCRelocateInst &Relocate) {
1208 const Value *Statepoint = Relocate.getStatepoint();
1209#ifndef NDEBUG
1210 // Consistency check
1211 // We skip this check for relocates not in the same basic block as their
1212 // statepoint. It would be too expensive to preserve validation info through
1213 // different basic blocks.
1214 assert((isa<GCStatepointInst>(Statepoint) || isa<UndefValue>(Statepoint)) &&
1215 "GetStatepoint must return one of two types");
1216 if (isa<UndefValue>(Statepoint))
1217 return;
1218
1219 if (cast<GCStatepointInst>(Statepoint)->getParent() == Relocate.getParent())
1221#endif
1222
1223 const Value *DerivedPtr = Relocate.getDerivedPtr();
1224 auto &RelocationMap =
1225 FuncInfo.StatepointRelocationMaps[cast<GCStatepointInst>(Statepoint)];
1226 auto SlotIt = RelocationMap.find(&Relocate);
1227 assert(SlotIt != RelocationMap.end() && "Relocating not lowered gc value");
1228 const RecordType &Record = SlotIt->second;
1229
1230 // If relocation was done via virtual register..
1231 if (Record.type == RecordType::SDValueNode) {
1232 assert(cast<GCStatepointInst>(Statepoint)->getParent() ==
1233 Relocate.getParent() &&
1234 "Nonlocal gc.relocate mapped via SDValue");
1236 assert(SDV.getNode() && "empty SDValue");
1237 setValue(&Relocate, SDV);
1238 return;
1239 }
1240 if (Record.type == RecordType::VReg) {
1241 Register InReg = Record.payload.Reg;
1243 DAG.getDataLayout(), InReg, Relocate.getType(),
1244 std::nullopt); // This is not an ABI copy.
1245 // We generate copy to/from regs even for local uses, hence we must
1246 // chain with current root to ensure proper ordering of copies w.r.t.
1247 // statepoint.
1248 SDValue Chain = DAG.getRoot();
1249 SDValue Relocation = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
1250 Chain, nullptr, nullptr);
1251 setValue(&Relocate, Relocation);
1252 return;
1253 }
1254
1255 if (Record.type == RecordType::Spill) {
1256 unsigned Index = Record.payload.FI;
1258
1259 // All the reloads are independent and are reading memory only modified by
1260 // statepoints (i.e. no other aliasing stores); informing SelectionDAG of
1261 // this lets CSE kick in for free and allows reordering of
1262 // instructions if possible. The lowering for statepoint sets the root,
1263 // so this is ordering all reloads with the either
1264 // a) the statepoint node itself, or
1265 // b) the entry of the current block for an invoke statepoint.
1266 const SDValue Chain = DAG.getRoot(); // != Builder.getRoot()
1267
1268 auto &MF = DAG.getMachineFunction();
1269 auto &MFI = MF.getFrameInfo();
1270 auto PtrInfo = MachinePointerInfo::getFixedStack(MF, Index);
1271 auto *LoadMMO = MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOLoad,
1272 MFI.getObjectSize(Index),
1273 MFI.getObjectAlign(Index));
1274
1276 Relocate.getType());
1277
1278 SDValue SpillLoad =
1279 DAG.getLoad(LoadVT, getCurSDLoc(), Chain, SpillSlot, LoadMMO);
1280 PendingLoads.push_back(SpillLoad.getValue(1));
1281
1282 assert(SpillLoad.getNode());
1283 setValue(&Relocate, SpillLoad);
1284 return;
1285 }
1286
1288 SDValue SD = getValue(DerivedPtr);
1289
1290 if (SD.isUndef() && SD.getValueType().getSizeInBits() <= 64) {
1291 // Lowering relocate(undef) as arbitrary constant. Current constant value
1292 // is chosen such that it's unlikely to be a valid pointer.
1293 setValue(&Relocate, DAG.getConstant(0xFEFEFEFE, SDLoc(SD), MVT::i64));
1294 return;
1295 }
1296
1297 // We didn't need to spill these special cases (constants and allocas).
1298 // See the handling in spillIncomingValueForStatepoint for detail.
1299 setValue(&Relocate, SD);
1300}
1301
1303 const auto &TLI = DAG.getTargetLoweringInfo();
1304 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::DEOPTIMIZE),
1305 TLI.getPointerTy(DAG.getDataLayout()));
1306
1307 // We don't lower calls to __llvm_deoptimize as varargs, but as a regular
1308 // call. We also do not lower the return value to any virtual register, and
1309 // change the immediately following return to a trap instruction.
1310 LowerCallSiteWithDeoptBundleImpl(CI, Callee, /* EHPadBB = */ nullptr,
1311 /* VarArgDisallowed = */ true,
1312 /* ForceVoidReturnTy = */ true);
1313}
1314
1316 // We do not lower the return value from llvm.deoptimize to any virtual
1317 // register, and change the immediately following return to a trap
1318 // instruction.
1320 DAG.setRoot(
1321 DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
1322}
static const Function * getParent(const Value *V)
return RetTy
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file implements the SmallBitVector class.
This file defines the SmallSet class.
This file defines the SmallVector class.
static cl::opt< bool > UseRegistersForGCPointersInLandingPad("use-registers-for-gc-values-in-landing-pad", cl::Hidden, cl::init(false), cl::desc("Allow using registers for gc pointer in landing pad"))
static void lowerIncomingStatepointValue(SDValue Incoming, bool RequireSpillSlot, SmallVectorImpl< SDValue > &Ops, SmallVectorImpl< MachineMemOperand * > &MemRefs, SelectionDAGBuilder &Builder)
Lower a single value incoming to a statepoint node.
static std::optional< int > findPreviousSpillSlot(const Value *Val, SelectionDAGBuilder &Builder, int LookUpDepth)
Utility function for reservePreviousStackSlotForValue.
static void pushStackMapConstant(SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder, uint64_t Value)
static bool isGCValue(const Value *V, SelectionDAGBuilder &Builder)
Return true if value V represents the GC value.
static bool willLowerDirectly(SDValue Incoming)
Return true if-and-only-if the given SDValue can be lowered as either a constant argument or a stack ...
static void lowerStatepointMetaArgs(SmallVectorImpl< SDValue > &Ops, SmallVectorImpl< MachineMemOperand * > &MemRefs, SmallVectorImpl< SDValue > &GCPtrs, DenseMap< SDValue, int > &LowerAsVReg, SelectionDAGBuilder::StatepointLoweringInfo &SI, SelectionDAGBuilder &Builder)
Lower deopt state and gc pointer arguments of the statepoint.
static std::pair< const GCResultInst *, const GCResultInst * > getGCResultLocality(const GCStatepointInst &S)
Return two gc.results if present.
static cl::opt< bool > UseRegistersForDeoptValues("use-registers-for-deopt-values", cl::Hidden, cl::init(false), cl::desc("Allow using registers for non pointer deopt args"))
static cl::opt< unsigned > MaxRegistersForGCPointers("max-registers-for-gc-values", cl::Hidden, cl::init(0), cl::desc("Max number of VRegs allowed to pass GC pointer meta args in"))
static void reservePreviousStackSlotForValue(const Value *IncomingValue, SelectionDAGBuilder &Builder)
Try to find existing copies of the incoming values in stack slots used for statepoint spilling.
FunctionLoweringInfo::StatepointRelocationRecord RecordType
static MachineMemOperand * getMachineMemOperand(MachineFunction &MF, FrameIndexSDNode &FI)
static std::pair< SDValue, SDNode * > lowerCallFromStatepointLoweringInfo(SelectionDAGBuilder::StatepointLoweringInfo &SI, SelectionDAGBuilder &Builder)
Extract call from statepoint, lower it and return pointer to the call node.
static std::tuple< SDValue, SDValue, MachineMemOperand * > spillIncomingStatepointValue(SDValue Incoming, SDValue Chain, SelectionDAGBuilder &Builder)
Spill a value incoming to the statepoint.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
This file describes how to lower LLVM code to machine code.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
This class represents a no-op cast from one type to another.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
This class represents a function call, abstracting a target machine's calling convention.
unsigned size() const
Definition: DenseMap.h:99
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
Register CreateRegs(const Value *V)
DenseMap< const Instruction *, StatepointSpillMapTy > StatepointRelocationMaps
int getArgumentFrameIndex(const Argument *A)
getArgumentFrameIndex - Get frame index for the byval argument.
DenseMap< const Value *, Register > ValueMap
ValueMap - Since we emit code for the function a basic block at a time, we must remember which virtua...
SmallVector< unsigned, 50 > StatepointStackSlots
StatepointStackSlots - A list of temporary stack slots (frame indices) used to spill values at a stat...
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:113
const Value * getStatepoint() const
The statepoint with which this gc.relocate is associated.
Represents calls to the gc.relocate intrinsic.
Value * getDerivedPtr() const
Represents calls to the gc.result intrinsic.
Represents a gc.statepoint intrinsic call.
Definition: Statepoint.h:61
bool useStatepoints() const
Returns true if this strategy is expecting the use of gc.statepoints, and false otherwise.
Definition: GCStrategy.h:92
The landingpad instruction holds all of the information necessary to generate correct exception handl...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void markAsStatepointSpillSlotObjectIndex(int ObjectIdx)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
A description of a memory reference used in the backend.
@ MOVolatile
The memory access is volatile.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
An SDNode that represents everything that will be needed to construct a MachineInstr.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
SDNode * getGluedNode() const
If this node has a glue operand, return the node to which the glue operand points.
op_iterator op_end() const
op_iterator op_begin() const
Represents a use of a SDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
void dump() const
EVT getValueType() const
Return the ValueType of the referenced return value.
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
MVT getFrameIndexTy()
Returns the type of FrameIndex and TargetFrameIndex nodes.
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
void LowerCallSiteWithDeoptBundleImpl(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB, bool VarArgDisallowed, bool ForceVoidReturnTy)
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
SDValue LowerAsSTATEPOINT(StatepointLoweringInfo &SI)
Lower SLI into a STATEPOINT instruction.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:565
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:492
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
void DeleteNode(SDNode *N)
Remove the specified node from the system.
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:486
SDValue getTargetFrameIndex(int FI, EVT VT)
Definition: SelectionDAG.h:741
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:487
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:690
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:481
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVMContext * getContext() const
Definition: SelectionDAG.h:499
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
Definition: SelectionDAG.h:574
SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:568
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition: SetVector.h:87
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
SmallBitVector & set()
bool test(unsigned Idx) const
void clear()
Clear all bits.
size_type size() const
Returns the number of bits in this bitvector.
void resize(unsigned N, bool t=false)
Grow or shrink the bitvector.
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:166
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:179
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:818
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
void clear()
Clear the memory usage of this object.
SDValue getLocation(SDValue Val)
Returns the spill location of a value incoming to the current statepoint.
SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder)
Get a stack slot we can use to store an value of type ValueType.
void scheduleRelocCall(const GCRelocateInst &RelocCall)
Record the fact that we expect to encounter a given gc_relocate before the next statepoint.
void setLocation(SDValue Val, SDValue Location)
void relocCallVisited(const GCRelocateInst &RelocCall)
Remove this gc_relocate from the list we're expecting to see before the next statepoint.
void startNewStatepoint(SelectionDAGBuilder &Builder)
Reset all state tracking for a newly encountered safepoint.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
TargetOptions Options
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getVoidTy(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
iterator_range< user_iterator > users()
Definition: Value.h:421
const ParentTy * getParent() const
Definition: ilist_node.h:32
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition: CallingConv.h:60
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1052
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1123
@ GC_TRANSITION_START
GC_TRANSITION_START/GC_TRANSITION_END - These operators mark the beginning and end of GC transition s...
Definition: ISDOpcodes.h:1334
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:215
@ TRAP
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:1229
@ GC_TRANSITION_END
Definition: ISDOpcodes.h:1335
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1742
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2067
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS)
Parse out statepoint directives from the function attributes present in AS.
Definition: Statepoint.cpp:24
@ MaskAll
A bitmask that includes all valid flags.
@ DeoptLiveIn
Mark the deopt arguments associated with the statepoint as only being "live-in".
@ GCTransition
Indicates that this statepoint is a transition from GC-aware code to code that is not GC-aware.
Extended Value Type.
Definition: ValueTypes.h:34
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:358
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:167
Helper object to track which of three possible relocation mechanisms are used for a particular value ...
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
Describes a gc.statepoint or a gc.statepoint like thing for the purposes of lowering into a STATEPOIN...
static const uint64_t DeoptBundleStatepointID
Definition: Statepoint.h:240