Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:10

0001 //===- llvm/IR/Statepoint.h - gc.statepoint utilities -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains utility functions and a wrapper class analogous to
0010 // CallBase for accessing the fields of gc.statepoint, gc.relocate,
0011 // gc.result intrinsics; and some general utilities helpful when dealing with
0012 // gc.statepoint.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_IR_STATEPOINT_H
0017 #define LLVM_IR_STATEPOINT_H
0018 
0019 #include "llvm/ADT/iterator_range.h"
0020 #include "llvm/IR/Attributes.h"
0021 #include "llvm/IR/Constants.h"
0022 #include "llvm/IR/Function.h"
0023 #include "llvm/IR/InstrTypes.h"
0024 #include "llvm/IR/Instructions.h"
0025 #include "llvm/IR/IntrinsicInst.h"
0026 #include "llvm/IR/Intrinsics.h"
0027 #include "llvm/Support/Casting.h"
0028 #include "llvm/Support/MathExtras.h"
0029 #include <cassert>
0030 #include <cstddef>
0031 #include <cstdint>
0032 #include <optional>
0033 #include <vector>
0034 
0035 namespace llvm {
0036 
0037 /// The statepoint intrinsic accepts a set of flags as its third argument.
0038 /// Valid values come out of this set.
0039 enum class StatepointFlags {
0040   None = 0,
0041   GCTransition = 1, ///< Indicates that this statepoint is a transition from
0042                     ///< GC-aware code to code that is not GC-aware.
0043   /// Mark the deopt arguments associated with the statepoint as only being
0044   /// "live-in". By default, deopt arguments are "live-through".  "live-through"
0045   /// requires that they the value be live on entry, on exit, and at any point
0046   /// during the call.  "live-in" only requires the value be available at the
0047   /// start of the call.  In particular, "live-in" values can be placed in
0048   /// unused argument registers or other non-callee saved registers.
0049   DeoptLiveIn = 2,
0050 
0051   MaskAll = 3 ///< A bitmask that includes all valid flags.
0052 };
0053 
0054 // These two are defined in IntrinsicInst since they're part of the
0055 // IntrinsicInst class hierarchy.
0056 class GCRelocateInst;
0057 
0058 /// Represents a gc.statepoint intrinsic call.  This extends directly from
0059 /// CallBase as the IntrinsicInst only supports calls and gc.statepoint is
0060 /// invokable.
0061 class GCStatepointInst : public CallBase {
0062 public:
0063   GCStatepointInst() = delete;
0064   GCStatepointInst(const GCStatepointInst &) = delete;
0065   GCStatepointInst &operator=(const GCStatepointInst &) = delete;
0066 
0067   static bool classof(const CallBase *I) {
0068     if (const Function *CF = I->getCalledFunction())
0069       return CF->getIntrinsicID() == Intrinsic::experimental_gc_statepoint;
0070     return false;
0071   }
0072 
0073   static bool classof(const Value *V) {
0074     return isa<CallBase>(V) && classof(cast<CallBase>(V));
0075   }
0076 
0077   enum {
0078     IDPos = 0,
0079     NumPatchBytesPos = 1,
0080     CalledFunctionPos = 2,
0081     NumCallArgsPos = 3,
0082     FlagsPos = 4,
0083     CallArgsBeginPos = 5,
0084   };
0085 
0086   /// Return the ID associated with this statepoint.
0087   uint64_t getID() const {
0088     return cast<ConstantInt>(getArgOperand(IDPos))->getZExtValue();
0089   }
0090 
0091   /// Return the number of patchable bytes associated with this statepoint.
0092   uint32_t getNumPatchBytes() const {
0093     const Value *NumPatchBytesVal = getArgOperand(NumPatchBytesPos);
0094     uint64_t NumPatchBytes =
0095       cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
0096     assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
0097     return NumPatchBytes;
0098   }
0099 
0100   /// Number of arguments to be passed to the actual callee.
0101   int getNumCallArgs() const {
0102     return cast<ConstantInt>(getArgOperand(NumCallArgsPos))->getZExtValue();
0103   }
0104 
0105   uint64_t getFlags() const {
0106     return cast<ConstantInt>(getArgOperand(FlagsPos))->getZExtValue();
0107   }
0108 
0109   /// Return the value actually being called or invoked.
0110   Value *getActualCalledOperand() const {
0111     return getArgOperand(CalledFunctionPos);
0112   }
0113 
0114   /// Returns the function called if this is a wrapping a direct call, and null
0115   /// otherwise.
0116   Function *getActualCalledFunction() const {
0117     return dyn_cast_or_null<Function>(getActualCalledOperand());
0118   }
0119 
0120   /// Return the type of the value returned by the call underlying the
0121   /// statepoint.
0122   Type *getActualReturnType() const {
0123     auto *FT = cast<FunctionType>(getParamElementType(CalledFunctionPos));
0124     return FT->getReturnType();
0125   }
0126 
0127 
0128   /// Return the number of arguments to the underlying call.
0129   size_t actual_arg_size() const { return getNumCallArgs(); }
0130   /// Return an iterator to the begining of the arguments to the underlying call
0131   const_op_iterator actual_arg_begin() const {
0132     assert(CallArgsBeginPos <= (int)arg_size());
0133     return arg_begin() + CallArgsBeginPos;
0134   }
0135   /// Return an end iterator of the arguments to the underlying call
0136   const_op_iterator actual_arg_end() const {
0137     auto I = actual_arg_begin() + actual_arg_size();
0138     assert((arg_end() - I) == 2);
0139     return I;
0140   }
0141   /// range adapter for actual call arguments
0142   iterator_range<const_op_iterator> actual_args() const {
0143     return make_range(actual_arg_begin(), actual_arg_end());
0144   }
0145 
0146   const_op_iterator gc_transition_args_begin() const {
0147     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
0148       return Opt->Inputs.begin();
0149     return arg_end();
0150   }
0151   const_op_iterator gc_transition_args_end() const {
0152     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_transition))
0153       return Opt->Inputs.end();
0154     return arg_end();
0155   }
0156 
0157   /// range adapter for GC transition arguments
0158   iterator_range<const_op_iterator> gc_transition_args() const {
0159     return make_range(gc_transition_args_begin(), gc_transition_args_end());
0160   }
0161 
0162   const_op_iterator deopt_begin() const {
0163     if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
0164       return Opt->Inputs.begin();
0165     return arg_end();
0166   }
0167   const_op_iterator deopt_end() const {
0168     if (auto Opt = getOperandBundle(LLVMContext::OB_deopt))
0169       return Opt->Inputs.end();
0170     return arg_end();
0171   }
0172 
0173   /// range adapter for vm state arguments
0174   iterator_range<const_op_iterator> deopt_operands() const {
0175     return make_range(deopt_begin(), deopt_end());
0176   }
0177 
0178   /// Returns an iterator to the begining of the argument range describing gc
0179   /// live values for the statepoint.
0180   const_op_iterator gc_live_begin() const {
0181     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
0182       return Opt->Inputs.begin();
0183     return arg_end();
0184   }
0185 
0186   /// Return an end iterator for the gc live range
0187   const_op_iterator gc_live_end() const {
0188     if (auto Opt = getOperandBundle(LLVMContext::OB_gc_live))
0189       return Opt->Inputs.end();
0190     return arg_end();
0191   }
0192 
0193   /// range adapter for gc live arguments
0194   iterator_range<const_op_iterator> gc_live() const {
0195     return make_range(gc_live_begin(), gc_live_end());
0196   }
0197 
0198 
0199   /// Get list of all gc reloactes linked to this statepoint
0200   /// May contain several relocations for the same base/derived pair.
0201   /// For example this could happen due to relocations on unwinding
0202   /// path of invoke.
0203   inline std::vector<const GCRelocateInst *> getGCRelocates() const;
0204 };
0205 
0206 std::vector<const GCRelocateInst *> GCStatepointInst::getGCRelocates() const {
0207   std::vector<const GCRelocateInst *> Result;
0208 
0209   // Search for relocated pointers.  Note that working backwards from the
0210   // gc_relocates ensures that we only get pairs which are actually relocated
0211   // and used after the statepoint.
0212   for (const User *U : users())
0213     if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
0214       Result.push_back(Relocate);
0215 
0216   auto *StatepointInvoke = dyn_cast<InvokeInst>(this);
0217   if (!StatepointInvoke)
0218     return Result;
0219 
0220   // We need to scan thorough exceptional relocations if it is invoke statepoint
0221   LandingPadInst *LandingPad = StatepointInvoke->getLandingPadInst();
0222 
0223   // Search for gc relocates that are attached to this landingpad.
0224   for (const User *LandingPadUser : LandingPad->users()) {
0225     if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
0226       Result.push_back(Relocate);
0227   }
0228   return Result;
0229 }
0230 
0231 /// Call sites that get wrapped by a gc.statepoint (currently only in
0232 /// RewriteStatepointsForGC and potentially in other passes in the future) can
0233 /// have attributes that describe properties of gc.statepoint call they will be
0234 /// eventually be wrapped in.  This struct is used represent such directives.
0235 struct StatepointDirectives {
0236   std::optional<uint32_t> NumPatchBytes;
0237   std::optional<uint64_t> StatepointID;
0238 
0239   static const uint64_t DefaultStatepointID = 0xABCDEF00;
0240   static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
0241 };
0242 
0243 /// Parse out statepoint directives from the function attributes present in \p
0244 /// AS.
0245 StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
0246 
0247 /// Return \c true if the \p Attr is an attribute that is a statepoint
0248 /// directive.
0249 bool isStatepointDirectiveAttr(Attribute Attr);
0250 
0251 } // end namespace llvm
0252 
0253 #endif // LLVM_IR_STATEPOINT_H