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; }
167 /// isMetadata - Tests if this is a MO_Metadata operand.
168 bool isMetadata() const { return OpKind == MO_Metadata; }
170 //===--------------------------------------------------------------------===//
171 // Accessors for Register Operands
172 //===--------------------------------------------------------------------===//
174 /// getReg - Returns the register number.
175 unsigned getReg() const {
176 assert(isReg() && "This is not a register operand!");
177 return Contents.Reg.RegNo;
180 unsigned getSubReg() const {
181 assert(isReg() && "Wrong MachineOperand accessor");
182 return (unsigned)SubReg;
186 assert(isReg() && "Wrong MachineOperand accessor");
191 assert(isReg() && "Wrong MachineOperand accessor");
195 bool isImplicit() const {
196 assert(isReg() && "Wrong MachineOperand accessor");
200 bool isDead() const {
201 assert(isReg() && "Wrong MachineOperand accessor");
205 bool isKill() const {
206 assert(isReg() && "Wrong MachineOperand accessor");
210 bool isUndef() const {
211 assert(isReg() && "Wrong MachineOperand accessor");
215 bool isEarlyClobber() const {
216 assert(isReg() && "Wrong MachineOperand accessor");
217 return IsEarlyClobber;
220 /// getNextOperandForReg - Return the next MachineOperand in the function that
221 /// uses or defines this register.
222 MachineOperand *getNextOperandForReg() const {
223 assert(isReg() && "This is not a register operand!");
224 return Contents.Reg.Next;
227 //===--------------------------------------------------------------------===//
228 // Mutators for Register Operands
229 //===--------------------------------------------------------------------===//
231 /// Change the register this operand corresponds to.
233 void setReg(unsigned Reg);
235 void setSubReg(unsigned subReg) {
236 assert(isReg() && "Wrong MachineOperand accessor");
237 SubReg = (unsigned char)subReg;
240 void setIsUse(bool Val = true) {
241 assert(isReg() && "Wrong MachineOperand accessor");
245 void setIsDef(bool Val = true) {
246 assert(isReg() && "Wrong MachineOperand accessor");
250 void setImplicit(bool Val = true) {
251 assert(isReg() && "Wrong MachineOperand accessor");
255 void setIsKill(bool Val = true) {
256 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
260 void setIsDead(bool Val = true) {
261 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
265 void setIsUndef(bool Val = true) {
266 assert(isReg() && "Wrong MachineOperand accessor");
270 void setIsEarlyClobber(bool Val = true) {
271 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
272 IsEarlyClobber = Val;
275 //===--------------------------------------------------------------------===//
276 // Accessors for various operand types.
277 //===--------------------------------------------------------------------===//
279 int64_t getImm() const {
280 assert(isImm() && "Wrong MachineOperand accessor");
281 return Contents.ImmVal;
284 const ConstantFP *getFPImm() const {
285 assert(isFPImm() && "Wrong MachineOperand accessor");
289 MachineBasicBlock *getMBB() const {
290 assert(isMBB() && "Wrong MachineOperand accessor");
294 int getIndex() const {
295 assert((isFI() || isCPI() || isJTI()) &&
296 "Wrong MachineOperand accessor");
297 return Contents.OffsetedInfo.Val.Index;
300 GlobalValue *getGlobal() const {
301 assert(isGlobal() && "Wrong MachineOperand accessor");
302 return Contents.OffsetedInfo.Val.GV;
305 MDNode *getMDNode() const {
306 return Contents.OffsetedInfo.Val.Node;
309 int64_t getOffset() const {
310 assert((isGlobal() || isSymbol() || isCPI()) &&
311 "Wrong MachineOperand accessor");
312 return Contents.OffsetedInfo.Offset;
315 const char *getSymbolName() const {
316 assert(isSymbol() && "Wrong MachineOperand accessor");
317 return Contents.OffsetedInfo.Val.SymbolName;
320 //===--------------------------------------------------------------------===//
321 // Mutators for various operand types.
322 //===--------------------------------------------------------------------===//
324 void setImm(int64_t immVal) {
325 assert(isImm() && "Wrong MachineOperand mutator");
326 Contents.ImmVal = immVal;
329 void setOffset(int64_t Offset) {
330 assert((isGlobal() || isSymbol() || isCPI() || isMetadata()) &&
331 "Wrong MachineOperand accessor");
332 Contents.OffsetedInfo.Offset = Offset;
335 void setIndex(int Idx) {
336 assert((isFI() || isCPI() || isJTI()) &&
337 "Wrong MachineOperand accessor");
338 Contents.OffsetedInfo.Val.Index = Idx;
341 void setMBB(MachineBasicBlock *MBB) {
342 assert(isMBB() && "Wrong MachineOperand accessor");
346 //===--------------------------------------------------------------------===//
348 //===--------------------------------------------------------------------===//
350 /// isIdenticalTo - Return true if this operand is identical to the specified
351 /// operand. Note: This method ignores isKill and isDead properties.
352 bool isIdenticalTo(const MachineOperand &Other) const;
354 /// ChangeToImmediate - Replace this operand with a new immediate operand of
355 /// the specified value. If an operand is known to be an immediate already,
356 /// the setImm method should be used.
357 void ChangeToImmediate(int64_t ImmVal);
359 /// ChangeToRegister - Replace this operand with a new register operand of
360 /// the specified value. If an operand is known to be an register already,
361 /// the setReg method should be used.
362 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
363 bool isKill = false, bool isDead = false,
364 bool isUndef = false);
366 //===--------------------------------------------------------------------===//
367 // Construction methods.
368 //===--------------------------------------------------------------------===//
370 static MachineOperand CreateImm(int64_t Val) {
371 MachineOperand Op(MachineOperand::MO_Immediate);
376 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
377 MachineOperand Op(MachineOperand::MO_FPImmediate);
378 Op.Contents.CFP = CFP;
382 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
383 bool isKill = false, bool isDead = false,
384 bool isUndef = false,
385 bool isEarlyClobber = false,
386 unsigned SubReg = 0) {
387 MachineOperand Op(MachineOperand::MO_Register);
392 Op.IsUndef = isUndef;
393 Op.IsEarlyClobber = isEarlyClobber;
394 Op.Contents.Reg.RegNo = Reg;
395 Op.Contents.Reg.Prev = 0;
396 Op.Contents.Reg.Next = 0;
400 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
401 unsigned char TargetFlags = 0) {
402 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
404 Op.setTargetFlags(TargetFlags);
407 static MachineOperand CreateFI(unsigned Idx) {
408 MachineOperand Op(MachineOperand::MO_FrameIndex);
412 static MachineOperand CreateCPI(unsigned Idx, int Offset,
413 unsigned char TargetFlags = 0) {
414 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
416 Op.setOffset(Offset);
417 Op.setTargetFlags(TargetFlags);
420 static MachineOperand CreateJTI(unsigned Idx,
421 unsigned char TargetFlags = 0) {
422 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
424 Op.setTargetFlags(TargetFlags);
427 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
428 unsigned char TargetFlags = 0) {
429 MachineOperand Op(MachineOperand::MO_GlobalAddress);
430 Op.Contents.OffsetedInfo.Val.GV = GV;
431 Op.setOffset(Offset);
432 Op.setTargetFlags(TargetFlags);
435 static MachineOperand CreateMDNode(MDNode *N, int64_t Offset,
436 unsigned char TargetFlags = 0) {
437 MachineOperand Op(MachineOperand::MO_Metadata);
438 Op.Contents.OffsetedInfo.Val.Node = N;
439 Op.setOffset(Offset);
440 Op.setTargetFlags(TargetFlags);
443 static MachineOperand CreateES(const char *SymName, int64_t Offset = 0,
444 unsigned char TargetFlags = 0) {
445 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
446 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
447 Op.setOffset(Offset);
448 Op.setTargetFlags(TargetFlags);
451 const MachineOperand &operator=(const MachineOperand &MO) {
457 IsUndef = MO.IsUndef;
458 IsEarlyClobber = MO.IsEarlyClobber;
460 ParentMI = MO.ParentMI;
461 Contents = MO.Contents;
462 TargetFlags = MO.TargetFlags;
466 friend class MachineInstr;
467 friend class MachineRegisterInfo;
469 //===--------------------------------------------------------------------===//
470 // Methods for handling register use/def lists.
471 //===--------------------------------------------------------------------===//
473 /// isOnRegUseList - Return true if this operand is on a register use/def list
474 /// or false if not. This can only be called for register operands that are
475 /// part of a machine instruction.
476 bool isOnRegUseList() const {
477 assert(isReg() && "Can only add reg operand to use lists");
478 return Contents.Reg.Prev != 0;
481 /// AddRegOperandToRegInfo - Add this register operand to the specified
482 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
483 /// explicitly nulled out.
484 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
486 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
487 /// MachineRegisterInfo it is linked with.
488 void RemoveRegOperandFromRegInfo();
491 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
496 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
501 } // End llvm namespace