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