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