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