1 //===-- StatepointLowering.h - SDAGBuilder's statepoint code -*- C++ -*---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file includes support code use by SelectionDAGBuilder when lowering a
11 // statepoint sequence in SelectionDAG IR.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
16 #define LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/CodeGen/SelectionDAGNodes.h"
24 class SelectionDAGBuilder;
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 {
33 StatepointLoweringState() : NextSlotToAllocate(0) {}
35 /// Reset all state tracking for a newly encountered safepoint. Also
36 /// performs some consistency checking.
37 void startNewStatepoint(SelectionDAGBuilder &Builder);
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.
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))
51 return Locations[val];
53 void setLocation(SDValue val, SDValue Location) {
54 assert(!Locations.count(val) &&
55 "Trying to allocate already allocated location");
56 Locations[val] = Location;
59 /// Record the fact that we expect to encounter a given gc_relocate
60 /// before the next statepoint. If we don't see it, we'll report
62 void scheduleRelocCall(const CallInst &RelocCall) {
63 PendingGCRelocateCalls.push_back(&RelocCall);
65 /// Remove this gc_relocate from the list we're expecting to see
66 /// before the next statepoint. If we weren't expecting to see
67 /// it, we'll report an assertion.
68 void relocCallVisited(const CallInst &RelocCall) {
69 SmallVectorImpl<const CallInst *>::iterator itr =
70 std::find(PendingGCRelocateCalls.begin(), PendingGCRelocateCalls.end(),
72 assert(itr != PendingGCRelocateCalls.end() &&
73 "Visited unexpected gcrelocate call");
74 PendingGCRelocateCalls.erase(itr);
77 // TODO: Should add consistency tracking to ensure we encounter
78 // expected gc_result calls too.
80 /// Get a stack slot we can use to store an value of type ValueType. This
81 /// will hopefully be a recylced slot from another statepoint.
82 SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder);
84 void reserveStackSlot(int Offset) {
85 assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
87 assert(!AllocatedStackSlots[Offset] && "already reserved!");
88 assert(NextSlotToAllocate <= (unsigned)Offset && "consistency!");
89 AllocatedStackSlots[Offset] = true;
91 bool isStackSlotAllocated(int Offset) {
92 assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
94 return AllocatedStackSlots[Offset];
98 /// Maps pre-relocation value (gc pointer directly incoming into statepoint)
99 /// into it's location (currently only stack slots)
100 DenseMap<SDValue, SDValue> Locations;
102 /// A boolean indicator for each slot listed in the FunctionInfo as to
103 /// whether it has been used in the current statepoint. Since we try to
104 /// preserve stack slots across safepoints, there can be gaps in which
105 /// slots have been allocated.
106 SmallVector<bool, 50> AllocatedStackSlots;
108 /// Points just beyond the last slot known to have been allocated
109 unsigned NextSlotToAllocate;
111 /// Keep track of pending gcrelocate calls for consistency check
112 SmallVector<const CallInst *, 10> PendingGCRelocateCalls;
114 } // end namespace llvm
116 #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H