make machine operands fatter: give each one an up-pointer to the
[oota-llvm.git] / include / llvm / CodeGen / MachineInstr.h
1 //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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
19 #include "llvm/CodeGen/MachineOperand.h"
20
21 namespace llvm {
22
23 class TargetInstrDescriptor;
24 class TargetMachine;
25
26 template <typename T> struct ilist_traits;
27 template <typename T> struct ilist;
28
29 //===----------------------------------------------------------------------===//
30 /// MachineInstr - Representation of each machine instruction.
31 ///
32 class MachineInstr {
33   const TargetInstrDescriptor *TID;     // Instruction descriptor.
34   unsigned short NumImplicitOps;        // Number of implicit operands (which
35                                         // are determined at construction time).
36
37   std::vector<MachineOperand> Operands; // the operands
38   MachineInstr* prev, *next;            // links for our intrusive list
39   MachineBasicBlock* parent;            // pointer to the owning basic block
40
41   // OperandComplete - Return true if it's illegal to add a new operand
42   bool OperandsComplete() const;
43
44   MachineInstr(const MachineInstr&);
45   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
46
47   // Intrusive list support
48   friend struct ilist_traits<MachineInstr>;
49
50 public:
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
56   /// implicit operands.  It reserves space for number of operands specified by
57   /// TargetInstrDescriptor.
58   explicit MachineInstr(const TargetInstrDescriptor &TID, bool NoImp = false);
59
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
68   const MachineBasicBlock* getParent() const { return parent; }
69   MachineBasicBlock* getParent() { return parent; }
70   
71   /// getInstrDescriptor - Returns the target instruction descriptor of this
72   /// MachineInstr.
73   const TargetInstrDescriptor *getInstrDescriptor() const { return TID; }
74
75   /// getOpcode - Returns the opcode of this MachineInstr.
76   ///
77   int getOpcode() const;
78
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   
136   /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
137   /// to two addr elimination.
138   bool isRegReDefinedByTwoAddr(unsigned Reg) const;
139
140   /// 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   }
153   void print(std::ostream &OS, const TargetMachine *TM) const;
154   void print(std::ostream &OS) const;
155   void print(std::ostream *OS) const { if (OS) print(*OS); }
156   void dump() const;
157   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr){
158     minstr.print(os);
159     return os;
160   }
161
162   //===--------------------------------------------------------------------===//
163   // Accessors to add operands when building up machine instructions.
164   //
165   void addOperand(const MachineOperand &Op) {
166     bool isImpReg = Op.isRegister() && Op.isImplicit();
167     assert((isImpReg || !OperandsComplete()) &&
168            "Trying to add an operand to a machine instr that is already done!");
169     if (isImpReg || NumImplicitOps == 0) {// This is true most of the time.
170       Operands.push_back(Op);
171       Operands.back().ParentMI = this;
172     } else {
173       // Insert a real operand before any implicit ones.
174       unsigned OpNo = Operands.size()-NumImplicitOps;
175       Operands.insert(Operands.begin()+OpNo, Op);
176       Operands[OpNo].ParentMI = this;
177     }
178   }
179   
180   //===--------------------------------------------------------------------===//
181   // Accessors used to modify instructions in place.
182   //
183
184   /// setInstrDescriptor - Replace the instruction descriptor (thus opcode) of
185   /// the current instruction with a new one.
186   ///
187   void setInstrDescriptor(const TargetInstrDescriptor &tid) { TID = &tid; }
188
189   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
190   /// fewer operand than it started with.
191   ///
192   void RemoveOperand(unsigned i) {
193     Operands.erase(Operands.begin()+i);
194   }
195 private:
196
197   /// addImplicitDefUseOperands - Add all implicit def and use operands to
198   /// this instruction.
199   void addImplicitDefUseOperands();
200 };
201
202 //===----------------------------------------------------------------------===//
203 // Debugging Support
204
205 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
206
207 } // End llvm namespace
208
209 #endif