Reformat.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / StatepointLowering.h
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:
33   StatepointLoweringState() : NextSlotToAllocate(0) {}
34
35   /// Reset all state tracking for a newly encountered safepoint.  Also
36   /// performs some consistency checking.
37   void startNewStatepoint(SelectionDAGBuilder &Builder);
38
39   /// Clear the memory usage of this object.  This is called from
40   /// SelectionDAGBuilder::clear.  We require this is never called in the
41   /// midst of processing a statepoint sequence.
42   void clear();
43
44   /// Returns the spill location of a value incoming to the current
45   /// statepoint.  Will return SDValue() if this value hasn't been
46   /// spilled.  Otherwise, the value has already been spilled and no
47   /// further action is required by the caller.
48   SDValue getLocation(SDValue val) {
49     if (!Locations.count(val))
50       return SDValue();
51     return Locations[val];
52   }
53   void setLocation(SDValue val, SDValue Location) {
54     assert(!Locations.count(val) &&
55            "Trying to allocate already allocated location");
56     Locations[val] = Location;
57   }
58
59   /// Returns the relocated value for a given input pointer. Will
60   /// return SDValue() if this value hasn't yet been reloaded from
61   /// it's stack slot after the statepoint.  Otherwise, the value
62   /// has already been reloaded and the SDValue of that reload will
63   /// be returned. Note that VMState values are spilled but not
64   /// reloaded (since they don't change at the safepoint unless
65   /// also listed in the GC pointer section) and will thus never
66   /// be in this map
67   SDValue getRelocLocation(SDValue val) {
68     if (!RelocLocations.count(val))
69       return SDValue();
70     return RelocLocations[val];
71   }
72   void setRelocLocation(SDValue val, SDValue Location) {
73     assert(!RelocLocations.count(val) &&
74            "Trying to allocate already allocated location");
75     RelocLocations[val] = Location;
76   }
77
78   /// Record the fact that we expect to encounter a given gc_relocate
79   /// before the next statepoint.  If we don't see it, we'll report
80   /// an assertion.
81   void scheduleRelocCall(const CallInst &RelocCall) {
82     PendingGCRelocateCalls.push_back(&RelocCall);
83   }
84   /// Remove this gc_relocate from the list we're expecting to see
85   /// before the next statepoint.  If we weren't expecting to see
86   /// it, we'll report an assertion.
87   void relocCallVisited(const CallInst &RelocCall) {
88     SmallVectorImpl<const CallInst *>::iterator itr =
89         std::find(PendingGCRelocateCalls.begin(), PendingGCRelocateCalls.end(),
90                   &RelocCall);
91     assert(itr != PendingGCRelocateCalls.end() &&
92            "Visited unexpected gcrelocate call");
93     PendingGCRelocateCalls.erase(itr);
94   }
95
96   // TODO: Should add consistency tracking to ensure we encounter
97   // expected gc_result calls too.
98
99   /// Get a stack slot we can use to store an value of type ValueType.  This
100   /// will hopefully be a recylced slot from another statepoint.
101   SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder);
102
103   void reserveStackSlot(int Offset) {
104     assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
105            "out of bounds");
106     assert(!AllocatedStackSlots[Offset] && "already reserved!");
107     assert(NextSlotToAllocate <= (unsigned)Offset && "consistency!");
108     AllocatedStackSlots[Offset] = true;
109   }
110   bool isStackSlotAllocated(int Offset) {
111     assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
112            "out of bounds");
113     return AllocatedStackSlots[Offset];
114   }
115
116 private:
117   /// Maps pre-relocation value (gc pointer directly incoming into statepoint)
118   /// into it's location (currently only stack slots)
119   DenseMap<SDValue, SDValue> Locations;
120   /// Map pre-relocated value into it's new relocated location
121   DenseMap<SDValue, SDValue> RelocLocations;
122
123   /// A boolean indicator for each slot listed in the FunctionInfo as to
124   /// whether it has been used in the current statepoint.  Since we try to
125   /// preserve stack slots across safepoints, there can be gaps in which
126   /// slots have been allocated.
127   SmallVector<bool, 50> AllocatedStackSlots;
128
129   /// Points just beyond the last slot known to have been allocated
130   unsigned NextSlotToAllocate;
131
132   /// Keep track of pending gcrelocate calls for consistency check
133   SmallVector<const CallInst *, 10> PendingGCRelocateCalls;
134 };
135 } // end namespace llvm
136
137 #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H