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;
31 /// MachineOperand class - Representation of each machine instruction operand.
33 class MachineOperand {
35 enum MachineOperandType {
36 MO_Register, // Register operand.
37 MO_Immediate, // Immediate Operand
39 MO_MachineBasicBlock, // MachineBasicBlock reference
40 MO_FrameIndex, // Abstract Stack Frame Index
41 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool
42 MO_JumpTableIndex, // Address of indexed Jump Table for switch
43 MO_ExternalSymbol, // Name of external global symbol
44 MO_GlobalAddress // Address of a global value
48 /// OpKind - Specify what kind of operand this is. This discriminates the
50 MachineOperandType OpKind : 8;
52 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
55 /// IsDef - True if this is a def, false if this is a use of the register.
59 /// IsImp - True if this is an implicit def or use, false if it is explicit.
63 /// IsKill - True if this instruction is the last use of the register on this
64 /// path through the function. This is only valid on uses of registers.
67 /// IsDead - True if this register is never used by a subsequent instruction.
68 /// This is only valid on definitions of registers.
71 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
72 /// indicates the MO_Register has no subReg.
75 /// ParentMI - This is the instruction that this operand is embedded into.
76 /// This is valid for all operand types, when the operand is in an instr.
77 MachineInstr *ParentMI;
79 /// Contents union - This contains the payload for the various operand types.
81 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
82 ConstantFP *CFP; // For MO_FPImmediate.
83 int64_t ImmVal; // For MO_Immediate.
85 struct { // For MO_Register.
87 MachineOperand **Prev; // Access list for register.
91 /// OffsetedInfo - This struct contains the offset and an object identifier.
92 /// this represent the object as with an optional offset from it.
95 int Index; // For MO_*Index - The index itself.
96 const char *SymbolName; // For MO_ExternalSymbol.
97 GlobalValue *GV; // For MO_GlobalAddress.
99 int Offset; // An offset from the object.
103 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {}
105 MachineOperand(const MachineOperand &M) {
111 /// getType - Returns the MachineOperandType for this operand.
113 MachineOperandType getType() const { return OpKind; }
115 /// getParent - Return the instruction that this operand belongs to.
117 MachineInstr *getParent() { return ParentMI; }
118 const MachineInstr *getParent() const { return ParentMI; }
120 void print(std::ostream &os, const TargetMachine *TM = 0) const;
121 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
123 /// Accessors that tell you what kind of MachineOperand you're looking at.
125 bool isRegister() const { return OpKind == MO_Register; }
126 bool isImmediate() const { return OpKind == MO_Immediate; }
127 bool isFPImmediate() const { return OpKind == MO_FPImmediate; }
128 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; }
129 bool isFrameIndex() const { return OpKind == MO_FrameIndex; }
130 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; }
131 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; }
132 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; }
133 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; }
135 bool isReg() const { return OpKind == MO_Register; }
136 bool isImm() const { return OpKind == MO_Immediate; }
137 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
138 bool isFI() const { return OpKind == MO_FrameIndex; }
139 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
140 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
141 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
142 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
144 //===--------------------------------------------------------------------===//
145 // Accessors for Register Operands
146 //===--------------------------------------------------------------------===//
148 /// getReg - Returns the register number.
149 unsigned getReg() const {
150 assert(isRegister() && "This is not a register operand!");
151 return Contents.Reg.RegNo;
154 unsigned getSubReg() const {
155 assert(isRegister() && "Wrong MachineOperand accessor");
156 return (unsigned)SubReg;
160 assert(isRegister() && "Wrong MachineOperand accessor");
165 assert(isRegister() && "Wrong MachineOperand accessor");
169 bool isImplicit() const {
170 assert(isRegister() && "Wrong MachineOperand accessor");
174 bool isDead() const {
175 assert(isRegister() && "Wrong MachineOperand accessor");
179 bool isKill() const {
180 assert(isRegister() && "Wrong MachineOperand accessor");
184 /// getNextOperandForReg - Return the next MachineOperand in the function that
185 /// uses or defines this register.
186 MachineOperand *getNextOperandForReg() const {
187 assert(isRegister() && "This is not a register operand!");
188 return Contents.Reg.Next;
191 //===--------------------------------------------------------------------===//
192 // Mutators for Register Operands
193 //===--------------------------------------------------------------------===//
195 /// Change the register this operand corresponds to.
197 void setReg(unsigned Reg);
199 void setSubReg(unsigned subReg) {
200 assert(isRegister() && "Wrong MachineOperand accessor");
201 SubReg = (unsigned char)subReg;
204 void setIsUse(bool Val = true) {
205 assert(isRegister() && "Wrong MachineOperand accessor");
209 void setIsDef(bool Val = true) {
210 assert(isRegister() && "Wrong MachineOperand accessor");
214 void setImplicit(bool Val = true) {
215 assert(isRegister() && "Wrong MachineOperand accessor");
219 void setIsKill(bool Val = true) {
220 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
224 void setIsDead(bool Val = true) {
225 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
230 //===--------------------------------------------------------------------===//
231 // Accessors for various operand types.
232 //===--------------------------------------------------------------------===//
234 int64_t getImm() const {
235 assert(isImmediate() && "Wrong MachineOperand accessor");
236 return Contents.ImmVal;
239 ConstantFP *getFPImm() const {
240 assert(isFPImmediate() && "Wrong MachineOperand accessor");
244 MachineBasicBlock *getMBB() const {
245 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
249 int getIndex() const {
250 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
251 "Wrong MachineOperand accessor");
252 return Contents.OffsetedInfo.Val.Index;
255 GlobalValue *getGlobal() const {
256 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
257 return Contents.OffsetedInfo.Val.GV;
260 int getOffset() const {
261 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
262 "Wrong MachineOperand accessor");
263 return Contents.OffsetedInfo.Offset;
266 const char *getSymbolName() const {
267 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
268 return Contents.OffsetedInfo.Val.SymbolName;
271 //===--------------------------------------------------------------------===//
272 // Mutators for various operand types.
273 //===--------------------------------------------------------------------===//
275 void setImm(int64_t immVal) {
276 assert(isImmediate() && "Wrong MachineOperand mutator");
277 Contents.ImmVal = immVal;
280 void setOffset(int Offset) {
281 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
282 "Wrong MachineOperand accessor");
283 Contents.OffsetedInfo.Offset = Offset;
286 void setIndex(int Idx) {
287 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
288 "Wrong MachineOperand accessor");
289 Contents.OffsetedInfo.Val.Index = Idx;
292 void setMBB(MachineBasicBlock *MBB) {
293 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
297 //===--------------------------------------------------------------------===//
299 //===--------------------------------------------------------------------===//
301 /// isIdenticalTo - Return true if this operand is identical to the specified
302 /// operand. Note: This method ignores isKill and isDead properties.
303 bool isIdenticalTo(const MachineOperand &Other) const;
305 /// ChangeToImmediate - Replace this operand with a new immediate operand of
306 /// the specified value. If an operand is known to be an immediate already,
307 /// the setImm method should be used.
308 void ChangeToImmediate(int64_t ImmVal);
310 /// ChangeToRegister - Replace this operand with a new register operand of
311 /// the specified value. If an operand is known to be an register already,
312 /// the setReg method should be used.
313 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
314 bool isKill = false, bool isDead = false);
316 //===--------------------------------------------------------------------===//
317 // Construction methods.
318 //===--------------------------------------------------------------------===//
320 static MachineOperand CreateImm(int64_t Val) {
321 MachineOperand Op(MachineOperand::MO_Immediate);
326 static MachineOperand CreateFPImm(ConstantFP *CFP) {
327 MachineOperand Op(MachineOperand::MO_FPImmediate);
328 Op.Contents.CFP = CFP;
332 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
333 bool isKill = false, bool isDead = false,
334 unsigned SubReg = 0) {
335 MachineOperand Op(MachineOperand::MO_Register);
340 Op.Contents.Reg.RegNo = Reg;
341 Op.Contents.Reg.Prev = 0;
342 Op.Contents.Reg.Next = 0;
346 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
347 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
351 static MachineOperand CreateFI(unsigned Idx) {
352 MachineOperand Op(MachineOperand::MO_FrameIndex);
356 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
357 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
359 Op.setOffset(Offset);
362 static MachineOperand CreateJTI(unsigned Idx) {
363 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
367 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
368 MachineOperand Op(MachineOperand::MO_GlobalAddress);
369 Op.Contents.OffsetedInfo.Val.GV = GV;
370 Op.setOffset(Offset);
373 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
374 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
375 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
376 Op.setOffset(Offset);
379 const MachineOperand &operator=(const MachineOperand &MO) {
386 ParentMI = MO.ParentMI;
387 Contents = MO.Contents;
391 friend class MachineInstr;
392 friend class MachineRegisterInfo;
394 //===--------------------------------------------------------------------===//
395 // Methods for handling register use/def lists.
396 //===--------------------------------------------------------------------===//
398 /// isOnRegUseList - Return true if this operand is on a register use/def list
399 /// or false if not. This can only be called for register operands that are
400 /// part of a machine instruction.
401 bool isOnRegUseList() const {
402 assert(isReg() && "Can only add reg operand to use lists");
403 return Contents.Reg.Prev != 0;
406 /// AddRegOperandToRegInfo - Add this register operand to the specified
407 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
408 /// explicitly nulled out.
409 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
411 void RemoveRegOperandFromRegInfo() {
412 assert(isOnRegUseList() && "Reg operand is not on a use list");
413 // Unlink this from the doubly linked list of operands.
414 MachineOperand *NextOp = Contents.Reg.Next;
415 *Contents.Reg.Prev = NextOp;
417 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
418 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
420 Contents.Reg.Prev = 0;
421 Contents.Reg.Next = 0;
425 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
430 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
435 } // End llvm namespace