setOperand should not zap the operand list or add implicit operands to an
[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 #include <iosfwd>
24
25 namespace llvm {
26
27 class Value;
28 class Function;
29 class MachineBasicBlock;
30 class TargetInstrInfo;
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 std::ostream& operator<<(std::ostream& os, const MachineOperand& mop);
289
290   friend class MachineInstr;
291 };
292
293
294 //===----------------------------------------------------------------------===//
295 /// MachineInstr - Representation of each machine instruction.
296 ///
297 class MachineInstr {
298   short Opcode;                         // the opcode
299   short NumImplicitOps;                 // Number of implicit operands (which
300                                         // are determined at construction time).
301
302   std::vector<MachineOperand> Operands; // the operands
303   MachineInstr* prev, *next;            // links for our intrusive list
304   MachineBasicBlock* parent;            // pointer to the owning basic block
305
306   // OperandComplete - Return true if it's illegal to add a new operand
307   bool OperandsComplete() const;
308
309   MachineInstr(const MachineInstr&);
310   void operator=(const MachineInstr&); // DO NOT IMPLEMENT
311
312   // Intrusive list support
313   //
314   friend struct ilist_traits<MachineInstr>;
315
316 public:
317   /// MachineInstr ctor - This constructor reserves space for numOperand
318   /// operands.
319   MachineInstr(short Opcode, unsigned numOperands);
320
321   /// MachineInstr ctor - This constructor create a MachineInstr and add the
322   /// implicit operands. It reserves space for numOperand operands.
323   MachineInstr(const TargetInstrInfo &TII, short Opcode, unsigned numOperands);
324
325   /// MachineInstr ctor - Work exactly the same as the ctor above, except that
326   /// the MachineInstr is created and added to the end of the specified basic
327   /// block.
328   ///
329   MachineInstr(MachineBasicBlock *MBB, short Opcode, unsigned numOps);
330
331   ~MachineInstr();
332
333   const MachineBasicBlock* getParent() const { return parent; }
334   MachineBasicBlock* getParent() { return parent; }
335
336   /// getOpcode - Returns the opcode of this MachineInstr.
337   ///
338   const int getOpcode() const { return Opcode; }
339
340   /// Access to explicit operands of the instruction.
341   ///
342   unsigned getNumOperands() const { return Operands.size(); }
343
344   const MachineOperand& getOperand(unsigned i) const {
345     assert(i < getNumOperands() && "getOperand() out of range!");
346     return Operands[i];
347   }
348   MachineOperand& getOperand(unsigned i) {
349     assert(i < getNumOperands() && "getOperand() out of range!");
350     return Operands[i];
351   }
352
353   
354   /// isIdenticalTo - Return true if this instruction is identical to (same
355   /// opcode and same operands as) the specified instruction.
356   bool isIdenticalTo(const MachineInstr *Other) const {
357     if (Other->getOpcode() != getOpcode() ||
358         Other->getNumOperands() != getNumOperands())
359       return false;
360     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
361       if (!getOperand(i).isIdenticalTo(Other->getOperand(i)))
362         return false;
363     return true;
364   }
365
366   /// clone - Create a copy of 'this' instruction that is identical in
367   /// all ways except the the instruction has no parent, prev, or next.
368   MachineInstr* clone() const { return new MachineInstr(*this); }
369   
370   /// removeFromParent - This method unlinks 'this' from the containing basic
371   /// block, and returns it, but does not delete it.
372   MachineInstr *removeFromParent();
373   
374   /// eraseFromParent - This method unlinks 'this' from the containing basic
375   /// block and deletes it.
376   void eraseFromParent() {
377     delete removeFromParent();
378   }
379
380   /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
381   ///
382   void copyKillDeadInfo(const MachineInstr *MI) {
383     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
384       const MachineOperand &MO = MI->getOperand(i);
385       if (MO.isReg() && (MO.isKill() || MO.isDead())) {
386         for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
387           MachineOperand &MOp = getOperand(j);
388           if (MOp.isIdenticalTo(MO)) {
389             if (MO.isKill())
390               MOp.setIsKill();
391             else
392               MOp.setIsDead();
393             break;
394           }
395         }
396       }
397     }
398   }
399
400   //
401   // Debugging support
402   //
403   void print(std::ostream &OS, const TargetMachine *TM) const;
404   void dump() const;
405   friend std::ostream& operator<<(std::ostream& os, const MachineInstr& minstr);
406
407   //===--------------------------------------------------------------------===//
408   // Accessors to add operands when building up machine instructions.
409   //
410
411   /// addRegOperand - Add a register operand.
412   ///
413   void addRegOperand(unsigned Reg, bool IsDef, bool IsImp = false,
414                      bool IsKill = false, bool IsDead = false) {
415     MachineOperand &Op = AddNewOperand(IsImp);
416     Op.opType = MachineOperand::MO_Register;
417     Op.IsDef = IsDef;
418     Op.IsImp = IsImp;
419     Op.IsKill = IsKill;
420     Op.IsDead = IsDead;
421     Op.contents.RegNo = Reg;
422     Op.offset = 0;
423   }
424
425   /// addImmOperand - Add a zero extended constant argument to the
426   /// machine instruction.
427   ///
428   void addImmOperand(int64_t Val) {
429     MachineOperand &Op = AddNewOperand();
430     Op.opType = MachineOperand::MO_Immediate;
431     Op.contents.immedVal = Val;
432     Op.offset = 0;
433   }
434
435   void addMachineBasicBlockOperand(MachineBasicBlock *MBB) {
436     MachineOperand &Op = AddNewOperand();
437     Op.opType = MachineOperand::MO_MachineBasicBlock;
438     Op.contents.MBB = MBB;
439     Op.offset = 0;
440   }
441
442   /// addFrameIndexOperand - Add an abstract frame index to the instruction
443   ///
444   void addFrameIndexOperand(unsigned Idx) {
445     MachineOperand &Op = AddNewOperand();
446     Op.opType = MachineOperand::MO_FrameIndex;
447     Op.contents.immedVal = Idx;
448     Op.offset = 0;
449   }
450
451   /// addConstantPoolndexOperand - Add a constant pool object index to the
452   /// instruction.
453   ///
454   void addConstantPoolIndexOperand(unsigned Idx, int Offset) {
455     MachineOperand &Op = AddNewOperand();
456     Op.opType = MachineOperand::MO_ConstantPoolIndex;
457     Op.contents.immedVal = Idx;
458     Op.offset = Offset;
459   }
460
461   /// addJumpTableIndexOperand - Add a jump table object index to the
462   /// instruction.
463   ///
464   void addJumpTableIndexOperand(unsigned Idx) {
465     MachineOperand &Op = AddNewOperand();
466     Op.opType = MachineOperand::MO_JumpTableIndex;
467     Op.contents.immedVal = Idx;
468     Op.offset = 0;
469   }
470   
471   void addGlobalAddressOperand(GlobalValue *GV, int Offset) {
472     MachineOperand &Op = AddNewOperand();
473     Op.opType = MachineOperand::MO_GlobalAddress;
474     Op.contents.GV = GV;
475     Op.offset = Offset;
476   }
477
478   /// addExternalSymbolOperand - Add an external symbol operand to this instr
479   ///
480   void addExternalSymbolOperand(const char *SymName) {
481     MachineOperand &Op = AddNewOperand();
482     Op.opType = MachineOperand::MO_ExternalSymbol;
483     Op.contents.SymbolName = SymName;
484     Op.offset = 0;
485   }
486
487   //===--------------------------------------------------------------------===//
488   // Accessors used to modify instructions in place.
489   //
490
491   /// setOpcode - Replace the opcode of the current instruction with a new one.
492   ///
493   void setOpcode(unsigned Op) { Opcode = Op; }
494
495   /// RemoveOperand - Erase an operand  from an instruction, leaving it with one
496   /// fewer operand than it started with.
497   ///
498   void RemoveOperand(unsigned i) {
499     Operands.erase(Operands.begin()+i);
500   }
501 private:
502   MachineOperand &AddNewOperand(bool IsImp = false) {
503     assert((IsImp || !OperandsComplete()) &&
504            "Trying to add an operand to a machine instr that is already done!");
505     if (NumImplicitOps == 0) { // This is true most of the time.
506       Operands.push_back(MachineOperand());
507       return Operands.back();
508     } else {
509       return *Operands.insert(Operands.begin()+Operands.size()-NumImplicitOps,
510                               MachineOperand());
511     }
512   }
513
514   /// addImplicitDefUseOperands - Add all implicit def and use operands to
515   /// this instruction.
516   void addImplicitDefUseOperands(const TargetInstrDescriptor &TID);
517 };
518
519 //===----------------------------------------------------------------------===//
520 // Debugging Support
521
522 std::ostream& operator<<(std::ostream &OS, const MachineInstr &MI);
523 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
524
525 } // End llvm namespace
526
527 #endif