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 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
79 /// by the MachineInstr before all input registers are read. This is used to
80 /// model the GCC inline asm '&' constraint modifier.
81 bool IsEarlyClobber : 1;
83 /// ParentMI - This is the instruction that this operand is embedded into.
84 /// This is valid for all operand types, when the operand is in an instr.
85 MachineInstr *ParentMI;
87 /// Contents union - This contains the payload for the various operand types.
89 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
90 const ConstantFP *CFP; // For MO_FPImmediate.
91 int64_t ImmVal; // For MO_Immediate.
93 struct { // For MO_Register.
95 MachineOperand **Prev; // Access list for register.
99 /// OffsetedInfo - This struct contains the offset and an object identifier.
100 /// this represent the object as with an optional offset from it.
103 int Index; // For MO_*Index - The index itself.
104 const char *SymbolName; // For MO_ExternalSymbol.
105 GlobalValue *GV; // For MO_GlobalAddress.
107 int64_t Offset; // An offset from the object.
111 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
115 MachineOperand(const MachineOperand &M) {
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(std::ostream &os, const TargetMachine *TM = 0) const;
136 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
138 //===--------------------------------------------------------------------===//
139 // Accessors that tell you what kind of MachineOperand you're looking at.
140 //===--------------------------------------------------------------------===//
142 /// isReg - Tests if this is a MO_Register operand.
143 bool isReg() const { return OpKind == MO_Register; }
144 /// isImm - Tests if this is a MO_Immediate operand.
145 bool isImm() const { return OpKind == MO_Immediate; }
146 /// isFPImm - Tests if this is a MO_FPImmediate operand.
147 bool isFPImm() const { return OpKind == MO_FPImmediate; }
148 /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
149 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
150 /// isFI - Tests if this is a MO_FrameIndex operand.
151 bool isFI() const { return OpKind == MO_FrameIndex; }
152 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
153 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
154 /// isJTI - Tests if this is a MO_JumpTableIndex operand.
155 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
156 /// isGlobal - Tests if this is a MO_GlobalAddress operand.
157 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
158 /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
159 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
161 //===--------------------------------------------------------------------===//
162 // Accessors for Register Operands
163 //===--------------------------------------------------------------------===//
165 /// getReg - Returns the register number.
166 unsigned getReg() const {
167 assert(isReg() && "This is not a register operand!");
168 return Contents.Reg.RegNo;
171 unsigned getSubReg() const {
172 assert(isReg() && "Wrong MachineOperand accessor");
173 return (unsigned)SubReg;
177 assert(isReg() && "Wrong MachineOperand accessor");
182 assert(isReg() && "Wrong MachineOperand accessor");
186 bool isImplicit() const {
187 assert(isReg() && "Wrong MachineOperand accessor");
191 bool isDead() const {
192 assert(isReg() && "Wrong MachineOperand accessor");
196 bool isKill() const {
197 assert(isReg() && "Wrong MachineOperand accessor");
201 bool isEarlyClobber() const {
202 assert(isReg() && "Wrong MachineOperand accessor");
203 return IsEarlyClobber;
206 /// getNextOperandForReg - Return the next MachineOperand in the function that
207 /// uses or defines this register.
208 MachineOperand *getNextOperandForReg() const {
209 assert(isReg() && "This is not a register operand!");
210 return Contents.Reg.Next;
213 //===--------------------------------------------------------------------===//
214 // Mutators for Register Operands
215 //===--------------------------------------------------------------------===//
217 /// Change the register this operand corresponds to.
219 void setReg(unsigned Reg);
221 void setSubReg(unsigned subReg) {
222 assert(isReg() && "Wrong MachineOperand accessor");
223 SubReg = (unsigned char)subReg;
226 void setIsUse(bool Val = true) {
227 assert(isReg() && "Wrong MachineOperand accessor");
231 void setIsDef(bool Val = true) {
232 assert(isReg() && "Wrong MachineOperand accessor");
236 void setImplicit(bool Val = true) {
237 assert(isReg() && "Wrong MachineOperand accessor");
241 void setIsKill(bool Val = true) {
242 assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
246 void setIsDead(bool Val = true) {
247 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
251 void setIsEarlyClobber(bool Val = true) {
252 assert(isReg() && IsDef && "Wrong MachineOperand accessor");
253 IsEarlyClobber = Val;
256 //===--------------------------------------------------------------------===//
257 // Accessors for various operand types.
258 //===--------------------------------------------------------------------===//
260 int64_t getImm() const {
261 assert(isImm() && "Wrong MachineOperand accessor");
262 return Contents.ImmVal;
265 const ConstantFP *getFPImm() const {
266 assert(isFPImm() && "Wrong MachineOperand accessor");
270 MachineBasicBlock *getMBB() const {
271 assert(isMBB() && "Wrong MachineOperand accessor");
275 int getIndex() const {
276 assert((isFI() || isCPI() || isJTI()) &&
277 "Wrong MachineOperand accessor");
278 return Contents.OffsetedInfo.Val.Index;
281 GlobalValue *getGlobal() const {
282 assert(isGlobal() && "Wrong MachineOperand accessor");
283 return Contents.OffsetedInfo.Val.GV;
286 int64_t getOffset() const {
287 assert((isGlobal() || isSymbol() || isCPI()) &&
288 "Wrong MachineOperand accessor");
289 return Contents.OffsetedInfo.Offset;
292 const char *getSymbolName() const {
293 assert(isSymbol() && "Wrong MachineOperand accessor");
294 return Contents.OffsetedInfo.Val.SymbolName;
297 //===--------------------------------------------------------------------===//
298 // Mutators for various operand types.
299 //===--------------------------------------------------------------------===//
301 void setImm(int64_t immVal) {
302 assert(isImm() && "Wrong MachineOperand mutator");
303 Contents.ImmVal = immVal;
306 void setOffset(int64_t Offset) {
307 assert((isGlobal() || isSymbol() || isCPI()) &&
308 "Wrong MachineOperand accessor");
309 Contents.OffsetedInfo.Offset = Offset;
312 void setIndex(int Idx) {
313 assert((isFI() || isCPI() || isJTI()) &&
314 "Wrong MachineOperand accessor");
315 Contents.OffsetedInfo.Val.Index = Idx;
318 void setMBB(MachineBasicBlock *MBB) {
319 assert(isMBB() && "Wrong MachineOperand accessor");
323 //===--------------------------------------------------------------------===//
325 //===--------------------------------------------------------------------===//
327 /// isIdenticalTo - Return true if this operand is identical to the specified
328 /// operand. Note: This method ignores isKill and isDead properties.
329 bool isIdenticalTo(const MachineOperand &Other) const;
331 /// ChangeToImmediate - Replace this operand with a new immediate operand of
332 /// the specified value. If an operand is known to be an immediate already,
333 /// the setImm method should be used.
334 void ChangeToImmediate(int64_t ImmVal);
336 /// ChangeToRegister - Replace this operand with a new register operand of
337 /// the specified value. If an operand is known to be an register already,
338 /// the setReg method should be used.
339 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
340 bool isKill = false, bool isDead = false);
342 //===--------------------------------------------------------------------===//
343 // Construction methods.
344 //===--------------------------------------------------------------------===//
346 static MachineOperand CreateImm(int64_t Val) {
347 MachineOperand Op(MachineOperand::MO_Immediate);
352 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
353 MachineOperand Op(MachineOperand::MO_FPImmediate);
354 Op.Contents.CFP = CFP;
358 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
359 bool isKill = false, bool isDead = false,
361 bool isEarlyClobber = false) {
362 MachineOperand Op(MachineOperand::MO_Register);
367 Op.IsEarlyClobber = isEarlyClobber;
368 Op.Contents.Reg.RegNo = Reg;
369 Op.Contents.Reg.Prev = 0;
370 Op.Contents.Reg.Next = 0;
374 static MachineOperand CreateMBB(MachineBasicBlock *MBB,
375 unsigned char TargetFlags = 0) {
376 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
378 Op.setTargetFlags(TargetFlags);
381 static MachineOperand CreateFI(unsigned Idx) {
382 MachineOperand Op(MachineOperand::MO_FrameIndex);
386 static MachineOperand CreateCPI(unsigned Idx, int Offset,
387 unsigned char TargetFlags = 0) {
388 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
390 Op.setOffset(Offset);
391 Op.setTargetFlags(TargetFlags);
394 static MachineOperand CreateJTI(unsigned Idx,
395 unsigned char TargetFlags = 0) {
396 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
398 Op.setTargetFlags(TargetFlags);
401 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
402 unsigned char TargetFlags = 0) {
403 MachineOperand Op(MachineOperand::MO_GlobalAddress);
404 Op.Contents.OffsetedInfo.Val.GV = GV;
405 Op.setOffset(Offset);
406 Op.setTargetFlags(TargetFlags);
409 static MachineOperand CreateES(const char *SymName, int64_t Offset = 0,
410 unsigned char TargetFlags = 0) {
411 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
412 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
413 Op.setOffset(Offset);
414 Op.setTargetFlags(TargetFlags);
417 const MachineOperand &operator=(const MachineOperand &MO) {
423 IsEarlyClobber = MO.IsEarlyClobber;
425 ParentMI = MO.ParentMI;
426 Contents = MO.Contents;
427 TargetFlags = MO.TargetFlags;
431 friend class MachineInstr;
432 friend class MachineRegisterInfo;
434 //===--------------------------------------------------------------------===//
435 // Methods for handling register use/def lists.
436 //===--------------------------------------------------------------------===//
438 /// isOnRegUseList - Return true if this operand is on a register use/def list
439 /// or false if not. This can only be called for register operands that are
440 /// part of a machine instruction.
441 bool isOnRegUseList() const {
442 assert(isReg() && "Can only add reg operand to use lists");
443 return Contents.Reg.Prev != 0;
446 /// AddRegOperandToRegInfo - Add this register operand to the specified
447 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
448 /// explicitly nulled out.
449 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
451 /// RemoveRegOperandFromRegInfo - Remove this register operand from the
452 /// MachineRegisterInfo it is linked with.
453 void RemoveRegOperandFromRegInfo();
456 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
461 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
466 } // End llvm namespace