StatepointLowering.h 4.8 KB

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