139d628a0SDimitry Andric //===-- StatepointLowering.cpp - SDAGBuilder's statepoint code -----------===// 239d628a0SDimitry Andric // 339d628a0SDimitry Andric // The LLVM Compiler Infrastructure 439d628a0SDimitry Andric // 539d628a0SDimitry Andric // This file is distributed under the University of Illinois Open Source 639d628a0SDimitry Andric // License. See LICENSE.TXT for details. 739d628a0SDimitry Andric // 839d628a0SDimitry Andric //===----------------------------------------------------------------------===// 939d628a0SDimitry Andric // 1039d628a0SDimitry Andric // This file includes support code use by SelectionDAGBuilder when lowering a 1139d628a0SDimitry Andric // statepoint sequence in SelectionDAG IR. 1239d628a0SDimitry Andric // 1339d628a0SDimitry Andric //===----------------------------------------------------------------------===// 1439d628a0SDimitry Andric 1539d628a0SDimitry Andric #include "StatepointLowering.h" 1639d628a0SDimitry Andric #include "SelectionDAGBuilder.h" 1739d628a0SDimitry Andric #include "llvm/ADT/SmallSet.h" 1839d628a0SDimitry Andric #include "llvm/ADT/Statistic.h" 1939d628a0SDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h" 207d523365SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 21ff0cc061SDimitry Andric #include "llvm/CodeGen/GCMetadata.h" 2239d628a0SDimitry Andric #include "llvm/CodeGen/GCStrategy.h" 2339d628a0SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 2439d628a0SDimitry Andric #include "llvm/CodeGen/StackMaps.h" 2539d628a0SDimitry Andric #include "llvm/IR/CallingConv.h" 2639d628a0SDimitry Andric #include "llvm/IR/Instructions.h" 2739d628a0SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 2839d628a0SDimitry Andric #include "llvm/IR/Intrinsics.h" 2939d628a0SDimitry Andric #include "llvm/IR/Statepoint.h" 3039d628a0SDimitry Andric #include "llvm/Target/TargetLowering.h" 3139d628a0SDimitry Andric #include <algorithm> 3239d628a0SDimitry Andric using namespace llvm; 3339d628a0SDimitry Andric 3439d628a0SDimitry Andric #define DEBUG_TYPE "statepoint-lowering" 3539d628a0SDimitry Andric 3639d628a0SDimitry Andric STATISTIC(NumSlotsAllocatedForStatepoints, 3739d628a0SDimitry Andric "Number of stack slots allocated for statepoints"); 3839d628a0SDimitry Andric STATISTIC(NumOfStatepoints, "Number of statepoint nodes encountered"); 3939d628a0SDimitry Andric STATISTIC(StatepointMaxSlotsRequired, 4039d628a0SDimitry Andric "Maximum number of stack slots required for a singe statepoint"); 4139d628a0SDimitry Andric 42ff0cc061SDimitry Andric static void pushStackMapConstant(SmallVectorImpl<SDValue>& Ops, 43ff0cc061SDimitry Andric SelectionDAGBuilder &Builder, uint64_t Value) { 44ff0cc061SDimitry Andric SDLoc L = Builder.getCurSDLoc(); 45ff0cc061SDimitry Andric Ops.push_back(Builder.DAG.getTargetConstant(StackMaps::ConstantOp, L, 46ff0cc061SDimitry Andric MVT::i64)); 47ff0cc061SDimitry Andric Ops.push_back(Builder.DAG.getTargetConstant(Value, L, MVT::i64)); 48ff0cc061SDimitry Andric } 49ff0cc061SDimitry Andric 50ff0cc061SDimitry Andric void StatepointLoweringState::startNewStatepoint(SelectionDAGBuilder &Builder) { 5139d628a0SDimitry Andric // Consistency check 5239d628a0SDimitry Andric assert(PendingGCRelocateCalls.empty() && 5339d628a0SDimitry Andric "Trying to visit statepoint before finished processing previous one"); 5439d628a0SDimitry Andric Locations.clear(); 5539d628a0SDimitry Andric NextSlotToAllocate = 0; 563ca95b02SDimitry Andric // Need to resize this on each safepoint - we need the two to stay in sync and 573ca95b02SDimitry Andric // the clear patterns of a SelectionDAGBuilder have no relation to 58d88c1a5aSDimitry Andric // FunctionLoweringInfo. Also need to ensure used bits get cleared. 59d88c1a5aSDimitry Andric AllocatedStackSlots.clear(); 6039d628a0SDimitry Andric AllocatedStackSlots.resize(Builder.FuncInfo.StatepointStackSlots.size()); 6139d628a0SDimitry Andric } 62ff0cc061SDimitry Andric 6339d628a0SDimitry Andric void StatepointLoweringState::clear() { 6439d628a0SDimitry Andric Locations.clear(); 6539d628a0SDimitry Andric AllocatedStackSlots.clear(); 6639d628a0SDimitry Andric assert(PendingGCRelocateCalls.empty() && 6739d628a0SDimitry Andric "cleared before statepoint sequence completed"); 6839d628a0SDimitry Andric } 6939d628a0SDimitry Andric 7039d628a0SDimitry Andric SDValue 7139d628a0SDimitry Andric StatepointLoweringState::allocateStackSlot(EVT ValueType, 7239d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 7339d628a0SDimitry Andric NumSlotsAllocatedForStatepoints++; 74d88c1a5aSDimitry Andric MachineFrameInfo &MFI = Builder.DAG.getMachineFunction().getFrameInfo(); 7539d628a0SDimitry Andric 763ca95b02SDimitry Andric unsigned SpillSize = ValueType.getSizeInBits() / 8; 773ca95b02SDimitry Andric assert((SpillSize * 8) == ValueType.getSizeInBits() && "Size not in bytes?"); 7839d628a0SDimitry Andric 793ca95b02SDimitry Andric // First look for a previously created stack slot which is not in 803ca95b02SDimitry Andric // use (accounting for the fact arbitrary slots may already be 813ca95b02SDimitry Andric // reserved), or to create a new stack slot and use it. 823ca95b02SDimitry Andric 8339d628a0SDimitry Andric const size_t NumSlots = AllocatedStackSlots.size(); 843ca95b02SDimitry Andric assert(NextSlotToAllocate <= NumSlots && "Broken invariant"); 8539d628a0SDimitry Andric 86d88c1a5aSDimitry Andric assert(AllocatedStackSlots.size() == 87d88c1a5aSDimitry Andric Builder.FuncInfo.StatepointStackSlots.size() && 883ca95b02SDimitry Andric "Broken invariant"); 893ca95b02SDimitry Andric 903ca95b02SDimitry Andric for (; NextSlotToAllocate < NumSlots; NextSlotToAllocate++) { 913ca95b02SDimitry Andric if (!AllocatedStackSlots.test(NextSlotToAllocate)) { 923ca95b02SDimitry Andric const int FI = Builder.FuncInfo.StatepointStackSlots[NextSlotToAllocate]; 93d88c1a5aSDimitry Andric if (MFI.getObjectSize(FI) == SpillSize) { 943ca95b02SDimitry Andric AllocatedStackSlots.set(NextSlotToAllocate); 95d88c1a5aSDimitry Andric // TODO: Is ValueType the right thing to use here? 963ca95b02SDimitry Andric return Builder.DAG.getFrameIndex(FI, ValueType); 9739d628a0SDimitry Andric } 983ca95b02SDimitry Andric } 993ca95b02SDimitry Andric } 1003ca95b02SDimitry Andric 1013ca95b02SDimitry Andric // Couldn't find a free slot, so create a new one: 10239d628a0SDimitry Andric 10339d628a0SDimitry Andric SDValue SpillSlot = Builder.DAG.CreateStackTemporary(ValueType); 10439d628a0SDimitry Andric const unsigned FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); 105d88c1a5aSDimitry Andric MFI.markAsStatepointSpillSlotObjectIndex(FI); 1067d523365SDimitry Andric 10739d628a0SDimitry Andric Builder.FuncInfo.StatepointStackSlots.push_back(FI); 108d88c1a5aSDimitry Andric AllocatedStackSlots.resize(AllocatedStackSlots.size()+1, true); 109d88c1a5aSDimitry Andric assert(AllocatedStackSlots.size() == 110d88c1a5aSDimitry Andric Builder.FuncInfo.StatepointStackSlots.size() && 111d88c1a5aSDimitry Andric "Broken invariant"); 1123ca95b02SDimitry Andric 1133ca95b02SDimitry Andric StatepointMaxSlotsRequired = std::max<unsigned long>( 1143ca95b02SDimitry Andric StatepointMaxSlotsRequired, Builder.FuncInfo.StatepointStackSlots.size()); 1153ca95b02SDimitry Andric 11639d628a0SDimitry Andric return SpillSlot; 11739d628a0SDimitry Andric } 11839d628a0SDimitry Andric 1198f0fd8f6SDimitry Andric /// Utility function for reservePreviousStackSlotForValue. Tries to find 1208f0fd8f6SDimitry Andric /// stack slot index to which we have spilled value for previous statepoints. 1218f0fd8f6SDimitry Andric /// LookUpDepth specifies maximum DFS depth this function is allowed to look. 1228f0fd8f6SDimitry Andric static Optional<int> findPreviousSpillSlot(const Value *Val, 1238f0fd8f6SDimitry Andric SelectionDAGBuilder &Builder, 1248f0fd8f6SDimitry Andric int LookUpDepth) { 1257d523365SDimitry Andric // Can not look any further - give up now 1268f0fd8f6SDimitry Andric if (LookUpDepth <= 0) 1273ca95b02SDimitry Andric return None; 1288f0fd8f6SDimitry Andric 1298f0fd8f6SDimitry Andric // Spill location is known for gc relocates 1304d0b32cdSDimitry Andric if (const auto *Relocate = dyn_cast<GCRelocateInst>(Val)) { 1313ca95b02SDimitry Andric const auto &SpillMap = 1323ca95b02SDimitry Andric Builder.FuncInfo.StatepointSpillMaps[Relocate->getStatepoint()]; 1338f0fd8f6SDimitry Andric 1344d0b32cdSDimitry Andric auto It = SpillMap.find(Relocate->getDerivedPtr()); 1358f0fd8f6SDimitry Andric if (It == SpillMap.end()) 1363ca95b02SDimitry Andric return None; 1378f0fd8f6SDimitry Andric 1388f0fd8f6SDimitry Andric return It->second; 1398f0fd8f6SDimitry Andric } 1408f0fd8f6SDimitry Andric 1418f0fd8f6SDimitry Andric // Look through bitcast instructions. 1423ca95b02SDimitry Andric if (const BitCastInst *Cast = dyn_cast<BitCastInst>(Val)) 1438f0fd8f6SDimitry Andric return findPreviousSpillSlot(Cast->getOperand(0), Builder, LookUpDepth - 1); 1448f0fd8f6SDimitry Andric 1458f0fd8f6SDimitry Andric // Look through phi nodes 1468f0fd8f6SDimitry Andric // All incoming values should have same known stack slot, otherwise result 1478f0fd8f6SDimitry Andric // is unknown. 1488f0fd8f6SDimitry Andric if (const PHINode *Phi = dyn_cast<PHINode>(Val)) { 1498f0fd8f6SDimitry Andric Optional<int> MergedResult = None; 1508f0fd8f6SDimitry Andric 1518f0fd8f6SDimitry Andric for (auto &IncomingValue : Phi->incoming_values()) { 1528f0fd8f6SDimitry Andric Optional<int> SpillSlot = 1538f0fd8f6SDimitry Andric findPreviousSpillSlot(IncomingValue, Builder, LookUpDepth - 1); 1548f0fd8f6SDimitry Andric if (!SpillSlot.hasValue()) 1553ca95b02SDimitry Andric return None; 1568f0fd8f6SDimitry Andric 1578f0fd8f6SDimitry Andric if (MergedResult.hasValue() && *MergedResult != *SpillSlot) 1583ca95b02SDimitry Andric return None; 1598f0fd8f6SDimitry Andric 1608f0fd8f6SDimitry Andric MergedResult = SpillSlot; 1618f0fd8f6SDimitry Andric } 1628f0fd8f6SDimitry Andric return MergedResult; 1638f0fd8f6SDimitry Andric } 1648f0fd8f6SDimitry Andric 1658f0fd8f6SDimitry Andric // TODO: We can do better for PHI nodes. In cases like this: 1668f0fd8f6SDimitry Andric // ptr = phi(relocated_pointer, not_relocated_pointer) 1678f0fd8f6SDimitry Andric // statepoint(ptr) 1688f0fd8f6SDimitry Andric // We will return that stack slot for ptr is unknown. And later we might 1698f0fd8f6SDimitry Andric // assign different stack slots for ptr and relocated_pointer. This limits 1708f0fd8f6SDimitry Andric // llvm's ability to remove redundant stores. 1718f0fd8f6SDimitry Andric // Unfortunately it's hard to accomplish in current infrastructure. 1728f0fd8f6SDimitry Andric // We use this function to eliminate spill store completely, while 1738f0fd8f6SDimitry Andric // in example we still need to emit store, but instead of any location 1748f0fd8f6SDimitry Andric // we need to use special "preferred" location. 1758f0fd8f6SDimitry Andric 1768f0fd8f6SDimitry Andric // TODO: handle simple updates. If a value is modified and the original 1778f0fd8f6SDimitry Andric // value is no longer live, it would be nice to put the modified value in the 1788f0fd8f6SDimitry Andric // same slot. This allows folding of the memory accesses for some 1798f0fd8f6SDimitry Andric // instructions types (like an increment). 1808f0fd8f6SDimitry Andric // statepoint (i) 1818f0fd8f6SDimitry Andric // i1 = i+1 1828f0fd8f6SDimitry Andric // statepoint (i1) 1838f0fd8f6SDimitry Andric // However we need to be careful for cases like this: 1848f0fd8f6SDimitry Andric // statepoint(i) 1858f0fd8f6SDimitry Andric // i1 = i+1 1868f0fd8f6SDimitry Andric // statepoint(i, i1) 1878f0fd8f6SDimitry Andric // Here we want to reserve spill slot for 'i', but not for 'i+1'. If we just 1888f0fd8f6SDimitry Andric // put handling of simple modifications in this function like it's done 1898f0fd8f6SDimitry Andric // for bitcasts we might end up reserving i's slot for 'i+1' because order in 1908f0fd8f6SDimitry Andric // which we visit values is unspecified. 1918f0fd8f6SDimitry Andric 1928f0fd8f6SDimitry Andric // Don't know any information about this instruction 1933ca95b02SDimitry Andric return None; 1948f0fd8f6SDimitry Andric } 1958f0fd8f6SDimitry Andric 19639d628a0SDimitry Andric /// Try to find existing copies of the incoming values in stack slots used for 19739d628a0SDimitry Andric /// statepoint spilling. If we can find a spill slot for the incoming value, 19839d628a0SDimitry Andric /// mark that slot as allocated, and reuse the same slot for this safepoint. 1997d523365SDimitry Andric /// This helps to avoid series of loads and stores that only serve to reshuffle 20039d628a0SDimitry Andric /// values on the stack between calls. 2018f0fd8f6SDimitry Andric static void reservePreviousStackSlotForValue(const Value *IncomingValue, 20239d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 20339d628a0SDimitry Andric 2048f0fd8f6SDimitry Andric SDValue Incoming = Builder.getValue(IncomingValue); 2058f0fd8f6SDimitry Andric 20639d628a0SDimitry Andric if (isa<ConstantSDNode>(Incoming) || isa<FrameIndexSDNode>(Incoming)) { 20739d628a0SDimitry Andric // We won't need to spill this, so no need to check for previously 20839d628a0SDimitry Andric // allocated stack slots 20939d628a0SDimitry Andric return; 21039d628a0SDimitry Andric } 21139d628a0SDimitry Andric 2128f0fd8f6SDimitry Andric SDValue OldLocation = Builder.StatepointLowering.getLocation(Incoming); 2138f0fd8f6SDimitry Andric if (OldLocation.getNode()) 2143ca95b02SDimitry Andric // Duplicates in input 21539d628a0SDimitry Andric return; 21639d628a0SDimitry Andric 2178f0fd8f6SDimitry Andric const int LookUpDepth = 6; 2188f0fd8f6SDimitry Andric Optional<int> Index = 2198f0fd8f6SDimitry Andric findPreviousSpillSlot(IncomingValue, Builder, LookUpDepth); 2208f0fd8f6SDimitry Andric if (!Index.hasValue()) 22139d628a0SDimitry Andric return; 2228f0fd8f6SDimitry Andric 2233ca95b02SDimitry Andric const auto &StatepointSlots = Builder.FuncInfo.StatepointStackSlots; 2243ca95b02SDimitry Andric 2253ca95b02SDimitry Andric auto SlotIt = find(StatepointSlots, *Index); 2263ca95b02SDimitry Andric assert(SlotIt != StatepointSlots.end() && 2273ca95b02SDimitry Andric "Value spilled to the unknown stack slot"); 2288f0fd8f6SDimitry Andric 22939d628a0SDimitry Andric // This is one of our dedicated lowering slots 2303ca95b02SDimitry Andric const int Offset = std::distance(StatepointSlots.begin(), SlotIt); 23139d628a0SDimitry Andric if (Builder.StatepointLowering.isStackSlotAllocated(Offset)) { 23239d628a0SDimitry Andric // stack slot already assigned to someone else, can't use it! 23339d628a0SDimitry Andric // TODO: currently we reserve space for gc arguments after doing 23439d628a0SDimitry Andric // normal allocation for deopt arguments. We should reserve for 23539d628a0SDimitry Andric // _all_ deopt and gc arguments, then start allocating. This 23639d628a0SDimitry Andric // will prevent some moves being inserted when vm state changes, 23739d628a0SDimitry Andric // but gc state doesn't between two calls. 23839d628a0SDimitry Andric return; 23939d628a0SDimitry Andric } 24039d628a0SDimitry Andric // Reserve this stack slot 24139d628a0SDimitry Andric Builder.StatepointLowering.reserveStackSlot(Offset); 24239d628a0SDimitry Andric 24339d628a0SDimitry Andric // Cache this slot so we find it when going through the normal 24439d628a0SDimitry Andric // assignment loop. 2458f0fd8f6SDimitry Andric SDValue Loc = Builder.DAG.getTargetFrameIndex(*Index, Incoming.getValueType()); 24639d628a0SDimitry Andric Builder.StatepointLowering.setLocation(Incoming, Loc); 24739d628a0SDimitry Andric } 24839d628a0SDimitry Andric 24939d628a0SDimitry Andric /// Remove any duplicate (as SDValues) from the derived pointer pairs. This 25039d628a0SDimitry Andric /// is not required for correctness. It's purpose is to reduce the size of 25139d628a0SDimitry Andric /// StackMap section. It has no effect on the number of spill slots required 25239d628a0SDimitry Andric /// or the actual lowering. 2533ca95b02SDimitry Andric static void 2543ca95b02SDimitry Andric removeDuplicateGCPtrs(SmallVectorImpl<const Value *> &Bases, 25539d628a0SDimitry Andric SmallVectorImpl<const Value *> &Ptrs, 2563ca95b02SDimitry Andric SmallVectorImpl<const GCRelocateInst *> &Relocs, 2573ca95b02SDimitry Andric SelectionDAGBuilder &Builder, 2583ca95b02SDimitry Andric FunctionLoweringInfo::StatepointSpillMap &SSM) { 2593ca95b02SDimitry Andric DenseMap<SDValue, const Value *> Seen; 26039d628a0SDimitry Andric 2613ca95b02SDimitry Andric SmallVector<const Value *, 64> NewBases, NewPtrs; 2623ca95b02SDimitry Andric SmallVector<const GCRelocateInst *, 64> NewRelocs; 2633ca95b02SDimitry Andric for (size_t i = 0, e = Ptrs.size(); i < e; i++) { 26439d628a0SDimitry Andric SDValue SD = Builder.getValue(Ptrs[i]); 2653ca95b02SDimitry Andric auto SeenIt = Seen.find(SD); 2663ca95b02SDimitry Andric 2673ca95b02SDimitry Andric if (SeenIt == Seen.end()) { 26839d628a0SDimitry Andric // Only add non-duplicates 26939d628a0SDimitry Andric NewBases.push_back(Bases[i]); 27039d628a0SDimitry Andric NewPtrs.push_back(Ptrs[i]); 27139d628a0SDimitry Andric NewRelocs.push_back(Relocs[i]); 2723ca95b02SDimitry Andric Seen[SD] = Ptrs[i]; 2733ca95b02SDimitry Andric } else { 2743ca95b02SDimitry Andric // Duplicate pointer found, note in SSM and move on: 2753ca95b02SDimitry Andric SSM.DuplicateMap[Ptrs[i]] = SeenIt->second; 27639d628a0SDimitry Andric } 27739d628a0SDimitry Andric } 27839d628a0SDimitry Andric assert(Bases.size() >= NewBases.size()); 27939d628a0SDimitry Andric assert(Ptrs.size() >= NewPtrs.size()); 28039d628a0SDimitry Andric assert(Relocs.size() >= NewRelocs.size()); 28139d628a0SDimitry Andric Bases = NewBases; 28239d628a0SDimitry Andric Ptrs = NewPtrs; 28339d628a0SDimitry Andric Relocs = NewRelocs; 28439d628a0SDimitry Andric assert(Ptrs.size() == Bases.size()); 28539d628a0SDimitry Andric assert(Ptrs.size() == Relocs.size()); 28639d628a0SDimitry Andric } 28739d628a0SDimitry Andric 28839d628a0SDimitry Andric /// Extract call from statepoint, lower it and return pointer to the 28939d628a0SDimitry Andric /// call node. Also update NodeMap so that getValue(statepoint) will 29039d628a0SDimitry Andric /// reference lowered call result 2913ca95b02SDimitry Andric static std::pair<SDValue, SDNode *> lowerCallFromStatepointLoweringInfo( 2923ca95b02SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI, 2933ca95b02SDimitry Andric SelectionDAGBuilder &Builder, SmallVectorImpl<SDValue> &PendingExports) { 294ff0cc061SDimitry Andric 295ff0cc061SDimitry Andric SDValue ReturnValue, CallEndVal; 2963ca95b02SDimitry Andric std::tie(ReturnValue, CallEndVal) = 2973ca95b02SDimitry Andric Builder.lowerInvokable(SI.CLI, SI.EHPadBB); 298ff0cc061SDimitry Andric SDNode *CallEnd = CallEndVal.getNode(); 299ff0cc061SDimitry Andric 300ff0cc061SDimitry Andric // Get a call instruction from the call sequence chain. Tail calls are not 301ff0cc061SDimitry Andric // allowed. The following code is essentially reverse engineering X86's 302ff0cc061SDimitry Andric // LowerCallTo. 303ff0cc061SDimitry Andric // 304ff0cc061SDimitry Andric // We are expecting DAG to have the following form: 305ff0cc061SDimitry Andric // 306ff0cc061SDimitry Andric // ch = eh_label (only in case of invoke statepoint) 307ff0cc061SDimitry Andric // ch, glue = callseq_start ch 308ff0cc061SDimitry Andric // ch, glue = X86::Call ch, glue 309ff0cc061SDimitry Andric // ch, glue = callseq_end ch, glue 310ff0cc061SDimitry Andric // get_return_value ch, glue 311ff0cc061SDimitry Andric // 3127d523365SDimitry Andric // get_return_value can either be a sequence of CopyFromReg instructions 3137d523365SDimitry Andric // to grab the return value from the return register(s), or it can be a LOAD 3147d523365SDimitry Andric // to load a value returned by reference via a stack slot. 315ff0cc061SDimitry Andric 3163ca95b02SDimitry Andric bool HasDef = !SI.CLI.RetTy->isVoidTy(); 3177d523365SDimitry Andric if (HasDef) { 3187d523365SDimitry Andric if (CallEnd->getOpcode() == ISD::LOAD) 319ff0cc061SDimitry Andric CallEnd = CallEnd->getOperand(0).getNode(); 3207d523365SDimitry Andric else 3217d523365SDimitry Andric while (CallEnd->getOpcode() == ISD::CopyFromReg) 3227d523365SDimitry Andric CallEnd = CallEnd->getOperand(0).getNode(); 3237d523365SDimitry Andric } 324ff0cc061SDimitry Andric 325ff0cc061SDimitry Andric assert(CallEnd->getOpcode() == ISD::CALLSEQ_END && "expected!"); 3263ca95b02SDimitry Andric return std::make_pair(ReturnValue, CallEnd->getOperand(0).getNode()); 32739d628a0SDimitry Andric } 32839d628a0SDimitry Andric 32939d628a0SDimitry Andric /// Spill a value incoming to the statepoint. It might be either part of 33039d628a0SDimitry Andric /// vmstate 33139d628a0SDimitry Andric /// or gcstate. In both cases unconditionally spill it on the stack unless it 33239d628a0SDimitry Andric /// is a null constant. Return pair with first element being frame index 33339d628a0SDimitry Andric /// containing saved value and second element with outgoing chain from the 33439d628a0SDimitry Andric /// emitted store 33539d628a0SDimitry Andric static std::pair<SDValue, SDValue> 33639d628a0SDimitry Andric spillIncomingStatepointValue(SDValue Incoming, SDValue Chain, 33739d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 33839d628a0SDimitry Andric SDValue Loc = Builder.StatepointLowering.getLocation(Incoming); 33939d628a0SDimitry Andric 34039d628a0SDimitry Andric // Emit new store if we didn't do it for this ptr before 34139d628a0SDimitry Andric if (!Loc.getNode()) { 34239d628a0SDimitry Andric Loc = Builder.StatepointLowering.allocateStackSlot(Incoming.getValueType(), 34339d628a0SDimitry Andric Builder); 34439d628a0SDimitry Andric int Index = cast<FrameIndexSDNode>(Loc)->getIndex(); 34539d628a0SDimitry Andric // We use TargetFrameIndex so that isel will not select it into LEA 34639d628a0SDimitry Andric Loc = Builder.DAG.getTargetFrameIndex(Index, Incoming.getValueType()); 34739d628a0SDimitry Andric 34839d628a0SDimitry Andric // TODO: We can create TokenFactor node instead of 34939d628a0SDimitry Andric // chaining stores one after another, this may allow 35039d628a0SDimitry Andric // a bit more optimal scheduling for them 3513ca95b02SDimitry Andric 3523ca95b02SDimitry Andric #ifndef NDEBUG 3533ca95b02SDimitry Andric // Right now we always allocate spill slots that are of the same 3543ca95b02SDimitry Andric // size as the value we're about to spill (the size of spillee can 3553ca95b02SDimitry Andric // vary since we spill vectors of pointers too). At some point we 3563ca95b02SDimitry Andric // can consider allowing spills of smaller values to larger slots 3573ca95b02SDimitry Andric // (i.e. change the '==' in the assert below to a '>='). 358d88c1a5aSDimitry Andric MachineFrameInfo &MFI = Builder.DAG.getMachineFunction().getFrameInfo(); 359d88c1a5aSDimitry Andric assert((MFI.getObjectSize(Index) * 8) == Incoming.getValueSizeInBits() && 3603ca95b02SDimitry Andric "Bad spill: stack slot does not match!"); 3613ca95b02SDimitry Andric #endif 3623ca95b02SDimitry Andric 36339d628a0SDimitry Andric Chain = Builder.DAG.getStore(Chain, Builder.getCurSDLoc(), Incoming, Loc, 3647d523365SDimitry Andric MachinePointerInfo::getFixedStack( 3653ca95b02SDimitry Andric Builder.DAG.getMachineFunction(), Index)); 36639d628a0SDimitry Andric 36739d628a0SDimitry Andric Builder.StatepointLowering.setLocation(Incoming, Loc); 36839d628a0SDimitry Andric } 36939d628a0SDimitry Andric 37039d628a0SDimitry Andric assert(Loc.getNode()); 37139d628a0SDimitry Andric return std::make_pair(Loc, Chain); 37239d628a0SDimitry Andric } 37339d628a0SDimitry Andric 37439d628a0SDimitry Andric /// Lower a single value incoming to a statepoint node. This value can be 37539d628a0SDimitry Andric /// either a deopt value or a gc value, the handling is the same. We special 37639d628a0SDimitry Andric /// case constants and allocas, then fall back to spilling if required. 377d88c1a5aSDimitry Andric static void lowerIncomingStatepointValue(SDValue Incoming, bool LiveInOnly, 37839d628a0SDimitry Andric SmallVectorImpl<SDValue> &Ops, 37939d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 38039d628a0SDimitry Andric SDValue Chain = Builder.getRoot(); 38139d628a0SDimitry Andric 38239d628a0SDimitry Andric if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Incoming)) { 38339d628a0SDimitry Andric // If the original value was a constant, make sure it gets recorded as 38439d628a0SDimitry Andric // such in the stackmap. This is required so that the consumer can 38539d628a0SDimitry Andric // parse any internal format to the deopt state. It also handles null 386444ed5c5SDimitry Andric // pointers and other constant pointers in GC states. Note the constant 387444ed5c5SDimitry Andric // vectors do not appear to actually hit this path and that anything larger 388444ed5c5SDimitry Andric // than an i64 value (not type!) will fail asserts here. 389ff0cc061SDimitry Andric pushStackMapConstant(Ops, Builder, C->getSExtValue()); 39039d628a0SDimitry Andric } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { 391ff0cc061SDimitry Andric // This handles allocas as arguments to the statepoint (this is only 392ff0cc061SDimitry Andric // really meaningful for a deopt value. For GC, we'd be trying to 393ff0cc061SDimitry Andric // relocate the address of the alloca itself?) 394ff0cc061SDimitry Andric Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), 395ff0cc061SDimitry Andric Incoming.getValueType())); 396d88c1a5aSDimitry Andric } else if (LiveInOnly) { 397d88c1a5aSDimitry Andric // If this value is live in (not live-on-return, or live-through), we can 398d88c1a5aSDimitry Andric // treat it the same way patchpoint treats it's "live in" values. We'll 399d88c1a5aSDimitry Andric // end up folding some of these into stack references, but they'll be 400d88c1a5aSDimitry Andric // handled by the register allocator. Note that we do not have the notion 401d88c1a5aSDimitry Andric // of a late use so these values might be placed in registers which are 402d88c1a5aSDimitry Andric // clobbered by the call. This is fine for live-in. 403d88c1a5aSDimitry Andric Ops.push_back(Incoming); 40439d628a0SDimitry Andric } else { 40539d628a0SDimitry Andric // Otherwise, locate a spill slot and explicitly spill it so it 40639d628a0SDimitry Andric // can be found by the runtime later. We currently do not support 40739d628a0SDimitry Andric // tracking values through callee saved registers to their eventual 40839d628a0SDimitry Andric // spill location. This would be a useful optimization, but would 40939d628a0SDimitry Andric // need to be optional since it requires a lot of complexity on the 41039d628a0SDimitry Andric // runtime side which not all would support. 4113ca95b02SDimitry Andric auto Res = spillIncomingStatepointValue(Incoming, Chain, Builder); 41239d628a0SDimitry Andric Ops.push_back(Res.first); 41339d628a0SDimitry Andric Chain = Res.second; 41439d628a0SDimitry Andric } 41539d628a0SDimitry Andric 41639d628a0SDimitry Andric Builder.DAG.setRoot(Chain); 41739d628a0SDimitry Andric } 41839d628a0SDimitry Andric 41939d628a0SDimitry Andric /// Lower deopt state and gc pointer arguments of the statepoint. The actual 42039d628a0SDimitry Andric /// lowering is described in lowerIncomingStatepointValue. This function is 42139d628a0SDimitry Andric /// responsible for lowering everything in the right position and playing some 42239d628a0SDimitry Andric /// tricks to avoid redundant stack manipulation where possible. On 42339d628a0SDimitry Andric /// completion, 'Ops' will contain ready to use operands for machine code 42439d628a0SDimitry Andric /// statepoint. The chain nodes will have already been created and the DAG root 42539d628a0SDimitry Andric /// will be set to the last value spilled (if any were). 4263ca95b02SDimitry Andric static void 4273ca95b02SDimitry Andric lowerStatepointMetaArgs(SmallVectorImpl<SDValue> &Ops, 4283ca95b02SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI, 42939d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 4303ca95b02SDimitry Andric // Lower the deopt and gc arguments for this statepoint. Layout will be: 4313ca95b02SDimitry Andric // deopt argument length, deopt arguments.., gc arguments... 43239d628a0SDimitry Andric #ifndef NDEBUG 4333ca95b02SDimitry Andric if (auto *GFI = Builder.GFI) { 43439d628a0SDimitry Andric // Check that each of the gc pointer and bases we've gotten out of the 435444ed5c5SDimitry Andric // safepoint is something the strategy thinks might be a pointer (or vector 436444ed5c5SDimitry Andric // of pointers) into the GC heap. This is basically just here to help catch 437444ed5c5SDimitry Andric // errors during statepoint insertion. TODO: This should actually be in the 438444ed5c5SDimitry Andric // Verifier, but we can't get to the GCStrategy from there (yet). 4393ca95b02SDimitry Andric GCStrategy &S = GFI->getStrategy(); 4403ca95b02SDimitry Andric for (const Value *V : SI.Bases) { 441444ed5c5SDimitry Andric auto Opt = S.isGCManagedPointer(V->getType()->getScalarType()); 44239d628a0SDimitry Andric if (Opt.hasValue()) { 44339d628a0SDimitry Andric assert(Opt.getValue() && 44439d628a0SDimitry Andric "non gc managed base pointer found in statepoint"); 44539d628a0SDimitry Andric } 44639d628a0SDimitry Andric } 4473ca95b02SDimitry Andric for (const Value *V : SI.Ptrs) { 448444ed5c5SDimitry Andric auto Opt = S.isGCManagedPointer(V->getType()->getScalarType()); 44939d628a0SDimitry Andric if (Opt.hasValue()) { 45039d628a0SDimitry Andric assert(Opt.getValue() && 45139d628a0SDimitry Andric "non gc managed derived pointer found in statepoint"); 45239d628a0SDimitry Andric } 45339d628a0SDimitry Andric } 454d88c1a5aSDimitry Andric assert(SI.Bases.size() == SI.Ptrs.size() && "Pointer without base!"); 4553ca95b02SDimitry Andric } else { 4563ca95b02SDimitry Andric assert(SI.Bases.empty() && "No gc specified, so cannot relocate pointers!"); 4573ca95b02SDimitry Andric assert(SI.Ptrs.empty() && "No gc specified, so cannot relocate pointers!"); 45839d628a0SDimitry Andric } 45939d628a0SDimitry Andric #endif 46039d628a0SDimitry Andric 461d88c1a5aSDimitry Andric // Figure out what lowering strategy we're going to use for each part 462d88c1a5aSDimitry Andric // Note: Is is conservatively correct to lower both "live-in" and "live-out" 463d88c1a5aSDimitry Andric // as "live-through". A "live-through" variable is one which is "live-in", 464d88c1a5aSDimitry Andric // "live-out", and live throughout the lifetime of the call (i.e. we can find 465d88c1a5aSDimitry Andric // it from any PC within the transitive callee of the statepoint). In 466d88c1a5aSDimitry Andric // particular, if the callee spills callee preserved registers we may not 467d88c1a5aSDimitry Andric // be able to find a value placed in that register during the call. This is 468d88c1a5aSDimitry Andric // fine for live-out, but not for live-through. If we were willing to make 469d88c1a5aSDimitry Andric // assumptions about the code generator producing the callee, we could 470d88c1a5aSDimitry Andric // potentially allow live-through values in callee saved registers. 471d88c1a5aSDimitry Andric const bool LiveInDeopt = 472d88c1a5aSDimitry Andric SI.StatepointFlags & (uint64_t)StatepointFlags::DeoptLiveIn; 473d88c1a5aSDimitry Andric 474d88c1a5aSDimitry Andric auto isGCValue =[&](const Value *V) { 475d88c1a5aSDimitry Andric return is_contained(SI.Ptrs, V) || is_contained(SI.Bases, V); 476d88c1a5aSDimitry Andric }; 477d88c1a5aSDimitry Andric 47839d628a0SDimitry Andric // Before we actually start lowering (and allocating spill slots for values), 47939d628a0SDimitry Andric // reserve any stack slots which we judge to be profitable to reuse for a 48039d628a0SDimitry Andric // particular value. This is purely an optimization over the code below and 48139d628a0SDimitry Andric // doesn't change semantics at all. It is important for performance that we 48239d628a0SDimitry Andric // reserve slots for both deopt and gc values before lowering either. 4833ca95b02SDimitry Andric for (const Value *V : SI.DeoptState) { 484d88c1a5aSDimitry Andric if (!LiveInDeopt || isGCValue(V)) 4858f0fd8f6SDimitry Andric reservePreviousStackSlotForValue(V, Builder); 48639d628a0SDimitry Andric } 4873ca95b02SDimitry Andric for (unsigned i = 0; i < SI.Bases.size(); ++i) { 4883ca95b02SDimitry Andric reservePreviousStackSlotForValue(SI.Bases[i], Builder); 4893ca95b02SDimitry Andric reservePreviousStackSlotForValue(SI.Ptrs[i], Builder); 49039d628a0SDimitry Andric } 49139d628a0SDimitry Andric 49239d628a0SDimitry Andric // First, prefix the list with the number of unique values to be 49339d628a0SDimitry Andric // lowered. Note that this is the number of *Values* not the 49439d628a0SDimitry Andric // number of SDValues required to lower them. 4953ca95b02SDimitry Andric const int NumVMSArgs = SI.DeoptState.size(); 496ff0cc061SDimitry Andric pushStackMapConstant(Ops, Builder, NumVMSArgs); 49739d628a0SDimitry Andric 4983ca95b02SDimitry Andric // The vm state arguments are lowered in an opaque manner. We do not know 4993ca95b02SDimitry Andric // what type of values are contained within. 5003ca95b02SDimitry Andric for (const Value *V : SI.DeoptState) { 50139d628a0SDimitry Andric SDValue Incoming = Builder.getValue(V); 502d88c1a5aSDimitry Andric const bool LiveInValue = LiveInDeopt && !isGCValue(V); 503d88c1a5aSDimitry Andric lowerIncomingStatepointValue(Incoming, LiveInValue, Ops, Builder); 50439d628a0SDimitry Andric } 50539d628a0SDimitry Andric 50639d628a0SDimitry Andric // Finally, go ahead and lower all the gc arguments. There's no prefixed 50739d628a0SDimitry Andric // length for this one. After lowering, we'll have the base and pointer 50839d628a0SDimitry Andric // arrays interwoven with each (lowered) base pointer immediately followed by 50939d628a0SDimitry Andric // it's (lowered) derived pointer. i.e 51039d628a0SDimitry Andric // (base[0], ptr[0], base[1], ptr[1], ...) 5113ca95b02SDimitry Andric for (unsigned i = 0; i < SI.Bases.size(); ++i) { 5123ca95b02SDimitry Andric const Value *Base = SI.Bases[i]; 513d88c1a5aSDimitry Andric lowerIncomingStatepointValue(Builder.getValue(Base), /*LiveInOnly*/ false, 514d88c1a5aSDimitry Andric Ops, Builder); 515ff0cc061SDimitry Andric 5163ca95b02SDimitry Andric const Value *Ptr = SI.Ptrs[i]; 517d88c1a5aSDimitry Andric lowerIncomingStatepointValue(Builder.getValue(Ptr), /*LiveInOnly*/ false, 518d88c1a5aSDimitry Andric Ops, Builder); 519ff0cc061SDimitry Andric } 520ff0cc061SDimitry Andric 521ff0cc061SDimitry Andric // If there are any explicit spill slots passed to the statepoint, record 522ff0cc061SDimitry Andric // them, but otherwise do not do anything special. These are user provided 523ff0cc061SDimitry Andric // allocas and give control over placement to the consumer. In this case, 524ff0cc061SDimitry Andric // it is the contents of the slot which may get updated, not the pointer to 525ff0cc061SDimitry Andric // the alloca 5263ca95b02SDimitry Andric for (Value *V : SI.GCArgs) { 52739d628a0SDimitry Andric SDValue Incoming = Builder.getValue(V); 528ff0cc061SDimitry Andric if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { 529ff0cc061SDimitry Andric // This handles allocas as arguments to the statepoint 530ff0cc061SDimitry Andric Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), 531ff0cc061SDimitry Andric Incoming.getValueType())); 53239d628a0SDimitry Andric } 53339d628a0SDimitry Andric } 534ff0cc061SDimitry Andric 535ff0cc061SDimitry Andric // Record computed locations for all lowered values. 536ff0cc061SDimitry Andric // This can not be embedded in lowering loops as we need to record *all* 537ff0cc061SDimitry Andric // values, while previous loops account only values with unique SDValues. 5383ca95b02SDimitry Andric const Instruction *StatepointInstr = SI.StatepointInstr; 5393ca95b02SDimitry Andric auto &SpillMap = Builder.FuncInfo.StatepointSpillMaps[StatepointInstr]; 540ff0cc061SDimitry Andric 5413ca95b02SDimitry Andric for (const GCRelocateInst *Relocate : SI.GCRelocates) { 5424d0b32cdSDimitry Andric const Value *V = Relocate->getDerivedPtr(); 543ff0cc061SDimitry Andric SDValue SDV = Builder.getValue(V); 544ff0cc061SDimitry Andric SDValue Loc = Builder.StatepointLowering.getLocation(SDV); 545ff0cc061SDimitry Andric 546ff0cc061SDimitry Andric if (Loc.getNode()) { 5473ca95b02SDimitry Andric SpillMap.SlotMap[V] = cast<FrameIndexSDNode>(Loc)->getIndex(); 548ff0cc061SDimitry Andric } else { 549ff0cc061SDimitry Andric // Record value as visited, but not spilled. This is case for allocas 5507d523365SDimitry Andric // and constants. For this values we can avoid emitting spill load while 551ff0cc061SDimitry Andric // visiting corresponding gc_relocate. 552ff0cc061SDimitry Andric // Actually we do not need to record them in this map at all. 5537d523365SDimitry Andric // We do this only to check that we are not relocating any unvisited 5547d523365SDimitry Andric // value. 5553ca95b02SDimitry Andric SpillMap.SlotMap[V] = None; 556ff0cc061SDimitry Andric 557ff0cc061SDimitry Andric // Default llvm mechanisms for exporting values which are used in 558ff0cc061SDimitry Andric // different basic blocks does not work for gc relocates. 559ff0cc061SDimitry Andric // Note that it would be incorrect to teach llvm that all relocates are 5607d523365SDimitry Andric // uses of the corresponding values so that it would automatically 561ff0cc061SDimitry Andric // export them. Relocates of the spilled values does not use original 562ff0cc061SDimitry Andric // value. 5634d0b32cdSDimitry Andric if (Relocate->getParent() != StatepointInstr->getParent()) 564ff0cc061SDimitry Andric Builder.ExportFromCurrentBlock(V); 565ff0cc061SDimitry Andric } 566ff0cc061SDimitry Andric } 567ff0cc061SDimitry Andric } 568ff0cc061SDimitry Andric 5693ca95b02SDimitry Andric SDValue SelectionDAGBuilder::LowerAsSTATEPOINT( 5703ca95b02SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI) { 57139d628a0SDimitry Andric // The basic scheme here is that information about both the original call and 57239d628a0SDimitry Andric // the safepoint is encoded in the CallInst. We create a temporary call and 57339d628a0SDimitry Andric // lower it, then reverse engineer the calling sequence. 57439d628a0SDimitry Andric 57539d628a0SDimitry Andric NumOfStatepoints++; 57639d628a0SDimitry Andric // Clear state 57739d628a0SDimitry Andric StatepointLowering.startNewStatepoint(*this); 57839d628a0SDimitry Andric 57939d628a0SDimitry Andric #ifndef NDEBUG 5803ca95b02SDimitry Andric // We schedule gc relocates before removeDuplicateGCPtrs since we _will_ 5813ca95b02SDimitry Andric // encounter the duplicate gc relocates we elide in removeDuplicateGCPtrs. 5823ca95b02SDimitry Andric for (auto *Reloc : SI.GCRelocates) 5833ca95b02SDimitry Andric if (Reloc->getParent() == SI.StatepointInstr->getParent()) 5843ca95b02SDimitry Andric StatepointLowering.scheduleRelocCall(*Reloc); 58539d628a0SDimitry Andric #endif 58639d628a0SDimitry Andric 5873ca95b02SDimitry Andric // Remove any redundant llvm::Values which map to the same SDValue as another 5883ca95b02SDimitry Andric // input. Also has the effect of removing duplicates in the original 5893ca95b02SDimitry Andric // llvm::Value input list as well. This is a useful optimization for 5903ca95b02SDimitry Andric // reducing the size of the StackMap section. It has no other impact. 5913ca95b02SDimitry Andric removeDuplicateGCPtrs(SI.Bases, SI.Ptrs, SI.GCRelocates, *this, 5923ca95b02SDimitry Andric FuncInfo.StatepointSpillMaps[SI.StatepointInstr]); 5933ca95b02SDimitry Andric assert(SI.Bases.size() == SI.Ptrs.size() && 5943ca95b02SDimitry Andric SI.Ptrs.size() == SI.GCRelocates.size()); 59539d628a0SDimitry Andric 59639d628a0SDimitry Andric // Lower statepoint vmstate and gcstate arguments 597ff0cc061SDimitry Andric SmallVector<SDValue, 10> LoweredMetaArgs; 5983ca95b02SDimitry Andric lowerStatepointMetaArgs(LoweredMetaArgs, SI, *this); 5993ca95b02SDimitry Andric 6003ca95b02SDimitry Andric // Now that we've emitted the spills, we need to update the root so that the 6013ca95b02SDimitry Andric // call sequence is ordered correctly. 6023ca95b02SDimitry Andric SI.CLI.setChain(getRoot()); 60339d628a0SDimitry Andric 60439d628a0SDimitry Andric // Get call node, we will replace it later with statepoint 6053ca95b02SDimitry Andric SDValue ReturnVal; 6063ca95b02SDimitry Andric SDNode *CallNode; 6073ca95b02SDimitry Andric std::tie(ReturnVal, CallNode) = 6083ca95b02SDimitry Andric lowerCallFromStatepointLoweringInfo(SI, *this, PendingExports); 60939d628a0SDimitry Andric 610ff0cc061SDimitry Andric // Construct the actual GC_TRANSITION_START, STATEPOINT, and GC_TRANSITION_END 611ff0cc061SDimitry Andric // nodes with all the appropriate arguments and return values. 612ff0cc061SDimitry Andric 613ff0cc061SDimitry Andric // Call Node: Chain, Target, {Args}, RegMask, [Glue] 614ff0cc061SDimitry Andric SDValue Chain = CallNode->getOperand(0); 615ff0cc061SDimitry Andric 616ff0cc061SDimitry Andric SDValue Glue; 617ff0cc061SDimitry Andric bool CallHasIncomingGlue = CallNode->getGluedNode(); 618ff0cc061SDimitry Andric if (CallHasIncomingGlue) { 619ff0cc061SDimitry Andric // Glue is always last operand 620ff0cc061SDimitry Andric Glue = CallNode->getOperand(CallNode->getNumOperands() - 1); 621ff0cc061SDimitry Andric } 622ff0cc061SDimitry Andric 623ff0cc061SDimitry Andric // Build the GC_TRANSITION_START node if necessary. 624ff0cc061SDimitry Andric // 625ff0cc061SDimitry Andric // The operands to the GC_TRANSITION_{START,END} nodes are laid out in the 626ff0cc061SDimitry Andric // order in which they appear in the call to the statepoint intrinsic. If 627ff0cc061SDimitry Andric // any of the operands is a pointer-typed, that operand is immediately 628ff0cc061SDimitry Andric // followed by a SRCVALUE for the pointer that may be used during lowering 629ff0cc061SDimitry Andric // (e.g. to form MachinePointerInfo values for loads/stores). 630ff0cc061SDimitry Andric const bool IsGCTransition = 6313ca95b02SDimitry Andric (SI.StatepointFlags & (uint64_t)StatepointFlags::GCTransition) == 632ff0cc061SDimitry Andric (uint64_t)StatepointFlags::GCTransition; 633ff0cc061SDimitry Andric if (IsGCTransition) { 634ff0cc061SDimitry Andric SmallVector<SDValue, 8> TSOps; 635ff0cc061SDimitry Andric 636ff0cc061SDimitry Andric // Add chain 637ff0cc061SDimitry Andric TSOps.push_back(Chain); 638ff0cc061SDimitry Andric 639ff0cc061SDimitry Andric // Add GC transition arguments 6403ca95b02SDimitry Andric for (const Value *V : SI.GCTransitionArgs) { 641ff0cc061SDimitry Andric TSOps.push_back(getValue(V)); 642ff0cc061SDimitry Andric if (V->getType()->isPointerTy()) 643ff0cc061SDimitry Andric TSOps.push_back(DAG.getSrcValue(V)); 644ff0cc061SDimitry Andric } 645ff0cc061SDimitry Andric 646ff0cc061SDimitry Andric // Add glue if necessary 647ff0cc061SDimitry Andric if (CallHasIncomingGlue) 648ff0cc061SDimitry Andric TSOps.push_back(Glue); 649ff0cc061SDimitry Andric 650ff0cc061SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 651ff0cc061SDimitry Andric 652ff0cc061SDimitry Andric SDValue GCTransitionStart = 653ff0cc061SDimitry Andric DAG.getNode(ISD::GC_TRANSITION_START, getCurSDLoc(), NodeTys, TSOps); 654ff0cc061SDimitry Andric 655ff0cc061SDimitry Andric Chain = GCTransitionStart.getValue(0); 656ff0cc061SDimitry Andric Glue = GCTransitionStart.getValue(1); 657ff0cc061SDimitry Andric } 65839d628a0SDimitry Andric 65939d628a0SDimitry Andric // TODO: Currently, all of these operands are being marked as read/write in 66039d628a0SDimitry Andric // PrologEpilougeInserter.cpp, we should special case the VMState arguments 66139d628a0SDimitry Andric // and flags to be read-only. 66239d628a0SDimitry Andric SmallVector<SDValue, 40> Ops; 66339d628a0SDimitry Andric 664ff0cc061SDimitry Andric // Add the <id> and <numBytes> constants. 6653ca95b02SDimitry Andric Ops.push_back(DAG.getTargetConstant(SI.ID, getCurSDLoc(), MVT::i64)); 666ff0cc061SDimitry Andric Ops.push_back( 6673ca95b02SDimitry Andric DAG.getTargetConstant(SI.NumPatchBytes, getCurSDLoc(), MVT::i32)); 668ff0cc061SDimitry Andric 66939d628a0SDimitry Andric // Calculate and push starting position of vmstate arguments 67039d628a0SDimitry Andric // Get number of arguments incoming directly into call node 67139d628a0SDimitry Andric unsigned NumCallRegArgs = 672ff0cc061SDimitry Andric CallNode->getNumOperands() - (CallHasIncomingGlue ? 4 : 3); 673ff0cc061SDimitry Andric Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, getCurSDLoc(), MVT::i32)); 67439d628a0SDimitry Andric 67539d628a0SDimitry Andric // Add call target 67639d628a0SDimitry Andric SDValue CallTarget = SDValue(CallNode->getOperand(1).getNode(), 0); 67739d628a0SDimitry Andric Ops.push_back(CallTarget); 67839d628a0SDimitry Andric 67939d628a0SDimitry Andric // Add call arguments 68039d628a0SDimitry Andric // Get position of register mask in the call 68139d628a0SDimitry Andric SDNode::op_iterator RegMaskIt; 682ff0cc061SDimitry Andric if (CallHasIncomingGlue) 68339d628a0SDimitry Andric RegMaskIt = CallNode->op_end() - 2; 68439d628a0SDimitry Andric else 68539d628a0SDimitry Andric RegMaskIt = CallNode->op_end() - 1; 68639d628a0SDimitry Andric Ops.insert(Ops.end(), CallNode->op_begin() + 2, RegMaskIt); 68739d628a0SDimitry Andric 688ff0cc061SDimitry Andric // Add a constant argument for the calling convention 6893ca95b02SDimitry Andric pushStackMapConstant(Ops, *this, SI.CLI.CallConv); 690ff0cc061SDimitry Andric 691ff0cc061SDimitry Andric // Add a constant argument for the flags 6923ca95b02SDimitry Andric uint64_t Flags = SI.StatepointFlags; 6933ca95b02SDimitry Andric assert(((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0) && 6943ca95b02SDimitry Andric "Unknown flag used"); 695ff0cc061SDimitry Andric pushStackMapConstant(Ops, *this, Flags); 69639d628a0SDimitry Andric 69739d628a0SDimitry Andric // Insert all vmstate and gcstate arguments 698ff0cc061SDimitry Andric Ops.insert(Ops.end(), LoweredMetaArgs.begin(), LoweredMetaArgs.end()); 69939d628a0SDimitry Andric 70039d628a0SDimitry Andric // Add register mask from call node 70139d628a0SDimitry Andric Ops.push_back(*RegMaskIt); 70239d628a0SDimitry Andric 70339d628a0SDimitry Andric // Add chain 704ff0cc061SDimitry Andric Ops.push_back(Chain); 70539d628a0SDimitry Andric 70639d628a0SDimitry Andric // Same for the glue, but we add it only if original call had it 70739d628a0SDimitry Andric if (Glue.getNode()) 70839d628a0SDimitry Andric Ops.push_back(Glue); 70939d628a0SDimitry Andric 710ff0cc061SDimitry Andric // Compute return values. Provide a glue output since we consume one as 711ff0cc061SDimitry Andric // input. This allows someone else to chain off us as needed. 712ff0cc061SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 71339d628a0SDimitry Andric 714ff0cc061SDimitry Andric SDNode *StatepointMCNode = 715ff0cc061SDimitry Andric DAG.getMachineNode(TargetOpcode::STATEPOINT, getCurSDLoc(), NodeTys, Ops); 716ff0cc061SDimitry Andric 717ff0cc061SDimitry Andric SDNode *SinkNode = StatepointMCNode; 718ff0cc061SDimitry Andric 719ff0cc061SDimitry Andric // Build the GC_TRANSITION_END node if necessary. 720ff0cc061SDimitry Andric // 721ff0cc061SDimitry Andric // See the comment above regarding GC_TRANSITION_START for the layout of 722ff0cc061SDimitry Andric // the operands to the GC_TRANSITION_END node. 723ff0cc061SDimitry Andric if (IsGCTransition) { 724ff0cc061SDimitry Andric SmallVector<SDValue, 8> TEOps; 725ff0cc061SDimitry Andric 726ff0cc061SDimitry Andric // Add chain 727ff0cc061SDimitry Andric TEOps.push_back(SDValue(StatepointMCNode, 0)); 728ff0cc061SDimitry Andric 729ff0cc061SDimitry Andric // Add GC transition arguments 7303ca95b02SDimitry Andric for (const Value *V : SI.GCTransitionArgs) { 731ff0cc061SDimitry Andric TEOps.push_back(getValue(V)); 732ff0cc061SDimitry Andric if (V->getType()->isPointerTy()) 733ff0cc061SDimitry Andric TEOps.push_back(DAG.getSrcValue(V)); 734ff0cc061SDimitry Andric } 735ff0cc061SDimitry Andric 736ff0cc061SDimitry Andric // Add glue 737ff0cc061SDimitry Andric TEOps.push_back(SDValue(StatepointMCNode, 1)); 738ff0cc061SDimitry Andric 739ff0cc061SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 740ff0cc061SDimitry Andric 741ff0cc061SDimitry Andric SDValue GCTransitionStart = 742ff0cc061SDimitry Andric DAG.getNode(ISD::GC_TRANSITION_END, getCurSDLoc(), NodeTys, TEOps); 743ff0cc061SDimitry Andric 744ff0cc061SDimitry Andric SinkNode = GCTransitionStart.getNode(); 745ff0cc061SDimitry Andric } 74639d628a0SDimitry Andric 74739d628a0SDimitry Andric // Replace original call 748ff0cc061SDimitry Andric DAG.ReplaceAllUsesWith(CallNode, SinkNode); // This may update Root 7497d523365SDimitry Andric // Remove original call node 75039d628a0SDimitry Andric DAG.DeleteNode(CallNode); 75139d628a0SDimitry Andric 75239d628a0SDimitry Andric // DON'T set the root - under the assumption that it's already set past the 75339d628a0SDimitry Andric // inserted node we created. 75439d628a0SDimitry Andric 75539d628a0SDimitry Andric // TODO: A better future implementation would be to emit a single variable 75639d628a0SDimitry Andric // argument, variable return value STATEPOINT node here and then hookup the 75739d628a0SDimitry Andric // return value of each gc.relocate to the respective output of the 75839d628a0SDimitry Andric // previously emitted STATEPOINT value. Unfortunately, this doesn't appear 75939d628a0SDimitry Andric // to actually be possible today. 7603ca95b02SDimitry Andric 7613ca95b02SDimitry Andric return ReturnVal; 76239d628a0SDimitry Andric } 76339d628a0SDimitry Andric 7643ca95b02SDimitry Andric void 7653ca95b02SDimitry Andric SelectionDAGBuilder::LowerStatepoint(ImmutableStatepoint ISP, 7663ca95b02SDimitry Andric const BasicBlock *EHPadBB /*= nullptr*/) { 7673ca95b02SDimitry Andric assert(ISP.getCallSite().getCallingConv() != CallingConv::AnyReg && 7683ca95b02SDimitry Andric "anyregcc is not supported on statepoints!"); 7693ca95b02SDimitry Andric 7703ca95b02SDimitry Andric #ifndef NDEBUG 7713ca95b02SDimitry Andric // If this is a malformed statepoint, report it early to simplify debugging. 7723ca95b02SDimitry Andric // This should catch any IR level mistake that's made when constructing or 7733ca95b02SDimitry Andric // transforming statepoints. 7743ca95b02SDimitry Andric ISP.verify(); 7753ca95b02SDimitry Andric 7763ca95b02SDimitry Andric // Check that the associated GCStrategy expects to encounter statepoints. 7773ca95b02SDimitry Andric assert(GFI->getStrategy().useStatepoints() && 7783ca95b02SDimitry Andric "GCStrategy does not expect to encounter statepoints"); 7793ca95b02SDimitry Andric #endif 7803ca95b02SDimitry Andric 7813ca95b02SDimitry Andric SDValue ActualCallee; 7823ca95b02SDimitry Andric 7833ca95b02SDimitry Andric if (ISP.getNumPatchBytes() > 0) { 7843ca95b02SDimitry Andric // If we've been asked to emit a nop sequence instead of a call instruction 7853ca95b02SDimitry Andric // for this statepoint then don't lower the call target, but use a constant 7863ca95b02SDimitry Andric // `null` instead. Not lowering the call target lets statepoint clients get 7873ca95b02SDimitry Andric // away without providing a physical address for the symbolic call target at 7883ca95b02SDimitry Andric // link time. 7893ca95b02SDimitry Andric 7903ca95b02SDimitry Andric const auto &TLI = DAG.getTargetLoweringInfo(); 7913ca95b02SDimitry Andric const auto &DL = DAG.getDataLayout(); 7923ca95b02SDimitry Andric 7933ca95b02SDimitry Andric unsigned AS = ISP.getCalledValue()->getType()->getPointerAddressSpace(); 7943ca95b02SDimitry Andric ActualCallee = DAG.getConstant(0, getCurSDLoc(), TLI.getPointerTy(DL, AS)); 7953ca95b02SDimitry Andric } else { 7963ca95b02SDimitry Andric ActualCallee = getValue(ISP.getCalledValue()); 7973ca95b02SDimitry Andric } 7983ca95b02SDimitry Andric 7993ca95b02SDimitry Andric StatepointLoweringInfo SI(DAG); 8003ca95b02SDimitry Andric populateCallLoweringInfo(SI.CLI, ISP.getCallSite(), 8013ca95b02SDimitry Andric ImmutableStatepoint::CallArgsBeginPos, 8023ca95b02SDimitry Andric ISP.getNumCallArgs(), ActualCallee, 8033ca95b02SDimitry Andric ISP.getActualReturnType(), false /* IsPatchPoint */); 8043ca95b02SDimitry Andric 8053ca95b02SDimitry Andric for (const GCRelocateInst *Relocate : ISP.getRelocates()) { 8063ca95b02SDimitry Andric SI.GCRelocates.push_back(Relocate); 8073ca95b02SDimitry Andric SI.Bases.push_back(Relocate->getBasePtr()); 8083ca95b02SDimitry Andric SI.Ptrs.push_back(Relocate->getDerivedPtr()); 8093ca95b02SDimitry Andric } 8103ca95b02SDimitry Andric 8113ca95b02SDimitry Andric SI.GCArgs = ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end()); 8123ca95b02SDimitry Andric SI.StatepointInstr = ISP.getInstruction(); 8133ca95b02SDimitry Andric SI.GCTransitionArgs = 8143ca95b02SDimitry Andric ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end()); 8153ca95b02SDimitry Andric SI.ID = ISP.getID(); 8163ca95b02SDimitry Andric SI.DeoptState = ArrayRef<const Use>(ISP.vm_state_begin(), ISP.vm_state_end()); 8173ca95b02SDimitry Andric SI.StatepointFlags = ISP.getFlags(); 8183ca95b02SDimitry Andric SI.NumPatchBytes = ISP.getNumPatchBytes(); 8193ca95b02SDimitry Andric SI.EHPadBB = EHPadBB; 8203ca95b02SDimitry Andric 8213ca95b02SDimitry Andric SDValue ReturnValue = LowerAsSTATEPOINT(SI); 8223ca95b02SDimitry Andric 8233ca95b02SDimitry Andric // Export the result value if needed 8243ca95b02SDimitry Andric const GCResultInst *GCResult = ISP.getGCResult(); 8253ca95b02SDimitry Andric Type *RetTy = ISP.getActualReturnType(); 8263ca95b02SDimitry Andric if (!RetTy->isVoidTy() && GCResult) { 8273ca95b02SDimitry Andric if (GCResult->getParent() != ISP.getCallSite().getParent()) { 8283ca95b02SDimitry Andric // Result value will be used in a different basic block so we need to 8293ca95b02SDimitry Andric // export it now. Default exporting mechanism will not work here because 8303ca95b02SDimitry Andric // statepoint call has a different type than the actual call. It means 8313ca95b02SDimitry Andric // that by default llvm will create export register of the wrong type 8323ca95b02SDimitry Andric // (always i32 in our case). So instead we need to create export register 8333ca95b02SDimitry Andric // with correct type manually. 8343ca95b02SDimitry Andric // TODO: To eliminate this problem we can remove gc.result intrinsics 8353ca95b02SDimitry Andric // completely and make statepoint call to return a tuple. 8363ca95b02SDimitry Andric unsigned Reg = FuncInfo.CreateRegs(RetTy); 8373ca95b02SDimitry Andric RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(), 8383ca95b02SDimitry Andric DAG.getDataLayout(), Reg, RetTy); 8393ca95b02SDimitry Andric SDValue Chain = DAG.getEntryNode(); 8403ca95b02SDimitry Andric 8413ca95b02SDimitry Andric RFV.getCopyToRegs(ReturnValue, DAG, getCurSDLoc(), Chain, nullptr); 8423ca95b02SDimitry Andric PendingExports.push_back(Chain); 8433ca95b02SDimitry Andric FuncInfo.ValueMap[ISP.getInstruction()] = Reg; 8443ca95b02SDimitry Andric } else { 8453ca95b02SDimitry Andric // Result value will be used in a same basic block. Don't export it or 8463ca95b02SDimitry Andric // perform any explicit register copies. 8473ca95b02SDimitry Andric // We'll replace the actuall call node shortly. gc_result will grab 8483ca95b02SDimitry Andric // this value. 8493ca95b02SDimitry Andric setValue(ISP.getInstruction(), ReturnValue); 8503ca95b02SDimitry Andric } 8513ca95b02SDimitry Andric } else { 8523ca95b02SDimitry Andric // The token value is never used from here on, just generate a poison value 8533ca95b02SDimitry Andric setValue(ISP.getInstruction(), DAG.getIntPtrConstant(-1, getCurSDLoc())); 8543ca95b02SDimitry Andric } 8553ca95b02SDimitry Andric } 8563ca95b02SDimitry Andric 8573ca95b02SDimitry Andric void SelectionDAGBuilder::LowerCallSiteWithDeoptBundleImpl( 8583ca95b02SDimitry Andric ImmutableCallSite CS, SDValue Callee, const BasicBlock *EHPadBB, 8593ca95b02SDimitry Andric bool VarArgDisallowed, bool ForceVoidReturnTy) { 8603ca95b02SDimitry Andric StatepointLoweringInfo SI(DAG); 8613ca95b02SDimitry Andric unsigned ArgBeginIndex = CS.arg_begin() - CS.getInstruction()->op_begin(); 8623ca95b02SDimitry Andric populateCallLoweringInfo( 8633ca95b02SDimitry Andric SI.CLI, CS, ArgBeginIndex, CS.getNumArgOperands(), Callee, 8643ca95b02SDimitry Andric ForceVoidReturnTy ? Type::getVoidTy(*DAG.getContext()) : CS.getType(), 8653ca95b02SDimitry Andric false); 8663ca95b02SDimitry Andric if (!VarArgDisallowed) 8673ca95b02SDimitry Andric SI.CLI.IsVarArg = CS.getFunctionType()->isVarArg(); 8683ca95b02SDimitry Andric 8693ca95b02SDimitry Andric auto DeoptBundle = *CS.getOperandBundle(LLVMContext::OB_deopt); 8703ca95b02SDimitry Andric 8713ca95b02SDimitry Andric unsigned DefaultID = StatepointDirectives::DeoptBundleStatepointID; 8723ca95b02SDimitry Andric 8733ca95b02SDimitry Andric auto SD = parseStatepointDirectivesFromAttrs(CS.getAttributes()); 8743ca95b02SDimitry Andric SI.ID = SD.StatepointID.getValueOr(DefaultID); 8753ca95b02SDimitry Andric SI.NumPatchBytes = SD.NumPatchBytes.getValueOr(0); 8763ca95b02SDimitry Andric 8773ca95b02SDimitry Andric SI.DeoptState = 8783ca95b02SDimitry Andric ArrayRef<const Use>(DeoptBundle.Inputs.begin(), DeoptBundle.Inputs.end()); 8793ca95b02SDimitry Andric SI.StatepointFlags = static_cast<uint64_t>(StatepointFlags::None); 8803ca95b02SDimitry Andric SI.EHPadBB = EHPadBB; 8813ca95b02SDimitry Andric 8823ca95b02SDimitry Andric // NB! The GC arguments are deliberately left empty. 8833ca95b02SDimitry Andric 8843ca95b02SDimitry Andric if (SDValue ReturnVal = LowerAsSTATEPOINT(SI)) { 8853ca95b02SDimitry Andric const Instruction *Inst = CS.getInstruction(); 8863ca95b02SDimitry Andric ReturnVal = lowerRangeToAssertZExt(DAG, *Inst, ReturnVal); 8873ca95b02SDimitry Andric setValue(Inst, ReturnVal); 8883ca95b02SDimitry Andric } 8893ca95b02SDimitry Andric } 8903ca95b02SDimitry Andric 8913ca95b02SDimitry Andric void SelectionDAGBuilder::LowerCallSiteWithDeoptBundle( 8923ca95b02SDimitry Andric ImmutableCallSite CS, SDValue Callee, const BasicBlock *EHPadBB) { 8933ca95b02SDimitry Andric LowerCallSiteWithDeoptBundleImpl(CS, Callee, EHPadBB, 8943ca95b02SDimitry Andric /* VarArgDisallowed = */ false, 8953ca95b02SDimitry Andric /* ForceVoidReturnTy = */ false); 8963ca95b02SDimitry Andric } 8973ca95b02SDimitry Andric 8983ca95b02SDimitry Andric void SelectionDAGBuilder::visitGCResult(const GCResultInst &CI) { 89939d628a0SDimitry Andric // The result value of the gc_result is simply the result of the actual 90039d628a0SDimitry Andric // call. We've already emitted this, so just grab the value. 9013ca95b02SDimitry Andric const Instruction *I = CI.getStatepoint(); 90239d628a0SDimitry Andric 9037d523365SDimitry Andric if (I->getParent() != CI.getParent()) { 9047d523365SDimitry Andric // Statepoint is in different basic block so we should have stored call 9057d523365SDimitry Andric // result in a virtual register. 906ff0cc061SDimitry Andric // We can not use default getValue() functionality to copy value from this 9073ca95b02SDimitry Andric // register because statepoint and actual call return types can be 908ff0cc061SDimitry Andric // different, and getValue() will use CopyFromReg of the wrong type, 909ff0cc061SDimitry Andric // which is always i32 in our case. 9103dac3a9bSDimitry Andric PointerType *CalleeType = cast<PointerType>( 9113dac3a9bSDimitry Andric ImmutableStatepoint(I).getCalledValue()->getType()); 912ff0cc061SDimitry Andric Type *RetTy = 913ff0cc061SDimitry Andric cast<FunctionType>(CalleeType->getElementType())->getReturnType(); 914ff0cc061SDimitry Andric SDValue CopyFromReg = getCopyFromRegs(I, RetTy); 915ff0cc061SDimitry Andric 916ff0cc061SDimitry Andric assert(CopyFromReg.getNode()); 917ff0cc061SDimitry Andric setValue(&CI, CopyFromReg); 918ff0cc061SDimitry Andric } else { 91939d628a0SDimitry Andric setValue(&CI, getValue(I)); 92039d628a0SDimitry Andric } 921ff0cc061SDimitry Andric } 92239d628a0SDimitry Andric 9234d0b32cdSDimitry Andric void SelectionDAGBuilder::visitGCRelocate(const GCRelocateInst &Relocate) { 92439d628a0SDimitry Andric #ifndef NDEBUG 92539d628a0SDimitry Andric // Consistency check 926d88c1a5aSDimitry Andric // We skip this check for relocates not in the same basic block as their 9277d523365SDimitry Andric // statepoint. It would be too expensive to preserve validation info through 9287d523365SDimitry Andric // different basic blocks. 9293ca95b02SDimitry Andric if (Relocate.getStatepoint()->getParent() == Relocate.getParent()) 9304d0b32cdSDimitry Andric StatepointLowering.relocCallVisited(Relocate); 9313ca95b02SDimitry Andric 9323ca95b02SDimitry Andric auto *Ty = Relocate.getType()->getScalarType(); 9333ca95b02SDimitry Andric if (auto IsManaged = GFI->getStrategy().isGCManagedPointer(Ty)) 9343ca95b02SDimitry Andric assert(*IsManaged && "Non gc managed pointer relocated!"); 93539d628a0SDimitry Andric #endif 93639d628a0SDimitry Andric 9374d0b32cdSDimitry Andric const Value *DerivedPtr = Relocate.getDerivedPtr(); 938ff0cc061SDimitry Andric SDValue SD = getValue(DerivedPtr); 93939d628a0SDimitry Andric 9403ca95b02SDimitry Andric auto &SpillMap = FuncInfo.StatepointSpillMaps[Relocate.getStatepoint()]; 9413ca95b02SDimitry Andric auto SlotIt = SpillMap.find(DerivedPtr); 9423ca95b02SDimitry Andric assert(SlotIt != SpillMap.end() && "Relocating not lowered gc value"); 9433ca95b02SDimitry Andric Optional<int> DerivedPtrLocation = SlotIt->second; 944ff0cc061SDimitry Andric 94539d628a0SDimitry Andric // We didn't need to spill these special cases (constants and allocas). 94639d628a0SDimitry Andric // See the handling in spillIncomingValueForStatepoint for detail. 947ff0cc061SDimitry Andric if (!DerivedPtrLocation) { 9484d0b32cdSDimitry Andric setValue(&Relocate, SD); 94939d628a0SDimitry Andric return; 95039d628a0SDimitry Andric } 95139d628a0SDimitry Andric 952ff0cc061SDimitry Andric SDValue SpillSlot = DAG.getTargetFrameIndex(*DerivedPtrLocation, 953ff0cc061SDimitry Andric SD.getValueType()); 95439d628a0SDimitry Andric 95539d628a0SDimitry Andric // Be conservative: flush all pending loads 95639d628a0SDimitry Andric // TODO: Probably we can be less restrictive on this, 9577d523365SDimitry Andric // it may allow more scheduling opportunities. 95839d628a0SDimitry Andric SDValue Chain = getRoot(); 95939d628a0SDimitry Andric 960ff0cc061SDimitry Andric SDValue SpillLoad = 961ff0cc061SDimitry Andric DAG.getLoad(SpillSlot.getValueType(), getCurSDLoc(), Chain, SpillSlot, 9627d523365SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), 9633ca95b02SDimitry Andric *DerivedPtrLocation)); 96439d628a0SDimitry Andric 96539d628a0SDimitry Andric // Again, be conservative, don't emit pending loads 966ff0cc061SDimitry Andric DAG.setRoot(SpillLoad.getValue(1)); 96739d628a0SDimitry Andric 968ff0cc061SDimitry Andric assert(SpillLoad.getNode()); 9694d0b32cdSDimitry Andric setValue(&Relocate, SpillLoad); 97039d628a0SDimitry Andric } 9713ca95b02SDimitry Andric 9723ca95b02SDimitry Andric void SelectionDAGBuilder::LowerDeoptimizeCall(const CallInst *CI) { 9733ca95b02SDimitry Andric const auto &TLI = DAG.getTargetLoweringInfo(); 9743ca95b02SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::DEOPTIMIZE), 9753ca95b02SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 9763ca95b02SDimitry Andric 9773ca95b02SDimitry Andric // We don't lower calls to __llvm_deoptimize as varargs, but as a regular 9783ca95b02SDimitry Andric // call. We also do not lower the return value to any virtual register, and 9793ca95b02SDimitry Andric // change the immediately following return to a trap instruction. 9803ca95b02SDimitry Andric LowerCallSiteWithDeoptBundleImpl(CI, Callee, /* EHPadBB = */ nullptr, 9813ca95b02SDimitry Andric /* VarArgDisallowed = */ true, 9823ca95b02SDimitry Andric /* ForceVoidReturnTy = */ true); 9833ca95b02SDimitry Andric } 9843ca95b02SDimitry Andric 9853ca95b02SDimitry Andric void SelectionDAGBuilder::LowerDeoptimizingReturn() { 9863ca95b02SDimitry Andric // We do not lower the return value from llvm.deoptimize to any virtual 9873ca95b02SDimitry Andric // register, and change the immediately following return to a trap 9883ca95b02SDimitry Andric // instruction. 9893ca95b02SDimitry Andric if (DAG.getTarget().Options.TrapUnreachable) 9903ca95b02SDimitry Andric DAG.setRoot( 9913ca95b02SDimitry Andric DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot())); 9923ca95b02SDimitry Andric } 993