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;
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
45 MO_BlockAddress ///< Address of a basic block
49 /// OpKind - Specify what kind of operand this is. This discriminates the
51 unsigned char OpKind; // MachineOperandType
53 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
54 /// indicates the MO_Register has no subReg.
57 /// TargetFlags - This is a set of target-specific operand flags.
58 unsigned char TargetFlags;
60 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
63 /// IsDef - True if this is a def, false if this is a use of the register.
67 /// IsImp - True if this is an implicit def or use, false if it is explicit.
71 /// IsKill - True if this instruction is the last use of the register on this
72 /// path through the function. This is only valid on uses of registers.
75 /// IsDead - True if this register is never used by a subsequent instruction.
76 /// This is only valid on definitions of registers.
79 /// IsUndef - True if this is a register def / use of "undef", i.e. register
80 /// defined by an IMPLICIT_DEF. This is only valid on registers.
83 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
84 /// by the MachineInstr before all input registers are read. This is used to
85 /// model the GCC inline asm '&' constraint modifier.
86 bool IsEarlyClobber : 1;
88 /// ParentMI - This is the instruction that this operand is embedded into.
89 /// This is valid for all operand types, when the operand is in an instr.
90 MachineInstr *ParentMI;
92 /// Contents union - This contains the payload for the various operand types.
94 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
95 const ConstantFP *CFP; // For MO_FPImmediate.
96 int64_t ImmVal; // For MO_Immediate.
98 struct { // For MO_Register.
100 MachineOperand **Prev; // Access list for register.
101 MachineOperand *Next;
104 /// OffsetedInfo - This struct contains the offset and an object identifier.
105 /// this represent the object as with an optional offset from it.
108 int Index; // For MO_*Index - The index itself.
109 const char *SymbolName; // For MO_ExternalSymbol.
110 GlobalValue *GV; // For MO_GlobalAddress.
111 BlockAddress *BA; // For MO_BlockAddress.
113 int64_t Offset; // An offset from the object.
117 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
121 /// getType - Returns the MachineOperandType for this operand.
123 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
125 unsigned char getTargetFlags() const { return TargetFlags; }
126 void setTargetFlags(unsigned char F) { TargetFlags = F; }
127 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
130 /// getParent - Return the instruction that this operand belongs to.
132 MachineInstr *getParent() { return ParentMI; }
133 const MachineInstr *getParent() const { return ParentMI; }
135 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
137 //===--------------------------------------------------------------------===//
138 // Accessors that tell you what kind of MachineOperand you're looking at.
139 //===--------------------------------------------------------------------===//
141 /// isReg - Tests if this is a MO_Register operand.
142 bool isReg() const { return OpKind == MO_Register; }
143 /// isImm - Tests if this is a MO_Immediate operand.
144 bool isImm() const { return OpKind == MO_Immediate; }
145 /// isFPImm - Tests if this is a MO_FPImmediate operand.
146 bool isFPImm() const { return OpKind == MO_FPImmediate; }
147 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
148 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
149 /// isFI - Tests if this is a MO_FrameIndex operand.
150 bool isFI() const { return OpKind == MO_FrameIndex; }
151 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
152 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
153 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
154 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
155 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
156 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
157 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
158 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
159 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
160 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
162 //===--------------------------------------------------------------------===//
163 // Accessors for Register Operands
164 //===--------------------------------------------------------------------===//
166 /// getReg - Returns the register number.
167 unsigned getReg() const {
168 assert(isReg() && "This is not a register operand!");
169 return Contents.Reg.RegNo;
172 unsigned getSubReg() const {
173 assert(isReg() && "Wrong MachineOperand accessor");
174 return (unsigned)SubReg;
178 assert(isReg() && "Wrong MachineOperand accessor");
183 assert(isReg() && "Wrong MachineOperand accessor");
187 bool isImplicit() const {
188 assert(isReg() && "Wrong MachineOperand accessor");
192 bool isDead() const {
193 assert(isReg() && "Wrong MachineOperand accessor");
197 bool isKill() const {
198 assert(isReg() && "Wrong MachineOperand accessor");
202 bool isUndef() const {
203 assert(isReg() && "Wrong MachineOperand accessor");
207 bool isEarlyClobber() const {
208 assert(isReg() && "Wrong MachineOperand accessor");
209 return IsEarlyClobber;
212 /// getNextOperandForReg - Return the next MachineOperand in the function that
213 /// uses or defines this register.
214 MachineOperand *getNextOperandForReg() const {
215 assert(isReg() && "This is not a register operand!");
216 return Contents.Reg.Next;
219 //===--------------------------------------------------------------------===//
220 // Mutators for Register Operands
221 //===--------------------------------------------------------------------===//
223 /// Change the register this operand corresponds to.
225 void setReg(unsigned Reg);
227 void setSubReg(unsigned subReg) {
228 assert(isReg() && "Wrong MachineOperand accessor");
229 SubReg = (unsigned char)subReg;
232 void setIsUse(bool Val = true) {
233 assert(isReg() && "Wrong MachineOperand accessor");
237 void setIsDef(bool Val = true) {
238 assert(isReg() && "Wrong MachineOperand accessor");
242 void setImplicit(bool Val = true) {
243 assert(isReg() && "Wrong MachineOperand accessor");
247 void setIsKill(bool Val = true) {
248 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
252 void setIsDead(bool Val = true) {
253 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
257 void setIsUndef(bool Val = true) {
258 assert(isReg() && "Wrong MachineOperand accessor");
262 void setIsEarlyClobber(bool Val = true) {
263 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
264 IsEarlyClobber = Val;
267 //===--------------------------------------------------------------------===//
268 // Accessors for various operand types.
269 //===--------------------------------------------------------------------===//
271 int64_t getImm() const {
272 assert(isImm() && "Wrong MachineOperand accessor");
273 return Contents.ImmVal;
276 const ConstantFP *getFPImm() const {
277 assert(isFPImm() && "Wrong MachineOperand accessor");
281 MachineBasicBlock *getMBB() const {
282 assert(isMBB() && "Wrong MachineOperand accessor");
286 int getIndex() const {
287 assert((isFI() || isCPI() || isJTI()) &&
288 "Wrong MachineOperand accessor");
289 return Contents.OffsetedInfo.Val.Index;
292 GlobalValue *getGlobal() const {
293 assert(isGlobal() && "Wrong MachineOperand accessor");
294 return Contents.OffsetedInfo.Val.GV;
297 BlockAddress *getBlockAddress() const {
298 assert(isBlockAddress() && "Wrong MachineOperand accessor");
299 return Contents.OffsetedInfo.Val.BA;
302 /// getOffset - Return the offset from the symbol in this operand. This always
303 /// returns 0 for ExternalSymbol operands.
304 int64_t getOffset() const {
305 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
306 "Wrong MachineOperand accessor");
307 return Contents.OffsetedInfo.Offset;
310 const char *getSymbolName() const {
311 assert(isSymbol() && "Wrong MachineOperand accessor");
312 return Contents.OffsetedInfo.Val.SymbolName;
315 //===--------------------------------------------------------------------===//
316 // Mutators for various operand types.
317 //===--------------------------------------------------------------------===//
319 void setImm(int64_t immVal) {
320 assert(isImm() && "Wrong MachineOperand mutator");
321 Contents.ImmVal = immVal;
324 void setOffset(int64_t Offset) {
325 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
326 "Wrong MachineOperand accessor");
327 Contents.OffsetedInfo.Offset = Offset;
330 void setIndex(int Idx) {
331 assert((isFI() || isCPI() || isJTI()) &&
332 "Wrong MachineOperand accessor");
333 Contents.OffsetedInfo.Val.Index = Idx;
336 void setMBB(MachineBasicBlock *MBB) {
337 assert(isMBB() && "Wrong MachineOperand accessor");
341 //===--------------------------------------------------------------------===//
343 //===--------------------------------------------------------------------===//
345 /// isIdenticalTo - Return true if this operand is identical to the specified
346 /// operand. Note: This method ignores isKill and isDead properties.
347 bool isIdenticalTo(const MachineOperand &Other) const;
349 /// ChangeToImmediate - Replace this operand with a new immediate operand of
350 /// the specified value. If an operand is known to be an immediate already,
351 /// the setImm method should be used.
352 void ChangeToImmediate(int64_t ImmVal);
354 /// ChangeToRegister - Replace this operand with a new register operand of
355 /// the specified value. If an operand is known to be an register already,
356 /// the setReg method should be used.
357 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
358 bool isKill = false, bool isDead = false,
359 bool isUndef = false);
361 //===--------------------------------------------------------------------===//
362 // Construction methods.
363 //===--------------------------------------------------------------------===//
365 static MachineOperand CreateImm(int64_t Val) {
366 MachineOperand Op(MachineOperand::MO_Immediate);
371 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
372 MachineOperand Op(MachineOperand::MO_FPImmediate);
373 Op.Contents.CFP = CFP;
377 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
378 bool isKill = false, bool isDead = false,
379 bool isUndef = false,
380 bool isEarlyClobber = false,
381 unsigned SubReg = 0) {
382 MachineOperand Op(MachineOperand::MO_Register);
387 Op.IsUndef = isUndef;
388 Op.IsEarlyClobber = isEarlyClobber;
389 Op.Contents.Reg.RegNo = Reg;
390 Op.Contents.Reg.Prev = 0;
391 Op.Contents.Reg.Next = 0;
395 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
396 unsigned char TargetFlags = 0) {
397 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
399 Op.setTargetFlags(TargetFlags);
402 static MachineOperand CreateFI(unsigned Idx) {
403 MachineOperand Op(MachineOperand::MO_FrameIndex);
407 static MachineOperand CreateCPI(unsigned Idx, int Offset,
408 unsigned char TargetFlags = 0) {
409 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
411 Op.setOffset(Offset);
412 Op.setTargetFlags(TargetFlags);
415 static MachineOperand CreateJTI(unsigned Idx,
416 unsigned char TargetFlags = 0) {
417 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
419 Op.setTargetFlags(TargetFlags);
422 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
423 unsigned char TargetFlags = 0) {
424 MachineOperand Op(MachineOperand::MO_GlobalAddress);
425 Op.Contents.OffsetedInfo.Val.GV = GV;
426 Op.setOffset(Offset);
427 Op.setTargetFlags(TargetFlags);
430 static MachineOperand CreateES(const char *SymName,
431 unsigned char TargetFlags = 0) {
432 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
433 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
434 Op.setOffset(0); // Offset is always 0.
435 Op.setTargetFlags(TargetFlags);
438 static MachineOperand CreateBA(BlockAddress *BA) {
439 MachineOperand Op(MachineOperand::MO_BlockAddress);
440 Op.Contents.OffsetedInfo.Val.BA = BA;
441 Op.setOffset(0); // Offset is always 0.
445 friend class MachineInstr;
446 friend class MachineRegisterInfo;
448 //===--------------------------------------------------------------------===//
449 // Methods for handling register use/def lists.
450 //===--------------------------------------------------------------------===//
452 /// isOnRegUseList - Return true if this operand is on a register use/def list
453 /// or false if not. This can only be called for register operands that are
454 /// part of a machine instruction.
455 bool isOnRegUseList() const {
456 assert(isReg() && "Can only add reg operand to use lists");
457 return Contents.Reg.Prev != 0;
460 /// AddRegOperandToRegInfo - Add this register operand to the specified
461 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
462 /// explicitly nulled out.
463 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
465 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
466 /// MachineRegisterInfo it is linked with.
467 void RemoveRegOperandFromRegInfo();
470 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
475 } // End llvm namespace