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