Move copyKillDeadInfo out-of-line. Add findRegisterUseOperand().
[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   const TargetInstrDescriptor *TID;     // Instruction descriptor.
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   /// TID NULL 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   /// getInstrDescriptor - Returns the target instruction descriptor of this
342   /// MachineInstr.
343   const TargetInstrDescriptor *getInstrDescriptor() const { return TID; }
344
345   /// getOpcode - Returns the opcode of this MachineInstr.
346   ///
347   const int getOpcode() const;
348
349   /// Access to explicit operands of the instruction.
350   ///
351   unsigned getNumOperands() const { return Operands.size(); }
352
353   const MachineOperand& getOperand(unsigned i) const {
354     assert(i < getNumOperands() && "getOperand() out of range!");
355     return Operands[i];
356   }
357   MachineOperand& getOperand(unsigned i) {
358     assert(i < getNumOperands() && "getOperand() out of range!");
359     return Operands[i];
360   }
361
362   
363   /// isIdenticalTo - Return true if this instruction is identical to (same
364   /// opcode and same operands as) the specified instruction.
365   bool isIdenticalTo(const MachineInstr *Other) const {
366     if (Other->getOpcode() != getOpcode() ||
367         Other->getNumOperands() != getNumOperands())
368       return false;
369     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
370       if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
371         return false;
372     return true;
373   }
374
375   /// clone - Create a copy of 'this' instruction that is identical in
376   /// all ways except the the instruction has no parent, prev, or next.
377   MachineInstr* clone() const { return new MachineInstr(*this); }
378   
379   /// removeFromParent - This method unlinks 'this' from the containing basic
380   /// block, and returns it, but does not delete it.
381   MachineInstr *removeFromParent();
382   
383   /// eraseFromParent - This method unlinks 'this' from the containing basic
384   /// block and deletes it.
385   void eraseFromParent() {
386     delete removeFromParent();
387   }
388
389   /// findRegisterUseOperand() - Returns the MachineOperand that is a use of
390   /// the specific register or NULL if it is not found.
391   MachineOperand *findRegisterUseOperand(unsigned Reg);
392   
393   /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
394   ///
395   void copyKillDeadInfo(const MachineInstr *MI);
396
397   //
398   // Debugging support
399   //
400   void print(llvm_ostream &OS, const TargetMachine *TM) const {
401     if (OS.stream()) print(*OS.stream(), TM);
402   }
403   void print(std::ostream &OS, const TargetMachine *TM) const;
404   void dump() const;
405   friend llvm_ostream& operator<<(llvm_ostream& os, const MachineInstr& minstr){
406     if (os.stream()) *os.stream() << minstr;
407     return os;
408   }
409   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
410
411   //===--------------------------------------------------------------------===//
412   // Accessors to add operands when building up machine instructions.
413   //
414
415   /// addRegOperand - Add a register operand.
416   ///
417   void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
418                      bool IsKill = false, bool IsDead = false) {
419     MachineOperand &Op = AddNewOperand(IsImp);
420     Op.opType = MachineOperand::MO_Register;
421     Op.IsDef = IsDef;
422     Op.IsImp = IsImp;
423     Op.IsKill = IsKill;
424     Op.IsDead = IsDead;
425     Op.contents.RegNo = Reg;
426     Op.offset = 0;
427   }
428
429   /// addImmOperand - Add a zero extended constant argument to the
430   /// machine instruction.
431   ///
432   void addImmOperand(int64_t Val) {
433     MachineOperand &Op = AddNewOperand();
434     Op.opType = MachineOperand::MO_Immediate;
435     Op.contents.immedVal = Val;
436     Op.offset = 0;
437   }
438
439   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
440     MachineOperand &Op = AddNewOperand();
441     Op.opType = MachineOperand::MO_MachineBasicBlock;
442     Op.contents.MBB = MBB;
443     Op.offset = 0;
444   }
445
446   /// addFrameIndexOperand - Add an abstract frame index to the instruction
447   ///
448   void addFrameIndexOperand(unsigned Idx) {
449     MachineOperand &Op = AddNewOperand();
450     Op.opType = MachineOperand::MO_FrameIndex;
451     Op.contents.immedVal = Idx;
452     Op.offset = 0;
453   }
454
455   /// addConstantPoolndexOperand - Add a constant pool object index to the
456   /// instruction.
457   ///
458   void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
459     MachineOperand &Op = AddNewOperand();
460     Op.opType = MachineOperand::MO_ConstantPoolIndex;
461     Op.contents.immedVal = Idx;
462     Op.offset = Offset;
463   }
464
465   /// addJumpTableIndexOperand - Add a jump table object index to the
466   /// instruction.
467   ///
468   void addJumpTableIndexOperand(unsigned Idx) {
469     MachineOperand &Op = AddNewOperand();
470     Op.opType = MachineOperand::MO_JumpTableIndex;
471     Op.contents.immedVal = Idx;
472     Op.offset = 0;
473   }
474   
475   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
476     MachineOperand &Op = AddNewOperand();
477     Op.opType = MachineOperand::MO_GlobalAddress;
478     Op.contents.GV = GV;
479     Op.offset = Offset;
480   }
481
482   /// addExternalSymbolOperand - Add an external symbol operand to this instr
483   ///
484   void addExternalSymbolOperand(const char *SymName) {
485     MachineOperand &Op = AddNewOperand();
486     Op.opType = MachineOperand::MO_ExternalSymbol;
487     Op.contents.SymbolName = SymName;
488     Op.offset = 0;
489   }
490
491   //===--------------------------------------------------------------------===//
492   // Accessors used to modify instructions in place.
493   //
494
495   /// setInstrDescriptor - Replace the instruction descriptor (thus opcode) of
496   /// the current instruction with a new one.
497   ///
498   void setInstrDescriptor(const TargetInstrDescriptor &tid) { TID = &tid; }
499
500   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
501   /// fewer operand than it started with.
502   ///
503   void RemoveOperand(unsigned i) {
504     Operands.erase(Operands.begin()+i);
505   }
506 private:
507   MachineOperand &AddNewOperand(bool IsImp = false) {
508     assert((IsImp || !OperandsComplete()) &&
509            "Trying to add an operand to a machine instr that is already done!");
510     if (NumImplicitOps == 0) { // This is true most of the time.
511       Operands.push_back(MachineOperand());
512       return Operands.back();
513     } else {
514       return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
515                               MachineOperand());
516     }
517   }
518
519   /// addImplicitDefUseOperands - Add all implicit def and use operands to
520   /// this instruction.
521   void addImplicitDefUseOperands();
522 };
523
524 //===----------------------------------------------------------------------===//
525 // Debugging Support
526
527 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
528 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
529
530 } // End llvm namespace
531
532 #endif