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