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/DerivedTypes.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/GlobalVariable.h" 31 #include "llvm/IR/Instructions.h" 32 #include "llvm/IR/Intrinsics.h" 33 #include "llvm/IR/Metadata.h" 34 #include "llvm/IR/Value.h" 35 #include "llvm/Support/Casting.h" 36 #include <cassert> 37 #include <cstdint> 38 39 namespace llvm { 40 41 /// A wrapper class for inspecting calls to intrinsic functions. 42 /// This allows the standard isa/dyncast/cast functionality to work with calls 43 /// to intrinsic functions. 44 class IntrinsicInst : public CallInst { 45 public: 46 IntrinsicInst() = delete; 47 IntrinsicInst(const IntrinsicInst &) = delete; 48 IntrinsicInst &operator=(const IntrinsicInst &) = delete; 49 50 /// Return the intrinsic ID of this intrinsic. 51 Intrinsic::ID getIntrinsicID() const { 52 return getCalledFunction()->getIntrinsicID(); 53 } 54 55 // Methods for support type inquiry through isa, cast, and dyn_cast: 56 static inline bool classof(const CallInst *I) { 57 if (const Function *CF = I->getCalledFunction()) 58 return CF->isIntrinsic(); 59 return false; 60 } 61 static inline bool classof(const Value *V) { 62 return isa<CallInst>(V) && classof(cast<CallInst>(V)); 63 } 64 }; 65 66 /// This is the common base class for debug info intrinsics. 67 class DbgInfoIntrinsic : public IntrinsicInst { 68 public: 69 /// Get the location corresponding to the variable referenced by the debug 70 /// info intrinsic. Depending on the intrinsic, this could be the 71 /// variable's value or its address. 72 Value *getVariableLocation(bool AllowNullOp = true) const; 73 74 // Methods for support type inquiry through isa, cast, and dyn_cast: 75 static inline bool classof(const IntrinsicInst *I) { 76 switch (I->getIntrinsicID()) { 77 case Intrinsic::dbg_declare: 78 case Intrinsic::dbg_value: 79 return true; 80 default: return false; 81 } 82 } 83 static inline bool classof(const Value *V) { 84 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 85 } 86 }; 87 88 /// This represents the llvm.dbg.declare instruction. 89 class DbgDeclareInst : public DbgInfoIntrinsic { 90 public: 91 Value *getAddress() const { return getVariableLocation(); } 92 93 DILocalVariable *getVariable() const { 94 return cast<DILocalVariable>(getRawVariable()); 95 } 96 97 DIExpression *getExpression() const { 98 return cast<DIExpression>(getRawExpression()); 99 } 100 101 Metadata *getRawVariable() const { 102 return cast<MetadataAsValue>(getArgOperand(1))->getMetadata(); 103 } 104 105 Metadata *getRawExpression() const { 106 return cast<MetadataAsValue>(getArgOperand(2))->getMetadata(); 107 } 108 109 // Methods for support type inquiry through isa, cast, and dyn_cast: 110 static inline bool classof(const IntrinsicInst *I) { 111 return I->getIntrinsicID() == Intrinsic::dbg_declare; 112 } 113 static inline bool classof(const Value *V) { 114 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 115 } 116 }; 117 118 /// This represents the llvm.dbg.value instruction. 119 class DbgValueInst : public DbgInfoIntrinsic { 120 public: 121 Value *getValue() const { 122 return getVariableLocation(/* AllowNullOp = */ false); 123 } 124 125 uint64_t getOffset() const { 126 return cast<ConstantInt>( 127 const_cast<Value*>(getArgOperand(1)))->getZExtValue(); 128 } 129 130 DILocalVariable *getVariable() const { 131 return cast<DILocalVariable>(getRawVariable()); 132 } 133 134 DIExpression *getExpression() const { 135 return cast<DIExpression>(getRawExpression()); 136 } 137 138 Metadata *getRawVariable() const { 139 return cast<MetadataAsValue>(getArgOperand(2))->getMetadata(); 140 } 141 142 Metadata *getRawExpression() const { 143 return cast<MetadataAsValue>(getArgOperand(3))->getMetadata(); 144 } 145 146 // Methods for support type inquiry through isa, cast, and dyn_cast: 147 static inline bool classof(const IntrinsicInst *I) { 148 return I->getIntrinsicID() == Intrinsic::dbg_value; 149 } 150 static inline bool classof(const Value *V) { 151 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 152 } 153 }; 154 155 /// This is the common base class for constrained floating point intrinsics. 156 class ConstrainedFPIntrinsic : public IntrinsicInst { 157 public: 158 enum RoundingMode { 159 rmInvalid, 160 rmDynamic, 161 rmToNearest, 162 rmDownward, 163 rmUpward, 164 rmTowardZero 165 }; 166 167 enum ExceptionBehavior { 168 ebInvalid, 169 ebIgnore, 170 ebMayTrap, 171 ebStrict 172 }; 173 174 bool isUnaryOp() const; 175 RoundingMode getRoundingMode() const; 176 ExceptionBehavior getExceptionBehavior() const; 177 178 // Methods for support type inquiry through isa, cast, and dyn_cast: 179 static inline bool classof(const IntrinsicInst *I) { 180 switch (I->getIntrinsicID()) { 181 case Intrinsic::experimental_constrained_fadd: 182 case Intrinsic::experimental_constrained_fsub: 183 case Intrinsic::experimental_constrained_fmul: 184 case Intrinsic::experimental_constrained_fdiv: 185 case Intrinsic::experimental_constrained_frem: 186 case Intrinsic::experimental_constrained_sqrt: 187 case Intrinsic::experimental_constrained_pow: 188 case Intrinsic::experimental_constrained_powi: 189 case Intrinsic::experimental_constrained_sin: 190 case Intrinsic::experimental_constrained_cos: 191 case Intrinsic::experimental_constrained_exp: 192 case Intrinsic::experimental_constrained_exp2: 193 case Intrinsic::experimental_constrained_log: 194 case Intrinsic::experimental_constrained_log10: 195 case Intrinsic::experimental_constrained_log2: 196 case Intrinsic::experimental_constrained_rint: 197 case Intrinsic::experimental_constrained_nearbyint: 198 return true; 199 default: return false; 200 } 201 } 202 static inline bool classof(const Value *V) { 203 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 204 } 205 }; 206 207 /// This class represents atomic memcpy intrinsic 208 /// TODO: Integrate this class into MemIntrinsic hierarchy; for now this is 209 /// C&P of all methods from that hierarchy 210 class ElementUnorderedAtomicMemCpyInst : public IntrinsicInst { 211 private: 212 enum { ARG_DEST = 0, ARG_SOURCE = 1, ARG_LENGTH = 2, ARG_ELEMENTSIZE = 3 }; 213 214 public: 215 Value *getRawDest() const { 216 return const_cast<Value *>(getArgOperand(ARG_DEST)); 217 } 218 const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); } 219 Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); } 220 221 /// Return the arguments to the instruction. 222 Value *getRawSource() const { 223 return const_cast<Value *>(getArgOperand(ARG_SOURCE)); 224 } 225 const Use &getRawSourceUse() const { return getArgOperandUse(ARG_SOURCE); } 226 Use &getRawSourceUse() { return getArgOperandUse(ARG_SOURCE); } 227 228 Value *getLength() const { 229 return const_cast<Value *>(getArgOperand(ARG_LENGTH)); 230 } 231 const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); } 232 Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); } 233 234 bool isVolatile() const { return false; } 235 236 Value *getRawElementSizeInBytes() const { 237 return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE)); 238 } 239 240 ConstantInt *getElementSizeInBytesCst() const { 241 return cast<ConstantInt>(getRawElementSizeInBytes()); 242 } 243 244 uint32_t getElementSizeInBytes() const { 245 return getElementSizeInBytesCst()->getZExtValue(); 246 } 247 248 /// This is just like getRawDest, but it strips off any cast 249 /// instructions that feed it, giving the original input. The returned 250 /// value is guaranteed to be a pointer. 251 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 252 253 /// This is just like getRawSource, but it strips off any cast 254 /// instructions that feed it, giving the original input. The returned 255 /// value is guaranteed to be a pointer. 256 Value *getSource() const { return getRawSource()->stripPointerCasts(); } 257 258 unsigned getDestAddressSpace() const { 259 return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); 260 } 261 262 unsigned getSourceAddressSpace() const { 263 return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); 264 } 265 266 /// Set the specified arguments of the instruction. 267 void setDest(Value *Ptr) { 268 assert(getRawDest()->getType() == Ptr->getType() && 269 "setDest called with pointer of wrong type!"); 270 setArgOperand(ARG_DEST, Ptr); 271 } 272 273 void setSource(Value *Ptr) { 274 assert(getRawSource()->getType() == Ptr->getType() && 275 "setSource called with pointer of wrong type!"); 276 setArgOperand(ARG_SOURCE, Ptr); 277 } 278 279 void setLength(Value *L) { 280 assert(getLength()->getType() == L->getType() && 281 "setLength called with value of wrong type!"); 282 setArgOperand(ARG_LENGTH, L); 283 } 284 285 void setElementSizeInBytes(Constant *V) { 286 assert(V->getType() == Type::getInt8Ty(getContext()) && 287 "setElementSizeInBytes called with value of wrong type!"); 288 setArgOperand(ARG_ELEMENTSIZE, V); 289 } 290 291 static inline bool classof(const IntrinsicInst *I) { 292 return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic; 293 } 294 static inline bool classof(const Value *V) { 295 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 296 } 297 }; 298 299 /// This is the common base class for memset/memcpy/memmove. 300 class MemIntrinsic : public IntrinsicInst { 301 public: 302 Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); } 303 const Use &getRawDestUse() const { return getArgOperandUse(0); } 304 Use &getRawDestUse() { return getArgOperandUse(0); } 305 306 Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); } 307 const Use &getLengthUse() const { return getArgOperandUse(2); } 308 Use &getLengthUse() { return getArgOperandUse(2); } 309 310 ConstantInt *getAlignmentCst() const { 311 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3))); 312 } 313 314 unsigned getAlignment() const { 315 return getAlignmentCst()->getZExtValue(); 316 } 317 318 ConstantInt *getVolatileCst() const { 319 return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4))); 320 } 321 322 bool isVolatile() const { 323 return !getVolatileCst()->isZero(); 324 } 325 326 unsigned getDestAddressSpace() const { 327 return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); 328 } 329 330 /// This is just like getRawDest, but it strips off any cast 331 /// instructions that feed it, giving the original input. The returned 332 /// value is guaranteed to be a pointer. 333 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 334 335 /// Set the specified arguments of the instruction. 336 void setDest(Value *Ptr) { 337 assert(getRawDest()->getType() == Ptr->getType() && 338 "setDest called with pointer of wrong type!"); 339 setArgOperand(0, Ptr); 340 } 341 342 void setLength(Value *L) { 343 assert(getLength()->getType() == L->getType() && 344 "setLength called with value of wrong type!"); 345 setArgOperand(2, L); 346 } 347 348 void setAlignment(Constant* A) { 349 setArgOperand(3, A); 350 } 351 352 void setVolatile(Constant* V) { 353 setArgOperand(4, V); 354 } 355 356 Type *getAlignmentType() const { 357 return getArgOperand(3)->getType(); 358 } 359 360 // Methods for support type inquiry through isa, cast, and dyn_cast: 361 static inline bool classof(const IntrinsicInst *I) { 362 switch (I->getIntrinsicID()) { 363 case Intrinsic::memcpy: 364 case Intrinsic::memmove: 365 case Intrinsic::memset: 366 return true; 367 default: return false; 368 } 369 } 370 static inline bool classof(const Value *V) { 371 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 372 } 373 }; 374 375 /// This class wraps the llvm.memset intrinsic. 376 class MemSetInst : public MemIntrinsic { 377 public: 378 /// Return the arguments to the instruction. 379 Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); } 380 const Use &getValueUse() const { return getArgOperandUse(1); } 381 Use &getValueUse() { return getArgOperandUse(1); } 382 383 void setValue(Value *Val) { 384 assert(getValue()->getType() == Val->getType() && 385 "setValue called with value of wrong type!"); 386 setArgOperand(1, Val); 387 } 388 389 // Methods for support type inquiry through isa, cast, and dyn_cast: 390 static inline bool classof(const IntrinsicInst *I) { 391 return I->getIntrinsicID() == Intrinsic::memset; 392 } 393 static inline bool classof(const Value *V) { 394 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 395 } 396 }; 397 398 /// This class wraps the llvm.memcpy/memmove intrinsics. 399 class MemTransferInst : public MemIntrinsic { 400 public: 401 /// Return the arguments to the instruction. 402 Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); } 403 const Use &getRawSourceUse() const { return getArgOperandUse(1); } 404 Use &getRawSourceUse() { return getArgOperandUse(1); } 405 406 /// This is just like getRawSource, but it strips off any cast 407 /// instructions that feed it, giving the original input. The returned 408 /// value is guaranteed to be a pointer. 409 Value *getSource() const { return getRawSource()->stripPointerCasts(); } 410 411 unsigned getSourceAddressSpace() const { 412 return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); 413 } 414 415 void setSource(Value *Ptr) { 416 assert(getRawSource()->getType() == Ptr->getType() && 417 "setSource called with pointer of wrong type!"); 418 setArgOperand(1, Ptr); 419 } 420 421 // Methods for support type inquiry through isa, cast, and dyn_cast: 422 static inline bool classof(const IntrinsicInst *I) { 423 return I->getIntrinsicID() == Intrinsic::memcpy || 424 I->getIntrinsicID() == Intrinsic::memmove; 425 } 426 static inline bool classof(const Value *V) { 427 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 428 } 429 }; 430 431 /// This class wraps the llvm.memcpy intrinsic. 432 class MemCpyInst : public MemTransferInst { 433 public: 434 // Methods for support type inquiry through isa, cast, and dyn_cast: 435 static inline bool classof(const IntrinsicInst *I) { 436 return I->getIntrinsicID() == Intrinsic::memcpy; 437 } 438 static inline bool classof(const Value *V) { 439 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 440 } 441 }; 442 443 /// This class wraps the llvm.memmove intrinsic. 444 class MemMoveInst : public MemTransferInst { 445 public: 446 // Methods for support type inquiry through isa, cast, and dyn_cast: 447 static inline bool classof(const IntrinsicInst *I) { 448 return I->getIntrinsicID() == Intrinsic::memmove; 449 } 450 static inline bool classof(const Value *V) { 451 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 452 } 453 }; 454 455 /// This represents the llvm.va_start intrinsic. 456 class VAStartInst : public IntrinsicInst { 457 public: 458 static inline bool classof(const IntrinsicInst *I) { 459 return I->getIntrinsicID() == Intrinsic::vastart; 460 } 461 static inline bool classof(const Value *V) { 462 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 463 } 464 465 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } 466 }; 467 468 /// This represents the llvm.va_end intrinsic. 469 class VAEndInst : public IntrinsicInst { 470 public: 471 static inline bool classof(const IntrinsicInst *I) { 472 return I->getIntrinsicID() == Intrinsic::vaend; 473 } 474 static inline bool classof(const Value *V) { 475 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 476 } 477 478 Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); } 479 }; 480 481 /// This represents the llvm.va_copy intrinsic. 482 class VACopyInst : public IntrinsicInst { 483 public: 484 static inline bool classof(const IntrinsicInst *I) { 485 return I->getIntrinsicID() == Intrinsic::vacopy; 486 } 487 static inline bool classof(const Value *V) { 488 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 489 } 490 491 Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); } 492 Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); } 493 }; 494 495 /// This represents the llvm.instrprof_increment intrinsic. 496 class InstrProfIncrementInst : public IntrinsicInst { 497 public: 498 static inline bool classof(const IntrinsicInst *I) { 499 return I->getIntrinsicID() == Intrinsic::instrprof_increment; 500 } 501 static inline bool classof(const Value *V) { 502 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 503 } 504 505 GlobalVariable *getName() const { 506 return cast<GlobalVariable>( 507 const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); 508 } 509 510 ConstantInt *getHash() const { 511 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 512 } 513 514 ConstantInt *getNumCounters() const { 515 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2))); 516 } 517 518 ConstantInt *getIndex() const { 519 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 520 } 521 522 Value *getStep() const; 523 }; 524 525 class InstrProfIncrementInstStep : public InstrProfIncrementInst { 526 public: 527 static inline bool classof(const IntrinsicInst *I) { 528 return I->getIntrinsicID() == Intrinsic::instrprof_increment_step; 529 } 530 static inline bool classof(const Value *V) { 531 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 532 } 533 }; 534 535 /// This represents the llvm.instrprof_value_profile intrinsic. 536 class InstrProfValueProfileInst : public IntrinsicInst { 537 public: 538 static inline bool classof(const IntrinsicInst *I) { 539 return I->getIntrinsicID() == Intrinsic::instrprof_value_profile; 540 } 541 static inline bool classof(const Value *V) { 542 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 543 } 544 545 GlobalVariable *getName() const { 546 return cast<GlobalVariable>( 547 const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); 548 } 549 550 ConstantInt *getHash() const { 551 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 552 } 553 554 Value *getTargetValue() const { 555 return cast<Value>(const_cast<Value *>(getArgOperand(2))); 556 } 557 558 ConstantInt *getValueKind() const { 559 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 560 } 561 562 // Returns the value site index. 563 ConstantInt *getIndex() const { 564 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4))); 565 } 566 }; 567 568 } // end namespace llvm 569 570 #endif // LLVM_IR_INTRINSICINST_H 571