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/System/DataTypes.h"
25 class MachineBasicBlock;
27 class MachineRegisterInfo;
33 /// MachineOperand class - Representation of each machine instruction operand.
35 class MachineOperand {
37 enum MachineOperandType {
38 MO_Register, ///< Register operand.
39 MO_Immediate, ///< Immediate operand
40 MO_FPImmediate, ///< Floating-point immediate operand
41 MO_MachineBasicBlock, ///< MachineBasicBlock reference
42 MO_FrameIndex, ///< Abstract Stack Frame Index
43 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
44 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
45 MO_ExternalSymbol, ///< Name of external global symbol
46 MO_GlobalAddress, ///< Address of a global value
47 MO_BlockAddress, ///< Address of a basic block
48 MO_Metadata, ///< Metadata reference (for debug info)
49 MO_MCSymbol ///< MCSymbol reference (for debug/eh info)
53 /// OpKind - Specify what kind of operand this is. This discriminates the
55 unsigned char OpKind; // MachineOperandType
57 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
58 /// indicates the MO_Register has no subReg.
61 /// TargetFlags - This is a set of target-specific operand flags.
62 unsigned char TargetFlags;
64 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
67 /// IsDef - True if this is a def, false if this is a use of the register.
71 /// IsImp - True if this is an implicit def or use, false if it is explicit.
75 /// IsKill - True if this instruction is the last use of the register on this
76 /// path through the function. This is only valid on uses of registers.
79 /// IsDead - True if this register is never used by a subsequent instruction.
80 /// This is only valid on definitions of registers.
83 /// IsUndef - True if this is a register def / use of "undef", i.e. register
84 /// defined by an IMPLICIT_DEF. This is only valid on registers.
87 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
88 /// by the MachineInstr before all input registers are read. This is used to
89 /// model the GCC inline asm '&' constraint modifier.
90 bool IsEarlyClobber : 1;
92 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
93 /// not a real instruction. Such uses should be ignored during codegen.
96 /// ParentMI - This is the instruction that this operand is embedded into.
97 /// This is valid for all operand types, when the operand is in an instr.
98 MachineInstr *ParentMI;
100 /// Contents union - This contains the payload for the various operand types.
102 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
103 const ConstantFP *CFP; // For MO_FPImmediate.
104 int64_t ImmVal; // For MO_Immediate.
105 const MDNode *MD; // For MO_Metadata.
106 MCSymbol *Sym; // For MO_MCSymbol
108 struct { // For MO_Register.
110 MachineOperand **Prev; // Access list for register.
111 MachineOperand *Next;
114 /// OffsetedInfo - This struct contains the offset and an object identifier.
115 /// this represent the object as with an optional offset from it.
118 int Index; // For MO_*Index - The index itself.
119 const char *SymbolName; // For MO_ExternalSymbol.
120 const GlobalValue *GV; // For MO_GlobalAddress.
121 const BlockAddress *BA; // For MO_BlockAddress.
123 int64_t Offset; // An offset from the object.
127 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
131 /// getType - Returns the MachineOperandType for this operand.
133 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
135 unsigned char getTargetFlags() const { return TargetFlags; }
136 void setTargetFlags(unsigned char F) { TargetFlags = F; }
137 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
140 /// getParent - Return the instruction that this operand belongs to.
142 MachineInstr *getParent() { return ParentMI; }
143 const MachineInstr *getParent() const { return ParentMI; }
145 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
147 //===--------------------------------------------------------------------===//
148 // Accessors that tell you what kind of MachineOperand you're looking at.
149 //===--------------------------------------------------------------------===//
151 /// isReg - Tests if this is a MO_Register operand.
152 bool isReg() const { return OpKind == MO_Register; }
153 /// isImm - Tests if this is a MO_Immediate operand.
154 bool isImm() const { return OpKind == MO_Immediate; }
155 /// isFPImm - Tests if this is a MO_FPImmediate operand.
156 bool isFPImm() const { return OpKind == MO_FPImmediate; }
157 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
158 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
159 /// isFI - Tests if this is a MO_FrameIndex operand.
160 bool isFI() const { return OpKind == MO_FrameIndex; }
161 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
162 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
163 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
164 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
165 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
166 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
167 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
168 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
169 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
170 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
171 /// isMetadata - Tests if this is a MO_Metadata operand.
172 bool isMetadata() const { return OpKind == MO_Metadata; }
173 bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
175 //===--------------------------------------------------------------------===//
176 // Accessors for Register Operands
177 //===--------------------------------------------------------------------===//
179 /// getReg - Returns the register number.
180 unsigned getReg() const {
181 assert(isReg() && "This is not a register operand!");
182 return Contents.Reg.RegNo;
185 unsigned getSubReg() const {
186 assert(isReg() && "Wrong MachineOperand accessor");
187 return (unsigned)SubReg;
191 assert(isReg() && "Wrong MachineOperand accessor");
196 assert(isReg() && "Wrong MachineOperand accessor");
200 bool isImplicit() const {
201 assert(isReg() && "Wrong MachineOperand accessor");
205 bool isDead() const {
206 assert(isReg() && "Wrong MachineOperand accessor");
210 bool isKill() const {
211 assert(isReg() && "Wrong MachineOperand accessor");
215 bool isUndef() const {
216 assert(isReg() && "Wrong MachineOperand accessor");
220 bool isEarlyClobber() const {
221 assert(isReg() && "Wrong MachineOperand accessor");
222 return IsEarlyClobber;
225 bool isDebug() const {
226 assert(isReg() && "Wrong MachineOperand accessor");
230 /// getNextOperandForReg - Return the next MachineOperand in the function that
231 /// uses or defines this register.
232 MachineOperand *getNextOperandForReg() const {
233 assert(isReg() && "This is not a register operand!");
234 return Contents.Reg.Next;
237 //===--------------------------------------------------------------------===//
238 // Mutators for Register Operands
239 //===--------------------------------------------------------------------===//
241 /// Change the register this operand corresponds to.
243 void setReg(unsigned Reg);
245 void setSubReg(unsigned subReg) {
246 assert(isReg() && "Wrong MachineOperand accessor");
247 SubReg = (unsigned char)subReg;
250 void setIsUse(bool Val = true) {
251 assert(isReg() && "Wrong MachineOperand accessor");
252 assert((Val || !isDebug()) && "Marking a debug operation as def");
256 void setIsDef(bool Val = true) {
257 assert(isReg() && "Wrong MachineOperand accessor");
258 assert((!Val || !isDebug()) && "Marking a debug operation as def");
262 void setImplicit(bool Val = true) {
263 assert(isReg() && "Wrong MachineOperand accessor");
267 void setIsKill(bool Val = true) {
268 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
269 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
273 void setIsDead(bool Val = true) {
274 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
278 void setIsUndef(bool Val = true) {
279 assert(isReg() && "Wrong MachineOperand accessor");
283 void setIsEarlyClobber(bool Val = true) {
284 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
285 IsEarlyClobber = Val;
288 void setIsDebug(bool Val = true) {
289 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
293 //===--------------------------------------------------------------------===//
294 // Accessors for various operand types.
295 //===--------------------------------------------------------------------===//
297 int64_t getImm() const {
298 assert(isImm() && "Wrong MachineOperand accessor");
299 return Contents.ImmVal;
302 const ConstantFP *getFPImm() const {
303 assert(isFPImm() && "Wrong MachineOperand accessor");
307 MachineBasicBlock *getMBB() const {
308 assert(isMBB() && "Wrong MachineOperand accessor");
312 int getIndex() const {
313 assert((isFI() || isCPI() || isJTI()) &&
314 "Wrong MachineOperand accessor");
315 return Contents.OffsetedInfo.Val.Index;
318 const GlobalValue *getGlobal() const {
319 assert(isGlobal() && "Wrong MachineOperand accessor");
320 return Contents.OffsetedInfo.Val.GV;
323 const BlockAddress *getBlockAddress() const {
324 assert(isBlockAddress() && "Wrong MachineOperand accessor");
325 return Contents.OffsetedInfo.Val.BA;
328 MCSymbol *getMCSymbol() const {
329 assert(isMCSymbol() && "Wrong MachineOperand accessor");
333 /// getOffset - Return the offset from the symbol in this operand. This always
334 /// returns 0 for ExternalSymbol operands.
335 int64_t getOffset() const {
336 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
337 "Wrong MachineOperand accessor");
338 return Contents.OffsetedInfo.Offset;
341 const char *getSymbolName() const {
342 assert(isSymbol() && "Wrong MachineOperand accessor");
343 return Contents.OffsetedInfo.Val.SymbolName;
346 const MDNode *getMetadata() const {
347 assert(isMetadata() && "Wrong MachineOperand accessor");
351 //===--------------------------------------------------------------------===//
352 // Mutators for various operand types.
353 //===--------------------------------------------------------------------===//
355 void setImm(int64_t immVal) {
356 assert(isImm() && "Wrong MachineOperand mutator");
357 Contents.ImmVal = immVal;
360 void setOffset(int64_t Offset) {
361 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
362 "Wrong MachineOperand accessor");
363 Contents.OffsetedInfo.Offset = Offset;
366 void setIndex(int Idx) {
367 assert((isFI() || isCPI() || isJTI()) &&
368 "Wrong MachineOperand accessor");
369 Contents.OffsetedInfo.Val.Index = Idx;
372 void setMBB(MachineBasicBlock *MBB) {
373 assert(isMBB() && "Wrong MachineOperand accessor");
377 //===--------------------------------------------------------------------===//
379 //===--------------------------------------------------------------------===//
381 /// isIdenticalTo - Return true if this operand is identical to the specified
382 /// operand. Note: This method ignores isKill and isDead properties.
383 bool isIdenticalTo(const MachineOperand &Other) const;
385 /// ChangeToImmediate - Replace this operand with a new immediate operand of
386 /// the specified value. If an operand is known to be an immediate already,
387 /// the setImm method should be used.
388 void ChangeToImmediate(int64_t ImmVal);
390 /// ChangeToRegister - Replace this operand with a new register operand of
391 /// the specified value. If an operand is known to be an register already,
392 /// the setReg method should be used.
393 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
394 bool isKill = false, bool isDead = false,
395 bool isUndef = false, bool isDebug = false);
397 //===--------------------------------------------------------------------===//
398 // Construction methods.
399 //===--------------------------------------------------------------------===//
401 static MachineOperand CreateImm(int64_t Val) {
402 MachineOperand Op(MachineOperand::MO_Immediate);
407 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
408 MachineOperand Op(MachineOperand::MO_FPImmediate);
409 Op.Contents.CFP = CFP;
413 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
414 bool isKill = false, bool isDead = false,
415 bool isUndef = false,
416 bool isEarlyClobber = false,
418 bool isDebug = false) {
419 MachineOperand Op(MachineOperand::MO_Register);
424 Op.IsUndef = isUndef;
425 Op.IsEarlyClobber = isEarlyClobber;
426 Op.IsDebug = isDebug;
427 Op.Contents.Reg.RegNo = Reg;
428 Op.Contents.Reg.Prev = 0;
429 Op.Contents.Reg.Next = 0;
433 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
434 unsigned char TargetFlags = 0) {
435 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
437 Op.setTargetFlags(TargetFlags);
440 static MachineOperand CreateFI(unsigned Idx) {
441 MachineOperand Op(MachineOperand::MO_FrameIndex);
445 static MachineOperand CreateCPI(unsigned Idx, int Offset,
446 unsigned char TargetFlags = 0) {
447 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
449 Op.setOffset(Offset);
450 Op.setTargetFlags(TargetFlags);
453 static MachineOperand CreateJTI(unsigned Idx,
454 unsigned char TargetFlags = 0) {
455 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
457 Op.setTargetFlags(TargetFlags);
460 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
461 unsigned char TargetFlags = 0) {
462 MachineOperand Op(MachineOperand::MO_GlobalAddress);
463 Op.Contents.OffsetedInfo.Val.GV = GV;
464 Op.setOffset(Offset);
465 Op.setTargetFlags(TargetFlags);
468 static MachineOperand CreateES(const char *SymName,
469 unsigned char TargetFlags = 0) {
470 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
471 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
472 Op.setOffset(0); // Offset is always 0.
473 Op.setTargetFlags(TargetFlags);
476 static MachineOperand CreateBA(const BlockAddress *BA,
477 unsigned char TargetFlags = 0) {
478 MachineOperand Op(MachineOperand::MO_BlockAddress);
479 Op.Contents.OffsetedInfo.Val.BA = BA;
480 Op.setOffset(0); // Offset is always 0.
481 Op.setTargetFlags(TargetFlags);
484 static MachineOperand CreateMetadata(const MDNode *Meta) {
485 MachineOperand Op(MachineOperand::MO_Metadata);
486 Op.Contents.MD = Meta;
490 static MachineOperand CreateMCSymbol(MCSymbol *Sym) {
491 MachineOperand Op(MachineOperand::MO_MCSymbol);
492 Op.Contents.Sym = Sym;
496 friend class MachineInstr;
497 friend class MachineRegisterInfo;
499 //===--------------------------------------------------------------------===//
500 // Methods for handling register use/def lists.
501 //===--------------------------------------------------------------------===//
503 /// isOnRegUseList - Return true if this operand is on a register use/def list
504 /// or false if not. This can only be called for register operands that are
505 /// part of a machine instruction.
506 bool isOnRegUseList() const {
507 assert(isReg() && "Can only add reg operand to use lists");
508 return Contents.Reg.Prev != 0;
511 /// AddRegOperandToRegInfo - Add this register operand to the specified
512 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
513 /// explicitly nulled out.
514 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
516 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
517 /// MachineRegisterInfo it is linked with.
518 void RemoveRegOperandFromRegInfo();
521 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
526 } // End llvm namespace