blob: 5450e839db2ce3fc27c19560b6ad9f1b0c036f1f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/MachineInstr.h - MachineInstr class --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner84e66db2007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declaration of the MachineInstr class, which is the
11// basic representation for all target dependent machine instructions used by
12// the back end.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CODEGEN_MACHINEINSTR_H
17#define LLVM_CODEGEN_MACHINEINSTR_H
18
Chris Lattnerd37bfbd2007-12-30 04:40:25 +000019#include "llvm/CodeGen/MachineOperand.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020
21namespace llvm {
22
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023class TargetInstrDescriptor;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024
25template <typename T> struct ilist_traits;
26template <typename T> struct ilist;
27
28//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029/// MachineInstr - Representation of each machine instruction.
30///
31class MachineInstr {
32 const TargetInstrDescriptor *TID; // Instruction descriptor.
33 unsigned short NumImplicitOps; // Number of implicit operands (which
34 // are determined at construction time).
35
36 std::vector<MachineOperand> Operands; // the operands
Chris Lattner7ce487f2007-12-31 04:56:33 +000037 MachineInstr *Prev, *Next; // Links for MBB's intrusive list.
38 MachineBasicBlock *Parent; // Pointer to the owning basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40 // OperandComplete - Return true if it's illegal to add a new operand
41 bool OperandsComplete() const;
42
43 MachineInstr(const MachineInstr&);
44 void operator=(const MachineInstr&); // DO NOT IMPLEMENT
45
46 // Intrusive list support
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 friend struct ilist_traits<MachineInstr>;
Chris Lattnere45742f2008-01-01 01:12:31 +000048 friend struct ilist_traits<MachineBasicBlock>;
Chris Lattner7ce487f2007-12-31 04:56:33 +000049 void setParent(MachineBasicBlock *P) { Parent = P; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050public:
51 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
52 /// TID NULL and no operands.
53 MachineInstr();
54
55 /// MachineInstr ctor - This constructor create a MachineInstr and add the
Chris Lattner720b6cf2007-12-30 00:12:25 +000056 /// implicit operands. It reserves space for number of operands specified by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 /// TargetInstrDescriptor.
Evan Chengbdf72b42007-10-13 02:23:01 +000058 explicit MachineInstr(const TargetInstrDescriptor &TID, bool NoImp = false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 /// MachineInstr ctor - Work exactly the same as the ctor above, except that
61 /// the MachineInstr is created and added to the end of the specified basic
62 /// block.
63 ///
64 MachineInstr(MachineBasicBlock *MBB, const TargetInstrDescriptor &TID);
65
66 ~MachineInstr();
67
Chris Lattner7ce487f2007-12-31 04:56:33 +000068 const MachineBasicBlock* getParent() const { return Parent; }
69 MachineBasicBlock* getParent() { return Parent; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
Chris Lattner62327602008-01-07 01:56:04 +000071 /// getDesc - Returns the target instruction descriptor of this
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 /// MachineInstr.
Chris Lattner62327602008-01-07 01:56:04 +000073 const TargetInstrDescriptor *getDesc() const { return TID; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 /// getOpcode - Returns the opcode of this MachineInstr.
76 ///
Dan Gohman5f222be2007-09-14 20:08:19 +000077 int getOpcode() const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 /// Access to explicit operands of the instruction.
80 ///
81 unsigned getNumOperands() const { return Operands.size(); }
82
83 const MachineOperand& getOperand(unsigned i) const {
84 assert(i < getNumOperands() && "getOperand() out of range!");
85 return Operands[i];
86 }
87 MachineOperand& getOperand(unsigned i) {
88 assert(i < getNumOperands() && "getOperand() out of range!");
89 return Operands[i];
90 }
91
92 /// getNumExplicitOperands - Returns the number of non-implicit operands.
93 ///
94 unsigned getNumExplicitOperands() const;
95
96 /// isIdenticalTo - Return true if this instruction is identical to (same
97 /// opcode and same operands as) the specified instruction.
98 bool isIdenticalTo(const MachineInstr *Other) const {
99 if (Other->getOpcode() != getOpcode() ||
100 Other->getNumOperands() != getNumOperands())
101 return false;
102 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
103 if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
104 return false;
105 return true;
106 }
107
108 /// clone - Create a copy of 'this' instruction that is identical in
109 /// all ways except the the instruction has no parent, prev, or next.
110 MachineInstr* clone() const { return new MachineInstr(*this); }
111
112 /// removeFromParent - This method unlinks 'this' from the containing basic
113 /// block, and returns it, but does not delete it.
114 MachineInstr *removeFromParent();
115
116 /// eraseFromParent - This method unlinks 'this' from the containing basic
117 /// block and deletes it.
118 void eraseFromParent() {
119 delete removeFromParent();
120 }
121
122 /// findRegisterUseOperandIdx() - Returns the operand index that is a use of
123 /// the specific register or -1 if it is not found. It further tightening
124 /// the search criteria to a use that kills the register if isKill is true.
125 int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false) const;
126
127 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of
128 /// the specific register or NULL if it is not found.
129 MachineOperand *findRegisterDefOperand(unsigned Reg);
130
131 /// findFirstPredOperandIdx() - Find the index of the first operand in the
132 /// operand list that is used to represent the predicate. It returns -1 if
133 /// none is found.
134 int findFirstPredOperandIdx() const;
135
Evan Cheng687d1082007-10-12 08:50:34 +0000136 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
137 /// to two addr elimination.
138 bool isRegReDefinedByTwoAddr(unsigned Reg) const;
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
141 ///
142 void copyKillDeadInfo(const MachineInstr *MI);
143
144 /// copyPredicates - Copies predicate operand(s) from MI.
145 void copyPredicates(const MachineInstr *MI);
146
147 //
148 // Debugging support
149 //
150 void print(std::ostream *OS, const TargetMachine *TM) const {
151 if (OS) print(*OS, TM);
152 }
Chris Lattner9607bb82007-12-30 21:31:53 +0000153 void print(std::ostream &OS, const TargetMachine *TM = 0) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 void print(std::ostream *OS) const { if (OS) print(*OS); }
155 void dump() const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
157 //===--------------------------------------------------------------------===//
Chris Lattnere45742f2008-01-01 01:12:31 +0000158 // Accessors used to build up machine instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Chris Lattnere45742f2008-01-01 01:12:31 +0000160 /// addOperand - Add the specified operand to the instruction. If it is an
161 /// implicit operand, it is added to the end of the operand list. If it is
162 /// an explicit operand it is added at the end of the explicit operand list
163 /// (before the first implicit operand).
164 void addOperand(const MachineOperand &Op);
165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 /// setInstrDescriptor - Replace the instruction descriptor (thus opcode) of
167 /// the current instruction with a new one.
168 ///
169 void setInstrDescriptor(const TargetInstrDescriptor &tid) { TID = &tid; }
170
171 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
172 /// fewer operand than it started with.
173 ///
Chris Lattnere45742f2008-01-01 01:12:31 +0000174 void RemoveOperand(unsigned i);
175
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176private:
Chris Lattnere45742f2008-01-01 01:12:31 +0000177 /// getRegInfo - If this instruction is embedded into a MachineFunction,
178 /// return the MachineRegisterInfo object for the current function, otherwise
179 /// return null.
180 MachineRegisterInfo *getRegInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 /// addImplicitDefUseOperands - Add all implicit def and use operands to
183 /// this instruction.
184 void addImplicitDefUseOperands();
Chris Lattnere45742f2008-01-01 01:12:31 +0000185
186 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
187 /// this instruction from their respective use lists. This requires that the
188 /// operands already be on their use lists.
189 void RemoveRegOperandsFromUseLists();
190
191 /// AddRegOperandsToUseLists - Add all of the register operands in
192 /// this instruction from their respective use lists. This requires that the
193 /// operands not be on their use lists yet.
194 void AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195};
196
197//===----------------------------------------------------------------------===//
198// Debugging Support
199
Chris Lattner9607bb82007-12-30 21:31:53 +0000200inline std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI) {
201 MI.print(OS);
202 return OS;
203}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
205} // End llvm namespace
206
207#endif