Home | History | Annotate | Download | only in IR
      1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- 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 defines classes that make it really easy to deal with intrinsic
     11 // functions with the isa/dyncast family of functions.  In particular, this
     12 // allows you to do things like:
     13 //
     14 //     if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
     15 //        ... MCI->getDest() ... MCI->getSource() ...
     16 //
     17 // All intrinsic function calls are instances of the call instruction, so these
     18 // are all subclasses of the CallInst class.  Note that none of these classes
     19 // has state or virtual methods, which is an important part of this gross/neat
     20 // hack working.
     21 //
     22 //===----------------------------------------------------------------------===//
     23 
     24 #ifndef LLVM_IR_INTRINSICINST_H
     25 #define LLVM_IR_INTRINSICINST_H
     26 
     27 #include "llvm/IR/Constants.h"
     28 #include "llvm/IR/Function.h"
     29 #include "llvm/IR/Instructions.h"
     30 #include "llvm/IR/Intrinsics.h"
     31 
     32 namespace llvm {
     33   /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
     34   /// functions.  This allows the standard isa/dyncast/cast functionality to
     35   /// work with calls to intrinsic functions.
     36   class IntrinsicInst : public CallInst {
     37     IntrinsicInst() LLVM_DELETED_FUNCTION;
     38     IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
     39     void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
     40   public:
     41     /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
     42     ///
     43     Intrinsic::ID getIntrinsicID() const {
     44       return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
     45     }
     46 
     47     // Methods for support type inquiry through isa, cast, and dyn_cast:
     48     static inline bool classof(const CallInst *I) {
     49       if (const Function *CF = I->getCalledFunction())
     50         return CF->isIntrinsic();
     51       return false;
     52     }
     53     static inline bool classof(const Value *V) {
     54       return isa<CallInst>(V) && classof(cast<CallInst>(V));
     55     }
     56   };
     57 
     58   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
     59   ///
     60   class DbgInfoIntrinsic : public IntrinsicInst {
     61   public:
     62 
     63     // Methods for support type inquiry through isa, cast, and dyn_cast:
     64     static inline bool classof(const IntrinsicInst *I) {
     65       switch (I->getIntrinsicID()) {
     66       case Intrinsic::dbg_declare:
     67       case Intrinsic::dbg_value:
     68         return true;
     69       default: return false;
     70       }
     71     }
     72     static inline bool classof(const Value *V) {
     73       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
     74     }
     75 
     76     static Value *StripCast(Value *C);
     77   };
     78 
     79   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
     80   ///
     81   class DbgDeclareInst : public DbgInfoIntrinsic {
     82   public:
     83     Value *getAddress() const;
     84     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
     85 
     86     // Methods for support type inquiry through isa, cast, and dyn_cast:
     87     static inline bool classof(const IntrinsicInst *I) {
     88       return I->getIntrinsicID() == Intrinsic::dbg_declare;
     89     }
     90     static inline bool classof(const Value *V) {
     91       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
     92     }
     93   };
     94 
     95   /// DbgValueInst - This represents the llvm.dbg.value instruction.
     96   ///
     97   class DbgValueInst : public DbgInfoIntrinsic {
     98   public:
     99     const Value *getValue() const;
    100     Value *getValue();
    101     uint64_t getOffset() const {
    102       return cast<ConstantInt>(
    103                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
    104     }
    105     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
    106 
    107     // Methods for support type inquiry through isa, cast, and dyn_cast:
    108     static inline bool classof(const IntrinsicInst *I) {
    109       return I->getIntrinsicID() == Intrinsic::dbg_value;
    110     }
    111     static inline bool classof(const Value *V) {
    112       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    113     }
    114   };
    115 
    116   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
    117   ///
    118   class MemIntrinsic : public IntrinsicInst {
    119   public:
    120     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
    121 
    122     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
    123     ConstantInt *getAlignmentCst() const {
    124       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
    125     }
    126 
    127     unsigned getAlignment() const {
    128       return getAlignmentCst()->getZExtValue();
    129     }
    130 
    131     ConstantInt *getVolatileCst() const {
    132       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
    133     }
    134     bool isVolatile() const {
    135       return !getVolatileCst()->isZero();
    136     }
    137 
    138     unsigned getDestAddressSpace() const {
    139       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
    140     }
    141 
    142     /// getDest - This is just like getRawDest, but it strips off any cast
    143     /// instructions that feed it, giving the original input.  The returned
    144     /// value is guaranteed to be a pointer.
    145     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
    146 
    147     /// set* - Set the specified arguments of the instruction.
    148     ///
    149     void setDest(Value *Ptr) {
    150       assert(getRawDest()->getType() == Ptr->getType() &&
    151              "setDest called with pointer of wrong type!");
    152       setArgOperand(0, Ptr);
    153     }
    154 
    155     void setLength(Value *L) {
    156       assert(getLength()->getType() == L->getType() &&
    157              "setLength called with value of wrong type!");
    158       setArgOperand(2, L);
    159     }
    160 
    161     void setAlignment(Constant* A) {
    162       setArgOperand(3, A);
    163     }
    164 
    165     void setVolatile(Constant* V) {
    166       setArgOperand(4, V);
    167     }
    168 
    169     Type *getAlignmentType() const {
    170       return getArgOperand(3)->getType();
    171     }
    172 
    173     // Methods for support type inquiry through isa, cast, and dyn_cast:
    174     static inline bool classof(const IntrinsicInst *I) {
    175       switch (I->getIntrinsicID()) {
    176       case Intrinsic::memcpy:
    177       case Intrinsic::memmove:
    178       case Intrinsic::memset:
    179         return true;
    180       default: return false;
    181       }
    182     }
    183     static inline bool classof(const Value *V) {
    184       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    185     }
    186   };
    187 
    188   /// MemSetInst - This class wraps the llvm.memset intrinsic.
    189   ///
    190   class MemSetInst : public MemIntrinsic {
    191   public:
    192     /// get* - Return the arguments to the instruction.
    193     ///
    194     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
    195 
    196     void setValue(Value *Val) {
    197       assert(getValue()->getType() == Val->getType() &&
    198              "setValue called with value of wrong type!");
    199       setArgOperand(1, Val);
    200     }
    201 
    202     // Methods for support type inquiry through isa, cast, and dyn_cast:
    203     static inline bool classof(const IntrinsicInst *I) {
    204       return I->getIntrinsicID() == Intrinsic::memset;
    205     }
    206     static inline bool classof(const Value *V) {
    207       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    208     }
    209   };
    210 
    211   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
    212   ///
    213   class MemTransferInst : public MemIntrinsic {
    214   public:
    215     /// get* - Return the arguments to the instruction.
    216     ///
    217     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
    218 
    219     /// getSource - This is just like getRawSource, but it strips off any cast
    220     /// instructions that feed it, giving the original input.  The returned
    221     /// value is guaranteed to be a pointer.
    222     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
    223 
    224     unsigned getSourceAddressSpace() const {
    225       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
    226     }
    227 
    228     void setSource(Value *Ptr) {
    229       assert(getRawSource()->getType() == Ptr->getType() &&
    230              "setSource called with pointer of wrong type!");
    231       setArgOperand(1, Ptr);
    232     }
    233 
    234     // Methods for support type inquiry through isa, cast, and dyn_cast:
    235     static inline bool classof(const IntrinsicInst *I) {
    236       return I->getIntrinsicID() == Intrinsic::memcpy ||
    237              I->getIntrinsicID() == Intrinsic::memmove;
    238     }
    239     static inline bool classof(const Value *V) {
    240       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    241     }
    242   };
    243 
    244 
    245   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
    246   ///
    247   class MemCpyInst : public MemTransferInst {
    248   public:
    249     // Methods for support type inquiry through isa, cast, and dyn_cast:
    250     static inline bool classof(const IntrinsicInst *I) {
    251       return I->getIntrinsicID() == Intrinsic::memcpy;
    252     }
    253     static inline bool classof(const Value *V) {
    254       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    255     }
    256   };
    257 
    258   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
    259   ///
    260   class MemMoveInst : public MemTransferInst {
    261   public:
    262     // Methods for support type inquiry through isa, cast, and dyn_cast:
    263     static inline bool classof(const IntrinsicInst *I) {
    264       return I->getIntrinsicID() == Intrinsic::memmove;
    265     }
    266     static inline bool classof(const Value *V) {
    267       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    268     }
    269   };
    270 
    271   /// VAStartInst - This represents the llvm.va_start intrinsic.
    272   ///
    273   class VAStartInst : public IntrinsicInst {
    274   public:
    275     static inline bool classof(const IntrinsicInst *I) {
    276       return I->getIntrinsicID() == Intrinsic::vastart;
    277     }
    278     static inline bool classof(const Value *V) {
    279       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    280     }
    281 
    282     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
    283   };
    284 
    285   /// VAEndInst - This represents the llvm.va_end intrinsic.
    286   ///
    287   class VAEndInst : public IntrinsicInst {
    288   public:
    289     static inline bool classof(const IntrinsicInst *I) {
    290       return I->getIntrinsicID() == Intrinsic::vaend;
    291     }
    292     static inline bool classof(const Value *V) {
    293       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    294     }
    295 
    296     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
    297   };
    298 
    299   /// VACopyInst - This represents the llvm.va_copy intrinsic.
    300   ///
    301   class VACopyInst : public IntrinsicInst {
    302   public:
    303     static inline bool classof(const IntrinsicInst *I) {
    304       return I->getIntrinsicID() == Intrinsic::vacopy;
    305     }
    306     static inline bool classof(const Value *V) {
    307       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
    308     }
    309 
    310     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
    311     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
    312   };
    313 
    314 }
    315 
    316 #endif
    317