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