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 /// A wrapper class for inspecting calls to intrinsic functions. 35 /// This allows the standard isa/dyncast/cast functionality to work with calls 36 /// to intrinsic functions. 37 class IntrinsicInst : public CallInst { 38 IntrinsicInst() = delete; 39 IntrinsicInst(const IntrinsicInst&) = delete; 40 void operator=(const IntrinsicInst&) = delete; 41 public: 42 /// Return the intrinsic ID of this intrinsic. 43 Intrinsic::ID getIntrinsicID() const { 44 return getCalledFunction()->getIntrinsicID(); 45 } 46 47 // Methods for support type inquiry through isa, cast, and dyn_cast: 48 static inline bool classof(const CallInst *I) { 49 if (const Function *CF = I->getCalledFunction()) 50 return CF->isIntrinsic(); 51 return false; 52 } 53 static inline bool classof(const Value *V) { 54 return isa<CallInst>(V) && classof(cast<CallInst>(V)); 55 } 56 }; 57 58 /// This is the common base class for debug info intrinsics. 59 class DbgInfoIntrinsic : public IntrinsicInst { 60 public: 61 /// Get the location corresponding to the variable referenced by the debug 62 /// info intrinsic. Depending on the intrinsic, this could be the 63 /// variable's value or its address. 64 Value *getVariableLocation(bool AllowNullOp = true) const; 65 66 // Methods for support type inquiry through isa, cast, and dyn_cast: 67 static inline bool classof(const IntrinsicInst *I) { 68 switch (I->getIntrinsicID()) { 69 case Intrinsic::dbg_declare: 70 case Intrinsic::dbg_value: 71 return true; 72 default: return false; 73 } 74 } 75 static inline bool classof(const Value *V) { 76 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 77 } 78 }; 79 80 /// This represents the llvm.dbg.declare instruction. 81 class DbgDeclareInst : public DbgInfoIntrinsic { 82 public: 83 Value *getAddress() const { return getVariableLocation(); } 84 DILocalVariable *getVariable() const { 85 return cast<DILocalVariable>(getRawVariable()); 86 } 87 DIExpression *getExpression() const { 88 return cast<DIExpression>(getRawExpression()); 89 } 90 91 Metadata *getRawVariable() const { 92 return cast<MetadataAsValue>(getArgOperand(1))->getMetadata(); 93 } 94 Metadata *getRawExpression() const { 95 return cast<MetadataAsValue>(getArgOperand(2))->getMetadata(); 96 } 97 98 // Methods for support type inquiry through isa, cast, and dyn_cast: 99 static inline bool classof(const IntrinsicInst *I) { 100 return I->getIntrinsicID() == Intrinsic::dbg_declare; 101 } 102 static inline bool classof(const Value *V) { 103 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 104 } 105 }; 106 107 /// This represents the llvm.dbg.value instruction. 108 class DbgValueInst : public DbgInfoIntrinsic { 109 public: 110 Value *getValue() const { 111 return getVariableLocation(/* AllowNullOp = */ false); 112 } 113 uint64_t getOffset() const { 114 return cast<ConstantInt>( 115 const_cast<Value*>(getArgOperand(1)))->getZExtValue(); 116 } 117 DILocalVariable *getVariable() const { 118 return cast<DILocalVariable>(getRawVariable()); 119 } 120 DIExpression *getExpression() const { 121 return cast<DIExpression>(getRawExpression()); 122 } 123 124 Metadata *getRawVariable() const { 125 return cast<MetadataAsValue>(getArgOperand(2))->getMetadata(); 126 } 127 Metadata *getRawExpression() const { 128 return cast<MetadataAsValue>(getArgOperand(3))->getMetadata(); 129 } 130 131 // Methods for support type inquiry through isa, cast, and dyn_cast: 132 static inline bool classof(const IntrinsicInst *I) { 133 return I->getIntrinsicID() == Intrinsic::dbg_value; 134 } 135 static inline bool classof(const Value *V) { 136 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 137 } 138 }; 139 140 /// This is the common base class for memset/memcpy/memmove. 141 class MemIntrinsic : public IntrinsicInst { 142 public: 143 Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); } 144 const Use &getRawDestUse() const { return getArgOperandUse(0); } 145 Use &getRawDestUse() { return getArgOperandUse(0); } 146 147 Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); } 148 const Use &getLengthUse() const { return getArgOperandUse(2); } 149 Use &getLengthUse() { return getArgOperandUse(2); } 150 151 ConstantInt *getAlignmentCst() const { 152 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3))); 153 } 154 155 unsigned getAlignment() const { 156 return getAlignmentCst()->getZExtValue(); 157 } 158 159 ConstantInt *getVolatileCst() const { 160 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4))); 161 } 162 bool isVolatile() const { 163 return !getVolatileCst()->isZero(); 164 } 165 166 unsigned getDestAddressSpace() const { 167 return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); 168 } 169 170 /// This is just like getRawDest, but it strips off any cast 171 /// instructions that feed it, giving the original input. The returned 172 /// value is guaranteed to be a pointer. 173 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 174 175 /// Set the specified arguments of the instruction. 176 void setDest(Value *Ptr) { 177 assert(getRawDest()->getType() == Ptr->getType() && 178 "setDest called with pointer of wrong type!"); 179 setArgOperand(0, Ptr); 180 } 181 182 void setLength(Value *L) { 183 assert(getLength()->getType() == L->getType() && 184 "setLength called with value of wrong type!"); 185 setArgOperand(2, L); 186 } 187 188 void setAlignment(Constant* A) { 189 setArgOperand(3, A); 190 } 191 192 void setVolatile(Constant* V) { 193 setArgOperand(4, V); 194 } 195 196 Type *getAlignmentType() const { 197 return getArgOperand(3)->getType(); 198 } 199 200 // Methods for support type inquiry through isa, cast, and dyn_cast: 201 static inline bool classof(const IntrinsicInst *I) { 202 switch (I->getIntrinsicID()) { 203 case Intrinsic::memcpy: 204 case Intrinsic::memmove: 205 case Intrinsic::memset: 206 return true; 207 default: return false; 208 } 209 } 210 static inline bool classof(const Value *V) { 211 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 212 } 213 }; 214 215 /// This class wraps the llvm.memset intrinsic. 216 class MemSetInst : public MemIntrinsic { 217 public: 218 /// Return the arguments to the instruction. 219 Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); } 220 const Use &getValueUse() const { return getArgOperandUse(1); } 221 Use &getValueUse() { return getArgOperandUse(1); } 222 223 void setValue(Value *Val) { 224 assert(getValue()->getType() == Val->getType() && 225 "setValue called with value of wrong type!"); 226 setArgOperand(1, Val); 227 } 228 229 // Methods for support type inquiry through isa, cast, and dyn_cast: 230 static inline bool classof(const IntrinsicInst *I) { 231 return I->getIntrinsicID() == Intrinsic::memset; 232 } 233 static inline bool classof(const Value *V) { 234 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 235 } 236 }; 237 238 /// This class wraps the llvm.memcpy/memmove intrinsics. 239 class MemTransferInst : public MemIntrinsic { 240 public: 241 /// Return the arguments to the instruction. 242 Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); } 243 const Use &getRawSourceUse() const { return getArgOperandUse(1); } 244 Use &getRawSourceUse() { return getArgOperandUse(1); } 245 246 /// This is just like getRawSource, but it strips off any cast 247 /// instructions that feed it, giving the original input. The returned 248 /// value is guaranteed to be a pointer. 249 Value *getSource() const { return getRawSource()->stripPointerCasts(); } 250 251 unsigned getSourceAddressSpace() const { 252 return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); 253 } 254 255 void setSource(Value *Ptr) { 256 assert(getRawSource()->getType() == Ptr->getType() && 257 "setSource called with pointer of wrong type!"); 258 setArgOperand(1, Ptr); 259 } 260 261 // Methods for support type inquiry through isa, cast, and dyn_cast: 262 static inline bool classof(const IntrinsicInst *I) { 263 return I->getIntrinsicID() == Intrinsic::memcpy || 264 I->getIntrinsicID() == Intrinsic::memmove; 265 } 266 static inline bool classof(const Value *V) { 267 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 268 } 269 }; 270 271 272 /// This class wraps the llvm.memcpy intrinsic. 273 class MemCpyInst : public MemTransferInst { 274 public: 275 // Methods for support type inquiry through isa, cast, and dyn_cast: 276 static inline bool classof(const IntrinsicInst *I) { 277 return I->getIntrinsicID() == Intrinsic::memcpy; 278 } 279 static inline bool classof(const Value *V) { 280 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 281 } 282 }; 283 284 /// This class wraps the llvm.memmove intrinsic. 285 class MemMoveInst : public MemTransferInst { 286 public: 287 // Methods for support type inquiry through isa, cast, and dyn_cast: 288 static inline bool classof(const IntrinsicInst *I) { 289 return I->getIntrinsicID() == Intrinsic::memmove; 290 } 291 static inline bool classof(const Value *V) { 292 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 293 } 294 }; 295 296 /// This represents the llvm.va_start intrinsic. 297 class VAStartInst : public IntrinsicInst { 298 public: 299 static inline bool classof(const IntrinsicInst *I) { 300 return I->getIntrinsicID() == Intrinsic::vastart; 301 } 302 static inline bool classof(const Value *V) { 303 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 304 } 305 306 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } 307 }; 308 309 /// This represents the llvm.va_end intrinsic. 310 class VAEndInst : public IntrinsicInst { 311 public: 312 static inline bool classof(const IntrinsicInst *I) { 313 return I->getIntrinsicID() == Intrinsic::vaend; 314 } 315 static inline bool classof(const Value *V) { 316 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 317 } 318 319 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } 320 }; 321 322 /// This represents the llvm.va_copy intrinsic. 323 class VACopyInst : public IntrinsicInst { 324 public: 325 static inline bool classof(const IntrinsicInst *I) { 326 return I->getIntrinsicID() == Intrinsic::vacopy; 327 } 328 static inline bool classof(const Value *V) { 329 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 330 } 331 332 Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); } 333 Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); } 334 }; 335 336 /// This represents the llvm.instrprof_increment intrinsic. 337 class InstrProfIncrementInst : public IntrinsicInst { 338 public: 339 static inline bool classof(const IntrinsicInst *I) { 340 return I->getIntrinsicID() == Intrinsic::instrprof_increment; 341 } 342 static inline bool classof(const Value *V) { 343 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 344 } 345 346 GlobalVariable *getName() const { 347 return cast<GlobalVariable>( 348 const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); 349 } 350 351 ConstantInt *getHash() const { 352 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 353 } 354 355 ConstantInt *getNumCounters() const { 356 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2))); 357 } 358 359 ConstantInt *getIndex() const { 360 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 361 } 362 }; 363 364 /// This represents the llvm.instrprof_value_profile intrinsic. 365 class InstrProfValueProfileInst : public IntrinsicInst { 366 public: 367 static inline bool classof(const IntrinsicInst *I) { 368 return I->getIntrinsicID() == Intrinsic::instrprof_value_profile; 369 } 370 static inline bool classof(const Value *V) { 371 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 372 } 373 374 GlobalVariable *getName() const { 375 return cast<GlobalVariable>( 376 const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); 377 } 378 379 ConstantInt *getHash() const { 380 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 381 } 382 383 Value *getTargetValue() const { 384 return cast<Value>(const_cast<Value *>(getArgOperand(2))); 385 } 386 387 ConstantInt *getValueKind() const { 388 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 389 } 390 391 // Returns the value site index. 392 ConstantInt *getIndex() const { 393 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4))); 394 } 395 }; 396 } // namespace llvm 397 398 #endif 399