Statepoint.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains utility functions and a wrapper class analogous to
  15. // CallBase for accessing the fields of gc.statepoint, gc.relocate,
  16. // gc.result intrinsics; and some general utilities helpful when dealing with
  17. // gc.statepoint.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_IR_STATEPOINT_H
  21. #define LLVM_IR_STATEPOINT_H
  22. #include "llvm/ADT/iterator_range.h"
  23. #include "llvm/IR/Attributes.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/InstrTypes.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/IR/IntrinsicInst.h"
  29. #include "llvm/IR/Intrinsics.h"
  30. #include "llvm/Support/Casting.h"
  31. #include "llvm/Support/MathExtras.h"
  32. #include <cassert>
  33. #include <cstddef>
  34. #include <cstdint>
  35. #include <optional>
  36. #include <vector>
  37. namespace llvm {
  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. MaskAll = 3 ///< A bitmask that includes all valid flags.
  52. };
  53. // These two are defined in IntrinsicInst since they're part of the
  54. // IntrinsicInst class hierarchy.
  55. class GCRelocateInst;
  56. /// Represents a gc.statepoint intrinsic call. This extends directly from
  57. /// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
  58. /// invokable.
  59. class GCStatepointInst : public CallBase {
  60. public:
  61. GCStatepointInst() = delete;
  62. GCStatepointInst(const GCStatepointInst &) = delete;
  63. GCStatepointInst &operator=(const GCStatepointInst &) = delete;
  64. static bool classof(const CallBase *I) {
  65. if (const Function *CF = I->getCalledFunction())
  66. return CF->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
  67. return false;
  68. }
  69. static bool classof(const Value *V) {
  70. return isa<CallBase>(V) && classof(cast<CallBase>(V));
  71. }
  72. enum {
  73. IDPos = 0,
  74. NumPatchBytesPos = 1,
  75. CalledFunctionPos = 2,
  76. NumCallArgsPos = 3,
  77. FlagsPos = 4,
  78. CallArgsBeginPos = 5,
  79. };
  80. /// Return the ID associated with this statepoint.
  81. uint64_t getID() const {
  82. return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
  83. }
  84. /// Return the number of patchable bytes associated with this statepoint.
  85. uint32_t getNumPatchBytes() const {
  86. const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
  87. uint64_t NumPatchBytes =
  88. cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
  89. assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
  90. return NumPatchBytes;
  91. }
  92. /// Number of arguments to be passed to the actual callee.
  93. int getNumCallArgs() const {
  94. return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
  95. }
  96. uint64_t getFlags() const {
  97. return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
  98. }
  99. /// Return the value actually being called or invoked.
  100. Value *getActualCalledOperand() const {
  101. return getArgOperand(CalledFunctionPos);
  102. }
  103. /// Returns the function called if this is a wrapping a direct call, and null
  104. /// otherwise.
  105. Function *getActualCalledFunction() const {
  106. return dyn_cast_or_null<Function>(getActualCalledOperand());
  107. }
  108. /// Return the type of the value returned by the call underlying the
  109. /// statepoint.
  110. Type *getActualReturnType() const {
  111. auto *FT = cast<FunctionType>(getParamElementType(CalledFunctionPos));
  112. return FT->getReturnType();
  113. }
  114. /// Return the number of arguments to the underlying call.
  115. size_t actual_arg_size() const { return getNumCallArgs(); }
  116. /// Return an iterator to the begining of the arguments to the underlying call
  117. const_op_iterator actual_arg_begin() const {
  118. assert(CallArgsBeginPos <= (int)arg_size());
  119. return arg_begin() + CallArgsBeginPos;
  120. }
  121. /// Return an end iterator of the arguments to the underlying call
  122. const_op_iterator actual_arg_end() const {
  123. auto I = actual_arg_begin() + actual_arg_size();
  124. assert((arg_end() - I) == 2);
  125. return I;
  126. }
  127. /// range adapter for actual call arguments
  128. iterator_range<const_op_iterator> actual_args() const {
  129. return make_range(actual_arg_begin(), actual_arg_end());
  130. }
  131. const_op_iterator gc_transition_args_begin() const {
  132. if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
  133. return Opt->Inputs.begin();
  134. return arg_end();
  135. }
  136. const_op_iterator gc_transition_args_end() const {
  137. if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
  138. return Opt->Inputs.end();
  139. return arg_end();
  140. }
  141. /// range adapter for GC transition arguments
  142. iterator_range<const_op_iterator> gc_transition_args() const {
  143. return make_range(gc_transition_args_begin(), gc_transition_args_end());
  144. }
  145. const_op_iterator deopt_begin() const {
  146. if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
  147. return Opt->Inputs.begin();
  148. return arg_end();
  149. }
  150. const_op_iterator deopt_end() const {
  151. if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
  152. return Opt->Inputs.end();
  153. return arg_end();
  154. }
  155. /// range adapter for vm state arguments
  156. iterator_range<const_op_iterator> deopt_operands() const {
  157. return make_range(deopt_begin(), deopt_end());
  158. }
  159. /// Returns an iterator to the begining of the argument range describing gc
  160. /// values for the statepoint.
  161. const_op_iterator gc_args_begin() const {
  162. if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
  163. return Opt->Inputs.begin();
  164. return arg_end();
  165. }
  166. /// Return an end iterator for the gc argument range
  167. const_op_iterator gc_args_end() const {
  168. if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
  169. return Opt->Inputs.end();
  170. return arg_end();
  171. }
  172. /// range adapter for gc arguments
  173. iterator_range<const_op_iterator> gc_args() const {
  174. return make_range(gc_args_begin(), gc_args_end());
  175. }
  176. /// Get list of all gc reloactes linked to this statepoint
  177. /// May contain several relocations for the same base/derived pair.
  178. /// For example this could happen due to relocations on unwinding
  179. /// path of invoke.
  180. inline std::vector<const GCRelocateInst *> getGCRelocates() const;
  181. };
  182. std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
  183. std::vector<const GCRelocateInst *> Result;
  184. // Search for relocated pointers. Note that working backwards from the
  185. // gc_relocates ensures that we only get pairs which are actually relocated
  186. // and used after the statepoint.
  187. for (const User *U : users())
  188. if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
  189. Result.push_back(Relocate);
  190. auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
  191. if (!StatepointInvoke)
  192. return Result;
  193. // We need to scan thorough exceptional relocations if it is invoke statepoint
  194. LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
  195. // Search for gc relocates that are attached to this landingpad.
  196. for (const User *LandingPadUser : LandingPad->users()) {
  197. if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
  198. Result.push_back(Relocate);
  199. }
  200. return Result;
  201. }
  202. /// Call sites that get wrapped by a gc.statepoint (currently only in
  203. /// RewriteStatepointsForGC and potentially in other passes in the future) can
  204. /// have attributes that describe properties of gc.statepoint call they will be
  205. /// eventually be wrapped in. This struct is used represent such directives.
  206. struct StatepointDirectives {
  207. std::optional<uint32_t> NumPatchBytes;
  208. std::optional<uint64_t> StatepointID;
  209. static const uint64_t DefaultStatepointID = 0xABCDEF00;
  210. static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
  211. };
  212. /// Parse out statepoint directives from the function attributes present in \p
  213. /// AS.
  214. StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
  215. /// Return \c true if the \p Attr is an attribute that is a statepoint
  216. /// directive.
  217. bool isStatepointDirectiveAttr(Attribute Attr);
  218. } // end namespace llvm
  219. #endif // LLVM_IR_STATEPOINT_H
  220. #ifdef __GNUC__
  221. #pragma GCC diagnostic pop
  222. #endif