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/Analysis/Loads.h" 16 #include "llvm/CodeGen/MIRPrinter.h" 17 #include "llvm/CodeGen/MachineJumpTableInfo.h" 18 #include "llvm/CodeGen/MachineRegisterInfo.h" 19 #include "llvm/CodeGen/TargetInstrInfo.h" 20 #include "llvm/CodeGen/TargetRegisterInfo.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/ModuleSlotTracker.h" 23 #include "llvm/Target/TargetIntrinsicInfo.h" 24 #include "llvm/Target/TargetMachine.h" 25 26 using namespace llvm; 27 28 static cl::opt<int> 29 PrintRegMaskNumRegs("print-regmask-num-regs", 30 cl::desc("Number of registers to limit to when " 31 "printing regmask operands in IR dumps. " 32 "unlimited = -1"), 33 cl::init(32), cl::Hidden); 34 35 static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) { 36 if (const MachineInstr *MI = MO.getParent()) 37 if (const MachineBasicBlock *MBB = MI->getParent()) 38 if (const MachineFunction *MF = MBB->getParent()) 39 return MF; 40 return nullptr; 41 } 42 static MachineFunction *getMFIfAvailable(MachineOperand &MO) { 43 return const_cast<MachineFunction *>( 44 getMFIfAvailable(const_cast<const MachineOperand &>(MO))); 45 } 46 47 void MachineOperand::setReg(unsigned Reg) { 48 if (getReg() == Reg) 49 return; // No change. 50 51 // Otherwise, we have to change the register. If this operand is embedded 52 // into a machine function, we need to update the old and new register's 53 // use/def lists. 54 if (MachineFunction *MF = getMFIfAvailable(*this)) { 55 MachineRegisterInfo &MRI = MF->getRegInfo(); 56 MRI.removeRegOperandFromUseList(this); 57 SmallContents.RegNo = Reg; 58 MRI.addRegOperandToUseList(this); 59 return; 60 } 61 62 // Otherwise, just change the register, no problem. :) 63 SmallContents.RegNo = Reg; 64 } 65 66 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 67 const TargetRegisterInfo &TRI) { 68 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 69 if (SubIdx && getSubReg()) 70 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 71 setReg(Reg); 72 if (SubIdx) 73 setSubReg(SubIdx); 74 } 75 76 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 77 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 78 if (getSubReg()) { 79 Reg = TRI.getSubReg(Reg, getSubReg()); 80 // Note that getSubReg() may return 0 if the sub-register doesn't exist. 81 // That won't happen in legal code. 82 setSubReg(0); 83 if (isDef()) 84 setIsUndef(false); 85 } 86 setReg(Reg); 87 } 88 89 /// Change a def to a use, or a use to a def. 90 void MachineOperand::setIsDef(bool Val) { 91 assert(isReg() && "Wrong MachineOperand accessor"); 92 assert((!Val || !isDebug()) && "Marking a debug operation as def"); 93 if (IsDef == Val) 94 return; 95 assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported"); 96 // MRI may keep uses and defs in different list positions. 97 if (MachineFunction *MF = getMFIfAvailable(*this)) { 98 MachineRegisterInfo &MRI = MF->getRegInfo(); 99 MRI.removeRegOperandFromUseList(this); 100 IsDef = Val; 101 MRI.addRegOperandToUseList(this); 102 return; 103 } 104 IsDef = Val; 105 } 106 107 bool MachineOperand::isRenamable() const { 108 assert(isReg() && "Wrong MachineOperand accessor"); 109 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) && 110 "isRenamable should only be checked on physical registers"); 111 return IsRenamable; 112 } 113 114 void MachineOperand::setIsRenamable(bool Val) { 115 assert(isReg() && "Wrong MachineOperand accessor"); 116 assert(TargetRegisterInfo::isPhysicalRegister(getReg()) && 117 "setIsRenamable should only be called on physical registers"); 118 if (const MachineInstr *MI = getParent()) 119 if ((isDef() && MI->hasExtraDefRegAllocReq()) || 120 (isUse() && MI->hasExtraSrcRegAllocReq())) 121 assert(!Val && "isRenamable should be false for " 122 "hasExtraDefRegAllocReq/hasExtraSrcRegAllocReq opcodes"); 123 IsRenamable = Val; 124 } 125 126 void MachineOperand::setIsRenamableIfNoExtraRegAllocReq() { 127 if (const MachineInstr *MI = getParent()) 128 if ((isDef() && MI->hasExtraDefRegAllocReq()) || 129 (isUse() && MI->hasExtraSrcRegAllocReq())) 130 return; 131 132 setIsRenamable(true); 133 } 134 135 // If this operand is currently a register operand, and if this is in a 136 // function, deregister the operand from the register's use/def list. 137 void MachineOperand::removeRegFromUses() { 138 if (!isReg() || !isOnRegUseList()) 139 return; 140 141 if (MachineFunction *MF = getMFIfAvailable(*this)) 142 MF->getRegInfo().removeRegOperandFromUseList(this); 143 } 144 145 /// ChangeToImmediate - Replace this operand with a new immediate operand of 146 /// the specified value. If an operand is known to be an immediate already, 147 /// the setImm method should be used. 148 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 149 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 150 151 removeRegFromUses(); 152 153 OpKind = MO_Immediate; 154 Contents.ImmVal = ImmVal; 155 } 156 157 void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) { 158 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 159 160 removeRegFromUses(); 161 162 OpKind = MO_FPImmediate; 163 Contents.CFP = FPImm; 164 } 165 166 void MachineOperand::ChangeToES(const char *SymName, 167 unsigned char TargetFlags) { 168 assert((!isReg() || !isTied()) && 169 "Cannot change a tied operand into an external symbol"); 170 171 removeRegFromUses(); 172 173 OpKind = MO_ExternalSymbol; 174 Contents.OffsetedInfo.Val.SymbolName = SymName; 175 setOffset(0); // Offset is always 0. 176 setTargetFlags(TargetFlags); 177 } 178 179 void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) { 180 assert((!isReg() || !isTied()) && 181 "Cannot change a tied operand into an MCSymbol"); 182 183 removeRegFromUses(); 184 185 OpKind = MO_MCSymbol; 186 Contents.Sym = Sym; 187 } 188 189 void MachineOperand::ChangeToFrameIndex(int Idx) { 190 assert((!isReg() || !isTied()) && 191 "Cannot change a tied operand into a FrameIndex"); 192 193 removeRegFromUses(); 194 195 OpKind = MO_FrameIndex; 196 setIndex(Idx); 197 } 198 199 void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset, 200 unsigned char TargetFlags) { 201 assert((!isReg() || !isTied()) && 202 "Cannot change a tied operand into a FrameIndex"); 203 204 removeRegFromUses(); 205 206 OpKind = MO_TargetIndex; 207 setIndex(Idx); 208 setOffset(Offset); 209 setTargetFlags(TargetFlags); 210 } 211 212 /// ChangeToRegister - Replace this operand with a new register operand of 213 /// the specified value. If an operand is known to be an register already, 214 /// the setReg method should be used. 215 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 216 bool isKill, bool isDead, bool isUndef, 217 bool isDebug) { 218 MachineRegisterInfo *RegInfo = nullptr; 219 if (MachineFunction *MF = getMFIfAvailable(*this)) 220 RegInfo = &MF->getRegInfo(); 221 // If this operand is already a register operand, remove it from the 222 // register's use/def lists. 223 bool WasReg = isReg(); 224 if (RegInfo && WasReg) 225 RegInfo->removeRegOperandFromUseList(this); 226 227 // Change this to a register and set the reg#. 228 assert(!(isDead && !isDef) && "Dead flag on non-def"); 229 assert(!(isKill && isDef) && "Kill flag on def"); 230 OpKind = MO_Register; 231 SmallContents.RegNo = Reg; 232 SubReg_TargetFlags = 0; 233 IsDef = isDef; 234 IsImp = isImp; 235 IsDeadOrKill = isKill | isDead; 236 IsRenamable = false; 237 IsUndef = isUndef; 238 IsInternalRead = false; 239 IsEarlyClobber = false; 240 IsDebug = isDebug; 241 // Ensure isOnRegUseList() returns false. 242 Contents.Reg.Prev = nullptr; 243 // Preserve the tie when the operand was already a register. 244 if (!WasReg) 245 TiedTo = 0; 246 247 // If this operand is embedded in a function, add the operand to the 248 // register's use/def list. 249 if (RegInfo) 250 RegInfo->addRegOperandToUseList(this); 251 } 252 253 /// isIdenticalTo - Return true if this operand is identical to the specified 254 /// operand. Note that this should stay in sync with the hash_value overload 255 /// below. 256 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 257 if (getType() != Other.getType() || 258 getTargetFlags() != Other.getTargetFlags()) 259 return false; 260 261 switch (getType()) { 262 case MachineOperand::MO_Register: 263 return getReg() == Other.getReg() && isDef() == Other.isDef() && 264 getSubReg() == Other.getSubReg(); 265 case MachineOperand::MO_Immediate: 266 return getImm() == Other.getImm(); 267 case MachineOperand::MO_CImmediate: 268 return getCImm() == Other.getCImm(); 269 case MachineOperand::MO_FPImmediate: 270 return getFPImm() == Other.getFPImm(); 271 case MachineOperand::MO_MachineBasicBlock: 272 return getMBB() == Other.getMBB(); 273 case MachineOperand::MO_FrameIndex: 274 return getIndex() == Other.getIndex(); 275 case MachineOperand::MO_ConstantPoolIndex: 276 case MachineOperand::MO_TargetIndex: 277 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 278 case MachineOperand::MO_JumpTableIndex: 279 return getIndex() == Other.getIndex(); 280 case MachineOperand::MO_GlobalAddress: 281 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 282 case MachineOperand::MO_ExternalSymbol: 283 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 && 284 getOffset() == Other.getOffset(); 285 case MachineOperand::MO_BlockAddress: 286 return getBlockAddress() == Other.getBlockAddress() && 287 getOffset() == Other.getOffset(); 288 case MachineOperand::MO_RegisterMask: 289 case MachineOperand::MO_RegisterLiveOut: { 290 // Shallow compare of the two RegMasks 291 const uint32_t *RegMask = getRegMask(); 292 const uint32_t *OtherRegMask = Other.getRegMask(); 293 if (RegMask == OtherRegMask) 294 return true; 295 296 if (const MachineFunction *MF = getMFIfAvailable(*this)) { 297 // Calculate the size of the RegMask 298 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 299 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32; 300 301 // Deep compare of the two RegMasks 302 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask); 303 } 304 // We don't know the size of the RegMask, so we can't deep compare the two 305 // reg masks. 306 return false; 307 } 308 case MachineOperand::MO_MCSymbol: 309 return getMCSymbol() == Other.getMCSymbol(); 310 case MachineOperand::MO_CFIIndex: 311 return getCFIIndex() == Other.getCFIIndex(); 312 case MachineOperand::MO_Metadata: 313 return getMetadata() == Other.getMetadata(); 314 case MachineOperand::MO_IntrinsicID: 315 return getIntrinsicID() == Other.getIntrinsicID(); 316 case MachineOperand::MO_Predicate: 317 return getPredicate() == Other.getPredicate(); 318 } 319 llvm_unreachable("Invalid machine operand type"); 320 } 321 322 // Note: this must stay exactly in sync with isIdenticalTo above. 323 hash_code llvm::hash_value(const MachineOperand &MO) { 324 switch (MO.getType()) { 325 case MachineOperand::MO_Register: 326 // Register operands don't have target flags. 327 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef()); 328 case MachineOperand::MO_Immediate: 329 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); 330 case MachineOperand::MO_CImmediate: 331 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); 332 case MachineOperand::MO_FPImmediate: 333 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); 334 case MachineOperand::MO_MachineBasicBlock: 335 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); 336 case MachineOperand::MO_FrameIndex: 337 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 338 case MachineOperand::MO_ConstantPoolIndex: 339 case MachineOperand::MO_TargetIndex: 340 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), 341 MO.getOffset()); 342 case MachineOperand::MO_JumpTableIndex: 343 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 344 case MachineOperand::MO_ExternalSymbol: 345 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), 346 MO.getSymbolName()); 347 case MachineOperand::MO_GlobalAddress: 348 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), 349 MO.getOffset()); 350 case MachineOperand::MO_BlockAddress: 351 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(), 352 MO.getOffset()); 353 case MachineOperand::MO_RegisterMask: 354 case MachineOperand::MO_RegisterLiveOut: 355 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask()); 356 case MachineOperand::MO_Metadata: 357 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); 358 case MachineOperand::MO_MCSymbol: 359 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); 360 case MachineOperand::MO_CFIIndex: 361 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); 362 case MachineOperand::MO_IntrinsicID: 363 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID()); 364 case MachineOperand::MO_Predicate: 365 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate()); 366 } 367 llvm_unreachable("Invalid machine operand type"); 368 } 369 370 // Try to crawl up to the machine function and get TRI and IntrinsicInfo from 371 // it. 372 static void tryToGetTargetInfo(const MachineOperand &MO, 373 const TargetRegisterInfo *&TRI, 374 const TargetIntrinsicInfo *&IntrinsicInfo) { 375 if (const MachineFunction *MF = getMFIfAvailable(MO)) { 376 TRI = MF->getSubtarget().getRegisterInfo(); 377 IntrinsicInfo = MF->getTarget().getIntrinsicInfo(); 378 } 379 } 380 381 static void printOffset(raw_ostream &OS, int64_t Offset) { 382 if (Offset == 0) 383 return; 384 if (Offset < 0) { 385 OS << " - " << -Offset; 386 return; 387 } 388 OS << " + " << Offset; 389 } 390 391 static const char *getTargetIndexName(const MachineFunction &MF, int Index) { 392 const auto *TII = MF.getSubtarget().getInstrInfo(); 393 assert(TII && "expected instruction info"); 394 auto Indices = TII->getSerializableTargetIndices(); 395 auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) { 396 return I.first == Index; 397 }); 398 if (Found != Indices.end()) 399 return Found->second; 400 return nullptr; 401 } 402 403 void MachineOperand::printSubregIdx(raw_ostream &OS, uint64_t Index, 404 const TargetRegisterInfo *TRI) { 405 OS << "%subreg."; 406 if (TRI) 407 OS << TRI->getSubRegIndexName(Index); 408 else 409 OS << Index; 410 } 411 412 void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI, 413 const TargetIntrinsicInfo *IntrinsicInfo) const { 414 tryToGetTargetInfo(*this, TRI, IntrinsicInfo); 415 ModuleSlotTracker DummyMST(nullptr); 416 print(OS, DummyMST, LLT{}, /*PrintDef=*/false, 417 /*ShouldPrintRegisterTies=*/true, 418 /*TiedOperandIdx=*/0, TRI, IntrinsicInfo); 419 } 420 421 void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST, 422 LLT TypeToPrint, bool PrintDef, 423 bool ShouldPrintRegisterTies, 424 unsigned TiedOperandIdx, 425 const TargetRegisterInfo *TRI, 426 const TargetIntrinsicInfo *IntrinsicInfo) const { 427 switch (getType()) { 428 case MachineOperand::MO_Register: { 429 unsigned Reg = getReg(); 430 if (isImplicit()) 431 OS << (isDef() ? "implicit-def " : "implicit "); 432 else if (PrintDef && isDef()) 433 // Print the 'def' flag only when the operand is defined after '='. 434 OS << "def "; 435 if (isInternalRead()) 436 OS << "internal "; 437 if (isDead()) 438 OS << "dead "; 439 if (isKill()) 440 OS << "killed "; 441 if (isUndef()) 442 OS << "undef "; 443 if (isEarlyClobber()) 444 OS << "early-clobber "; 445 if (isDebug()) 446 OS << "debug-use "; 447 if (TargetRegisterInfo::isPhysicalRegister(getReg()) && isRenamable()) 448 OS << "renamable "; 449 OS << printReg(Reg, TRI); 450 // Print the sub register. 451 if (unsigned SubReg = getSubReg()) { 452 if (TRI) 453 OS << '.' << TRI->getSubRegIndexName(SubReg); 454 else 455 OS << ".subreg" << SubReg; 456 } 457 // Print the register class / bank. 458 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 459 if (const MachineFunction *MF = getMFIfAvailable(*this)) { 460 const MachineRegisterInfo &MRI = MF->getRegInfo(); 461 if (!PrintDef || MRI.def_empty(Reg)) { 462 OS << ':'; 463 OS << printRegClassOrBank(Reg, MRI, TRI); 464 } 465 } 466 } 467 // Print ties. 468 if (ShouldPrintRegisterTies && isTied() && !isDef()) 469 OS << "(tied-def " << TiedOperandIdx << ")"; 470 // Print types. 471 if (TypeToPrint.isValid()) 472 OS << '(' << TypeToPrint << ')'; 473 break; 474 } 475 case MachineOperand::MO_Immediate: 476 OS << getImm(); 477 break; 478 case MachineOperand::MO_CImmediate: 479 getCImm()->printAsOperand(OS, /*PrintType=*/true, MST); 480 break; 481 case MachineOperand::MO_FPImmediate: 482 if (getFPImm()->getType()->isFloatTy()) { 483 OS << getFPImm()->getValueAPF().convertToFloat(); 484 } else if (getFPImm()->getType()->isHalfTy()) { 485 APFloat APF = getFPImm()->getValueAPF(); 486 bool Unused; 487 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused); 488 OS << "half " << APF.convertToFloat(); 489 } else if (getFPImm()->getType()->isFP128Ty()) { 490 APFloat APF = getFPImm()->getValueAPF(); 491 SmallString<16> Str; 492 getFPImm()->getValueAPF().toString(Str); 493 OS << "quad " << Str; 494 } else if (getFPImm()->getType()->isX86_FP80Ty()) { 495 APFloat APF = getFPImm()->getValueAPF(); 496 OS << "x86_fp80 0xK"; 497 APInt API = APF.bitcastToAPInt(); 498 OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4, 499 /*Upper=*/true); 500 OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 501 /*Upper=*/true); 502 } else { 503 OS << getFPImm()->getValueAPF().convertToDouble(); 504 } 505 break; 506 case MachineOperand::MO_MachineBasicBlock: 507 OS << printMBBReference(*getMBB()); 508 break; 509 case MachineOperand::MO_FrameIndex: 510 OS << "<fi#" << getIndex() << '>'; 511 break; 512 case MachineOperand::MO_ConstantPoolIndex: 513 OS << "%const." << getIndex(); 514 printOffset(OS, getOffset()); 515 break; 516 case MachineOperand::MO_TargetIndex: { 517 OS << "target-index("; 518 const char *Name = "<unknown>"; 519 if (const MachineFunction *MF = getMFIfAvailable(*this)) 520 if (const auto *TargetIndexName = getTargetIndexName(*MF, getIndex())) 521 Name = TargetIndexName; 522 OS << Name << ')'; 523 printOffset(OS, getOffset()); 524 break; 525 } 526 case MachineOperand::MO_JumpTableIndex: 527 OS << printJumpTableEntryReference(getIndex()); 528 break; 529 case MachineOperand::MO_GlobalAddress: 530 OS << "<ga:"; 531 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); 532 if (getOffset()) 533 OS << "+" << getOffset(); 534 OS << '>'; 535 break; 536 case MachineOperand::MO_ExternalSymbol: 537 OS << "<es:" << getSymbolName(); 538 if (getOffset()) 539 OS << "+" << getOffset(); 540 OS << '>'; 541 break; 542 case MachineOperand::MO_BlockAddress: 543 OS << '<'; 544 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST); 545 if (getOffset()) 546 OS << "+" << getOffset(); 547 OS << '>'; 548 break; 549 case MachineOperand::MO_RegisterMask: { 550 OS << "<regmask"; 551 if (TRI) { 552 unsigned NumRegsInMask = 0; 553 unsigned NumRegsEmitted = 0; 554 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) { 555 unsigned MaskWord = i / 32; 556 unsigned MaskBit = i % 32; 557 if (getRegMask()[MaskWord] & (1 << MaskBit)) { 558 if (PrintRegMaskNumRegs < 0 || 559 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) { 560 OS << " " << printReg(i, TRI); 561 NumRegsEmitted++; 562 } 563 NumRegsInMask++; 564 } 565 } 566 if (NumRegsEmitted != NumRegsInMask) 567 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more..."; 568 } else { 569 OS << " ..."; 570 } 571 OS << ">"; 572 break; 573 } 574 case MachineOperand::MO_RegisterLiveOut: 575 OS << "<regliveout>"; 576 break; 577 case MachineOperand::MO_Metadata: 578 OS << '<'; 579 getMetadata()->printAsOperand(OS, MST); 580 OS << '>'; 581 break; 582 case MachineOperand::MO_MCSymbol: 583 OS << "<MCSym=" << *getMCSymbol() << '>'; 584 break; 585 case MachineOperand::MO_CFIIndex: 586 OS << "<call frame instruction>"; 587 break; 588 case MachineOperand::MO_IntrinsicID: { 589 Intrinsic::ID ID = getIntrinsicID(); 590 if (ID < Intrinsic::num_intrinsics) 591 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>'; 592 else if (IntrinsicInfo) 593 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>'; 594 else 595 OS << "<intrinsic:" << ID << '>'; 596 break; 597 } 598 case MachineOperand::MO_Predicate: { 599 auto Pred = static_cast<CmpInst::Predicate>(getPredicate()); 600 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred") 601 << CmpInst::getPredicateName(Pred) << '>'; 602 break; 603 } 604 } 605 if (unsigned TF = getTargetFlags()) 606 OS << "[TF=" << TF << ']'; 607 } 608 609 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 610 LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; } 611 #endif 612 613 //===----------------------------------------------------------------------===// 614 // MachineMemOperand Implementation 615 //===----------------------------------------------------------------------===// 616 617 /// getAddrSpace - Return the LLVM IR address space number that this pointer 618 /// points into. 619 unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; } 620 621 /// isDereferenceable - Return true if V is always dereferenceable for 622 /// Offset + Size byte. 623 bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C, 624 const DataLayout &DL) const { 625 if (!V.is<const Value *>()) 626 return false; 627 628 const Value *BasePtr = V.get<const Value *>(); 629 if (BasePtr == nullptr) 630 return false; 631 632 return isDereferenceableAndAlignedPointer( 633 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL); 634 } 635 636 /// getConstantPool - Return a MachinePointerInfo record that refers to the 637 /// constant pool. 638 MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) { 639 return MachinePointerInfo(MF.getPSVManager().getConstantPool()); 640 } 641 642 /// getFixedStack - Return a MachinePointerInfo record that refers to the 643 /// the specified FrameIndex. 644 MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF, 645 int FI, int64_t Offset) { 646 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset); 647 } 648 649 MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) { 650 return MachinePointerInfo(MF.getPSVManager().getJumpTable()); 651 } 652 653 MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) { 654 return MachinePointerInfo(MF.getPSVManager().getGOT()); 655 } 656 657 MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF, 658 int64_t Offset, uint8_t ID) { 659 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID); 660 } 661 662 MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) { 663 return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace()); 664 } 665 666 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f, 667 uint64_t s, unsigned int a, 668 const AAMDNodes &AAInfo, 669 const MDNode *Ranges, SyncScope::ID SSID, 670 AtomicOrdering Ordering, 671 AtomicOrdering FailureOrdering) 672 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1), 673 AAInfo(AAInfo), Ranges(Ranges) { 674 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() || 675 isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) && 676 "invalid pointer value"); 677 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 678 assert((isLoad() || isStore()) && "Not a load/store!"); 679 680 AtomicInfo.SSID = static_cast<unsigned>(SSID); 681 assert(getSyncScopeID() == SSID && "Value truncated"); 682 AtomicInfo.Ordering = static_cast<unsigned>(Ordering); 683 assert(getOrdering() == Ordering && "Value truncated"); 684 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering); 685 assert(getFailureOrdering() == FailureOrdering && "Value truncated"); 686 } 687 688 /// Profile - Gather unique data for the object. 689 /// 690 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 691 ID.AddInteger(getOffset()); 692 ID.AddInteger(Size); 693 ID.AddPointer(getOpaqueValue()); 694 ID.AddInteger(getFlags()); 695 ID.AddInteger(getBaseAlignment()); 696 } 697 698 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 699 // The Value and Offset may differ due to CSE. But the flags and size 700 // should be the same. 701 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 702 assert(MMO->getSize() == getSize() && "Size mismatch!"); 703 704 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 705 // Update the alignment value. 706 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1; 707 // Also update the base and offset, because the new alignment may 708 // not be applicable with the old ones. 709 PtrInfo = MMO->PtrInfo; 710 } 711 } 712 713 /// getAlignment - Return the minimum known alignment in bytes of the 714 /// actual memory reference. 715 uint64_t MachineMemOperand::getAlignment() const { 716 return MinAlign(getBaseAlignment(), getOffset()); 717 } 718 719 void MachineMemOperand::print(raw_ostream &OS) const { 720 ModuleSlotTracker DummyMST(nullptr); 721 print(OS, DummyMST); 722 } 723 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const { 724 assert((isLoad() || isStore()) && "SV has to be a load, store or both."); 725 726 if (isVolatile()) 727 OS << "Volatile "; 728 729 if (isLoad()) 730 OS << "LD"; 731 if (isStore()) 732 OS << "ST"; 733 OS << getSize(); 734 735 // Print the address information. 736 OS << "["; 737 if (const Value *V = getValue()) 738 V->printAsOperand(OS, /*PrintType=*/false, MST); 739 else if (const PseudoSourceValue *PSV = getPseudoValue()) 740 PSV->printCustom(OS); 741 else 742 OS << "<unknown>"; 743 744 unsigned AS = getAddrSpace(); 745 if (AS != 0) 746 OS << "(addrspace=" << AS << ')'; 747 748 // If the alignment of the memory reference itself differs from the alignment 749 // of the base pointer, print the base alignment explicitly, next to the base 750 // pointer. 751 if (getBaseAlignment() != getAlignment()) 752 OS << "(align=" << getBaseAlignment() << ")"; 753 754 if (getOffset() != 0) 755 OS << "+" << getOffset(); 756 OS << "]"; 757 758 // Print the alignment of the reference. 759 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize()) 760 OS << "(align=" << getAlignment() << ")"; 761 762 // Print TBAA info. 763 if (const MDNode *TBAAInfo = getAAInfo().TBAA) { 764 OS << "(tbaa="; 765 if (TBAAInfo->getNumOperands() > 0) 766 TBAAInfo->getOperand(0)->printAsOperand(OS, MST); 767 else 768 OS << "<unknown>"; 769 OS << ")"; 770 } 771 772 // Print AA scope info. 773 if (const MDNode *ScopeInfo = getAAInfo().Scope) { 774 OS << "(alias.scope="; 775 if (ScopeInfo->getNumOperands() > 0) 776 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) { 777 ScopeInfo->getOperand(i)->printAsOperand(OS, MST); 778 if (i != ie - 1) 779 OS << ","; 780 } 781 else 782 OS << "<unknown>"; 783 OS << ")"; 784 } 785 786 // Print AA noalias scope info. 787 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) { 788 OS << "(noalias="; 789 if (NoAliasInfo->getNumOperands() > 0) 790 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) { 791 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST); 792 if (i != ie - 1) 793 OS << ","; 794 } 795 else 796 OS << "<unknown>"; 797 OS << ")"; 798 } 799 800 if (const MDNode *Ranges = getRanges()) { 801 unsigned NumRanges = Ranges->getNumOperands(); 802 if (NumRanges != 0) { 803 OS << "(ranges="; 804 805 for (unsigned I = 0; I != NumRanges; ++I) { 806 Ranges->getOperand(I)->printAsOperand(OS, MST); 807 if (I != NumRanges - 1) 808 OS << ','; 809 } 810 811 OS << ')'; 812 } 813 } 814 815 if (isNonTemporal()) 816 OS << "(nontemporal)"; 817 if (isDereferenceable()) 818 OS << "(dereferenceable)"; 819 if (isInvariant()) 820 OS << "(invariant)"; 821 if (getFlags() & MOTargetFlag1) 822 OS << "(flag1)"; 823 if (getFlags() & MOTargetFlag2) 824 OS << "(flag2)"; 825 if (getFlags() & MOTargetFlag3) 826 OS << "(flag3)"; 827 } 828