1059b7c3d505f00260f397b5f065f71941eaead6
[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_SignExtendedImmed,
66     MO_UnextendedImmed,
67     MO_MachineBasicBlock,       // MachineBasicBlock reference
68     MO_FrameIndex,              // Abstract Stack Frame Index
69     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
70     MO_JumpTableIndex,          // Address of indexed Jump Table for switch
71     MO_ExternalSymbol,          // Name of external global symbol
72     MO_GlobalAddress            // Address of a global value
73   };
74
75 private:
76   union {
77     Value*  value;      // BasicBlockVal for a label operand.
78                         // ConstantVal for a non-address immediate.
79                         // Virtual register for an SSA operand,
80                         //   including hidden operands required for
81                         //   the generated machine code.
82                         // LLVM global for MO_GlobalAddress.
83
84     int64_t immedVal;   // Constant value for an explicit constant
85
86     MachineBasicBlock *MBB;     // For MO_MachineBasicBlock type
87     const char *SymbolName;     // For MO_ExternalSymbol type
88   } contents;
89
90   char flags;                   // see bit field definitions above
91   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
92   union {
93     int regNum;                 // register number for an explicit register
94                                 // will be set for a value after reg allocation
95
96     int offset;                 // Offset to address of global or external, only
97                                 // valid for MO_GlobalAddress, MO_ExternalSym
98                                 // and MO_ConstantPoolIndex
99   } extra;
100
101   void zeroContents () {
102     memset (&contents, 0, sizeof (contents));
103     memset (&extra, 0, sizeof (extra));
104   }
105
106   MachineOperand(int64_t ImmVal, MachineOperandType OpTy, int Offset = 0)
107     : flags(0), opType(OpTy) {
108     zeroContents ();
109     contents.immedVal = ImmVal;
110     extra.offset = Offset;
111   }
112
113   MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
114     : flags(UseTy), opType(OpTy) {
115     zeroContents ();
116     extra.regNum = Reg;
117   }
118
119   MachineOperand(GlobalValue *V, int Offset = 0)
120     : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
121     zeroContents ();
122     contents.value = (Value*)V;
123     extra.offset = Offset;
124   }
125
126   MachineOperand(MachineBasicBlock *mbb)
127     : flags(0), opType(MO_MachineBasicBlock) {
128     zeroContents ();
129     contents.MBB = mbb;
130   }
131
132   MachineOperand(const char *SymName, int Offset)
133     : flags(0), opType(MO_ExternalSymbol) {
134     zeroContents ();
135     contents.SymbolName = SymName;
136     extra.offset = Offset;
137   }
138
139 public:
140   MachineOperand(const MachineOperand &M)
141     : flags(M.flags), opType(M.opType) {
142     zeroContents ();
143     contents = M.contents;
144     extra = M.extra;
145   }
146
147   ~MachineOperand() {}
148
149   const MachineOperand &operator=(const MachineOperand &MO) {
150     contents = MO.contents;
151     flags    = MO.flags;
152     opType   = MO.opType;
153     extra    = MO.extra;
154     return *this;
155   }
156
157   /// getType - Returns the MachineOperandType for this operand.
158   ///
159   MachineOperandType getType() const { return opType; }
160
161   /// getUseType - Returns the MachineOperandUseType of this operand.
162   ///
163   UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
164
165   /// isRegister - Return true if this operand is a register operand.
166   ///
167   bool isRegister() const {
168     return opType == MO_VirtualRegister;
169   }
170
171   /// Accessors that tell you what kind of MachineOperand you're looking at.
172   ///
173   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
174   bool isImmediate() const {
175     return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
176   }
177   bool isFrameIndex() const { return opType == MO_FrameIndex; }
178   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
179   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
180   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
181   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
182
183   int64_t getImmedValue() const {
184     assert(isImmediate() && "Wrong MachineOperand accessor");
185     return contents.immedVal;
186   }
187   MachineBasicBlock *getMachineBasicBlock() const {
188     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
189     return contents.MBB;
190   }
191   void setMachineBasicBlock(MachineBasicBlock *MBB) {
192     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
193     contents.MBB = MBB;
194   }
195   int getFrameIndex() const {
196     assert(isFrameIndex() && "Wrong MachineOperand accessor");
197     return (int)contents.immedVal;
198   }
199   unsigned getConstantPoolIndex() const {
200     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
201     return (unsigned)contents.immedVal;
202   }
203   unsigned getJumpTableIndex() const {
204     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
205     return (unsigned)contents.immedVal;
206   }
207   GlobalValue *getGlobal() const {
208     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
209     return (GlobalValue*)contents.value;
210   }
211   int getOffset() const {
212     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
213         "Wrong MachineOperand accessor");
214     return extra.offset;
215   }
216   const char *getSymbolName() const {
217     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
218     return contents.SymbolName;
219   }
220
221   /// MachineOperand methods for testing that work on any kind of
222   /// MachineOperand...
223   ///
224   bool            isUse           () const { return flags & USEFLAG; }
225   MachineOperand& setUse          ()       { flags |= USEFLAG; return *this; }
226   bool            isDef           () const { return flags & DEFFLAG; }
227   MachineOperand& setDef          ()       { flags |= DEFFLAG; return *this; }
228
229   /// hasAllocatedReg - Returns true iff a machine register has been
230   /// allocated to this operand.
231   ///
232   bool hasAllocatedReg() const {
233     return extra.regNum >= 0 && opType == MO_VirtualRegister;
234   }
235
236   /// getReg - Returns the register number. It is a runtime error to call this
237   /// if a register is not allocated.
238   ///
239   unsigned getReg() const {
240     assert(hasAllocatedReg());
241     return extra.regNum;
242   }
243
244   /// MachineOperand mutators.
245   ///
246   void setReg(unsigned Reg) {
247     assert(hasAllocatedReg() && "This operand cannot have a register number!");
248     extra.regNum = Reg;
249   }
250
251   void setImmedValue(int immVal) {
252     assert(isImmediate() && "Wrong MachineOperand mutator");
253     contents.immedVal = immVal;
254   }
255
256   void setOffset(int Offset) {
257     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
258             isJumpTableIndex()) &&
259         "Wrong MachineOperand accessor");
260     extra.offset = Offset;
261   }
262
263   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
264
265   friend class MachineInstr;
266 };
267
268
269 //===----------------------------------------------------------------------===//
270 // class MachineInstr
271 //
272 // Purpose:
273 //   Representation of each machine instruction.
274 //
275 //   MachineOpCode must be an enum, defined separately for each target.
276 //   E.g., It is defined in SparcInstructionSelection.h for the SPARC.
277 //
278 //  There are 2 kinds of operands:
279 //
280 //  (1) Explicit operands of the machine instruction in vector operands[]
281 //
282 //  (2) "Implicit operands" are values implicitly used or defined by the
283 //      machine instruction, such as arguments to a CALL, return value of
284 //      a CALL (if any), and return value of a RETURN.
285 //===----------------------------------------------------------------------===//
286
287 class MachineInstr {
288   short Opcode;                         // the opcode
289   std::vector<MachineOperand> operands; // the operands
290   MachineInstr* prev, *next;            // links for our intrusive list
291   MachineBasicBlock* parent;            // pointer to the owning basic block
292
293   // OperandComplete - Return true if it's illegal to add a new operand
294   bool OperandsComplete() const;
295
296   //Constructor used by clone() method
297   MachineInstr(const MachineInstr&);
298
299   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
300
301   // Intrusive list support
302   //
303   friend struct ilist_traits<MachineInstr>;
304
305 public:
306   /// MachineInstr ctor - This constructor only does a _reserve_ of the
307   /// operands, not a resize for them.  It is expected that if you use this that
308   /// you call add* methods below to fill up the operands, instead of the Set
309   /// methods.  Eventually, the "resizing" ctors will be phased out.
310   ///
311   MachineInstr(short Opcode, unsigned numOperands, bool XX, bool YY);
312
313   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
314   /// the MachineInstr is created and added to the end of the specified basic
315   /// block.
316   ///
317   MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
318
319   ~MachineInstr();
320
321   const MachineBasicBlock* getParent() const { return parent; }
322   MachineBasicBlock* getParent() { return parent; }
323
324   /// getOpcode - Returns the opcode of this MachineInstr.
325   ///
326   const int getOpcode() const { return Opcode; }
327
328   /// Access to explicit operands of the instruction.
329   ///
330   unsigned getNumOperands() const { return operands.size(); }
331
332   const MachineOperand& getOperand(unsigned i) const {
333     assert(i < getNumOperands() && "getOperand() out of range!");
334     return operands[i];
335   }
336   MachineOperand& getOperand(unsigned i) {
337     assert(i < getNumOperands() && "getOperand() out of range!");
338     return operands[i];
339   }
340
341
342   /// clone - Create a copy of 'this' instruction that is identical in
343   /// all ways except the the instruction has no parent, prev, or next.
344   MachineInstr* clone() const;
345   
346   /// removeFromParent - This method unlinks 'this' from the containing basic
347   /// block, and returns it, but does not delete it.
348   MachineInstr *removeFromParent();
349   
350   /// eraseFromParent - This method unlinks 'this' from the containing basic
351   /// block and deletes it.
352   void eraseFromParent() {
353     delete removeFromParent();
354   }
355
356   //
357   // Debugging support
358   //
359   void print(std::ostream &OS, const TargetMachine *TM) const;
360   void dump() const;
361   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
362
363   //===--------------------------------------------------------------------===//
364   // Accessors to add operands when building up machine instructions
365   //
366
367   /// addRegOperand - Add a symbolic virtual register reference...
368   ///
369   void addRegOperand(int reg, bool isDef) {
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,
374                      isDef ? MachineOperand::Def : MachineOperand::Use));
375   }
376
377   /// addRegOperand - Add a symbolic virtual register reference...
378   ///
379   void addRegOperand(int reg,
380                      MachineOperand::UseType UTy = MachineOperand::Use) {
381     assert(!OperandsComplete() &&
382            "Trying to add an operand to a machine instr that is already done!");
383     operands.push_back(
384       MachineOperand(reg, MachineOperand::MO_VirtualRegister, UTy));
385   }
386
387   /// addZeroExtImmOperand - Add a zero extended constant argument to the
388   /// machine instruction.
389   ///
390   void addZeroExtImmOperand(int intValue) {
391     assert(!OperandsComplete() &&
392            "Trying to add an operand to a machine instr that is already done!");
393     operands.push_back(
394       MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
395   }
396
397   /// addZeroExtImm64Operand - Add a zero extended 64-bit constant argument
398   /// to the machine instruction.
399   ///
400   void addZeroExtImm64Operand(uint64_t intValue) {
401     assert(!OperandsComplete() &&
402            "Trying to add an operand to a machine instr that is already done!");
403     operands.push_back(
404       MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
405   }
406
407   /// addSignExtImmOperand - Add a zero extended constant argument to the
408   /// machine instruction.
409   ///
410   void addSignExtImmOperand(int intValue) {
411     assert(!OperandsComplete() &&
412            "Trying to add an operand to a machine instr that is already done!");
413     operands.push_back(
414       MachineOperand(intValue, MachineOperand::MO_SignExtendedImmed));
415   }
416
417   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
418     assert(!OperandsComplete() &&
419            "Trying to add an operand to a machine instr that is already done!");
420     operands.push_back(MachineOperand(MBB));
421   }
422
423   /// addFrameIndexOperand - Add an abstract frame index to the instruction
424   ///
425   void addFrameIndexOperand(unsigned Idx) {
426     assert(!OperandsComplete() &&
427            "Trying to add an operand to a machine instr that is already done!");
428     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
429   }
430
431   /// addConstantPoolndexOperand - Add a constant pool object index to the
432   /// instruction.
433   ///
434   void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
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_ConstantPoolIndex));
438   }
439
440   /// addJumpTableIndexOperand - Add a jump table object index to the
441   /// instruction.
442   ///
443   void addJumpTableIndexOperand(unsigned I) {
444     assert(!OperandsComplete() &&
445            "Trying to add an operand to a machine instr that is already done!");
446     operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
447   }
448   
449   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
450     assert(!OperandsComplete() &&
451            "Trying to add an operand to a machine instr that is already done!");
452     operands.push_back(MachineOperand(GV, Offset));
453   }
454
455   /// addExternalSymbolOperand - Add an external symbol operand to this instr
456   ///
457   void addExternalSymbolOperand(const char *SymName) {
458     operands.push_back(MachineOperand(SymName, 0));
459   }
460
461   //===--------------------------------------------------------------------===//
462   // Accessors used to modify instructions in place.
463   //
464   // FIXME: Move this stuff to MachineOperand itself!
465
466   /// setOpcode - Replace the opcode of the current instruction with a new one.
467   ///
468   void setOpcode(unsigned Op) { Opcode = Op; }
469
470   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
471   /// fewer operand than it started with.
472   ///
473   void RemoveOperand(unsigned i) {
474     operands.erase(operands.begin()+i);
475   }
476
477   // Access to set the operands when building the machine instruction
478   //
479   void SetMachineOperandVal(unsigned i,
480                             MachineOperand::MachineOperandType operandType,
481                             Value* V);
482
483   void SetMachineOperandConst(unsigned i,
484                               MachineOperand::MachineOperandType operandType,
485                               int intValue);
486
487   void SetMachineOperandReg(unsigned i, int regNum);
488 };
489
490 //===----------------------------------------------------------------------===//
491 // Debugging Support
492
493 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
494 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
495
496 } // End llvm namespace
497
498 #endif