StatepointLowering.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===- StatepointLowering.h - SDAGBuilder's statepoint code ---*- C++ -*---===//
  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. #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
  14. #define LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallBitVector.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/CodeGen/SelectionDAGNodes.h"
  19. #include "llvm/IR/IntrinsicInst.h"
  20. #include <cassert>
  21. namespace llvm {
  22. class SelectionDAGBuilder;
  23. /// This class tracks both per-statepoint and per-selectiondag information.
  24. /// For each statepoint it tracks locations of it's gc valuess (incoming and
  25. /// relocated) and list of gcreloc calls scheduled for visiting (this is
  26. /// used for a debug mode consistency check only). The spill slot tracking
  27. /// works in concert with information in FunctionLoweringInfo.
  28. class StatepointLoweringState {
  29. public:
  30. StatepointLoweringState() = default;
  31. /// Reset all state tracking for a newly encountered safepoint. Also
  32. /// performs some consistency checking.
  33. void startNewStatepoint(SelectionDAGBuilder &Builder);
  34. /// Clear the memory usage of this object. This is called from
  35. /// SelectionDAGBuilder::clear. We require this is never called in the
  36. /// midst of processing a statepoint sequence.
  37. void clear();
  38. /// Returns the spill location of a value incoming to the current
  39. /// statepoint. Will return SDValue() if this value hasn't been
  40. /// spilled. Otherwise, the value has already been spilled and no
  41. /// further action is required by the caller.
  42. SDValue getLocation(SDValue Val) {
  43. auto I = Locations.find(Val);
  44. if (I == Locations.end())
  45. return SDValue();
  46. return I->second;
  47. }
  48. void setLocation(SDValue Val, SDValue Location) {
  49. assert(!Locations.count(Val) &&
  50. "Trying to allocate already allocated location");
  51. Locations[Val] = Location;
  52. }
  53. /// Record the fact that we expect to encounter a given gc_relocate
  54. /// before the next statepoint. If we don't see it, we'll report
  55. /// an assertion.
  56. void scheduleRelocCall(const GCRelocateInst &RelocCall) {
  57. // We are not interested in lowering dead instructions.
  58. if (!RelocCall.use_empty())
  59. PendingGCRelocateCalls.push_back(&RelocCall);
  60. }
  61. /// Remove this gc_relocate from the list we're expecting to see
  62. /// before the next statepoint. If we weren't expecting to see
  63. /// it, we'll report an assertion.
  64. void relocCallVisited(const GCRelocateInst &RelocCall) {
  65. // We are not interested in lowering dead instructions.
  66. if (RelocCall.use_empty())
  67. return;
  68. auto I = llvm::find(PendingGCRelocateCalls, &RelocCall);
  69. assert(I != PendingGCRelocateCalls.end() &&
  70. "Visited unexpected gcrelocate call");
  71. PendingGCRelocateCalls.erase(I);
  72. }
  73. // TODO: Should add consistency tracking to ensure we encounter
  74. // expected gc_result calls too.
  75. /// Get a stack slot we can use to store an value of type ValueType. This
  76. /// will hopefully be a recylced slot from another statepoint.
  77. SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder);
  78. void reserveStackSlot(int Offset) {
  79. assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
  80. "out of bounds");
  81. assert(!AllocatedStackSlots.test(Offset) && "already reserved!");
  82. assert(NextSlotToAllocate <= (unsigned)Offset && "consistency!");
  83. AllocatedStackSlots.set(Offset);
  84. }
  85. bool isStackSlotAllocated(int Offset) {
  86. assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
  87. "out of bounds");
  88. return AllocatedStackSlots.test(Offset);
  89. }
  90. private:
  91. /// Maps pre-relocation value (gc pointer directly incoming into statepoint)
  92. /// into it's location (currently only stack slots)
  93. DenseMap<SDValue, SDValue> Locations;
  94. /// A boolean indicator for each slot listed in the FunctionInfo as to
  95. /// whether it has been used in the current statepoint. Since we try to
  96. /// preserve stack slots across safepoints, there can be gaps in which
  97. /// slots have been allocated.
  98. SmallBitVector AllocatedStackSlots;
  99. /// Points just beyond the last slot known to have been allocated
  100. unsigned NextSlotToAllocate = 0;
  101. /// Keep track of pending gcrelocate calls for consistency check
  102. SmallVector<const GCRelocateInst *, 10> PendingGCRelocateCalls;
  103. };
  104. } // end namespace llvm
  105. #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H