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"
24 class MachineBasicBlock;
28 class MachineRegisterInfo;
32 /// MachineOperand class - Representation of each machine instruction operand.
34 class MachineOperand {
36 enum MachineOperandType {
37 MO_Register, ///< Register operand.
38 MO_Immediate, ///< Immediate operand
39 MO_FPImmediate, ///< Floating-point immediate operand
40 MO_MachineBasicBlock, ///< MachineBasicBlock reference
41 MO_FrameIndex, ///< Abstract Stack Frame Index
42 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
43 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
44 MO_ExternalSymbol, ///< Name of external global symbol
45 MO_GlobalAddress, ///< Address of a global value
46 MO_BlockAddress, ///< Address of a basic block
47 MO_Metadata ///< Metadata reference (for debug info)
51 /// OpKind - Specify what kind of operand this is. This discriminates the
53 unsigned char OpKind; // MachineOperandType
55 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
56 /// indicates the MO_Register has no subReg.
59 /// TargetFlags - This is a set of target-specific operand flags.
60 unsigned char TargetFlags;
62 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
65 /// IsDef - True if this is a def, false if this is a use of the register.
69 /// IsImp - True if this is an implicit def or use, false if it is explicit.
73 /// IsKill - True if this instruction is the last use of the register on this
74 /// path through the function. This is only valid on uses of registers.
77 /// IsDead - True if this register is never used by a subsequent instruction.
78 /// This is only valid on definitions of registers.
81 /// IsUndef - True if this is a register def / use of "undef", i.e. register
82 /// defined by an IMPLICIT_DEF. This is only valid on registers.
85 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
86 /// by the MachineInstr before all input registers are read. This is used to
87 /// model the GCC inline asm '&' constraint modifier.
88 bool IsEarlyClobber : 1;
90 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
91 /// not a real instruction. Such uses should be ignored during codegen.
94 /// ParentMI - This is the instruction that this operand is embedded into.
95 /// This is valid for all operand types, when the operand is in an instr.
96 MachineInstr *ParentMI;
98 /// Contents union - This contains the payload for the various operand types.
100 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
101 const ConstantFP *CFP; // For MO_FPImmediate.
102 int64_t ImmVal; // For MO_Immediate.
103 MDNode *MD; // For MO_Metadata.
105 struct { // For MO_Register.
107 MachineOperand **Prev; // Access list for register.
108 MachineOperand *Next;
111 /// OffsetedInfo - This struct contains the offset and an object identifier.
112 /// this represent the object as with an optional offset from it.
115 int Index; // For MO_*Index - The index itself.
116 const char *SymbolName; // For MO_ExternalSymbol.
117 GlobalValue *GV; // For MO_GlobalAddress.
118 BlockAddress *BA; // For MO_BlockAddress.
120 int64_t Offset; // An offset from the object.
124 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
128 /// getType - Returns the MachineOperandType for this operand.
130 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
132 unsigned char getTargetFlags() const { return TargetFlags; }
133 void setTargetFlags(unsigned char F) { TargetFlags = F; }
134 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
137 /// getParent - Return the instruction that this operand belongs to.
139 MachineInstr *getParent() { return ParentMI; }
140 const MachineInstr *getParent() const { return ParentMI; }
142 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
144 //===--------------------------------------------------------------------===//
145 // Accessors that tell you what kind of MachineOperand you're looking at.
146 //===--------------------------------------------------------------------===//
148 /// isReg - Tests if this is a MO_Register operand.
149 bool isReg() const { return OpKind == MO_Register; }
150 /// isImm - Tests if this is a MO_Immediate operand.
151 bool isImm() const { return OpKind == MO_Immediate; }
152 /// isFPImm - Tests if this is a MO_FPImmediate operand.
153 bool isFPImm() const { return OpKind == MO_FPImmediate; }
154 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
155 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
156 /// isFI - Tests if this is a MO_FrameIndex operand.
157 bool isFI() const { return OpKind == MO_FrameIndex; }
158 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
159 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
160 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
161 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
162 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
163 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
164 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
165 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
166 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
167 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
168 /// isMetadata - Tests if this is a MO_Metadata operand.
169 bool isMetadata() const { return OpKind == MO_Metadata; }
171 //===--------------------------------------------------------------------===//
172 // Accessors for Register Operands
173 //===--------------------------------------------------------------------===//
175 /// getReg - Returns the register number.
176 unsigned getReg() const {
177 assert(isReg() && "This is not a register operand!");
178 return Contents.Reg.RegNo;
181 unsigned getSubReg() const {
182 assert(isReg() && "Wrong MachineOperand accessor");
183 return (unsigned)SubReg;
187 assert(isReg() && "Wrong MachineOperand accessor");
192 assert(isReg() && "Wrong MachineOperand accessor");
196 bool isImplicit() const {
197 assert(isReg() && "Wrong MachineOperand accessor");
201 bool isDead() const {
202 assert(isReg() && "Wrong MachineOperand accessor");
206 bool isKill() const {
207 assert(isReg() && "Wrong MachineOperand accessor");
211 bool isUndef() const {
212 assert(isReg() && "Wrong MachineOperand accessor");
216 bool isEarlyClobber() const {
217 assert(isReg() && "Wrong MachineOperand accessor");
218 return IsEarlyClobber;
221 bool isDebug() const {
222 assert(isReg() && "Wrong MachineOperand accessor");
223 assert(!isDef() && "Wrong MachineOperand accessor");
227 /// getNextOperandForReg - Return the next MachineOperand in the function that
228 /// uses or defines this register.
229 MachineOperand *getNextOperandForReg() const {
230 assert(isReg() && "This is not a register operand!");
231 return Contents.Reg.Next;
234 //===--------------------------------------------------------------------===//
235 // Mutators for Register Operands
236 //===--------------------------------------------------------------------===//
238 /// Change the register this operand corresponds to.
240 void setReg(unsigned Reg);
242 void setSubReg(unsigned subReg) {
243 assert(isReg() && "Wrong MachineOperand accessor");
244 SubReg = (unsigned char)subReg;
247 void setIsUse(bool Val = true) {
248 assert(isReg() && "Wrong MachineOperand accessor");
249 assert((Val || !isDebug()) && "Marking a debug operation as def");
253 void setIsDef(bool Val = true) {
254 assert(isReg() && "Wrong MachineOperand accessor");
255 assert((!Val || !isDebug()) && "Marking a debug operation as def");
259 void setImplicit(bool Val = true) {
260 assert(isReg() && "Wrong MachineOperand accessor");
264 void setIsKill(bool Val = true) {
265 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
266 assert((!Val || !isDebug()) && "Marking a debug operation as kill");
270 void setIsDead(bool Val = true) {
271 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
275 void setIsUndef(bool Val = true) {
276 assert(isReg() && "Wrong MachineOperand accessor");
280 void setIsEarlyClobber(bool Val = true) {
281 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
282 IsEarlyClobber = Val;
285 //===--------------------------------------------------------------------===//
286 // Accessors for various operand types.
287 //===--------------------------------------------------------------------===//
289 int64_t getImm() const {
290 assert(isImm() && "Wrong MachineOperand accessor");
291 return Contents.ImmVal;
294 const ConstantFP *getFPImm() const {
295 assert(isFPImm() && "Wrong MachineOperand accessor");
299 MachineBasicBlock *getMBB() const {
300 assert(isMBB() && "Wrong MachineOperand accessor");
304 int getIndex() const {
305 assert((isFI() || isCPI() || isJTI()) &&
306 "Wrong MachineOperand accessor");
307 return Contents.OffsetedInfo.Val.Index;
310 GlobalValue *getGlobal() const {
311 assert(isGlobal() && "Wrong MachineOperand accessor");
312 return Contents.OffsetedInfo.Val.GV;
315 BlockAddress *getBlockAddress() const {
316 assert(isBlockAddress() && "Wrong MachineOperand accessor");
317 return Contents.OffsetedInfo.Val.BA;
320 /// getOffset - Return the offset from the symbol in this operand. This always
321 /// returns 0 for ExternalSymbol operands.
322 int64_t getOffset() const {
323 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
324 "Wrong MachineOperand accessor");
325 return Contents.OffsetedInfo.Offset;
328 const char *getSymbolName() const {
329 assert(isSymbol() && "Wrong MachineOperand accessor");
330 return Contents.OffsetedInfo.Val.SymbolName;
333 const MDNode *getMetadata() const {
334 assert(isMetadata() && "Wrong MachineOperand accessor");
338 //===--------------------------------------------------------------------===//
339 // Mutators for various operand types.
340 //===--------------------------------------------------------------------===//
342 void setImm(int64_t immVal) {
343 assert(isImm() && "Wrong MachineOperand mutator");
344 Contents.ImmVal = immVal;
347 void setOffset(int64_t Offset) {
348 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
349 "Wrong MachineOperand accessor");
350 Contents.OffsetedInfo.Offset = Offset;
353 void setIndex(int Idx) {
354 assert((isFI() || isCPI() || isJTI()) &&
355 "Wrong MachineOperand accessor");
356 Contents.OffsetedInfo.Val.Index = Idx;
359 void setMBB(MachineBasicBlock *MBB) {
360 assert(isMBB() && "Wrong MachineOperand accessor");
364 //===--------------------------------------------------------------------===//
366 //===--------------------------------------------------------------------===//
368 /// isIdenticalTo - Return true if this operand is identical to the specified
369 /// operand. Note: This method ignores isKill and isDead properties.
370 bool isIdenticalTo(const MachineOperand &Other) const;
372 /// ChangeToImmediate - Replace this operand with a new immediate operand of
373 /// the specified value. If an operand is known to be an immediate already,
374 /// the setImm method should be used.
375 void ChangeToImmediate(int64_t ImmVal);
377 /// ChangeToRegister - Replace this operand with a new register operand of
378 /// the specified value. If an operand is known to be an register already,
379 /// the setReg method should be used.
380 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
381 bool isKill = false, bool isDead = false,
382 bool isUndef = false, bool isDebug = false);
384 //===--------------------------------------------------------------------===//
385 // Construction methods.
386 //===--------------------------------------------------------------------===//
388 static MachineOperand CreateImm(int64_t Val) {
389 MachineOperand Op(MachineOperand::MO_Immediate);
394 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
395 MachineOperand Op(MachineOperand::MO_FPImmediate);
396 Op.Contents.CFP = CFP;
400 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
401 bool isKill = false, bool isDead = false,
402 bool isUndef = false,
403 bool isEarlyClobber = false,
405 bool isDebug = false) {
406 MachineOperand Op(MachineOperand::MO_Register);
411 Op.IsUndef = isUndef;
412 Op.IsEarlyClobber = isEarlyClobber;
413 Op.IsDebug = isDebug;
414 Op.Contents.Reg.RegNo = Reg;
415 Op.Contents.Reg.Prev = 0;
416 Op.Contents.Reg.Next = 0;
420 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
421 unsigned char TargetFlags = 0) {
422 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
424 Op.setTargetFlags(TargetFlags);
427 static MachineOperand CreateFI(unsigned Idx) {
428 MachineOperand Op(MachineOperand::MO_FrameIndex);
432 static MachineOperand CreateCPI(unsigned Idx, int Offset,
433 unsigned char TargetFlags = 0) {
434 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
436 Op.setOffset(Offset);
437 Op.setTargetFlags(TargetFlags);
440 static MachineOperand CreateJTI(unsigned Idx,
441 unsigned char TargetFlags = 0) {
442 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
444 Op.setTargetFlags(TargetFlags);
447 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
448 unsigned char TargetFlags = 0) {
449 MachineOperand Op(MachineOperand::MO_GlobalAddress);
450 Op.Contents.OffsetedInfo.Val.GV = GV;
451 Op.setOffset(Offset);
452 Op.setTargetFlags(TargetFlags);
455 static MachineOperand CreateES(const char *SymName,
456 unsigned char TargetFlags = 0) {
457 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
458 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
459 Op.setOffset(0); // Offset is always 0.
460 Op.setTargetFlags(TargetFlags);
463 static MachineOperand CreateBA(BlockAddress *BA,
464 unsigned char TargetFlags = 0) {
465 MachineOperand Op(MachineOperand::MO_BlockAddress);
466 Op.Contents.OffsetedInfo.Val.BA = BA;
467 Op.setOffset(0); // Offset is always 0.
468 Op.setTargetFlags(TargetFlags);
471 static MachineOperand CreateMetadata(MDNode *Meta) {
472 MachineOperand Op(MachineOperand::MO_Metadata);
473 Op.Contents.MD = Meta;
477 friend class MachineInstr;
478 friend class MachineRegisterInfo;
480 //===--------------------------------------------------------------------===//
481 // Methods for handling register use/def lists.
482 //===--------------------------------------------------------------------===//
484 /// isOnRegUseList - Return true if this operand is on a register use/def list
485 /// or false if not. This can only be called for register operands that are
486 /// part of a machine instruction.
487 bool isOnRegUseList() const {
488 assert(isReg() && "Can only add reg operand to use lists");
489 return Contents.Reg.Prev != 0;
492 /// AddRegOperandToRegInfo - Add this register operand to the specified
493 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
494 /// explicitly nulled out.
495 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
497 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
498 /// MachineRegisterInfo it is linked with.
499 void RemoveRegOperandFromRegInfo();
502 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
507 } // End llvm namespace