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