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