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