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