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