Home | History | Annotate | Download | only in CodeGen
      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     if (isInlineAsm()) {
    530       unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
    531       if (ExtraInfo & InlineAsm::Extra_IsConvergent)
    532         return true;
    533     }
    534     return hasProperty(MCID::Convergent, Type);
    535   }
    536 
    537   /// Returns true if the specified instruction has a delay slot
    538   /// which must be filled by the code generator.
    539   bool hasDelaySlot(QueryType Type = AnyInBundle) const {
    540     return hasProperty(MCID::DelaySlot, Type);
    541   }
    542 
    543   /// Return true for instructions that can be folded as
    544   /// memory operands in other instructions. The most common use for this
    545   /// is instructions that are simple loads from memory that don't modify
    546   /// the loaded value in any way, but it can also be used for instructions
    547   /// that can be expressed as constant-pool loads, such as V_SETALLONES
    548   /// on x86, to allow them to be folded when it is beneficial.
    549   /// This should only be set on instructions that return a value in their
    550   /// only virtual register definition.
    551   bool canFoldAsLoad(QueryType Type = IgnoreBundle) const {
    552     return hasProperty(MCID::FoldableAsLoad, Type);
    553   }
    554 
    555   /// \brief Return true if this instruction behaves
    556   /// the same way as the generic REG_SEQUENCE instructions.
    557   /// E.g., on ARM,
    558   /// dX VMOVDRR rY, rZ
    559   /// is equivalent to
    560   /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1.
    561   ///
    562   /// Note that for the optimizers to be able to take advantage of
    563   /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be
    564   /// override accordingly.
    565   bool isRegSequenceLike(QueryType Type = IgnoreBundle) const {
    566     return hasProperty(MCID::RegSequence, Type);
    567   }
    568 
    569   /// \brief Return true if this instruction behaves
    570   /// the same way as the generic EXTRACT_SUBREG instructions.
    571   /// E.g., on ARM,
    572   /// rX, rY VMOVRRD dZ
    573   /// is equivalent to two EXTRACT_SUBREG:
    574   /// rX = EXTRACT_SUBREG dZ, ssub_0
    575   /// rY = EXTRACT_SUBREG dZ, ssub_1
    576   ///
    577   /// Note that for the optimizers to be able to take advantage of
    578   /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be
    579   /// override accordingly.
    580   bool isExtractSubregLike(QueryType Type = IgnoreBundle) const {
    581     return hasProperty(MCID::ExtractSubreg, Type);
    582   }
    583 
    584   /// \brief Return true if this instruction behaves
    585   /// the same way as the generic INSERT_SUBREG instructions.
    586   /// E.g., on ARM,
    587   /// dX = VSETLNi32 dY, rZ, Imm
    588   /// is equivalent to a INSERT_SUBREG:
    589   /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm)
    590   ///
    591   /// Note that for the optimizers to be able to take advantage of
    592   /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be
    593   /// override accordingly.
    594   bool isInsertSubregLike(QueryType Type = IgnoreBundle) const {
    595     return hasProperty(MCID::InsertSubreg, Type);
    596   }
    597 
    598   //===--------------------------------------------------------------------===//
    599   // Side Effect Analysis
    600   //===--------------------------------------------------------------------===//
    601 
    602   /// Return true if this instruction could possibly read memory.
    603   /// Instructions with this flag set are not necessarily simple load
    604   /// instructions, they may load a value and modify it, for example.
    605   bool mayLoad(QueryType Type = AnyInBundle) const {
    606     if (isInlineAsm()) {
    607       unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
    608       if (ExtraInfo & InlineAsm::Extra_MayLoad)
    609         return true;
    610     }
    611     return hasProperty(MCID::MayLoad, Type);
    612   }
    613 
    614   /// Return true if this instruction could possibly modify memory.
    615   /// Instructions with this flag set are not necessarily simple store
    616   /// instructions, they may store a modified value based on their operands, or
    617   /// may not actually modify anything, for example.
    618   bool mayStore(QueryType Type = AnyInBundle) const {
    619     if (isInlineAsm()) {
    620       unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
    621       if (ExtraInfo & InlineAsm::Extra_MayStore)
    622         return true;
    623     }
    624     return hasProperty(MCID::MayStore, Type);
    625   }
    626 
    627   /// Return true if this instruction could possibly read or modify memory.
    628   bool mayLoadOrStore(QueryType Type = AnyInBundle) const {
    629     return mayLoad(Type) || mayStore(Type);
    630   }
    631 
    632   //===--------------------------------------------------------------------===//
    633   // Flags that indicate whether an instruction can be modified by a method.
    634   //===--------------------------------------------------------------------===//
    635 
    636   /// Return true if this may be a 2- or 3-address
    637   /// instruction (of the form "X = op Y, Z, ..."), which produces the same
    638   /// result if Y and Z are exchanged.  If this flag is set, then the
    639   /// TargetInstrInfo::commuteInstruction method may be used to hack on the
    640   /// instruction.
    641   ///
    642   /// Note that this flag may be set on instructions that are only commutable
    643   /// sometimes.  In these cases, the call to commuteInstruction will fail.
    644   /// Also note that some instructions require non-trivial modification to
    645   /// commute them.
    646   bool isCommutable(QueryType Type = IgnoreBundle) const {
    647     return hasProperty(MCID::Commutable, Type);
    648   }
    649 
    650   /// Return true if this is a 2-address instruction
    651   /// which can be changed into a 3-address instruction if needed.  Doing this
    652   /// transformation can be profitable in the register allocator, because it
    653   /// means that the instruction can use a 2-address form if possible, but
    654   /// degrade into a less efficient form if the source and dest register cannot
    655   /// be assigned to the same register.  For example, this allows the x86
    656   /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
    657   /// is the same speed as the shift but has bigger code size.
    658   ///
    659   /// If this returns true, then the target must implement the
    660   /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
    661   /// is allowed to fail if the transformation isn't valid for this specific
    662   /// instruction (e.g. shl reg, 4 on x86).
    663   ///
    664   bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const {
    665     return hasProperty(MCID::ConvertibleTo3Addr, Type);
    666   }
    667 
    668   /// Return true if this instruction requires
    669   /// custom insertion support when the DAG scheduler is inserting it into a
    670   /// machine basic block.  If this is true for the instruction, it basically
    671   /// means that it is a pseudo instruction used at SelectionDAG time that is
    672   /// expanded out into magic code by the target when MachineInstrs are formed.
    673   ///
    674   /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
    675   /// is used to insert this into the MachineBasicBlock.
    676   bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const {
    677     return hasProperty(MCID::UsesCustomInserter, Type);
    678   }
    679 
    680   /// Return true if this instruction requires *adjustment*
    681   /// after instruction selection by calling a target hook. For example, this
    682   /// can be used to fill in ARM 's' optional operand depending on whether
    683   /// the conditional flag register is used.
    684   bool hasPostISelHook(QueryType Type = IgnoreBundle) const {
    685     return hasProperty(MCID::HasPostISelHook, Type);
    686   }
    687 
    688   /// Returns true if this instruction is a candidate for remat.
    689   /// This flag is deprecated, please don't use it anymore.  If this
    690   /// flag is set, the isReallyTriviallyReMaterializable() method is called to
    691   /// verify the instruction is really rematable.
    692   bool isRematerializable(QueryType Type = AllInBundle) const {
    693     // It's only possible to re-mat a bundle if all bundled instructions are
    694     // re-materializable.
    695     return hasProperty(MCID::Rematerializable, Type);
    696   }
    697 
    698   /// Returns true if this instruction has the same cost (or less) than a move
    699   /// instruction. This is useful during certain types of optimizations
    700   /// (e.g., remat during two-address conversion or machine licm)
    701   /// where we would like to remat or hoist the instruction, but not if it costs
    702   /// more than moving the instruction into the appropriate register. Note, we
    703   /// are not marking copies from and to the same register class with this flag.
    704   bool isAsCheapAsAMove(QueryType Type = AllInBundle) const {
    705     // Only returns true for a bundle if all bundled instructions are cheap.
    706     return hasProperty(MCID::CheapAsAMove, Type);
    707   }
    708 
    709   /// Returns true if this instruction source operands
    710   /// have special register allocation requirements that are not captured by the
    711   /// operand register classes. e.g. ARM::STRD's two source registers must be an
    712   /// even / odd pair, ARM::STM registers have to be in ascending order.
    713   /// Post-register allocation passes should not attempt to change allocations
    714   /// for sources of instructions with this flag.
    715   bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const {
    716     return hasProperty(MCID::ExtraSrcRegAllocReq, Type);
    717   }
    718 
    719   /// Returns true if this instruction def operands
    720   /// have special register allocation requirements that are not captured by the
    721   /// operand register classes. e.g. ARM::LDRD's two def registers must be an
    722   /// even / odd pair, ARM::LDM registers have to be in ascending order.
    723   /// Post-register allocation passes should not attempt to change allocations
    724   /// for definitions of instructions with this flag.
    725   bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const {
    726     return hasProperty(MCID::ExtraDefRegAllocReq, Type);
    727   }
    728 
    729 
    730   enum MICheckType {
    731     CheckDefs,      // Check all operands for equality
    732     CheckKillDead,  // Check all operands including kill / dead markers
    733     IgnoreDefs,     // Ignore all definitions
    734     IgnoreVRegDefs  // Ignore virtual register definitions
    735   };
    736 
    737   /// Return true if this instruction is identical to (same
    738   /// opcode and same operands as) the specified instruction.
    739   bool isIdenticalTo(const MachineInstr &Other,
    740                      MICheckType Check = CheckDefs) const;
    741 
    742   /// Unlink 'this' from the containing basic block, and return it without
    743   /// deleting it.
    744   ///
    745   /// This function can not be used on bundled instructions, use
    746   /// removeFromBundle() to remove individual instructions from a bundle.
    747   MachineInstr *removeFromParent();
    748 
    749   /// Unlink this instruction from its basic block and return it without
    750   /// deleting it.
    751   ///
    752   /// If the instruction is part of a bundle, the other instructions in the
    753   /// bundle remain bundled.
    754   MachineInstr *removeFromBundle();
    755 
    756   /// Unlink 'this' from the containing basic block and delete it.
    757   ///
    758   /// If this instruction is the header of a bundle, the whole bundle is erased.
    759   /// This function can not be used for instructions inside a bundle, use
    760   /// eraseFromBundle() to erase individual bundled instructions.
    761   void eraseFromParent();
    762 
    763   /// Unlink 'this' from the containing basic block and delete it.
    764   ///
    765   /// For all definitions mark their uses in DBG_VALUE nodes
    766   /// as undefined. Otherwise like eraseFromParent().
    767   void eraseFromParentAndMarkDBGValuesForRemoval();
    768 
    769   /// Unlink 'this' form its basic block and delete it.
    770   ///
    771   /// If the instruction is part of a bundle, the other instructions in the
    772   /// bundle remain bundled.
    773   void eraseFromBundle();
    774 
    775   bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
    776   bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
    777 
    778   /// Returns true if the MachineInstr represents a label.
    779   bool isLabel() const { return isEHLabel() || isGCLabel(); }
    780   bool isCFIInstruction() const {
    781     return getOpcode() == TargetOpcode::CFI_INSTRUCTION;
    782   }
    783 
    784   // True if the instruction represents a position in the function.
    785   bool isPosition() const { return isLabel() || isCFIInstruction(); }
    786 
    787   bool isDebugValue() const { return getOpcode() == TargetOpcode::DBG_VALUE; }
    788   /// A DBG_VALUE is indirect iff the first operand is a register and
    789   /// the second operand is an immediate.
    790   bool isIndirectDebugValue() const {
    791     return isDebugValue()
    792       && getOperand(0).isReg()
    793       && getOperand(1).isImm();
    794   }
    795 
    796   bool isPHI() const { return getOpcode() == TargetOpcode::PHI; }
    797   bool isKill() const { return getOpcode() == TargetOpcode::KILL; }
    798   bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; }
    799   bool isInlineAsm() const { return getOpcode() == TargetOpcode::INLINEASM; }
    800   bool isMSInlineAsm() const {
    801     return getOpcode() == TargetOpcode::INLINEASM && getInlineAsmDialect();
    802   }
    803   bool isStackAligningInlineAsm() const;
    804   InlineAsm::AsmDialect getInlineAsmDialect() const;
    805   bool isInsertSubreg() const {
    806     return getOpcode() == TargetOpcode::INSERT_SUBREG;
    807   }
    808   bool isSubregToReg() const {
    809     return getOpcode() == TargetOpcode::SUBREG_TO_REG;
    810   }
    811   bool isRegSequence() const {
    812     return getOpcode() == TargetOpcode::REG_SEQUENCE;
    813   }
    814   bool isBundle() const {
    815     return getOpcode() == TargetOpcode::BUNDLE;
    816   }
    817   bool isCopy() const {
    818     return getOpcode() == TargetOpcode::COPY;
    819   }
    820   bool isFullCopy() const {
    821     return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg();
    822   }
    823   bool isExtractSubreg() const {
    824     return getOpcode() == TargetOpcode::EXTRACT_SUBREG;
    825   }
    826 
    827   /// Return true if the instruction behaves like a copy.
    828   /// This does not include native copy instructions.
    829   bool isCopyLike() const {
    830     return isCopy() || isSubregToReg();
    831   }
    832 
    833   /// Return true is the instruction is an identity copy.
    834   bool isIdentityCopy() const {
    835     return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() &&
    836       getOperand(0).getSubReg() == getOperand(1).getSubReg();
    837   }
    838 
    839   /// Return true if this is a transient instruction that is
    840   /// either very likely to be eliminated during register allocation (such as
    841   /// copy-like instructions), or if this instruction doesn't have an
    842   /// execution-time cost.
    843   bool isTransient() const {
    844     switch(getOpcode()) {
    845     default: return false;
    846     // Copy-like instructions are usually eliminated during register allocation.
    847     case TargetOpcode::PHI:
    848     case TargetOpcode::COPY:
    849     case TargetOpcode::INSERT_SUBREG:
    850     case TargetOpcode::SUBREG_TO_REG:
    851     case TargetOpcode::REG_SEQUENCE:
    852     // Pseudo-instructions that don't produce any real output.
    853     case TargetOpcode::IMPLICIT_DEF:
    854     case TargetOpcode::KILL:
    855     case TargetOpcode::CFI_INSTRUCTION:
    856     case TargetOpcode::EH_LABEL:
    857     case TargetOpcode::GC_LABEL:
    858     case TargetOpcode::DBG_VALUE:
    859       return true;
    860     }
    861   }
    862 
    863   /// Return the number of instructions inside the MI bundle, excluding the
    864   /// bundle header.
    865   ///
    866   /// This is the number of instructions that MachineBasicBlock::iterator
    867   /// skips, 0 for unbundled instructions.
    868   unsigned getBundleSize() const;
    869 
    870   /// Return true if the MachineInstr reads the specified register.
    871   /// If TargetRegisterInfo is passed, then it also checks if there
    872   /// is a read of a super-register.
    873   /// This does not count partial redefines of virtual registers as reads:
    874   ///   %reg1024:6 = OP.
    875   bool readsRegister(unsigned Reg,
    876                      const TargetRegisterInfo *TRI = nullptr) const {
    877     return findRegisterUseOperandIdx(Reg, false, TRI) != -1;
    878   }
    879 
    880   /// Return true if the MachineInstr reads the specified virtual register.
    881   /// Take into account that a partial define is a
    882   /// read-modify-write operation.
    883   bool readsVirtualRegister(unsigned Reg) const {
    884     return readsWritesVirtualRegister(Reg).first;
    885   }
    886 
    887   /// Return a pair of bools (reads, writes) indicating if this instruction
    888   /// reads or writes Reg. This also considers partial defines.
    889   /// If Ops is not null, all operand indices for Reg are added.
    890   std::pair<bool,bool> readsWritesVirtualRegister(unsigned Reg,
    891                                 SmallVectorImpl<unsigned> *Ops = nullptr) const;
    892 
    893   /// Return true if the MachineInstr kills the specified register.
    894   /// If TargetRegisterInfo is passed, then it also checks if there is
    895   /// a kill of a super-register.
    896   bool killsRegister(unsigned Reg,
    897                      const TargetRegisterInfo *TRI = nullptr) const {
    898     return findRegisterUseOperandIdx(Reg, true, TRI) != -1;
    899   }
    900 
    901   /// Return true if the MachineInstr fully defines the specified register.
    902   /// If TargetRegisterInfo is passed, then it also checks
    903   /// if there is a def of a super-register.
    904   /// NOTE: It's ignoring subreg indices on virtual registers.
    905   bool definesRegister(unsigned Reg,
    906                        const TargetRegisterInfo *TRI = nullptr) const {
    907     return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1;
    908   }
    909 
    910   /// Return true if the MachineInstr modifies (fully define or partially
    911   /// define) the specified register.
    912   /// NOTE: It's ignoring subreg indices on virtual registers.
    913   bool modifiesRegister(unsigned Reg, const TargetRegisterInfo *TRI) const {
    914     return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1;
    915   }
    916 
    917   /// Returns true if the register is dead in this machine instruction.
    918   /// If TargetRegisterInfo is passed, then it also checks
    919   /// if there is a dead def of a super-register.
    920   bool registerDefIsDead(unsigned Reg,
    921                          const TargetRegisterInfo *TRI = nullptr) const {
    922     return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1;
    923   }
    924 
    925   /// Returns true if the MachineInstr has an implicit-use operand of exactly
    926   /// the given register (not considering sub/super-registers).
    927   bool hasRegisterImplicitUseOperand(unsigned Reg) const;
    928 
    929   /// Returns the operand index that is a use of the specific register or -1
    930   /// if it is not found. It further tightens the search criteria to a use
    931   /// that kills the register if isKill is true.
    932   int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false,
    933                                 const TargetRegisterInfo *TRI = nullptr) const;
    934 
    935   /// Wrapper for findRegisterUseOperandIdx, it returns
    936   /// a pointer to the MachineOperand rather than an index.
    937   MachineOperand *findRegisterUseOperand(unsigned Reg, bool isKill = false,
    938                                       const TargetRegisterInfo *TRI = nullptr) {
    939     int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI);
    940     return (Idx == -1) ? nullptr : &getOperand(Idx);
    941   }
    942 
    943   const MachineOperand *findRegisterUseOperand(
    944     unsigned Reg, bool isKill = false,
    945     const TargetRegisterInfo *TRI = nullptr) const {
    946     return const_cast<MachineInstr *>(this)->
    947       findRegisterUseOperand(Reg, isKill, TRI);
    948   }
    949 
    950   /// Returns the operand index that is a def of the specified register or
    951   /// -1 if it is not found. If isDead is true, defs that are not dead are
    952   /// skipped. If Overlap is true, then it also looks for defs that merely
    953   /// overlap the specified register. If TargetRegisterInfo is non-null,
    954   /// then it also checks if there is a def of a super-register.
    955   /// This may also return a register mask operand when Overlap is true.
    956   int findRegisterDefOperandIdx(unsigned Reg,
    957                                 bool isDead = false, bool Overlap = false,
    958                                 const TargetRegisterInfo *TRI = nullptr) const;
    959 
    960   /// Wrapper for findRegisterDefOperandIdx, it returns
    961   /// a pointer to the MachineOperand rather than an index.
    962   MachineOperand *findRegisterDefOperand(unsigned Reg, bool isDead = false,
    963                                       const TargetRegisterInfo *TRI = nullptr) {
    964     int Idx = findRegisterDefOperandIdx(Reg, isDead, false, TRI);
    965     return (Idx == -1) ? nullptr : &getOperand(Idx);
    966   }
    967 
    968   /// Find the index of the first operand in the
    969   /// operand list that is used to represent the predicate. It returns -1 if
    970   /// none is found.
    971   int findFirstPredOperandIdx() const;
    972 
    973   /// Find the index of the flag word operand that
    974   /// corresponds to operand OpIdx on an inline asm instruction.  Returns -1 if
    975   /// getOperand(OpIdx) does not belong to an inline asm operand group.
    976   ///
    977   /// If GroupNo is not NULL, it will receive the number of the operand group
    978   /// containing OpIdx.
    979   ///
    980   /// The flag operand is an immediate that can be decoded with methods like
    981   /// InlineAsm::hasRegClassConstraint().
    982   ///
    983   int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = nullptr) const;
    984 
    985   /// Compute the static register class constraint for operand OpIdx.
    986   /// For normal instructions, this is derived from the MCInstrDesc.
    987   /// For inline assembly it is derived from the flag words.
    988   ///
    989   /// Returns NULL if the static register class constraint cannot be
    990   /// determined.
    991   ///
    992   const TargetRegisterClass*
    993   getRegClassConstraint(unsigned OpIdx,
    994                         const TargetInstrInfo *TII,
    995                         const TargetRegisterInfo *TRI) const;
    996 
    997   /// \brief Applies the constraints (def/use) implied by this MI on \p Reg to
    998   /// the given \p CurRC.
    999   /// If \p ExploreBundle is set and MI is part of a bundle, all the
   1000   /// instructions inside the bundle will be taken into account. In other words,
   1001   /// this method accumulates all the constraints of the operand of this MI and
   1002   /// the related bundle if MI is a bundle or inside a bundle.
   1003   ///
   1004   /// Returns the register class that satisfies both \p CurRC and the
   1005   /// constraints set by MI. Returns NULL if such a register class does not
   1006   /// exist.
   1007   ///
   1008   /// \pre CurRC must not be NULL.
   1009   const TargetRegisterClass *getRegClassConstraintEffectForVReg(
   1010       unsigned Reg, const TargetRegisterClass *CurRC,
   1011       const TargetInstrInfo *TII, const TargetRegisterInfo *TRI,
   1012       bool ExploreBundle = false) const;
   1013 
   1014   /// \brief Applies the constraints (def/use) implied by the \p OpIdx operand
   1015   /// to the given \p CurRC.
   1016   ///
   1017   /// Returns the register class that satisfies both \p CurRC and the
   1018   /// constraints set by \p OpIdx MI. Returns NULL if such a register class
   1019   /// does not exist.
   1020   ///
   1021   /// \pre CurRC must not be NULL.
   1022   /// \pre The operand at \p OpIdx must be a register.
   1023   const TargetRegisterClass *
   1024   getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC,
   1025                               const TargetInstrInfo *TII,
   1026                               const TargetRegisterInfo *TRI) const;
   1027 
   1028   /// Add a tie between the register operands at DefIdx and UseIdx.
   1029   /// The tie will cause the register allocator to ensure that the two
   1030   /// operands are assigned the same physical register.
   1031   ///
   1032   /// Tied operands are managed automatically for explicit operands in the
   1033   /// MCInstrDesc. This method is for exceptional cases like inline asm.
   1034   void tieOperands(unsigned DefIdx, unsigned UseIdx);
   1035 
   1036   /// Given the index of a tied register operand, find the
   1037   /// operand it is tied to. Defs are tied to uses and vice versa. Returns the
   1038   /// index of the tied operand which must exist.
   1039   unsigned findTiedOperandIdx(unsigned OpIdx) const;
   1040 
   1041   /// Given the index of a register def operand,
   1042   /// check if the register def is tied to a source operand, due to either
   1043   /// two-address elimination or inline assembly constraints. Returns the
   1044   /// first tied use operand index by reference if UseOpIdx is not null.
   1045   bool isRegTiedToUseOperand(unsigned DefOpIdx,
   1046                              unsigned *UseOpIdx = nullptr) const {
   1047     const MachineOperand &MO = getOperand(DefOpIdx);
   1048     if (!MO.isReg() || !MO.isDef() || !MO.isTied())
   1049       return false;
   1050     if (UseOpIdx)
   1051       *UseOpIdx = findTiedOperandIdx(DefOpIdx);
   1052     return true;
   1053   }
   1054 
   1055   /// Return true if the use operand of the specified index is tied to a def
   1056   /// operand. It also returns the def operand index by reference if DefOpIdx
   1057   /// is not null.
   1058   bool isRegTiedToDefOperand(unsigned UseOpIdx,
   1059                              unsigned *DefOpIdx = nullptr) const {
   1060     const MachineOperand &MO = getOperand(UseOpIdx);
   1061     if (!MO.isReg() || !MO.isUse() || !MO.isTied())
   1062       return false;
   1063     if (DefOpIdx)
   1064       *DefOpIdx = findTiedOperandIdx(UseOpIdx);
   1065     return true;
   1066   }
   1067 
   1068   /// Clears kill flags on all operands.
   1069   void clearKillInfo();
   1070 
   1071   /// Replace all occurrences of FromReg with ToReg:SubIdx,
   1072   /// properly composing subreg indices where necessary.
   1073   void substituteRegister(unsigned FromReg, unsigned ToReg, unsigned SubIdx,
   1074                           const TargetRegisterInfo &RegInfo);
   1075 
   1076   /// We have determined MI kills a register. Look for the
   1077   /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
   1078   /// add a implicit operand if it's not found. Returns true if the operand
   1079   /// exists / is added.
   1080   bool addRegisterKilled(unsigned IncomingReg,
   1081                          const TargetRegisterInfo *RegInfo,
   1082                          bool AddIfNotFound = false);
   1083 
   1084   /// Clear all kill flags affecting Reg.  If RegInfo is provided, this includes
   1085   /// all aliasing registers.
   1086   void clearRegisterKills(unsigned Reg, const TargetRegisterInfo *RegInfo);
   1087 
   1088   /// We have determined MI defined a register without a use.
   1089   /// Look for the operand that defines it and mark it as IsDead. If
   1090   /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
   1091   /// true if the operand exists / is added.
   1092   bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo,
   1093                        bool AddIfNotFound = false);
   1094 
   1095   /// Clear all dead flags on operands defining register @p Reg.
   1096   void clearRegisterDeads(unsigned Reg);
   1097 
   1098   /// Mark all subregister defs of register @p Reg with the undef flag.
   1099   /// This function is used when we determined to have a subregister def in an
   1100   /// otherwise undefined super register.
   1101   void setRegisterDefReadUndef(unsigned Reg, bool IsUndef = true);
   1102 
   1103   /// We have determined MI defines a register. Make sure there is an operand
   1104   /// defining Reg.
   1105   void addRegisterDefined(unsigned Reg,
   1106                           const TargetRegisterInfo *RegInfo = nullptr);
   1107 
   1108   /// Mark every physreg used by this instruction as
   1109   /// dead except those in the UsedRegs list.
   1110   ///
   1111   /// On instructions with register mask operands, also add implicit-def
   1112   /// operands for all registers in UsedRegs.
   1113   void setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
   1114                              const TargetRegisterInfo &TRI);
   1115 
   1116   /// Return true if it is safe to move this instruction. If
   1117   /// SawStore is set to true, it means that there is a store (or call) between
   1118   /// the instruction's location and its intended destination.
   1119   bool isSafeToMove(AliasAnalysis *AA, bool &SawStore) const;
   1120 
   1121   /// Return true if this instruction may have an ordered
   1122   /// or volatile memory reference, or if the information describing the memory
   1123   /// reference is not available. Return false if it is known to have no
   1124   /// ordered or volatile memory references.
   1125   bool hasOrderedMemoryRef() const;
   1126 
   1127   /// Return true if this instruction is loading from a
   1128   /// location whose value is invariant across the function.  For example,
   1129   /// loading a value from the constant pool or from the argument area of
   1130   /// a function if it does not change.  This should only return true of *all*
   1131   /// loads the instruction does are invariant (if it does multiple loads).
   1132   bool isInvariantLoad(AliasAnalysis *AA) const;
   1133 
   1134   /// If the specified instruction is a PHI that always merges together the
   1135   /// same virtual register, return the register, otherwise return 0.
   1136   unsigned isConstantValuePHI() const;
   1137 
   1138   /// Return true if this instruction has side effects that are not modeled
   1139   /// by mayLoad / mayStore, etc.
   1140   /// For all instructions, the property is encoded in MCInstrDesc::Flags
   1141   /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is
   1142   /// INLINEASM instruction, in which case the side effect property is encoded
   1143   /// in one of its operands (see InlineAsm::Extra_HasSideEffect).
   1144   ///
   1145   bool hasUnmodeledSideEffects() const;
   1146 
   1147   /// Returns true if it is illegal to fold a load across this instruction.
   1148   bool isLoadFoldBarrier() const;
   1149 
   1150   /// Return true if all the defs of this instruction are dead.
   1151   bool allDefsAreDead() const;
   1152 
   1153   /// Copy implicit register operands from specified
   1154   /// instruction to this instruction.
   1155   void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI);
   1156 
   1157   //
   1158   // Debugging support
   1159   //
   1160   void print(raw_ostream &OS, bool SkipOpers = false) const;
   1161   void print(raw_ostream &OS, ModuleSlotTracker &MST,
   1162              bool SkipOpers = false) const;
   1163   void dump() const;
   1164 
   1165   //===--------------------------------------------------------------------===//
   1166   // Accessors used to build up machine instructions.
   1167 
   1168   /// Add the specified operand to the instruction.  If it is an implicit
   1169   /// operand, it is added to the end of the operand list.  If it is an
   1170   /// explicit operand it is added at the end of the explicit operand list
   1171   /// (before the first implicit operand).
   1172   ///
   1173   /// MF must be the machine function that was used to allocate this
   1174   /// instruction.
   1175   ///
   1176   /// MachineInstrBuilder provides a more convenient interface for creating
   1177   /// instructions and adding operands.
   1178   void addOperand(MachineFunction &MF, const MachineOperand &Op);
   1179 
   1180   /// Add an operand without providing an MF reference. This only works for
   1181   /// instructions that are inserted in a basic block.
   1182   ///
   1183   /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be
   1184   /// preferred.
   1185   void addOperand(const MachineOperand &Op);
   1186 
   1187   /// Replace the instruction descriptor (thus opcode) of
   1188   /// the current instruction with a new one.
   1189   void setDesc(const MCInstrDesc &tid) { MCID = &tid; }
   1190 
   1191   /// Replace current source information with new such.
   1192   /// Avoid using this, the constructor argument is preferable.
   1193   void setDebugLoc(DebugLoc dl) {
   1194     debugLoc = std::move(dl);
   1195     assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
   1196   }
   1197 
   1198   /// Erase an operand from an instruction, leaving it with one
   1199   /// fewer operand than it started with.
   1200   void RemoveOperand(unsigned i);
   1201 
   1202   /// Add a MachineMemOperand to the machine instruction.
   1203   /// This function should be used only occasionally. The setMemRefs function
   1204   /// is the primary method for setting up a MachineInstr's MemRefs list.
   1205   void addMemOperand(MachineFunction &MF, MachineMemOperand *MO);
   1206 
   1207   /// Assign this MachineInstr's memory reference descriptor list.
   1208   /// This does not transfer ownership.
   1209   void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
   1210     setMemRefs(std::make_pair(NewMemRefs, NewMemRefsEnd-NewMemRefs));
   1211   }
   1212 
   1213   /// Assign this MachineInstr's memory reference descriptor list.  First
   1214   /// element in the pair is the begin iterator/pointer to the array; the
   1215   /// second is the number of MemoryOperands.  This does not transfer ownership
   1216   /// of the underlying memory.
   1217   void setMemRefs(std::pair<mmo_iterator, unsigned> NewMemRefs) {
   1218     MemRefs = NewMemRefs.first;
   1219     NumMemRefs = uint8_t(NewMemRefs.second);
   1220     assert(NumMemRefs == NewMemRefs.second &&
   1221            "Too many memrefs - must drop memory operands");
   1222   }
   1223 
   1224   /// Return a set of memrefs (begin iterator, size) which conservatively
   1225   /// describe the memory behavior of both MachineInstrs.  This is appropriate
   1226   /// for use when merging two MachineInstrs into one. This routine does not
   1227   /// modify the memrefs of the this MachineInstr.
   1228   std::pair<mmo_iterator, unsigned> mergeMemRefsWith(const MachineInstr& Other);
   1229 
   1230   /// Clear this MachineInstr's memory reference descriptor list.  This resets
   1231   /// the memrefs to their most conservative state.  This should be used only
   1232   /// as a last resort since it greatly pessimizes our knowledge of the memory
   1233   /// access performed by the instruction.
   1234   void dropMemRefs() {
   1235     MemRefs = nullptr;
   1236     NumMemRefs = 0;
   1237   }
   1238 
   1239   /// Break any tie involving OpIdx.
   1240   void untieRegOperand(unsigned OpIdx) {
   1241     MachineOperand &MO = getOperand(OpIdx);
   1242     if (MO.isReg() && MO.isTied()) {
   1243       getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0;
   1244       MO.TiedTo = 0;
   1245     }
   1246   }
   1247 
   1248   /// Add all implicit def and use operands to this instruction.
   1249   void addImplicitDefUseOperands(MachineFunction &MF);
   1250 
   1251 private:
   1252   /// If this instruction is embedded into a MachineFunction, return the
   1253   /// MachineRegisterInfo object for the current function, otherwise
   1254   /// return null.
   1255   MachineRegisterInfo *getRegInfo();
   1256 
   1257   /// Unlink all of the register operands in this instruction from their
   1258   /// respective use lists.  This requires that the operands already be on their
   1259   /// use lists.
   1260   void RemoveRegOperandsFromUseLists(MachineRegisterInfo&);
   1261 
   1262   /// Add all of the register operands in this instruction from their
   1263   /// respective use lists.  This requires that the operands not be on their
   1264   /// use lists yet.
   1265   void AddRegOperandsToUseLists(MachineRegisterInfo&);
   1266 
   1267   /// Slow path for hasProperty when we're dealing with a bundle.
   1268   bool hasPropertyInBundle(unsigned Mask, QueryType Type) const;
   1269 
   1270   /// \brief Implements the logic of getRegClassConstraintEffectForVReg for the
   1271   /// this MI and the given operand index \p OpIdx.
   1272   /// If the related operand does not constrained Reg, this returns CurRC.
   1273   const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl(
   1274       unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
   1275       const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const;
   1276 };
   1277 
   1278 /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the
   1279 /// instruction rather than by pointer value.
   1280 /// The hashing and equality testing functions ignore definitions so this is
   1281 /// useful for CSE, etc.
   1282 struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> {
   1283   static inline MachineInstr *getEmptyKey() {
   1284     return nullptr;
   1285   }
   1286 
   1287   static inline MachineInstr *getTombstoneKey() {
   1288     return reinterpret_cast<MachineInstr*>(-1);
   1289   }
   1290 
   1291   static unsigned getHashValue(const MachineInstr* const &MI);
   1292 
   1293   static bool isEqual(const MachineInstr* const &LHS,
   1294                       const MachineInstr* const &RHS) {
   1295     if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
   1296         LHS == getEmptyKey() || LHS == getTombstoneKey())
   1297       return LHS == RHS;
   1298     return LHS->isIdenticalTo(*RHS, MachineInstr::IgnoreVRegDefs);
   1299   }
   1300 };
   1301 
   1302 //===----------------------------------------------------------------------===//
   1303 // Debugging Support
   1304 
   1305 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) {
   1306   MI.print(OS);
   1307   return OS;
   1308 }
   1309 
   1310 } // End llvm namespace
   1311 
   1312 #endif
   1313