0cf822aa0e85104c979de514fbde282b14aeb474
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/ADT/iterator"
20 #include "llvm/Support/DataTypes.h"
21 #include <vector>
22 #include <cassert>
23
24 namespace llvm {
25
26 class Value;
27 class Function;
28 class MachineBasicBlock;
29 class TargetMachine;
30 class GlobalValue;
31
32 template <typename T> struct ilist_traits;
33 template <typename T> struct ilist;
34
35 typedef short MachineOpCode;
36
37 //===----------------------------------------------------------------------===//
38 // class MachineOperand
39 //
40 //   Representation of each machine instruction operand.
41 //
42 struct MachineOperand {
43 private:
44   // Bit fields of the flags variable used for different operand properties
45   enum {
46     DEFFLAG     = 0x01,       // this is a def of the operand
47     USEFLAG     = 0x02,       // this is a use of the operand
48   };
49
50 public:
51   // UseType - This enum describes how the machine operand is used by
52   // the instruction. Note that the MachineInstr/Operator class
53   // currently uses bool arguments to represent this information
54   // instead of an enum.  Eventually this should change over to use
55   // this _easier to read_ representation instead.
56   //
57   enum UseType {
58     Use = USEFLAG,        /// only read
59     Def = DEFFLAG,        /// only written
60     UseAndDef = Use | Def /// read AND written
61   };
62
63   enum MachineOperandType {
64     MO_Register,                // Register operand.
65     MO_Immediate,               // Immediate Operand
66     MO_MachineBasicBlock,       // MachineBasicBlock reference
67     MO_FrameIndex,              // Abstract Stack Frame Index
68     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
69     MO_JumpTableIndex,          // Address of indexed Jump Table for switch
70     MO_ExternalSymbol,          // Name of external global symbol
71     MO_GlobalAddress            // Address of a global value
72   };
73
74 private:
75   union {
76     GlobalValue *GV;    // LLVM global for MO_GlobalAddress.
77     int64_t immedVal;   // Constant value for an explicit constant
78     MachineBasicBlock *MBB;     // For MO_MachineBasicBlock type
79     const char *SymbolName;     // For MO_ExternalSymbol type
80   } contents;
81
82   char flags;                   // see bit field definitions above
83   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
84   union {
85     int regNum;     // register number for an explicit register
86     int offset;     // Offset to address of global or external, only
87                     // valid for MO_GlobalAddress, MO_ExternalSym
88                     // and MO_ConstantPoolIndex
89   } extra;
90
91   void zeroContents() {
92     contents.immedVal = 0;
93     extra.offset = 0;
94   }
95
96   MachineOperand(int64_t ImmVal) : flags(0), opType(MO_Immediate) {
97     contents.immedVal = ImmVal;
98     extra.offset = 0;
99   }
100
101   MachineOperand(unsigned Idx, MachineOperandType OpTy)
102     : flags(0), opType(OpTy) {
103     contents.immedVal = Idx;
104     extra.offset = 0;
105   }
106   
107   MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
108     : flags(UseTy), opType(OpTy) {
109     zeroContents();
110     extra.regNum = Reg;
111   }
112
113   MachineOperand(GlobalValue *V, int Offset = 0)
114     : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
115     contents.GV = V;
116     extra.offset = Offset;
117   }
118
119   MachineOperand(MachineBasicBlock *mbb)
120     : flags(0), opType(MO_MachineBasicBlock) {
121     zeroContents ();
122     contents.MBB = mbb;
123   }
124
125   MachineOperand(const char *SymName, int Offset)
126     : flags(0), opType(MO_ExternalSymbol) {
127     zeroContents ();
128     contents.SymbolName = SymName;
129     extra.offset = Offset;
130   }
131
132 public:
133   MachineOperand(const MachineOperand &M)
134     : flags(M.flags), opType(M.opType) {
135     zeroContents ();
136     contents = M.contents;
137     extra = M.extra;
138   }
139
140   ~MachineOperand() {}
141
142   const MachineOperand &operator=(const MachineOperand &MO) {
143     contents = MO.contents;
144     flags    = MO.flags;
145     opType   = MO.opType;
146     extra    = MO.extra;
147     return *this;
148   }
149
150   /// getType - Returns the MachineOperandType for this operand.
151   ///
152   MachineOperandType getType() const { return opType; }
153
154   /// getUseType - Returns the MachineOperandUseType of this operand.
155   ///
156   UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
157
158   /// Accessors that tell you what kind of MachineOperand you're looking at.
159   ///
160   bool isRegister() const { return opType == MO_Register; }
161   bool isImmediate() const { return opType == MO_Immediate; }
162   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
163   bool isFrameIndex() const { return opType == MO_FrameIndex; }
164   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
165   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
166   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
167   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
168
169   int64_t getImmedValue() const {
170     assert(isImmediate() && "Wrong MachineOperand accessor");
171     return contents.immedVal;
172   }
173   MachineBasicBlock *getMachineBasicBlock() const {
174     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
175     return contents.MBB;
176   }
177   void setMachineBasicBlock(MachineBasicBlock *MBB) {
178     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
179     contents.MBB = MBB;
180   }
181   int getFrameIndex() const {
182     assert(isFrameIndex() && "Wrong MachineOperand accessor");
183     return (int)contents.immedVal;
184   }
185   unsigned getConstantPoolIndex() const {
186     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
187     return (unsigned)contents.immedVal;
188   }
189   unsigned getJumpTableIndex() const {
190     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
191     return (unsigned)contents.immedVal;
192   }
193   GlobalValue *getGlobal() const {
194     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
195     return contents.GV;
196   }
197   int getOffset() const {
198     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
199         "Wrong MachineOperand accessor");
200     return extra.offset;
201   }
202   const char *getSymbolName() const {
203     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
204     return contents.SymbolName;
205   }
206
207   /// MachineOperand methods for testing that work on any kind of
208   /// MachineOperand...
209   ///
210   bool            isUse           () const { return flags & USEFLAG; }
211   MachineOperand& setUse          ()       { flags |= USEFLAG; return *this; }
212   bool            isDef           () const { return flags & DEFFLAG; }
213   MachineOperand& setDef          ()       { flags |= DEFFLAG; return *this; }
214
215   /// getReg - Returns the register number.
216   ///
217   unsigned getReg() const {
218     assert(isRegister() && "This is not a register operand!");
219     return extra.regNum;
220   }
221
222   /// MachineOperand mutators.
223   ///
224   void setReg(unsigned Reg) {
225     assert(isRegister() && "This is not a register operand!");
226     extra.regNum = Reg;
227   }
228
229   void setImmedValue(int64_t immVal) {
230     assert(isImmediate() && "Wrong MachineOperand mutator");
231     contents.immedVal = immVal;
232   }
233
234   void setOffset(int Offset) {
235     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
236             isJumpTableIndex()) &&
237         "Wrong MachineOperand accessor");
238     extra.offset = Offset;
239   }
240   
241   /// ChangeToImmediate - Replace this operand with a new immediate operand of
242   /// the specified value.  If an operand is known to be an immediate already,
243   /// the setImmedValue method should be used.
244   void ChangeToImmediate(int64_t ImmVal) {
245     opType = MO_Immediate;
246     contents.immedVal = ImmVal;
247   }
248
249   /// ChangeToRegister - Replace this operand with a new register operand of
250   /// the specified value.  If an operand is known to be an register already,
251   /// the setReg method should be used.
252   void ChangeToRegister(unsigned Reg) {
253     opType = MO_Register;
254     extra.regNum = Reg;
255   }
256
257   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
258
259   friend class MachineInstr;
260 };
261
262
263 //===----------------------------------------------------------------------===//
264 // class MachineInstr
265 //
266 // Purpose:
267 //   Representation of each machine instruction.
268 //
269 //   MachineOpCode must be an enum, defined separately for each target.
270 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
271 //
272 //  There are 2 kinds of operands:
273 //
274 //  (1) Explicit operands of the machine instruction in vector operands[]
275 //
276 //  (2) "Implicit operands" are values implicitly used or defined by the
277 //      machine instruction, such as arguments to a CALL, return value of
278 //      a CALL (if any), and return value of a RETURN.
279 //===----------------------------------------------------------------------===//
280
281 class MachineInstr {
282   short Opcode;                         // the opcode
283   std::vector<MachineOperand> operands; // the operands
284   MachineInstr* prev, *next;            // links for our intrusive list
285   MachineBasicBlock* parent;            // pointer to the owning basic block
286
287   // OperandComplete - Return true if it's illegal to add a new operand
288   bool OperandsComplete() const;
289
290   //Constructor used by clone() method
291   MachineInstr(const MachineInstr&);
292
293   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
294
295   // Intrusive list support
296   //
297   friend struct ilist_traits<MachineInstr>;
298
299 public:
300   /// MachineInstr ctor - This constructor only does a _reserve_ of the
301   /// operands, not a resize for them.  It is expected that if you use this that
302   /// you call add* methods below to fill up the operands, instead of the Set
303   /// methods.  Eventually, the "resizing" ctors will be phased out.
304   ///
305   MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
306
307   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
308   /// the MachineInstr is created and added to the end of the specified basic
309   /// block.
310   ///
311   MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
312
313   ~MachineInstr();
314
315   const MachineBasicBlock* getParent() const { return parent; }
316   MachineBasicBlock* getParent() { return parent; }
317
318   /// getOpcode - Returns the opcode of this MachineInstr.
319   ///
320   const int getOpcode() const { return Opcode; }
321
322   /// Access to explicit operands of the instruction.
323   ///
324   unsigned getNumOperands() const { return operands.size(); }
325
326   const MachineOperand& getOperand(unsigned i) const {
327     assert(i < getNumOperands() && "getOperand() out of range!");
328     return operands[i];
329   }
330   MachineOperand& getOperand(unsigned i) {
331     assert(i < getNumOperands() && "getOperand() out of range!");
332     return operands[i];
333   }
334
335
336   /// clone - Create a copy of 'this' instruction that is identical in
337   /// all ways except the the instruction has no parent, prev, or next.
338   MachineInstr* clone() const;
339   
340   /// removeFromParent - This method unlinks 'this' from the containing basic
341   /// block, and returns it, but does not delete it.
342   MachineInstr *removeFromParent();
343   
344   /// eraseFromParent - This method unlinks 'this' from the containing basic
345   /// block and deletes it.
346   void eraseFromParent() {
347     delete removeFromParent();
348   }
349
350   //
351   // Debugging support
352   //
353   void print(std::ostream &OS, const TargetMachine *TM) const;
354   void dump() const;
355   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
356
357   //===--------------------------------------------------------------------===//
358   // Accessors to add operands when building up machine instructions
359   //
360
361   /// addRegOperand - Add a symbolic virtual register reference...
362   ///
363   void addRegOperand(int reg,
364                      MachineOperand::UseType UTy = MachineOperand::Use) {
365     assert(!OperandsComplete() &&
366            "Trying to add an operand to a machine instr that is already done!");
367     operands.push_back(
368       MachineOperand(reg, MachineOperand::MO_Register, UTy));
369   }
370
371   /// addImmOperand - Add a zero extended constant argument to the
372   /// machine instruction.
373   ///
374   void addImmOperand(int64_t Val) {
375     assert(!OperandsComplete() &&
376            "Trying to add an operand to a machine instr that is already done!");
377     operands.push_back(MachineOperand(Val));
378   }
379
380   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
381     assert(!OperandsComplete() &&
382            "Trying to add an operand to a machine instr that is already done!");
383     operands.push_back(MachineOperand(MBB));
384   }
385
386   /// addFrameIndexOperand - Add an abstract frame index to the instruction
387   ///
388   void addFrameIndexOperand(unsigned Idx) {
389     assert(!OperandsComplete() &&
390            "Trying to add an operand to a machine instr that is already done!");
391     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
392   }
393
394   /// addConstantPoolndexOperand - Add a constant pool object index to the
395   /// instruction.
396   ///
397   void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
398     assert(!OperandsComplete() &&
399            "Trying to add an operand to a machine instr that is already done!");
400     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
401   }
402
403   /// addJumpTableIndexOperand - Add a jump table object index to the
404   /// instruction.
405   ///
406   void addJumpTableIndexOperand(unsigned I) {
407     assert(!OperandsComplete() &&
408            "Trying to add an operand to a machine instr that is already done!");
409     operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
410   }
411   
412   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
413     assert(!OperandsComplete() &&
414            "Trying to add an operand to a machine instr that is already done!");
415     operands.push_back(MachineOperand(GV, Offset));
416   }
417
418   /// addExternalSymbolOperand - Add an external symbol operand to this instr
419   ///
420   void addExternalSymbolOperand(const char *SymName) {
421     operands.push_back(MachineOperand(SymName, 0));
422   }
423
424   //===--------------------------------------------------------------------===//
425   // Accessors used to modify instructions in place.
426   //
427
428   /// setOpcode - Replace the opcode of the current instruction with a new one.
429   ///
430   void setOpcode(unsigned Op) { Opcode = Op; }
431
432   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
433   /// fewer operand than it started with.
434   ///
435   void RemoveOperand(unsigned i) {
436     operands.erase(operands.begin()+i);
437   }
438 };
439
440 //===----------------------------------------------------------------------===//
441 // Debugging Support
442
443 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
444 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
445
446 } // End llvm namespace
447
448 #endif