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