LLVM  14.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/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"
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 // These two are defined in IntrinsicInst since they're part of the
56 // IntrinsicInst class hierarchy.
57 class GCRelocateInst;
58 class GCResultInst;
59 
60 /// Represents a gc.statepoint intrinsic call. This extends directly from
61 /// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
62 /// invokable.
63 class GCStatepointInst : public CallBase {
64 public:
65  GCStatepointInst() = delete;
66  GCStatepointInst(const GCStatepointInst &) = delete;
67  GCStatepointInst &operator=(const GCStatepointInst &) = delete;
68 
69  static bool classof(const CallBase *I) {
70  if (const Function *CF = I->getCalledFunction())
71  return CF->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
72  return false;
73  }
74 
75  static bool classof(const Value *V) {
76  return isa<CallBase>(V) && classof(cast<CallBase>(V));
77  }
78 
79  enum {
80  IDPos = 0,
84  FlagsPos = 4,
86  };
87 
88  /// Return the ID associated with this statepoint.
89  uint64_t getID() const {
90  return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
91  }
92 
93  /// Return the number of patchable bytes associated with this statepoint.
95  const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
96  uint64_t NumPatchBytes =
97  cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
98  assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
99  return NumPatchBytes;
100  }
101 
102  /// Number of arguments to be passed to the actual callee.
103  int getNumCallArgs() const {
104  return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
105  }
106 
107  uint64_t getFlags() const {
108  return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
109  }
110 
111  /// Return the value actually being called or invoked.
114  }
115 
116  /// Returns the function called if this is a wrapping a direct call, and null
117  /// otherwise.
119  return dyn_cast_or_null<Function>(getActualCalledOperand());
120  }
121 
122  /// Return the type of the value returned by the call underlying the
123  /// statepoint.
125  auto *CalleeTy =
126  cast<PointerType>(getActualCalledOperand()->getType())->getElementType();
127  return cast<FunctionType>(CalleeTy)->getReturnType();
128  }
129 
130 
131  /// Return the number of arguments to the underlying call.
132  size_t actual_arg_size() const { return getNumCallArgs(); }
133  /// Return an iterator to the begining of the arguments to the underlying call
135  assert(CallArgsBeginPos <= (int)arg_size());
136  return arg_begin() + CallArgsBeginPos;
137  }
138  /// Return an end iterator of the arguments to the underlying call
140  auto I = actual_arg_begin() + actual_arg_size();
141  assert((arg_end() - I) == 2);
142  return I;
143  }
144  /// range adapter for actual call arguments
147  }
148 
151  return Opt->Inputs.begin();
152  return arg_end();
153  }
156  return Opt->Inputs.end();
157  return arg_end();
158  }
159 
160  /// range adapter for GC transition arguments
163  }
164 
166  if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
167  return Opt->Inputs.begin();
168  return arg_end();
169  }
171  if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
172  return Opt->Inputs.end();
173  return arg_end();
174  }
175 
176  /// range adapter for vm state arguments
178  return make_range(deopt_begin(), deopt_end());
179  }
180 
181  /// Returns an iterator to the begining of the argument range describing gc
182  /// values for the statepoint.
185  return Opt->Inputs.begin();
186  return arg_end();
187  }
188 
189  /// Return an end iterator for the gc argument range
192  return Opt->Inputs.end();
193  return arg_end();
194  }
195 
196  /// range adapter for gc arguments
198  return make_range(gc_args_begin(), gc_args_end());
199  }
200 
201 
202  /// Get list of all gc reloactes linked to this statepoint
203  /// May contain several relocations for the same base/derived pair.
204  /// For example this could happen due to relocations on unwinding
205  /// path of invoke.
206  inline std::vector<const GCRelocateInst *> getGCRelocates() const;
207 
208  /// Returns pair of boolean flags. The first one is true is there is
209  /// a gc.result intrinsic in the same block as statepoint. The second flag
210  /// is true if there is an intrinsic outside of the block with statepoint.
211  inline std::pair<bool, bool> getGCResultLocality() const;
212 };
213 
214 std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
215  std::vector<const GCRelocateInst *> Result;
216 
217  // Search for relocated pointers. Note that working backwards from the
218  // gc_relocates ensures that we only get pairs which are actually relocated
219  // and used after the statepoint.
220  for (const User *U : users())
221  if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
222  Result.push_back(Relocate);
223 
224  auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
225  if (!StatepointInvoke)
226  return Result;
227 
228  // We need to scan thorough exceptional relocations if it is invoke statepoint
229  LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
230 
231  // Search for gc relocates that are attached to this landingpad.
232  for (const User *LandingPadUser : LandingPad->users()) {
233  if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
234  Result.push_back(Relocate);
235  }
236  return Result;
237 }
238 
239 std::pair<bool, bool> GCStatepointInst::getGCResultLocality() const {
240  std::pair<bool, bool> Res(false, false);
241  for (auto *U : users())
242  if (auto *GRI = dyn_cast<GCResultInst>(U)) {
243  if (GRI->getParent() == this->getParent())
244  Res.first = true;
245  else
246  Res.second = true;
247  }
248  return Res;
249 }
250 
251 /// Call sites that get wrapped by a gc.statepoint (currently only in
252 /// RewriteStatepointsForGC and potentially in other passes in the future) can
253 /// have attributes that describe properties of gc.statepoint call they will be
254 /// eventually be wrapped in. This struct is used represent such directives.
258 
259  static const uint64_t DefaultStatepointID = 0xABCDEF00;
260  static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
261 };
262 
263 /// Parse out statepoint directives from the function attributes present in \p
264 /// AS.
266 
267 /// Return \c true if the \p Attr is an attribute that is a statepoint
268 /// directive.
270 
271 } // end namespace llvm
272 
273 #endif // LLVM_IR_STATEPOINT_H
llvm::GCStatepointInst::getID
uint64_t getID() const
Return the ID associated with this statepoint.
Definition: Statepoint.h:89
MathExtras.h
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::CallBase::getOperandBundle
Optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:1977
Optional.h
IntrinsicInst.h
llvm::GCStatepointInst::getFlags
uint64_t getFlags() const
Definition: Statepoint.h:107
llvm::Function
Definition: Function.h:62
llvm::Attribute
Definition: Attributes.h:52
llvm::LandingPadInst
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Definition: Instructions.h:2885
llvm::GCStatepointInst::gc_transition_args
iterator_range< const_op_iterator > gc_transition_args() const
range adapter for GC transition arguments
Definition: Statepoint.h:161
llvm::GCStatepointInst::getNumPatchBytes
uint32_t getNumPatchBytes() const
Return the number of patchable bytes associated with this statepoint.
Definition: Statepoint.h:94
llvm::GCStatepointInst::gc_transition_args_end
const_op_iterator gc_transition_args_end() const
Definition: Statepoint.h:154
llvm::GCStatepointInst::NumPatchBytesPos
@ NumPatchBytesPos
Definition: Statepoint.h:81
llvm::GCStatepointInst::deopt_end
const_op_iterator deopt_end() const
Definition: Statepoint.h:170
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:177
llvm::AttributeList
Definition: Attributes.h:399
llvm::Optional< uint32_t >
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:183
llvm::CallBase::arg_begin
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1303
llvm::GCStatepointInst::getActualCalledOperand
Value * getActualCalledOperand() const
Return the value actually being called or invoked.
Definition: Statepoint.h:112
llvm::GCStatepointInst::GCStatepointInst
GCStatepointInst()=delete
llvm::GCStatepointInst
Represents a gc.statepoint intrinsic call.
Definition: Statepoint.h:63
Instruction.h
Constants.h
llvm::User
Definition: User.h:44
Intrinsics.h
llvm::LLVMContext::OB_gc_transition
@ OB_gc_transition
Definition: LLVMContext.h:92
llvm::GCStatepointInst::CalledFunctionPos
@ CalledFunctionPos
Definition: Statepoint.h:82
llvm::StatepointDirectives::DeoptBundleStatepointID
static const uint64_t DeoptBundleStatepointID
Definition: Statepoint.h:260
llvm::GCStatepointInst::deopt_begin
const_op_iterator deopt_begin() const
Definition: Statepoint.h:165
llvm::GCStatepointInst::classof
static bool classof(const Value *V)
Definition: Statepoint.h:75
llvm::GCStatepointInst::NumCallArgsPos
@ NumCallArgsPos
Definition: Statepoint.h:83
llvm::LLVMContext::OB_gc_live
@ OB_gc_live
Definition: LLVMContext.h:95
llvm::GCStatepointInst::getGCResultLocality
std::pair< bool, bool > getGCResultLocality() const
Returns pair of boolean flags.
Definition: Statepoint.h:239
llvm::GCStatepointInst::gc_args_end
const_op_iterator gc_args_end() const
Return an end iterator for the gc argument range.
Definition: Statepoint.h:190
llvm::GCStatepointInst::getActualCalledFunction
Function * getActualCalledFunction() const
Returns the function called if this is a wrapping a direct call, and null otherwise.
Definition: Statepoint.h:118
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:214
BasicBlock.h
llvm::isStatepointDirectiveAttr
bool isStatepointDirectiveAttr(Attribute Attr)
Return true if the Attr is an attribute that is a statepoint directive.
Definition: Statepoint.cpp:20
llvm::GCStatepointInst::FlagsPos
@ FlagsPos
Definition: Statepoint.h:84
llvm::isInt< 32 >
constexpr bool isInt< 32 >(int64_t x)
Definition: MathExtras.h:373
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:132
llvm::StatepointDirectives
Call sites that get wrapped by a gc.statepoint (currently only in RewriteStatepointsForGC and potenti...
Definition: Statepoint.h:255
llvm::GCStatepointInst::IDPos
@ IDPos
Definition: Statepoint.h:80
I
#define I(x, y, z)
Definition: MD5.cpp:59
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:145
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:1309
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:139
llvm::StatepointDirectives::StatepointID
Optional< uint64_t > StatepointID
Definition: Statepoint.h:257
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
uint32_t
llvm::StatepointDirectives::NumPatchBytes
Optional< uint32_t > NumPatchBytes
Definition: Statepoint.h:256
llvm::GCStatepointInst::classof
static bool classof(const CallBase *I)
Definition: Statepoint.h:69
llvm::StatepointFlags::GCTransition
@ GCTransition
Indicates that this statepoint is a transition from GC-aware code to code that is not GC-aware.
llvm::GCStatepointInst::CallArgsBeginPos
@ CallArgsBeginPos
Definition: Statepoint.h:85
Attributes.h
llvm::LLVMContext::OB_deopt
@ OB_deopt
Definition: LLVMContext.h:90
llvm::CallBase::arg_size
unsigned arg_size() const
Definition: InstrTypes.h:1326
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:124
Instructions.h
llvm::parseStatepointDirectivesFromAttrs
StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS)
Parse out statepoint directives from the function attributes present in AS.
Definition: Statepoint.cpp:26
llvm::StatepointDirectives::DefaultStatepointID
static const uint64_t DefaultStatepointID
Definition: Statepoint.h:259
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1328
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:149
llvm::GCStatepointInst::gc_args
iterator_range< const_op_iterator > gc_args() const
range adapter for gc arguments
Definition: Statepoint.h:197
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::StatepointFlags::DeoptLiveIn
@ DeoptLiveIn
Mark the deopt arguments associated with the statepoint as only being "live-in".
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:134
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:44
llvm::GCStatepointInst::getNumCallArgs
int getNumCallArgs() const
Number of arguments to be passed to the actual callee.
Definition: Statepoint.h:103