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
37 MO_MachineBasicBlock, // MachineBasicBlock reference
38 MO_FrameIndex, // Abstract Stack Frame Index
39 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
40 MO_JumpTableIndex, // Address of indexed Jump Table for switch
41 MO_ExternalSymbol, // Name of external global symbol
42 MO_GlobalAddress // Address of a global value
46 /// OpKind - Specify what kind of operand this is. This discriminates the
48 MachineOperandType OpKind : 8;
50 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
53 /// IsDef - True if this is a def, false if this is a use of the register.
57 /// IsImp - True if this is an implicit def or use, false if it is explicit.
61 /// IsKill - True if this instruction is the last use of the register on this
62 /// path through the function. This is only valid on uses of registers.
65 /// IsDead - True if this register is never used by a subsequent instruction.
66 /// This is only valid on definitions of registers.
69 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
70 /// indicates the MO_Register has no subReg.
73 /// ParentMI - This is the instruction that this operand is embedded into.
74 /// This is valid for all operand types, when the operand is in an instr.
75 MachineInstr *ParentMI;
77 /// Contents union - This contains the payload for the various operand types.
79 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
80 int64_t ImmVal; // For MO_Immediate.
82 struct { // For MO_Register.
84 MachineOperand **Prev; // Access list for register.
88 /// OffsetedInfo - This struct contains the offset and an object identifier.
89 /// this represent the object as with an optional offset from it.
92 int Index; // For MO_*Index - The index itself.
93 const char *SymbolName; // For MO_ExternalSymbol.
94 GlobalValue *GV; // For MO_GlobalAddress.
96 int Offset; // An offset from the object.
100 MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {}
102 MachineOperand(const MachineOperand &M) {
108 /// getType - Returns the MachineOperandType for this operand.
110 MachineOperandType getType() const { return OpKind; }
112 /// getParent - Return the instruction that this operand belongs to.
114 MachineInstr *getParent() { return ParentMI; }
115 const MachineInstr *getParent() const { return ParentMI; }
117 void print(std::ostream &os, const TargetMachine *TM = 0) const;
119 /// Accessors that tell you what kind of MachineOperand you're looking at.
121 bool isRegister() const { return OpKind == MO_Register; }
122 bool isImmediate() const { return OpKind == MO_Immediate; }
123 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; }
124 bool isFrameIndex() const { return OpKind == MO_FrameIndex; }
125 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; }
126 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; }
127 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; }
128 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; }
130 bool isReg() const { return OpKind == MO_Register; }
131 bool isImm() const { return OpKind == MO_Immediate; }
132 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
133 bool isFI() const { return OpKind == MO_FrameIndex; }
134 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
135 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
136 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
137 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
139 //===--------------------------------------------------------------------===//
140 // Accessors for Register Operands
141 //===--------------------------------------------------------------------===//
143 /// getReg - Returns the register number.
144 unsigned getReg() const {
145 assert(isRegister() && "This is not a register operand!");
146 return Contents.Reg.RegNo;
149 unsigned getSubReg() const {
150 assert(isRegister() && "Wrong MachineOperand accessor");
151 return (unsigned)SubReg;
155 assert(isRegister() && "Wrong MachineOperand accessor");
160 assert(isRegister() && "Wrong MachineOperand accessor");
164 bool isImplicit() const {
165 assert(isRegister() && "Wrong MachineOperand accessor");
169 bool isDead() const {
170 assert(isRegister() && "Wrong MachineOperand accessor");
174 bool isKill() const {
175 assert(isRegister() && "Wrong MachineOperand accessor");
179 /// getNextOperandForReg - Return the next MachineOperand in the function that
180 /// uses or defines this register.
181 MachineOperand *getNextOperandForReg() const {
182 assert(isRegister() && "This is not a register operand!");
183 return Contents.Reg.Next;
186 //===--------------------------------------------------------------------===//
187 // Mutators for Register Operands
188 //===--------------------------------------------------------------------===//
190 /// Change the register this operand corresponds to.
192 void setReg(unsigned Reg);
194 void setSubReg(unsigned subReg) {
195 assert(isRegister() && "Wrong MachineOperand accessor");
196 SubReg = (unsigned char)subReg;
199 void setIsUse(bool Val = true) {
200 assert(isRegister() && "Wrong MachineOperand accessor");
204 void setIsDef(bool Val = true) {
205 assert(isRegister() && "Wrong MachineOperand accessor");
209 void setImplicit(bool Val = true) {
210 assert(isRegister() && "Wrong MachineOperand accessor");
214 void setIsKill(bool Val = true) {
215 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
219 void setIsDead(bool Val = true) {
220 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
225 //===--------------------------------------------------------------------===//
226 // Accessors for various operand types.
227 //===--------------------------------------------------------------------===//
229 int64_t getImm() const {
230 assert(isImmediate() && "Wrong MachineOperand accessor");
231 return Contents.ImmVal;
234 MachineBasicBlock *getMBB() const {
235 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
239 int getIndex() const {
240 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
241 "Wrong MachineOperand accessor");
242 return Contents.OffsetedInfo.Val.Index;
245 GlobalValue *getGlobal() const {
246 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
247 return Contents.OffsetedInfo.Val.GV;
250 int getOffset() const {
251 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
252 "Wrong MachineOperand accessor");
253 return Contents.OffsetedInfo.Offset;
256 const char *getSymbolName() const {
257 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
258 return Contents.OffsetedInfo.Val.SymbolName;
261 //===--------------------------------------------------------------------===//
262 // Mutators for various operand types.
263 //===--------------------------------------------------------------------===//
265 void setImm(int64_t immVal) {
266 assert(isImmediate() && "Wrong MachineOperand mutator");
267 Contents.ImmVal = immVal;
270 void setOffset(int Offset) {
271 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
272 "Wrong MachineOperand accessor");
273 Contents.OffsetedInfo.Offset = Offset;
276 void setIndex(int Idx) {
277 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
278 "Wrong MachineOperand accessor");
279 Contents.OffsetedInfo.Val.Index = Idx;
282 void setMBB(MachineBasicBlock *MBB) {
283 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
287 //===--------------------------------------------------------------------===//
289 //===--------------------------------------------------------------------===//
291 /// isIdenticalTo - Return true if this operand is identical to the specified
292 /// operand. Note: This method ignores isKill and isDead properties.
293 bool isIdenticalTo(const MachineOperand &Other) const;
295 /// ChangeToImmediate - Replace this operand with a new immediate operand of
296 /// the specified value. If an operand is known to be an immediate already,
297 /// the setImm method should be used.
298 void ChangeToImmediate(int64_t ImmVal);
300 /// ChangeToRegister - Replace this operand with a new register operand of
301 /// the specified value. If an operand is known to be an register already,
302 /// the setReg method should be used.
303 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
304 bool isKill = false, bool isDead = false);
306 //===--------------------------------------------------------------------===//
307 // Construction methods.
308 //===--------------------------------------------------------------------===//
310 static MachineOperand CreateImm(int64_t Val) {
311 MachineOperand Op(MachineOperand::MO_Immediate);
316 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
317 bool isKill = false, bool isDead = false,
318 unsigned SubReg = 0) {
319 MachineOperand Op(MachineOperand::MO_Register);
324 Op.Contents.Reg.RegNo = Reg;
325 Op.Contents.Reg.Prev = 0;
326 Op.Contents.Reg.Next = 0;
330 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
331 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
335 static MachineOperand CreateFI(unsigned Idx) {
336 MachineOperand Op(MachineOperand::MO_FrameIndex);
340 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
341 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
343 Op.setOffset(Offset);
346 static MachineOperand CreateJTI(unsigned Idx) {
347 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
351 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
352 MachineOperand Op(MachineOperand::MO_GlobalAddress);
353 Op.Contents.OffsetedInfo.Val.GV = GV;
354 Op.setOffset(Offset);
357 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
358 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
359 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
360 Op.setOffset(Offset);
363 const MachineOperand &operator=(const MachineOperand &MO) {
370 ParentMI = MO.ParentMI;
371 Contents = MO.Contents;
375 friend class MachineInstr;
376 friend class MachineRegisterInfo;
378 //===--------------------------------------------------------------------===//
379 // Methods for handling register use/def lists.
380 //===--------------------------------------------------------------------===//
382 /// isOnRegUseList - Return true if this operand is on a register use/def list
383 /// or false if not. This can only be called for register operands that are
384 /// part of a machine instruction.
385 bool isOnRegUseList() const {
386 assert(isReg() && "Can only add reg operand to use lists");
387 return Contents.Reg.Prev != 0;
390 /// AddRegOperandToRegInfo - Add this register operand to the specified
391 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
392 /// explicitly nulled out.
393 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
395 void RemoveRegOperandFromRegInfo() {
396 assert(isOnRegUseList() && "Can only add reg operand to use lists");
397 // Unlink this from the doubly linked list of operands.
398 MachineOperand *NextOp = Contents.Reg.Next;
399 *Contents.Reg.Prev = NextOp;
401 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
402 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
404 Contents.Reg.Prev = 0;
405 Contents.Reg.Next = 0;
409 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
414 } // End llvm namespace