add a method for hacking on JTIdx's
[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 //===----------------------------------------------------------------------===//
36 // class MachineOperand
37 //
38 //   Representation of each machine instruction operand.
39 //
40 struct MachineOperand {
41   enum MachineOperandType {
42     MO_Register,                // Register operand.
43     MO_Immediate,               // Immediate Operand
44     MO_MachineBasicBlock,       // MachineBasicBlock reference
45     MO_FrameIndex,              // Abstract Stack Frame Index
46     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
47     MO_JumpTableIndex,          // Address of indexed Jump Table for switch
48     MO_ExternalSymbol,          // Name of external global symbol
49     MO_GlobalAddress            // Address of a global value
50   };
51
52 private:
53   union {
54     GlobalValue *GV;          // For MO_GlobalAddress.
55     MachineBasicBlock *MBB;   // For MO_MachineBasicBlock.
56     const char *SymbolName;   // For MO_ExternalSymbol.
57     unsigned RegNo;           // For MO_Register.
58     int64_t immedVal;         // For MO_Immediate and MO_*Index.
59   } contents;
60
61   MachineOperandType opType:8; // Discriminate the union.
62   bool IsDef : 1;              // True if this is a def, false if this is a use.
63   
64   /// offset - Offset to address of global or external, only valid for
65   /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex
66   int offset;
67
68   MachineOperand() {}
69 public:
70   MachineOperand(const MachineOperand &M) {
71     *this = M;
72   }
73   
74   ~MachineOperand() {}
75   
76   static MachineOperand CreateImm(int64_t Val) {
77     MachineOperand Op;
78     Op.opType = MachineOperand::MO_Immediate;
79     Op.contents.immedVal = Val;
80     Op.IsDef = false;
81     Op.offset = 0;
82     return Op;
83   }
84   
85   const MachineOperand &operator=(const MachineOperand &MO) {
86     contents = MO.contents;
87     IsDef    = MO.IsDef;
88     opType   = MO.opType;
89     offset   = MO.offset;
90     return *this;
91   }
92
93   /// getType - Returns the MachineOperandType for this operand.
94   ///
95   MachineOperandType getType() const { return opType; }
96
97   /// Accessors that tell you what kind of MachineOperand you're looking at.
98   ///
99   bool isReg() const { return opType == MO_Register; }
100   bool isImm() const { return opType == MO_Immediate; }
101   bool isMBB() const { return opType == MO_MachineBasicBlock; }
102   
103   bool isRegister() const { return opType == MO_Register; }
104   bool isImmediate() const { return opType == MO_Immediate; }
105   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
106   bool isFrameIndex() const { return opType == MO_FrameIndex; }
107   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
108   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
109   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
110   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
111
112   int64_t getImm() const {
113     assert(isImm() && "Wrong MachineOperand accessor");
114     return contents.immedVal;
115   }
116   
117   int64_t getImmedValue() const {
118     assert(isImm() && "Wrong MachineOperand accessor");
119     return contents.immedVal;
120   }
121   MachineBasicBlock *getMBB() const {
122     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
123     return contents.MBB;
124   }
125   MachineBasicBlock *getMachineBasicBlock() const {
126     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
127     return contents.MBB;
128   }
129   void setMachineBasicBlock(MachineBasicBlock *MBB) {
130     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
131     contents.MBB = MBB;
132   }
133   int getFrameIndex() const {
134     assert(isFrameIndex() && "Wrong MachineOperand accessor");
135     return (int)contents.immedVal;
136   }
137   unsigned getConstantPoolIndex() const {
138     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
139     return (unsigned)contents.immedVal;
140   }
141   unsigned getJumpTableIndex() const {
142     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
143     return (unsigned)contents.immedVal;
144   }
145   GlobalValue *getGlobal() const {
146     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
147     return contents.GV;
148   }
149   int getOffset() const {
150     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
151         "Wrong MachineOperand accessor");
152     return offset;
153   }
154   const char *getSymbolName() const {
155     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
156     return contents.SymbolName;
157   }
158
159   bool isUse() const { 
160     assert(isRegister() && "Wrong MachineOperand accessor");
161     return !IsDef;
162   }
163   bool isDef() const {
164     assert(isRegister() && "Wrong MachineOperand accessor");
165     return IsDef;
166   }
167   void setIsUse() {
168     assert(isRegister() && "Wrong MachineOperand accessor");
169     IsDef = false;
170   }
171   void setIsDef() {
172     assert(isRegister() && "Wrong MachineOperand accessor");
173     IsDef = true;
174   }
175
176   /// getReg - Returns the register number.
177   ///
178   unsigned getReg() const {
179     assert(isRegister() && "This is not a register operand!");
180     return contents.RegNo;
181   }
182
183   /// MachineOperand mutators.
184   ///
185   void setReg(unsigned Reg) {
186     assert(isRegister() && "This is not a register operand!");
187     contents.RegNo = Reg;
188   }
189
190   void setImmedValue(int64_t immVal) {
191     assert(isImm() && "Wrong MachineOperand mutator");
192     contents.immedVal = immVal;
193   }
194   void setImm(int64_t immVal) {
195     assert(isImm() && "Wrong MachineOperand mutator");
196     contents.immedVal = immVal;
197   }
198
199   void setOffset(int Offset) {
200     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
201             isJumpTableIndex()) &&
202         "Wrong MachineOperand accessor");
203     offset = Offset;
204   }
205   void setConstantPoolIndex(unsigned Idx) {
206     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
207     contents.immedVal = Idx;
208   }
209   void setJumpTableIndex(unsigned Idx) {
210     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
211     contents.immedVal = Idx;
212   }
213   
214   /// isIdenticalTo - Return true if this operand is identical to the specified
215   /// operand.
216   bool isIdenticalTo(const MachineOperand &Other) const;
217   
218   /// ChangeToImmediate - Replace this operand with a new immediate operand of
219   /// the specified value.  If an operand is known to be an immediate already,
220   /// the setImmedValue method should be used.
221   void ChangeToImmediate(int64_t ImmVal) {
222     opType = MO_Immediate;
223     contents.immedVal = ImmVal;
224   }
225
226   /// ChangeToRegister - Replace this operand with a new register operand of
227   /// the specified value.  If an operand is known to be an register already,
228   /// the setReg method should be used.
229   void ChangeToRegister(unsigned Reg, bool isDef) {
230     opType = MO_Register;
231     contents.RegNo = Reg;
232     IsDef = isDef;
233   }
234
235   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
236
237   friend class MachineInstr;
238 };
239
240
241 //===----------------------------------------------------------------------===//
242 /// MachineInstr - Representation of each machine instruction.
243 ///
244 class MachineInstr {
245   short Opcode;                         // the opcode
246   std::vector<MachineOperand> Operands; // the operands
247   MachineInstr* prev, *next;            // links for our intrusive list
248   MachineBasicBlock* parent;            // pointer to the owning basic block
249
250   // OperandComplete - Return true if it's illegal to add a new operand
251   bool OperandsComplete() const;
252
253   MachineInstr(const MachineInstr&);
254   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
255
256   // Intrusive list support
257   //
258   friend struct ilist_traits<MachineInstr>;
259
260 public:
261   /// MachineInstr ctor - This constructor reserve's space for numOperand
262   /// operands.
263   MachineInstr(short Opcode, unsigned numOperands);
264
265   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
266   /// the MachineInstr is created and added to the end of the specified basic
267   /// block.
268   ///
269   MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
270
271   ~MachineInstr();
272
273   const MachineBasicBlock* getParent() const { return parent; }
274   MachineBasicBlock* getParent() { return parent; }
275
276   /// getOpcode - Returns the opcode of this MachineInstr.
277   ///
278   const int getOpcode() const { return Opcode; }
279
280   /// Access to explicit operands of the instruction.
281   ///
282   unsigned getNumOperands() const { return Operands.size(); }
283
284   const MachineOperand& getOperand(unsigned i) const {
285     assert(i < getNumOperands() && "getOperand() out of range!");
286     return Operands[i];
287   }
288   MachineOperand& getOperand(unsigned i) {
289     assert(i < getNumOperands() && "getOperand() out of range!");
290     return Operands[i];
291   }
292
293   
294   /// isIdenticalTo - Return true if this instruction is identical to (same
295   /// opcode and same operands as) the specified instruction.
296   bool isIdenticalTo(const MachineInstr *Other) const {
297     if (Other->getOpcode() != getOpcode() ||
298         Other->getNumOperands() != getNumOperands())
299       return false;
300     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
301       if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
302         return false;
303     return true;
304   }
305
306   /// clone - Create a copy of 'this' instruction that is identical in
307   /// all ways except the the instruction has no parent, prev, or next.
308   MachineInstr* clone() const { return new MachineInstr(*this); }
309   
310   /// removeFromParent - This method unlinks 'this' from the containing basic
311   /// block, and returns it, but does not delete it.
312   MachineInstr *removeFromParent();
313   
314   /// eraseFromParent - This method unlinks 'this' from the containing basic
315   /// block and deletes it.
316   void eraseFromParent() {
317     delete removeFromParent();
318   }
319
320   //
321   // Debugging support
322   //
323   void print(std::ostream &OS, const TargetMachine *TM) const;
324   void dump() const;
325   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
326
327   //===--------------------------------------------------------------------===//
328   // Accessors to add operands when building up machine instructions.
329   //
330
331   /// addRegOperand - Add a register operand.
332   ///
333   void addRegOperand(unsigned Reg, bool IsDef) {
334     MachineOperand &Op = AddNewOperand();
335     Op.opType = MachineOperand::MO_Register;
336     Op.IsDef = IsDef;
337     Op.contents.RegNo = Reg;
338     Op.offset = 0;
339   }
340
341   /// addImmOperand - Add a zero extended constant argument to the
342   /// machine instruction.
343   ///
344   void addImmOperand(int64_t Val) {
345     MachineOperand &Op = AddNewOperand();
346     Op.opType = MachineOperand::MO_Immediate;
347     Op.contents.immedVal = Val;
348     Op.offset = 0;
349   }
350
351   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
352     MachineOperand &Op = AddNewOperand();
353     Op.opType = MachineOperand::MO_MachineBasicBlock;
354     Op.contents.MBB = MBB;
355     Op.offset = 0;
356   }
357
358   /// addFrameIndexOperand - Add an abstract frame index to the instruction
359   ///
360   void addFrameIndexOperand(unsigned Idx) {
361     MachineOperand &Op = AddNewOperand();
362     Op.opType = MachineOperand::MO_FrameIndex;
363     Op.contents.immedVal = Idx;
364     Op.offset = 0;
365   }
366
367   /// addConstantPoolndexOperand - Add a constant pool object index to the
368   /// instruction.
369   ///
370   void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
371     MachineOperand &Op = AddNewOperand();
372     Op.opType = MachineOperand::MO_ConstantPoolIndex;
373     Op.contents.immedVal = Idx;
374     Op.offset = Offset;
375   }
376
377   /// addJumpTableIndexOperand - Add a jump table object index to the
378   /// instruction.
379   ///
380   void addJumpTableIndexOperand(unsigned Idx) {
381     MachineOperand &Op = AddNewOperand();
382     Op.opType = MachineOperand::MO_JumpTableIndex;
383     Op.contents.immedVal = Idx;
384     Op.offset = 0;
385   }
386   
387   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
388     MachineOperand &Op = AddNewOperand();
389     Op.opType = MachineOperand::MO_GlobalAddress;
390     Op.contents.GV = GV;
391     Op.offset = Offset;
392   }
393
394   /// addExternalSymbolOperand - Add an external symbol operand to this instr
395   ///
396   void addExternalSymbolOperand(const char *SymName) {
397     MachineOperand &Op = AddNewOperand();
398     Op.opType = MachineOperand::MO_ExternalSymbol;
399     Op.contents.SymbolName = SymName;
400     Op.offset = 0;
401   }
402
403   //===--------------------------------------------------------------------===//
404   // Accessors used to modify instructions in place.
405   //
406
407   /// setOpcode - Replace the opcode of the current instruction with a new one.
408   ///
409   void setOpcode(unsigned Op) { Opcode = Op; }
410
411   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
412   /// fewer operand than it started with.
413   ///
414   void RemoveOperand(unsigned i) {
415     Operands.erase(Operands.begin()+i);
416   }
417 private:
418   MachineOperand &AddNewOperand() {
419     assert(!OperandsComplete() &&
420            "Trying to add an operand to a machine instr that is already done!");
421     Operands.push_back(MachineOperand());
422     return Operands.back();
423   }
424 };
425
426 //===----------------------------------------------------------------------===//
427 // Debugging Support
428
429 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
430 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
431
432 } // End llvm namespace
433
434 #endif