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