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/CodeGen/MemOperand.h"
24 class TargetInstrDesc;
25 class TargetRegisterInfo;
27 template <typename T> struct ilist_traits;
28 template <typename T> struct ilist;
30 //===----------------------------------------------------------------------===//
31 /// MachineInstr - Representation of each machine instruction.
34 const TargetInstrDesc *TID; // Instruction descriptor.
35 unsigned short NumImplicitOps; // Number of implicit operands (which
36 // are determined at construction time).
38 std::vector<MachineOperand> Operands; // the operands
39 std::vector<MemOperand> MemOperands; // information on memory references
40 MachineInstr *Prev, *Next; // Links for MBB's intrusive list.
41 MachineBasicBlock *Parent; // Pointer to the owning basic block.
43 // OperandComplete - Return true if it's illegal to add a new operand
44 bool OperandsComplete() const;
46 MachineInstr(const MachineInstr&);
47 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
49 // Intrusive list support
50 friend struct ilist_traits<MachineInstr>;
51 friend struct ilist_traits<MachineBasicBlock>;
52 void setParent(MachineBasicBlock *P) { Parent = P; }
54 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
55 /// TID NULL and no operands.
58 /// MachineInstr ctor - This constructor create a MachineInstr and add the
59 /// implicit operands. It reserves space for number of operands specified by
61 explicit MachineInstr(const TargetInstrDesc &TID, bool NoImp = false);
63 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
64 /// the MachineInstr is created and added to the end of the specified basic
67 MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &TID);
71 const MachineBasicBlock* getParent() const { return Parent; }
72 MachineBasicBlock* getParent() { return Parent; }
74 /// getDesc - Returns the target instruction descriptor of this
76 const TargetInstrDesc &getDesc() const { return *TID; }
78 /// getOpcode - Returns the opcode of this MachineInstr.
80 int getOpcode() const;
82 /// Access to explicit operands of the instruction.
84 unsigned getNumOperands() const { return Operands.size(); }
86 const MachineOperand& getOperand(unsigned i) const {
87 assert(i < getNumOperands() && "getOperand() out of range!");
90 MachineOperand& getOperand(unsigned i) {
91 assert(i < getNumOperands() && "getOperand() out of range!");
95 /// getNumExplicitOperands - Returns the number of non-implicit operands.
97 unsigned getNumExplicitOperands() const;
99 /// Access to memory operands of the instruction
100 unsigned getNumMemOperands() const { return MemOperands.size(); }
102 const MemOperand& getMemOperand(unsigned i) const {
103 assert(i < getNumMemOperands() && "getMemOperand() out of range!");
104 return MemOperands[i];
106 MemOperand& getMemOperand(unsigned i) {
107 assert(i < getNumMemOperands() && "getMemOperand() out of range!");
108 return MemOperands[i];
111 /// isIdenticalTo - Return true if this instruction is identical to (same
112 /// opcode and same operands as) the specified instruction.
113 bool isIdenticalTo(const MachineInstr *Other) const {
114 if (Other->getOpcode() != getOpcode() ||
115 Other->getNumOperands() != getNumOperands())
117 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
118 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
123 /// clone - Create a copy of 'this' instruction that is identical in
124 /// all ways except the the instruction has no parent, prev, or next.
125 MachineInstr* clone() const { return new MachineInstr(*this); }
127 /// removeFromParent - This method unlinks 'this' from the containing basic
128 /// block, and returns it, but does not delete it.
129 MachineInstr *removeFromParent();
131 /// eraseFromParent - This method unlinks 'this' from the containing basic
132 /// block and deletes it.
133 void eraseFromParent() {
134 delete removeFromParent();
137 /// isDebugLabel - Returns true if the MachineInstr represents a debug label.
139 bool isDebugLabel() const;
141 /// findRegisterUseOperandIdx() - Returns the operand index that is a use of
142 /// the specific register or -1 if it is not found. It further tightening
143 /// the search criteria to a use that kills the register if isKill is true.
144 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false) const;
146 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of
147 /// the specific register or NULL if it is not found.
148 MachineOperand *findRegisterDefOperand(unsigned Reg);
150 /// findFirstPredOperandIdx() - Find the index of the first operand in the
151 /// operand list that is used to represent the predicate. It returns -1 if
153 int findFirstPredOperandIdx() const;
155 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
156 /// to two addr elimination.
157 bool isRegReDefinedByTwoAddr(unsigned Reg) const;
159 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
161 void copyKillDeadInfo(const MachineInstr *MI);
163 /// copyPredicates - Copies predicate operand(s) from MI.
164 void copyPredicates(const MachineInstr *MI);
166 /// addRegisterKilled - We have determined MI kills a register. Look for the
167 /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
168 /// add a implicit operand if it's not found. Returns true if the operand
169 /// exists / is added.
170 bool addRegisterKilled(unsigned IncomingReg,
171 const TargetRegisterInfo *RegInfo,
172 bool AddIfNotFound = false);
174 /// addRegisterDead - We have determined MI defined a register without a use.
175 /// Look for the operand that defines it and mark it as IsDead. If
176 /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
177 /// true if the operand exists / is added.
178 bool addRegisterDead(unsigned IncomingReg, const TargetRegisterInfo *RegInfo,
179 bool AddIfNotFound = false);
181 /// copyKillDeadInfo - copies killed/dead information from one instr to another
182 void copyKillDeadInfo(MachineInstr *OldMI,
183 const TargetRegisterInfo *RegInfo);
188 void print(std::ostream *OS, const TargetMachine *TM) const {
189 if (OS) print(*OS, TM);
191 void print(std::ostream &OS, const TargetMachine *TM = 0) const;
192 void print(std::ostream *OS) const { if (OS) print(*OS); }
195 //===--------------------------------------------------------------------===//
196 // Accessors used to build up machine instructions.
198 /// addOperand - Add the specified operand to the instruction. If it is an
199 /// implicit operand, it is added to the end of the operand list. If it is
200 /// an explicit operand it is added at the end of the explicit operand list
201 /// (before the first implicit operand).
202 void addOperand(const MachineOperand &Op);
204 /// setDesc - Replace the instruction descriptor (thus opcode) of
205 /// the current instruction with a new one.
207 void setDesc(const TargetInstrDesc &tid) { TID = &tid; }
209 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
210 /// fewer operand than it started with.
212 void RemoveOperand(unsigned i);
214 /// addMemOperand - Add a MemOperand to the machine instruction, referencing
215 /// arbitrary storage.
216 void addMemOperand(const MemOperand &MO) {
217 MemOperands.push_back(MO);
221 /// getRegInfo - If this instruction is embedded into a MachineFunction,
222 /// return the MachineRegisterInfo object for the current function, otherwise
224 MachineRegisterInfo *getRegInfo();
226 /// addImplicitDefUseOperands - Add all implicit def and use operands to
227 /// this instruction.
228 void addImplicitDefUseOperands();
230 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
231 /// this instruction from their respective use lists. This requires that the
232 /// operands already be on their use lists.
233 void RemoveRegOperandsFromUseLists();
235 /// AddRegOperandsToUseLists - Add all of the register operands in
236 /// this instruction from their respective use lists. This requires that the
237 /// operands not be on their use lists yet.
238 void AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo);
241 //===----------------------------------------------------------------------===//
244 inline std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI) {
249 } // End llvm namespace