Change "value" in MachineOperand to be a GlobalValue, as that is the only
[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     GlobalValue *GV;    // LLVM global for MO_GlobalAddress.
78     int64_t immedVal;   // Constant value for an explicit constant
79     MachineBasicBlock *MBB;     // For MO_MachineBasicBlock type
80     const char *SymbolName;     // For MO_ExternalSymbol type
81   } contents;
82
83   char flags;                   // see bit field definitions above
84   MachineOperandType opType:8;  // Pack into 8 bits efficiently after flags.
85   union {
86     int regNum;     // register number for an explicit register
87     int offset;     // Offset to address of global or external, only
88                     // valid for MO_GlobalAddress, MO_ExternalSym
89                     // and MO_ConstantPoolIndex
90   } extra;
91
92   void zeroContents() {
93     contents.immedVal = 0;
94     extra.offset = 0;
95   }
96
97   MachineOperand(int64_t ImmVal, MachineOperandType OpTy, int Offset = 0)
98     : flags(0), opType(OpTy) {
99     contents.immedVal = ImmVal;
100     extra.offset = Offset;
101   }
102
103   MachineOperand(int Reg, MachineOperandType OpTy, UseType UseTy)
104     : flags(UseTy), opType(OpTy) {
105     zeroContents();
106     extra.regNum = Reg;
107   }
108
109   MachineOperand(GlobalValue *V, int Offset = 0)
110     : flags(MachineOperand::Use), opType(MachineOperand::MO_GlobalAddress) {
111     contents.GV = V;
112     extra.offset = Offset;
113   }
114
115   MachineOperand(MachineBasicBlock *mbb)
116     : flags(0), opType(MO_MachineBasicBlock) {
117     zeroContents ();
118     contents.MBB = mbb;
119   }
120
121   MachineOperand(const char *SymName, int Offset)
122     : flags(0), opType(MO_ExternalSymbol) {
123     zeroContents ();
124     contents.SymbolName = SymName;
125     extra.offset = Offset;
126   }
127
128 public:
129   MachineOperand(const MachineOperand &M)
130     : flags(M.flags), opType(M.opType) {
131     zeroContents ();
132     contents = M.contents;
133     extra = M.extra;
134   }
135
136   ~MachineOperand() {}
137
138   const MachineOperand &operator=(const MachineOperand &MO) {
139     contents = MO.contents;
140     flags    = MO.flags;
141     opType   = MO.opType;
142     extra    = MO.extra;
143     return *this;
144   }
145
146   /// getType - Returns the MachineOperandType for this operand.
147   ///
148   MachineOperandType getType() const { return opType; }
149
150   /// getUseType - Returns the MachineOperandUseType of this operand.
151   ///
152   UseType getUseType() const { return UseType(flags & (USEFLAG|DEFFLAG)); }
153
154   /// isRegister - Return true if this operand is a register operand.
155   ///
156   bool isRegister() const {
157     return opType == MO_VirtualRegister;
158   }
159
160   /// Accessors that tell you what kind of MachineOperand you're looking at.
161   ///
162   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
163   bool isImmediate() const {
164     return opType == MO_SignExtendedImmed || opType == MO_UnextendedImmed;
165   }
166   bool isFrameIndex() const { return opType == MO_FrameIndex; }
167   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
168   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
169   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
170   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
171
172   int64_t getImmedValue() const {
173     assert(isImmediate() && "Wrong MachineOperand accessor");
174     return contents.immedVal;
175   }
176   MachineBasicBlock *getMachineBasicBlock() const {
177     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
178     return contents.MBB;
179   }
180   void setMachineBasicBlock(MachineBasicBlock *MBB) {
181     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
182     contents.MBB = MBB;
183   }
184   int getFrameIndex() const {
185     assert(isFrameIndex() && "Wrong MachineOperand accessor");
186     return (int)contents.immedVal;
187   }
188   unsigned getConstantPoolIndex() const {
189     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
190     return (unsigned)contents.immedVal;
191   }
192   unsigned getJumpTableIndex() const {
193     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
194     return (unsigned)contents.immedVal;
195   }
196   GlobalValue *getGlobal() const {
197     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
198     return contents.GV;
199   }
200   int getOffset() const {
201     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
202         "Wrong MachineOperand accessor");
203     return extra.offset;
204   }
205   const char *getSymbolName() const {
206     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
207     return contents.SymbolName;
208   }
209
210   /// MachineOperand methods for testing that work on any kind of
211   /// MachineOperand...
212   ///
213   bool            isUse           () const { return flags & USEFLAG; }
214   MachineOperand& setUse          ()       { flags |= USEFLAG; return *this; }
215   bool            isDef           () const { return flags & DEFFLAG; }
216   MachineOperand& setDef          ()       { flags |= DEFFLAG; return *this; }
217
218   /// hasAllocatedReg - Returns true iff a machine register has been
219   /// allocated to this operand.
220   ///
221   bool hasAllocatedReg() const {
222     return extra.regNum >= 0 && opType == MO_VirtualRegister;
223   }
224
225   /// getReg - Returns the register number. It is a runtime error to call this
226   /// if a register is not allocated.
227   ///
228   unsigned getReg() const {
229     assert(hasAllocatedReg());
230     return extra.regNum;
231   }
232
233   /// MachineOperand mutators.
234   ///
235   void setReg(unsigned Reg) {
236     assert(hasAllocatedReg() && "This operand cannot have a register number!");
237     extra.regNum = Reg;
238   }
239
240   void setImmedValue(int immVal) {
241     assert(isImmediate() && "Wrong MachineOperand mutator");
242     contents.immedVal = immVal;
243   }
244
245   void setOffset(int Offset) {
246     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
247             isJumpTableIndex()) &&
248         "Wrong MachineOperand accessor");
249     extra.offset = Offset;
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_UnextendedImmed));
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(
393       MachineOperand(intValue, MachineOperand::MO_UnextendedImmed));
394   }
395
396   /// addSignExtImmOperand - Add a zero extended constant argument to the
397   /// machine instruction.
398   ///
399   void addSignExtImmOperand(int intValue) {
400     assert(!OperandsComplete() &&
401            "Trying to add an operand to a machine instr that is already done!");
402     operands.push_back(
403       MachineOperand(intValue, MachineOperand::MO_SignExtendedImmed));
404   }
405
406   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
407     assert(!OperandsComplete() &&
408            "Trying to add an operand to a machine instr that is already done!");
409     operands.push_back(MachineOperand(MBB));
410   }
411
412   /// addFrameIndexOperand - Add an abstract frame index to the instruction
413   ///
414   void addFrameIndexOperand(unsigned Idx) {
415     assert(!OperandsComplete() &&
416            "Trying to add an operand to a machine instr that is already done!");
417     operands.push_back(MachineOperand(Idx, MachineOperand::MO_FrameIndex));
418   }
419
420   /// addConstantPoolndexOperand - Add a constant pool object index to the
421   /// instruction.
422   ///
423   void addConstantPoolIndexOperand(unsigned I, int Offset=0) {
424     assert(!OperandsComplete() &&
425            "Trying to add an operand to a machine instr that is already done!");
426     operands.push_back(MachineOperand(I, MachineOperand::MO_ConstantPoolIndex));
427   }
428
429   /// addJumpTableIndexOperand - Add a jump table object index to the
430   /// instruction.
431   ///
432   void addJumpTableIndexOperand(unsigned I) {
433     assert(!OperandsComplete() &&
434            "Trying to add an operand to a machine instr that is already done!");
435     operands.push_back(MachineOperand(I, MachineOperand::MO_JumpTableIndex));
436   }
437   
438   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
439     assert(!OperandsComplete() &&
440            "Trying to add an operand to a machine instr that is already done!");
441     operands.push_back(MachineOperand(GV, Offset));
442   }
443
444   /// addExternalSymbolOperand - Add an external symbol operand to this instr
445   ///
446   void addExternalSymbolOperand(const char *SymName) {
447     operands.push_back(MachineOperand(SymName, 0));
448   }
449
450   //===--------------------------------------------------------------------===//
451   // Accessors used to modify instructions in place.
452   //
453   // FIXME: Move this stuff to MachineOperand itself!
454
455   /// setOpcode - Replace the opcode of the current instruction with a new one.
456   ///
457   void setOpcode(unsigned Op) { Opcode = Op; }
458
459   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
460   /// fewer operand than it started with.
461   ///
462   void RemoveOperand(unsigned i) {
463     operands.erase(operands.begin()+i);
464   }
465
466   // Access to set the operands when building the machine instruction
467   //
468   void SetMachineOperandConst(unsigned i,
469                               MachineOperand::MachineOperandType operandType,
470                               int intValue);
471
472   void SetMachineOperandReg(unsigned i, int regNum);
473 };
474
475 //===----------------------------------------------------------------------===//
476 // Debugging Support
477
478 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
479 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
480
481 } // End llvm namespace
482
483 #endif