1 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MachineInstr class, which is the
11 // basic representation for all target dependent machine instructions used by
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_MACHINEINSTR_H
17 #define LLVM_CODEGEN_MACHINEINSTR_H
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/CodeGen/MachineOperand.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/InlineAsm.h"
29 #include "llvm/MC/MCInstrDesc.h"
30 #include "llvm/Support/ArrayRecycler.h"
31 #include "llvm/Target/TargetOpcodes.h"
35 template <typename T> class SmallVectorImpl;
37 class TargetInstrInfo;
38 class TargetRegisterClass;
39 class TargetRegisterInfo;
40 class MachineFunction;
41 class MachineMemOperand;
43 //===----------------------------------------------------------------------===//
44 /// MachineInstr - Representation of each machine instruction.
46 /// This class isn't a POD type, but it must have a trivial destructor. When a
47 /// MachineFunction is deleted, all the contained MachineInstrs are deallocated
48 /// without having their destructor called.
50 class MachineInstr : public ilist_node<MachineInstr> {
52 typedef MachineMemOperand **mmo_iterator;
54 /// Flags to specify different kinds of comments to output in
55 /// assembly code. These flags carry semantic information not
56 /// otherwise easily derivable from the IR text.
64 FrameSetup = 1 << 0, // Instruction is used as a part of
65 // function frame setup code.
66 BundledPred = 1 << 1, // Instruction has bundled predecessors.
67 BundledSucc = 1 << 2 // Instruction has bundled successors.
70 const MCInstrDesc *MCID; // Instruction descriptor.
71 MachineBasicBlock *Parent; // Pointer to the owning basic block.
73 // Operands are allocated by an ArrayRecycler.
74 MachineOperand *Operands; // Pointer to the first operand.
75 unsigned NumOperands; // Number of operands on instruction.
76 typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
77 OperandCapacity CapOperands; // Capacity of the Operands array.
79 uint8_t Flags; // Various bits of additional
80 // information about machine
83 uint8_t AsmPrinterFlags; // Various bits of information used by
84 // the AsmPrinter to emit helpful
85 // comments. This is *not* semantic
86 // information. Do not use this for
87 // anything other than to convey comment
88 // information to AsmPrinter.
90 uint8_t NumMemRefs; // Information on memory references.
93 DebugLoc debugLoc; // Source line information.
95 MachineInstr(const MachineInstr&) LLVM_DELETED_FUNCTION;
96 void operator=(const MachineInstr&) LLVM_DELETED_FUNCTION;
97 // Use MachineFunction::DeleteMachineInstr() instead.
98 ~MachineInstr() LLVM_DELETED_FUNCTION;
100 // Intrusive list support
101 friend struct ilist_traits<MachineInstr>;
102 friend struct ilist_traits<MachineBasicBlock>;
103 void setParent(MachineBasicBlock *P) { Parent = P; }
105 /// MachineInstr ctor - This constructor creates a copy of the given
106 /// MachineInstr in the given MachineFunction.
107 MachineInstr(MachineFunction &, const MachineInstr &);
109 /// MachineInstr ctor - This constructor create a MachineInstr and add the
110 /// implicit operands. It reserves space for number of operands specified by
111 /// MCInstrDesc. An explicit DebugLoc is supplied.
112 MachineInstr(MachineFunction&, const MCInstrDesc &MCID,
113 const DebugLoc dl, bool NoImp = false);
115 // MachineInstrs are pool-allocated and owned by MachineFunction.
116 friend class MachineFunction;
119 const MachineBasicBlock* getParent() const { return Parent; }
120 MachineBasicBlock* getParent() { return Parent; }
122 /// getAsmPrinterFlags - Return the asm printer flags bitvector.
124 uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; }
126 /// clearAsmPrinterFlags - clear the AsmPrinter bitvector
128 void clearAsmPrinterFlags() { AsmPrinterFlags = 0; }
130 /// getAsmPrinterFlag - Return whether an AsmPrinter flag is set.
132 bool getAsmPrinterFlag(CommentFlag Flag) const {
133 return AsmPrinterFlags & Flag;
136 /// setAsmPrinterFlag - Set a flag for the AsmPrinter.
138 void setAsmPrinterFlag(CommentFlag Flag) {
139 AsmPrinterFlags |= (uint8_t)Flag;
142 /// clearAsmPrinterFlag - clear specific AsmPrinter flags
144 void clearAsmPrinterFlag(CommentFlag Flag) {
145 AsmPrinterFlags &= ~Flag;
148 /// getFlags - Return the MI flags bitvector.
149 uint8_t getFlags() const {
153 /// getFlag - Return whether an MI flag is set.
154 bool getFlag(MIFlag Flag) const {
158 /// setFlag - Set a MI flag.
159 void setFlag(MIFlag Flag) {
160 Flags |= (uint8_t)Flag;
163 void setFlags(unsigned flags) {
164 // Filter out the automatically maintained flags.
165 unsigned Mask = BundledPred | BundledSucc;
166 Flags = (Flags & Mask) | (flags & ~Mask);
169 /// clearFlag - Clear a MI flag.
170 void clearFlag(MIFlag Flag) {
171 Flags &= ~((uint8_t)Flag);
174 /// isInsideBundle - Return true if MI is in a bundle (but not the first MI
177 /// A bundle looks like this before it's finalized:
189 /// In this case, the first MI starts a bundle but is not inside a bundle, the
190 /// next 2 MIs are considered "inside" the bundle.
192 /// After a bundle is finalized, it looks like this:
208 /// The first instruction has the special opcode "BUNDLE". It's not "inside"
209 /// a bundle, but the next three MIs are.
210 bool isInsideBundle() const {
211 return getFlag(BundledPred);
214 /// isBundled - Return true if this instruction part of a bundle. This is true
215 /// if either itself or its following instruction is marked "InsideBundle".
216 bool isBundled() const {
217 return isBundledWithPred() || isBundledWithSucc();
220 /// Return true if this instruction is part of a bundle, and it is not the
221 /// first instruction in the bundle.
222 bool isBundledWithPred() const { return getFlag(BundledPred); }
224 /// Return true if this instruction is part of a bundle, and it is not the
225 /// last instruction in the bundle.
226 bool isBundledWithSucc() const { return getFlag(BundledSucc); }
228 /// Bundle this instruction with its predecessor. This can be an unbundled
229 /// instruction, or it can be the first instruction in a bundle.
230 void bundleWithPred();
232 /// Bundle this instruction with its successor. This can be an unbundled
233 /// instruction, or it can be the last instruction in a bundle.
234 void bundleWithSucc();
236 /// Break bundle above this instruction.
237 void unbundleFromPred();
239 /// Break bundle below this instruction.
240 void unbundleFromSucc();
242 /// getDebugLoc - Returns the debug location id of this MachineInstr.
244 DebugLoc getDebugLoc() const { return debugLoc; }
246 /// emitError - Emit an error referring to the source location of this
247 /// instruction. This should only be used for inline assembly that is somehow
248 /// impossible to compile. Other errors should have been handled much
251 /// If this method returns, the caller should try to recover from the error.
253 void emitError(StringRef Msg) const;
255 /// getDesc - Returns the target instruction descriptor of this
257 const MCInstrDesc &getDesc() const { return *MCID; }
259 /// getOpcode - Returns the opcode of this MachineInstr.
261 int getOpcode() const { return MCID->Opcode; }
263 /// Access to explicit operands of the instruction.
265 unsigned getNumOperands() const { return NumOperands; }
267 const MachineOperand& getOperand(unsigned i) const {
268 assert(i < getNumOperands() && "getOperand() out of range!");
271 MachineOperand& getOperand(unsigned i) {
272 assert(i < getNumOperands() && "getOperand() out of range!");
276 /// getNumExplicitOperands - Returns the number of non-implicit operands.
278 unsigned getNumExplicitOperands() const;
280 /// iterator/begin/end - Iterate over all operands of a machine instruction.
281 typedef MachineOperand *mop_iterator;
282 typedef const MachineOperand *const_mop_iterator;
284 mop_iterator operands_begin() { return Operands; }
285 mop_iterator operands_end() { return Operands + NumOperands; }
287 const_mop_iterator operands_begin() const { return Operands; }
288 const_mop_iterator operands_end() const { return Operands + NumOperands; }
290 inline iterator_range<mop_iterator> operands() {
291 return iterator_range<mop_iterator>(operands_begin(), operands_end());
293 inline iterator_range<const_mop_iterator> operands() const {
294 return iterator_range<const_mop_iterator>(operands_begin(), operands_end());
297 /// Access to memory operands of the instruction
298 mmo_iterator memoperands_begin() const { return MemRefs; }
299 mmo_iterator memoperands_end() const { return MemRefs + NumMemRefs; }
300 bool memoperands_empty() const { return NumMemRefs == 0; }
302 inline iterator_range<mmo_iterator> memoperands() {
303 return iterator_range<mmo_iterator>(memoperands_begin(), memoperands_end());
305 inline iterator_range<mmo_iterator> memoperands() const {
306 return iterator_range<mmo_iterator>(memoperands_begin(), memoperands_end());
309 /// hasOneMemOperand - Return true if this instruction has exactly one
310 /// MachineMemOperand.
311 bool hasOneMemOperand() const {
312 return NumMemRefs == 1;
315 /// API for querying MachineInstr properties. They are the same as MCInstrDesc
316 /// queries but they are bundle aware.
319 IgnoreBundle, // Ignore bundles
320 AnyInBundle, // Return true if any instruction in bundle has property
321 AllInBundle // Return true if all instructions in bundle have property
324 /// hasProperty - Return true if the instruction (or in the case of a bundle,
325 /// the instructions inside the bundle) has the specified property.
326 /// The first argument is the property being queried.
327 /// The second argument indicates whether the query should look inside
328 /// instruction bundles.
329 bool hasProperty(unsigned MCFlag, QueryType Type = AnyInBundle) const {
330 // Inline the fast path for unbundled or bundle-internal instructions.
331 if (Type == IgnoreBundle || !isBundled() || isBundledWithPred())
332 return getDesc().getFlags() & (1 << MCFlag);
334 // If this is the first instruction in a bundle, take the slow path.
335 return hasPropertyInBundle(1 << MCFlag, Type);
338 /// isVariadic - Return true if this instruction can have a variable number of
339 /// operands. In this case, the variable operands will be after the normal
340 /// operands but before the implicit definitions and uses (if any are
342 bool isVariadic(QueryType Type = IgnoreBundle) const {
343 return hasProperty(MCID::Variadic, Type);
346 /// hasOptionalDef - Set if this instruction has an optional definition, e.g.
347 /// ARM instructions which can set condition code if 's' bit is set.
348 bool hasOptionalDef(QueryType Type = IgnoreBundle) const {
349 return hasProperty(MCID::HasOptionalDef, Type);
352 /// isPseudo - Return true if this is a pseudo instruction that doesn't
353 /// correspond to a real machine instruction.
355 bool isPseudo(QueryType Type = IgnoreBundle) const {
356 return hasProperty(MCID::Pseudo, Type);
359 bool isReturn(QueryType Type = AnyInBundle) const {
360 return hasProperty(MCID::Return, Type);
363 bool isCall(QueryType Type = AnyInBundle) const {
364 return hasProperty(MCID::Call, Type);
367 /// isBarrier - Returns true if the specified instruction stops control flow
368 /// from executing the instruction immediately following it. Examples include
369 /// unconditional branches and return instructions.
370 bool isBarrier(QueryType Type = AnyInBundle) const {
371 return hasProperty(MCID::Barrier, Type);
374 /// isTerminator - Returns true if this instruction part of the terminator for
375 /// a basic block. Typically this is things like return and branch
378 /// Various passes use this to insert code into the bottom of a basic block,
379 /// but before control flow occurs.
380 bool isTerminator(QueryType Type = AnyInBundle) const {
381 return hasProperty(MCID::Terminator, Type);
384 /// isBranch - Returns true if this is a conditional, unconditional, or
385 /// indirect branch. Predicates below can be used to discriminate between
386 /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
387 /// get more information.
388 bool isBranch(QueryType Type = AnyInBundle) const {
389 return hasProperty(MCID::Branch, Type);
392 /// isIndirectBranch - Return true if this is an indirect branch, such as a
393 /// branch through a register.
394 bool isIndirectBranch(QueryType Type = AnyInBundle) const {
395 return hasProperty(MCID::IndirectBranch, Type);
398 /// isConditionalBranch - Return true if this is a branch which may fall
399 /// through to the next instruction or may transfer control flow to some other
400 /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
401 /// information about this branch.
402 bool isConditionalBranch(QueryType Type = AnyInBundle) const {
403 return isBranch(Type) & !isBarrier(Type) & !isIndirectBranch(Type);
406 /// isUnconditionalBranch - Return true if this is a branch which always
407 /// transfers control flow to some other block. The
408 /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
409 /// about this branch.
410 bool isUnconditionalBranch(QueryType Type = AnyInBundle) const {
411 return isBranch(Type) & isBarrier(Type) & !isIndirectBranch(Type);
414 /// Return true if this instruction has a predicate operand that
415 /// controls execution. It may be set to 'always', or may be set to other
416 /// values. There are various methods in TargetInstrInfo that can be used to
417 /// control and modify the predicate in this instruction.
418 bool isPredicable(QueryType Type = AllInBundle) const {
419 // If it's a bundle than all bundled instructions must be predicable for this
421 return hasProperty(MCID::Predicable, Type);
424 /// isCompare - Return true if this instruction is a comparison.
425 bool isCompare(QueryType Type = IgnoreBundle) const {
426 return hasProperty(MCID::Compare, Type);
429 /// isMoveImmediate - Return true if this instruction is a move immediate
430 /// (including conditional moves) instruction.
431 bool isMoveImmediate(QueryType Type = IgnoreBundle) const {
432 return hasProperty(MCID::MoveImm, Type);
435 /// isBitcast - Return true if this instruction is a bitcast instruction.
437 bool isBitcast(QueryType Type = IgnoreBundle) const {
438 return hasProperty(MCID::Bitcast, Type);
441 /// isSelect - Return true if this instruction is a select instruction.
443 bool isSelect(QueryType Type = IgnoreBundle) const {
444 return hasProperty(MCID::Select, Type);
447 /// isNotDuplicable - Return true if this instruction cannot be safely
448 /// duplicated. For example, if the instruction has a unique labels attached
449 /// to it, duplicating it would cause multiple definition errors.
450 bool isNotDuplicable(QueryType Type = AnyInBundle) const {
451 return hasProperty(MCID::NotDuplicable, Type);
454 /// hasDelaySlot - Returns true if the specified instruction has a delay slot
455 /// which must be filled by the code generator.
456 bool hasDelaySlot(QueryType Type = AnyInBundle) const {
457 return hasProperty(MCID::DelaySlot, Type);
460 /// canFoldAsLoad - Return true for instructions that can be folded as
461 /// memory operands in other instructions. The most common use for this
462 /// is instructions that are simple loads from memory that don't modify
463 /// the loaded value in any way, but it can also be used for instructions
464 /// that can be expressed as constant-pool loads, such as V_SETALLONES
465 /// on x86, to allow them to be folded when it is beneficial.
466 /// This should only be set on instructions that return a value in their
467 /// only virtual register definition.
468 bool canFoldAsLoad(QueryType Type = IgnoreBundle) const {
469 return hasProperty(MCID::FoldableAsLoad, Type);
472 //===--------------------------------------------------------------------===//
473 // Side Effect Analysis
474 //===--------------------------------------------------------------------===//
476 /// mayLoad - Return true if this instruction could possibly read memory.
477 /// Instructions with this flag set are not necessarily simple load
478 /// instructions, they may load a value and modify it, for example.
479 bool mayLoad(QueryType Type = AnyInBundle) const {
481 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
482 if (ExtraInfo & InlineAsm::Extra_MayLoad)
485 return hasProperty(MCID::MayLoad, Type);
489 /// mayStore - Return true if this instruction could possibly modify memory.
490 /// Instructions with this flag set are not necessarily simple store
491 /// instructions, they may store a modified value based on their operands, or
492 /// may not actually modify anything, for example.
493 bool mayStore(QueryType Type = AnyInBundle) const {
495 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
496 if (ExtraInfo & InlineAsm::Extra_MayStore)
499 return hasProperty(MCID::MayStore, Type);
502 //===--------------------------------------------------------------------===//
503 // Flags that indicate whether an instruction can be modified by a method.
504 //===--------------------------------------------------------------------===//
506 /// isCommutable - Return true if this may be a 2- or 3-address
507 /// instruction (of the form "X = op Y, Z, ..."), which produces the same
508 /// result if Y and Z are exchanged. If this flag is set, then the
509 /// TargetInstrInfo::commuteInstruction method may be used to hack on the
512 /// Note that this flag may be set on instructions that are only commutable
513 /// sometimes. In these cases, the call to commuteInstruction will fail.
514 /// Also note that some instructions require non-trivial modification to
516 bool isCommutable(QueryType Type = IgnoreBundle) const {
517 return hasProperty(MCID::Commutable, Type);
520 /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
521 /// which can be changed into a 3-address instruction if needed. Doing this
522 /// transformation can be profitable in the register allocator, because it
523 /// means that the instruction can use a 2-address form if possible, but
524 /// degrade into a less efficient form if the source and dest register cannot
525 /// be assigned to the same register. For example, this allows the x86
526 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
527 /// is the same speed as the shift but has bigger code size.
529 /// If this returns true, then the target must implement the
530 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
531 /// is allowed to fail if the transformation isn't valid for this specific
532 /// instruction (e.g. shl reg, 4 on x86).
534 bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const {
535 return hasProperty(MCID::ConvertibleTo3Addr, Type);
538 /// usesCustomInsertionHook - Return true if this instruction requires
539 /// custom insertion support when the DAG scheduler is inserting it into a
540 /// machine basic block. If this is true for the instruction, it basically
541 /// means that it is a pseudo instruction used at SelectionDAG time that is
542 /// expanded out into magic code by the target when MachineInstrs are formed.
544 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
545 /// is used to insert this into the MachineBasicBlock.
546 bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const {
547 return hasProperty(MCID::UsesCustomInserter, Type);
550 /// hasPostISelHook - Return true if this instruction requires *adjustment*
551 /// after instruction selection by calling a target hook. For example, this
552 /// can be used to fill in ARM 's' optional operand depending on whether
553 /// the conditional flag register is used.
554 bool hasPostISelHook(QueryType Type = IgnoreBundle) const {
555 return hasProperty(MCID::HasPostISelHook, Type);
558 /// isRematerializable - Returns true if this instruction is a candidate for
559 /// remat. This flag is deprecated, please don't use it anymore. If this
560 /// flag is set, the isReallyTriviallyReMaterializable() method is called to
561 /// verify the instruction is really rematable.
562 bool isRematerializable(QueryType Type = AllInBundle) const {
563 // It's only possible to re-mat a bundle if all bundled instructions are
564 // re-materializable.
565 return hasProperty(MCID::Rematerializable, Type);
568 /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
569 /// less) than a move instruction. This is useful during certain types of
570 /// optimizations (e.g., remat during two-address conversion or machine licm)
571 /// where we would like to remat or hoist the instruction, but not if it costs
572 /// more than moving the instruction into the appropriate register. Note, we
573 /// are not marking copies from and to the same register class with this flag.
574 bool isAsCheapAsAMove(QueryType Type = AllInBundle) const {
575 // Only returns true for a bundle if all bundled instructions are cheap.
576 // FIXME: This probably requires a target hook.
577 return hasProperty(MCID::CheapAsAMove, Type);
580 /// hasExtraSrcRegAllocReq - Returns true if this instruction source operands
581 /// have special register allocation requirements that are not captured by the
582 /// operand register classes. e.g. ARM::STRD's two source registers must be an
583 /// even / odd pair, ARM::STM registers have to be in ascending order.
584 /// Post-register allocation passes should not attempt to change allocations
585 /// for sources of instructions with this flag.
586 bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const {
587 return hasProperty(MCID::ExtraSrcRegAllocReq, Type);
590 /// hasExtraDefRegAllocReq - Returns true if this instruction def operands
591 /// have special register allocation requirements that are not captured by the
592 /// operand register classes. e.g. ARM::LDRD's two def registers must be an
593 /// even / odd pair, ARM::LDM registers have to be in ascending order.
594 /// Post-register allocation passes should not attempt to change allocations
595 /// for definitions of instructions with this flag.
596 bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const {
597 return hasProperty(MCID::ExtraDefRegAllocReq, Type);
602 CheckDefs, // Check all operands for equality
603 CheckKillDead, // Check all operands including kill / dead markers
604 IgnoreDefs, // Ignore all definitions
605 IgnoreVRegDefs // Ignore virtual register definitions
608 /// isIdenticalTo - Return true if this instruction is identical to (same
609 /// opcode and same operands as) the specified instruction.
610 bool isIdenticalTo(const MachineInstr *Other,
611 MICheckType Check = CheckDefs) const;
613 /// Unlink 'this' from the containing basic block, and return it without
616 /// This function can not be used on bundled instructions, use
617 /// removeFromBundle() to remove individual instructions from a bundle.
618 MachineInstr *removeFromParent();
620 /// Unlink this instruction from its basic block and return it without
623 /// If the instruction is part of a bundle, the other instructions in the
624 /// bundle remain bundled.
625 MachineInstr *removeFromBundle();
627 /// Unlink 'this' from the containing basic block and delete it.
629 /// If this instruction is the header of a bundle, the whole bundle is erased.
630 /// This function can not be used for instructions inside a bundle, use
631 /// eraseFromBundle() to erase individual bundled instructions.
632 void eraseFromParent();
634 /// Unlink 'this' form its basic block and delete it.
636 /// If the instruction is part of a bundle, the other instructions in the
637 /// bundle remain bundled.
638 void eraseFromBundle();
640 /// isLabel - Returns true if the MachineInstr represents a label.
642 bool isLabel() const {
643 return getOpcode() == TargetOpcode::PROLOG_LABEL ||
644 getOpcode() == TargetOpcode::EH_LABEL ||
645 getOpcode() == TargetOpcode::GC_LABEL;
648 bool isPrologLabel() const {
649 return getOpcode() == TargetOpcode::PROLOG_LABEL;
651 bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
652 bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
653 bool isDebugValue() const { return getOpcode() == TargetOpcode::DBG_VALUE; }
654 /// A DBG_VALUE is indirect iff the first operand is a register and
655 /// the second operand is an immediate.
656 bool isIndirectDebugValue() const {
657 return isDebugValue()
658 && getOperand(0).isReg()
659 && getOperand(1).isImm();
662 bool isPHI() const { return getOpcode() == TargetOpcode::PHI; }
663 bool isKill() const { return getOpcode() == TargetOpcode::KILL; }
664 bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; }
665 bool isInlineAsm() const { return getOpcode() == TargetOpcode::INLINEASM; }
666 bool isMSInlineAsm() const {
667 return getOpcode() == TargetOpcode::INLINEASM && getInlineAsmDialect();
669 bool isStackAligningInlineAsm() const;
670 InlineAsm::AsmDialect getInlineAsmDialect() const;
671 bool isInsertSubreg() const {
672 return getOpcode() == TargetOpcode::INSERT_SUBREG;
674 bool isSubregToReg() const {
675 return getOpcode() == TargetOpcode::SUBREG_TO_REG;
677 bool isRegSequence() const {
678 return getOpcode() == TargetOpcode::REG_SEQUENCE;
680 bool isBundle() const {
681 return getOpcode() == TargetOpcode::BUNDLE;
683 bool isCopy() const {
684 return getOpcode() == TargetOpcode::COPY;
686 bool isFullCopy() const {
687 return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg();
690 /// isCopyLike - Return true if the instruction behaves like a copy.
691 /// This does not include native copy instructions.
692 bool isCopyLike() const {
693 return isCopy() || isSubregToReg();
696 /// isIdentityCopy - Return true is the instruction is an identity copy.
697 bool isIdentityCopy() const {
698 return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() &&
699 getOperand(0).getSubReg() == getOperand(1).getSubReg();
702 /// isTransient - Return true if this is a transient instruction that is
703 /// either very likely to be eliminated during register allocation (such as
704 /// copy-like instructions), or if this instruction doesn't have an
705 /// execution-time cost.
706 bool isTransient() const {
707 switch(getOpcode()) {
708 default: return false;
709 // Copy-like instructions are usually eliminated during register allocation.
710 case TargetOpcode::PHI:
711 case TargetOpcode::COPY:
712 case TargetOpcode::INSERT_SUBREG:
713 case TargetOpcode::SUBREG_TO_REG:
714 case TargetOpcode::REG_SEQUENCE:
715 // Pseudo-instructions that don't produce any real output.
716 case TargetOpcode::IMPLICIT_DEF:
717 case TargetOpcode::KILL:
718 case TargetOpcode::PROLOG_LABEL:
719 case TargetOpcode::EH_LABEL:
720 case TargetOpcode::GC_LABEL:
721 case TargetOpcode::DBG_VALUE:
726 /// Return the number of instructions inside the MI bundle, excluding the
729 /// This is the number of instructions that MachineBasicBlock::iterator
730 /// skips, 0 for unbundled instructions.
731 unsigned getBundleSize() const;
733 /// readsRegister - Return true if the MachineInstr reads the specified
734 /// register. If TargetRegisterInfo is passed, then it also checks if there
735 /// is a read of a super-register.
736 /// This does not count partial redefines of virtual registers as reads:
738 bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI = NULL) const {
739 return findRegisterUseOperandIdx(Reg, false, TRI) != -1;
742 /// readsVirtualRegister - Return true if the MachineInstr reads the specified
743 /// virtual register. Take into account that a partial define is a
744 /// read-modify-write operation.
745 bool readsVirtualRegister(unsigned Reg) const {
746 return readsWritesVirtualRegister(Reg).first;
749 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
750 /// indicating if this instruction reads or writes Reg. This also considers
752 /// If Ops is not null, all operand indices for Reg are added.
753 std::pair<bool,bool> readsWritesVirtualRegister(unsigned Reg,
754 SmallVectorImpl<unsigned> *Ops = 0) const;
756 /// killsRegister - Return true if the MachineInstr kills the specified
757 /// register. If TargetRegisterInfo is passed, then it also checks if there is
758 /// a kill of a super-register.
759 bool killsRegister(unsigned Reg, const TargetRegisterInfo *TRI = NULL) const {
760 return findRegisterUseOperandIdx(Reg, true, TRI) != -1;
763 /// definesRegister - Return true if the MachineInstr fully defines the
764 /// specified register. If TargetRegisterInfo is passed, then it also checks
765 /// if there is a def of a super-register.
766 /// NOTE: It's ignoring subreg indices on virtual registers.
767 bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=NULL) const {
768 return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1;
771 /// modifiesRegister - Return true if the MachineInstr modifies (fully define
772 /// or partially define) the specified register.
773 /// NOTE: It's ignoring subreg indices on virtual registers.
774 bool modifiesRegister(unsigned Reg, const TargetRegisterInfo *TRI) const {
775 return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1;
778 /// registerDefIsDead - Returns true if the register is dead in this machine
779 /// instruction. If TargetRegisterInfo is passed, then it also checks
780 /// if there is a dead def of a super-register.
781 bool registerDefIsDead(unsigned Reg,
782 const TargetRegisterInfo *TRI = NULL) const {
783 return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1;
786 /// findRegisterUseOperandIdx() - Returns the operand index that is a use of
787 /// the specific register or -1 if it is not found. It further tightens
788 /// the search criteria to a use that kills the register if isKill is true.
789 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false,
790 const TargetRegisterInfo *TRI = NULL) const;
792 /// findRegisterUseOperand - Wrapper for findRegisterUseOperandIdx, it returns
793 /// a pointer to the MachineOperand rather than an index.
794 MachineOperand *findRegisterUseOperand(unsigned Reg, bool isKill = false,
795 const TargetRegisterInfo *TRI = NULL) {
796 int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI);
797 return (Idx == -1) ? NULL : &getOperand(Idx);
800 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
801 /// the specified register or -1 if it is not found. If isDead is true, defs
802 /// that are not dead are skipped. If Overlap is true, then it also looks for
803 /// defs that merely overlap the specified register. If TargetRegisterInfo is
804 /// non-null, then it also checks if there is a def of a super-register.
805 /// This may also return a register mask operand when Overlap is true.
806 int findRegisterDefOperandIdx(unsigned Reg,
807 bool isDead = false, bool Overlap = false,
808 const TargetRegisterInfo *TRI = NULL) const;
810 /// findRegisterDefOperand - Wrapper for findRegisterDefOperandIdx, it returns
811 /// a pointer to the MachineOperand rather than an index.
812 MachineOperand *findRegisterDefOperand(unsigned Reg, bool isDead = false,
813 const TargetRegisterInfo *TRI = NULL) {
814 int Idx = findRegisterDefOperandIdx(Reg, isDead, false, TRI);
815 return (Idx == -1) ? NULL : &getOperand(Idx);
818 /// findFirstPredOperandIdx() - Find the index of the first operand in the
819 /// operand list that is used to represent the predicate. It returns -1 if
821 int findFirstPredOperandIdx() const;
823 /// findInlineAsmFlagIdx() - Find the index of the flag word operand that
824 /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if
825 /// getOperand(OpIdx) does not belong to an inline asm operand group.
827 /// If GroupNo is not NULL, it will receive the number of the operand group
828 /// containing OpIdx.
830 /// The flag operand is an immediate that can be decoded with methods like
831 /// InlineAsm::hasRegClassConstraint().
833 int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = 0) const;
835 /// getRegClassConstraint - Compute the static register class constraint for
836 /// operand OpIdx. For normal instructions, this is derived from the
837 /// MCInstrDesc. For inline assembly it is derived from the flag words.
839 /// Returns NULL if the static register classs constraint cannot be
842 const TargetRegisterClass*
843 getRegClassConstraint(unsigned OpIdx,
844 const TargetInstrInfo *TII,
845 const TargetRegisterInfo *TRI) const;
847 /// \brief Applies the constraints (def/use) implied by this MI on \p Reg to
848 /// the given \p CurRC.
849 /// If \p ExploreBundle is set and MI is part of a bundle, all the
850 /// instructions inside the bundle will be taken into account. In other words,
851 /// this method accumulates all the constrains of the operand of this MI and
852 /// the related bundle if MI is a bundle or inside a bundle.
854 /// Returns the register class that statisfies both \p CurRC and the
855 /// constraints set by MI. Returns NULL if such a register class does not
858 /// \pre CurRC must not be NULL.
859 const TargetRegisterClass *getRegClassConstraintEffectForVReg(
860 unsigned Reg, const TargetRegisterClass *CurRC,
861 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI,
862 bool ExploreBundle = false) const;
864 /// \brief Applies the constraints (def/use) implied by the \p OpIdx operand
865 /// to the given \p CurRC.
867 /// Returns the register class that statisfies both \p CurRC and the
868 /// constraints set by \p OpIdx MI. Returns NULL if such a register class
871 /// \pre CurRC must not be NULL.
872 /// \pre The operand at \p OpIdx must be a register.
873 const TargetRegisterClass *
874 getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC,
875 const TargetInstrInfo *TII,
876 const TargetRegisterInfo *TRI) const;
878 /// tieOperands - Add a tie between the register operands at DefIdx and
879 /// UseIdx. The tie will cause the register allocator to ensure that the two
880 /// operands are assigned the same physical register.
882 /// Tied operands are managed automatically for explicit operands in the
883 /// MCInstrDesc. This method is for exceptional cases like inline asm.
884 void tieOperands(unsigned DefIdx, unsigned UseIdx);
886 /// findTiedOperandIdx - Given the index of a tied register operand, find the
887 /// operand it is tied to. Defs are tied to uses and vice versa. Returns the
888 /// index of the tied operand which must exist.
889 unsigned findTiedOperandIdx(unsigned OpIdx) const;
891 /// isRegTiedToUseOperand - Given the index of a register def operand,
892 /// check if the register def is tied to a source operand, due to either
893 /// two-address elimination or inline assembly constraints. Returns the
894 /// first tied use operand index by reference if UseOpIdx is not null.
895 bool isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx = 0) const {
896 const MachineOperand &MO = getOperand(DefOpIdx);
897 if (!MO.isReg() || !MO.isDef() || !MO.isTied())
900 *UseOpIdx = findTiedOperandIdx(DefOpIdx);
904 /// isRegTiedToDefOperand - Return true if the use operand of the specified
905 /// index is tied to an def operand. It also returns the def operand index by
906 /// reference if DefOpIdx is not null.
907 bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx = 0) const {
908 const MachineOperand &MO = getOperand(UseOpIdx);
909 if (!MO.isReg() || !MO.isUse() || !MO.isTied())
912 *DefOpIdx = findTiedOperandIdx(UseOpIdx);
916 /// clearKillInfo - Clears kill flags on all operands.
918 void clearKillInfo();
920 /// substituteRegister - Replace all occurrences of FromReg with ToReg:SubIdx,
921 /// properly composing subreg indices where necessary.
922 void substituteRegister(unsigned FromReg, unsigned ToReg, unsigned SubIdx,
923 const TargetRegisterInfo &RegInfo);
925 /// addRegisterKilled - We have determined MI kills a register. Look for the
926 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
927 /// add a implicit operand if it's not found. Returns true if the operand
928 /// exists / is added.
929 bool addRegisterKilled(unsigned IncomingReg,
930 const TargetRegisterInfo *RegInfo,
931 bool AddIfNotFound = false);
933 /// clearRegisterKills - Clear all kill flags affecting Reg. If RegInfo is
934 /// provided, this includes super-register kills.
935 void clearRegisterKills(unsigned Reg, const TargetRegisterInfo *RegInfo);
937 /// addRegisterDead - We have determined MI defined a register without a use.
938 /// Look for the operand that defines it and mark it as IsDead. If
939 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
940 /// true if the operand exists / is added.
941 bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo,
942 bool AddIfNotFound = false);
944 /// addRegisterDefined - We have determined MI defines a register. Make sure
945 /// there is an operand defining Reg.
946 void addRegisterDefined(unsigned Reg, const TargetRegisterInfo *RegInfo = 0);
948 /// setPhysRegsDeadExcept - Mark every physreg used by this instruction as
949 /// dead except those in the UsedRegs list.
951 /// On instructions with register mask operands, also add implicit-def
952 /// operands for all registers in UsedRegs.
953 void setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
954 const TargetRegisterInfo &TRI);
956 /// isSafeToMove - Return true if it is safe to move this instruction. If
957 /// SawStore is set to true, it means that there is a store (or call) between
958 /// the instruction's location and its intended destination.
959 bool isSafeToMove(const TargetInstrInfo *TII, AliasAnalysis *AA,
960 bool &SawStore) const;
962 /// hasOrderedMemoryRef - Return true if this instruction may have an ordered
963 /// or volatile memory reference, or if the information describing the memory
964 /// reference is not available. Return false if it is known to have no
965 /// ordered or volatile memory references.
966 bool hasOrderedMemoryRef() const;
968 /// isInvariantLoad - Return true if this instruction is loading from a
969 /// location whose value is invariant across the function. For example,
970 /// loading a value from the constant pool or from the argument area of
971 /// a function if it does not change. This should only return true of *all*
972 /// loads the instruction does are invariant (if it does multiple loads).
973 bool isInvariantLoad(AliasAnalysis *AA) const;
975 /// isConstantValuePHI - If the specified instruction is a PHI that always
976 /// merges together the same virtual register, return the register, otherwise
978 unsigned isConstantValuePHI() const;
980 /// hasUnmodeledSideEffects - Return true if this instruction has side
981 /// effects that are not modeled by mayLoad / mayStore, etc.
982 /// For all instructions, the property is encoded in MCInstrDesc::Flags
983 /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is
984 /// INLINEASM instruction, in which case the side effect property is encoded
985 /// in one of its operands (see InlineAsm::Extra_HasSideEffect).
987 bool hasUnmodeledSideEffects() const;
989 /// allDefsAreDead - Return true if all the defs of this instruction are dead.
991 bool allDefsAreDead() const;
993 /// copyImplicitOps - Copy implicit register operands from specified
994 /// instruction to this instruction.
995 void copyImplicitOps(MachineFunction &MF, const MachineInstr *MI);
1000 void print(raw_ostream &OS, const TargetMachine *TM = 0,
1001 bool SkipOpers = false) const;
1004 //===--------------------------------------------------------------------===//
1005 // Accessors used to build up machine instructions.
1007 /// Add the specified operand to the instruction. If it is an implicit
1008 /// operand, it is added to the end of the operand list. If it is an
1009 /// explicit operand it is added at the end of the explicit operand list
1010 /// (before the first implicit operand).
1012 /// MF must be the machine function that was used to allocate this
1015 /// MachineInstrBuilder provides a more convenient interface for creating
1016 /// instructions and adding operands.
1017 void addOperand(MachineFunction &MF, const MachineOperand &Op);
1019 /// Add an operand without providing an MF reference. This only works for
1020 /// instructions that are inserted in a basic block.
1022 /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be
1024 void addOperand(const MachineOperand &Op);
1026 /// setDesc - Replace the instruction descriptor (thus opcode) of
1027 /// the current instruction with a new one.
1029 void setDesc(const MCInstrDesc &tid) { MCID = &tid; }
1031 /// setDebugLoc - Replace current source information with new such.
1032 /// Avoid using this, the constructor argument is preferable.
1034 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
1036 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
1037 /// fewer operand than it started with.
1039 void RemoveOperand(unsigned i);
1041 /// addMemOperand - Add a MachineMemOperand to the machine instruction.
1042 /// This function should be used only occasionally. The setMemRefs function
1043 /// is the primary method for setting up a MachineInstr's MemRefs list.
1044 void addMemOperand(MachineFunction &MF, MachineMemOperand *MO);
1046 /// setMemRefs - Assign this MachineInstr's memory reference descriptor
1047 /// list. This does not transfer ownership.
1048 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
1049 MemRefs = NewMemRefs;
1050 NumMemRefs = uint8_t(NewMemRefsEnd - NewMemRefs);
1051 assert(NumMemRefs == NewMemRefsEnd - NewMemRefs && "Too many memrefs");
1055 /// getRegInfo - If this instruction is embedded into a MachineFunction,
1056 /// return the MachineRegisterInfo object for the current function, otherwise
1058 MachineRegisterInfo *getRegInfo();
1060 /// untieRegOperand - Break any tie involving OpIdx.
1061 void untieRegOperand(unsigned OpIdx) {
1062 MachineOperand &MO = getOperand(OpIdx);
1063 if (MO.isReg() && MO.isTied()) {
1064 getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0;
1069 /// addImplicitDefUseOperands - Add all implicit def and use operands to
1070 /// this instruction.
1071 void addImplicitDefUseOperands(MachineFunction &MF);
1073 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
1074 /// this instruction from their respective use lists. This requires that the
1075 /// operands already be on their use lists.
1076 void RemoveRegOperandsFromUseLists(MachineRegisterInfo&);
1078 /// AddRegOperandsToUseLists - Add all of the register operands in
1079 /// this instruction from their respective use lists. This requires that the
1080 /// operands not be on their use lists yet.
1081 void AddRegOperandsToUseLists(MachineRegisterInfo&);
1083 /// hasPropertyInBundle - Slow path for hasProperty when we're dealing with a
1085 bool hasPropertyInBundle(unsigned Mask, QueryType Type) const;
1087 /// \brief Implements the logic of getRegClassConstraintEffectForVReg for the
1088 /// this MI and the given operand index \p OpIdx.
1089 /// If the related operand does not constrained Reg, this returns CurRC.
1090 const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl(
1091 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1092 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const;
1095 /// MachineInstrExpressionTrait - Special DenseMapInfo traits to compare
1096 /// MachineInstr* by *value* of the instruction rather than by pointer value.
1097 /// The hashing and equality testing functions ignore definitions so this is
1098 /// useful for CSE, etc.
1099 struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> {
1100 static inline MachineInstr *getEmptyKey() {
1104 static inline MachineInstr *getTombstoneKey() {
1105 return reinterpret_cast<MachineInstr*>(-1);
1108 static unsigned getHashValue(const MachineInstr* const &MI);
1110 static bool isEqual(const MachineInstr* const &LHS,
1111 const MachineInstr* const &RHS) {
1112 if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
1113 LHS == getEmptyKey() || LHS == getTombstoneKey())
1115 return LHS->isIdenticalTo(RHS, MachineInstr::IgnoreVRegDefs);
1119 //===----------------------------------------------------------------------===//
1120 // Debugging Support
1122 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) {
1127 } // End llvm namespace