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