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 
19 #include "llvm/ADT/Optional.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/Support/Casting.h"
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdint>
33 #include <optional>
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 // These two are defined in IntrinsicInst since they're part of the
56 // IntrinsicInst class hierarchy.
57 class GCRelocateInst;
58 
59 /// Represents a gc.statepoint intrinsic call. This extends directly from
60 /// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
61 /// invokable.
62 class GCStatepointInst : public CallBase {
63 public:
64  GCStatepointInst() = delete;
65  GCStatepointInst(const GCStatepointInst &) = delete;
66  GCStatepointInst &operator=(const GCStatepointInst &) = delete;
67 
68  static bool classof(const CallBase *I) {
69  if (const Function *CF = I->getCalledFunction())
70  return CF->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
71  return false;
72  }
73 
74  static bool classof(const Value *V) {
75  return isa<CallBase>(V) && classof(cast<CallBase>(V));
76  }
77 
78  enum {
79  IDPos = 0,
83  FlagsPos = 4,
85  };
86 
87  /// Return the ID associated with this statepoint.
88  uint64_t getID() const {
89  return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
90  }
91 
92  /// Return the number of patchable bytes associated with this statepoint.
94  const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
95  uint64_t NumPatchBytes =
96  cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
97  assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
98  return NumPatchBytes;
99  }
100 
101  /// Number of arguments to be passed to the actual callee.
102  int getNumCallArgs() const {
103  return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
104  }
105 
106  uint64_t getFlags() const {
107  return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
108  }
109 
110  /// Return the value actually being called or invoked.
113  }
114 
115  /// Returns the function called if this is a wrapping a direct call, and null
116  /// otherwise.
118  return dyn_cast_or_null<Function>(getActualCalledOperand());
119  }
120 
121  /// Return the type of the value returned by the call underlying the
122  /// statepoint.
124  auto *FT = cast<FunctionType>(getParamElementType(CalledFunctionPos));
125  return FT->getReturnType();
126  }
127 
128 
129  /// 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
133  assert(CallArgsBeginPos <= (int)arg_size());
134  return arg_begin() + CallArgsBeginPos;
135  }
136  /// Return an end iterator of the arguments to the underlying call
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
145  }
146 
149  return Opt->Inputs.begin();
150  return arg_end();
151  }
154  return Opt->Inputs.end();
155  return arg_end();
156  }
157 
158  /// range adapter for GC transition arguments
161  }
162 
164  if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
165  return Opt->Inputs.begin();
166  return arg_end();
167  }
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
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.
183  return Opt->Inputs.begin();
184  return arg_end();
185  }
186 
187  /// Return an end iterator for the gc argument range
190  return Opt->Inputs.end();
191  return arg_end();
192  }
193 
194  /// range adapter for gc arguments
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 
207 std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
208  std::vector<const GCRelocateInst *> Result;
209 
210  // Search for relocated pointers. Note that working backwards from the
211  // gc_relocates ensures that we only get pairs which are actually relocated
212  // and used after the statepoint.
213  for (const User *U : users())
214  if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
215  Result.push_back(Relocate);
216 
217  auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
218  if (!StatepointInvoke)
219  return Result;
220 
221  // We need to scan thorough exceptional relocations if it is invoke statepoint
222  LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
223 
224  // Search for gc relocates that are attached to this landingpad.
225  for (const User *LandingPadUser : LandingPad->users()) {
226  if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
227  Result.push_back(Relocate);
228  }
229  return Result;
230 }
231 
232 /// Call sites that get wrapped by a gc.statepoint (currently only in
233 /// RewriteStatepointsForGC and potentially in other passes in the future) can
234 /// have attributes that describe properties of gc.statepoint call they will be
235 /// eventually be wrapped in. This struct is used represent such directives.
237  std::optional<uint32_t> NumPatchBytes;
238  std::optional<uint64_t> StatepointID;
239 
240  static const uint64_t DefaultStatepointID = 0xABCDEF00;
241  static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
242 };
243 
244 /// Parse out statepoint directives from the function attributes present in \p
245 /// AS.
247 
248 /// Return \c true if the \p Attr is an attribute that is a statepoint
249 /// directive.
251 
252 } // end namespace llvm
253 
254 #endif // LLVM_IR_STATEPOINT_H
llvm::LLVMContext::OB_deopt
@ OB_deopt
Definition: LLVMContext.h:89
llvm::GCStatepointInst::NumCallArgsPos
@ NumCallArgsPos
Definition: Statepoint.h:82
llvm::GCStatepointInst::getID
uint64_t getID() const
Return the ID associated with this statepoint.
Definition: Statepoint.h:88
llvm::StatepointDirectives::StatepointID
std::optional< uint64_t > StatepointID
Definition: Statepoint.h:238
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:79
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
Optional.h
IntrinsicInst.h
llvm::GCStatepointInst::getFlags
uint64_t getFlags() const
Definition: Statepoint.h:106
llvm::Function
Definition: Function.h:60
llvm::Attribute
Definition: Attributes.h:66
llvm::LandingPadInst
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Definition: Instructions.h:2951
llvm::GCStatepointInst::gc_transition_args
iterator_range< const_op_iterator > gc_transition_args() const
range adapter for GC transition arguments
Definition: Statepoint.h:159
llvm::GCStatepointInst::getNumPatchBytes
uint32_t getNumPatchBytes() const
Return the number of patchable bytes associated with this statepoint.
Definition: Statepoint.h:93
llvm::GCStatepointInst::gc_transition_args_end
const_op_iterator gc_transition_args_end() const
Definition: Statepoint.h:152
llvm::GCStatepointInst::deopt_end
const_op_iterator deopt_end() const
Definition: Statepoint.h:168
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:175
llvm::AttributeList
Definition: Attributes.h:430
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:181
llvm::CallBase::arg_begin
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1317
llvm::GCStatepointInst::getActualCalledOperand
Value * getActualCalledOperand() const
Return the value actually being called or invoked.
Definition: Statepoint.h:111
llvm::GCStatepointInst::GCStatepointInst
GCStatepointInst()=delete
llvm::GCStatepointInst::CallArgsBeginPos
@ CallArgsBeginPos
Definition: Statepoint.h:84
llvm::GCStatepointInst
Represents a gc.statepoint intrinsic call.
Definition: Statepoint.h:62
Constants.h
llvm::User
Definition: User.h:44
Intrinsics.h
InstrTypes.h
llvm::StatepointDirectives::DeoptBundleStatepointID
static const uint64_t DeoptBundleStatepointID
Definition: Statepoint.h:241
llvm::GCStatepointInst::deopt_begin
const_op_iterator deopt_begin() const
Definition: Statepoint.h:163
llvm::GCStatepointInst::classof
static bool classof(const Value *V)
Definition: Statepoint.h:74
llvm::GCStatepointInst::FlagsPos
@ FlagsPos
Definition: Statepoint.h:83
llvm::GCStatepointInst::CalledFunctionPos
@ CalledFunctionPos
Definition: Statepoint.h:81
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:188
llvm::GCStatepointInst::getActualCalledFunction
Function * getActualCalledFunction() const
Returns the function called if this is a wrapping a direct call, and null otherwise.
Definition: Statepoint.h:117
llvm::CallBase::getParamElementType
Type * getParamElementType(unsigned ArgNo) const
Extract the elementtype type for a parameter.
Definition: InstrTypes.h:1790
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:207
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:130
llvm::StatepointDirectives
Call sites that get wrapped by a gc.statepoint (currently only in RewriteStatepointsForGC and potenti...
Definition: Statepoint.h:236
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:143
llvm::StatepointDirectives::NumPatchBytes
std::optional< uint32_t > NumPatchBytes
Definition: Statepoint.h:237
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:1323
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:137
llvm::CallBase::getOperandBundle
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2018
uint32_t
llvm::GCStatepointInst::classof
static bool classof(const CallBase *I)
Definition: Statepoint.h:68
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:1340
Casting.h
Function.h
llvm::StatepointFlags
StatepointFlags
The statepoint intrinsic accepts a set of flags as its third argument.
Definition: Statepoint.h:40
llvm::GCStatepointInst::getActualReturnType
Type * getActualReturnType() const
Return the type of the value returned by the call underlying the statepoint.
Definition: Statepoint.h:123
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:240
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1342
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:147
llvm::GCStatepointInst::gc_args
iterator_range< const_op_iterator > gc_args() const
range adapter for gc arguments
Definition: Statepoint.h:195
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1175
llvm::StatepointFlags::DeoptLiveIn
@ DeoptLiveIn
Mark the deopt arguments associated with the statepoint as only being "live-in".
llvm::GCStatepointInst::NumPatchBytesPos
@ NumPatchBytesPos
Definition: Statepoint.h:80
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:132
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:102