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 //===--------------------------------------------------------------------===//
126 // Accessors for Register Operands
127 //===--------------------------------------------------------------------===//
129 /// getReg - Returns the register number.
130 unsigned getReg() const {
131 assert(isRegister() && "This is not a register operand!");
132 return Contents.RegNo;
135 unsigned getSubReg() const {
136 assert(isRegister() && "Wrong MachineOperand accessor");
137 return (unsigned)SubReg;
141 assert(isRegister() && "Wrong MachineOperand accessor");
146 assert(isRegister() && "Wrong MachineOperand accessor");
150 bool isImplicit() const {
151 assert(isRegister() && "Wrong MachineOperand accessor");
155 bool isDead() const {
156 assert(isRegister() && "Wrong MachineOperand accessor");
160 bool isKill() const {
161 assert(isRegister() && "Wrong MachineOperand accessor");
165 //===--------------------------------------------------------------------===//
166 // Mutators for Register Operands
167 //===--------------------------------------------------------------------===//
169 void setReg(unsigned Reg) {
170 assert(isRegister() && "This is not a register operand!");
171 Contents.RegNo = Reg;
174 void setSubReg(unsigned subReg) {
175 assert(isRegister() && "Wrong MachineOperand accessor");
176 SubReg = (unsigned char)subReg;
179 void setIsUse(bool Val = true) {
180 assert(isRegister() && "Wrong MachineOperand accessor");
184 void setIsDef(bool Val = true) {
185 assert(isRegister() && "Wrong MachineOperand accessor");
189 void setImplicit(bool Val = true) {
190 assert(isRegister() && "Wrong MachineOperand accessor");
194 void setIsKill(bool Val = true) {
195 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
199 void setIsDead(bool Val = true) {
200 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
205 //===--------------------------------------------------------------------===//
206 // Accessors for various operand types.
207 //===--------------------------------------------------------------------===//
209 int64_t getImm() const {
210 assert(isImmediate() && "Wrong MachineOperand accessor");
211 return Contents.ImmVal;
214 MachineBasicBlock *getMBB() const {
215 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
218 MachineBasicBlock *getMachineBasicBlock() const {
219 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
223 int getIndex() const {
224 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
225 "Wrong MachineOperand accessor");
226 return Contents.OffsetedInfo.Val.Index;
229 int getFrameIndex() const { return getIndex(); }
230 unsigned getConstantPoolIndex() const { return getIndex(); }
231 unsigned getJumpTableIndex() const { return getIndex(); }
233 GlobalValue *getGlobal() const {
234 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
235 return Contents.OffsetedInfo.Val.GV;
237 int getOffset() const {
238 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
239 "Wrong MachineOperand accessor");
240 return Contents.OffsetedInfo.Offset;
242 const char *getSymbolName() const {
243 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
244 return Contents.OffsetedInfo.Val.SymbolName;
247 //===--------------------------------------------------------------------===//
248 // Mutators for various operand types.
249 //===--------------------------------------------------------------------===//
251 void setImm(int64_t immVal) {
252 assert(isImmediate() && "Wrong MachineOperand mutator");
253 Contents.ImmVal = immVal;
256 void setOffset(int Offset) {
257 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
258 "Wrong MachineOperand accessor");
259 Contents.OffsetedInfo.Offset = Offset;
262 void setIndex(int Idx) {
263 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
264 "Wrong MachineOperand accessor");
265 Contents.OffsetedInfo.Val.Index = Idx;
268 void setConstantPoolIndex(unsigned Idx) { setIndex(Idx); }
269 void setJumpTableIndex(unsigned Idx) { setIndex(Idx); }
271 void setMachineBasicBlock(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);
330 Op.setMachineBasicBlock(MBB);
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