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