1 //===- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the declaration of the MachineInstr class, which is the 11 // basic representation for all target dependent machine instructions used by 12 // the back end. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_CODEGEN_MACHINEINSTR_H 17 #define LLVM_CODEGEN_MACHINEINSTR_H 18 19 #include "llvm/ADT/DenseMapInfo.h" 20 #include "llvm/ADT/ilist.h" 21 #include "llvm/ADT/ilist_node.h" 22 #include "llvm/ADT/iterator_range.h" 23 #include "llvm/Analysis/AliasAnalysis.h" 24 #include "llvm/CodeGen/MachineOperand.h" 25 #include "llvm/CodeGen/TargetOpcodes.h" 26 #include "llvm/IR/DebugLoc.h" 27 #include "llvm/IR/InlineAsm.h" 28 #include "llvm/MC/MCInstrDesc.h" 29 #include "llvm/Support/ArrayRecycler.h" 30 #include <algorithm> 31 #include <cassert> 32 #include <cstdint> 33 #include <utility> 34 35 namespace llvm { 36 37 template <typename T> class ArrayRef; 38 class DIExpression; 39 class DILocalVariable; 40 class MachineBasicBlock; 41 class MachineFunction; 42 class MachineMemOperand; 43 class MachineRegisterInfo; 44 class ModuleSlotTracker; 45 class raw_ostream; 46 template <typename T> class SmallVectorImpl; 47 class SmallBitVector; 48 class StringRef; 49 class TargetInstrInfo; 50 class TargetRegisterClass; 51 class TargetRegisterInfo; 52 53 //===----------------------------------------------------------------------===// 54 /// Representation of each machine instruction. 55 /// 56 /// This class isn't a POD type, but it must have a trivial destructor. When a 57 /// MachineFunction is deleted, all the contained MachineInstrs are deallocated 58 /// without having their destructor called. 59 /// 60 class MachineInstr 61 : public ilist_node_with_parent<MachineInstr, MachineBasicBlock, 62 ilist_sentinel_tracking<true>> { 63 public: 64 using mmo_iterator = MachineMemOperand **; 65 66 /// Flags to specify different kinds of comments to output in 67 /// assembly code. These flags carry semantic information not 68 /// otherwise easily derivable from the IR text. 69 /// 70 enum CommentFlag { 71 ReloadReuse = 0x1, // higher bits are reserved for target dep comments. 72 NoSchedComment = 0x2, 73 TAsmComments = 0x4 // Target Asm comments should start from this value. 74 }; 75 76 enum MIFlag { 77 NoFlags = 0, 78 FrameSetup = 1 << 0, // Instruction is used as a part of 79 // function frame setup code. 80 FrameDestroy = 1 << 1, // Instruction is used as a part of 81 // function frame destruction code. 82 BundledPred = 1 << 2, // Instruction has bundled predecessors. 83 BundledSucc = 1 << 3, // Instruction has bundled successors. 84 FmNoNans = 1 << 4, // Instruction does not support Fast 85 // math nan values. 86 FmNoInfs = 1 << 5, // Instruction does not support Fast 87 // math infinity values. 88 FmNsz = 1 << 6, // Instruction is not required to retain 89 // signed zero values. 90 FmArcp = 1 << 7, // Instruction supports Fast math 91 // reciprocal approximations. 92 FmContract = 1 << 8, // Instruction supports Fast math 93 // contraction operations like fma. 94 FmAfn = 1 << 9, // Instruction may map to Fast math 95 // instrinsic approximation. 96 FmReassoc = 1 << 10 // Instruction supports Fast math 97 // reassociation of operand order. 98 }; 99 100 private: 101 const MCInstrDesc *MCID; // Instruction descriptor. 102 MachineBasicBlock *Parent = nullptr; // Pointer to the owning basic block. 103 104 // Operands are allocated by an ArrayRecycler. 105 MachineOperand *Operands = nullptr; // Pointer to the first operand. 106 unsigned NumOperands = 0; // Number of operands on instruction. 107 using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity; 108 OperandCapacity CapOperands; // Capacity of the Operands array. 109 110 uint16_t Flags = 0; // Various bits of additional 111 // information about machine 112 // instruction. 113 114 uint8_t AsmPrinterFlags = 0; // Various bits of information used by 115 // the AsmPrinter to emit helpful 116 // comments. This is *not* semantic 117 // information. Do not use this for 118 // anything other than to convey comment 119 // information to AsmPrinter. 120 121 uint8_t NumMemRefs = 0; // Information on memory references. 122 // Note that MemRefs == nullptr, means 'don't know', not 'no memory access'. 123 // Calling code must treat missing information conservatively. If the number 124 // of memory operands required to be precise exceeds the maximum value of 125 // NumMemRefs - currently 256 - we remove the operands entirely. Note also 126 // that this is a non-owning reference to a shared copy on write buffer owned 127 // by the MachineFunction and created via MF.allocateMemRefsArray. 128 mmo_iterator MemRefs = nullptr; 129 130 DebugLoc debugLoc; // Source line information. 131 132 // Intrusive list support 133 friend struct ilist_traits<MachineInstr>; 134 friend struct ilist_callback_traits<MachineBasicBlock>; 135 void setParent(MachineBasicBlock *P) { Parent = P; } 136 137 /// This constructor creates a copy of the given 138 /// MachineInstr in the given MachineFunction. 139 MachineInstr(MachineFunction &, const MachineInstr &); 140 141 /// This constructor create a MachineInstr and add the implicit operands. 142 /// It reserves space for number of operands specified by 143 /// MCInstrDesc. An explicit DebugLoc is supplied. 144 MachineInstr(MachineFunction &, const MCInstrDesc &MCID, DebugLoc dl, 145 bool NoImp = false); 146 147 // MachineInstrs are pool-allocated and owned by MachineFunction. 148 friend class MachineFunction; 149 150 public: 151 MachineInstr(const MachineInstr &) = delete; 152 MachineInstr &operator=(const MachineInstr &) = delete; 153 // Use MachineFunction::DeleteMachineInstr() instead. 154 ~MachineInstr() = delete; 155 156 const MachineBasicBlock* getParent() const { return Parent; } 157 MachineBasicBlock* getParent() { return Parent; } 158 159 /// Return the function that contains the basic block that this instruction 160 /// belongs to. 161 /// 162 /// Note: this is undefined behaviour if the instruction does not have a 163 /// parent. 164 const MachineFunction *getMF() const; 165 MachineFunction *getMF() { 166 return const_cast<MachineFunction *>( 167 static_cast<const MachineInstr *>(this)->getMF()); 168 } 169 170 /// Return the asm printer flags bitvector. 171 uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; } 172 173 /// Clear the AsmPrinter bitvector. 174 void clearAsmPrinterFlags() { AsmPrinterFlags = 0; } 175 176 /// Return whether an AsmPrinter flag is set. 177 bool getAsmPrinterFlag(CommentFlag Flag) const { 178 return AsmPrinterFlags & Flag; 179 } 180 181 /// Set a flag for the AsmPrinter. 182 void setAsmPrinterFlag(uint8_t Flag) { 183 AsmPrinterFlags |= Flag; 184 } 185 186 /// Clear specific AsmPrinter flags. 187 void clearAsmPrinterFlag(CommentFlag Flag) { 188 AsmPrinterFlags &= ~Flag; 189 } 190 191 /// Return the MI flags bitvector. 192 uint8_t getFlags() const { 193 return Flags; 194 } 195 196 /// Return whether an MI flag is set. 197 bool getFlag(MIFlag Flag) const { 198 return Flags & Flag; 199 } 200 201 /// Set a MI flag. 202 void setFlag(MIFlag Flag) { 203 Flags |= (uint16_t)Flag; 204 } 205 206 void setFlags(unsigned flags) { 207 // Filter out the automatically maintained flags. 208 unsigned Mask = BundledPred | BundledSucc; 209 Flags = (Flags & Mask) | (flags & ~Mask); 210 } 211 212 /// clearFlag - Clear a MI flag. 213 void clearFlag(MIFlag Flag) { 214 Flags &= ~((uint16_t)Flag); 215 } 216 217 /// Return true if MI is in a bundle (but not the first MI in a bundle). 218 /// 219 /// A bundle looks like this before it's finalized: 220 /// ---------------- 221 /// | MI | 222 /// ---------------- 223 /// | 224 /// ---------------- 225 /// | MI * | 226 /// ---------------- 227 /// | 228 /// ---------------- 229 /// | MI * | 230 /// ---------------- 231 /// In this case, the first MI starts a bundle but is not inside a bundle, the 232 /// next 2 MIs are considered "inside" the bundle. 233 /// 234 /// After a bundle is finalized, it looks like this: 235 /// ---------------- 236 /// | Bundle | 237 /// ---------------- 238 /// | 239 /// ---------------- 240 /// | MI * | 241 /// ---------------- 242 /// | 243 /// ---------------- 244 /// | MI * | 245 /// ---------------- 246 /// | 247 /// ---------------- 248 /// | MI * | 249 /// ---------------- 250 /// The first instruction has the special opcode "BUNDLE". It's not "inside" 251 /// a bundle, but the next three MIs are. 252 bool isInsideBundle() const { 253 return getFlag(BundledPred); 254 } 255 256 /// Return true if this instruction part of a bundle. This is true 257 /// if either itself or its following instruction is marked "InsideBundle". 258 bool isBundled() const { 259 return isBundledWithPred() || isBundledWithSucc(); 260 } 261 262 /// Return true if this instruction is part of a bundle, and it is not the 263 /// first instruction in the bundle. 264 bool isBundledWithPred() const { return getFlag(BundledPred); } 265 266 /// Return true if this instruction is part of a bundle, and it is not the 267 /// last instruction in the bundle. 268 bool isBundledWithSucc() const { return getFlag(BundledSucc); } 269 270 /// Bundle this instruction with its predecessor. This can be an unbundled 271 /// instruction, or it can be the first instruction in a bundle. 272 void bundleWithPred(); 273 274 /// Bundle this instruction with its successor. This can be an unbundled 275 /// instruction, or it can be the last instruction in a bundle. 276 void bundleWithSucc(); 277 278 /// Break bundle above this instruction. 279 void unbundleFromPred(); 280 281 /// Break bundle below this instruction. 282 void unbundleFromSucc(); 283 284 /// Returns the debug location id of this MachineInstr. 285 const DebugLoc &getDebugLoc() const { return debugLoc; } 286 287 /// Return the debug variable referenced by 288 /// this DBG_VALUE instruction. 289 const DILocalVariable *getDebugVariable() const; 290 291 /// Return the complex address expression referenced by 292 /// this DBG_VALUE instruction. 293 const DIExpression *getDebugExpression() const; 294 295 /// Return the debug label referenced by 296 /// this DBG_LABEL instruction. 297 const DILabel *getDebugLabel() const; 298 299 /// Emit an error referring to the source location of this instruction. 300 /// This should only be used for inline assembly that is somehow 301 /// impossible to compile. Other errors should have been handled much 302 /// earlier. 303 /// 304 /// If this method returns, the caller should try to recover from the error. 305 void emitError(StringRef Msg) const; 306 307 /// Returns the target instruction descriptor of this MachineInstr. 308 const MCInstrDesc &getDesc() const { return *MCID; } 309 310 /// Returns the opcode of this MachineInstr. 311 unsigned getOpcode() const { return MCID->Opcode; } 312 313 /// Access to explicit operands of the instruction. 314 unsigned getNumOperands() const { return NumOperands; } 315 316 const MachineOperand& getOperand(unsigned i) const { 317 assert(i < getNumOperands() && "getOperand() out of range!"); 318 return Operands[i]; 319 } 320 MachineOperand& getOperand(unsigned i) { 321 assert(i < getNumOperands() && "getOperand() out of range!"); 322 return Operands[i]; 323 } 324 325 /// Return true if operand \p OpIdx is a subregister index. 326 bool isOperandSubregIdx(unsigned OpIdx) const { 327 assert(getOperand(OpIdx).getType() == MachineOperand::MO_Immediate && 328 "Expected MO_Immediate operand type."); 329 if (isExtractSubreg() && OpIdx == 2) 330 return true; 331 if (isInsertSubreg() && OpIdx == 3) 332 return true; 333 if (isRegSequence() && OpIdx > 1 && (OpIdx % 2) == 0) 334 return true; 335 if (isSubregToReg() && OpIdx == 3) 336 return true; 337 return false; 338 } 339 340 /// Returns the number of non-implicit operands. 341 unsigned getNumExplicitOperands() const; 342 343 /// iterator/begin/end - Iterate over all operands of a machine instruction. 344 using mop_iterator = MachineOperand *; 345 using const_mop_iterator = const MachineOperand *; 346 347 mop_iterator operands_begin() { return Operands; } 348 mop_iterator operands_end() { return Operands + NumOperands; } 349 350 const_mop_iterator operands_begin() const { return Operands; } 351 const_mop_iterator operands_end() const { return Operands + NumOperands; } 352 353 iterator_range<mop_iterator> operands() { 354 return make_range(operands_begin(), operands_end()); 355 } 356 iterator_range<const_mop_iterator> operands() const { 357 return make_range(operands_begin(), operands_end()); 358 } 359 iterator_range<mop_iterator> explicit_operands() { 360 return make_range(operands_begin(), 361 operands_begin() + getNumExplicitOperands()); 362 } 363 iterator_range<const_mop_iterator> explicit_operands() const { 364 return make_range(operands_begin(), 365 operands_begin() + getNumExplicitOperands()); 366 } 367 iterator_range<mop_iterator> implicit_operands() { 368 return make_range(explicit_operands().end(), operands_end()); 369 } 370 iterator_range<const_mop_iterator> implicit_operands() const { 371 return make_range(explicit_operands().end(), operands_end()); 372 } 373 /// Returns a range over all explicit operands that are register definitions. 374 /// Implicit definition are not included! 375 iterator_range<mop_iterator> defs() { 376 return make_range(operands_begin(), 377 operands_begin() + getDesc().getNumDefs()); 378 } 379 /// \copydoc defs() 380 iterator_range<const_mop_iterator> defs() const { 381 return make_range(operands_begin(), 382 operands_begin() + getDesc().getNumDefs()); 383 } 384 /// Returns a range that includes all operands that are register uses. 385 /// This may include unrelated operands which are not register uses. 386 iterator_range<mop_iterator> uses() { 387 return make_range(operands_begin() + getDesc().getNumDefs(), 388 operands_end()); 389 } 390 /// \copydoc uses() 391 iterator_range<const_mop_iterator> uses() const { 392 return make_range(operands_begin() + getDesc().getNumDefs(), 393 operands_end()); 394 } 395 iterator_range<mop_iterator> explicit_uses() { 396 return make_range(operands_begin() + getDesc().getNumDefs(), 397 operands_begin() + getNumExplicitOperands() ); 398 } 399 iterator_range<const_mop_iterator> explicit_uses() const { 400 return make_range(operands_begin() + getDesc().getNumDefs(), 401 operands_begin() + getNumExplicitOperands() ); 402 } 403 404 /// Returns the number of the operand iterator \p I points to. 405 unsigned getOperandNo(const_mop_iterator I) const { 406 return I - operands_begin(); 407 } 408 409 /// Access to memory operands of the instruction 410 mmo_iterator memoperands_begin() const { return MemRefs; } 411 mmo_iterator memoperands_end() const { return MemRefs + NumMemRefs; } 412 /// Return true if we don't have any memory operands which described the 413 /// memory access done by this instruction. If this is true, calling code 414 /// must be conservative. 415 bool memoperands_empty() const { return NumMemRefs == 0; } 416 417 iterator_range<mmo_iterator> memoperands() { 418 return make_range(memoperands_begin(), memoperands_end()); 419 } 420 iterator_range<mmo_iterator> memoperands() const { 421 return make_range(memoperands_begin(), memoperands_end()); 422 } 423 424 /// Return true if this instruction has exactly one MachineMemOperand. 425 bool hasOneMemOperand() const { 426 return NumMemRefs == 1; 427 } 428 429 /// Return the number of memory operands. 430 unsigned getNumMemOperands() const { return NumMemRefs; } 431 432 /// API for querying MachineInstr properties. They are the same as MCInstrDesc 433 /// queries but they are bundle aware. 434 435 enum QueryType { 436 IgnoreBundle, // Ignore bundles 437 AnyInBundle, // Return true if any instruction in bundle has property 438 AllInBundle // Return true if all instructions in bundle have property 439 }; 440 441 /// Return true if the instruction (or in the case of a bundle, 442 /// the instructions inside the bundle) has the specified property. 443 /// The first argument is the property being queried. 444 /// The second argument indicates whether the query should look inside 445 /// instruction bundles. 446 bool hasProperty(unsigned MCFlag, QueryType Type = AnyInBundle) const { 447 // Inline the fast path for unbundled or bundle-internal instructions. 448 if (Type == IgnoreBundle || !isBundled() || isBundledWithPred()) 449 return getDesc().getFlags() & (1ULL << MCFlag); 450 451 // If this is the first instruction in a bundle, take the slow path. 452 return hasPropertyInBundle(1ULL << MCFlag, Type); 453 } 454 455 /// Return true if this instruction can have a variable number of operands. 456 /// In this case, the variable operands will be after the normal 457 /// operands but before the implicit definitions and uses (if any are 458 /// present). 459 bool isVariadic(QueryType Type = IgnoreBundle) const { 460 return hasProperty(MCID::Variadic, Type); 461 } 462 463 /// Set if this instruction has an optional definition, e.g. 464 /// ARM instructions which can set condition code if 's' bit is set. 465 bool hasOptionalDef(QueryType Type = IgnoreBundle) const { 466 return hasProperty(MCID::HasOptionalDef, Type); 467 } 468 469 /// Return true if this is a pseudo instruction that doesn't 470 /// correspond to a real machine instruction. 471 bool isPseudo(QueryType Type = IgnoreBundle) const { 472 return hasProperty(MCID::Pseudo, Type); 473 } 474 475 bool isReturn(QueryType Type = AnyInBundle) const { 476 return hasProperty(MCID::Return, Type); 477 } 478 479 bool isCall(QueryType Type = AnyInBundle) const { 480 return hasProperty(MCID::Call, Type); 481 } 482 483 /// Returns true if the specified instruction stops control flow 484 /// from executing the instruction immediately following it. Examples include 485 /// unconditional branches and return instructions. 486 bool isBarrier(QueryType Type = AnyInBundle) const { 487 return hasProperty(MCID::Barrier, Type); 488 } 489 490 /// Returns true if this instruction part of the terminator for a basic block. 491 /// Typically this is things like return and branch instructions. 492 /// 493 /// Various passes use this to insert code into the bottom of a basic block, 494 /// but before control flow occurs. 495 bool isTerminator(QueryType Type = AnyInBundle) const { 496 return hasProperty(MCID::Terminator, Type); 497 } 498 499 /// Returns true if this is a conditional, unconditional, or indirect branch. 500 /// Predicates below can be used to discriminate between 501 /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to 502 /// get more information. 503 bool isBranch(QueryType Type = AnyInBundle) const { 504 return hasProperty(MCID::Branch, Type); 505 } 506 507 /// Return true if this is an indirect branch, such as a 508 /// branch through a register. 509 bool isIndirectBranch(QueryType Type = AnyInBundle) const { 510 return hasProperty(MCID::IndirectBranch, Type); 511 } 512 513 /// Return true if this is a branch which may fall 514 /// through to the next instruction or may transfer control flow to some other 515 /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more 516 /// information about this branch. 517 bool isConditionalBranch(QueryType Type = AnyInBundle) const { 518 return isBranch(Type) & !isBarrier(Type) & !isIndirectBranch(Type); 519 } 520 521 /// Return true if this is a branch which always 522 /// transfers control flow to some other block. The 523 /// TargetInstrInfo::AnalyzeBranch method can be used to get more information 524 /// about this branch. 525 bool isUnconditionalBranch(QueryType Type = AnyInBundle) const { 526 return isBranch(Type) & isBarrier(Type) & !isIndirectBranch(Type); 527 } 528 529 /// Return true if this instruction has a predicate operand that 530 /// controls execution. It may be set to 'always', or may be set to other 531 /// values. There are various methods in TargetInstrInfo that can be used to 532 /// control and modify the predicate in this instruction. 533 bool isPredicable(QueryType Type = AllInBundle) const { 534 // If it's a bundle than all bundled instructions must be predicable for this 535 // to return true. 536 return hasProperty(MCID::Predicable, Type); 537 } 538 539 /// Return true if this instruction is a comparison. 540 bool isCompare(QueryType Type = IgnoreBundle) const { 541 return hasProperty(MCID::Compare, Type); 542 } 543 544 /// Return true if this instruction is a move immediate 545 /// (including conditional moves) instruction. 546 bool isMoveImmediate(QueryType Type = IgnoreBundle) const { 547 return hasProperty(MCID::MoveImm, Type); 548 } 549 550 /// Return true if this instruction is a register move. 551 /// (including moving values from subreg to reg) 552 bool isMoveReg(QueryType Type = IgnoreBundle) const { 553 return hasProperty(MCID::MoveReg, Type); 554 } 555 556 /// Return true if this instruction is a bitcast instruction. 557 bool isBitcast(QueryType Type = IgnoreBundle) const { 558 return hasProperty(MCID::Bitcast, Type); 559 } 560 561 /// Return true if this instruction is a select instruction. 562 bool isSelect(QueryType Type = IgnoreBundle) const { 563 return hasProperty(MCID::Select, Type); 564 } 565 566 /// Return true if this instruction cannot be safely duplicated. 567 /// For example, if the instruction has a unique labels attached 568 /// to it, duplicating it would cause multiple definition errors. 569 bool isNotDuplicable(QueryType Type = AnyInBundle) const { 570 return hasProperty(MCID::NotDuplicable, Type); 571 } 572 573 /// Return true if this instruction is convergent. 574 /// Convergent instructions can not be made control-dependent on any 575 /// additional values. 576 bool isConvergent(QueryType Type = AnyInBundle) const { 577 if (isInlineAsm()) { 578 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 579 if (ExtraInfo & InlineAsm::Extra_IsConvergent) 580 return true; 581 } 582 return hasProperty(MCID::Convergent, Type); 583 } 584 585 /// Returns true if the specified instruction has a delay slot 586 /// which must be filled by the code generator. 587 bool hasDelaySlot(QueryType Type = AnyInBundle) const { 588 return hasProperty(MCID::DelaySlot, Type); 589 } 590 591 /// Return true for instructions that can be folded as 592 /// memory operands in other instructions. The most common use for this 593 /// is instructions that are simple loads from memory that don't modify 594 /// the loaded value in any way, but it can also be used for instructions 595 /// that can be expressed as constant-pool loads, such as V_SETALLONES 596 /// on x86, to allow them to be folded when it is beneficial. 597 /// This should only be set on instructions that return a value in their 598 /// only virtual register definition. 599 bool canFoldAsLoad(QueryType Type = IgnoreBundle) const { 600 return hasProperty(MCID::FoldableAsLoad, Type); 601 } 602 603 /// Return true if this instruction behaves 604 /// the same way as the generic REG_SEQUENCE instructions. 605 /// E.g., on ARM, 606 /// dX VMOVDRR rY, rZ 607 /// is equivalent to 608 /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1. 609 /// 610 /// Note that for the optimizers to be able to take advantage of 611 /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be 612 /// override accordingly. 613 bool isRegSequenceLike(QueryType Type = IgnoreBundle) const { 614 return hasProperty(MCID::RegSequence, Type); 615 } 616 617 /// Return true if this instruction behaves 618 /// the same way as the generic EXTRACT_SUBREG instructions. 619 /// E.g., on ARM, 620 /// rX, rY VMOVRRD dZ 621 /// is equivalent to two EXTRACT_SUBREG: 622 /// rX = EXTRACT_SUBREG dZ, ssub_0 623 /// rY = EXTRACT_SUBREG dZ, ssub_1 624 /// 625 /// Note that for the optimizers to be able to take advantage of 626 /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be 627 /// override accordingly. 628 bool isExtractSubregLike(QueryType Type = IgnoreBundle) const { 629 return hasProperty(MCID::ExtractSubreg, Type); 630 } 631 632 /// Return true if this instruction behaves 633 /// the same way as the generic INSERT_SUBREG instructions. 634 /// E.g., on ARM, 635 /// dX = VSETLNi32 dY, rZ, Imm 636 /// is equivalent to a INSERT_SUBREG: 637 /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm) 638 /// 639 /// Note that for the optimizers to be able to take advantage of 640 /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be 641 /// override accordingly. 642 bool isInsertSubregLike(QueryType Type = IgnoreBundle) const { 643 return hasProperty(MCID::InsertSubreg, Type); 644 } 645 646 //===--------------------------------------------------------------------===// 647 // Side Effect Analysis 648 //===--------------------------------------------------------------------===// 649 650 /// Return true if this instruction could possibly read memory. 651 /// Instructions with this flag set are not necessarily simple load 652 /// instructions, they may load a value and modify it, for example. 653 bool mayLoad(QueryType Type = AnyInBundle) const { 654 if (isInlineAsm()) { 655 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 656 if (ExtraInfo & InlineAsm::Extra_MayLoad) 657 return true; 658 } 659 return hasProperty(MCID::MayLoad, Type); 660 } 661 662 /// Return true if this instruction could possibly modify memory. 663 /// Instructions with this flag set are not necessarily simple store 664 /// instructions, they may store a modified value based on their operands, or 665 /// may not actually modify anything, for example. 666 bool mayStore(QueryType Type = AnyInBundle) const { 667 if (isInlineAsm()) { 668 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 669 if (ExtraInfo & InlineAsm::Extra_MayStore) 670 return true; 671 } 672 return hasProperty(MCID::MayStore, Type); 673 } 674 675 /// Return true if this instruction could possibly read or modify memory. 676 bool mayLoadOrStore(QueryType Type = AnyInBundle) const { 677 return mayLoad(Type) || mayStore(Type); 678 } 679 680 //===--------------------------------------------------------------------===// 681 // Flags that indicate whether an instruction can be modified by a method. 682 //===--------------------------------------------------------------------===// 683 684 /// Return true if this may be a 2- or 3-address 685 /// instruction (of the form "X = op Y, Z, ..."), which produces the same 686 /// result if Y and Z are exchanged. If this flag is set, then the 687 /// TargetInstrInfo::commuteInstruction method may be used to hack on the 688 /// instruction. 689 /// 690 /// Note that this flag may be set on instructions that are only commutable 691 /// sometimes. In these cases, the call to commuteInstruction will fail. 692 /// Also note that some instructions require non-trivial modification to 693 /// commute them. 694 bool isCommutable(QueryType Type = IgnoreBundle) const { 695 return hasProperty(MCID::Commutable, Type); 696 } 697 698 /// Return true if this is a 2-address instruction 699 /// which can be changed into a 3-address instruction if needed. Doing this 700 /// transformation can be profitable in the register allocator, because it 701 /// means that the instruction can use a 2-address form if possible, but 702 /// degrade into a less efficient form if the source and dest register cannot 703 /// be assigned to the same register. For example, this allows the x86 704 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which 705 /// is the same speed as the shift but has bigger code size. 706 /// 707 /// If this returns true, then the target must implement the 708 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which 709 /// is allowed to fail if the transformation isn't valid for this specific 710 /// instruction (e.g. shl reg, 4 on x86). 711 /// 712 bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const { 713 return hasProperty(MCID::ConvertibleTo3Addr, Type); 714 } 715 716 /// Return true if this instruction requires 717 /// custom insertion support when the DAG scheduler is inserting it into a 718 /// machine basic block. If this is true for the instruction, it basically 719 /// means that it is a pseudo instruction used at SelectionDAG time that is 720 /// expanded out into magic code by the target when MachineInstrs are formed. 721 /// 722 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method 723 /// is used to insert this into the MachineBasicBlock. 724 bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const { 725 return hasProperty(MCID::UsesCustomInserter, Type); 726 } 727 728 /// Return true if this instruction requires *adjustment* 729 /// after instruction selection by calling a target hook. For example, this 730 /// can be used to fill in ARM 's' optional operand depending on whether 731 /// the conditional flag register is used. 732 bool hasPostISelHook(QueryType Type = IgnoreBundle) const { 733 return hasProperty(MCID::HasPostISelHook, Type); 734 } 735 736 /// Returns true if this instruction is a candidate for remat. 737 /// This flag is deprecated, please don't use it anymore. If this 738 /// flag is set, the isReallyTriviallyReMaterializable() method is called to 739 /// verify the instruction is really rematable. 740 bool isRematerializable(QueryType Type = AllInBundle) const { 741 // It's only possible to re-mat a bundle if all bundled instructions are 742 // re-materializable. 743 return hasProperty(MCID::Rematerializable, Type); 744 } 745 746 /// Returns true if this instruction has the same cost (or less) than a move 747 /// instruction. This is useful during certain types of optimizations 748 /// (e.g., remat during two-address conversion or machine licm) 749 /// where we would like to remat or hoist the instruction, but not if it costs 750 /// more than moving the instruction into the appropriate register. Note, we 751 /// are not marking copies from and to the same register class with this flag. 752 bool isAsCheapAsAMove(QueryType Type = AllInBundle) const { 753 // Only returns true for a bundle if all bundled instructions are cheap. 754 return hasProperty(MCID::CheapAsAMove, Type); 755 } 756 757 /// Returns true if this instruction source operands 758 /// have special register allocation requirements that are not captured by the 759 /// operand register classes. e.g. ARM::STRD's two source registers must be an 760 /// even / odd pair, ARM::STM registers have to be in ascending order. 761 /// Post-register allocation passes should not attempt to change allocations 762 /// for sources of instructions with this flag. 763 bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const { 764 return hasProperty(MCID::ExtraSrcRegAllocReq, Type); 765 } 766 767 /// Returns true if this instruction def operands 768 /// have special register allocation requirements that are not captured by the 769 /// operand register classes. e.g. ARM::LDRD's two def registers must be an 770 /// even / odd pair, ARM::LDM registers have to be in ascending order. 771 /// Post-register allocation passes should not attempt to change allocations 772 /// for definitions of instructions with this flag. 773 bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const { 774 return hasProperty(MCID::ExtraDefRegAllocReq, Type); 775 } 776 777 enum MICheckType { 778 CheckDefs, // Check all operands for equality 779 CheckKillDead, // Check all operands including kill / dead markers 780 IgnoreDefs, // Ignore all definitions 781 IgnoreVRegDefs // Ignore virtual register definitions 782 }; 783 784 /// Return true if this instruction is identical to \p Other. 785 /// Two instructions are identical if they have the same opcode and all their 786 /// operands are identical (with respect to MachineOperand::isIdenticalTo()). 787 /// Note that this means liveness related flags (dead, undef, kill) do not 788 /// affect the notion of identical. 789 bool isIdenticalTo(const MachineInstr &Other, 790 MICheckType Check = CheckDefs) const; 791 792 /// Unlink 'this' from the containing basic block, and return it without 793 /// deleting it. 794 /// 795 /// This function can not be used on bundled instructions, use 796 /// removeFromBundle() to remove individual instructions from a bundle. 797 MachineInstr *removeFromParent(); 798 799 /// Unlink this instruction from its basic block and return it without 800 /// deleting it. 801 /// 802 /// If the instruction is part of a bundle, the other instructions in the 803 /// bundle remain bundled. 804 MachineInstr *removeFromBundle(); 805 806 /// Unlink 'this' from the containing basic block and delete it. 807 /// 808 /// If this instruction is the header of a bundle, the whole bundle is erased. 809 /// This function can not be used for instructions inside a bundle, use 810 /// eraseFromBundle() to erase individual bundled instructions. 811 void eraseFromParent(); 812 813 /// Unlink 'this' from the containing basic block and delete it. 814 /// 815 /// For all definitions mark their uses in DBG_VALUE nodes 816 /// as undefined. Otherwise like eraseFromParent(). 817 void eraseFromParentAndMarkDBGValuesForRemoval(); 818 819 /// Unlink 'this' form its basic block and delete it. 820 /// 821 /// If the instruction is part of a bundle, the other instructions in the 822 /// bundle remain bundled. 823 void eraseFromBundle(); 824 825 bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; } 826 bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; } 827 bool isAnnotationLabel() const { 828 return getOpcode() == TargetOpcode::ANNOTATION_LABEL; 829 } 830 831 /// Returns true if the MachineInstr represents a label. 832 bool isLabel() const { 833 return isEHLabel() || isGCLabel() || isAnnotationLabel(); 834 } 835 836 bool isCFIInstruction() const { 837 return getOpcode() == TargetOpcode::CFI_INSTRUCTION; 838 } 839 840 // True if the instruction represents a position in the function. 841 bool isPosition() const { return isLabel() || isCFIInstruction(); } 842 843 bool isDebugValue() const { return getOpcode() == TargetOpcode::DBG_VALUE; } 844 bool isDebugLabel() const { return getOpcode() == TargetOpcode::DBG_LABEL; } 845 bool isDebugInstr() const { return isDebugValue() || isDebugLabel(); } 846 847 /// A DBG_VALUE is indirect iff the first operand is a register and 848 /// the second operand is an immediate. 849 bool isIndirectDebugValue() const { 850 return isDebugValue() 851 && getOperand(0).isReg() 852 && getOperand(1).isImm(); 853 } 854 855 bool isPHI() const { 856 return getOpcode() == TargetOpcode::PHI || 857 getOpcode() == TargetOpcode::G_PHI; 858 } 859 bool isKill() const { return getOpcode() == TargetOpcode::KILL; } 860 bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; } 861 bool isInlineAsm() const { return getOpcode() == TargetOpcode::INLINEASM; } 862 863 bool isMSInlineAsm() const { 864 return getOpcode() == TargetOpcode::INLINEASM && getInlineAsmDialect(); 865 } 866 867 bool isStackAligningInlineAsm() const; 868 InlineAsm::AsmDialect getInlineAsmDialect() const; 869 870 bool isInsertSubreg() const { 871 return getOpcode() == TargetOpcode::INSERT_SUBREG; 872 } 873 874 bool isSubregToReg() const { 875 return getOpcode() == TargetOpcode::SUBREG_TO_REG; 876 } 877 878 bool isRegSequence() const { 879 return getOpcode() == TargetOpcode::REG_SEQUENCE; 880 } 881 882 bool isBundle() const { 883 return getOpcode() == TargetOpcode::BUNDLE; 884 } 885 886 bool isCopy() const { 887 return getOpcode() == TargetOpcode::COPY; 888 } 889 890 bool isFullCopy() const { 891 return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg(); 892 } 893 894 bool isExtractSubreg() const { 895 return getOpcode() == TargetOpcode::EXTRACT_SUBREG; 896 } 897 898 /// Return true if the instruction behaves like a copy. 899 /// This does not include native copy instructions. 900 bool isCopyLike() const { 901 return isCopy() || isSubregToReg(); 902 } 903 904 /// Return true is the instruction is an identity copy. 905 bool isIdentityCopy() const { 906 return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() && 907 getOperand(0).getSubReg() == getOperand(1).getSubReg(); 908 } 909 910 /// Return true if this instruction doesn't produce any output in the form of 911 /// executable instructions. 912 bool isMetaInstruction() const { 913 switch (getOpcode()) { 914 default: 915 return false; 916 case TargetOpcode::IMPLICIT_DEF: 917 case TargetOpcode::KILL: 918 case TargetOpcode::CFI_INSTRUCTION: 919 case TargetOpcode::EH_LABEL: 920 case TargetOpcode::GC_LABEL: 921 case TargetOpcode::DBG_VALUE: 922 case TargetOpcode::DBG_LABEL: 923 case TargetOpcode::LIFETIME_START: 924 case TargetOpcode::LIFETIME_END: 925 return true; 926 } 927 } 928 929 /// Return true if this is a transient instruction that is either very likely 930 /// to be eliminated during register allocation (such as copy-like 931 /// instructions), or if this instruction doesn't have an execution-time cost. 932 bool isTransient() const { 933 switch (getOpcode()) { 934 default: 935 return isMetaInstruction(); 936 // Copy-like instructions are usually eliminated during register allocation. 937 case TargetOpcode::PHI: 938 case TargetOpcode::G_PHI: 939 case TargetOpcode::COPY: 940 case TargetOpcode::INSERT_SUBREG: 941 case TargetOpcode::SUBREG_TO_REG: 942 case TargetOpcode::REG_SEQUENCE: 943 return true; 944 } 945 } 946 947 /// Return the number of instructions inside the MI bundle, excluding the 948 /// bundle header. 949 /// 950 /// This is the number of instructions that MachineBasicBlock::iterator 951 /// skips, 0 for unbundled instructions. 952 unsigned getBundleSize() const; 953 954 /// Return true if the MachineInstr reads the specified register. 955 /// If TargetRegisterInfo is passed, then it also checks if there 956 /// is a read of a super-register. 957 /// This does not count partial redefines of virtual registers as reads: 958 /// %reg1024:6 = OP. 959 bool readsRegister(unsigned Reg, 960 const TargetRegisterInfo *TRI = nullptr) const { 961 return findRegisterUseOperandIdx(Reg, false, TRI) != -1; 962 } 963 964 /// Return true if the MachineInstr reads the specified virtual register. 965 /// Take into account that a partial define is a 966 /// read-modify-write operation. 967 bool readsVirtualRegister(unsigned Reg) const { 968 return readsWritesVirtualRegister(Reg).first; 969 } 970 971 /// Return a pair of bools (reads, writes) indicating if this instruction 972 /// reads or writes Reg. This also considers partial defines. 973 /// If Ops is not null, all operand indices for Reg are added. 974 std::pair<bool,bool> readsWritesVirtualRegister(unsigned Reg, 975 SmallVectorImpl<unsigned> *Ops = nullptr) const; 976 977 /// Return true if the MachineInstr kills the specified register. 978 /// If TargetRegisterInfo is passed, then it also checks if there is 979 /// a kill of a super-register. 980 bool killsRegister(unsigned Reg, 981 const TargetRegisterInfo *TRI = nullptr) const { 982 return findRegisterUseOperandIdx(Reg, true, TRI) != -1; 983 } 984 985 /// Return true if the MachineInstr fully defines the specified register. 986 /// If TargetRegisterInfo is passed, then it also checks 987 /// if there is a def of a super-register. 988 /// NOTE: It's ignoring subreg indices on virtual registers. 989 bool definesRegister(unsigned Reg, 990 const TargetRegisterInfo *TRI = nullptr) const { 991 return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1; 992 } 993 994 /// Return true if the MachineInstr modifies (fully define or partially 995 /// define) the specified register. 996 /// NOTE: It's ignoring subreg indices on virtual registers. 997 bool modifiesRegister(unsigned Reg, const TargetRegisterInfo *TRI) const { 998 return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1; 999 } 1000 1001 /// Returns true if the register is dead in this machine instruction. 1002 /// If TargetRegisterInfo is passed, then it also checks 1003 /// if there is a dead def of a super-register. 1004 bool registerDefIsDead(unsigned Reg, 1005 const TargetRegisterInfo *TRI = nullptr) const { 1006 return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1; 1007 } 1008 1009 /// Returns true if the MachineInstr has an implicit-use operand of exactly 1010 /// the given register (not considering sub/super-registers). 1011 bool hasRegisterImplicitUseOperand(unsigned Reg) const; 1012 1013 /// Returns the operand index that is a use of the specific register or -1 1014 /// if it is not found. It further tightens the search criteria to a use 1015 /// that kills the register if isKill is true. 1016 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false, 1017 const TargetRegisterInfo *TRI = nullptr) const; 1018 1019 /// Wrapper for findRegisterUseOperandIdx, it returns 1020 /// a pointer to the MachineOperand rather than an index. 1021 MachineOperand *findRegisterUseOperand(unsigned Reg, bool isKill = false, 1022 const TargetRegisterInfo *TRI = nullptr) { 1023 int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI); 1024 return (Idx == -1) ? nullptr : &getOperand(Idx); 1025 } 1026 1027 const MachineOperand *findRegisterUseOperand( 1028 unsigned Reg, bool isKill = false, 1029 const TargetRegisterInfo *TRI = nullptr) const { 1030 return const_cast<MachineInstr *>(this)-> 1031 findRegisterUseOperand(Reg, isKill, TRI); 1032 } 1033 1034 /// Returns the operand index that is a def of the specified register or 1035 /// -1 if it is not found. If isDead is true, defs that are not dead are 1036 /// skipped. If Overlap is true, then it also looks for defs that merely 1037 /// overlap the specified register. If TargetRegisterInfo is non-null, 1038 /// then it also checks if there is a def of a super-register. 1039 /// This may also return a register mask operand when Overlap is true. 1040 int findRegisterDefOperandIdx(unsigned Reg, 1041 bool isDead = false, bool Overlap = false, 1042 const TargetRegisterInfo *TRI = nullptr) const; 1043 1044 /// Wrapper for findRegisterDefOperandIdx, it returns 1045 /// a pointer to the MachineOperand rather than an index. 1046 MachineOperand *findRegisterDefOperand(unsigned Reg, bool isDead = false, 1047 const TargetRegisterInfo *TRI = nullptr) { 1048 int Idx = findRegisterDefOperandIdx(Reg, isDead, false, TRI); 1049 return (Idx == -1) ? nullptr : &getOperand(Idx); 1050 } 1051 1052 /// Find the index of the first operand in the 1053 /// operand list that is used to represent the predicate. It returns -1 if 1054 /// none is found. 1055 int findFirstPredOperandIdx() const; 1056 1057 /// Find the index of the flag word operand that 1058 /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if 1059 /// getOperand(OpIdx) does not belong to an inline asm operand group. 1060 /// 1061 /// If GroupNo is not NULL, it will receive the number of the operand group 1062 /// containing OpIdx. 1063 /// 1064 /// The flag operand is an immediate that can be decoded with methods like 1065 /// InlineAsm::hasRegClassConstraint(). 1066 int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = nullptr) const; 1067 1068 /// Compute the static register class constraint for operand OpIdx. 1069 /// For normal instructions, this is derived from the MCInstrDesc. 1070 /// For inline assembly it is derived from the flag words. 1071 /// 1072 /// Returns NULL if the static register class constraint cannot be 1073 /// determined. 1074 const TargetRegisterClass* 1075 getRegClassConstraint(unsigned OpIdx, 1076 const TargetInstrInfo *TII, 1077 const TargetRegisterInfo *TRI) const; 1078 1079 /// Applies the constraints (def/use) implied by this MI on \p Reg to 1080 /// the given \p CurRC. 1081 /// If \p ExploreBundle is set and MI is part of a bundle, all the 1082 /// instructions inside the bundle will be taken into account. In other words, 1083 /// this method accumulates all the constraints of the operand of this MI and 1084 /// the related bundle if MI is a bundle or inside a bundle. 1085 /// 1086 /// Returns the register class that satisfies both \p CurRC and the 1087 /// constraints set by MI. Returns NULL if such a register class does not 1088 /// exist. 1089 /// 1090 /// \pre CurRC must not be NULL. 1091 const TargetRegisterClass *getRegClassConstraintEffectForVReg( 1092 unsigned Reg, const TargetRegisterClass *CurRC, 1093 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, 1094 bool ExploreBundle = false) const; 1095 1096 /// Applies the constraints (def/use) implied by the \p OpIdx operand 1097 /// to the given \p CurRC. 1098 /// 1099 /// Returns the register class that satisfies both \p CurRC and the 1100 /// constraints set by \p OpIdx MI. Returns NULL if such a register class 1101 /// does not exist. 1102 /// 1103 /// \pre CurRC must not be NULL. 1104 /// \pre The operand at \p OpIdx must be a register. 1105 const TargetRegisterClass * 1106 getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC, 1107 const TargetInstrInfo *TII, 1108 const TargetRegisterInfo *TRI) const; 1109 1110 /// Add a tie between the register operands at DefIdx and UseIdx. 1111 /// The tie will cause the register allocator to ensure that the two 1112 /// operands are assigned the same physical register. 1113 /// 1114 /// Tied operands are managed automatically for explicit operands in the 1115 /// MCInstrDesc. This method is for exceptional cases like inline asm. 1116 void tieOperands(unsigned DefIdx, unsigned UseIdx); 1117 1118 /// Given the index of a tied register operand, find the 1119 /// operand it is tied to. Defs are tied to uses and vice versa. Returns the 1120 /// index of the tied operand which must exist. 1121 unsigned findTiedOperandIdx(unsigned OpIdx) const; 1122 1123 /// Given the index of a register def operand, 1124 /// check if the register def is tied to a source operand, due to either 1125 /// two-address elimination or inline assembly constraints. Returns the 1126 /// first tied use operand index by reference if UseOpIdx is not null. 1127 bool isRegTiedToUseOperand(unsigned DefOpIdx, 1128 unsigned *UseOpIdx = nullptr) const { 1129 const MachineOperand &MO = getOperand(DefOpIdx); 1130 if (!MO.isReg() || !MO.isDef() || !MO.isTied()) 1131 return false; 1132 if (UseOpIdx) 1133 *UseOpIdx = findTiedOperandIdx(DefOpIdx); 1134 return true; 1135 } 1136 1137 /// Return true if the use operand of the specified index is tied to a def 1138 /// operand. It also returns the def operand index by reference if DefOpIdx 1139 /// is not null. 1140 bool isRegTiedToDefOperand(unsigned UseOpIdx, 1141 unsigned *DefOpIdx = nullptr) const { 1142 const MachineOperand &MO = getOperand(UseOpIdx); 1143 if (!MO.isReg() || !MO.isUse() || !MO.isTied()) 1144 return false; 1145 if (DefOpIdx) 1146 *DefOpIdx = findTiedOperandIdx(UseOpIdx); 1147 return true; 1148 } 1149 1150 /// Clears kill flags on all operands. 1151 void clearKillInfo(); 1152 1153 /// Replace all occurrences of FromReg with ToReg:SubIdx, 1154 /// properly composing subreg indices where necessary. 1155 void substituteRegister(unsigned FromReg, unsigned ToReg, unsigned SubIdx, 1156 const TargetRegisterInfo &RegInfo); 1157 1158 /// We have determined MI kills a register. Look for the 1159 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true, 1160 /// add a implicit operand if it's not found. Returns true if the operand 1161 /// exists / is added. 1162 bool addRegisterKilled(unsigned IncomingReg, 1163 const TargetRegisterInfo *RegInfo, 1164 bool AddIfNotFound = false); 1165 1166 /// Clear all kill flags affecting Reg. If RegInfo is provided, this includes 1167 /// all aliasing registers. 1168 void clearRegisterKills(unsigned Reg, const TargetRegisterInfo *RegInfo); 1169 1170 /// We have determined MI defined a register without a use. 1171 /// Look for the operand that defines it and mark it as IsDead. If 1172 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns 1173 /// true if the operand exists / is added. 1174 bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo, 1175 bool AddIfNotFound = false); 1176 1177 /// Clear all dead flags on operands defining register @p Reg. 1178 void clearRegisterDeads(unsigned Reg); 1179 1180 /// Mark all subregister defs of register @p Reg with the undef flag. 1181 /// This function is used when we determined to have a subregister def in an 1182 /// otherwise undefined super register. 1183 void setRegisterDefReadUndef(unsigned Reg, bool IsUndef = true); 1184 1185 /// We have determined MI defines a register. Make sure there is an operand 1186 /// defining Reg. 1187 void addRegisterDefined(unsigned Reg, 1188 const TargetRegisterInfo *RegInfo = nullptr); 1189 1190 /// Mark every physreg used by this instruction as 1191 /// dead except those in the UsedRegs list. 1192 /// 1193 /// On instructions with register mask operands, also add implicit-def 1194 /// operands for all registers in UsedRegs. 1195 void setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs, 1196 const TargetRegisterInfo &TRI); 1197 1198 /// Return true if it is safe to move this instruction. If 1199 /// SawStore is set to true, it means that there is a store (or call) between 1200 /// the instruction's location and its intended destination. 1201 bool isSafeToMove(AliasAnalysis *AA, bool &SawStore) const; 1202 1203 /// Returns true if this instruction's memory access aliases the memory 1204 /// access of Other. 1205 // 1206 /// Assumes any physical registers used to compute addresses 1207 /// have the same value for both instructions. Returns false if neither 1208 /// instruction writes to memory. 1209 /// 1210 /// @param AA Optional alias analysis, used to compare memory operands. 1211 /// @param Other MachineInstr to check aliasing against. 1212 /// @param UseTBAA Whether to pass TBAA information to alias analysis. 1213 bool mayAlias(AliasAnalysis *AA, MachineInstr &Other, bool UseTBAA); 1214 1215 /// Return true if this instruction may have an ordered 1216 /// or volatile memory reference, or if the information describing the memory 1217 /// reference is not available. Return false if it is known to have no 1218 /// ordered or volatile memory references. 1219 bool hasOrderedMemoryRef() const; 1220 1221 /// Return true if this load instruction never traps and points to a memory 1222 /// location whose value doesn't change during the execution of this function. 1223 /// 1224 /// Examples include loading a value from the constant pool or from the 1225 /// argument area of a function (if it does not change). If the instruction 1226 /// does multiple loads, this returns true only if all of the loads are 1227 /// dereferenceable and invariant. 1228 bool isDereferenceableInvariantLoad(AliasAnalysis *AA) const; 1229 1230 /// If the specified instruction is a PHI that always merges together the 1231 /// same virtual register, return the register, otherwise return 0. 1232 unsigned isConstantValuePHI() const; 1233 1234 /// Return true if this instruction has side effects that are not modeled 1235 /// by mayLoad / mayStore, etc. 1236 /// For all instructions, the property is encoded in MCInstrDesc::Flags 1237 /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is 1238 /// INLINEASM instruction, in which case the side effect property is encoded 1239 /// in one of its operands (see InlineAsm::Extra_HasSideEffect). 1240 /// 1241 bool hasUnmodeledSideEffects() const; 1242 1243 /// Returns true if it is illegal to fold a load across this instruction. 1244 bool isLoadFoldBarrier() const; 1245 1246 /// Return true if all the defs of this instruction are dead. 1247 bool allDefsAreDead() const; 1248 1249 /// Copy implicit register operands from specified 1250 /// instruction to this instruction. 1251 void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI); 1252 1253 /// Debugging support 1254 /// @{ 1255 /// Determine the generic type to be printed (if needed) on uses and defs. 1256 LLT getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes, 1257 const MachineRegisterInfo &MRI) const; 1258 1259 /// Return true when an instruction has tied register that can't be determined 1260 /// by the instruction's descriptor. This is useful for MIR printing, to 1261 /// determine whether we need to print the ties or not. 1262 bool hasComplexRegisterTies() const; 1263 1264 /// Print this MI to \p OS. 1265 /// Don't print information that can be inferred from other instructions if 1266 /// \p IsStandalone is false. It is usually true when only a fragment of the 1267 /// function is printed. 1268 /// Only print the defs and the opcode if \p SkipOpers is true. 1269 /// Otherwise, also print operands if \p SkipDebugLoc is true. 1270 /// Otherwise, also print the debug loc, with a terminating newline. 1271 /// \p TII is used to print the opcode name. If it's not present, but the 1272 /// MI is in a function, the opcode will be printed using the function's TII. 1273 void print(raw_ostream &OS, bool IsStandalone = true, bool SkipOpers = false, 1274 bool SkipDebugLoc = false, bool AddNewLine = true, 1275 const TargetInstrInfo *TII = nullptr) const; 1276 void print(raw_ostream &OS, ModuleSlotTracker &MST, bool IsStandalone = true, 1277 bool SkipOpers = false, bool SkipDebugLoc = false, 1278 bool AddNewLine = true, 1279 const TargetInstrInfo *TII = nullptr) const; 1280 void dump() const; 1281 /// @} 1282 1283 //===--------------------------------------------------------------------===// 1284 // Accessors used to build up machine instructions. 1285 1286 /// Add the specified operand to the instruction. If it is an implicit 1287 /// operand, it is added to the end of the operand list. If it is an 1288 /// explicit operand it is added at the end of the explicit operand list 1289 /// (before the first implicit operand). 1290 /// 1291 /// MF must be the machine function that was used to allocate this 1292 /// instruction. 1293 /// 1294 /// MachineInstrBuilder provides a more convenient interface for creating 1295 /// instructions and adding operands. 1296 void addOperand(MachineFunction &MF, const MachineOperand &Op); 1297 1298 /// Add an operand without providing an MF reference. This only works for 1299 /// instructions that are inserted in a basic block. 1300 /// 1301 /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be 1302 /// preferred. 1303 void addOperand(const MachineOperand &Op); 1304 1305 /// Replace the instruction descriptor (thus opcode) of 1306 /// the current instruction with a new one. 1307 void setDesc(const MCInstrDesc &tid) { MCID = &tid; } 1308 1309 /// Replace current source information with new such. 1310 /// Avoid using this, the constructor argument is preferable. 1311 void setDebugLoc(DebugLoc dl) { 1312 debugLoc = std::move(dl); 1313 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); 1314 } 1315 1316 /// Erase an operand from an instruction, leaving it with one 1317 /// fewer operand than it started with. 1318 void RemoveOperand(unsigned i); 1319 1320 /// Add a MachineMemOperand to the machine instruction. 1321 /// This function should be used only occasionally. The setMemRefs function 1322 /// is the primary method for setting up a MachineInstr's MemRefs list. 1323 void addMemOperand(MachineFunction &MF, MachineMemOperand *MO); 1324 1325 /// Assign this MachineInstr's memory reference descriptor list. 1326 /// This does not transfer ownership. 1327 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) { 1328 setMemRefs(std::make_pair(NewMemRefs, NewMemRefsEnd-NewMemRefs)); 1329 } 1330 1331 /// Assign this MachineInstr's memory reference descriptor list. First 1332 /// element in the pair is the begin iterator/pointer to the array; the 1333 /// second is the number of MemoryOperands. This does not transfer ownership 1334 /// of the underlying memory. 1335 void setMemRefs(std::pair<mmo_iterator, unsigned> NewMemRefs) { 1336 MemRefs = NewMemRefs.first; 1337 NumMemRefs = uint8_t(NewMemRefs.second); 1338 assert(NumMemRefs == NewMemRefs.second && 1339 "Too many memrefs - must drop memory operands"); 1340 } 1341 1342 /// Return a set of memrefs (begin iterator, size) which conservatively 1343 /// describe the memory behavior of both MachineInstrs. This is appropriate 1344 /// for use when merging two MachineInstrs into one. This routine does not 1345 /// modify the memrefs of the this MachineInstr. 1346 std::pair<mmo_iterator, unsigned> mergeMemRefsWith(const MachineInstr& Other); 1347 1348 /// Return the MIFlags which represent both MachineInstrs. This 1349 /// should be used when merging two MachineInstrs into one. This routine does 1350 /// not modify the MIFlags of this MachineInstr. 1351 uint8_t mergeFlagsWith(const MachineInstr& Other) const; 1352 1353 /// Clear this MachineInstr's memory reference descriptor list. This resets 1354 /// the memrefs to their most conservative state. This should be used only 1355 /// as a last resort since it greatly pessimizes our knowledge of the memory 1356 /// access performed by the instruction. 1357 void dropMemRefs() { 1358 MemRefs = nullptr; 1359 NumMemRefs = 0; 1360 } 1361 1362 /// Break any tie involving OpIdx. 1363 void untieRegOperand(unsigned OpIdx) { 1364 MachineOperand &MO = getOperand(OpIdx); 1365 if (MO.isReg() && MO.isTied()) { 1366 getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0; 1367 MO.TiedTo = 0; 1368 } 1369 } 1370 1371 /// Add all implicit def and use operands to this instruction. 1372 void addImplicitDefUseOperands(MachineFunction &MF); 1373 1374 private: 1375 /// If this instruction is embedded into a MachineFunction, return the 1376 /// MachineRegisterInfo object for the current function, otherwise 1377 /// return null. 1378 MachineRegisterInfo *getRegInfo(); 1379 1380 /// Unlink all of the register operands in this instruction from their 1381 /// respective use lists. This requires that the operands already be on their 1382 /// use lists. 1383 void RemoveRegOperandsFromUseLists(MachineRegisterInfo&); 1384 1385 /// Add all of the register operands in this instruction from their 1386 /// respective use lists. This requires that the operands not be on their 1387 /// use lists yet. 1388 void AddRegOperandsToUseLists(MachineRegisterInfo&); 1389 1390 /// Slow path for hasProperty when we're dealing with a bundle. 1391 bool hasPropertyInBundle(unsigned Mask, QueryType Type) const; 1392 1393 /// Implements the logic of getRegClassConstraintEffectForVReg for the 1394 /// this MI and the given operand index \p OpIdx. 1395 /// If the related operand does not constrained Reg, this returns CurRC. 1396 const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl( 1397 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC, 1398 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const; 1399 }; 1400 1401 /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the 1402 /// instruction rather than by pointer value. 1403 /// The hashing and equality testing functions ignore definitions so this is 1404 /// useful for CSE, etc. 1405 struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> { 1406 static inline MachineInstr *getEmptyKey() { 1407 return nullptr; 1408 } 1409 1410 static inline MachineInstr *getTombstoneKey() { 1411 return reinterpret_cast<MachineInstr*>(-1); 1412 } 1413 1414 static unsigned getHashValue(const MachineInstr* const &MI); 1415 1416 static bool isEqual(const MachineInstr* const &LHS, 1417 const MachineInstr* const &RHS) { 1418 if (RHS == getEmptyKey() || RHS == getTombstoneKey() || 1419 LHS == getEmptyKey() || LHS == getTombstoneKey()) 1420 return LHS == RHS; 1421 return LHS->isIdenticalTo(*RHS, MachineInstr::IgnoreVRegDefs); 1422 } 1423 }; 1424 1425 //===----------------------------------------------------------------------===// 1426 // Debugging Support 1427 1428 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) { 1429 MI.print(OS); 1430 return OS; 1431 } 1432 1433 } // end namespace llvm 1434 1435 #endif // LLVM_CODEGEN_MACHINEINSTR_H 1436