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