LLVM  16.0.0git
Statepoint.h
Go to the documentation of this file.
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 
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/InstrTypes.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/Support/Casting.h"
29 #include <cassert>
30 #include <cstddef>
31 #include <cstdint>
32 #include <optional>
33 #include <vector>
34 
35 namespace llvm {
36 
37 /// The statepoint intrinsic accepts a set of flags as its third argument.
38 /// Valid values come out of this set.
39 enum class StatepointFlags {
40  None = 0,
41  GCTransition = 1, ///< Indicates that this statepoint is a transition from
42  ///< GC-aware code to code that is not GC-aware.
43  /// Mark the deopt arguments associated with the statepoint as only being
44  /// "live-in". By default, deopt arguments are "live-through". "live-through"
45  /// requires that they the value be live on entry, on exit, and at any point
46  /// during the call. "live-in" only requires the value be available at the
47  /// start of the call. In particular, "live-in" values can be placed in
48  /// unused argument registers or other non-callee saved registers.
49  DeoptLiveIn = 2,
50 
51  MaskAll = 3 ///< A bitmask that includes all valid flags.
52 };
53 
54 // These two are defined in IntrinsicInst since they're part of the
55 // IntrinsicInst class hierarchy.
56 class GCRelocateInst;
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 
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  }
76 
77  enum {
78  IDPos = 0,
82  FlagsPos = 4,
84  };
85 
86  /// Return the ID associated with this statepoint.
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.
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.
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 
109  /// Return the value actually being called or invoked.
112  }
113 
114  /// Returns the function called if this is a wrapping a direct call, and null
115  /// otherwise.
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.
123  auto *FT = cast<FunctionType>(getParamElementType(CalledFunctionPos));
124  return FT->getReturnType();
125  }
126 
127 
128  /// Return the number of arguments to the underlying call.
129  size_t actual_arg_size() const { return getNumCallArgs(); }
130  /// Return an iterator to the begining of the arguments to the underlying call
132  assert(CallArgsBeginPos <= (int)arg_size());
133  return arg_begin() + CallArgsBeginPos;
134  }
135  /// Return an end iterator of the arguments to the underlying call
137  auto I = actual_arg_begin() + actual_arg_size();
138  assert((arg_end() - I) == 2);
139  return I;
140  }
141  /// range adapter for actual call arguments
144  }
145 
148  return Opt->Inputs.begin();
149  return arg_end();
150  }
153  return Opt->Inputs.end();
154  return arg_end();
155  }
156 
157  /// range adapter for GC transition arguments
160  }
161 
163  if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
164  return Opt->Inputs.begin();
165  return arg_end();
166  }
168  if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
169  return Opt->Inputs.end();
170  return arg_end();
171  }
172 
173  /// range adapter for vm state arguments
175  return make_range(deopt_begin(), deopt_end());
176  }
177 
178  /// Returns an iterator to the begining of the argument range describing gc
179  /// values for the statepoint.
182  return Opt->Inputs.begin();
183  return arg_end();
184  }
185 
186  /// Return an end iterator for the gc argument range
189  return Opt->Inputs.end();
190  return arg_end();
191  }
192 
193  /// range adapter for gc arguments
195  return make_range(gc_args_begin(), gc_args_end());
196  }
197 
198 
199  /// Get list of all gc reloactes linked to this statepoint
200  /// May contain several relocations for the same base/derived pair.
201  /// For example this could happen due to relocations on unwinding
202  /// path of invoke.
203  inline std::vector<const GCRelocateInst *> getGCRelocates() const;
204 };
205 
206 std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
207  std::vector<const GCRelocateInst *> Result;
208 
209  // Search for relocated pointers. Note that working backwards from the
210  // gc_relocates ensures that we only get pairs which are actually relocated
211  // and used after the statepoint.
212  for (const User *U : users())
213  if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
214  Result.push_back(Relocate);
215 
216  auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
217  if (!StatepointInvoke)
218  return Result;
219 
220  // We need to scan thorough exceptional relocations if it is invoke statepoint
221  LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
222 
223  // Search for gc relocates that are attached to this landingpad.
224  for (const User *LandingPadUser : LandingPad->users()) {
225  if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
226  Result.push_back(Relocate);
227  }
228  return Result;
229 }
230 
231 /// Call sites that get wrapped by a gc.statepoint (currently only in
232 /// RewriteStatepointsForGC and potentially in other passes in the future) can
233 /// have attributes that describe properties of gc.statepoint call they will be
234 /// eventually be wrapped in. This struct is used represent such directives.
236  std::optional<uint32_t> NumPatchBytes;
237  std::optional<uint64_t> StatepointID;
238 
239  static const uint64_t DefaultStatepointID = 0xABCDEF00;
240  static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
241 };
242 
243 /// Parse out statepoint directives from the function attributes present in \p
244 /// AS.
246 
247 /// Return \c true if the \p Attr is an attribute that is a statepoint
248 /// directive.
250 
251 } // end namespace llvm
252 
253 #endif // LLVM_IR_STATEPOINT_H
llvm::LLVMContext::OB_deopt
@ OB_deopt
Definition: LLVMContext.h:89
llvm::GCStatepointInst::NumCallArgsPos
@ NumCallArgsPos
Definition: Statepoint.h:81
llvm::GCStatepointInst::getID
uint64_t getID() const
Return the ID associated with this statepoint.
Definition: Statepoint.h:87
llvm::StatepointDirectives::StatepointID
std::optional< uint64_t > StatepointID
Definition: Statepoint.h:237
MathExtras.h
llvm::LLVMContext::OB_gc_transition
@ OB_gc_transition
Definition: LLVMContext.h:91
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::GCStatepointInst::IDPos
@ IDPos
Definition: Statepoint.h:78
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
IntrinsicInst.h
llvm::GCStatepointInst::getFlags
uint64_t getFlags() const
Definition: Statepoint.h:105
llvm::Function
Definition: Function.h:60
llvm::Attribute
Definition: Attributes.h:67
llvm::LandingPadInst
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Definition: Instructions.h:2949
llvm::GCStatepointInst::gc_transition_args
iterator_range< const_op_iterator > gc_transition_args() const
range adapter for GC transition arguments
Definition: Statepoint.h:158
llvm::GCStatepointInst::getNumPatchBytes
uint32_t getNumPatchBytes() const
Return the number of patchable bytes associated with this statepoint.
Definition: Statepoint.h:92
llvm::GCStatepointInst::gc_transition_args_end
const_op_iterator gc_transition_args_end() const
Definition: Statepoint.h:151
llvm::GCStatepointInst::deopt_end
const_op_iterator deopt_end() const
Definition: Statepoint.h:167
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::GCStatepointInst::deopt_operands
iterator_range< const_op_iterator > deopt_operands() const
range adapter for vm state arguments
Definition: Statepoint.h:174
llvm::AttributeList
Definition: Attributes.h:432
llvm::GCStatepointInst::gc_args_begin
const_op_iterator gc_args_begin() const
Returns an iterator to the begining of the argument range describing gc values for the statepoint.
Definition: Statepoint.h:180
llvm::CallBase::arg_begin
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1316
llvm::GCStatepointInst::getActualCalledOperand
Value * getActualCalledOperand() const
Return the value actually being called or invoked.
Definition: Statepoint.h:110
llvm::GCStatepointInst::GCStatepointInst
GCStatepointInst()=delete
llvm::GCStatepointInst::CallArgsBeginPos
@ CallArgsBeginPos
Definition: Statepoint.h:83
llvm::GCStatepointInst
Represents a gc.statepoint intrinsic call.
Definition: Statepoint.h:61
Constants.h
llvm::User
Definition: User.h:44
Intrinsics.h
InstrTypes.h
llvm::StatepointDirectives::DeoptBundleStatepointID
static const uint64_t DeoptBundleStatepointID
Definition: Statepoint.h:240
llvm::GCStatepointInst::deopt_begin
const_op_iterator deopt_begin() const
Definition: Statepoint.h:162
llvm::GCStatepointInst::classof
static bool classof(const Value *V)
Definition: Statepoint.h:73
llvm::GCStatepointInst::FlagsPos
@ FlagsPos
Definition: Statepoint.h:82
llvm::GCStatepointInst::CalledFunctionPos
@ CalledFunctionPos
Definition: Statepoint.h:80
llvm::LLVMContext::OB_gc_live
@ OB_gc_live
Definition: LLVMContext.h:94
llvm::GCStatepointInst::gc_args_end
const_op_iterator gc_args_end() const
Return an end iterator for the gc argument range.
Definition: Statepoint.h:187
llvm::GCStatepointInst::getActualCalledFunction
Function * getActualCalledFunction() const
Returns the function called if this is a wrapping a direct call, and null otherwise.
Definition: Statepoint.h:116
llvm::CallBase::getParamElementType
Type * getParamElementType(unsigned ArgNo) const
Extract the elementtype type for a parameter.
Definition: InstrTypes.h:1789
llvm::GCStatepointInst::getGCRelocates
std::vector< const GCRelocateInst * > getGCRelocates() const
Get list of all gc reloactes linked to this statepoint May contain several relocations for the same b...
Definition: Statepoint.h:206
llvm::isStatepointDirectiveAttr
bool isStatepointDirectiveAttr(Attribute Attr)
Return true if the Attr is an attribute that is a statepoint directive.
Definition: Statepoint.cpp:18
llvm::GCStatepointInst::operator=
GCStatepointInst & operator=(const GCStatepointInst &)=delete
llvm::StatepointFlags::None
@ None
uint64_t
llvm::GCStatepointInst::actual_arg_size
size_t actual_arg_size() const
Return the number of arguments to the underlying call.
Definition: Statepoint.h:129
llvm::StatepointDirectives
Call sites that get wrapped by a gc.statepoint (currently only in RewriteStatepointsForGC and potenti...
Definition: Statepoint.h:235
I
#define I(x, y, z)
Definition: MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::GCStatepointInst::actual_args
iterator_range< const_op_iterator > actual_args() const
range adapter for actual call arguments
Definition: Statepoint.h:142
llvm::StatepointDirectives::NumPatchBytes
std::optional< uint32_t > NumPatchBytes
Definition: Statepoint.h:236
iterator_range.h
llvm::CallBase::arg_end
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1322
llvm::GCStatepointInst::actual_arg_end
const_op_iterator actual_arg_end() const
Return an end iterator of the arguments to the underlying call.
Definition: Statepoint.h:136
llvm::CallBase::getOperandBundle
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2017
uint32_t
llvm::GCStatepointInst::classof
static bool classof(const CallBase *I)
Definition: Statepoint.h:67
llvm::StatepointFlags::GCTransition
@ GCTransition
Indicates that this statepoint is a transition from GC-aware code to code that is not GC-aware.
Attributes.h
llvm::CallBase::arg_size
unsigned arg_size() const
Definition: InstrTypes.h:1339
Casting.h
Function.h
llvm::StatepointFlags
StatepointFlags
The statepoint intrinsic accepts a set of flags as its third argument.
Definition: Statepoint.h:39
llvm::GCStatepointInst::getActualReturnType
Type * getActualReturnType() const
Return the type of the value returned by the call underlying the statepoint.
Definition: Statepoint.h:122
Instructions.h
llvm::parseStatepointDirectivesFromAttrs
StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS)
Parse out statepoint directives from the function attributes present in AS.
Definition: Statepoint.cpp:24
llvm::StatepointDirectives::DefaultStatepointID
static const uint64_t DefaultStatepointID
Definition: Statepoint.h:239
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1341
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::GCStatepointInst::gc_transition_args_begin
const_op_iterator gc_transition_args_begin() const
Definition: Statepoint.h:146
llvm::GCStatepointInst::gc_args
iterator_range< const_op_iterator > gc_args() const
range adapter for gc arguments
Definition: Statepoint.h:194
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::StatepointFlags::DeoptLiveIn
@ DeoptLiveIn
Mark the deopt arguments associated with the statepoint as only being "live-in".
llvm::GCStatepointInst::NumPatchBytesPos
@ NumPatchBytesPos
Definition: Statepoint.h:79
llvm::StatepointFlags::MaskAll
@ MaskAll
A bitmask that includes all valid flags.
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::GCStatepointInst::actual_arg_begin
const_op_iterator actual_arg_begin() const
Return an iterator to the begining of the arguments to the underlying call.
Definition: Statepoint.h:131
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:421
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
llvm::GCStatepointInst::getNumCallArgs
int getNumCallArgs() const
Number of arguments to be passed to the actual callee.
Definition: Statepoint.h:101