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"
24 class MachineBasicBlock;
28 class MachineRegisterInfo;
31 /// MachineOperand class - Representation of each machine instruction operand.
33 class MachineOperand {
35 enum MachineOperandType {
36 MO_Register, ///< Register operand.
37 MO_Immediate, ///< Immediate operand
38 MO_FPImmediate, ///< Floating-point immediate operand
39 MO_MachineBasicBlock, ///< MachineBasicBlock reference
40 MO_FrameIndex, ///< Abstract Stack Frame Index
41 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
42 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
43 MO_ExternalSymbol, ///< Name of external global symbol
44 MO_GlobalAddress ///< Address of a global value
48 /// OpKind - Specify what kind of operand this is. This discriminates the
50 unsigned char OpKind; // MachineOperandType
52 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
53 /// indicates the MO_Register has no subReg.
56 /// TargetFlags - This is a set of target-specific operand flags.
57 unsigned char TargetFlags;
59 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
62 /// IsDef - True if this is a def, false if this is a use of the register.
66 /// IsImp - True if this is an implicit def or use, false if it is explicit.
70 /// IsKill - True if this instruction is the last use of the register on this
71 /// path through the function. This is only valid on uses of registers.
74 /// IsDead - True if this register is never used by a subsequent instruction.
75 /// This is only valid on definitions of registers.
78 /// IsUndef - True if this is a register def / use of "undef", i.e. register
79 /// defined by an IMPLICIT_DEF. This is only valid on registers.
82 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
83 /// by the MachineInstr before all input registers are read. This is used to
84 /// model the GCC inline asm '&' constraint modifier.
85 bool IsEarlyClobber : 1;
87 /// ParentMI - This is the instruction that this operand is embedded into.
88 /// This is valid for all operand types, when the operand is in an instr.
89 MachineInstr *ParentMI;
91 /// Contents union - This contains the payload for the various operand types.
93 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
94 const ConstantFP *CFP; // For MO_FPImmediate.
95 int64_t ImmVal; // For MO_Immediate.
97 struct { // For MO_Register.
99 MachineOperand **Prev; // Access list for register.
100 MachineOperand *Next;
103 /// OffsetedInfo - This struct contains the offset and an object identifier.
104 /// this represent the object as with an optional offset from it.
107 int Index; // For MO_*Index - The index itself.
108 const char *SymbolName; // For MO_ExternalSymbol.
109 GlobalValue *GV; // For MO_GlobalAddress.
111 int64_t Offset; // An offset from the object.
115 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
119 MachineOperand(const MachineOperand &M) {
125 /// getType - Returns the MachineOperandType for this operand.
127 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
129 unsigned char getTargetFlags() const { return TargetFlags; }
130 void setTargetFlags(unsigned char F) { TargetFlags = F; }
131 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
134 /// getParent - Return the instruction that this operand belongs to.
136 MachineInstr *getParent() { return ParentMI; }
137 const MachineInstr *getParent() const { return ParentMI; }
139 void print(std::ostream &os, const TargetMachine *TM = 0) const;
140 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
142 //===--------------------------------------------------------------------===//
143 // Accessors that tell you what kind of MachineOperand you're looking at.
144 //===--------------------------------------------------------------------===//
146 /// isReg - Tests if this is a MO_Register operand.
147 bool isReg() const { return OpKind == MO_Register; }
148 /// isImm - Tests if this is a MO_Immediate operand.
149 bool isImm() const { return OpKind == MO_Immediate; }
150 /// isFPImm - Tests if this is a MO_FPImmediate operand.
151 bool isFPImm() const { return OpKind == MO_FPImmediate; }
152 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
153 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
154 /// isFI - Tests if this is a MO_FrameIndex operand.
155 bool isFI() const { return OpKind == MO_FrameIndex; }
156 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
157 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
158 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
159 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
160 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
161 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
162 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
163 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
165 //===--------------------------------------------------------------------===//
166 // Accessors for Register Operands
167 //===--------------------------------------------------------------------===//
169 /// getReg - Returns the register number.
170 unsigned getReg() const {
171 assert(isReg() && "This is not a register operand!");
172 return Contents.Reg.RegNo;
175 unsigned getSubReg() const {
176 assert(isReg() && "Wrong MachineOperand accessor");
177 return (unsigned)SubReg;
181 assert(isReg() && "Wrong MachineOperand accessor");
186 assert(isReg() && "Wrong MachineOperand accessor");
190 bool isImplicit() const {
191 assert(isReg() && "Wrong MachineOperand accessor");
195 bool isDead() const {
196 assert(isReg() && "Wrong MachineOperand accessor");
200 bool isKill() const {
201 assert(isReg() && "Wrong MachineOperand accessor");
205 bool isUndef() const {
206 assert(isReg() && "Wrong MachineOperand accessor");
210 bool isEarlyClobber() const {
211 assert(isReg() && "Wrong MachineOperand accessor");
212 return IsEarlyClobber;
215 /// getNextOperandForReg - Return the next MachineOperand in the function that
216 /// uses or defines this register.
217 MachineOperand *getNextOperandForReg() const {
218 assert(isReg() && "This is not a register operand!");
219 return Contents.Reg.Next;
222 //===--------------------------------------------------------------------===//
223 // Mutators for Register Operands
224 //===--------------------------------------------------------------------===//
226 /// Change the register this operand corresponds to.
228 void setReg(unsigned Reg);
230 void setSubReg(unsigned subReg) {
231 assert(isReg() && "Wrong MachineOperand accessor");
232 SubReg = (unsigned char)subReg;
235 void setIsUse(bool Val = true) {
236 assert(isReg() && "Wrong MachineOperand accessor");
240 void setIsDef(bool Val = true) {
241 assert(isReg() && "Wrong MachineOperand accessor");
245 void setImplicit(bool Val = true) {
246 assert(isReg() && "Wrong MachineOperand accessor");
250 void setIsKill(bool Val = true) {
251 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
255 void setIsDead(bool Val = true) {
256 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
260 void setIsUndef(bool Val = true) {
261 assert(isReg() && "Wrong MachineOperand accessor");
265 void setIsEarlyClobber(bool Val = true) {
266 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
267 IsEarlyClobber = Val;
270 //===--------------------------------------------------------------------===//
271 // Accessors for various operand types.
272 //===--------------------------------------------------------------------===//
274 int64_t getImm() const {
275 assert(isImm() && "Wrong MachineOperand accessor");
276 return Contents.ImmVal;
279 const ConstantFP *getFPImm() const {
280 assert(isFPImm() && "Wrong MachineOperand accessor");
284 MachineBasicBlock *getMBB() const {
285 assert(isMBB() && "Wrong MachineOperand accessor");
289 int getIndex() const {
290 assert((isFI() || isCPI() || isJTI()) &&
291 "Wrong MachineOperand accessor");
292 return Contents.OffsetedInfo.Val.Index;
295 GlobalValue *getGlobal() const {
296 assert(isGlobal() && "Wrong MachineOperand accessor");
297 return Contents.OffsetedInfo.Val.GV;
300 int64_t getOffset() const {
301 assert((isGlobal() || isSymbol() || isCPI()) &&
302 "Wrong MachineOperand accessor");
303 return Contents.OffsetedInfo.Offset;
306 const char *getSymbolName() const {
307 assert(isSymbol() && "Wrong MachineOperand accessor");
308 return Contents.OffsetedInfo.Val.SymbolName;
311 //===--------------------------------------------------------------------===//
312 // Mutators for various operand types.
313 //===--------------------------------------------------------------------===//
315 void setImm(int64_t immVal) {
316 assert(isImm() && "Wrong MachineOperand mutator");
317 Contents.ImmVal = immVal;
320 void setOffset(int64_t Offset) {
321 assert((isGlobal() || isSymbol() || isCPI()) &&
322 "Wrong MachineOperand accessor");
323 Contents.OffsetedInfo.Offset = Offset;
326 void setIndex(int Idx) {
327 assert((isFI() || isCPI() || isJTI()) &&
328 "Wrong MachineOperand accessor");
329 Contents.OffsetedInfo.Val.Index = Idx;
332 void setMBB(MachineBasicBlock *MBB) {
333 assert(isMBB() && "Wrong MachineOperand accessor");
337 //===--------------------------------------------------------------------===//
339 //===--------------------------------------------------------------------===//
341 /// isIdenticalTo - Return true if this operand is identical to the specified
342 /// operand. Note: This method ignores isKill and isDead properties.
343 bool isIdenticalTo(const MachineOperand &Other) const;
345 /// ChangeToImmediate - Replace this operand with a new immediate operand of
346 /// the specified value. If an operand is known to be an immediate already,
347 /// the setImm method should be used.
348 void ChangeToImmediate(int64_t ImmVal);
350 /// ChangeToRegister - Replace this operand with a new register operand of
351 /// the specified value. If an operand is known to be an register already,
352 /// the setReg method should be used.
353 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
354 bool isKill = false, bool isDead = false,
355 bool isUndef = false);
357 //===--------------------------------------------------------------------===//
358 // Construction methods.
359 //===--------------------------------------------------------------------===//
361 static MachineOperand CreateImm(int64_t Val) {
362 MachineOperand Op(MachineOperand::MO_Immediate);
367 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
368 MachineOperand Op(MachineOperand::MO_FPImmediate);
369 Op.Contents.CFP = CFP;
373 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
374 bool isKill = false, bool isDead = false,
375 bool isUndef = false,
376 bool isEarlyClobber = false,
377 unsigned SubReg = 0) {
378 MachineOperand Op(MachineOperand::MO_Register);
383 Op.IsUndef = isUndef;
384 Op.IsEarlyClobber = isEarlyClobber;
385 Op.Contents.Reg.RegNo = Reg;
386 Op.Contents.Reg.Prev = 0;
387 Op.Contents.Reg.Next = 0;
391 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
392 unsigned char TargetFlags = 0) {
393 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
395 Op.setTargetFlags(TargetFlags);
398 static MachineOperand CreateFI(unsigned Idx) {
399 MachineOperand Op(MachineOperand::MO_FrameIndex);
403 static MachineOperand CreateCPI(unsigned Idx, int Offset,
404 unsigned char TargetFlags = 0) {
405 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
407 Op.setOffset(Offset);
408 Op.setTargetFlags(TargetFlags);
411 static MachineOperand CreateJTI(unsigned Idx,
412 unsigned char TargetFlags = 0) {
413 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
415 Op.setTargetFlags(TargetFlags);
418 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
419 unsigned char TargetFlags = 0) {
420 MachineOperand Op(MachineOperand::MO_GlobalAddress);
421 Op.Contents.OffsetedInfo.Val.GV = GV;
422 Op.setOffset(Offset);
423 Op.setTargetFlags(TargetFlags);
426 static MachineOperand CreateES(const char *SymName, int64_t Offset = 0,
427 unsigned char TargetFlags = 0) {
428 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
429 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
430 Op.setOffset(Offset);
431 Op.setTargetFlags(TargetFlags);
434 const MachineOperand &operator=(const MachineOperand &MO) {
440 IsUndef = MO.IsUndef;
441 IsEarlyClobber = MO.IsEarlyClobber;
443 ParentMI = MO.ParentMI;
444 Contents = MO.Contents;
445 TargetFlags = MO.TargetFlags;
449 friend class MachineInstr;
450 friend class MachineRegisterInfo;
452 //===--------------------------------------------------------------------===//
453 // Methods for handling register use/def lists.
454 //===--------------------------------------------------------------------===//
456 /// isOnRegUseList - Return true if this operand is on a register use/def list
457 /// or false if not. This can only be called for register operands that are
458 /// part of a machine instruction.
459 bool isOnRegUseList() const {
460 assert(isReg() && "Can only add reg operand to use lists");
461 return Contents.Reg.Prev != 0;
464 /// AddRegOperandToRegInfo - Add this register operand to the specified
465 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
466 /// explicitly nulled out.
467 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
469 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
470 /// MachineRegisterInfo it is linked with.
471 void RemoveRegOperandFromRegInfo();
474 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
479 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
484 } // End llvm namespace