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"
25 class MachineBasicBlock;
29 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;
122 /// Accessors that tell you what kind of MachineOperand you're looking at.
124 bool isRegister() const { return OpKind == MO_Register; }
125 bool isImmediate() const { return OpKind == MO_Immediate; }
126 bool isFPImmediate() const { return OpKind == MO_FPImmediate; }
127 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; }
128 bool isFrameIndex() const { return OpKind == MO_FrameIndex; }
129 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; }
130 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; }
131 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; }
132 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; }
134 bool isReg() const { return OpKind == MO_Register; }
135 bool isImm() const { return OpKind == MO_Immediate; }
136 bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
137 bool isFI() const { return OpKind == MO_FrameIndex; }
138 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
139 bool isJTI() const { return OpKind == MO_JumpTableIndex; }
140 bool isGlobal() const { return OpKind == MO_GlobalAddress; }
141 bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
143 //===--------------------------------------------------------------------===//
144 // Accessors for Register Operands
145 //===--------------------------------------------------------------------===//
147 /// getReg - Returns the register number.
148 unsigned getReg() const {
149 assert(isRegister() && "This is not a register operand!");
150 return Contents.Reg.RegNo;
153 unsigned getSubReg() const {
154 assert(isRegister() && "Wrong MachineOperand accessor");
155 return (unsigned)SubReg;
159 assert(isRegister() && "Wrong MachineOperand accessor");
164 assert(isRegister() && "Wrong MachineOperand accessor");
168 bool isImplicit() const {
169 assert(isRegister() && "Wrong MachineOperand accessor");
173 bool isDead() const {
174 assert(isRegister() && "Wrong MachineOperand accessor");
178 bool isKill() const {
179 assert(isRegister() && "Wrong MachineOperand accessor");
183 /// getNextOperandForReg - Return the next MachineOperand in the function that
184 /// uses or defines this register.
185 MachineOperand *getNextOperandForReg() const {
186 assert(isRegister() && "This is not a register operand!");
187 return Contents.Reg.Next;
190 //===--------------------------------------------------------------------===//
191 // Mutators for Register Operands
192 //===--------------------------------------------------------------------===//
194 /// Change the register this operand corresponds to.
196 void setReg(unsigned Reg);
198 void setSubReg(unsigned subReg) {
199 assert(isRegister() && "Wrong MachineOperand accessor");
200 SubReg = (unsigned char)subReg;
203 void setIsUse(bool Val = true) {
204 assert(isRegister() && "Wrong MachineOperand accessor");
208 void setIsDef(bool Val = true) {
209 assert(isRegister() && "Wrong MachineOperand accessor");
213 void setImplicit(bool Val = true) {
214 assert(isRegister() && "Wrong MachineOperand accessor");
218 void setIsKill(bool Val = true) {
219 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor");
223 void setIsDead(bool Val = true) {
224 assert(isRegister() && IsDef && "Wrong MachineOperand accessor");
229 //===--------------------------------------------------------------------===//
230 // Accessors for various operand types.
231 //===--------------------------------------------------------------------===//
233 int64_t getImm() const {
234 assert(isImmediate() && "Wrong MachineOperand accessor");
235 return Contents.ImmVal;
238 ConstantFP *getFPImm() const {
239 assert(isFPImmediate() && "Wrong MachineOperand accessor");
243 MachineBasicBlock *getMBB() const {
244 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
248 int getIndex() const {
249 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
250 "Wrong MachineOperand accessor");
251 return Contents.OffsetedInfo.Val.Index;
254 GlobalValue *getGlobal() const {
255 assert(isGlobalAddress() && "Wrong MachineOperand accessor");
256 return Contents.OffsetedInfo.Val.GV;
259 int getOffset() const {
260 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
261 "Wrong MachineOperand accessor");
262 return Contents.OffsetedInfo.Offset;
265 const char *getSymbolName() const {
266 assert(isExternalSymbol() && "Wrong MachineOperand accessor");
267 return Contents.OffsetedInfo.Val.SymbolName;
270 //===--------------------------------------------------------------------===//
271 // Mutators for various operand types.
272 //===--------------------------------------------------------------------===//
274 void setImm(int64_t immVal) {
275 assert(isImmediate() && "Wrong MachineOperand mutator");
276 Contents.ImmVal = immVal;
279 void setOffset(int Offset) {
280 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) &&
281 "Wrong MachineOperand accessor");
282 Contents.OffsetedInfo.Offset = Offset;
285 void setIndex(int Idx) {
286 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) &&
287 "Wrong MachineOperand accessor");
288 Contents.OffsetedInfo.Val.Index = Idx;
291 void setMBB(MachineBasicBlock *MBB) {
292 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor");
296 //===--------------------------------------------------------------------===//
298 //===--------------------------------------------------------------------===//
300 /// isIdenticalTo - Return true if this operand is identical to the specified
301 /// operand. Note: This method ignores isKill and isDead properties.
302 bool isIdenticalTo(const MachineOperand &Other) const;
304 /// ChangeToImmediate - Replace this operand with a new immediate operand of
305 /// the specified value. If an operand is known to be an immediate already,
306 /// the setImm method should be used.
307 void ChangeToImmediate(int64_t ImmVal);
309 /// ChangeToRegister - Replace this operand with a new register operand of
310 /// the specified value. If an operand is known to be an register already,
311 /// the setReg method should be used.
312 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
313 bool isKill = false, bool isDead = false);
315 //===--------------------------------------------------------------------===//
316 // Construction methods.
317 //===--------------------------------------------------------------------===//
319 static MachineOperand CreateImm(int64_t Val) {
320 MachineOperand Op(MachineOperand::MO_Immediate);
325 static MachineOperand CreateFPImm(ConstantFP *CFP) {
326 MachineOperand Op(MachineOperand::MO_FPImmediate);
327 Op.Contents.CFP = CFP;
331 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
332 bool isKill = false, bool isDead = false,
333 unsigned SubReg = 0) {
334 MachineOperand Op(MachineOperand::MO_Register);
339 Op.Contents.Reg.RegNo = Reg;
340 Op.Contents.Reg.Prev = 0;
341 Op.Contents.Reg.Next = 0;
345 static MachineOperand CreateMBB(MachineBasicBlock *MBB) {
346 MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
350 static MachineOperand CreateFI(unsigned Idx) {
351 MachineOperand Op(MachineOperand::MO_FrameIndex);
355 static MachineOperand CreateCPI(unsigned Idx, int Offset) {
356 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
358 Op.setOffset(Offset);
361 static MachineOperand CreateJTI(unsigned Idx) {
362 MachineOperand Op(MachineOperand::MO_JumpTableIndex);
366 static MachineOperand CreateGA(GlobalValue *GV, int Offset) {
367 MachineOperand Op(MachineOperand::MO_GlobalAddress);
368 Op.Contents.OffsetedInfo.Val.GV = GV;
369 Op.setOffset(Offset);
372 static MachineOperand CreateES(const char *SymName, int Offset = 0) {
373 MachineOperand Op(MachineOperand::MO_ExternalSymbol);
374 Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
375 Op.setOffset(Offset);
378 const MachineOperand &operator=(const MachineOperand &MO) {
385 ParentMI = MO.ParentMI;
386 Contents = MO.Contents;
390 friend class MachineInstr;
391 friend class MachineRegisterInfo;
393 //===--------------------------------------------------------------------===//
394 // Methods for handling register use/def lists.
395 //===--------------------------------------------------------------------===//
397 /// isOnRegUseList - Return true if this operand is on a register use/def list
398 /// or false if not. This can only be called for register operands that are
399 /// part of a machine instruction.
400 bool isOnRegUseList() const {
401 assert(isReg() && "Can only add reg operand to use lists");
402 return Contents.Reg.Prev != 0;
405 /// AddRegOperandToRegInfo - Add this register operand to the specified
406 /// MachineRegisterInfo. If it is null, then the next/prev fields should be
407 /// explicitly nulled out.
408 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
410 void RemoveRegOperandFromRegInfo() {
411 assert(isOnRegUseList() && "Can only add reg operand to use lists");
412 // Unlink this from the doubly linked list of operands.
413 MachineOperand *NextOp = Contents.Reg.Next;
414 *Contents.Reg.Prev = NextOp;
416 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
417 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
419 Contents.Reg.Prev = 0;
420 Contents.Reg.Next = 0;
424 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
429 } // End llvm namespace