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;
29 /// MachineOperand class - Representation of each machine instruction operand.
31 class MachineOperand {
33 enum MachineOperandType {
34 MO_Register, // Register operand.
35 MO_Immediate, // Immediate Operand
36 MO_MachineBasicBlock, // MachineBasicBlock reference
37 MO_FrameIndex, // Abstract Stack Frame Index
38 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
39 MO_JumpTableIndex, // Address of indexed Jump Table for switch
40 MO_ExternalSymbol, // Name of external global symbol
41 MO_GlobalAddress // Address of a global value
45 /// OpKind - Specify what kind of operand this is. This discriminates the
47 MachineOperandType OpKind : 8;
49 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
52 /// IsDef - True if this is a def, false if this is a use of the register.
56 /// IsImp - True if this is an implicit def or use, false if it is explicit.
60 /// IsKill - True if this instruction is the last use of the register on this
61 /// path through the function. This is only valid on uses of registers.
64 /// IsDead - True if this register is never used by a subsequent instruction.
65 /// This is only valid on definitions of registers.
68 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
69 /// indicates the MO_Register has no subReg.
72 /// ParentMI - This is the instruction that this operand is embedded into.
73 /// This is valid for all operand types, when the operand is in an instr.
74 MachineInstr *ParentMI;
76 /// Contents union - This contains the payload for the various operand types.
78 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
79 unsigned RegNo; // For MO_Register.
80 int64_t ImmVal; // For MO_Immediate.
82 /// OffsetedInfo - This struct contains the offset and an object identifier.
83 /// this represent the object as with an optional offset from it.
86 int Index; // For MO_*Index - The index itself.
87 const char *SymbolName; // For MO_ExternalSymbol.
88 GlobalValue *GV; // For MO_GlobalAddress.
90 int Offset; // An offset from the object.
94 MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {}
97 MachineOperand(const MachineOperand &M) {
103 /// getType - Returns the MachineOperandType for this operand.
105 MachineOperandType getType() const { return OpKind; }
107 /// getParent - Return the instruction that this operand belongs to.
109 MachineInstr *getParent() { return ParentMI; }
110 const MachineInstr *getParent() const { return ParentMI; }
112 void print(std::ostream &os, const TargetMachine *TM = 0) const;
114 /// Accessors that tell you what kind of MachineOperand you're looking at.
116 bool isRegister() const { return OpKind == MO_Register; }
117 bool isImmediate() const { return OpKind == MO_Immediate; }
118 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; }
119 bool isFrameIndex() const { return OpKind == MO_FrameIndex; }
120 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; }
121 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; }
122 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; }
123 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; }
125 bool isReg() const { return OpKind == MO_Register; }
126 bool isImm() const { return OpKind == MO_Immediate; }
127 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
128 bool isFI() const { return OpKind == MO_FrameIndex; }
129 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
130 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
131 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
132 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
134 //===--------------------------------------------------------------------===//
135 // Accessors for Register Operands
136 //===--------------------------------------------------------------------===//
138 /// getReg - Returns the register number.
139 unsigned getReg() const {
140 assert(isRegister() && "This is not a register operand!");
141 return Contents.RegNo;
144 unsigned getSubReg() const {
145 assert(isRegister() && "Wrong MachineOperand accessor");
146 return (unsigned)SubReg;
150 assert(isRegister() && "Wrong MachineOperand accessor");
155 assert(isRegister() && "Wrong MachineOperand accessor");
159 bool isImplicit() const {
160 assert(isRegister() && "Wrong MachineOperand accessor");
164 bool isDead() const {
165 assert(isRegister() && "Wrong MachineOperand accessor");
169 bool isKill() const {
170 assert(isRegister() && "Wrong MachineOperand accessor");
174 //===--------------------------------------------------------------------===//
175 // Mutators for Register Operands
176 //===--------------------------------------------------------------------===//
178 void setReg(unsigned Reg) {
179 assert(isRegister() && "This is not a register operand!");
180 Contents.RegNo = Reg;
183 void setSubReg(unsigned subReg) {
184 assert(isRegister() && "Wrong MachineOperand accessor");
185 SubReg = (unsigned char)subReg;
188 void setIsUse(bool Val = true) {
189 assert(isRegister() && "Wrong MachineOperand accessor");
193 void setIsDef(bool Val = true) {
194 assert(isRegister() && "Wrong MachineOperand accessor");
198 void setImplicit(bool Val = true) {
199 assert(isRegister() && "Wrong MachineOperand accessor");
203 void setIsKill(bool Val = true) {
204 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
208 void setIsDead(bool Val = true) {
209 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
214 //===--------------------------------------------------------------------===//
215 // Accessors for various operand types.
216 //===--------------------------------------------------------------------===//
218 int64_t getImm() const {
219 assert(isImmediate() && "Wrong MachineOperand accessor");
220 return Contents.ImmVal;
223 MachineBasicBlock *getMBB() const {
224 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
228 int getIndex() const {
229 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
230 "Wrong MachineOperand accessor");
231 return Contents.OffsetedInfo.Val.Index;
234 GlobalValue *getGlobal() const {
235 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
236 return Contents.OffsetedInfo.Val.GV;
239 int getOffset() const {
240 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
241 "Wrong MachineOperand accessor");
242 return Contents.OffsetedInfo.Offset;
245 const char *getSymbolName() const {
246 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
247 return Contents.OffsetedInfo.Val.SymbolName;
250 //===--------------------------------------------------------------------===//
251 // Mutators for various operand types.
252 //===--------------------------------------------------------------------===//
254 void setImm(int64_t immVal) {
255 assert(isImmediate() && "Wrong MachineOperand mutator");
256 Contents.ImmVal = immVal;
259 void setOffset(int Offset) {
260 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
261 "Wrong MachineOperand accessor");
262 Contents.OffsetedInfo.Offset = Offset;
265 void setIndex(int Idx) {
266 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
267 "Wrong MachineOperand accessor");
268 Contents.OffsetedInfo.Val.Index = Idx;
271 void setMBB(MachineBasicBlock *MBB) {
272 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
276 //===--------------------------------------------------------------------===//
278 //===--------------------------------------------------------------------===//
280 /// isIdenticalTo - Return true if this operand is identical to the specified
281 /// operand. Note: This method ignores isKill and isDead properties.
282 bool isIdenticalTo(const MachineOperand &Other) const;
284 /// ChangeToImmediate - Replace this operand with a new immediate operand of
285 /// the specified value. If an operand is known to be an immediate already,
286 /// the setImm method should be used.
287 void ChangeToImmediate(int64_t ImmVal) {
288 OpKind = MO_Immediate;
289 Contents.ImmVal = ImmVal;
292 /// ChangeToRegister - Replace this operand with a new register operand of
293 /// the specified value. If an operand is known to be an register already,
294 /// the setReg method should be used.
295 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
296 bool isKill = false, bool isDead = false) {
297 OpKind = MO_Register;
298 Contents.RegNo = Reg;
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.RegNo = Reg;
328 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
329 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
333 static MachineOperand CreateFI(unsigned Idx) {
334 MachineOperand Op(MachineOperand::MO_FrameIndex);
338 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
339 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
341 Op.setOffset(Offset);
344 static MachineOperand CreateJTI(unsigned Idx) {
345 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
349 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
350 MachineOperand Op(MachineOperand::MO_GlobalAddress);
351 Op.Contents.OffsetedInfo.Val.GV = GV;
352 Op.setOffset(Offset);
355 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
356 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
357 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
358 Op.setOffset(Offset);
361 const MachineOperand &operator=(const MachineOperand &MO) {
368 ParentMI = MO.ParentMI;
369 Contents = MO.Contents;
373 friend class MachineInstr;
376 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
381 } // End llvm namespace