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"
26 class MachineBasicBlock;
28 class MachineRegisterInfo;
31 class TargetRegisterInfo;
35 /// MachineOperand class - Representation of each machine instruction operand.
37 class MachineOperand {
39 enum MachineOperandType {
40 MO_Register, ///< Register operand.
41 MO_Immediate, ///< Immediate operand
42 MO_CImmediate, ///< Immediate >64bit operand
43 MO_FPImmediate, ///< Floating-point immediate operand
44 MO_MachineBasicBlock, ///< MachineBasicBlock reference
45 MO_FrameIndex, ///< Abstract Stack Frame Index
46 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
47 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
48 MO_ExternalSymbol, ///< Name of external global symbol
49 MO_GlobalAddress, ///< Address of a global value
50 MO_BlockAddress, ///< Address of a basic block
51 MO_Metadata, ///< Metadata reference (for debug info)
52 MO_MCSymbol ///< MCSymbol reference (for debug/eh info)
56 /// OpKind - Specify what kind of operand this is. This discriminates the
58 unsigned char OpKind; // MachineOperandType
60 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
61 /// indicates the MO_Register has no subReg.
64 /// TargetFlags - This is a set of target-specific operand flags.
65 unsigned char TargetFlags;
67 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
70 /// IsDef - True if this is a def, false if this is a use of the register.
74 /// IsImp - True if this is an implicit def or use, false if it is explicit.
78 /// IsKill - True if this instruction is the last use of the register on this
79 /// path through the function. This is only valid on uses of registers.
82 /// IsDead - True if this register is never used by a subsequent instruction.
83 /// This is only valid on definitions of registers.
86 /// IsUndef - True if this register operand reads an "undef" value, i.e. the
87 /// read value doesn't matter. This flag can be set on both use and def
88 /// operands. On a sub-register def operand, it refers to the part of the
89 /// register that isn't written. On a full-register def operand, it is a
90 /// noop. See readsReg().
92 /// This is only valid on registers.
94 /// Note that an instruction may have multiple <undef> operands referring to
95 /// the same register. In that case, the instruction may depend on those
96 /// operands reading the same dont-care value. For example:
98 /// %vreg1<def> = XOR %vreg2<undef>, %vreg2<undef>
100 /// Any register can be used for %vreg2, and its value doesn't matter, but
101 /// the two operands must be the same register.
105 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
106 /// by the MachineInstr before all input registers are read. This is used to
107 /// model the GCC inline asm '&' constraint modifier.
108 bool IsEarlyClobber : 1;
110 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
111 /// not a real instruction. Such uses should be ignored during codegen.
114 /// SmallContents - This really should be part of the Contents union, but
115 /// lives out here so we can get a better packed struct.
116 /// MO_Register: Register number.
117 /// OffsetedInfo: Low bits of offset.
119 unsigned RegNo; // For MO_Register.
120 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
123 /// ParentMI - This is the instruction that this operand is embedded into.
124 /// This is valid for all operand types, when the operand is in an instr.
125 MachineInstr *ParentMI;
127 /// Contents union - This contains the payload for the various operand types.
129 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
130 const ConstantFP *CFP; // For MO_FPImmediate.
131 const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit.
132 int64_t ImmVal; // For MO_Immediate.
133 const MDNode *MD; // For MO_Metadata.
134 MCSymbol *Sym; // For MO_MCSymbol
136 struct { // For MO_Register.
137 // Register number is in SmallContents.RegNo.
138 MachineOperand **Prev; // Access list for register.
139 MachineOperand *Next;
142 /// OffsetedInfo - This struct contains the offset and an object identifier.
143 /// this represent the object as with an optional offset from it.
146 int Index; // For MO_*Index - The index itself.
147 const char *SymbolName; // For MO_ExternalSymbol.
148 const GlobalValue *GV; // For MO_GlobalAddress.
149 const BlockAddress *BA; // For MO_BlockAddress.
151 // Low bits of offset are in SmallContents.OffsetLo.
152 int OffsetHi; // An offset from the object, high 32 bits.
156 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
160 /// getType - Returns the MachineOperandType for this operand.
162 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
164 unsigned char getTargetFlags() const { return TargetFlags; }
165 void setTargetFlags(unsigned char F) { TargetFlags = F; }
166 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
169 /// getParent - Return the instruction that this operand belongs to.
171 MachineInstr *getParent() { return ParentMI; }
172 const MachineInstr *getParent() const { return ParentMI; }
174 /// clearParent - Reset the parent pointer.
176 /// The MachineOperand copy constructor also copies ParentMI, expecting the
177 /// original to be deleted. If a MachineOperand is ever stored outside a
178 /// MachineInstr, the parent pointer must be cleared.
180 /// Never call clearParent() on an operand in a MachineInstr.
182 void clearParent() { ParentMI = 0; }
184 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
186 //===--------------------------------------------------------------------===//
187 // Accessors that tell you what kind of MachineOperand you're looking at.
188 //===--------------------------------------------------------------------===//
190 /// isReg - Tests if this is a MO_Register operand.
191 bool isReg() const { return OpKind == MO_Register; }
192 /// isImm - Tests if this is a MO_Immediate operand.
193 bool isImm() const { return OpKind == MO_Immediate; }
194 /// isCImm - Test if t his is a MO_CImmediate operand.
195 bool isCImm() const { return OpKind == MO_CImmediate; }
196 /// isFPImm - Tests if this is a MO_FPImmediate operand.
197 bool isFPImm() const { return OpKind == MO_FPImmediate; }
198 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
199 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
200 /// isFI - Tests if this is a MO_FrameIndex operand.
201 bool isFI() const { return OpKind == MO_FrameIndex; }
202 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
203 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
204 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
205 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
206 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
207 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
208 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
209 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
210 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
211 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
212 /// isMetadata - Tests if this is a MO_Metadata operand.
213 bool isMetadata() const { return OpKind == MO_Metadata; }
214 bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
216 //===--------------------------------------------------------------------===//
217 // Accessors for Register Operands
218 //===--------------------------------------------------------------------===//
220 /// getReg - Returns the register number.
221 unsigned getReg() const {
222 assert(isReg() && "This is not a register operand!");
223 return SmallContents.RegNo;
226 unsigned getSubReg() const {
227 assert(isReg() && "Wrong MachineOperand accessor");
228 return (unsigned)SubReg;
232 assert(isReg() && "Wrong MachineOperand accessor");
237 assert(isReg() && "Wrong MachineOperand accessor");
241 bool isImplicit() const {
242 assert(isReg() && "Wrong MachineOperand accessor");
246 bool isDead() const {
247 assert(isReg() && "Wrong MachineOperand accessor");
251 bool isKill() const {
252 assert(isReg() && "Wrong MachineOperand accessor");
256 bool isUndef() const {
257 assert(isReg() && "Wrong MachineOperand accessor");
261 bool isEarlyClobber() const {
262 assert(isReg() && "Wrong MachineOperand accessor");
263 return IsEarlyClobber;
266 bool isDebug() const {
267 assert(isReg() && "Wrong MachineOperand accessor");
271 /// readsReg - Returns true if this operand reads the previous value of its
272 /// register. A use operand with the <undef> flag set doesn't read its
273 /// register. A sub-register def implicitly reads the other parts of the
274 /// register being redefined unless the <undef> flag is set.
275 bool readsReg() const {
276 assert(isReg() && "Wrong MachineOperand accessor");
277 return !isUndef() && (isUse() || getSubReg());
280 /// getNextOperandForReg - Return the next MachineOperand in the function that
281 /// uses or defines this register.
282 MachineOperand *getNextOperandForReg() const {
283 assert(isReg() && "This is not a register operand!");
284 return Contents.Reg.Next;
287 //===--------------------------------------------------------------------===//
288 // Mutators for Register Operands
289 //===--------------------------------------------------------------------===//
291 /// Change the register this operand corresponds to.
293 void setReg(unsigned Reg);
295 void setSubReg(unsigned subReg) {
296 assert(isReg() && "Wrong MachineOperand accessor");
297 SubReg = (unsigned char)subReg;
300 /// substVirtReg - Substitute the current register with the virtual
301 /// subregister Reg:SubReg. Take any existing SubReg index into account,
302 /// using TargetRegisterInfo to compose the subreg indices if necessary.
303 /// Reg must be a virtual register, SubIdx can be 0.
305 void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo&);
307 /// substPhysReg - Substitute the current register with the physical register
308 /// Reg, taking any existing SubReg into account. For instance,
309 /// substPhysReg(%EAX) will change %reg1024:sub_8bit to %AL.
311 void substPhysReg(unsigned Reg, const TargetRegisterInfo&);
313 void setIsUse(bool Val = true) {
314 assert(isReg() && "Wrong MachineOperand accessor");
315 assert((Val || !isDebug()) && "Marking a debug operation as def");
319 void setIsDef(bool Val = true) {
320 assert(isReg() && "Wrong MachineOperand accessor");
321 assert((!Val || !isDebug()) && "Marking a debug operation as def");
325 void setImplicit(bool Val = true) {
326 assert(isReg() && "Wrong MachineOperand accessor");
330 void setIsKill(bool Val = true) {
331 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
332 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
336 void setIsDead(bool Val = true) {
337 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
341 void setIsUndef(bool Val = true) {
342 assert(isReg() && "Wrong MachineOperand accessor");
346 void setIsEarlyClobber(bool Val = true) {
347 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
348 IsEarlyClobber = Val;
351 void setIsDebug(bool Val = true) {
352 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
356 //===--------------------------------------------------------------------===//
357 // Accessors for various operand types.
358 //===--------------------------------------------------------------------===//
360 int64_t getImm() const {
361 assert(isImm() && "Wrong MachineOperand accessor");
362 return Contents.ImmVal;
365 const ConstantInt *getCImm() const {
366 assert(isCImm() && "Wrong MachineOperand accessor");
370 const ConstantFP *getFPImm() const {
371 assert(isFPImm() && "Wrong MachineOperand accessor");
375 MachineBasicBlock *getMBB() const {
376 assert(isMBB() && "Wrong MachineOperand accessor");
380 int getIndex() const {
381 assert((isFI() || isCPI() || isJTI()) &&
382 "Wrong MachineOperand accessor");
383 return Contents.OffsetedInfo.Val.Index;
386 const GlobalValue *getGlobal() const {
387 assert(isGlobal() && "Wrong MachineOperand accessor");
388 return Contents.OffsetedInfo.Val.GV;
391 const BlockAddress *getBlockAddress() const {
392 assert(isBlockAddress() && "Wrong MachineOperand accessor");
393 return Contents.OffsetedInfo.Val.BA;
396 MCSymbol *getMCSymbol() const {
397 assert(isMCSymbol() && "Wrong MachineOperand accessor");
401 /// getOffset - Return the offset from the symbol in this operand. This always
402 /// returns 0 for ExternalSymbol operands.
403 int64_t getOffset() const {
404 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
405 "Wrong MachineOperand accessor");
406 return (int64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
407 SmallContents.OffsetLo;
410 const char *getSymbolName() const {
411 assert(isSymbol() && "Wrong MachineOperand accessor");
412 return Contents.OffsetedInfo.Val.SymbolName;
415 const MDNode *getMetadata() const {
416 assert(isMetadata() && "Wrong MachineOperand accessor");
420 //===--------------------------------------------------------------------===//
421 // Mutators for various operand types.
422 //===--------------------------------------------------------------------===//
424 void setImm(int64_t immVal) {
425 assert(isImm() && "Wrong MachineOperand mutator");
426 Contents.ImmVal = immVal;
429 void setOffset(int64_t Offset) {
430 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
431 "Wrong MachineOperand accessor");
432 SmallContents.OffsetLo = unsigned(Offset);
433 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
436 void setIndex(int Idx) {
437 assert((isFI() || isCPI() || isJTI()) &&
438 "Wrong MachineOperand accessor");
439 Contents.OffsetedInfo.Val.Index = Idx;
442 void setMBB(MachineBasicBlock *MBB) {
443 assert(isMBB() && "Wrong MachineOperand accessor");
447 //===--------------------------------------------------------------------===//
449 //===--------------------------------------------------------------------===//
451 /// isIdenticalTo - Return true if this operand is identical to the specified
452 /// operand. Note: This method ignores isKill and isDead properties.
453 bool isIdenticalTo(const MachineOperand &Other) const;
455 /// ChangeToImmediate - Replace this operand with a new immediate operand of
456 /// the specified value. If an operand is known to be an immediate already,
457 /// the setImm method should be used.
458 void ChangeToImmediate(int64_t ImmVal);
460 /// ChangeToRegister - Replace this operand with a new register operand of
461 /// the specified value. If an operand is known to be an register already,
462 /// the setReg method should be used.
463 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
464 bool isKill = false, bool isDead = false,
465 bool isUndef = false, bool isDebug = false);
467 //===--------------------------------------------------------------------===//
468 // Construction methods.
469 //===--------------------------------------------------------------------===//
471 static MachineOperand CreateImm(int64_t Val) {
472 MachineOperand Op(MachineOperand::MO_Immediate);
477 static MachineOperand CreateCImm(const ConstantInt *CI) {
478 MachineOperand Op(MachineOperand::MO_CImmediate);
483 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
484 MachineOperand Op(MachineOperand::MO_FPImmediate);
485 Op.Contents.CFP = CFP;
489 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
490 bool isKill = false, bool isDead = false,
491 bool isUndef = false,
492 bool isEarlyClobber = false,
494 bool isDebug = false) {
495 MachineOperand Op(MachineOperand::MO_Register);
500 Op.IsUndef = isUndef;
501 Op.IsEarlyClobber = isEarlyClobber;
502 Op.IsDebug = isDebug;
503 Op.SmallContents.RegNo = Reg;
504 Op.Contents.Reg.Prev = 0;
505 Op.Contents.Reg.Next = 0;
509 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
510 unsigned char TargetFlags = 0) {
511 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
513 Op.setTargetFlags(TargetFlags);
516 static MachineOperand CreateFI(int Idx) {
517 MachineOperand Op(MachineOperand::MO_FrameIndex);
521 static MachineOperand CreateCPI(unsigned Idx, int Offset,
522 unsigned char TargetFlags = 0) {
523 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
525 Op.setOffset(Offset);
526 Op.setTargetFlags(TargetFlags);
529 static MachineOperand CreateJTI(unsigned Idx,
530 unsigned char TargetFlags = 0) {
531 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
533 Op.setTargetFlags(TargetFlags);
536 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
537 unsigned char TargetFlags = 0) {
538 MachineOperand Op(MachineOperand::MO_GlobalAddress);
539 Op.Contents.OffsetedInfo.Val.GV = GV;
540 Op.setOffset(Offset);
541 Op.setTargetFlags(TargetFlags);
544 static MachineOperand CreateES(const char *SymName,
545 unsigned char TargetFlags = 0) {
546 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
547 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
548 Op.setOffset(0); // Offset is always 0.
549 Op.setTargetFlags(TargetFlags);
552 static MachineOperand CreateBA(const BlockAddress *BA,
553 unsigned char TargetFlags = 0) {
554 MachineOperand Op(MachineOperand::MO_BlockAddress);
555 Op.Contents.OffsetedInfo.Val.BA = BA;
556 Op.setOffset(0); // Offset is always 0.
557 Op.setTargetFlags(TargetFlags);
560 static MachineOperand CreateMetadata(const MDNode *Meta) {
561 MachineOperand Op(MachineOperand::MO_Metadata);
562 Op.Contents.MD = Meta;
566 static MachineOperand CreateMCSymbol(MCSymbol *Sym) {
567 MachineOperand Op(MachineOperand::MO_MCSymbol);
568 Op.Contents.Sym = Sym;
572 friend class MachineInstr;
573 friend class MachineRegisterInfo;
575 //===--------------------------------------------------------------------===//
576 // Methods for handling register use/def lists.
577 //===--------------------------------------------------------------------===//
579 /// isOnRegUseList - Return true if this operand is on a register use/def list
580 /// or false if not. This can only be called for register operands that are
581 /// part of a machine instruction.
582 bool isOnRegUseList() const {
583 assert(isReg() && "Can only add reg operand to use lists");
584 return Contents.Reg.Prev != 0;
587 /// AddRegOperandToRegInfo - Add this register operand to the specified
588 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
589 /// explicitly nulled out.
590 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
592 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
593 /// MachineRegisterInfo it is linked with.
594 void RemoveRegOperandFromRegInfo();
597 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
602 } // End llvm namespace