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;
30 /// MachineOperand class - Representation of each machine instruction operand.
32 class MachineOperand {
34 enum MachineOperandType {
35 MO_Register, // Register operand.
36 MO_Immediate, // Immediate Operand
38 MO_MachineBasicBlock, // MachineBasicBlock reference
39 MO_FrameIndex, // Abstract Stack Frame Index
40 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
41 MO_JumpTableIndex, // Address of indexed Jump Table for switch
42 MO_ExternalSymbol, // Name of external global symbol
43 MO_GlobalAddress // Address of a global value
47 /// OpKind - Specify what kind of operand this is. This discriminates the
49 MachineOperandType OpKind : 8;
51 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
54 /// IsDef - True if this is a def, false if this is a use of the register.
58 /// IsImp - True if this is an implicit def or use, false if it is explicit.
62 /// IsKill - True if this instruction is the last use of the register on this
63 /// path through the function. This is only valid on uses of registers.
66 /// IsDead - True if this register is never used by a subsequent instruction.
67 /// This is only valid on definitions of registers.
70 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
71 /// indicates the MO_Register has no subReg.
74 /// ParentMI - This is the instruction that this operand is embedded into.
75 /// This is valid for all operand types, when the operand is in an instr.
76 MachineInstr *ParentMI;
78 /// Contents union - This contains the payload for the various operand types.
80 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
81 ConstantFP *CFP; // For MO_FPImmediate.
82 int64_t ImmVal; // For MO_Immediate.
84 struct { // For MO_Register.
86 MachineOperand **Prev; // Access list for register.
90 /// OffsetedInfo - This struct contains the offset and an object identifier.
91 /// this represent the object as with an optional offset from it.
94 int Index; // For MO_*Index - The index itself.
95 const char *SymbolName; // For MO_ExternalSymbol.
96 GlobalValue *GV; // For MO_GlobalAddress.
98 int Offset; // An offset from the object.
102 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {}
104 MachineOperand(const MachineOperand &M) {
110 /// getType - Returns the MachineOperandType for this operand.
112 MachineOperandType getType() const { return OpKind; }
114 /// getParent - Return the instruction that this operand belongs to.
116 MachineInstr *getParent() { return ParentMI; }
117 const MachineInstr *getParent() const { return ParentMI; }
119 void print(std::ostream &os, const TargetMachine *TM = 0) const;
121 /// Accessors that tell you what kind of MachineOperand you're looking at.
123 bool isRegister() const { return OpKind == MO_Register; }
124 bool isImmediate() const { return OpKind == MO_Immediate; }
125 bool isFPImmediate() const { return OpKind == MO_FPImmediate; }
126 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; }
127 bool isFrameIndex() const { return OpKind == MO_FrameIndex; }
128 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; }
129 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; }
130 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; }
131 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; }
133 bool isReg() const { return OpKind == MO_Register; }
134 bool isImm() const { return OpKind == MO_Immediate; }
135 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
136 bool isFI() const { return OpKind == MO_FrameIndex; }
137 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
138 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
139 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
140 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
142 //===--------------------------------------------------------------------===//
143 // Accessors for Register Operands
144 //===--------------------------------------------------------------------===//
146 /// getReg - Returns the register number.
147 unsigned getReg() const {
148 assert(isRegister() && "This is not a register operand!");
149 return Contents.Reg.RegNo;
152 unsigned getSubReg() const {
153 assert(isRegister() && "Wrong MachineOperand accessor");
154 return (unsigned)SubReg;
158 assert(isRegister() && "Wrong MachineOperand accessor");
163 assert(isRegister() && "Wrong MachineOperand accessor");
167 bool isImplicit() const {
168 assert(isRegister() && "Wrong MachineOperand accessor");
172 bool isDead() const {
173 assert(isRegister() && "Wrong MachineOperand accessor");
177 bool isKill() const {
178 assert(isRegister() && "Wrong MachineOperand accessor");
182 /// getNextOperandForReg - Return the next MachineOperand in the function that
183 /// uses or defines this register.
184 MachineOperand *getNextOperandForReg() const {
185 assert(isRegister() && "This is not a register operand!");
186 return Contents.Reg.Next;
189 //===--------------------------------------------------------------------===//
190 // Mutators for Register Operands
191 //===--------------------------------------------------------------------===//
193 /// Change the register this operand corresponds to.
195 void setReg(unsigned Reg);
197 void setSubReg(unsigned subReg) {
198 assert(isRegister() && "Wrong MachineOperand accessor");
199 SubReg = (unsigned char)subReg;
202 void setIsUse(bool Val = true) {
203 assert(isRegister() && "Wrong MachineOperand accessor");
207 void setIsDef(bool Val = true) {
208 assert(isRegister() && "Wrong MachineOperand accessor");
212 void setImplicit(bool Val = true) {
213 assert(isRegister() && "Wrong MachineOperand accessor");
217 void setIsKill(bool Val = true) {
218 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
222 void setIsDead(bool Val = true) {
223 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
228 //===--------------------------------------------------------------------===//
229 // Accessors for various operand types.
230 //===--------------------------------------------------------------------===//
232 int64_t getImm() const {
233 assert(isImmediate() && "Wrong MachineOperand accessor");
234 return Contents.ImmVal;
237 ConstantFP *getFPImm() const {
238 assert(isFPImmediate() && "Wrong MachineOperand accessor");
242 MachineBasicBlock *getMBB() const {
243 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
247 int getIndex() const {
248 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
249 "Wrong MachineOperand accessor");
250 return Contents.OffsetedInfo.Val.Index;
253 GlobalValue *getGlobal() const {
254 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
255 return Contents.OffsetedInfo.Val.GV;
258 int getOffset() const {
259 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
260 "Wrong MachineOperand accessor");
261 return Contents.OffsetedInfo.Offset;
264 const char *getSymbolName() const {
265 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
266 return Contents.OffsetedInfo.Val.SymbolName;
269 //===--------------------------------------------------------------------===//
270 // Mutators for various operand types.
271 //===--------------------------------------------------------------------===//
273 void setImm(int64_t immVal) {
274 assert(isImmediate() && "Wrong MachineOperand mutator");
275 Contents.ImmVal = immVal;
278 void setOffset(int Offset) {
279 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
280 "Wrong MachineOperand accessor");
281 Contents.OffsetedInfo.Offset = Offset;
284 void setIndex(int Idx) {
285 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
286 "Wrong MachineOperand accessor");
287 Contents.OffsetedInfo.Val.Index = Idx;
290 void setMBB(MachineBasicBlock *MBB) {
291 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
295 //===--------------------------------------------------------------------===//
297 //===--------------------------------------------------------------------===//
299 /// isIdenticalTo - Return true if this operand is identical to the specified
300 /// operand. Note: This method ignores isKill and isDead properties.
301 bool isIdenticalTo(const MachineOperand &Other) const;
303 /// ChangeToImmediate - Replace this operand with a new immediate operand of
304 /// the specified value. If an operand is known to be an immediate already,
305 /// the setImm method should be used.
306 void ChangeToImmediate(int64_t ImmVal);
308 /// ChangeToRegister - Replace this operand with a new register operand of
309 /// the specified value. If an operand is known to be an register already,
310 /// the setReg method should be used.
311 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
312 bool isKill = false, bool isDead = false);
314 //===--------------------------------------------------------------------===//
315 // Construction methods.
316 //===--------------------------------------------------------------------===//
318 static MachineOperand CreateImm(int64_t Val) {
319 MachineOperand Op(MachineOperand::MO_Immediate);
324 static MachineOperand CreateFPImm(ConstantFP *CFP) {
325 MachineOperand Op(MachineOperand::MO_FPImmediate);
326 Op.Contents.CFP = CFP;
330 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
331 bool isKill = false, bool isDead = false,
332 unsigned SubReg = 0) {
333 MachineOperand Op(MachineOperand::MO_Register);
338 Op.Contents.Reg.RegNo = Reg;
339 Op.Contents.Reg.Prev = 0;
340 Op.Contents.Reg.Next = 0;
344 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
345 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
349 static MachineOperand CreateFI(unsigned Idx) {
350 MachineOperand Op(MachineOperand::MO_FrameIndex);
354 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
355 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
357 Op.setOffset(Offset);
360 static MachineOperand CreateJTI(unsigned Idx) {
361 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
365 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
366 MachineOperand Op(MachineOperand::MO_GlobalAddress);
367 Op.Contents.OffsetedInfo.Val.GV = GV;
368 Op.setOffset(Offset);
371 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
372 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
373 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
374 Op.setOffset(Offset);
377 const MachineOperand &operator=(const MachineOperand &MO) {
384 ParentMI = MO.ParentMI;
385 Contents = MO.Contents;
389 friend class MachineInstr;
390 friend class MachineRegisterInfo;
392 //===--------------------------------------------------------------------===//
393 // Methods for handling register use/def lists.
394 //===--------------------------------------------------------------------===//
396 /// isOnRegUseList - Return true if this operand is on a register use/def list
397 /// or false if not. This can only be called for register operands that are
398 /// part of a machine instruction.
399 bool isOnRegUseList() const {
400 assert(isReg() && "Can only add reg operand to use lists");
401 return Contents.Reg.Prev != 0;
404 /// AddRegOperandToRegInfo - Add this register operand to the specified
405 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
406 /// explicitly nulled out.
407 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
409 void RemoveRegOperandFromRegInfo() {
410 assert(isOnRegUseList() && "Reg operand is not on a use list");
411 // Unlink this from the doubly linked list of operands.
412 MachineOperand *NextOp = Contents.Reg.Next;
413 *Contents.Reg.Prev = NextOp;
415 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
416 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
418 Contents.Reg.Prev = 0;
419 Contents.Reg.Next = 0;
423 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
428 } // End llvm namespace