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 /// IsEarlyClobber flag - this is only valid for MO_Register operands in
74 /// IsEarlyClobber - True if this operand is marked earlyclobber in an
75 /// inline asm. See gcc doc for description of earlyclobber.
76 bool IsEarlyClobber : 1;
78 /// SubReg - Subregister number, only valid for MO_Register. A value of 0
79 /// indicates the MO_Register has no subReg.
82 /// ParentMI - This is the instruction that this operand is embedded into.
83 /// This is valid for all operand types, when the operand is in an instr.
84 MachineInstr *ParentMI;
86 /// Contents union - This contains the payload for the various operand types.
88 MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
89 const ConstantFP *CFP; // For MO_FPImmediate.
90 int64_t ImmVal; // For MO_Immediate.
92 struct { // For MO_Register.
94 MachineOperand **Prev; // Access list for register.
98 /// OffsetedInfo - This struct contains the offset and an object identifier.
99 /// this represent the object as with an optional offset from it.
102 int Index; // For MO_*Index - The index itself.
103 const char *SymbolName; // For MO_ExternalSymbol.
104 GlobalValue *GV; // For MO_GlobalAddress.
106 int Offset; // An offset from the object.
110 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {}
112 MachineOperand(const MachineOperand &M) {
118 /// getType - Returns the MachineOperandType for this operand.
120 MachineOperandType getType() const { return OpKind; }
122 /// getParent - Return the instruction that this operand belongs to.
124 MachineInstr *getParent() { return ParentMI; }
125 const MachineInstr *getParent() const { return ParentMI; }
127 void print(std::ostream &os, const TargetMachine *TM = 0) const;
128 void print(raw_ostream &os, const TargetMachine *TM = 0) const;
130 /// Accessors that tell you what kind of MachineOperand you're looking at.
132 bool isRegister() const { return OpKind == MO_Register; }
133 bool isImmediate() const { return OpKind == MO_Immediate; }
134 bool isFPImmediate() const { return OpKind == MO_FPImmediate; }
135 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; }
136 bool isFrameIndex() const { return OpKind == MO_FrameIndex; }
137 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; }
138 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; }
139 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; }
140 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; }
142 bool isReg() const { return OpKind == MO_Register; }
143 bool isImm() const { return OpKind == MO_Immediate; }
144 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
145 bool isFI() const { return OpKind == MO_FrameIndex; }
146 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
147 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
148 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
149 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
151 //===--------------------------------------------------------------------===//
152 // Accessors for Register Operands
153 //===--------------------------------------------------------------------===//
155 /// getReg - Returns the register number.
156 unsigned getReg() const {
157 assert(isRegister() && "This is not a register operand!");
158 return Contents.Reg.RegNo;
161 unsigned getSubReg() const {
162 assert(isRegister() && "Wrong MachineOperand accessor");
163 return (unsigned)SubReg;
167 assert(isRegister() && "Wrong MachineOperand accessor");
172 assert(isRegister() && "Wrong MachineOperand accessor");
176 bool isImplicit() const {
177 assert(isRegister() && "Wrong MachineOperand accessor");
181 bool isDead() const {
182 assert(isRegister() && "Wrong MachineOperand accessor");
186 bool isKill() const {
187 assert(isRegister() && "Wrong MachineOperand accessor");
191 bool isEarlyClobber() const {
192 assert(isRegister() && "Wrong MachineOperand accessor");
193 return IsEarlyClobber;
196 /// getNextOperandForReg - Return the next MachineOperand in the function that
197 /// uses or defines this register.
198 MachineOperand *getNextOperandForReg() const {
199 assert(isRegister() && "This is not a register operand!");
200 return Contents.Reg.Next;
203 //===--------------------------------------------------------------------===//
204 // Mutators for Register Operands
205 //===--------------------------------------------------------------------===//
207 /// Change the register this operand corresponds to.
209 void setReg(unsigned Reg);
211 void setSubReg(unsigned subReg) {
212 assert(isRegister() && "Wrong MachineOperand accessor");
213 SubReg = (unsigned char)subReg;
216 void setIsUse(bool Val = true) {
217 assert(isRegister() && "Wrong MachineOperand accessor");
221 void setIsDef(bool Val = true) {
222 assert(isRegister() && "Wrong MachineOperand accessor");
226 void setImplicit(bool Val = true) {
227 assert(isRegister() && "Wrong MachineOperand accessor");
231 void setIsKill(bool Val = true) {
232 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
236 void setIsDead(bool Val = true) {
237 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
241 void setIsEarlyClobber(bool Val = true) {
242 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
243 IsEarlyClobber = Val;
246 //===--------------------------------------------------------------------===//
247 // Accessors for various operand types.
248 //===--------------------------------------------------------------------===//
250 int64_t getImm() const {
251 assert(isImmediate() && "Wrong MachineOperand accessor");
252 return Contents.ImmVal;
255 const ConstantFP *getFPImm() const {
256 assert(isFPImmediate() && "Wrong MachineOperand accessor");
260 MachineBasicBlock *getMBB() const {
261 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
265 int getIndex() const {
266 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
267 "Wrong MachineOperand accessor");
268 return Contents.OffsetedInfo.Val.Index;
271 GlobalValue *getGlobal() const {
272 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
273 return Contents.OffsetedInfo.Val.GV;
276 int getOffset() const {
277 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
278 "Wrong MachineOperand accessor");
279 return Contents.OffsetedInfo.Offset;
282 const char *getSymbolName() const {
283 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
284 return Contents.OffsetedInfo.Val.SymbolName;
287 //===--------------------------------------------------------------------===//
288 // Mutators for various operand types.
289 //===--------------------------------------------------------------------===//
291 void setImm(int64_t immVal) {
292 assert(isImmediate() && "Wrong MachineOperand mutator");
293 Contents.ImmVal = immVal;
296 void setOffset(int Offset) {
297 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
298 "Wrong MachineOperand accessor");
299 Contents.OffsetedInfo.Offset = Offset;
302 void setIndex(int Idx) {
303 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
304 "Wrong MachineOperand accessor");
305 Contents.OffsetedInfo.Val.Index = Idx;
308 void setMBB(MachineBasicBlock *MBB) {
309 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
313 //===--------------------------------------------------------------------===//
315 //===--------------------------------------------------------------------===//
317 /// isIdenticalTo - Return true if this operand is identical to the specified
318 /// operand. Note: This method ignores isKill and isDead properties.
319 bool isIdenticalTo(const MachineOperand &Other) const;
321 /// ChangeToImmediate - Replace this operand with a new immediate operand of
322 /// the specified value. If an operand is known to be an immediate already,
323 /// the setImm method should be used.
324 void ChangeToImmediate(int64_t ImmVal);
326 /// ChangeToRegister - Replace this operand with a new register operand of
327 /// the specified value. If an operand is known to be an register already,
328 /// the setReg method should be used.
329 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
330 bool isKill = false, bool isDead = false,
331 bool isEarlyClobber = false);
333 //===--------------------------------------------------------------------===//
334 // Construction methods.
335 //===--------------------------------------------------------------------===//
337 static MachineOperand CreateImm(int64_t Val) {
338 MachineOperand Op(MachineOperand::MO_Immediate);
343 static MachineOperand CreateFPImm(const ConstantFP *CFP) {
344 MachineOperand Op(MachineOperand::MO_FPImmediate);
345 Op.Contents.CFP = CFP;
349 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
350 bool isKill = false, bool isDead = false,
352 bool isEarlyClobber = false) {
353 MachineOperand Op(MachineOperand::MO_Register);
358 Op.IsEarlyClobber = isEarlyClobber;
359 Op.Contents.Reg.RegNo = Reg;
360 Op.Contents.Reg.Prev = 0;
361 Op.Contents.Reg.Next = 0;
365 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
366 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
370 static MachineOperand CreateFI(unsigned Idx) {
371 MachineOperand Op(MachineOperand::MO_FrameIndex);
375 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
376 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
378 Op.setOffset(Offset);
381 static MachineOperand CreateJTI(unsigned Idx) {
382 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
386 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
387 MachineOperand Op(MachineOperand::MO_GlobalAddress);
388 Op.Contents.OffsetedInfo.Val.GV = GV;
389 Op.setOffset(Offset);
392 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
393 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
394 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
395 Op.setOffset(Offset);
398 const MachineOperand &operator=(const MachineOperand &MO) {
404 IsEarlyClobber = MO.IsEarlyClobber;
406 ParentMI = MO.ParentMI;
407 Contents = MO.Contents;
411 friend class MachineInstr;
412 friend class MachineRegisterInfo;
414 //===--------------------------------------------------------------------===//
415 // Methods for handling register use/def lists.
416 //===--------------------------------------------------------------------===//
418 /// isOnRegUseList - Return true if this operand is on a register use/def list
419 /// or false if not. This can only be called for register operands that are
420 /// part of a machine instruction.
421 bool isOnRegUseList() const {
422 assert(isReg() && "Can only add reg operand to use lists");
423 return Contents.Reg.Prev != 0;
426 /// AddRegOperandToRegInfo - Add this register operand to the specified
427 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
428 /// explicitly nulled out.
429 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
431 void RemoveRegOperandFromRegInfo() {
432 assert(isOnRegUseList() && "Reg operand is not on a use list");
433 // Unlink this from the doubly linked list of operands.
434 MachineOperand *NextOp = Contents.Reg.Next;
435 *Contents.Reg.Prev = NextOp;
437 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
438 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
440 Contents.Reg.Prev = 0;
441 Contents.Reg.Next = 0;
445 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
450 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
455 } // End llvm namespace