MachineOperand.h revision 1baa88e3de8947b02d9ef4caa73e5860f048ec6e
1//===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains the declaration of the MachineOperand class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CODEGEN_MACHINEOPERAND_H 15#define LLVM_CODEGEN_MACHINEOPERAND_H 16 17#include "llvm/Support/DataTypes.h" 18#include <cassert> 19#include <iosfwd> 20 21namespace llvm { 22 23class ConstantFP; 24class MachineBasicBlock; 25class GlobalValue; 26class MachineInstr; 27class TargetMachine; 28class MachineRegisterInfo; 29 30/// MachineOperand class - Representation of each machine instruction operand. 31/// 32class MachineOperand { 33public: 34 enum MachineOperandType { 35 MO_Register, // Register operand. 36 MO_Immediate, // Immediate Operand 37 MO_FPImmediate, 38 MO_MachineBasicBlock, // MachineBasicBlock reference 39 MO_FrameIndex, // Abstract Stack Frame Index 40 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool 41 MO_JumpTableIndex, // Address of indexed Jump Table for switch 42 MO_ExternalSymbol, // Name of external global symbol 43 MO_GlobalAddress // Address of a global value 44 }; 45 46private: 47 /// OpKind - Specify what kind of operand this is. This discriminates the 48 /// union. 49 MachineOperandType OpKind : 8; 50 51 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register 52 /// operands. 53 54 /// IsDef - True if this is a def, false if this is a use of the register. 55 /// 56 bool IsDef : 1; 57 58 /// IsImp - True if this is an implicit def or use, false if it is explicit. 59 /// 60 bool IsImp : 1; 61 62 /// IsKill - True if this instruction is the last use of the register on this 63 /// path through the function. This is only valid on uses of registers. 64 bool IsKill : 1; 65 66 /// IsDead - True if this register is never used by a subsequent instruction. 67 /// This is only valid on definitions of registers. 68 bool IsDead : 1; 69 70 /// SubReg - Subregister number, only valid for MO_Register. A value of 0 71 /// indicates the MO_Register has no subReg. 72 unsigned char SubReg; 73 74 /// ParentMI - This is the instruction that this operand is embedded into. 75 /// This is valid for all operand types, when the operand is in an instr. 76 MachineInstr *ParentMI; 77 78 /// Contents union - This contains the payload for the various operand types. 79 union { 80 MachineBasicBlock *MBB; // For MO_MachineBasicBlock. 81 ConstantFP *CFP; // For MO_FPImmediate. 82 int64_t ImmVal; // For MO_Immediate. 83 84 struct { // For MO_Register. 85 unsigned RegNo; 86 MachineOperand **Prev; // Access list for register. 87 MachineOperand *Next; 88 } Reg; 89 90 /// OffsetedInfo - This struct contains the offset and an object identifier. 91 /// this represent the object as with an optional offset from it. 92 struct { 93 union { 94 int Index; // For MO_*Index - The index itself. 95 const char *SymbolName; // For MO_ExternalSymbol. 96 GlobalValue *GV; // For MO_GlobalAddress. 97 } Val; 98 int Offset; // An offset from the object. 99 } OffsetedInfo; 100 } Contents; 101 102 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {} 103public: 104 MachineOperand(const MachineOperand &M) { 105 *this = M; 106 } 107 108 ~MachineOperand() {} 109 110 /// getType - Returns the MachineOperandType for this operand. 111 /// 112 MachineOperandType getType() const { return OpKind; } 113 114 /// getParent - Return the instruction that this operand belongs to. 115 /// 116 MachineInstr *getParent() { return ParentMI; } 117 const MachineInstr *getParent() const { return ParentMI; } 118 119 void print(std::ostream &os, const TargetMachine *TM = 0) const; 120 121 /// Accessors that tell you what kind of MachineOperand you're looking at. 122 /// 123 bool isRegister() const { return OpKind == MO_Register; } 124 bool isImmediate() const { return OpKind == MO_Immediate; } 125 bool isFPImmediate() const { return OpKind == MO_FPImmediate; } 126 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; } 127 bool isFrameIndex() const { return OpKind == MO_FrameIndex; } 128 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; } 129 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; } 130 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; } 131 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; } 132 133 bool isReg() const { return OpKind == MO_Register; } 134 bool isImm() const { return OpKind == MO_Immediate; } 135 bool isMBB() const { return OpKind == MO_MachineBasicBlock; } 136 bool isFI() const { return OpKind == MO_FrameIndex; } 137 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; } 138 bool isJTI() const { return OpKind == MO_JumpTableIndex; } 139 bool isGlobal() const { return OpKind == MO_GlobalAddress; } 140 bool isSymbol() const { return OpKind == MO_ExternalSymbol; } 141 142 //===--------------------------------------------------------------------===// 143 // Accessors for Register Operands 144 //===--------------------------------------------------------------------===// 145 146 /// getReg - Returns the register number. 147 unsigned getReg() const { 148 assert(isRegister() && "This is not a register operand!"); 149 return Contents.Reg.RegNo; 150 } 151 152 unsigned getSubReg() const { 153 assert(isRegister() && "Wrong MachineOperand accessor"); 154 return (unsigned)SubReg; 155 } 156 157 bool isUse() const { 158 assert(isRegister() && "Wrong MachineOperand accessor"); 159 return !IsDef; 160 } 161 162 bool isDef() const { 163 assert(isRegister() && "Wrong MachineOperand accessor"); 164 return IsDef; 165 } 166 167 bool isImplicit() const { 168 assert(isRegister() && "Wrong MachineOperand accessor"); 169 return IsImp; 170 } 171 172 bool isDead() const { 173 assert(isRegister() && "Wrong MachineOperand accessor"); 174 return IsDead; 175 } 176 177 bool isKill() const { 178 assert(isRegister() && "Wrong MachineOperand accessor"); 179 return IsKill; 180 } 181 182 /// getNextOperandForReg - Return the next MachineOperand in the function that 183 /// uses or defines this register. 184 MachineOperand *getNextOperandForReg() const { 185 assert(isRegister() && "This is not a register operand!"); 186 return Contents.Reg.Next; 187 } 188 189 //===--------------------------------------------------------------------===// 190 // Mutators for Register Operands 191 //===--------------------------------------------------------------------===// 192 193 /// Change the register this operand corresponds to. 194 /// 195 void setReg(unsigned Reg); 196 197 void setSubReg(unsigned subReg) { 198 assert(isRegister() && "Wrong MachineOperand accessor"); 199 SubReg = (unsigned char)subReg; 200 } 201 202 void setIsUse(bool Val = true) { 203 assert(isRegister() && "Wrong MachineOperand accessor"); 204 IsDef = !Val; 205 } 206 207 void setIsDef(bool Val = true) { 208 assert(isRegister() && "Wrong MachineOperand accessor"); 209 IsDef = Val; 210 } 211 212 void setImplicit(bool Val = true) { 213 assert(isRegister() && "Wrong MachineOperand accessor"); 214 IsImp = Val; 215 } 216 217 void setIsKill(bool Val = true) { 218 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor"); 219 IsKill = Val; 220 } 221 222 void setIsDead(bool Val = true) { 223 assert(isRegister() && IsDef && "Wrong MachineOperand accessor"); 224 IsDead = Val; 225 } 226 227 228 //===--------------------------------------------------------------------===// 229 // Accessors for various operand types. 230 //===--------------------------------------------------------------------===// 231 232 int64_t getImm() const { 233 assert(isImmediate() && "Wrong MachineOperand accessor"); 234 return Contents.ImmVal; 235 } 236 237 ConstantFP *getFPImm() const { 238 assert(isFPImmediate() && "Wrong MachineOperand accessor"); 239 return Contents.CFP; 240 } 241 242 MachineBasicBlock *getMBB() const { 243 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor"); 244 return Contents.MBB; 245 } 246 247 int getIndex() const { 248 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) && 249 "Wrong MachineOperand accessor"); 250 return Contents.OffsetedInfo.Val.Index; 251 } 252 253 GlobalValue *getGlobal() const { 254 assert(isGlobalAddress() && "Wrong MachineOperand accessor"); 255 return Contents.OffsetedInfo.Val.GV; 256 } 257 258 int getOffset() const { 259 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) && 260 "Wrong MachineOperand accessor"); 261 return Contents.OffsetedInfo.Offset; 262 } 263 264 const char *getSymbolName() const { 265 assert(isExternalSymbol() && "Wrong MachineOperand accessor"); 266 return Contents.OffsetedInfo.Val.SymbolName; 267 } 268 269 //===--------------------------------------------------------------------===// 270 // Mutators for various operand types. 271 //===--------------------------------------------------------------------===// 272 273 void setImm(int64_t immVal) { 274 assert(isImmediate() && "Wrong MachineOperand mutator"); 275 Contents.ImmVal = immVal; 276 } 277 278 void setOffset(int Offset) { 279 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) && 280 "Wrong MachineOperand accessor"); 281 Contents.OffsetedInfo.Offset = Offset; 282 } 283 284 void setIndex(int Idx) { 285 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) && 286 "Wrong MachineOperand accessor"); 287 Contents.OffsetedInfo.Val.Index = Idx; 288 } 289 290 void setMBB(MachineBasicBlock *MBB) { 291 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor"); 292 Contents.MBB = MBB; 293 } 294 295 //===--------------------------------------------------------------------===// 296 // Other methods. 297 //===--------------------------------------------------------------------===// 298 299 /// isIdenticalTo - Return true if this operand is identical to the specified 300 /// operand. Note: This method ignores isKill and isDead properties. 301 bool isIdenticalTo(const MachineOperand &Other) const; 302 303 /// ChangeToImmediate - Replace this operand with a new immediate operand of 304 /// the specified value. If an operand is known to be an immediate already, 305 /// the setImm method should be used. 306 void ChangeToImmediate(int64_t ImmVal); 307 308 /// ChangeToRegister - Replace this operand with a new register operand of 309 /// the specified value. If an operand is known to be an register already, 310 /// the setReg method should be used. 311 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false, 312 bool isKill = false, bool isDead = false); 313 314 //===--------------------------------------------------------------------===// 315 // Construction methods. 316 //===--------------------------------------------------------------------===// 317 318 static MachineOperand CreateImm(int64_t Val) { 319 MachineOperand Op(MachineOperand::MO_Immediate); 320 Op.setImm(Val); 321 return Op; 322 } 323 324 static MachineOperand CreateFPImm(ConstantFP *CFP) { 325 MachineOperand Op(MachineOperand::MO_FPImmediate); 326 Op.Contents.CFP = CFP; 327 return Op; 328 } 329 330 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false, 331 bool isKill = false, bool isDead = false, 332 unsigned SubReg = 0) { 333 MachineOperand Op(MachineOperand::MO_Register); 334 Op.IsDef = isDef; 335 Op.IsImp = isImp; 336 Op.IsKill = isKill; 337 Op.IsDead = isDead; 338 Op.Contents.Reg.RegNo = Reg; 339 Op.Contents.Reg.Prev = 0; 340 Op.Contents.Reg.Next = 0; 341 Op.SubReg = SubReg; 342 return Op; 343 } 344 static MachineOperand CreateMBB(MachineBasicBlock *MBB) { 345 MachineOperand Op(MachineOperand::MO_MachineBasicBlock); 346 Op.setMBB(MBB); 347 return Op; 348 } 349 static MachineOperand CreateFI(unsigned Idx) { 350 MachineOperand Op(MachineOperand::MO_FrameIndex); 351 Op.setIndex(Idx); 352 return Op; 353 } 354 static MachineOperand CreateCPI(unsigned Idx, int Offset) { 355 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex); 356 Op.setIndex(Idx); 357 Op.setOffset(Offset); 358 return Op; 359 } 360 static MachineOperand CreateJTI(unsigned Idx) { 361 MachineOperand Op(MachineOperand::MO_JumpTableIndex); 362 Op.setIndex(Idx); 363 return Op; 364 } 365 static MachineOperand CreateGA(GlobalValue *GV, int Offset) { 366 MachineOperand Op(MachineOperand::MO_GlobalAddress); 367 Op.Contents.OffsetedInfo.Val.GV = GV; 368 Op.setOffset(Offset); 369 return Op; 370 } 371 static MachineOperand CreateES(const char *SymName, int Offset = 0) { 372 MachineOperand Op(MachineOperand::MO_ExternalSymbol); 373 Op.Contents.OffsetedInfo.Val.SymbolName = SymName; 374 Op.setOffset(Offset); 375 return Op; 376 } 377 const MachineOperand &operator=(const MachineOperand &MO) { 378 OpKind = MO.OpKind; 379 IsDef = MO.IsDef; 380 IsImp = MO.IsImp; 381 IsKill = MO.IsKill; 382 IsDead = MO.IsDead; 383 SubReg = MO.SubReg; 384 ParentMI = MO.ParentMI; 385 Contents = MO.Contents; 386 return *this; 387 } 388 389 friend class MachineInstr; 390 friend class MachineRegisterInfo; 391private: 392 //===--------------------------------------------------------------------===// 393 // Methods for handling register use/def lists. 394 //===--------------------------------------------------------------------===// 395 396 /// isOnRegUseList - Return true if this operand is on a register use/def list 397 /// or false if not. This can only be called for register operands that are 398 /// part of a machine instruction. 399 bool isOnRegUseList() const { 400 assert(isReg() && "Can only add reg operand to use lists"); 401 return Contents.Reg.Prev != 0; 402 } 403 404 /// AddRegOperandToRegInfo - Add this register operand to the specified 405 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 406 /// explicitly nulled out. 407 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo); 408 409 void RemoveRegOperandFromRegInfo() { 410 assert(isOnRegUseList() && "Can only add reg operand to use lists"); 411 // Unlink this from the doubly linked list of operands. 412 MachineOperand *NextOp = Contents.Reg.Next; 413 *Contents.Reg.Prev = NextOp; 414 if (NextOp) { 415 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); 416 NextOp->Contents.Reg.Prev = Contents.Reg.Prev; 417 } 418 Contents.Reg.Prev = 0; 419 Contents.Reg.Next = 0; 420 } 421}; 422 423inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) { 424 MO.print(OS, 0); 425 return OS; 426} 427 428} // End llvm namespace 429 430#endif 431