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 /// MachineOperand class - Representation of each machine instruction operand.
30 class MachineOperand {
32 enum MachineOperandType {
33 MO_Register, // Register operand.
34 MO_Immediate, // Immediate Operand
35 MO_MachineBasicBlock, // MachineBasicBlock reference
36 MO_FrameIndex, // Abstract Stack Frame Index
37 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
38 MO_JumpTableIndex, // Address of indexed Jump Table for switch
39 MO_ExternalSymbol, // Name of external global symbol
40 MO_GlobalAddress // Address of a global value
45 GlobalValue *GV; // For MO_GlobalAddress.
46 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
47 const char *SymbolName; // For MO_ExternalSymbol.
48 unsigned RegNo; // For MO_Register.
49 int64_t immedVal; // For MO_Immediate and MO_*Index.
52 /// ParentMI - This is the instruction that this operand is embedded into.
53 MachineInstr *ParentMI;
55 MachineOperandType opType:8; // Discriminate the union.
56 bool IsDef : 1; // True if this is a def, false if this is a use.
57 bool IsImp : 1; // True if this is an implicit def or use.
59 bool IsKill : 1; // True if this is a reg use and the reg is dead
60 // immediately after the read.
61 bool IsDead : 1; // True if this is a reg def and the reg is dead
62 // immediately after the write. i.e. A register
63 // that is defined but never used.
65 /// auxInfo - auxiliary information used by the MachineOperand
67 /// offset - Offset to address of global or external, only valid for
68 /// MO_GlobalAddress, MO_ExternalSym and MO_ConstantPoolIndex
71 /// subReg - SubRegister number, only valid for MO_Register. A value of 0
72 /// indicates the MO_Register has no subReg.
76 MachineOperand() : ParentMI(0) {}
78 void print(std::ostream &os) const;
79 void print(std::ostream *os) const { if (os) print(*os); }
82 MachineOperand(const MachineOperand &M) {
88 /// getType - Returns the MachineOperandType for this operand.
90 MachineOperandType getType() const { return opType; }
92 /// Accessors that tell you what kind of MachineOperand you're looking at.
94 bool isRegister() const { return opType == MO_Register; }
95 bool isImmediate() const { return opType == MO_Immediate; }
96 bool isMachineBasicBlock() const { return opType == MO_MachineBasicBlock; }
97 bool isFrameIndex() const { return opType == MO_FrameIndex; }
98 bool isConstantPoolIndex() const { return opType == MO_ConstantPoolIndex; }
99 bool isJumpTableIndex() const { return opType == MO_JumpTableIndex; }
100 bool isGlobalAddress() const { return opType == MO_GlobalAddress; }
101 bool isExternalSymbol() const { return opType == MO_ExternalSymbol; }
103 int64_t getImm() const {
104 assert(isImmediate() && "Wrong MachineOperand accessor");
105 return contents.immedVal;
108 int64_t getImmedValue() const {
109 assert(isImmediate() && "Wrong MachineOperand accessor");
110 return contents.immedVal;
112 MachineBasicBlock *getMBB() const {
113 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
116 MachineBasicBlock *getMachineBasicBlock() const {
117 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
120 void setMachineBasicBlock(MachineBasicBlock *MBB) {
121 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
124 int getFrameIndex() const {
125 assert(isFrameIndex() && "Wrong MachineOperand accessor");
126 return (int)contents.immedVal;
128 unsigned getConstantPoolIndex() const {
129 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
130 return (unsigned)contents.immedVal;
132 unsigned getJumpTableIndex() const {
133 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
134 return (unsigned)contents.immedVal;
136 GlobalValue *getGlobal() const {
137 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
140 int getOffset() const {
141 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
142 "Wrong MachineOperand accessor");
143 return auxInfo.offset;
145 unsigned getSubReg() const {
146 assert(isRegister() && "Wrong MachineOperand accessor");
147 return (unsigned)auxInfo.subReg;
149 const char *getSymbolName() const {
150 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
151 return contents.SymbolName;
155 assert(isRegister() && "Wrong MachineOperand accessor");
159 assert(isRegister() && "Wrong MachineOperand accessor");
163 assert(isRegister() && "Wrong MachineOperand accessor");
167 assert(isRegister() && "Wrong MachineOperand accessor");
171 bool isImplicit() const {
172 assert(isRegister() && "Wrong MachineOperand accessor");
176 assert(isRegister() && "Wrong MachineOperand accessor");
180 bool isKill() const {
181 assert(isRegister() && "Wrong MachineOperand accessor");
184 bool isDead() const {
185 assert(isRegister() && "Wrong MachineOperand accessor");
189 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
193 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
197 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
201 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
205 /// getReg - Returns the register number.
207 unsigned getReg() const {
208 assert(isRegister() && "This is not a register operand!");
209 return contents.RegNo;
212 /// MachineOperand mutators.
214 void setReg(unsigned Reg) {
215 assert(isRegister() && "This is not a register operand!");
216 contents.RegNo = Reg;
219 void setImmedValue(int64_t immVal) {
220 assert(isImmediate() && "Wrong MachineOperand mutator");
221 contents.immedVal = immVal;
223 void setImm(int64_t immVal) {
224 assert(isImmediate() && "Wrong MachineOperand mutator");
225 contents.immedVal = immVal;
228 void setOffset(int Offset) {
229 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex() ||
230 isJumpTableIndex()) &&
231 "Wrong MachineOperand accessor");
232 auxInfo.offset = Offset;
234 void setSubReg(unsigned subReg) {
235 assert(isRegister() && "Wrong MachineOperand accessor");
236 auxInfo.subReg = (unsigned char)subReg;
238 void setConstantPoolIndex(unsigned Idx) {
239 assert(isConstantPoolIndex() && "Wrong MachineOperand accessor");
240 contents.immedVal = Idx;
242 void setJumpTableIndex(unsigned Idx) {
243 assert(isJumpTableIndex() && "Wrong MachineOperand accessor");
244 contents.immedVal = Idx;
247 /// isIdenticalTo - Return true if this operand is identical to the specified
248 /// operand. Note: This method ignores isKill and isDead properties.
249 bool isIdenticalTo(const MachineOperand &Other) const;
251 /// ChangeToImmediate - Replace this operand with a new immediate operand of
252 /// the specified value. If an operand is known to be an immediate already,
253 /// the setImmedValue method should be used.
254 void ChangeToImmediate(int64_t ImmVal) {
255 opType = MO_Immediate;
256 contents.immedVal = ImmVal;
259 /// ChangeToRegister - Replace this operand with a new register operand of
260 /// the specified value. If an operand is known to be an register already,
261 /// the setReg method should be used.
262 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
263 bool isKill = false, bool isDead = false) {
264 opType = MO_Register;
265 contents.RegNo = Reg;
272 static MachineOperand CreateImm(int64_t Val) {
274 Op.opType = MachineOperand::MO_Immediate;
275 Op.contents.immedVal = Val;
276 Op.auxInfo.offset = 0;
279 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
280 bool isKill = false, bool isDead = false,
281 unsigned SubReg = 0) {
283 Op.opType = MachineOperand::MO_Register;
288 Op.contents.RegNo = Reg;
289 Op.auxInfo.subReg = SubReg;
292 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
294 Op.opType = MachineOperand::MO_MachineBasicBlock;
295 Op.contents.MBB = MBB;
296 Op.auxInfo.offset = 0;
299 static MachineOperand CreateFI(unsigned Idx) {
301 Op.opType = MachineOperand::MO_FrameIndex;
302 Op.contents.immedVal = Idx;
303 Op.auxInfo.offset = 0;
306 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
308 Op.opType = MachineOperand::MO_ConstantPoolIndex;
309 Op.contents.immedVal = Idx;
310 Op.auxInfo.offset = Offset;
313 static MachineOperand CreateJTI(unsigned Idx) {
315 Op.opType = MachineOperand::MO_JumpTableIndex;
316 Op.contents.immedVal = Idx;
317 Op.auxInfo.offset = 0;
320 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
322 Op.opType = MachineOperand::MO_GlobalAddress;
324 Op.auxInfo.offset = Offset;
327 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
329 Op.opType = MachineOperand::MO_ExternalSymbol;
330 Op.contents.SymbolName = SymName;
331 Op.auxInfo.offset = Offset;
334 const MachineOperand &operator=(const MachineOperand &MO) {
335 contents = MO.contents;
341 auxInfo = MO.auxInfo;
342 ParentMI = MO.ParentMI;
346 friend std::ostream& operator<<(std::ostream& os, const MachineOperand& mop) {
351 friend class MachineInstr;
354 std::ostream& operator<<(std::ostream &OS, const MachineOperand &MO);
356 } // End llvm namespace