1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines classes that make it really easy to deal with intrinsic 10 // functions with the isa/dyncast family of functions. In particular, this 11 // allows you to do things like: 12 // 13 // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst)) 14 // ... MCI->getDest() ... MCI->getSource() ... 15 // 16 // All intrinsic function calls are instances of the call instruction, so these 17 // are all subclasses of the CallInst class. Note that none of these classes 18 // has state or virtual methods, which is an important part of this gross/neat 19 // hack working. 20 // 21 //===----------------------------------------------------------------------===// 22 23 #ifndef LLVM_IR_INTRINSICINST_H 24 #define LLVM_IR_INTRINSICINST_H 25 26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/DebugInfoMetadata.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/FPEnv.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/IR/GlobalVariable.h" 32 #include "llvm/IR/Instructions.h" 33 #include "llvm/IR/Intrinsics.h" 34 #include "llvm/IR/Value.h" 35 #include "llvm/Support/Casting.h" 36 #include <cassert> 37 #include <cstdint> 38 #include <optional> 39 40 namespace llvm { 41 42 class Metadata; 43 44 /// A wrapper class for inspecting calls to intrinsic functions. 45 /// This allows the standard isa/dyncast/cast functionality to work with calls 46 /// to intrinsic functions. 47 class IntrinsicInst : public CallInst { 48 public: 49 IntrinsicInst() = delete; 50 IntrinsicInst(const IntrinsicInst &) = delete; 51 IntrinsicInst &operator=(const IntrinsicInst &) = delete; 52 53 /// Return the intrinsic ID of this intrinsic. 54 Intrinsic::ID getIntrinsicID() const { 55 return getCalledFunction()->getIntrinsicID(); 56 } 57 58 /// Return true if swapping the first two arguments to the intrinsic produces 59 /// the same result. 60 bool isCommutative() const { 61 switch (getIntrinsicID()) { 62 case Intrinsic::maxnum: 63 case Intrinsic::minnum: 64 case Intrinsic::maximum: 65 case Intrinsic::minimum: 66 case Intrinsic::smax: 67 case Intrinsic::smin: 68 case Intrinsic::umax: 69 case Intrinsic::umin: 70 case Intrinsic::sadd_sat: 71 case Intrinsic::uadd_sat: 72 case Intrinsic::sadd_with_overflow: 73 case Intrinsic::uadd_with_overflow: 74 case Intrinsic::smul_with_overflow: 75 case Intrinsic::umul_with_overflow: 76 case Intrinsic::smul_fix: 77 case Intrinsic::umul_fix: 78 case Intrinsic::smul_fix_sat: 79 case Intrinsic::umul_fix_sat: 80 case Intrinsic::fma: 81 case Intrinsic::fmuladd: 82 return true; 83 default: 84 return false; 85 } 86 } 87 88 /// Checks if the intrinsic is an annotation. 89 bool isAssumeLikeIntrinsic() const { 90 switch (getIntrinsicID()) { 91 default: break; 92 case Intrinsic::assume: 93 case Intrinsic::sideeffect: 94 case Intrinsic::pseudoprobe: 95 case Intrinsic::dbg_assign: 96 case Intrinsic::dbg_declare: 97 case Intrinsic::dbg_value: 98 case Intrinsic::dbg_label: 99 case Intrinsic::invariant_start: 100 case Intrinsic::invariant_end: 101 case Intrinsic::lifetime_start: 102 case Intrinsic::lifetime_end: 103 case Intrinsic::experimental_noalias_scope_decl: 104 case Intrinsic::objectsize: 105 case Intrinsic::ptr_annotation: 106 case Intrinsic::var_annotation: 107 return true; 108 } 109 return false; 110 } 111 112 /// Check if the intrinsic might lower into a regular function call in the 113 /// course of IR transformations 114 static bool mayLowerToFunctionCall(Intrinsic::ID IID); 115 116 /// Methods for support type inquiry through isa, cast, and dyn_cast: 117 static bool classof(const CallInst *I) { 118 if (const Function *CF = I->getCalledFunction()) 119 return CF->isIntrinsic(); 120 return false; 121 } 122 static bool classof(const Value *V) { 123 return isa<CallInst>(V) && classof(cast<CallInst>(V)); 124 } 125 }; 126 127 /// Check if \p ID corresponds to a lifetime intrinsic. 128 static inline bool isLifetimeIntrinsic(Intrinsic::ID ID) { 129 switch (ID) { 130 case Intrinsic::lifetime_start: 131 case Intrinsic::lifetime_end: 132 return true; 133 default: 134 return false; 135 } 136 } 137 138 /// This is the common base class for lifetime intrinsics. 139 class LifetimeIntrinsic : public IntrinsicInst { 140 public: 141 /// \name Casting methods 142 /// @{ 143 static bool classof(const IntrinsicInst *I) { 144 return isLifetimeIntrinsic(I->getIntrinsicID()); 145 } 146 static bool classof(const Value *V) { 147 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 148 } 149 /// @} 150 }; 151 152 /// Check if \p ID corresponds to a debug info intrinsic. 153 static inline bool isDbgInfoIntrinsic(Intrinsic::ID ID) { 154 switch (ID) { 155 case Intrinsic::dbg_declare: 156 case Intrinsic::dbg_value: 157 case Intrinsic::dbg_label: 158 case Intrinsic::dbg_assign: 159 return true; 160 default: 161 return false; 162 } 163 } 164 165 /// This is the common base class for debug info intrinsics. 166 class DbgInfoIntrinsic : public IntrinsicInst { 167 public: 168 /// \name Casting methods 169 /// @{ 170 static bool classof(const IntrinsicInst *I) { 171 return isDbgInfoIntrinsic(I->getIntrinsicID()); 172 } 173 static bool classof(const Value *V) { 174 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 175 } 176 /// @} 177 }; 178 179 // Iterator for ValueAsMetadata that internally uses direct pointer iteration 180 // over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the 181 // ValueAsMetadata . 182 class location_op_iterator 183 : public iterator_facade_base<location_op_iterator, 184 std::bidirectional_iterator_tag, Value *> { 185 PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I; 186 187 public: 188 location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {} 189 location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {} 190 191 location_op_iterator(const location_op_iterator &R) : I(R.I) {} 192 location_op_iterator &operator=(const location_op_iterator &R) { 193 I = R.I; 194 return *this; 195 } 196 bool operator==(const location_op_iterator &RHS) const { return I == RHS.I; } 197 const Value *operator*() const { 198 ValueAsMetadata *VAM = isa<ValueAsMetadata *>(I) 199 ? cast<ValueAsMetadata *>(I) 200 : *cast<ValueAsMetadata **>(I); 201 return VAM->getValue(); 202 }; 203 Value *operator*() { 204 ValueAsMetadata *VAM = isa<ValueAsMetadata *>(I) 205 ? cast<ValueAsMetadata *>(I) 206 : *cast<ValueAsMetadata **>(I); 207 return VAM->getValue(); 208 } 209 location_op_iterator &operator++() { 210 if (isa<ValueAsMetadata *>(I)) 211 I = cast<ValueAsMetadata *>(I) + 1; 212 else 213 I = cast<ValueAsMetadata **>(I) + 1; 214 return *this; 215 } 216 location_op_iterator &operator--() { 217 if (isa<ValueAsMetadata *>(I)) 218 I = cast<ValueAsMetadata *>(I) - 1; 219 else 220 I = cast<ValueAsMetadata **>(I) - 1; 221 return *this; 222 } 223 }; 224 225 /// Lightweight class that wraps the location operand metadata of a debug 226 /// intrinsic. The raw location may be a ValueAsMetadata, an empty MDTuple, 227 /// or a DIArgList. 228 class RawLocationWrapper { 229 Metadata *RawLocation = nullptr; 230 231 public: 232 RawLocationWrapper() = default; 233 explicit RawLocationWrapper(Metadata *RawLocation) 234 : RawLocation(RawLocation) { 235 // Allow ValueAsMetadata, empty MDTuple, DIArgList. 236 assert(RawLocation && "unexpected null RawLocation"); 237 assert(isa<ValueAsMetadata>(RawLocation) || isa<DIArgList>(RawLocation) || 238 (isa<MDNode>(RawLocation) && 239 !cast<MDNode>(RawLocation)->getNumOperands())); 240 } 241 Metadata *getRawLocation() const { return RawLocation; } 242 /// Get the locations corresponding to the variable referenced by the debug 243 /// info intrinsic. Depending on the intrinsic, this could be the 244 /// variable's value or its address. 245 iterator_range<location_op_iterator> location_ops() const; 246 Value *getVariableLocationOp(unsigned OpIdx) const; 247 unsigned getNumVariableLocationOps() const { 248 if (hasArgList()) 249 return cast<DIArgList>(getRawLocation())->getArgs().size(); 250 return 1; 251 } 252 bool hasArgList() const { return isa<DIArgList>(getRawLocation()); } 253 bool isKillLocation(const DIExpression *Expression) const { 254 // Check for "kill" sentinel values. 255 // Non-variadic: empty metadata. 256 if (!hasArgList() && isa<MDNode>(getRawLocation())) 257 return true; 258 // Variadic: empty DIArgList with empty expression. 259 if (getNumVariableLocationOps() == 0 && !Expression->isComplex()) 260 return true; 261 // Variadic and non-variadic: Interpret expressions using undef or poison 262 // values as kills. 263 return any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); }); 264 } 265 266 friend bool operator==(const RawLocationWrapper &A, 267 const RawLocationWrapper &B) { 268 return A.RawLocation == B.RawLocation; 269 } 270 friend bool operator!=(const RawLocationWrapper &A, 271 const RawLocationWrapper &B) { 272 return !(A == B); 273 } 274 friend bool operator>(const RawLocationWrapper &A, 275 const RawLocationWrapper &B) { 276 return A.RawLocation > B.RawLocation; 277 } 278 friend bool operator>=(const RawLocationWrapper &A, 279 const RawLocationWrapper &B) { 280 return A.RawLocation >= B.RawLocation; 281 } 282 friend bool operator<(const RawLocationWrapper &A, 283 const RawLocationWrapper &B) { 284 return A.RawLocation < B.RawLocation; 285 } 286 friend bool operator<=(const RawLocationWrapper &A, 287 const RawLocationWrapper &B) { 288 return A.RawLocation <= B.RawLocation; 289 } 290 }; 291 292 /// This is the common base class for debug info intrinsics for variables. 293 class DbgVariableIntrinsic : public DbgInfoIntrinsic { 294 public: 295 /// Get the locations corresponding to the variable referenced by the debug 296 /// info intrinsic. Depending on the intrinsic, this could be the 297 /// variable's value or its address. 298 iterator_range<location_op_iterator> location_ops() const; 299 300 Value *getVariableLocationOp(unsigned OpIdx) const; 301 302 void replaceVariableLocationOp(Value *OldValue, Value *NewValue); 303 void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue); 304 /// Adding a new location operand will always result in this intrinsic using 305 /// an ArgList, and must always be accompanied by a new expression that uses 306 /// the new operand. 307 void addVariableLocationOps(ArrayRef<Value *> NewValues, 308 DIExpression *NewExpr); 309 310 void setVariable(DILocalVariable *NewVar) { 311 setArgOperand(1, MetadataAsValue::get(NewVar->getContext(), NewVar)); 312 } 313 314 void setExpression(DIExpression *NewExpr) { 315 setArgOperand(2, MetadataAsValue::get(NewExpr->getContext(), NewExpr)); 316 } 317 318 unsigned getNumVariableLocationOps() const { 319 return getWrappedLocation().getNumVariableLocationOps(); 320 } 321 322 bool hasArgList() const { return getWrappedLocation().hasArgList(); } 323 324 /// Does this describe the address of a local variable. True for dbg.declare, 325 /// but not dbg.value, which describes its value, or dbg.assign, which 326 /// describes a combination of the variable's value and address. 327 bool isAddressOfVariable() const { 328 return getIntrinsicID() == Intrinsic::dbg_declare; 329 } 330 331 void setKillLocation() { 332 // TODO: When/if we remove duplicate values from DIArgLists, we don't need 333 // this set anymore. 334 SmallPtrSet<Value *, 4> RemovedValues; 335 for (Value *OldValue : location_ops()) { 336 if (!RemovedValues.insert(OldValue).second) 337 continue; 338 Value *Poison = PoisonValue::get(OldValue->getType()); 339 replaceVariableLocationOp(OldValue, Poison); 340 } 341 } 342 343 bool isKillLocation() const { 344 return getWrappedLocation().isKillLocation(getExpression()); 345 } 346 347 DILocalVariable *getVariable() const { 348 return cast<DILocalVariable>(getRawVariable()); 349 } 350 351 DIExpression *getExpression() const { 352 return cast<DIExpression>(getRawExpression()); 353 } 354 355 Metadata *getRawLocation() const { 356 return cast<MetadataAsValue>(getArgOperand(0))->getMetadata(); 357 } 358 359 RawLocationWrapper getWrappedLocation() const { 360 return RawLocationWrapper(getRawLocation()); 361 } 362 363 Metadata *getRawVariable() const { 364 return cast<MetadataAsValue>(getArgOperand(1))->getMetadata(); 365 } 366 367 Metadata *getRawExpression() const { 368 return cast<MetadataAsValue>(getArgOperand(2))->getMetadata(); 369 } 370 371 /// Use of this should generally be avoided; instead, 372 /// replaceVariableLocationOp and addVariableLocationOps should be used where 373 /// possible to avoid creating invalid state. 374 void setRawLocation(Metadata *Location) { 375 return setArgOperand(0, MetadataAsValue::get(getContext(), Location)); 376 } 377 378 /// Get the size (in bits) of the variable, or fragment of the variable that 379 /// is described. 380 std::optional<uint64_t> getFragmentSizeInBits() const; 381 382 /// Get the FragmentInfo for the variable. 383 std::optional<DIExpression::FragmentInfo> getFragment() const { 384 return getExpression()->getFragmentInfo(); 385 } 386 387 /// Get the FragmentInfo for the variable if it exists, otherwise return a 388 /// FragmentInfo that covers the entire variable if the variable size is 389 /// known, otherwise return a zero-sized fragment. 390 DIExpression::FragmentInfo getFragmentOrEntireVariable() const { 391 DIExpression::FragmentInfo VariableSlice(0, 0); 392 // Get the fragment or variable size, or zero. 393 if (auto Sz = getFragmentSizeInBits()) 394 VariableSlice.SizeInBits = *Sz; 395 if (auto Frag = getExpression()->getFragmentInfo()) 396 VariableSlice.OffsetInBits = Frag->OffsetInBits; 397 return VariableSlice; 398 } 399 400 /// \name Casting methods 401 /// @{ 402 static bool classof(const IntrinsicInst *I) { 403 switch (I->getIntrinsicID()) { 404 case Intrinsic::dbg_declare: 405 case Intrinsic::dbg_value: 406 case Intrinsic::dbg_assign: 407 return true; 408 default: 409 return false; 410 } 411 } 412 static bool classof(const Value *V) { 413 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 414 } 415 /// @} 416 protected: 417 void setArgOperand(unsigned i, Value *v) { 418 DbgInfoIntrinsic::setArgOperand(i, v); 419 } 420 void setOperand(unsigned i, Value *v) { DbgInfoIntrinsic::setOperand(i, v); } 421 }; 422 423 /// This represents the llvm.dbg.declare instruction. 424 class DbgDeclareInst : public DbgVariableIntrinsic { 425 public: 426 Value *getAddress() const { 427 assert(getNumVariableLocationOps() == 1 && 428 "dbg.declare must have exactly 1 location operand."); 429 return getVariableLocationOp(0); 430 } 431 432 /// \name Casting methods 433 /// @{ 434 static bool classof(const IntrinsicInst *I) { 435 return I->getIntrinsicID() == Intrinsic::dbg_declare; 436 } 437 static bool classof(const Value *V) { 438 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 439 } 440 /// @} 441 }; 442 443 /// This represents the llvm.dbg.value instruction. 444 class DbgValueInst : public DbgVariableIntrinsic { 445 public: 446 // The default argument should only be used in ISel, and the default option 447 // should be removed once ISel support for multiple location ops is complete. 448 Value *getValue(unsigned OpIdx = 0) const { 449 return getVariableLocationOp(OpIdx); 450 } 451 iterator_range<location_op_iterator> getValues() const { 452 return location_ops(); 453 } 454 455 /// \name Casting methods 456 /// @{ 457 static bool classof(const IntrinsicInst *I) { 458 return I->getIntrinsicID() == Intrinsic::dbg_value || 459 I->getIntrinsicID() == Intrinsic::dbg_assign; 460 } 461 static bool classof(const Value *V) { 462 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 463 } 464 /// @} 465 }; 466 467 /// This represents the llvm.dbg.assign instruction. 468 class DbgAssignIntrinsic : public DbgValueInst { 469 enum Operands { 470 OpValue, 471 OpVar, 472 OpExpr, 473 OpAssignID, 474 OpAddress, 475 OpAddressExpr, 476 }; 477 478 public: 479 Value *getAddress() const; 480 Metadata *getRawAddress() const { 481 return cast<MetadataAsValue>(getArgOperand(OpAddress))->getMetadata(); 482 } 483 Metadata *getRawAssignID() const { 484 return cast<MetadataAsValue>(getArgOperand(OpAssignID))->getMetadata(); 485 } 486 DIAssignID *getAssignID() const { return cast<DIAssignID>(getRawAssignID()); } 487 Metadata *getRawAddressExpression() const { 488 return cast<MetadataAsValue>(getArgOperand(OpAddressExpr))->getMetadata(); 489 } 490 DIExpression *getAddressExpression() const { 491 return cast<DIExpression>(getRawAddressExpression()); 492 } 493 void setAddressExpression(DIExpression *NewExpr) { 494 setArgOperand(OpAddressExpr, 495 MetadataAsValue::get(NewExpr->getContext(), NewExpr)); 496 } 497 void setAssignId(DIAssignID *New); 498 void setAddress(Value *V); 499 /// Kill the address component. 500 void setKillAddress(); 501 /// Check whether this kills the address component. This doesn't take into 502 /// account the position of the intrinsic, therefore a returned value of false 503 /// does not guarentee the address is a valid location for the variable at the 504 /// intrinsic's position in IR. 505 bool isKillAddress() const; 506 void setValue(Value *V); 507 /// \name Casting methods 508 /// @{ 509 static bool classof(const IntrinsicInst *I) { 510 return I->getIntrinsicID() == Intrinsic::dbg_assign; 511 } 512 static bool classof(const Value *V) { 513 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 514 } 515 /// @} 516 }; 517 518 /// This represents the llvm.dbg.label instruction. 519 class DbgLabelInst : public DbgInfoIntrinsic { 520 public: 521 DILabel *getLabel() const { return cast<DILabel>(getRawLabel()); } 522 523 Metadata *getRawLabel() const { 524 return cast<MetadataAsValue>(getArgOperand(0))->getMetadata(); 525 } 526 527 /// Methods for support type inquiry through isa, cast, and dyn_cast: 528 /// @{ 529 static bool classof(const IntrinsicInst *I) { 530 return I->getIntrinsicID() == Intrinsic::dbg_label; 531 } 532 static bool classof(const Value *V) { 533 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 534 } 535 /// @} 536 }; 537 538 /// This is the common base class for vector predication intrinsics. 539 class VPIntrinsic : public IntrinsicInst { 540 public: 541 /// \brief Declares a llvm.vp.* intrinsic in \p M that matches the parameters 542 /// \p Params. Additionally, the load and gather intrinsics require 543 /// \p ReturnType to be specified. 544 static Function *getDeclarationForParams(Module *M, Intrinsic::ID, 545 Type *ReturnType, 546 ArrayRef<Value *> Params); 547 548 static std::optional<unsigned> getMaskParamPos(Intrinsic::ID IntrinsicID); 549 static std::optional<unsigned> getVectorLengthParamPos( 550 Intrinsic::ID IntrinsicID); 551 552 /// The llvm.vp.* intrinsics for this instruction Opcode 553 static Intrinsic::ID getForOpcode(unsigned OC); 554 555 // Whether \p ID is a VP intrinsic ID. 556 static bool isVPIntrinsic(Intrinsic::ID); 557 558 /// \return The mask parameter or nullptr. 559 Value *getMaskParam() const; 560 void setMaskParam(Value *); 561 562 /// \return The vector length parameter or nullptr. 563 Value *getVectorLengthParam() const; 564 void setVectorLengthParam(Value *); 565 566 /// \return Whether the vector length param can be ignored. 567 bool canIgnoreVectorLengthParam() const; 568 569 /// \return The static element count (vector number of elements) the vector 570 /// length parameter applies to. 571 ElementCount getStaticVectorLength() const; 572 573 /// \return The alignment of the pointer used by this load/store/gather or 574 /// scatter. 575 MaybeAlign getPointerAlignment() const; 576 // MaybeAlign setPointerAlignment(Align NewAlign); // TODO 577 578 /// \return The pointer operand of this load,store, gather or scatter. 579 Value *getMemoryPointerParam() const; 580 static std::optional<unsigned> getMemoryPointerParamPos(Intrinsic::ID); 581 582 /// \return The data (payload) operand of this store or scatter. 583 Value *getMemoryDataParam() const; 584 static std::optional<unsigned> getMemoryDataParamPos(Intrinsic::ID); 585 586 // Methods for support type inquiry through isa, cast, and dyn_cast: 587 static bool classof(const IntrinsicInst *I) { 588 return isVPIntrinsic(I->getIntrinsicID()); 589 } 590 static bool classof(const Value *V) { 591 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 592 } 593 594 // Equivalent non-predicated opcode 595 std::optional<unsigned> getFunctionalOpcode() const { 596 return getFunctionalOpcodeForVP(getIntrinsicID()); 597 } 598 599 // Equivalent non-predicated constrained ID 600 std::optional<unsigned> getConstrainedIntrinsicID() const { 601 return getConstrainedIntrinsicIDForVP(getIntrinsicID()); 602 } 603 604 // Equivalent non-predicated opcode 605 static std::optional<unsigned> getFunctionalOpcodeForVP(Intrinsic::ID ID); 606 607 // Equivalent non-predicated constrained ID 608 static std::optional<unsigned> 609 getConstrainedIntrinsicIDForVP(Intrinsic::ID ID); 610 }; 611 612 /// This represents vector predication reduction intrinsics. 613 class VPReductionIntrinsic : public VPIntrinsic { 614 public: 615 static bool isVPReduction(Intrinsic::ID ID); 616 617 unsigned getStartParamPos() const; 618 unsigned getVectorParamPos() const; 619 620 static std::optional<unsigned> getStartParamPos(Intrinsic::ID ID); 621 static std::optional<unsigned> getVectorParamPos(Intrinsic::ID ID); 622 623 /// Methods for support type inquiry through isa, cast, and dyn_cast: 624 /// @{ 625 static bool classof(const IntrinsicInst *I) { 626 return VPReductionIntrinsic::isVPReduction(I->getIntrinsicID()); 627 } 628 static bool classof(const Value *V) { 629 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 630 } 631 /// @} 632 }; 633 634 class VPCastIntrinsic : public VPIntrinsic { 635 public: 636 static bool isVPCast(Intrinsic::ID ID); 637 638 /// Methods for support type inquiry through isa, cast, and dyn_cast: 639 /// @{ 640 static bool classof(const IntrinsicInst *I) { 641 return VPCastIntrinsic::isVPCast(I->getIntrinsicID()); 642 } 643 static bool classof(const Value *V) { 644 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 645 } 646 /// @} 647 }; 648 649 class VPCmpIntrinsic : public VPIntrinsic { 650 public: 651 static bool isVPCmp(Intrinsic::ID ID); 652 653 CmpInst::Predicate getPredicate() const; 654 655 /// Methods for support type inquiry through isa, cast, and dyn_cast: 656 /// @{ 657 static bool classof(const IntrinsicInst *I) { 658 return VPCmpIntrinsic::isVPCmp(I->getIntrinsicID()); 659 } 660 static bool classof(const Value *V) { 661 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 662 } 663 /// @} 664 }; 665 666 /// This is the common base class for constrained floating point intrinsics. 667 class ConstrainedFPIntrinsic : public IntrinsicInst { 668 public: 669 bool isUnaryOp() const; 670 bool isTernaryOp() const; 671 std::optional<RoundingMode> getRoundingMode() const; 672 std::optional<fp::ExceptionBehavior> getExceptionBehavior() const; 673 bool isDefaultFPEnvironment() const; 674 675 // Methods for support type inquiry through isa, cast, and dyn_cast: 676 static bool classof(const IntrinsicInst *I); 677 static bool classof(const Value *V) { 678 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 679 } 680 }; 681 682 /// Constrained floating point compare intrinsics. 683 class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic { 684 public: 685 FCmpInst::Predicate getPredicate() const; 686 bool isSignaling() const { 687 return getIntrinsicID() == Intrinsic::experimental_constrained_fcmps; 688 } 689 690 // Methods for support type inquiry through isa, cast, and dyn_cast: 691 static bool classof(const IntrinsicInst *I) { 692 switch (I->getIntrinsicID()) { 693 case Intrinsic::experimental_constrained_fcmp: 694 case Intrinsic::experimental_constrained_fcmps: 695 return true; 696 default: 697 return false; 698 } 699 } 700 static bool classof(const Value *V) { 701 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 702 } 703 }; 704 705 /// This class represents min/max intrinsics. 706 class MinMaxIntrinsic : public IntrinsicInst { 707 public: 708 static bool classof(const IntrinsicInst *I) { 709 switch (I->getIntrinsicID()) { 710 case Intrinsic::umin: 711 case Intrinsic::umax: 712 case Intrinsic::smin: 713 case Intrinsic::smax: 714 return true; 715 default: 716 return false; 717 } 718 } 719 static bool classof(const Value *V) { 720 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 721 } 722 723 Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); } 724 Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); } 725 726 /// Returns the comparison predicate underlying the intrinsic. 727 static ICmpInst::Predicate getPredicate(Intrinsic::ID ID) { 728 switch (ID) { 729 case Intrinsic::umin: 730 return ICmpInst::Predicate::ICMP_ULT; 731 case Intrinsic::umax: 732 return ICmpInst::Predicate::ICMP_UGT; 733 case Intrinsic::smin: 734 return ICmpInst::Predicate::ICMP_SLT; 735 case Intrinsic::smax: 736 return ICmpInst::Predicate::ICMP_SGT; 737 default: 738 llvm_unreachable("Invalid intrinsic"); 739 } 740 } 741 742 /// Returns the comparison predicate underlying the intrinsic. 743 ICmpInst::Predicate getPredicate() const { 744 return getPredicate(getIntrinsicID()); 745 } 746 747 /// Whether the intrinsic is signed or unsigned. 748 static bool isSigned(Intrinsic::ID ID) { 749 return ICmpInst::isSigned(getPredicate(ID)); 750 }; 751 752 /// Whether the intrinsic is signed or unsigned. 753 bool isSigned() const { return isSigned(getIntrinsicID()); }; 754 755 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, 756 /// so there is a certain threshold value, upon reaching which, 757 /// their value can no longer change. Return said threshold. 758 static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits) { 759 switch (ID) { 760 case Intrinsic::umin: 761 return APInt::getMinValue(numBits); 762 case Intrinsic::umax: 763 return APInt::getMaxValue(numBits); 764 case Intrinsic::smin: 765 return APInt::getSignedMinValue(numBits); 766 case Intrinsic::smax: 767 return APInt::getSignedMaxValue(numBits); 768 default: 769 llvm_unreachable("Invalid intrinsic"); 770 } 771 } 772 773 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, 774 /// so there is a certain threshold value, upon reaching which, 775 /// their value can no longer change. Return said threshold. 776 APInt getSaturationPoint(unsigned numBits) const { 777 return getSaturationPoint(getIntrinsicID(), numBits); 778 } 779 780 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, 781 /// so there is a certain threshold value, upon reaching which, 782 /// their value can no longer change. Return said threshold. 783 static Constant *getSaturationPoint(Intrinsic::ID ID, Type *Ty) { 784 return Constant::getIntegerValue( 785 Ty, getSaturationPoint(ID, Ty->getScalarSizeInBits())); 786 } 787 788 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, 789 /// so there is a certain threshold value, upon reaching which, 790 /// their value can no longer change. Return said threshold. 791 Constant *getSaturationPoint(Type *Ty) const { 792 return getSaturationPoint(getIntrinsicID(), Ty); 793 } 794 }; 795 796 /// This class represents an intrinsic that is based on a binary operation. 797 /// This includes op.with.overflow and saturating add/sub intrinsics. 798 class BinaryOpIntrinsic : public IntrinsicInst { 799 public: 800 static bool classof(const IntrinsicInst *I) { 801 switch (I->getIntrinsicID()) { 802 case Intrinsic::uadd_with_overflow: 803 case Intrinsic::sadd_with_overflow: 804 case Intrinsic::usub_with_overflow: 805 case Intrinsic::ssub_with_overflow: 806 case Intrinsic::umul_with_overflow: 807 case Intrinsic::smul_with_overflow: 808 case Intrinsic::uadd_sat: 809 case Intrinsic::sadd_sat: 810 case Intrinsic::usub_sat: 811 case Intrinsic::ssub_sat: 812 return true; 813 default: 814 return false; 815 } 816 } 817 static bool classof(const Value *V) { 818 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 819 } 820 821 Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); } 822 Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); } 823 824 /// Returns the binary operation underlying the intrinsic. 825 Instruction::BinaryOps getBinaryOp() const; 826 827 /// Whether the intrinsic is signed or unsigned. 828 bool isSigned() const; 829 830 /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap. 831 unsigned getNoWrapKind() const; 832 }; 833 834 /// Represents an op.with.overflow intrinsic. 835 class WithOverflowInst : public BinaryOpIntrinsic { 836 public: 837 static bool classof(const IntrinsicInst *I) { 838 switch (I->getIntrinsicID()) { 839 case Intrinsic::uadd_with_overflow: 840 case Intrinsic::sadd_with_overflow: 841 case Intrinsic::usub_with_overflow: 842 case Intrinsic::ssub_with_overflow: 843 case Intrinsic::umul_with_overflow: 844 case Intrinsic::smul_with_overflow: 845 return true; 846 default: 847 return false; 848 } 849 } 850 static bool classof(const Value *V) { 851 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 852 } 853 }; 854 855 /// Represents a saturating add/sub intrinsic. 856 class SaturatingInst : public BinaryOpIntrinsic { 857 public: 858 static bool classof(const IntrinsicInst *I) { 859 switch (I->getIntrinsicID()) { 860 case Intrinsic::uadd_sat: 861 case Intrinsic::sadd_sat: 862 case Intrinsic::usub_sat: 863 case Intrinsic::ssub_sat: 864 return true; 865 default: 866 return false; 867 } 868 } 869 static bool classof(const Value *V) { 870 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 871 } 872 }; 873 874 /// Common base class for all memory intrinsics. Simply provides 875 /// common methods. 876 /// Written as CRTP to avoid a common base class amongst the 877 /// three atomicity hierarchies. 878 template <typename Derived> class MemIntrinsicBase : public IntrinsicInst { 879 private: 880 enum { ARG_DEST = 0, ARG_LENGTH = 2 }; 881 882 public: 883 Value *getRawDest() const { 884 return const_cast<Value *>(getArgOperand(ARG_DEST)); 885 } 886 const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); } 887 Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); } 888 889 Value *getLength() const { 890 return const_cast<Value *>(getArgOperand(ARG_LENGTH)); 891 } 892 const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); } 893 Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); } 894 895 /// This is just like getRawDest, but it strips off any cast 896 /// instructions (including addrspacecast) that feed it, giving the 897 /// original input. The returned value is guaranteed to be a pointer. 898 Value *getDest() const { return getRawDest()->stripPointerCasts(); } 899 900 unsigned getDestAddressSpace() const { 901 return cast<PointerType>(getRawDest()->getType())->getAddressSpace(); 902 } 903 904 /// FIXME: Remove this function once transition to Align is over. 905 /// Use getDestAlign() instead. 906 LLVM_DEPRECATED("Use getDestAlign() instead", "getDestAlign") 907 unsigned getDestAlignment() const { 908 if (auto MA = getParamAlign(ARG_DEST)) 909 return MA->value(); 910 return 0; 911 } 912 MaybeAlign getDestAlign() const { return getParamAlign(ARG_DEST); } 913 914 /// Set the specified arguments of the instruction. 915 void setDest(Value *Ptr) { 916 assert(getRawDest()->getType() == Ptr->getType() && 917 "setDest called with pointer of wrong type!"); 918 setArgOperand(ARG_DEST, Ptr); 919 } 920 921 void setDestAlignment(MaybeAlign Alignment) { 922 removeParamAttr(ARG_DEST, Attribute::Alignment); 923 if (Alignment) 924 addParamAttr(ARG_DEST, 925 Attribute::getWithAlignment(getContext(), *Alignment)); 926 } 927 void setDestAlignment(Align Alignment) { 928 removeParamAttr(ARG_DEST, Attribute::Alignment); 929 addParamAttr(ARG_DEST, 930 Attribute::getWithAlignment(getContext(), Alignment)); 931 } 932 933 void setLength(Value *L) { 934 assert(getLength()->getType() == L->getType() && 935 "setLength called with value of wrong type!"); 936 setArgOperand(ARG_LENGTH, L); 937 } 938 }; 939 940 /// Common base class for all memory transfer intrinsics. Simply provides 941 /// common methods. 942 template <class BaseCL> class MemTransferBase : public BaseCL { 943 private: 944 enum { ARG_SOURCE = 1 }; 945 946 public: 947 /// Return the arguments to the instruction. 948 Value *getRawSource() const { 949 return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE)); 950 } 951 const Use &getRawSourceUse() const { 952 return BaseCL::getArgOperandUse(ARG_SOURCE); 953 } 954 Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); } 955 956 /// This is just like getRawSource, but it strips off any cast 957 /// instructions that feed it, giving the original input. The returned 958 /// value is guaranteed to be a pointer. 959 Value *getSource() const { return getRawSource()->stripPointerCasts(); } 960 961 unsigned getSourceAddressSpace() const { 962 return cast<PointerType>(getRawSource()->getType())->getAddressSpace(); 963 } 964 965 /// FIXME: Remove this function once transition to Align is over. 966 /// Use getSourceAlign() instead. 967 LLVM_DEPRECATED("Use getSourceAlign() instead", "getSourceAlign") 968 unsigned getSourceAlignment() const { 969 if (auto MA = BaseCL::getParamAlign(ARG_SOURCE)) 970 return MA->value(); 971 return 0; 972 } 973 974 MaybeAlign getSourceAlign() const { 975 return BaseCL::getParamAlign(ARG_SOURCE); 976 } 977 978 void setSource(Value *Ptr) { 979 assert(getRawSource()->getType() == Ptr->getType() && 980 "setSource called with pointer of wrong type!"); 981 BaseCL::setArgOperand(ARG_SOURCE, Ptr); 982 } 983 984 void setSourceAlignment(MaybeAlign Alignment) { 985 BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment); 986 if (Alignment) 987 BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment( 988 BaseCL::getContext(), *Alignment)); 989 } 990 991 void setSourceAlignment(Align Alignment) { 992 BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment); 993 BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment( 994 BaseCL::getContext(), Alignment)); 995 } 996 }; 997 998 /// Common base class for all memset intrinsics. Simply provides 999 /// common methods. 1000 template <class BaseCL> class MemSetBase : public BaseCL { 1001 private: 1002 enum { ARG_VALUE = 1 }; 1003 1004 public: 1005 Value *getValue() const { 1006 return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE)); 1007 } 1008 const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); } 1009 Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); } 1010 1011 void setValue(Value *Val) { 1012 assert(getValue()->getType() == Val->getType() && 1013 "setValue called with value of wrong type!"); 1014 BaseCL::setArgOperand(ARG_VALUE, Val); 1015 } 1016 }; 1017 1018 // The common base class for the atomic memset/memmove/memcpy intrinsics 1019 // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove 1020 class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> { 1021 private: 1022 enum { ARG_ELEMENTSIZE = 3 }; 1023 1024 public: 1025 Value *getRawElementSizeInBytes() const { 1026 return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE)); 1027 } 1028 1029 ConstantInt *getElementSizeInBytesCst() const { 1030 return cast<ConstantInt>(getRawElementSizeInBytes()); 1031 } 1032 1033 uint32_t getElementSizeInBytes() const { 1034 return getElementSizeInBytesCst()->getZExtValue(); 1035 } 1036 1037 void setElementSizeInBytes(Constant *V) { 1038 assert(V->getType() == Type::getInt8Ty(getContext()) && 1039 "setElementSizeInBytes called with value of wrong type!"); 1040 setArgOperand(ARG_ELEMENTSIZE, V); 1041 } 1042 1043 static bool classof(const IntrinsicInst *I) { 1044 switch (I->getIntrinsicID()) { 1045 case Intrinsic::memcpy_element_unordered_atomic: 1046 case Intrinsic::memmove_element_unordered_atomic: 1047 case Intrinsic::memset_element_unordered_atomic: 1048 return true; 1049 default: 1050 return false; 1051 } 1052 } 1053 static bool classof(const Value *V) { 1054 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1055 } 1056 }; 1057 1058 /// This class represents atomic memset intrinsic 1059 // i.e. llvm.element.unordered.atomic.memset 1060 class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> { 1061 public: 1062 static bool classof(const IntrinsicInst *I) { 1063 return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic; 1064 } 1065 static bool classof(const Value *V) { 1066 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1067 } 1068 }; 1069 1070 // This class wraps the atomic memcpy/memmove intrinsics 1071 // i.e. llvm.element.unordered.atomic.memcpy/memmove 1072 class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> { 1073 public: 1074 static bool classof(const IntrinsicInst *I) { 1075 switch (I->getIntrinsicID()) { 1076 case Intrinsic::memcpy_element_unordered_atomic: 1077 case Intrinsic::memmove_element_unordered_atomic: 1078 return true; 1079 default: 1080 return false; 1081 } 1082 } 1083 static bool classof(const Value *V) { 1084 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1085 } 1086 }; 1087 1088 /// This class represents the atomic memcpy intrinsic 1089 /// i.e. llvm.element.unordered.atomic.memcpy 1090 class AtomicMemCpyInst : public AtomicMemTransferInst { 1091 public: 1092 static bool classof(const IntrinsicInst *I) { 1093 return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic; 1094 } 1095 static bool classof(const Value *V) { 1096 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1097 } 1098 }; 1099 1100 /// This class represents the atomic memmove intrinsic 1101 /// i.e. llvm.element.unordered.atomic.memmove 1102 class AtomicMemMoveInst : public AtomicMemTransferInst { 1103 public: 1104 static bool classof(const IntrinsicInst *I) { 1105 return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic; 1106 } 1107 static bool classof(const Value *V) { 1108 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1109 } 1110 }; 1111 1112 /// This is the common base class for memset/memcpy/memmove. 1113 class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> { 1114 private: 1115 enum { ARG_VOLATILE = 3 }; 1116 1117 public: 1118 ConstantInt *getVolatileCst() const { 1119 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(ARG_VOLATILE))); 1120 } 1121 1122 bool isVolatile() const { return !getVolatileCst()->isZero(); } 1123 1124 void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); } 1125 1126 // Methods for support type inquiry through isa, cast, and dyn_cast: 1127 static bool classof(const IntrinsicInst *I) { 1128 switch (I->getIntrinsicID()) { 1129 case Intrinsic::memcpy: 1130 case Intrinsic::memmove: 1131 case Intrinsic::memset: 1132 case Intrinsic::memset_inline: 1133 case Intrinsic::memcpy_inline: 1134 return true; 1135 default: 1136 return false; 1137 } 1138 } 1139 static bool classof(const Value *V) { 1140 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1141 } 1142 }; 1143 1144 /// This class wraps the llvm.memset and llvm.memset.inline intrinsics. 1145 class MemSetInst : public MemSetBase<MemIntrinsic> { 1146 public: 1147 // Methods for support type inquiry through isa, cast, and dyn_cast: 1148 static bool classof(const IntrinsicInst *I) { 1149 switch (I->getIntrinsicID()) { 1150 case Intrinsic::memset: 1151 case Intrinsic::memset_inline: 1152 return true; 1153 default: 1154 return false; 1155 } 1156 } 1157 static bool classof(const Value *V) { 1158 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1159 } 1160 }; 1161 1162 /// This class wraps the llvm.memset.inline intrinsic. 1163 class MemSetInlineInst : public MemSetInst { 1164 public: 1165 ConstantInt *getLength() const { 1166 return cast<ConstantInt>(MemSetInst::getLength()); 1167 } 1168 // Methods for support type inquiry through isa, cast, and dyn_cast: 1169 static bool classof(const IntrinsicInst *I) { 1170 return I->getIntrinsicID() == Intrinsic::memset_inline; 1171 } 1172 static bool classof(const Value *V) { 1173 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1174 } 1175 }; 1176 1177 /// This class wraps the llvm.memcpy/memmove intrinsics. 1178 class MemTransferInst : public MemTransferBase<MemIntrinsic> { 1179 public: 1180 // Methods for support type inquiry through isa, cast, and dyn_cast: 1181 static bool classof(const IntrinsicInst *I) { 1182 switch (I->getIntrinsicID()) { 1183 case Intrinsic::memcpy: 1184 case Intrinsic::memmove: 1185 case Intrinsic::memcpy_inline: 1186 return true; 1187 default: 1188 return false; 1189 } 1190 } 1191 static bool classof(const Value *V) { 1192 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1193 } 1194 }; 1195 1196 /// This class wraps the llvm.memcpy intrinsic. 1197 class MemCpyInst : public MemTransferInst { 1198 public: 1199 // Methods for support type inquiry through isa, cast, and dyn_cast: 1200 static bool classof(const IntrinsicInst *I) { 1201 return I->getIntrinsicID() == Intrinsic::memcpy || 1202 I->getIntrinsicID() == Intrinsic::memcpy_inline; 1203 } 1204 static bool classof(const Value *V) { 1205 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1206 } 1207 }; 1208 1209 /// This class wraps the llvm.memmove intrinsic. 1210 class MemMoveInst : public MemTransferInst { 1211 public: 1212 // Methods for support type inquiry through isa, cast, and dyn_cast: 1213 static bool classof(const IntrinsicInst *I) { 1214 return I->getIntrinsicID() == Intrinsic::memmove; 1215 } 1216 static bool classof(const Value *V) { 1217 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1218 } 1219 }; 1220 1221 /// This class wraps the llvm.memcpy.inline intrinsic. 1222 class MemCpyInlineInst : public MemCpyInst { 1223 public: 1224 ConstantInt *getLength() const { 1225 return cast<ConstantInt>(MemCpyInst::getLength()); 1226 } 1227 // Methods for support type inquiry through isa, cast, and dyn_cast: 1228 static bool classof(const IntrinsicInst *I) { 1229 return I->getIntrinsicID() == Intrinsic::memcpy_inline; 1230 } 1231 static bool classof(const Value *V) { 1232 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1233 } 1234 }; 1235 1236 // The common base class for any memset/memmove/memcpy intrinsics; 1237 // whether they be atomic or non-atomic. 1238 // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove 1239 // and llvm.memset/memcpy/memmove 1240 class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> { 1241 public: 1242 bool isVolatile() const { 1243 // Only the non-atomic intrinsics can be volatile 1244 if (auto *MI = dyn_cast<MemIntrinsic>(this)) 1245 return MI->isVolatile(); 1246 return false; 1247 } 1248 1249 static bool classof(const IntrinsicInst *I) { 1250 switch (I->getIntrinsicID()) { 1251 case Intrinsic::memcpy: 1252 case Intrinsic::memcpy_inline: 1253 case Intrinsic::memmove: 1254 case Intrinsic::memset: 1255 case Intrinsic::memset_inline: 1256 case Intrinsic::memcpy_element_unordered_atomic: 1257 case Intrinsic::memmove_element_unordered_atomic: 1258 case Intrinsic::memset_element_unordered_atomic: 1259 return true; 1260 default: 1261 return false; 1262 } 1263 } 1264 static bool classof(const Value *V) { 1265 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1266 } 1267 }; 1268 1269 /// This class represents any memset intrinsic 1270 // i.e. llvm.element.unordered.atomic.memset 1271 // and llvm.memset 1272 class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> { 1273 public: 1274 static bool classof(const IntrinsicInst *I) { 1275 switch (I->getIntrinsicID()) { 1276 case Intrinsic::memset: 1277 case Intrinsic::memset_inline: 1278 case Intrinsic::memset_element_unordered_atomic: 1279 return true; 1280 default: 1281 return false; 1282 } 1283 } 1284 static bool classof(const Value *V) { 1285 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1286 } 1287 }; 1288 1289 // This class wraps any memcpy/memmove intrinsics 1290 // i.e. llvm.element.unordered.atomic.memcpy/memmove 1291 // and llvm.memcpy/memmove 1292 class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> { 1293 public: 1294 static bool classof(const IntrinsicInst *I) { 1295 switch (I->getIntrinsicID()) { 1296 case Intrinsic::memcpy: 1297 case Intrinsic::memcpy_inline: 1298 case Intrinsic::memmove: 1299 case Intrinsic::memcpy_element_unordered_atomic: 1300 case Intrinsic::memmove_element_unordered_atomic: 1301 return true; 1302 default: 1303 return false; 1304 } 1305 } 1306 static bool classof(const Value *V) { 1307 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1308 } 1309 }; 1310 1311 /// This class represents any memcpy intrinsic 1312 /// i.e. llvm.element.unordered.atomic.memcpy 1313 /// and llvm.memcpy 1314 class AnyMemCpyInst : public AnyMemTransferInst { 1315 public: 1316 static bool classof(const IntrinsicInst *I) { 1317 switch (I->getIntrinsicID()) { 1318 case Intrinsic::memcpy: 1319 case Intrinsic::memcpy_inline: 1320 case Intrinsic::memcpy_element_unordered_atomic: 1321 return true; 1322 default: 1323 return false; 1324 } 1325 } 1326 static bool classof(const Value *V) { 1327 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1328 } 1329 }; 1330 1331 /// This class represents any memmove intrinsic 1332 /// i.e. llvm.element.unordered.atomic.memmove 1333 /// and llvm.memmove 1334 class AnyMemMoveInst : public AnyMemTransferInst { 1335 public: 1336 static bool classof(const IntrinsicInst *I) { 1337 switch (I->getIntrinsicID()) { 1338 case Intrinsic::memmove: 1339 case Intrinsic::memmove_element_unordered_atomic: 1340 return true; 1341 default: 1342 return false; 1343 } 1344 } 1345 static bool classof(const Value *V) { 1346 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1347 } 1348 }; 1349 1350 /// This represents the llvm.va_start intrinsic. 1351 class VAStartInst : public IntrinsicInst { 1352 public: 1353 static bool classof(const IntrinsicInst *I) { 1354 return I->getIntrinsicID() == Intrinsic::vastart; 1355 } 1356 static bool classof(const Value *V) { 1357 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1358 } 1359 1360 Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); } 1361 }; 1362 1363 /// This represents the llvm.va_end intrinsic. 1364 class VAEndInst : public IntrinsicInst { 1365 public: 1366 static bool classof(const IntrinsicInst *I) { 1367 return I->getIntrinsicID() == Intrinsic::vaend; 1368 } 1369 static bool classof(const Value *V) { 1370 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1371 } 1372 1373 Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); } 1374 }; 1375 1376 /// This represents the llvm.va_copy intrinsic. 1377 class VACopyInst : public IntrinsicInst { 1378 public: 1379 static bool classof(const IntrinsicInst *I) { 1380 return I->getIntrinsicID() == Intrinsic::vacopy; 1381 } 1382 static bool classof(const Value *V) { 1383 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1384 } 1385 1386 Value *getDest() const { return const_cast<Value *>(getArgOperand(0)); } 1387 Value *getSrc() const { return const_cast<Value *>(getArgOperand(1)); } 1388 }; 1389 1390 /// A base class for all instrprof intrinsics. 1391 class InstrProfInstBase : public IntrinsicInst { 1392 public: 1393 // The name of the instrumented function. 1394 GlobalVariable *getName() const { 1395 return cast<GlobalVariable>( 1396 const_cast<Value *>(getArgOperand(0))->stripPointerCasts()); 1397 } 1398 // The hash of the CFG for the instrumented function. 1399 ConstantInt *getHash() const { 1400 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 1401 } 1402 // The number of counters for the instrumented function. 1403 ConstantInt *getNumCounters() const; 1404 // The index of the counter that this instruction acts on. 1405 ConstantInt *getIndex() const; 1406 }; 1407 1408 /// This represents the llvm.instrprof.cover intrinsic. 1409 class InstrProfCoverInst : public InstrProfInstBase { 1410 public: 1411 static bool classof(const IntrinsicInst *I) { 1412 return I->getIntrinsicID() == Intrinsic::instrprof_cover; 1413 } 1414 static bool classof(const Value *V) { 1415 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1416 } 1417 }; 1418 1419 /// This represents the llvm.instrprof.increment intrinsic. 1420 class InstrProfIncrementInst : public InstrProfInstBase { 1421 public: 1422 static bool classof(const IntrinsicInst *I) { 1423 return I->getIntrinsicID() == Intrinsic::instrprof_increment || 1424 I->getIntrinsicID() == Intrinsic::instrprof_increment_step; 1425 } 1426 static bool classof(const Value *V) { 1427 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1428 } 1429 Value *getStep() const; 1430 }; 1431 1432 /// This represents the llvm.instrprof.increment.step intrinsic. 1433 class InstrProfIncrementInstStep : public InstrProfIncrementInst { 1434 public: 1435 static bool classof(const IntrinsicInst *I) { 1436 return I->getIntrinsicID() == Intrinsic::instrprof_increment_step; 1437 } 1438 static bool classof(const Value *V) { 1439 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1440 } 1441 }; 1442 1443 /// This represents the llvm.instrprof.timestamp intrinsic. 1444 class InstrProfTimestampInst : public InstrProfInstBase { 1445 public: 1446 static bool classof(const IntrinsicInst *I) { 1447 return I->getIntrinsicID() == Intrinsic::instrprof_timestamp; 1448 } 1449 static bool classof(const Value *V) { 1450 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1451 } 1452 }; 1453 1454 /// This represents the llvm.instrprof.value.profile intrinsic. 1455 class InstrProfValueProfileInst : public InstrProfInstBase { 1456 public: 1457 static bool classof(const IntrinsicInst *I) { 1458 return I->getIntrinsicID() == Intrinsic::instrprof_value_profile; 1459 } 1460 static bool classof(const Value *V) { 1461 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1462 } 1463 1464 Value *getTargetValue() const { 1465 return cast<Value>(const_cast<Value *>(getArgOperand(2))); 1466 } 1467 1468 ConstantInt *getValueKind() const { 1469 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 1470 } 1471 1472 // Returns the value site index. 1473 ConstantInt *getIndex() const { 1474 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4))); 1475 } 1476 }; 1477 1478 class PseudoProbeInst : public IntrinsicInst { 1479 public: 1480 static bool classof(const IntrinsicInst *I) { 1481 return I->getIntrinsicID() == Intrinsic::pseudoprobe; 1482 } 1483 1484 static bool classof(const Value *V) { 1485 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1486 } 1487 1488 ConstantInt *getFuncGuid() const { 1489 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(0))); 1490 } 1491 1492 ConstantInt *getIndex() const { 1493 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1))); 1494 } 1495 1496 ConstantInt *getAttributes() const { 1497 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2))); 1498 } 1499 1500 ConstantInt *getFactor() const { 1501 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3))); 1502 } 1503 }; 1504 1505 class NoAliasScopeDeclInst : public IntrinsicInst { 1506 public: 1507 static bool classof(const IntrinsicInst *I) { 1508 return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl; 1509 } 1510 1511 static bool classof(const Value *V) { 1512 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1513 } 1514 1515 MDNode *getScopeList() const { 1516 auto *MV = 1517 cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg)); 1518 return cast<MDNode>(MV->getMetadata()); 1519 } 1520 1521 void setScopeList(MDNode *ScopeList) { 1522 setOperand(Intrinsic::NoAliasScopeDeclScopeArg, 1523 MetadataAsValue::get(getContext(), ScopeList)); 1524 } 1525 }; 1526 1527 /// Common base class for representing values projected from a statepoint. 1528 /// Currently, the only projections available are gc.result and gc.relocate. 1529 class GCProjectionInst : public IntrinsicInst { 1530 public: 1531 static bool classof(const IntrinsicInst *I) { 1532 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate || 1533 I->getIntrinsicID() == Intrinsic::experimental_gc_result; 1534 } 1535 1536 static bool classof(const Value *V) { 1537 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1538 } 1539 1540 /// Return true if this relocate is tied to the invoke statepoint. 1541 /// This includes relocates which are on the unwinding path. 1542 bool isTiedToInvoke() const { 1543 const Value *Token = getArgOperand(0); 1544 1545 return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token); 1546 } 1547 1548 /// The statepoint with which this gc.relocate is associated. 1549 const Value *getStatepoint() const; 1550 }; 1551 1552 /// Represents calls to the gc.relocate intrinsic. 1553 class GCRelocateInst : public GCProjectionInst { 1554 public: 1555 static bool classof(const IntrinsicInst *I) { 1556 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate; 1557 } 1558 1559 static bool classof(const Value *V) { 1560 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1561 } 1562 1563 /// The index into the associate statepoint's argument list 1564 /// which contains the base pointer of the pointer whose 1565 /// relocation this gc.relocate describes. 1566 unsigned getBasePtrIndex() const { 1567 return cast<ConstantInt>(getArgOperand(1))->getZExtValue(); 1568 } 1569 1570 /// The index into the associate statepoint's argument list which 1571 /// contains the pointer whose relocation this gc.relocate describes. 1572 unsigned getDerivedPtrIndex() const { 1573 return cast<ConstantInt>(getArgOperand(2))->getZExtValue(); 1574 } 1575 1576 Value *getBasePtr() const; 1577 Value *getDerivedPtr() const; 1578 }; 1579 1580 /// Represents calls to the gc.result intrinsic. 1581 class GCResultInst : public GCProjectionInst { 1582 public: 1583 static bool classof(const IntrinsicInst *I) { 1584 return I->getIntrinsicID() == Intrinsic::experimental_gc_result; 1585 } 1586 1587 static bool classof(const Value *V) { 1588 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1589 } 1590 }; 1591 1592 1593 /// This represents the llvm.assume intrinsic. 1594 class AssumeInst : public IntrinsicInst { 1595 public: 1596 static bool classof(const IntrinsicInst *I) { 1597 return I->getIntrinsicID() == Intrinsic::assume; 1598 } 1599 static bool classof(const Value *V) { 1600 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V)); 1601 } 1602 }; 1603 1604 } // end namespace llvm 1605 1606 #endif // LLVM_IR_INTRINSICINST_H 1607