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