move MachineFrameInfo::CreateFixedObject out of line, give MachineFrameInfo
[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 TargetInstrDesc;
24 class MRegisterInfo;
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 TargetInstrDesc *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 MBB's 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   friend struct ilist_traits<MachineBasicBlock>;
50   void setParent(MachineBasicBlock *P) { Parent = P; }
51 public:
52   /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
53   /// TID NULL and no operands.
54   MachineInstr();
55
56   /// MachineInstr ctor - This constructor create a MachineInstr and add the
57   /// implicit operands.  It reserves space for number of operands specified by
58   /// TargetInstrDesc.
59   explicit MachineInstr(const TargetInstrDesc &TID, bool NoImp = false);
60
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
63   /// block.
64   ///
65   MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &TID);
66
67   ~MachineInstr();
68
69   const MachineBasicBlock* getParent() const { return Parent; }
70   MachineBasicBlock* getParent() { return Parent; }
71   
72   /// getDesc - Returns the target instruction descriptor of this
73   /// MachineInstr.
74   const TargetInstrDesc &getDesc() const { return *TID; }
75
76   /// getOpcode - Returns the opcode of this MachineInstr.
77   ///
78   int getOpcode() const;
79
80   /// Access to explicit operands of the instruction.
81   ///
82   unsigned getNumOperands() const { return Operands.size(); }
83
84   const MachineOperand& getOperand(unsigned i) const {
85     assert(i < getNumOperands() && "getOperand() out of range!");
86     return Operands[i];
87   }
88   MachineOperand& getOperand(unsigned i) {
89     assert(i < getNumOperands() && "getOperand() out of range!");
90     return Operands[i];
91   }
92
93   /// getNumExplicitOperands - Returns the number of non-implicit operands.
94   ///
95   unsigned getNumExplicitOperands() const;
96   
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())
102       return false;
103     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
104       if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
105         return false;
106     return true;
107   }
108
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); }
112   
113   /// removeFromParent - This method unlinks 'this' from the containing basic
114   /// block, and returns it, but does not delete it.
115   MachineInstr *removeFromParent();
116   
117   /// eraseFromParent - This method unlinks 'this' from the containing basic
118   /// block and deletes it.
119   void eraseFromParent() {
120     delete removeFromParent();
121   }
122
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;
127   
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);
131
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
134   /// none is found.
135   int findFirstPredOperandIdx() const;
136   
137   /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due
138   /// to two addr elimination.
139   bool isRegReDefinedByTwoAddr(unsigned Reg) const;
140
141   /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
142   ///
143   void copyKillDeadInfo(const MachineInstr *MI);
144
145   /// copyPredicates - Copies predicate operand(s) from MI.
146   void copyPredicates(const MachineInstr *MI);
147
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);
154   
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);
161
162   /// copyKillDeadInfo - copies killed/dead information from one instr to another
163   void copyKillDeadInfo(MachineInstr *OldMI,
164                         const MRegisterInfo *RegInfo);
165
166   //
167   // Debugging support
168   //
169   void print(std::ostream *OS, const TargetMachine *TM) const {
170     if (OS) print(*OS, TM);
171   }
172   void print(std::ostream &OS, const TargetMachine *TM = 0) const;
173   void print(std::ostream *OS) const { if (OS) print(*OS); }
174   void dump() const;
175
176   //===--------------------------------------------------------------------===//
177   // Accessors used to build up machine instructions.
178
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);
184   
185   /// setDesc - Replace the instruction descriptor (thus opcode) of
186   /// the current instruction with a new one.
187   ///
188   void setDesc(const TargetInstrDesc &tid) { TID = &tid; }
189
190   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
191   /// fewer operand than it started with.
192   ///
193   void RemoveOperand(unsigned i);
194
195 private:
196   /// getRegInfo - If this instruction is embedded into a MachineFunction,
197   /// return the MachineRegisterInfo object for the current function, otherwise
198   /// return null.
199   MachineRegisterInfo *getRegInfo();
200
201   /// addImplicitDefUseOperands - Add all implicit def and use operands to
202   /// this instruction.
203   void addImplicitDefUseOperands();
204   
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();
209   
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);
214 };
215
216 //===----------------------------------------------------------------------===//
217 // Debugging Support
218
219 inline std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI) {
220   MI.print(OS);
221   return OS;
222 }
223
224 } // End llvm namespace
225
226 #endif