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