a782ff538ca90a2923bea401b6d41fe417b03ab9
[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   /// getNextOperandForReg - Return the next MachineOperand in the function that
180   /// uses or defines this register.
181   MachineOperand *getNextOperandForReg() const {
182     assert(isRegister() && "This is not a register operand!");
183     return Contents.Reg.Next;
184   }
185
186   //===--------------------------------------------------------------------===//
187   // Mutators for Register Operands
188   //===--------------------------------------------------------------------===//
189   
190   /// Change the register this operand corresponds to.
191   ///
192   void setReg(unsigned Reg);
193   
194   void setSubReg(unsigned subReg) {
195     assert(isRegister() && "Wrong MachineOperand accessor");
196     SubReg = (unsigned char)subReg;
197   }
198   
199   void setIsUse(bool Val = true) {
200     assert(isRegister() && "Wrong MachineOperand accessor");
201     IsDef = !Val;
202   }
203   
204   void setIsDef(bool Val = true) {
205     assert(isRegister() && "Wrong MachineOperand accessor");
206     IsDef = Val;
207   }
208
209   void setImplicit(bool Val = true) { 
210     assert(isRegister() && "Wrong MachineOperand accessor");
211     IsImp = Val;
212   }
213
214   void setIsKill(bool Val = true) {
215     assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
216     IsKill = Val;
217   }
218   
219   void setIsDead(bool Val = true) {
220     assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
221     IsDead = Val;
222   }
223
224
225   //===--------------------------------------------------------------------===//
226   // Accessors for various operand types.
227   //===--------------------------------------------------------------------===//
228   
229   int64_t getImm() const {
230     assert(isImmediate() && "Wrong MachineOperand accessor");
231     return Contents.ImmVal;
232   }
233   
234   MachineBasicBlock *getMBB() const {
235     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
236     return Contents.MBB;
237   }
238
239   int getIndex() const {
240     assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
241            "Wrong MachineOperand accessor");
242     return Contents.OffsetedInfo.Val.Index;
243   }
244   
245   GlobalValue *getGlobal() const {
246     assert(isGlobalAddress() && "Wrong MachineOperand accessor");
247     return Contents.OffsetedInfo.Val.GV;
248   }
249   
250   int getOffset() const {
251     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
252            "Wrong MachineOperand accessor");
253     return Contents.OffsetedInfo.Offset;
254   }
255   
256   const char *getSymbolName() const {
257     assert(isExternalSymbol() && "Wrong MachineOperand accessor");
258     return Contents.OffsetedInfo.Val.SymbolName;
259   }
260   
261   //===--------------------------------------------------------------------===//
262   // Mutators for various operand types.
263   //===--------------------------------------------------------------------===//
264   
265   void setImm(int64_t immVal) {
266     assert(isImmediate() && "Wrong MachineOperand mutator");
267     Contents.ImmVal = immVal;
268   }
269
270   void setOffset(int Offset) {
271     assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
272         "Wrong MachineOperand accessor");
273     Contents.OffsetedInfo.Offset = Offset;
274   }
275   
276   void setIndex(int Idx) {
277     assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
278            "Wrong MachineOperand accessor");
279     Contents.OffsetedInfo.Val.Index = Idx;
280   }
281   
282   void setMBB(MachineBasicBlock *MBB) {
283     assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
284     Contents.MBB = MBB;
285   }
286   
287   //===--------------------------------------------------------------------===//
288   // Other methods.
289   //===--------------------------------------------------------------------===//
290   
291   /// isIdenticalTo - Return true if this operand is identical to the specified
292   /// operand. Note: This method ignores isKill and isDead properties.
293   bool isIdenticalTo(const MachineOperand &Other) const;
294   
295   /// ChangeToImmediate - Replace this operand with a new immediate operand of
296   /// the specified value.  If an operand is known to be an immediate already,
297   /// the setImm method should be used.
298   void ChangeToImmediate(int64_t ImmVal);
299   
300   /// ChangeToRegister - Replace this operand with a new register operand of
301   /// the specified value.  If an operand is known to be an register already,
302   /// the setReg method should be used.
303   void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
304                         bool isKill = false, bool isDead = false);
305   
306   //===--------------------------------------------------------------------===//
307   // Construction methods.
308   //===--------------------------------------------------------------------===//
309   
310   static MachineOperand CreateImm(int64_t Val) {
311     MachineOperand Op(MachineOperand::MO_Immediate);
312     Op.setImm(Val);
313     return Op;
314   }
315   
316   static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
317                                   bool isKill = false, bool isDead = false,
318                                   unsigned SubReg = 0) {
319     MachineOperand Op(MachineOperand::MO_Register);
320     Op.IsDef = isDef;
321     Op.IsImp = isImp;
322     Op.IsKill = isKill;
323     Op.IsDead = isDead;
324     Op.Contents.Reg.RegNo = Reg;
325     Op.Contents.Reg.Prev = 0;
326     Op.Contents.Reg.Next = 0;
327     Op.SubReg = SubReg;
328     return Op;
329   }
330   static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
331     MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
332     Op.setMBB(MBB);
333     return Op;
334   }
335   static MachineOperand CreateFI(unsigned Idx) {
336     MachineOperand Op(MachineOperand::MO_FrameIndex);
337     Op.setIndex(Idx);
338     return Op;
339   }
340   static MachineOperand CreateCPI(unsigned Idx, int Offset) {
341     MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
342     Op.setIndex(Idx);
343     Op.setOffset(Offset);
344     return Op;
345   }
346   static MachineOperand CreateJTI(unsigned Idx) {
347     MachineOperand Op(MachineOperand::MO_JumpTableIndex);
348     Op.setIndex(Idx);
349     return Op;
350   }
351   static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
352     MachineOperand Op(MachineOperand::MO_GlobalAddress);
353     Op.Contents.OffsetedInfo.Val.GV = GV;
354     Op.setOffset(Offset);
355     return Op;
356   }
357   static MachineOperand CreateES(const char *SymName, int Offset = 0) {
358     MachineOperand Op(MachineOperand::MO_ExternalSymbol);
359     Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
360     Op.setOffset(Offset);
361     return Op;
362   }
363   const MachineOperand &operator=(const MachineOperand &MO) {
364     OpKind   = MO.OpKind;
365     IsDef    = MO.IsDef;
366     IsImp    = MO.IsImp;
367     IsKill   = MO.IsKill;
368     IsDead   = MO.IsDead;
369     SubReg   = MO.SubReg;
370     ParentMI = MO.ParentMI;
371     Contents = MO.Contents;
372     return *this;
373   }
374
375   friend class MachineInstr;
376   friend class MachineRegisterInfo;
377 private:
378   //===--------------------------------------------------------------------===//
379   // Methods for handling register use/def lists.
380   //===--------------------------------------------------------------------===//
381
382   /// isOnRegUseList - Return true if this operand is on a register use/def list
383   /// or false if not.  This can only be called for register operands that are
384   /// part of a machine instruction.
385   bool isOnRegUseList() const {
386     assert(isReg() && "Can only add reg operand to use lists");
387     return Contents.Reg.Prev != 0;
388   }
389   
390   /// AddRegOperandToRegInfo - Add this register operand to the specified
391   /// MachineRegisterInfo.  If it is null, then the next/prev fields should be
392   /// explicitly nulled out.
393   void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
394
395   void RemoveRegOperandFromRegInfo() {
396     assert(isOnRegUseList() && "Can only add reg operand to use lists");
397     // Unlink this from the doubly linked list of operands.
398     MachineOperand *NextOp = Contents.Reg.Next;
399     *Contents.Reg.Prev = NextOp; 
400     if (NextOp) {
401       assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
402       NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
403     }
404     Contents.Reg.Prev = 0;
405     Contents.Reg.Next = 0;
406   }
407 };
408
409 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
410   MO.print(OS, 0);
411   return OS;
412 }
413
414 } // End llvm namespace
415
416 #endif