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