Change how dbg_value sdnodes are converted into machine instructions. Their placement...
[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/System/DataTypes.h"
18 #include <cassert>
19
20 namespace llvm {
21   
22 class BlockAddress;
23 class ConstantFP;
24 class GlobalValue;
25 class MachineBasicBlock;
26 class MachineInstr;
27 class MachineRegisterInfo;
28 class MDNode;
29 class TargetMachine;
30 class raw_ostream;
31 class MCSymbol;
32   
33 /// MachineOperand class - Representation of each machine instruction operand.
34 ///
35 class MachineOperand {
36 public:
37   enum MachineOperandType {
38     MO_Register,               ///< Register operand.
39     MO_Immediate,              ///< Immediate operand
40     MO_FPImmediate,            ///< Floating-point immediate operand
41     MO_MachineBasicBlock,      ///< MachineBasicBlock reference
42     MO_FrameIndex,             ///< Abstract Stack Frame Index
43     MO_ConstantPoolIndex,      ///< Address of indexed Constant in Constant Pool
44     MO_JumpTableIndex,         ///< Address of indexed Jump Table for switch
45     MO_ExternalSymbol,         ///< Name of external global symbol
46     MO_GlobalAddress,          ///< Address of a global value
47     MO_BlockAddress,           ///< Address of a basic block
48     MO_Metadata,               ///< Metadata reference (for debug info)
49     MO_MCSymbol                ///< MCSymbol reference (for debug/eh info)
50   };
51
52 private:
53   /// OpKind - Specify what kind of operand this is.  This discriminates the
54   /// union.
55   unsigned char OpKind; // MachineOperandType
56   
57   /// SubReg - Subregister number, only valid for MO_Register.  A value of 0
58   /// indicates the MO_Register has no subReg.
59   unsigned char SubReg;
60   
61   /// TargetFlags - This is a set of target-specific operand flags.
62   unsigned char TargetFlags;
63   
64   /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
65   /// operands.
66   
67   /// IsDef - True if this is a def, false if this is a use of the register.
68   ///
69   bool IsDef : 1;
70   
71   /// IsImp - True if this is an implicit def or use, false if it is explicit.
72   ///
73   bool IsImp : 1;
74
75   /// IsKill - True if this instruction is the last use of the register on this
76   /// path through the function.  This is only valid on uses of registers.
77   bool IsKill : 1;
78
79   /// IsDead - True if this register is never used by a subsequent instruction.
80   /// This is only valid on definitions of registers.
81   bool IsDead : 1;
82
83   /// IsUndef - True if this is a register def / use of "undef", i.e. register
84   /// defined by an IMPLICIT_DEF. This is only valid on registers.
85   bool IsUndef : 1;
86
87   /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
88   /// by the MachineInstr before all input registers are read.  This is used to
89   /// model the GCC inline asm '&' constraint modifier.
90   bool IsEarlyClobber : 1;
91
92   /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
93   /// not a real instruction.  Such uses should be ignored during codegen.
94   bool IsDebug : 1;
95
96   /// ParentMI - This is the instruction that this operand is embedded into. 
97   /// This is valid for all operand types, when the operand is in an instr.
98   MachineInstr *ParentMI;
99
100   /// Contents union - This contains the payload for the various operand types.
101   union {
102     MachineBasicBlock *MBB;   // For MO_MachineBasicBlock.
103     const ConstantFP *CFP;    // For MO_FPImmediate.
104     int64_t ImmVal;           // For MO_Immediate.
105     const MDNode *MD;         // For MO_Metadata.
106     MCSymbol *Sym;            // For MO_MCSymbol
107
108     struct {                  // For MO_Register.
109       unsigned RegNo;
110       MachineOperand **Prev;  // Access list for register.
111       MachineOperand *Next;
112     } Reg;
113     
114     /// OffsetedInfo - This struct contains the offset and an object identifier.
115     /// this represent the object as with an optional offset from it.
116     struct {
117       union {
118         int Index;                // For MO_*Index - The index itself.
119         const char *SymbolName;   // For MO_ExternalSymbol.
120         GlobalValue *GV;          // For MO_GlobalAddress.
121         BlockAddress *BA;         // For MO_BlockAddress.
122       } Val;
123       int64_t Offset;             // An offset from the object.
124     } OffsetedInfo;
125   } Contents;
126   
127   explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
128     TargetFlags = 0;
129   }
130 public:
131   /// getType - Returns the MachineOperandType for this operand.
132   ///
133   MachineOperandType getType() const { return (MachineOperandType)OpKind; }
134   
135   unsigned char getTargetFlags() const { return TargetFlags; }
136   void setTargetFlags(unsigned char F) { TargetFlags = F; }
137   void addTargetFlag(unsigned char F) { TargetFlags |= F; }
138   
139
140   /// getParent - Return the instruction that this operand belongs to.
141   ///
142   MachineInstr *getParent() { return ParentMI; }
143   const MachineInstr *getParent() const { return ParentMI; }
144   
145   void print(raw_ostream &os, const TargetMachine *TM = 0) const;
146
147   //===--------------------------------------------------------------------===//
148   // Accessors that tell you what kind of MachineOperand you're looking at.
149   //===--------------------------------------------------------------------===//
150
151   /// isReg - Tests if this is a MO_Register operand.
152   bool isReg() const { return OpKind == MO_Register; }
153   /// isImm - Tests if this is a MO_Immediate operand.
154   bool isImm() const { return OpKind == MO_Immediate; }
155   /// isFPImm - Tests if this is a MO_FPImmediate operand.
156   bool isFPImm() const { return OpKind == MO_FPImmediate; }
157   /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
158   bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
159   /// isFI - Tests if this is a MO_FrameIndex operand.
160   bool isFI() const { return OpKind == MO_FrameIndex; }
161   /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
162   bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
163   /// isJTI - Tests if this is a MO_JumpTableIndex operand.
164   bool isJTI() const { return OpKind == MO_JumpTableIndex; }
165   /// isGlobal - Tests if this is a MO_GlobalAddress operand.
166   bool isGlobal() const { return OpKind == MO_GlobalAddress; }
167   /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
168   bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
169   /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
170   bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
171   /// isMetadata - Tests if this is a MO_Metadata operand.
172   bool isMetadata() const { return OpKind == MO_Metadata; }
173   bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
174
175   //===--------------------------------------------------------------------===//
176   // Accessors for Register Operands
177   //===--------------------------------------------------------------------===//
178
179   /// getReg - Returns the register number.
180   unsigned getReg() const {
181     assert(isReg() && "This is not a register operand!");
182     return Contents.Reg.RegNo;
183   }
184   
185   unsigned getSubReg() const {
186     assert(isReg() && "Wrong MachineOperand accessor");
187     return (unsigned)SubReg;
188   }
189   
190   bool isUse() const { 
191     assert(isReg() && "Wrong MachineOperand accessor");
192     return !IsDef;
193   }
194   
195   bool isDef() const {
196     assert(isReg() && "Wrong MachineOperand accessor");
197     return IsDef;
198   }
199   
200   bool isImplicit() const { 
201     assert(isReg() && "Wrong MachineOperand accessor");
202     return IsImp;
203   }
204   
205   bool isDead() const {
206     assert(isReg() && "Wrong MachineOperand accessor");
207     return IsDead;
208   }
209   
210   bool isKill() const {
211     assert(isReg() && "Wrong MachineOperand accessor");
212     return IsKill;
213   }
214   
215   bool isUndef() const {
216     assert(isReg() && "Wrong MachineOperand accessor");
217     return IsUndef;
218   }
219   
220   bool isEarlyClobber() const {
221     assert(isReg() && "Wrong MachineOperand accessor");
222     return IsEarlyClobber;
223   }
224
225   bool isDebug() const {
226     assert(isReg() && "Wrong MachineOperand accessor");
227     return IsDebug;
228   }
229
230   /// getNextOperandForReg - Return the next MachineOperand in the function that
231   /// uses or defines this register.
232   MachineOperand *getNextOperandForReg() const {
233     assert(isReg() && "This is not a register operand!");
234     return Contents.Reg.Next;
235   }
236
237   //===--------------------------------------------------------------------===//
238   // Mutators for Register Operands
239   //===--------------------------------------------------------------------===//
240   
241   /// Change the register this operand corresponds to.
242   ///
243   void setReg(unsigned Reg);
244   
245   void setSubReg(unsigned subReg) {
246     assert(isReg() && "Wrong MachineOperand accessor");
247     SubReg = (unsigned char)subReg;
248   }
249   
250   void setIsUse(bool Val = true) {
251     assert(isReg() && "Wrong MachineOperand accessor");
252     assert((Val || !isDebug()) && "Marking a debug operation as def");
253     IsDef = !Val;
254   }
255   
256   void setIsDef(bool Val = true) {
257     assert(isReg() && "Wrong MachineOperand accessor");
258     assert((!Val || !isDebug()) && "Marking a debug operation as def");
259     IsDef = Val;
260   }
261
262   void setImplicit(bool Val = true) { 
263     assert(isReg() && "Wrong MachineOperand accessor");
264     IsImp = Val;
265   }
266
267   void setIsKill(bool Val = true) {
268     assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
269     assert((!Val || !isDebug()) && "Marking a debug operation as kill");
270     IsKill = Val;
271   }
272   
273   void setIsDead(bool Val = true) {
274     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
275     IsDead = Val;
276   }
277
278   void setIsUndef(bool Val = true) {
279     assert(isReg() && "Wrong MachineOperand accessor");
280     IsUndef = Val;
281   }
282   
283   void setIsEarlyClobber(bool Val = true) {
284     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
285     IsEarlyClobber = Val;
286   }
287
288   void setIsDebug(bool Val = true) {
289     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
290     IsDebug = Val;
291   }
292
293   //===--------------------------------------------------------------------===//
294   // Accessors for various operand types.
295   //===--------------------------------------------------------------------===//
296   
297   int64_t getImm() const {
298     assert(isImm() && "Wrong MachineOperand accessor");
299     return Contents.ImmVal;
300   }
301   
302   const ConstantFP *getFPImm() const {
303     assert(isFPImm() && "Wrong MachineOperand accessor");
304     return Contents.CFP;
305   }
306   
307   MachineBasicBlock *getMBB() const {
308     assert(isMBB() && "Wrong MachineOperand accessor");
309     return Contents.MBB;
310   }
311
312   int getIndex() const {
313     assert((isFI() || isCPI() || isJTI()) &&
314            "Wrong MachineOperand accessor");
315     return Contents.OffsetedInfo.Val.Index;
316   }
317   
318   GlobalValue *getGlobal() const {
319     assert(isGlobal() && "Wrong MachineOperand accessor");
320     return Contents.OffsetedInfo.Val.GV;
321   }
322
323   BlockAddress *getBlockAddress() const {
324     assert(isBlockAddress() && "Wrong MachineOperand accessor");
325     return Contents.OffsetedInfo.Val.BA;
326   }
327
328   MCSymbol *getMCSymbol() const {
329     assert(isMCSymbol() && "Wrong MachineOperand accessor");
330     return Contents.Sym;
331   }
332   
333   /// getOffset - Return the offset from the symbol in this operand. This always
334   /// returns 0 for ExternalSymbol operands.
335   int64_t getOffset() const {
336     assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
337            "Wrong MachineOperand accessor");
338     return Contents.OffsetedInfo.Offset;
339   }
340   
341   const char *getSymbolName() const {
342     assert(isSymbol() && "Wrong MachineOperand accessor");
343     return Contents.OffsetedInfo.Val.SymbolName;
344   }
345
346   const MDNode *getMetadata() const {
347     assert(isMetadata() && "Wrong MachineOperand accessor");
348     return Contents.MD;
349   }
350   
351   //===--------------------------------------------------------------------===//
352   // Mutators for various operand types.
353   //===--------------------------------------------------------------------===//
354   
355   void setImm(int64_t immVal) {
356     assert(isImm() && "Wrong MachineOperand mutator");
357     Contents.ImmVal = immVal;
358   }
359
360   void setOffset(int64_t Offset) {
361     assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
362         "Wrong MachineOperand accessor");
363     Contents.OffsetedInfo.Offset = Offset;
364   }
365   
366   void setIndex(int Idx) {
367     assert((isFI() || isCPI() || isJTI()) &&
368            "Wrong MachineOperand accessor");
369     Contents.OffsetedInfo.Val.Index = Idx;
370   }
371   
372   void setMBB(MachineBasicBlock *MBB) {
373     assert(isMBB() && "Wrong MachineOperand accessor");
374     Contents.MBB = MBB;
375   }
376   
377   //===--------------------------------------------------------------------===//
378   // Other methods.
379   //===--------------------------------------------------------------------===//
380   
381   /// isIdenticalTo - Return true if this operand is identical to the specified
382   /// operand. Note: This method ignores isKill and isDead properties.
383   bool isIdenticalTo(const MachineOperand &Other) const;
384   
385   /// ChangeToImmediate - Replace this operand with a new immediate operand of
386   /// the specified value.  If an operand is known to be an immediate already,
387   /// the setImm method should be used.
388   void ChangeToImmediate(int64_t ImmVal);
389   
390   /// ChangeToRegister - Replace this operand with a new register operand of
391   /// the specified value.  If an operand is known to be an register already,
392   /// the setReg method should be used.
393   void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
394                         bool isKill = false, bool isDead = false,
395                         bool isUndef = false, bool isDebug = false);
396   
397   //===--------------------------------------------------------------------===//
398   // Construction methods.
399   //===--------------------------------------------------------------------===//
400   
401   static MachineOperand CreateImm(int64_t Val) {
402     MachineOperand Op(MachineOperand::MO_Immediate);
403     Op.setImm(Val);
404     return Op;
405   }
406   
407   static MachineOperand CreateFPImm(const ConstantFP *CFP) {
408     MachineOperand Op(MachineOperand::MO_FPImmediate);
409     Op.Contents.CFP = CFP;
410     return Op;
411   }
412   
413   static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
414                                   bool isKill = false, bool isDead = false,
415                                   bool isUndef = false,
416                                   bool isEarlyClobber = false,
417                                   unsigned SubReg = 0,
418                                   bool isDebug = false) {
419     MachineOperand Op(MachineOperand::MO_Register);
420     Op.IsDef = isDef;
421     Op.IsImp = isImp;
422     Op.IsKill = isKill;
423     Op.IsDead = isDead;
424     Op.IsUndef = isUndef;
425     Op.IsEarlyClobber = isEarlyClobber;
426     Op.IsDebug = isDebug;
427     Op.Contents.Reg.RegNo = Reg;
428     Op.Contents.Reg.Prev = 0;
429     Op.Contents.Reg.Next = 0;
430     Op.SubReg = SubReg;
431     return Op;
432   }
433   static MachineOperand CreateMBB(MachineBasicBlock *MBB,
434                                   unsigned char TargetFlags = 0) {
435     MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
436     Op.setMBB(MBB);
437     Op.setTargetFlags(TargetFlags);
438     return Op;
439   }
440   static MachineOperand CreateFI(unsigned Idx) {
441     MachineOperand Op(MachineOperand::MO_FrameIndex);
442     Op.setIndex(Idx);
443     return Op;
444   }
445   static MachineOperand CreateCPI(unsigned Idx, int Offset,
446                                   unsigned char TargetFlags = 0) {
447     MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
448     Op.setIndex(Idx);
449     Op.setOffset(Offset);
450     Op.setTargetFlags(TargetFlags);
451     return Op;
452   }
453   static MachineOperand CreateJTI(unsigned Idx,
454                                   unsigned char TargetFlags = 0) {
455     MachineOperand Op(MachineOperand::MO_JumpTableIndex);
456     Op.setIndex(Idx);
457     Op.setTargetFlags(TargetFlags);
458     return Op;
459   }
460   static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
461                                  unsigned char TargetFlags = 0) {
462     MachineOperand Op(MachineOperand::MO_GlobalAddress);
463     Op.Contents.OffsetedInfo.Val.GV = GV;
464     Op.setOffset(Offset);
465     Op.setTargetFlags(TargetFlags);
466     return Op;
467   }
468   static MachineOperand CreateES(const char *SymName,
469                                  unsigned char TargetFlags = 0) {
470     MachineOperand Op(MachineOperand::MO_ExternalSymbol);
471     Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
472     Op.setOffset(0); // Offset is always 0.
473     Op.setTargetFlags(TargetFlags);
474     return Op;
475   }
476   static MachineOperand CreateBA(BlockAddress *BA,
477                                  unsigned char TargetFlags = 0) {
478     MachineOperand Op(MachineOperand::MO_BlockAddress);
479     Op.Contents.OffsetedInfo.Val.BA = BA;
480     Op.setOffset(0); // Offset is always 0.
481     Op.setTargetFlags(TargetFlags);
482     return Op;
483   }
484   static MachineOperand CreateMetadata(const MDNode *Meta) {
485     MachineOperand Op(MachineOperand::MO_Metadata);
486     Op.Contents.MD = Meta;
487     return Op;
488   }
489
490   static MachineOperand CreateMCSymbol(MCSymbol *Sym) {
491     MachineOperand Op(MachineOperand::MO_MCSymbol);
492     Op.Contents.Sym = Sym;
493     return Op;
494   }
495   
496   friend class MachineInstr;
497   friend class MachineRegisterInfo;
498 private:
499   //===--------------------------------------------------------------------===//
500   // Methods for handling register use/def lists.
501   //===--------------------------------------------------------------------===//
502
503   /// isOnRegUseList - Return true if this operand is on a register use/def list
504   /// or false if not.  This can only be called for register operands that are
505   /// part of a machine instruction.
506   bool isOnRegUseList() const {
507     assert(isReg() && "Can only add reg operand to use lists");
508     return Contents.Reg.Prev != 0;
509   }
510   
511   /// AddRegOperandToRegInfo - Add this register operand to the specified
512   /// MachineRegisterInfo.  If it is null, then the next/prev fields should be
513   /// explicitly nulled out.
514   void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
515
516   /// RemoveRegOperandFromRegInfo - Remove this register operand from the
517   /// MachineRegisterInfo it is linked with.
518   void RemoveRegOperandFromRegInfo();
519 };
520
521 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
522   MO.print(OS, 0);
523   return OS;
524 }
525
526 } // End llvm namespace
527
528 #endif