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 //===--------------------------------------------------------------------===//
180 // Mutators for Register Operands
181 //===--------------------------------------------------------------------===//
183 /// Change the register this operand corresponds to.
185 void setReg(unsigned Reg);
187 void setSubReg(unsigned subReg) {
188 assert(isRegister() && "Wrong MachineOperand accessor");
189 SubReg = (unsigned char)subReg;
192 void setIsUse(bool Val = true) {
193 assert(isRegister() && "Wrong MachineOperand accessor");
197 void setIsDef(bool Val = true) {
198 assert(isRegister() && "Wrong MachineOperand accessor");
202 void setImplicit(bool Val = true) {
203 assert(isRegister() && "Wrong MachineOperand accessor");
207 void setIsKill(bool Val = true) {
208 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
212 void setIsDead(bool Val = true) {
213 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
218 //===--------------------------------------------------------------------===//
219 // Accessors for various operand types.
220 //===--------------------------------------------------------------------===//
222 int64_t getImm() const {
223 assert(isImmediate() && "Wrong MachineOperand accessor");
224 return Contents.ImmVal;
227 MachineBasicBlock *getMBB() const {
228 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
232 int getIndex() const {
233 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
234 "Wrong MachineOperand accessor");
235 return Contents.OffsetedInfo.Val.Index;
238 GlobalValue *getGlobal() const {
239 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
240 return Contents.OffsetedInfo.Val.GV;
243 int getOffset() const {
244 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
245 "Wrong MachineOperand accessor");
246 return Contents.OffsetedInfo.Offset;
249 const char *getSymbolName() const {
250 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
251 return Contents.OffsetedInfo.Val.SymbolName;
254 //===--------------------------------------------------------------------===//
255 // Mutators for various operand types.
256 //===--------------------------------------------------------------------===//
258 void setImm(int64_t immVal) {
259 assert(isImmediate() && "Wrong MachineOperand mutator");
260 Contents.ImmVal = immVal;
263 void setOffset(int Offset) {
264 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
265 "Wrong MachineOperand accessor");
266 Contents.OffsetedInfo.Offset = Offset;
269 void setIndex(int Idx) {
270 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
271 "Wrong MachineOperand accessor");
272 Contents.OffsetedInfo.Val.Index = Idx;
275 void setMBB(MachineBasicBlock *MBB) {
276 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
280 //===--------------------------------------------------------------------===//
282 //===--------------------------------------------------------------------===//
284 /// isIdenticalTo - Return true if this operand is identical to the specified
285 /// operand. Note: This method ignores isKill and isDead properties.
286 bool isIdenticalTo(const MachineOperand &Other) const;
288 /// ChangeToImmediate - Replace this operand with a new immediate operand of
289 /// the specified value. If an operand is known to be an immediate already,
290 /// the setImm method should be used.
291 void ChangeToImmediate(int64_t ImmVal);
293 /// ChangeToRegister - Replace this operand with a new register operand of
294 /// the specified value. If an operand is known to be an register already,
295 /// the setReg method should be used.
296 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
297 bool isKill = false, bool isDead = false);
299 //===--------------------------------------------------------------------===//
300 // Construction methods.
301 //===--------------------------------------------------------------------===//
303 static MachineOperand CreateImm(int64_t Val) {
304 MachineOperand Op(MachineOperand::MO_Immediate);
309 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
310 bool isKill = false, bool isDead = false,
311 unsigned SubReg = 0) {
312 MachineOperand Op(MachineOperand::MO_Register);
317 Op.Contents.Reg.RegNo = Reg;
318 Op.Contents.Reg.Prev = 0;
319 Op.Contents.Reg.Next = 0;
323 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
324 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
328 static MachineOperand CreateFI(unsigned Idx) {
329 MachineOperand Op(MachineOperand::MO_FrameIndex);
333 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
334 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
336 Op.setOffset(Offset);
339 static MachineOperand CreateJTI(unsigned Idx) {
340 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
344 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
345 MachineOperand Op(MachineOperand::MO_GlobalAddress);
346 Op.Contents.OffsetedInfo.Val.GV = GV;
347 Op.setOffset(Offset);
350 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
351 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
352 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
353 Op.setOffset(Offset);
356 const MachineOperand &operator=(const MachineOperand &MO) {
363 ParentMI = MO.ParentMI;
364 Contents = MO.Contents;
368 friend class MachineInstr;
369 friend class MachineRegisterInfo;
371 //===--------------------------------------------------------------------===//
372 // Methods for handling register use/def lists.
373 //===--------------------------------------------------------------------===//
375 /// isOnRegUseList - Return true if this operand is on a register use/def list
376 /// or false if not. This can only be called for register operands that are
377 /// part of a machine instruction.
378 bool isOnRegUseList() const {
379 assert(isReg() && "Can only add reg operand to use lists");
380 return Contents.Reg.Prev != 0;
383 /// AddRegOperandToRegInfo - Add this register operand to the specified
384 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
385 /// explicitly nulled out.
386 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
388 void RemoveRegOperandFromRegInfo() {
389 assert(isOnRegUseList() && "Can only add reg operand to use lists");
390 // Unlink this from the doubly linked list of operands.
391 MachineOperand *NextOp = Contents.Reg.Next;
392 *Contents.Reg.Prev = NextOp;
394 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
395 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
397 Contents.Reg.Prev = 0;
398 Contents.Reg.Next = 0;
402 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
407 } // End llvm namespace