remove hasAllocatedReg
[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   /// Accessors that tell you what kind of MachineOperand you're looking at.
154   ///
155   bool isRegister() const { return opType == MO_VirtualRegister; }
156   bool isImmediate() const { return opType == MO_Immediate; }
157   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
158   bool isFrameIndex() const { return opType == MO_FrameIndex; }
159   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
160   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
161   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
162   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
163
164   int64_t getImmedValue() const {
165     assert(isImmediate() && "Wrong MachineOperand accessor");
166     return contents.immedVal;
167   }
168   MachineBasicBlock *getMachineBasicBlock() const {
169     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
170     return contents.MBB;
171   }
172   void setMachineBasicBlock(MachineBasicBlock *MBB) {
173     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
174     contents.MBB = MBB;
175   }
176   int getFrameIndex() const {
177     assert(isFrameIndex() && "Wrong MachineOperand accessor");
178     return (int)contents.immedVal;
179   }
180   unsigned getConstantPoolIndex() const {
181     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
182     return (unsigned)contents.immedVal;
183   }
184   unsigned getJumpTableIndex() const {
185     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
186     return (unsigned)contents.immedVal;
187   }
188   GlobalValue *getGlobal() const {
189     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
190     return contents.GV;
191   }
192   int getOffset() const {
193     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
194         "Wrong MachineOperand accessor");
195     return extra.offset;
196   }
197   const char *getSymbolName() const {
198     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
199     return contents.SymbolName;
200   }
201
202   /// MachineOperand methods for testing that work on any kind of
203   /// MachineOperand...
204   ///
205   bool            isUse           () const { return flags & USEFLAG; }
206   MachineOperand& setUse          ()       { flags |= USEFLAG; return *this; }
207   bool            isDef           () const { return flags & DEFFLAG; }
208   MachineOperand& setDef          ()       { flags |= DEFFLAG; return *this; }
209
210   /// getReg - Returns the register number.
211   ///
212   unsigned getReg() const {
213     assert(isRegister() && "This is not a register operand!");
214     return extra.regNum;
215   }
216
217   /// MachineOperand mutators.
218   ///
219   void setReg(unsigned Reg) {
220     assert(isRegister() && "This is not a register operand!");
221     extra.regNum = Reg;
222   }
223
224   void setImmedValue(int64_t immVal) {
225     assert(isImmediate() && "Wrong MachineOperand mutator");
226     contents.immedVal = immVal;
227   }
228
229   void setOffset(int Offset) {
230     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
231             isJumpTableIndex()) &&
232         "Wrong MachineOperand accessor");
233     extra.offset = Offset;
234   }
235   
236   /// ChangeToImmediate - Replace this operand with a new immediate operand of
237   /// the specified value.  If an operand is known to be an immediate already,
238   /// the setImmedValue method should be used.
239   void ChangeToImmediate(int64_t ImmVal) {
240     opType = MO_Immediate;
241     contents.immedVal = ImmVal;
242   }
243
244   /// ChangeToRegister - Replace this operand with a new register operand of
245   /// the specified value.  If an operand is known to be an register already,
246   /// the setReg method should be used.
247   void ChangeToRegister(unsigned Reg) {
248     opType = MO_VirtualRegister;
249     extra.regNum = Reg;
250   }
251
252   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
253
254   friend class MachineInstr;
255 };
256
257
258 //===----------------------------------------------------------------------===//
259 // class MachineInstr
260 //
261 // Purpose:
262 //   Representation of each machine instruction.
263 //
264 //   MachineOpCode must be an enum, defined separately for each target.
265 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
266 //
267 //  There are 2 kinds of operands:
268 //
269 //  (1) Explicit operands of the machine instruction in vector operands[]
270 //
271 //  (2) "Implicit operands" are values implicitly used or defined by the
272 //      machine instruction, such as arguments to a CALL, return value of
273 //      a CALL (if any), and return value of a RETURN.
274 //===----------------------------------------------------------------------===//
275
276 class MachineInstr {
277   short Opcode;                         // the opcode
278   std::vector<MachineOperand> operands; // the operands
279   MachineInstr* prev, *next;            // links for our intrusive list
280   MachineBasicBlock* parent;            // pointer to the owning basic block
281
282   // OperandComplete - Return true if it's illegal to add a new operand
283   bool OperandsComplete() const;
284
285   //Constructor used by clone() method
286   MachineInstr(const MachineInstr&);
287
288   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
289
290   // Intrusive list support
291   //
292   friend struct ilist_traits<MachineInstr>;
293
294 public:
295   /// MachineInstr ctor - This constructor only does a _reserve_ of the
296   /// operands, not a resize for them.  It is expected that if you use this that
297   /// you call add* methods below to fill up the operands, instead of the Set
298   /// methods.  Eventually, the "resizing" ctors will be phased out.
299   ///
300   MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
301
302   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
303   /// the MachineInstr is created and added to the end of the specified basic
304   /// block.
305   ///
306   MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
307
308   ~MachineInstr();
309
310   const MachineBasicBlock* getParent() const { return parent; }
311   MachineBasicBlock* getParent() { return parent; }
312
313   /// getOpcode - Returns the opcode of this MachineInstr.
314   ///
315   const int getOpcode() const { return Opcode; }
316
317   /// Access to explicit operands of the instruction.
318   ///
319   unsigned getNumOperands() const { return operands.size(); }
320
321   const MachineOperand& getOperand(unsigned i) const {
322     assert(i < getNumOperands() && "getOperand() out of range!");
323     return operands[i];
324   }
325   MachineOperand& getOperand(unsigned i) {
326     assert(i < getNumOperands() && "getOperand() out of range!");
327     return operands[i];
328   }
329
330
331   /// clone - Create a copy of 'this' instruction that is identical in
332   /// all ways except the the instruction has no parent, prev, or next.
333   MachineInstr* clone() const;
334   
335   /// removeFromParent - This method unlinks 'this' from the containing basic
336   /// block, and returns it, but does not delete it.
337   MachineInstr *removeFromParent();
338   
339   /// eraseFromParent - This method unlinks 'this' from the containing basic
340   /// block and deletes it.
341   void eraseFromParent() {
342     delete removeFromParent();
343   }
344
345   //
346   // Debugging support
347   //
348   void print(std::ostream &OS, const TargetMachine *TM) const;
349   void dump() const;
350   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
351
352   //===--------------------------------------------------------------------===//
353   // Accessors to add operands when building up machine instructions
354   //
355
356   /// addRegOperand - Add a symbolic virtual register reference...
357   ///
358   void addRegOperand(int reg, bool isDef) {
359     assert(!OperandsComplete() &&
360            "Trying to add an operand to a machine instr that is already done!");
361     operands.push_back(
362       MachineOperand(reg, MachineOperand::MO_VirtualRegister,
363                      isDef ? MachineOperand::Def : MachineOperand::Use));
364   }
365
366   /// addRegOperand - Add a symbolic virtual register reference...
367   ///
368   void addRegOperand(int reg,
369                      MachineOperand::UseType UTy = MachineOperand::Use) {
370     assert(!OperandsComplete() &&
371            "Trying to add an operand to a machine instr that is already done!");
372     operands.push_back(
373       MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
374   }
375
376   /// addZeroExtImmOperand - Add a zero extended constant argument to the
377   /// machine instruction.
378   ///
379   void addZeroExtImmOperand(int intValue) {
380     assert(!OperandsComplete() &&
381            "Trying to add an operand to a machine instr that is already done!");
382     operands.push_back(
383       MachineOperand(intValue, MachineOperand::MO_Immediate));
384   }
385
386   /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
387   /// to the machine instruction.
388   ///
389   void addZeroExtImm64Operand(uint64_t intValue) {
390     assert(!OperandsComplete() &&
391            "Trying to add an operand to a machine instr that is already done!");
392     operands.push_back(MachineOperand(intValue, MachineOperand::MO_Immediate));
393   }
394
395   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
396     assert(!OperandsComplete() &&
397            "Trying to add an operand to a machine instr that is already done!");
398     operands.push_back(MachineOperand(MBB));
399   }
400
401   /// addFrameIndexOperand - Add an abstract frame index to the instruction
402   ///
403   void addFrameIndexOperand(unsigned Idx) {
404     assert(!OperandsComplete() &&
405            "Trying to add an operand to a machine instr that is already done!");
406     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
407   }
408
409   /// addConstantPoolndexOperand - Add a constant pool object index to the
410   /// instruction.
411   ///
412   void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
413     assert(!OperandsComplete() &&
414            "Trying to add an operand to a machine instr that is already done!");
415     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
416   }
417
418   /// addJumpTableIndexOperand - Add a jump table object index to the
419   /// instruction.
420   ///
421   void addJumpTableIndexOperand(unsigned I) {
422     assert(!OperandsComplete() &&
423            "Trying to add an operand to a machine instr that is already done!");
424     operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
425   }
426   
427   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
428     assert(!OperandsComplete() &&
429            "Trying to add an operand to a machine instr that is already done!");
430     operands.push_back(MachineOperand(GV, Offset));
431   }
432
433   /// addExternalSymbolOperand - Add an external symbol operand to this instr
434   ///
435   void addExternalSymbolOperand(const char *SymName) {
436     operands.push_back(MachineOperand(SymName, 0));
437   }
438
439   //===--------------------------------------------------------------------===//
440   // Accessors used to modify instructions in place.
441   //
442
443   /// setOpcode - Replace the opcode of the current instruction with a new one.
444   ///
445   void setOpcode(unsigned Op) { Opcode = Op; }
446
447   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
448   /// fewer operand than it started with.
449   ///
450   void RemoveOperand(unsigned i) {
451     operands.erase(operands.begin()+i);
452   }
453 };
454
455 //===----------------------------------------------------------------------===//
456 // Debugging Support
457
458 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
459 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
460
461 } // End llvm namespace
462
463 #endif