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 is a register def / use of "undef", i.e. register
87 /// defined by an IMPLICIT_DEF. This is only valid on registers.
90 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
91 /// by the MachineInstr before all input registers are read. This is used to
92 /// model the GCC inline asm '&' constraint modifier.
93 bool IsEarlyClobber : 1;
95 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
96 /// not a real instruction. Such uses should be ignored during codegen.
99 /// SmallContents - This really should be part of the Contents union, but
100 /// lives out here so we can get a better packed struct.
101 /// MO_Register: Register number.
102 /// OffsetedInfo: Low bits of offset.
104 unsigned RegNo; // For MO_Register.
105 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
108 /// ParentMI - This is the instruction that this operand is embedded into.
109 /// This is valid for all operand types, when the operand is in an instr.
110 MachineInstr *ParentMI;
112 /// Contents union - This contains the payload for the various operand types.
114 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
115 const ConstantFP *CFP; // For MO_FPImmediate.
116 const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit.
117 int64_t ImmVal; // For MO_Immediate.
118 const MDNode *MD; // For MO_Metadata.
119 MCSymbol *Sym; // For MO_MCSymbol
121 struct { // For MO_Register.
122 // Register number is in SmallContents.RegNo.
123 MachineOperand **Prev; // Access list for register.
124 MachineOperand *Next;
127 /// OffsetedInfo - This struct contains the offset and an object identifier.
128 /// this represent the object as with an optional offset from it.
131 int Index; // For MO_*Index - The index itself.
132 const char *SymbolName; // For MO_ExternalSymbol.
133 const GlobalValue *GV; // For MO_GlobalAddress.
134 const BlockAddress *BA; // For MO_BlockAddress.
136 // Low bits of offset are in SmallContents.OffsetLo.
137 int OffsetHi; // An offset from the object, high 32 bits.
141 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
145 /// getType - Returns the MachineOperandType for this operand.
147 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
149 unsigned char getTargetFlags() const { return TargetFlags; }
150 void setTargetFlags(unsigned char F) { TargetFlags = F; }
151 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
154 /// getParent - Return the instruction that this operand belongs to.
156 MachineInstr *getParent() { return ParentMI; }
157 const MachineInstr *getParent() const { return ParentMI; }
159 /// clearParent - Reset the parent pointer.
161 /// The MachineOperand copy constructor also copies ParentMI, expecting the
162 /// original to be deleted. If a MachineOperand is ever stored outside a
163 /// MachineInstr, the parent pointer must be cleared.
165 /// Never call clearParent() on an operand in a MachineInstr.
167 void clearParent() { ParentMI = 0; }
169 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
171 //===--------------------------------------------------------------------===//
172 // Accessors that tell you what kind of MachineOperand you're looking at.
173 //===--------------------------------------------------------------------===//
175 /// isReg - Tests if this is a MO_Register operand.
176 bool isReg() const { return OpKind == MO_Register; }
177 /// isImm - Tests if this is a MO_Immediate operand.
178 bool isImm() const { return OpKind == MO_Immediate; }
179 /// isCImm - Test if t his is a MO_CImmediate operand.
180 bool isCImm() const { return OpKind == MO_CImmediate; }
181 /// isFPImm - Tests if this is a MO_FPImmediate operand.
182 bool isFPImm() const { return OpKind == MO_FPImmediate; }
183 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
184 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
185 /// isFI - Tests if this is a MO_FrameIndex operand.
186 bool isFI() const { return OpKind == MO_FrameIndex; }
187 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
188 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
189 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
190 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
191 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
192 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
193 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
194 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
195 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
196 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
197 /// isMetadata - Tests if this is a MO_Metadata operand.
198 bool isMetadata() const { return OpKind == MO_Metadata; }
199 bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
201 //===--------------------------------------------------------------------===//
202 // Accessors for Register Operands
203 //===--------------------------------------------------------------------===//
205 /// getReg - Returns the register number.
206 unsigned getReg() const {
207 assert(isReg() && "This is not a register operand!");
208 return SmallContents.RegNo;
211 unsigned getSubReg() const {
212 assert(isReg() && "Wrong MachineOperand accessor");
213 return (unsigned)SubReg;
217 assert(isReg() && "Wrong MachineOperand accessor");
222 assert(isReg() && "Wrong MachineOperand accessor");
226 bool isImplicit() const {
227 assert(isReg() && "Wrong MachineOperand accessor");
231 bool isDead() const {
232 assert(isReg() && "Wrong MachineOperand accessor");
236 bool isKill() const {
237 assert(isReg() && "Wrong MachineOperand accessor");
241 bool isUndef() const {
242 assert(isReg() && "Wrong MachineOperand accessor");
246 bool isEarlyClobber() const {
247 assert(isReg() && "Wrong MachineOperand accessor");
248 return IsEarlyClobber;
251 bool isDebug() const {
252 assert(isReg() && "Wrong MachineOperand accessor");
256 /// getNextOperandForReg - Return the next MachineOperand in the function that
257 /// uses or defines this register.
258 MachineOperand *getNextOperandForReg() const {
259 assert(isReg() && "This is not a register operand!");
260 return Contents.Reg.Next;
263 //===--------------------------------------------------------------------===//
264 // Mutators for Register Operands
265 //===--------------------------------------------------------------------===//
267 /// Change the register this operand corresponds to.
269 void setReg(unsigned Reg);
271 void setSubReg(unsigned subReg) {
272 assert(isReg() && "Wrong MachineOperand accessor");
273 SubReg = (unsigned char)subReg;
276 /// substVirtReg - Substitute the current register with the virtual
277 /// subregister Reg:SubReg. Take any existing SubReg index into account,
278 /// using TargetRegisterInfo to compose the subreg indices if necessary.
279 /// Reg must be a virtual register, SubIdx can be 0.
281 void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo&);
283 /// substPhysReg - Substitute the current register with the physical register
284 /// Reg, taking any existing SubReg into account. For instance,
285 /// substPhysReg(%EAX) will change %reg1024:sub_8bit to %AL.
287 void substPhysReg(unsigned Reg, const TargetRegisterInfo&);
289 void setIsUse(bool Val = true) {
290 assert(isReg() && "Wrong MachineOperand accessor");
291 assert((Val || !isDebug()) && "Marking a debug operation as def");
295 void setIsDef(bool Val = true) {
296 assert(isReg() && "Wrong MachineOperand accessor");
297 assert((!Val || !isDebug()) && "Marking a debug operation as def");
301 void setImplicit(bool Val = true) {
302 assert(isReg() && "Wrong MachineOperand accessor");
306 void setIsKill(bool Val = true) {
307 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
308 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
312 void setIsDead(bool Val = true) {
313 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
317 void setIsUndef(bool Val = true) {
318 assert(isReg() && "Wrong MachineOperand accessor");
322 void setIsEarlyClobber(bool Val = true) {
323 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
324 IsEarlyClobber = Val;
327 void setIsDebug(bool Val = true) {
328 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
332 //===--------------------------------------------------------------------===//
333 // Accessors for various operand types.
334 //===--------------------------------------------------------------------===//
336 int64_t getImm() const {
337 assert(isImm() && "Wrong MachineOperand accessor");
338 return Contents.ImmVal;
341 const ConstantInt *getCImm() const {
342 assert(isCImm() && "Wrong MachineOperand accessor");
346 const ConstantFP *getFPImm() const {
347 assert(isFPImm() && "Wrong MachineOperand accessor");
351 MachineBasicBlock *getMBB() const {
352 assert(isMBB() && "Wrong MachineOperand accessor");
356 int getIndex() const {
357 assert((isFI() || isCPI() || isJTI()) &&
358 "Wrong MachineOperand accessor");
359 return Contents.OffsetedInfo.Val.Index;
362 const GlobalValue *getGlobal() const {
363 assert(isGlobal() && "Wrong MachineOperand accessor");
364 return Contents.OffsetedInfo.Val.GV;
367 const BlockAddress *getBlockAddress() const {
368 assert(isBlockAddress() && "Wrong MachineOperand accessor");
369 return Contents.OffsetedInfo.Val.BA;
372 MCSymbol *getMCSymbol() const {
373 assert(isMCSymbol() && "Wrong MachineOperand accessor");
377 /// getOffset - Return the offset from the symbol in this operand. This always
378 /// returns 0 for ExternalSymbol operands.
379 int64_t getOffset() const {
380 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
381 "Wrong MachineOperand accessor");
382 return (int64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
383 SmallContents.OffsetLo;
386 const char *getSymbolName() const {
387 assert(isSymbol() && "Wrong MachineOperand accessor");
388 return Contents.OffsetedInfo.Val.SymbolName;
391 const MDNode *getMetadata() const {
392 assert(isMetadata() && "Wrong MachineOperand accessor");
396 //===--------------------------------------------------------------------===//
397 // Mutators for various operand types.
398 //===--------------------------------------------------------------------===//
400 void setImm(int64_t immVal) {
401 assert(isImm() && "Wrong MachineOperand mutator");
402 Contents.ImmVal = immVal;
405 void setOffset(int64_t Offset) {
406 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
407 "Wrong MachineOperand accessor");
408 SmallContents.OffsetLo = unsigned(Offset);
409 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
412 void setIndex(int Idx) {
413 assert((isFI() || isCPI() || isJTI()) &&
414 "Wrong MachineOperand accessor");
415 Contents.OffsetedInfo.Val.Index = Idx;
418 void setMBB(MachineBasicBlock *MBB) {
419 assert(isMBB() && "Wrong MachineOperand accessor");
423 //===--------------------------------------------------------------------===//
425 //===--------------------------------------------------------------------===//
427 /// isIdenticalTo - Return true if this operand is identical to the specified
428 /// operand. Note: This method ignores isKill and isDead properties.
429 bool isIdenticalTo(const MachineOperand &Other) const;
431 /// ChangeToImmediate - Replace this operand with a new immediate operand of
432 /// the specified value. If an operand is known to be an immediate already,
433 /// the setImm method should be used.
434 void ChangeToImmediate(int64_t ImmVal);
436 /// ChangeToRegister - Replace this operand with a new register operand of
437 /// the specified value. If an operand is known to be an register already,
438 /// the setReg method should be used.
439 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
440 bool isKill = false, bool isDead = false,
441 bool isUndef = false, bool isDebug = false);
443 //===--------------------------------------------------------------------===//
444 // Construction methods.
445 //===--------------------------------------------------------------------===//
447 static MachineOperand CreateImm(int64_t Val) {
448 MachineOperand Op(MachineOperand::MO_Immediate);
453 static MachineOperand CreateCImm(const ConstantInt *CI) {
454 MachineOperand Op(MachineOperand::MO_CImmediate);
459 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
460 MachineOperand Op(MachineOperand::MO_FPImmediate);
461 Op.Contents.CFP = CFP;
465 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
466 bool isKill = false, bool isDead = false,
467 bool isUndef = false,
468 bool isEarlyClobber = false,
470 bool isDebug = false) {
471 MachineOperand Op(MachineOperand::MO_Register);
476 Op.IsUndef = isUndef;
477 Op.IsEarlyClobber = isEarlyClobber;
478 Op.IsDebug = isDebug;
479 Op.SmallContents.RegNo = Reg;
480 Op.Contents.Reg.Prev = 0;
481 Op.Contents.Reg.Next = 0;
485 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
486 unsigned char TargetFlags = 0) {
487 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
489 Op.setTargetFlags(TargetFlags);
492 static MachineOperand CreateFI(int Idx) {
493 MachineOperand Op(MachineOperand::MO_FrameIndex);
497 static MachineOperand CreateCPI(unsigned Idx, int Offset,
498 unsigned char TargetFlags = 0) {
499 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
501 Op.setOffset(Offset);
502 Op.setTargetFlags(TargetFlags);
505 static MachineOperand CreateJTI(unsigned Idx,
506 unsigned char TargetFlags = 0) {
507 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
509 Op.setTargetFlags(TargetFlags);
512 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
513 unsigned char TargetFlags = 0) {
514 MachineOperand Op(MachineOperand::MO_GlobalAddress);
515 Op.Contents.OffsetedInfo.Val.GV = GV;
516 Op.setOffset(Offset);
517 Op.setTargetFlags(TargetFlags);
520 static MachineOperand CreateES(const char *SymName,
521 unsigned char TargetFlags = 0) {
522 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
523 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
524 Op.setOffset(0); // Offset is always 0.
525 Op.setTargetFlags(TargetFlags);
528 static MachineOperand CreateBA(const BlockAddress *BA,
529 unsigned char TargetFlags = 0) {
530 MachineOperand Op(MachineOperand::MO_BlockAddress);
531 Op.Contents.OffsetedInfo.Val.BA = BA;
532 Op.setOffset(0); // Offset is always 0.
533 Op.setTargetFlags(TargetFlags);
536 static MachineOperand CreateMetadata(const MDNode *Meta) {
537 MachineOperand Op(MachineOperand::MO_Metadata);
538 Op.Contents.MD = Meta;
542 static MachineOperand CreateMCSymbol(MCSymbol *Sym) {
543 MachineOperand Op(MachineOperand::MO_MCSymbol);
544 Op.Contents.Sym = Sym;
548 friend class MachineInstr;
549 friend class MachineRegisterInfo;
551 //===--------------------------------------------------------------------===//
552 // Methods for handling register use/def lists.
553 //===--------------------------------------------------------------------===//
555 /// isOnRegUseList - Return true if this operand is on a register use/def list
556 /// or false if not. This can only be called for register operands that are
557 /// part of a machine instruction.
558 bool isOnRegUseList() const {
559 assert(isReg() && "Can only add reg operand to use lists");
560 return Contents.Reg.Prev != 0;
563 /// AddRegOperandToRegInfo - Add this register operand to the specified
564 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
565 /// explicitly nulled out.
566 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
568 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
569 /// MachineRegisterInfo it is linked with.
570 void RemoveRegOperandFromRegInfo();
573 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
578 } // End llvm namespace