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