11a769c930c98656e2c662860f5c89c5839fe891
[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 "llvm/Support/Streams.h"
22 #include <vector>
23 #include <cassert>
24 #include <iosfwd>
25
26 namespace llvm {
27
28 class Value;
29 class Function;
30 class MachineBasicBlock;
31 class TargetInstrDescriptor;
32 class TargetMachine;
33 class GlobalValue;
34
35 template <typename T> struct ilist_traits;
36 template <typename T> struct ilist;
37
38 //===----------------------------------------------------------------------===//
39 // class MachineOperand
40 //
41 //   Representation of each machine instruction operand.
42 //
43 struct MachineOperand {
44   enum MachineOperandType {
45     MO_Register,                // Register operand.
46     MO_Immediate,               // Immediate Operand
47     MO_MachineBasicBlock,       // MachineBasicBlock reference
48     MO_FrameIndex,              // Abstract Stack Frame Index
49     MO_ConstantPoolIndex,       // Address of indexed Constant in Constant Pool
50     MO_JumpTableIndex,          // Address of indexed Jump Table for switch
51     MO_ExternalSymbol,          // Name of external global symbol
52     MO_GlobalAddress            // Address of a global value
53   };
54
55 private:
56   union {
57     GlobalValue *GV;          // For MO_GlobalAddress.
58     MachineBasicBlock *MBB;   // For MO_MachineBasicBlock.
59     const char *SymbolName;   // For MO_ExternalSymbol.
60     unsigned RegNo;           // For MO_Register.
61     int64_t immedVal;         // For MO_Immediate and MO_*Index.
62   } contents;
63
64   MachineOperandType opType:8; // Discriminate the union.
65   bool IsDef : 1;              // True if this is a def, false if this is a use.
66   bool IsImp : 1;              // True if this is an implicit def or use.
67
68   bool IsKill : 1;             // True if this is a reg use and the reg is dead
69                                // immediately after the read.
70   bool IsDead : 1;             // True if this is a reg def and the reg is dead
71                                // immediately after the write. i.e. A register
72                                // that is defined but never used.
73   
74   /// offset - Offset to address of global or external, only valid for
75   /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex
76   int offset;
77
78   MachineOperand() {}
79 public:
80   MachineOperand(const MachineOperand &M) {
81     *this = M;
82   }
83   
84   ~MachineOperand() {}
85   
86   static MachineOperand CreateImm(int64_t Val) {
87     MachineOperand Op;
88     Op.opType = MachineOperand::MO_Immediate;
89     Op.contents.immedVal = Val;
90     Op.IsDef = false;
91     Op.IsImp = false;
92     Op.IsKill = false;
93     Op.IsDead = false;
94     Op.offset = 0;
95     return Op;
96   }
97   
98   const MachineOperand &operator=(const MachineOperand &MO) {
99     contents = MO.contents;
100     IsDef    = MO.IsDef;
101     IsImp    = MO.IsImp;
102     IsKill   = MO.IsKill;
103     IsDead   = MO.IsDead;
104     opType   = MO.opType;
105     offset   = MO.offset;
106     return *this;
107   }
108
109   /// getType - Returns the MachineOperandType for this operand.
110   ///
111   MachineOperandType getType() const { return opType; }
112
113   /// Accessors that tell you what kind of MachineOperand you're looking at.
114   ///
115   bool isReg() const { return opType == MO_Register; }
116   bool isImm() const { return opType == MO_Immediate; }
117   bool isMBB() const { return opType == MO_MachineBasicBlock; }
118   
119   bool isRegister() const { return opType == MO_Register; }
120   bool isImmediate() const { return opType == MO_Immediate; }
121   bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
122   bool isFrameIndex() const { return opType == MO_FrameIndex; }
123   bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
124   bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
125   bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
126   bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
127
128   int64_t getImm() const {
129     assert(isImm() && "Wrong MachineOperand accessor");
130     return contents.immedVal;
131   }
132   
133   int64_t getImmedValue() const {
134     assert(isImm() && "Wrong MachineOperand accessor");
135     return contents.immedVal;
136   }
137   MachineBasicBlock *getMBB() const {
138     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
139     return contents.MBB;
140   }
141   MachineBasicBlock *getMachineBasicBlock() const {
142     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
143     return contents.MBB;
144   }
145   void setMachineBasicBlock(MachineBasicBlock *MBB) {
146     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
147     contents.MBB = MBB;
148   }
149   int getFrameIndex() const {
150     assert(isFrameIndex() && "Wrong MachineOperand accessor");
151     return (int)contents.immedVal;
152   }
153   unsigned getConstantPoolIndex() const {
154     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
155     return (unsigned)contents.immedVal;
156   }
157   unsigned getJumpTableIndex() const {
158     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
159     return (unsigned)contents.immedVal;
160   }
161   GlobalValue *getGlobal() const {
162     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
163     return contents.GV;
164   }
165   int getOffset() const {
166     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
167         "Wrong MachineOperand accessor");
168     return offset;
169   }
170   const char *getSymbolName() const {
171     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
172     return contents.SymbolName;
173   }
174
175   bool isUse() const { 
176     assert(isRegister() && "Wrong MachineOperand accessor");
177     return !IsDef;
178   }
179   bool isDef() const {
180     assert(isRegister() && "Wrong MachineOperand accessor");
181     return IsDef;
182   }
183   void setIsUse() {
184     assert(isRegister() && "Wrong MachineOperand accessor");
185     IsDef = false;
186   }
187   void setIsDef() {
188     assert(isRegister() && "Wrong MachineOperand accessor");
189     IsDef = true;
190   }
191
192   bool isImplicit() const { 
193     assert(isRegister() && "Wrong MachineOperand accessor");
194     return IsImp;
195   }
196   void setImplicit() { 
197     assert(isRegister() && "Wrong MachineOperand accessor");
198     IsImp = true;
199   }
200
201   bool isKill() const {
202     assert(isRegister() && "Wrong MachineOperand accessor");
203     return IsKill;
204   }
205   bool isDead() const {
206     assert(isRegister() && "Wrong MachineOperand accessor");
207     return IsDead;
208   }
209   void setIsKill() {
210     assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
211     IsKill = true;
212   }
213   void setIsDead() {
214     assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
215     IsDead = true;
216   }
217   void unsetIsKill() {
218     assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
219     IsKill = false;
220   }
221   void unsetIsDead() {
222     assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
223     IsDead = false;
224   }
225
226   /// getReg - Returns the register number.
227   ///
228   unsigned getReg() const {
229     assert(isRegister() && "This is not a register operand!");
230     return contents.RegNo;
231   }
232
233   /// MachineOperand mutators.
234   ///
235   void setReg(unsigned Reg) {
236     assert(isRegister() && "This is not a register operand!");
237     contents.RegNo = Reg;
238   }
239
240   void setImmedValue(int64_t immVal) {
241     assert(isImm() && "Wrong MachineOperand mutator");
242     contents.immedVal = immVal;
243   }
244   void setImm(int64_t immVal) {
245     assert(isImm() && "Wrong MachineOperand mutator");
246     contents.immedVal = immVal;
247   }
248
249   void setOffset(int Offset) {
250     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
251             isJumpTableIndex()) &&
252         "Wrong MachineOperand accessor");
253     offset = Offset;
254   }
255   void setConstantPoolIndex(unsigned Idx) {
256     assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
257     contents.immedVal = Idx;
258   }
259   void setJumpTableIndex(unsigned Idx) {
260     assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
261     contents.immedVal = Idx;
262   }
263   
264   /// isIdenticalTo - Return true if this operand is identical to the specified
265   /// operand. Note: This method ignores isKill and isDead properties.
266   bool isIdenticalTo(const MachineOperand &Other) const;
267   
268   /// ChangeToImmediate - Replace this operand with a new immediate operand of
269   /// the specified value.  If an operand is known to be an immediate already,
270   /// the setImmedValue method should be used.
271   void ChangeToImmediate(int64_t ImmVal) {
272     opType = MO_Immediate;
273     contents.immedVal = ImmVal;
274   }
275
276   /// ChangeToRegister - Replace this operand with a new register operand of
277   /// the specified value.  If an operand is known to be an register already,
278   /// the setReg method should be used.
279   void ChangeToRegister(unsigned Reg, bool isDef) {
280     opType = MO_Register;
281     contents.RegNo = Reg;
282     IsDef = isDef;
283     IsImp = false;
284     IsKill = false;
285     IsDead = false;
286   }
287
288   friend llvm_ostream& operator<<(llvm_ostream& os, const MachineOperand& mop) {
289     if (os.stream()) *os.stream() << mop;
290     return os;
291   }
292   friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
293
294   friend class MachineInstr;
295 };
296
297
298 //===----------------------------------------------------------------------===//
299 /// MachineInstr - Representation of each machine instruction.
300 ///
301 class MachineInstr {
302   short Opcode;                         // the opcode
303   unsigned short NumImplicitOps;        // Number of implicit operands (which
304                                         // are determined at construction time).
305
306   std::vector<MachineOperand> Operands; // the operands
307   MachineInstr* prev, *next;            // links for our intrusive list
308   MachineBasicBlock* parent;            // pointer to the owning basic block
309
310   // OperandComplete - Return true if it's illegal to add a new operand
311   bool OperandsComplete() const;
312
313   MachineInstr(const MachineInstr&);
314   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
315
316   // Intrusive list support
317   //
318   friend struct ilist_traits<MachineInstr>;
319
320 public:
321   /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
322   /// opcode 0 and no operands.
323   MachineInstr();
324
325   /// MachineInstr ctor - This constructor create a MachineInstr and add the
326   /// implicit operands. It reserves space for number of operands specified by
327   /// TargetInstrDescriptor.
328   MachineInstr(const TargetInstrDescriptor &TID);
329
330   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
331   /// the MachineInstr is created and added to the end of the specified basic
332   /// block.
333   ///
334   MachineInstr(MachineBasicBlock *MBB, const TargetInstrDescriptor &TID);
335
336   ~MachineInstr();
337
338   const MachineBasicBlock* getParent() const { return parent; }
339   MachineBasicBlock* getParent() { return parent; }
340
341   /// getOpcode - Returns the opcode of this MachineInstr.
342   ///
343   const int getOpcode() const { return Opcode; }
344
345   /// Access to explicit operands of the instruction.
346   ///
347   unsigned getNumOperands() const { return Operands.size(); }
348
349   const MachineOperand& getOperand(unsigned i) const {
350     assert(i < getNumOperands() && "getOperand() out of range!");
351     return Operands[i];
352   }
353   MachineOperand& getOperand(unsigned i) {
354     assert(i < getNumOperands() && "getOperand() out of range!");
355     return Operands[i];
356   }
357
358   
359   /// isIdenticalTo - Return true if this instruction is identical to (same
360   /// opcode and same operands as) the specified instruction.
361   bool isIdenticalTo(const MachineInstr *Other) const {
362     if (Other->getOpcode() != getOpcode() ||
363         Other->getNumOperands() != getNumOperands())
364       return false;
365     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
366       if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
367         return false;
368     return true;
369   }
370
371   /// clone - Create a copy of 'this' instruction that is identical in
372   /// all ways except the the instruction has no parent, prev, or next.
373   MachineInstr* clone() const { return new MachineInstr(*this); }
374   
375   /// removeFromParent - This method unlinks 'this' from the containing basic
376   /// block, and returns it, but does not delete it.
377   MachineInstr *removeFromParent();
378   
379   /// eraseFromParent - This method unlinks 'this' from the containing basic
380   /// block and deletes it.
381   void eraseFromParent() {
382     delete removeFromParent();
383   }
384
385   /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
386   ///
387   void copyKillDeadInfo(const MachineInstr *MI) {
388     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
389       const MachineOperand &MO = MI->getOperand(i);
390       if (MO.isReg() && (MO.isKill() || MO.isDead())) {
391         for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
392           MachineOperand &MOp = getOperand(j);
393           if (MOp.isIdenticalTo(MO)) {
394             if (MO.isKill())
395               MOp.setIsKill();
396             else
397               MOp.setIsDead();
398             break;
399           }
400         }
401       }
402     }
403   }
404
405   //
406   // Debugging support
407   //
408   void print(llvm_ostream &OS, const TargetMachine *TM) const {
409     if (OS.stream()) print(*OS.stream(), TM);
410   }
411   void print(std::ostream &OS, const TargetMachine *TM) const;
412   void dump() const;
413   friend llvm_ostream& operator<<(llvm_ostream& os, const MachineInstr& minstr){
414     if (os.stream()) *os.stream() << minstr;
415     return os;
416   }
417   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
418
419   //===--------------------------------------------------------------------===//
420   // Accessors to add operands when building up machine instructions.
421   //
422
423   /// addRegOperand - Add a register operand.
424   ///
425   void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
426                      bool IsKill = false, bool IsDead = false) {
427     MachineOperand &Op = AddNewOperand(IsImp);
428     Op.opType = MachineOperand::MO_Register;
429     Op.IsDef = IsDef;
430     Op.IsImp = IsImp;
431     Op.IsKill = IsKill;
432     Op.IsDead = IsDead;
433     Op.contents.RegNo = Reg;
434     Op.offset = 0;
435   }
436
437   /// addImmOperand - Add a zero extended constant argument to the
438   /// machine instruction.
439   ///
440   void addImmOperand(int64_t Val) {
441     MachineOperand &Op = AddNewOperand();
442     Op.opType = MachineOperand::MO_Immediate;
443     Op.contents.immedVal = Val;
444     Op.offset = 0;
445   }
446
447   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
448     MachineOperand &Op = AddNewOperand();
449     Op.opType = MachineOperand::MO_MachineBasicBlock;
450     Op.contents.MBB = MBB;
451     Op.offset = 0;
452   }
453
454   /// addFrameIndexOperand - Add an abstract frame index to the instruction
455   ///
456   void addFrameIndexOperand(unsigned Idx) {
457     MachineOperand &Op = AddNewOperand();
458     Op.opType = MachineOperand::MO_FrameIndex;
459     Op.contents.immedVal = Idx;
460     Op.offset = 0;
461   }
462
463   /// addConstantPoolndexOperand - Add a constant pool object index to the
464   /// instruction.
465   ///
466   void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
467     MachineOperand &Op = AddNewOperand();
468     Op.opType = MachineOperand::MO_ConstantPoolIndex;
469     Op.contents.immedVal = Idx;
470     Op.offset = Offset;
471   }
472
473   /// addJumpTableIndexOperand - Add a jump table object index to the
474   /// instruction.
475   ///
476   void addJumpTableIndexOperand(unsigned Idx) {
477     MachineOperand &Op = AddNewOperand();
478     Op.opType = MachineOperand::MO_JumpTableIndex;
479     Op.contents.immedVal = Idx;
480     Op.offset = 0;
481   }
482   
483   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
484     MachineOperand &Op = AddNewOperand();
485     Op.opType = MachineOperand::MO_GlobalAddress;
486     Op.contents.GV = GV;
487     Op.offset = Offset;
488   }
489
490   /// addExternalSymbolOperand - Add an external symbol operand to this instr
491   ///
492   void addExternalSymbolOperand(const char *SymName) {
493     MachineOperand &Op = AddNewOperand();
494     Op.opType = MachineOperand::MO_ExternalSymbol;
495     Op.contents.SymbolName = SymName;
496     Op.offset = 0;
497   }
498
499   //===--------------------------------------------------------------------===//
500   // Accessors used to modify instructions in place.
501   //
502
503   /// setOpcode - Replace the opcode of the current instruction with a new one.
504   ///
505   void setOpcode(unsigned Op) { Opcode = Op; }
506
507   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
508   /// fewer operand than it started with.
509   ///
510   void RemoveOperand(unsigned i) {
511     Operands.erase(Operands.begin()+i);
512   }
513 private:
514   MachineOperand &AddNewOperand(bool IsImp = false) {
515     assert((IsImp || !OperandsComplete()) &&
516            "Trying to add an operand to a machine instr that is already done!");
517     if (NumImplicitOps == 0) { // This is true most of the time.
518       Operands.push_back(MachineOperand());
519       return Operands.back();
520     } else {
521       return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
522                               MachineOperand());
523     }
524   }
525
526   /// addImplicitDefUseOperands - Add all implicit def and use operands to
527   /// this instruction.
528   void addImplicitDefUseOperands(const TargetInstrDescriptor &TID);
529 };
530
531 //===----------------------------------------------------------------------===//
532 // Debugging Support
533
534 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
535 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
536
537 } // End llvm namespace
538
539 #endif