blob: 78cdc39db1f45f14b37d1a11463fd5edad37a8c1 [file] [log] [blame]
Philip Reames1a1bdb22014-12-02 18:50:36 +00001//===-- 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 Das171313c2016-02-19 17:15:26 +000019#include "llvm/ADT/SmallBitVector.h"
Philip Reames1a1bdb22014-12-02 18:50:36 +000020#include "llvm/CodeGen/SelectionDAG.h"
21#include "llvm/CodeGen/SelectionDAGNodes.h"
22#include <vector>
23
24namespace llvm {
25class 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.
32class StatepointLoweringState {
33public:
Pat Gavlin022c5ac2015-04-29 21:52:45 +000034 StatepointLoweringState() : NextSlotToAllocate(0) {}
Philip Reames1a1bdb22014-12-02 18:50:36 +000035
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 Das7edbef32016-03-23 02:24:13 +000049 SDValue getLocation(SDValue Val) {
50 if (!Locations.count(Val))
Philip Reames1a1bdb22014-12-02 18:50:36 +000051 return SDValue();
Sanjoy Das7edbef32016-03-23 02:24:13 +000052 return Locations[Val];
Philip Reames1a1bdb22014-12-02 18:50:36 +000053 }
Sanjoy Das7edbef32016-03-23 02:24:13 +000054
55 void setLocation(SDValue Val, SDValue Location) {
56 assert(!Locations.count(Val) &&
Philip Reames1a1bdb22014-12-02 18:50:36 +000057 "Trying to allocate already allocated location");
Sanjoy Das7edbef32016-03-23 02:24:13 +000058 Locations[Val] = Location;
Philip Reames1a1bdb22014-12-02 18:50:36 +000059 }
60
Philip Reames1a1bdb22014-12-02 18:50:36 +000061 /// Record the fact that we expect to encounter a given gc_relocate
62 /// before the next statepoint. If we don't see it, we'll report
63 /// an assertion.
64 void scheduleRelocCall(const CallInst &RelocCall) {
65 PendingGCRelocateCalls.push_back(&RelocCall);
66 }
Sanjoy Das7edbef32016-03-23 02:24:13 +000067
Philip Reames1a1bdb22014-12-02 18:50:36 +000068 /// Remove this gc_relocate from the list we're expecting to see
69 /// before the next statepoint. If we weren't expecting to see
70 /// it, we'll report an assertion.
71 void relocCallVisited(const CallInst &RelocCall) {
Sanjoy Das7edbef32016-03-23 02:24:13 +000072 auto I = find(PendingGCRelocateCalls, &RelocCall);
73 assert(I != PendingGCRelocateCalls.end() &&
Philip Reames1a1bdb22014-12-02 18:50:36 +000074 "Visited unexpected gcrelocate call");
Sanjoy Das7edbef32016-03-23 02:24:13 +000075 PendingGCRelocateCalls.erase(I);
Philip Reames1a1bdb22014-12-02 18:50:36 +000076 }
77
78 // TODO: Should add consistency tracking to ensure we encounter
79 // expected gc_result calls too.
80
81 /// Get a stack slot we can use to store an value of type ValueType. This
82 /// will hopefully be a recylced slot from another statepoint.
83 SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder);
84
85 void reserveStackSlot(int Offset) {
86 assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
87 "out of bounds");
Sanjoy Das171313c2016-02-19 17:15:26 +000088 assert(!AllocatedStackSlots.test(Offset) && "already reserved!");
Philip Reames1a1bdb22014-12-02 18:50:36 +000089 assert(NextSlotToAllocate <= (unsigned)Offset && "consistency!");
Sanjoy Das171313c2016-02-19 17:15:26 +000090 AllocatedStackSlots.set(Offset);
Philip Reames1a1bdb22014-12-02 18:50:36 +000091 }
Sanjoy Das7edbef32016-03-23 02:24:13 +000092
Philip Reames1a1bdb22014-12-02 18:50:36 +000093 bool isStackSlotAllocated(int Offset) {
94 assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
95 "out of bounds");
Sanjoy Das171313c2016-02-19 17:15:26 +000096 return AllocatedStackSlots.test(Offset);
Philip Reames1a1bdb22014-12-02 18:50:36 +000097 }
98
99private:
100 /// Maps pre-relocation value (gc pointer directly incoming into statepoint)
101 /// into it's location (currently only stack slots)
102 DenseMap<SDValue, SDValue> Locations;
Philip Reames1a1bdb22014-12-02 18:50:36 +0000103
104 /// A boolean indicator for each slot listed in the FunctionInfo as to
105 /// whether it has been used in the current statepoint. Since we try to
106 /// preserve stack slots across safepoints, there can be gaps in which
107 /// slots have been allocated.
Sanjoy Das171313c2016-02-19 17:15:26 +0000108 SmallBitVector AllocatedStackSlots;
Philip Reames1a1bdb22014-12-02 18:50:36 +0000109
110 /// Points just beyond the last slot known to have been allocated
111 unsigned NextSlotToAllocate;
112
113 /// Keep track of pending gcrelocate calls for consistency check
114 SmallVector<const CallInst *, 10> PendingGCRelocateCalls;
115};
116} // end namespace llvm
117
118#endif // LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H