1 //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===// 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 /// \file Methods common to all machine operands. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineOperand.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/Analysis/Loads.h" 17 #include "llvm/CodeGen/MIRPrinter.h" 18 #include "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/CodeGen/MachineJumpTableInfo.h" 20 #include "llvm/CodeGen/MachineRegisterInfo.h" 21 #include "llvm/CodeGen/TargetInstrInfo.h" 22 #include "llvm/CodeGen/TargetRegisterInfo.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/IRPrintingPasses.h" 25 #include "llvm/IR/ModuleSlotTracker.h" 26 #include "llvm/Target/TargetIntrinsicInfo.h" 27 #include "llvm/Target/TargetMachine.h" 28 29 using namespace llvm; 30 31 static cl::opt<int> 32 PrintRegMaskNumRegs("print-regmask-num-regs", 33 cl::desc("Number of registers to limit to when " 34 "printing regmask operands in IR dumps. " 35 "unlimited = -1"), 36 cl::init(32), cl::Hidden); 37 38 static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) { 39 if (const MachineInstr *MI = MO.getParent()) 40 if (const MachineBasicBlock *MBB = MI->getParent()) 41 if (const MachineFunction *MF = MBB->getParent()) 42 return MF; 43 return nullptr; 44 } 45 static MachineFunction *getMFIfAvailable(MachineOperand &MO) { 46 return const_cast<MachineFunction *>( 47 getMFIfAvailable(const_cast<const MachineOperand &>(MO))); 48 } 49 50 void MachineOperand::setReg(unsigned Reg) { 51 if (getReg() == Reg) 52 return; // No change. 53 54 // Clear the IsRenamable bit to keep it conservatively correct. 55 IsRenamable = false; 56 57 // Otherwise, we have to change the register. If this operand is embedded 58 // into a machine function, we need to update the old and new register's 59 // use/def lists. 60 if (MachineFunction *MF = getMFIfAvailable(*this)) { 61 MachineRegisterInfo &MRI = MF->getRegInfo(); 62 MRI.removeRegOperandFromUseList(this); 63 SmallContents.RegNo = Reg; 64 MRI.addRegOperandToUseList(this); 65 return; 66 } 67 68 // Otherwise, just change the register, no problem. :) 69 SmallContents.RegNo = Reg; 70 } 71 72 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 73 const TargetRegisterInfo &TRI) { 74 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 75 if (SubIdx && getSubReg()) 76 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 77 setReg(Reg); 78 if (SubIdx) 79 setSubReg(SubIdx); 80 } 81 82 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 83 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 84 if (getSubReg()) { 85 Reg = TRI.getSubReg(Reg, getSubReg()); 86 // Note that getSubReg() may return 0 if the sub-register doesn't exist. 87 // That won't happen in legal code. 88 setSubReg(0); 89 if (isDef()) 90 setIsUndef(false); 91 } 92 setReg(Reg); 93 } 94 95 /// Change a def to a use, or a use to a def. 96 void MachineOperand::setIsDef(bool Val) { 97 assert(isReg() && "Wrong MachineOperand accessor"); 98 assert((!Val || !isDebug()) && "Marking a debug operation as def"); 99 if (IsDef == Val) 100 return; 101 assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported"); 102 // MRI may keep uses and defs in different list positions. 103 if (MachineFunction *MF = getMFIfAvailable(*this)) { 104 MachineRegisterInfo &MRI = MF->getRegInfo(); 105 MRI.removeRegOperandFromUseList(this); 106 IsDef = Val; 107 MRI.addRegOperandToUseList(this); 108 return; 109 } 110 IsDef = Val; 111 } 112 113 bool MachineOperand::isRenamable() const { 114 assert(isReg() && "Wrong MachineOperand accessor"); 115 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) && 116 "isRenamable should only be checked on physical registers"); 117 if (!IsRenamable) 118 return false; 119 120 const MachineInstr *MI = getParent(); 121 if (!MI) 122 return true; 123 124 if (isDef()) 125 return !MI->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle); 126 127 assert(isUse() && "Reg is not def or use"); 128 return !MI->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle); 129 } 130 131 void MachineOperand::setIsRenamable(bool Val) { 132 assert(isReg() && "Wrong MachineOperand accessor"); 133 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) && 134 "setIsRenamable should only be called on physical registers"); 135 IsRenamable = Val; 136 } 137 138 // If this operand is currently a register operand, and if this is in a 139 // function, deregister the operand from the register's use/def list. 140 void MachineOperand::removeRegFromUses() { 141 if (!isReg() || !isOnRegUseList()) 142 return; 143 144 if (MachineFunction *MF = getMFIfAvailable(*this)) 145 MF->getRegInfo().removeRegOperandFromUseList(this); 146 } 147 148 /// ChangeToImmediate - Replace this operand with a new immediate operand of 149 /// the specified value. If an operand is known to be an immediate already, 150 /// the setImm method should be used. 151 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 152 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 153 154 removeRegFromUses(); 155 156 OpKind = MO_Immediate; 157 Contents.ImmVal = ImmVal; 158 } 159 160 void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) { 161 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 162 163 removeRegFromUses(); 164 165 OpKind = MO_FPImmediate; 166 Contents.CFP = FPImm; 167 } 168 169 void MachineOperand::ChangeToES(const char *SymName, 170 unsigned char TargetFlags) { 171 assert((!isReg() || !isTied()) && 172 "Cannot change a tied operand into an external symbol"); 173 174 removeRegFromUses(); 175 176 OpKind = MO_ExternalSymbol; 177 Contents.OffsetedInfo.Val.SymbolName = SymName; 178 setOffset(0); // Offset is always 0. 179 setTargetFlags(TargetFlags); 180 } 181 182 void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) { 183 assert((!isReg() || !isTied()) && 184 "Cannot change a tied operand into an MCSymbol"); 185 186 removeRegFromUses(); 187 188 OpKind = MO_MCSymbol; 189 Contents.Sym = Sym; 190 } 191 192 void MachineOperand::ChangeToFrameIndex(int Idx) { 193 assert((!isReg() || !isTied()) && 194 "Cannot change a tied operand into a FrameIndex"); 195 196 removeRegFromUses(); 197 198 OpKind = MO_FrameIndex; 199 setIndex(Idx); 200 } 201 202 void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset, 203 unsigned char TargetFlags) { 204 assert((!isReg() || !isTied()) && 205 "Cannot change a tied operand into a FrameIndex"); 206 207 removeRegFromUses(); 208 209 OpKind = MO_TargetIndex; 210 setIndex(Idx); 211 setOffset(Offset); 212 setTargetFlags(TargetFlags); 213 } 214 215 /// ChangeToRegister - Replace this operand with a new register operand of 216 /// the specified value. If an operand is known to be an register already, 217 /// the setReg method should be used. 218 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 219 bool isKill, bool isDead, bool isUndef, 220 bool isDebug) { 221 MachineRegisterInfo *RegInfo = nullptr; 222 if (MachineFunction *MF = getMFIfAvailable(*this)) 223 RegInfo = &MF->getRegInfo(); 224 // If this operand is already a register operand, remove it from the 225 // register's use/def lists. 226 bool WasReg = isReg(); 227 if (RegInfo && WasReg) 228 RegInfo->removeRegOperandFromUseList(this); 229 230 // Change this to a register and set the reg#. 231 assert(!(isDead && !isDef) && "Dead flag on non-def"); 232 assert(!(isKill && isDef) && "Kill flag on def"); 233 OpKind = MO_Register; 234 SmallContents.RegNo = Reg; 235 SubReg_TargetFlags = 0; 236 IsDef = isDef; 237 IsImp = isImp; 238 IsDeadOrKill = isKill | isDead; 239 IsRenamable = false; 240 IsUndef = isUndef; 241 IsInternalRead = false; 242 IsEarlyClobber = false; 243 IsDebug = isDebug; 244 // Ensure isOnRegUseList() returns false. 245 Contents.Reg.Prev = nullptr; 246 // Preserve the tie when the operand was already a register. 247 if (!WasReg) 248 TiedTo = 0; 249 250 // If this operand is embedded in a function, add the operand to the 251 // register's use/def list. 252 if (RegInfo) 253 RegInfo->addRegOperandToUseList(this); 254 } 255 256 /// isIdenticalTo - Return true if this operand is identical to the specified 257 /// operand. Note that this should stay in sync with the hash_value overload 258 /// below. 259 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 260 if (getType() != Other.getType() || 261 getTargetFlags() != Other.getTargetFlags()) 262 return false; 263 264 switch (getType()) { 265 case MachineOperand::MO_Register: 266 return getReg() == Other.getReg() && isDef() == Other.isDef() && 267 getSubReg() == Other.getSubReg(); 268 case MachineOperand::MO_Immediate: 269 return getImm() == Other.getImm(); 270 case MachineOperand::MO_CImmediate: 271 return getCImm() == Other.getCImm(); 272 case MachineOperand::MO_FPImmediate: 273 return getFPImm() == Other.getFPImm(); 274 case MachineOperand::MO_MachineBasicBlock: 275 return getMBB() == Other.getMBB(); 276 case MachineOperand::MO_FrameIndex: 277 return getIndex() == Other.getIndex(); 278 case MachineOperand::MO_ConstantPoolIndex: 279 case MachineOperand::MO_TargetIndex: 280 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 281 case MachineOperand::MO_JumpTableIndex: 282 return getIndex() == Other.getIndex(); 283 case MachineOperand::MO_GlobalAddress: 284 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 285 case MachineOperand::MO_ExternalSymbol: 286 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 && 287 getOffset() == Other.getOffset(); 288 case MachineOperand::MO_BlockAddress: 289 return getBlockAddress() == Other.getBlockAddress() && 290 getOffset() == Other.getOffset(); 291 case MachineOperand::MO_RegisterMask: 292 case MachineOperand::MO_RegisterLiveOut: { 293 // Shallow compare of the two RegMasks 294 const uint32_t *RegMask = getRegMask(); 295 const uint32_t *OtherRegMask = Other.getRegMask(); 296 if (RegMask == OtherRegMask) 297 return true; 298 299 if (const MachineFunction *MF = getMFIfAvailable(*this)) { 300 // Calculate the size of the RegMask 301 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 302 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32; 303 304 // Deep compare of the two RegMasks 305 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask); 306 } 307 // We don't know the size of the RegMask, so we can't deep compare the two 308 // reg masks. 309 return false; 310 } 311 case MachineOperand::MO_MCSymbol: 312 return getMCSymbol() == Other.getMCSymbol(); 313 case MachineOperand::MO_CFIIndex: 314 return getCFIIndex() == Other.getCFIIndex(); 315 case MachineOperand::MO_Metadata: 316 return getMetadata() == Other.getMetadata(); 317 case MachineOperand::MO_IntrinsicID: 318 return getIntrinsicID() == Other.getIntrinsicID(); 319 case MachineOperand::MO_Predicate: 320 return getPredicate() == Other.getPredicate(); 321 } 322 llvm_unreachable("Invalid machine operand type"); 323 } 324 325 // Note: this must stay exactly in sync with isIdenticalTo above. 326 hash_code llvm::hash_value(const MachineOperand &MO) { 327 switch (MO.getType()) { 328 case MachineOperand::MO_Register: 329 // Register operands don't have target flags. 330 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef()); 331 case MachineOperand::MO_Immediate: 332 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); 333 case MachineOperand::MO_CImmediate: 334 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); 335 case MachineOperand::MO_FPImmediate: 336 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); 337 case MachineOperand::MO_MachineBasicBlock: 338 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); 339 case MachineOperand::MO_FrameIndex: 340 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 341 case MachineOperand::MO_ConstantPoolIndex: 342 case MachineOperand::MO_TargetIndex: 343 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), 344 MO.getOffset()); 345 case MachineOperand::MO_JumpTableIndex: 346 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 347 case MachineOperand::MO_ExternalSymbol: 348 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), 349 MO.getSymbolName()); 350 case MachineOperand::MO_GlobalAddress: 351 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), 352 MO.getOffset()); 353 case MachineOperand::MO_BlockAddress: 354 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(), 355 MO.getOffset()); 356 case MachineOperand::MO_RegisterMask: 357 case MachineOperand::MO_RegisterLiveOut: 358 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask()); 359 case MachineOperand::MO_Metadata: 360 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); 361 case MachineOperand::MO_MCSymbol: 362 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); 363 case MachineOperand::MO_CFIIndex: 364 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); 365 case MachineOperand::MO_IntrinsicID: 366 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID()); 367 case MachineOperand::MO_Predicate: 368 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate()); 369 } 370 llvm_unreachable("Invalid machine operand type"); 371 } 372 373 // Try to crawl up to the machine function and get TRI and IntrinsicInfo from 374 // it. 375 static void tryToGetTargetInfo(const MachineOperand &MO, 376 const TargetRegisterInfo *&TRI, 377 const TargetIntrinsicInfo *&IntrinsicInfo) { 378 if (const MachineFunction *MF = getMFIfAvailable(MO)) { 379 TRI = MF->getSubtarget().getRegisterInfo(); 380 IntrinsicInfo = MF->getTarget().getIntrinsicInfo(); 381 } 382 } 383 384 static const char *getTargetIndexName(const MachineFunction &MF, int Index) { 385 const auto *TII = MF.getSubtarget().getInstrInfo(); 386 assert(TII && "expected instruction info"); 387 auto Indices = TII->getSerializableTargetIndices(); 388 auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) { 389 return I.first == Index; 390 }); 391 if (Found != Indices.end()) 392 return Found->second; 393 return nullptr; 394 } 395 396 static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) { 397 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); 398 for (const auto &I : Flags) { 399 if (I.first == TF) { 400 return I.second; 401 } 402 } 403 return nullptr; 404 } 405 406 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS, 407 const TargetRegisterInfo *TRI) { 408 if (!TRI) { 409 OS << "%dwarfreg." << DwarfReg; 410 return; 411 } 412 413 int Reg = TRI->getLLVMRegNum(DwarfReg, true); 414 if (Reg == -1) { 415 OS << "<badreg>"; 416 return; 417 } 418 OS << printReg(Reg, TRI); 419 } 420 421 static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB, 422 ModuleSlotTracker &MST) { 423 OS << "%ir-block."; 424 if (BB.hasName()) { 425 printLLVMNameWithoutPrefix(OS, BB.getName()); 426 return; 427 } 428 Optional<int> Slot; 429 if (const Function *F = BB.getParent()) { 430 if (F == MST.getCurrentFunction()) { 431 Slot = MST.getLocalSlot(&BB); 432 } else if (const Module *M = F->getParent()) { 433 ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false); 434 CustomMST.incorporateFunction(*F); 435 Slot = CustomMST.getLocalSlot(&BB); 436 } 437 } 438 if (Slot) 439 MachineOperand::printIRSlotNumber(OS, *Slot); 440 else 441 OS << "<unknown>"; 442 } 443 444 static void printIRValueReference(raw_ostream &OS, const Value &V, 445 ModuleSlotTracker &MST) { 446 if (isa<GlobalValue>(V)) { 447 V.printAsOperand(OS, /*PrintType=*/false, MST); 448 return; 449 } 450 if (isa<Constant>(V)) { 451 // Machine memory operands can load/store to/from constant value pointers. 452 OS << '`'; 453 V.printAsOperand(OS, /*PrintType=*/true, MST); 454 OS << '`'; 455 return; 456 } 457 OS << "%ir."; 458 if (V.hasName()) { 459 printLLVMNameWithoutPrefix(OS, V.getName()); 460 return; 461 } 462 MachineOperand::printIRSlotNumber(OS, MST.getLocalSlot(&V)); 463 } 464 465 static void printSyncScope(raw_ostream &OS, const LLVMContext &Context, 466 SyncScope::ID SSID, 467 SmallVectorImpl<StringRef> &SSNs) { 468 switch (SSID) { 469 case SyncScope::System: 470 break; 471 default: 472 if (SSNs.empty()) 473 Context.getSyncScopeNames(SSNs); 474 475 OS << "syncscope(\""; 476 PrintEscapedString(SSNs[SSID], OS); 477 OS << "\") "; 478 break; 479 } 480 } 481 482 static const char *getTargetMMOFlagName(const TargetInstrInfo &TII, 483 unsigned TMMOFlag) { 484 auto Flags = TII.getSerializableMachineMemOperandTargetFlags(); 485 for (const auto &I : Flags) { 486 if (I.first == TMMOFlag) { 487 return I.second; 488 } 489 } 490 return nullptr; 491 } 492 493 static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed, 494 const MachineFrameInfo *MFI) { 495 StringRef Name; 496 if (MFI) { 497 IsFixed = MFI->isFixedObjectIndex(FrameIndex); 498 if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex)) 499 if (Alloca->hasName()) 500 Name = Alloca->getName(); 501 if (IsFixed) 502 FrameIndex -= MFI->getObjectIndexBegin(); 503 } 504 MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name); 505 } 506 507 void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index, 508 const TargetRegisterInfo *TRI) { 509 OS << "%subreg."; 510 if (TRI) 511 OS << TRI->getSubRegIndexName(Index); 512 else 513 OS << Index; 514 } 515 516 void MachineOperand::printTargetFlags(raw_ostream &OS, 517 const MachineOperand &Op) { 518 if (!Op.getTargetFlags()) 519 return; 520 const MachineFunction *MF = getMFIfAvailable(Op); 521 if (!MF) 522 return; 523 524 const auto *TII = MF->getSubtarget().getInstrInfo(); 525 assert(TII && "expected instruction info"); 526 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags()); 527 OS << "target-flags("; 528 const bool HasDirectFlags = Flags.first; 529 const bool HasBitmaskFlags = Flags.second; 530 if (!HasDirectFlags && !HasBitmaskFlags) { 531 OS << "<unknown>) "; 532 return; 533 } 534 if (HasDirectFlags) { 535 if (const auto *Name = getTargetFlagName(TII, Flags.first)) 536 OS << Name; 537 else 538 OS << "<unknown target flag>"; 539 } 540 if (!HasBitmaskFlags) { 541 OS << ") "; 542 return; 543 } 544 bool IsCommaNeeded = HasDirectFlags; 545 unsigned BitMask = Flags.second; 546 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags(); 547 for (const auto &Mask : BitMasks) { 548 // Check if the flag's bitmask has the bits of the current mask set. 549 if ((BitMask & Mask.first) == Mask.first) { 550 if (IsCommaNeeded) 551 OS << ", "; 552 IsCommaNeeded = true; 553 OS << Mask.second; 554 // Clear the bits which were serialized from the flag's bitmask. 555 BitMask &= ~(Mask.first); 556 } 557 } 558 if (BitMask) { 559 // When the resulting flag's bitmask isn't zero, we know that we didn't 560 // serialize all of the bit flags. 561 if (IsCommaNeeded) 562 OS << ", "; 563 OS << "<unknown bitmask target flag>"; 564 } 565 OS << ") "; 566 } 567 568 void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) { 569 OS << "<mcsymbol " << Sym << ">"; 570 } 571 572 void MachineOperand::printStackObjectReference(raw_ostream &OS, 573 unsigned FrameIndex, 574 bool IsFixed, StringRef Name) { 575 if (IsFixed) { 576 OS << "%fixed-stack." << FrameIndex; 577 return; 578 } 579 580 OS << "%stack." << FrameIndex; 581 if (!Name.empty()) 582 OS << '.' << Name; 583 } 584 585 void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) { 586 if (Offset == 0) 587 return; 588 if (Offset < 0) { 589 OS << " - " << -Offset; 590 return; 591 } 592 OS << " + " << Offset; 593 } 594 595 void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) { 596 if (Slot == -1) 597 OS << "<badref>"; 598 else 599 OS << Slot; 600 } 601 602 static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI, 603 const TargetRegisterInfo *TRI) { 604 switch (CFI.getOperation()) { 605 case MCCFIInstruction::OpSameValue: 606 OS << "same_value "; 607 if (MCSymbol *Label = CFI.getLabel()) 608 MachineOperand::printSymbol(OS, *Label); 609 printCFIRegister(CFI.getRegister(), OS, TRI); 610 break; 611 case MCCFIInstruction::OpRememberState: 612 OS << "remember_state "; 613 if (MCSymbol *Label = CFI.getLabel()) 614 MachineOperand::printSymbol(OS, *Label); 615 break; 616 case MCCFIInstruction::OpRestoreState: 617 OS << "restore_state "; 618 if (MCSymbol *Label = CFI.getLabel()) 619 MachineOperand::printSymbol(OS, *Label); 620 break; 621 case MCCFIInstruction::OpOffset: 622 OS << "offset "; 623 if (MCSymbol *Label = CFI.getLabel()) 624 MachineOperand::printSymbol(OS, *Label); 625 printCFIRegister(CFI.getRegister(), OS, TRI); 626 OS << ", " << CFI.getOffset(); 627 break; 628 case MCCFIInstruction::OpDefCfaRegister: 629 OS << "def_cfa_register "; 630 if (MCSymbol *Label = CFI.getLabel()) 631 MachineOperand::printSymbol(OS, *Label); 632 printCFIRegister(CFI.getRegister(), OS, TRI); 633 break; 634 case MCCFIInstruction::OpDefCfaOffset: 635 OS << "def_cfa_offset "; 636 if (MCSymbol *Label = CFI.getLabel()) 637 MachineOperand::printSymbol(OS, *Label); 638 OS << CFI.getOffset(); 639 break; 640 case MCCFIInstruction::OpDefCfa: 641 OS << "def_cfa "; 642 if (MCSymbol *Label = CFI.getLabel()) 643 MachineOperand::printSymbol(OS, *Label); 644 printCFIRegister(CFI.getRegister(), OS, TRI); 645 OS << ", " << CFI.getOffset(); 646 break; 647 case MCCFIInstruction::OpRelOffset: 648 OS << "rel_offset "; 649 if (MCSymbol *Label = CFI.getLabel()) 650 MachineOperand::printSymbol(OS, *Label); 651 printCFIRegister(CFI.getRegister(), OS, TRI); 652 OS << ", " << CFI.getOffset(); 653 break; 654 case MCCFIInstruction::OpAdjustCfaOffset: 655 OS << "adjust_cfa_offset "; 656 if (MCSymbol *Label = CFI.getLabel()) 657 MachineOperand::printSymbol(OS, *Label); 658 OS << CFI.getOffset(); 659 break; 660 case MCCFIInstruction::OpRestore: 661 OS << "restore "; 662 if (MCSymbol *Label = CFI.getLabel()) 663 MachineOperand::printSymbol(OS, *Label); 664 printCFIRegister(CFI.getRegister(), OS, TRI); 665 break; 666 case MCCFIInstruction::OpEscape: { 667 OS << "escape "; 668 if (MCSymbol *Label = CFI.getLabel()) 669 MachineOperand::printSymbol(OS, *Label); 670 if (!CFI.getValues().empty()) { 671 size_t e = CFI.getValues().size() - 1; 672 for (size_t i = 0; i < e; ++i) 673 OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", "; 674 OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", "; 675 } 676 break; 677 } 678 case MCCFIInstruction::OpUndefined: 679 OS << "undefined "; 680 if (MCSymbol *Label = CFI.getLabel()) 681 MachineOperand::printSymbol(OS, *Label); 682 printCFIRegister(CFI.getRegister(), OS, TRI); 683 break; 684 case MCCFIInstruction::OpRegister: 685 OS << "register "; 686 if (MCSymbol *Label = CFI.getLabel()) 687 MachineOperand::printSymbol(OS, *Label); 688 printCFIRegister(CFI.getRegister(), OS, TRI); 689 OS << ", "; 690 printCFIRegister(CFI.getRegister2(), OS, TRI); 691 break; 692 case MCCFIInstruction::OpWindowSave: 693 OS << "window_save "; 694 if (MCSymbol *Label = CFI.getLabel()) 695 MachineOperand::printSymbol(OS, *Label); 696 break; 697 default: 698 // TODO: Print the other CFI Operations. 699 OS << "<unserializable cfi directive>"; 700 break; 701 } 702 } 703 704 void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI, 705 const TargetIntrinsicInfo *IntrinsicInfo) const { 706 tryToGetTargetInfo(*this, TRI, IntrinsicInfo); 707 ModuleSlotTracker DummyMST(nullptr); 708 print(OS, DummyMST, LLT{}, /*PrintDef=*/false, /*IsStandalone=*/true, 709 /*ShouldPrintRegisterTies=*/true, 710 /*TiedOperandIdx=*/0, TRI, IntrinsicInfo); 711 } 712 713 void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST, 714 LLT TypeToPrint, bool PrintDef, bool IsStandalone, 715 bool ShouldPrintRegisterTies, 716 unsigned TiedOperandIdx, 717 const TargetRegisterInfo *TRI, 718 const TargetIntrinsicInfo *IntrinsicInfo) const { 719 printTargetFlags(OS, *this); 720 switch (getType()) { 721 case MachineOperand::MO_Register: { 722 unsigned Reg = getReg(); 723 if (isImplicit()) 724 OS << (isDef() ? "implicit-def " : "implicit "); 725 else if (PrintDef && isDef()) 726 // Print the 'def' flag only when the operand is defined after '='. 727 OS << "def "; 728 if (isInternalRead()) 729 OS << "internal "; 730 if (isDead()) 731 OS << "dead "; 732 if (isKill()) 733 OS << "killed "; 734 if (isUndef()) 735 OS << "undef "; 736 if (isEarlyClobber()) 737 OS << "early-clobber "; 738 if (isDebug()) 739 OS << "debug-use "; 740 if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable()) 741 OS << "renamable "; 742 OS << printReg(Reg, TRI); 743 // Print the sub register. 744 if (unsigned SubReg = getSubReg()) { 745 if (TRI) 746 OS << '.' << TRI->getSubRegIndexName(SubReg); 747 else 748 OS << ".subreg" << SubReg; 749 } 750 // Print the register class / bank. 751 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 752 if (const MachineFunction *MF = getMFIfAvailable(*this)) { 753 const MachineRegisterInfo &MRI = MF->getRegInfo(); 754 if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) { 755 OS << ':'; 756 OS << printRegClassOrBank(Reg, MRI, TRI); 757 } 758 } 759 } 760 // Print ties. 761 if (ShouldPrintRegisterTies && isTied() && !isDef()) 762 OS << "(tied-def " << TiedOperandIdx << ")"; 763 // Print types. 764 if (TypeToPrint.isValid()) 765 OS << '(' << TypeToPrint << ')'; 766 break; 767 } 768 case MachineOperand::MO_Immediate: 769 OS << getImm(); 770 break; 771 case MachineOperand::MO_CImmediate: 772 getCImm()->printAsOperand(OS, /*PrintType=*/true, MST); 773 break; 774 case MachineOperand::MO_FPImmediate: 775 getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST); 776 break; 777 case MachineOperand::MO_MachineBasicBlock: 778 OS << printMBBReference(*getMBB()); 779 break; 780 case MachineOperand::MO_FrameIndex: { 781 int FrameIndex = getIndex(); 782 bool IsFixed = false; 783 const MachineFrameInfo *MFI = nullptr; 784 if (const MachineFunction *MF = getMFIfAvailable(*this)) 785 MFI = &MF->getFrameInfo(); 786 printFrameIndex(OS, FrameIndex, IsFixed, MFI); 787 break; 788 } 789 case MachineOperand::MO_ConstantPoolIndex: 790 OS << "%const." << getIndex(); 791 printOperandOffset(OS, getOffset()); 792 break; 793 case MachineOperand::MO_TargetIndex: { 794 OS << "target-index("; 795 const char *Name = "<unknown>"; 796 if (const MachineFunction *MF = getMFIfAvailable(*this)) 797 if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex())) 798 Name = TargetIndexName; 799 OS << Name << ')'; 800 printOperandOffset(OS, getOffset()); 801 break; 802 } 803 case MachineOperand::MO_JumpTableIndex: 804 OS << printJumpTableEntryReference(getIndex()); 805 break; 806 case MachineOperand::MO_GlobalAddress: 807 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); 808 printOperandOffset(OS, getOffset()); 809 break; 810 case MachineOperand::MO_ExternalSymbol: { 811 StringRef Name = getSymbolName(); 812 OS << '&'; 813 if (Name.empty()) { 814 OS << "\"\""; 815 } else { 816 printLLVMNameWithoutPrefix(OS, Name); 817 } 818 printOperandOffset(OS, getOffset()); 819 break; 820 } 821 case MachineOperand::MO_BlockAddress: { 822 OS << "blockaddress("; 823 getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false, 824 MST); 825 OS << ", "; 826 printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST); 827 OS << ')'; 828 MachineOperand::printOperandOffset(OS, getOffset()); 829 break; 830 } 831 case MachineOperand::MO_RegisterMask: { 832 OS << "<regmask"; 833 if (TRI) { 834 unsigned NumRegsInMask = 0; 835 unsigned NumRegsEmitted = 0; 836 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) { 837 unsigned MaskWord = i / 32; 838 unsigned MaskBit = i % 32; 839 if (getRegMask()[MaskWord] & (1 << MaskBit)) { 840 if (PrintRegMaskNumRegs < 0 || 841 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) { 842 OS << " " << printReg(i, TRI); 843 NumRegsEmitted++; 844 } 845 NumRegsInMask++; 846 } 847 } 848 if (NumRegsEmitted != NumRegsInMask) 849 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more..."; 850 } else { 851 OS << " ..."; 852 } 853 OS << ">"; 854 break; 855 } 856 case MachineOperand::MO_RegisterLiveOut: { 857 const uint32_t *RegMask = getRegLiveOut(); 858 OS << "liveout("; 859 if (!TRI) { 860 OS << "<unknown>"; 861 } else { 862 bool IsCommaNeeded = false; 863 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) { 864 if (RegMask[Reg / 32] & (1U << (Reg % 32))) { 865 if (IsCommaNeeded) 866 OS << ", "; 867 OS << printReg(Reg, TRI); 868 IsCommaNeeded = true; 869 } 870 } 871 } 872 OS << ")"; 873 break; 874 } 875 case MachineOperand::MO_Metadata: 876 getMetadata()->printAsOperand(OS, MST); 877 break; 878 case MachineOperand::MO_MCSymbol: 879 printSymbol(OS, *getMCSymbol()); 880 break; 881 case MachineOperand::MO_CFIIndex: { 882 if (const MachineFunction *MF = getMFIfAvailable(*this)) 883 printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI); 884 else 885 OS << "<cfi directive>"; 886 break; 887 } 888 case MachineOperand::MO_IntrinsicID: { 889 Intrinsic::ID ID = getIntrinsicID(); 890 if (ID < Intrinsic::num_intrinsics) 891 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')'; 892 else if (IntrinsicInfo) 893 OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')'; 894 else 895 OS << "intrinsic(" << ID << ')'; 896 break; 897 } 898 case MachineOperand::MO_Predicate: { 899 auto Pred = static_cast<CmpInst::Predicate>(getPredicate()); 900 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred(" 901 << CmpInst::getPredicateName(Pred) << ')'; 902 break; 903 } 904 } 905 } 906 907 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 908 LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; } 909 #endif 910 911 //===----------------------------------------------------------------------===// 912 // MachineMemOperand Implementation 913 //===----------------------------------------------------------------------===// 914 915 /// getAddrSpace - Return the LLVM IR address space number that this pointer 916 /// points into. 917 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; } 918 919 /// isDereferenceable - Return true if V is always dereferenceable for 920 /// Offset + Size byte. 921 bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C, 922 const DataLayout &DL) const { 923 if (!V.is<const Value *>()) 924 return false; 925 926 const Value *BasePtr = V.get<const Value *>(); 927 if (BasePtr == nullptr) 928 return false; 929 930 return isDereferenceableAndAlignedPointer( 931 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL); 932 } 933 934 /// getConstantPool - Return a MachinePointerInfo record that refers to the 935 /// constant pool. 936 MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) { 937 return MachinePointerInfo(MF.getPSVManager().getConstantPool()); 938 } 939 940 /// getFixedStack - Return a MachinePointerInfo record that refers to the 941 /// the specified FrameIndex. 942 MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF, 943 int FI, int64_t Offset) { 944 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset); 945 } 946 947 MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) { 948 return MachinePointerInfo(MF.getPSVManager().getJumpTable()); 949 } 950 951 MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) { 952 return MachinePointerInfo(MF.getPSVManager().getGOT()); 953 } 954 955 MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF, 956 int64_t Offset, uint8_t ID) { 957 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID); 958 } 959 960 MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) { 961 return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace()); 962 } 963 964 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f, 965 uint64_t s, unsigned int a, 966 const AAMDNodes &AAInfo, 967 const MDNode *Ranges, SyncScope::ID SSID, 968 AtomicOrdering Ordering, 969 AtomicOrdering FailureOrdering) 970 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1), 971 AAInfo(AAInfo), Ranges(Ranges) { 972 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() || 973 isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) && 974 "invalid pointer value"); 975 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 976 assert((isLoad() || isStore()) && "Not a load/store!"); 977 978 AtomicInfo.SSID = static_cast<unsigned>(SSID); 979 assert(getSyncScopeID() == SSID && "Value truncated"); 980 AtomicInfo.Ordering = static_cast<unsigned>(Ordering); 981 assert(getOrdering() == Ordering && "Value truncated"); 982 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering); 983 assert(getFailureOrdering() == FailureOrdering && "Value truncated"); 984 } 985 986 /// Profile - Gather unique data for the object. 987 /// 988 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 989 ID.AddInteger(getOffset()); 990 ID.AddInteger(Size); 991 ID.AddPointer(getOpaqueValue()); 992 ID.AddInteger(getFlags()); 993 ID.AddInteger(getBaseAlignment()); 994 } 995 996 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 997 // The Value and Offset may differ due to CSE. But the flags and size 998 // should be the same. 999 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 1000 assert(MMO->getSize() == getSize() && "Size mismatch!"); 1001 1002 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 1003 // Update the alignment value. 1004 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1; 1005 // Also update the base and offset, because the new alignment may 1006 // not be applicable with the old ones. 1007 PtrInfo = MMO->PtrInfo; 1008 } 1009 } 1010 1011 /// getAlignment - Return the minimum known alignment in bytes of the 1012 /// actual memory reference. 1013 uint64_t MachineMemOperand::getAlignment() const { 1014 return MinAlign(getBaseAlignment(), getOffset()); 1015 } 1016 1017 void MachineMemOperand::print(raw_ostream &OS) const { 1018 ModuleSlotTracker DummyMST(nullptr); 1019 print(OS, DummyMST); 1020 } 1021 1022 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const { 1023 SmallVector<StringRef, 0> SSNs; 1024 LLVMContext Ctx; 1025 print(OS, MST, SSNs, Ctx, nullptr, nullptr); 1026 } 1027 1028 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST, 1029 SmallVectorImpl<StringRef> &SSNs, 1030 const LLVMContext &Context, 1031 const MachineFrameInfo *MFI, 1032 const TargetInstrInfo *TII) const { 1033 OS << '('; 1034 if (isVolatile()) 1035 OS << "volatile "; 1036 if (isNonTemporal()) 1037 OS << "non-temporal "; 1038 if (isDereferenceable()) 1039 OS << "dereferenceable "; 1040 if (isInvariant()) 1041 OS << "invariant "; 1042 if (getFlags() & MachineMemOperand::MOTargetFlag1) 1043 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1) 1044 << "\" "; 1045 if (getFlags() & MachineMemOperand::MOTargetFlag2) 1046 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2) 1047 << "\" "; 1048 if (getFlags() & MachineMemOperand::MOTargetFlag3) 1049 OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3) 1050 << "\" "; 1051 1052 assert((isLoad() || isStore()) && 1053 "machine memory operand must be a load or store (or both)"); 1054 if (isLoad()) 1055 OS << "load "; 1056 if (isStore()) 1057 OS << "store "; 1058 1059 printSyncScope(OS, Context, getSyncScopeID(), SSNs); 1060 1061 if (getOrdering() != AtomicOrdering::NotAtomic) 1062 OS << toIRString(getOrdering()) << ' '; 1063 if (getFailureOrdering() != AtomicOrdering::NotAtomic) 1064 OS << toIRString(getFailureOrdering()) << ' '; 1065 1066 OS << getSize(); 1067 if (const Value *Val = getValue()) { 1068 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into "); 1069 printIRValueReference(OS, *Val, MST); 1070 } else if (const PseudoSourceValue *PVal = getPseudoValue()) { 1071 OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into "); 1072 assert(PVal && "Expected a pseudo source value"); 1073 switch (PVal->kind()) { 1074 case PseudoSourceValue::Stack: 1075 OS << "stack"; 1076 break; 1077 case PseudoSourceValue::GOT: 1078 OS << "got"; 1079 break; 1080 case PseudoSourceValue::JumpTable: 1081 OS << "jump-table"; 1082 break; 1083 case PseudoSourceValue::ConstantPool: 1084 OS << "constant-pool"; 1085 break; 1086 case PseudoSourceValue::FixedStack: { 1087 int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); 1088 bool IsFixed = true; 1089 printFrameIndex(OS, FrameIndex, IsFixed, MFI); 1090 break; 1091 } 1092 case PseudoSourceValue::GlobalValueCallEntry: 1093 OS << "call-entry "; 1094 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand( 1095 OS, /*PrintType=*/false, MST); 1096 break; 1097 case PseudoSourceValue::ExternalSymbolCallEntry: 1098 OS << "call-entry &"; 1099 printLLVMNameWithoutPrefix( 1100 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol()); 1101 break; 1102 case PseudoSourceValue::TargetCustom: 1103 llvm_unreachable("TargetCustom pseudo source values are not supported"); 1104 break; 1105 } 1106 } 1107 MachineOperand::printOperandOffset(OS, getOffset()); 1108 if (getBaseAlignment() != getSize()) 1109 OS << ", align " << getBaseAlignment(); 1110 auto AAInfo = getAAInfo(); 1111 if (AAInfo.TBAA) { 1112 OS << ", !tbaa "; 1113 AAInfo.TBAA->printAsOperand(OS, MST); 1114 } 1115 if (AAInfo.Scope) { 1116 OS << ", !alias.scope "; 1117 AAInfo.Scope->printAsOperand(OS, MST); 1118 } 1119 if (AAInfo.NoAlias) { 1120 OS << ", !noalias "; 1121 AAInfo.NoAlias->printAsOperand(OS, MST); 1122 } 1123 if (getRanges()) { 1124 OS << ", !range "; 1125 getRanges()->printAsOperand(OS, MST); 1126 } 1127 // FIXME: Implement addrspace printing/parsing in MIR. 1128 // For now, print this even though parsing it is not available in MIR. 1129 if (unsigned AS = getAddrSpace()) 1130 OS << ", addrspace " << AS; 1131 1132 OS << ')'; 1133 } 1134