Move some methods out of MachineInstr into MachineOperand
[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_VirtualRegister,         // virtual register for *value
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, MachineOperandType OpTy, int Offset = 0)
97     : flags(0), opType(OpTy) {
98     contents.immedVal = ImmVal;
99     extra.offset = Offset;
100   }
101
102   MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
103     : flags(UseTy), opType(OpTy) {
104     zeroContents();
105     extra.regNum = Reg;
106   }
107
108   MachineOperand(GlobalValue *V, int Offset = 0)
109     : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
110     contents.GV = V;
111     extra.offset = Offset;
112   }
113
114   MachineOperand(MachineBasicBlock *mbb)
115     : flags(0), opType(MO_MachineBasicBlock) {
116     zeroContents ();
117     contents.MBB = mbb;
118   }
119
120   MachineOperand(const char *SymName, int Offset)
121     : flags(0), opType(MO_ExternalSymbol) {
122     zeroContents ();
123     contents.SymbolName = SymName;
124     extra.offset = Offset;
125   }
126
127 public:
128   MachineOperand(const MachineOperand &M)
129     : flags(M.flags), opType(M.opType) {
130     zeroContents ();
131     contents = M.contents;
132     extra = M.extra;
133   }
134
135   ~MachineOperand() {}
136
137   const MachineOperand &operator=(const MachineOperand &MO) {
138     contents = MO.contents;
139     flags    = MO.flags;
140     opType   = MO.opType;
141     extra    = MO.extra;
142     return *this;
143   }
144
145   /// getType - Returns the MachineOperandType for this operand.
146   ///
147   MachineOperandType getType() const { return opType; }
148
149   /// getUseType - Returns the MachineOperandUseType of this operand.
150   ///
151   UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
152
153   /// isRegister - Return true if this operand is a register operand.
154   ///
155   bool isRegister() const {
156     return opType == MO_VirtualRegister;
157   }
158
159   /// Accessors that tell you what kind of MachineOperand you're looking at.
160   ///
161   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
162   bool isImmediate() const { return opType == MO_Immediate; }
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   /// hasAllocatedReg - Returns true iff a machine register has been
216   /// allocated to this operand.
217   ///
218   bool hasAllocatedReg() const {
219     return extra.regNum >= 0 && opType == MO_VirtualRegister;
220   }
221
222   /// getReg - Returns the register number. It is a runtime error to call this
223   /// if a register is not allocated.
224   ///
225   unsigned getReg() const {
226     assert(hasAllocatedReg());
227     return extra.regNum;
228   }
229
230   /// MachineOperand mutators.
231   ///
232   void setReg(unsigned Reg) {
233     assert(hasAllocatedReg() && "This operand cannot have a register number!");
234     extra.regNum = Reg;
235   }
236
237   void setImmedValue(int64_t immVal) {
238     assert(isImmediate() && "Wrong MachineOperand mutator");
239     contents.immedVal = immVal;
240   }
241
242   void setOffset(int Offset) {
243     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
244             isJumpTableIndex()) &&
245         "Wrong MachineOperand accessor");
246     extra.offset = Offset;
247   }
248   
249   /// ChangeToImmediate - Replace this operand with a new immediate operand of
250   /// the specified value.  If an operand is known to be an immediate already,
251   /// the setImmedValue method should be used.
252   void ChangeToImmediate(int64_t ImmVal) {
253     opType = MO_Immediate;
254     contents.immedVal = ImmVal;
255   }
256
257   /// ChangeToRegister - Replace this operand with a new register operand of
258   /// the specified value.  If an operand is known to be an register already,
259   /// the setReg method should be used.
260   void ChangeToRegister(unsigned Reg) {
261     opType = MO_VirtualRegister;
262     extra.regNum = Reg;
263   }
264
265   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
266
267   friend class MachineInstr;
268 };
269
270
271 //===----------------------------------------------------------------------===//
272 // class MachineInstr
273 //
274 // Purpose:
275 //   Representation of each machine instruction.
276 //
277 //   MachineOpCode must be an enum, defined separately for each target.
278 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
279 //
280 //  There are 2 kinds of operands:
281 //
282 //  (1) Explicit operands of the machine instruction in vector operands[]
283 //
284 //  (2) "Implicit operands" are values implicitly used or defined by the
285 //      machine instruction, such as arguments to a CALL, return value of
286 //      a CALL (if any), and return value of a RETURN.
287 //===----------------------------------------------------------------------===//
288
289 class MachineInstr {
290   short Opcode;                         // the opcode
291   std::vector<MachineOperand> operands; // the operands
292   MachineInstr* prev, *next;            // links for our intrusive list
293   MachineBasicBlock* parent;            // pointer to the owning basic block
294
295   // OperandComplete - Return true if it's illegal to add a new operand
296   bool OperandsComplete() const;
297
298   //Constructor used by clone() method
299   MachineInstr(const MachineInstr&);
300
301   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
302
303   // Intrusive list support
304   //
305   friend struct ilist_traits<MachineInstr>;
306
307 public:
308   /// MachineInstr ctor - This constructor only does a _reserve_ of the
309   /// operands, not a resize for them.  It is expected that if you use this that
310   /// you call add* methods below to fill up the operands, instead of the Set
311   /// methods.  Eventually, the "resizing" ctors will be phased out.
312   ///
313   MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
314
315   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
316   /// the MachineInstr is created and added to the end of the specified basic
317   /// block.
318   ///
319   MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
320
321   ~MachineInstr();
322
323   const MachineBasicBlock* getParent() const { return parent; }
324   MachineBasicBlock* getParent() { return parent; }
325
326   /// getOpcode - Returns the opcode of this MachineInstr.
327   ///
328   const int getOpcode() const { return Opcode; }
329
330   /// Access to explicit operands of the instruction.
331   ///
332   unsigned getNumOperands() const { return operands.size(); }
333
334   const MachineOperand& getOperand(unsigned i) const {
335     assert(i < getNumOperands() && "getOperand() out of range!");
336     return operands[i];
337   }
338   MachineOperand& getOperand(unsigned i) {
339     assert(i < getNumOperands() && "getOperand() out of range!");
340     return operands[i];
341   }
342
343
344   /// clone - Create a copy of 'this' instruction that is identical in
345   /// all ways except the the instruction has no parent, prev, or next.
346   MachineInstr* clone() const;
347   
348   /// removeFromParent - This method unlinks 'this' from the containing basic
349   /// block, and returns it, but does not delete it.
350   MachineInstr *removeFromParent();
351   
352   /// eraseFromParent - This method unlinks 'this' from the containing basic
353   /// block and deletes it.
354   void eraseFromParent() {
355     delete removeFromParent();
356   }
357
358   //
359   // Debugging support
360   //
361   void print(std::ostream &OS, const TargetMachine *TM) const;
362   void dump() const;
363   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
364
365   //===--------------------------------------------------------------------===//
366   // Accessors to add operands when building up machine instructions
367   //
368
369   /// addRegOperand - Add a symbolic virtual register reference...
370   ///
371   void addRegOperand(int reg, bool isDef) {
372     assert(!OperandsComplete() &&
373            "Trying to add an operand to a machine instr that is already done!");
374     operands.push_back(
375       MachineOperand(reg, MachineOperand::MO_VirtualRegister,
376                      isDef ? MachineOperand::Def : MachineOperand::Use));
377   }
378
379   /// addRegOperand - Add a symbolic virtual register reference...
380   ///
381   void addRegOperand(int reg,
382                      MachineOperand::UseType UTy = MachineOperand::Use) {
383     assert(!OperandsComplete() &&
384            "Trying to add an operand to a machine instr that is already done!");
385     operands.push_back(
386       MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
387   }
388
389   /// addZeroExtImmOperand - Add a zero extended constant argument to the
390   /// machine instruction.
391   ///
392   void addZeroExtImmOperand(int intValue) {
393     assert(!OperandsComplete() &&
394            "Trying to add an operand to a machine instr that is already done!");
395     operands.push_back(
396       MachineOperand(intValue, MachineOperand::MO_Immediate));
397   }
398
399   /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
400   /// to the machine instruction.
401   ///
402   void addZeroExtImm64Operand(uint64_t intValue) {
403     assert(!OperandsComplete() &&
404            "Trying to add an operand to a machine instr that is already done!");
405     operands.push_back(MachineOperand(intValue, MachineOperand::MO_Immediate));
406   }
407
408   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
409     assert(!OperandsComplete() &&
410            "Trying to add an operand to a machine instr that is already done!");
411     operands.push_back(MachineOperand(MBB));
412   }
413
414   /// addFrameIndexOperand - Add an abstract frame index to the instruction
415   ///
416   void addFrameIndexOperand(unsigned Idx) {
417     assert(!OperandsComplete() &&
418            "Trying to add an operand to a machine instr that is already done!");
419     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
420   }
421
422   /// addConstantPoolndexOperand - Add a constant pool object index to the
423   /// instruction.
424   ///
425   void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
426     assert(!OperandsComplete() &&
427            "Trying to add an operand to a machine instr that is already done!");
428     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
429   }
430
431   /// addJumpTableIndexOperand - Add a jump table object index to the
432   /// instruction.
433   ///
434   void addJumpTableIndexOperand(unsigned I) {
435     assert(!OperandsComplete() &&
436            "Trying to add an operand to a machine instr that is already done!");
437     operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
438   }
439   
440   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
441     assert(!OperandsComplete() &&
442            "Trying to add an operand to a machine instr that is already done!");
443     operands.push_back(MachineOperand(GV, Offset));
444   }
445
446   /// addExternalSymbolOperand - Add an external symbol operand to this instr
447   ///
448   void addExternalSymbolOperand(const char *SymName) {
449     operands.push_back(MachineOperand(SymName, 0));
450   }
451
452   //===--------------------------------------------------------------------===//
453   // Accessors used to modify instructions in place.
454   //
455
456   /// setOpcode - Replace the opcode of the current instruction with a new one.
457   ///
458   void setOpcode(unsigned Op) { Opcode = Op; }
459
460   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
461   /// fewer operand than it started with.
462   ///
463   void RemoveOperand(unsigned i) {
464     operands.erase(operands.begin()+i);
465   }
466 };
467
468 //===----------------------------------------------------------------------===//
469 // Debugging Support
470
471 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
472 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
473
474 } // End llvm namespace
475
476 #endif