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