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_INTRINSICINST_H
00025 #define LLVM_INTRINSICINST_H
00026 
00027 #include "llvm/Constants.h"
00028 #include "llvm/Function.h"
00029 #include "llvm/Instructions.h"
00030 #include "llvm/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();                      // DO NOT IMPLEMENT
00038     IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
00039     void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
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 IntrinsicInst *) { return true; }
00049     static inline bool classof(const CallInst *I) {
00050       if (const Function *CF = I->getCalledFunction())
00051         return CF->getIntrinsicID() != 0;
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   struct DbgInfoIntrinsic : public IntrinsicInst {
00062 
00063     // Methods for support type inquiry through isa, cast, and dyn_cast:
00064     static inline bool classof(const DbgInfoIntrinsic *) { return true; }
00065     static inline bool classof(const IntrinsicInst *I) {
00066       switch (I->getIntrinsicID()) {
00067       case Intrinsic::dbg_stoppoint:
00068       case Intrinsic::dbg_func_start:
00069       case Intrinsic::dbg_region_start:
00070       case Intrinsic::dbg_region_end:
00071       case Intrinsic::dbg_declare:
00072         return true;
00073       default: return false;
00074       }
00075     }
00076     static inline bool classof(const Value *V) {
00077       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00078     }
00079     
00080     static Value *StripCast(Value *C);
00081   };
00082 
00083   /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
00084   ///
00085   struct DbgStopPointInst : public DbgInfoIntrinsic {
00086     Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
00087     Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
00088     Value *getContext() const {
00089       return StripCast(getOperand(3));
00090     }
00091 
00092     unsigned getLine() const {
00093       return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
00094     }
00095     unsigned getColumn() const {
00096       return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
00097     }
00098     
00099     Value* getFileName() const;
00100     Value* getDirectory() const;
00101 
00102     // Methods for support type inquiry through isa, cast, and dyn_cast:
00103     static inline bool classof(const DbgStopPointInst *) { return true; }
00104     static inline bool classof(const IntrinsicInst *I) {
00105       return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
00106     }
00107     static inline bool classof(const Value *V) {
00108       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00109     }
00110   };
00111   
00112   /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
00113   ///
00114   struct DbgFuncStartInst : public DbgInfoIntrinsic {
00115     Value *getSubprogram() const { return StripCast(getOperand(1)); }
00116 
00117     // Methods for support type inquiry through isa, cast, and dyn_cast:
00118     static inline bool classof(const DbgFuncStartInst *) { return true; }
00119     static inline bool classof(const IntrinsicInst *I) {
00120       return I->getIntrinsicID() == Intrinsic::dbg_func_start;
00121     }
00122     static inline bool classof(const Value *V) {
00123       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00124     }
00125   };
00126 
00127   /// DbgRegionStartInst - This represents the llvm.dbg.region.start
00128   /// instruction.
00129   struct DbgRegionStartInst : public DbgInfoIntrinsic {
00130     Value *getContext() const { return StripCast(getOperand(1)); }
00131 
00132     // Methods for support type inquiry through isa, cast, and dyn_cast:
00133     static inline bool classof(const DbgRegionStartInst *) { return true; }
00134     static inline bool classof(const IntrinsicInst *I) {
00135       return I->getIntrinsicID() == Intrinsic::dbg_region_start;
00136     }
00137     static inline bool classof(const Value *V) {
00138       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00139     }
00140   };
00141 
00142   /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
00143   ///
00144   struct DbgRegionEndInst : public DbgInfoIntrinsic {
00145     Value *getContext() const { return StripCast(getOperand(1)); }
00146 
00147     // Methods for support type inquiry through isa, cast, and dyn_cast:
00148     static inline bool classof(const DbgRegionEndInst *) { return true; }
00149     static inline bool classof(const IntrinsicInst *I) {
00150       return I->getIntrinsicID() == Intrinsic::dbg_region_end;
00151     }
00152     static inline bool classof(const Value *V) {
00153       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00154     }
00155   };
00156 
00157   /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
00158   ///
00159   struct DbgDeclareInst : public DbgInfoIntrinsic {
00160     Value *getAddress()  const { return getOperand(1); }
00161     Value *getVariable() const { return StripCast(getOperand(2)); }
00162 
00163     // Methods for support type inquiry through isa, cast, and dyn_cast:
00164     static inline bool classof(const DbgDeclareInst *) { return true; }
00165     static inline bool classof(const IntrinsicInst *I) {
00166       return I->getIntrinsicID() == Intrinsic::dbg_declare;
00167     }
00168     static inline bool classof(const Value *V) {
00169       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00170     }
00171   };
00172 
00173   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
00174   ///
00175   struct MemIntrinsic : public IntrinsicInst {
00176     Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
00177 
00178     Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
00179     ConstantInt *getAlignment() const {
00180       return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
00181     }
00182 
00183     /// getDest - This is just like getRawDest, but it strips off any cast
00184     /// instructions that feed it, giving the original input.  The returned
00185     /// value is guaranteed to be a pointer.
00186     Value *getDest() const { return getRawDest()->stripPointerCasts(); }
00187 
00188     /// set* - Set the specified arguments of the instruction.
00189     ///
00190     void setDest(Value *Ptr) {
00191       assert(getRawDest()->getType() == Ptr->getType() &&
00192              "setDest called with pointer of wrong type!");
00193       setOperand(1, Ptr);
00194     }
00195 
00196     void setLength(Value *L) {
00197       assert(getLength()->getType() == L->getType() &&
00198              "setLength called with value of wrong type!");
00199       setOperand(3, L);
00200     }
00201     void setAlignment(ConstantInt *A) {
00202       assert(getAlignment()->getType() == A->getType() &&
00203              "setAlignment called with value of wrong type!");
00204       setOperand(4, A);
00205     }
00206 
00207     // Methods for support type inquiry through isa, cast, and dyn_cast:
00208     static inline bool classof(const MemIntrinsic *) { return true; }
00209     static inline bool classof(const IntrinsicInst *I) {
00210       switch (I->getIntrinsicID()) {
00211       case Intrinsic::memcpy:
00212       case Intrinsic::memmove:
00213       case Intrinsic::memset:
00214         return true;
00215       default: return false;
00216       }
00217     }
00218     static inline bool classof(const Value *V) {
00219       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00220     }
00221   };
00222 
00223 
00224   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
00225   ///
00226   struct MemCpyInst : public MemIntrinsic {
00227     /// get* - Return the arguments to the instruction.
00228     ///
00229     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
00230 
00231     /// getSource - This is just like getRawSource, but it strips off any cast
00232     /// instructions that feed it, giving the original input.  The returned
00233     /// value is guaranteed to be a pointer.
00234     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
00235 
00236 
00237     void setSource(Value *Ptr) {
00238       assert(getRawSource()->getType() == Ptr->getType() &&
00239              "setSource called with pointer of wrong type!");
00240       setOperand(2, Ptr);
00241     }
00242 
00243     // Methods for support type inquiry through isa, cast, and dyn_cast:
00244     static inline bool classof(const MemCpyInst *) { return true; }
00245     static inline bool classof(const IntrinsicInst *I) {
00246       return I->getIntrinsicID() == Intrinsic::memcpy;
00247     }
00248     static inline bool classof(const Value *V) {
00249       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00250     }
00251   };
00252 
00253   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
00254   ///
00255   struct MemMoveInst : public MemIntrinsic {
00256     /// get* - Return the arguments to the instruction.
00257     ///
00258     Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
00259 
00260     /// getSource - This is just like getRawSource, but it strips off any cast
00261     /// instructions that feed it, giving the original input.  The returned
00262     /// value is guaranteed to be a pointer.
00263     Value *getSource() const { return getRawSource()->stripPointerCasts(); }
00264 
00265     void setSource(Value *Ptr) {
00266       assert(getRawSource()->getType() == Ptr->getType() &&
00267              "setSource called with pointer of wrong type!");
00268       setOperand(2, Ptr);
00269     }
00270 
00271     // Methods for support type inquiry through isa, cast, and dyn_cast:
00272     static inline bool classof(const MemMoveInst *) { return true; }
00273     static inline bool classof(const IntrinsicInst *I) {
00274       return I->getIntrinsicID() == Intrinsic::memmove;
00275     }
00276     static inline bool classof(const Value *V) {
00277       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00278     }
00279   };
00280 
00281   /// MemSetInst - This class wraps the llvm.memset intrinsic.
00282   ///
00283   struct MemSetInst : public MemIntrinsic {
00284     /// get* - Return the arguments to the instruction.
00285     ///
00286     Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
00287 
00288     void setValue(Value *Val) {
00289       assert(getValue()->getType() == Val->getType() &&
00290              "setSource called with pointer of wrong type!");
00291       setOperand(2, Val);
00292     }
00293 
00294     // Methods for support type inquiry through isa, cast, and dyn_cast:
00295     static inline bool classof(const MemSetInst *) { return true; }
00296     static inline bool classof(const IntrinsicInst *I) {
00297       return I->getIntrinsicID() == Intrinsic::memset;
00298     }
00299     static inline bool classof(const Value *V) {
00300       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00301     }
00302   };
00303 
00304   /// EHSelectorInst - This represents the llvm.eh.selector instruction.
00305   ///
00306   struct EHSelectorInst : public IntrinsicInst {
00307     // Methods for support type inquiry through isa, cast, and dyn_cast:
00308     static inline bool classof(const EHSelectorInst *) { return true; }
00309     static inline bool classof(const IntrinsicInst *I) {
00310       return I->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
00311              I->getIntrinsicID() == Intrinsic::eh_selector_i64;
00312     }
00313     static inline bool classof(const Value *V) {
00314       return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
00315     }
00316   };
00317   
00318 }
00319 
00320 #endif