Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 1 | //===-- StatepointLowering.cpp - SDAGBuilder's statepoint code -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file includes support code use by SelectionDAGBuilder when lowering a |
| 11 | // statepoint sequence in SelectionDAG IR. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "StatepointLowering.h" |
| 16 | #include "SelectionDAGBuilder.h" |
| 17 | #include "llvm/ADT/SmallSet.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Chandler Carruth | 71f308a | 2015-02-13 09:09:03 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/GCMetadata.h" |
Philip Reames | 56a0393 | 2015-01-26 18:26:35 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/GCStrategy.h" |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SelectionDAG.h" |
| 23 | #include "llvm/CodeGen/StackMaps.h" |
| 24 | #include "llvm/IR/CallingConv.h" |
| 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
| 28 | #include "llvm/IR/Statepoint.h" |
| 29 | #include "llvm/Target/TargetLowering.h" |
| 30 | #include <algorithm> |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "statepoint-lowering" |
| 34 | |
| 35 | STATISTIC(NumSlotsAllocatedForStatepoints, |
| 36 | "Number of stack slots allocated for statepoints"); |
| 37 | STATISTIC(NumOfStatepoints, "Number of statepoint nodes encountered"); |
| 38 | STATISTIC(StatepointMaxSlotsRequired, |
| 39 | "Maximum number of stack slots required for a singe statepoint"); |
| 40 | |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 41 | void StatepointLoweringState::startNewStatepoint(SelectionDAGBuilder &Builder) { |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 42 | // Consistency check |
| 43 | assert(PendingGCRelocateCalls.empty() && |
| 44 | "Trying to visit statepoint before finished processing previous one"); |
| 45 | Locations.clear(); |
| 46 | RelocLocations.clear(); |
| 47 | NextSlotToAllocate = 0; |
| 48 | // Need to resize this on each safepoint - we need the two to stay in |
| 49 | // sync and the clear patterns of a SelectionDAGBuilder have no relation |
| 50 | // to FunctionLoweringInfo. |
| 51 | AllocatedStackSlots.resize(Builder.FuncInfo.StatepointStackSlots.size()); |
| 52 | for (size_t i = 0; i < AllocatedStackSlots.size(); i++) { |
| 53 | AllocatedStackSlots[i] = false; |
| 54 | } |
| 55 | } |
| 56 | void StatepointLoweringState::clear() { |
| 57 | Locations.clear(); |
| 58 | RelocLocations.clear(); |
| 59 | AllocatedStackSlots.clear(); |
| 60 | assert(PendingGCRelocateCalls.empty() && |
| 61 | "cleared before statepoint sequence completed"); |
| 62 | } |
| 63 | |
| 64 | SDValue |
| 65 | StatepointLoweringState::allocateStackSlot(EVT ValueType, |
| 66 | SelectionDAGBuilder &Builder) { |
| 67 | |
| 68 | NumSlotsAllocatedForStatepoints++; |
| 69 | |
| 70 | // The basic scheme here is to first look for a previously created stack slot |
| 71 | // which is not in use (accounting for the fact arbitrary slots may already |
| 72 | // be reserved), or to create a new stack slot and use it. |
| 73 | |
| 74 | // If this doesn't succeed in 40000 iterations, something is seriously wrong |
| 75 | for (int i = 0; i < 40000; i++) { |
| 76 | assert(Builder.FuncInfo.StatepointStackSlots.size() == |
| 77 | AllocatedStackSlots.size() && |
| 78 | "broken invariant"); |
| 79 | const size_t NumSlots = AllocatedStackSlots.size(); |
| 80 | assert(NextSlotToAllocate <= NumSlots && "broken invariant"); |
| 81 | |
| 82 | if (NextSlotToAllocate >= NumSlots) { |
| 83 | assert(NextSlotToAllocate == NumSlots); |
| 84 | // record stats |
| 85 | if (NumSlots + 1 > StatepointMaxSlotsRequired) { |
| 86 | StatepointMaxSlotsRequired = NumSlots + 1; |
| 87 | } |
| 88 | |
| 89 | SDValue SpillSlot = Builder.DAG.CreateStackTemporary(ValueType); |
| 90 | const unsigned FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
| 91 | Builder.FuncInfo.StatepointStackSlots.push_back(FI); |
| 92 | AllocatedStackSlots.push_back(true); |
| 93 | return SpillSlot; |
| 94 | } |
| 95 | if (!AllocatedStackSlots[NextSlotToAllocate]) { |
| 96 | const int FI = Builder.FuncInfo.StatepointStackSlots[NextSlotToAllocate]; |
| 97 | AllocatedStackSlots[NextSlotToAllocate] = true; |
| 98 | return Builder.DAG.getFrameIndex(FI, ValueType); |
| 99 | } |
| 100 | // Note: We deliberately choose to advance this only on the failing path. |
| 101 | // Doing so on the suceeding path involes a bit of complexity that caused a |
| 102 | // minor bug previously. Unless performance shows this matters, please |
| 103 | // keep this code as simple as possible. |
| 104 | NextSlotToAllocate++; |
| 105 | } |
| 106 | llvm_unreachable("infinite loop?"); |
| 107 | } |
| 108 | |
| 109 | /// Try to find existing copies of the incoming values in stack slots used for |
| 110 | /// statepoint spilling. If we can find a spill slot for the incoming value, |
| 111 | /// mark that slot as allocated, and reuse the same slot for this safepoint. |
| 112 | /// This helps to avoid series of loads and stores that only serve to resuffle |
| 113 | /// values on the stack between calls. |
| 114 | static void reservePreviousStackSlotForValue(SDValue Incoming, |
| 115 | SelectionDAGBuilder &Builder) { |
| 116 | |
| 117 | if (isa<ConstantSDNode>(Incoming) || isa<FrameIndexSDNode>(Incoming)) { |
| 118 | // We won't need to spill this, so no need to check for previously |
| 119 | // allocated stack slots |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | SDValue Loc = Builder.StatepointLowering.getLocation(Incoming); |
| 124 | if (Loc.getNode()) { |
| 125 | // duplicates in input |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // Search back for the load from a stack slot pattern to find the original |
| 130 | // slot we allocated for this value. We could extend this to deal with |
| 131 | // simple modification patterns, but simple dealing with trivial load/store |
| 132 | // sequences helps a lot already. |
| 133 | if (LoadSDNode *Load = dyn_cast<LoadSDNode>(Incoming)) { |
| 134 | if (auto *FI = dyn_cast<FrameIndexSDNode>(Load->getBasePtr())) { |
| 135 | const int Index = FI->getIndex(); |
| 136 | auto Itr = std::find(Builder.FuncInfo.StatepointStackSlots.begin(), |
| 137 | Builder.FuncInfo.StatepointStackSlots.end(), Index); |
| 138 | if (Itr == Builder.FuncInfo.StatepointStackSlots.end()) { |
| 139 | // not one of the lowering stack slots, can't reuse! |
| 140 | // TODO: Actually, we probably could reuse the stack slot if the value |
| 141 | // hasn't changed at all, but we'd need to look for intervening writes |
| 142 | return; |
| 143 | } else { |
| 144 | // This is one of our dedicated lowering slots |
| 145 | const int Offset = |
| 146 | std::distance(Builder.FuncInfo.StatepointStackSlots.begin(), Itr); |
| 147 | if (Builder.StatepointLowering.isStackSlotAllocated(Offset)) { |
| 148 | // stack slot already assigned to someone else, can't use it! |
| 149 | // TODO: currently we reserve space for gc arguments after doing |
| 150 | // normal allocation for deopt arguments. We should reserve for |
| 151 | // _all_ deopt and gc arguments, then start allocating. This |
| 152 | // will prevent some moves being inserted when vm state changes, |
| 153 | // but gc state doesn't between two calls. |
| 154 | return; |
| 155 | } |
| 156 | // Reserve this stack slot |
| 157 | Builder.StatepointLowering.reserveStackSlot(Offset); |
| 158 | } |
| 159 | |
| 160 | // Cache this slot so we find it when going through the normal |
| 161 | // assignment loop. |
| 162 | SDValue Loc = |
| 163 | Builder.DAG.getTargetFrameIndex(Index, Incoming.getValueType()); |
| 164 | |
| 165 | Builder.StatepointLowering.setLocation(Incoming, Loc); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // TODO: handle case where a reloaded value flows through a phi to |
| 170 | // another safepoint. e.g. |
| 171 | // bb1: |
| 172 | // a' = relocated... |
| 173 | // bb2: % pred: bb1, bb3, bb4, etc. |
| 174 | // a_phi = phi(a', ...) |
| 175 | // statepoint ... a_phi |
| 176 | // NOTE: This will require reasoning about cross basic block values. This is |
| 177 | // decidedly non trivial and this might not be the right place to do it. We |
| 178 | // don't really have the information we need here... |
| 179 | |
| 180 | // TODO: handle simple updates. If a value is modified and the original |
| 181 | // value is no longer live, it would be nice to put the modified value in the |
| 182 | // same slot. This allows folding of the memory accesses for some |
| 183 | // instructions types (like an increment). |
| 184 | // statepoint (i) |
| 185 | // i1 = i+1 |
| 186 | // statepoint (i1) |
| 187 | } |
| 188 | |
| 189 | /// Remove any duplicate (as SDValues) from the derived pointer pairs. This |
| 190 | /// is not required for correctness. It's purpose is to reduce the size of |
| 191 | /// StackMap section. It has no effect on the number of spill slots required |
| 192 | /// or the actual lowering. |
| 193 | static void removeDuplicatesGCPtrs(SmallVectorImpl<const Value *> &Bases, |
| 194 | SmallVectorImpl<const Value *> &Ptrs, |
| 195 | SmallVectorImpl<const Value *> &Relocs, |
| 196 | SelectionDAGBuilder &Builder) { |
| 197 | |
| 198 | // This is horribly ineffecient, but I don't care right now |
| 199 | SmallSet<SDValue, 64> Seen; |
| 200 | |
| 201 | SmallVector<const Value *, 64> NewBases, NewPtrs, NewRelocs; |
| 202 | for (size_t i = 0; i < Ptrs.size(); i++) { |
| 203 | SDValue SD = Builder.getValue(Ptrs[i]); |
| 204 | // Only add non-duplicates |
| 205 | if (Seen.count(SD) == 0) { |
| 206 | NewBases.push_back(Bases[i]); |
| 207 | NewPtrs.push_back(Ptrs[i]); |
| 208 | NewRelocs.push_back(Relocs[i]); |
| 209 | } |
| 210 | Seen.insert(SD); |
| 211 | } |
| 212 | assert(Bases.size() >= NewBases.size()); |
| 213 | assert(Ptrs.size() >= NewPtrs.size()); |
| 214 | assert(Relocs.size() >= NewRelocs.size()); |
| 215 | Bases = NewBases; |
| 216 | Ptrs = NewPtrs; |
| 217 | Relocs = NewRelocs; |
| 218 | assert(Ptrs.size() == Bases.size()); |
| 219 | assert(Ptrs.size() == Relocs.size()); |
| 220 | } |
| 221 | |
| 222 | /// Extract call from statepoint, lower it and return pointer to the |
| 223 | /// call node. Also update NodeMap so that getValue(statepoint) will |
| 224 | /// reference lowered call result |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 225 | static SDNode * |
| 226 | lowerCallFromStatepoint(ImmutableStatepoint ISP, MachineBasicBlock *LandingPad, |
| 227 | SelectionDAGBuilder &Builder, |
| 228 | SmallVectorImpl<SDValue> &PendingExports) { |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 229 | |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 230 | ImmutableCallSite CS(ISP.getCallSite()); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 231 | |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 232 | SDValue ActualCallee = Builder.getValue(ISP.getActualCallee()); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 233 | |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 234 | // Handle immediate and symbolic callees. |
| 235 | if (auto *ConstCallee = dyn_cast<ConstantSDNode>(ActualCallee.getNode())) |
| 236 | ActualCallee = Builder.DAG.getIntPtrConstant(ConstCallee->getZExtValue(), |
| 237 | Builder.getCurSDLoc(), |
| 238 | /*isTarget=*/true); |
| 239 | else if (auto *SymbolicCallee = |
| 240 | dyn_cast<GlobalAddressSDNode>(ActualCallee.getNode())) |
| 241 | ActualCallee = Builder.DAG.getTargetGlobalAddress( |
| 242 | SymbolicCallee->getGlobal(), SDLoc(SymbolicCallee), |
| 243 | SymbolicCallee->getValueType(0)); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 244 | |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 245 | assert(CS.getCallingConv() != CallingConv::AnyReg && |
| 246 | "anyregcc is not supported on statepoints!"); |
| 247 | |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 248 | Type *DefTy = ISP.getActualReturnType(); |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 249 | bool HasDef = !DefTy->isVoidTy(); |
| 250 | |
| 251 | SDValue ReturnValue, CallEndVal; |
| 252 | std::tie(ReturnValue, CallEndVal) = Builder.lowerCallOperands( |
Sanjoy Das | 4bfb472 | 2015-05-06 02:36:31 +0000 | [diff] [blame] | 253 | ISP.getCallSite(), ImmutableStatepoint::CallArgsBeginPos, |
| 254 | ISP.getNumCallArgs(), ActualCallee, DefTy, LandingPad, |
| 255 | false /* IsPatchPoint */); |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 256 | |
| 257 | SDNode *CallEnd = CallEndVal.getNode(); |
| 258 | |
| 259 | // Get a call instruction from the call sequence chain. Tail calls are not |
| 260 | // allowed. The following code is essentially reverse engineering X86's |
| 261 | // LowerCallTo. |
| 262 | // |
| 263 | // We are expecting DAG to have the following form: |
| 264 | // |
| 265 | // ch = eh_label (only in case of invoke statepoint) |
| 266 | // ch, glue = callseq_start ch |
| 267 | // ch, glue = X86::Call ch, glue |
| 268 | // ch, glue = callseq_end ch, glue |
| 269 | // get_return_value ch, glue |
| 270 | // |
| 271 | // get_return_value can either be a CopyFromReg to grab the return value from |
| 272 | // %RAX, or it can be a LOAD to load a value returned by reference via a stack |
| 273 | // slot. |
| 274 | |
| 275 | if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg || |
| 276 | CallEnd->getOpcode() == ISD::LOAD)) |
| 277 | CallEnd = CallEnd->getOperand(0).getNode(); |
| 278 | |
| 279 | assert(CallEnd->getOpcode() == ISD::CALLSEQ_END && "expected!"); |
| 280 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 281 | if (HasDef) { |
Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 282 | if (CS.isInvoke()) { |
| 283 | // Result value will be used in different basic block for invokes |
| 284 | // so we need to export it now. But statepoint call has a different type |
| 285 | // than the actuall call. It means that standart exporting mechanism will |
| 286 | // create register of the wrong type. So instead we need to create |
| 287 | // register with correct type and save value into it manually. |
| 288 | // TODO: To eliminate this problem we can remove gc.result intrinsics |
| 289 | // completelly and make statepoint call to return a tuple. |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 290 | unsigned Reg = Builder.FuncInfo.CreateRegs(ISP.getActualReturnType()); |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 291 | RegsForValue RFV(*Builder.DAG.getContext(), |
| 292 | Builder.DAG.getTargetLoweringInfo(), Reg, |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 293 | ISP.getActualReturnType()); |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 294 | SDValue Chain = Builder.DAG.getEntryNode(); |
| 295 | |
| 296 | RFV.getCopyToRegs(ReturnValue, Builder.DAG, Builder.getCurSDLoc(), Chain, |
| 297 | nullptr); |
| 298 | PendingExports.push_back(Chain); |
| 299 | Builder.FuncInfo.ValueMap[CS.getInstruction()] = Reg; |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 300 | } else { |
Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 301 | // The value of the statepoint itself will be the value of call itself. |
| 302 | // We'll replace the actually call node shortly. gc_result will grab |
| 303 | // this value. |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 304 | Builder.setValue(CS.getInstruction(), ReturnValue); |
Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 305 | } |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 306 | } else { |
| 307 | // The token value is never used from here on, just generate a poison value |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 308 | Builder.setValue(CS.getInstruction(), |
| 309 | Builder.DAG.getIntPtrConstant(-1, Builder.getCurSDLoc())); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 310 | } |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 311 | |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 312 | return CallEnd->getOperand(0).getNode(); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /// Callect all gc pointers coming into statepoint intrinsic, clean them up, |
| 316 | /// and return two arrays: |
| 317 | /// Bases - base pointers incoming to this statepoint |
| 318 | /// Ptrs - derived pointers incoming to this statepoint |
| 319 | /// Relocs - the gc_relocate corresponding to each base/ptr pair |
| 320 | /// Elements of this arrays should be in one-to-one correspondence with each |
| 321 | /// other i.e Bases[i], Ptrs[i] are from the same gcrelocate call |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 322 | static void getIncomingStatepointGCValues( |
| 323 | SmallVectorImpl<const Value *> &Bases, SmallVectorImpl<const Value *> &Ptrs, |
| 324 | SmallVectorImpl<const Value *> &Relocs, ImmutableStatepoint StatepointSite, |
| 325 | SelectionDAGBuilder &Builder) { |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 326 | for (GCRelocateOperands relocateOpers : |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 327 | StatepointSite.getRelocates(StatepointSite)) { |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 328 | Relocs.push_back(relocateOpers.getUnderlyingCallSite().getInstruction()); |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 329 | Bases.push_back(relocateOpers.getBasePtr()); |
| 330 | Ptrs.push_back(relocateOpers.getDerivedPtr()); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Remove any redundant llvm::Values which map to the same SDValue as another |
| 334 | // input. Also has the effect of removing duplicates in the original |
| 335 | // llvm::Value input list as well. This is a useful optimization for |
| 336 | // reducing the size of the StackMap section. It has no other impact. |
| 337 | removeDuplicatesGCPtrs(Bases, Ptrs, Relocs, Builder); |
| 338 | |
| 339 | assert(Bases.size() == Ptrs.size() && Ptrs.size() == Relocs.size()); |
| 340 | } |
| 341 | |
| 342 | /// Spill a value incoming to the statepoint. It might be either part of |
| 343 | /// vmstate |
| 344 | /// or gcstate. In both cases unconditionally spill it on the stack unless it |
| 345 | /// is a null constant. Return pair with first element being frame index |
| 346 | /// containing saved value and second element with outgoing chain from the |
| 347 | /// emitted store |
| 348 | static std::pair<SDValue, SDValue> |
| 349 | spillIncomingStatepointValue(SDValue Incoming, SDValue Chain, |
| 350 | SelectionDAGBuilder &Builder) { |
| 351 | SDValue Loc = Builder.StatepointLowering.getLocation(Incoming); |
| 352 | |
| 353 | // Emit new store if we didn't do it for this ptr before |
| 354 | if (!Loc.getNode()) { |
| 355 | Loc = Builder.StatepointLowering.allocateStackSlot(Incoming.getValueType(), |
| 356 | Builder); |
| 357 | assert(isa<FrameIndexSDNode>(Loc)); |
| 358 | int Index = cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 359 | // We use TargetFrameIndex so that isel will not select it into LEA |
| 360 | Loc = Builder.DAG.getTargetFrameIndex(Index, Incoming.getValueType()); |
| 361 | |
| 362 | // TODO: We can create TokenFactor node instead of |
| 363 | // chaining stores one after another, this may allow |
| 364 | // a bit more optimal scheduling for them |
| 365 | Chain = Builder.DAG.getStore(Chain, Builder.getCurSDLoc(), Incoming, Loc, |
| 366 | MachinePointerInfo::getFixedStack(Index), |
| 367 | false, false, 0); |
| 368 | |
| 369 | Builder.StatepointLowering.setLocation(Incoming, Loc); |
| 370 | } |
| 371 | |
| 372 | assert(Loc.getNode()); |
| 373 | return std::make_pair(Loc, Chain); |
| 374 | } |
| 375 | |
| 376 | /// Lower a single value incoming to a statepoint node. This value can be |
| 377 | /// either a deopt value or a gc value, the handling is the same. We special |
| 378 | /// case constants and allocas, then fall back to spilling if required. |
| 379 | static void lowerIncomingStatepointValue(SDValue Incoming, |
| 380 | SmallVectorImpl<SDValue> &Ops, |
| 381 | SelectionDAGBuilder &Builder) { |
| 382 | SDValue Chain = Builder.getRoot(); |
| 383 | |
| 384 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Incoming)) { |
| 385 | // If the original value was a constant, make sure it gets recorded as |
| 386 | // such in the stackmap. This is required so that the consumer can |
| 387 | // parse any internal format to the deopt state. It also handles null |
| 388 | // pointers and other constant pointers in GC states |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 389 | Ops.push_back(Builder.DAG.getTargetConstant(StackMaps::ConstantOp, |
| 390 | Builder.getCurSDLoc(), |
| 391 | MVT::i64)); |
| 392 | Ops.push_back(Builder.DAG.getTargetConstant(C->getSExtValue(), |
| 393 | Builder.getCurSDLoc(), |
| 394 | MVT::i64)); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 395 | } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 396 | // This handles allocas as arguments to the statepoint (this is only |
| 397 | // really meaningful for a deopt value. For GC, we'd be trying to |
| 398 | // relocate the address of the alloca itself?) |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 399 | Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 400 | Incoming.getValueType())); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 401 | } else { |
| 402 | // Otherwise, locate a spill slot and explicitly spill it so it |
| 403 | // can be found by the runtime later. We currently do not support |
| 404 | // tracking values through callee saved registers to their eventual |
| 405 | // spill location. This would be a useful optimization, but would |
| 406 | // need to be optional since it requires a lot of complexity on the |
| 407 | // runtime side which not all would support. |
| 408 | std::pair<SDValue, SDValue> Res = |
| 409 | spillIncomingStatepointValue(Incoming, Chain, Builder); |
| 410 | Ops.push_back(Res.first); |
| 411 | Chain = Res.second; |
| 412 | } |
| 413 | |
| 414 | Builder.DAG.setRoot(Chain); |
| 415 | } |
| 416 | |
| 417 | /// Lower deopt state and gc pointer arguments of the statepoint. The actual |
| 418 | /// lowering is described in lowerIncomingStatepointValue. This function is |
| 419 | /// responsible for lowering everything in the right position and playing some |
| 420 | /// tricks to avoid redundant stack manipulation where possible. On |
| 421 | /// completion, 'Ops' will contain ready to use operands for machine code |
| 422 | /// statepoint. The chain nodes will have already been created and the DAG root |
| 423 | /// will be set to the last value spilled (if any were). |
| 424 | static void lowerStatepointMetaArgs(SmallVectorImpl<SDValue> &Ops, |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 425 | ImmutableStatepoint StatepointSite, |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 426 | SelectionDAGBuilder &Builder) { |
| 427 | |
| 428 | // Lower the deopt and gc arguments for this statepoint. Layout will |
| 429 | // be: deopt argument length, deopt arguments.., gc arguments... |
| 430 | |
| 431 | SmallVector<const Value *, 64> Bases, Ptrs, Relocations; |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 432 | getIncomingStatepointGCValues(Bases, Ptrs, Relocations, StatepointSite, |
| 433 | Builder); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 434 | |
Philip Reames | 4ac17a3 | 2015-01-07 19:07:50 +0000 | [diff] [blame] | 435 | #ifndef NDEBUG |
| 436 | // Check that each of the gc pointer and bases we've gotten out of the |
| 437 | // safepoint is something the strategy thinks might be a pointer into the GC |
| 438 | // heap. This is basically just here to help catch errors during statepoint |
| 439 | // insertion. TODO: This should actually be in the Verifier, but we can't get |
| 440 | // to the GCStrategy from there (yet). |
Philip Reames | e1bf270 | 2015-03-27 05:09:33 +0000 | [diff] [blame] | 441 | GCStrategy &S = Builder.GFI->getStrategy(); |
| 442 | for (const Value *V : Bases) { |
| 443 | auto Opt = S.isGCManagedPointer(V); |
| 444 | if (Opt.hasValue()) { |
| 445 | assert(Opt.getValue() && |
| 446 | "non gc managed base pointer found in statepoint"); |
Philip Reames | 4ac17a3 | 2015-01-07 19:07:50 +0000 | [diff] [blame] | 447 | } |
Philip Reames | e1bf270 | 2015-03-27 05:09:33 +0000 | [diff] [blame] | 448 | } |
| 449 | for (const Value *V : Ptrs) { |
| 450 | auto Opt = S.isGCManagedPointer(V); |
| 451 | if (Opt.hasValue()) { |
| 452 | assert(Opt.getValue() && |
| 453 | "non gc managed derived pointer found in statepoint"); |
Philip Reames | 4ac17a3 | 2015-01-07 19:07:50 +0000 | [diff] [blame] | 454 | } |
Philip Reames | e1bf270 | 2015-03-27 05:09:33 +0000 | [diff] [blame] | 455 | } |
| 456 | for (const Value *V : Relocations) { |
| 457 | auto Opt = S.isGCManagedPointer(V); |
| 458 | if (Opt.hasValue()) { |
| 459 | assert(Opt.getValue() && "non gc managed pointer relocated"); |
Philip Reames | 4ac17a3 | 2015-01-07 19:07:50 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | #endif |
| 463 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 464 | // Before we actually start lowering (and allocating spill slots for values), |
| 465 | // reserve any stack slots which we judge to be profitable to reuse for a |
| 466 | // particular value. This is purely an optimization over the code below and |
| 467 | // doesn't change semantics at all. It is important for performance that we |
| 468 | // reserve slots for both deopt and gc values before lowering either. |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 469 | for (auto I = StatepointSite.vm_state_begin() + 1, |
| 470 | E = StatepointSite.vm_state_end(); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 471 | I != E; ++I) { |
| 472 | Value *V = *I; |
| 473 | SDValue Incoming = Builder.getValue(V); |
| 474 | reservePreviousStackSlotForValue(Incoming, Builder); |
| 475 | } |
| 476 | for (unsigned i = 0; i < Bases.size() * 2; ++i) { |
| 477 | // Even elements will contain base, odd elements - derived ptr |
| 478 | const Value *V = i % 2 ? Bases[i / 2] : Ptrs[i / 2]; |
| 479 | SDValue Incoming = Builder.getValue(V); |
| 480 | reservePreviousStackSlotForValue(Incoming, Builder); |
| 481 | } |
| 482 | |
| 483 | // First, prefix the list with the number of unique values to be |
| 484 | // lowered. Note that this is the number of *Values* not the |
| 485 | // number of SDValues required to lower them. |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 486 | const int NumVMSArgs = StatepointSite.getNumTotalVMSArgs(); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 487 | Ops.push_back( Builder.DAG.getTargetConstant(StackMaps::ConstantOp, |
| 488 | Builder.getCurSDLoc(), |
| 489 | MVT::i64)); |
| 490 | Ops.push_back(Builder.DAG.getTargetConstant(NumVMSArgs, Builder.getCurSDLoc(), |
| 491 | MVT::i64)); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 492 | |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 493 | assert(NumVMSArgs + 1 == std::distance(StatepointSite.vm_state_begin(), |
| 494 | StatepointSite.vm_state_end())); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 495 | |
| 496 | // The vm state arguments are lowered in an opaque manner. We do |
| 497 | // not know what type of values are contained within. We skip the |
| 498 | // first one since that happens to be the total number we lowered |
| 499 | // explicitly just above. We could have left it in the loop and |
| 500 | // not done it explicitly, but it's far easier to understand this |
| 501 | // way. |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 502 | for (auto I = StatepointSite.vm_state_begin() + 1, |
| 503 | E = StatepointSite.vm_state_end(); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 504 | I != E; ++I) { |
| 505 | const Value *V = *I; |
| 506 | SDValue Incoming = Builder.getValue(V); |
| 507 | lowerIncomingStatepointValue(Incoming, Ops, Builder); |
| 508 | } |
| 509 | |
| 510 | // Finally, go ahead and lower all the gc arguments. There's no prefixed |
| 511 | // length for this one. After lowering, we'll have the base and pointer |
| 512 | // arrays interwoven with each (lowered) base pointer immediately followed by |
| 513 | // it's (lowered) derived pointer. i.e |
| 514 | // (base[0], ptr[0], base[1], ptr[1], ...) |
| 515 | for (unsigned i = 0; i < Bases.size() * 2; ++i) { |
| 516 | // Even elements will contain base, odd elements - derived ptr |
| 517 | const Value *V = i % 2 ? Bases[i / 2] : Ptrs[i / 2]; |
| 518 | SDValue Incoming = Builder.getValue(V); |
| 519 | lowerIncomingStatepointValue(Incoming, Ops, Builder); |
| 520 | } |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 521 | |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 522 | // If there are any explicit spill slots passed to the statepoint, record |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 523 | // them, but otherwise do not do anything special. These are user provided |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 524 | // allocas and give control over placement to the consumer. In this case, |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 525 | // it is the contents of the slot which may get updated, not the pointer to |
| 526 | // the alloca |
| 527 | for (Value *V : StatepointSite.gc_args()) { |
| 528 | SDValue Incoming = Builder.getValue(V); |
| 529 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Incoming)) { |
| 530 | // This handles allocas as arguments to the statepoint |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 531 | Ops.push_back(Builder.DAG.getTargetFrameIndex(FI->getIndex(), |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 532 | Incoming.getValueType())); |
Philip Reames | f8f0933 | 2015-03-27 04:52:48 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 535 | } |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 536 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 537 | void SelectionDAGBuilder::visitStatepoint(const CallInst &CI) { |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 538 | // Check some preconditions for sanity |
| 539 | assert(isStatepoint(&CI) && |
| 540 | "function called must be the statepoint function"); |
| 541 | |
| 542 | LowerStatepoint(ImmutableStatepoint(&CI)); |
| 543 | } |
| 544 | |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 545 | void SelectionDAGBuilder::LowerStatepoint( |
| 546 | ImmutableStatepoint ISP, MachineBasicBlock *LandingPad /*=nullptr*/) { |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 547 | // The basic scheme here is that information about both the original call and |
| 548 | // the safepoint is encoded in the CallInst. We create a temporary call and |
| 549 | // lower it, then reverse engineer the calling sequence. |
| 550 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 551 | NumOfStatepoints++; |
| 552 | // Clear state |
| 553 | StatepointLowering.startNewStatepoint(*this); |
| 554 | |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 555 | ImmutableCallSite CS(ISP.getCallSite()); |
| 556 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 557 | #ifndef NDEBUG |
| 558 | // Consistency check |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 559 | for (const User *U : CS->users()) { |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 560 | const CallInst *Call = cast<CallInst>(U); |
| 561 | if (isGCRelocate(Call)) |
| 562 | StatepointLowering.scheduleRelocCall(*Call); |
| 563 | } |
| 564 | #endif |
| 565 | |
Philip Reames | 72fbe7a | 2014-12-02 21:01:48 +0000 | [diff] [blame] | 566 | #ifndef NDEBUG |
| 567 | // If this is a malformed statepoint, report it early to simplify debugging. |
| 568 | // This should catch any IR level mistake that's made when constructing or |
| 569 | // transforming statepoints. |
| 570 | ISP.verify(); |
Philip Reames | 4ac17a3 | 2015-01-07 19:07:50 +0000 | [diff] [blame] | 571 | |
| 572 | // Check that the associated GCStrategy expects to encounter statepoints. |
| 573 | // TODO: This if should become an assert. For now, we allow the GCStrategy |
| 574 | // to be optional for backwards compatibility. This will only last a short |
| 575 | // period (i.e. a couple of weeks). |
Philip Reames | e1bf270 | 2015-03-27 05:09:33 +0000 | [diff] [blame] | 576 | assert(GFI->getStrategy().useStatepoints() && |
| 577 | "GCStrategy does not expect to encounter statepoints"); |
Philip Reames | 72fbe7a | 2014-12-02 21:01:48 +0000 | [diff] [blame] | 578 | #endif |
| 579 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 580 | // Lower statepoint vmstate and gcstate arguments |
Sanjoy Das | 3fb91c0 | 2015-05-05 23:06:49 +0000 | [diff] [blame] | 581 | SmallVector<SDValue, 10> LoweredMetaArgs; |
| 582 | lowerStatepointMetaArgs(LoweredMetaArgs, ISP, *this); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 583 | |
| 584 | // Get call node, we will replace it later with statepoint |
Sanjoy Das | c6bf3e9 | 2015-05-06 02:36:20 +0000 | [diff] [blame] | 585 | SDNode *CallNode = |
| 586 | lowerCallFromStatepoint(ISP, LandingPad, *this, PendingExports); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 587 | |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 588 | // Construct the actual GC_TRANSITION_START, STATEPOINT, and GC_TRANSITION_END |
| 589 | // nodes with all the appropriate arguments and return values. |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 590 | |
| 591 | // TODO: Currently, all of these operands are being marked as read/write in |
| 592 | // PrologEpilougeInserter.cpp, we should special case the VMState arguments |
| 593 | // and flags to be read-only. |
| 594 | SmallVector<SDValue, 40> Ops; |
| 595 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 596 | // Call Node: Chain, Target, {Args}, RegMask, [Glue] |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 597 | SDValue Chain = CallNode->getOperand(0); |
| 598 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 599 | SDValue Glue; |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 600 | bool CallHasIncomingGlue = CallNode->getGluedNode(); |
| 601 | if (CallHasIncomingGlue) { |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 602 | // Glue is always last operand |
| 603 | Glue = CallNode->getOperand(CallNode->getNumOperands() - 1); |
| 604 | } |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 605 | |
| 606 | // Build the GC_TRANSITION_START node if necessary. |
| 607 | // |
| 608 | // The operands to the GC_TRANSITION_{START,END} nodes are laid out in the |
| 609 | // order in which they appear in the call to the statepoint intrinsic. If |
| 610 | // any of the operands is a pointer-typed, that operand is immediately |
| 611 | // followed by a SRCVALUE for the pointer that may be used during lowering |
| 612 | // (e.g. to form MachinePointerInfo values for loads/stores). |
| 613 | const bool IsGCTransition = |
| 614 | (ISP.getFlags() & (uint64_t)StatepointFlags::GCTransition) == |
| 615 | (uint64_t)StatepointFlags::GCTransition; |
| 616 | if (IsGCTransition) { |
| 617 | SmallVector<SDValue, 8> TSOps; |
| 618 | |
| 619 | // Add chain |
| 620 | TSOps.push_back(Chain); |
| 621 | |
| 622 | // Add GC transition arguments |
| 623 | for (auto I = ISP.gc_transition_args_begin() + 1, |
| 624 | E = ISP.gc_transition_args_end(); |
| 625 | I != E; ++I) { |
| 626 | TSOps.push_back(getValue(*I)); |
| 627 | if ((*I)->getType()->isPointerTy()) |
| 628 | TSOps.push_back(DAG.getSrcValue(*I)); |
| 629 | } |
| 630 | |
| 631 | // Add glue if necessary |
| 632 | if (CallHasIncomingGlue) |
| 633 | TSOps.push_back(Glue); |
| 634 | |
| 635 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| 636 | |
| 637 | SDValue GCTransitionStart = |
| 638 | DAG.getNode(ISD::GC_TRANSITION_START, getCurSDLoc(), NodeTys, TSOps); |
| 639 | |
| 640 | Chain = GCTransitionStart.getValue(0); |
| 641 | Glue = GCTransitionStart.getValue(1); |
| 642 | } |
| 643 | |
| 644 | // Calculate and push starting position of vmstate arguments |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 645 | // Get number of arguments incoming directly into call node |
| 646 | unsigned NumCallRegArgs = |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 647 | CallNode->getNumOperands() - (CallHasIncomingGlue ? 4 : 3); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 648 | Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, getCurSDLoc(), MVT::i32)); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 649 | |
| 650 | // Add call target |
| 651 | SDValue CallTarget = SDValue(CallNode->getOperand(1).getNode(), 0); |
| 652 | Ops.push_back(CallTarget); |
| 653 | |
| 654 | // Add call arguments |
| 655 | // Get position of register mask in the call |
| 656 | SDNode::op_iterator RegMaskIt; |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 657 | if (CallHasIncomingGlue) |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 658 | RegMaskIt = CallNode->op_end() - 2; |
| 659 | else |
| 660 | RegMaskIt = CallNode->op_end() - 1; |
| 661 | Ops.insert(Ops.end(), CallNode->op_begin() + 2, RegMaskIt); |
| 662 | |
| 663 | // Add a leading constant argument with the Flags and the calling convention |
| 664 | // masked together |
Igor Laevsky | 7fc58a4 | 2015-02-20 15:28:35 +0000 | [diff] [blame] | 665 | CallingConv::ID CallConv = CS.getCallingConv(); |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 666 | uint64_t Flags = cast<ConstantInt>(CS.getArgument(2))->getZExtValue(); |
| 667 | assert( |
| 668 | ((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0) |
| 669 | && "unknown flag used"); |
| 670 | const int Shift = 1; |
| 671 | static_assert( |
| 672 | ((~(uint64_t)0 << Shift) & (uint64_t)StatepointFlags::MaskAll) == 0, |
| 673 | "shift width too small"); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 674 | Ops.push_back(DAG.getTargetConstant(StackMaps::ConstantOp, getCurSDLoc(), |
| 675 | MVT::i64)); |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 676 | Ops.push_back(DAG.getTargetConstant(Flags | ((unsigned)CallConv << Shift), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 677 | getCurSDLoc(), MVT::i64)); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 678 | |
| 679 | // Insert all vmstate and gcstate arguments |
Sanjoy Das | 3fb91c0 | 2015-05-05 23:06:49 +0000 | [diff] [blame] | 680 | Ops.insert(Ops.end(), LoweredMetaArgs.begin(), LoweredMetaArgs.end()); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 681 | |
| 682 | // Add register mask from call node |
| 683 | Ops.push_back(*RegMaskIt); |
| 684 | |
| 685 | // Add chain |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 686 | Ops.push_back(Chain); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 687 | |
| 688 | // Same for the glue, but we add it only if original call had it |
| 689 | if (Glue.getNode()) |
| 690 | Ops.push_back(Glue); |
| 691 | |
Benjamin Kramer | ea68a94 | 2015-02-19 15:26:17 +0000 | [diff] [blame] | 692 | // Compute return values. Provide a glue output since we consume one as |
| 693 | // input. This allows someone else to chain off us as needed. |
| 694 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 695 | |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 696 | SDNode *StatepointMCNode = |
| 697 | DAG.getMachineNode(TargetOpcode::STATEPOINT, getCurSDLoc(), NodeTys, Ops); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 698 | |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 699 | SDNode *SinkNode = StatepointMCNode; |
| 700 | |
| 701 | // Build the GC_TRANSITION_END node if necessary. |
| 702 | // |
| 703 | // See the comment above regarding GC_TRANSITION_START for the layout of |
| 704 | // the operands to the GC_TRANSITION_END node. |
| 705 | if (IsGCTransition) { |
| 706 | SmallVector<SDValue, 8> TEOps; |
| 707 | |
| 708 | // Add chain |
| 709 | TEOps.push_back(SDValue(StatepointMCNode, 0)); |
| 710 | |
| 711 | // Add GC transition arguments |
| 712 | for (auto I = ISP.gc_transition_args_begin() + 1, |
| 713 | E = ISP.gc_transition_args_end(); |
| 714 | I != E; ++I) { |
| 715 | TEOps.push_back(getValue(*I)); |
| 716 | if ((*I)->getType()->isPointerTy()) |
| 717 | TEOps.push_back(DAG.getSrcValue(*I)); |
| 718 | } |
| 719 | |
| 720 | // Add glue |
| 721 | TEOps.push_back(SDValue(StatepointMCNode, 1)); |
| 722 | |
| 723 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| 724 | |
| 725 | SDValue GCTransitionStart = |
| 726 | DAG.getNode(ISD::GC_TRANSITION_END, getCurSDLoc(), NodeTys, TEOps); |
| 727 | |
| 728 | SinkNode = GCTransitionStart.getNode(); |
| 729 | } |
| 730 | |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 731 | // Replace original call |
Pat Gavlin | cc0431d | 2015-05-08 18:07:42 +0000 | [diff] [blame^] | 732 | DAG.ReplaceAllUsesWith(CallNode, SinkNode); // This may update Root |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 733 | // Remove originall call node |
| 734 | DAG.DeleteNode(CallNode); |
| 735 | |
| 736 | // DON'T set the root - under the assumption that it's already set past the |
| 737 | // inserted node we created. |
| 738 | |
| 739 | // TODO: A better future implementation would be to emit a single variable |
| 740 | // argument, variable return value STATEPOINT node here and then hookup the |
| 741 | // return value of each gc.relocate to the respective output of the |
| 742 | // previously emitted STATEPOINT value. Unfortunately, this doesn't appear |
| 743 | // to actually be possible today. |
| 744 | } |
| 745 | |
| 746 | void SelectionDAGBuilder::visitGCResult(const CallInst &CI) { |
| 747 | // The result value of the gc_result is simply the result of the actual |
| 748 | // call. We've already emitted this, so just grab the value. |
| 749 | Instruction *I = cast<Instruction>(CI.getArgOperand(0)); |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 750 | assert(isStatepoint(I) && "first argument must be a statepoint token"); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 751 | |
Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 752 | if (isa<InvokeInst>(I)) { |
| 753 | // For invokes we should have stored call result in a virtual register. |
| 754 | // We can not use default getValue() functionality to copy value from this |
| 755 | // register because statepoint and actuall call return types can be |
| 756 | // different, and getValue() will use CopyFromReg of the wrong type, |
| 757 | // which is always i32 in our case. |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 758 | PointerType *CalleeType = |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 759 | cast<PointerType>(ImmutableStatepoint(I).getActualCallee()->getType()); |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 760 | Type *RetTy = |
| 761 | cast<FunctionType>(CalleeType->getElementType())->getReturnType(); |
Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 762 | SDValue CopyFromReg = getCopyFromRegs(I, RetTy); |
| 763 | |
| 764 | assert(CopyFromReg.getNode()); |
| 765 | setValue(&CI, CopyFromReg); |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 766 | } else { |
Igor Laevsky | 85f7f72 | 2015-03-10 16:26:48 +0000 | [diff] [blame] | 767 | setValue(&CI, getValue(I)); |
| 768 | } |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void SelectionDAGBuilder::visitGCRelocate(const CallInst &CI) { |
| 772 | #ifndef NDEBUG |
| 773 | // Consistency check |
| 774 | StatepointLowering.relocCallVisited(CI); |
| 775 | #endif |
| 776 | |
| 777 | GCRelocateOperands relocateOpers(&CI); |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 778 | SDValue SD = getValue(relocateOpers.getDerivedPtr()); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 779 | |
| 780 | if (isa<ConstantSDNode>(SD) || isa<FrameIndexSDNode>(SD)) { |
| 781 | // We didn't need to spill these special cases (constants and allocas). |
| 782 | // See the handling in spillIncomingValueForStatepoint for detail. |
| 783 | setValue(&CI, SD); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | SDValue Loc = StatepointLowering.getRelocLocation(SD); |
| 788 | // Emit new load if we did not emit it before |
| 789 | if (!Loc.getNode()) { |
| 790 | SDValue SpillSlot = StatepointLowering.getLocation(SD); |
| 791 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
| 792 | |
| 793 | // Be conservative: flush all pending loads |
| 794 | // TODO: Probably we can be less restrictive on this, |
| 795 | // it may allow more scheduling opprtunities |
| 796 | SDValue Chain = getRoot(); |
| 797 | |
Pat Gavlin | 022c5ac | 2015-04-29 21:52:45 +0000 | [diff] [blame] | 798 | Loc = DAG.getLoad(SpillSlot.getValueType(), getCurSDLoc(), Chain, SpillSlot, |
| 799 | MachinePointerInfo::getFixedStack(FI), false, false, |
| 800 | false, 0); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 801 | |
| 802 | StatepointLowering.setRelocLocation(SD, Loc); |
| 803 | |
| 804 | // Again, be conservative, don't emit pending loads |
| 805 | DAG.setRoot(Loc.getValue(1)); |
| 806 | } |
| 807 | |
| 808 | assert(Loc.getNode()); |
| 809 | setValue(&CI, Loc); |
| 810 | } |