1 //===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MachineOperand class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEOPERAND_H
15 #define LLVM_CODEGEN_MACHINEOPERAND_H
17 #include "llvm/Support/DataTypes.h"
24 class MachineBasicBlock;
29 class MachineRegisterInfo;
32 /// MachineOperand class - Representation of each machine instruction operand.
34 class MachineOperand {
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
50 /// OpKind - Specify what kind of operand this is. This discriminates the
52 unsigned char OpKind; // MachineOperandType
54 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
55 /// indicates the MO_Register has no subReg.
58 /// TargetFlags - This is a set of target-specific operand flags.
59 unsigned char TargetFlags;
61 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
64 /// IsDef - True if this is a def, false if this is a use of the register.
68 /// IsImp - True if this is an implicit def or use, false if it is explicit.
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.
76 /// IsDead - True if this register is never used by a subsequent instruction.
77 /// This is only valid on definitions of registers.
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.
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;
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;
93 /// Contents union - This contains the payload for the various operand types.
95 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
96 const ConstantFP *CFP; // For MO_FPImmediate.
97 int64_t ImmVal; // For MO_Immediate.
99 struct { // For MO_Register.
101 MachineOperand **Prev; // Access list for register.
102 MachineOperand *Next;
105 /// OffsetedInfo - This struct contains the offset and an object identifier.
106 /// this represent the object as with an optional offset from it.
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.
114 int64_t Offset; // An offset from the object.
118 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
122 MachineOperand(const MachineOperand &M) {
128 /// getType - Returns the MachineOperandType for this operand.
130 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
132 unsigned char getTargetFlags() const { return TargetFlags; }
133 void setTargetFlags(unsigned char F) { TargetFlags = F; }
134 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
137 /// getParent - Return the instruction that this operand belongs to.
139 MachineInstr *getParent() { return ParentMI; }
140 const MachineInstr *getParent() const { return ParentMI; }
142 void print(std::ostream &os, const TargetMachine *TM = 0) const;
143 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
145 //===--------------------------------------------------------------------===//
146 // Accessors that tell you what kind of MachineOperand you're looking at.
147 //===--------------------------------------------------------------------===//
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; }
168 //===--------------------------------------------------------------------===//
169 // Accessors for Register Operands
170 //===--------------------------------------------------------------------===//
172 /// getReg - Returns the register number.
173 unsigned getReg() const {
174 assert(isReg() && "This is not a register operand!");
175 return Contents.Reg.RegNo;
178 unsigned getSubReg() const {
179 assert(isReg() && "Wrong MachineOperand accessor");
180 return (unsigned)SubReg;
184 assert(isReg() && "Wrong MachineOperand accessor");
189 assert(isReg() && "Wrong MachineOperand accessor");
193 bool isImplicit() const {
194 assert(isReg() && "Wrong MachineOperand accessor");
198 bool isDead() const {
199 assert(isReg() && "Wrong MachineOperand accessor");
203 bool isKill() const {
204 assert(isReg() && "Wrong MachineOperand accessor");
208 bool isUndef() const {
209 assert(isReg() && "Wrong MachineOperand accessor");
213 bool isEarlyClobber() const {
214 assert(isReg() && "Wrong MachineOperand accessor");
215 return IsEarlyClobber;
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;
225 //===--------------------------------------------------------------------===//
226 // Mutators for Register Operands
227 //===--------------------------------------------------------------------===//
229 /// Change the register this operand corresponds to.
231 void setReg(unsigned Reg);
233 void setSubReg(unsigned subReg) {
234 assert(isReg() && "Wrong MachineOperand accessor");
235 SubReg = (unsigned char)subReg;
238 void setIsUse(bool Val = true) {
239 assert(isReg() && "Wrong MachineOperand accessor");
243 void setIsDef(bool Val = true) {
244 assert(isReg() && "Wrong MachineOperand accessor");
248 void setImplicit(bool Val = true) {
249 assert(isReg() && "Wrong MachineOperand accessor");
253 void setIsKill(bool Val = true) {
254 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
258 void setIsDead(bool Val = true) {
259 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
263 void setIsUndef(bool Val = true) {
264 assert(isReg() && "Wrong MachineOperand accessor");
268 void setIsEarlyClobber(bool Val = true) {
269 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
270 IsEarlyClobber = Val;
273 //===--------------------------------------------------------------------===//
274 // Accessors for various operand types.
275 //===--------------------------------------------------------------------===//
277 int64_t getImm() const {
278 assert(isImm() && "Wrong MachineOperand accessor");
279 return Contents.ImmVal;
282 const ConstantFP *getFPImm() const {
283 assert(isFPImm() && "Wrong MachineOperand accessor");
287 MachineBasicBlock *getMBB() const {
288 assert(isMBB() && "Wrong MachineOperand accessor");
292 int getIndex() const {
293 assert((isFI() || isCPI() || isJTI()) &&
294 "Wrong MachineOperand accessor");
295 return Contents.OffsetedInfo.Val.Index;
298 GlobalValue *getGlobal() const {
299 assert(isGlobal() && "Wrong MachineOperand accessor");
300 return Contents.OffsetedInfo.Val.GV;
303 MDNode *getMDNode() const {
304 return Contents.OffsetedInfo.Val.Node;
307 int64_t getOffset() const {
308 assert((isGlobal() || isSymbol() || isCPI()) &&
309 "Wrong MachineOperand accessor");
310 return Contents.OffsetedInfo.Offset;
313 const char *getSymbolName() const {
314 assert(isSymbol() && "Wrong MachineOperand accessor");
315 return Contents.OffsetedInfo.Val.SymbolName;
318 //===--------------------------------------------------------------------===//
319 // Mutators for various operand types.
320 //===--------------------------------------------------------------------===//
322 void setImm(int64_t immVal) {
323 assert(isImm() && "Wrong MachineOperand mutator");
324 Contents.ImmVal = immVal;
327 void setOffset(int64_t Offset) {
328 assert((isGlobal() || isSymbol() || isCPI()) &&
329 "Wrong MachineOperand accessor");
330 Contents.OffsetedInfo.Offset = Offset;
333 void setIndex(int Idx) {
334 assert((isFI() || isCPI() || isJTI()) &&
335 "Wrong MachineOperand accessor");
336 Contents.OffsetedInfo.Val.Index = Idx;
339 void setMBB(MachineBasicBlock *MBB) {
340 assert(isMBB() && "Wrong MachineOperand accessor");
344 //===--------------------------------------------------------------------===//
346 //===--------------------------------------------------------------------===//
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;
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);
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);
364 //===--------------------------------------------------------------------===//
365 // Construction methods.
366 //===--------------------------------------------------------------------===//
368 static MachineOperand CreateImm(int64_t Val) {
369 MachineOperand Op(MachineOperand::MO_Immediate);
374 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
375 MachineOperand Op(MachineOperand::MO_FPImmediate);
376 Op.Contents.CFP = CFP;
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);
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;
398 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
399 unsigned char TargetFlags = 0) {
400 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
402 Op.setTargetFlags(TargetFlags);
405 static MachineOperand CreateFI(unsigned Idx) {
406 MachineOperand Op(MachineOperand::MO_FrameIndex);
410 static MachineOperand CreateCPI(unsigned Idx, int Offset,
411 unsigned char TargetFlags = 0) {
412 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
414 Op.setOffset(Offset);
415 Op.setTargetFlags(TargetFlags);
418 static MachineOperand CreateJTI(unsigned Idx,
419 unsigned char TargetFlags = 0) {
420 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
422 Op.setTargetFlags(TargetFlags);
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);
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);
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);
449 const MachineOperand &operator=(const MachineOperand &MO) {
455 IsUndef = MO.IsUndef;
456 IsEarlyClobber = MO.IsEarlyClobber;
458 ParentMI = MO.ParentMI;
459 Contents = MO.Contents;
460 TargetFlags = MO.TargetFlags;
464 friend class MachineInstr;
465 friend class MachineRegisterInfo;
467 //===--------------------------------------------------------------------===//
468 // Methods for handling register use/def lists.
469 //===--------------------------------------------------------------------===//
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;
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);
484 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
485 /// MachineRegisterInfo it is linked with.
486 void RemoveRegOperandFromRegInfo();
489 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
494 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
499 } // End llvm namespace