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     MDNode *getExpression() const { return cast<MDNode>(getArgOperand(2)); }
00086 
00087     // Methods for support type inquiry through isa, cast, and dyn_cast:
00088     static inline bool classof(const IntrinsicInst *I) {
00089       return I->getIntrinsicID() == Intrinsic::dbg_declare;
00090     }
00091     static inline bool classof(const Value *V) {
00092       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00093     }
00094   };
00095 
00096   /// DbgValueInst - This represents the llvm.dbg.value instruction.
00097   ///
00098   class DbgValueInst : public DbgInfoIntrinsic {
00099   public:
00100     const Value *getValue() const;
00101     Value *getValue();
00102     uint64_t getOffset() const {
00103       return cast<ConstantInt>(
00104                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
00105     }
00106     MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
00107     MDNode *getExpression() const { return cast<MDNode>(getArgOperand(3)); }
00108 
00109     // Methods for support type inquiry through isa, cast, and dyn_cast:
00110     static inline bool classof(const IntrinsicInst *I) {
00111       return I->getIntrinsicID() == Intrinsic::dbg_value;
00112     }
00113     static inline bool classof(const Value *V) {
00114       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00115     }
00116   };
00117 
00118   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
00119   ///
00120   class MemIntrinsic : public IntrinsicInst {
00121   public:
00122     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
00123     const Use &getRawDestUse() const { return getArgOperandUse(0); }
00124     Use &getRawDestUse() { return getArgOperandUse(0); }
00125 
00126     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
00127     const Use &getLengthUse() const { return getArgOperandUse(2); }
00128     Use &getLengthUse() { return getArgOperandUse(2); }
00129 
00130     ConstantInt *getAlignmentCst() const {
00131       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
00132     }
00133 
00134     unsigned getAlignment() const {
00135       return getAlignmentCst()->getZExtValue();
00136     }
00137 
00138     ConstantInt *getVolatileCst() const {
00139       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
00140     }
00141     bool isVolatile() const {
00142       return !getVolatileCst()->isZero();
00143     }
00144 
00145     unsigned getDestAddressSpace() const {
00146       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
00147     }
00148 
00149     /// getDest - This is just like getRawDest, but it strips off any cast
00150     /// instructions that feed it, giving the original input.  The returned
00151     /// value is guaranteed to be a pointer.
00152     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
00153 
00154     /// set* - Set the specified arguments of the instruction.
00155     ///
00156     void setDest(Value *Ptr) {
00157       assert(getRawDest()->getType() == Ptr->getType() &&
00158              "setDest called with pointer of wrong type!");
00159       setArgOperand(0, Ptr);
00160     }
00161 
00162     void setLength(Value *L) {
00163       assert(getLength()->getType() == L->getType() &&
00164              "setLength called with value of wrong type!");
00165       setArgOperand(2, L);
00166     }
00167 
00168     void setAlignment(Constant* A) {
00169       setArgOperand(3, A);
00170     }
00171 
00172     void setVolatile(Constant* V) {
00173       setArgOperand(4, V);
00174     }
00175 
00176     Type *getAlignmentType() const {
00177       return getArgOperand(3)->getType();
00178     }
00179 
00180     // Methods for support type inquiry through isa, cast, and dyn_cast:
00181     static inline bool classof(const IntrinsicInst *I) {
00182       switch (I->getIntrinsicID()) {
00183       case Intrinsic::memcpy:
00184       case Intrinsic::memmove:
00185       case Intrinsic::memset:
00186         return true;
00187       default: return false;
00188       }
00189     }
00190     static inline bool classof(const Value *V) {
00191       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00192     }
00193   };
00194 
00195   /// MemSetInst - This class wraps the llvm.memset intrinsic.
00196   ///
00197   class MemSetInst : public MemIntrinsic {
00198   public:
00199     /// get* - Return the arguments to the instruction.
00200     ///
00201     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
00202     const Use &getValueUse() const { return getArgOperandUse(1); }
00203     Use &getValueUse() { return getArgOperandUse(1); }
00204 
00205     void setValue(Value *Val) {
00206       assert(getValue()->getType() == Val->getType() &&
00207              "setValue called with value of wrong type!");
00208       setArgOperand(1, Val);
00209     }
00210 
00211     // Methods for support type inquiry through isa, cast, and dyn_cast:
00212     static inline bool classof(const IntrinsicInst *I) {
00213       return I->getIntrinsicID() == Intrinsic::memset;
00214     }
00215     static inline bool classof(const Value *V) {
00216       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00217     }
00218   };
00219 
00220   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
00221   ///
00222   class MemTransferInst : public MemIntrinsic {
00223   public:
00224     /// get* - Return the arguments to the instruction.
00225     ///
00226     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
00227     const Use &getRawSourceUse() const { return getArgOperandUse(1); }
00228     Use &getRawSourceUse() { return getArgOperandUse(1); }
00229 
00230     /// getSource - This is just like getRawSource, but it strips off any cast
00231     /// instructions that feed it, giving the original input.  The returned
00232     /// value is guaranteed to be a pointer.
00233     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
00234 
00235     unsigned getSourceAddressSpace() const {
00236       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
00237     }
00238 
00239     void setSource(Value *Ptr) {
00240       assert(getRawSource()->getType() == Ptr->getType() &&
00241              "setSource called with pointer of wrong type!");
00242       setArgOperand(1, Ptr);
00243     }
00244 
00245     // Methods for support type inquiry through isa, cast, and dyn_cast:
00246     static inline bool classof(const IntrinsicInst *I) {
00247       return I->getIntrinsicID() == Intrinsic::memcpy ||
00248              I->getIntrinsicID() == Intrinsic::memmove;
00249     }
00250     static inline bool classof(const Value *V) {
00251       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00252     }
00253   };
00254 
00255 
00256   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
00257   ///
00258   class MemCpyInst : public MemTransferInst {
00259   public:
00260     // Methods for support type inquiry through isa, cast, and dyn_cast:
00261     static inline bool classof(const IntrinsicInst *I) {
00262       return I->getIntrinsicID() == Intrinsic::memcpy;
00263     }
00264     static inline bool classof(const Value *V) {
00265       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00266     }
00267   };
00268 
00269   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
00270   ///
00271   class MemMoveInst : public MemTransferInst {
00272   public:
00273     // Methods for support type inquiry through isa, cast, and dyn_cast:
00274     static inline bool classof(const IntrinsicInst *I) {
00275       return I->getIntrinsicID() == Intrinsic::memmove;
00276     }
00277     static inline bool classof(const Value *V) {
00278       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00279     }
00280   };
00281 
00282   /// VAStartInst - This represents the llvm.va_start intrinsic.
00283   ///
00284   class VAStartInst : public IntrinsicInst {
00285   public:
00286     static inline bool classof(const IntrinsicInst *I) {
00287       return I->getIntrinsicID() == Intrinsic::vastart;
00288     }
00289     static inline bool classof(const Value *V) {
00290       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00291     }
00292 
00293     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
00294   };
00295 
00296   /// VAEndInst - This represents the llvm.va_end intrinsic.
00297   ///
00298   class VAEndInst : public IntrinsicInst {
00299   public:
00300     static inline bool classof(const IntrinsicInst *I) {
00301       return I->getIntrinsicID() == Intrinsic::vaend;
00302     }
00303     static inline bool classof(const Value *V) {
00304       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00305     }
00306 
00307     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
00308   };
00309 
00310   /// VACopyInst - This represents the llvm.va_copy intrinsic.
00311   ///
00312   class VACopyInst : public IntrinsicInst {
00313   public:
00314     static inline bool classof(const IntrinsicInst *I) {
00315       return I->getIntrinsicID() == Intrinsic::vacopy;
00316     }
00317     static inline bool classof(const Value *V) {
00318       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00319     }
00320 
00321     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
00322     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
00323   };
00324 
00325 }
00326 
00327 #endif