1 //===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the declaration of the MachineOperand class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CODEGEN_MACHINEOPERAND_H 14 #define LLVM_CODEGEN_MACHINEOPERAND_H 15 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/CodeGen/Register.h" 18 #include "llvm/IR/Intrinsics.h" 19 #include "llvm/Support/DataTypes.h" 20 #include "llvm/Support/LowLevelTypeImpl.h" 21 #include <cassert> 22 23 namespace llvm { 24 25 class BlockAddress; 26 class Constant; 27 class ConstantFP; 28 class ConstantInt; 29 class GlobalValue; 30 class MachineBasicBlock; 31 class MachineInstr; 32 class MachineRegisterInfo; 33 class MCCFIInstruction; 34 class MDNode; 35 class ModuleSlotTracker; 36 class TargetMachine; 37 class TargetIntrinsicInfo; 38 class TargetRegisterInfo; 39 class hash_code; 40 class raw_ostream; 41 class MCSymbol; 42 43 /// MachineOperand class - Representation of each machine instruction operand. 44 /// 45 /// This class isn't a POD type because it has a private constructor, but its 46 /// destructor must be trivial. Functions like MachineInstr::addOperand(), 47 /// MachineRegisterInfo::moveOperands(), and MF::DeleteMachineInstr() depend on 48 /// not having to call the MachineOperand destructor. 49 /// 50 class MachineOperand { 51 public: 52 enum MachineOperandType : unsigned char { 53 MO_Register, ///< Register operand. 54 MO_Immediate, ///< Immediate operand 55 MO_CImmediate, ///< Immediate >64bit operand 56 MO_FPImmediate, ///< Floating-point immediate operand 57 MO_MachineBasicBlock, ///< MachineBasicBlock reference 58 MO_FrameIndex, ///< Abstract Stack Frame Index 59 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool 60 MO_TargetIndex, ///< Target-dependent index+offset operand. 61 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch 62 MO_ExternalSymbol, ///< Name of external global symbol 63 MO_GlobalAddress, ///< Address of a global value 64 MO_BlockAddress, ///< Address of a basic block 65 MO_RegisterMask, ///< Mask of preserved registers. 66 MO_RegisterLiveOut, ///< Mask of live-out registers. 67 MO_Metadata, ///< Metadata reference (for debug info) 68 MO_MCSymbol, ///< MCSymbol reference (for debug/eh info) 69 MO_CFIIndex, ///< MCCFIInstruction index. 70 MO_IntrinsicID, ///< Intrinsic ID for ISel 71 MO_Predicate, ///< Generic predicate for ISel 72 MO_ShuffleMask, ///< Other IR Constant for ISel (shuffle masks) 73 MO_Last = MO_ShuffleMask 74 }; 75 76 private: 77 /// OpKind - Specify what kind of operand this is. This discriminates the 78 /// union. 79 unsigned OpKind : 8; 80 81 /// Subregister number for MO_Register. A value of 0 indicates the 82 /// MO_Register has no subReg. 83 /// 84 /// For all other kinds of operands, this field holds target-specific flags. 85 unsigned SubReg_TargetFlags : 12; 86 87 /// TiedTo - Non-zero when this register operand is tied to another register 88 /// operand. The encoding of this field is described in the block comment 89 /// before MachineInstr::tieOperands(). 90 unsigned TiedTo : 4; 91 92 /// IsDef - True if this is a def, false if this is a use of the register. 93 /// This is only valid on register operands. 94 /// 95 unsigned IsDef : 1; 96 97 /// IsImp - True if this is an implicit def or use, false if it is explicit. 98 /// This is only valid on register opderands. 99 /// 100 unsigned IsImp : 1; 101 102 /// IsDeadOrKill 103 /// For uses: IsKill - True if this instruction is the last use of the 104 /// register on this path through the function. 105 /// For defs: IsDead - True if this register is never used by a subsequent 106 /// instruction. 107 /// This is only valid on register operands. 108 unsigned IsDeadOrKill : 1; 109 110 /// See isRenamable(). 111 unsigned IsRenamable : 1; 112 113 /// IsUndef - True if this register operand reads an "undef" value, i.e. the 114 /// read value doesn't matter. This flag can be set on both use and def 115 /// operands. On a sub-register def operand, it refers to the part of the 116 /// register that isn't written. On a full-register def operand, it is a 117 /// noop. See readsReg(). 118 /// 119 /// This is only valid on registers. 120 /// 121 /// Note that an instruction may have multiple <undef> operands referring to 122 /// the same register. In that case, the instruction may depend on those 123 /// operands reading the same dont-care value. For example: 124 /// 125 /// %1 = XOR undef %2, undef %2 126 /// 127 /// Any register can be used for %2, and its value doesn't matter, but 128 /// the two operands must be the same register. 129 /// 130 unsigned IsUndef : 1; 131 132 /// IsInternalRead - True if this operand reads a value that was defined 133 /// inside the same instruction or bundle. This flag can be set on both use 134 /// and def operands. On a sub-register def operand, it refers to the part 135 /// of the register that isn't written. On a full-register def operand, it 136 /// is a noop. 137 /// 138 /// When this flag is set, the instruction bundle must contain at least one 139 /// other def of the register. If multiple instructions in the bundle define 140 /// the register, the meaning is target-defined. 141 unsigned IsInternalRead : 1; 142 143 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to 144 /// by the MachineInstr before all input registers are read. This is used to 145 /// model the GCC inline asm '&' constraint modifier. 146 unsigned IsEarlyClobber : 1; 147 148 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo, 149 /// not a real instruction. Such uses should be ignored during codegen. 150 unsigned IsDebug : 1; 151 152 /// SmallContents - This really should be part of the Contents union, but 153 /// lives out here so we can get a better packed struct. 154 /// MO_Register: Register number. 155 /// OffsetedInfo: Low bits of offset. 156 union { 157 unsigned RegNo; // For MO_Register. 158 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi. 159 } SmallContents; 160 161 /// ParentMI - This is the instruction that this operand is embedded into. 162 /// This is valid for all operand types, when the operand is in an instr. 163 MachineInstr *ParentMI; 164 165 /// Contents union - This contains the payload for the various operand types. 166 union ContentsUnion { ContentsUnion()167 ContentsUnion() {} 168 MachineBasicBlock *MBB; // For MO_MachineBasicBlock. 169 const ConstantFP *CFP; // For MO_FPImmediate. 170 const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit. 171 int64_t ImmVal; // For MO_Immediate. 172 const uint32_t *RegMask; // For MO_RegisterMask and MO_RegisterLiveOut. 173 const MDNode *MD; // For MO_Metadata. 174 MCSymbol *Sym; // For MO_MCSymbol. 175 unsigned CFIIndex; // For MO_CFI. 176 Intrinsic::ID IntrinsicID; // For MO_IntrinsicID. 177 unsigned Pred; // For MO_Predicate 178 ArrayRef<int> ShuffleMask; // For MO_ShuffleMask 179 180 struct { // For MO_Register. 181 // Register number is in SmallContents.RegNo. 182 MachineOperand *Prev; // Access list for register. See MRI. 183 MachineOperand *Next; 184 } Reg; 185 186 /// OffsetedInfo - This struct contains the offset and an object identifier. 187 /// this represent the object as with an optional offset from it. 188 struct { 189 union { 190 int Index; // For MO_*Index - The index itself. 191 const char *SymbolName; // For MO_ExternalSymbol. 192 const GlobalValue *GV; // For MO_GlobalAddress. 193 const BlockAddress *BA; // For MO_BlockAddress. 194 } Val; 195 // Low bits of offset are in SmallContents.OffsetLo. 196 int OffsetHi; // An offset from the object, high 32 bits. 197 } OffsetedInfo; 198 } Contents; 199 MachineOperand(MachineOperandType K)200 explicit MachineOperand(MachineOperandType K) 201 : OpKind(K), SubReg_TargetFlags(0), ParentMI(nullptr) { 202 // Assert that the layout is what we expect. It's easy to grow this object. 203 static_assert(alignof(MachineOperand) <= alignof(int64_t), 204 "MachineOperand shouldn't be more than 8 byte aligned"); 205 static_assert(sizeof(Contents) <= 2 * sizeof(void *), 206 "Contents should be at most two pointers"); 207 static_assert(sizeof(MachineOperand) <= 208 alignTo<alignof(int64_t)>(2 * sizeof(unsigned) + 209 3 * sizeof(void *)), 210 "MachineOperand too big. Should be Kind, SmallContents, " 211 "ParentMI, and Contents"); 212 } 213 214 public: 215 /// getType - Returns the MachineOperandType for this operand. 216 /// getType()217 MachineOperandType getType() const { return (MachineOperandType)OpKind; } 218 getTargetFlags()219 unsigned getTargetFlags() const { 220 return isReg() ? 0 : SubReg_TargetFlags; 221 } setTargetFlags(unsigned F)222 void setTargetFlags(unsigned F) { 223 assert(!isReg() && "Register operands can't have target flags"); 224 SubReg_TargetFlags = F; 225 assert(SubReg_TargetFlags == F && "Target flags out of range"); 226 } addTargetFlag(unsigned F)227 void addTargetFlag(unsigned F) { 228 assert(!isReg() && "Register operands can't have target flags"); 229 SubReg_TargetFlags |= F; 230 assert((SubReg_TargetFlags & F) && "Target flags out of range"); 231 } 232 233 234 /// getParent - Return the instruction that this operand belongs to. 235 /// getParent()236 MachineInstr *getParent() { return ParentMI; } getParent()237 const MachineInstr *getParent() const { return ParentMI; } 238 239 /// clearParent - Reset the parent pointer. 240 /// 241 /// The MachineOperand copy constructor also copies ParentMI, expecting the 242 /// original to be deleted. If a MachineOperand is ever stored outside a 243 /// MachineInstr, the parent pointer must be cleared. 244 /// 245 /// Never call clearParent() on an operand in a MachineInstr. 246 /// clearParent()247 void clearParent() { ParentMI = nullptr; } 248 249 /// Print a subreg index operand. 250 /// MO_Immediate operands can also be subreg idices. If it's the case, the 251 /// subreg index name will be printed. MachineInstr::isOperandSubregIdx can be 252 /// called to check this. 253 static void printSubRegIdx(raw_ostream &OS, uint64_t Index, 254 const TargetRegisterInfo *TRI); 255 256 /// Print operand target flags. 257 static void printTargetFlags(raw_ostream& OS, const MachineOperand &Op); 258 259 /// Print a MCSymbol as an operand. 260 static void printSymbol(raw_ostream &OS, MCSymbol &Sym); 261 262 /// Print a stack object reference. 263 static void printStackObjectReference(raw_ostream &OS, unsigned FrameIndex, 264 bool IsFixed, StringRef Name); 265 266 /// Print the offset with explicit +/- signs. 267 static void printOperandOffset(raw_ostream &OS, int64_t Offset); 268 269 /// Print an IRSlotNumber. 270 static void printIRSlotNumber(raw_ostream &OS, int Slot); 271 272 /// Print the MachineOperand to \p os. 273 /// Providing a valid \p TRI and \p IntrinsicInfo results in a more 274 /// target-specific printing. If \p TRI and \p IntrinsicInfo are null, the 275 /// function will try to pick it up from the parent. 276 void print(raw_ostream &os, const TargetRegisterInfo *TRI = nullptr, 277 const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const; 278 279 /// More complex way of printing a MachineOperand. 280 /// \param TypeToPrint specifies the generic type to be printed on uses and 281 /// defs. It can be determined using MachineInstr::getTypeToPrint. 282 /// \param OpIdx - specifies the index of the operand in machine instruction. 283 /// This will be used by target dependent MIR formatter. Could be None if the 284 /// index is unknown, e.g. called by dump(). 285 /// \param PrintDef - whether we want to print `def` on an operand which 286 /// isDef. Sometimes, if the operand is printed before '=', we don't print 287 /// `def`. 288 /// \param IsStandalone - whether we want a verbose output of the MO. This 289 /// prints extra information that can be easily inferred when printing the 290 /// whole function, but not when printing only a fragment of it. 291 /// \param ShouldPrintRegisterTies - whether we want to print register ties. 292 /// Sometimes they are easily determined by the instruction's descriptor 293 /// (MachineInstr::hasComplexRegiterTies can determine if it's needed). 294 /// \param TiedOperandIdx - if we need to print register ties this needs to 295 /// provide the index of the tied register. If not, it will be ignored. 296 /// \param TRI - provide more target-specific information to the printer. 297 /// Unlike the previous function, this one will not try and get the 298 /// information from it's parent. 299 /// \param IntrinsicInfo - same as \p TRI. 300 void print(raw_ostream &os, ModuleSlotTracker &MST, LLT TypeToPrint, 301 Optional<unsigned> OpIdx, bool PrintDef, bool IsStandalone, 302 bool ShouldPrintRegisterTies, unsigned TiedOperandIdx, 303 const TargetRegisterInfo *TRI, 304 const TargetIntrinsicInfo *IntrinsicInfo) const; 305 306 /// Same as print(os, TRI, IntrinsicInfo), but allows to specify the low-level 307 /// type to be printed the same way the full version of print(...) does it. 308 void print(raw_ostream &os, LLT TypeToPrint, 309 const TargetRegisterInfo *TRI = nullptr, 310 const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const; 311 312 void dump() const; 313 314 //===--------------------------------------------------------------------===// 315 // Accessors that tell you what kind of MachineOperand you're looking at. 316 //===--------------------------------------------------------------------===// 317 318 /// isReg - Tests if this is a MO_Register operand. isReg()319 bool isReg() const { return OpKind == MO_Register; } 320 /// isImm - Tests if this is a MO_Immediate operand. isImm()321 bool isImm() const { return OpKind == MO_Immediate; } 322 /// isCImm - Test if this is a MO_CImmediate operand. isCImm()323 bool isCImm() const { return OpKind == MO_CImmediate; } 324 /// isFPImm - Tests if this is a MO_FPImmediate operand. isFPImm()325 bool isFPImm() const { return OpKind == MO_FPImmediate; } 326 /// isMBB - Tests if this is a MO_MachineBasicBlock operand. isMBB()327 bool isMBB() const { return OpKind == MO_MachineBasicBlock; } 328 /// isFI - Tests if this is a MO_FrameIndex operand. isFI()329 bool isFI() const { return OpKind == MO_FrameIndex; } 330 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand. isCPI()331 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; } 332 /// isTargetIndex - Tests if this is a MO_TargetIndex operand. isTargetIndex()333 bool isTargetIndex() const { return OpKind == MO_TargetIndex; } 334 /// isJTI - Tests if this is a MO_JumpTableIndex operand. isJTI()335 bool isJTI() const { return OpKind == MO_JumpTableIndex; } 336 /// isGlobal - Tests if this is a MO_GlobalAddress operand. isGlobal()337 bool isGlobal() const { return OpKind == MO_GlobalAddress; } 338 /// isSymbol - Tests if this is a MO_ExternalSymbol operand. isSymbol()339 bool isSymbol() const { return OpKind == MO_ExternalSymbol; } 340 /// isBlockAddress - Tests if this is a MO_BlockAddress operand. isBlockAddress()341 bool isBlockAddress() const { return OpKind == MO_BlockAddress; } 342 /// isRegMask - Tests if this is a MO_RegisterMask operand. isRegMask()343 bool isRegMask() const { return OpKind == MO_RegisterMask; } 344 /// isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand. isRegLiveOut()345 bool isRegLiveOut() const { return OpKind == MO_RegisterLiveOut; } 346 /// isMetadata - Tests if this is a MO_Metadata operand. isMetadata()347 bool isMetadata() const { return OpKind == MO_Metadata; } isMCSymbol()348 bool isMCSymbol() const { return OpKind == MO_MCSymbol; } isCFIIndex()349 bool isCFIIndex() const { return OpKind == MO_CFIIndex; } isIntrinsicID()350 bool isIntrinsicID() const { return OpKind == MO_IntrinsicID; } isPredicate()351 bool isPredicate() const { return OpKind == MO_Predicate; } isShuffleMask()352 bool isShuffleMask() const { return OpKind == MO_ShuffleMask; } 353 //===--------------------------------------------------------------------===// 354 // Accessors for Register Operands 355 //===--------------------------------------------------------------------===// 356 357 /// getReg - Returns the register number. getReg()358 Register getReg() const { 359 assert(isReg() && "This is not a register operand!"); 360 return Register(SmallContents.RegNo); 361 } 362 getSubReg()363 unsigned getSubReg() const { 364 assert(isReg() && "Wrong MachineOperand accessor"); 365 return SubReg_TargetFlags; 366 } 367 isUse()368 bool isUse() const { 369 assert(isReg() && "Wrong MachineOperand accessor"); 370 return !IsDef; 371 } 372 isDef()373 bool isDef() const { 374 assert(isReg() && "Wrong MachineOperand accessor"); 375 return IsDef; 376 } 377 isImplicit()378 bool isImplicit() const { 379 assert(isReg() && "Wrong MachineOperand accessor"); 380 return IsImp; 381 } 382 isDead()383 bool isDead() const { 384 assert(isReg() && "Wrong MachineOperand accessor"); 385 return IsDeadOrKill & IsDef; 386 } 387 isKill()388 bool isKill() const { 389 assert(isReg() && "Wrong MachineOperand accessor"); 390 return IsDeadOrKill & !IsDef; 391 } 392 isUndef()393 bool isUndef() const { 394 assert(isReg() && "Wrong MachineOperand accessor"); 395 return IsUndef; 396 } 397 398 /// isRenamable - Returns true if this register may be renamed, i.e. it does 399 /// not generate a value that is somehow read in a way that is not represented 400 /// by the Machine IR (e.g. to meet an ABI or ISA requirement). This is only 401 /// valid on physical register operands. Virtual registers are assumed to 402 /// always be renamable regardless of the value of this field. 403 /// 404 /// Operands that are renamable can freely be changed to any other register 405 /// that is a member of the register class returned by 406 /// MI->getRegClassConstraint(). 407 /// 408 /// isRenamable can return false for several different reasons: 409 /// 410 /// - ABI constraints (since liveness is not always precisely modeled). We 411 /// conservatively handle these cases by setting all physical register 412 /// operands that didn’t start out as virtual regs to not be renamable. 413 /// Also any physical register operands created after register allocation or 414 /// whose register is changed after register allocation will not be 415 /// renamable. This state is tracked in the MachineOperand::IsRenamable 416 /// bit. 417 /// 418 /// - Opcode/target constraints: for opcodes that have complex register class 419 /// requirements (e.g. that depend on other operands/instructions), we set 420 /// hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq in the machine opcode 421 /// description. Operands belonging to instructions with opcodes that are 422 /// marked hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq return false from 423 /// isRenamable(). Additionally, the AllowRegisterRenaming target property 424 /// prevents any operands from being marked renamable for targets that don't 425 /// have detailed opcode hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq 426 /// values. 427 bool isRenamable() const; 428 isInternalRead()429 bool isInternalRead() const { 430 assert(isReg() && "Wrong MachineOperand accessor"); 431 return IsInternalRead; 432 } 433 isEarlyClobber()434 bool isEarlyClobber() const { 435 assert(isReg() && "Wrong MachineOperand accessor"); 436 return IsEarlyClobber; 437 } 438 isTied()439 bool isTied() const { 440 assert(isReg() && "Wrong MachineOperand accessor"); 441 return TiedTo; 442 } 443 isDebug()444 bool isDebug() const { 445 assert(isReg() && "Wrong MachineOperand accessor"); 446 return IsDebug; 447 } 448 449 /// readsReg - Returns true if this operand reads the previous value of its 450 /// register. A use operand with the <undef> flag set doesn't read its 451 /// register. A sub-register def implicitly reads the other parts of the 452 /// register being redefined unless the <undef> flag is set. 453 /// 454 /// This refers to reading the register value from before the current 455 /// instruction or bundle. Internal bundle reads are not included. readsReg()456 bool readsReg() const { 457 assert(isReg() && "Wrong MachineOperand accessor"); 458 return !isUndef() && !isInternalRead() && (isUse() || getSubReg()); 459 } 460 461 //===--------------------------------------------------------------------===// 462 // Mutators for Register Operands 463 //===--------------------------------------------------------------------===// 464 465 /// Change the register this operand corresponds to. 466 /// 467 void setReg(Register Reg); 468 setSubReg(unsigned subReg)469 void setSubReg(unsigned subReg) { 470 assert(isReg() && "Wrong MachineOperand mutator"); 471 SubReg_TargetFlags = subReg; 472 assert(SubReg_TargetFlags == subReg && "SubReg out of range"); 473 } 474 475 /// substVirtReg - Substitute the current register with the virtual 476 /// subregister Reg:SubReg. Take any existing SubReg index into account, 477 /// using TargetRegisterInfo to compose the subreg indices if necessary. 478 /// Reg must be a virtual register, SubIdx can be 0. 479 /// 480 void substVirtReg(Register Reg, unsigned SubIdx, const TargetRegisterInfo&); 481 482 /// substPhysReg - Substitute the current register with the physical register 483 /// Reg, taking any existing SubReg into account. For instance, 484 /// substPhysReg(%eax) will change %reg1024:sub_8bit to %al. 485 /// 486 void substPhysReg(MCRegister Reg, const TargetRegisterInfo&); 487 488 void setIsUse(bool Val = true) { setIsDef(!Val); } 489 490 /// Change a def to a use, or a use to a def. 491 void setIsDef(bool Val = true); 492 493 void setImplicit(bool Val = true) { 494 assert(isReg() && "Wrong MachineOperand mutator"); 495 IsImp = Val; 496 } 497 498 void setIsKill(bool Val = true) { 499 assert(isReg() && !IsDef && "Wrong MachineOperand mutator"); 500 assert((!Val || !isDebug()) && "Marking a debug operation as kill"); 501 IsDeadOrKill = Val; 502 } 503 504 void setIsDead(bool Val = true) { 505 assert(isReg() && IsDef && "Wrong MachineOperand mutator"); 506 IsDeadOrKill = Val; 507 } 508 509 void setIsUndef(bool Val = true) { 510 assert(isReg() && "Wrong MachineOperand mutator"); 511 IsUndef = Val; 512 } 513 514 void setIsRenamable(bool Val = true); 515 516 void setIsInternalRead(bool Val = true) { 517 assert(isReg() && "Wrong MachineOperand mutator"); 518 IsInternalRead = Val; 519 } 520 521 void setIsEarlyClobber(bool Val = true) { 522 assert(isReg() && IsDef && "Wrong MachineOperand mutator"); 523 IsEarlyClobber = Val; 524 } 525 526 void setIsDebug(bool Val = true) { 527 assert(isReg() && !IsDef && "Wrong MachineOperand mutator"); 528 IsDebug = Val; 529 } 530 531 //===--------------------------------------------------------------------===// 532 // Accessors for various operand types. 533 //===--------------------------------------------------------------------===// 534 getImm()535 int64_t getImm() const { 536 assert(isImm() && "Wrong MachineOperand accessor"); 537 return Contents.ImmVal; 538 } 539 getCImm()540 const ConstantInt *getCImm() const { 541 assert(isCImm() && "Wrong MachineOperand accessor"); 542 return Contents.CI; 543 } 544 getFPImm()545 const ConstantFP *getFPImm() const { 546 assert(isFPImm() && "Wrong MachineOperand accessor"); 547 return Contents.CFP; 548 } 549 getMBB()550 MachineBasicBlock *getMBB() const { 551 assert(isMBB() && "Wrong MachineOperand accessor"); 552 return Contents.MBB; 553 } 554 getIndex()555 int getIndex() const { 556 assert((isFI() || isCPI() || isTargetIndex() || isJTI()) && 557 "Wrong MachineOperand accessor"); 558 return Contents.OffsetedInfo.Val.Index; 559 } 560 getGlobal()561 const GlobalValue *getGlobal() const { 562 assert(isGlobal() && "Wrong MachineOperand accessor"); 563 return Contents.OffsetedInfo.Val.GV; 564 } 565 getBlockAddress()566 const BlockAddress *getBlockAddress() const { 567 assert(isBlockAddress() && "Wrong MachineOperand accessor"); 568 return Contents.OffsetedInfo.Val.BA; 569 } 570 getMCSymbol()571 MCSymbol *getMCSymbol() const { 572 assert(isMCSymbol() && "Wrong MachineOperand accessor"); 573 return Contents.Sym; 574 } 575 getCFIIndex()576 unsigned getCFIIndex() const { 577 assert(isCFIIndex() && "Wrong MachineOperand accessor"); 578 return Contents.CFIIndex; 579 } 580 getIntrinsicID()581 Intrinsic::ID getIntrinsicID() const { 582 assert(isIntrinsicID() && "Wrong MachineOperand accessor"); 583 return Contents.IntrinsicID; 584 } 585 getPredicate()586 unsigned getPredicate() const { 587 assert(isPredicate() && "Wrong MachineOperand accessor"); 588 return Contents.Pred; 589 } 590 getShuffleMask()591 ArrayRef<int> getShuffleMask() const { 592 assert(isShuffleMask() && "Wrong MachineOperand accessor"); 593 return Contents.ShuffleMask; 594 } 595 596 /// Return the offset from the symbol in this operand. This always returns 0 597 /// for ExternalSymbol operands. getOffset()598 int64_t getOffset() const { 599 assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() || 600 isTargetIndex() || isBlockAddress()) && 601 "Wrong MachineOperand accessor"); 602 return int64_t(uint64_t(Contents.OffsetedInfo.OffsetHi) << 32) | 603 SmallContents.OffsetLo; 604 } 605 getSymbolName()606 const char *getSymbolName() const { 607 assert(isSymbol() && "Wrong MachineOperand accessor"); 608 return Contents.OffsetedInfo.Val.SymbolName; 609 } 610 611 /// clobbersPhysReg - Returns true if this RegMask clobbers PhysReg. 612 /// It is sometimes necessary to detach the register mask pointer from its 613 /// machine operand. This static method can be used for such detached bit 614 /// mask pointers. clobbersPhysReg(const uint32_t * RegMask,unsigned PhysReg)615 static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg) { 616 // See TargetRegisterInfo.h. 617 assert(PhysReg < (1u << 30) && "Not a physical register"); 618 return !(RegMask[PhysReg / 32] & (1u << PhysReg % 32)); 619 } 620 621 /// clobbersPhysReg - Returns true if this RegMask operand clobbers PhysReg. clobbersPhysReg(unsigned PhysReg)622 bool clobbersPhysReg(unsigned PhysReg) const { 623 return clobbersPhysReg(getRegMask(), PhysReg); 624 } 625 626 /// getRegMask - Returns a bit mask of registers preserved by this RegMask 627 /// operand. getRegMask()628 const uint32_t *getRegMask() const { 629 assert(isRegMask() && "Wrong MachineOperand accessor"); 630 return Contents.RegMask; 631 } 632 633 /// Returns number of elements needed for a regmask array. getRegMaskSize(unsigned NumRegs)634 static unsigned getRegMaskSize(unsigned NumRegs) { 635 return (NumRegs + 31) / 32; 636 } 637 638 /// getRegLiveOut - Returns a bit mask of live-out registers. getRegLiveOut()639 const uint32_t *getRegLiveOut() const { 640 assert(isRegLiveOut() && "Wrong MachineOperand accessor"); 641 return Contents.RegMask; 642 } 643 getMetadata()644 const MDNode *getMetadata() const { 645 assert(isMetadata() && "Wrong MachineOperand accessor"); 646 return Contents.MD; 647 } 648 649 //===--------------------------------------------------------------------===// 650 // Mutators for various operand types. 651 //===--------------------------------------------------------------------===// 652 setImm(int64_t immVal)653 void setImm(int64_t immVal) { 654 assert(isImm() && "Wrong MachineOperand mutator"); 655 Contents.ImmVal = immVal; 656 } 657 setCImm(const ConstantInt * CI)658 void setCImm(const ConstantInt *CI) { 659 assert(isCImm() && "Wrong MachineOperand mutator"); 660 Contents.CI = CI; 661 } 662 setFPImm(const ConstantFP * CFP)663 void setFPImm(const ConstantFP *CFP) { 664 assert(isFPImm() && "Wrong MachineOperand mutator"); 665 Contents.CFP = CFP; 666 } 667 setOffset(int64_t Offset)668 void setOffset(int64_t Offset) { 669 assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() || 670 isTargetIndex() || isBlockAddress()) && 671 "Wrong MachineOperand mutator"); 672 SmallContents.OffsetLo = unsigned(Offset); 673 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32); 674 } 675 setIndex(int Idx)676 void setIndex(int Idx) { 677 assert((isFI() || isCPI() || isTargetIndex() || isJTI()) && 678 "Wrong MachineOperand mutator"); 679 Contents.OffsetedInfo.Val.Index = Idx; 680 } 681 setMetadata(const MDNode * MD)682 void setMetadata(const MDNode *MD) { 683 assert(isMetadata() && "Wrong MachineOperand mutator"); 684 Contents.MD = MD; 685 } 686 setMBB(MachineBasicBlock * MBB)687 void setMBB(MachineBasicBlock *MBB) { 688 assert(isMBB() && "Wrong MachineOperand mutator"); 689 Contents.MBB = MBB; 690 } 691 692 /// Sets value of register mask operand referencing Mask. The 693 /// operand does not take ownership of the memory referenced by Mask, it must 694 /// remain valid for the lifetime of the operand. See CreateRegMask(). 695 /// Any physreg with a 0 bit in the mask is clobbered by the instruction. setRegMask(const uint32_t * RegMaskPtr)696 void setRegMask(const uint32_t *RegMaskPtr) { 697 assert(isRegMask() && "Wrong MachineOperand mutator"); 698 Contents.RegMask = RegMaskPtr; 699 } 700 setPredicate(unsigned Predicate)701 void setPredicate(unsigned Predicate) { 702 assert(isPredicate() && "Wrong MachineOperand mutator"); 703 Contents.Pred = Predicate; 704 } 705 706 //===--------------------------------------------------------------------===// 707 // Other methods. 708 //===--------------------------------------------------------------------===// 709 710 /// Returns true if this operand is identical to the specified operand except 711 /// for liveness related flags (isKill, isUndef and isDead). Note that this 712 /// should stay in sync with the hash_value overload below. 713 bool isIdenticalTo(const MachineOperand &Other) const; 714 715 /// MachineOperand hash_value overload. 716 /// 717 /// Note that this includes the same information in the hash that 718 /// isIdenticalTo uses for comparison. It is thus suited for use in hash 719 /// tables which use that function for equality comparisons only. This must 720 /// stay exactly in sync with isIdenticalTo above. 721 friend hash_code hash_value(const MachineOperand &MO); 722 723 /// ChangeToImmediate - Replace this operand with a new immediate operand of 724 /// the specified value. If an operand is known to be an immediate already, 725 /// the setImm method should be used. 726 void ChangeToImmediate(int64_t ImmVal); 727 728 /// ChangeToFPImmediate - Replace this operand with a new FP immediate operand 729 /// of the specified value. If an operand is known to be an FP immediate 730 /// already, the setFPImm method should be used. 731 void ChangeToFPImmediate(const ConstantFP *FPImm); 732 733 /// ChangeToES - Replace this operand with a new external symbol operand. 734 void ChangeToES(const char *SymName, unsigned TargetFlags = 0); 735 736 /// ChangeToGA - Replace this operand with a new global address operand. 737 void ChangeToGA(const GlobalValue *GV, int64_t Offset, 738 unsigned TargetFlags = 0); 739 740 /// ChangeToMCSymbol - Replace this operand with a new MC symbol operand. 741 void ChangeToMCSymbol(MCSymbol *Sym); 742 743 /// Replace this operand with a frame index. 744 void ChangeToFrameIndex(int Idx); 745 746 /// Replace this operand with a target index. 747 void ChangeToTargetIndex(unsigned Idx, int64_t Offset, 748 unsigned TargetFlags = 0); 749 750 /// ChangeToRegister - Replace this operand with a new register operand of 751 /// the specified value. If an operand is known to be an register already, 752 /// the setReg method should be used. 753 void ChangeToRegister(Register Reg, bool isDef, bool isImp = false, 754 bool isKill = false, bool isDead = false, 755 bool isUndef = false, bool isDebug = false); 756 757 //===--------------------------------------------------------------------===// 758 // Construction methods. 759 //===--------------------------------------------------------------------===// 760 CreateImm(int64_t Val)761 static MachineOperand CreateImm(int64_t Val) { 762 MachineOperand Op(MachineOperand::MO_Immediate); 763 Op.setImm(Val); 764 return Op; 765 } 766 CreateCImm(const ConstantInt * CI)767 static MachineOperand CreateCImm(const ConstantInt *CI) { 768 MachineOperand Op(MachineOperand::MO_CImmediate); 769 Op.Contents.CI = CI; 770 return Op; 771 } 772 CreateFPImm(const ConstantFP * CFP)773 static MachineOperand CreateFPImm(const ConstantFP *CFP) { 774 MachineOperand Op(MachineOperand::MO_FPImmediate); 775 Op.Contents.CFP = CFP; 776 return Op; 777 } 778 779 static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp = false, 780 bool isKill = false, bool isDead = false, 781 bool isUndef = false, 782 bool isEarlyClobber = false, 783 unsigned SubReg = 0, bool isDebug = false, 784 bool isInternalRead = false, 785 bool isRenamable = false) { 786 assert(!(isDead && !isDef) && "Dead flag on non-def"); 787 assert(!(isKill && isDef) && "Kill flag on def"); 788 MachineOperand Op(MachineOperand::MO_Register); 789 Op.IsDef = isDef; 790 Op.IsImp = isImp; 791 Op.IsDeadOrKill = isKill | isDead; 792 Op.IsRenamable = isRenamable; 793 Op.IsUndef = isUndef; 794 Op.IsInternalRead = isInternalRead; 795 Op.IsEarlyClobber = isEarlyClobber; 796 Op.TiedTo = 0; 797 Op.IsDebug = isDebug; 798 Op.SmallContents.RegNo = Reg; 799 Op.Contents.Reg.Prev = nullptr; 800 Op.Contents.Reg.Next = nullptr; 801 Op.setSubReg(SubReg); 802 return Op; 803 } 804 static MachineOperand CreateMBB(MachineBasicBlock *MBB, 805 unsigned TargetFlags = 0) { 806 MachineOperand Op(MachineOperand::MO_MachineBasicBlock); 807 Op.setMBB(MBB); 808 Op.setTargetFlags(TargetFlags); 809 return Op; 810 } CreateFI(int Idx)811 static MachineOperand CreateFI(int Idx) { 812 MachineOperand Op(MachineOperand::MO_FrameIndex); 813 Op.setIndex(Idx); 814 return Op; 815 } 816 static MachineOperand CreateCPI(unsigned Idx, int Offset, 817 unsigned TargetFlags = 0) { 818 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex); 819 Op.setIndex(Idx); 820 Op.setOffset(Offset); 821 Op.setTargetFlags(TargetFlags); 822 return Op; 823 } 824 static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset, 825 unsigned TargetFlags = 0) { 826 MachineOperand Op(MachineOperand::MO_TargetIndex); 827 Op.setIndex(Idx); 828 Op.setOffset(Offset); 829 Op.setTargetFlags(TargetFlags); 830 return Op; 831 } 832 static MachineOperand CreateJTI(unsigned Idx, unsigned TargetFlags = 0) { 833 MachineOperand Op(MachineOperand::MO_JumpTableIndex); 834 Op.setIndex(Idx); 835 Op.setTargetFlags(TargetFlags); 836 return Op; 837 } 838 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, 839 unsigned TargetFlags = 0) { 840 MachineOperand Op(MachineOperand::MO_GlobalAddress); 841 Op.Contents.OffsetedInfo.Val.GV = GV; 842 Op.setOffset(Offset); 843 Op.setTargetFlags(TargetFlags); 844 return Op; 845 } 846 static MachineOperand CreateES(const char *SymName, 847 unsigned TargetFlags = 0) { 848 MachineOperand Op(MachineOperand::MO_ExternalSymbol); 849 Op.Contents.OffsetedInfo.Val.SymbolName = SymName; 850 Op.setOffset(0); // Offset is always 0. 851 Op.setTargetFlags(TargetFlags); 852 return Op; 853 } 854 static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset, 855 unsigned TargetFlags = 0) { 856 MachineOperand Op(MachineOperand::MO_BlockAddress); 857 Op.Contents.OffsetedInfo.Val.BA = BA; 858 Op.setOffset(Offset); 859 Op.setTargetFlags(TargetFlags); 860 return Op; 861 } 862 /// CreateRegMask - Creates a register mask operand referencing Mask. The 863 /// operand does not take ownership of the memory referenced by Mask, it 864 /// must remain valid for the lifetime of the operand. 865 /// 866 /// A RegMask operand represents a set of non-clobbered physical registers 867 /// on an instruction that clobbers many registers, typically a call. The 868 /// bit mask has a bit set for each physreg that is preserved by this 869 /// instruction, as described in the documentation for 870 /// TargetRegisterInfo::getCallPreservedMask(). 871 /// 872 /// Any physreg with a 0 bit in the mask is clobbered by the instruction. 873 /// CreateRegMask(const uint32_t * Mask)874 static MachineOperand CreateRegMask(const uint32_t *Mask) { 875 assert(Mask && "Missing register mask"); 876 MachineOperand Op(MachineOperand::MO_RegisterMask); 877 Op.Contents.RegMask = Mask; 878 return Op; 879 } CreateRegLiveOut(const uint32_t * Mask)880 static MachineOperand CreateRegLiveOut(const uint32_t *Mask) { 881 assert(Mask && "Missing live-out register mask"); 882 MachineOperand Op(MachineOperand::MO_RegisterLiveOut); 883 Op.Contents.RegMask = Mask; 884 return Op; 885 } CreateMetadata(const MDNode * Meta)886 static MachineOperand CreateMetadata(const MDNode *Meta) { 887 MachineOperand Op(MachineOperand::MO_Metadata); 888 Op.Contents.MD = Meta; 889 return Op; 890 } 891 892 static MachineOperand CreateMCSymbol(MCSymbol *Sym, 893 unsigned TargetFlags = 0) { 894 MachineOperand Op(MachineOperand::MO_MCSymbol); 895 Op.Contents.Sym = Sym; 896 Op.setOffset(0); 897 Op.setTargetFlags(TargetFlags); 898 return Op; 899 } 900 CreateCFIIndex(unsigned CFIIndex)901 static MachineOperand CreateCFIIndex(unsigned CFIIndex) { 902 MachineOperand Op(MachineOperand::MO_CFIIndex); 903 Op.Contents.CFIIndex = CFIIndex; 904 return Op; 905 } 906 CreateIntrinsicID(Intrinsic::ID ID)907 static MachineOperand CreateIntrinsicID(Intrinsic::ID ID) { 908 MachineOperand Op(MachineOperand::MO_IntrinsicID); 909 Op.Contents.IntrinsicID = ID; 910 return Op; 911 } 912 CreatePredicate(unsigned Pred)913 static MachineOperand CreatePredicate(unsigned Pred) { 914 MachineOperand Op(MachineOperand::MO_Predicate); 915 Op.Contents.Pred = Pred; 916 return Op; 917 } 918 CreateShuffleMask(ArrayRef<int> Mask)919 static MachineOperand CreateShuffleMask(ArrayRef<int> Mask) { 920 MachineOperand Op(MachineOperand::MO_ShuffleMask); 921 Op.Contents.ShuffleMask = Mask; 922 return Op; 923 } 924 925 friend class MachineInstr; 926 friend class MachineRegisterInfo; 927 928 private: 929 // If this operand is currently a register operand, and if this is in a 930 // function, deregister the operand from the register's use/def list. 931 void removeRegFromUses(); 932 933 /// Artificial kinds for DenseMap usage. 934 enum : unsigned char { 935 MO_Empty = MO_Last + 1, 936 MO_Tombstone, 937 }; 938 939 friend struct DenseMapInfo<MachineOperand>; 940 941 //===--------------------------------------------------------------------===// 942 // Methods for handling register use/def lists. 943 //===--------------------------------------------------------------------===// 944 945 /// isOnRegUseList - Return true if this operand is on a register use/def 946 /// list or false if not. This can only be called for register operands 947 /// that are part of a machine instruction. 948 bool isOnRegUseList() const { 949 assert(isReg() && "Can only add reg operand to use lists"); 950 return Contents.Reg.Prev != nullptr; 951 } 952 }; 953 954 template <> struct DenseMapInfo<MachineOperand> { 955 static MachineOperand getEmptyKey() { 956 return MachineOperand(static_cast<MachineOperand::MachineOperandType>( 957 MachineOperand::MO_Empty)); 958 } 959 static MachineOperand getTombstoneKey() { 960 return MachineOperand(static_cast<MachineOperand::MachineOperandType>( 961 MachineOperand::MO_Tombstone)); 962 } 963 static unsigned getHashValue(const MachineOperand &MO) { 964 return hash_value(MO); 965 } 966 static bool isEqual(const MachineOperand &LHS, const MachineOperand &RHS) { 967 if (LHS.getType() == static_cast<MachineOperand::MachineOperandType>( 968 MachineOperand::MO_Empty) || 969 LHS.getType() == static_cast<MachineOperand::MachineOperandType>( 970 MachineOperand::MO_Tombstone)) 971 return LHS.getType() == RHS.getType(); 972 return LHS.isIdenticalTo(RHS); 973 } 974 }; 975 976 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand &MO) { 977 MO.print(OS); 978 return OS; 979 } 980 981 // See friend declaration above. This additional declaration is required in 982 // order to compile LLVM with IBM xlC compiler. 983 hash_code hash_value(const MachineOperand &MO); 984 } // namespace llvm 985 986 #endif 987