Add a Debug bit to MachineOperand, for uses that
[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 ConstantFP;
23 class BlockAddress;
24 class MachineBasicBlock;
25 class GlobalValue;
26 class MachineInstr;
27 class TargetMachine;
28 class MachineRegisterInfo;
29 class MDNode;
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     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     assert(!isDef() && "Wrong MachineOperand accessor");
224     return IsDebug;
225   }
226
227   /// getNextOperandForReg - Return the next MachineOperand in the function that
228   /// uses or defines this register.
229   MachineOperand *getNextOperandForReg() const {
230     assert(isReg() && "This is not a register operand!");
231     return Contents.Reg.Next;
232   }
233
234   //===--------------------------------------------------------------------===//
235   // Mutators for Register Operands
236   //===--------------------------------------------------------------------===//
237   
238   /// Change the register this operand corresponds to.
239   ///
240   void setReg(unsigned Reg);
241   
242   void setSubReg(unsigned subReg) {
243     assert(isReg() && "Wrong MachineOperand accessor");
244     SubReg = (unsigned char)subReg;
245   }
246   
247   void setIsUse(bool Val = true) {
248     assert(isReg() && "Wrong MachineOperand accessor");
249     IsDef = !Val;
250   }
251   
252   void setIsDef(bool Val = true) {
253     assert(isReg() && "Wrong MachineOperand accessor");
254     IsDef = Val;
255   }
256
257   void setImplicit(bool Val = true) { 
258     assert(isReg() && "Wrong MachineOperand accessor");
259     IsImp = Val;
260   }
261
262   void setIsKill(bool Val = true) {
263     assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
264     IsKill = Val;
265   }
266   
267   void setIsDead(bool Val = true) {
268     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
269     IsDead = Val;
270   }
271
272   void setIsUndef(bool Val = true) {
273     assert(isReg() && "Wrong MachineOperand accessor");
274     IsUndef = Val;
275   }
276   
277   void setIsEarlyClobber(bool Val = true) {
278     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
279     IsEarlyClobber = Val;
280   }
281
282   //===--------------------------------------------------------------------===//
283   // Accessors for various operand types.
284   //===--------------------------------------------------------------------===//
285   
286   int64_t getImm() const {
287     assert(isImm() && "Wrong MachineOperand accessor");
288     return Contents.ImmVal;
289   }
290   
291   const ConstantFP *getFPImm() const {
292     assert(isFPImm() && "Wrong MachineOperand accessor");
293     return Contents.CFP;
294   }
295   
296   MachineBasicBlock *getMBB() const {
297     assert(isMBB() && "Wrong MachineOperand accessor");
298     return Contents.MBB;
299   }
300
301   int getIndex() const {
302     assert((isFI() || isCPI() || isJTI()) &&
303            "Wrong MachineOperand accessor");
304     return Contents.OffsetedInfo.Val.Index;
305   }
306   
307   GlobalValue *getGlobal() const {
308     assert(isGlobal() && "Wrong MachineOperand accessor");
309     return Contents.OffsetedInfo.Val.GV;
310   }
311
312   BlockAddress *getBlockAddress() const {
313     assert(isBlockAddress() && "Wrong MachineOperand accessor");
314     return Contents.OffsetedInfo.Val.BA;
315   }
316   
317   /// getOffset - Return the offset from the symbol in this operand. This always
318   /// returns 0 for ExternalSymbol operands.
319   int64_t getOffset() const {
320     assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
321            "Wrong MachineOperand accessor");
322     return Contents.OffsetedInfo.Offset;
323   }
324   
325   const char *getSymbolName() const {
326     assert(isSymbol() && "Wrong MachineOperand accessor");
327     return Contents.OffsetedInfo.Val.SymbolName;
328   }
329
330   const MDNode *getMetadata() const {
331     assert(isMetadata() && "Wrong MachineOperand accessor");
332     return Contents.MD;
333   }
334   
335   //===--------------------------------------------------------------------===//
336   // Mutators for various operand types.
337   //===--------------------------------------------------------------------===//
338   
339   void setImm(int64_t immVal) {
340     assert(isImm() && "Wrong MachineOperand mutator");
341     Contents.ImmVal = immVal;
342   }
343
344   void setOffset(int64_t Offset) {
345     assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
346         "Wrong MachineOperand accessor");
347     Contents.OffsetedInfo.Offset = Offset;
348   }
349   
350   void setIndex(int Idx) {
351     assert((isFI() || isCPI() || isJTI()) &&
352            "Wrong MachineOperand accessor");
353     Contents.OffsetedInfo.Val.Index = Idx;
354   }
355   
356   void setMBB(MachineBasicBlock *MBB) {
357     assert(isMBB() && "Wrong MachineOperand accessor");
358     Contents.MBB = MBB;
359   }
360   
361   //===--------------------------------------------------------------------===//
362   // Other methods.
363   //===--------------------------------------------------------------------===//
364   
365   /// isIdenticalTo - Return true if this operand is identical to the specified
366   /// operand. Note: This method ignores isKill and isDead properties.
367   bool isIdenticalTo(const MachineOperand &Other) const;
368   
369   /// ChangeToImmediate - Replace this operand with a new immediate operand of
370   /// the specified value.  If an operand is known to be an immediate already,
371   /// the setImm method should be used.
372   void ChangeToImmediate(int64_t ImmVal);
373   
374   /// ChangeToRegister - Replace this operand with a new register operand of
375   /// the specified value.  If an operand is known to be an register already,
376   /// the setReg method should be used.
377   void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
378                         bool isKill = false, bool isDead = false,
379                         bool isUndef = false);
380   
381   //===--------------------------------------------------------------------===//
382   // Construction methods.
383   //===--------------------------------------------------------------------===//
384   
385   static MachineOperand CreateImm(int64_t Val) {
386     MachineOperand Op(MachineOperand::MO_Immediate);
387     Op.setImm(Val);
388     return Op;
389   }
390   
391   static MachineOperand CreateFPImm(const ConstantFP *CFP) {
392     MachineOperand Op(MachineOperand::MO_FPImmediate);
393     Op.Contents.CFP = CFP;
394     return Op;
395   }
396   
397   static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
398                                   bool isKill = false, bool isDead = false,
399                                   bool isUndef = false,
400                                   bool isEarlyClobber = false,
401                                   unsigned SubReg = 0,
402                                   bool isDebug = false) {
403     MachineOperand Op(MachineOperand::MO_Register);
404     Op.IsDef = isDef;
405     Op.IsImp = isImp;
406     Op.IsKill = isKill;
407     Op.IsDead = isDead;
408     Op.IsUndef = isUndef;
409     Op.IsEarlyClobber = isEarlyClobber;
410     Op.IsDebug = isDebug;
411     Op.Contents.Reg.RegNo = Reg;
412     Op.Contents.Reg.Prev = 0;
413     Op.Contents.Reg.Next = 0;
414     Op.SubReg = SubReg;
415     return Op;
416   }
417   static MachineOperand CreateMBB(MachineBasicBlock *MBB,
418                                   unsigned char TargetFlags = 0) {
419     MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
420     Op.setMBB(MBB);
421     Op.setTargetFlags(TargetFlags);
422     return Op;
423   }
424   static MachineOperand CreateFI(unsigned Idx) {
425     MachineOperand Op(MachineOperand::MO_FrameIndex);
426     Op.setIndex(Idx);
427     return Op;
428   }
429   static MachineOperand CreateCPI(unsigned Idx, int Offset,
430                                   unsigned char TargetFlags = 0) {
431     MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
432     Op.setIndex(Idx);
433     Op.setOffset(Offset);
434     Op.setTargetFlags(TargetFlags);
435     return Op;
436   }
437   static MachineOperand CreateJTI(unsigned Idx,
438                                   unsigned char TargetFlags = 0) {
439     MachineOperand Op(MachineOperand::MO_JumpTableIndex);
440     Op.setIndex(Idx);
441     Op.setTargetFlags(TargetFlags);
442     return Op;
443   }
444   static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
445                                  unsigned char TargetFlags = 0) {
446     MachineOperand Op(MachineOperand::MO_GlobalAddress);
447     Op.Contents.OffsetedInfo.Val.GV = GV;
448     Op.setOffset(Offset);
449     Op.setTargetFlags(TargetFlags);
450     return Op;
451   }
452   static MachineOperand CreateES(const char *SymName,
453                                  unsigned char TargetFlags = 0) {
454     MachineOperand Op(MachineOperand::MO_ExternalSymbol);
455     Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
456     Op.setOffset(0); // Offset is always 0.
457     Op.setTargetFlags(TargetFlags);
458     return Op;
459   }
460   static MachineOperand CreateBA(BlockAddress *BA,
461                                  unsigned char TargetFlags = 0) {
462     MachineOperand Op(MachineOperand::MO_BlockAddress);
463     Op.Contents.OffsetedInfo.Val.BA = BA;
464     Op.setOffset(0); // Offset is always 0.
465     Op.setTargetFlags(TargetFlags);
466     return Op;
467   }
468   static MachineOperand CreateMetadata(MDNode *Meta) {
469     MachineOperand Op(MachineOperand::MO_Metadata);
470     Op.Contents.MD = Meta;
471     return Op;
472   }
473
474   friend class MachineInstr;
475   friend class MachineRegisterInfo;
476 private:
477   //===--------------------------------------------------------------------===//
478   // Methods for handling register use/def lists.
479   //===--------------------------------------------------------------------===//
480
481   /// isOnRegUseList - Return true if this operand is on a register use/def list
482   /// or false if not.  This can only be called for register operands that are
483   /// part of a machine instruction.
484   bool isOnRegUseList() const {
485     assert(isReg() && "Can only add reg operand to use lists");
486     return Contents.Reg.Prev != 0;
487   }
488   
489   /// AddRegOperandToRegInfo - Add this register operand to the specified
490   /// MachineRegisterInfo.  If it is null, then the next/prev fields should be
491   /// explicitly nulled out.
492   void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
493
494   /// RemoveRegOperandFromRegInfo - Remove this register operand from the
495   /// MachineRegisterInfo it is linked with.
496   void RemoveRegOperandFromRegInfo();
497 };
498
499 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
500   MO.print(OS, 0);
501   return OS;
502 }
503
504 } // End llvm namespace
505
506 #endif