LLVM API Documentation
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 *getAlignmentCst() const { 00180 return cast<ConstantInt>(const_cast<Value*>(getOperand(4))); 00181 } 00182 00183 unsigned getAlignment() const { 00184 return getAlignmentCst()->getZExtValue(); 00185 } 00186 00187 /// getDest - This is just like getRawDest, but it strips off any cast 00188 /// instructions that feed it, giving the original input. The returned 00189 /// value is guaranteed to be a pointer. 00190 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 00191 00192 /// set* - Set the specified arguments of the instruction. 00193 /// 00194 void setDest(Value *Ptr) { 00195 assert(getRawDest()->getType() == Ptr->getType() && 00196 "setDest called with pointer of wrong type!"); 00197 setOperand(1, Ptr); 00198 } 00199 00200 void setLength(Value *L) { 00201 assert(getLength()->getType() == L->getType() && 00202 "setLength called with value of wrong type!"); 00203 setOperand(3, L); 00204 } 00205 00206 void setAlignment(Constant* A) { 00207 setOperand(4, A); 00208 } 00209 00210 const Type *getAlignmentType() const { 00211 return getOperand(4)->getType(); 00212 } 00213 00214 // Methods for support type inquiry through isa, cast, and dyn_cast: 00215 static inline bool classof(const MemIntrinsic *) { return true; } 00216 static inline bool classof(const IntrinsicInst *I) { 00217 switch (I->getIntrinsicID()) { 00218 case Intrinsic::memcpy: 00219 case Intrinsic::memmove: 00220 case Intrinsic::memset: 00221 return true; 00222 default: return false; 00223 } 00224 } 00225 static inline bool classof(const Value *V) { 00226 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 00227 } 00228 }; 00229 00230 /// MemSetInst - This class wraps the llvm.memset intrinsic. 00231 /// 00232 struct MemSetInst : public MemIntrinsic { 00233 /// get* - Return the arguments to the instruction. 00234 /// 00235 Value *getValue() const { return const_cast<Value*>(getOperand(2)); } 00236 00237 void setValue(Value *Val) { 00238 assert(getValue()->getType() == Val->getType() && 00239 "setSource called with pointer of wrong type!"); 00240 setOperand(2, Val); 00241 } 00242 00243 // Methods for support type inquiry through isa, cast, and dyn_cast: 00244 static inline bool classof(const MemSetInst *) { return true; } 00245 static inline bool classof(const IntrinsicInst *I) { 00246 return I->getIntrinsicID() == Intrinsic::memset; 00247 } 00248 static inline bool classof(const Value *V) { 00249 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 00250 } 00251 }; 00252 00253 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics. 00254 /// 00255 struct MemTransferInst : 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 MemTransferInst *) { return true; } 00273 static inline bool classof(const IntrinsicInst *I) { 00274 return I->getIntrinsicID() == Intrinsic::memcpy || 00275 I->getIntrinsicID() == Intrinsic::memmove; 00276 } 00277 static inline bool classof(const Value *V) { 00278 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 00279 } 00280 }; 00281 00282 00283 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic. 00284 /// 00285 struct MemCpyInst : public MemTransferInst { 00286 // Methods for support type inquiry through isa, cast, and dyn_cast: 00287 static inline bool classof(const MemCpyInst *) { return true; } 00288 static inline bool classof(const IntrinsicInst *I) { 00289 return I->getIntrinsicID() == Intrinsic::memcpy; 00290 } 00291 static inline bool classof(const Value *V) { 00292 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 00293 } 00294 }; 00295 00296 /// MemMoveInst - This class wraps the llvm.memmove intrinsic. 00297 /// 00298 struct MemMoveInst : public MemTransferInst { 00299 // Methods for support type inquiry through isa, cast, and dyn_cast: 00300 static inline bool classof(const MemMoveInst *) { return true; } 00301 static inline bool classof(const IntrinsicInst *I) { 00302 return I->getIntrinsicID() == Intrinsic::memmove; 00303 } 00304 static inline bool classof(const Value *V) { 00305 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 00306 } 00307 }; 00308 00309 /// EHSelectorInst - This represents the llvm.eh.selector instruction. 00310 /// 00311 struct EHSelectorInst : public IntrinsicInst { 00312 // Methods for support type inquiry through isa, cast, and dyn_cast: 00313 static inline bool classof(const EHSelectorInst *) { return true; } 00314 static inline bool classof(const IntrinsicInst *I) { 00315 return I->getIntrinsicID() == Intrinsic::eh_selector_i32 || 00316 I->getIntrinsicID() == Intrinsic::eh_selector_i64; 00317 } 00318 static inline bool classof(const Value *V) { 00319 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 00320 } 00321 }; 00322 00323 } 00324 00325 #endif