blob: 70507932681d04af7071583f9ebf65f73e50b518 [file] [log] [blame]
Eugene Zelenkofa57bd02017-09-27 23:26:01 +00001//===- StatepointLowering.h - SDAGBuilder's statepoint code ---*- C++ -*---===//
Philip Reames1a1bdb22014-12-02 18:50:36 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Philip Reames1a1bdb22014-12-02 18:50:36 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file includes support code use by SelectionDAGBuilder when lowering a
10// statepoint sequence in SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
15#define LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H
16
17#include "llvm/ADT/DenseMap.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000018#include "llvm/ADT/STLExtras.h"
Sanjoy Das171313c2016-02-19 17:15:26 +000019#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000020#include "llvm/ADT/SmallVector.h"
Philip Reames1a1bdb22014-12-02 18:50:36 +000021#include "llvm/CodeGen/SelectionDAGNodes.h"
Craig Topper2fa14362018-03-29 17:21:10 +000022#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000023#include <cassert>
Philip Reames1a1bdb22014-12-02 18:50:36 +000024
25namespace llvm {
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000026
27class CallInst;
Philip Reames1a1bdb22014-12-02 18:50:36 +000028class SelectionDAGBuilder;
29
30/// This class tracks both per-statepoint and per-selectiondag information.
31/// For each statepoint it tracks locations of it's gc valuess (incoming and
32/// relocated) and list of gcreloc calls scheduled for visiting (this is
33/// used for a debug mode consistency check only). The spill slot tracking
34/// works in concert with information in FunctionLoweringInfo.
35class StatepointLoweringState {
36public:
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000037 StatepointLoweringState() = default;
Philip Reames1a1bdb22014-12-02 18:50:36 +000038
39 /// Reset all state tracking for a newly encountered safepoint. Also
40 /// performs some consistency checking.
41 void startNewStatepoint(SelectionDAGBuilder &Builder);
42
43 /// Clear the memory usage of this object. This is called from
44 /// SelectionDAGBuilder::clear. We require this is never called in the
45 /// midst of processing a statepoint sequence.
46 void clear();
47
48 /// Returns the spill location of a value incoming to the current
49 /// statepoint. Will return SDValue() if this value hasn't been
50 /// spilled. Otherwise, the value has already been spilled and no
51 /// further action is required by the caller.
Sanjoy Das7edbef32016-03-23 02:24:13 +000052 SDValue getLocation(SDValue Val) {
Sanjoy Dasac53dc72016-03-23 02:24:15 +000053 auto I = Locations.find(Val);
54 if (I == Locations.end())
Philip Reames1a1bdb22014-12-02 18:50:36 +000055 return SDValue();
Sanjoy Dasac53dc72016-03-23 02:24:15 +000056 return I->second;
Philip Reames1a1bdb22014-12-02 18:50:36 +000057 }
Sanjoy Das7edbef32016-03-23 02:24:13 +000058
59 void setLocation(SDValue Val, SDValue Location) {
60 assert(!Locations.count(Val) &&
Philip Reames1a1bdb22014-12-02 18:50:36 +000061 "Trying to allocate already allocated location");
Sanjoy Das7edbef32016-03-23 02:24:13 +000062 Locations[Val] = Location;
Philip Reames1a1bdb22014-12-02 18:50:36 +000063 }
64
Philip Reames1a1bdb22014-12-02 18:50:36 +000065 /// Record the fact that we expect to encounter a given gc_relocate
66 /// before the next statepoint. If we don't see it, we'll report
67 /// an assertion.
68 void scheduleRelocCall(const CallInst &RelocCall) {
Serguei Katkovc39636c2019-04-05 05:41:08 +000069 // We are not interested in lowering dead instructions.
70 if (!RelocCall.use_empty())
71 PendingGCRelocateCalls.push_back(&RelocCall);
Philip Reames1a1bdb22014-12-02 18:50:36 +000072 }
Sanjoy Das7edbef32016-03-23 02:24:13 +000073
Philip Reames1a1bdb22014-12-02 18:50:36 +000074 /// Remove this gc_relocate from the list we're expecting to see
75 /// before the next statepoint. If we weren't expecting to see
76 /// it, we'll report an assertion.
77 void relocCallVisited(const CallInst &RelocCall) {
Serguei Katkovc39636c2019-04-05 05:41:08 +000078 // We are not interested in lowering dead instructions.
79 if (RelocCall.use_empty())
80 return;
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000081 auto I = llvm::find(PendingGCRelocateCalls, &RelocCall);
Sanjoy Das7edbef32016-03-23 02:24:13 +000082 assert(I != PendingGCRelocateCalls.end() &&
Philip Reames1a1bdb22014-12-02 18:50:36 +000083 "Visited unexpected gcrelocate call");
Sanjoy Das7edbef32016-03-23 02:24:13 +000084 PendingGCRelocateCalls.erase(I);
Philip Reames1a1bdb22014-12-02 18:50:36 +000085 }
86
87 // TODO: Should add consistency tracking to ensure we encounter
88 // expected gc_result calls too.
89
90 /// Get a stack slot we can use to store an value of type ValueType. This
91 /// will hopefully be a recylced slot from another statepoint.
92 SDValue allocateStackSlot(EVT ValueType, SelectionDAGBuilder &Builder);
93
94 void reserveStackSlot(int Offset) {
95 assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
96 "out of bounds");
Sanjoy Das171313c2016-02-19 17:15:26 +000097 assert(!AllocatedStackSlots.test(Offset) && "already reserved!");
Philip Reames1a1bdb22014-12-02 18:50:36 +000098 assert(NextSlotToAllocate <= (unsigned)Offset && "consistency!");
Sanjoy Das171313c2016-02-19 17:15:26 +000099 AllocatedStackSlots.set(Offset);
Philip Reames1a1bdb22014-12-02 18:50:36 +0000100 }
Sanjoy Das7edbef32016-03-23 02:24:13 +0000101
Philip Reames1a1bdb22014-12-02 18:50:36 +0000102 bool isStackSlotAllocated(int Offset) {
103 assert(Offset >= 0 && Offset < (int)AllocatedStackSlots.size() &&
104 "out of bounds");
Sanjoy Das171313c2016-02-19 17:15:26 +0000105 return AllocatedStackSlots.test(Offset);
Philip Reames1a1bdb22014-12-02 18:50:36 +0000106 }
107
108private:
109 /// Maps pre-relocation value (gc pointer directly incoming into statepoint)
110 /// into it's location (currently only stack slots)
111 DenseMap<SDValue, SDValue> Locations;
Philip Reames1a1bdb22014-12-02 18:50:36 +0000112
113 /// A boolean indicator for each slot listed in the FunctionInfo as to
114 /// whether it has been used in the current statepoint. Since we try to
115 /// preserve stack slots across safepoints, there can be gaps in which
116 /// slots have been allocated.
Sanjoy Das171313c2016-02-19 17:15:26 +0000117 SmallBitVector AllocatedStackSlots;
Philip Reames1a1bdb22014-12-02 18:50:36 +0000118
119 /// Points just beyond the last slot known to have been allocated
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000120 unsigned NextSlotToAllocate = 0;
Philip Reames1a1bdb22014-12-02 18:50:36 +0000121
122 /// Keep track of pending gcrelocate calls for consistency check
123 SmallVector<const CallInst *, 10> PendingGCRelocateCalls;
124};
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000125
Philip Reames1a1bdb22014-12-02 18:50:36 +0000126} // end namespace llvm
127
128#endif // LLVM_LIB_CODEGEN_SELECTIONDAG_STATEPOINTLOWERING_H