StatepointLowering.cpp 35 KB

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