MachineOperand.h revision 3f5db85b573cbb3f778413d882e6fd1854a5991b
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/System/DataTypes.h" 18#include <cassert> 19 20namespace llvm { 21 22class ConstantFP; 23class BlockAddress; 24class MachineBasicBlock; 25class GlobalValue; 26class MachineInstr; 27class TargetMachine; 28class MachineRegisterInfo; 29class MDNode; 30class raw_ostream; 31 32/// MachineOperand class - Representation of each machine instruction operand. 33/// 34class MachineOperand { 35public: 36 enum MachineOperandType { 37 MO_Register, ///< Register operand. 38 MO_Immediate, ///< Immediate operand 39 MO_FPImmediate, ///< Floating-point immediate operand 40 MO_MachineBasicBlock, ///< MachineBasicBlock reference 41 MO_FrameIndex, ///< Abstract Stack Frame Index 42 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool 43 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch 44 MO_ExternalSymbol, ///< Name of external global symbol 45 MO_GlobalAddress, ///< Address of a global value 46 MO_BlockAddress, ///< Address of a basic block 47 MO_Metadata ///< Metadata reference (for debug info) 48 }; 49 50private: 51 /// OpKind - Specify what kind of operand this is. This discriminates the 52 /// union. 53 unsigned char OpKind; // MachineOperandType 54 55 /// SubReg - Subregister number, only valid for MO_Register. A value of 0 56 /// indicates the MO_Register has no subReg. 57 unsigned char SubReg; 58 59 /// TargetFlags - This is a set of target-specific operand flags. 60 unsigned char TargetFlags; 61 62 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register 63 /// operands. 64 65 /// IsDef - True if this is a def, false if this is a use of the register. 66 /// 67 bool IsDef : 1; 68 69 /// IsImp - True if this is an implicit def or use, false if it is explicit. 70 /// 71 bool IsImp : 1; 72 73 /// IsKill - True if this instruction is the last use of the register on this 74 /// path through the function. This is only valid on uses of registers. 75 bool IsKill : 1; 76 77 /// IsDead - True if this register is never used by a subsequent instruction. 78 /// This is only valid on definitions of registers. 79 bool IsDead : 1; 80 81 /// IsUndef - True if this is a register def / use of "undef", i.e. register 82 /// defined by an IMPLICIT_DEF. This is only valid on registers. 83 bool IsUndef : 1; 84 85 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to 86 /// by the MachineInstr before all input registers are read. This is used to 87 /// model the GCC inline asm '&' constraint modifier. 88 bool IsEarlyClobber : 1; 89 90 /// ParentMI - This is the instruction that this operand is embedded into. 91 /// This is valid for all operand types, when the operand is in an instr. 92 MachineInstr *ParentMI; 93 94 /// Contents union - This contains the payload for the various operand types. 95 union { 96 MachineBasicBlock *MBB; // For MO_MachineBasicBlock. 97 const ConstantFP *CFP; // For MO_FPImmediate. 98 int64_t ImmVal; // For MO_Immediate. 99 MDNode *MD; // For MO_Metadata. 100 101 struct { // For MO_Register. 102 unsigned RegNo; 103 MachineOperand **Prev; // Access list for register. 104 MachineOperand *Next; 105 } Reg; 106 107 /// OffsetedInfo - This struct contains the offset and an object identifier. 108 /// this represent the object as with an optional offset from it. 109 struct { 110 union { 111 int Index; // For MO_*Index - The index itself. 112 const char *SymbolName; // For MO_ExternalSymbol. 113 GlobalValue *GV; // For MO_GlobalAddress. 114 BlockAddress *BA; // For MO_BlockAddress. 115 } Val; 116 int64_t Offset; // An offset from the object. 117 } OffsetedInfo; 118 } Contents; 119 120 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) { 121 TargetFlags = 0; 122 } 123public: 124 /// getType - Returns the MachineOperandType for this operand. 125 /// 126 MachineOperandType getType() const { return (MachineOperandType)OpKind; } 127 128 unsigned char getTargetFlags() const { return TargetFlags; } 129 void setTargetFlags(unsigned char F) { TargetFlags = F; } 130 void addTargetFlag(unsigned char F) { TargetFlags |= F; } 131 132 133 /// getParent - Return the instruction that this operand belongs to. 134 /// 135 MachineInstr *getParent() { return ParentMI; } 136 const MachineInstr *getParent() const { return ParentMI; } 137 138 void print(raw_ostream &os, const TargetMachine *TM = 0) const; 139 140 //===--------------------------------------------------------------------===// 141 // Accessors that tell you what kind of MachineOperand you're looking at. 142 //===--------------------------------------------------------------------===// 143 144 /// isReg - Tests if this is a MO_Register operand. 145 bool isReg() const { return OpKind == MO_Register; } 146 /// isImm - Tests if this is a MO_Immediate operand. 147 bool isImm() const { return OpKind == MO_Immediate; } 148 /// isFPImm - Tests if this is a MO_FPImmediate operand. 149 bool isFPImm() const { return OpKind == MO_FPImmediate; } 150 /// isMBB - Tests if this is a MO_MachineBasicBlock operand. 151 bool isMBB() const { return OpKind == MO_MachineBasicBlock; } 152 /// isFI - Tests if this is a MO_FrameIndex operand. 153 bool isFI() const { return OpKind == MO_FrameIndex; } 154 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand. 155 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; } 156 /// isJTI - Tests if this is a MO_JumpTableIndex operand. 157 bool isJTI() const { return OpKind == MO_JumpTableIndex; } 158 /// isGlobal - Tests if this is a MO_GlobalAddress operand. 159 bool isGlobal() const { return OpKind == MO_GlobalAddress; } 160 /// isSymbol - Tests if this is a MO_ExternalSymbol operand. 161 bool isSymbol() const { return OpKind == MO_ExternalSymbol; } 162 /// isBlockAddress - Tests if this is a MO_BlockAddress operand. 163 bool isBlockAddress() const { return OpKind == MO_BlockAddress; } 164 /// isMetadata - Tests if this is a MO_Metadata operand. 165 bool isMetadata() const { return OpKind == MO_Metadata; } 166 167 //===--------------------------------------------------------------------===// 168 // Accessors for Register Operands 169 //===--------------------------------------------------------------------===// 170 171 /// getReg - Returns the register number. 172 unsigned getReg() const { 173 assert(isReg() && "This is not a register operand!"); 174 return Contents.Reg.RegNo; 175 } 176 177 unsigned getSubReg() const { 178 assert(isReg() && "Wrong MachineOperand accessor"); 179 return (unsigned)SubReg; 180 } 181 182 bool isUse() const { 183 assert(isReg() && "Wrong MachineOperand accessor"); 184 return !IsDef; 185 } 186 187 bool isDef() const { 188 assert(isReg() && "Wrong MachineOperand accessor"); 189 return IsDef; 190 } 191 192 bool isImplicit() const { 193 assert(isReg() && "Wrong MachineOperand accessor"); 194 return IsImp; 195 } 196 197 bool isDead() const { 198 assert(isReg() && "Wrong MachineOperand accessor"); 199 return IsDead; 200 } 201 202 bool isKill() const { 203 assert(isReg() && "Wrong MachineOperand accessor"); 204 return IsKill; 205 } 206 207 bool isUndef() const { 208 assert(isReg() && "Wrong MachineOperand accessor"); 209 return IsUndef; 210 } 211 212 bool isEarlyClobber() const { 213 assert(isReg() && "Wrong MachineOperand accessor"); 214 return IsEarlyClobber; 215 } 216 217 /// getNextOperandForReg - Return the next MachineOperand in the function that 218 /// uses or defines this register. 219 MachineOperand *getNextOperandForReg() const { 220 assert(isReg() && "This is not a register operand!"); 221 return Contents.Reg.Next; 222 } 223 224 //===--------------------------------------------------------------------===// 225 // Mutators for Register Operands 226 //===--------------------------------------------------------------------===// 227 228 /// Change the register this operand corresponds to. 229 /// 230 void setReg(unsigned Reg); 231 232 void setSubReg(unsigned subReg) { 233 assert(isReg() && "Wrong MachineOperand accessor"); 234 SubReg = (unsigned char)subReg; 235 } 236 237 void setIsUse(bool Val = true) { 238 assert(isReg() && "Wrong MachineOperand accessor"); 239 IsDef = !Val; 240 } 241 242 void setIsDef(bool Val = true) { 243 assert(isReg() && "Wrong MachineOperand accessor"); 244 IsDef = Val; 245 } 246 247 void setImplicit(bool Val = true) { 248 assert(isReg() && "Wrong MachineOperand accessor"); 249 IsImp = Val; 250 } 251 252 void setIsKill(bool Val = true) { 253 assert(isReg() && !IsDef && "Wrong MachineOperand accessor"); 254 IsKill = Val; 255 } 256 257 void setIsDead(bool Val = true) { 258 assert(isReg() && IsDef && "Wrong MachineOperand accessor"); 259 IsDead = Val; 260 } 261 262 void setIsUndef(bool Val = true) { 263 assert(isReg() && "Wrong MachineOperand accessor"); 264 IsUndef = Val; 265 } 266 267 void setIsEarlyClobber(bool Val = true) { 268 assert(isReg() && IsDef && "Wrong MachineOperand accessor"); 269 IsEarlyClobber = Val; 270 } 271 272 //===--------------------------------------------------------------------===// 273 // Accessors for various operand types. 274 //===--------------------------------------------------------------------===// 275 276 int64_t getImm() const { 277 assert(isImm() && "Wrong MachineOperand accessor"); 278 return Contents.ImmVal; 279 } 280 281 const ConstantFP *getFPImm() const { 282 assert(isFPImm() && "Wrong MachineOperand accessor"); 283 return Contents.CFP; 284 } 285 286 MachineBasicBlock *getMBB() const { 287 assert(isMBB() && "Wrong MachineOperand accessor"); 288 return Contents.MBB; 289 } 290 291 int getIndex() const { 292 assert((isFI() || isCPI() || isJTI()) && 293 "Wrong MachineOperand accessor"); 294 return Contents.OffsetedInfo.Val.Index; 295 } 296 297 GlobalValue *getGlobal() const { 298 assert(isGlobal() && "Wrong MachineOperand accessor"); 299 return Contents.OffsetedInfo.Val.GV; 300 } 301 302 BlockAddress *getBlockAddress() const { 303 assert(isBlockAddress() && "Wrong MachineOperand accessor"); 304 return Contents.OffsetedInfo.Val.BA; 305 } 306 307 /// getOffset - Return the offset from the symbol in this operand. This always 308 /// returns 0 for ExternalSymbol operands. 309 int64_t getOffset() const { 310 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) && 311 "Wrong MachineOperand accessor"); 312 return Contents.OffsetedInfo.Offset; 313 } 314 315 const char *getSymbolName() const { 316 assert(isSymbol() && "Wrong MachineOperand accessor"); 317 return Contents.OffsetedInfo.Val.SymbolName; 318 } 319 320 const MDNode *getMetadata() const { 321 assert(isMetadata() && "Wrong MachineOperand accessor"); 322 return Contents.MD; 323 } 324 325 //===--------------------------------------------------------------------===// 326 // Mutators for various operand types. 327 //===--------------------------------------------------------------------===// 328 329 void setImm(int64_t immVal) { 330 assert(isImm() && "Wrong MachineOperand mutator"); 331 Contents.ImmVal = immVal; 332 } 333 334 void setOffset(int64_t Offset) { 335 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) && 336 "Wrong MachineOperand accessor"); 337 Contents.OffsetedInfo.Offset = Offset; 338 } 339 340 void setIndex(int Idx) { 341 assert((isFI() || isCPI() || isJTI()) && 342 "Wrong MachineOperand accessor"); 343 Contents.OffsetedInfo.Val.Index = Idx; 344 } 345 346 void setMBB(MachineBasicBlock *MBB) { 347 assert(isMBB() && "Wrong MachineOperand accessor"); 348 Contents.MBB = MBB; 349 } 350 351 //===--------------------------------------------------------------------===// 352 // Other methods. 353 //===--------------------------------------------------------------------===// 354 355 /// isIdenticalTo - Return true if this operand is identical to the specified 356 /// operand. Note: This method ignores isKill and isDead properties. 357 bool isIdenticalTo(const MachineOperand &Other) const; 358 359 /// ChangeToImmediate - Replace this operand with a new immediate operand of 360 /// the specified value. If an operand is known to be an immediate already, 361 /// the setImm method should be used. 362 void ChangeToImmediate(int64_t ImmVal); 363 364 /// ChangeToRegister - Replace this operand with a new register operand of 365 /// the specified value. If an operand is known to be an register already, 366 /// the setReg method should be used. 367 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false, 368 bool isKill = false, bool isDead = false, 369 bool isUndef = false); 370 371 //===--------------------------------------------------------------------===// 372 // Construction methods. 373 //===--------------------------------------------------------------------===// 374 375 static MachineOperand CreateImm(int64_t Val) { 376 MachineOperand Op(MachineOperand::MO_Immediate); 377 Op.setImm(Val); 378 return Op; 379 } 380 381 static MachineOperand CreateFPImm(const ConstantFP *CFP) { 382 MachineOperand Op(MachineOperand::MO_FPImmediate); 383 Op.Contents.CFP = CFP; 384 return Op; 385 } 386 387 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false, 388 bool isKill = false, bool isDead = false, 389 bool isUndef = false, 390 bool isEarlyClobber = false, 391 unsigned SubReg = 0) { 392 MachineOperand Op(MachineOperand::MO_Register); 393 Op.IsDef = isDef; 394 Op.IsImp = isImp; 395 Op.IsKill = isKill; 396 Op.IsDead = isDead; 397 Op.IsUndef = isUndef; 398 Op.IsEarlyClobber = isEarlyClobber; 399 Op.Contents.Reg.RegNo = Reg; 400 Op.Contents.Reg.Prev = 0; 401 Op.Contents.Reg.Next = 0; 402 Op.SubReg = SubReg; 403 return Op; 404 } 405 static MachineOperand CreateMBB(MachineBasicBlock *MBB, 406 unsigned char TargetFlags = 0) { 407 MachineOperand Op(MachineOperand::MO_MachineBasicBlock); 408 Op.setMBB(MBB); 409 Op.setTargetFlags(TargetFlags); 410 return Op; 411 } 412 static MachineOperand CreateFI(unsigned Idx) { 413 MachineOperand Op(MachineOperand::MO_FrameIndex); 414 Op.setIndex(Idx); 415 return Op; 416 } 417 static MachineOperand CreateCPI(unsigned Idx, int Offset, 418 unsigned char TargetFlags = 0) { 419 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex); 420 Op.setIndex(Idx); 421 Op.setOffset(Offset); 422 Op.setTargetFlags(TargetFlags); 423 return Op; 424 } 425 static MachineOperand CreateJTI(unsigned Idx, 426 unsigned char TargetFlags = 0) { 427 MachineOperand Op(MachineOperand::MO_JumpTableIndex); 428 Op.setIndex(Idx); 429 Op.setTargetFlags(TargetFlags); 430 return Op; 431 } 432 static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset, 433 unsigned char TargetFlags = 0) { 434 MachineOperand Op(MachineOperand::MO_GlobalAddress); 435 Op.Contents.OffsetedInfo.Val.GV = GV; 436 Op.setOffset(Offset); 437 Op.setTargetFlags(TargetFlags); 438 return Op; 439 } 440 static MachineOperand CreateES(const char *SymName, 441 unsigned char TargetFlags = 0) { 442 MachineOperand Op(MachineOperand::MO_ExternalSymbol); 443 Op.Contents.OffsetedInfo.Val.SymbolName = SymName; 444 Op.setOffset(0); // Offset is always 0. 445 Op.setTargetFlags(TargetFlags); 446 return Op; 447 } 448 static MachineOperand CreateBA(BlockAddress *BA, 449 unsigned char TargetFlags = 0) { 450 MachineOperand Op(MachineOperand::MO_BlockAddress); 451 Op.Contents.OffsetedInfo.Val.BA = BA; 452 Op.setOffset(0); // Offset is always 0. 453 Op.setTargetFlags(TargetFlags); 454 return Op; 455 } 456 static MachineOperand CreateMetadata(MDNode *Meta) { 457 MachineOperand Op(MachineOperand::MO_Metadata); 458 Op.Contents.MD = Meta; 459 return Op; 460 } 461 462 friend class MachineInstr; 463 friend class MachineRegisterInfo; 464private: 465 //===--------------------------------------------------------------------===// 466 // Methods for handling register use/def lists. 467 //===--------------------------------------------------------------------===// 468 469 /// isOnRegUseList - Return true if this operand is on a register use/def list 470 /// or false if not. This can only be called for register operands that are 471 /// part of a machine instruction. 472 bool isOnRegUseList() const { 473 assert(isReg() && "Can only add reg operand to use lists"); 474 return Contents.Reg.Prev != 0; 475 } 476 477 /// AddRegOperandToRegInfo - Add this register operand to the specified 478 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 479 /// explicitly nulled out. 480 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo); 481 482 /// RemoveRegOperandFromRegInfo - Remove this register operand from the 483 /// MachineRegisterInfo it is linked with. 484 void RemoveRegOperandFromRegInfo(); 485}; 486 487inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) { 488 MO.print(OS, 0); 489 return OS; 490} 491 492} // End llvm namespace 493 494#endif 495