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