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"
23 class TargetInstrDesc;
26 template <typename T> struct ilist_traits;
27 template <typename T> struct ilist;
29 //===----------------------------------------------------------------------===//
30 /// MachineInstr - Representation of each machine instruction.
33 const TargetInstrDesc *TID; // Instruction descriptor.
34 unsigned short NumImplicitOps; // Number of implicit operands (which
35 // are determined at construction time).
37 std::vector<MachineOperand> Operands; // the operands
38 MachineInstr *Prev, *Next; // Links for MBB's intrusive list.
39 MachineBasicBlock *Parent; // Pointer to the owning basic block.
41 // OperandComplete - Return true if it's illegal to add a new operand
42 bool OperandsComplete() const;
44 MachineInstr(const MachineInstr&);
45 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
47 // Intrusive list support
48 friend struct ilist_traits<MachineInstr>;
49 friend struct ilist_traits<MachineBasicBlock>;
50 void setParent(MachineBasicBlock *P) { Parent = P; }
52 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
53 /// TID NULL and no operands.
56 /// MachineInstr ctor - This constructor create a MachineInstr and add the
57 /// implicit operands. It reserves space for number of operands specified by
59 explicit MachineInstr(const TargetInstrDesc &TID, bool NoImp = false);
61 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
62 /// the MachineInstr is created and added to the end of the specified basic
65 MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &TID);
69 const MachineBasicBlock* getParent() const { return Parent; }
70 MachineBasicBlock* getParent() { return Parent; }
72 /// getDesc - Returns the target instruction descriptor of this
74 const TargetInstrDesc &getDesc() const { return *TID; }
76 /// getOpcode - Returns the opcode of this MachineInstr.
78 int getOpcode() const;
80 /// Access to explicit operands of the instruction.
82 unsigned getNumOperands() const { return Operands.size(); }
84 const MachineOperand& getOperand(unsigned i) const {
85 assert(i < getNumOperands() && "getOperand() out of range!");
88 MachineOperand& getOperand(unsigned i) {
89 assert(i < getNumOperands() && "getOperand() out of range!");
93 /// getNumExplicitOperands - Returns the number of non-implicit operands.
95 unsigned getNumExplicitOperands() const;
97 /// isIdenticalTo - Return true if this instruction is identical to (same
98 /// opcode and same operands as) the specified instruction.
99 bool isIdenticalTo(const MachineInstr *Other) const {
100 if (Other->getOpcode() != getOpcode() ||
101 Other->getNumOperands() != getNumOperands())
103 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
104 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
109 /// clone - Create a copy of 'this' instruction that is identical in
110 /// all ways except the the instruction has no parent, prev, or next.
111 MachineInstr* clone() const { return new MachineInstr(*this); }
113 /// removeFromParent - This method unlinks 'this' from the containing basic
114 /// block, and returns it, but does not delete it.
115 MachineInstr *removeFromParent();
117 /// eraseFromParent - This method unlinks 'this' from the containing basic
118 /// block and deletes it.
119 void eraseFromParent() {
120 delete removeFromParent();
123 /// findRegisterUseOperandIdx() - Returns the operand index that is a use of
124 /// the specific register or -1 if it is not found. It further tightening
125 /// the search criteria to a use that kills the register if isKill is true.
126 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false) const;
128 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of
129 /// the specific register or NULL if it is not found.
130 MachineOperand *findRegisterDefOperand(unsigned Reg);
132 /// findFirstPredOperandIdx() - Find the index of the first operand in the
133 /// operand list that is used to represent the predicate. It returns -1 if
135 int findFirstPredOperandIdx() const;
137 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
138 /// to two addr elimination.
139 bool isRegReDefinedByTwoAddr(unsigned Reg) const;
141 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
143 void copyKillDeadInfo(const MachineInstr *MI);
145 /// copyPredicates - Copies predicate operand(s) from MI.
146 void copyPredicates(const MachineInstr *MI);
148 /// addRegisterKilled - We have determined MI kills a register. Look for the
149 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
150 /// add a implicit operand if it's not found. Returns true if the operand
151 /// exists / is added.
152 bool addRegisterKilled(unsigned IncomingReg, const MRegisterInfo *RegInfo,
153 bool AddIfNotFound = false);
155 /// addRegisterDead - We have determined MI defined a register without a use.
156 /// Look for the operand that defines it and mark it as IsDead. If
157 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
158 /// true if the operand exists / is added.
159 bool addRegisterDead(unsigned IncomingReg, const MRegisterInfo *RegInfo,
160 bool AddIfNotFound = false);
162 /// copyKillDeadInfo - copies killed/dead information from one instr to another
163 void copyKillDeadInfo(MachineInstr *OldMI,
164 const MRegisterInfo *RegInfo);
169 void print(std::ostream *OS, const TargetMachine *TM) const {
170 if (OS) print(*OS, TM);
172 void print(std::ostream &OS, const TargetMachine *TM = 0) const;
173 void print(std::ostream *OS) const { if (OS) print(*OS); }
176 //===--------------------------------------------------------------------===//
177 // Accessors used to build up machine instructions.
179 /// addOperand - Add the specified operand to the instruction. If it is an
180 /// implicit operand, it is added to the end of the operand list. If it is
181 /// an explicit operand it is added at the end of the explicit operand list
182 /// (before the first implicit operand).
183 void addOperand(const MachineOperand &Op);
185 /// setDesc - Replace the instruction descriptor (thus opcode) of
186 /// the current instruction with a new one.
188 void setDesc(const TargetInstrDesc &tid) { TID = &tid; }
190 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
191 /// fewer operand than it started with.
193 void RemoveOperand(unsigned i);
196 /// getRegInfo - If this instruction is embedded into a MachineFunction,
197 /// return the MachineRegisterInfo object for the current function, otherwise
199 MachineRegisterInfo *getRegInfo();
201 /// addImplicitDefUseOperands - Add all implicit def and use operands to
202 /// this instruction.
203 void addImplicitDefUseOperands();
205 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
206 /// this instruction from their respective use lists. This requires that the
207 /// operands already be on their use lists.
208 void RemoveRegOperandsFromUseLists();
210 /// AddRegOperandsToUseLists - Add all of the register operands in
211 /// this instruction from their respective use lists. This requires that the
212 /// operands not be on their use lists yet.
213 void AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo);
216 //===----------------------------------------------------------------------===//
219 inline std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI) {
224 } // End llvm namespace