Statepoint.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //===-- llvm/IR/Statepoint.h - gc.statepoint utilities ------ --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains utility functions and a wrapper class analogous to
  11. // CallSite for accessing the fields of gc.statepoint, gc.relocate, and
  12. // gc.result intrinsics
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_IR_STATEPOINT_H
  16. #define LLVM_IR_STATEPOINT_H
  17. #include "llvm/ADT/iterator_range.h"
  18. #include "llvm/IR/BasicBlock.h"
  19. #include "llvm/IR/CallSite.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/IR/Instructions.h"
  23. #include "llvm/IR/Intrinsics.h"
  24. #include "llvm/Support/Compiler.h"
  25. namespace llvm {
  26. /// The statepoint intrinsic accepts a set of flags as its third argument.
  27. /// Valid values come out of this set.
  28. enum class StatepointFlags {
  29. None = 0,
  30. GCTransition = 1, ///< Indicates that this statepoint is a transition from
  31. ///< GC-aware code to code that is not GC-aware.
  32. MaskAll = GCTransition ///< A bitmask that includes all valid flags.
  33. };
  34. class GCRelocateOperands;
  35. class ImmutableStatepoint;
  36. bool isStatepoint(const ImmutableCallSite &CS);
  37. bool isStatepoint(const Value *V);
  38. bool isStatepoint(const Value &V);
  39. bool isGCRelocate(const Value *V);
  40. bool isGCRelocate(const ImmutableCallSite &CS);
  41. bool isGCResult(const Value *V);
  42. bool isGCResult(const ImmutableCallSite &CS);
  43. /// Analogous to CallSiteBase, this provides most of the actual
  44. /// functionality for Statepoint and ImmutableStatepoint. It is
  45. /// templatized to allow easily specializing of const and non-const
  46. /// concrete subtypes. This is structured analogous to CallSite
  47. /// rather than the IntrinsicInst.h helpers since we want to support
  48. /// invokable statepoints in the near future.
  49. template <typename FunTy, typename InstructionTy, typename ValueTy,
  50. typename CallSiteTy>
  51. class StatepointBase {
  52. CallSiteTy StatepointCS;
  53. void *operator new(size_t, unsigned) = delete;
  54. void *operator new(size_t s) = delete;
  55. protected:
  56. explicit StatepointBase(InstructionTy *I) {
  57. if (isStatepoint(I)) {
  58. StatepointCS = CallSiteTy(I);
  59. assert(StatepointCS && "isStatepoint implies CallSite");
  60. }
  61. }
  62. explicit StatepointBase(CallSiteTy CS) {
  63. if (isStatepoint(CS))
  64. StatepointCS = CS;
  65. }
  66. public:
  67. typedef typename CallSiteTy::arg_iterator arg_iterator;
  68. enum {
  69. IDPos = 0,
  70. NumPatchBytesPos = 1,
  71. CalledFunctionPos = 2,
  72. NumCallArgsPos = 3,
  73. FlagsPos = 4,
  74. CallArgsBeginPos = 5,
  75. };
  76. explicit operator bool() const {
  77. // We do not assign non-statepoint CallSites to StatepointCS.
  78. return (bool)StatepointCS;
  79. }
  80. /// Return the underlying CallSite.
  81. CallSiteTy getCallSite() const {
  82. assert(*this && "check validity first!");
  83. return StatepointCS;
  84. }
  85. uint64_t getFlags() const {
  86. return cast<ConstantInt>(getCallSite().getArgument(FlagsPos))
  87. ->getZExtValue();
  88. }
  89. /// Return the ID associated with this statepoint.
  90. uint64_t getID() const {
  91. const Value *IDVal = getCallSite().getArgument(IDPos);
  92. return cast<ConstantInt>(IDVal)->getZExtValue();
  93. }
  94. /// Return the number of patchable bytes associated with this statepoint.
  95. uint32_t getNumPatchBytes() const {
  96. const Value *NumPatchBytesVal = getCallSite().getArgument(NumPatchBytesPos);
  97. uint64_t NumPatchBytes =
  98. cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
  99. assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
  100. return NumPatchBytes;
  101. }
  102. /// Return the value actually being called or invoked.
  103. ValueTy *getCalledValue() const {
  104. return getCallSite().getArgument(CalledFunctionPos);
  105. }
  106. InstructionTy *getInstruction() const {
  107. return getCallSite().getInstruction();
  108. }
  109. /// Return the function being called if this is a direct call, otherwise
  110. /// return null (if it's an indirect call).
  111. FunTy *getCalledFunction() const {
  112. return dyn_cast<Function>(getCalledValue());
  113. }
  114. /// Return the caller function for this statepoint.
  115. FunTy *getCaller() const { return getCallSite().getCaller(); }
  116. /// Determine if the statepoint cannot unwind.
  117. bool doesNotThrow() const {
  118. Function *F = getCalledFunction();
  119. return getCallSite().doesNotThrow() || (F ? F->doesNotThrow() : false);
  120. }
  121. /// Return the type of the value returned by the call underlying the
  122. /// statepoint.
  123. Type *getActualReturnType() const {
  124. auto *FTy = cast<FunctionType>(
  125. cast<PointerType>(getCalledValue()->getType())->getElementType());
  126. return FTy->getReturnType();
  127. }
  128. /// Number of arguments to be passed to the actual callee.
  129. int getNumCallArgs() const {
  130. const Value *NumCallArgsVal = getCallSite().getArgument(NumCallArgsPos);
  131. return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
  132. }
  133. size_t arg_size() const { return getNumCallArgs(); }
  134. typename CallSiteTy::arg_iterator arg_begin() const {
  135. assert(CallArgsBeginPos <= (int)getCallSite().arg_size());
  136. return getCallSite().arg_begin() + CallArgsBeginPos;
  137. }
  138. typename CallSiteTy::arg_iterator arg_end() const {
  139. auto I = arg_begin() + arg_size();
  140. assert((getCallSite().arg_end() - I) >= 0);
  141. return I;
  142. }
  143. ValueTy *getArgument(unsigned Index) {
  144. assert(Index < arg_size() && "out of bounds!");
  145. return *(arg_begin() + Index);
  146. }
  147. /// range adapter for call arguments
  148. iterator_range<arg_iterator> call_args() const {
  149. return iterator_range<arg_iterator>(arg_begin(), arg_end());
  150. }
  151. /// \brief Return true if the call or the callee has the given attribute.
  152. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
  153. Function *F = getCalledFunction();
  154. return getCallSite().paramHasAttr(i + CallArgsBeginPos, A) ||
  155. (F ? F->getAttributes().hasAttribute(i, A) : false);
  156. }
  157. /// Number of GC transition args.
  158. int getNumTotalGCTransitionArgs() const {
  159. const Value *NumGCTransitionArgs = *arg_end();
  160. return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
  161. }
  162. typename CallSiteTy::arg_iterator gc_transition_args_begin() const {
  163. auto I = arg_end() + 1;
  164. assert((getCallSite().arg_end() - I) >= 0);
  165. return I;
  166. }
  167. typename CallSiteTy::arg_iterator gc_transition_args_end() const {
  168. auto I = gc_transition_args_begin() + getNumTotalGCTransitionArgs();
  169. assert((getCallSite().arg_end() - I) >= 0);
  170. return I;
  171. }
  172. /// range adapter for GC transition arguments
  173. iterator_range<arg_iterator> gc_transition_args() const {
  174. return iterator_range<arg_iterator>(gc_transition_args_begin(),
  175. gc_transition_args_end());
  176. }
  177. /// Number of additional arguments excluding those intended
  178. /// for garbage collection.
  179. int getNumTotalVMSArgs() const {
  180. const Value *NumVMSArgs = *gc_transition_args_end();
  181. return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
  182. }
  183. typename CallSiteTy::arg_iterator vm_state_begin() const {
  184. auto I = gc_transition_args_end() + 1;
  185. assert((getCallSite().arg_end() - I) >= 0);
  186. return I;
  187. }
  188. typename CallSiteTy::arg_iterator vm_state_end() const {
  189. auto I = vm_state_begin() + getNumTotalVMSArgs();
  190. assert((getCallSite().arg_end() - I) >= 0);
  191. return I;
  192. }
  193. /// range adapter for vm state arguments
  194. iterator_range<arg_iterator> vm_state_args() const {
  195. return iterator_range<arg_iterator>(vm_state_begin(), vm_state_end());
  196. }
  197. typename CallSiteTy::arg_iterator gc_args_begin() const {
  198. return vm_state_end();
  199. }
  200. typename CallSiteTy::arg_iterator gc_args_end() const {
  201. return getCallSite().arg_end();
  202. }
  203. /// range adapter for gc arguments
  204. iterator_range<arg_iterator> gc_args() const {
  205. return iterator_range<arg_iterator>(gc_args_begin(), gc_args_end());
  206. }
  207. /// Get list of all gc reloactes linked to this statepoint
  208. /// May contain several relocations for the same base/derived pair.
  209. /// For example this could happen due to relocations on unwinding
  210. /// path of invoke.
  211. std::vector<GCRelocateOperands> getRelocates() const;
  212. /// Get the experimental_gc_result call tied to this statepoint. Can be
  213. /// nullptr if there isn't a gc_result tied to this statepoint. Guaranteed to
  214. /// be a CallInst if non-null.
  215. InstructionTy *getGCResult() const {
  216. for (auto *U : getInstruction()->users())
  217. if (isGCResult(U))
  218. return cast<CallInst>(U);
  219. return nullptr;
  220. }
  221. #ifndef NDEBUG
  222. /// Asserts if this statepoint is malformed. Common cases for failure
  223. /// include incorrect length prefixes for variable length sections or
  224. /// illegal values for parameters.
  225. void verify() {
  226. assert(getNumCallArgs() >= 0 &&
  227. "number of arguments to actually callee can't be negative");
  228. // The internal asserts in the iterator accessors do the rest.
  229. (void)arg_begin();
  230. (void)arg_end();
  231. (void)gc_transition_args_begin();
  232. (void)gc_transition_args_end();
  233. (void)vm_state_begin();
  234. (void)vm_state_end();
  235. (void)gc_args_begin();
  236. (void)gc_args_end();
  237. }
  238. #endif
  239. };
  240. /// A specialization of it's base class for read only access
  241. /// to a gc.statepoint.
  242. class ImmutableStatepoint
  243. : public StatepointBase<const Function, const Instruction, const Value,
  244. ImmutableCallSite> {
  245. typedef StatepointBase<const Function, const Instruction, const Value,
  246. ImmutableCallSite> Base;
  247. public:
  248. explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
  249. explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
  250. };
  251. /// A specialization of it's base class for read-write access
  252. /// to a gc.statepoint.
  253. class Statepoint
  254. : public StatepointBase<Function, Instruction, Value, CallSite> {
  255. typedef StatepointBase<Function, Instruction, Value, CallSite> Base;
  256. public:
  257. explicit Statepoint(Instruction *I) : Base(I) {}
  258. explicit Statepoint(CallSite CS) : Base(CS) {}
  259. };
  260. /// Wraps a call to a gc.relocate and provides access to it's operands.
  261. /// TODO: This should likely be refactored to resememble the wrappers in
  262. /// InstrinsicInst.h.
  263. class GCRelocateOperands {
  264. ImmutableCallSite RelocateCS;
  265. public:
  266. GCRelocateOperands(const User *U) : RelocateCS(U) { assert(isGCRelocate(U)); }
  267. GCRelocateOperands(const Instruction *inst) : RelocateCS(inst) {
  268. assert(isGCRelocate(inst));
  269. }
  270. GCRelocateOperands(CallSite CS) : RelocateCS(CS) { assert(isGCRelocate(CS)); }
  271. /// Return true if this relocate is tied to the invoke statepoint.
  272. /// This includes relocates which are on the unwinding path.
  273. bool isTiedToInvoke() const {
  274. const Value *Token = RelocateCS.getArgument(0);
  275. return isa<ExtractValueInst>(Token) || isa<InvokeInst>(Token);
  276. }
  277. /// Get enclosed relocate intrinsic
  278. ImmutableCallSite getUnderlyingCallSite() { return RelocateCS; }
  279. /// The statepoint with which this gc.relocate is associated.
  280. const Instruction *getStatepoint() {
  281. const Value *Token = RelocateCS.getArgument(0);
  282. // This takes care both of relocates for call statepoints and relocates
  283. // on normal path of invoke statepoint.
  284. if (!isa<ExtractValueInst>(Token)) {
  285. return cast<Instruction>(Token);
  286. }
  287. // This relocate is on exceptional path of an invoke statepoint
  288. const BasicBlock *InvokeBB =
  289. cast<Instruction>(Token)->getParent()->getUniquePredecessor();
  290. assert(InvokeBB && "safepoints should have unique landingpads");
  291. assert(InvokeBB->getTerminator() &&
  292. "safepoint block should be well formed");
  293. assert(isStatepoint(InvokeBB->getTerminator()));
  294. return InvokeBB->getTerminator();
  295. }
  296. /// The index into the associate statepoint's argument list
  297. /// which contains the base pointer of the pointer whose
  298. /// relocation this gc.relocate describes.
  299. unsigned getBasePtrIndex() {
  300. return cast<ConstantInt>(RelocateCS.getArgument(1))->getZExtValue();
  301. }
  302. /// The index into the associate statepoint's argument list which
  303. /// contains the pointer whose relocation this gc.relocate describes.
  304. unsigned getDerivedPtrIndex() {
  305. return cast<ConstantInt>(RelocateCS.getArgument(2))->getZExtValue();
  306. }
  307. Value *getBasePtr() {
  308. ImmutableCallSite CS(getStatepoint());
  309. return *(CS.arg_begin() + getBasePtrIndex());
  310. }
  311. Value *getDerivedPtr() {
  312. ImmutableCallSite CS(getStatepoint());
  313. return *(CS.arg_begin() + getDerivedPtrIndex());
  314. }
  315. };
  316. template <typename FunTy, typename InstructionTy, typename ValueTy,
  317. typename CallSiteTy>
  318. std::vector<GCRelocateOperands>
  319. StatepointBase<FunTy, InstructionTy, ValueTy, CallSiteTy>::getRelocates()
  320. const {
  321. std::vector<GCRelocateOperands> Result;
  322. CallSiteTy StatepointCS = getCallSite();
  323. // Search for relocated pointers. Note that working backwards from the
  324. // gc_relocates ensures that we only get pairs which are actually relocated
  325. // and used after the statepoint.
  326. for (const User *U : getInstruction()->users())
  327. if (isGCRelocate(U))
  328. Result.push_back(GCRelocateOperands(U));
  329. if (!StatepointCS.isInvoke())
  330. return Result;
  331. // We need to scan thorough exceptional relocations if it is invoke statepoint
  332. LandingPadInst *LandingPad =
  333. cast<InvokeInst>(getInstruction())->getLandingPadInst();
  334. // Search for extract value from landingpad instruction to which
  335. // gc relocates will be attached
  336. for (const User *LandingPadUser : LandingPad->users()) {
  337. if (!isa<ExtractValueInst>(LandingPadUser))
  338. continue;
  339. // gc relocates should be attached to this extract value
  340. for (const User *U : LandingPadUser->users())
  341. if (isGCRelocate(U))
  342. Result.push_back(GCRelocateOperands(U));
  343. }
  344. return Result;
  345. }
  346. }
  347. #endif