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 113d8866befSDimitry Andric StatepointMaxSlotsRequired.updateMax( 114d8866befSDimitry Andric 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. 245f37b6182SDimitry Andric SDValue Loc = 246f37b6182SDimitry Andric Builder.DAG.getTargetFrameIndex(*Index, Builder.getFrameIndexTy()); 24739d628a0SDimitry Andric Builder.StatepointLowering.setLocation(Incoming, Loc); 24839d628a0SDimitry Andric } 24939d628a0SDimitry Andric 25039d628a0SDimitry Andric /// Remove any duplicate (as SDValues) from the derived pointer pairs. This 25139d628a0SDimitry Andric /// is not required for correctness. It's purpose is to reduce the size of 25239d628a0SDimitry Andric /// StackMap section. It has no effect on the number of spill slots required 25339d628a0SDimitry Andric /// or the actual lowering. 2543ca95b02SDimitry Andric static void 2553ca95b02SDimitry Andric removeDuplicateGCPtrs(SmallVectorImpl<const Value *> &Bases, 25639d628a0SDimitry Andric SmallVectorImpl<const Value *> &Ptrs, 2573ca95b02SDimitry Andric SmallVectorImpl<const GCRelocateInst *> &Relocs, 2583ca95b02SDimitry Andric SelectionDAGBuilder &Builder, 2593ca95b02SDimitry Andric FunctionLoweringInfo::StatepointSpillMap &SSM) { 2603ca95b02SDimitry Andric DenseMap<SDValue, const Value *> Seen; 26139d628a0SDimitry Andric 2623ca95b02SDimitry Andric SmallVector<const Value *, 64> NewBases, NewPtrs; 2633ca95b02SDimitry Andric SmallVector<const GCRelocateInst *, 64> NewRelocs; 2643ca95b02SDimitry Andric for (size_t i = 0, e = Ptrs.size(); i < e; i++) { 26539d628a0SDimitry Andric SDValue SD = Builder.getValue(Ptrs[i]); 2663ca95b02SDimitry Andric auto SeenIt = Seen.find(SD); 2673ca95b02SDimitry Andric 2683ca95b02SDimitry Andric if (SeenIt == Seen.end()) { 26939d628a0SDimitry Andric // Only add non-duplicates 27039d628a0SDimitry Andric NewBases.push_back(Bases[i]); 27139d628a0SDimitry Andric NewPtrs.push_back(Ptrs[i]); 27239d628a0SDimitry Andric NewRelocs.push_back(Relocs[i]); 2733ca95b02SDimitry Andric Seen[SD] = Ptrs[i]; 2743ca95b02SDimitry Andric } else { 2753ca95b02SDimitry Andric // Duplicate pointer found, note in SSM and move on: 2763ca95b02SDimitry Andric SSM.DuplicateMap[Ptrs[i]] = SeenIt->second; 27739d628a0SDimitry Andric } 27839d628a0SDimitry Andric } 27939d628a0SDimitry Andric assert(Bases.size() >= NewBases.size()); 28039d628a0SDimitry Andric assert(Ptrs.size() >= NewPtrs.size()); 28139d628a0SDimitry Andric assert(Relocs.size() >= NewRelocs.size()); 28239d628a0SDimitry Andric Bases = NewBases; 28339d628a0SDimitry Andric Ptrs = NewPtrs; 28439d628a0SDimitry Andric Relocs = NewRelocs; 28539d628a0SDimitry Andric assert(Ptrs.size() == Bases.size()); 28639d628a0SDimitry Andric assert(Ptrs.size() == Relocs.size()); 28739d628a0SDimitry Andric } 28839d628a0SDimitry Andric 28939d628a0SDimitry Andric /// Extract call from statepoint, lower it and return pointer to the 29039d628a0SDimitry Andric /// call node. Also update NodeMap so that getValue(statepoint) will 29139d628a0SDimitry Andric /// reference lowered call result 2923ca95b02SDimitry Andric static std::pair<SDValue, SDNode *> lowerCallFromStatepointLoweringInfo( 2933ca95b02SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI, 2943ca95b02SDimitry Andric SelectionDAGBuilder &Builder, SmallVectorImpl<SDValue> &PendingExports) { 295ff0cc061SDimitry Andric 296ff0cc061SDimitry Andric SDValue ReturnValue, CallEndVal; 2973ca95b02SDimitry Andric std::tie(ReturnValue, CallEndVal) = 2983ca95b02SDimitry Andric Builder.lowerInvokable(SI.CLI, SI.EHPadBB); 299ff0cc061SDimitry Andric SDNode *CallEnd = CallEndVal.getNode(); 300ff0cc061SDimitry Andric 301ff0cc061SDimitry Andric // Get a call instruction from the call sequence chain. Tail calls are not 302ff0cc061SDimitry Andric // allowed. The following code is essentially reverse engineering X86's 303ff0cc061SDimitry Andric // LowerCallTo. 304ff0cc061SDimitry Andric // 305ff0cc061SDimitry Andric // We are expecting DAG to have the following form: 306ff0cc061SDimitry Andric // 307ff0cc061SDimitry Andric // ch = eh_label (only in case of invoke statepoint) 308ff0cc061SDimitry Andric // ch, glue = callseq_start ch 309ff0cc061SDimitry Andric // ch, glue = X86::Call ch, glue 310ff0cc061SDimitry Andric // ch, glue = callseq_end ch, glue 311ff0cc061SDimitry Andric // get_return_value ch, glue 312ff0cc061SDimitry Andric // 3137d523365SDimitry Andric // get_return_value can either be a sequence of CopyFromReg instructions 3147d523365SDimitry Andric // to grab the return value from the return register(s), or it can be a LOAD 3157d523365SDimitry Andric // to load a value returned by reference via a stack slot. 316ff0cc061SDimitry Andric 3173ca95b02SDimitry Andric bool HasDef = !SI.CLI.RetTy->isVoidTy(); 3187d523365SDimitry Andric if (HasDef) { 3197d523365SDimitry Andric if (CallEnd->getOpcode() == ISD::LOAD) 320ff0cc061SDimitry Andric CallEnd = CallEnd->getOperand(0).getNode(); 3217d523365SDimitry Andric else 3227d523365SDimitry Andric while (CallEnd->getOpcode() == ISD::CopyFromReg) 3237d523365SDimitry Andric CallEnd = CallEnd->getOperand(0).getNode(); 3247d523365SDimitry Andric } 325ff0cc061SDimitry Andric 326ff0cc061SDimitry Andric assert(CallEnd->getOpcode() == ISD::CALLSEQ_END && "expected!"); 3273ca95b02SDimitry Andric return std::make_pair(ReturnValue, CallEnd->getOperand(0).getNode()); 32839d628a0SDimitry Andric } 32939d628a0SDimitry Andric 33039d628a0SDimitry Andric /// Spill a value incoming to the statepoint. It might be either part of 33139d628a0SDimitry Andric /// vmstate 33239d628a0SDimitry Andric /// or gcstate. In both cases unconditionally spill it on the stack unless it 33339d628a0SDimitry Andric /// is a null constant. Return pair with first element being frame index 33439d628a0SDimitry Andric /// containing saved value and second element with outgoing chain from the 33539d628a0SDimitry Andric /// emitted store 33639d628a0SDimitry Andric static std::pair<SDValue, SDValue> 33739d628a0SDimitry Andric spillIncomingStatepointValue(SDValue Incoming, SDValue Chain, 33839d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 33939d628a0SDimitry Andric SDValue Loc = Builder.StatepointLowering.getLocation(Incoming); 34039d628a0SDimitry Andric 34139d628a0SDimitry Andric // Emit new store if we didn't do it for this ptr before 34239d628a0SDimitry Andric if (!Loc.getNode()) { 34339d628a0SDimitry Andric Loc = Builder.StatepointLowering.allocateStackSlot(Incoming.getValueType(), 34439d628a0SDimitry Andric Builder); 34539d628a0SDimitry Andric int Index = cast<FrameIndexSDNode>(Loc)->getIndex(); 34639d628a0SDimitry Andric // We use TargetFrameIndex so that isel will not select it into LEA 347f37b6182SDimitry Andric Loc = Builder.DAG.getTargetFrameIndex(Index, Builder.getFrameIndexTy()); 34839d628a0SDimitry Andric 34939d628a0SDimitry Andric // TODO: We can create TokenFactor node instead of 35039d628a0SDimitry Andric // chaining stores one after another, this may allow 35139d628a0SDimitry Andric // a bit more optimal scheduling for them 3523ca95b02SDimitry Andric 3533ca95b02SDimitry Andric #ifndef NDEBUG 3543ca95b02SDimitry Andric // Right now we always allocate spill slots that are of the same 3553ca95b02SDimitry Andric // size as the value we're about to spill (the size of spillee can 3563ca95b02SDimitry Andric // vary since we spill vectors of pointers too). At some point we 3573ca95b02SDimitry Andric // can consider allowing spills of smaller values to larger slots 3583ca95b02SDimitry Andric // (i.e. change the '==' in the assert below to a '>='). 359d88c1a5aSDimitry Andric MachineFrameInfo &MFI = Builder.DAG.getMachineFunction().getFrameInfo(); 360d88c1a5aSDimitry Andric assert((MFI.getObjectSize(Index) * 8) == Incoming.getValueSizeInBits() && 3613ca95b02SDimitry Andric "Bad spill: stack slot does not match!"); 3623ca95b02SDimitry Andric #endif 3633ca95b02SDimitry Andric 36439d628a0SDimitry Andric Chain = Builder.DAG.getStore(Chain, Builder.getCurSDLoc(), Incoming, Loc, 3657d523365SDimitry Andric MachinePointerInfo::getFixedStack( 3663ca95b02SDimitry Andric Builder.DAG.getMachineFunction(), Index)); 36739d628a0SDimitry Andric 36839d628a0SDimitry Andric Builder.StatepointLowering.setLocation(Incoming, Loc); 36939d628a0SDimitry Andric } 37039d628a0SDimitry Andric 37139d628a0SDimitry Andric assert(Loc.getNode()); 37239d628a0SDimitry Andric return std::make_pair(Loc, Chain); 37339d628a0SDimitry Andric } 37439d628a0SDimitry Andric 37539d628a0SDimitry Andric /// Lower a single value incoming to a statepoint node. This value can be 37639d628a0SDimitry Andric /// either a deopt value or a gc value, the handling is the same. We special 37739d628a0SDimitry Andric /// case constants and allocas, then fall back to spilling if required. 378d88c1a5aSDimitry Andric static void lowerIncomingStatepointValue(SDValue Incoming, bool LiveInOnly, 37939d628a0SDimitry Andric SmallVectorImpl<SDValue> &Ops, 38039d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 38139d628a0SDimitry Andric SDValue Chain = Builder.getRoot(); 38239d628a0SDimitry Andric 38339d628a0SDimitry Andric if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Incoming)) { 38439d628a0SDimitry Andric // If the original value was a constant, make sure it gets recorded as 38539d628a0SDimitry Andric // such in the stackmap. This is required so that the consumer can 38639d628a0SDimitry Andric // parse any internal format to the deopt state. It also handles null 387444ed5c5SDimitry Andric // pointers and other constant pointers in GC states. Note the constant 388444ed5c5SDimitry Andric // vectors do not appear to actually hit this path and that anything larger 389444ed5c5SDimitry Andric // than an i64 value (not type!) will fail asserts here. 390ff0cc061SDimitry Andric pushStackMapConstant(Ops, Builder, C->getSExtValue()); 39139d628a0SDimitry Andric } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { 392ff0cc061SDimitry Andric // This handles allocas as arguments to the statepoint (this is only 393ff0cc061SDimitry Andric // really meaningful for a deopt value. For GC, we'd be trying to 394ff0cc061SDimitry Andric // relocate the address of the alloca itself?) 395f37b6182SDimitry Andric assert(Incoming.getValueType() == Builder.getFrameIndexTy() && 396f37b6182SDimitry Andric "Incoming value is a frame index!"); 397ff0cc061SDimitry Andric Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), 398f37b6182SDimitry Andric Builder.getFrameIndexTy())); 399d88c1a5aSDimitry Andric } else if (LiveInOnly) { 400d88c1a5aSDimitry Andric // If this value is live in (not live-on-return, or live-through), we can 401d88c1a5aSDimitry Andric // treat it the same way patchpoint treats it's "live in" values. We'll 402d88c1a5aSDimitry Andric // end up folding some of these into stack references, but they'll be 403d88c1a5aSDimitry Andric // handled by the register allocator. Note that we do not have the notion 404d88c1a5aSDimitry Andric // of a late use so these values might be placed in registers which are 405d88c1a5aSDimitry Andric // clobbered by the call. This is fine for live-in. 406d88c1a5aSDimitry Andric Ops.push_back(Incoming); 40739d628a0SDimitry Andric } else { 40839d628a0SDimitry Andric // Otherwise, locate a spill slot and explicitly spill it so it 40939d628a0SDimitry Andric // can be found by the runtime later. We currently do not support 41039d628a0SDimitry Andric // tracking values through callee saved registers to their eventual 41139d628a0SDimitry Andric // spill location. This would be a useful optimization, but would 41239d628a0SDimitry Andric // need to be optional since it requires a lot of complexity on the 41339d628a0SDimitry Andric // runtime side which not all would support. 4143ca95b02SDimitry Andric auto Res = spillIncomingStatepointValue(Incoming, Chain, Builder); 41539d628a0SDimitry Andric Ops.push_back(Res.first); 41639d628a0SDimitry Andric Chain = Res.second; 41739d628a0SDimitry Andric } 41839d628a0SDimitry Andric 41939d628a0SDimitry Andric Builder.DAG.setRoot(Chain); 42039d628a0SDimitry Andric } 42139d628a0SDimitry Andric 42239d628a0SDimitry Andric /// Lower deopt state and gc pointer arguments of the statepoint. The actual 42339d628a0SDimitry Andric /// lowering is described in lowerIncomingStatepointValue. This function is 42439d628a0SDimitry Andric /// responsible for lowering everything in the right position and playing some 42539d628a0SDimitry Andric /// tricks to avoid redundant stack manipulation where possible. On 42639d628a0SDimitry Andric /// completion, 'Ops' will contain ready to use operands for machine code 42739d628a0SDimitry Andric /// statepoint. The chain nodes will have already been created and the DAG root 42839d628a0SDimitry Andric /// will be set to the last value spilled (if any were). 4293ca95b02SDimitry Andric static void 4303ca95b02SDimitry Andric lowerStatepointMetaArgs(SmallVectorImpl<SDValue> &Ops, 4313ca95b02SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI, 43239d628a0SDimitry Andric SelectionDAGBuilder &Builder) { 4333ca95b02SDimitry Andric // Lower the deopt and gc arguments for this statepoint. Layout will be: 4343ca95b02SDimitry Andric // deopt argument length, deopt arguments.., gc arguments... 43539d628a0SDimitry Andric #ifndef NDEBUG 4363ca95b02SDimitry Andric if (auto *GFI = Builder.GFI) { 43739d628a0SDimitry Andric // Check that each of the gc pointer and bases we've gotten out of the 438444ed5c5SDimitry Andric // safepoint is something the strategy thinks might be a pointer (or vector 439444ed5c5SDimitry Andric // of pointers) into the GC heap. This is basically just here to help catch 440444ed5c5SDimitry Andric // errors during statepoint insertion. TODO: This should actually be in the 441444ed5c5SDimitry Andric // Verifier, but we can't get to the GCStrategy from there (yet). 4423ca95b02SDimitry Andric GCStrategy &S = GFI->getStrategy(); 4433ca95b02SDimitry Andric for (const Value *V : SI.Bases) { 444444ed5c5SDimitry Andric auto Opt = S.isGCManagedPointer(V->getType()->getScalarType()); 44539d628a0SDimitry Andric if (Opt.hasValue()) { 44639d628a0SDimitry Andric assert(Opt.getValue() && 44739d628a0SDimitry Andric "non gc managed base pointer found in statepoint"); 44839d628a0SDimitry Andric } 44939d628a0SDimitry Andric } 4503ca95b02SDimitry Andric for (const Value *V : SI.Ptrs) { 451444ed5c5SDimitry Andric auto Opt = S.isGCManagedPointer(V->getType()->getScalarType()); 45239d628a0SDimitry Andric if (Opt.hasValue()) { 45339d628a0SDimitry Andric assert(Opt.getValue() && 45439d628a0SDimitry Andric "non gc managed derived pointer found in statepoint"); 45539d628a0SDimitry Andric } 45639d628a0SDimitry Andric } 457d88c1a5aSDimitry Andric assert(SI.Bases.size() == SI.Ptrs.size() && "Pointer without base!"); 4583ca95b02SDimitry Andric } else { 4593ca95b02SDimitry Andric assert(SI.Bases.empty() && "No gc specified, so cannot relocate pointers!"); 4603ca95b02SDimitry Andric assert(SI.Ptrs.empty() && "No gc specified, so cannot relocate pointers!"); 46139d628a0SDimitry Andric } 46239d628a0SDimitry Andric #endif 46339d628a0SDimitry Andric 464d88c1a5aSDimitry Andric // Figure out what lowering strategy we're going to use for each part 465d88c1a5aSDimitry Andric // Note: Is is conservatively correct to lower both "live-in" and "live-out" 466d88c1a5aSDimitry Andric // as "live-through". A "live-through" variable is one which is "live-in", 467d88c1a5aSDimitry Andric // "live-out", and live throughout the lifetime of the call (i.e. we can find 468d88c1a5aSDimitry Andric // it from any PC within the transitive callee of the statepoint). In 469d88c1a5aSDimitry Andric // particular, if the callee spills callee preserved registers we may not 470d88c1a5aSDimitry Andric // be able to find a value placed in that register during the call. This is 471d88c1a5aSDimitry Andric // fine for live-out, but not for live-through. If we were willing to make 472d88c1a5aSDimitry Andric // assumptions about the code generator producing the callee, we could 473d88c1a5aSDimitry Andric // potentially allow live-through values in callee saved registers. 474d88c1a5aSDimitry Andric const bool LiveInDeopt = 475d88c1a5aSDimitry Andric SI.StatepointFlags & (uint64_t)StatepointFlags::DeoptLiveIn; 476d88c1a5aSDimitry Andric 477d88c1a5aSDimitry Andric auto isGCValue =[&](const Value *V) { 478d88c1a5aSDimitry Andric return is_contained(SI.Ptrs, V) || is_contained(SI.Bases, V); 479d88c1a5aSDimitry Andric }; 480d88c1a5aSDimitry Andric 48139d628a0SDimitry Andric // Before we actually start lowering (and allocating spill slots for values), 48239d628a0SDimitry Andric // reserve any stack slots which we judge to be profitable to reuse for a 48339d628a0SDimitry Andric // particular value. This is purely an optimization over the code below and 48439d628a0SDimitry Andric // doesn't change semantics at all. It is important for performance that we 48539d628a0SDimitry Andric // reserve slots for both deopt and gc values before lowering either. 4863ca95b02SDimitry Andric for (const Value *V : SI.DeoptState) { 487d88c1a5aSDimitry Andric if (!LiveInDeopt || isGCValue(V)) 4888f0fd8f6SDimitry Andric reservePreviousStackSlotForValue(V, Builder); 48939d628a0SDimitry Andric } 4903ca95b02SDimitry Andric for (unsigned i = 0; i < SI.Bases.size(); ++i) { 4913ca95b02SDimitry Andric reservePreviousStackSlotForValue(SI.Bases[i], Builder); 4923ca95b02SDimitry Andric reservePreviousStackSlotForValue(SI.Ptrs[i], Builder); 49339d628a0SDimitry Andric } 49439d628a0SDimitry Andric 49539d628a0SDimitry Andric // First, prefix the list with the number of unique values to be 49639d628a0SDimitry Andric // lowered. Note that this is the number of *Values* not the 49739d628a0SDimitry Andric // number of SDValues required to lower them. 4983ca95b02SDimitry Andric const int NumVMSArgs = SI.DeoptState.size(); 499ff0cc061SDimitry Andric pushStackMapConstant(Ops, Builder, NumVMSArgs); 50039d628a0SDimitry Andric 5013ca95b02SDimitry Andric // The vm state arguments are lowered in an opaque manner. We do not know 5023ca95b02SDimitry Andric // what type of values are contained within. 5033ca95b02SDimitry Andric for (const Value *V : SI.DeoptState) { 50439d628a0SDimitry Andric SDValue Incoming = Builder.getValue(V); 505d88c1a5aSDimitry Andric const bool LiveInValue = LiveInDeopt && !isGCValue(V); 506d88c1a5aSDimitry Andric lowerIncomingStatepointValue(Incoming, LiveInValue, Ops, Builder); 50739d628a0SDimitry Andric } 50839d628a0SDimitry Andric 50939d628a0SDimitry Andric // Finally, go ahead and lower all the gc arguments. There's no prefixed 51039d628a0SDimitry Andric // length for this one. After lowering, we'll have the base and pointer 51139d628a0SDimitry Andric // arrays interwoven with each (lowered) base pointer immediately followed by 51239d628a0SDimitry Andric // it's (lowered) derived pointer. i.e 51339d628a0SDimitry Andric // (base[0], ptr[0], base[1], ptr[1], ...) 5143ca95b02SDimitry Andric for (unsigned i = 0; i < SI.Bases.size(); ++i) { 5153ca95b02SDimitry Andric const Value *Base = SI.Bases[i]; 516d88c1a5aSDimitry Andric lowerIncomingStatepointValue(Builder.getValue(Base), /*LiveInOnly*/ false, 517d88c1a5aSDimitry Andric Ops, Builder); 518ff0cc061SDimitry Andric 5193ca95b02SDimitry Andric const Value *Ptr = SI.Ptrs[i]; 520d88c1a5aSDimitry Andric lowerIncomingStatepointValue(Builder.getValue(Ptr), /*LiveInOnly*/ false, 521d88c1a5aSDimitry Andric Ops, Builder); 522ff0cc061SDimitry Andric } 523ff0cc061SDimitry Andric 524ff0cc061SDimitry Andric // If there are any explicit spill slots passed to the statepoint, record 525ff0cc061SDimitry Andric // them, but otherwise do not do anything special. These are user provided 526ff0cc061SDimitry Andric // allocas and give control over placement to the consumer. In this case, 527ff0cc061SDimitry Andric // it is the contents of the slot which may get updated, not the pointer to 528ff0cc061SDimitry Andric // the alloca 5293ca95b02SDimitry Andric for (Value *V : SI.GCArgs) { 53039d628a0SDimitry Andric SDValue Incoming = Builder.getValue(V); 531ff0cc061SDimitry Andric if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { 532ff0cc061SDimitry Andric // This handles allocas as arguments to the statepoint 533f37b6182SDimitry Andric assert(Incoming.getValueType() == Builder.getFrameIndexTy() && 534f37b6182SDimitry Andric "Incoming value is a frame index!"); 535ff0cc061SDimitry Andric Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), 536f37b6182SDimitry Andric Builder.getFrameIndexTy())); 53739d628a0SDimitry Andric } 53839d628a0SDimitry Andric } 539ff0cc061SDimitry Andric 540ff0cc061SDimitry Andric // Record computed locations for all lowered values. 541ff0cc061SDimitry Andric // This can not be embedded in lowering loops as we need to record *all* 542ff0cc061SDimitry Andric // values, while previous loops account only values with unique SDValues. 5433ca95b02SDimitry Andric const Instruction *StatepointInstr = SI.StatepointInstr; 5443ca95b02SDimitry Andric auto &SpillMap = Builder.FuncInfo.StatepointSpillMaps[StatepointInstr]; 545ff0cc061SDimitry Andric 5463ca95b02SDimitry Andric for (const GCRelocateInst *Relocate : SI.GCRelocates) { 5474d0b32cdSDimitry Andric const Value *V = Relocate->getDerivedPtr(); 548ff0cc061SDimitry Andric SDValue SDV = Builder.getValue(V); 549ff0cc061SDimitry Andric SDValue Loc = Builder.StatepointLowering.getLocation(SDV); 550ff0cc061SDimitry Andric 551ff0cc061SDimitry Andric if (Loc.getNode()) { 5523ca95b02SDimitry Andric SpillMap.SlotMap[V] = cast<FrameIndexSDNode>(Loc)->getIndex(); 553ff0cc061SDimitry Andric } else { 554ff0cc061SDimitry Andric // Record value as visited, but not spilled. This is case for allocas 5557d523365SDimitry Andric // and constants. For this values we can avoid emitting spill load while 556ff0cc061SDimitry Andric // visiting corresponding gc_relocate. 557ff0cc061SDimitry Andric // Actually we do not need to record them in this map at all. 5587d523365SDimitry Andric // We do this only to check that we are not relocating any unvisited 5597d523365SDimitry Andric // value. 5603ca95b02SDimitry Andric SpillMap.SlotMap[V] = None; 561ff0cc061SDimitry Andric 562ff0cc061SDimitry Andric // Default llvm mechanisms for exporting values which are used in 563ff0cc061SDimitry Andric // different basic blocks does not work for gc relocates. 564ff0cc061SDimitry Andric // Note that it would be incorrect to teach llvm that all relocates are 5657d523365SDimitry Andric // uses of the corresponding values so that it would automatically 566ff0cc061SDimitry Andric // export them. Relocates of the spilled values does not use original 567ff0cc061SDimitry Andric // value. 5684d0b32cdSDimitry Andric if (Relocate->getParent() != StatepointInstr->getParent()) 569ff0cc061SDimitry Andric Builder.ExportFromCurrentBlock(V); 570ff0cc061SDimitry Andric } 571ff0cc061SDimitry Andric } 572ff0cc061SDimitry Andric } 573ff0cc061SDimitry Andric 5743ca95b02SDimitry Andric SDValue SelectionDAGBuilder::LowerAsSTATEPOINT( 5753ca95b02SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI) { 57639d628a0SDimitry Andric // The basic scheme here is that information about both the original call and 57739d628a0SDimitry Andric // the safepoint is encoded in the CallInst. We create a temporary call and 57839d628a0SDimitry Andric // lower it, then reverse engineer the calling sequence. 57939d628a0SDimitry Andric 58039d628a0SDimitry Andric NumOfStatepoints++; 58139d628a0SDimitry Andric // Clear state 58239d628a0SDimitry Andric StatepointLowering.startNewStatepoint(*this); 58339d628a0SDimitry Andric 58439d628a0SDimitry Andric #ifndef NDEBUG 5853ca95b02SDimitry Andric // We schedule gc relocates before removeDuplicateGCPtrs since we _will_ 5863ca95b02SDimitry Andric // encounter the duplicate gc relocates we elide in removeDuplicateGCPtrs. 5873ca95b02SDimitry Andric for (auto *Reloc : SI.GCRelocates) 5883ca95b02SDimitry Andric if (Reloc->getParent() == SI.StatepointInstr->getParent()) 5893ca95b02SDimitry Andric StatepointLowering.scheduleRelocCall(*Reloc); 59039d628a0SDimitry Andric #endif 59139d628a0SDimitry Andric 5923ca95b02SDimitry Andric // Remove any redundant llvm::Values which map to the same SDValue as another 5933ca95b02SDimitry Andric // input. Also has the effect of removing duplicates in the original 5943ca95b02SDimitry Andric // llvm::Value input list as well. This is a useful optimization for 5953ca95b02SDimitry Andric // reducing the size of the StackMap section. It has no other impact. 5963ca95b02SDimitry Andric removeDuplicateGCPtrs(SI.Bases, SI.Ptrs, SI.GCRelocates, *this, 5973ca95b02SDimitry Andric FuncInfo.StatepointSpillMaps[SI.StatepointInstr]); 5983ca95b02SDimitry Andric assert(SI.Bases.size() == SI.Ptrs.size() && 5993ca95b02SDimitry Andric SI.Ptrs.size() == SI.GCRelocates.size()); 60039d628a0SDimitry Andric 60139d628a0SDimitry Andric // Lower statepoint vmstate and gcstate arguments 602ff0cc061SDimitry Andric SmallVector<SDValue, 10> LoweredMetaArgs; 6033ca95b02SDimitry Andric lowerStatepointMetaArgs(LoweredMetaArgs, SI, *this); 6043ca95b02SDimitry Andric 6053ca95b02SDimitry Andric // Now that we've emitted the spills, we need to update the root so that the 6063ca95b02SDimitry Andric // call sequence is ordered correctly. 6073ca95b02SDimitry Andric SI.CLI.setChain(getRoot()); 60839d628a0SDimitry Andric 60939d628a0SDimitry Andric // Get call node, we will replace it later with statepoint 6103ca95b02SDimitry Andric SDValue ReturnVal; 6113ca95b02SDimitry Andric SDNode *CallNode; 6123ca95b02SDimitry Andric std::tie(ReturnVal, CallNode) = 6133ca95b02SDimitry Andric lowerCallFromStatepointLoweringInfo(SI, *this, PendingExports); 61439d628a0SDimitry Andric 615ff0cc061SDimitry Andric // Construct the actual GC_TRANSITION_START, STATEPOINT, and GC_TRANSITION_END 616ff0cc061SDimitry Andric // nodes with all the appropriate arguments and return values. 617ff0cc061SDimitry Andric 618ff0cc061SDimitry Andric // Call Node: Chain, Target, {Args}, RegMask, [Glue] 619ff0cc061SDimitry Andric SDValue Chain = CallNode->getOperand(0); 620ff0cc061SDimitry Andric 621ff0cc061SDimitry Andric SDValue Glue; 622ff0cc061SDimitry Andric bool CallHasIncomingGlue = CallNode->getGluedNode(); 623ff0cc061SDimitry Andric if (CallHasIncomingGlue) { 624ff0cc061SDimitry Andric // Glue is always last operand 625ff0cc061SDimitry Andric Glue = CallNode->getOperand(CallNode->getNumOperands() - 1); 626ff0cc061SDimitry Andric } 627ff0cc061SDimitry Andric 628ff0cc061SDimitry Andric // Build the GC_TRANSITION_START node if necessary. 629ff0cc061SDimitry Andric // 630ff0cc061SDimitry Andric // The operands to the GC_TRANSITION_{START,END} nodes are laid out in the 631ff0cc061SDimitry Andric // order in which they appear in the call to the statepoint intrinsic. If 632ff0cc061SDimitry Andric // any of the operands is a pointer-typed, that operand is immediately 633ff0cc061SDimitry Andric // followed by a SRCVALUE for the pointer that may be used during lowering 634ff0cc061SDimitry Andric // (e.g. to form MachinePointerInfo values for loads/stores). 635ff0cc061SDimitry Andric const bool IsGCTransition = 6363ca95b02SDimitry Andric (SI.StatepointFlags & (uint64_t)StatepointFlags::GCTransition) == 637ff0cc061SDimitry Andric (uint64_t)StatepointFlags::GCTransition; 638ff0cc061SDimitry Andric if (IsGCTransition) { 639ff0cc061SDimitry Andric SmallVector<SDValue, 8> TSOps; 640ff0cc061SDimitry Andric 641ff0cc061SDimitry Andric // Add chain 642ff0cc061SDimitry Andric TSOps.push_back(Chain); 643ff0cc061SDimitry Andric 644ff0cc061SDimitry Andric // Add GC transition arguments 6453ca95b02SDimitry Andric for (const Value *V : SI.GCTransitionArgs) { 646ff0cc061SDimitry Andric TSOps.push_back(getValue(V)); 647ff0cc061SDimitry Andric if (V->getType()->isPointerTy()) 648ff0cc061SDimitry Andric TSOps.push_back(DAG.getSrcValue(V)); 649ff0cc061SDimitry Andric } 650ff0cc061SDimitry Andric 651ff0cc061SDimitry Andric // Add glue if necessary 652ff0cc061SDimitry Andric if (CallHasIncomingGlue) 653ff0cc061SDimitry Andric TSOps.push_back(Glue); 654ff0cc061SDimitry Andric 655ff0cc061SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 656ff0cc061SDimitry Andric 657ff0cc061SDimitry Andric SDValue GCTransitionStart = 658ff0cc061SDimitry Andric DAG.getNode(ISD::GC_TRANSITION_START, getCurSDLoc(), NodeTys, TSOps); 659ff0cc061SDimitry Andric 660ff0cc061SDimitry Andric Chain = GCTransitionStart.getValue(0); 661ff0cc061SDimitry Andric Glue = GCTransitionStart.getValue(1); 662ff0cc061SDimitry Andric } 66339d628a0SDimitry Andric 66439d628a0SDimitry Andric // TODO: Currently, all of these operands are being marked as read/write in 66539d628a0SDimitry Andric // PrologEpilougeInserter.cpp, we should special case the VMState arguments 66639d628a0SDimitry Andric // and flags to be read-only. 66739d628a0SDimitry Andric SmallVector<SDValue, 40> Ops; 66839d628a0SDimitry Andric 669ff0cc061SDimitry Andric // Add the <id> and <numBytes> constants. 6703ca95b02SDimitry Andric Ops.push_back(DAG.getTargetConstant(SI.ID, getCurSDLoc(), MVT::i64)); 671ff0cc061SDimitry Andric Ops.push_back( 6723ca95b02SDimitry Andric DAG.getTargetConstant(SI.NumPatchBytes, getCurSDLoc(), MVT::i32)); 673ff0cc061SDimitry Andric 67439d628a0SDimitry Andric // Calculate and push starting position of vmstate arguments 67539d628a0SDimitry Andric // Get number of arguments incoming directly into call node 67639d628a0SDimitry Andric unsigned NumCallRegArgs = 677ff0cc061SDimitry Andric CallNode->getNumOperands() - (CallHasIncomingGlue ? 4 : 3); 678ff0cc061SDimitry Andric Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, getCurSDLoc(), MVT::i32)); 67939d628a0SDimitry Andric 68039d628a0SDimitry Andric // Add call target 68139d628a0SDimitry Andric SDValue CallTarget = SDValue(CallNode->getOperand(1).getNode(), 0); 68239d628a0SDimitry Andric Ops.push_back(CallTarget); 68339d628a0SDimitry Andric 68439d628a0SDimitry Andric // Add call arguments 68539d628a0SDimitry Andric // Get position of register mask in the call 68639d628a0SDimitry Andric SDNode::op_iterator RegMaskIt; 687ff0cc061SDimitry Andric if (CallHasIncomingGlue) 68839d628a0SDimitry Andric RegMaskIt = CallNode->op_end() - 2; 68939d628a0SDimitry Andric else 69039d628a0SDimitry Andric RegMaskIt = CallNode->op_end() - 1; 69139d628a0SDimitry Andric Ops.insert(Ops.end(), CallNode->op_begin() + 2, RegMaskIt); 69239d628a0SDimitry Andric 693ff0cc061SDimitry Andric // Add a constant argument for the calling convention 6943ca95b02SDimitry Andric pushStackMapConstant(Ops, *this, SI.CLI.CallConv); 695ff0cc061SDimitry Andric 696ff0cc061SDimitry Andric // Add a constant argument for the flags 6973ca95b02SDimitry Andric uint64_t Flags = SI.StatepointFlags; 6983ca95b02SDimitry Andric assert(((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0) && 6993ca95b02SDimitry Andric "Unknown flag used"); 700ff0cc061SDimitry Andric pushStackMapConstant(Ops, *this, Flags); 70139d628a0SDimitry Andric 70239d628a0SDimitry Andric // Insert all vmstate and gcstate arguments 703ff0cc061SDimitry Andric Ops.insert(Ops.end(), LoweredMetaArgs.begin(), LoweredMetaArgs.end()); 70439d628a0SDimitry Andric 70539d628a0SDimitry Andric // Add register mask from call node 70639d628a0SDimitry Andric Ops.push_back(*RegMaskIt); 70739d628a0SDimitry Andric 70839d628a0SDimitry Andric // Add chain 709ff0cc061SDimitry Andric Ops.push_back(Chain); 71039d628a0SDimitry Andric 71139d628a0SDimitry Andric // Same for the glue, but we add it only if original call had it 71239d628a0SDimitry Andric if (Glue.getNode()) 71339d628a0SDimitry Andric Ops.push_back(Glue); 71439d628a0SDimitry Andric 715ff0cc061SDimitry Andric // Compute return values. Provide a glue output since we consume one as 716ff0cc061SDimitry Andric // input. This allows someone else to chain off us as needed. 717ff0cc061SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 71839d628a0SDimitry Andric 719ff0cc061SDimitry Andric SDNode *StatepointMCNode = 720ff0cc061SDimitry Andric DAG.getMachineNode(TargetOpcode::STATEPOINT, getCurSDLoc(), NodeTys, Ops); 721ff0cc061SDimitry Andric 722ff0cc061SDimitry Andric SDNode *SinkNode = StatepointMCNode; 723ff0cc061SDimitry Andric 724ff0cc061SDimitry Andric // Build the GC_TRANSITION_END node if necessary. 725ff0cc061SDimitry Andric // 726ff0cc061SDimitry Andric // See the comment above regarding GC_TRANSITION_START for the layout of 727ff0cc061SDimitry Andric // the operands to the GC_TRANSITION_END node. 728ff0cc061SDimitry Andric if (IsGCTransition) { 729ff0cc061SDimitry Andric SmallVector<SDValue, 8> TEOps; 730ff0cc061SDimitry Andric 731ff0cc061SDimitry Andric // Add chain 732ff0cc061SDimitry Andric TEOps.push_back(SDValue(StatepointMCNode, 0)); 733ff0cc061SDimitry Andric 734ff0cc061SDimitry Andric // Add GC transition arguments 7353ca95b02SDimitry Andric for (const Value *V : SI.GCTransitionArgs) { 736ff0cc061SDimitry Andric TEOps.push_back(getValue(V)); 737ff0cc061SDimitry Andric if (V->getType()->isPointerTy()) 738ff0cc061SDimitry Andric TEOps.push_back(DAG.getSrcValue(V)); 739ff0cc061SDimitry Andric } 740ff0cc061SDimitry Andric 741ff0cc061SDimitry Andric // Add glue 742ff0cc061SDimitry Andric TEOps.push_back(SDValue(StatepointMCNode, 1)); 743ff0cc061SDimitry Andric 744ff0cc061SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 745ff0cc061SDimitry Andric 746ff0cc061SDimitry Andric SDValue GCTransitionStart = 747ff0cc061SDimitry Andric DAG.getNode(ISD::GC_TRANSITION_END, getCurSDLoc(), NodeTys, TEOps); 748ff0cc061SDimitry Andric 749ff0cc061SDimitry Andric SinkNode = GCTransitionStart.getNode(); 750ff0cc061SDimitry Andric } 75139d628a0SDimitry Andric 75239d628a0SDimitry Andric // Replace original call 753ff0cc061SDimitry Andric DAG.ReplaceAllUsesWith(CallNode, SinkNode); // This may update Root 7547d523365SDimitry Andric // Remove original call node 75539d628a0SDimitry Andric DAG.DeleteNode(CallNode); 75639d628a0SDimitry Andric 75739d628a0SDimitry Andric // DON'T set the root - under the assumption that it's already set past the 75839d628a0SDimitry Andric // inserted node we created. 75939d628a0SDimitry Andric 76039d628a0SDimitry Andric // TODO: A better future implementation would be to emit a single variable 76139d628a0SDimitry Andric // argument, variable return value STATEPOINT node here and then hookup the 76239d628a0SDimitry Andric // return value of each gc.relocate to the respective output of the 76339d628a0SDimitry Andric // previously emitted STATEPOINT value. Unfortunately, this doesn't appear 76439d628a0SDimitry Andric // to actually be possible today. 7653ca95b02SDimitry Andric 7663ca95b02SDimitry Andric return ReturnVal; 76739d628a0SDimitry Andric } 76839d628a0SDimitry Andric 7693ca95b02SDimitry Andric void 7703ca95b02SDimitry Andric SelectionDAGBuilder::LowerStatepoint(ImmutableStatepoint ISP, 7713ca95b02SDimitry Andric const BasicBlock *EHPadBB /*= nullptr*/) { 7723ca95b02SDimitry Andric assert(ISP.getCallSite().getCallingConv() != CallingConv::AnyReg && 7733ca95b02SDimitry Andric "anyregcc is not supported on statepoints!"); 7743ca95b02SDimitry Andric 7753ca95b02SDimitry Andric #ifndef NDEBUG 7763ca95b02SDimitry Andric // If this is a malformed statepoint, report it early to simplify debugging. 7773ca95b02SDimitry Andric // This should catch any IR level mistake that's made when constructing or 7783ca95b02SDimitry Andric // transforming statepoints. 7793ca95b02SDimitry Andric ISP.verify(); 7803ca95b02SDimitry Andric 7813ca95b02SDimitry Andric // Check that the associated GCStrategy expects to encounter statepoints. 7823ca95b02SDimitry Andric assert(GFI->getStrategy().useStatepoints() && 7833ca95b02SDimitry Andric "GCStrategy does not expect to encounter statepoints"); 7843ca95b02SDimitry Andric #endif 7853ca95b02SDimitry Andric 7863ca95b02SDimitry Andric SDValue ActualCallee; 7873ca95b02SDimitry Andric 7883ca95b02SDimitry Andric if (ISP.getNumPatchBytes() > 0) { 7893ca95b02SDimitry Andric // If we've been asked to emit a nop sequence instead of a call instruction 7903ca95b02SDimitry Andric // for this statepoint then don't lower the call target, but use a constant 7913ca95b02SDimitry Andric // `null` instead. Not lowering the call target lets statepoint clients get 7923ca95b02SDimitry Andric // away without providing a physical address for the symbolic call target at 7933ca95b02SDimitry Andric // link time. 7943ca95b02SDimitry Andric 7953ca95b02SDimitry Andric const auto &TLI = DAG.getTargetLoweringInfo(); 7963ca95b02SDimitry Andric const auto &DL = DAG.getDataLayout(); 7973ca95b02SDimitry Andric 7983ca95b02SDimitry Andric unsigned AS = ISP.getCalledValue()->getType()->getPointerAddressSpace(); 7993ca95b02SDimitry Andric ActualCallee = DAG.getConstant(0, getCurSDLoc(), TLI.getPointerTy(DL, AS)); 8003ca95b02SDimitry Andric } else { 8013ca95b02SDimitry Andric ActualCallee = getValue(ISP.getCalledValue()); 8023ca95b02SDimitry Andric } 8033ca95b02SDimitry Andric 8043ca95b02SDimitry Andric StatepointLoweringInfo SI(DAG); 8053ca95b02SDimitry Andric populateCallLoweringInfo(SI.CLI, ISP.getCallSite(), 8063ca95b02SDimitry Andric ImmutableStatepoint::CallArgsBeginPos, 8073ca95b02SDimitry Andric ISP.getNumCallArgs(), ActualCallee, 8083ca95b02SDimitry Andric ISP.getActualReturnType(), false /* IsPatchPoint */); 8093ca95b02SDimitry Andric 8103ca95b02SDimitry Andric for (const GCRelocateInst *Relocate : ISP.getRelocates()) { 8113ca95b02SDimitry Andric SI.GCRelocates.push_back(Relocate); 8123ca95b02SDimitry Andric SI.Bases.push_back(Relocate->getBasePtr()); 8133ca95b02SDimitry Andric SI.Ptrs.push_back(Relocate->getDerivedPtr()); 8143ca95b02SDimitry Andric } 8153ca95b02SDimitry Andric 8163ca95b02SDimitry Andric SI.GCArgs = ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end()); 8173ca95b02SDimitry Andric SI.StatepointInstr = ISP.getInstruction(); 8183ca95b02SDimitry Andric SI.GCTransitionArgs = 8193ca95b02SDimitry Andric ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end()); 8203ca95b02SDimitry Andric SI.ID = ISP.getID(); 8216d97bb29SDimitry Andric SI.DeoptState = ArrayRef<const Use>(ISP.deopt_begin(), ISP.deopt_end()); 8223ca95b02SDimitry Andric SI.StatepointFlags = ISP.getFlags(); 8233ca95b02SDimitry Andric SI.NumPatchBytes = ISP.getNumPatchBytes(); 8243ca95b02SDimitry Andric SI.EHPadBB = EHPadBB; 8253ca95b02SDimitry Andric 8263ca95b02SDimitry Andric SDValue ReturnValue = LowerAsSTATEPOINT(SI); 8273ca95b02SDimitry Andric 8283ca95b02SDimitry Andric // Export the result value if needed 8293ca95b02SDimitry Andric const GCResultInst *GCResult = ISP.getGCResult(); 8303ca95b02SDimitry Andric Type *RetTy = ISP.getActualReturnType(); 8313ca95b02SDimitry Andric if (!RetTy->isVoidTy() && GCResult) { 8323ca95b02SDimitry Andric if (GCResult->getParent() != ISP.getCallSite().getParent()) { 8333ca95b02SDimitry Andric // Result value will be used in a different basic block so we need to 8343ca95b02SDimitry Andric // export it now. Default exporting mechanism will not work here because 8353ca95b02SDimitry Andric // statepoint call has a different type than the actual call. It means 8363ca95b02SDimitry Andric // that by default llvm will create export register of the wrong type 8373ca95b02SDimitry Andric // (always i32 in our case). So instead we need to create export register 8383ca95b02SDimitry Andric // with correct type manually. 8393ca95b02SDimitry Andric // TODO: To eliminate this problem we can remove gc.result intrinsics 8403ca95b02SDimitry Andric // completely and make statepoint call to return a tuple. 8413ca95b02SDimitry Andric unsigned Reg = FuncInfo.CreateRegs(RetTy); 8423ca95b02SDimitry Andric RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(), 8433ca95b02SDimitry Andric DAG.getDataLayout(), Reg, RetTy); 8443ca95b02SDimitry Andric SDValue Chain = DAG.getEntryNode(); 8453ca95b02SDimitry Andric 8463ca95b02SDimitry Andric RFV.getCopyToRegs(ReturnValue, DAG, getCurSDLoc(), Chain, nullptr); 8473ca95b02SDimitry Andric PendingExports.push_back(Chain); 8483ca95b02SDimitry Andric FuncInfo.ValueMap[ISP.getInstruction()] = Reg; 8493ca95b02SDimitry Andric } else { 8503ca95b02SDimitry Andric // Result value will be used in a same basic block. Don't export it or 8513ca95b02SDimitry Andric // perform any explicit register copies. 8523ca95b02SDimitry Andric // We'll replace the actuall call node shortly. gc_result will grab 8533ca95b02SDimitry Andric // this value. 8543ca95b02SDimitry Andric setValue(ISP.getInstruction(), ReturnValue); 8553ca95b02SDimitry Andric } 8563ca95b02SDimitry Andric } else { 8573ca95b02SDimitry Andric // The token value is never used from here on, just generate a poison value 8583ca95b02SDimitry Andric setValue(ISP.getInstruction(), DAG.getIntPtrConstant(-1, getCurSDLoc())); 8593ca95b02SDimitry Andric } 8603ca95b02SDimitry Andric } 8613ca95b02SDimitry Andric 8623ca95b02SDimitry Andric void SelectionDAGBuilder::LowerCallSiteWithDeoptBundleImpl( 8633ca95b02SDimitry Andric ImmutableCallSite CS, SDValue Callee, const BasicBlock *EHPadBB, 8643ca95b02SDimitry Andric bool VarArgDisallowed, bool ForceVoidReturnTy) { 8653ca95b02SDimitry Andric StatepointLoweringInfo SI(DAG); 8663ca95b02SDimitry Andric unsigned ArgBeginIndex = CS.arg_begin() - CS.getInstruction()->op_begin(); 8673ca95b02SDimitry Andric populateCallLoweringInfo( 8683ca95b02SDimitry Andric SI.CLI, CS, ArgBeginIndex, CS.getNumArgOperands(), Callee, 8693ca95b02SDimitry Andric ForceVoidReturnTy ? Type::getVoidTy(*DAG.getContext()) : CS.getType(), 8703ca95b02SDimitry Andric false); 8713ca95b02SDimitry Andric if (!VarArgDisallowed) 8723ca95b02SDimitry Andric SI.CLI.IsVarArg = CS.getFunctionType()->isVarArg(); 8733ca95b02SDimitry Andric 8743ca95b02SDimitry Andric auto DeoptBundle = *CS.getOperandBundle(LLVMContext::OB_deopt); 8753ca95b02SDimitry Andric 8763ca95b02SDimitry Andric unsigned DefaultID = StatepointDirectives::DeoptBundleStatepointID; 8773ca95b02SDimitry Andric 8783ca95b02SDimitry Andric auto SD = parseStatepointDirectivesFromAttrs(CS.getAttributes()); 8793ca95b02SDimitry Andric SI.ID = SD.StatepointID.getValueOr(DefaultID); 8803ca95b02SDimitry Andric SI.NumPatchBytes = SD.NumPatchBytes.getValueOr(0); 8813ca95b02SDimitry Andric 8823ca95b02SDimitry Andric SI.DeoptState = 8833ca95b02SDimitry Andric ArrayRef<const Use>(DeoptBundle.Inputs.begin(), DeoptBundle.Inputs.end()); 8843ca95b02SDimitry Andric SI.StatepointFlags = static_cast<uint64_t>(StatepointFlags::None); 8853ca95b02SDimitry Andric SI.EHPadBB = EHPadBB; 8863ca95b02SDimitry Andric 8873ca95b02SDimitry Andric // NB! The GC arguments are deliberately left empty. 8883ca95b02SDimitry Andric 8893ca95b02SDimitry Andric if (SDValue ReturnVal = LowerAsSTATEPOINT(SI)) { 8903ca95b02SDimitry Andric const Instruction *Inst = CS.getInstruction(); 8913ca95b02SDimitry Andric ReturnVal = lowerRangeToAssertZExt(DAG, *Inst, ReturnVal); 8923ca95b02SDimitry Andric setValue(Inst, ReturnVal); 8933ca95b02SDimitry Andric } 8943ca95b02SDimitry Andric } 8953ca95b02SDimitry Andric 8963ca95b02SDimitry Andric void SelectionDAGBuilder::LowerCallSiteWithDeoptBundle( 8973ca95b02SDimitry Andric ImmutableCallSite CS, SDValue Callee, const BasicBlock *EHPadBB) { 8983ca95b02SDimitry Andric LowerCallSiteWithDeoptBundleImpl(CS, Callee, EHPadBB, 8993ca95b02SDimitry Andric /* VarArgDisallowed = */ false, 9003ca95b02SDimitry Andric /* ForceVoidReturnTy = */ false); 9013ca95b02SDimitry Andric } 9023ca95b02SDimitry Andric 9033ca95b02SDimitry Andric void SelectionDAGBuilder::visitGCResult(const GCResultInst &CI) { 90439d628a0SDimitry Andric // The result value of the gc_result is simply the result of the actual 90539d628a0SDimitry Andric // call. We've already emitted this, so just grab the value. 9063ca95b02SDimitry Andric const Instruction *I = CI.getStatepoint(); 90739d628a0SDimitry Andric 9087d523365SDimitry Andric if (I->getParent() != CI.getParent()) { 9097d523365SDimitry Andric // Statepoint is in different basic block so we should have stored call 9107d523365SDimitry Andric // result in a virtual register. 911ff0cc061SDimitry Andric // We can not use default getValue() functionality to copy value from this 9123ca95b02SDimitry Andric // register because statepoint and actual call return types can be 913ff0cc061SDimitry Andric // different, and getValue() will use CopyFromReg of the wrong type, 914ff0cc061SDimitry Andric // which is always i32 in our case. 9153dac3a9bSDimitry Andric PointerType *CalleeType = cast<PointerType>( 9163dac3a9bSDimitry Andric ImmutableStatepoint(I).getCalledValue()->getType()); 917ff0cc061SDimitry Andric Type *RetTy = 918ff0cc061SDimitry Andric cast<FunctionType>(CalleeType->getElementType())->getReturnType(); 919ff0cc061SDimitry Andric SDValue CopyFromReg = getCopyFromRegs(I, RetTy); 920ff0cc061SDimitry Andric 921ff0cc061SDimitry Andric assert(CopyFromReg.getNode()); 922ff0cc061SDimitry Andric setValue(&CI, CopyFromReg); 923ff0cc061SDimitry Andric } else { 92439d628a0SDimitry Andric setValue(&CI, getValue(I)); 92539d628a0SDimitry Andric } 926ff0cc061SDimitry Andric } 92739d628a0SDimitry Andric 9284d0b32cdSDimitry Andric void SelectionDAGBuilder::visitGCRelocate(const GCRelocateInst &Relocate) { 92939d628a0SDimitry Andric #ifndef NDEBUG 93039d628a0SDimitry Andric // Consistency check 931d88c1a5aSDimitry Andric // We skip this check for relocates not in the same basic block as their 9327d523365SDimitry Andric // statepoint. It would be too expensive to preserve validation info through 9337d523365SDimitry Andric // different basic blocks. 9343ca95b02SDimitry Andric if (Relocate.getStatepoint()->getParent() == Relocate.getParent()) 9354d0b32cdSDimitry Andric StatepointLowering.relocCallVisited(Relocate); 9363ca95b02SDimitry Andric 9373ca95b02SDimitry Andric auto *Ty = Relocate.getType()->getScalarType(); 9383ca95b02SDimitry Andric if (auto IsManaged = GFI->getStrategy().isGCManagedPointer(Ty)) 9393ca95b02SDimitry Andric assert(*IsManaged && "Non gc managed pointer relocated!"); 94039d628a0SDimitry Andric #endif 94139d628a0SDimitry Andric 9424d0b32cdSDimitry Andric const Value *DerivedPtr = Relocate.getDerivedPtr(); 943ff0cc061SDimitry Andric SDValue SD = getValue(DerivedPtr); 94439d628a0SDimitry Andric 9453ca95b02SDimitry Andric auto &SpillMap = FuncInfo.StatepointSpillMaps[Relocate.getStatepoint()]; 9463ca95b02SDimitry Andric auto SlotIt = SpillMap.find(DerivedPtr); 9473ca95b02SDimitry Andric assert(SlotIt != SpillMap.end() && "Relocating not lowered gc value"); 9483ca95b02SDimitry Andric Optional<int> DerivedPtrLocation = SlotIt->second; 949ff0cc061SDimitry Andric 95039d628a0SDimitry Andric // We didn't need to spill these special cases (constants and allocas). 95139d628a0SDimitry Andric // See the handling in spillIncomingValueForStatepoint for detail. 952ff0cc061SDimitry Andric if (!DerivedPtrLocation) { 9534d0b32cdSDimitry Andric setValue(&Relocate, SD); 95439d628a0SDimitry Andric return; 95539d628a0SDimitry Andric } 95639d628a0SDimitry Andric 957f37b6182SDimitry Andric SDValue SpillSlot = 958f37b6182SDimitry Andric DAG.getTargetFrameIndex(*DerivedPtrLocation, getFrameIndexTy()); 95939d628a0SDimitry Andric 96039d628a0SDimitry Andric // Be conservative: flush all pending loads 96139d628a0SDimitry Andric // TODO: Probably we can be less restrictive on this, 9627d523365SDimitry Andric // it may allow more scheduling opportunities. 96339d628a0SDimitry Andric SDValue Chain = getRoot(); 96439d628a0SDimitry Andric 965ff0cc061SDimitry Andric SDValue SpillLoad = 966f37b6182SDimitry Andric DAG.getLoad(DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 967f37b6182SDimitry Andric Relocate.getType()), 968f37b6182SDimitry Andric getCurSDLoc(), Chain, SpillSlot, 9697d523365SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), 9703ca95b02SDimitry Andric *DerivedPtrLocation)); 97139d628a0SDimitry Andric 97239d628a0SDimitry Andric // Again, be conservative, don't emit pending loads 973ff0cc061SDimitry Andric DAG.setRoot(SpillLoad.getValue(1)); 97439d628a0SDimitry Andric 975ff0cc061SDimitry Andric assert(SpillLoad.getNode()); 9764d0b32cdSDimitry Andric setValue(&Relocate, SpillLoad); 97739d628a0SDimitry Andric } 9783ca95b02SDimitry Andric 9793ca95b02SDimitry Andric void SelectionDAGBuilder::LowerDeoptimizeCall(const CallInst *CI) { 9803ca95b02SDimitry Andric const auto &TLI = DAG.getTargetLoweringInfo(); 9813ca95b02SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::DEOPTIMIZE), 9823ca95b02SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 9833ca95b02SDimitry Andric 9843ca95b02SDimitry Andric // We don't lower calls to __llvm_deoptimize as varargs, but as a regular 9853ca95b02SDimitry Andric // call. We also do not lower the return value to any virtual register, and 9863ca95b02SDimitry Andric // change the immediately following return to a trap instruction. 9873ca95b02SDimitry Andric LowerCallSiteWithDeoptBundleImpl(CI, Callee, /* EHPadBB = */ nullptr, 9883ca95b02SDimitry Andric /* VarArgDisallowed = */ true, 9893ca95b02SDimitry Andric /* ForceVoidReturnTy = */ true); 9903ca95b02SDimitry Andric } 9913ca95b02SDimitry Andric 9923ca95b02SDimitry Andric void SelectionDAGBuilder::LowerDeoptimizingReturn() { 9933ca95b02SDimitry Andric // We do not lower the return value from llvm.deoptimize to any virtual 9943ca95b02SDimitry Andric // register, and change the immediately following return to a trap 9953ca95b02SDimitry Andric // instruction. 9963ca95b02SDimitry Andric if (DAG.getTarget().Options.TrapUnreachable) 9973ca95b02SDimitry Andric DAG.setRoot( 9983ca95b02SDimitry Andric DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot())); 9993ca95b02SDimitry Andric } 1000