1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines classes that make it really easy to deal with intrinsic 11 // functions with the isa/dyncast family of functions. In particular, this 12 // allows you to do things like: 13 // 14 // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst)) 15 // ... MCI->getDest() ... MCI->getSource() ... 16 // 17 // All intrinsic function calls are instances of the call instruction, so these 18 // are all subclasses of the CallInst class. Note that none of these classes 19 // has state or virtual methods, which is an important part of this gross/neat 20 // hack working. 21 // 22 //===----------------------------------------------------------------------===// 23 24 #ifndef LLVM_IR_INTRINSICINST_H 25 #define LLVM_IR_INTRINSICINST_H 26 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/Intrinsics.h" 31 #include "llvm/IR/Metadata.h" 32 33 namespace llvm { 34 /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic 35 /// functions. This allows the standard isa/dyncast/cast functionality to 36 /// work with calls to intrinsic functions. 37 class IntrinsicInst : public CallInst { 38 IntrinsicInst() LLVM_DELETED_FUNCTION; 39 IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION; 40 void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION; 41 public: 42 /// getIntrinsicID - Return the intrinsic ID of this intrinsic. 43 /// getIntrinsicID()44 Intrinsic::ID getIntrinsicID() const { 45 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID(); 46 } 47 48 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const CallInst * I)49 static inline bool classof(const CallInst *I) { 50 if (const Function *CF = I->getCalledFunction()) 51 return CF->isIntrinsic(); 52 return false; 53 } classof(const Value * V)54 static inline bool classof(const Value *V) { 55 return isa<CallInst>(V) && classof(cast<CallInst>(V)); 56 } 57 }; 58 59 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics 60 /// 61 class DbgInfoIntrinsic : public IntrinsicInst { 62 public: 63 64 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)65 static inline bool classof(const IntrinsicInst *I) { 66 switch (I->getIntrinsicID()) { 67 case Intrinsic::dbg_declare: 68 case Intrinsic::dbg_value: 69 return true; 70 default: return false; 71 } 72 } classof(const Value * V)73 static inline bool classof(const Value *V) { 74 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 75 } 76 77 static Value *StripCast(Value *C); 78 }; 79 80 /// DbgDeclareInst - This represents the llvm.dbg.declare instruction. 81 /// 82 class DbgDeclareInst : public DbgInfoIntrinsic { 83 public: 84 Value *getAddress() const; getVariable()85 MDNode *getVariable() const { 86 return cast<MDNode>( 87 cast<MetadataAsValue>(getArgOperand(1))->getMetadata()); 88 } getExpression()89 MDNode *getExpression() const { 90 return cast<MDNode>( 91 cast<MetadataAsValue>(getArgOperand(2))->getMetadata()); 92 } 93 94 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)95 static inline bool classof(const IntrinsicInst *I) { 96 return I->getIntrinsicID() == Intrinsic::dbg_declare; 97 } classof(const Value * V)98 static inline bool classof(const Value *V) { 99 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 100 } 101 }; 102 103 /// DbgValueInst - This represents the llvm.dbg.value instruction. 104 /// 105 class DbgValueInst : public DbgInfoIntrinsic { 106 public: 107 const Value *getValue() const; 108 Value *getValue(); getOffset()109 uint64_t getOffset() const { 110 return cast<ConstantInt>( 111 const_cast<Value*>(getArgOperand(1)))->getZExtValue(); 112 } getVariable()113 MDNode *getVariable() const { 114 return cast<MDNode>( 115 cast<MetadataAsValue>(getArgOperand(2))->getMetadata()); 116 } getExpression()117 MDNode *getExpression() const { 118 return cast<MDNode>( 119 cast<MetadataAsValue>(getArgOperand(3))->getMetadata()); 120 } 121 122 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)123 static inline bool classof(const IntrinsicInst *I) { 124 return I->getIntrinsicID() == Intrinsic::dbg_value; 125 } classof(const Value * V)126 static inline bool classof(const Value *V) { 127 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 128 } 129 }; 130 131 /// MemIntrinsic - This is the common base class for memset/memcpy/memmove. 132 /// 133 class MemIntrinsic : public IntrinsicInst { 134 public: getRawDest()135 Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); } getRawDestUse()136 const Use &getRawDestUse() const { return getArgOperandUse(0); } getRawDestUse()137 Use &getRawDestUse() { return getArgOperandUse(0); } 138 getLength()139 Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); } getLengthUse()140 const Use &getLengthUse() const { return getArgOperandUse(2); } getLengthUse()141 Use &getLengthUse() { return getArgOperandUse(2); } 142 getAlignmentCst()143 ConstantInt *getAlignmentCst() const { 144 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3))); 145 } 146 getAlignment()147 unsigned getAlignment() const { 148 return getAlignmentCst()->getZExtValue(); 149 } 150 getVolatileCst()151 ConstantInt *getVolatileCst() const { 152 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4))); 153 } isVolatile()154 bool isVolatile() const { 155 return !getVolatileCst()->isZero(); 156 } 157 getDestAddressSpace()158 unsigned getDestAddressSpace() const { 159 return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); 160 } 161 162 /// getDest - This is just like getRawDest, but it strips off any cast 163 /// instructions that feed it, giving the original input. The returned 164 /// value is guaranteed to be a pointer. getDest()165 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 166 167 /// set* - Set the specified arguments of the instruction. 168 /// setDest(Value * Ptr)169 void setDest(Value *Ptr) { 170 assert(getRawDest()->getType() == Ptr->getType() && 171 "setDest called with pointer of wrong type!"); 172 setArgOperand(0, Ptr); 173 } 174 setLength(Value * L)175 void setLength(Value *L) { 176 assert(getLength()->getType() == L->getType() && 177 "setLength called with value of wrong type!"); 178 setArgOperand(2, L); 179 } 180 setAlignment(Constant * A)181 void setAlignment(Constant* A) { 182 setArgOperand(3, A); 183 } 184 setVolatile(Constant * V)185 void setVolatile(Constant* V) { 186 setArgOperand(4, V); 187 } 188 getAlignmentType()189 Type *getAlignmentType() const { 190 return getArgOperand(3)->getType(); 191 } 192 193 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)194 static inline bool classof(const IntrinsicInst *I) { 195 switch (I->getIntrinsicID()) { 196 case Intrinsic::memcpy: 197 case Intrinsic::memmove: 198 case Intrinsic::memset: 199 return true; 200 default: return false; 201 } 202 } classof(const Value * V)203 static inline bool classof(const Value *V) { 204 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 205 } 206 }; 207 208 /// MemSetInst - This class wraps the llvm.memset intrinsic. 209 /// 210 class MemSetInst : public MemIntrinsic { 211 public: 212 /// get* - Return the arguments to the instruction. 213 /// getValue()214 Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); } getValueUse()215 const Use &getValueUse() const { return getArgOperandUse(1); } getValueUse()216 Use &getValueUse() { return getArgOperandUse(1); } 217 setValue(Value * Val)218 void setValue(Value *Val) { 219 assert(getValue()->getType() == Val->getType() && 220 "setValue called with value of wrong type!"); 221 setArgOperand(1, Val); 222 } 223 224 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)225 static inline bool classof(const IntrinsicInst *I) { 226 return I->getIntrinsicID() == Intrinsic::memset; 227 } classof(const Value * V)228 static inline bool classof(const Value *V) { 229 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 230 } 231 }; 232 233 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics. 234 /// 235 class MemTransferInst : public MemIntrinsic { 236 public: 237 /// get* - Return the arguments to the instruction. 238 /// getRawSource()239 Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); } getRawSourceUse()240 const Use &getRawSourceUse() const { return getArgOperandUse(1); } getRawSourceUse()241 Use &getRawSourceUse() { return getArgOperandUse(1); } 242 243 /// getSource - This is just like getRawSource, but it strips off any cast 244 /// instructions that feed it, giving the original input. The returned 245 /// value is guaranteed to be a pointer. getSource()246 Value *getSource() const { return getRawSource()->stripPointerCasts(); } 247 getSourceAddressSpace()248 unsigned getSourceAddressSpace() const { 249 return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); 250 } 251 setSource(Value * Ptr)252 void setSource(Value *Ptr) { 253 assert(getRawSource()->getType() == Ptr->getType() && 254 "setSource called with pointer of wrong type!"); 255 setArgOperand(1, Ptr); 256 } 257 258 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)259 static inline bool classof(const IntrinsicInst *I) { 260 return I->getIntrinsicID() == Intrinsic::memcpy || 261 I->getIntrinsicID() == Intrinsic::memmove; 262 } classof(const Value * V)263 static inline bool classof(const Value *V) { 264 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 265 } 266 }; 267 268 269 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic. 270 /// 271 class MemCpyInst : public MemTransferInst { 272 public: 273 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)274 static inline bool classof(const IntrinsicInst *I) { 275 return I->getIntrinsicID() == Intrinsic::memcpy; 276 } classof(const Value * V)277 static inline bool classof(const Value *V) { 278 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 279 } 280 }; 281 282 /// MemMoveInst - This class wraps the llvm.memmove intrinsic. 283 /// 284 class MemMoveInst : public MemTransferInst { 285 public: 286 // Methods for support type inquiry through isa, cast, and dyn_cast: classof(const IntrinsicInst * I)287 static inline bool classof(const IntrinsicInst *I) { 288 return I->getIntrinsicID() == Intrinsic::memmove; 289 } classof(const Value * V)290 static inline bool classof(const Value *V) { 291 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 292 } 293 }; 294 295 /// VAStartInst - This represents the llvm.va_start intrinsic. 296 /// 297 class VAStartInst : public IntrinsicInst { 298 public: classof(const IntrinsicInst * I)299 static inline bool classof(const IntrinsicInst *I) { 300 return I->getIntrinsicID() == Intrinsic::vastart; 301 } classof(const Value * V)302 static inline bool classof(const Value *V) { 303 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 304 } 305 getArgList()306 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } 307 }; 308 309 /// VAEndInst - This represents the llvm.va_end intrinsic. 310 /// 311 class VAEndInst : public IntrinsicInst { 312 public: classof(const IntrinsicInst * I)313 static inline bool classof(const IntrinsicInst *I) { 314 return I->getIntrinsicID() == Intrinsic::vaend; 315 } classof(const Value * V)316 static inline bool classof(const Value *V) { 317 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 318 } 319 getArgList()320 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } 321 }; 322 323 /// VACopyInst - This represents the llvm.va_copy intrinsic. 324 /// 325 class VACopyInst : public IntrinsicInst { 326 public: classof(const IntrinsicInst * I)327 static inline bool classof(const IntrinsicInst *I) { 328 return I->getIntrinsicID() == Intrinsic::vacopy; 329 } classof(const Value * V)330 static inline bool classof(const Value *V) { 331 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 332 } 333 getDest()334 Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); } getSrc()335 Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); } 336 }; 337 338 /// This represents the llvm.instrprof_increment intrinsic. 339 class InstrProfIncrementInst : public IntrinsicInst { 340 public: classof(const IntrinsicInst * I)341 static inline bool classof(const IntrinsicInst *I) { 342 return I->getIntrinsicID() == Intrinsic::instrprof_increment; 343 } classof(const Value * V)344 static inline bool classof(const Value *V) { 345 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 346 } 347 getName()348 GlobalVariable *getName() const { 349 return cast<GlobalVariable>( 350 const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); 351 } 352 getHash()353 ConstantInt *getHash() const { 354 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 355 } 356 getNumCounters()357 ConstantInt *getNumCounters() const { 358 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2))); 359 } 360 getIndex()361 ConstantInt *getIndex() const { 362 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 363 } 364 }; 365 } 366 367 #endif 368