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