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