LLVM API Documentation

IntrinsicInst.h
Go to the documentation of this file.
00001 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines classes that make it really easy to deal with intrinsic
00011 // functions with the isa/dyncast family of functions.  In particular, this
00012 // allows you to do things like:
00013 //
00014 //     if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
00015 //        ... MCI->getDest() ... MCI->getSource() ...
00016 //
00017 // All intrinsic function calls are instances of the call instruction, so these
00018 // are all subclasses of the CallInst class.  Note that none of these classes
00019 // has state or virtual methods, which is an important part of this gross/neat
00020 // hack working.
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #ifndef LLVM_IR_INTRINSICINST_H
00025 #define LLVM_IR_INTRINSICINST_H
00026 
00027 #include "llvm/IR/Constants.h"
00028 #include "llvm/IR/Function.h"
00029 #include "llvm/IR/Instructions.h"
00030 #include "llvm/IR/Intrinsics.h"
00031 
00032 namespace llvm {
00033   /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
00034   /// functions.  This allows the standard isa/dyncast/cast functionality to
00035   /// work with calls to intrinsic functions.
00036   class IntrinsicInst : public CallInst {
00037     IntrinsicInst() LLVM_DELETED_FUNCTION;
00038     IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
00039     void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
00040   public:
00041     /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
00042     ///
00043     Intrinsic::ID getIntrinsicID() const {
00044       return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
00045     }
00046 
00047     // Methods for support type inquiry through isa, cast, and dyn_cast:
00048     static inline bool classof(const CallInst *I) {
00049       if (const Function *CF = I->getCalledFunction())
00050         return CF->isIntrinsic();
00051       return false;
00052     }
00053     static inline bool classof(const Value *V) {
00054       return isa<CallInst>(V) && classof(cast<CallInst>(V));
00055     }
00056   };
00057 
00058   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
00059   ///
00060   class DbgInfoIntrinsic : public IntrinsicInst {
00061   public:
00062 
00063     // Methods for support type inquiry through isa, cast, and dyn_cast:
00064     static inline bool classof(const IntrinsicInst *I) {
00065       switch (I->getIntrinsicID()) {
00066       case Intrinsic::dbg_declare:
00067       case Intrinsic::dbg_value:
00068         return true;
00069       default: return false;
00070       }
00071     }
00072     static inline bool classof(const Value *V) {
00073       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00074     }
00075 
00076     static Value *StripCast(Value *C);
00077   };
00078 
00079   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
00080   ///
00081   class DbgDeclareInst : public DbgInfoIntrinsic {
00082   public:
00083     Value *getAddress() const;
00084     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
00085 
00086     // Methods for support type inquiry through isa, cast, and dyn_cast:
00087     static inline bool classof(const IntrinsicInst *I) {
00088       return I->getIntrinsicID() == Intrinsic::dbg_declare;
00089     }
00090     static inline bool classof(const Value *V) {
00091       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00092     }
00093   };
00094 
00095   /// DbgValueInst - This represents the llvm.dbg.value instruction.
00096   ///
00097   class DbgValueInst : public DbgInfoIntrinsic {
00098   public:
00099     const Value *getValue() const;
00100     Value *getValue();
00101     uint64_t getOffset() const {
00102       return cast<ConstantInt>(
00103                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
00104     }
00105     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
00106 
00107     // Methods for support type inquiry through isa, cast, and dyn_cast:
00108     static inline bool classof(const IntrinsicInst *I) {
00109       return I->getIntrinsicID() == Intrinsic::dbg_value;
00110     }
00111     static inline bool classof(const Value *V) {
00112       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00113     }
00114   };
00115 
00116   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
00117   ///
00118   class MemIntrinsic : public IntrinsicInst {
00119   public:
00120     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
00121 
00122     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
00123     ConstantInt *getAlignmentCst() const {
00124       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
00125     }
00126 
00127     unsigned getAlignment() const {
00128       return getAlignmentCst()->getZExtValue();
00129     }
00130 
00131     ConstantInt *getVolatileCst() const {
00132       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
00133     }
00134     bool isVolatile() const {
00135       return !getVolatileCst()->isZero();
00136     }
00137 
00138     unsigned getDestAddressSpace() const {
00139       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
00140     }
00141 
00142     /// getDest - This is just like getRawDest, but it strips off any cast
00143     /// instructions that feed it, giving the original input.  The returned
00144     /// value is guaranteed to be a pointer.
00145     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
00146 
00147     /// set* - Set the specified arguments of the instruction.
00148     ///
00149     void setDest(Value *Ptr) {
00150       assert(getRawDest()->getType() == Ptr->getType() &&
00151              "setDest called with pointer of wrong type!");
00152       setArgOperand(0, Ptr);
00153     }
00154 
00155     void setLength(Value *L) {
00156       assert(getLength()->getType() == L->getType() &&
00157              "setLength called with value of wrong type!");
00158       setArgOperand(2, L);
00159     }
00160 
00161     void setAlignment(Constant* A) {
00162       setArgOperand(3, A);
00163     }
00164 
00165     void setVolatile(Constant* V) {
00166       setArgOperand(4, V);
00167     }
00168 
00169     Type *getAlignmentType() const {
00170       return getArgOperand(3)->getType();
00171     }
00172 
00173     // Methods for support type inquiry through isa, cast, and dyn_cast:
00174     static inline bool classof(const IntrinsicInst *I) {
00175       switch (I->getIntrinsicID()) {
00176       case Intrinsic::memcpy:
00177       case Intrinsic::memmove:
00178       case Intrinsic::memset:
00179         return true;
00180       default: return false;
00181       }
00182     }
00183     static inline bool classof(const Value *V) {
00184       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00185     }
00186   };
00187 
00188   /// MemSetInst - This class wraps the llvm.memset intrinsic.
00189   ///
00190   class MemSetInst : public MemIntrinsic {
00191   public:
00192     /// get* - Return the arguments to the instruction.
00193     ///
00194     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
00195 
00196     void setValue(Value *Val) {
00197       assert(getValue()->getType() == Val->getType() &&
00198              "setValue called with value of wrong type!");
00199       setArgOperand(1, Val);
00200     }
00201 
00202     // Methods for support type inquiry through isa, cast, and dyn_cast:
00203     static inline bool classof(const IntrinsicInst *I) {
00204       return I->getIntrinsicID() == Intrinsic::memset;
00205     }
00206     static inline bool classof(const Value *V) {
00207       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00208     }
00209   };
00210 
00211   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
00212   ///
00213   class MemTransferInst : public MemIntrinsic {
00214   public:
00215     /// get* - Return the arguments to the instruction.
00216     ///
00217     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
00218 
00219     /// getSource - This is just like getRawSource, but it strips off any cast
00220     /// instructions that feed it, giving the original input.  The returned
00221     /// value is guaranteed to be a pointer.
00222     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
00223 
00224     unsigned getSourceAddressSpace() const {
00225       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
00226     }
00227 
00228     void setSource(Value *Ptr) {
00229       assert(getRawSource()->getType() == Ptr->getType() &&
00230              "setSource called with pointer of wrong type!");
00231       setArgOperand(1, Ptr);
00232     }
00233 
00234     // Methods for support type inquiry through isa, cast, and dyn_cast:
00235     static inline bool classof(const IntrinsicInst *I) {
00236       return I->getIntrinsicID() == Intrinsic::memcpy ||
00237              I->getIntrinsicID() == Intrinsic::memmove;
00238     }
00239     static inline bool classof(const Value *V) {
00240       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00241     }
00242   };
00243 
00244 
00245   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
00246   ///
00247   class MemCpyInst : public MemTransferInst {
00248   public:
00249     // Methods for support type inquiry through isa, cast, and dyn_cast:
00250     static inline bool classof(const IntrinsicInst *I) {
00251       return I->getIntrinsicID() == Intrinsic::memcpy;
00252     }
00253     static inline bool classof(const Value *V) {
00254       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00255     }
00256   };
00257 
00258   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
00259   ///
00260   class MemMoveInst : public MemTransferInst {
00261   public:
00262     // Methods for support type inquiry through isa, cast, and dyn_cast:
00263     static inline bool classof(const IntrinsicInst *I) {
00264       return I->getIntrinsicID() == Intrinsic::memmove;
00265     }
00266     static inline bool classof(const Value *V) {
00267       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00268     }
00269   };
00270 
00271   /// VAStartInst - This represents the llvm.va_start intrinsic.
00272   ///
00273   class VAStartInst : public IntrinsicInst {
00274   public:
00275     static inline bool classof(const IntrinsicInst *I) {
00276       return I->getIntrinsicID() == Intrinsic::vastart;
00277     }
00278     static inline bool classof(const Value *V) {
00279       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00280     }
00281 
00282     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
00283   };
00284 
00285   /// VAEndInst - This represents the llvm.va_end intrinsic.
00286   ///
00287   class VAEndInst : public IntrinsicInst {
00288   public:
00289     static inline bool classof(const IntrinsicInst *I) {
00290       return I->getIntrinsicID() == Intrinsic::vaend;
00291     }
00292     static inline bool classof(const Value *V) {
00293       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00294     }
00295 
00296     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
00297   };
00298 
00299   /// VACopyInst - This represents the llvm.va_copy intrinsic.
00300   ///
00301   class VACopyInst : public IntrinsicInst {
00302   public:
00303     static inline bool classof(const IntrinsicInst *I) {
00304       return I->getIntrinsicID() == Intrinsic::vacopy;
00305     }
00306     static inline bool classof(const Value *V) {
00307       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00308     }
00309 
00310     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
00311     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
00312   };
00313 
00314 }
00315 
00316 #endif