Home | History | Annotate | Download | only in IR
      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,
     12 // gc.result intrinsics; and some general utilities helpful when dealing with
     13 // gc.statepoint.
     14 //
     15 //===----------------------------------------------------------------------===//
     16 
     17 #ifndef LLVM_IR_STATEPOINT_H
     18 #define LLVM_IR_STATEPOINT_H
     19 
     20 #include "llvm/ADT/Optional.h"
     21 #include "llvm/ADT/iterator_range.h"
     22 #include "llvm/IR/Attributes.h"
     23 #include "llvm/IR/BasicBlock.h"
     24 #include "llvm/IR/CallSite.h"
     25 #include "llvm/IR/Constants.h"
     26 #include "llvm/IR/Function.h"
     27 #include "llvm/IR/Instruction.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 
     38 namespace llvm {
     39 
     40 /// The statepoint intrinsic accepts a set of flags as its third argument.
     41 /// Valid values come out of this set.
     42 enum class StatepointFlags {
     43   None = 0,
     44   GCTransition = 1, ///< Indicates that this statepoint is a transition from
     45                     ///< GC-aware code to code that is not GC-aware.
     46   /// Mark the deopt arguments associated with the statepoint as only being
     47   /// "live-in". By default, deopt arguments are "live-through".  "live-through"
     48   /// requires that they the value be live on entry, on exit, and at any point
     49   /// during the call.  "live-in" only requires the value be available at the
     50   /// start of the call.  In particular, "live-in" values can be placed in
     51   /// unused argument registers or other non-callee saved registers.
     52   DeoptLiveIn = 2,
     53 
     54   MaskAll = 3 ///< A bitmask that includes all valid flags.
     55 };
     56 
     57 class GCRelocateInst;
     58 class GCResultInst;
     59 
     60 bool isStatepoint(ImmutableCallSite CS);
     61 bool isStatepoint(const Value *V);
     62 bool isStatepoint(const Value &V);
     63 
     64 bool isGCRelocate(ImmutableCallSite CS);
     65 bool isGCResult(ImmutableCallSite CS);
     66 
     67 /// Analogous to CallSiteBase, this provides most of the actual
     68 /// functionality for Statepoint and ImmutableStatepoint.  It is
     69 /// templatized to allow easily specializing of const and non-const
     70 /// concrete subtypes.  This is structured analogous to CallSite
     71 /// rather than the IntrinsicInst.h helpers since we need to support
     72 /// invokable statepoints.
     73 template <typename FunTy, typename InstructionTy, typename ValueTy,
     74           typename CallSiteTy>
     75 class StatepointBase {
     76   CallSiteTy StatepointCS;
     77 
     78 protected:
     79   explicit StatepointBase(InstructionTy *I) {
     80     if (isStatepoint(I)) {
     81       StatepointCS = CallSiteTy(I);
     82       assert(StatepointCS && "isStatepoint implies CallSite");
     83     }
     84   }
     85 
     86   explicit StatepointBase(CallSiteTy CS) {
     87     if (isStatepoint(CS))
     88       StatepointCS = CS;
     89   }
     90 
     91 public:
     92   using arg_iterator = typename CallSiteTy::arg_iterator;
     93 
     94   enum {
     95     IDPos = 0,
     96     NumPatchBytesPos = 1,
     97     CalledFunctionPos = 2,
     98     NumCallArgsPos = 3,
     99     FlagsPos = 4,
    100     CallArgsBeginPos = 5,
    101   };
    102 
    103   void *operator new(size_t, unsigned) = delete;
    104   void *operator new(size_t s) = delete;
    105 
    106   explicit operator bool() const {
    107     // We do not assign non-statepoint CallSites to StatepointCS.
    108     return (bool)StatepointCS;
    109   }
    110 
    111   /// Return the underlying CallSite.
    112   CallSiteTy getCallSite() const {
    113     assert(*this && "check validity first!");
    114     return StatepointCS;
    115   }
    116 
    117   uint64_t getFlags() const {
    118     return cast<ConstantInt>(getCallSite().getArgument(FlagsPos))
    119         ->getZExtValue();
    120   }
    121 
    122   /// Return the ID associated with this statepoint.
    123   uint64_t getID() const {
    124     const Value *IDVal = getCallSite().getArgument(IDPos);
    125     return cast<ConstantInt>(IDVal)->getZExtValue();
    126   }
    127 
    128   /// Return the number of patchable bytes associated with this statepoint.
    129   uint32_t getNumPatchBytes() const {
    130     const Value *NumPatchBytesVal = getCallSite().getArgument(NumPatchBytesPos);
    131     uint64_t NumPatchBytes =
    132       cast<ConstantInt>(NumPatchBytesVal)->getZExtValue();
    133     assert(isInt<32>(NumPatchBytes) && "should fit in 32 bits!");
    134     return NumPatchBytes;
    135   }
    136 
    137   /// Return the value actually being called or invoked.
    138   ValueTy *getCalledValue() const {
    139     return getCallSite().getArgument(CalledFunctionPos);
    140   }
    141 
    142   InstructionTy *getInstruction() const {
    143     return getCallSite().getInstruction();
    144   }
    145 
    146   /// Return the function being called if this is a direct call, otherwise
    147   /// return null (if it's an indirect call).
    148   FunTy *getCalledFunction() const {
    149     return dyn_cast<Function>(getCalledValue());
    150   }
    151 
    152   /// Return the caller function for this statepoint.
    153   FunTy *getCaller() const { return getCallSite().getCaller(); }
    154 
    155   /// Determine if the statepoint cannot unwind.
    156   bool doesNotThrow() const {
    157     Function *F = getCalledFunction();
    158     return getCallSite().doesNotThrow() || (F ? F->doesNotThrow() : false);
    159   }
    160 
    161   /// Return the type of the value returned by the call underlying the
    162   /// statepoint.
    163   Type *getActualReturnType() const {
    164     auto *FTy = cast<FunctionType>(
    165         cast<PointerType>(getCalledValue()->getType())->getElementType());
    166     return FTy->getReturnType();
    167   }
    168 
    169   /// Number of arguments to be passed to the actual callee.
    170   int getNumCallArgs() const {
    171     const Value *NumCallArgsVal = getCallSite().getArgument(NumCallArgsPos);
    172     return cast<ConstantInt>(NumCallArgsVal)->getZExtValue();
    173   }
    174 
    175   size_t arg_size() const { return getNumCallArgs(); }
    176   typename CallSiteTy::arg_iterator arg_begin() const {
    177     assert(CallArgsBeginPos <= (int)getCallSite().arg_size());
    178     return getCallSite().arg_begin() + CallArgsBeginPos;
    179   }
    180   typename CallSiteTy::arg_iterator arg_end() const {
    181     auto I = arg_begin() + arg_size();
    182     assert((getCallSite().arg_end() - I) >= 0);
    183     return I;
    184   }
    185 
    186   ValueTy *getArgument(unsigned Index) {
    187     assert(Index < arg_size() && "out of bounds!");
    188     return *(arg_begin() + Index);
    189   }
    190 
    191   /// range adapter for call arguments
    192   iterator_range<arg_iterator> call_args() const {
    193     return make_range(arg_begin(), arg_end());
    194   }
    195 
    196   /// \brief Return true if the call or the callee has the given attribute.
    197   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
    198     Function *F = getCalledFunction();
    199     return getCallSite().paramHasAttr(i + CallArgsBeginPos, A) ||
    200           (F ? F->getAttributes().hasAttribute(i, A) : false);
    201   }
    202 
    203   /// Number of GC transition args.
    204   int getNumTotalGCTransitionArgs() const {
    205     const Value *NumGCTransitionArgs = *arg_end();
    206     return cast<ConstantInt>(NumGCTransitionArgs)->getZExtValue();
    207   }
    208   typename CallSiteTy::arg_iterator gc_transition_args_begin() const {
    209     auto I = arg_end() + 1;
    210     assert((getCallSite().arg_end() - I) >= 0);
    211     return I;
    212   }
    213   typename CallSiteTy::arg_iterator gc_transition_args_end() const {
    214     auto I = gc_transition_args_begin() + getNumTotalGCTransitionArgs();
    215     assert((getCallSite().arg_end() - I) >= 0);
    216     return I;
    217   }
    218 
    219   /// range adapter for GC transition arguments
    220   iterator_range<arg_iterator> gc_transition_args() const {
    221     return make_range(gc_transition_args_begin(), gc_transition_args_end());
    222   }
    223 
    224   /// Number of additional arguments excluding those intended
    225   /// for garbage collection.
    226   int getNumTotalVMSArgs() const {
    227     const Value *NumVMSArgs = *gc_transition_args_end();
    228     return cast<ConstantInt>(NumVMSArgs)->getZExtValue();
    229   }
    230 
    231   typename CallSiteTy::arg_iterator deopt_begin() const {
    232     auto I = gc_transition_args_end() + 1;
    233     assert((getCallSite().arg_end() - I) >= 0);
    234     return I;
    235   }
    236   typename CallSiteTy::arg_iterator deopt_end() const {
    237     auto I = deopt_begin() + getNumTotalVMSArgs();
    238     assert((getCallSite().arg_end() - I) >= 0);
    239     return I;
    240   }
    241 
    242   /// range adapter for vm state arguments
    243   iterator_range<arg_iterator> deopt_operands() const {
    244     return make_range(deopt_begin(), deopt_end());
    245   }
    246 
    247   typename CallSiteTy::arg_iterator gc_args_begin() const {
    248     return deopt_end();
    249   }
    250   typename CallSiteTy::arg_iterator gc_args_end() const {
    251     return getCallSite().arg_end();
    252   }
    253 
    254   unsigned gcArgsStartIdx() const {
    255     return gc_args_begin() - getInstruction()->op_begin();
    256   }
    257 
    258   /// range adapter for gc arguments
    259   iterator_range<arg_iterator> gc_args() const {
    260     return make_range(gc_args_begin(), gc_args_end());
    261   }
    262 
    263   /// Get list of all gc reloactes linked to this statepoint
    264   /// May contain several relocations for the same base/derived pair.
    265   /// For example this could happen due to relocations on unwinding
    266   /// path of invoke.
    267   std::vector<const GCRelocateInst *> getRelocates() const;
    268 
    269   /// Get the experimental_gc_result call tied to this statepoint.  Can be
    270   /// nullptr if there isn't a gc_result tied to this statepoint.  Guaranteed to
    271   /// be a CallInst if non-null.
    272   const GCResultInst *getGCResult() const {
    273     for (auto *U : getInstruction()->users())
    274       if (auto *GRI = dyn_cast<GCResultInst>(U))
    275         return GRI;
    276     return nullptr;
    277   }
    278 
    279 #ifndef NDEBUG
    280   /// Asserts if this statepoint is malformed.  Common cases for failure
    281   /// include incorrect length prefixes for variable length sections or
    282   /// illegal values for parameters.
    283   void verify() {
    284     assert(getNumCallArgs() >= 0 &&
    285            "number of arguments to actually callee can't be negative");
    286 
    287     // The internal asserts in the iterator accessors do the rest.
    288     (void)arg_begin();
    289     (void)arg_end();
    290     (void)gc_transition_args_begin();
    291     (void)gc_transition_args_end();
    292     (void)deopt_begin();
    293     (void)deopt_end();
    294     (void)gc_args_begin();
    295     (void)gc_args_end();
    296   }
    297 #endif
    298 };
    299 
    300 /// A specialization of it's base class for read only access
    301 /// to a gc.statepoint.
    302 class ImmutableStatepoint
    303     : public StatepointBase<const Function, const Instruction, const Value,
    304                             ImmutableCallSite> {
    305   using Base =
    306       StatepointBase<const Function, const Instruction, const Value,
    307                      ImmutableCallSite>;
    308 
    309 public:
    310   explicit ImmutableStatepoint(const Instruction *I) : Base(I) {}
    311   explicit ImmutableStatepoint(ImmutableCallSite CS) : Base(CS) {}
    312 };
    313 
    314 /// A specialization of it's base class for read-write access
    315 /// to a gc.statepoint.
    316 class Statepoint
    317     : public StatepointBase<Function, Instruction, Value, CallSite> {
    318   using Base = StatepointBase<Function, Instruction, Value, CallSite>;
    319 
    320 public:
    321   explicit Statepoint(Instruction *I) : Base(I) {}
    322   explicit Statepoint(CallSite CS) : Base(CS) {}
    323 };
    324 
    325 /// Common base class for representing values projected from a statepoint.
    326 /// Currently, the only projections available are gc.result and gc.relocate.
    327 class GCProjectionInst : public IntrinsicInst {
    328 public:
    329   static inline bool classof(const IntrinsicInst *I) {
    330     return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
    331       I->getIntrinsicID() == Intrinsic::experimental_gc_result;
    332   }
    333 
    334   static inline bool classof(const Value *V) {
    335     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    336   }
    337 
    338   /// Return true if this relocate is tied to the invoke statepoint.
    339   /// This includes relocates which are on the unwinding path.
    340   bool isTiedToInvoke() const {
    341     const Value *Token = getArgOperand(0);
    342 
    343     return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
    344   }
    345 
    346   /// The statepoint with which this gc.relocate is associated.
    347   const Instruction *getStatepoint() const {
    348     const Value *Token = getArgOperand(0);
    349 
    350     // This takes care both of relocates for call statepoints and relocates
    351     // on normal path of invoke statepoint.
    352     if (!isa<LandingPadInst>(Token)) {
    353       assert(isStatepoint(Token));
    354       return cast<Instruction>(Token);
    355     }
    356 
    357     // This relocate is on exceptional path of an invoke statepoint
    358     const BasicBlock *InvokeBB =
    359         cast<Instruction>(Token)->getParent()->getUniquePredecessor();
    360 
    361     assert(InvokeBB && "safepoints should have unique landingpads");
    362     assert(InvokeBB->getTerminator() &&
    363            "safepoint block should be well formed");
    364     assert(isStatepoint(InvokeBB->getTerminator()));
    365 
    366     return InvokeBB->getTerminator();
    367   }
    368 };
    369 
    370 /// Represents calls to the gc.relocate intrinsic.
    371 class GCRelocateInst : public GCProjectionInst {
    372 public:
    373   static inline bool classof(const IntrinsicInst *I) {
    374     return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
    375   }
    376 
    377   static inline bool classof(const Value *V) {
    378     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    379   }
    380 
    381   /// The index into the associate statepoint's argument list
    382   /// which contains the base pointer of the pointer whose
    383   /// relocation this gc.relocate describes.
    384   unsigned getBasePtrIndex() const {
    385     return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
    386   }
    387 
    388   /// The index into the associate statepoint's argument list which
    389   /// contains the pointer whose relocation this gc.relocate describes.
    390   unsigned getDerivedPtrIndex() const {
    391     return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
    392   }
    393 
    394   Value *getBasePtr() const {
    395     ImmutableCallSite CS(getStatepoint());
    396     return *(CS.arg_begin() + getBasePtrIndex());
    397   }
    398 
    399   Value *getDerivedPtr() const {
    400     ImmutableCallSite CS(getStatepoint());
    401     return *(CS.arg_begin() + getDerivedPtrIndex());
    402   }
    403 };
    404 
    405 /// Represents calls to the gc.result intrinsic.
    406 class GCResultInst : public GCProjectionInst {
    407 public:
    408   static inline bool classof(const IntrinsicInst *I) {
    409     return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
    410   }
    411 
    412   static inline bool classof(const Value *V) {
    413     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    414   }
    415 };
    416 
    417 template <typename FunTy, typename InstructionTy, typename ValueTy,
    418           typename CallSiteTy>
    419 std::vector<const GCRelocateInst *>
    420 StatepointBase<FunTy, InstructionTy, ValueTy, CallSiteTy>::getRelocates()
    421     const {
    422 
    423   std::vector<const GCRelocateInst *> Result;
    424 
    425   CallSiteTy StatepointCS = getCallSite();
    426 
    427   // Search for relocated pointers.  Note that working backwards from the
    428   // gc_relocates ensures that we only get pairs which are actually relocated
    429   // and used after the statepoint.
    430   for (const User *U : getInstruction()->users())
    431     if (auto *Relocate = dyn_cast<GCRelocateInst>(U))
    432       Result.push_back(Relocate);
    433 
    434   if (!StatepointCS.isInvoke())
    435     return Result;
    436 
    437   // We need to scan thorough exceptional relocations if it is invoke statepoint
    438   LandingPadInst *LandingPad =
    439       cast<InvokeInst>(getInstruction())->getLandingPadInst();
    440 
    441   // Search for gc relocates that are attached to this landingpad.
    442   for (const User *LandingPadUser : LandingPad->users()) {
    443     if (auto *Relocate = dyn_cast<GCRelocateInst>(LandingPadUser))
    444       Result.push_back(Relocate);
    445   }
    446   return Result;
    447 }
    448 
    449 /// Call sites that get wrapped by a gc.statepoint (currently only in
    450 /// RewriteStatepointsForGC and potentially in other passes in the future) can
    451 /// have attributes that describe properties of gc.statepoint call they will be
    452 /// eventually be wrapped in.  This struct is used represent such directives.
    453 struct StatepointDirectives {
    454   Optional<uint32_t> NumPatchBytes;
    455   Optional<uint64_t> StatepointID;
    456 
    457   static const uint64_t DefaultStatepointID = 0xABCDEF00;
    458   static const uint64_t DeoptBundleStatepointID = 0xABCDEF0F;
    459 };
    460 
    461 /// Parse out statepoint directives from the function attributes present in \p
    462 /// AS.
    463 StatepointDirectives parseStatepointDirectivesFromAttrs(AttributeList AS);
    464 
    465 /// Return \c true if the the \p Attr is an attribute that is a statepoint
    466 /// directive.
    467 bool isStatepointDirectiveAttr(Attribute Attr);
    468 
    469 } // end namespace llvm
    470 
    471 #endif // LLVM_IR_STATEPOINT_H
    472