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