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 #include "llvm/IR/Metadata.h"
00032 
00033 namespace llvm {
00034   /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
00035   /// functions.  This allows the standard isa/dyncast/cast functionality to
00036   /// work with calls to intrinsic functions.
00037   class IntrinsicInst : public CallInst {
00038     IntrinsicInst() LLVM_DELETED_FUNCTION;
00039     IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
00040     void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
00041   public:
00042     /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
00043     ///
00044     Intrinsic::ID getIntrinsicID() const {
00045       return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
00046     }
00047 
00048     // Methods for support type inquiry through isa, cast, and dyn_cast:
00049     static inline bool classof(const CallInst *I) {
00050       if (const Function *CF = I->getCalledFunction())
00051         return CF->isIntrinsic();
00052       return false;
00053     }
00054     static inline bool classof(const Value *V) {
00055       return isa<CallInst>(V) && classof(cast<CallInst>(V));
00056     }
00057   };
00058 
00059   /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
00060   ///
00061   class DbgInfoIntrinsic : public IntrinsicInst {
00062   public:
00063 
00064     // Methods for support type inquiry through isa, cast, and dyn_cast:
00065     static inline bool classof(const IntrinsicInst *I) {
00066       switch (I->getIntrinsicID()) {
00067       case Intrinsic::dbg_declare:
00068       case Intrinsic::dbg_value:
00069         return true;
00070       default: return false;
00071       }
00072     }
00073     static inline bool classof(const Value *V) {
00074       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00075     }
00076 
00077     static Value *StripCast(Value *C);
00078   };
00079 
00080   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
00081   ///
00082   class DbgDeclareInst : public DbgInfoIntrinsic {
00083   public:
00084     Value *getAddress() const;
00085     MDNode *getVariable() const {
00086       return cast<MDNode>(
00087           cast<MetadataAsValue>(getArgOperand(1))->getMetadata());
00088     }
00089     MDNode *getExpression() const {
00090       return cast<MDNode>(
00091           cast<MetadataAsValue>(getArgOperand(2))->getMetadata());
00092     }
00093 
00094     // Methods for support type inquiry through isa, cast, and dyn_cast:
00095     static inline bool classof(const IntrinsicInst *I) {
00096       return I->getIntrinsicID() == Intrinsic::dbg_declare;
00097     }
00098     static inline bool classof(const Value *V) {
00099       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00100     }
00101   };
00102 
00103   /// DbgValueInst - This represents the llvm.dbg.value instruction.
00104   ///
00105   class DbgValueInst : public DbgInfoIntrinsic {
00106   public:
00107     const Value *getValue() const;
00108     Value *getValue();
00109     uint64_t getOffset() const {
00110       return cast<ConstantInt>(
00111                           const_cast<Value*>(getArgOperand(1)))->getZExtValue();
00112     }
00113     MDNode *getVariable() const {
00114       return cast<MDNode>(
00115           cast<MetadataAsValue>(getArgOperand(2))->getMetadata());
00116     }
00117     MDNode *getExpression() const {
00118       return cast<MDNode>(
00119           cast<MetadataAsValue>(getArgOperand(3))->getMetadata());
00120     }
00121 
00122     // Methods for support type inquiry through isa, cast, and dyn_cast:
00123     static inline bool classof(const IntrinsicInst *I) {
00124       return I->getIntrinsicID() == Intrinsic::dbg_value;
00125     }
00126     static inline bool classof(const Value *V) {
00127       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00128     }
00129   };
00130 
00131   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
00132   ///
00133   class MemIntrinsic : public IntrinsicInst {
00134   public:
00135     Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
00136     const Use &getRawDestUse() const { return getArgOperandUse(0); }
00137     Use &getRawDestUse() { return getArgOperandUse(0); }
00138 
00139     Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
00140     const Use &getLengthUse() const { return getArgOperandUse(2); }
00141     Use &getLengthUse() { return getArgOperandUse(2); }
00142 
00143     ConstantInt *getAlignmentCst() const {
00144       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
00145     }
00146 
00147     unsigned getAlignment() const {
00148       return getAlignmentCst()->getZExtValue();
00149     }
00150 
00151     ConstantInt *getVolatileCst() const {
00152       return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
00153     }
00154     bool isVolatile() const {
00155       return !getVolatileCst()->isZero();
00156     }
00157 
00158     unsigned getDestAddressSpace() const {
00159       return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
00160     }
00161 
00162     /// getDest - This is just like getRawDest, but it strips off any cast
00163     /// instructions that feed it, giving the original input.  The returned
00164     /// value is guaranteed to be a pointer.
00165     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
00166 
00167     /// set* - Set the specified arguments of the instruction.
00168     ///
00169     void setDest(Value *Ptr) {
00170       assert(getRawDest()->getType() == Ptr->getType() &&
00171              "setDest called with pointer of wrong type!");
00172       setArgOperand(0, Ptr);
00173     }
00174 
00175     void setLength(Value *L) {
00176       assert(getLength()->getType() == L->getType() &&
00177              "setLength called with value of wrong type!");
00178       setArgOperand(2, L);
00179     }
00180 
00181     void setAlignment(Constant* A) {
00182       setArgOperand(3, A);
00183     }
00184 
00185     void setVolatile(Constant* V) {
00186       setArgOperand(4, V);
00187     }
00188 
00189     Type *getAlignmentType() const {
00190       return getArgOperand(3)->getType();
00191     }
00192 
00193     // Methods for support type inquiry through isa, cast, and dyn_cast:
00194     static inline bool classof(const IntrinsicInst *I) {
00195       switch (I->getIntrinsicID()) {
00196       case Intrinsic::memcpy:
00197       case Intrinsic::memmove:
00198       case Intrinsic::memset:
00199         return true;
00200       default: return false;
00201       }
00202     }
00203     static inline bool classof(const Value *V) {
00204       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00205     }
00206   };
00207 
00208   /// MemSetInst - This class wraps the llvm.memset intrinsic.
00209   ///
00210   class MemSetInst : public MemIntrinsic {
00211   public:
00212     /// get* - Return the arguments to the instruction.
00213     ///
00214     Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
00215     const Use &getValueUse() const { return getArgOperandUse(1); }
00216     Use &getValueUse() { return getArgOperandUse(1); }
00217 
00218     void setValue(Value *Val) {
00219       assert(getValue()->getType() == Val->getType() &&
00220              "setValue called with value of wrong type!");
00221       setArgOperand(1, Val);
00222     }
00223 
00224     // Methods for support type inquiry through isa, cast, and dyn_cast:
00225     static inline bool classof(const IntrinsicInst *I) {
00226       return I->getIntrinsicID() == Intrinsic::memset;
00227     }
00228     static inline bool classof(const Value *V) {
00229       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00230     }
00231   };
00232 
00233   /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
00234   ///
00235   class MemTransferInst : public MemIntrinsic {
00236   public:
00237     /// get* - Return the arguments to the instruction.
00238     ///
00239     Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
00240     const Use &getRawSourceUse() const { return getArgOperandUse(1); }
00241     Use &getRawSourceUse() { return getArgOperandUse(1); }
00242 
00243     /// getSource - This is just like getRawSource, but it strips off any cast
00244     /// instructions that feed it, giving the original input.  The returned
00245     /// value is guaranteed to be a pointer.
00246     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
00247 
00248     unsigned getSourceAddressSpace() const {
00249       return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
00250     }
00251 
00252     void setSource(Value *Ptr) {
00253       assert(getRawSource()->getType() == Ptr->getType() &&
00254              "setSource called with pointer of wrong type!");
00255       setArgOperand(1, Ptr);
00256     }
00257 
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              I->getIntrinsicID() == Intrinsic::memmove;
00262     }
00263     static inline bool classof(const Value *V) {
00264       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00265     }
00266   };
00267 
00268 
00269   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
00270   ///
00271   class MemCpyInst : 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::memcpy;
00276     }
00277     static inline bool classof(const Value *V) {
00278       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00279     }
00280   };
00281 
00282   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
00283   ///
00284   class MemMoveInst : public MemTransferInst {
00285   public:
00286     // Methods for support type inquiry through isa, cast, and dyn_cast:
00287     static inline bool classof(const IntrinsicInst *I) {
00288       return I->getIntrinsicID() == Intrinsic::memmove;
00289     }
00290     static inline bool classof(const Value *V) {
00291       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00292     }
00293   };
00294 
00295   /// VAStartInst - This represents the llvm.va_start intrinsic.
00296   ///
00297   class VAStartInst : public IntrinsicInst {
00298   public:
00299     static inline bool classof(const IntrinsicInst *I) {
00300       return I->getIntrinsicID() == Intrinsic::vastart;
00301     }
00302     static inline bool classof(const Value *V) {
00303       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00304     }
00305 
00306     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
00307   };
00308 
00309   /// VAEndInst - This represents the llvm.va_end intrinsic.
00310   ///
00311   class VAEndInst : public IntrinsicInst {
00312   public:
00313     static inline bool classof(const IntrinsicInst *I) {
00314       return I->getIntrinsicID() == Intrinsic::vaend;
00315     }
00316     static inline bool classof(const Value *V) {
00317       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00318     }
00319 
00320     Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
00321   };
00322 
00323   /// VACopyInst - This represents the llvm.va_copy intrinsic.
00324   ///
00325   class VACopyInst : public IntrinsicInst {
00326   public:
00327     static inline bool classof(const IntrinsicInst *I) {
00328       return I->getIntrinsicID() == Intrinsic::vacopy;
00329     }
00330     static inline bool classof(const Value *V) {
00331       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00332     }
00333 
00334     Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
00335     Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
00336   };
00337 
00338   /// This represents the llvm.instrprof_increment intrinsic.
00339   class InstrProfIncrementInst : public IntrinsicInst {
00340   public:
00341     static inline bool classof(const IntrinsicInst *I) {
00342       return I->getIntrinsicID() == Intrinsic::instrprof_increment;
00343     }
00344     static inline bool classof(const Value *V) {
00345       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00346     }
00347 
00348     GlobalVariable *getName() const {
00349       return cast<GlobalVariable>(
00350           const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
00351     }
00352 
00353     ConstantInt *getHash() const {
00354       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
00355     }
00356 
00357     ConstantInt *getNumCounters() const {
00358       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
00359     }
00360 
00361     ConstantInt *getIndex() const {
00362       return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
00363     }
00364   };
00365 }
00366 
00367 #endif