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;
29 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 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 /// ParentMI - This is the instruction that this operand is embedded into.
91 /// This is valid for all operand types, when the operand is in an instr.
92 MachineInstr *ParentMI;
94 /// Contents union - This contains the payload for the various operand types.
96 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
97 const ConstantFP *CFP; // For MO_FPImmediate.
98 int64_t ImmVal; // For MO_Immediate.
100 struct { // For MO_Register.
102 MachineOperand **Prev; // Access list for register.
103 MachineOperand *Next;
106 /// OffsetedInfo - This struct contains the offset and an object identifier.
107 /// this represent the object as with an optional offset from it.
110 int Index; // For MO_*Index - The index itself.
111 const char *SymbolName; // For MO_ExternalSymbol.
112 GlobalValue *GV; // For MO_GlobalAddress.
113 BlockAddress *BA; // For MO_BlockAddress.
114 MDNode *Node; // For MO_Metadata.
116 int64_t Offset; // An offset from the object.
120 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
124 /// getType - Returns the MachineOperandType for this operand.
126 MachineOperandType getType() const { return (MachineOperandType)OpKind; }
128 unsigned char getTargetFlags() const { return TargetFlags; }
129 void setTargetFlags(unsigned char F) { TargetFlags = F; }
130 void addTargetFlag(unsigned char F) { TargetFlags |= F; }
133 /// getParent - Return the instruction that this operand belongs to.
135 MachineInstr *getParent() { return ParentMI; }
136 const MachineInstr *getParent() const { return ParentMI; }
138 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
140 //===--------------------------------------------------------------------===//
141 // Accessors that tell you what kind of MachineOperand you're looking at.
142 //===--------------------------------------------------------------------===//
144 /// isReg - Tests if this is a MO_Register operand.
145 bool isReg() const { return OpKind == MO_Register; }
146 /// isImm - Tests if this is a MO_Immediate operand.
147 bool isImm() const { return OpKind == MO_Immediate; }
148 /// isFPImm - Tests if this is a MO_FPImmediate operand.
149 bool isFPImm() const { return OpKind == MO_FPImmediate; }
150 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
151 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
152 /// isFI - Tests if this is a MO_FrameIndex operand.
153 bool isFI() const { return OpKind == MO_FrameIndex; }
154 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
155 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
156 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
157 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
158 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
159 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
160 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
161 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
162 /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
163 bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
164 /// isMetadata - Tests if this is a MO_Metadata operand.
165 bool isMetadata() const { return OpKind == MO_Metadata; }
167 //===--------------------------------------------------------------------===//
168 // Accessors for Register Operands
169 //===--------------------------------------------------------------------===//
171 /// getReg - Returns the register number.
172 unsigned getReg() const {
173 assert(isReg() && "This is not a register operand!");
174 return Contents.Reg.RegNo;
177 unsigned getSubReg() const {
178 assert(isReg() && "Wrong MachineOperand accessor");
179 return (unsigned)SubReg;
183 assert(isReg() && "Wrong MachineOperand accessor");
188 assert(isReg() && "Wrong MachineOperand accessor");
192 bool isImplicit() const {
193 assert(isReg() && "Wrong MachineOperand accessor");
197 bool isDead() const {
198 assert(isReg() && "Wrong MachineOperand accessor");
202 bool isKill() const {
203 assert(isReg() && "Wrong MachineOperand accessor");
207 bool isUndef() const {
208 assert(isReg() && "Wrong MachineOperand accessor");
212 bool isEarlyClobber() const {
213 assert(isReg() && "Wrong MachineOperand accessor");
214 return IsEarlyClobber;
217 /// getNextOperandForReg - Return the next MachineOperand in the function that
218 /// uses or defines this register.
219 MachineOperand *getNextOperandForReg() const {
220 assert(isReg() && "This is not a register operand!");
221 return Contents.Reg.Next;
224 //===--------------------------------------------------------------------===//
225 // Mutators for Register Operands
226 //===--------------------------------------------------------------------===//
228 /// Change the register this operand corresponds to.
230 void setReg(unsigned Reg);
232 void setSubReg(unsigned subReg) {
233 assert(isReg() && "Wrong MachineOperand accessor");
234 SubReg = (unsigned char)subReg;
237 void setIsUse(bool Val = true) {
238 assert(isReg() && "Wrong MachineOperand accessor");
242 void setIsDef(bool Val = true) {
243 assert(isReg() && "Wrong MachineOperand accessor");
247 void setImplicit(bool Val = true) {
248 assert(isReg() && "Wrong MachineOperand accessor");
252 void setIsKill(bool Val = true) {
253 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
257 void setIsDead(bool Val = true) {
258 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
262 void setIsUndef(bool Val = true) {
263 assert(isReg() && "Wrong MachineOperand accessor");
267 void setIsEarlyClobber(bool Val = true) {
268 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
269 IsEarlyClobber = Val;
272 //===--------------------------------------------------------------------===//
273 // Accessors for various operand types.
274 //===--------------------------------------------------------------------===//
276 int64_t getImm() const {
277 assert(isImm() && "Wrong MachineOperand accessor");
278 return Contents.ImmVal;
281 const ConstantFP *getFPImm() const {
282 assert(isFPImm() && "Wrong MachineOperand accessor");
286 MachineBasicBlock *getMBB() const {
287 assert(isMBB() && "Wrong MachineOperand accessor");
291 int getIndex() const {
292 assert((isFI() || isCPI() || isJTI()) &&
293 "Wrong MachineOperand accessor");
294 return Contents.OffsetedInfo.Val.Index;
297 GlobalValue *getGlobal() const {
298 assert(isGlobal() && "Wrong MachineOperand accessor");
299 return Contents.OffsetedInfo.Val.GV;
302 BlockAddress *getBlockAddress() const {
303 assert(isBlockAddress() && "Wrong MachineOperand accessor");
304 return Contents.OffsetedInfo.Val.BA;
307 MDNode *getMDNode() const {
308 return Contents.OffsetedInfo.Val.Node;
311 /// getOffset - Return the offset from the symbol in this operand. This always
312 /// returns 0 for ExternalSymbol operands.
313 int64_t getOffset() const {
314 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) &&
315 "Wrong MachineOperand accessor");
316 return Contents.OffsetedInfo.Offset;
319 const char *getSymbolName() const {
320 assert(isSymbol() && "Wrong MachineOperand accessor");
321 return Contents.OffsetedInfo.Val.SymbolName;
324 //===--------------------------------------------------------------------===//
325 // Mutators for various operand types.
326 //===--------------------------------------------------------------------===//
328 void setImm(int64_t immVal) {
329 assert(isImm() && "Wrong MachineOperand mutator");
330 Contents.ImmVal = immVal;
333 void setOffset(int64_t Offset) {
334 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress() ||
336 "Wrong MachineOperand accessor");
337 Contents.OffsetedInfo.Offset = Offset;
340 void setIndex(int Idx) {
341 assert((isFI() || isCPI() || isJTI()) &&
342 "Wrong MachineOperand accessor");
343 Contents.OffsetedInfo.Val.Index = Idx;
346 void setMBB(MachineBasicBlock *MBB) {
347 assert(isMBB() && "Wrong MachineOperand accessor");
351 //===--------------------------------------------------------------------===//
353 //===--------------------------------------------------------------------===//
355 /// isIdenticalTo - Return true if this operand is identical to the specified
356 /// operand. Note: This method ignores isKill and isDead properties.
357 bool isIdenticalTo(const MachineOperand &Other) const;
359 /// ChangeToImmediate - Replace this operand with a new immediate operand of
360 /// the specified value. If an operand is known to be an immediate already,
361 /// the setImm method should be used.
362 void ChangeToImmediate(int64_t ImmVal);
364 /// ChangeToRegister - Replace this operand with a new register operand of
365 /// the specified value. If an operand is known to be an register already,
366 /// the setReg method should be used.
367 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
368 bool isKill = false, bool isDead = false,
369 bool isUndef = false);
371 //===--------------------------------------------------------------------===//
372 // Construction methods.
373 //===--------------------------------------------------------------------===//
375 static MachineOperand CreateImm(int64_t Val) {
376 MachineOperand Op(MachineOperand::MO_Immediate);
381 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
382 MachineOperand Op(MachineOperand::MO_FPImmediate);
383 Op.Contents.CFP = CFP;
387 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
388 bool isKill = false, bool isDead = false,
389 bool isUndef = false,
390 bool isEarlyClobber = false,
391 unsigned SubReg = 0) {
392 MachineOperand Op(MachineOperand::MO_Register);
397 Op.IsUndef = isUndef;
398 Op.IsEarlyClobber = isEarlyClobber;
399 Op.Contents.Reg.RegNo = Reg;
400 Op.Contents.Reg.Prev = 0;
401 Op.Contents.Reg.Next = 0;
405 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
406 unsigned char TargetFlags = 0) {
407 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
409 Op.setTargetFlags(TargetFlags);
412 static MachineOperand CreateFI(unsigned Idx) {
413 MachineOperand Op(MachineOperand::MO_FrameIndex);
417 static MachineOperand CreateCPI(unsigned Idx, int Offset,
418 unsigned char TargetFlags = 0) {
419 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
421 Op.setOffset(Offset);
422 Op.setTargetFlags(TargetFlags);
425 static MachineOperand CreateJTI(unsigned Idx,
426 unsigned char TargetFlags = 0) {
427 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
429 Op.setTargetFlags(TargetFlags);
432 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
433 unsigned char TargetFlags = 0) {
434 MachineOperand Op(MachineOperand::MO_GlobalAddress);
435 Op.Contents.OffsetedInfo.Val.GV = GV;
436 Op.setOffset(Offset);
437 Op.setTargetFlags(TargetFlags);
440 static MachineOperand CreateES(const char *SymName,
441 unsigned char TargetFlags = 0) {
442 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
443 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
444 Op.setOffset(0); // Offset is always 0.
445 Op.setTargetFlags(TargetFlags);
448 static MachineOperand CreateBA(BlockAddress *BA) {
449 MachineOperand Op(MachineOperand::MO_BlockAddress);
450 Op.Contents.OffsetedInfo.Val.BA = BA;
451 Op.setOffset(0); // Offset is always 0.
454 static MachineOperand CreateMDNode(MDNode *N, int64_t Offset,
455 unsigned char TargetFlags = 0) {
456 MachineOperand Op(MachineOperand::MO_Metadata);
457 Op.Contents.OffsetedInfo.Val.Node = N;
458 Op.setOffset(Offset);
459 Op.setTargetFlags(TargetFlags);
463 friend class MachineInstr;
464 friend class MachineRegisterInfo;
466 //===--------------------------------------------------------------------===//
467 // Methods for handling register use/def lists.
468 //===--------------------------------------------------------------------===//
470 /// isOnRegUseList - Return true if this operand is on a register use/def list
471 /// or false if not. This can only be called for register operands that are
472 /// part of a machine instruction.
473 bool isOnRegUseList() const {
474 assert(isReg() && "Can only add reg operand to use lists");
475 return Contents.Reg.Prev != 0;
478 /// AddRegOperandToRegInfo - Add this register operand to the specified
479 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
480 /// explicitly nulled out.
481 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
483 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
484 /// MachineRegisterInfo it is linked with.
485 void RemoveRegOperandFromRegInfo();
488 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
493 } // End llvm namespace