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/CodeGen/MachineOperand.h"
20 #include "llvm/MC/MCInstrDesc.h"
21 #include "llvm/Target/TargetOpcodes.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/ilist.h"
24 #include "llvm/ADT/ilist_node.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/DenseMapInfo.h"
28 #include "llvm/Support/DebugLoc.h"
33 template <typename T> class SmallVectorImpl;
35 class TargetInstrInfo;
36 class TargetRegisterClass;
37 class TargetRegisterInfo;
38 class MachineFunction;
39 class MachineMemOperand;
41 //===----------------------------------------------------------------------===//
42 /// MachineInstr - Representation of each machine instruction.
44 class MachineInstr : public ilist_node<MachineInstr> {
46 typedef MachineMemOperand **mmo_iterator;
48 /// Flags to specify different kinds of comments to output in
49 /// assembly code. These flags carry semantic information not
50 /// otherwise easily derivable from the IR text.
58 FrameSetup = 1 << 0, // Instruction is used as a part of
59 // function frame setup code.
60 InsideBundle = 1 << 1 // Instruction is inside a bundle (not
61 // the first MI in a bundle)
64 const MCInstrDesc *MCID; // Instruction descriptor.
66 uint8_t Flags; // Various bits of additional
67 // information about machine
70 uint8_t AsmPrinterFlags; // Various bits of information used by
71 // the AsmPrinter to emit helpful
72 // comments. This is *not* semantic
73 // information. Do not use this for
74 // anything other than to convey comment
75 // information to AsmPrinter.
77 std::vector<MachineOperand> Operands; // the operands
78 mmo_iterator MemRefs; // information on memory references
79 mmo_iterator MemRefsEnd;
80 MachineBasicBlock *Parent; // Pointer to the owning basic block.
81 DebugLoc debugLoc; // Source line information.
83 MachineInstr(const MachineInstr&); // DO NOT IMPLEMENT
84 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
86 // Intrusive list support
87 friend struct ilist_traits<MachineInstr>;
88 friend struct ilist_traits<MachineBasicBlock>;
89 void setParent(MachineBasicBlock *P) { Parent = P; }
91 /// MachineInstr ctor - This constructor creates a copy of the given
92 /// MachineInstr in the given MachineFunction.
93 MachineInstr(MachineFunction &, const MachineInstr &);
95 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
96 /// MCID NULL and no operands.
99 // The next two constructors have DebugLoc and non-DebugLoc versions;
100 // over time, the non-DebugLoc versions should be phased out and eventually
103 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the
104 /// implicit operands. It reserves space for the number of operands specified
105 /// by the MCInstrDesc. The version with a DebugLoc should be preferred.
106 explicit MachineInstr(const MCInstrDesc &MCID, bool NoImp = false);
108 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
109 /// the MachineInstr is created and added to the end of the specified basic
110 /// block. The version with a DebugLoc should be preferred.
111 MachineInstr(MachineBasicBlock *MBB, const MCInstrDesc &MCID);
113 /// MachineInstr ctor - This constructor create a MachineInstr and add the
114 /// implicit operands. It reserves space for number of operands specified by
115 /// MCInstrDesc. An explicit DebugLoc is supplied.
116 explicit MachineInstr(const MCInstrDesc &MCID, const DebugLoc dl,
119 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
120 /// the MachineInstr is created and added to the end of the specified basic
122 MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
123 const MCInstrDesc &MCID);
127 // MachineInstrs are pool-allocated and owned by MachineFunction.
128 friend class MachineFunction;
131 const MachineBasicBlock* getParent() const { return Parent; }
132 MachineBasicBlock* getParent() { return Parent; }
134 /// getAsmPrinterFlags - Return the asm printer flags bitvector.
136 uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; }
138 /// clearAsmPrinterFlags - clear the AsmPrinter bitvector
140 void clearAsmPrinterFlags() { AsmPrinterFlags = 0; }
142 /// getAsmPrinterFlag - Return whether an AsmPrinter flag is set.
144 bool getAsmPrinterFlag(CommentFlag Flag) const {
145 return AsmPrinterFlags & Flag;
148 /// setAsmPrinterFlag - Set a flag for the AsmPrinter.
150 void setAsmPrinterFlag(CommentFlag Flag) {
151 AsmPrinterFlags |= (uint8_t)Flag;
154 /// clearAsmPrinterFlag - clear specific AsmPrinter flags
156 void clearAsmPrinterFlag(CommentFlag Flag) {
157 AsmPrinterFlags &= ~Flag;
160 /// getFlags - Return the MI flags bitvector.
161 uint8_t getFlags() const {
165 /// getFlag - Return whether an MI flag is set.
166 bool getFlag(MIFlag Flag) const {
170 /// setFlag - Set a MI flag.
171 void setFlag(MIFlag Flag) {
172 Flags |= (uint8_t)Flag;
175 void setFlags(unsigned flags) {
179 /// clearFlag - Clear a MI flag.
180 void clearFlag(MIFlag Flag) {
181 Flags &= ~((uint8_t)Flag);
184 /// isInsideBundle - Return true if MI is in a bundle (but not the first MI
187 /// A bundle looks like this before it's finalized:
199 /// In this case, the first MI starts a bundle but is not inside a bundle, the
200 /// next 2 MIs are considered "inside" the bundle.
202 /// After a bundle is finalized, it looks like this:
218 /// The first instruction has the special opcode "BUNDLE". It's not "inside"
219 /// a bundle, but the next three MIs are.
220 bool isInsideBundle() const {
221 return getFlag(InsideBundle);
224 /// setIsInsideBundle - Set InsideBundle bit.
226 void setIsInsideBundle(bool Val = true) {
228 setFlag(InsideBundle);
230 clearFlag(InsideBundle);
233 /// isBundled - Return true if this instruction part of a bundle. This is true
234 /// if either itself or its following instruction is marked "InsideBundle".
235 bool isBundled() const;
237 /// getBundleStart - If this instruction is inside a bundle return the
238 /// instruction at the start of the bundle. Otherwise just returns the
239 /// instruction itself.
240 MachineInstr* getBundleStart();
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 (unsigned)Operands.size(); }
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 std::vector<MachineOperand>::iterator mop_iterator;
282 typedef std::vector<MachineOperand>::const_iterator const_mop_iterator;
284 mop_iterator operands_begin() { return Operands.begin(); }
285 mop_iterator operands_end() { return Operands.end(); }
287 const_mop_iterator operands_begin() const { return Operands.begin(); }
288 const_mop_iterator operands_end() const { return Operands.end(); }
290 /// Access to memory operands of the instruction
291 mmo_iterator memoperands_begin() const { return MemRefs; }
292 mmo_iterator memoperands_end() const { return MemRefsEnd; }
293 bool memoperands_empty() const { return MemRefsEnd == MemRefs; }
295 /// hasOneMemOperand - Return true if this instruction has exactly one
296 /// MachineMemOperand.
297 bool hasOneMemOperand() const {
298 return MemRefsEnd - MemRefs == 1;
301 /// API for querying MachineInstr properties. They are the same as MCInstrDesc
302 /// queries but they are bundle aware.
305 IgnoreBundle, // Ignore bundles
306 AnyInBundle, // Return true if any instruction in bundle has property
307 AllInBundle // Return true if all instructions in bundle have property
310 /// hasProperty - Return true if the instruction (or in the case of a bundle,
311 /// the instructions inside the bundle) has the specified property.
312 /// The first argument is the property being queried.
313 /// The second argument indicates whether the query should look inside
314 /// instruction bundles.
315 bool hasProperty(unsigned Flag, QueryType Type = AnyInBundle) const;
317 /// isVariadic - Return true if this instruction can have a variable number of
318 /// operands. In this case, the variable operands will be after the normal
319 /// operands but before the implicit definitions and uses (if any are
321 bool isVariadic(QueryType Type = IgnoreBundle) const {
322 return hasProperty(MCID::Variadic, Type);
325 /// hasOptionalDef - Set if this instruction has an optional definition, e.g.
326 /// ARM instructions which can set condition code if 's' bit is set.
327 bool hasOptionalDef(QueryType Type = IgnoreBundle) const {
328 return hasProperty(MCID::HasOptionalDef, Type);
331 /// isPseudo - Return true if this is a pseudo instruction that doesn't
332 /// correspond to a real machine instruction.
334 bool isPseudo(QueryType Type = IgnoreBundle) const {
335 return hasProperty(MCID::Pseudo, Type);
338 bool isReturn(QueryType Type = AnyInBundle) const {
339 return hasProperty(MCID::Return, Type);
342 bool isCall(QueryType Type = AnyInBundle) const {
343 return hasProperty(MCID::Call, Type);
346 /// isBarrier - Returns true if the specified instruction stops control flow
347 /// from executing the instruction immediately following it. Examples include
348 /// unconditional branches and return instructions.
349 bool isBarrier(QueryType Type = AnyInBundle) const {
350 return hasProperty(MCID::Barrier, Type);
353 /// isTerminator - Returns true if this instruction part of the terminator for
354 /// a basic block. Typically this is things like return and branch
357 /// Various passes use this to insert code into the bottom of a basic block,
358 /// but before control flow occurs.
359 bool isTerminator(QueryType Type = AnyInBundle) const {
360 return hasProperty(MCID::Terminator, Type);
363 /// isBranch - Returns true if this is a conditional, unconditional, or
364 /// indirect branch. Predicates below can be used to discriminate between
365 /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
366 /// get more information.
367 bool isBranch(QueryType Type = AnyInBundle) const {
368 return hasProperty(MCID::Branch, Type);
371 /// isIndirectBranch - Return true if this is an indirect branch, such as a
372 /// branch through a register.
373 bool isIndirectBranch(QueryType Type = AnyInBundle) const {
374 return hasProperty(MCID::IndirectBranch, Type);
377 /// isConditionalBranch - Return true if this is a branch which may fall
378 /// through to the next instruction or may transfer control flow to some other
379 /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
380 /// information about this branch.
381 bool isConditionalBranch(QueryType Type = AnyInBundle) const {
382 return isBranch(Type) & !isBarrier(Type) & !isIndirectBranch(Type);
385 /// isUnconditionalBranch - Return true if this is a branch which always
386 /// transfers control flow to some other block. The
387 /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
388 /// about this branch.
389 bool isUnconditionalBranch(QueryType Type = AnyInBundle) const {
390 return isBranch(Type) & isBarrier(Type) & !isIndirectBranch(Type);
393 // isPredicable - Return true if this instruction has a predicate operand that
394 // controls execution. It may be set to 'always', or may be set to other
395 /// values. There are various methods in TargetInstrInfo that can be used to
396 /// control and modify the predicate in this instruction.
397 bool isPredicable(QueryType Type = AllInBundle) const {
398 // If it's a bundle than all bundled instructions must be predicable for this
400 return hasProperty(MCID::Predicable, Type);
403 /// isCompare - Return true if this instruction is a comparison.
404 bool isCompare(QueryType Type = IgnoreBundle) const {
405 return hasProperty(MCID::Compare, Type);
408 /// isMoveImmediate - Return true if this instruction is a move immediate
409 /// (including conditional moves) instruction.
410 bool isMoveImmediate(QueryType Type = IgnoreBundle) const {
411 return hasProperty(MCID::MoveImm, Type);
414 /// isBitcast - Return true if this instruction is a bitcast instruction.
416 bool isBitcast(QueryType Type = IgnoreBundle) const {
417 return hasProperty(MCID::Bitcast, Type);
420 /// isNotDuplicable - Return true if this instruction cannot be safely
421 /// duplicated. For example, if the instruction has a unique labels attached
422 /// to it, duplicating it would cause multiple definition errors.
423 bool isNotDuplicable(QueryType Type = AnyInBundle) const {
424 return hasProperty(MCID::NotDuplicable, Type);
427 /// hasDelaySlot - Returns true if the specified instruction has a delay slot
428 /// which must be filled by the code generator.
429 bool hasDelaySlot(QueryType Type = AnyInBundle) const {
430 return hasProperty(MCID::DelaySlot, Type);
433 /// canFoldAsLoad - Return true for instructions that can be folded as
434 /// memory operands in other instructions. The most common use for this
435 /// is instructions that are simple loads from memory that don't modify
436 /// the loaded value in any way, but it can also be used for instructions
437 /// that can be expressed as constant-pool loads, such as V_SETALLONES
438 /// on x86, to allow them to be folded when it is beneficial.
439 /// This should only be set on instructions that return a value in their
440 /// only virtual register definition.
441 bool canFoldAsLoad(QueryType Type = IgnoreBundle) const {
442 return hasProperty(MCID::FoldableAsLoad, Type);
445 //===--------------------------------------------------------------------===//
446 // Side Effect Analysis
447 //===--------------------------------------------------------------------===//
449 /// mayLoad - Return true if this instruction could possibly read memory.
450 /// Instructions with this flag set are not necessarily simple load
451 /// instructions, they may load a value and modify it, for example.
452 bool mayLoad(QueryType Type = AnyInBundle) const {
453 return hasProperty(MCID::MayLoad, Type);
457 /// mayStore - Return true if this instruction could possibly modify memory.
458 /// Instructions with this flag set are not necessarily simple store
459 /// instructions, they may store a modified value based on their operands, or
460 /// may not actually modify anything, for example.
461 bool mayStore(QueryType Type = AnyInBundle) const {
462 return hasProperty(MCID::MayStore, Type);
465 //===--------------------------------------------------------------------===//
466 // Flags that indicate whether an instruction can be modified by a method.
467 //===--------------------------------------------------------------------===//
469 /// isCommutable - Return true if this may be a 2- or 3-address
470 /// instruction (of the form "X = op Y, Z, ..."), which produces the same
471 /// result if Y and Z are exchanged. If this flag is set, then the
472 /// TargetInstrInfo::commuteInstruction method may be used to hack on the
475 /// Note that this flag may be set on instructions that are only commutable
476 /// sometimes. In these cases, the call to commuteInstruction will fail.
477 /// Also note that some instructions require non-trivial modification to
479 bool isCommutable(QueryType Type = IgnoreBundle) const {
480 return hasProperty(MCID::Commutable, Type);
483 /// isConvertibleTo3Addr - Return true if this is a 2-address instruction
484 /// which can be changed into a 3-address instruction if needed. Doing this
485 /// transformation can be profitable in the register allocator, because it
486 /// means that the instruction can use a 2-address form if possible, but
487 /// degrade into a less efficient form if the source and dest register cannot
488 /// be assigned to the same register. For example, this allows the x86
489 /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
490 /// is the same speed as the shift but has bigger code size.
492 /// If this returns true, then the target must implement the
493 /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
494 /// is allowed to fail if the transformation isn't valid for this specific
495 /// instruction (e.g. shl reg, 4 on x86).
497 bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const {
498 return hasProperty(MCID::ConvertibleTo3Addr, Type);
501 /// usesCustomInsertionHook - Return true if this instruction requires
502 /// custom insertion support when the DAG scheduler is inserting it into a
503 /// machine basic block. If this is true for the instruction, it basically
504 /// means that it is a pseudo instruction used at SelectionDAG time that is
505 /// expanded out into magic code by the target when MachineInstrs are formed.
507 /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
508 /// is used to insert this into the MachineBasicBlock.
509 bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const {
510 return hasProperty(MCID::UsesCustomInserter, Type);
513 /// hasPostISelHook - Return true if this instruction requires *adjustment*
514 /// after instruction selection by calling a target hook. For example, this
515 /// can be used to fill in ARM 's' optional operand depending on whether
516 /// the conditional flag register is used.
517 bool hasPostISelHook(QueryType Type = IgnoreBundle) const {
518 return hasProperty(MCID::HasPostISelHook, Type);
521 /// isRematerializable - Returns true if this instruction is a candidate for
522 /// remat. This flag is deprecated, please don't use it anymore. If this
523 /// flag is set, the isReallyTriviallyReMaterializable() method is called to
524 /// verify the instruction is really rematable.
525 bool isRematerializable(QueryType Type = AllInBundle) const {
526 // It's only possible to re-mat a bundle if all bundled instructions are
527 // re-materializable.
528 return hasProperty(MCID::Rematerializable, Type);
531 /// isAsCheapAsAMove - Returns true if this instruction has the same cost (or
532 /// less) than a move instruction. This is useful during certain types of
533 /// optimizations (e.g., remat during two-address conversion or machine licm)
534 /// where we would like to remat or hoist the instruction, but not if it costs
535 /// more than moving the instruction into the appropriate register. Note, we
536 /// are not marking copies from and to the same register class with this flag.
537 bool isAsCheapAsAMove(QueryType Type = AllInBundle) const {
538 // Only returns true for a bundle if all bundled instructions are cheap.
539 // FIXME: This probably requires a target hook.
540 return hasProperty(MCID::CheapAsAMove, Type);
543 /// hasExtraSrcRegAllocReq - Returns true if this instruction source operands
544 /// have special register allocation requirements that are not captured by the
545 /// operand register classes. e.g. ARM::STRD's two source registers must be an
546 /// even / odd pair, ARM::STM registers have to be in ascending order.
547 /// Post-register allocation passes should not attempt to change allocations
548 /// for sources of instructions with this flag.
549 bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const {
550 return hasProperty(MCID::ExtraSrcRegAllocReq, Type);
553 /// hasExtraDefRegAllocReq - Returns true if this instruction def operands
554 /// have special register allocation requirements that are not captured by the
555 /// operand register classes. e.g. ARM::LDRD's two def registers must be an
556 /// even / odd pair, ARM::LDM registers have to be in ascending order.
557 /// Post-register allocation passes should not attempt to change allocations
558 /// for definitions of instructions with this flag.
559 bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const {
560 return hasProperty(MCID::ExtraDefRegAllocReq, Type);
565 CheckDefs, // Check all operands for equality
566 CheckKillDead, // Check all operands including kill / dead markers
567 IgnoreDefs, // Ignore all definitions
568 IgnoreVRegDefs // Ignore virtual register definitions
571 /// isIdenticalTo - Return true if this instruction is identical to (same
572 /// opcode and same operands as) the specified instruction.
573 bool isIdenticalTo(const MachineInstr *Other,
574 MICheckType Check = CheckDefs) const;
576 /// removeFromParent - This method unlinks 'this' from the containing basic
577 /// block, and returns it, but does not delete it.
578 MachineInstr *removeFromParent();
580 /// eraseFromParent - This method unlinks 'this' from the containing basic
581 /// block and deletes it.
582 void eraseFromParent();
584 /// isLabel - Returns true if the MachineInstr represents a label.
586 bool isLabel() const {
587 return getOpcode() == TargetOpcode::PROLOG_LABEL ||
588 getOpcode() == TargetOpcode::EH_LABEL ||
589 getOpcode() == TargetOpcode::GC_LABEL;
592 bool isPrologLabel() const {
593 return getOpcode() == TargetOpcode::PROLOG_LABEL;
595 bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
596 bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
597 bool isDebugValue() const { return getOpcode() == TargetOpcode::DBG_VALUE; }
599 bool isPHI() const { return getOpcode() == TargetOpcode::PHI; }
600 bool isKill() const { return getOpcode() == TargetOpcode::KILL; }
601 bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; }
602 bool isInlineAsm() const { return getOpcode() == TargetOpcode::INLINEASM; }
603 bool isStackAligningInlineAsm() const;
604 bool isInsertSubreg() const {
605 return getOpcode() == TargetOpcode::INSERT_SUBREG;
607 bool isSubregToReg() const {
608 return getOpcode() == TargetOpcode::SUBREG_TO_REG;
610 bool isRegSequence() const {
611 return getOpcode() == TargetOpcode::REG_SEQUENCE;
613 bool isBundle() const {
614 return getOpcode() == TargetOpcode::BUNDLE;
616 bool isCopy() const {
617 return getOpcode() == TargetOpcode::COPY;
619 bool isFullCopy() const {
620 return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg();
623 /// isCopyLike - Return true if the instruction behaves like a copy.
624 /// This does not include native copy instructions.
625 bool isCopyLike() const {
626 return isCopy() || isSubregToReg();
629 /// isIdentityCopy - Return true is the instruction is an identity copy.
630 bool isIdentityCopy() const {
631 return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() &&
632 getOperand(0).getSubReg() == getOperand(1).getSubReg();
635 /// getBundleSize - Return the number of instructions inside the MI bundle.
636 unsigned getBundleSize() const;
638 /// readsRegister - Return true if the MachineInstr reads the specified
639 /// register. If TargetRegisterInfo is passed, then it also checks if there
640 /// is a read of a super-register.
641 /// This does not count partial redefines of virtual registers as reads:
643 bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI = NULL) const {
644 return findRegisterUseOperandIdx(Reg, false, TRI) != -1;
647 /// readsVirtualRegister - Return true if the MachineInstr reads the specified
648 /// virtual register. Take into account that a partial define is a
649 /// read-modify-write operation.
650 bool readsVirtualRegister(unsigned Reg) const {
651 return readsWritesVirtualRegister(Reg).first;
654 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
655 /// indicating if this instruction reads or writes Reg. This also considers
657 /// If Ops is not null, all operand indices for Reg are added.
658 std::pair<bool,bool> readsWritesVirtualRegister(unsigned Reg,
659 SmallVectorImpl<unsigned> *Ops = 0) const;
661 /// killsRegister - Return true if the MachineInstr kills the specified
662 /// register. If TargetRegisterInfo is passed, then it also checks if there is
663 /// a kill of a super-register.
664 bool killsRegister(unsigned Reg, const TargetRegisterInfo *TRI = NULL) const {
665 return findRegisterUseOperandIdx(Reg, true, TRI) != -1;
668 /// definesRegister - Return true if the MachineInstr fully defines the
669 /// specified register. If TargetRegisterInfo is passed, then it also checks
670 /// if there is a def of a super-register.
671 /// NOTE: It's ignoring subreg indices on virtual registers.
672 bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=NULL) const {
673 return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1;
676 /// modifiesRegister - Return true if the MachineInstr modifies (fully define
677 /// or partially define) the specified register.
678 /// NOTE: It's ignoring subreg indices on virtual registers.
679 bool modifiesRegister(unsigned Reg, const TargetRegisterInfo *TRI) const {
680 return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1;
683 /// registerDefIsDead - Returns true if the register is dead in this machine
684 /// instruction. If TargetRegisterInfo is passed, then it also checks
685 /// if there is a dead def of a super-register.
686 bool registerDefIsDead(unsigned Reg,
687 const TargetRegisterInfo *TRI = NULL) const {
688 return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1;
691 /// findRegisterUseOperandIdx() - Returns the operand index that is a use of
692 /// the specific register or -1 if it is not found. It further tightens
693 /// the search criteria to a use that kills the register if isKill is true.
694 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false,
695 const TargetRegisterInfo *TRI = NULL) const;
697 /// findRegisterUseOperand - Wrapper for findRegisterUseOperandIdx, it returns
698 /// a pointer to the MachineOperand rather than an index.
699 MachineOperand *findRegisterUseOperand(unsigned Reg, bool isKill = false,
700 const TargetRegisterInfo *TRI = NULL) {
701 int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI);
702 return (Idx == -1) ? NULL : &getOperand(Idx);
705 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
706 /// the specified register or -1 if it is not found. If isDead is true, defs
707 /// that are not dead are skipped. If Overlap is true, then it also looks for
708 /// defs that merely overlap the specified register. If TargetRegisterInfo is
709 /// non-null, then it also checks if there is a def of a super-register.
710 /// This may also return a register mask operand when Overlap is true.
711 int findRegisterDefOperandIdx(unsigned Reg,
712 bool isDead = false, bool Overlap = false,
713 const TargetRegisterInfo *TRI = NULL) const;
715 /// findRegisterDefOperand - Wrapper for findRegisterDefOperandIdx, it returns
716 /// a pointer to the MachineOperand rather than an index.
717 MachineOperand *findRegisterDefOperand(unsigned Reg, bool isDead = false,
718 const TargetRegisterInfo *TRI = NULL) {
719 int Idx = findRegisterDefOperandIdx(Reg, isDead, false, TRI);
720 return (Idx == -1) ? NULL : &getOperand(Idx);
723 /// findFirstPredOperandIdx() - Find the index of the first operand in the
724 /// operand list that is used to represent the predicate. It returns -1 if
726 int findFirstPredOperandIdx() const;
728 /// findInlineAsmFlagIdx() - Find the index of the flag word operand that
729 /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if
730 /// getOperand(OpIdx) does not belong to an inline asm operand group.
732 /// If GroupNo is not NULL, it will receive the number of the operand group
733 /// containing OpIdx.
735 /// The flag operand is an immediate that can be decoded with methods like
736 /// InlineAsm::hasRegClassConstraint().
738 int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = 0) const;
740 /// getRegClassConstraint - Compute the static register class constraint for
741 /// operand OpIdx. For normal instructions, this is derived from the
742 /// MCInstrDesc. For inline assembly it is derived from the flag words.
744 /// Returns NULL if the static register classs constraint cannot be
747 const TargetRegisterClass*
748 getRegClassConstraint(unsigned OpIdx,
749 const TargetInstrInfo *TII,
750 const TargetRegisterInfo *TRI) const;
752 /// isRegTiedToUseOperand - Given the index of a register def operand,
753 /// check if the register def is tied to a source operand, due to either
754 /// two-address elimination or inline assembly constraints. Returns the
755 /// first tied use operand index by reference if UseOpIdx is not null.
756 bool isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx = 0) const;
758 /// isRegTiedToDefOperand - Return true if the use operand of the specified
759 /// index is tied to an def operand. It also returns the def operand index by
760 /// reference if DefOpIdx is not null.
761 bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx = 0) const;
763 /// clearKillInfo - Clears kill flags on all operands.
765 void clearKillInfo();
767 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
769 void copyKillDeadInfo(const MachineInstr *MI);
771 /// copyPredicates - Copies predicate operand(s) from MI.
772 void copyPredicates(const MachineInstr *MI);
774 /// substituteRegister - Replace all occurrences of FromReg with ToReg:SubIdx,
775 /// properly composing subreg indices where necessary.
776 void substituteRegister(unsigned FromReg, unsigned ToReg, unsigned SubIdx,
777 const TargetRegisterInfo &RegInfo);
779 /// addRegisterKilled - We have determined MI kills a register. Look for the
780 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
781 /// add a implicit operand if it's not found. Returns true if the operand
782 /// exists / is added.
783 bool addRegisterKilled(unsigned IncomingReg,
784 const TargetRegisterInfo *RegInfo,
785 bool AddIfNotFound = false);
787 /// clearRegisterKills - Clear all kill flags affecting Reg. If RegInfo is
788 /// provided, this includes super-register kills.
789 void clearRegisterKills(unsigned Reg, const TargetRegisterInfo *RegInfo);
791 /// addRegisterDead - We have determined MI defined a register without a use.
792 /// Look for the operand that defines it and mark it as IsDead. If
793 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
794 /// true if the operand exists / is added.
795 bool addRegisterDead(unsigned IncomingReg, const TargetRegisterInfo *RegInfo,
796 bool AddIfNotFound = false);
798 /// addRegisterDefined - We have determined MI defines a register. Make sure
799 /// there is an operand defining Reg.
800 void addRegisterDefined(unsigned IncomingReg,
801 const TargetRegisterInfo *RegInfo = 0);
803 /// setPhysRegsDeadExcept - Mark every physreg used by this instruction as
804 /// dead except those in the UsedRegs list.
806 /// On instructions with register mask operands, also add implicit-def
807 /// operands for all registers in UsedRegs.
808 void setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
809 const TargetRegisterInfo &TRI);
811 /// isSafeToMove - Return true if it is safe to move this instruction. If
812 /// SawStore is set to true, it means that there is a store (or call) between
813 /// the instruction's location and its intended destination.
814 bool isSafeToMove(const TargetInstrInfo *TII, AliasAnalysis *AA,
815 bool &SawStore) const;
817 /// isSafeToReMat - Return true if it's safe to rematerialize the specified
818 /// instruction which defined the specified register instead of copying it.
819 bool isSafeToReMat(const TargetInstrInfo *TII, AliasAnalysis *AA,
820 unsigned DstReg) const;
822 /// hasVolatileMemoryRef - Return true if this instruction may have a
823 /// volatile memory reference, or if the information describing the
824 /// memory reference is not available. Return false if it is known to
825 /// have no volatile memory references.
826 bool hasVolatileMemoryRef() const;
828 /// isInvariantLoad - Return true if this instruction is loading from a
829 /// location whose value is invariant across the function. For example,
830 /// loading a value from the constant pool or from the argument area of
831 /// a function if it does not change. This should only return true of *all*
832 /// loads the instruction does are invariant (if it does multiple loads).
833 bool isInvariantLoad(AliasAnalysis *AA) const;
835 /// isConstantValuePHI - If the specified instruction is a PHI that always
836 /// merges together the same virtual register, return the register, otherwise
838 unsigned isConstantValuePHI() const;
840 /// hasUnmodeledSideEffects - Return true if this instruction has side
841 /// effects that are not modeled by mayLoad / mayStore, etc.
842 /// For all instructions, the property is encoded in MCInstrDesc::Flags
843 /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is
844 /// INLINEASM instruction, in which case the side effect property is encoded
845 /// in one of its operands (see InlineAsm::Extra_HasSideEffect).
847 bool hasUnmodeledSideEffects() const;
849 /// allDefsAreDead - Return true if all the defs of this instruction are dead.
851 bool allDefsAreDead() const;
853 /// copyImplicitOps - Copy implicit register operands from specified
854 /// instruction to this instruction.
855 void copyImplicitOps(const MachineInstr *MI);
860 void print(raw_ostream &OS, const TargetMachine *TM = 0) const;
863 //===--------------------------------------------------------------------===//
864 // Accessors used to build up machine instructions.
866 /// addOperand - Add the specified operand to the instruction. If it is an
867 /// implicit operand, it is added to the end of the operand list. If it is
868 /// an explicit operand it is added at the end of the explicit operand list
869 /// (before the first implicit operand).
870 void addOperand(const MachineOperand &Op);
872 /// setDesc - Replace the instruction descriptor (thus opcode) of
873 /// the current instruction with a new one.
875 void setDesc(const MCInstrDesc &tid) { MCID = &tid; }
877 /// setDebugLoc - Replace current source information with new such.
878 /// Avoid using this, the constructor argument is preferable.
880 void setDebugLoc(const DebugLoc dl) { debugLoc = dl; }
882 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
883 /// fewer operand than it started with.
885 void RemoveOperand(unsigned i);
887 /// addMemOperand - Add a MachineMemOperand to the machine instruction.
888 /// This function should be used only occasionally. The setMemRefs function
889 /// is the primary method for setting up a MachineInstr's MemRefs list.
890 void addMemOperand(MachineFunction &MF, MachineMemOperand *MO);
892 /// setMemRefs - Assign this MachineInstr's memory reference descriptor
893 /// list. This does not transfer ownership.
894 void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
895 MemRefs = NewMemRefs;
896 MemRefsEnd = NewMemRefsEnd;
900 /// getRegInfo - If this instruction is embedded into a MachineFunction,
901 /// return the MachineRegisterInfo object for the current function, otherwise
903 MachineRegisterInfo *getRegInfo();
905 /// addImplicitDefUseOperands - Add all implicit def and use operands to
906 /// this instruction.
907 void addImplicitDefUseOperands();
909 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
910 /// this instruction from their respective use lists. This requires that the
911 /// operands already be on their use lists.
912 void RemoveRegOperandsFromUseLists();
914 /// AddRegOperandsToUseLists - Add all of the register operands in
915 /// this instruction from their respective use lists. This requires that the
916 /// operands not be on their use lists yet.
917 void AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo);
920 /// MachineInstrExpressionTrait - Special DenseMapInfo traits to compare
921 /// MachineInstr* by *value* of the instruction rather than by pointer value.
922 /// The hashing and equality testing functions ignore definitions so this is
923 /// useful for CSE, etc.
924 struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> {
925 static inline MachineInstr *getEmptyKey() {
929 static inline MachineInstr *getTombstoneKey() {
930 return reinterpret_cast<MachineInstr*>(-1);
933 static unsigned getHashValue(const MachineInstr* const &MI);
935 static bool isEqual(const MachineInstr* const &LHS,
936 const MachineInstr* const &RHS) {
937 if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
938 LHS == getEmptyKey() || LHS == getTombstoneKey())
940 return LHS->isIdenticalTo(RHS, MachineInstr::IgnoreVRegDefs);
944 //===----------------------------------------------------------------------===//
947 inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) {
952 } // End llvm namespace