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 /// IsInternalRead - True if this operand reads a value that was defined
106 /// inside the same instruction or bundle. This flag can be set on both use
107 /// and def operands. On a sub-register def operand, it refers to the part
108 /// of the register that isn't written. On a full-register def operand, it
111 /// When this flag is set, the instruction bundle must contain at least one
112 /// other def of the register. If multiple instructions in the bundle define
113 /// the register, the meaning is target-defined.
114 bool IsInternalRead : 1;
116 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
117 /// by the MachineInstr before all input registers are read. This is used to
118 /// model the GCC inline asm '&' constraint modifier.
119 bool IsEarlyClobber : 1;
121 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
122 /// not a real instruction. Such uses should be ignored during codegen.
125 /// SmallContents - This really should be part of the Contents union, but
126 /// lives out here so we can get a better packed struct.
127 /// MO_Register: Register number.
128 /// OffsetedInfo: Low bits of offset.
130 unsigned RegNo; // For MO_Register.
131 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
134 /// ParentMI - This is the instruction that this operand is embedded into.
135 /// This is valid for all operand types, when the operand is in an instr.
136 MachineInstr *ParentMI;
138 /// Contents union - This contains the payload for the various operand types.
140 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
141 const ConstantFP *CFP; // For MO_FPImmediate.
142 const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit.
143 int64_t ImmVal; // For MO_Immediate.
144 const MDNode *MD; // For MO_Metadata.
145 MCSymbol *Sym; // For MO_MCSymbol
147 struct { // For MO_Register.
148 // Register number is in SmallContents.RegNo.
149 MachineOperand **Prev; // Access list for register.
150 MachineOperand *Next;
153 /// OffsetedInfo - This struct contains the offset and an object identifier.
154 /// this represent the object as with an optional offset from it.
157 int Index; // For MO_*Index - The index itself.
158 const char *SymbolName; // For MO_ExternalSymbol.
159 const GlobalValue *GV; // For MO_GlobalAddress.
160 const BlockAddress *BA; // For MO_BlockAddress.
162 // Low bits of offset are in SmallContents.OffsetLo.
163 int OffsetHi; // An offset from the object, high 32 bits.
167 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
171 /// getType - Returns the MachineOperandType for this operand.
173 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
175 unsigned char getTargetFlags() const { return TargetFlags; }
176 void setTargetFlags(unsigned char F) { TargetFlags = F; }
177 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
180 /// getParent - Return the instruction that this operand belongs to.
182 MachineInstr *getParent() { return ParentMI; }
183 const MachineInstr *getParent() const { return ParentMI; }
185 /// clearParent - Reset the parent pointer.
187 /// The MachineOperand copy constructor also copies ParentMI, expecting the
188 /// original to be deleted. If a MachineOperand is ever stored outside a
189 /// MachineInstr, the parent pointer must be cleared.
191 /// Never call clearParent() on an operand in a MachineInstr.
193 void clearParent() { ParentMI = 0; }
195 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
197 //===--------------------------------------------------------------------===//
198 // Accessors that tell you what kind of MachineOperand you're looking at.
199 //===--------------------------------------------------------------------===//
201 /// isReg - Tests if this is a MO_Register operand.
202 bool isReg() const { return OpKind == MO_Register; }
203 /// isImm - Tests if this is a MO_Immediate operand.
204 bool isImm() const { return OpKind == MO_Immediate; }
205 /// isCImm - Test if t his is a MO_CImmediate operand.
206 bool isCImm() const { return OpKind == MO_CImmediate; }
207 /// isFPImm - Tests if this is a MO_FPImmediate operand.
208 bool isFPImm() const { return OpKind == MO_FPImmediate; }
209 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
210 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
211 /// isFI - Tests if this is a MO_FrameIndex operand.
212 bool isFI() const { return OpKind == MO_FrameIndex; }
213 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
214 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
215 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
216 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
217 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
218 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
219 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
220 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
221 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
222 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
223 /// isMetadata - Tests if this is a MO_Metadata operand.
224 bool isMetadata() const { return OpKind == MO_Metadata; }
225 bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
227 //===--------------------------------------------------------------------===//
228 // Accessors for Register Operands
229 //===--------------------------------------------------------------------===//
231 /// getReg - Returns the register number.
232 unsigned getReg() const {
233 assert(isReg() && "This is not a register operand!");
234 return SmallContents.RegNo;
237 unsigned getSubReg() const {
238 assert(isReg() && "Wrong MachineOperand accessor");
239 return (unsigned)SubReg;
243 assert(isReg() && "Wrong MachineOperand accessor");
248 assert(isReg() && "Wrong MachineOperand accessor");
252 bool isImplicit() const {
253 assert(isReg() && "Wrong MachineOperand accessor");
257 bool isDead() const {
258 assert(isReg() && "Wrong MachineOperand accessor");
262 bool isKill() const {
263 assert(isReg() && "Wrong MachineOperand accessor");
267 bool isUndef() const {
268 assert(isReg() && "Wrong MachineOperand accessor");
272 bool isInternalRead() const {
273 assert(isReg() && "Wrong MachineOperand accessor");
274 return IsInternalRead;
277 bool isEarlyClobber() const {
278 assert(isReg() && "Wrong MachineOperand accessor");
279 return IsEarlyClobber;
282 bool isDebug() const {
283 assert(isReg() && "Wrong MachineOperand accessor");
287 /// readsReg - Returns true if this operand reads the previous value of its
288 /// register. A use operand with the <undef> flag set doesn't read its
289 /// register. A sub-register def implicitly reads the other parts of the
290 /// register being redefined unless the <undef> flag is set.
292 /// This refers to reading the register value from before the current
293 /// instruction or bundle. Internal bundle reads are not included.
294 bool readsReg() const {
295 assert(isReg() && "Wrong MachineOperand accessor");
296 return !isUndef() && !isInternalRead() && (isUse() || getSubReg());
299 /// getNextOperandForReg - Return the next MachineOperand in the function that
300 /// uses or defines this register.
301 MachineOperand *getNextOperandForReg() const {
302 assert(isReg() && "This is not a register operand!");
303 return Contents.Reg.Next;
306 //===--------------------------------------------------------------------===//
307 // Mutators for Register Operands
308 //===--------------------------------------------------------------------===//
310 /// Change the register this operand corresponds to.
312 void setReg(unsigned Reg);
314 void setSubReg(unsigned subReg) {
315 assert(isReg() && "Wrong MachineOperand accessor");
316 SubReg = (unsigned char)subReg;
319 /// substVirtReg - Substitute the current register with the virtual
320 /// subregister Reg:SubReg. Take any existing SubReg index into account,
321 /// using TargetRegisterInfo to compose the subreg indices if necessary.
322 /// Reg must be a virtual register, SubIdx can be 0.
324 void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo&);
326 /// substPhysReg - Substitute the current register with the physical register
327 /// Reg, taking any existing SubReg into account. For instance,
328 /// substPhysReg(%EAX) will change %reg1024:sub_8bit to %AL.
330 void substPhysReg(unsigned Reg, const TargetRegisterInfo&);
332 void setIsUse(bool Val = true) {
333 assert(isReg() && "Wrong MachineOperand accessor");
334 assert((Val || !isDebug()) && "Marking a debug operation as def");
338 void setIsDef(bool Val = true) {
339 assert(isReg() && "Wrong MachineOperand accessor");
340 assert((!Val || !isDebug()) && "Marking a debug operation as def");
344 void setImplicit(bool Val = true) {
345 assert(isReg() && "Wrong MachineOperand accessor");
349 void setIsKill(bool Val = true) {
350 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
351 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
355 void setIsDead(bool Val = true) {
356 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
360 void setIsUndef(bool Val = true) {
361 assert(isReg() && "Wrong MachineOperand accessor");
365 void setIsInternalRead(bool Val = true) {
366 assert(isReg() && "Wrong MachineOperand accessor");
367 IsInternalRead = Val;
370 void setIsEarlyClobber(bool Val = true) {
371 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
372 IsEarlyClobber = Val;
375 void setIsDebug(bool Val = true) {
376 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
380 //===--------------------------------------------------------------------===//
381 // Accessors for various operand types.
382 //===--------------------------------------------------------------------===//
384 int64_t getImm() const {
385 assert(isImm() && "Wrong MachineOperand accessor");
386 return Contents.ImmVal;
389 const ConstantInt *getCImm() const {
390 assert(isCImm() && "Wrong MachineOperand accessor");
394 const ConstantFP *getFPImm() const {
395 assert(isFPImm() && "Wrong MachineOperand accessor");
399 MachineBasicBlock *getMBB() const {
400 assert(isMBB() && "Wrong MachineOperand accessor");
404 int getIndex() const {
405 assert((isFI() || isCPI() || isJTI()) &&
406 "Wrong MachineOperand accessor");
407 return Contents.OffsetedInfo.Val.Index;
410 const GlobalValue *getGlobal() const {
411 assert(isGlobal() && "Wrong MachineOperand accessor");
412 return Contents.OffsetedInfo.Val.GV;
415 const BlockAddress *getBlockAddress() const {
416 assert(isBlockAddress() && "Wrong MachineOperand accessor");
417 return Contents.OffsetedInfo.Val.BA;
420 MCSymbol *getMCSymbol() const {
421 assert(isMCSymbol() && "Wrong MachineOperand accessor");
425 /// getOffset - Return the offset from the symbol in this operand. This always
426 /// returns 0 for ExternalSymbol operands.
427 int64_t getOffset() const {
428 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
429 "Wrong MachineOperand accessor");
430 return (int64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
431 SmallContents.OffsetLo;
434 const char *getSymbolName() const {
435 assert(isSymbol() && "Wrong MachineOperand accessor");
436 return Contents.OffsetedInfo.Val.SymbolName;
439 const MDNode *getMetadata() const {
440 assert(isMetadata() && "Wrong MachineOperand accessor");
444 //===--------------------------------------------------------------------===//
445 // Mutators for various operand types.
446 //===--------------------------------------------------------------------===//
448 void setImm(int64_t immVal) {
449 assert(isImm() && "Wrong MachineOperand mutator");
450 Contents.ImmVal = immVal;
453 void setOffset(int64_t Offset) {
454 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
455 "Wrong MachineOperand accessor");
456 SmallContents.OffsetLo = unsigned(Offset);
457 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
460 void setIndex(int Idx) {
461 assert((isFI() || isCPI() || isJTI()) &&
462 "Wrong MachineOperand accessor");
463 Contents.OffsetedInfo.Val.Index = Idx;
466 void setMBB(MachineBasicBlock *MBB) {
467 assert(isMBB() && "Wrong MachineOperand accessor");
471 //===--------------------------------------------------------------------===//
473 //===--------------------------------------------------------------------===//
475 /// isIdenticalTo - Return true if this operand is identical to the specified
476 /// operand. Note: This method ignores isKill and isDead properties.
477 bool isIdenticalTo(const MachineOperand &Other) const;
479 /// ChangeToImmediate - Replace this operand with a new immediate operand of
480 /// the specified value. If an operand is known to be an immediate already,
481 /// the setImm method should be used.
482 void ChangeToImmediate(int64_t ImmVal);
484 /// ChangeToRegister - Replace this operand with a new register operand of
485 /// the specified value. If an operand is known to be an register already,
486 /// the setReg method should be used.
487 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
488 bool isKill = false, bool isDead = false,
489 bool isUndef = false, bool isDebug = false);
491 //===--------------------------------------------------------------------===//
492 // Construction methods.
493 //===--------------------------------------------------------------------===//
495 static MachineOperand CreateImm(int64_t Val) {
496 MachineOperand Op(MachineOperand::MO_Immediate);
501 static MachineOperand CreateCImm(const ConstantInt *CI) {
502 MachineOperand Op(MachineOperand::MO_CImmediate);
507 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
508 MachineOperand Op(MachineOperand::MO_FPImmediate);
509 Op.Contents.CFP = CFP;
513 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
514 bool isKill = false, bool isDead = false,
515 bool isUndef = false,
516 bool isEarlyClobber = false,
518 bool isDebug = false) {
519 MachineOperand Op(MachineOperand::MO_Register);
524 Op.IsUndef = isUndef;
525 Op.IsInternalRead = false;
526 Op.IsEarlyClobber = isEarlyClobber;
527 Op.IsDebug = isDebug;
528 Op.SmallContents.RegNo = Reg;
529 Op.Contents.Reg.Prev = 0;
530 Op.Contents.Reg.Next = 0;
534 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
535 unsigned char TargetFlags = 0) {
536 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
538 Op.setTargetFlags(TargetFlags);
541 static MachineOperand CreateFI(int Idx) {
542 MachineOperand Op(MachineOperand::MO_FrameIndex);
546 static MachineOperand CreateCPI(unsigned Idx, int Offset,
547 unsigned char TargetFlags = 0) {
548 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
550 Op.setOffset(Offset);
551 Op.setTargetFlags(TargetFlags);
554 static MachineOperand CreateJTI(unsigned Idx,
555 unsigned char TargetFlags = 0) {
556 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
558 Op.setTargetFlags(TargetFlags);
561 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
562 unsigned char TargetFlags = 0) {
563 MachineOperand Op(MachineOperand::MO_GlobalAddress);
564 Op.Contents.OffsetedInfo.Val.GV = GV;
565 Op.setOffset(Offset);
566 Op.setTargetFlags(TargetFlags);
569 static MachineOperand CreateES(const char *SymName,
570 unsigned char TargetFlags = 0) {
571 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
572 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
573 Op.setOffset(0); // Offset is always 0.
574 Op.setTargetFlags(TargetFlags);
577 static MachineOperand CreateBA(const BlockAddress *BA,
578 unsigned char TargetFlags = 0) {
579 MachineOperand Op(MachineOperand::MO_BlockAddress);
580 Op.Contents.OffsetedInfo.Val.BA = BA;
581 Op.setOffset(0); // Offset is always 0.
582 Op.setTargetFlags(TargetFlags);
585 static MachineOperand CreateMetadata(const MDNode *Meta) {
586 MachineOperand Op(MachineOperand::MO_Metadata);
587 Op.Contents.MD = Meta;
591 static MachineOperand CreateMCSymbol(MCSymbol *Sym) {
592 MachineOperand Op(MachineOperand::MO_MCSymbol);
593 Op.Contents.Sym = Sym;
597 friend class MachineInstr;
598 friend class MachineRegisterInfo;
600 //===--------------------------------------------------------------------===//
601 // Methods for handling register use/def lists.
602 //===--------------------------------------------------------------------===//
604 /// isOnRegUseList - Return true if this operand is on a register use/def list
605 /// or false if not. This can only be called for register operands that are
606 /// part of a machine instruction.
607 bool isOnRegUseList() const {
608 assert(isReg() && "Can only add reg operand to use lists");
609 return Contents.Reg.Prev != 0;
612 /// AddRegOperandToRegInfo - Add this register operand to the specified
613 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
614 /// explicitly nulled out.
615 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
617 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
618 /// MachineRegisterInfo it is linked with.
619 void RemoveRegOperandFromRegInfo();
622 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
627 } // End llvm namespace