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"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/CodeGen/SelectionDAGNodes.h"
21 #include <vector>
22 
23 namespace llvm {
24 class SelectionDAGBuilder;
25 
26 /// This class tracks both per-statepoint and per-selectiondag information.
27 /// For each statepoint it tracks locations of it's gc valuess (incoming and
28 /// relocated) and list of gcreloc calls scheduled for visiting (this is
29 /// used for a debug mode consistency check only).  The spill slot tracking
30 /// works in concert with information in FunctionLoweringInfo.
31 class StatepointLoweringState {
32 public:
StatepointLoweringState()33   StatepointLoweringState() : NextSlotToAllocate(0) {
34   }
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.
getLocation(SDValue val)49   SDValue getLocation(SDValue val) {
50     if (!Locations.count(val))
51       return SDValue();
52     return Locations[val];
53   }
setLocation(SDValue val,SDValue Location)54   void setLocation(SDValue val, SDValue Location) {
55     assert(!Locations.count(val) &&
56            "Trying to allocate already allocated location");
57     Locations[val] = Location;
58   }
59 
60   /// Returns the relocated value for a given input pointer. Will
61   /// return SDValue() if this value hasn't yet been reloaded from
62   /// it's stack slot after the statepoint.  Otherwise, the value
63   /// has already been reloaded and the SDValue of that reload will
64   /// be returned. Note that VMState values are spilled but not
65   /// reloaded (since they don't change at the safepoint unless
66   /// also listed in the GC pointer section) and will thus never
67   /// be in this map
getRelocLocation(SDValue val)68   SDValue getRelocLocation(SDValue val) {
69     if (!RelocLocations.count(val))
70       return SDValue();
71     return RelocLocations[val];
72   }
setRelocLocation(SDValue val,SDValue Location)73   void setRelocLocation(SDValue val, SDValue Location) {
74     assert(!RelocLocations.count(val) &&
75            "Trying to allocate already allocated location");
76     RelocLocations[val] = Location;
77   }
78 
79   /// Record the fact that we expect to encounter a given gc_relocate
80   /// before the next statepoint.  If we don't see it, we'll report
81   /// an assertion.
scheduleRelocCall(const CallInst & RelocCall)82   void scheduleRelocCall(const CallInst &RelocCall) {
83     PendingGCRelocateCalls.push_back(&RelocCall);
84   }
85   /// Remove this gc_relocate from the list we're expecting to see
86   /// before the next statepoint.  If we weren't expecting to see
87   /// it, we'll report an assertion.
relocCallVisited(const CallInst & RelocCall)88   void relocCallVisited(const CallInst &RelocCall) {
89     SmallVectorImpl<const CallInst *>::iterator itr =
90         std::find(PendingGCRelocateCalls.begin(), PendingGCRelocateCalls.end(),
91                   &RelocCall);
92     assert(itr != PendingGCRelocateCalls.end() &&
93            "Visited unexpected gcrelocate call");
94     PendingGCRelocateCalls.erase(itr);
95   }
96 
97   // TODO: Should add consistency tracking to ensure we encounter
98   // expected gc_result calls too.
99 
100   /// Get a stack slot we can use to store an value of type ValueType.  This
101   /// will hopefully be a recylced slot from another statepoint.
102   SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder);
103 
reserveStackSlot(int Offset)104   void reserveStackSlot(int Offset) {
105     assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
106            "out of bounds");
107     assert(!AllocatedStackSlots[Offset] && "already reserved!");
108     assert(NextSlotToAllocate <= (unsigned)Offset && "consistency!");
109     AllocatedStackSlots[Offset] = true;
110   }
isStackSlotAllocated(int Offset)111   bool isStackSlotAllocated(int Offset) {
112     assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
113            "out of bounds");
114     return AllocatedStackSlots[Offset];
115   }
116 
117 private:
118   /// Maps pre-relocation value (gc pointer directly incoming into statepoint)
119   /// into it's location (currently only stack slots)
120   DenseMap<SDValue, SDValue> Locations;
121   /// Map pre-relocated value into it's new relocated location
122   DenseMap<SDValue, SDValue> RelocLocations;
123 
124   /// A boolean indicator for each slot listed in the FunctionInfo as to
125   /// whether it has been used in the current statepoint.  Since we try to
126   /// preserve stack slots across safepoints, there can be gaps in which
127   /// slots have been allocated.
128   SmallVector<bool, 50> AllocatedStackSlots;
129 
130   /// Points just beyond the last slot known to have been allocated
131   unsigned NextSlotToAllocate;
132 
133   /// Keep track of pending gcrelocate calls for consistency check
134   SmallVector<const CallInst *, 10> PendingGCRelocateCalls;
135 };
136 } // end namespace llvm
137 
138 #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
139