1*0b57cec5SDimitry Andric //===- StatepointLowering.cpp - SDAGBuilder's statepoint code -------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file includes support code use by SelectionDAGBuilder when lowering a 10*0b57cec5SDimitry Andric // statepoint sequence in SelectionDAG IR. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "StatepointLowering.h" 15*0b57cec5SDimitry Andric #include "SelectionDAGBuilder.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/None.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/FunctionLoweringInfo.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/GCMetadata.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/GCStrategy.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 28*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 29*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 30*0b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h" 31*0b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 32*0b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 33*0b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h" 34*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 35*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 38*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 39*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 40*0b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 41*0b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h" 42*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 43*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 44*0b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h" 45*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 46*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 47*0b57cec5SDimitry Andric #include <cassert> 48*0b57cec5SDimitry Andric #include <cstddef> 49*0b57cec5SDimitry Andric #include <cstdint> 50*0b57cec5SDimitry Andric #include <iterator> 51*0b57cec5SDimitry Andric #include <tuple> 52*0b57cec5SDimitry Andric #include <utility> 53*0b57cec5SDimitry Andric 54*0b57cec5SDimitry Andric using namespace llvm; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric #define DEBUG_TYPE "statepoint-lowering" 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric STATISTIC(NumSlotsAllocatedForStatepoints, 59*0b57cec5SDimitry Andric "Number of stack slots allocated for statepoints"); 60*0b57cec5SDimitry Andric STATISTIC(NumOfStatepoints, "Number of statepoint nodes encountered"); 61*0b57cec5SDimitry Andric STATISTIC(StatepointMaxSlotsRequired, 62*0b57cec5SDimitry Andric "Maximum number of stack slots required for a singe statepoint"); 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric static void pushStackMapConstant(SmallVectorImpl<SDValue>& Ops, 65*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder, uint64_t Value) { 66*0b57cec5SDimitry Andric SDLoc L = Builder.getCurSDLoc(); 67*0b57cec5SDimitry Andric Ops.push_back(Builder.DAG.getTargetConstant(StackMaps::ConstantOp, L, 68*0b57cec5SDimitry Andric MVT::i64)); 69*0b57cec5SDimitry Andric Ops.push_back(Builder.DAG.getTargetConstant(Value, L, MVT::i64)); 70*0b57cec5SDimitry Andric } 71*0b57cec5SDimitry Andric 72*0b57cec5SDimitry Andric void StatepointLoweringState::startNewStatepoint(SelectionDAGBuilder &Builder) { 73*0b57cec5SDimitry Andric // Consistency check 74*0b57cec5SDimitry Andric assert(PendingGCRelocateCalls.empty() && 75*0b57cec5SDimitry Andric "Trying to visit statepoint before finished processing previous one"); 76*0b57cec5SDimitry Andric Locations.clear(); 77*0b57cec5SDimitry Andric NextSlotToAllocate = 0; 78*0b57cec5SDimitry Andric // Need to resize this on each safepoint - we need the two to stay in sync and 79*0b57cec5SDimitry Andric // the clear patterns of a SelectionDAGBuilder have no relation to 80*0b57cec5SDimitry Andric // FunctionLoweringInfo. Also need to ensure used bits get cleared. 81*0b57cec5SDimitry Andric AllocatedStackSlots.clear(); 82*0b57cec5SDimitry Andric AllocatedStackSlots.resize(Builder.FuncInfo.StatepointStackSlots.size()); 83*0b57cec5SDimitry Andric } 84*0b57cec5SDimitry Andric 85*0b57cec5SDimitry Andric void StatepointLoweringState::clear() { 86*0b57cec5SDimitry Andric Locations.clear(); 87*0b57cec5SDimitry Andric AllocatedStackSlots.clear(); 88*0b57cec5SDimitry Andric assert(PendingGCRelocateCalls.empty() && 89*0b57cec5SDimitry Andric "cleared before statepoint sequence completed"); 90*0b57cec5SDimitry Andric } 91*0b57cec5SDimitry Andric 92*0b57cec5SDimitry Andric SDValue 93*0b57cec5SDimitry Andric StatepointLoweringState::allocateStackSlot(EVT ValueType, 94*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder) { 95*0b57cec5SDimitry Andric NumSlotsAllocatedForStatepoints++; 96*0b57cec5SDimitry Andric MachineFrameInfo &MFI = Builder.DAG.getMachineFunction().getFrameInfo(); 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric unsigned SpillSize = ValueType.getStoreSize(); 99*0b57cec5SDimitry Andric assert((SpillSize * 8) == ValueType.getSizeInBits() && "Size not in bytes?"); 100*0b57cec5SDimitry Andric 101*0b57cec5SDimitry Andric // First look for a previously created stack slot which is not in 102*0b57cec5SDimitry Andric // use (accounting for the fact arbitrary slots may already be 103*0b57cec5SDimitry Andric // reserved), or to create a new stack slot and use it. 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric const size_t NumSlots = AllocatedStackSlots.size(); 106*0b57cec5SDimitry Andric assert(NextSlotToAllocate <= NumSlots && "Broken invariant"); 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric assert(AllocatedStackSlots.size() == 109*0b57cec5SDimitry Andric Builder.FuncInfo.StatepointStackSlots.size() && 110*0b57cec5SDimitry Andric "Broken invariant"); 111*0b57cec5SDimitry Andric 112*0b57cec5SDimitry Andric for (; NextSlotToAllocate < NumSlots; NextSlotToAllocate++) { 113*0b57cec5SDimitry Andric if (!AllocatedStackSlots.test(NextSlotToAllocate)) { 114*0b57cec5SDimitry Andric const int FI = Builder.FuncInfo.StatepointStackSlots[NextSlotToAllocate]; 115*0b57cec5SDimitry Andric if (MFI.getObjectSize(FI) == SpillSize) { 116*0b57cec5SDimitry Andric AllocatedStackSlots.set(NextSlotToAllocate); 117*0b57cec5SDimitry Andric // TODO: Is ValueType the right thing to use here? 118*0b57cec5SDimitry Andric return Builder.DAG.getFrameIndex(FI, ValueType); 119*0b57cec5SDimitry Andric } 120*0b57cec5SDimitry Andric } 121*0b57cec5SDimitry Andric } 122*0b57cec5SDimitry Andric 123*0b57cec5SDimitry Andric // Couldn't find a free slot, so create a new one: 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric SDValue SpillSlot = Builder.DAG.CreateStackTemporary(ValueType); 126*0b57cec5SDimitry Andric const unsigned FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); 127*0b57cec5SDimitry Andric MFI.markAsStatepointSpillSlotObjectIndex(FI); 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric Builder.FuncInfo.StatepointStackSlots.push_back(FI); 130*0b57cec5SDimitry Andric AllocatedStackSlots.resize(AllocatedStackSlots.size()+1, true); 131*0b57cec5SDimitry Andric assert(AllocatedStackSlots.size() == 132*0b57cec5SDimitry Andric Builder.FuncInfo.StatepointStackSlots.size() && 133*0b57cec5SDimitry Andric "Broken invariant"); 134*0b57cec5SDimitry Andric 135*0b57cec5SDimitry Andric StatepointMaxSlotsRequired.updateMax( 136*0b57cec5SDimitry Andric Builder.FuncInfo.StatepointStackSlots.size()); 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric return SpillSlot; 139*0b57cec5SDimitry Andric } 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric /// Utility function for reservePreviousStackSlotForValue. Tries to find 142*0b57cec5SDimitry Andric /// stack slot index to which we have spilled value for previous statepoints. 143*0b57cec5SDimitry Andric /// LookUpDepth specifies maximum DFS depth this function is allowed to look. 144*0b57cec5SDimitry Andric static Optional<int> findPreviousSpillSlot(const Value *Val, 145*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder, 146*0b57cec5SDimitry Andric int LookUpDepth) { 147*0b57cec5SDimitry Andric // Can not look any further - give up now 148*0b57cec5SDimitry Andric if (LookUpDepth <= 0) 149*0b57cec5SDimitry Andric return None; 150*0b57cec5SDimitry Andric 151*0b57cec5SDimitry Andric // Spill location is known for gc relocates 152*0b57cec5SDimitry Andric if (const auto *Relocate = dyn_cast<GCRelocateInst>(Val)) { 153*0b57cec5SDimitry Andric const auto &SpillMap = 154*0b57cec5SDimitry Andric Builder.FuncInfo.StatepointSpillMaps[Relocate->getStatepoint()]; 155*0b57cec5SDimitry Andric 156*0b57cec5SDimitry Andric auto It = SpillMap.find(Relocate->getDerivedPtr()); 157*0b57cec5SDimitry Andric if (It == SpillMap.end()) 158*0b57cec5SDimitry Andric return None; 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric return It->second; 161*0b57cec5SDimitry Andric } 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric // Look through bitcast instructions. 164*0b57cec5SDimitry Andric if (const BitCastInst *Cast = dyn_cast<BitCastInst>(Val)) 165*0b57cec5SDimitry Andric return findPreviousSpillSlot(Cast->getOperand(0), Builder, LookUpDepth - 1); 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric // Look through phi nodes 168*0b57cec5SDimitry Andric // All incoming values should have same known stack slot, otherwise result 169*0b57cec5SDimitry Andric // is unknown. 170*0b57cec5SDimitry Andric if (const PHINode *Phi = dyn_cast<PHINode>(Val)) { 171*0b57cec5SDimitry Andric Optional<int> MergedResult = None; 172*0b57cec5SDimitry Andric 173*0b57cec5SDimitry Andric for (auto &IncomingValue : Phi->incoming_values()) { 174*0b57cec5SDimitry Andric Optional<int> SpillSlot = 175*0b57cec5SDimitry Andric findPreviousSpillSlot(IncomingValue, Builder, LookUpDepth - 1); 176*0b57cec5SDimitry Andric if (!SpillSlot.hasValue()) 177*0b57cec5SDimitry Andric return None; 178*0b57cec5SDimitry Andric 179*0b57cec5SDimitry Andric if (MergedResult.hasValue() && *MergedResult != *SpillSlot) 180*0b57cec5SDimitry Andric return None; 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric MergedResult = SpillSlot; 183*0b57cec5SDimitry Andric } 184*0b57cec5SDimitry Andric return MergedResult; 185*0b57cec5SDimitry Andric } 186*0b57cec5SDimitry Andric 187*0b57cec5SDimitry Andric // TODO: We can do better for PHI nodes. In cases like this: 188*0b57cec5SDimitry Andric // ptr = phi(relocated_pointer, not_relocated_pointer) 189*0b57cec5SDimitry Andric // statepoint(ptr) 190*0b57cec5SDimitry Andric // We will return that stack slot for ptr is unknown. And later we might 191*0b57cec5SDimitry Andric // assign different stack slots for ptr and relocated_pointer. This limits 192*0b57cec5SDimitry Andric // llvm's ability to remove redundant stores. 193*0b57cec5SDimitry Andric // Unfortunately it's hard to accomplish in current infrastructure. 194*0b57cec5SDimitry Andric // We use this function to eliminate spill store completely, while 195*0b57cec5SDimitry Andric // in example we still need to emit store, but instead of any location 196*0b57cec5SDimitry Andric // we need to use special "preferred" location. 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric // TODO: handle simple updates. If a value is modified and the original 199*0b57cec5SDimitry Andric // value is no longer live, it would be nice to put the modified value in the 200*0b57cec5SDimitry Andric // same slot. This allows folding of the memory accesses for some 201*0b57cec5SDimitry Andric // instructions types (like an increment). 202*0b57cec5SDimitry Andric // statepoint (i) 203*0b57cec5SDimitry Andric // i1 = i+1 204*0b57cec5SDimitry Andric // statepoint (i1) 205*0b57cec5SDimitry Andric // However we need to be careful for cases like this: 206*0b57cec5SDimitry Andric // statepoint(i) 207*0b57cec5SDimitry Andric // i1 = i+1 208*0b57cec5SDimitry Andric // statepoint(i, i1) 209*0b57cec5SDimitry Andric // Here we want to reserve spill slot for 'i', but not for 'i+1'. If we just 210*0b57cec5SDimitry Andric // put handling of simple modifications in this function like it's done 211*0b57cec5SDimitry Andric // for bitcasts we might end up reserving i's slot for 'i+1' because order in 212*0b57cec5SDimitry Andric // which we visit values is unspecified. 213*0b57cec5SDimitry Andric 214*0b57cec5SDimitry Andric // Don't know any information about this instruction 215*0b57cec5SDimitry Andric return None; 216*0b57cec5SDimitry Andric } 217*0b57cec5SDimitry Andric 218*0b57cec5SDimitry Andric /// Try to find existing copies of the incoming values in stack slots used for 219*0b57cec5SDimitry Andric /// statepoint spilling. If we can find a spill slot for the incoming value, 220*0b57cec5SDimitry Andric /// mark that slot as allocated, and reuse the same slot for this safepoint. 221*0b57cec5SDimitry Andric /// This helps to avoid series of loads and stores that only serve to reshuffle 222*0b57cec5SDimitry Andric /// values on the stack between calls. 223*0b57cec5SDimitry Andric static void reservePreviousStackSlotForValue(const Value *IncomingValue, 224*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder) { 225*0b57cec5SDimitry Andric SDValue Incoming = Builder.getValue(IncomingValue); 226*0b57cec5SDimitry Andric 227*0b57cec5SDimitry Andric if (isa<ConstantSDNode>(Incoming) || isa<FrameIndexSDNode>(Incoming)) { 228*0b57cec5SDimitry Andric // We won't need to spill this, so no need to check for previously 229*0b57cec5SDimitry Andric // allocated stack slots 230*0b57cec5SDimitry Andric return; 231*0b57cec5SDimitry Andric } 232*0b57cec5SDimitry Andric 233*0b57cec5SDimitry Andric SDValue OldLocation = Builder.StatepointLowering.getLocation(Incoming); 234*0b57cec5SDimitry Andric if (OldLocation.getNode()) 235*0b57cec5SDimitry Andric // Duplicates in input 236*0b57cec5SDimitry Andric return; 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric const int LookUpDepth = 6; 239*0b57cec5SDimitry Andric Optional<int> Index = 240*0b57cec5SDimitry Andric findPreviousSpillSlot(IncomingValue, Builder, LookUpDepth); 241*0b57cec5SDimitry Andric if (!Index.hasValue()) 242*0b57cec5SDimitry Andric return; 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric const auto &StatepointSlots = Builder.FuncInfo.StatepointStackSlots; 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric auto SlotIt = find(StatepointSlots, *Index); 247*0b57cec5SDimitry Andric assert(SlotIt != StatepointSlots.end() && 248*0b57cec5SDimitry Andric "Value spilled to the unknown stack slot"); 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric // This is one of our dedicated lowering slots 251*0b57cec5SDimitry Andric const int Offset = std::distance(StatepointSlots.begin(), SlotIt); 252*0b57cec5SDimitry Andric if (Builder.StatepointLowering.isStackSlotAllocated(Offset)) { 253*0b57cec5SDimitry Andric // stack slot already assigned to someone else, can't use it! 254*0b57cec5SDimitry Andric // TODO: currently we reserve space for gc arguments after doing 255*0b57cec5SDimitry Andric // normal allocation for deopt arguments. We should reserve for 256*0b57cec5SDimitry Andric // _all_ deopt and gc arguments, then start allocating. This 257*0b57cec5SDimitry Andric // will prevent some moves being inserted when vm state changes, 258*0b57cec5SDimitry Andric // but gc state doesn't between two calls. 259*0b57cec5SDimitry Andric return; 260*0b57cec5SDimitry Andric } 261*0b57cec5SDimitry Andric // Reserve this stack slot 262*0b57cec5SDimitry Andric Builder.StatepointLowering.reserveStackSlot(Offset); 263*0b57cec5SDimitry Andric 264*0b57cec5SDimitry Andric // Cache this slot so we find it when going through the normal 265*0b57cec5SDimitry Andric // assignment loop. 266*0b57cec5SDimitry Andric SDValue Loc = 267*0b57cec5SDimitry Andric Builder.DAG.getTargetFrameIndex(*Index, Builder.getFrameIndexTy()); 268*0b57cec5SDimitry Andric Builder.StatepointLowering.setLocation(Incoming, Loc); 269*0b57cec5SDimitry Andric } 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric /// Remove any duplicate (as SDValues) from the derived pointer pairs. This 272*0b57cec5SDimitry Andric /// is not required for correctness. It's purpose is to reduce the size of 273*0b57cec5SDimitry Andric /// StackMap section. It has no effect on the number of spill slots required 274*0b57cec5SDimitry Andric /// or the actual lowering. 275*0b57cec5SDimitry Andric static void 276*0b57cec5SDimitry Andric removeDuplicateGCPtrs(SmallVectorImpl<const Value *> &Bases, 277*0b57cec5SDimitry Andric SmallVectorImpl<const Value *> &Ptrs, 278*0b57cec5SDimitry Andric SmallVectorImpl<const GCRelocateInst *> &Relocs, 279*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder, 280*0b57cec5SDimitry Andric FunctionLoweringInfo::StatepointSpillMap &SSM) { 281*0b57cec5SDimitry Andric DenseMap<SDValue, const Value *> Seen; 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric SmallVector<const Value *, 64> NewBases, NewPtrs; 284*0b57cec5SDimitry Andric SmallVector<const GCRelocateInst *, 64> NewRelocs; 285*0b57cec5SDimitry Andric for (size_t i = 0, e = Ptrs.size(); i < e; i++) { 286*0b57cec5SDimitry Andric SDValue SD = Builder.getValue(Ptrs[i]); 287*0b57cec5SDimitry Andric auto SeenIt = Seen.find(SD); 288*0b57cec5SDimitry Andric 289*0b57cec5SDimitry Andric if (SeenIt == Seen.end()) { 290*0b57cec5SDimitry Andric // Only add non-duplicates 291*0b57cec5SDimitry Andric NewBases.push_back(Bases[i]); 292*0b57cec5SDimitry Andric NewPtrs.push_back(Ptrs[i]); 293*0b57cec5SDimitry Andric NewRelocs.push_back(Relocs[i]); 294*0b57cec5SDimitry Andric Seen[SD] = Ptrs[i]; 295*0b57cec5SDimitry Andric } else { 296*0b57cec5SDimitry Andric // Duplicate pointer found, note in SSM and move on: 297*0b57cec5SDimitry Andric SSM.DuplicateMap[Ptrs[i]] = SeenIt->second; 298*0b57cec5SDimitry Andric } 299*0b57cec5SDimitry Andric } 300*0b57cec5SDimitry Andric assert(Bases.size() >= NewBases.size()); 301*0b57cec5SDimitry Andric assert(Ptrs.size() >= NewPtrs.size()); 302*0b57cec5SDimitry Andric assert(Relocs.size() >= NewRelocs.size()); 303*0b57cec5SDimitry Andric Bases = NewBases; 304*0b57cec5SDimitry Andric Ptrs = NewPtrs; 305*0b57cec5SDimitry Andric Relocs = NewRelocs; 306*0b57cec5SDimitry Andric assert(Ptrs.size() == Bases.size()); 307*0b57cec5SDimitry Andric assert(Ptrs.size() == Relocs.size()); 308*0b57cec5SDimitry Andric } 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric /// Extract call from statepoint, lower it and return pointer to the 311*0b57cec5SDimitry Andric /// call node. Also update NodeMap so that getValue(statepoint) will 312*0b57cec5SDimitry Andric /// reference lowered call result 313*0b57cec5SDimitry Andric static std::pair<SDValue, SDNode *> lowerCallFromStatepointLoweringInfo( 314*0b57cec5SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI, 315*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder, SmallVectorImpl<SDValue> &PendingExports) { 316*0b57cec5SDimitry Andric SDValue ReturnValue, CallEndVal; 317*0b57cec5SDimitry Andric std::tie(ReturnValue, CallEndVal) = 318*0b57cec5SDimitry Andric Builder.lowerInvokable(SI.CLI, SI.EHPadBB); 319*0b57cec5SDimitry Andric SDNode *CallEnd = CallEndVal.getNode(); 320*0b57cec5SDimitry Andric 321*0b57cec5SDimitry Andric // Get a call instruction from the call sequence chain. Tail calls are not 322*0b57cec5SDimitry Andric // allowed. The following code is essentially reverse engineering X86's 323*0b57cec5SDimitry Andric // LowerCallTo. 324*0b57cec5SDimitry Andric // 325*0b57cec5SDimitry Andric // We are expecting DAG to have the following form: 326*0b57cec5SDimitry Andric // 327*0b57cec5SDimitry Andric // ch = eh_label (only in case of invoke statepoint) 328*0b57cec5SDimitry Andric // ch, glue = callseq_start ch 329*0b57cec5SDimitry Andric // ch, glue = X86::Call ch, glue 330*0b57cec5SDimitry Andric // ch, glue = callseq_end ch, glue 331*0b57cec5SDimitry Andric // get_return_value ch, glue 332*0b57cec5SDimitry Andric // 333*0b57cec5SDimitry Andric // get_return_value can either be a sequence of CopyFromReg instructions 334*0b57cec5SDimitry Andric // to grab the return value from the return register(s), or it can be a LOAD 335*0b57cec5SDimitry Andric // to load a value returned by reference via a stack slot. 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric bool HasDef = !SI.CLI.RetTy->isVoidTy(); 338*0b57cec5SDimitry Andric if (HasDef) { 339*0b57cec5SDimitry Andric if (CallEnd->getOpcode() == ISD::LOAD) 340*0b57cec5SDimitry Andric CallEnd = CallEnd->getOperand(0).getNode(); 341*0b57cec5SDimitry Andric else 342*0b57cec5SDimitry Andric while (CallEnd->getOpcode() == ISD::CopyFromReg) 343*0b57cec5SDimitry Andric CallEnd = CallEnd->getOperand(0).getNode(); 344*0b57cec5SDimitry Andric } 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric assert(CallEnd->getOpcode() == ISD::CALLSEQ_END && "expected!"); 347*0b57cec5SDimitry Andric return std::make_pair(ReturnValue, CallEnd->getOperand(0).getNode()); 348*0b57cec5SDimitry Andric } 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric static MachineMemOperand* getMachineMemOperand(MachineFunction &MF, 351*0b57cec5SDimitry Andric FrameIndexSDNode &FI) { 352*0b57cec5SDimitry Andric auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FI.getIndex()); 353*0b57cec5SDimitry Andric auto MMOFlags = MachineMemOperand::MOStore | 354*0b57cec5SDimitry Andric MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile; 355*0b57cec5SDimitry Andric auto &MFI = MF.getFrameInfo(); 356*0b57cec5SDimitry Andric return MF.getMachineMemOperand(PtrInfo, MMOFlags, 357*0b57cec5SDimitry Andric MFI.getObjectSize(FI.getIndex()), 358*0b57cec5SDimitry Andric MFI.getObjectAlignment(FI.getIndex())); 359*0b57cec5SDimitry Andric } 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric /// Spill a value incoming to the statepoint. It might be either part of 362*0b57cec5SDimitry Andric /// vmstate 363*0b57cec5SDimitry Andric /// or gcstate. In both cases unconditionally spill it on the stack unless it 364*0b57cec5SDimitry Andric /// is a null constant. Return pair with first element being frame index 365*0b57cec5SDimitry Andric /// containing saved value and second element with outgoing chain from the 366*0b57cec5SDimitry Andric /// emitted store 367*0b57cec5SDimitry Andric static std::tuple<SDValue, SDValue, MachineMemOperand*> 368*0b57cec5SDimitry Andric spillIncomingStatepointValue(SDValue Incoming, SDValue Chain, 369*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder) { 370*0b57cec5SDimitry Andric SDValue Loc = Builder.StatepointLowering.getLocation(Incoming); 371*0b57cec5SDimitry Andric MachineMemOperand* MMO = nullptr; 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric // Emit new store if we didn't do it for this ptr before 374*0b57cec5SDimitry Andric if (!Loc.getNode()) { 375*0b57cec5SDimitry Andric Loc = Builder.StatepointLowering.allocateStackSlot(Incoming.getValueType(), 376*0b57cec5SDimitry Andric Builder); 377*0b57cec5SDimitry Andric int Index = cast<FrameIndexSDNode>(Loc)->getIndex(); 378*0b57cec5SDimitry Andric // We use TargetFrameIndex so that isel will not select it into LEA 379*0b57cec5SDimitry Andric Loc = Builder.DAG.getTargetFrameIndex(Index, Builder.getFrameIndexTy()); 380*0b57cec5SDimitry Andric 381*0b57cec5SDimitry Andric #ifndef NDEBUG 382*0b57cec5SDimitry Andric // Right now we always allocate spill slots that are of the same 383*0b57cec5SDimitry Andric // size as the value we're about to spill (the size of spillee can 384*0b57cec5SDimitry Andric // vary since we spill vectors of pointers too). At some point we 385*0b57cec5SDimitry Andric // can consider allowing spills of smaller values to larger slots 386*0b57cec5SDimitry Andric // (i.e. change the '==' in the assert below to a '>='). 387*0b57cec5SDimitry Andric MachineFrameInfo &MFI = Builder.DAG.getMachineFunction().getFrameInfo(); 388*0b57cec5SDimitry Andric assert((MFI.getObjectSize(Index) * 8) == Incoming.getValueSizeInBits() && 389*0b57cec5SDimitry Andric "Bad spill: stack slot does not match!"); 390*0b57cec5SDimitry Andric #endif 391*0b57cec5SDimitry Andric 392*0b57cec5SDimitry Andric auto &MF = Builder.DAG.getMachineFunction(); 393*0b57cec5SDimitry Andric auto PtrInfo = MachinePointerInfo::getFixedStack(MF, Index); 394*0b57cec5SDimitry Andric Chain = Builder.DAG.getStore(Chain, Builder.getCurSDLoc(), Incoming, Loc, 395*0b57cec5SDimitry Andric PtrInfo); 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric MMO = getMachineMemOperand(MF, *cast<FrameIndexSDNode>(Loc)); 398*0b57cec5SDimitry Andric 399*0b57cec5SDimitry Andric Builder.StatepointLowering.setLocation(Incoming, Loc); 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric assert(Loc.getNode()); 403*0b57cec5SDimitry Andric return std::make_tuple(Loc, Chain, MMO); 404*0b57cec5SDimitry Andric } 405*0b57cec5SDimitry Andric 406*0b57cec5SDimitry Andric /// Lower a single value incoming to a statepoint node. This value can be 407*0b57cec5SDimitry Andric /// either a deopt value or a gc value, the handling is the same. We special 408*0b57cec5SDimitry Andric /// case constants and allocas, then fall back to spilling if required. 409*0b57cec5SDimitry Andric static void lowerIncomingStatepointValue(SDValue Incoming, bool LiveInOnly, 410*0b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Ops, 411*0b57cec5SDimitry Andric SmallVectorImpl<MachineMemOperand*> &MemRefs, 412*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder) { 413*0b57cec5SDimitry Andric // Note: We know all of these spills are independent, but don't bother to 414*0b57cec5SDimitry Andric // exploit that chain wise. DAGCombine will happily do so as needed, so 415*0b57cec5SDimitry Andric // doing it here would be a small compile time win at most. 416*0b57cec5SDimitry Andric SDValue Chain = Builder.getRoot(); 417*0b57cec5SDimitry Andric 418*0b57cec5SDimitry Andric if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Incoming)) { 419*0b57cec5SDimitry Andric // If the original value was a constant, make sure it gets recorded as 420*0b57cec5SDimitry Andric // such in the stackmap. This is required so that the consumer can 421*0b57cec5SDimitry Andric // parse any internal format to the deopt state. It also handles null 422*0b57cec5SDimitry Andric // pointers and other constant pointers in GC states. Note the constant 423*0b57cec5SDimitry Andric // vectors do not appear to actually hit this path and that anything larger 424*0b57cec5SDimitry Andric // than an i64 value (not type!) will fail asserts here. 425*0b57cec5SDimitry Andric pushStackMapConstant(Ops, Builder, C->getSExtValue()); 426*0b57cec5SDimitry Andric } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { 427*0b57cec5SDimitry Andric // This handles allocas as arguments to the statepoint (this is only 428*0b57cec5SDimitry Andric // really meaningful for a deopt value. For GC, we'd be trying to 429*0b57cec5SDimitry Andric // relocate the address of the alloca itself?) 430*0b57cec5SDimitry Andric assert(Incoming.getValueType() == Builder.getFrameIndexTy() && 431*0b57cec5SDimitry Andric "Incoming value is a frame index!"); 432*0b57cec5SDimitry Andric Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), 433*0b57cec5SDimitry Andric Builder.getFrameIndexTy())); 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric auto &MF = Builder.DAG.getMachineFunction(); 436*0b57cec5SDimitry Andric auto *MMO = getMachineMemOperand(MF, *FI); 437*0b57cec5SDimitry Andric MemRefs.push_back(MMO); 438*0b57cec5SDimitry Andric 439*0b57cec5SDimitry Andric } else if (LiveInOnly) { 440*0b57cec5SDimitry Andric // If this value is live in (not live-on-return, or live-through), we can 441*0b57cec5SDimitry Andric // treat it the same way patchpoint treats it's "live in" values. We'll 442*0b57cec5SDimitry Andric // end up folding some of these into stack references, but they'll be 443*0b57cec5SDimitry Andric // handled by the register allocator. Note that we do not have the notion 444*0b57cec5SDimitry Andric // of a late use so these values might be placed in registers which are 445*0b57cec5SDimitry Andric // clobbered by the call. This is fine for live-in. 446*0b57cec5SDimitry Andric Ops.push_back(Incoming); 447*0b57cec5SDimitry Andric } else { 448*0b57cec5SDimitry Andric // Otherwise, locate a spill slot and explicitly spill it so it 449*0b57cec5SDimitry Andric // can be found by the runtime later. We currently do not support 450*0b57cec5SDimitry Andric // tracking values through callee saved registers to their eventual 451*0b57cec5SDimitry Andric // spill location. This would be a useful optimization, but would 452*0b57cec5SDimitry Andric // need to be optional since it requires a lot of complexity on the 453*0b57cec5SDimitry Andric // runtime side which not all would support. 454*0b57cec5SDimitry Andric auto Res = spillIncomingStatepointValue(Incoming, Chain, Builder); 455*0b57cec5SDimitry Andric Ops.push_back(std::get<0>(Res)); 456*0b57cec5SDimitry Andric if (auto *MMO = std::get<2>(Res)) 457*0b57cec5SDimitry Andric MemRefs.push_back(MMO); 458*0b57cec5SDimitry Andric Chain = std::get<1>(Res);; 459*0b57cec5SDimitry Andric } 460*0b57cec5SDimitry Andric 461*0b57cec5SDimitry Andric Builder.DAG.setRoot(Chain); 462*0b57cec5SDimitry Andric } 463*0b57cec5SDimitry Andric 464*0b57cec5SDimitry Andric /// Lower deopt state and gc pointer arguments of the statepoint. The actual 465*0b57cec5SDimitry Andric /// lowering is described in lowerIncomingStatepointValue. This function is 466*0b57cec5SDimitry Andric /// responsible for lowering everything in the right position and playing some 467*0b57cec5SDimitry Andric /// tricks to avoid redundant stack manipulation where possible. On 468*0b57cec5SDimitry Andric /// completion, 'Ops' will contain ready to use operands for machine code 469*0b57cec5SDimitry Andric /// statepoint. The chain nodes will have already been created and the DAG root 470*0b57cec5SDimitry Andric /// will be set to the last value spilled (if any were). 471*0b57cec5SDimitry Andric static void 472*0b57cec5SDimitry Andric lowerStatepointMetaArgs(SmallVectorImpl<SDValue> &Ops, 473*0b57cec5SDimitry Andric SmallVectorImpl<MachineMemOperand*> &MemRefs, SelectionDAGBuilder::StatepointLoweringInfo &SI, 474*0b57cec5SDimitry Andric SelectionDAGBuilder &Builder) { 475*0b57cec5SDimitry Andric // Lower the deopt and gc arguments for this statepoint. Layout will be: 476*0b57cec5SDimitry Andric // deopt argument length, deopt arguments.., gc arguments... 477*0b57cec5SDimitry Andric #ifndef NDEBUG 478*0b57cec5SDimitry Andric if (auto *GFI = Builder.GFI) { 479*0b57cec5SDimitry Andric // Check that each of the gc pointer and bases we've gotten out of the 480*0b57cec5SDimitry Andric // safepoint is something the strategy thinks might be a pointer (or vector 481*0b57cec5SDimitry Andric // of pointers) into the GC heap. This is basically just here to help catch 482*0b57cec5SDimitry Andric // errors during statepoint insertion. TODO: This should actually be in the 483*0b57cec5SDimitry Andric // Verifier, but we can't get to the GCStrategy from there (yet). 484*0b57cec5SDimitry Andric GCStrategy &S = GFI->getStrategy(); 485*0b57cec5SDimitry Andric for (const Value *V : SI.Bases) { 486*0b57cec5SDimitry Andric auto Opt = S.isGCManagedPointer(V->getType()->getScalarType()); 487*0b57cec5SDimitry Andric if (Opt.hasValue()) { 488*0b57cec5SDimitry Andric assert(Opt.getValue() && 489*0b57cec5SDimitry Andric "non gc managed base pointer found in statepoint"); 490*0b57cec5SDimitry Andric } 491*0b57cec5SDimitry Andric } 492*0b57cec5SDimitry Andric for (const Value *V : SI.Ptrs) { 493*0b57cec5SDimitry Andric auto Opt = S.isGCManagedPointer(V->getType()->getScalarType()); 494*0b57cec5SDimitry Andric if (Opt.hasValue()) { 495*0b57cec5SDimitry Andric assert(Opt.getValue() && 496*0b57cec5SDimitry Andric "non gc managed derived pointer found in statepoint"); 497*0b57cec5SDimitry Andric } 498*0b57cec5SDimitry Andric } 499*0b57cec5SDimitry Andric assert(SI.Bases.size() == SI.Ptrs.size() && "Pointer without base!"); 500*0b57cec5SDimitry Andric } else { 501*0b57cec5SDimitry Andric assert(SI.Bases.empty() && "No gc specified, so cannot relocate pointers!"); 502*0b57cec5SDimitry Andric assert(SI.Ptrs.empty() && "No gc specified, so cannot relocate pointers!"); 503*0b57cec5SDimitry Andric } 504*0b57cec5SDimitry Andric #endif 505*0b57cec5SDimitry Andric 506*0b57cec5SDimitry Andric // Figure out what lowering strategy we're going to use for each part 507*0b57cec5SDimitry Andric // Note: Is is conservatively correct to lower both "live-in" and "live-out" 508*0b57cec5SDimitry Andric // as "live-through". A "live-through" variable is one which is "live-in", 509*0b57cec5SDimitry Andric // "live-out", and live throughout the lifetime of the call (i.e. we can find 510*0b57cec5SDimitry Andric // it from any PC within the transitive callee of the statepoint). In 511*0b57cec5SDimitry Andric // particular, if the callee spills callee preserved registers we may not 512*0b57cec5SDimitry Andric // be able to find a value placed in that register during the call. This is 513*0b57cec5SDimitry Andric // fine for live-out, but not for live-through. If we were willing to make 514*0b57cec5SDimitry Andric // assumptions about the code generator producing the callee, we could 515*0b57cec5SDimitry Andric // potentially allow live-through values in callee saved registers. 516*0b57cec5SDimitry Andric const bool LiveInDeopt = 517*0b57cec5SDimitry Andric SI.StatepointFlags & (uint64_t)StatepointFlags::DeoptLiveIn; 518*0b57cec5SDimitry Andric 519*0b57cec5SDimitry Andric auto isGCValue =[&](const Value *V) { 520*0b57cec5SDimitry Andric return is_contained(SI.Ptrs, V) || is_contained(SI.Bases, V); 521*0b57cec5SDimitry Andric }; 522*0b57cec5SDimitry Andric 523*0b57cec5SDimitry Andric // Before we actually start lowering (and allocating spill slots for values), 524*0b57cec5SDimitry Andric // reserve any stack slots which we judge to be profitable to reuse for a 525*0b57cec5SDimitry Andric // particular value. This is purely an optimization over the code below and 526*0b57cec5SDimitry Andric // doesn't change semantics at all. It is important for performance that we 527*0b57cec5SDimitry Andric // reserve slots for both deopt and gc values before lowering either. 528*0b57cec5SDimitry Andric for (const Value *V : SI.DeoptState) { 529*0b57cec5SDimitry Andric if (!LiveInDeopt || isGCValue(V)) 530*0b57cec5SDimitry Andric reservePreviousStackSlotForValue(V, Builder); 531*0b57cec5SDimitry Andric } 532*0b57cec5SDimitry Andric for (unsigned i = 0; i < SI.Bases.size(); ++i) { 533*0b57cec5SDimitry Andric reservePreviousStackSlotForValue(SI.Bases[i], Builder); 534*0b57cec5SDimitry Andric reservePreviousStackSlotForValue(SI.Ptrs[i], Builder); 535*0b57cec5SDimitry Andric } 536*0b57cec5SDimitry Andric 537*0b57cec5SDimitry Andric // First, prefix the list with the number of unique values to be 538*0b57cec5SDimitry Andric // lowered. Note that this is the number of *Values* not the 539*0b57cec5SDimitry Andric // number of SDValues required to lower them. 540*0b57cec5SDimitry Andric const int NumVMSArgs = SI.DeoptState.size(); 541*0b57cec5SDimitry Andric pushStackMapConstant(Ops, Builder, NumVMSArgs); 542*0b57cec5SDimitry Andric 543*0b57cec5SDimitry Andric // The vm state arguments are lowered in an opaque manner. We do not know 544*0b57cec5SDimitry Andric // what type of values are contained within. 545*0b57cec5SDimitry Andric for (const Value *V : SI.DeoptState) { 546*0b57cec5SDimitry Andric SDValue Incoming; 547*0b57cec5SDimitry Andric // If this is a function argument at a static frame index, generate it as 548*0b57cec5SDimitry Andric // the frame index. 549*0b57cec5SDimitry Andric if (const Argument *Arg = dyn_cast<Argument>(V)) { 550*0b57cec5SDimitry Andric int FI = Builder.FuncInfo.getArgumentFrameIndex(Arg); 551*0b57cec5SDimitry Andric if (FI != INT_MAX) 552*0b57cec5SDimitry Andric Incoming = Builder.DAG.getFrameIndex(FI, Builder.getFrameIndexTy()); 553*0b57cec5SDimitry Andric } 554*0b57cec5SDimitry Andric if (!Incoming.getNode()) 555*0b57cec5SDimitry Andric Incoming = Builder.getValue(V); 556*0b57cec5SDimitry Andric const bool LiveInValue = LiveInDeopt && !isGCValue(V); 557*0b57cec5SDimitry Andric lowerIncomingStatepointValue(Incoming, LiveInValue, Ops, MemRefs, Builder); 558*0b57cec5SDimitry Andric } 559*0b57cec5SDimitry Andric 560*0b57cec5SDimitry Andric // Finally, go ahead and lower all the gc arguments. There's no prefixed 561*0b57cec5SDimitry Andric // length for this one. After lowering, we'll have the base and pointer 562*0b57cec5SDimitry Andric // arrays interwoven with each (lowered) base pointer immediately followed by 563*0b57cec5SDimitry Andric // it's (lowered) derived pointer. i.e 564*0b57cec5SDimitry Andric // (base[0], ptr[0], base[1], ptr[1], ...) 565*0b57cec5SDimitry Andric for (unsigned i = 0; i < SI.Bases.size(); ++i) { 566*0b57cec5SDimitry Andric const Value *Base = SI.Bases[i]; 567*0b57cec5SDimitry Andric lowerIncomingStatepointValue(Builder.getValue(Base), /*LiveInOnly*/ false, 568*0b57cec5SDimitry Andric Ops, MemRefs, Builder); 569*0b57cec5SDimitry Andric 570*0b57cec5SDimitry Andric const Value *Ptr = SI.Ptrs[i]; 571*0b57cec5SDimitry Andric lowerIncomingStatepointValue(Builder.getValue(Ptr), /*LiveInOnly*/ false, 572*0b57cec5SDimitry Andric Ops, MemRefs, Builder); 573*0b57cec5SDimitry Andric } 574*0b57cec5SDimitry Andric 575*0b57cec5SDimitry Andric // If there are any explicit spill slots passed to the statepoint, record 576*0b57cec5SDimitry Andric // them, but otherwise do not do anything special. These are user provided 577*0b57cec5SDimitry Andric // allocas and give control over placement to the consumer. In this case, 578*0b57cec5SDimitry Andric // it is the contents of the slot which may get updated, not the pointer to 579*0b57cec5SDimitry Andric // the alloca 580*0b57cec5SDimitry Andric for (Value *V : SI.GCArgs) { 581*0b57cec5SDimitry Andric SDValue Incoming = Builder.getValue(V); 582*0b57cec5SDimitry Andric if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { 583*0b57cec5SDimitry Andric // This handles allocas as arguments to the statepoint 584*0b57cec5SDimitry Andric assert(Incoming.getValueType() == Builder.getFrameIndexTy() && 585*0b57cec5SDimitry Andric "Incoming value is a frame index!"); 586*0b57cec5SDimitry Andric Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), 587*0b57cec5SDimitry Andric Builder.getFrameIndexTy())); 588*0b57cec5SDimitry Andric 589*0b57cec5SDimitry Andric auto &MF = Builder.DAG.getMachineFunction(); 590*0b57cec5SDimitry Andric auto *MMO = getMachineMemOperand(MF, *FI); 591*0b57cec5SDimitry Andric MemRefs.push_back(MMO); 592*0b57cec5SDimitry Andric } 593*0b57cec5SDimitry Andric } 594*0b57cec5SDimitry Andric 595*0b57cec5SDimitry Andric // Record computed locations for all lowered values. 596*0b57cec5SDimitry Andric // This can not be embedded in lowering loops as we need to record *all* 597*0b57cec5SDimitry Andric // values, while previous loops account only values with unique SDValues. 598*0b57cec5SDimitry Andric const Instruction *StatepointInstr = SI.StatepointInstr; 599*0b57cec5SDimitry Andric auto &SpillMap = Builder.FuncInfo.StatepointSpillMaps[StatepointInstr]; 600*0b57cec5SDimitry Andric 601*0b57cec5SDimitry Andric for (const GCRelocateInst *Relocate : SI.GCRelocates) { 602*0b57cec5SDimitry Andric const Value *V = Relocate->getDerivedPtr(); 603*0b57cec5SDimitry Andric SDValue SDV = Builder.getValue(V); 604*0b57cec5SDimitry Andric SDValue Loc = Builder.StatepointLowering.getLocation(SDV); 605*0b57cec5SDimitry Andric 606*0b57cec5SDimitry Andric if (Loc.getNode()) { 607*0b57cec5SDimitry Andric SpillMap.SlotMap[V] = cast<FrameIndexSDNode>(Loc)->getIndex(); 608*0b57cec5SDimitry Andric } else { 609*0b57cec5SDimitry Andric // Record value as visited, but not spilled. This is case for allocas 610*0b57cec5SDimitry Andric // and constants. For this values we can avoid emitting spill load while 611*0b57cec5SDimitry Andric // visiting corresponding gc_relocate. 612*0b57cec5SDimitry Andric // Actually we do not need to record them in this map at all. 613*0b57cec5SDimitry Andric // We do this only to check that we are not relocating any unvisited 614*0b57cec5SDimitry Andric // value. 615*0b57cec5SDimitry Andric SpillMap.SlotMap[V] = None; 616*0b57cec5SDimitry Andric 617*0b57cec5SDimitry Andric // Default llvm mechanisms for exporting values which are used in 618*0b57cec5SDimitry Andric // different basic blocks does not work for gc relocates. 619*0b57cec5SDimitry Andric // Note that it would be incorrect to teach llvm that all relocates are 620*0b57cec5SDimitry Andric // uses of the corresponding values so that it would automatically 621*0b57cec5SDimitry Andric // export them. Relocates of the spilled values does not use original 622*0b57cec5SDimitry Andric // value. 623*0b57cec5SDimitry Andric if (Relocate->getParent() != StatepointInstr->getParent()) 624*0b57cec5SDimitry Andric Builder.ExportFromCurrentBlock(V); 625*0b57cec5SDimitry Andric } 626*0b57cec5SDimitry Andric } 627*0b57cec5SDimitry Andric } 628*0b57cec5SDimitry Andric 629*0b57cec5SDimitry Andric SDValue SelectionDAGBuilder::LowerAsSTATEPOINT( 630*0b57cec5SDimitry Andric SelectionDAGBuilder::StatepointLoweringInfo &SI) { 631*0b57cec5SDimitry Andric // The basic scheme here is that information about both the original call and 632*0b57cec5SDimitry Andric // the safepoint is encoded in the CallInst. We create a temporary call and 633*0b57cec5SDimitry Andric // lower it, then reverse engineer the calling sequence. 634*0b57cec5SDimitry Andric 635*0b57cec5SDimitry Andric NumOfStatepoints++; 636*0b57cec5SDimitry Andric // Clear state 637*0b57cec5SDimitry Andric StatepointLowering.startNewStatepoint(*this); 638*0b57cec5SDimitry Andric 639*0b57cec5SDimitry Andric #ifndef NDEBUG 640*0b57cec5SDimitry Andric // We schedule gc relocates before removeDuplicateGCPtrs since we _will_ 641*0b57cec5SDimitry Andric // encounter the duplicate gc relocates we elide in removeDuplicateGCPtrs. 642*0b57cec5SDimitry Andric for (auto *Reloc : SI.GCRelocates) 643*0b57cec5SDimitry Andric if (Reloc->getParent() == SI.StatepointInstr->getParent()) 644*0b57cec5SDimitry Andric StatepointLowering.scheduleRelocCall(*Reloc); 645*0b57cec5SDimitry Andric #endif 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric // Remove any redundant llvm::Values which map to the same SDValue as another 648*0b57cec5SDimitry Andric // input. Also has the effect of removing duplicates in the original 649*0b57cec5SDimitry Andric // llvm::Value input list as well. This is a useful optimization for 650*0b57cec5SDimitry Andric // reducing the size of the StackMap section. It has no other impact. 651*0b57cec5SDimitry Andric removeDuplicateGCPtrs(SI.Bases, SI.Ptrs, SI.GCRelocates, *this, 652*0b57cec5SDimitry Andric FuncInfo.StatepointSpillMaps[SI.StatepointInstr]); 653*0b57cec5SDimitry Andric assert(SI.Bases.size() == SI.Ptrs.size() && 654*0b57cec5SDimitry Andric SI.Ptrs.size() == SI.GCRelocates.size()); 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric // Lower statepoint vmstate and gcstate arguments 657*0b57cec5SDimitry Andric SmallVector<SDValue, 10> LoweredMetaArgs; 658*0b57cec5SDimitry Andric SmallVector<MachineMemOperand*, 16> MemRefs; 659*0b57cec5SDimitry Andric lowerStatepointMetaArgs(LoweredMetaArgs, MemRefs, SI, *this); 660*0b57cec5SDimitry Andric 661*0b57cec5SDimitry Andric // Now that we've emitted the spills, we need to update the root so that the 662*0b57cec5SDimitry Andric // call sequence is ordered correctly. 663*0b57cec5SDimitry Andric SI.CLI.setChain(getRoot()); 664*0b57cec5SDimitry Andric 665*0b57cec5SDimitry Andric // Get call node, we will replace it later with statepoint 666*0b57cec5SDimitry Andric SDValue ReturnVal; 667*0b57cec5SDimitry Andric SDNode *CallNode; 668*0b57cec5SDimitry Andric std::tie(ReturnVal, CallNode) = 669*0b57cec5SDimitry Andric lowerCallFromStatepointLoweringInfo(SI, *this, PendingExports); 670*0b57cec5SDimitry Andric 671*0b57cec5SDimitry Andric // Construct the actual GC_TRANSITION_START, STATEPOINT, and GC_TRANSITION_END 672*0b57cec5SDimitry Andric // nodes with all the appropriate arguments and return values. 673*0b57cec5SDimitry Andric 674*0b57cec5SDimitry Andric // Call Node: Chain, Target, {Args}, RegMask, [Glue] 675*0b57cec5SDimitry Andric SDValue Chain = CallNode->getOperand(0); 676*0b57cec5SDimitry Andric 677*0b57cec5SDimitry Andric SDValue Glue; 678*0b57cec5SDimitry Andric bool CallHasIncomingGlue = CallNode->getGluedNode(); 679*0b57cec5SDimitry Andric if (CallHasIncomingGlue) { 680*0b57cec5SDimitry Andric // Glue is always last operand 681*0b57cec5SDimitry Andric Glue = CallNode->getOperand(CallNode->getNumOperands() - 1); 682*0b57cec5SDimitry Andric } 683*0b57cec5SDimitry Andric 684*0b57cec5SDimitry Andric // Build the GC_TRANSITION_START node if necessary. 685*0b57cec5SDimitry Andric // 686*0b57cec5SDimitry Andric // The operands to the GC_TRANSITION_{START,END} nodes are laid out in the 687*0b57cec5SDimitry Andric // order in which they appear in the call to the statepoint intrinsic. If 688*0b57cec5SDimitry Andric // any of the operands is a pointer-typed, that operand is immediately 689*0b57cec5SDimitry Andric // followed by a SRCVALUE for the pointer that may be used during lowering 690*0b57cec5SDimitry Andric // (e.g. to form MachinePointerInfo values for loads/stores). 691*0b57cec5SDimitry Andric const bool IsGCTransition = 692*0b57cec5SDimitry Andric (SI.StatepointFlags & (uint64_t)StatepointFlags::GCTransition) == 693*0b57cec5SDimitry Andric (uint64_t)StatepointFlags::GCTransition; 694*0b57cec5SDimitry Andric if (IsGCTransition) { 695*0b57cec5SDimitry Andric SmallVector<SDValue, 8> TSOps; 696*0b57cec5SDimitry Andric 697*0b57cec5SDimitry Andric // Add chain 698*0b57cec5SDimitry Andric TSOps.push_back(Chain); 699*0b57cec5SDimitry Andric 700*0b57cec5SDimitry Andric // Add GC transition arguments 701*0b57cec5SDimitry Andric for (const Value *V : SI.GCTransitionArgs) { 702*0b57cec5SDimitry Andric TSOps.push_back(getValue(V)); 703*0b57cec5SDimitry Andric if (V->getType()->isPointerTy()) 704*0b57cec5SDimitry Andric TSOps.push_back(DAG.getSrcValue(V)); 705*0b57cec5SDimitry Andric } 706*0b57cec5SDimitry Andric 707*0b57cec5SDimitry Andric // Add glue if necessary 708*0b57cec5SDimitry Andric if (CallHasIncomingGlue) 709*0b57cec5SDimitry Andric TSOps.push_back(Glue); 710*0b57cec5SDimitry Andric 711*0b57cec5SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 712*0b57cec5SDimitry Andric 713*0b57cec5SDimitry Andric SDValue GCTransitionStart = 714*0b57cec5SDimitry Andric DAG.getNode(ISD::GC_TRANSITION_START, getCurSDLoc(), NodeTys, TSOps); 715*0b57cec5SDimitry Andric 716*0b57cec5SDimitry Andric Chain = GCTransitionStart.getValue(0); 717*0b57cec5SDimitry Andric Glue = GCTransitionStart.getValue(1); 718*0b57cec5SDimitry Andric } 719*0b57cec5SDimitry Andric 720*0b57cec5SDimitry Andric // TODO: Currently, all of these operands are being marked as read/write in 721*0b57cec5SDimitry Andric // PrologEpilougeInserter.cpp, we should special case the VMState arguments 722*0b57cec5SDimitry Andric // and flags to be read-only. 723*0b57cec5SDimitry Andric SmallVector<SDValue, 40> Ops; 724*0b57cec5SDimitry Andric 725*0b57cec5SDimitry Andric // Add the <id> and <numBytes> constants. 726*0b57cec5SDimitry Andric Ops.push_back(DAG.getTargetConstant(SI.ID, getCurSDLoc(), MVT::i64)); 727*0b57cec5SDimitry Andric Ops.push_back( 728*0b57cec5SDimitry Andric DAG.getTargetConstant(SI.NumPatchBytes, getCurSDLoc(), MVT::i32)); 729*0b57cec5SDimitry Andric 730*0b57cec5SDimitry Andric // Calculate and push starting position of vmstate arguments 731*0b57cec5SDimitry Andric // Get number of arguments incoming directly into call node 732*0b57cec5SDimitry Andric unsigned NumCallRegArgs = 733*0b57cec5SDimitry Andric CallNode->getNumOperands() - (CallHasIncomingGlue ? 4 : 3); 734*0b57cec5SDimitry Andric Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, getCurSDLoc(), MVT::i32)); 735*0b57cec5SDimitry Andric 736*0b57cec5SDimitry Andric // Add call target 737*0b57cec5SDimitry Andric SDValue CallTarget = SDValue(CallNode->getOperand(1).getNode(), 0); 738*0b57cec5SDimitry Andric Ops.push_back(CallTarget); 739*0b57cec5SDimitry Andric 740*0b57cec5SDimitry Andric // Add call arguments 741*0b57cec5SDimitry Andric // Get position of register mask in the call 742*0b57cec5SDimitry Andric SDNode::op_iterator RegMaskIt; 743*0b57cec5SDimitry Andric if (CallHasIncomingGlue) 744*0b57cec5SDimitry Andric RegMaskIt = CallNode->op_end() - 2; 745*0b57cec5SDimitry Andric else 746*0b57cec5SDimitry Andric RegMaskIt = CallNode->op_end() - 1; 747*0b57cec5SDimitry Andric Ops.insert(Ops.end(), CallNode->op_begin() + 2, RegMaskIt); 748*0b57cec5SDimitry Andric 749*0b57cec5SDimitry Andric // Add a constant argument for the calling convention 750*0b57cec5SDimitry Andric pushStackMapConstant(Ops, *this, SI.CLI.CallConv); 751*0b57cec5SDimitry Andric 752*0b57cec5SDimitry Andric // Add a constant argument for the flags 753*0b57cec5SDimitry Andric uint64_t Flags = SI.StatepointFlags; 754*0b57cec5SDimitry Andric assert(((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0) && 755*0b57cec5SDimitry Andric "Unknown flag used"); 756*0b57cec5SDimitry Andric pushStackMapConstant(Ops, *this, Flags); 757*0b57cec5SDimitry Andric 758*0b57cec5SDimitry Andric // Insert all vmstate and gcstate arguments 759*0b57cec5SDimitry Andric Ops.insert(Ops.end(), LoweredMetaArgs.begin(), LoweredMetaArgs.end()); 760*0b57cec5SDimitry Andric 761*0b57cec5SDimitry Andric // Add register mask from call node 762*0b57cec5SDimitry Andric Ops.push_back(*RegMaskIt); 763*0b57cec5SDimitry Andric 764*0b57cec5SDimitry Andric // Add chain 765*0b57cec5SDimitry Andric Ops.push_back(Chain); 766*0b57cec5SDimitry Andric 767*0b57cec5SDimitry Andric // Same for the glue, but we add it only if original call had it 768*0b57cec5SDimitry Andric if (Glue.getNode()) 769*0b57cec5SDimitry Andric Ops.push_back(Glue); 770*0b57cec5SDimitry Andric 771*0b57cec5SDimitry Andric // Compute return values. Provide a glue output since we consume one as 772*0b57cec5SDimitry Andric // input. This allows someone else to chain off us as needed. 773*0b57cec5SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric MachineSDNode *StatepointMCNode = 776*0b57cec5SDimitry Andric DAG.getMachineNode(TargetOpcode::STATEPOINT, getCurSDLoc(), NodeTys, Ops); 777*0b57cec5SDimitry Andric DAG.setNodeMemRefs(StatepointMCNode, MemRefs); 778*0b57cec5SDimitry Andric 779*0b57cec5SDimitry Andric SDNode *SinkNode = StatepointMCNode; 780*0b57cec5SDimitry Andric 781*0b57cec5SDimitry Andric // Build the GC_TRANSITION_END node if necessary. 782*0b57cec5SDimitry Andric // 783*0b57cec5SDimitry Andric // See the comment above regarding GC_TRANSITION_START for the layout of 784*0b57cec5SDimitry Andric // the operands to the GC_TRANSITION_END node. 785*0b57cec5SDimitry Andric if (IsGCTransition) { 786*0b57cec5SDimitry Andric SmallVector<SDValue, 8> TEOps; 787*0b57cec5SDimitry Andric 788*0b57cec5SDimitry Andric // Add chain 789*0b57cec5SDimitry Andric TEOps.push_back(SDValue(StatepointMCNode, 0)); 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric // Add GC transition arguments 792*0b57cec5SDimitry Andric for (const Value *V : SI.GCTransitionArgs) { 793*0b57cec5SDimitry Andric TEOps.push_back(getValue(V)); 794*0b57cec5SDimitry Andric if (V->getType()->isPointerTy()) 795*0b57cec5SDimitry Andric TEOps.push_back(DAG.getSrcValue(V)); 796*0b57cec5SDimitry Andric } 797*0b57cec5SDimitry Andric 798*0b57cec5SDimitry Andric // Add glue 799*0b57cec5SDimitry Andric TEOps.push_back(SDValue(StatepointMCNode, 1)); 800*0b57cec5SDimitry Andric 801*0b57cec5SDimitry Andric SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 802*0b57cec5SDimitry Andric 803*0b57cec5SDimitry Andric SDValue GCTransitionStart = 804*0b57cec5SDimitry Andric DAG.getNode(ISD::GC_TRANSITION_END, getCurSDLoc(), NodeTys, TEOps); 805*0b57cec5SDimitry Andric 806*0b57cec5SDimitry Andric SinkNode = GCTransitionStart.getNode(); 807*0b57cec5SDimitry Andric } 808*0b57cec5SDimitry Andric 809*0b57cec5SDimitry Andric // Replace original call 810*0b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(CallNode, SinkNode); // This may update Root 811*0b57cec5SDimitry Andric // Remove original call node 812*0b57cec5SDimitry Andric DAG.DeleteNode(CallNode); 813*0b57cec5SDimitry Andric 814*0b57cec5SDimitry Andric // DON'T set the root - under the assumption that it's already set past the 815*0b57cec5SDimitry Andric // inserted node we created. 816*0b57cec5SDimitry Andric 817*0b57cec5SDimitry Andric // TODO: A better future implementation would be to emit a single variable 818*0b57cec5SDimitry Andric // argument, variable return value STATEPOINT node here and then hookup the 819*0b57cec5SDimitry Andric // return value of each gc.relocate to the respective output of the 820*0b57cec5SDimitry Andric // previously emitted STATEPOINT value. Unfortunately, this doesn't appear 821*0b57cec5SDimitry Andric // to actually be possible today. 822*0b57cec5SDimitry Andric 823*0b57cec5SDimitry Andric return ReturnVal; 824*0b57cec5SDimitry Andric } 825*0b57cec5SDimitry Andric 826*0b57cec5SDimitry Andric void 827*0b57cec5SDimitry Andric SelectionDAGBuilder::LowerStatepoint(ImmutableStatepoint ISP, 828*0b57cec5SDimitry Andric const BasicBlock *EHPadBB /*= nullptr*/) { 829*0b57cec5SDimitry Andric assert(ISP.getCall()->getCallingConv() != CallingConv::AnyReg && 830*0b57cec5SDimitry Andric "anyregcc is not supported on statepoints!"); 831*0b57cec5SDimitry Andric 832*0b57cec5SDimitry Andric #ifndef NDEBUG 833*0b57cec5SDimitry Andric // If this is a malformed statepoint, report it early to simplify debugging. 834*0b57cec5SDimitry Andric // This should catch any IR level mistake that's made when constructing or 835*0b57cec5SDimitry Andric // transforming statepoints. 836*0b57cec5SDimitry Andric ISP.verify(); 837*0b57cec5SDimitry Andric 838*0b57cec5SDimitry Andric // Check that the associated GCStrategy expects to encounter statepoints. 839*0b57cec5SDimitry Andric assert(GFI->getStrategy().useStatepoints() && 840*0b57cec5SDimitry Andric "GCStrategy does not expect to encounter statepoints"); 841*0b57cec5SDimitry Andric #endif 842*0b57cec5SDimitry Andric 843*0b57cec5SDimitry Andric SDValue ActualCallee; 844*0b57cec5SDimitry Andric 845*0b57cec5SDimitry Andric if (ISP.getNumPatchBytes() > 0) { 846*0b57cec5SDimitry Andric // If we've been asked to emit a nop sequence instead of a call instruction 847*0b57cec5SDimitry Andric // for this statepoint then don't lower the call target, but use a constant 848*0b57cec5SDimitry Andric // `null` instead. Not lowering the call target lets statepoint clients get 849*0b57cec5SDimitry Andric // away without providing a physical address for the symbolic call target at 850*0b57cec5SDimitry Andric // link time. 851*0b57cec5SDimitry Andric 852*0b57cec5SDimitry Andric const auto &TLI = DAG.getTargetLoweringInfo(); 853*0b57cec5SDimitry Andric const auto &DL = DAG.getDataLayout(); 854*0b57cec5SDimitry Andric 855*0b57cec5SDimitry Andric unsigned AS = ISP.getCalledValue()->getType()->getPointerAddressSpace(); 856*0b57cec5SDimitry Andric ActualCallee = DAG.getConstant(0, getCurSDLoc(), TLI.getPointerTy(DL, AS)); 857*0b57cec5SDimitry Andric } else { 858*0b57cec5SDimitry Andric ActualCallee = getValue(ISP.getCalledValue()); 859*0b57cec5SDimitry Andric } 860*0b57cec5SDimitry Andric 861*0b57cec5SDimitry Andric StatepointLoweringInfo SI(DAG); 862*0b57cec5SDimitry Andric populateCallLoweringInfo(SI.CLI, ISP.getCall(), 863*0b57cec5SDimitry Andric ImmutableStatepoint::CallArgsBeginPos, 864*0b57cec5SDimitry Andric ISP.getNumCallArgs(), ActualCallee, 865*0b57cec5SDimitry Andric ISP.getActualReturnType(), false /* IsPatchPoint */); 866*0b57cec5SDimitry Andric 867*0b57cec5SDimitry Andric for (const GCRelocateInst *Relocate : ISP.getRelocates()) { 868*0b57cec5SDimitry Andric SI.GCRelocates.push_back(Relocate); 869*0b57cec5SDimitry Andric SI.Bases.push_back(Relocate->getBasePtr()); 870*0b57cec5SDimitry Andric SI.Ptrs.push_back(Relocate->getDerivedPtr()); 871*0b57cec5SDimitry Andric } 872*0b57cec5SDimitry Andric 873*0b57cec5SDimitry Andric SI.GCArgs = ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end()); 874*0b57cec5SDimitry Andric SI.StatepointInstr = ISP.getInstruction(); 875*0b57cec5SDimitry Andric SI.GCTransitionArgs = 876*0b57cec5SDimitry Andric ArrayRef<const Use>(ISP.gc_args_begin(), ISP.gc_args_end()); 877*0b57cec5SDimitry Andric SI.ID = ISP.getID(); 878*0b57cec5SDimitry Andric SI.DeoptState = ArrayRef<const Use>(ISP.deopt_begin(), ISP.deopt_end()); 879*0b57cec5SDimitry Andric SI.StatepointFlags = ISP.getFlags(); 880*0b57cec5SDimitry Andric SI.NumPatchBytes = ISP.getNumPatchBytes(); 881*0b57cec5SDimitry Andric SI.EHPadBB = EHPadBB; 882*0b57cec5SDimitry Andric 883*0b57cec5SDimitry Andric SDValue ReturnValue = LowerAsSTATEPOINT(SI); 884*0b57cec5SDimitry Andric 885*0b57cec5SDimitry Andric // Export the result value if needed 886*0b57cec5SDimitry Andric const GCResultInst *GCResult = ISP.getGCResult(); 887*0b57cec5SDimitry Andric Type *RetTy = ISP.getActualReturnType(); 888*0b57cec5SDimitry Andric if (!RetTy->isVoidTy() && GCResult) { 889*0b57cec5SDimitry Andric if (GCResult->getParent() != ISP.getCall()->getParent()) { 890*0b57cec5SDimitry Andric // Result value will be used in a different basic block so we need to 891*0b57cec5SDimitry Andric // export it now. Default exporting mechanism will not work here because 892*0b57cec5SDimitry Andric // statepoint call has a different type than the actual call. It means 893*0b57cec5SDimitry Andric // that by default llvm will create export register of the wrong type 894*0b57cec5SDimitry Andric // (always i32 in our case). So instead we need to create export register 895*0b57cec5SDimitry Andric // with correct type manually. 896*0b57cec5SDimitry Andric // TODO: To eliminate this problem we can remove gc.result intrinsics 897*0b57cec5SDimitry Andric // completely and make statepoint call to return a tuple. 898*0b57cec5SDimitry Andric unsigned Reg = FuncInfo.CreateRegs(RetTy); 899*0b57cec5SDimitry Andric RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(), 900*0b57cec5SDimitry Andric DAG.getDataLayout(), Reg, RetTy, 901*0b57cec5SDimitry Andric ISP.getCall()->getCallingConv()); 902*0b57cec5SDimitry Andric SDValue Chain = DAG.getEntryNode(); 903*0b57cec5SDimitry Andric 904*0b57cec5SDimitry Andric RFV.getCopyToRegs(ReturnValue, DAG, getCurSDLoc(), Chain, nullptr); 905*0b57cec5SDimitry Andric PendingExports.push_back(Chain); 906*0b57cec5SDimitry Andric FuncInfo.ValueMap[ISP.getInstruction()] = Reg; 907*0b57cec5SDimitry Andric } else { 908*0b57cec5SDimitry Andric // Result value will be used in a same basic block. Don't export it or 909*0b57cec5SDimitry Andric // perform any explicit register copies. 910*0b57cec5SDimitry Andric // We'll replace the actuall call node shortly. gc_result will grab 911*0b57cec5SDimitry Andric // this value. 912*0b57cec5SDimitry Andric setValue(ISP.getInstruction(), ReturnValue); 913*0b57cec5SDimitry Andric } 914*0b57cec5SDimitry Andric } else { 915*0b57cec5SDimitry Andric // The token value is never used from here on, just generate a poison value 916*0b57cec5SDimitry Andric setValue(ISP.getInstruction(), DAG.getIntPtrConstant(-1, getCurSDLoc())); 917*0b57cec5SDimitry Andric } 918*0b57cec5SDimitry Andric } 919*0b57cec5SDimitry Andric 920*0b57cec5SDimitry Andric void SelectionDAGBuilder::LowerCallSiteWithDeoptBundleImpl( 921*0b57cec5SDimitry Andric const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB, 922*0b57cec5SDimitry Andric bool VarArgDisallowed, bool ForceVoidReturnTy) { 923*0b57cec5SDimitry Andric StatepointLoweringInfo SI(DAG); 924*0b57cec5SDimitry Andric unsigned ArgBeginIndex = Call->arg_begin() - Call->op_begin(); 925*0b57cec5SDimitry Andric populateCallLoweringInfo( 926*0b57cec5SDimitry Andric SI.CLI, Call, ArgBeginIndex, Call->getNumArgOperands(), Callee, 927*0b57cec5SDimitry Andric ForceVoidReturnTy ? Type::getVoidTy(*DAG.getContext()) : Call->getType(), 928*0b57cec5SDimitry Andric false); 929*0b57cec5SDimitry Andric if (!VarArgDisallowed) 930*0b57cec5SDimitry Andric SI.CLI.IsVarArg = Call->getFunctionType()->isVarArg(); 931*0b57cec5SDimitry Andric 932*0b57cec5SDimitry Andric auto DeoptBundle = *Call->getOperandBundle(LLVMContext::OB_deopt); 933*0b57cec5SDimitry Andric 934*0b57cec5SDimitry Andric unsigned DefaultID = StatepointDirectives::DeoptBundleStatepointID; 935*0b57cec5SDimitry Andric 936*0b57cec5SDimitry Andric auto SD = parseStatepointDirectivesFromAttrs(Call->getAttributes()); 937*0b57cec5SDimitry Andric SI.ID = SD.StatepointID.getValueOr(DefaultID); 938*0b57cec5SDimitry Andric SI.NumPatchBytes = SD.NumPatchBytes.getValueOr(0); 939*0b57cec5SDimitry Andric 940*0b57cec5SDimitry Andric SI.DeoptState = 941*0b57cec5SDimitry Andric ArrayRef<const Use>(DeoptBundle.Inputs.begin(), DeoptBundle.Inputs.end()); 942*0b57cec5SDimitry Andric SI.StatepointFlags = static_cast<uint64_t>(StatepointFlags::None); 943*0b57cec5SDimitry Andric SI.EHPadBB = EHPadBB; 944*0b57cec5SDimitry Andric 945*0b57cec5SDimitry Andric // NB! The GC arguments are deliberately left empty. 946*0b57cec5SDimitry Andric 947*0b57cec5SDimitry Andric if (SDValue ReturnVal = LowerAsSTATEPOINT(SI)) { 948*0b57cec5SDimitry Andric ReturnVal = lowerRangeToAssertZExt(DAG, *Call, ReturnVal); 949*0b57cec5SDimitry Andric setValue(Call, ReturnVal); 950*0b57cec5SDimitry Andric } 951*0b57cec5SDimitry Andric } 952*0b57cec5SDimitry Andric 953*0b57cec5SDimitry Andric void SelectionDAGBuilder::LowerCallSiteWithDeoptBundle( 954*0b57cec5SDimitry Andric const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB) { 955*0b57cec5SDimitry Andric LowerCallSiteWithDeoptBundleImpl(Call, Callee, EHPadBB, 956*0b57cec5SDimitry Andric /* VarArgDisallowed = */ false, 957*0b57cec5SDimitry Andric /* ForceVoidReturnTy = */ false); 958*0b57cec5SDimitry Andric } 959*0b57cec5SDimitry Andric 960*0b57cec5SDimitry Andric void SelectionDAGBuilder::visitGCResult(const GCResultInst &CI) { 961*0b57cec5SDimitry Andric // The result value of the gc_result is simply the result of the actual 962*0b57cec5SDimitry Andric // call. We've already emitted this, so just grab the value. 963*0b57cec5SDimitry Andric const Instruction *I = CI.getStatepoint(); 964*0b57cec5SDimitry Andric 965*0b57cec5SDimitry Andric if (I->getParent() != CI.getParent()) { 966*0b57cec5SDimitry Andric // Statepoint is in different basic block so we should have stored call 967*0b57cec5SDimitry Andric // result in a virtual register. 968*0b57cec5SDimitry Andric // We can not use default getValue() functionality to copy value from this 969*0b57cec5SDimitry Andric // register because statepoint and actual call return types can be 970*0b57cec5SDimitry Andric // different, and getValue() will use CopyFromReg of the wrong type, 971*0b57cec5SDimitry Andric // which is always i32 in our case. 972*0b57cec5SDimitry Andric PointerType *CalleeType = cast<PointerType>( 973*0b57cec5SDimitry Andric ImmutableStatepoint(I).getCalledValue()->getType()); 974*0b57cec5SDimitry Andric Type *RetTy = 975*0b57cec5SDimitry Andric cast<FunctionType>(CalleeType->getElementType())->getReturnType(); 976*0b57cec5SDimitry Andric SDValue CopyFromReg = getCopyFromRegs(I, RetTy); 977*0b57cec5SDimitry Andric 978*0b57cec5SDimitry Andric assert(CopyFromReg.getNode()); 979*0b57cec5SDimitry Andric setValue(&CI, CopyFromReg); 980*0b57cec5SDimitry Andric } else { 981*0b57cec5SDimitry Andric setValue(&CI, getValue(I)); 982*0b57cec5SDimitry Andric } 983*0b57cec5SDimitry Andric } 984*0b57cec5SDimitry Andric 985*0b57cec5SDimitry Andric void SelectionDAGBuilder::visitGCRelocate(const GCRelocateInst &Relocate) { 986*0b57cec5SDimitry Andric #ifndef NDEBUG 987*0b57cec5SDimitry Andric // Consistency check 988*0b57cec5SDimitry Andric // We skip this check for relocates not in the same basic block as their 989*0b57cec5SDimitry Andric // statepoint. It would be too expensive to preserve validation info through 990*0b57cec5SDimitry Andric // different basic blocks. 991*0b57cec5SDimitry Andric if (Relocate.getStatepoint()->getParent() == Relocate.getParent()) 992*0b57cec5SDimitry Andric StatepointLowering.relocCallVisited(Relocate); 993*0b57cec5SDimitry Andric 994*0b57cec5SDimitry Andric auto *Ty = Relocate.getType()->getScalarType(); 995*0b57cec5SDimitry Andric if (auto IsManaged = GFI->getStrategy().isGCManagedPointer(Ty)) 996*0b57cec5SDimitry Andric assert(*IsManaged && "Non gc managed pointer relocated!"); 997*0b57cec5SDimitry Andric #endif 998*0b57cec5SDimitry Andric 999*0b57cec5SDimitry Andric const Value *DerivedPtr = Relocate.getDerivedPtr(); 1000*0b57cec5SDimitry Andric SDValue SD = getValue(DerivedPtr); 1001*0b57cec5SDimitry Andric 1002*0b57cec5SDimitry Andric auto &SpillMap = FuncInfo.StatepointSpillMaps[Relocate.getStatepoint()]; 1003*0b57cec5SDimitry Andric auto SlotIt = SpillMap.find(DerivedPtr); 1004*0b57cec5SDimitry Andric assert(SlotIt != SpillMap.end() && "Relocating not lowered gc value"); 1005*0b57cec5SDimitry Andric Optional<int> DerivedPtrLocation = SlotIt->second; 1006*0b57cec5SDimitry Andric 1007*0b57cec5SDimitry Andric // We didn't need to spill these special cases (constants and allocas). 1008*0b57cec5SDimitry Andric // See the handling in spillIncomingValueForStatepoint for detail. 1009*0b57cec5SDimitry Andric if (!DerivedPtrLocation) { 1010*0b57cec5SDimitry Andric setValue(&Relocate, SD); 1011*0b57cec5SDimitry Andric return; 1012*0b57cec5SDimitry Andric } 1013*0b57cec5SDimitry Andric 1014*0b57cec5SDimitry Andric SDValue SpillSlot = 1015*0b57cec5SDimitry Andric DAG.getTargetFrameIndex(*DerivedPtrLocation, getFrameIndexTy()); 1016*0b57cec5SDimitry Andric 1017*0b57cec5SDimitry Andric // Note: We know all of these reloads are independent, but don't bother to 1018*0b57cec5SDimitry Andric // exploit that chain wise. DAGCombine will happily do so as needed, so 1019*0b57cec5SDimitry Andric // doing it here would be a small compile time win at most. 1020*0b57cec5SDimitry Andric SDValue Chain = getRoot(); 1021*0b57cec5SDimitry Andric 1022*0b57cec5SDimitry Andric SDValue SpillLoad = 1023*0b57cec5SDimitry Andric DAG.getLoad(DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 1024*0b57cec5SDimitry Andric Relocate.getType()), 1025*0b57cec5SDimitry Andric getCurSDLoc(), Chain, SpillSlot, 1026*0b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), 1027*0b57cec5SDimitry Andric *DerivedPtrLocation)); 1028*0b57cec5SDimitry Andric 1029*0b57cec5SDimitry Andric DAG.setRoot(SpillLoad.getValue(1)); 1030*0b57cec5SDimitry Andric 1031*0b57cec5SDimitry Andric assert(SpillLoad.getNode()); 1032*0b57cec5SDimitry Andric setValue(&Relocate, SpillLoad); 1033*0b57cec5SDimitry Andric } 1034*0b57cec5SDimitry Andric 1035*0b57cec5SDimitry Andric void SelectionDAGBuilder::LowerDeoptimizeCall(const CallInst *CI) { 1036*0b57cec5SDimitry Andric const auto &TLI = DAG.getTargetLoweringInfo(); 1037*0b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::DEOPTIMIZE), 1038*0b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 1039*0b57cec5SDimitry Andric 1040*0b57cec5SDimitry Andric // We don't lower calls to __llvm_deoptimize as varargs, but as a regular 1041*0b57cec5SDimitry Andric // call. We also do not lower the return value to any virtual register, and 1042*0b57cec5SDimitry Andric // change the immediately following return to a trap instruction. 1043*0b57cec5SDimitry Andric LowerCallSiteWithDeoptBundleImpl(CI, Callee, /* EHPadBB = */ nullptr, 1044*0b57cec5SDimitry Andric /* VarArgDisallowed = */ true, 1045*0b57cec5SDimitry Andric /* ForceVoidReturnTy = */ true); 1046*0b57cec5SDimitry Andric } 1047*0b57cec5SDimitry Andric 1048*0b57cec5SDimitry Andric void SelectionDAGBuilder::LowerDeoptimizingReturn() { 1049*0b57cec5SDimitry Andric // We do not lower the return value from llvm.deoptimize to any virtual 1050*0b57cec5SDimitry Andric // register, and change the immediately following return to a trap 1051*0b57cec5SDimitry Andric // instruction. 1052*0b57cec5SDimitry Andric if (DAG.getTarget().Options.TrapUnreachable) 1053*0b57cec5SDimitry Andric DAG.setRoot( 1054*0b57cec5SDimitry Andric DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot())); 1055*0b57cec5SDimitry Andric } 1056