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"
25 class MachineBasicBlock;
27 class MachineRegisterInfo;
30 class TargetRegisterInfo;
34 /// MachineOperand class - Representation of each machine instruction operand.
36 class MachineOperand {
38 enum MachineOperandType {
39 MO_Register, ///< Register operand.
40 MO_Immediate, ///< Immediate operand
41 MO_FPImmediate, ///< Floating-point immediate operand
42 MO_MachineBasicBlock, ///< MachineBasicBlock reference
43 MO_FrameIndex, ///< Abstract Stack Frame Index
44 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
45 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
46 MO_ExternalSymbol, ///< Name of external global symbol
47 MO_GlobalAddress, ///< Address of a global value
48 MO_BlockAddress, ///< Address of a basic block
49 MO_Metadata, ///< Metadata reference (for debug info)
50 MO_MCSymbol ///< MCSymbol reference (for debug/eh info)
54 /// OpKind - Specify what kind of operand this is. This discriminates the
56 unsigned char OpKind; // MachineOperandType
58 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
59 /// indicates the MO_Register has no subReg.
62 /// TargetFlags - This is a set of target-specific operand flags.
63 unsigned char TargetFlags;
65 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
68 /// IsDef - True if this is a def, false if this is a use of the register.
72 /// IsImp - True if this is an implicit def or use, false if it is explicit.
76 /// IsKill - True if this instruction is the last use of the register on this
77 /// path through the function. This is only valid on uses of registers.
80 /// IsDead - True if this register is never used by a subsequent instruction.
81 /// This is only valid on definitions of registers.
84 /// IsUndef - True if this is a register def / use of "undef", i.e. register
85 /// defined by an IMPLICIT_DEF. This is only valid on registers.
88 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
89 /// by the MachineInstr before all input registers are read. This is used to
90 /// model the GCC inline asm '&' constraint modifier.
91 bool IsEarlyClobber : 1;
93 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
94 /// not a real instruction. Such uses should be ignored during codegen.
97 /// SmallContents - Thisreally should be part of the Contents union, but lives
98 /// out here so we can get a better packed struct.
99 /// MO_Register: Register number.
100 /// OffsetedInfo: Low bits of offset.
102 unsigned RegNo; // For MO_Register.
103 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
106 /// ParentMI - This is the instruction that this operand is embedded into.
107 /// This is valid for all operand types, when the operand is in an instr.
108 MachineInstr *ParentMI;
110 /// Contents union - This contains the payload for the various operand types.
112 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
113 const ConstantFP *CFP; // For MO_FPImmediate.
114 int64_t ImmVal; // For MO_Immediate.
115 const MDNode *MD; // For MO_Metadata.
116 MCSymbol *Sym; // For MO_MCSymbol
118 struct { // For MO_Register.
119 // Register number is in SmallContents.RegNo.
120 MachineOperand **Prev; // Access list for register.
121 MachineOperand *Next;
124 /// OffsetedInfo - This struct contains the offset and an object identifier.
125 /// this represent the object as with an optional offset from it.
128 int Index; // For MO_*Index - The index itself.
129 const char *SymbolName; // For MO_ExternalSymbol.
130 const GlobalValue *GV; // For MO_GlobalAddress.
131 const BlockAddress *BA; // For MO_BlockAddress.
133 // Low bits of offset are in SmallContents.OffsetLo.
134 int OffsetHi; // An offset from the object, high 32 bits.
138 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
142 /// getType - Returns the MachineOperandType for this operand.
144 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
146 unsigned char getTargetFlags() const { return TargetFlags; }
147 void setTargetFlags(unsigned char F) { TargetFlags = F; }
148 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
151 /// getParent - Return the instruction that this operand belongs to.
153 MachineInstr *getParent() { return ParentMI; }
154 const MachineInstr *getParent() const { return ParentMI; }
156 /// clearParent - Reset the parent pointer.
158 /// The MachineOperand copy constructor also copies ParentMI, expecting the
159 /// original to be deleted. If a MachineOperand is ever stored outside a
160 /// MachineInstr, the parent pointer must be cleared.
162 /// Never call clearParent() on an operand in a MachineInstr.
164 void clearParent() { ParentMI = 0; }
166 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
168 //===--------------------------------------------------------------------===//
169 // Accessors that tell you what kind of MachineOperand you're looking at.
170 //===--------------------------------------------------------------------===//
172 /// isReg - Tests if this is a MO_Register operand.
173 bool isReg() const { return OpKind == MO_Register; }
174 /// isImm - Tests if this is a MO_Immediate operand.
175 bool isImm() const { return OpKind == MO_Immediate; }
176 /// isFPImm - Tests if this is a MO_FPImmediate operand.
177 bool isFPImm() const { return OpKind == MO_FPImmediate; }
178 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
179 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
180 /// isFI - Tests if this is a MO_FrameIndex operand.
181 bool isFI() const { return OpKind == MO_FrameIndex; }
182 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
183 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
184 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
185 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
186 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
187 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
188 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
189 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
190 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
191 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
192 /// isMetadata - Tests if this is a MO_Metadata operand.
193 bool isMetadata() const { return OpKind == MO_Metadata; }
194 bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
196 //===--------------------------------------------------------------------===//
197 // Accessors for Register Operands
198 //===--------------------------------------------------------------------===//
200 /// getReg - Returns the register number.
201 unsigned getReg() const {
202 assert(isReg() && "This is not a register operand!");
203 return SmallContents.RegNo;
206 unsigned getSubReg() const {
207 assert(isReg() && "Wrong MachineOperand accessor");
208 return (unsigned)SubReg;
212 assert(isReg() && "Wrong MachineOperand accessor");
217 assert(isReg() && "Wrong MachineOperand accessor");
221 bool isImplicit() const {
222 assert(isReg() && "Wrong MachineOperand accessor");
226 bool isDead() const {
227 assert(isReg() && "Wrong MachineOperand accessor");
231 bool isKill() const {
232 assert(isReg() && "Wrong MachineOperand accessor");
236 bool isUndef() const {
237 assert(isReg() && "Wrong MachineOperand accessor");
241 bool isEarlyClobber() const {
242 assert(isReg() && "Wrong MachineOperand accessor");
243 return IsEarlyClobber;
246 bool isDebug() const {
247 assert(isReg() && "Wrong MachineOperand accessor");
251 /// getNextOperandForReg - Return the next MachineOperand in the function that
252 /// uses or defines this register.
253 MachineOperand *getNextOperandForReg() const {
254 assert(isReg() && "This is not a register operand!");
255 return Contents.Reg.Next;
258 //===--------------------------------------------------------------------===//
259 // Mutators for Register Operands
260 //===--------------------------------------------------------------------===//
262 /// Change the register this operand corresponds to.
264 void setReg(unsigned Reg);
266 void setSubReg(unsigned subReg) {
267 assert(isReg() && "Wrong MachineOperand accessor");
268 SubReg = (unsigned char)subReg;
271 /// substVirtReg - Substitute the current register with the virtual
272 /// subregister Reg:SubReg. Take any existing SubReg index into account,
273 /// using TargetRegisterInfo to compose the subreg indices if necessary.
274 /// Reg must be a virtual register, SubIdx can be 0.
276 void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo&);
278 /// substPhysReg - Substitute the current register with the physical register
279 /// Reg, taking any existing SubReg into account. For instance,
280 /// substPhysReg(%EAX) will change %reg1024:sub_8bit to %AL.
282 void substPhysReg(unsigned Reg, const TargetRegisterInfo&);
284 void setIsUse(bool Val = true) {
285 assert(isReg() && "Wrong MachineOperand accessor");
286 assert((Val || !isDebug()) && "Marking a debug operation as def");
290 void setIsDef(bool Val = true) {
291 assert(isReg() && "Wrong MachineOperand accessor");
292 assert((!Val || !isDebug()) && "Marking a debug operation as def");
296 void setImplicit(bool Val = true) {
297 assert(isReg() && "Wrong MachineOperand accessor");
301 void setIsKill(bool Val = true) {
302 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
303 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
307 void setIsDead(bool Val = true) {
308 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
312 void setIsUndef(bool Val = true) {
313 assert(isReg() && "Wrong MachineOperand accessor");
317 void setIsEarlyClobber(bool Val = true) {
318 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
319 IsEarlyClobber = Val;
322 void setIsDebug(bool Val = true) {
323 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
327 //===--------------------------------------------------------------------===//
328 // Accessors for various operand types.
329 //===--------------------------------------------------------------------===//
331 int64_t getImm() const {
332 assert(isImm() && "Wrong MachineOperand accessor");
333 return Contents.ImmVal;
336 const ConstantFP *getFPImm() const {
337 assert(isFPImm() && "Wrong MachineOperand accessor");
341 MachineBasicBlock *getMBB() const {
342 assert(isMBB() && "Wrong MachineOperand accessor");
346 int getIndex() const {
347 assert((isFI() || isCPI() || isJTI()) &&
348 "Wrong MachineOperand accessor");
349 return Contents.OffsetedInfo.Val.Index;
352 const GlobalValue *getGlobal() const {
353 assert(isGlobal() && "Wrong MachineOperand accessor");
354 return Contents.OffsetedInfo.Val.GV;
357 const BlockAddress *getBlockAddress() const {
358 assert(isBlockAddress() && "Wrong MachineOperand accessor");
359 return Contents.OffsetedInfo.Val.BA;
362 MCSymbol *getMCSymbol() const {
363 assert(isMCSymbol() && "Wrong MachineOperand accessor");
367 /// getOffset - Return the offset from the symbol in this operand. This always
368 /// returns 0 for ExternalSymbol operands.
369 int64_t getOffset() const {
370 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
371 "Wrong MachineOperand accessor");
372 return (int64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
373 SmallContents.OffsetLo;
376 const char *getSymbolName() const {
377 assert(isSymbol() && "Wrong MachineOperand accessor");
378 return Contents.OffsetedInfo.Val.SymbolName;
381 const MDNode *getMetadata() const {
382 assert(isMetadata() && "Wrong MachineOperand accessor");
386 //===--------------------------------------------------------------------===//
387 // Mutators for various operand types.
388 //===--------------------------------------------------------------------===//
390 void setImm(int64_t immVal) {
391 assert(isImm() && "Wrong MachineOperand mutator");
392 Contents.ImmVal = immVal;
395 void setOffset(int64_t Offset) {
396 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
397 "Wrong MachineOperand accessor");
398 SmallContents.OffsetLo = unsigned(Offset);
399 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
402 void setIndex(int Idx) {
403 assert((isFI() || isCPI() || isJTI()) &&
404 "Wrong MachineOperand accessor");
405 Contents.OffsetedInfo.Val.Index = Idx;
408 void setMBB(MachineBasicBlock *MBB) {
409 assert(isMBB() && "Wrong MachineOperand accessor");
413 //===--------------------------------------------------------------------===//
415 //===--------------------------------------------------------------------===//
417 /// isIdenticalTo - Return true if this operand is identical to the specified
418 /// operand. Note: This method ignores isKill and isDead properties.
419 bool isIdenticalTo(const MachineOperand &Other) const;
421 /// ChangeToImmediate - Replace this operand with a new immediate operand of
422 /// the specified value. If an operand is known to be an immediate already,
423 /// the setImm method should be used.
424 void ChangeToImmediate(int64_t ImmVal);
426 /// ChangeToRegister - Replace this operand with a new register operand of
427 /// the specified value. If an operand is known to be an register already,
428 /// the setReg method should be used.
429 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
430 bool isKill = false, bool isDead = false,
431 bool isUndef = false, bool isDebug = false);
433 //===--------------------------------------------------------------------===//
434 // Construction methods.
435 //===--------------------------------------------------------------------===//
437 static MachineOperand CreateImm(int64_t Val) {
438 MachineOperand Op(MachineOperand::MO_Immediate);
443 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
444 MachineOperand Op(MachineOperand::MO_FPImmediate);
445 Op.Contents.CFP = CFP;
449 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
450 bool isKill = false, bool isDead = false,
451 bool isUndef = false,
452 bool isEarlyClobber = false,
454 bool isDebug = false) {
455 MachineOperand Op(MachineOperand::MO_Register);
460 Op.IsUndef = isUndef;
461 Op.IsEarlyClobber = isEarlyClobber;
462 Op.IsDebug = isDebug;
463 Op.SmallContents.RegNo = Reg;
464 Op.Contents.Reg.Prev = 0;
465 Op.Contents.Reg.Next = 0;
469 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
470 unsigned char TargetFlags = 0) {
471 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
473 Op.setTargetFlags(TargetFlags);
476 static MachineOperand CreateFI(int Idx) {
477 MachineOperand Op(MachineOperand::MO_FrameIndex);
481 static MachineOperand CreateCPI(unsigned Idx, int Offset,
482 unsigned char TargetFlags = 0) {
483 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
485 Op.setOffset(Offset);
486 Op.setTargetFlags(TargetFlags);
489 static MachineOperand CreateJTI(unsigned Idx,
490 unsigned char TargetFlags = 0) {
491 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
493 Op.setTargetFlags(TargetFlags);
496 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
497 unsigned char TargetFlags = 0) {
498 MachineOperand Op(MachineOperand::MO_GlobalAddress);
499 Op.Contents.OffsetedInfo.Val.GV = GV;
500 Op.setOffset(Offset);
501 Op.setTargetFlags(TargetFlags);
504 static MachineOperand CreateES(const char *SymName,
505 unsigned char TargetFlags = 0) {
506 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
507 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
508 Op.setOffset(0); // Offset is always 0.
509 Op.setTargetFlags(TargetFlags);
512 static MachineOperand CreateBA(const BlockAddress *BA,
513 unsigned char TargetFlags = 0) {
514 MachineOperand Op(MachineOperand::MO_BlockAddress);
515 Op.Contents.OffsetedInfo.Val.BA = BA;
516 Op.setOffset(0); // Offset is always 0.
517 Op.setTargetFlags(TargetFlags);
520 static MachineOperand CreateMetadata(const MDNode *Meta) {
521 MachineOperand Op(MachineOperand::MO_Metadata);
522 Op.Contents.MD = Meta;
526 static MachineOperand CreateMCSymbol(MCSymbol *Sym) {
527 MachineOperand Op(MachineOperand::MO_MCSymbol);
528 Op.Contents.Sym = Sym;
532 friend class MachineInstr;
533 friend class MachineRegisterInfo;
535 //===--------------------------------------------------------------------===//
536 // Methods for handling register use/def lists.
537 //===--------------------------------------------------------------------===//
539 /// isOnRegUseList - Return true if this operand is on a register use/def list
540 /// or false if not. This can only be called for register operands that are
541 /// part of a machine instruction.
542 bool isOnRegUseList() const {
543 assert(isReg() && "Can only add reg operand to use lists");
544 return Contents.Reg.Prev != 0;
547 /// AddRegOperandToRegInfo - Add this register operand to the specified
548 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
549 /// explicitly nulled out.
550 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
552 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
553 /// MachineRegisterInfo it is linked with.
554 void RemoveRegOperandFromRegInfo();
557 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
562 } // End llvm namespace