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