blob: 6ce15839df46879a371432e0fca569b2bdc9f4a7 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains utility functions and a wrapper class analogous to
Andrew Walbran16937d02019-10-22 13:54:20 +010010// CallBase for accessing the fields of gc.statepoint, gc.relocate,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011// 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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023#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
36namespace llvm {
37
38/// The statepoint intrinsic accepts a set of flags as its third argument.
39/// Valid values come out of this set.
40enum 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
55class GCRelocateInst;
56class GCResultInst;
57
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020058/// Represents a gc.statepoint intrinsic call. This extends directly from
59/// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
60/// invokable.
61class GCStatepointInst : public CallBase {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020063 GCStatepointInst() = delete;
64 GCStatepointInst(const GCStatepointInst &) = delete;
65 GCStatepointInst &operator=(const GCStatepointInst &) = delete;
66
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
73 static bool classof(const Value *V) {
74 return isa<CallBase>(V) && classof(cast<CallBase>(V));
75 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010076
77 enum {
78 IDPos = 0,
79 NumPatchBytesPos = 1,
80 CalledFunctionPos = 2,
81 NumCallArgsPos = 3,
82 FlagsPos = 4,
83 CallArgsBeginPos = 5,
84 };
85
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086 /// Return the ID associated with this statepoint.
87 uint64_t getID() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020088 return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 }
90
91 /// Return the number of patchable bytes associated with this statepoint.
92 uint32_t getNumPatchBytes() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020093 const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094 uint64_t NumPatchBytes =
95 cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
96 assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
97 return NumPatchBytes;
98 }
99
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200100 /// Number of arguments to be passed to the actual callee.
101 int getNumCallArgs() const {
102 return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
103 }
104
105 uint64_t getFlags() const {
106 return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
107 }
108
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 /// Return the value actually being called or invoked.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200110 Value *getActualCalledOperand() const {
111 return getArgOperand(CalledFunctionPos);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100112 }
113
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200114 /// Returns the function called if this is a wrapping a direct call, and null
115 /// otherwise.
116 Function *getActualCalledFunction() const {
117 return dyn_cast_or_null<Function>(getActualCalledOperand());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 }
119
120 /// Return the type of the value returned by the call underlying the
121 /// statepoint.
122 Type *getActualReturnType() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200123 auto *CalleeTy =
124 cast<PointerType>(getActualCalledOperand()->getType())->getElementType();
125 return cast<FunctionType>(CalleeTy)->getReturnType();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126 }
127
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200129 /// Return the number of arguments to the underlying call.
130 size_t actual_arg_size() const { return getNumCallArgs(); }
131 /// Return an iterator to the begining of the arguments to the underlying call
132 const_op_iterator actual_arg_begin() const {
133 assert(CallArgsBeginPos <= (int)arg_size());
134 return arg_begin() + CallArgsBeginPos;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200136 /// Return an end iterator of the arguments to the underlying call
137 const_op_iterator actual_arg_end() const {
138 auto I = actual_arg_begin() + actual_arg_size();
139 assert((arg_end() - I) == 2);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140 return I;
141 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200142 /// range adapter for actual call arguments
143 iterator_range<const_op_iterator> actual_args() const {
144 return make_range(actual_arg_begin(), actual_arg_end());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100145 }
146
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200147 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();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200152 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();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 }
157
158 /// range adapter for GC transition arguments
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200159 iterator_range<const_op_iterator> gc_transition_args() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100160 return make_range(gc_transition_args_begin(), gc_transition_args_end());
161 }
162
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200163 const_op_iterator deopt_begin() const {
164 if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
165 return Opt->Inputs.begin();
166 return arg_end();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200168 const_op_iterator deopt_end() const {
169 if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
170 return Opt->Inputs.end();
171 return arg_end();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100172 }
173
174 /// range adapter for vm state arguments
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200175 iterator_range<const_op_iterator> deopt_operands() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100176 return make_range(deopt_begin(), deopt_end());
177 }
178
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200179 /// Returns an iterator to the begining of the argument range describing gc
180 /// values for the statepoint.
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 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200187 /// Return an end iterator for the gc argument range
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();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100192 }
193
194 /// range adapter for gc arguments
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200195 iterator_range<const_op_iterator> gc_args() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100196 return make_range(gc_args_begin(), gc_args_end());
197 }
198
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200199
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100200 /// 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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200204 inline std::vector<const GCRelocateInst *> getGCRelocates() const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100205
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200206 /// Get the experimental_gc_result call tied to this statepoint if there is
207 /// one, otherwise return nullptr.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 const GCResultInst *getGCResult() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200209 for (auto *U : users())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100210 if (auto *GRI = dyn_cast<GCResultInst>(U))
211 return GRI;
212 return nullptr;
213 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100214};
215
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100216/// Common base class for representing values projected from a statepoint.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217/// Currently, the only projections available are gc.result and gc.relocate.
218class GCProjectionInst : public IntrinsicInst {
219public:
220 static bool classof(const IntrinsicInst *I) {
221 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
222 I->getIntrinsicID() == Intrinsic::experimental_gc_result;
223 }
224
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.
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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200238 const GCStatepointInst *getStatepoint() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100239 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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200243 if (!isa<LandingPadInst>(Token))
244 return cast<GCStatepointInst>(Token);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100245
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");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100253
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200254 return cast<GCStatepointInst>(InvokeBB->getTerminator());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100255 }
256};
257
258/// Represents calls to the gc.relocate intrinsic.
259class GCRelocateInst : public GCProjectionInst {
260public:
261 static bool classof(const IntrinsicInst *I) {
262 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
263 }
264
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.
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.
278 unsigned getDerivedPtrIndex() const {
279 return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
280 }
281
282 Value *getBasePtr() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200283 if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
284 return *(Opt->Inputs.begin() + getBasePtrIndex());
Andrew Walbran16937d02019-10-22 13:54:20 +0100285 return *(getStatepoint()->arg_begin() + getBasePtrIndex());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286 }
287
288 Value *getDerivedPtr() const {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200289 if (auto Opt = getStatepoint()->getOperandBundle(LLVMContext::OB_gc_live))
290 return *(Opt->Inputs.begin() + getDerivedPtrIndex());
Andrew Walbran16937d02019-10-22 13:54:20 +0100291 return *(getStatepoint()->arg_begin() + getDerivedPtrIndex());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100292 }
293};
294
295/// Represents calls to the gc.result intrinsic.
296class GCResultInst : public GCProjectionInst {
297public:
298 static bool classof(const IntrinsicInst *I) {
299 return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
300 }
301
302 static bool classof(const Value *V) {
303 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
304 }
305};
306
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200307std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100308 std::vector<const GCRelocateInst *> Result;
309
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100310 // 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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200313 for (const User *U : users())
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100314 if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
315 Result.push_back(Relocate);
316
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200317 auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
Andrew Walbran16937d02019-10-22 13:54:20 +0100318 if (!StatepointInvoke)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100319 return Result;
320
321 // We need to scan thorough exceptional relocations if it is invoke statepoint
Andrew Walbran16937d02019-10-22 13:54:20 +0100322 LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100323
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.
336struct 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.
346StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
347
348/// Return \c true if the \p Attr is an attribute that is a statepoint
349/// directive.
350bool isStatepointDirectiveAttr(Attribute Attr);
351
352} // end namespace llvm
353
354#endif // LLVM_IR_STATEPOINT_H