1 //===- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- 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 contains the declaration of the MachineInstr class, which is the 10 // basic representation for all target dependent machine instructions used by 11 // the back end. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CODEGEN_MACHINEINSTR_H 16 #define LLVM_CODEGEN_MACHINEINSTR_H 17 18 #include "llvm/ADT/DenseMapInfo.h" 19 #include "llvm/ADT/PointerSumType.h" 20 #include "llvm/ADT/SmallSet.h" 21 #include "llvm/ADT/ilist.h" 22 #include "llvm/ADT/ilist_node.h" 23 #include "llvm/ADT/iterator_range.h" 24 #include "llvm/CodeGen/MachineMemOperand.h" 25 #include "llvm/CodeGen/MachineOperand.h" 26 #include "llvm/CodeGen/TargetOpcodes.h" 27 #include "llvm/IR/DebugLoc.h" 28 #include "llvm/IR/InlineAsm.h" 29 #include "llvm/IR/PseudoProbe.h" 30 #include "llvm/MC/MCInstrDesc.h" 31 #include "llvm/MC/MCSymbol.h" 32 #include "llvm/Support/ArrayRecycler.h" 33 #include "llvm/Support/TrailingObjects.h" 34 #include <algorithm> 35 #include <cassert> 36 #include <cstdint> 37 #include <utility> 38 39 namespace llvm { 40 41 class AAResults; 42 template <typename T> class ArrayRef; 43 class DIExpression; 44 class DILocalVariable; 45 class MachineBasicBlock; 46 class MachineFunction; 47 class MachineRegisterInfo; 48 class ModuleSlotTracker; 49 class raw_ostream; 50 template <typename T> class SmallVectorImpl; 51 class SmallBitVector; 52 class StringRef; 53 class TargetInstrInfo; 54 class TargetRegisterClass; 55 class TargetRegisterInfo; 56 57 //===----------------------------------------------------------------------===// 58 /// Representation of each machine instruction. 59 /// 60 /// This class isn't a POD type, but it must have a trivial destructor. When a 61 /// MachineFunction is deleted, all the contained MachineInstrs are deallocated 62 /// without having their destructor called. 63 /// 64 class MachineInstr 65 : public ilist_node_with_parent<MachineInstr, MachineBasicBlock, 66 ilist_sentinel_tracking<true>> { 67 public: 68 using mmo_iterator = ArrayRef<MachineMemOperand *>::iterator; 69 70 /// Flags to specify different kinds of comments to output in 71 /// assembly code. These flags carry semantic information not 72 /// otherwise easily derivable from the IR text. 73 /// 74 enum CommentFlag { 75 ReloadReuse = 0x1, // higher bits are reserved for target dep comments. 76 NoSchedComment = 0x2, 77 TAsmComments = 0x4 // Target Asm comments should start from this value. 78 }; 79 80 enum MIFlag { 81 NoFlags = 0, 82 FrameSetup = 1 << 0, // Instruction is used as a part of 83 // function frame setup code. 84 FrameDestroy = 1 << 1, // Instruction is used as a part of 85 // function frame destruction code. 86 BundledPred = 1 << 2, // Instruction has bundled predecessors. 87 BundledSucc = 1 << 3, // Instruction has bundled successors. 88 FmNoNans = 1 << 4, // Instruction does not support Fast 89 // math nan values. 90 FmNoInfs = 1 << 5, // Instruction does not support Fast 91 // math infinity values. 92 FmNsz = 1 << 6, // Instruction is not required to retain 93 // signed zero values. 94 FmArcp = 1 << 7, // Instruction supports Fast math 95 // reciprocal approximations. 96 FmContract = 1 << 8, // Instruction supports Fast math 97 // contraction operations like fma. 98 FmAfn = 1 << 9, // Instruction may map to Fast math 99 // instrinsic approximation. 100 FmReassoc = 1 << 10, // Instruction supports Fast math 101 // reassociation of operand order. 102 NoUWrap = 1 << 11, // Instruction supports binary operator 103 // no unsigned wrap. 104 NoSWrap = 1 << 12, // Instruction supports binary operator 105 // no signed wrap. 106 IsExact = 1 << 13, // Instruction supports division is 107 // known to be exact. 108 NoFPExcept = 1 << 14, // Instruction does not raise 109 // floatint-point exceptions. 110 NoMerge = 1 << 15, // Passes that drop source location info 111 // (e.g. branch folding) should skip 112 // this instruction. 113 }; 114 115 private: 116 const MCInstrDesc *MCID; // Instruction descriptor. 117 MachineBasicBlock *Parent = nullptr; // Pointer to the owning basic block. 118 119 // Operands are allocated by an ArrayRecycler. 120 MachineOperand *Operands = nullptr; // Pointer to the first operand. 121 unsigned NumOperands = 0; // Number of operands on instruction. 122 123 uint16_t Flags = 0; // Various bits of additional 124 // information about machine 125 // instruction. 126 127 uint8_t AsmPrinterFlags = 0; // Various bits of information used by 128 // the AsmPrinter to emit helpful 129 // comments. This is *not* semantic 130 // information. Do not use this for 131 // anything other than to convey comment 132 // information to AsmPrinter. 133 134 // OperandCapacity has uint8_t size, so it should be next to AsmPrinterFlags 135 // to properly pack. 136 using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity; 137 OperandCapacity CapOperands; // Capacity of the Operands array. 138 139 /// Internal implementation detail class that provides out-of-line storage for 140 /// extra info used by the machine instruction when this info cannot be stored 141 /// in-line within the instruction itself. 142 /// 143 /// This has to be defined eagerly due to the implementation constraints of 144 /// `PointerSumType` where it is used. 145 class ExtraInfo final 146 : TrailingObjects<ExtraInfo, MachineMemOperand *, MCSymbol *, MDNode *> { 147 public: 148 static ExtraInfo *create(BumpPtrAllocator &Allocator, 149 ArrayRef<MachineMemOperand *> MMOs, 150 MCSymbol *PreInstrSymbol = nullptr, 151 MCSymbol *PostInstrSymbol = nullptr, 152 MDNode *HeapAllocMarker = nullptr) { 153 bool HasPreInstrSymbol = PreInstrSymbol != nullptr; 154 bool HasPostInstrSymbol = PostInstrSymbol != nullptr; 155 bool HasHeapAllocMarker = HeapAllocMarker != nullptr; 156 auto *Result = new (Allocator.Allocate( 157 totalSizeToAlloc<MachineMemOperand *, MCSymbol *, MDNode *>( 158 MMOs.size(), HasPreInstrSymbol + HasPostInstrSymbol, 159 HasHeapAllocMarker), 160 alignof(ExtraInfo))) 161 ExtraInfo(MMOs.size(), HasPreInstrSymbol, HasPostInstrSymbol, 162 HasHeapAllocMarker); 163 164 // Copy the actual data into the trailing objects. 165 std::copy(MMOs.begin(), MMOs.end(), 166 Result->getTrailingObjects<MachineMemOperand *>()); 167 168 if (HasPreInstrSymbol) 169 Result->getTrailingObjects<MCSymbol *>()[0] = PreInstrSymbol; 170 if (HasPostInstrSymbol) 171 Result->getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol] = 172 PostInstrSymbol; 173 if (HasHeapAllocMarker) 174 Result->getTrailingObjects<MDNode *>()[0] = HeapAllocMarker; 175 176 return Result; 177 } 178 179 ArrayRef<MachineMemOperand *> getMMOs() const { 180 return makeArrayRef(getTrailingObjects<MachineMemOperand *>(), NumMMOs); 181 } 182 183 MCSymbol *getPreInstrSymbol() const { 184 return HasPreInstrSymbol ? getTrailingObjects<MCSymbol *>()[0] : nullptr; 185 } 186 187 MCSymbol *getPostInstrSymbol() const { 188 return HasPostInstrSymbol 189 ? getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol] 190 : nullptr; 191 } 192 193 MDNode *getHeapAllocMarker() const { 194 return HasHeapAllocMarker ? getTrailingObjects<MDNode *>()[0] : nullptr; 195 } 196 197 private: 198 friend TrailingObjects; 199 200 // Description of the extra info, used to interpret the actual optional 201 // data appended. 202 // 203 // Note that this is not terribly space optimized. This leaves a great deal 204 // of flexibility to fit more in here later. 205 const int NumMMOs; 206 const bool HasPreInstrSymbol; 207 const bool HasPostInstrSymbol; 208 const bool HasHeapAllocMarker; 209 210 // Implement the `TrailingObjects` internal API. 211 size_t numTrailingObjects(OverloadToken<MachineMemOperand *>) const { 212 return NumMMOs; 213 } 214 size_t numTrailingObjects(OverloadToken<MCSymbol *>) const { 215 return HasPreInstrSymbol + HasPostInstrSymbol; 216 } 217 size_t numTrailingObjects(OverloadToken<MDNode *>) const { 218 return HasHeapAllocMarker; 219 } 220 221 // Just a boring constructor to allow us to initialize the sizes. Always use 222 // the `create` routine above. 223 ExtraInfo(int NumMMOs, bool HasPreInstrSymbol, bool HasPostInstrSymbol, 224 bool HasHeapAllocMarker) 225 : NumMMOs(NumMMOs), HasPreInstrSymbol(HasPreInstrSymbol), 226 HasPostInstrSymbol(HasPostInstrSymbol), 227 HasHeapAllocMarker(HasHeapAllocMarker) {} 228 }; 229 230 /// Enumeration of the kinds of inline extra info available. It is important 231 /// that the `MachineMemOperand` inline kind has a tag value of zero to make 232 /// it accessible as an `ArrayRef`. 233 enum ExtraInfoInlineKinds { 234 EIIK_MMO = 0, 235 EIIK_PreInstrSymbol, 236 EIIK_PostInstrSymbol, 237 EIIK_OutOfLine 238 }; 239 240 // We store extra information about the instruction here. The common case is 241 // expected to be nothing or a single pointer (typically a MMO or a symbol). 242 // We work to optimize this common case by storing it inline here rather than 243 // requiring a separate allocation, but we fall back to an allocation when 244 // multiple pointers are needed. 245 PointerSumType<ExtraInfoInlineKinds, 246 PointerSumTypeMember<EIIK_MMO, MachineMemOperand *>, 247 PointerSumTypeMember<EIIK_PreInstrSymbol, MCSymbol *>, 248 PointerSumTypeMember<EIIK_PostInstrSymbol, MCSymbol *>, 249 PointerSumTypeMember<EIIK_OutOfLine, ExtraInfo *>> 250 Info; 251 252 DebugLoc debugLoc; // Source line information. 253 254 /// Unique instruction number. Used by DBG_INSTR_REFs to refer to the values 255 /// defined by this instruction. 256 unsigned DebugInstrNum; 257 258 // Intrusive list support 259 friend struct ilist_traits<MachineInstr>; 260 friend struct ilist_callback_traits<MachineBasicBlock>; 261 void setParent(MachineBasicBlock *P) { Parent = P; } 262 263 /// This constructor creates a copy of the given 264 /// MachineInstr in the given MachineFunction. 265 MachineInstr(MachineFunction &, const MachineInstr &); 266 267 /// This constructor create a MachineInstr and add the implicit operands. 268 /// It reserves space for number of operands specified by 269 /// MCInstrDesc. An explicit DebugLoc is supplied. 270 MachineInstr(MachineFunction &, const MCInstrDesc &tid, DebugLoc dl, 271 bool NoImp = false); 272 273 // MachineInstrs are pool-allocated and owned by MachineFunction. 274 friend class MachineFunction; 275 276 void 277 dumprImpl(const MachineRegisterInfo &MRI, unsigned Depth, unsigned MaxDepth, 278 SmallPtrSetImpl<const MachineInstr *> &AlreadySeenInstrs) const; 279 280 public: 281 MachineInstr(const MachineInstr &) = delete; 282 MachineInstr &operator=(const MachineInstr &) = delete; 283 // Use MachineFunction::DeleteMachineInstr() instead. 284 ~MachineInstr() = delete; 285 286 const MachineBasicBlock* getParent() const { return Parent; } 287 MachineBasicBlock* getParent() { return Parent; } 288 289 /// Move the instruction before \p MovePos. 290 void moveBefore(MachineInstr *MovePos); 291 292 /// Return the function that contains the basic block that this instruction 293 /// belongs to. 294 /// 295 /// Note: this is undefined behaviour if the instruction does not have a 296 /// parent. 297 const MachineFunction *getMF() const; 298 MachineFunction *getMF() { 299 return const_cast<MachineFunction *>( 300 static_cast<const MachineInstr *>(this)->getMF()); 301 } 302 303 /// Return the asm printer flags bitvector. 304 uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; } 305 306 /// Clear the AsmPrinter bitvector. 307 void clearAsmPrinterFlags() { AsmPrinterFlags = 0; } 308 309 /// Return whether an AsmPrinter flag is set. 310 bool getAsmPrinterFlag(CommentFlag Flag) const { 311 return AsmPrinterFlags & Flag; 312 } 313 314 /// Set a flag for the AsmPrinter. 315 void setAsmPrinterFlag(uint8_t Flag) { 316 AsmPrinterFlags |= Flag; 317 } 318 319 /// Clear specific AsmPrinter flags. 320 void clearAsmPrinterFlag(CommentFlag Flag) { 321 AsmPrinterFlags &= ~Flag; 322 } 323 324 /// Return the MI flags bitvector. 325 uint16_t getFlags() const { 326 return Flags; 327 } 328 329 /// Return whether an MI flag is set. 330 bool getFlag(MIFlag Flag) const { 331 return Flags & Flag; 332 } 333 334 /// Set a MI flag. 335 void setFlag(MIFlag Flag) { 336 Flags |= (uint16_t)Flag; 337 } 338 339 void setFlags(unsigned flags) { 340 // Filter out the automatically maintained flags. 341 unsigned Mask = BundledPred | BundledSucc; 342 Flags = (Flags & Mask) | (flags & ~Mask); 343 } 344 345 /// clearFlag - Clear a MI flag. 346 void clearFlag(MIFlag Flag) { 347 Flags &= ~((uint16_t)Flag); 348 } 349 350 /// Return true if MI is in a bundle (but not the first MI in a bundle). 351 /// 352 /// A bundle looks like this before it's finalized: 353 /// ---------------- 354 /// | MI | 355 /// ---------------- 356 /// | 357 /// ---------------- 358 /// | MI * | 359 /// ---------------- 360 /// | 361 /// ---------------- 362 /// | MI * | 363 /// ---------------- 364 /// In this case, the first MI starts a bundle but is not inside a bundle, the 365 /// next 2 MIs are considered "inside" the bundle. 366 /// 367 /// After a bundle is finalized, it looks like this: 368 /// ---------------- 369 /// | Bundle | 370 /// ---------------- 371 /// | 372 /// ---------------- 373 /// | MI * | 374 /// ---------------- 375 /// | 376 /// ---------------- 377 /// | MI * | 378 /// ---------------- 379 /// | 380 /// ---------------- 381 /// | MI * | 382 /// ---------------- 383 /// The first instruction has the special opcode "BUNDLE". It's not "inside" 384 /// a bundle, but the next three MIs are. 385 bool isInsideBundle() const { 386 return getFlag(BundledPred); 387 } 388 389 /// Return true if this instruction part of a bundle. This is true 390 /// if either itself or its following instruction is marked "InsideBundle". 391 bool isBundled() const { 392 return isBundledWithPred() || isBundledWithSucc(); 393 } 394 395 /// Return true if this instruction is part of a bundle, and it is not the 396 /// first instruction in the bundle. 397 bool isBundledWithPred() const { return getFlag(BundledPred); } 398 399 /// Return true if this instruction is part of a bundle, and it is not the 400 /// last instruction in the bundle. 401 bool isBundledWithSucc() const { return getFlag(BundledSucc); } 402 403 /// Bundle this instruction with its predecessor. This can be an unbundled 404 /// instruction, or it can be the first instruction in a bundle. 405 void bundleWithPred(); 406 407 /// Bundle this instruction with its successor. This can be an unbundled 408 /// instruction, or it can be the last instruction in a bundle. 409 void bundleWithSucc(); 410 411 /// Break bundle above this instruction. 412 void unbundleFromPred(); 413 414 /// Break bundle below this instruction. 415 void unbundleFromSucc(); 416 417 /// Returns the debug location id of this MachineInstr. 418 const DebugLoc &getDebugLoc() const { return debugLoc; } 419 420 /// Return the operand containing the offset to be used if this DBG_VALUE 421 /// instruction is indirect; will be an invalid register if this value is 422 /// not indirect, and an immediate with value 0 otherwise. 423 const MachineOperand &getDebugOffset() const { 424 assert(isNonListDebugValue() && "not a DBG_VALUE"); 425 return getOperand(1); 426 } 427 MachineOperand &getDebugOffset() { 428 assert(isNonListDebugValue() && "not a DBG_VALUE"); 429 return getOperand(1); 430 } 431 432 /// Return the operand for the debug variable referenced by 433 /// this DBG_VALUE instruction. 434 const MachineOperand &getDebugVariableOp() const; 435 MachineOperand &getDebugVariableOp(); 436 437 /// Return the debug variable referenced by 438 /// this DBG_VALUE instruction. 439 const DILocalVariable *getDebugVariable() const; 440 441 /// Return the operand for the complex address expression referenced by 442 /// this DBG_VALUE instruction. 443 const MachineOperand &getDebugExpressionOp() const; 444 MachineOperand &getDebugExpressionOp(); 445 446 /// Return the complex address expression referenced by 447 /// this DBG_VALUE instruction. 448 const DIExpression *getDebugExpression() const; 449 450 /// Return the debug label referenced by 451 /// this DBG_LABEL instruction. 452 const DILabel *getDebugLabel() const; 453 454 /// Fetch the instruction number of this MachineInstr. If it does not have 455 /// one already, a new and unique number will be assigned. 456 unsigned getDebugInstrNum(); 457 458 /// Fetch instruction number of this MachineInstr -- but before it's inserted 459 /// into \p MF. Needed for transformations that create an instruction but 460 /// don't immediately insert them. 461 unsigned getDebugInstrNum(MachineFunction &MF); 462 463 /// Examine the instruction number of this MachineInstr. May be zero if 464 /// it hasn't been assigned a number yet. 465 unsigned peekDebugInstrNum() const { return DebugInstrNum; } 466 467 /// Set instruction number of this MachineInstr. Avoid using unless you're 468 /// deserializing this information. 469 void setDebugInstrNum(unsigned Num) { DebugInstrNum = Num; } 470 471 /// Emit an error referring to the source location of this instruction. 472 /// This should only be used for inline assembly that is somehow 473 /// impossible to compile. Other errors should have been handled much 474 /// earlier. 475 /// 476 /// If this method returns, the caller should try to recover from the error. 477 void emitError(StringRef Msg) const; 478 479 /// Returns the target instruction descriptor of this MachineInstr. 480 const MCInstrDesc &getDesc() const { return *MCID; } 481 482 /// Returns the opcode of this MachineInstr. 483 unsigned getOpcode() const { return MCID->Opcode; } 484 485 /// Retuns the total number of operands. 486 unsigned getNumOperands() const { return NumOperands; } 487 488 /// Returns the total number of operands which are debug locations. 489 unsigned getNumDebugOperands() const { 490 return std::distance(debug_operands().begin(), debug_operands().end()); 491 } 492 493 const MachineOperand& getOperand(unsigned i) const { 494 assert(i < getNumOperands() && "getOperand() out of range!"); 495 return Operands[i]; 496 } 497 MachineOperand& getOperand(unsigned i) { 498 assert(i < getNumOperands() && "getOperand() out of range!"); 499 return Operands[i]; 500 } 501 502 MachineOperand &getDebugOperand(unsigned Index) { 503 assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!"); 504 return *(debug_operands().begin() + Index); 505 } 506 const MachineOperand &getDebugOperand(unsigned Index) const { 507 assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!"); 508 return *(debug_operands().begin() + Index); 509 } 510 511 SmallSet<Register, 4> getUsedDebugRegs() const { 512 assert(isDebugValue() && "not a DBG_VALUE*"); 513 SmallSet<Register, 4> UsedRegs; 514 for (auto MO : debug_operands()) 515 if (MO.isReg() && MO.getReg()) 516 UsedRegs.insert(MO.getReg()); 517 return UsedRegs; 518 } 519 520 /// Returns whether this debug value has at least one debug operand with the 521 /// register \p Reg. 522 bool hasDebugOperandForReg(Register Reg) const { 523 return any_of(debug_operands(), [Reg](const MachineOperand &Op) { 524 return Op.isReg() && Op.getReg() == Reg; 525 }); 526 } 527 528 /// Returns a range of all of the operands that correspond to a debug use of 529 /// \p Reg. 530 template <typename Operand, typename Instruction> 531 static iterator_range< 532 filter_iterator<Operand *, std::function<bool(Operand &Op)>>> 533 getDebugOperandsForReg(Instruction *MI, Register Reg) { 534 std::function<bool(Operand & Op)> OpUsesReg( 535 [Reg](Operand &Op) { return Op.isReg() && Op.getReg() == Reg; }); 536 return make_filter_range(MI->debug_operands(), OpUsesReg); 537 } 538 iterator_range<filter_iterator<const MachineOperand *, 539 std::function<bool(const MachineOperand &Op)>>> 540 getDebugOperandsForReg(Register Reg) const { 541 return MachineInstr::getDebugOperandsForReg<const MachineOperand, 542 const MachineInstr>(this, Reg); 543 } 544 iterator_range<filter_iterator<MachineOperand *, 545 std::function<bool(MachineOperand &Op)>>> 546 getDebugOperandsForReg(Register Reg) { 547 return MachineInstr::getDebugOperandsForReg<MachineOperand, MachineInstr>( 548 this, Reg); 549 } 550 551 bool isDebugOperand(const MachineOperand *Op) const { 552 return Op >= adl_begin(debug_operands()) && Op <= adl_end(debug_operands()); 553 } 554 555 unsigned getDebugOperandIndex(const MachineOperand *Op) const { 556 assert(isDebugOperand(Op) && "Expected a debug operand."); 557 return std::distance(adl_begin(debug_operands()), Op); 558 } 559 560 /// Returns the total number of definitions. 561 unsigned getNumDefs() const { 562 return getNumExplicitDefs() + MCID->getNumImplicitDefs(); 563 } 564 565 /// Returns true if the instruction has implicit definition. 566 bool hasImplicitDef() const { 567 for (unsigned I = getNumExplicitOperands(), E = getNumOperands(); 568 I != E; ++I) { 569 const MachineOperand &MO = getOperand(I); 570 if (MO.isDef() && MO.isImplicit()) 571 return true; 572 } 573 return false; 574 } 575 576 /// Returns the implicit operands number. 577 unsigned getNumImplicitOperands() const { 578 return getNumOperands() - getNumExplicitOperands(); 579 } 580 581 /// Return true if operand \p OpIdx is a subregister index. 582 bool isOperandSubregIdx(unsigned OpIdx) const { 583 assert(getOperand(OpIdx).getType() == MachineOperand::MO_Immediate && 584 "Expected MO_Immediate operand type."); 585 if (isExtractSubreg() && OpIdx == 2) 586 return true; 587 if (isInsertSubreg() && OpIdx == 3) 588 return true; 589 if (isRegSequence() && OpIdx > 1 && (OpIdx % 2) == 0) 590 return true; 591 if (isSubregToReg() && OpIdx == 3) 592 return true; 593 return false; 594 } 595 596 /// Returns the number of non-implicit operands. 597 unsigned getNumExplicitOperands() const; 598 599 /// Returns the number of non-implicit definitions. 600 unsigned getNumExplicitDefs() const; 601 602 /// iterator/begin/end - Iterate over all operands of a machine instruction. 603 using mop_iterator = MachineOperand *; 604 using const_mop_iterator = const MachineOperand *; 605 606 mop_iterator operands_begin() { return Operands; } 607 mop_iterator operands_end() { return Operands + NumOperands; } 608 609 const_mop_iterator operands_begin() const { return Operands; } 610 const_mop_iterator operands_end() const { return Operands + NumOperands; } 611 612 iterator_range<mop_iterator> operands() { 613 return make_range(operands_begin(), operands_end()); 614 } 615 iterator_range<const_mop_iterator> operands() const { 616 return make_range(operands_begin(), operands_end()); 617 } 618 iterator_range<mop_iterator> explicit_operands() { 619 return make_range(operands_begin(), 620 operands_begin() + getNumExplicitOperands()); 621 } 622 iterator_range<const_mop_iterator> explicit_operands() const { 623 return make_range(operands_begin(), 624 operands_begin() + getNumExplicitOperands()); 625 } 626 iterator_range<mop_iterator> implicit_operands() { 627 return make_range(explicit_operands().end(), operands_end()); 628 } 629 iterator_range<const_mop_iterator> implicit_operands() const { 630 return make_range(explicit_operands().end(), operands_end()); 631 } 632 /// Returns a range over all operands that are used to determine the variable 633 /// location for this DBG_VALUE instruction. 634 iterator_range<mop_iterator> debug_operands() { 635 assert(isDebugValue() && "Must be a debug value instruction."); 636 return isDebugValueList() 637 ? make_range(operands_begin() + 2, operands_end()) 638 : make_range(operands_begin(), operands_begin() + 1); 639 } 640 /// \copydoc debug_operands() 641 iterator_range<const_mop_iterator> debug_operands() const { 642 assert(isDebugValue() && "Must be a debug value instruction."); 643 return isDebugValueList() 644 ? make_range(operands_begin() + 2, operands_end()) 645 : make_range(operands_begin(), operands_begin() + 1); 646 } 647 /// Returns a range over all explicit operands that are register definitions. 648 /// Implicit definition are not included! 649 iterator_range<mop_iterator> defs() { 650 return make_range(operands_begin(), 651 operands_begin() + getNumExplicitDefs()); 652 } 653 /// \copydoc defs() 654 iterator_range<const_mop_iterator> defs() const { 655 return make_range(operands_begin(), 656 operands_begin() + getNumExplicitDefs()); 657 } 658 /// Returns a range that includes all operands that are register uses. 659 /// This may include unrelated operands which are not register uses. 660 iterator_range<mop_iterator> uses() { 661 return make_range(operands_begin() + getNumExplicitDefs(), operands_end()); 662 } 663 /// \copydoc uses() 664 iterator_range<const_mop_iterator> uses() const { 665 return make_range(operands_begin() + getNumExplicitDefs(), operands_end()); 666 } 667 iterator_range<mop_iterator> explicit_uses() { 668 return make_range(operands_begin() + getNumExplicitDefs(), 669 operands_begin() + getNumExplicitOperands()); 670 } 671 iterator_range<const_mop_iterator> explicit_uses() const { 672 return make_range(operands_begin() + getNumExplicitDefs(), 673 operands_begin() + getNumExplicitOperands()); 674 } 675 676 /// Returns the number of the operand iterator \p I points to. 677 unsigned getOperandNo(const_mop_iterator I) const { 678 return I - operands_begin(); 679 } 680 681 /// Access to memory operands of the instruction. If there are none, that does 682 /// not imply anything about whether the function accesses memory. Instead, 683 /// the caller must behave conservatively. 684 ArrayRef<MachineMemOperand *> memoperands() const { 685 if (!Info) 686 return {}; 687 688 if (Info.is<EIIK_MMO>()) 689 return makeArrayRef(Info.getAddrOfZeroTagPointer(), 1); 690 691 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 692 return EI->getMMOs(); 693 694 return {}; 695 } 696 697 /// Access to memory operands of the instruction. 698 /// 699 /// If `memoperands_begin() == memoperands_end()`, that does not imply 700 /// anything about whether the function accesses memory. Instead, the caller 701 /// must behave conservatively. 702 mmo_iterator memoperands_begin() const { return memoperands().begin(); } 703 704 /// Access to memory operands of the instruction. 705 /// 706 /// If `memoperands_begin() == memoperands_end()`, that does not imply 707 /// anything about whether the function accesses memory. Instead, the caller 708 /// must behave conservatively. 709 mmo_iterator memoperands_end() const { return memoperands().end(); } 710 711 /// Return true if we don't have any memory operands which described the 712 /// memory access done by this instruction. If this is true, calling code 713 /// must be conservative. 714 bool memoperands_empty() const { return memoperands().empty(); } 715 716 /// Return true if this instruction has exactly one MachineMemOperand. 717 bool hasOneMemOperand() const { return memoperands().size() == 1; } 718 719 /// Return the number of memory operands. 720 unsigned getNumMemOperands() const { return memoperands().size(); } 721 722 /// Helper to extract a pre-instruction symbol if one has been added. 723 MCSymbol *getPreInstrSymbol() const { 724 if (!Info) 725 return nullptr; 726 if (MCSymbol *S = Info.get<EIIK_PreInstrSymbol>()) 727 return S; 728 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 729 return EI->getPreInstrSymbol(); 730 731 return nullptr; 732 } 733 734 /// Helper to extract a post-instruction symbol if one has been added. 735 MCSymbol *getPostInstrSymbol() const { 736 if (!Info) 737 return nullptr; 738 if (MCSymbol *S = Info.get<EIIK_PostInstrSymbol>()) 739 return S; 740 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 741 return EI->getPostInstrSymbol(); 742 743 return nullptr; 744 } 745 746 /// Helper to extract a heap alloc marker if one has been added. 747 MDNode *getHeapAllocMarker() const { 748 if (!Info) 749 return nullptr; 750 if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>()) 751 return EI->getHeapAllocMarker(); 752 753 return nullptr; 754 } 755 756 /// API for querying MachineInstr properties. They are the same as MCInstrDesc 757 /// queries but they are bundle aware. 758 759 enum QueryType { 760 IgnoreBundle, // Ignore bundles 761 AnyInBundle, // Return true if any instruction in bundle has property 762 AllInBundle // Return true if all instructions in bundle have property 763 }; 764 765 /// Return true if the instruction (or in the case of a bundle, 766 /// the instructions inside the bundle) has the specified property. 767 /// The first argument is the property being queried. 768 /// The second argument indicates whether the query should look inside 769 /// instruction bundles. 770 bool hasProperty(unsigned MCFlag, QueryType Type = AnyInBundle) const { 771 assert(MCFlag < 64 && 772 "MCFlag out of range for bit mask in getFlags/hasPropertyInBundle."); 773 // Inline the fast path for unbundled or bundle-internal instructions. 774 if (Type == IgnoreBundle || !isBundled() || isBundledWithPred()) 775 return getDesc().getFlags() & (1ULL << MCFlag); 776 777 // If this is the first instruction in a bundle, take the slow path. 778 return hasPropertyInBundle(1ULL << MCFlag, Type); 779 } 780 781 /// Return true if this is an instruction that should go through the usual 782 /// legalization steps. 783 bool isPreISelOpcode(QueryType Type = IgnoreBundle) const { 784 return hasProperty(MCID::PreISelOpcode, Type); 785 } 786 787 /// Return true if this instruction can have a variable number of operands. 788 /// In this case, the variable operands will be after the normal 789 /// operands but before the implicit definitions and uses (if any are 790 /// present). 791 bool isVariadic(QueryType Type = IgnoreBundle) const { 792 return hasProperty(MCID::Variadic, Type); 793 } 794 795 /// Set if this instruction has an optional definition, e.g. 796 /// ARM instructions which can set condition code if 's' bit is set. 797 bool hasOptionalDef(QueryType Type = IgnoreBundle) const { 798 return hasProperty(MCID::HasOptionalDef, Type); 799 } 800 801 /// Return true if this is a pseudo instruction that doesn't 802 /// correspond to a real machine instruction. 803 bool isPseudo(QueryType Type = IgnoreBundle) const { 804 return hasProperty(MCID::Pseudo, Type); 805 } 806 807 bool isReturn(QueryType Type = AnyInBundle) const { 808 return hasProperty(MCID::Return, Type); 809 } 810 811 /// Return true if this is an instruction that marks the end of an EH scope, 812 /// i.e., a catchpad or a cleanuppad instruction. 813 bool isEHScopeReturn(QueryType Type = AnyInBundle) const { 814 return hasProperty(MCID::EHScopeReturn, Type); 815 } 816 817 bool isCall(QueryType Type = AnyInBundle) const { 818 return hasProperty(MCID::Call, Type); 819 } 820 821 /// Return true if this is a call instruction that may have an associated 822 /// call site entry in the debug info. 823 bool isCandidateForCallSiteEntry(QueryType Type = IgnoreBundle) const; 824 /// Return true if copying, moving, or erasing this instruction requires 825 /// updating Call Site Info (see \ref copyCallSiteInfo, \ref moveCallSiteInfo, 826 /// \ref eraseCallSiteInfo). 827 bool shouldUpdateCallSiteInfo() const; 828 829 /// Returns true if the specified instruction stops control flow 830 /// from executing the instruction immediately following it. Examples include 831 /// unconditional branches and return instructions. 832 bool isBarrier(QueryType Type = AnyInBundle) const { 833 return hasProperty(MCID::Barrier, Type); 834 } 835 836 /// Returns true if this instruction part of the terminator for a basic block. 837 /// Typically this is things like return and branch instructions. 838 /// 839 /// Various passes use this to insert code into the bottom of a basic block, 840 /// but before control flow occurs. 841 bool isTerminator(QueryType Type = AnyInBundle) const { 842 return hasProperty(MCID::Terminator, Type); 843 } 844 845 /// Returns true if this is a conditional, unconditional, or indirect branch. 846 /// Predicates below can be used to discriminate between 847 /// these cases, and the TargetInstrInfo::analyzeBranch method can be used to 848 /// get more information. 849 bool isBranch(QueryType Type = AnyInBundle) const { 850 return hasProperty(MCID::Branch, Type); 851 } 852 853 /// Return true if this is an indirect branch, such as a 854 /// branch through a register. 855 bool isIndirectBranch(QueryType Type = AnyInBundle) const { 856 return hasProperty(MCID::IndirectBranch, Type); 857 } 858 859 /// Return true if this is a branch which may fall 860 /// through to the next instruction or may transfer control flow to some other 861 /// block. The TargetInstrInfo::analyzeBranch method can be used to get more 862 /// information about this branch. 863 bool isConditionalBranch(QueryType Type = AnyInBundle) const { 864 return isBranch(Type) && !isBarrier(Type) && !isIndirectBranch(Type); 865 } 866 867 /// Return true if this is a branch which always 868 /// transfers control flow to some other block. The 869 /// TargetInstrInfo::analyzeBranch method can be used to get more information 870 /// about this branch. 871 bool isUnconditionalBranch(QueryType Type = AnyInBundle) const { 872 return isBranch(Type) && isBarrier(Type) && !isIndirectBranch(Type); 873 } 874 875 /// Return true if this instruction has a predicate operand that 876 /// controls execution. It may be set to 'always', or may be set to other 877 /// values. There are various methods in TargetInstrInfo that can be used to 878 /// control and modify the predicate in this instruction. 879 bool isPredicable(QueryType Type = AllInBundle) const { 880 // If it's a bundle than all bundled instructions must be predicable for this 881 // to return true. 882 return hasProperty(MCID::Predicable, Type); 883 } 884 885 /// Return true if this instruction is a comparison. 886 bool isCompare(QueryType Type = IgnoreBundle) const { 887 return hasProperty(MCID::Compare, Type); 888 } 889 890 /// Return true if this instruction is a move immediate 891 /// (including conditional moves) instruction. 892 bool isMoveImmediate(QueryType Type = IgnoreBundle) const { 893 return hasProperty(MCID::MoveImm, Type); 894 } 895 896 /// Return true if this instruction is a register move. 897 /// (including moving values from subreg to reg) 898 bool isMoveReg(QueryType Type = IgnoreBundle) const { 899 return hasProperty(MCID::MoveReg, Type); 900 } 901 902 /// Return true if this instruction is a bitcast instruction. 903 bool isBitcast(QueryType Type = IgnoreBundle) const { 904 return hasProperty(MCID::Bitcast, Type); 905 } 906 907 /// Return true if this instruction is a select instruction. 908 bool isSelect(QueryType Type = IgnoreBundle) const { 909 return hasProperty(MCID::Select, Type); 910 } 911 912 /// Return true if this instruction cannot be safely duplicated. 913 /// For example, if the instruction has a unique labels attached 914 /// to it, duplicating it would cause multiple definition errors. 915 bool isNotDuplicable(QueryType Type = AnyInBundle) const { 916 return hasProperty(MCID::NotDuplicable, Type); 917 } 918 919 /// Return true if this instruction is convergent. 920 /// Convergent instructions can not be made control-dependent on any 921 /// additional values. 922 bool isConvergent(QueryType Type = AnyInBundle) const { 923 if (isInlineAsm()) { 924 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 925 if (ExtraInfo & InlineAsm::Extra_IsConvergent) 926 return true; 927 } 928 return hasProperty(MCID::Convergent, Type); 929 } 930 931 /// Returns true if the specified instruction has a delay slot 932 /// which must be filled by the code generator. 933 bool hasDelaySlot(QueryType Type = AnyInBundle) const { 934 return hasProperty(MCID::DelaySlot, Type); 935 } 936 937 /// Return true for instructions that can be folded as 938 /// memory operands in other instructions. The most common use for this 939 /// is instructions that are simple loads from memory that don't modify 940 /// the loaded value in any way, but it can also be used for instructions 941 /// that can be expressed as constant-pool loads, such as V_SETALLONES 942 /// on x86, to allow them to be folded when it is beneficial. 943 /// This should only be set on instructions that return a value in their 944 /// only virtual register definition. 945 bool canFoldAsLoad(QueryType Type = IgnoreBundle) const { 946 return hasProperty(MCID::FoldableAsLoad, Type); 947 } 948 949 /// Return true if this instruction behaves 950 /// the same way as the generic REG_SEQUENCE instructions. 951 /// E.g., on ARM, 952 /// dX VMOVDRR rY, rZ 953 /// is equivalent to 954 /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1. 955 /// 956 /// Note that for the optimizers to be able to take advantage of 957 /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be 958 /// override accordingly. 959 bool isRegSequenceLike(QueryType Type = IgnoreBundle) const { 960 return hasProperty(MCID::RegSequence, Type); 961 } 962 963 /// Return true if this instruction behaves 964 /// the same way as the generic EXTRACT_SUBREG instructions. 965 /// E.g., on ARM, 966 /// rX, rY VMOVRRD dZ 967 /// is equivalent to two EXTRACT_SUBREG: 968 /// rX = EXTRACT_SUBREG dZ, ssub_0 969 /// rY = EXTRACT_SUBREG dZ, ssub_1 970 /// 971 /// Note that for the optimizers to be able to take advantage of 972 /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be 973 /// override accordingly. 974 bool isExtractSubregLike(QueryType Type = IgnoreBundle) const { 975 return hasProperty(MCID::ExtractSubreg, Type); 976 } 977 978 /// Return true if this instruction behaves 979 /// the same way as the generic INSERT_SUBREG instructions. 980 /// E.g., on ARM, 981 /// dX = VSETLNi32 dY, rZ, Imm 982 /// is equivalent to a INSERT_SUBREG: 983 /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm) 984 /// 985 /// Note that for the optimizers to be able to take advantage of 986 /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be 987 /// override accordingly. 988 bool isInsertSubregLike(QueryType Type = IgnoreBundle) const { 989 return hasProperty(MCID::InsertSubreg, Type); 990 } 991 992 //===--------------------------------------------------------------------===// 993 // Side Effect Analysis 994 //===--------------------------------------------------------------------===// 995 996 /// Return true if this instruction could possibly read memory. 997 /// Instructions with this flag set are not necessarily simple load 998 /// instructions, they may load a value and modify it, for example. 999 bool mayLoad(QueryType Type = AnyInBundle) const { 1000 if (isInlineAsm()) { 1001 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1002 if (ExtraInfo & InlineAsm::Extra_MayLoad) 1003 return true; 1004 } 1005 return hasProperty(MCID::MayLoad, Type); 1006 } 1007 1008 /// Return true if this instruction could possibly modify memory. 1009 /// Instructions with this flag set are not necessarily simple store 1010 /// instructions, they may store a modified value based on their operands, or 1011 /// may not actually modify anything, for example. 1012 bool mayStore(QueryType Type = AnyInBundle) const { 1013 if (isInlineAsm()) { 1014 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1015 if (ExtraInfo & InlineAsm::Extra_MayStore) 1016 return true; 1017 } 1018 return hasProperty(MCID::MayStore, Type); 1019 } 1020 1021 /// Return true if this instruction could possibly read or modify memory. 1022 bool mayLoadOrStore(QueryType Type = AnyInBundle) const { 1023 return mayLoad(Type) || mayStore(Type); 1024 } 1025 1026 /// Return true if this instruction could possibly raise a floating-point 1027 /// exception. This is the case if the instruction is a floating-point 1028 /// instruction that can in principle raise an exception, as indicated 1029 /// by the MCID::MayRaiseFPException property, *and* at the same time, 1030 /// the instruction is used in a context where we expect floating-point 1031 /// exceptions are not disabled, as indicated by the NoFPExcept MI flag. 1032 bool mayRaiseFPException() const { 1033 return hasProperty(MCID::MayRaiseFPException) && 1034 !getFlag(MachineInstr::MIFlag::NoFPExcept); 1035 } 1036 1037 //===--------------------------------------------------------------------===// 1038 // Flags that indicate whether an instruction can be modified by a method. 1039 //===--------------------------------------------------------------------===// 1040 1041 /// Return true if this may be a 2- or 3-address 1042 /// instruction (of the form "X = op Y, Z, ..."), which produces the same 1043 /// result if Y and Z are exchanged. If this flag is set, then the 1044 /// TargetInstrInfo::commuteInstruction method may be used to hack on the 1045 /// instruction. 1046 /// 1047 /// Note that this flag may be set on instructions that are only commutable 1048 /// sometimes. In these cases, the call to commuteInstruction will fail. 1049 /// Also note that some instructions require non-trivial modification to 1050 /// commute them. 1051 bool isCommutable(QueryType Type = IgnoreBundle) const { 1052 return hasProperty(MCID::Commutable, Type); 1053 } 1054 1055 /// Return true if this is a 2-address instruction 1056 /// which can be changed into a 3-address instruction if needed. Doing this 1057 /// transformation can be profitable in the register allocator, because it 1058 /// means that the instruction can use a 2-address form if possible, but 1059 /// degrade into a less efficient form if the source and dest register cannot 1060 /// be assigned to the same register. For example, this allows the x86 1061 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which 1062 /// is the same speed as the shift but has bigger code size. 1063 /// 1064 /// If this returns true, then the target must implement the 1065 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which 1066 /// is allowed to fail if the transformation isn't valid for this specific 1067 /// instruction (e.g. shl reg, 4 on x86). 1068 /// 1069 bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const { 1070 return hasProperty(MCID::ConvertibleTo3Addr, Type); 1071 } 1072 1073 /// Return true if this instruction requires 1074 /// custom insertion support when the DAG scheduler is inserting it into a 1075 /// machine basic block. If this is true for the instruction, it basically 1076 /// means that it is a pseudo instruction used at SelectionDAG time that is 1077 /// expanded out into magic code by the target when MachineInstrs are formed. 1078 /// 1079 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method 1080 /// is used to insert this into the MachineBasicBlock. 1081 bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const { 1082 return hasProperty(MCID::UsesCustomInserter, Type); 1083 } 1084 1085 /// Return true if this instruction requires *adjustment* 1086 /// after instruction selection by calling a target hook. For example, this 1087 /// can be used to fill in ARM 's' optional operand depending on whether 1088 /// the conditional flag register is used. 1089 bool hasPostISelHook(QueryType Type = IgnoreBundle) const { 1090 return hasProperty(MCID::HasPostISelHook, Type); 1091 } 1092 1093 /// Returns true if this instruction is a candidate for remat. 1094 /// This flag is deprecated, please don't use it anymore. If this 1095 /// flag is set, the isReallyTriviallyReMaterializable() method is called to 1096 /// verify the instruction is really rematable. 1097 bool isRematerializable(QueryType Type = AllInBundle) const { 1098 // It's only possible to re-mat a bundle if all bundled instructions are 1099 // re-materializable. 1100 return hasProperty(MCID::Rematerializable, Type); 1101 } 1102 1103 /// Returns true if this instruction has the same cost (or less) than a move 1104 /// instruction. This is useful during certain types of optimizations 1105 /// (e.g., remat during two-address conversion or machine licm) 1106 /// where we would like to remat or hoist the instruction, but not if it costs 1107 /// more than moving the instruction into the appropriate register. Note, we 1108 /// are not marking copies from and to the same register class with this flag. 1109 bool isAsCheapAsAMove(QueryType Type = AllInBundle) const { 1110 // Only returns true for a bundle if all bundled instructions are cheap. 1111 return hasProperty(MCID::CheapAsAMove, Type); 1112 } 1113 1114 /// Returns true if this instruction source operands 1115 /// have special register allocation requirements that are not captured by the 1116 /// operand register classes. e.g. ARM::STRD's two source registers must be an 1117 /// even / odd pair, ARM::STM registers have to be in ascending order. 1118 /// Post-register allocation passes should not attempt to change allocations 1119 /// for sources of instructions with this flag. 1120 bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const { 1121 return hasProperty(MCID::ExtraSrcRegAllocReq, Type); 1122 } 1123 1124 /// Returns true if this instruction def operands 1125 /// have special register allocation requirements that are not captured by the 1126 /// operand register classes. e.g. ARM::LDRD's two def registers must be an 1127 /// even / odd pair, ARM::LDM registers have to be in ascending order. 1128 /// Post-register allocation passes should not attempt to change allocations 1129 /// for definitions of instructions with this flag. 1130 bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const { 1131 return hasProperty(MCID::ExtraDefRegAllocReq, Type); 1132 } 1133 1134 enum MICheckType { 1135 CheckDefs, // Check all operands for equality 1136 CheckKillDead, // Check all operands including kill / dead markers 1137 IgnoreDefs, // Ignore all definitions 1138 IgnoreVRegDefs // Ignore virtual register definitions 1139 }; 1140 1141 /// Return true if this instruction is identical to \p Other. 1142 /// Two instructions are identical if they have the same opcode and all their 1143 /// operands are identical (with respect to MachineOperand::isIdenticalTo()). 1144 /// Note that this means liveness related flags (dead, undef, kill) do not 1145 /// affect the notion of identical. 1146 bool isIdenticalTo(const MachineInstr &Other, 1147 MICheckType Check = CheckDefs) const; 1148 1149 /// Unlink 'this' from the containing basic block, and return it without 1150 /// deleting it. 1151 /// 1152 /// This function can not be used on bundled instructions, use 1153 /// removeFromBundle() to remove individual instructions from a bundle. 1154 MachineInstr *removeFromParent(); 1155 1156 /// Unlink this instruction from its basic block and return it without 1157 /// deleting it. 1158 /// 1159 /// If the instruction is part of a bundle, the other instructions in the 1160 /// bundle remain bundled. 1161 MachineInstr *removeFromBundle(); 1162 1163 /// Unlink 'this' from the containing basic block and delete it. 1164 /// 1165 /// If this instruction is the header of a bundle, the whole bundle is erased. 1166 /// This function can not be used for instructions inside a bundle, use 1167 /// eraseFromBundle() to erase individual bundled instructions. 1168 void eraseFromParent(); 1169 1170 /// Unlink 'this' from the containing basic block and delete it. 1171 /// 1172 /// For all definitions mark their uses in DBG_VALUE nodes 1173 /// as undefined. Otherwise like eraseFromParent(). 1174 void eraseFromParentAndMarkDBGValuesForRemoval(); 1175 1176 /// Unlink 'this' form its basic block and delete it. 1177 /// 1178 /// If the instruction is part of a bundle, the other instructions in the 1179 /// bundle remain bundled. 1180 void eraseFromBundle(); 1181 1182 bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; } 1183 bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; } 1184 bool isAnnotationLabel() const { 1185 return getOpcode() == TargetOpcode::ANNOTATION_LABEL; 1186 } 1187 1188 /// Returns true if the MachineInstr represents a label. 1189 bool isLabel() const { 1190 return isEHLabel() || isGCLabel() || isAnnotationLabel(); 1191 } 1192 1193 bool isCFIInstruction() const { 1194 return getOpcode() == TargetOpcode::CFI_INSTRUCTION; 1195 } 1196 1197 bool isPseudoProbe() const { 1198 return getOpcode() == TargetOpcode::PSEUDO_PROBE; 1199 } 1200 1201 // True if the instruction represents a position in the function. 1202 bool isPosition() const { return isLabel() || isCFIInstruction(); } 1203 1204 bool isNonListDebugValue() const { 1205 return getOpcode() == TargetOpcode::DBG_VALUE; 1206 } 1207 bool isDebugValueList() const { 1208 return getOpcode() == TargetOpcode::DBG_VALUE_LIST; 1209 } 1210 bool isDebugValue() const { 1211 return isNonListDebugValue() || isDebugValueList(); 1212 } 1213 bool isDebugLabel() const { return getOpcode() == TargetOpcode::DBG_LABEL; } 1214 bool isDebugRef() const { return getOpcode() == TargetOpcode::DBG_INSTR_REF; } 1215 bool isDebugPHI() const { return getOpcode() == TargetOpcode::DBG_PHI; } 1216 bool isDebugInstr() const { 1217 return isDebugValue() || isDebugLabel() || isDebugRef() || isDebugPHI(); 1218 } 1219 bool isDebugOrPseudoInstr() const { 1220 return isDebugInstr() || isPseudoProbe(); 1221 } 1222 1223 bool isDebugOffsetImm() const { 1224 return isNonListDebugValue() && getDebugOffset().isImm(); 1225 } 1226 1227 /// A DBG_VALUE is indirect iff the location operand is a register and 1228 /// the offset operand is an immediate. 1229 bool isIndirectDebugValue() const { 1230 return isDebugOffsetImm() && getDebugOperand(0).isReg(); 1231 } 1232 1233 /// A DBG_VALUE is an entry value iff its debug expression contains the 1234 /// DW_OP_LLVM_entry_value operation. 1235 bool isDebugEntryValue() const; 1236 1237 /// Return true if the instruction is a debug value which describes a part of 1238 /// a variable as unavailable. 1239 bool isUndefDebugValue() const { 1240 if (!isDebugValue()) 1241 return false; 1242 // If any $noreg locations are given, this DV is undef. 1243 for (const MachineOperand &Op : debug_operands()) 1244 if (Op.isReg() && !Op.getReg().isValid()) 1245 return true; 1246 return false; 1247 } 1248 1249 bool isPHI() const { 1250 return getOpcode() == TargetOpcode::PHI || 1251 getOpcode() == TargetOpcode::G_PHI; 1252 } 1253 bool isKill() const { return getOpcode() == TargetOpcode::KILL; } 1254 bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; } 1255 bool isInlineAsm() const { 1256 return getOpcode() == TargetOpcode::INLINEASM || 1257 getOpcode() == TargetOpcode::INLINEASM_BR; 1258 } 1259 1260 /// FIXME: Seems like a layering violation that the AsmDialect, which is X86 1261 /// specific, be attached to a generic MachineInstr. 1262 bool isMSInlineAsm() const { 1263 return isInlineAsm() && getInlineAsmDialect() == InlineAsm::AD_Intel; 1264 } 1265 1266 bool isStackAligningInlineAsm() const; 1267 InlineAsm::AsmDialect getInlineAsmDialect() const; 1268 1269 bool isInsertSubreg() const { 1270 return getOpcode() == TargetOpcode::INSERT_SUBREG; 1271 } 1272 1273 bool isSubregToReg() const { 1274 return getOpcode() == TargetOpcode::SUBREG_TO_REG; 1275 } 1276 1277 bool isRegSequence() const { 1278 return getOpcode() == TargetOpcode::REG_SEQUENCE; 1279 } 1280 1281 bool isBundle() const { 1282 return getOpcode() == TargetOpcode::BUNDLE; 1283 } 1284 1285 bool isCopy() const { 1286 return getOpcode() == TargetOpcode::COPY; 1287 } 1288 1289 bool isFullCopy() const { 1290 return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg(); 1291 } 1292 1293 bool isExtractSubreg() const { 1294 return getOpcode() == TargetOpcode::EXTRACT_SUBREG; 1295 } 1296 1297 /// Return true if the instruction behaves like a copy. 1298 /// This does not include native copy instructions. 1299 bool isCopyLike() const { 1300 return isCopy() || isSubregToReg(); 1301 } 1302 1303 /// Return true is the instruction is an identity copy. 1304 bool isIdentityCopy() const { 1305 return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() && 1306 getOperand(0).getSubReg() == getOperand(1).getSubReg(); 1307 } 1308 1309 /// Return true if this instruction doesn't produce any output in the form of 1310 /// executable instructions. 1311 bool isMetaInstruction() const { 1312 switch (getOpcode()) { 1313 default: 1314 return false; 1315 case TargetOpcode::IMPLICIT_DEF: 1316 case TargetOpcode::KILL: 1317 case TargetOpcode::CFI_INSTRUCTION: 1318 case TargetOpcode::EH_LABEL: 1319 case TargetOpcode::GC_LABEL: 1320 case TargetOpcode::DBG_VALUE: 1321 case TargetOpcode::DBG_VALUE_LIST: 1322 case TargetOpcode::DBG_INSTR_REF: 1323 case TargetOpcode::DBG_PHI: 1324 case TargetOpcode::DBG_LABEL: 1325 case TargetOpcode::LIFETIME_START: 1326 case TargetOpcode::LIFETIME_END: 1327 case TargetOpcode::PSEUDO_PROBE: 1328 return true; 1329 } 1330 } 1331 1332 /// Return true if this is a transient instruction that is either very likely 1333 /// to be eliminated during register allocation (such as copy-like 1334 /// instructions), or if this instruction doesn't have an execution-time cost. 1335 bool isTransient() const { 1336 switch (getOpcode()) { 1337 default: 1338 return isMetaInstruction(); 1339 // Copy-like instructions are usually eliminated during register allocation. 1340 case TargetOpcode::PHI: 1341 case TargetOpcode::G_PHI: 1342 case TargetOpcode::COPY: 1343 case TargetOpcode::INSERT_SUBREG: 1344 case TargetOpcode::SUBREG_TO_REG: 1345 case TargetOpcode::REG_SEQUENCE: 1346 return true; 1347 } 1348 } 1349 1350 /// Return the number of instructions inside the MI bundle, excluding the 1351 /// bundle header. 1352 /// 1353 /// This is the number of instructions that MachineBasicBlock::iterator 1354 /// skips, 0 for unbundled instructions. 1355 unsigned getBundleSize() const; 1356 1357 /// Return true if the MachineInstr reads the specified register. 1358 /// If TargetRegisterInfo is passed, then it also checks if there 1359 /// is a read of a super-register. 1360 /// This does not count partial redefines of virtual registers as reads: 1361 /// %reg1024:6 = OP. 1362 bool readsRegister(Register Reg, 1363 const TargetRegisterInfo *TRI = nullptr) const { 1364 return findRegisterUseOperandIdx(Reg, false, TRI) != -1; 1365 } 1366 1367 /// Return true if the MachineInstr reads the specified virtual register. 1368 /// Take into account that a partial define is a 1369 /// read-modify-write operation. 1370 bool readsVirtualRegister(Register Reg) const { 1371 return readsWritesVirtualRegister(Reg).first; 1372 } 1373 1374 /// Return a pair of bools (reads, writes) indicating if this instruction 1375 /// reads or writes Reg. This also considers partial defines. 1376 /// If Ops is not null, all operand indices for Reg are added. 1377 std::pair<bool,bool> readsWritesVirtualRegister(Register Reg, 1378 SmallVectorImpl<unsigned> *Ops = nullptr) const; 1379 1380 /// Return true if the MachineInstr kills the specified register. 1381 /// If TargetRegisterInfo is passed, then it also checks if there is 1382 /// a kill of a super-register. 1383 bool killsRegister(Register Reg, 1384 const TargetRegisterInfo *TRI = nullptr) const { 1385 return findRegisterUseOperandIdx(Reg, true, TRI) != -1; 1386 } 1387 1388 /// Return true if the MachineInstr fully defines the specified register. 1389 /// If TargetRegisterInfo is passed, then it also checks 1390 /// if there is a def of a super-register. 1391 /// NOTE: It's ignoring subreg indices on virtual registers. 1392 bool definesRegister(Register Reg, 1393 const TargetRegisterInfo *TRI = nullptr) const { 1394 return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1; 1395 } 1396 1397 /// Return true if the MachineInstr modifies (fully define or partially 1398 /// define) the specified register. 1399 /// NOTE: It's ignoring subreg indices on virtual registers. 1400 bool modifiesRegister(Register Reg, 1401 const TargetRegisterInfo *TRI = nullptr) const { 1402 return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1; 1403 } 1404 1405 /// Returns true if the register is dead in this machine instruction. 1406 /// If TargetRegisterInfo is passed, then it also checks 1407 /// if there is a dead def of a super-register. 1408 bool registerDefIsDead(Register Reg, 1409 const TargetRegisterInfo *TRI = nullptr) const { 1410 return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1; 1411 } 1412 1413 /// Returns true if the MachineInstr has an implicit-use operand of exactly 1414 /// the given register (not considering sub/super-registers). 1415 bool hasRegisterImplicitUseOperand(Register Reg) const; 1416 1417 /// Returns the operand index that is a use of the specific register or -1 1418 /// if it is not found. It further tightens the search criteria to a use 1419 /// that kills the register if isKill is true. 1420 int findRegisterUseOperandIdx(Register Reg, bool isKill = false, 1421 const TargetRegisterInfo *TRI = nullptr) const; 1422 1423 /// Wrapper for findRegisterUseOperandIdx, it returns 1424 /// a pointer to the MachineOperand rather than an index. 1425 MachineOperand *findRegisterUseOperand(Register Reg, bool isKill = false, 1426 const TargetRegisterInfo *TRI = nullptr) { 1427 int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI); 1428 return (Idx == -1) ? nullptr : &getOperand(Idx); 1429 } 1430 1431 const MachineOperand *findRegisterUseOperand( 1432 Register Reg, bool isKill = false, 1433 const TargetRegisterInfo *TRI = nullptr) const { 1434 return const_cast<MachineInstr *>(this)-> 1435 findRegisterUseOperand(Reg, isKill, TRI); 1436 } 1437 1438 /// Returns the operand index that is a def of the specified register or 1439 /// -1 if it is not found. If isDead is true, defs that are not dead are 1440 /// skipped. If Overlap is true, then it also looks for defs that merely 1441 /// overlap the specified register. If TargetRegisterInfo is non-null, 1442 /// then it also checks if there is a def of a super-register. 1443 /// This may also return a register mask operand when Overlap is true. 1444 int findRegisterDefOperandIdx(Register Reg, 1445 bool isDead = false, bool Overlap = false, 1446 const TargetRegisterInfo *TRI = nullptr) const; 1447 1448 /// Wrapper for findRegisterDefOperandIdx, it returns 1449 /// a pointer to the MachineOperand rather than an index. 1450 MachineOperand * 1451 findRegisterDefOperand(Register Reg, bool isDead = false, 1452 bool Overlap = false, 1453 const TargetRegisterInfo *TRI = nullptr) { 1454 int Idx = findRegisterDefOperandIdx(Reg, isDead, Overlap, TRI); 1455 return (Idx == -1) ? nullptr : &getOperand(Idx); 1456 } 1457 1458 const MachineOperand * 1459 findRegisterDefOperand(Register Reg, bool isDead = false, 1460 bool Overlap = false, 1461 const TargetRegisterInfo *TRI = nullptr) const { 1462 return const_cast<MachineInstr *>(this)->findRegisterDefOperand( 1463 Reg, isDead, Overlap, TRI); 1464 } 1465 1466 /// Find the index of the first operand in the 1467 /// operand list that is used to represent the predicate. It returns -1 if 1468 /// none is found. 1469 int findFirstPredOperandIdx() const; 1470 1471 /// Find the index of the flag word operand that 1472 /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if 1473 /// getOperand(OpIdx) does not belong to an inline asm operand group. 1474 /// 1475 /// If GroupNo is not NULL, it will receive the number of the operand group 1476 /// containing OpIdx. 1477 int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = nullptr) const; 1478 1479 /// Compute the static register class constraint for operand OpIdx. 1480 /// For normal instructions, this is derived from the MCInstrDesc. 1481 /// For inline assembly it is derived from the flag words. 1482 /// 1483 /// Returns NULL if the static register class constraint cannot be 1484 /// determined. 1485 const TargetRegisterClass* 1486 getRegClassConstraint(unsigned OpIdx, 1487 const TargetInstrInfo *TII, 1488 const TargetRegisterInfo *TRI) const; 1489 1490 /// Applies the constraints (def/use) implied by this MI on \p Reg to 1491 /// the given \p CurRC. 1492 /// If \p ExploreBundle is set and MI is part of a bundle, all the 1493 /// instructions inside the bundle will be taken into account. In other words, 1494 /// this method accumulates all the constraints of the operand of this MI and 1495 /// the related bundle if MI is a bundle or inside a bundle. 1496 /// 1497 /// Returns the register class that satisfies both \p CurRC and the 1498 /// constraints set by MI. Returns NULL if such a register class does not 1499 /// exist. 1500 /// 1501 /// \pre CurRC must not be NULL. 1502 const TargetRegisterClass *getRegClassConstraintEffectForVReg( 1503 Register Reg, const TargetRegisterClass *CurRC, 1504 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, 1505 bool ExploreBundle = false) const; 1506 1507 /// Applies the constraints (def/use) implied by the \p OpIdx operand 1508 /// to the given \p CurRC. 1509 /// 1510 /// Returns the register class that satisfies both \p CurRC and the 1511 /// constraints set by \p OpIdx MI. Returns NULL if such a register class 1512 /// does not exist. 1513 /// 1514 /// \pre CurRC must not be NULL. 1515 /// \pre The operand at \p OpIdx must be a register. 1516 const TargetRegisterClass * 1517 getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC, 1518 const TargetInstrInfo *TII, 1519 const TargetRegisterInfo *TRI) const; 1520 1521 /// Add a tie between the register operands at DefIdx and UseIdx. 1522 /// The tie will cause the register allocator to ensure that the two 1523 /// operands are assigned the same physical register. 1524 /// 1525 /// Tied operands are managed automatically for explicit operands in the 1526 /// MCInstrDesc. This method is for exceptional cases like inline asm. 1527 void tieOperands(unsigned DefIdx, unsigned UseIdx); 1528 1529 /// Given the index of a tied register operand, find the 1530 /// operand it is tied to. Defs are tied to uses and vice versa. Returns the 1531 /// index of the tied operand which must exist. 1532 unsigned findTiedOperandIdx(unsigned OpIdx) const; 1533 1534 /// Given the index of a register def operand, 1535 /// check if the register def is tied to a source operand, due to either 1536 /// two-address elimination or inline assembly constraints. Returns the 1537 /// first tied use operand index by reference if UseOpIdx is not null. 1538 bool isRegTiedToUseOperand(unsigned DefOpIdx, 1539 unsigned *UseOpIdx = nullptr) const { 1540 const MachineOperand &MO = getOperand(DefOpIdx); 1541 if (!MO.isReg() || !MO.isDef() || !MO.isTied()) 1542 return false; 1543 if (UseOpIdx) 1544 *UseOpIdx = findTiedOperandIdx(DefOpIdx); 1545 return true; 1546 } 1547 1548 /// Return true if the use operand of the specified index is tied to a def 1549 /// operand. It also returns the def operand index by reference if DefOpIdx 1550 /// is not null. 1551 bool isRegTiedToDefOperand(unsigned UseOpIdx, 1552 unsigned *DefOpIdx = nullptr) const { 1553 const MachineOperand &MO = getOperand(UseOpIdx); 1554 if (!MO.isReg() || !MO.isUse() || !MO.isTied()) 1555 return false; 1556 if (DefOpIdx) 1557 *DefOpIdx = findTiedOperandIdx(UseOpIdx); 1558 return true; 1559 } 1560 1561 /// Clears kill flags on all operands. 1562 void clearKillInfo(); 1563 1564 /// Replace all occurrences of FromReg with ToReg:SubIdx, 1565 /// properly composing subreg indices where necessary. 1566 void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, 1567 const TargetRegisterInfo &RegInfo); 1568 1569 /// We have determined MI kills a register. Look for the 1570 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true, 1571 /// add a implicit operand if it's not found. Returns true if the operand 1572 /// exists / is added. 1573 bool addRegisterKilled(Register IncomingReg, 1574 const TargetRegisterInfo *RegInfo, 1575 bool AddIfNotFound = false); 1576 1577 /// Clear all kill flags affecting Reg. If RegInfo is provided, this includes 1578 /// all aliasing registers. 1579 void clearRegisterKills(Register Reg, const TargetRegisterInfo *RegInfo); 1580 1581 /// We have determined MI defined a register without a use. 1582 /// Look for the operand that defines it and mark it as IsDead. If 1583 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns 1584 /// true if the operand exists / is added. 1585 bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, 1586 bool AddIfNotFound = false); 1587 1588 /// Clear all dead flags on operands defining register @p Reg. 1589 void clearRegisterDeads(Register Reg); 1590 1591 /// Mark all subregister defs of register @p Reg with the undef flag. 1592 /// This function is used when we determined to have a subregister def in an 1593 /// otherwise undefined super register. 1594 void setRegisterDefReadUndef(Register Reg, bool IsUndef = true); 1595 1596 /// We have determined MI defines a register. Make sure there is an operand 1597 /// defining Reg. 1598 void addRegisterDefined(Register Reg, 1599 const TargetRegisterInfo *RegInfo = nullptr); 1600 1601 /// Mark every physreg used by this instruction as 1602 /// dead except those in the UsedRegs list. 1603 /// 1604 /// On instructions with register mask operands, also add implicit-def 1605 /// operands for all registers in UsedRegs. 1606 void setPhysRegsDeadExcept(ArrayRef<Register> UsedRegs, 1607 const TargetRegisterInfo &TRI); 1608 1609 /// Return true if it is safe to move this instruction. If 1610 /// SawStore is set to true, it means that there is a store (or call) between 1611 /// the instruction's location and its intended destination. 1612 bool isSafeToMove(AAResults *AA, bool &SawStore) const; 1613 1614 /// Returns true if this instruction's memory access aliases the memory 1615 /// access of Other. 1616 // 1617 /// Assumes any physical registers used to compute addresses 1618 /// have the same value for both instructions. Returns false if neither 1619 /// instruction writes to memory. 1620 /// 1621 /// @param AA Optional alias analysis, used to compare memory operands. 1622 /// @param Other MachineInstr to check aliasing against. 1623 /// @param UseTBAA Whether to pass TBAA information to alias analysis. 1624 bool mayAlias(AAResults *AA, const MachineInstr &Other, bool UseTBAA) const; 1625 1626 /// Return true if this instruction may have an ordered 1627 /// or volatile memory reference, or if the information describing the memory 1628 /// reference is not available. Return false if it is known to have no 1629 /// ordered or volatile memory references. 1630 bool hasOrderedMemoryRef() const; 1631 1632 /// Return true if this load instruction never traps and points to a memory 1633 /// location whose value doesn't change during the execution of this function. 1634 /// 1635 /// Examples include loading a value from the constant pool or from the 1636 /// argument area of a function (if it does not change). If the instruction 1637 /// does multiple loads, this returns true only if all of the loads are 1638 /// dereferenceable and invariant. 1639 bool isDereferenceableInvariantLoad(AAResults *AA) const; 1640 1641 /// If the specified instruction is a PHI that always merges together the 1642 /// same virtual register, return the register, otherwise return 0. 1643 unsigned isConstantValuePHI() const; 1644 1645 /// Return true if this instruction has side effects that are not modeled 1646 /// by mayLoad / mayStore, etc. 1647 /// For all instructions, the property is encoded in MCInstrDesc::Flags 1648 /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is 1649 /// INLINEASM instruction, in which case the side effect property is encoded 1650 /// in one of its operands (see InlineAsm::Extra_HasSideEffect). 1651 /// 1652 bool hasUnmodeledSideEffects() const; 1653 1654 /// Returns true if it is illegal to fold a load across this instruction. 1655 bool isLoadFoldBarrier() const; 1656 1657 /// Return true if all the defs of this instruction are dead. 1658 bool allDefsAreDead() const; 1659 1660 /// Return a valid size if the instruction is a spill instruction. 1661 Optional<unsigned> getSpillSize(const TargetInstrInfo *TII) const; 1662 1663 /// Return a valid size if the instruction is a folded spill instruction. 1664 Optional<unsigned> getFoldedSpillSize(const TargetInstrInfo *TII) const; 1665 1666 /// Return a valid size if the instruction is a restore instruction. 1667 Optional<unsigned> getRestoreSize(const TargetInstrInfo *TII) const; 1668 1669 /// Return a valid size if the instruction is a folded restore instruction. 1670 Optional<unsigned> 1671 getFoldedRestoreSize(const TargetInstrInfo *TII) const; 1672 1673 /// Copy implicit register operands from specified 1674 /// instruction to this instruction. 1675 void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI); 1676 1677 /// Debugging support 1678 /// @{ 1679 /// Determine the generic type to be printed (if needed) on uses and defs. 1680 LLT getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes, 1681 const MachineRegisterInfo &MRI) const; 1682 1683 /// Return true when an instruction has tied register that can't be determined 1684 /// by the instruction's descriptor. This is useful for MIR printing, to 1685 /// determine whether we need to print the ties or not. 1686 bool hasComplexRegisterTies() const; 1687 1688 /// Print this MI to \p OS. 1689 /// Don't print information that can be inferred from other instructions if 1690 /// \p IsStandalone is false. It is usually true when only a fragment of the 1691 /// function is printed. 1692 /// Only print the defs and the opcode if \p SkipOpers is true. 1693 /// Otherwise, also print operands if \p SkipDebugLoc is true. 1694 /// Otherwise, also print the debug loc, with a terminating newline. 1695 /// \p TII is used to print the opcode name. If it's not present, but the 1696 /// MI is in a function, the opcode will be printed using the function's TII. 1697 void print(raw_ostream &OS, bool IsStandalone = true, bool SkipOpers = false, 1698 bool SkipDebugLoc = false, bool AddNewLine = true, 1699 const TargetInstrInfo *TII = nullptr) const; 1700 void print(raw_ostream &OS, ModuleSlotTracker &MST, bool IsStandalone = true, 1701 bool SkipOpers = false, bool SkipDebugLoc = false, 1702 bool AddNewLine = true, 1703 const TargetInstrInfo *TII = nullptr) const; 1704 void dump() const; 1705 /// Print on dbgs() the current instruction and the instructions defining its 1706 /// operands and so on until we reach \p MaxDepth. 1707 void dumpr(const MachineRegisterInfo &MRI, 1708 unsigned MaxDepth = UINT_MAX) const; 1709 /// @} 1710 1711 //===--------------------------------------------------------------------===// 1712 // Accessors used to build up machine instructions. 1713 1714 /// Add the specified operand to the instruction. If it is an implicit 1715 /// operand, it is added to the end of the operand list. If it is an 1716 /// explicit operand it is added at the end of the explicit operand list 1717 /// (before the first implicit operand). 1718 /// 1719 /// MF must be the machine function that was used to allocate this 1720 /// instruction. 1721 /// 1722 /// MachineInstrBuilder provides a more convenient interface for creating 1723 /// instructions and adding operands. 1724 void addOperand(MachineFunction &MF, const MachineOperand &Op); 1725 1726 /// Add an operand without providing an MF reference. This only works for 1727 /// instructions that are inserted in a basic block. 1728 /// 1729 /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be 1730 /// preferred. 1731 void addOperand(const MachineOperand &Op); 1732 1733 /// Replace the instruction descriptor (thus opcode) of 1734 /// the current instruction with a new one. 1735 void setDesc(const MCInstrDesc &tid) { MCID = &tid; } 1736 1737 /// Replace current source information with new such. 1738 /// Avoid using this, the constructor argument is preferable. 1739 void setDebugLoc(DebugLoc dl) { 1740 debugLoc = std::move(dl); 1741 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); 1742 } 1743 1744 /// Erase an operand from an instruction, leaving it with one 1745 /// fewer operand than it started with. 1746 void RemoveOperand(unsigned OpNo); 1747 1748 /// Clear this MachineInstr's memory reference descriptor list. This resets 1749 /// the memrefs to their most conservative state. This should be used only 1750 /// as a last resort since it greatly pessimizes our knowledge of the memory 1751 /// access performed by the instruction. 1752 void dropMemRefs(MachineFunction &MF); 1753 1754 /// Assign this MachineInstr's memory reference descriptor list. 1755 /// 1756 /// Unlike other methods, this *will* allocate them into a new array 1757 /// associated with the provided `MachineFunction`. 1758 void setMemRefs(MachineFunction &MF, ArrayRef<MachineMemOperand *> MemRefs); 1759 1760 /// Add a MachineMemOperand to the machine instruction. 1761 /// This function should be used only occasionally. The setMemRefs function 1762 /// is the primary method for setting up a MachineInstr's MemRefs list. 1763 void addMemOperand(MachineFunction &MF, MachineMemOperand *MO); 1764 1765 /// Clone another MachineInstr's memory reference descriptor list and replace 1766 /// ours with it. 1767 /// 1768 /// Note that `*this` may be the incoming MI! 1769 /// 1770 /// Prefer this API whenever possible as it can avoid allocations in common 1771 /// cases. 1772 void cloneMemRefs(MachineFunction &MF, const MachineInstr &MI); 1773 1774 /// Clone the merge of multiple MachineInstrs' memory reference descriptors 1775 /// list and replace ours with it. 1776 /// 1777 /// Note that `*this` may be one of the incoming MIs! 1778 /// 1779 /// Prefer this API whenever possible as it can avoid allocations in common 1780 /// cases. 1781 void cloneMergedMemRefs(MachineFunction &MF, 1782 ArrayRef<const MachineInstr *> MIs); 1783 1784 /// Set a symbol that will be emitted just prior to the instruction itself. 1785 /// 1786 /// Setting this to a null pointer will remove any such symbol. 1787 /// 1788 /// FIXME: This is not fully implemented yet. 1789 void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol); 1790 1791 /// Set a symbol that will be emitted just after the instruction itself. 1792 /// 1793 /// Setting this to a null pointer will remove any such symbol. 1794 /// 1795 /// FIXME: This is not fully implemented yet. 1796 void setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol); 1797 1798 /// Clone another MachineInstr's pre- and post- instruction symbols and 1799 /// replace ours with it. 1800 void cloneInstrSymbols(MachineFunction &MF, const MachineInstr &MI); 1801 1802 /// Set a marker on instructions that denotes where we should create and emit 1803 /// heap alloc site labels. This waits until after instruction selection and 1804 /// optimizations to create the label, so it should still work if the 1805 /// instruction is removed or duplicated. 1806 void setHeapAllocMarker(MachineFunction &MF, MDNode *MD); 1807 1808 /// Return the MIFlags which represent both MachineInstrs. This 1809 /// should be used when merging two MachineInstrs into one. This routine does 1810 /// not modify the MIFlags of this MachineInstr. 1811 uint16_t mergeFlagsWith(const MachineInstr& Other) const; 1812 1813 static uint16_t copyFlagsFromInstruction(const Instruction &I); 1814 1815 /// Copy all flags to MachineInst MIFlags 1816 void copyIRFlags(const Instruction &I); 1817 1818 /// Break any tie involving OpIdx. 1819 void untieRegOperand(unsigned OpIdx) { 1820 MachineOperand &MO = getOperand(OpIdx); 1821 if (MO.isReg() && MO.isTied()) { 1822 getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0; 1823 MO.TiedTo = 0; 1824 } 1825 } 1826 1827 /// Add all implicit def and use operands to this instruction. 1828 void addImplicitDefUseOperands(MachineFunction &MF); 1829 1830 /// Scan instructions immediately following MI and collect any matching 1831 /// DBG_VALUEs. 1832 void collectDebugValues(SmallVectorImpl<MachineInstr *> &DbgValues); 1833 1834 /// Find all DBG_VALUEs that point to the register def in this instruction 1835 /// and point them to \p Reg instead. 1836 void changeDebugValuesDefReg(Register Reg); 1837 1838 /// Returns the Intrinsic::ID for this instruction. 1839 /// \pre Must have an intrinsic ID operand. 1840 unsigned getIntrinsicID() const { 1841 return getOperand(getNumExplicitDefs()).getIntrinsicID(); 1842 } 1843 1844 /// Sets all register debug operands in this debug value instruction to be 1845 /// undef. 1846 void setDebugValueUndef() { 1847 assert(isDebugValue() && "Must be a debug value instruction."); 1848 for (MachineOperand &MO : debug_operands()) { 1849 if (MO.isReg()) { 1850 MO.setReg(0); 1851 MO.setSubReg(0); 1852 } 1853 } 1854 } 1855 1856 PseudoProbeAttributes getPseudoProbeAttribute() const { 1857 assert(isPseudoProbe() && "Must be a pseudo probe instruction"); 1858 return (PseudoProbeAttributes)getOperand(3).getImm(); 1859 } 1860 1861 void addPseudoProbeAttribute(PseudoProbeAttributes Attr) { 1862 assert(isPseudoProbe() && "Must be a pseudo probe instruction"); 1863 MachineOperand &AttrOperand = getOperand(3); 1864 AttrOperand.setImm(AttrOperand.getImm() | (uint32_t)Attr); 1865 } 1866 1867 private: 1868 /// If this instruction is embedded into a MachineFunction, return the 1869 /// MachineRegisterInfo object for the current function, otherwise 1870 /// return null. 1871 MachineRegisterInfo *getRegInfo(); 1872 1873 /// Unlink all of the register operands in this instruction from their 1874 /// respective use lists. This requires that the operands already be on their 1875 /// use lists. 1876 void RemoveRegOperandsFromUseLists(MachineRegisterInfo&); 1877 1878 /// Add all of the register operands in this instruction from their 1879 /// respective use lists. This requires that the operands not be on their 1880 /// use lists yet. 1881 void AddRegOperandsToUseLists(MachineRegisterInfo&); 1882 1883 /// Slow path for hasProperty when we're dealing with a bundle. 1884 bool hasPropertyInBundle(uint64_t Mask, QueryType Type) const; 1885 1886 /// Implements the logic of getRegClassConstraintEffectForVReg for the 1887 /// this MI and the given operand index \p OpIdx. 1888 /// If the related operand does not constrained Reg, this returns CurRC. 1889 const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl( 1890 unsigned OpIdx, Register Reg, const TargetRegisterClass *CurRC, 1891 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const; 1892 1893 /// Stores extra instruction information inline or allocates as ExtraInfo 1894 /// based on the number of pointers. 1895 void setExtraInfo(MachineFunction &MF, ArrayRef<MachineMemOperand *> MMOs, 1896 MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol, 1897 MDNode *HeapAllocMarker); 1898 }; 1899 1900 /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the 1901 /// instruction rather than by pointer value. 1902 /// The hashing and equality testing functions ignore definitions so this is 1903 /// useful for CSE, etc. 1904 struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> { 1905 static inline MachineInstr *getEmptyKey() { 1906 return nullptr; 1907 } 1908 1909 static inline MachineInstr *getTombstoneKey() { 1910 return reinterpret_cast<MachineInstr*>(-1); 1911 } 1912 1913 static unsigned getHashValue(const MachineInstr* const &MI); 1914 1915 static bool isEqual(const MachineInstr* const &LHS, 1916 const MachineInstr* const &RHS) { 1917 if (RHS == getEmptyKey() || RHS == getTombstoneKey() || 1918 LHS == getEmptyKey() || LHS == getTombstoneKey()) 1919 return LHS == RHS; 1920 return LHS->isIdenticalTo(*RHS, MachineInstr::IgnoreVRegDefs); 1921 } 1922 }; 1923 1924 //===----------------------------------------------------------------------===// 1925 // Debugging Support 1926 1927 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) { 1928 MI.print(OS); 1929 return OS; 1930 } 1931 1932 } // end namespace llvm 1933 1934 #endif // LLVM_CODEGEN_MACHINEINSTR_H 1935