Statepoint.h 9.0 KB

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