Add an "alignment" field to the MachineFunction object. It makes more sense to
[oota-llvm.git] / include / llvm / CodeGen / MachineOperand.h
1 //===-- llvm/CodeGen/MachineOperand.h - MachineOperand 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 MachineOperand class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEOPERAND_H
15 #define LLVM_CODEGEN_MACHINEOPERAND_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <cassert>
19 #include <iosfwd>
20
21 namespace llvm {
22   
23 class ConstantFP;
24 class MachineBasicBlock;
25 class GlobalValue;
26 class MachineInstr;
27 class TargetMachine;
28 class MachineRegisterInfo;
29 class raw_ostream;
30   
31 /// MachineOperand class - Representation of each machine instruction operand.
32 ///
33 class MachineOperand {
34 public:
35   enum MachineOperandType {
36     MO_Register,               ///< Register operand.
37     MO_Immediate,              ///< Immediate operand
38     MO_FPImmediate,            ///< Floating-point immediate operand
39     MO_MachineBasicBlock,      ///< MachineBasicBlock reference
40     MO_FrameIndex,             ///< Abstract Stack Frame Index
41     MO_ConstantPoolIndex,      ///< Address of indexed Constant in Constant Pool
42     MO_JumpTableIndex,         ///< Address of indexed Jump Table for switch
43     MO_ExternalSymbol,         ///< Name of external global symbol
44     MO_GlobalAddress           ///< Address of a global value
45   };
46
47 private:
48   /// OpKind - Specify what kind of operand this is.  This discriminates the
49   /// union.
50   unsigned char OpKind; // MachineOperandType
51   
52   /// SubReg - Subregister number, only valid for MO_Register.  A value of 0
53   /// indicates the MO_Register has no subReg.
54   unsigned char SubReg;
55   
56   /// TargetFlags - This is a set of target-specific operand flags.
57   unsigned char TargetFlags;
58   
59   /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
60   /// operands.
61   
62   /// IsDef - True if this is a def, false if this is a use of the register.
63   ///
64   bool IsDef : 1;
65   
66   /// IsImp - True if this is an implicit def or use, false if it is explicit.
67   ///
68   bool IsImp : 1;
69
70   /// IsKill - True if this instruction is the last use of the register on this
71   /// path through the function.  This is only valid on uses of registers.
72   bool IsKill : 1;
73
74   /// IsDead - True if this register is never used by a subsequent instruction.
75   /// This is only valid on definitions of registers.
76   bool IsDead : 1;
77
78   /// IsUndef - True if this is a register def / use of "undef", i.e. register
79   /// defined by an IMPLICIT_DEF. This is only valid on registers.
80   bool IsUndef : 1;
81
82   /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
83   /// by the MachineInstr before all input registers are read.  This is used to
84   /// model the GCC inline asm '&' constraint modifier.
85   bool IsEarlyClobber : 1;
86
87   /// ParentMI - This is the instruction that this operand is embedded into. 
88   /// This is valid for all operand types, when the operand is in an instr.
89   MachineInstr *ParentMI;
90
91   /// Contents union - This contains the payload for the various operand types.
92   union {
93     MachineBasicBlock *MBB;   // For MO_MachineBasicBlock.
94     const ConstantFP *CFP;    // For MO_FPImmediate.
95     int64_t ImmVal;           // For MO_Immediate.
96
97     struct {                  // For MO_Register.
98       unsigned RegNo;
99       MachineOperand **Prev;  // Access list for register.
100       MachineOperand *Next;
101     } Reg;
102     
103     /// OffsetedInfo - This struct contains the offset and an object identifier.
104     /// this represent the object as with an optional offset from it.
105     struct {
106       union {
107         int Index;                // For MO_*Index - The index itself.
108         const char *SymbolName;   // For MO_ExternalSymbol.
109         GlobalValue *GV;          // For MO_GlobalAddress.
110       } Val;
111       int64_t Offset;   // An offset from the object.
112     } OffsetedInfo;
113   } Contents;
114   
115   explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
116     TargetFlags = 0;
117   }
118 public:
119   MachineOperand(const MachineOperand &M) {
120     *this = M;
121   }
122   
123   ~MachineOperand() {}
124   
125   /// getType - Returns the MachineOperandType for this operand.
126   ///
127   MachineOperandType getType() const { return (MachineOperandType)OpKind; }
128   
129   unsigned char getTargetFlags() const { return TargetFlags; }
130   void setTargetFlags(unsigned char F) { TargetFlags = F; }
131   void addTargetFlag(unsigned char F) { TargetFlags |= F; }
132   
133
134   /// getParent - Return the instruction that this operand belongs to.
135   ///
136   MachineInstr *getParent() { return ParentMI; }
137   const MachineInstr *getParent() const { return ParentMI; }
138   
139   void print(std::ostream &os, const TargetMachine *TM = 0) const;
140   void print(raw_ostream &os, const TargetMachine *TM = 0) const;
141
142   //===--------------------------------------------------------------------===//
143   // Accessors that tell you what kind of MachineOperand you're looking at.
144   //===--------------------------------------------------------------------===//
145
146   /// isReg - Tests if this is a MO_Register operand.
147   bool isReg() const { return OpKind == MO_Register; }
148   /// isImm - Tests if this is a MO_Immediate operand.
149   bool isImm() const { return OpKind == MO_Immediate; }
150   /// isFPImm - Tests if this is a MO_FPImmediate operand.
151   bool isFPImm() const { return OpKind == MO_FPImmediate; }
152   /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
153   bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
154   /// isFI - Tests if this is a MO_FrameIndex operand.
155   bool isFI() const { return OpKind == MO_FrameIndex; }
156   /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
157   bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
158   /// isJTI - Tests if this is a MO_JumpTableIndex operand.
159   bool isJTI() const { return OpKind == MO_JumpTableIndex; }
160   /// isGlobal - Tests if this is a MO_GlobalAddress operand.
161   bool isGlobal() const { return OpKind == MO_GlobalAddress; }
162   /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
163   bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
164
165   //===--------------------------------------------------------------------===//
166   // Accessors for Register Operands
167   //===--------------------------------------------------------------------===//
168
169   /// getReg - Returns the register number.
170   unsigned getReg() const {
171     assert(isReg() && "This is not a register operand!");
172     return Contents.Reg.RegNo;
173   }
174   
175   unsigned getSubReg() const {
176     assert(isReg() && "Wrong MachineOperand accessor");
177     return (unsigned)SubReg;
178   }
179   
180   bool isUse() const { 
181     assert(isReg() && "Wrong MachineOperand accessor");
182     return !IsDef;
183   }
184   
185   bool isDef() const {
186     assert(isReg() && "Wrong MachineOperand accessor");
187     return IsDef;
188   }
189   
190   bool isImplicit() const { 
191     assert(isReg() && "Wrong MachineOperand accessor");
192     return IsImp;
193   }
194   
195   bool isDead() const {
196     assert(isReg() && "Wrong MachineOperand accessor");
197     return IsDead;
198   }
199   
200   bool isKill() const {
201     assert(isReg() && "Wrong MachineOperand accessor");
202     return IsKill;
203   }
204   
205   bool isUndef() const {
206     assert(isReg() && "Wrong MachineOperand accessor");
207     return IsUndef;
208   }
209   
210   bool isEarlyClobber() const {
211     assert(isReg() && "Wrong MachineOperand accessor");
212     return IsEarlyClobber;
213   }
214
215   /// getNextOperandForReg - Return the next MachineOperand in the function that
216   /// uses or defines this register.
217   MachineOperand *getNextOperandForReg() const {
218     assert(isReg() && "This is not a register operand!");
219     return Contents.Reg.Next;
220   }
221
222   //===--------------------------------------------------------------------===//
223   // Mutators for Register Operands
224   //===--------------------------------------------------------------------===//
225   
226   /// Change the register this operand corresponds to.
227   ///
228   void setReg(unsigned Reg);
229   
230   void setSubReg(unsigned subReg) {
231     assert(isReg() && "Wrong MachineOperand accessor");
232     SubReg = (unsigned char)subReg;
233   }
234   
235   void setIsUse(bool Val = true) {
236     assert(isReg() && "Wrong MachineOperand accessor");
237     IsDef = !Val;
238   }
239   
240   void setIsDef(bool Val = true) {
241     assert(isReg() && "Wrong MachineOperand accessor");
242     IsDef = Val;
243   }
244
245   void setImplicit(bool Val = true) { 
246     assert(isReg() && "Wrong MachineOperand accessor");
247     IsImp = Val;
248   }
249
250   void setIsKill(bool Val = true) {
251     assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
252     IsKill = Val;
253   }
254   
255   void setIsDead(bool Val = true) {
256     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
257     IsDead = Val;
258   }
259
260   void setIsUndef(bool Val = true) {
261     assert(isReg() && "Wrong MachineOperand accessor");
262     IsUndef = Val;
263   }
264   
265   void setIsEarlyClobber(bool Val = true) {
266     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
267     IsEarlyClobber = Val;
268   }
269
270   //===--------------------------------------------------------------------===//
271   // Accessors for various operand types.
272   //===--------------------------------------------------------------------===//
273   
274   int64_t getImm() const {
275     assert(isImm() && "Wrong MachineOperand accessor");
276     return Contents.ImmVal;
277   }
278   
279   const ConstantFP *getFPImm() const {
280     assert(isFPImm() && "Wrong MachineOperand accessor");
281     return Contents.CFP;
282   }
283   
284   MachineBasicBlock *getMBB() const {
285     assert(isMBB() && "Wrong MachineOperand accessor");
286     return Contents.MBB;
287   }
288
289   int getIndex() const {
290     assert((isFI() || isCPI() || isJTI()) &&
291            "Wrong MachineOperand accessor");
292     return Contents.OffsetedInfo.Val.Index;
293   }
294   
295   GlobalValue *getGlobal() const {
296     assert(isGlobal() && "Wrong MachineOperand accessor");
297     return Contents.OffsetedInfo.Val.GV;
298   }
299   
300   int64_t getOffset() const {
301     assert((isGlobal() || isSymbol() || isCPI()) &&
302            "Wrong MachineOperand accessor");
303     return Contents.OffsetedInfo.Offset;
304   }
305   
306   const char *getSymbolName() const {
307     assert(isSymbol() && "Wrong MachineOperand accessor");
308     return Contents.OffsetedInfo.Val.SymbolName;
309   }
310   
311   //===--------------------------------------------------------------------===//
312   // Mutators for various operand types.
313   //===--------------------------------------------------------------------===//
314   
315   void setImm(int64_t immVal) {
316     assert(isImm() && "Wrong MachineOperand mutator");
317     Contents.ImmVal = immVal;
318   }
319
320   void setOffset(int64_t Offset) {
321     assert((isGlobal() || isSymbol() || isCPI()) &&
322         "Wrong MachineOperand accessor");
323     Contents.OffsetedInfo.Offset = Offset;
324   }
325   
326   void setIndex(int Idx) {
327     assert((isFI() || isCPI() || isJTI()) &&
328            "Wrong MachineOperand accessor");
329     Contents.OffsetedInfo.Val.Index = Idx;
330   }
331   
332   void setMBB(MachineBasicBlock *MBB) {
333     assert(isMBB() && "Wrong MachineOperand accessor");
334     Contents.MBB = MBB;
335   }
336   
337   //===--------------------------------------------------------------------===//
338   // Other methods.
339   //===--------------------------------------------------------------------===//
340   
341   /// isIdenticalTo - Return true if this operand is identical to the specified
342   /// operand. Note: This method ignores isKill and isDead properties.
343   bool isIdenticalTo(const MachineOperand &Other) const;
344   
345   /// ChangeToImmediate - Replace this operand with a new immediate operand of
346   /// the specified value.  If an operand is known to be an immediate already,
347   /// the setImm method should be used.
348   void ChangeToImmediate(int64_t ImmVal);
349   
350   /// ChangeToRegister - Replace this operand with a new register operand of
351   /// the specified value.  If an operand is known to be an register already,
352   /// the setReg method should be used.
353   void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
354                         bool isKill = false, bool isDead = false,
355                         bool isUndef = false);
356   
357   //===--------------------------------------------------------------------===//
358   // Construction methods.
359   //===--------------------------------------------------------------------===//
360   
361   static MachineOperand CreateImm(int64_t Val) {
362     MachineOperand Op(MachineOperand::MO_Immediate);
363     Op.setImm(Val);
364     return Op;
365   }
366   
367   static MachineOperand CreateFPImm(const ConstantFP *CFP) {
368     MachineOperand Op(MachineOperand::MO_FPImmediate);
369     Op.Contents.CFP = CFP;
370     return Op;
371   }
372   
373   static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
374                                   bool isKill = false, bool isDead = false,
375                                   bool isUndef = false,
376                                   bool isEarlyClobber = false,
377                                   unsigned SubReg = 0) {
378     MachineOperand Op(MachineOperand::MO_Register);
379     Op.IsDef = isDef;
380     Op.IsImp = isImp;
381     Op.IsKill = isKill;
382     Op.IsDead = isDead;
383     Op.IsUndef = isUndef;
384     Op.IsEarlyClobber = isEarlyClobber;
385     Op.Contents.Reg.RegNo = Reg;
386     Op.Contents.Reg.Prev = 0;
387     Op.Contents.Reg.Next = 0;
388     Op.SubReg = SubReg;
389     return Op;
390   }
391   static MachineOperand CreateMBB(MachineBasicBlock *MBB,
392                                   unsigned char TargetFlags = 0) {
393     MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
394     Op.setMBB(MBB);
395     Op.setTargetFlags(TargetFlags);
396     return Op;
397   }
398   static MachineOperand CreateFI(unsigned Idx) {
399     MachineOperand Op(MachineOperand::MO_FrameIndex);
400     Op.setIndex(Idx);
401     return Op;
402   }
403   static MachineOperand CreateCPI(unsigned Idx, int Offset,
404                                   unsigned char TargetFlags = 0) {
405     MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
406     Op.setIndex(Idx);
407     Op.setOffset(Offset);
408     Op.setTargetFlags(TargetFlags);
409     return Op;
410   }
411   static MachineOperand CreateJTI(unsigned Idx,
412                                   unsigned char TargetFlags = 0) {
413     MachineOperand Op(MachineOperand::MO_JumpTableIndex);
414     Op.setIndex(Idx);
415     Op.setTargetFlags(TargetFlags);
416     return Op;
417   }
418   static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
419                                  unsigned char TargetFlags = 0) {
420     MachineOperand Op(MachineOperand::MO_GlobalAddress);
421     Op.Contents.OffsetedInfo.Val.GV = GV;
422     Op.setOffset(Offset);
423     Op.setTargetFlags(TargetFlags);
424     return Op;
425   }
426   static MachineOperand CreateES(const char *SymName, int64_t Offset = 0,
427                                  unsigned char TargetFlags = 0) {
428     MachineOperand Op(MachineOperand::MO_ExternalSymbol);
429     Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
430     Op.setOffset(Offset);
431     Op.setTargetFlags(TargetFlags);
432     return Op;
433   }
434   const MachineOperand &operator=(const MachineOperand &MO) {
435     OpKind   = MO.OpKind;
436     IsDef    = MO.IsDef;
437     IsImp    = MO.IsImp;
438     IsKill   = MO.IsKill;
439     IsDead   = MO.IsDead;
440     IsUndef  = MO.IsUndef;
441     IsEarlyClobber = MO.IsEarlyClobber;
442     SubReg   = MO.SubReg;
443     ParentMI = MO.ParentMI;
444     Contents = MO.Contents;
445     TargetFlags = MO.TargetFlags;
446     return *this;
447   }
448
449   friend class MachineInstr;
450   friend class MachineRegisterInfo;
451 private:
452   //===--------------------------------------------------------------------===//
453   // Methods for handling register use/def lists.
454   //===--------------------------------------------------------------------===//
455
456   /// isOnRegUseList - Return true if this operand is on a register use/def list
457   /// or false if not.  This can only be called for register operands that are
458   /// part of a machine instruction.
459   bool isOnRegUseList() const {
460     assert(isReg() && "Can only add reg operand to use lists");
461     return Contents.Reg.Prev != 0;
462   }
463   
464   /// AddRegOperandToRegInfo - Add this register operand to the specified
465   /// MachineRegisterInfo.  If it is null, then the next/prev fields should be
466   /// explicitly nulled out.
467   void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
468
469   /// RemoveRegOperandFromRegInfo - Remove this register operand from the
470   /// MachineRegisterInfo it is linked with.
471   void RemoveRegOperandFromRegInfo();
472 };
473
474 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
475   MO.print(OS, 0);
476   return OS;
477 }
478
479 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
480   MO.print(OS, 0);
481   return OS;
482 }
483
484 } // End llvm namespace
485
486 #endif