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