1 //===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains utility functions and a wrapper class analogous to
10 // CallBase for accessing the fields of gc.statepoint, gc.relocate,
11 // gc.result intrinsics; and some general utilities helpful when dealing with
12 // gc.statepoint.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_IR_STATEPOINT_H
17 #define LLVM_IR_STATEPOINT_H
18
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/MathExtras.h"
31 #include <cassert>
32 #include <cstddef>
33 #include <cstdint>
34 #include <vector>
35
36 namespace llvm {
37
38 /// The statepoint intrinsic accepts a set of flags as its third argument.
39 /// Valid values come out of this set.
40 enum class StatepointFlags {
41 None = 0,
42 GCTransition = 1, ///< Indicates that this statepoint is a transition from
43 ///< GC-aware code to code that is not GC-aware.
44 /// Mark the deopt arguments associated with the statepoint as only being
45 /// "live-in". By default, deopt arguments are "live-through". "live-through"
46 /// requires that they the value be live on entry, on exit, and at any point
47 /// during the call. "live-in" only requires the value be available at the
48 /// start of the call. In particular, "live-in" values can be placed in
49 /// unused argument registers or other non-callee saved registers.
50 DeoptLiveIn = 2,
51
52 MaskAll = 3 ///< A bitmask that includes all valid flags.
53 };
54
55 class GCRelocateInst;
56 class GCResultInst;
57
58 /// Represents a gc.statepoint intrinsic call. This extends directly from
59 /// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
60 /// invokable.
61 class GCStatepointInst : public CallBase {
62 public:
63 GCStatepointInst() = delete;
64 GCStatepointInst(const GCStatepointInst &) = delete;
65 GCStatepointInst &operator=(const GCStatepointInst &) = delete;
66
classof(const CallBase * I)67 static bool classof(const CallBase *I) {
68 if (const Function *CF = I->getCalledFunction())
69 return CF->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
70 return false;
71 }
72
classof(const Value * V)73 static bool classof(const Value *V) {
74 return isa<CallBase>(V) && classof(cast<CallBase>(V));
75 }
76
77 enum {
78 IDPos = 0,
79 NumPatchBytesPos = 1,
80 CalledFunctionPos = 2,
81 NumCallArgsPos = 3,
82 FlagsPos = 4,
83 CallArgsBeginPos = 5,
84 };
85
86 /// Return the ID associated with this statepoint.
getID()87 uint64_t getID() const {
88 return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
89 }
90
91 /// Return the number of patchable bytes associated with this statepoint.
getNumPatchBytes()92 uint32_t getNumPatchBytes() const {
93 const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
94 uint64_t NumPatchBytes =
95 cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
96 assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
97 return NumPatchBytes;
98 }
99
100 /// Number of arguments to be passed to the actual callee.
getNumCallArgs()101 int getNumCallArgs() const {
102 return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
103 }
104
getFlags()105 uint64_t getFlags() const {
106 return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
107 }
108
109 /// Return the value actually being called or invoked.
getActualCalledOperand()110 Value *getActualCalledOperand() const {
111 return getArgOperand(CalledFunctionPos);
112 }
113
114 /// Returns the function called if this is a wrapping a direct call, and null
115 /// otherwise.
getActualCalledFunction()116 Function *getActualCalledFunction() const {
117 return dyn_cast_or_null<Function>(getActualCalledOperand());
118 }
119
120 /// Return the type of the value returned by the call underlying the
121 /// statepoint.
getActualReturnType()122 Type *getActualReturnType() const {
123 auto *CalleeTy =
124 cast<PointerType>(getActualCalledOperand()->getType())->getElementType();
125 return cast<FunctionType>(CalleeTy)->getReturnType();
126 }
127
128
129 /// Return the number of arguments to the underlying call.
actual_arg_size()130 size_t actual_arg_size() const { return getNumCallArgs(); }
131 /// Return an iterator to the begining of the arguments to the underlying call
actual_arg_begin()132 const_op_iterator actual_arg_begin() const {
133 assert(CallArgsBeginPos <= (int)arg_size());
134 return arg_begin() + CallArgsBeginPos;
135 }
136 /// Return an end iterator of the arguments to the underlying call
actual_arg_end()137 const_op_iterator actual_arg_end() const {
138 auto I = actual_arg_begin() + actual_arg_size();
139 assert((arg_end() - I) == 2);
140 return I;
141 }
142 /// range adapter for actual call arguments
actual_args()143 iterator_range<const_op_iterator> actual_args() const {
144 return make_range(actual_arg_begin(), actual_arg_end());
145 }
146
gc_transition_args_begin()147 const_op_iterator gc_transition_args_begin() const {
148 if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
149 return Opt->Inputs.begin();
150 return arg_end();
151 }
gc_transition_args_end()152 const_op_iterator gc_transition_args_end() const {
153 if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
154 return Opt->Inputs.end();
155 return arg_end();
156 }
157
158 /// range adapter for GC transition arguments
gc_transition_args()159 iterator_range<const_op_iterator> gc_transition_args() const {
160 return make_range(gc_transition_args_begin(), gc_transition_args_end());
161 }
162
deopt_begin()163 const_op_iterator deopt_begin() const {
164 if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
165 return Opt->Inputs.begin();
166 return arg_end();
167 }
deopt_end()168 const_op_iterator deopt_end() const {
169 if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
170 return Opt->Inputs.end();
171 return arg_end();
172 }
173
174 /// range adapter for vm state arguments
deopt_operands()175 iterator_range<const_op_iterator> deopt_operands() const {
176 return make_range(deopt_begin(), deopt_end());
177 }
178
179 /// Returns an iterator to the begining of the argument range describing gc
180 /// values for the statepoint.
gc_args_begin()181 const_op_iterator gc_args_begin() const {
182 if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
183 return Opt->Inputs.begin();
184 return arg_end();
185 }
186
187 /// Return an end iterator for the gc argument range
gc_args_end()188 const_op_iterator gc_args_end() const {
189 if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
190 return Opt->Inputs.end();
191 return arg_end();
192 }
193
194 /// range adapter for gc arguments
gc_args()195 iterator_range<const_op_iterator> gc_args() const {
196 return make_range(gc_args_begin(), gc_args_end());
197 }
198
199
200 /// Get list of all gc reloactes linked to this statepoint
201 /// May contain several relocations for the same base/derived pair.
202 /// For example this could happen due to relocations on unwinding
203 /// path of invoke.
204 inline std::vector<const GCRelocateInst *> getGCRelocates() const;
205
206 /// Get the experimental_gc_result call tied to this statepoint if there is
207 /// one, otherwise return nullptr.
getGCResult()208 const GCResultInst *getGCResult() const {
209 for (auto *U : users())
210 if (auto *GRI = dyn_cast<GCResultInst>(U))
211 return GRI;
212 return nullptr;
213 }
214 };
215
216 /// Common base class for representing values projected from a statepoint.
217 /// Currently, the only projections available are gc.result and gc.relocate.
218 class GCProjectionInst : public IntrinsicInst {
219 public:
classof(const IntrinsicInst * I)220 static bool classof(const IntrinsicInst *I) {
221 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
222 I->getIntrinsicID() == Intrinsic::experimental_gc_result;
223 }
224
classof(const Value * V)225 static bool classof(const Value *V) {
226 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
227 }
228
229 /// Return true if this relocate is tied to the invoke statepoint.
230 /// This includes relocates which are on the unwinding path.
isTiedToInvoke()231 bool isTiedToInvoke() const {
232 const Value *Token = getArgOperand(0);
233
234 return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
235 }
236
237 /// The statepoint with which this gc.relocate is associated.
getStatepoint()238 const GCStatepointInst *getStatepoint() const {
239 const Value *Token = getArgOperand(0);
240
241 // This takes care both of relocates for call statepoints and relocates
242 // on normal path of invoke statepoint.
243 if (!isa<LandingPadInst>(Token))
244 return cast<GCStatepointInst>(Token);
245
246 // This relocate is on exceptional path of an invoke statepoint
247 const BasicBlock *InvokeBB =
248 cast<Instruction>(Token)->getParent()->getUniquePredecessor();
249
250 assert(InvokeBB && "safepoints should have unique landingpads");
251 assert(InvokeBB->getTerminator() &&
252 "safepoint block should be well formed");
253
254 return cast<GCStatepointInst>(InvokeBB->getTerminator());
255 }
256 };
257
258 /// Represents calls to the gc.relocate intrinsic.
259 class GCRelocateInst : public GCProjectionInst {
260 public:
classof(const IntrinsicInst * I)261 static bool classof(const IntrinsicInst *I) {
262 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
263 }
264
classof(const Value * V)265 static bool classof(const Value *V) {
266 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
267 }
268
269 /// The index into the associate statepoint's argument list
270 /// which contains the base pointer of the pointer whose
271 /// relocation this gc.relocate describes.
getBasePtrIndex()272 unsigned getBasePtrIndex() const {
273 return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
274 }
275
276 /// The index into the associate statepoint's argument list which
277 /// contains the pointer whose relocation this gc.relocate describes.
getDerivedPtrIndex()278 unsigned getDerivedPtrIndex() const {
279 return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
280 }
281
getBasePtr()282 Value *getBasePtr() const {
283 if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
284 return *(Opt->Inputs.begin() + getBasePtrIndex());
285 return *(getStatepoint()->arg_begin() + getBasePtrIndex());
286 }
287
getDerivedPtr()288 Value *getDerivedPtr() const {
289 if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
290 return *(Opt->Inputs.begin() + getDerivedPtrIndex());
291 return *(getStatepoint()->arg_begin() + getDerivedPtrIndex());
292 }
293 };
294
295 /// Represents calls to the gc.result intrinsic.
296 class GCResultInst : public GCProjectionInst {
297 public:
classof(const IntrinsicInst * I)298 static bool classof(const IntrinsicInst *I) {
299 return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
300 }
301
classof(const Value * V)302 static bool classof(const Value *V) {
303 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
304 }
305 };
306
getGCRelocates()307 std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
308 std::vector<const GCRelocateInst *> Result;
309
310 // Search for relocated pointers. Note that working backwards from the
311 // gc_relocates ensures that we only get pairs which are actually relocated
312 // and used after the statepoint.
313 for (const User *U : users())
314 if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
315 Result.push_back(Relocate);
316
317 auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
318 if (!StatepointInvoke)
319 return Result;
320
321 // We need to scan thorough exceptional relocations if it is invoke statepoint
322 LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
323
324 // Search for gc relocates that are attached to this landingpad.
325 for (const User *LandingPadUser : LandingPad->users()) {
326 if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
327 Result.push_back(Relocate);
328 }
329 return Result;
330 }
331
332 /// Call sites that get wrapped by a gc.statepoint (currently only in
333 /// RewriteStatepointsForGC and potentially in other passes in the future) can
334 /// have attributes that describe properties of gc.statepoint call they will be
335 /// eventually be wrapped in. This struct is used represent such directives.
336 struct StatepointDirectives {
337 Optional<uint32_t> NumPatchBytes;
338 Optional<uint64_t> StatepointID;
339
340 static const uint64_t DefaultStatepointID = 0xABCDEF00;
341 static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
342 };
343
344 /// Parse out statepoint directives from the function attributes present in \p
345 /// AS.
346 StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
347
348 /// Return \c true if the \p Attr is an attribute that is a statepoint
349 /// directive.
350 bool isStatepointDirectiveAttr(Attribute Attr);
351
352 } // end namespace llvm
353
354 #endif // LLVM_IR_STATEPOINT_H
355