1 //===- lib/CodeGen/MachineInstr.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 // Methods common to all machine instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/APFloat.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/FoldingSet.h" 17 #include "llvm/ADT/Hashing.h" 18 #include "llvm/ADT/None.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/Analysis/AliasAnalysis.h" 23 #include "llvm/Analysis/MemoryLocation.h" 24 #include "llvm/CodeGen/GlobalISel/RegisterBank.h" 25 #include "llvm/CodeGen/MachineBasicBlock.h" 26 #include "llvm/CodeGen/MachineFunction.h" 27 #include "llvm/CodeGen/MachineInstr.h" 28 #include "llvm/CodeGen/MachineInstrBuilder.h" 29 #include "llvm/CodeGen/MachineInstrBundle.h" 30 #include "llvm/CodeGen/MachineMemOperand.h" 31 #include "llvm/CodeGen/MachineModuleInfo.h" 32 #include "llvm/CodeGen/MachineOperand.h" 33 #include "llvm/CodeGen/MachineRegisterInfo.h" 34 #include "llvm/CodeGen/PseudoSourceValue.h" 35 #include "llvm/IR/Constants.h" 36 #include "llvm/IR/DebugInfoMetadata.h" 37 #include "llvm/IR/DebugLoc.h" 38 #include "llvm/IR/DerivedTypes.h" 39 #include "llvm/IR/Function.h" 40 #include "llvm/IR/InlineAsm.h" 41 #include "llvm/IR/InstrTypes.h" 42 #include "llvm/IR/Intrinsics.h" 43 #include "llvm/IR/LLVMContext.h" 44 #include "llvm/IR/Metadata.h" 45 #include "llvm/IR/Module.h" 46 #include "llvm/IR/ModuleSlotTracker.h" 47 #include "llvm/IR/Type.h" 48 #include "llvm/IR/Value.h" 49 #include "llvm/MC/MCInstrDesc.h" 50 #include "llvm/MC/MCSymbol.h" 51 #include "llvm/MC/MCRegisterInfo.h" 52 #include "llvm/Support/Casting.h" 53 #include "llvm/Support/CommandLine.h" 54 #include "llvm/Support/Compiler.h" 55 #include "llvm/Support/Debug.h" 56 #include "llvm/Support/ErrorHandling.h" 57 #include "llvm/Support/LowLevelTypeImpl.h" 58 #include "llvm/Support/MathExtras.h" 59 #include "llvm/Support/raw_ostream.h" 60 #include "llvm/Target/TargetInstrInfo.h" 61 #include "llvm/Target/TargetIntrinsicInfo.h" 62 #include "llvm/Target/TargetMachine.h" 63 #include "llvm/Target/TargetRegisterInfo.h" 64 #include "llvm/Target/TargetSubtargetInfo.h" 65 #include <algorithm> 66 #include <cassert> 67 #include <cstddef> 68 #include <cstdint> 69 #include <cstring> 70 #include <iterator> 71 #include <utility> 72 73 using namespace llvm; 74 75 static cl::opt<bool> PrintWholeRegMask( 76 "print-whole-regmask", 77 cl::desc("Print the full contents of regmask operands in IR dumps"), 78 cl::init(true), cl::Hidden); 79 80 //===----------------------------------------------------------------------===// 81 // MachineOperand Implementation 82 //===----------------------------------------------------------------------===// 83 84 void MachineOperand::setReg(unsigned Reg) { 85 if (getReg() == Reg) return; // No change. 86 87 // Otherwise, we have to change the register. If this operand is embedded 88 // into a machine function, we need to update the old and new register's 89 // use/def lists. 90 if (MachineInstr *MI = getParent()) 91 if (MachineBasicBlock *MBB = MI->getParent()) 92 if (MachineFunction *MF = MBB->getParent()) { 93 MachineRegisterInfo &MRI = MF->getRegInfo(); 94 MRI.removeRegOperandFromUseList(this); 95 SmallContents.RegNo = Reg; 96 MRI.addRegOperandToUseList(this); 97 return; 98 } 99 100 // Otherwise, just change the register, no problem. :) 101 SmallContents.RegNo = Reg; 102 } 103 104 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 105 const TargetRegisterInfo &TRI) { 106 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 107 if (SubIdx && getSubReg()) 108 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 109 setReg(Reg); 110 if (SubIdx) 111 setSubReg(SubIdx); 112 } 113 114 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 115 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 116 if (getSubReg()) { 117 Reg = TRI.getSubReg(Reg, getSubReg()); 118 // Note that getSubReg() may return 0 if the sub-register doesn't exist. 119 // That won't happen in legal code. 120 setSubReg(0); 121 if (isDef()) 122 setIsUndef(false); 123 } 124 setReg(Reg); 125 } 126 127 /// Change a def to a use, or a use to a def. 128 void MachineOperand::setIsDef(bool Val) { 129 assert(isReg() && "Wrong MachineOperand accessor"); 130 assert((!Val || !isDebug()) && "Marking a debug operation as def"); 131 if (IsDef == Val) 132 return; 133 // MRI may keep uses and defs in different list positions. 134 if (MachineInstr *MI = getParent()) 135 if (MachineBasicBlock *MBB = MI->getParent()) 136 if (MachineFunction *MF = MBB->getParent()) { 137 MachineRegisterInfo &MRI = MF->getRegInfo(); 138 MRI.removeRegOperandFromUseList(this); 139 IsDef = Val; 140 MRI.addRegOperandToUseList(this); 141 return; 142 } 143 IsDef = Val; 144 } 145 146 // If this operand is currently a register operand, and if this is in a 147 // function, deregister the operand from the register's use/def list. 148 void MachineOperand::removeRegFromUses() { 149 if (!isReg() || !isOnRegUseList()) 150 return; 151 152 if (MachineInstr *MI = getParent()) { 153 if (MachineBasicBlock *MBB = MI->getParent()) { 154 if (MachineFunction *MF = MBB->getParent()) 155 MF->getRegInfo().removeRegOperandFromUseList(this); 156 } 157 } 158 } 159 160 /// ChangeToImmediate - Replace this operand with a new immediate operand of 161 /// the specified value. If an operand is known to be an immediate already, 162 /// the setImm method should be used. 163 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 164 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 165 166 removeRegFromUses(); 167 168 OpKind = MO_Immediate; 169 Contents.ImmVal = ImmVal; 170 } 171 172 void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) { 173 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 174 175 removeRegFromUses(); 176 177 OpKind = MO_FPImmediate; 178 Contents.CFP = FPImm; 179 } 180 181 void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) { 182 assert((!isReg() || !isTied()) && 183 "Cannot change a tied operand into an external symbol"); 184 185 removeRegFromUses(); 186 187 OpKind = MO_ExternalSymbol; 188 Contents.OffsetedInfo.Val.SymbolName = SymName; 189 setOffset(0); // Offset is always 0. 190 setTargetFlags(TargetFlags); 191 } 192 193 void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) { 194 assert((!isReg() || !isTied()) && 195 "Cannot change a tied operand into an MCSymbol"); 196 197 removeRegFromUses(); 198 199 OpKind = MO_MCSymbol; 200 Contents.Sym = Sym; 201 } 202 203 void MachineOperand::ChangeToFrameIndex(int Idx) { 204 assert((!isReg() || !isTied()) && 205 "Cannot change a tied operand into a FrameIndex"); 206 207 removeRegFromUses(); 208 209 OpKind = MO_FrameIndex; 210 setIndex(Idx); 211 } 212 213 /// ChangeToRegister - Replace this operand with a new register operand of 214 /// the specified value. If an operand is known to be an register already, 215 /// the setReg method should be used. 216 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 217 bool isKill, bool isDead, bool isUndef, 218 bool isDebug) { 219 MachineRegisterInfo *RegInfo = nullptr; 220 if (MachineInstr *MI = getParent()) 221 if (MachineBasicBlock *MBB = MI->getParent()) 222 if (MachineFunction *MF = MBB->getParent()) 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 OpKind = MO_Register; 232 SmallContents.RegNo = Reg; 233 SubReg_TargetFlags = 0; 234 IsDef = isDef; 235 IsImp = isImp; 236 IsKill = isKill; 237 IsDead = isDead; 238 IsUndef = isUndef; 239 IsInternalRead = false; 240 IsEarlyClobber = false; 241 IsDebug = isDebug; 242 // Ensure isOnRegUseList() returns false. 243 Contents.Reg.Prev = nullptr; 244 // Preserve the tie when the operand was already a register. 245 if (!WasReg) 246 TiedTo = 0; 247 248 // If this operand is embedded in a function, add the operand to the 249 // register's use/def list. 250 if (RegInfo) 251 RegInfo->addRegOperandToUseList(this); 252 } 253 254 /// isIdenticalTo - Return true if this operand is identical to the specified 255 /// operand. Note that this should stay in sync with the hash_value overload 256 /// below. 257 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 258 if (getType() != Other.getType() || 259 getTargetFlags() != Other.getTargetFlags()) 260 return false; 261 262 switch (getType()) { 263 case MachineOperand::MO_Register: 264 return getReg() == Other.getReg() && isDef() == Other.isDef() && 265 getSubReg() == Other.getSubReg(); 266 case MachineOperand::MO_Immediate: 267 return getImm() == Other.getImm(); 268 case MachineOperand::MO_CImmediate: 269 return getCImm() == Other.getCImm(); 270 case MachineOperand::MO_FPImmediate: 271 return getFPImm() == Other.getFPImm(); 272 case MachineOperand::MO_MachineBasicBlock: 273 return getMBB() == Other.getMBB(); 274 case MachineOperand::MO_FrameIndex: 275 return getIndex() == Other.getIndex(); 276 case MachineOperand::MO_ConstantPoolIndex: 277 case MachineOperand::MO_TargetIndex: 278 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 279 case MachineOperand::MO_JumpTableIndex: 280 return getIndex() == Other.getIndex(); 281 case MachineOperand::MO_GlobalAddress: 282 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 283 case MachineOperand::MO_ExternalSymbol: 284 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 && 285 getOffset() == Other.getOffset(); 286 case MachineOperand::MO_BlockAddress: 287 return getBlockAddress() == Other.getBlockAddress() && 288 getOffset() == Other.getOffset(); 289 case MachineOperand::MO_RegisterMask: 290 case MachineOperand::MO_RegisterLiveOut: { 291 // Shallow compare of the two RegMasks 292 const uint32_t *RegMask = getRegMask(); 293 const uint32_t *OtherRegMask = Other.getRegMask(); 294 if (RegMask == OtherRegMask) 295 return true; 296 297 // Calculate the size of the RegMask 298 const MachineFunction *MF = getParent()->getParent()->getParent(); 299 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 300 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32; 301 302 // Deep compare of the two RegMasks 303 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask); 304 } 305 case MachineOperand::MO_MCSymbol: 306 return getMCSymbol() == Other.getMCSymbol(); 307 case MachineOperand::MO_CFIIndex: 308 return getCFIIndex() == Other.getCFIIndex(); 309 case MachineOperand::MO_Metadata: 310 return getMetadata() == Other.getMetadata(); 311 case MachineOperand::MO_IntrinsicID: 312 return getIntrinsicID() == Other.getIntrinsicID(); 313 case MachineOperand::MO_Predicate: 314 return getPredicate() == Other.getPredicate(); 315 } 316 llvm_unreachable("Invalid machine operand type"); 317 } 318 319 // Note: this must stay exactly in sync with isIdenticalTo above. 320 hash_code llvm::hash_value(const MachineOperand &MO) { 321 switch (MO.getType()) { 322 case MachineOperand::MO_Register: 323 // Register operands don't have target flags. 324 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef()); 325 case MachineOperand::MO_Immediate: 326 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); 327 case MachineOperand::MO_CImmediate: 328 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); 329 case MachineOperand::MO_FPImmediate: 330 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); 331 case MachineOperand::MO_MachineBasicBlock: 332 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); 333 case MachineOperand::MO_FrameIndex: 334 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 335 case MachineOperand::MO_ConstantPoolIndex: 336 case MachineOperand::MO_TargetIndex: 337 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), 338 MO.getOffset()); 339 case MachineOperand::MO_JumpTableIndex: 340 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 341 case MachineOperand::MO_ExternalSymbol: 342 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), 343 MO.getSymbolName()); 344 case MachineOperand::MO_GlobalAddress: 345 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), 346 MO.getOffset()); 347 case MachineOperand::MO_BlockAddress: 348 return hash_combine(MO.getType(), MO.getTargetFlags(), 349 MO.getBlockAddress(), MO.getOffset()); 350 case MachineOperand::MO_RegisterMask: 351 case MachineOperand::MO_RegisterLiveOut: 352 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask()); 353 case MachineOperand::MO_Metadata: 354 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); 355 case MachineOperand::MO_MCSymbol: 356 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); 357 case MachineOperand::MO_CFIIndex: 358 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); 359 case MachineOperand::MO_IntrinsicID: 360 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID()); 361 case MachineOperand::MO_Predicate: 362 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate()); 363 } 364 llvm_unreachable("Invalid machine operand type"); 365 } 366 367 void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI, 368 const TargetIntrinsicInfo *IntrinsicInfo) const { 369 ModuleSlotTracker DummyMST(nullptr); 370 print(OS, DummyMST, TRI, IntrinsicInfo); 371 } 372 373 void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST, 374 const TargetRegisterInfo *TRI, 375 const TargetIntrinsicInfo *IntrinsicInfo) const { 376 switch (getType()) { 377 case MachineOperand::MO_Register: 378 OS << PrintReg(getReg(), TRI, getSubReg()); 379 380 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 381 isInternalRead() || isEarlyClobber() || isTied()) { 382 OS << '<'; 383 bool NeedComma = false; 384 if (isDef()) { 385 if (NeedComma) OS << ','; 386 if (isEarlyClobber()) 387 OS << "earlyclobber,"; 388 if (isImplicit()) 389 OS << "imp-"; 390 OS << "def"; 391 NeedComma = true; 392 // <def,read-undef> only makes sense when getSubReg() is set. 393 // Don't clutter the output otherwise. 394 if (isUndef() && getSubReg()) 395 OS << ",read-undef"; 396 } else if (isImplicit()) { 397 OS << "imp-use"; 398 NeedComma = true; 399 } 400 401 if (isKill()) { 402 if (NeedComma) OS << ','; 403 OS << "kill"; 404 NeedComma = true; 405 } 406 if (isDead()) { 407 if (NeedComma) OS << ','; 408 OS << "dead"; 409 NeedComma = true; 410 } 411 if (isUndef() && isUse()) { 412 if (NeedComma) OS << ','; 413 OS << "undef"; 414 NeedComma = true; 415 } 416 if (isInternalRead()) { 417 if (NeedComma) OS << ','; 418 OS << "internal"; 419 NeedComma = true; 420 } 421 if (isTied()) { 422 if (NeedComma) OS << ','; 423 OS << "tied"; 424 if (TiedTo != 15) 425 OS << unsigned(TiedTo - 1); 426 } 427 OS << '>'; 428 } 429 break; 430 case MachineOperand::MO_Immediate: 431 OS << getImm(); 432 break; 433 case MachineOperand::MO_CImmediate: 434 getCImm()->getValue().print(OS, false); 435 break; 436 case MachineOperand::MO_FPImmediate: 437 if (getFPImm()->getType()->isFloatTy()) { 438 OS << getFPImm()->getValueAPF().convertToFloat(); 439 } else if (getFPImm()->getType()->isHalfTy()) { 440 APFloat APF = getFPImm()->getValueAPF(); 441 bool Unused; 442 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused); 443 OS << "half " << APF.convertToFloat(); 444 } else if (getFPImm()->getType()->isFP128Ty()) { 445 APFloat APF = getFPImm()->getValueAPF(); 446 SmallString<16> Str; 447 getFPImm()->getValueAPF().toString(Str); 448 OS << "quad " << Str; 449 } else { 450 OS << getFPImm()->getValueAPF().convertToDouble(); 451 } 452 break; 453 case MachineOperand::MO_MachineBasicBlock: 454 OS << "<BB#" << getMBB()->getNumber() << ">"; 455 break; 456 case MachineOperand::MO_FrameIndex: 457 OS << "<fi#" << getIndex() << '>'; 458 break; 459 case MachineOperand::MO_ConstantPoolIndex: 460 OS << "<cp#" << getIndex(); 461 if (getOffset()) OS << "+" << getOffset(); 462 OS << '>'; 463 break; 464 case MachineOperand::MO_TargetIndex: 465 OS << "<ti#" << getIndex(); 466 if (getOffset()) OS << "+" << getOffset(); 467 OS << '>'; 468 break; 469 case MachineOperand::MO_JumpTableIndex: 470 OS << "<jt#" << getIndex() << '>'; 471 break; 472 case MachineOperand::MO_GlobalAddress: 473 OS << "<ga:"; 474 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); 475 if (getOffset()) OS << "+" << getOffset(); 476 OS << '>'; 477 break; 478 case MachineOperand::MO_ExternalSymbol: 479 OS << "<es:" << getSymbolName(); 480 if (getOffset()) OS << "+" << getOffset(); 481 OS << '>'; 482 break; 483 case MachineOperand::MO_BlockAddress: 484 OS << '<'; 485 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST); 486 if (getOffset()) OS << "+" << getOffset(); 487 OS << '>'; 488 break; 489 case MachineOperand::MO_RegisterMask: { 490 unsigned NumRegsInMask = 0; 491 unsigned NumRegsEmitted = 0; 492 OS << "<regmask"; 493 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) { 494 unsigned MaskWord = i / 32; 495 unsigned MaskBit = i % 32; 496 if (getRegMask()[MaskWord] & (1 << MaskBit)) { 497 if (PrintWholeRegMask || NumRegsEmitted <= 10) { 498 OS << " " << PrintReg(i, TRI); 499 NumRegsEmitted++; 500 } 501 NumRegsInMask++; 502 } 503 } 504 if (NumRegsEmitted != NumRegsInMask) 505 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more..."; 506 OS << ">"; 507 break; 508 } 509 case MachineOperand::MO_RegisterLiveOut: 510 OS << "<regliveout>"; 511 break; 512 case MachineOperand::MO_Metadata: 513 OS << '<'; 514 getMetadata()->printAsOperand(OS, MST); 515 OS << '>'; 516 break; 517 case MachineOperand::MO_MCSymbol: 518 OS << "<MCSym=" << *getMCSymbol() << '>'; 519 break; 520 case MachineOperand::MO_CFIIndex: 521 OS << "<call frame instruction>"; 522 break; 523 case MachineOperand::MO_IntrinsicID: { 524 Intrinsic::ID ID = getIntrinsicID(); 525 if (ID < Intrinsic::num_intrinsics) 526 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>'; 527 else if (IntrinsicInfo) 528 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>'; 529 else 530 OS << "<intrinsic:" << ID << '>'; 531 break; 532 } 533 case MachineOperand::MO_Predicate: { 534 auto Pred = static_cast<CmpInst::Predicate>(getPredicate()); 535 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred") 536 << CmpInst::getPredicateName(Pred) << '>'; 537 break; 538 } 539 } 540 if (unsigned TF = getTargetFlags()) 541 OS << "[TF=" << TF << ']'; 542 } 543 544 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 545 LLVM_DUMP_METHOD void MachineOperand::dump() const { 546 dbgs() << *this << '\n'; 547 } 548 #endif 549 550 //===----------------------------------------------------------------------===// 551 // MachineMemOperand Implementation 552 //===----------------------------------------------------------------------===// 553 554 /// getAddrSpace - Return the LLVM IR address space number that this pointer 555 /// points into. 556 unsigned MachinePointerInfo::getAddrSpace() const { 557 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0; 558 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace(); 559 } 560 561 /// getConstantPool - Return a MachinePointerInfo record that refers to the 562 /// constant pool. 563 MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) { 564 return MachinePointerInfo(MF.getPSVManager().getConstantPool()); 565 } 566 567 /// getFixedStack - Return a MachinePointerInfo record that refers to the 568 /// the specified FrameIndex. 569 MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF, 570 int FI, int64_t Offset) { 571 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset); 572 } 573 574 MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) { 575 return MachinePointerInfo(MF.getPSVManager().getJumpTable()); 576 } 577 578 MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) { 579 return MachinePointerInfo(MF.getPSVManager().getGOT()); 580 } 581 582 MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF, 583 int64_t Offset) { 584 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset); 585 } 586 587 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f, 588 uint64_t s, unsigned int a, 589 const AAMDNodes &AAInfo, 590 const MDNode *Ranges, 591 SynchronizationScope SynchScope, 592 AtomicOrdering Ordering, 593 AtomicOrdering FailureOrdering) 594 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1), 595 AAInfo(AAInfo), Ranges(Ranges) { 596 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() || 597 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) && 598 "invalid pointer value"); 599 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 600 assert((isLoad() || isStore()) && "Not a load/store!"); 601 602 AtomicInfo.SynchScope = static_cast<unsigned>(SynchScope); 603 assert(getSynchScope() == SynchScope && "Value truncated"); 604 AtomicInfo.Ordering = static_cast<unsigned>(Ordering); 605 assert(getOrdering() == Ordering && "Value truncated"); 606 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering); 607 assert(getFailureOrdering() == FailureOrdering && "Value truncated"); 608 } 609 610 /// Profile - Gather unique data for the object. 611 /// 612 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 613 ID.AddInteger(getOffset()); 614 ID.AddInteger(Size); 615 ID.AddPointer(getOpaqueValue()); 616 ID.AddInteger(getFlags()); 617 ID.AddInteger(getBaseAlignment()); 618 } 619 620 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 621 // The Value and Offset may differ due to CSE. But the flags and size 622 // should be the same. 623 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 624 assert(MMO->getSize() == getSize() && "Size mismatch!"); 625 626 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 627 // Update the alignment value. 628 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1; 629 // Also update the base and offset, because the new alignment may 630 // not be applicable with the old ones. 631 PtrInfo = MMO->PtrInfo; 632 } 633 } 634 635 /// getAlignment - Return the minimum known alignment in bytes of the 636 /// actual memory reference. 637 uint64_t MachineMemOperand::getAlignment() const { 638 return MinAlign(getBaseAlignment(), getOffset()); 639 } 640 641 void MachineMemOperand::print(raw_ostream &OS) const { 642 ModuleSlotTracker DummyMST(nullptr); 643 print(OS, DummyMST); 644 } 645 void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const { 646 assert((isLoad() || isStore()) && 647 "SV has to be a load, store or both."); 648 649 if (isVolatile()) 650 OS << "Volatile "; 651 652 if (isLoad()) 653 OS << "LD"; 654 if (isStore()) 655 OS << "ST"; 656 OS << getSize(); 657 658 // Print the address information. 659 OS << "["; 660 if (const Value *V = getValue()) 661 V->printAsOperand(OS, /*PrintType=*/false, MST); 662 else if (const PseudoSourceValue *PSV = getPseudoValue()) 663 PSV->printCustom(OS); 664 else 665 OS << "<unknown>"; 666 667 unsigned AS = getAddrSpace(); 668 if (AS != 0) 669 OS << "(addrspace=" << AS << ')'; 670 671 // If the alignment of the memory reference itself differs from the alignment 672 // of the base pointer, print the base alignment explicitly, next to the base 673 // pointer. 674 if (getBaseAlignment() != getAlignment()) 675 OS << "(align=" << getBaseAlignment() << ")"; 676 677 if (getOffset() != 0) 678 OS << "+" << getOffset(); 679 OS << "]"; 680 681 // Print the alignment of the reference. 682 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize()) 683 OS << "(align=" << getAlignment() << ")"; 684 685 // Print TBAA info. 686 if (const MDNode *TBAAInfo = getAAInfo().TBAA) { 687 OS << "(tbaa="; 688 if (TBAAInfo->getNumOperands() > 0) 689 TBAAInfo->getOperand(0)->printAsOperand(OS, MST); 690 else 691 OS << "<unknown>"; 692 OS << ")"; 693 } 694 695 // Print AA scope info. 696 if (const MDNode *ScopeInfo = getAAInfo().Scope) { 697 OS << "(alias.scope="; 698 if (ScopeInfo->getNumOperands() > 0) 699 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) { 700 ScopeInfo->getOperand(i)->printAsOperand(OS, MST); 701 if (i != ie-1) 702 OS << ","; 703 } 704 else 705 OS << "<unknown>"; 706 OS << ")"; 707 } 708 709 // Print AA noalias scope info. 710 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) { 711 OS << "(noalias="; 712 if (NoAliasInfo->getNumOperands() > 0) 713 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) { 714 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST); 715 if (i != ie-1) 716 OS << ","; 717 } 718 else 719 OS << "<unknown>"; 720 OS << ")"; 721 } 722 723 if (isNonTemporal()) 724 OS << "(nontemporal)"; 725 if (isDereferenceable()) 726 OS << "(dereferenceable)"; 727 if (isInvariant()) 728 OS << "(invariant)"; 729 } 730 731 //===----------------------------------------------------------------------===// 732 // MachineInstr Implementation 733 //===----------------------------------------------------------------------===// 734 735 void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) { 736 if (MCID->ImplicitDefs) 737 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs; 738 ++ImpDefs) 739 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true)); 740 if (MCID->ImplicitUses) 741 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses; 742 ++ImpUses) 743 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true)); 744 } 745 746 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the 747 /// implicit operands. It reserves space for the number of operands specified by 748 /// the MCInstrDesc. 749 MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid, 750 DebugLoc dl, bool NoImp) 751 : MCID(&tid), debugLoc(std::move(dl)) { 752 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); 753 754 // Reserve space for the expected number of operands. 755 if (unsigned NumOps = MCID->getNumOperands() + 756 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) { 757 CapOperands = OperandCapacity::get(NumOps); 758 Operands = MF.allocateOperandArray(CapOperands); 759 } 760 761 if (!NoImp) 762 addImplicitDefUseOperands(MF); 763 } 764 765 /// MachineInstr ctor - Copies MachineInstr arg exactly 766 /// 767 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 768 : MCID(&MI.getDesc()), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), 769 debugLoc(MI.getDebugLoc()) { 770 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); 771 772 CapOperands = OperandCapacity::get(MI.getNumOperands()); 773 Operands = MF.allocateOperandArray(CapOperands); 774 775 // Copy operands. 776 for (const MachineOperand &MO : MI.operands()) 777 addOperand(MF, MO); 778 779 // Copy all the sensible flags. 780 setFlags(MI.Flags); 781 } 782 783 /// getRegInfo - If this instruction is embedded into a MachineFunction, 784 /// return the MachineRegisterInfo object for the current function, otherwise 785 /// return null. 786 MachineRegisterInfo *MachineInstr::getRegInfo() { 787 if (MachineBasicBlock *MBB = getParent()) 788 return &MBB->getParent()->getRegInfo(); 789 return nullptr; 790 } 791 792 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 793 /// this instruction from their respective use lists. This requires that the 794 /// operands already be on their use lists. 795 void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) { 796 for (MachineOperand &MO : operands()) 797 if (MO.isReg()) 798 MRI.removeRegOperandFromUseList(&MO); 799 } 800 801 /// AddRegOperandsToUseLists - Add all of the register operands in 802 /// this instruction from their respective use lists. This requires that the 803 /// operands not be on their use lists yet. 804 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) { 805 for (MachineOperand &MO : operands()) 806 if (MO.isReg()) 807 MRI.addRegOperandToUseList(&MO); 808 } 809 810 void MachineInstr::addOperand(const MachineOperand &Op) { 811 MachineBasicBlock *MBB = getParent(); 812 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs"); 813 MachineFunction *MF = MBB->getParent(); 814 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs"); 815 addOperand(*MF, Op); 816 } 817 818 /// Move NumOps MachineOperands from Src to Dst, with support for overlapping 819 /// ranges. If MRI is non-null also update use-def chains. 820 static void moveOperands(MachineOperand *Dst, MachineOperand *Src, 821 unsigned NumOps, MachineRegisterInfo *MRI) { 822 if (MRI) 823 return MRI->moveOperands(Dst, Src, NumOps); 824 825 // MachineOperand is a trivially copyable type so we can just use memmove. 826 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand)); 827 } 828 829 /// addOperand - Add the specified operand to the instruction. If it is an 830 /// implicit operand, it is added to the end of the operand list. If it is 831 /// an explicit operand it is added at the end of the explicit operand list 832 /// (before the first implicit operand). 833 void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) { 834 assert(MCID && "Cannot add operands before providing an instr descriptor"); 835 836 // Check if we're adding one of our existing operands. 837 if (&Op >= Operands && &Op < Operands + NumOperands) { 838 // This is unusual: MI->addOperand(MI->getOperand(i)). 839 // If adding Op requires reallocating or moving existing operands around, 840 // the Op reference could go stale. Support it by copying Op. 841 MachineOperand CopyOp(Op); 842 return addOperand(MF, CopyOp); 843 } 844 845 // Find the insert location for the new operand. Implicit registers go at 846 // the end, everything else goes before the implicit regs. 847 // 848 // FIXME: Allow mixed explicit and implicit operands on inline asm. 849 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as 850 // implicit-defs, but they must not be moved around. See the FIXME in 851 // InstrEmitter.cpp. 852 unsigned OpNo = getNumOperands(); 853 bool isImpReg = Op.isReg() && Op.isImplicit(); 854 if (!isImpReg && !isInlineAsm()) { 855 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) { 856 --OpNo; 857 assert(!Operands[OpNo].isTied() && "Cannot move tied operands"); 858 } 859 } 860 861 #ifndef NDEBUG 862 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata; 863 // OpNo now points as the desired insertion point. Unless this is a variadic 864 // instruction, only implicit regs are allowed beyond MCID->getNumOperands(). 865 // RegMask operands go between the explicit and implicit operands. 866 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() || 867 OpNo < MCID->getNumOperands() || isMetaDataOp) && 868 "Trying to add an operand to a machine instr that is already done!"); 869 #endif 870 871 MachineRegisterInfo *MRI = getRegInfo(); 872 873 // Determine if the Operands array needs to be reallocated. 874 // Save the old capacity and operand array. 875 OperandCapacity OldCap = CapOperands; 876 MachineOperand *OldOperands = Operands; 877 if (!OldOperands || OldCap.getSize() == getNumOperands()) { 878 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1); 879 Operands = MF.allocateOperandArray(CapOperands); 880 // Move the operands before the insertion point. 881 if (OpNo) 882 moveOperands(Operands, OldOperands, OpNo, MRI); 883 } 884 885 // Move the operands following the insertion point. 886 if (OpNo != NumOperands) 887 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo, 888 MRI); 889 ++NumOperands; 890 891 // Deallocate the old operand array. 892 if (OldOperands != Operands && OldOperands) 893 MF.deallocateOperandArray(OldCap, OldOperands); 894 895 // Copy Op into place. It still needs to be inserted into the MRI use lists. 896 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op); 897 NewMO->ParentMI = this; 898 899 // When adding a register operand, tell MRI about it. 900 if (NewMO->isReg()) { 901 // Ensure isOnRegUseList() returns false, regardless of Op's status. 902 NewMO->Contents.Reg.Prev = nullptr; 903 // Ignore existing ties. This is not a property that can be copied. 904 NewMO->TiedTo = 0; 905 // Add the new operand to MRI, but only for instructions in an MBB. 906 if (MRI) 907 MRI->addRegOperandToUseList(NewMO); 908 // The MCID operand information isn't accurate until we start adding 909 // explicit operands. The implicit operands are added first, then the 910 // explicits are inserted before them. 911 if (!isImpReg) { 912 // Tie uses to defs as indicated in MCInstrDesc. 913 if (NewMO->isUse()) { 914 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO); 915 if (DefIdx != -1) 916 tieOperands(DefIdx, OpNo); 917 } 918 // If the register operand is flagged as early, mark the operand as such. 919 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1) 920 NewMO->setIsEarlyClobber(true); 921 } 922 } 923 } 924 925 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 926 /// fewer operand than it started with. 927 /// 928 void MachineInstr::RemoveOperand(unsigned OpNo) { 929 assert(OpNo < getNumOperands() && "Invalid operand number"); 930 untieRegOperand(OpNo); 931 932 #ifndef NDEBUG 933 // Moving tied operands would break the ties. 934 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i) 935 if (Operands[i].isReg()) 936 assert(!Operands[i].isTied() && "Cannot move tied operands"); 937 #endif 938 939 MachineRegisterInfo *MRI = getRegInfo(); 940 if (MRI && Operands[OpNo].isReg()) 941 MRI->removeRegOperandFromUseList(Operands + OpNo); 942 943 // Don't call the MachineOperand destructor. A lot of this code depends on 944 // MachineOperand having a trivial destructor anyway, and adding a call here 945 // wouldn't make it 'destructor-correct'. 946 947 if (unsigned N = NumOperands - 1 - OpNo) 948 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI); 949 --NumOperands; 950 } 951 952 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 953 /// This function should be used only occasionally. The setMemRefs function 954 /// is the primary method for setting up a MachineInstr's MemRefs list. 955 void MachineInstr::addMemOperand(MachineFunction &MF, 956 MachineMemOperand *MO) { 957 mmo_iterator OldMemRefs = MemRefs; 958 unsigned OldNumMemRefs = NumMemRefs; 959 960 unsigned NewNum = NumMemRefs + 1; 961 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 962 963 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs); 964 NewMemRefs[NewNum - 1] = MO; 965 setMemRefs(NewMemRefs, NewMemRefs + NewNum); 966 } 967 968 /// Check to see if the MMOs pointed to by the two MemRefs arrays are 969 /// identical. 970 static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) { 971 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end(); 972 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end(); 973 if ((E1 - I1) != (E2 - I2)) 974 return false; 975 for (; I1 != E1; ++I1, ++I2) { 976 if (**I1 != **I2) 977 return false; 978 } 979 return true; 980 } 981 982 std::pair<MachineInstr::mmo_iterator, unsigned> 983 MachineInstr::mergeMemRefsWith(const MachineInstr& Other) { 984 985 // If either of the incoming memrefs are empty, we must be conservative and 986 // treat this as if we've exhausted our space for memrefs and dropped them. 987 if (memoperands_empty() || Other.memoperands_empty()) 988 return std::make_pair(nullptr, 0); 989 990 // If both instructions have identical memrefs, we don't need to merge them. 991 // Since many instructions have a single memref, and we tend to merge things 992 // like pairs of loads from the same location, this catches a large number of 993 // cases in practice. 994 if (hasIdenticalMMOs(*this, Other)) 995 return std::make_pair(MemRefs, NumMemRefs); 996 997 // TODO: consider uniquing elements within the operand lists to reduce 998 // space usage and fall back to conservative information less often. 999 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs; 1000 1001 // If we don't have enough room to store this many memrefs, be conservative 1002 // and drop them. Otherwise, we'd fail asserts when trying to add them to 1003 // the new instruction. 1004 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs)) 1005 return std::make_pair(nullptr, 0); 1006 1007 MachineFunction *MF = getParent()->getParent(); 1008 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs); 1009 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(), 1010 MemBegin); 1011 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(), 1012 MemEnd); 1013 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs && 1014 "missing memrefs"); 1015 1016 return std::make_pair(MemBegin, CombinedNumMemRefs); 1017 } 1018 1019 bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const { 1020 assert(!isBundledWithPred() && "Must be called on bundle header"); 1021 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) { 1022 if (MII->getDesc().getFlags() & Mask) { 1023 if (Type == AnyInBundle) 1024 return true; 1025 } else { 1026 if (Type == AllInBundle && !MII->isBundle()) 1027 return false; 1028 } 1029 // This was the last instruction in the bundle. 1030 if (!MII->isBundledWithSucc()) 1031 return Type == AllInBundle; 1032 } 1033 } 1034 1035 bool MachineInstr::isIdenticalTo(const MachineInstr &Other, 1036 MICheckType Check) const { 1037 // If opcodes or number of operands are not the same then the two 1038 // instructions are obviously not identical. 1039 if (Other.getOpcode() != getOpcode() || 1040 Other.getNumOperands() != getNumOperands()) 1041 return false; 1042 1043 if (isBundle()) { 1044 // We have passed the test above that both instructions have the same 1045 // opcode, so we know that both instructions are bundles here. Let's compare 1046 // MIs inside the bundle. 1047 assert(Other.isBundle() && "Expected that both instructions are bundles."); 1048 MachineBasicBlock::const_instr_iterator I1 = getIterator(); 1049 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator(); 1050 // Loop until we analysed the last intruction inside at least one of the 1051 // bundles. 1052 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) { 1053 ++I1; 1054 ++I2; 1055 if (!I1->isIdenticalTo(*I2, Check)) 1056 return false; 1057 } 1058 // If we've reached the end of just one of the two bundles, but not both, 1059 // the instructions are not identical. 1060 if (I1->isBundledWithSucc() || I2->isBundledWithSucc()) 1061 return false; 1062 } 1063 1064 // Check operands to make sure they match. 1065 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1066 const MachineOperand &MO = getOperand(i); 1067 const MachineOperand &OMO = Other.getOperand(i); 1068 if (!MO.isReg()) { 1069 if (!MO.isIdenticalTo(OMO)) 1070 return false; 1071 continue; 1072 } 1073 1074 // Clients may or may not want to ignore defs when testing for equality. 1075 // For example, machine CSE pass only cares about finding common 1076 // subexpressions, so it's safe to ignore virtual register defs. 1077 if (MO.isDef()) { 1078 if (Check == IgnoreDefs) 1079 continue; 1080 else if (Check == IgnoreVRegDefs) { 1081 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || 1082 TargetRegisterInfo::isPhysicalRegister(OMO.getReg())) 1083 if (MO.getReg() != OMO.getReg()) 1084 return false; 1085 } else { 1086 if (!MO.isIdenticalTo(OMO)) 1087 return false; 1088 if (Check == CheckKillDead && MO.isDead() != OMO.isDead()) 1089 return false; 1090 } 1091 } else { 1092 if (!MO.isIdenticalTo(OMO)) 1093 return false; 1094 if (Check == CheckKillDead && MO.isKill() != OMO.isKill()) 1095 return false; 1096 } 1097 } 1098 // If DebugLoc does not match then two dbg.values are not identical. 1099 if (isDebugValue()) 1100 if (getDebugLoc() && Other.getDebugLoc() && 1101 getDebugLoc() != Other.getDebugLoc()) 1102 return false; 1103 return true; 1104 } 1105 1106 MachineInstr *MachineInstr::removeFromParent() { 1107 assert(getParent() && "Not embedded in a basic block!"); 1108 return getParent()->remove(this); 1109 } 1110 1111 MachineInstr *MachineInstr::removeFromBundle() { 1112 assert(getParent() && "Not embedded in a basic block!"); 1113 return getParent()->remove_instr(this); 1114 } 1115 1116 void MachineInstr::eraseFromParent() { 1117 assert(getParent() && "Not embedded in a basic block!"); 1118 getParent()->erase(this); 1119 } 1120 1121 void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() { 1122 assert(getParent() && "Not embedded in a basic block!"); 1123 MachineBasicBlock *MBB = getParent(); 1124 MachineFunction *MF = MBB->getParent(); 1125 assert(MF && "Not embedded in a function!"); 1126 1127 MachineInstr *MI = (MachineInstr *)this; 1128 MachineRegisterInfo &MRI = MF->getRegInfo(); 1129 1130 for (const MachineOperand &MO : MI->operands()) { 1131 if (!MO.isReg() || !MO.isDef()) 1132 continue; 1133 unsigned Reg = MO.getReg(); 1134 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 1135 continue; 1136 MRI.markUsesInDebugValueAsUndef(Reg); 1137 } 1138 MI->eraseFromParent(); 1139 } 1140 1141 void MachineInstr::eraseFromBundle() { 1142 assert(getParent() && "Not embedded in a basic block!"); 1143 getParent()->erase_instr(this); 1144 } 1145 1146 /// getNumExplicitOperands - Returns the number of non-implicit operands. 1147 /// 1148 unsigned MachineInstr::getNumExplicitOperands() const { 1149 unsigned NumOperands = MCID->getNumOperands(); 1150 if (!MCID->isVariadic()) 1151 return NumOperands; 1152 1153 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 1154 const MachineOperand &MO = getOperand(i); 1155 if (!MO.isReg() || !MO.isImplicit()) 1156 NumOperands++; 1157 } 1158 return NumOperands; 1159 } 1160 1161 void MachineInstr::bundleWithPred() { 1162 assert(!isBundledWithPred() && "MI is already bundled with its predecessor"); 1163 setFlag(BundledPred); 1164 MachineBasicBlock::instr_iterator Pred = getIterator(); 1165 --Pred; 1166 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags"); 1167 Pred->setFlag(BundledSucc); 1168 } 1169 1170 void MachineInstr::bundleWithSucc() { 1171 assert(!isBundledWithSucc() && "MI is already bundled with its successor"); 1172 setFlag(BundledSucc); 1173 MachineBasicBlock::instr_iterator Succ = getIterator(); 1174 ++Succ; 1175 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags"); 1176 Succ->setFlag(BundledPred); 1177 } 1178 1179 void MachineInstr::unbundleFromPred() { 1180 assert(isBundledWithPred() && "MI isn't bundled with its predecessor"); 1181 clearFlag(BundledPred); 1182 MachineBasicBlock::instr_iterator Pred = getIterator(); 1183 --Pred; 1184 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags"); 1185 Pred->clearFlag(BundledSucc); 1186 } 1187 1188 void MachineInstr::unbundleFromSucc() { 1189 assert(isBundledWithSucc() && "MI isn't bundled with its successor"); 1190 clearFlag(BundledSucc); 1191 MachineBasicBlock::instr_iterator Succ = getIterator(); 1192 ++Succ; 1193 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags"); 1194 Succ->clearFlag(BundledPred); 1195 } 1196 1197 bool MachineInstr::isStackAligningInlineAsm() const { 1198 if (isInlineAsm()) { 1199 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1200 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 1201 return true; 1202 } 1203 return false; 1204 } 1205 1206 InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const { 1207 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!"); 1208 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1209 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0); 1210 } 1211 1212 int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx, 1213 unsigned *GroupNo) const { 1214 assert(isInlineAsm() && "Expected an inline asm instruction"); 1215 assert(OpIdx < getNumOperands() && "OpIdx out of range"); 1216 1217 // Ignore queries about the initial operands. 1218 if (OpIdx < InlineAsm::MIOp_FirstOperand) 1219 return -1; 1220 1221 unsigned Group = 0; 1222 unsigned NumOps; 1223 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; 1224 i += NumOps) { 1225 const MachineOperand &FlagMO = getOperand(i); 1226 // If we reach the implicit register operands, stop looking. 1227 if (!FlagMO.isImm()) 1228 return -1; 1229 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); 1230 if (i + NumOps > OpIdx) { 1231 if (GroupNo) 1232 *GroupNo = Group; 1233 return i; 1234 } 1235 ++Group; 1236 } 1237 return -1; 1238 } 1239 1240 const DILocalVariable *MachineInstr::getDebugVariable() const { 1241 assert(isDebugValue() && "not a DBG_VALUE"); 1242 return cast<DILocalVariable>(getOperand(2).getMetadata()); 1243 } 1244 1245 const DIExpression *MachineInstr::getDebugExpression() const { 1246 assert(isDebugValue() && "not a DBG_VALUE"); 1247 return cast<DIExpression>(getOperand(3).getMetadata()); 1248 } 1249 1250 const TargetRegisterClass* 1251 MachineInstr::getRegClassConstraint(unsigned OpIdx, 1252 const TargetInstrInfo *TII, 1253 const TargetRegisterInfo *TRI) const { 1254 assert(getParent() && "Can't have an MBB reference here!"); 1255 assert(getParent()->getParent() && "Can't have an MF reference here!"); 1256 const MachineFunction &MF = *getParent()->getParent(); 1257 1258 // Most opcodes have fixed constraints in their MCInstrDesc. 1259 if (!isInlineAsm()) 1260 return TII->getRegClass(getDesc(), OpIdx, TRI, MF); 1261 1262 if (!getOperand(OpIdx).isReg()) 1263 return nullptr; 1264 1265 // For tied uses on inline asm, get the constraint from the def. 1266 unsigned DefIdx; 1267 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx)) 1268 OpIdx = DefIdx; 1269 1270 // Inline asm stores register class constraints in the flag word. 1271 int FlagIdx = findInlineAsmFlagIdx(OpIdx); 1272 if (FlagIdx < 0) 1273 return nullptr; 1274 1275 unsigned Flag = getOperand(FlagIdx).getImm(); 1276 unsigned RCID; 1277 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse || 1278 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef || 1279 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) && 1280 InlineAsm::hasRegClassConstraint(Flag, RCID)) 1281 return TRI->getRegClass(RCID); 1282 1283 // Assume that all registers in a memory operand are pointers. 1284 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem) 1285 return TRI->getPointerRegClass(MF); 1286 1287 return nullptr; 1288 } 1289 1290 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg( 1291 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII, 1292 const TargetRegisterInfo *TRI, bool ExploreBundle) const { 1293 // Check every operands inside the bundle if we have 1294 // been asked to. 1295 if (ExploreBundle) 1296 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC; 1297 ++OpndIt) 1298 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl( 1299 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI); 1300 else 1301 // Otherwise, just check the current operands. 1302 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i) 1303 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI); 1304 return CurRC; 1305 } 1306 1307 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl( 1308 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC, 1309 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { 1310 assert(CurRC && "Invalid initial register class"); 1311 // Check if Reg is constrained by some of its use/def from MI. 1312 const MachineOperand &MO = getOperand(OpIdx); 1313 if (!MO.isReg() || MO.getReg() != Reg) 1314 return CurRC; 1315 // If yes, accumulate the constraints through the operand. 1316 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI); 1317 } 1318 1319 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect( 1320 unsigned OpIdx, const TargetRegisterClass *CurRC, 1321 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { 1322 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI); 1323 const MachineOperand &MO = getOperand(OpIdx); 1324 assert(MO.isReg() && 1325 "Cannot get register constraints for non-register operand"); 1326 assert(CurRC && "Invalid initial register class"); 1327 if (unsigned SubIdx = MO.getSubReg()) { 1328 if (OpRC) 1329 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx); 1330 else 1331 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx); 1332 } else if (OpRC) 1333 CurRC = TRI->getCommonSubClass(CurRC, OpRC); 1334 return CurRC; 1335 } 1336 1337 /// Return the number of instructions inside the MI bundle, not counting the 1338 /// header instruction. 1339 unsigned MachineInstr::getBundleSize() const { 1340 MachineBasicBlock::const_instr_iterator I = getIterator(); 1341 unsigned Size = 0; 1342 while (I->isBundledWithSucc()) { 1343 ++Size; 1344 ++I; 1345 } 1346 return Size; 1347 } 1348 1349 /// Returns true if the MachineInstr has an implicit-use operand of exactly 1350 /// the given register (not considering sub/super-registers). 1351 bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const { 1352 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1353 const MachineOperand &MO = getOperand(i); 1354 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg) 1355 return true; 1356 } 1357 return false; 1358 } 1359 1360 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 1361 /// the specific register or -1 if it is not found. It further tightens 1362 /// the search criteria to a use that kills the register if isKill is true. 1363 int MachineInstr::findRegisterUseOperandIdx( 1364 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const { 1365 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1366 const MachineOperand &MO = getOperand(i); 1367 if (!MO.isReg() || !MO.isUse()) 1368 continue; 1369 unsigned MOReg = MO.getReg(); 1370 if (!MOReg) 1371 continue; 1372 if (MOReg == Reg || (TRI && TargetRegisterInfo::isPhysicalRegister(MOReg) && 1373 TargetRegisterInfo::isPhysicalRegister(Reg) && 1374 TRI->isSubRegister(MOReg, Reg))) 1375 if (!isKill || MO.isKill()) 1376 return i; 1377 } 1378 return -1; 1379 } 1380 1381 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes) 1382 /// indicating if this instruction reads or writes Reg. This also considers 1383 /// partial defines. 1384 std::pair<bool,bool> 1385 MachineInstr::readsWritesVirtualRegister(unsigned Reg, 1386 SmallVectorImpl<unsigned> *Ops) const { 1387 bool PartDef = false; // Partial redefine. 1388 bool FullDef = false; // Full define. 1389 bool Use = false; 1390 1391 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1392 const MachineOperand &MO = getOperand(i); 1393 if (!MO.isReg() || MO.getReg() != Reg) 1394 continue; 1395 if (Ops) 1396 Ops->push_back(i); 1397 if (MO.isUse()) 1398 Use |= !MO.isUndef(); 1399 else if (MO.getSubReg() && !MO.isUndef()) 1400 // A partial <def,undef> doesn't count as reading the register. 1401 PartDef = true; 1402 else 1403 FullDef = true; 1404 } 1405 // A partial redefine uses Reg unless there is also a full define. 1406 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef); 1407 } 1408 1409 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 1410 /// the specified register or -1 if it is not found. If isDead is true, defs 1411 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 1412 /// also checks if there is a def of a super-register. 1413 int 1414 MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap, 1415 const TargetRegisterInfo *TRI) const { 1416 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg); 1417 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1418 const MachineOperand &MO = getOperand(i); 1419 // Accept regmask operands when Overlap is set. 1420 // Ignore them when looking for a specific def operand (Overlap == false). 1421 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg)) 1422 return i; 1423 if (!MO.isReg() || !MO.isDef()) 1424 continue; 1425 unsigned MOReg = MO.getReg(); 1426 bool Found = (MOReg == Reg); 1427 if (!Found && TRI && isPhys && 1428 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 1429 if (Overlap) 1430 Found = TRI->regsOverlap(MOReg, Reg); 1431 else 1432 Found = TRI->isSubRegister(MOReg, Reg); 1433 } 1434 if (Found && (!isDead || MO.isDead())) 1435 return i; 1436 } 1437 return -1; 1438 } 1439 1440 /// findFirstPredOperandIdx() - Find the index of the first operand in the 1441 /// operand list that is used to represent the predicate. It returns -1 if 1442 /// none is found. 1443 int MachineInstr::findFirstPredOperandIdx() const { 1444 // Don't call MCID.findFirstPredOperandIdx() because this variant 1445 // is sometimes called on an instruction that's not yet complete, and 1446 // so the number of operands is less than the MCID indicates. In 1447 // particular, the PTX target does this. 1448 const MCInstrDesc &MCID = getDesc(); 1449 if (MCID.isPredicable()) { 1450 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 1451 if (MCID.OpInfo[i].isPredicate()) 1452 return i; 1453 } 1454 1455 return -1; 1456 } 1457 1458 // MachineOperand::TiedTo is 4 bits wide. 1459 const unsigned TiedMax = 15; 1460 1461 /// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other. 1462 /// 1463 /// Use and def operands can be tied together, indicated by a non-zero TiedTo 1464 /// field. TiedTo can have these values: 1465 /// 1466 /// 0: Operand is not tied to anything. 1467 /// 1 to TiedMax-1: Tied to getOperand(TiedTo-1). 1468 /// TiedMax: Tied to an operand >= TiedMax-1. 1469 /// 1470 /// The tied def must be one of the first TiedMax operands on a normal 1471 /// instruction. INLINEASM instructions allow more tied defs. 1472 /// 1473 void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) { 1474 MachineOperand &DefMO = getOperand(DefIdx); 1475 MachineOperand &UseMO = getOperand(UseIdx); 1476 assert(DefMO.isDef() && "DefIdx must be a def operand"); 1477 assert(UseMO.isUse() && "UseIdx must be a use operand"); 1478 assert(!DefMO.isTied() && "Def is already tied to another use"); 1479 assert(!UseMO.isTied() && "Use is already tied to another def"); 1480 1481 if (DefIdx < TiedMax) 1482 UseMO.TiedTo = DefIdx + 1; 1483 else { 1484 // Inline asm can use the group descriptors to find tied operands, but on 1485 // normal instruction, the tied def must be within the first TiedMax 1486 // operands. 1487 assert(isInlineAsm() && "DefIdx out of range"); 1488 UseMO.TiedTo = TiedMax; 1489 } 1490 1491 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx(). 1492 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax); 1493 } 1494 1495 /// Given the index of a tied register operand, find the operand it is tied to. 1496 /// Defs are tied to uses and vice versa. Returns the index of the tied operand 1497 /// which must exist. 1498 unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const { 1499 const MachineOperand &MO = getOperand(OpIdx); 1500 assert(MO.isTied() && "Operand isn't tied"); 1501 1502 // Normally TiedTo is in range. 1503 if (MO.TiedTo < TiedMax) 1504 return MO.TiedTo - 1; 1505 1506 // Uses on normal instructions can be out of range. 1507 if (!isInlineAsm()) { 1508 // Normal tied defs must be in the 0..TiedMax-1 range. 1509 if (MO.isUse()) 1510 return TiedMax - 1; 1511 // MO is a def. Search for the tied use. 1512 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) { 1513 const MachineOperand &UseMO = getOperand(i); 1514 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1) 1515 return i; 1516 } 1517 llvm_unreachable("Can't find tied use"); 1518 } 1519 1520 // Now deal with inline asm by parsing the operand group descriptor flags. 1521 // Find the beginning of each operand group. 1522 SmallVector<unsigned, 8> GroupIdx; 1523 unsigned OpIdxGroup = ~0u; 1524 unsigned NumOps; 1525 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; 1526 i += NumOps) { 1527 const MachineOperand &FlagMO = getOperand(i); 1528 assert(FlagMO.isImm() && "Invalid tied operand on inline asm"); 1529 unsigned CurGroup = GroupIdx.size(); 1530 GroupIdx.push_back(i); 1531 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); 1532 // OpIdx belongs to this operand group. 1533 if (OpIdx > i && OpIdx < i + NumOps) 1534 OpIdxGroup = CurGroup; 1535 unsigned TiedGroup; 1536 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup)) 1537 continue; 1538 // Operands in this group are tied to operands in TiedGroup which must be 1539 // earlier. Find the number of operands between the two groups. 1540 unsigned Delta = i - GroupIdx[TiedGroup]; 1541 1542 // OpIdx is a use tied to TiedGroup. 1543 if (OpIdxGroup == CurGroup) 1544 return OpIdx - Delta; 1545 1546 // OpIdx is a def tied to this use group. 1547 if (OpIdxGroup == TiedGroup) 1548 return OpIdx + Delta; 1549 } 1550 llvm_unreachable("Invalid tied operand on inline asm"); 1551 } 1552 1553 /// clearKillInfo - Clears kill flags on all operands. 1554 /// 1555 void MachineInstr::clearKillInfo() { 1556 for (MachineOperand &MO : operands()) { 1557 if (MO.isReg() && MO.isUse()) 1558 MO.setIsKill(false); 1559 } 1560 } 1561 1562 void MachineInstr::substituteRegister(unsigned FromReg, 1563 unsigned ToReg, 1564 unsigned SubIdx, 1565 const TargetRegisterInfo &RegInfo) { 1566 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 1567 if (SubIdx) 1568 ToReg = RegInfo.getSubReg(ToReg, SubIdx); 1569 for (MachineOperand &MO : operands()) { 1570 if (!MO.isReg() || MO.getReg() != FromReg) 1571 continue; 1572 MO.substPhysReg(ToReg, RegInfo); 1573 } 1574 } else { 1575 for (MachineOperand &MO : operands()) { 1576 if (!MO.isReg() || MO.getReg() != FromReg) 1577 continue; 1578 MO.substVirtReg(ToReg, SubIdx, RegInfo); 1579 } 1580 } 1581 } 1582 1583 /// isSafeToMove - Return true if it is safe to move this instruction. If 1584 /// SawStore is set to true, it means that there is a store (or call) between 1585 /// the instruction's location and its intended destination. 1586 bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const { 1587 // Ignore stuff that we obviously can't move. 1588 // 1589 // Treat volatile loads as stores. This is not strictly necessary for 1590 // volatiles, but it is required for atomic loads. It is not allowed to move 1591 // a load across an atomic load with Ordering > Monotonic. 1592 if (mayStore() || isCall() || 1593 (mayLoad() && hasOrderedMemoryRef())) { 1594 SawStore = true; 1595 return false; 1596 } 1597 1598 if (isPosition() || isDebugValue() || isTerminator() || 1599 hasUnmodeledSideEffects()) 1600 return false; 1601 1602 // See if this instruction does a load. If so, we have to guarantee that the 1603 // loaded value doesn't change between the load and the its intended 1604 // destination. The check for isInvariantLoad gives the targe the chance to 1605 // classify the load as always returning a constant, e.g. a constant pool 1606 // load. 1607 if (mayLoad() && !isDereferenceableInvariantLoad(AA)) 1608 // Otherwise, this is a real load. If there is a store between the load and 1609 // end of block, we can't move it. 1610 return !SawStore; 1611 1612 return true; 1613 } 1614 1615 bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other, 1616 bool UseTBAA) { 1617 const MachineFunction *MF = getParent()->getParent(); 1618 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 1619 1620 // If neither instruction stores to memory, they can't alias in any 1621 // meaningful way, even if they read from the same address. 1622 if (!mayStore() && !Other.mayStore()) 1623 return false; 1624 1625 // Let the target decide if memory accesses cannot possibly overlap. 1626 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA)) 1627 return false; 1628 1629 if (!AA) 1630 return true; 1631 1632 // FIXME: Need to handle multiple memory operands to support all targets. 1633 if (!hasOneMemOperand() || !Other.hasOneMemOperand()) 1634 return true; 1635 1636 MachineMemOperand *MMOa = *memoperands_begin(); 1637 MachineMemOperand *MMOb = *Other.memoperands_begin(); 1638 1639 if (!MMOa->getValue() || !MMOb->getValue()) 1640 return true; 1641 1642 // The following interface to AA is fashioned after DAGCombiner::isAlias 1643 // and operates with MachineMemOperand offset with some important 1644 // assumptions: 1645 // - LLVM fundamentally assumes flat address spaces. 1646 // - MachineOperand offset can *only* result from legalization and 1647 // cannot affect queries other than the trivial case of overlap 1648 // checking. 1649 // - These offsets never wrap and never step outside 1650 // of allocated objects. 1651 // - There should never be any negative offsets here. 1652 // 1653 // FIXME: Modify API to hide this math from "user" 1654 // FIXME: Even before we go to AA we can reason locally about some 1655 // memory objects. It can save compile time, and possibly catch some 1656 // corner cases not currently covered. 1657 1658 assert((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset"); 1659 assert((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset"); 1660 1661 int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset()); 1662 int64_t Overlapa = MMOa->getSize() + MMOa->getOffset() - MinOffset; 1663 int64_t Overlapb = MMOb->getSize() + MMOb->getOffset() - MinOffset; 1664 1665 AliasResult AAResult = 1666 AA->alias(MemoryLocation(MMOa->getValue(), Overlapa, 1667 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()), 1668 MemoryLocation(MMOb->getValue(), Overlapb, 1669 UseTBAA ? MMOb->getAAInfo() : AAMDNodes())); 1670 1671 return (AAResult != NoAlias); 1672 } 1673 1674 /// hasOrderedMemoryRef - Return true if this instruction may have an ordered 1675 /// or volatile memory reference, or if the information describing the memory 1676 /// reference is not available. Return false if it is known to have no ordered 1677 /// memory references. 1678 bool MachineInstr::hasOrderedMemoryRef() const { 1679 // An instruction known never to access memory won't have a volatile access. 1680 if (!mayStore() && 1681 !mayLoad() && 1682 !isCall() && 1683 !hasUnmodeledSideEffects()) 1684 return false; 1685 1686 // Otherwise, if the instruction has no memory reference information, 1687 // conservatively assume it wasn't preserved. 1688 if (memoperands_empty()) 1689 return true; 1690 1691 // Check if any of our memory operands are ordered. 1692 return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) { 1693 return !MMO->isUnordered(); 1694 }); 1695 } 1696 1697 /// isDereferenceableInvariantLoad - Return true if this instruction will never 1698 /// trap and is loading from a location whose value is invariant across a run of 1699 /// this function. 1700 bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const { 1701 // If the instruction doesn't load at all, it isn't an invariant load. 1702 if (!mayLoad()) 1703 return false; 1704 1705 // If the instruction has lost its memoperands, conservatively assume that 1706 // it may not be an invariant load. 1707 if (memoperands_empty()) 1708 return false; 1709 1710 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo(); 1711 1712 for (MachineMemOperand *MMO : memoperands()) { 1713 if (MMO->isVolatile()) return false; 1714 if (MMO->isStore()) return false; 1715 if (MMO->isInvariant() && MMO->isDereferenceable()) 1716 continue; 1717 1718 // A load from a constant PseudoSourceValue is invariant. 1719 if (const PseudoSourceValue *PSV = MMO->getPseudoValue()) 1720 if (PSV->isConstant(&MFI)) 1721 continue; 1722 1723 if (const Value *V = MMO->getValue()) { 1724 // If we have an AliasAnalysis, ask it whether the memory is constant. 1725 if (AA && 1726 AA->pointsToConstantMemory( 1727 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo()))) 1728 continue; 1729 } 1730 1731 // Otherwise assume conservatively. 1732 return false; 1733 } 1734 1735 // Everything checks out. 1736 return true; 1737 } 1738 1739 /// isConstantValuePHI - If the specified instruction is a PHI that always 1740 /// merges together the same virtual register, return the register, otherwise 1741 /// return 0. 1742 unsigned MachineInstr::isConstantValuePHI() const { 1743 if (!isPHI()) 1744 return 0; 1745 assert(getNumOperands() >= 3 && 1746 "It's illegal to have a PHI without source operands"); 1747 1748 unsigned Reg = getOperand(1).getReg(); 1749 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) 1750 if (getOperand(i).getReg() != Reg) 1751 return 0; 1752 return Reg; 1753 } 1754 1755 bool MachineInstr::hasUnmodeledSideEffects() const { 1756 if (hasProperty(MCID::UnmodeledSideEffects)) 1757 return true; 1758 if (isInlineAsm()) { 1759 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1760 if (ExtraInfo & InlineAsm::Extra_HasSideEffects) 1761 return true; 1762 } 1763 1764 return false; 1765 } 1766 1767 bool MachineInstr::isLoadFoldBarrier() const { 1768 return mayStore() || isCall() || hasUnmodeledSideEffects(); 1769 } 1770 1771 /// allDefsAreDead - Return true if all the defs of this instruction are dead. 1772 /// 1773 bool MachineInstr::allDefsAreDead() const { 1774 for (const MachineOperand &MO : operands()) { 1775 if (!MO.isReg() || MO.isUse()) 1776 continue; 1777 if (!MO.isDead()) 1778 return false; 1779 } 1780 return true; 1781 } 1782 1783 /// copyImplicitOps - Copy implicit register operands from specified 1784 /// instruction to this instruction. 1785 void MachineInstr::copyImplicitOps(MachineFunction &MF, 1786 const MachineInstr &MI) { 1787 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands(); 1788 i != e; ++i) { 1789 const MachineOperand &MO = MI.getOperand(i); 1790 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask()) 1791 addOperand(MF, MO); 1792 } 1793 } 1794 1795 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1796 LLVM_DUMP_METHOD void MachineInstr::dump() const { 1797 dbgs() << " "; 1798 print(dbgs()); 1799 } 1800 #endif 1801 1802 void MachineInstr::print(raw_ostream &OS, bool SkipOpers, bool SkipDebugLoc, 1803 const TargetInstrInfo *TII) const { 1804 const Module *M = nullptr; 1805 if (const MachineBasicBlock *MBB = getParent()) 1806 if (const MachineFunction *MF = MBB->getParent()) 1807 M = MF->getFunction()->getParent(); 1808 1809 ModuleSlotTracker MST(M); 1810 print(OS, MST, SkipOpers, SkipDebugLoc, TII); 1811 } 1812 1813 void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST, 1814 bool SkipOpers, bool SkipDebugLoc, 1815 const TargetInstrInfo *TII) const { 1816 // We can be a bit tidier if we know the MachineFunction. 1817 const MachineFunction *MF = nullptr; 1818 const TargetRegisterInfo *TRI = nullptr; 1819 const MachineRegisterInfo *MRI = nullptr; 1820 const TargetIntrinsicInfo *IntrinsicInfo = nullptr; 1821 1822 if (const MachineBasicBlock *MBB = getParent()) { 1823 MF = MBB->getParent(); 1824 if (MF) { 1825 MRI = &MF->getRegInfo(); 1826 TRI = MF->getSubtarget().getRegisterInfo(); 1827 if (!TII) 1828 TII = MF->getSubtarget().getInstrInfo(); 1829 IntrinsicInfo = MF->getTarget().getIntrinsicInfo(); 1830 } 1831 } 1832 1833 // Save a list of virtual registers. 1834 SmallVector<unsigned, 8> VirtRegs; 1835 1836 // Print explicitly defined operands on the left of an assignment syntax. 1837 unsigned StartOp = 0, e = getNumOperands(); 1838 for (; StartOp < e && getOperand(StartOp).isReg() && 1839 getOperand(StartOp).isDef() && 1840 !getOperand(StartOp).isImplicit(); 1841 ++StartOp) { 1842 if (StartOp != 0) OS << ", "; 1843 getOperand(StartOp).print(OS, MST, TRI, IntrinsicInfo); 1844 unsigned Reg = getOperand(StartOp).getReg(); 1845 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 1846 VirtRegs.push_back(Reg); 1847 LLT Ty = MRI ? MRI->getType(Reg) : LLT{}; 1848 if (Ty.isValid()) 1849 OS << '(' << Ty << ')'; 1850 } 1851 } 1852 1853 if (StartOp != 0) 1854 OS << " = "; 1855 1856 // Print the opcode name. 1857 if (TII) 1858 OS << TII->getName(getOpcode()); 1859 else 1860 OS << "UNKNOWN"; 1861 1862 if (SkipOpers) 1863 return; 1864 1865 // Print the rest of the operands. 1866 bool FirstOp = true; 1867 unsigned AsmDescOp = ~0u; 1868 unsigned AsmOpCount = 0; 1869 1870 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) { 1871 // Print asm string. 1872 OS << " "; 1873 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI); 1874 1875 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack 1876 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1877 if (ExtraInfo & InlineAsm::Extra_HasSideEffects) 1878 OS << " [sideeffect]"; 1879 if (ExtraInfo & InlineAsm::Extra_MayLoad) 1880 OS << " [mayload]"; 1881 if (ExtraInfo & InlineAsm::Extra_MayStore) 1882 OS << " [maystore]"; 1883 if (ExtraInfo & InlineAsm::Extra_IsConvergent) 1884 OS << " [isconvergent]"; 1885 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 1886 OS << " [alignstack]"; 1887 if (getInlineAsmDialect() == InlineAsm::AD_ATT) 1888 OS << " [attdialect]"; 1889 if (getInlineAsmDialect() == InlineAsm::AD_Intel) 1890 OS << " [inteldialect]"; 1891 1892 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand; 1893 FirstOp = false; 1894 } 1895 1896 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 1897 const MachineOperand &MO = getOperand(i); 1898 1899 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1900 VirtRegs.push_back(MO.getReg()); 1901 1902 if (FirstOp) FirstOp = false; else OS << ","; 1903 OS << " "; 1904 if (i < getDesc().NumOperands) { 1905 const MCOperandInfo &MCOI = getDesc().OpInfo[i]; 1906 if (MCOI.isPredicate()) 1907 OS << "pred:"; 1908 if (MCOI.isOptionalDef()) 1909 OS << "opt:"; 1910 } 1911 if (isDebugValue() && MO.isMetadata()) { 1912 // Pretty print DBG_VALUE instructions. 1913 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata()); 1914 if (DIV && !DIV->getName().empty()) 1915 OS << "!\"" << DIV->getName() << '\"'; 1916 else 1917 MO.print(OS, MST, TRI); 1918 } else if (TRI && (isInsertSubreg() || isRegSequence() || 1919 (isSubregToReg() && i == 3)) && MO.isImm()) { 1920 OS << TRI->getSubRegIndexName(MO.getImm()); 1921 } else if (i == AsmDescOp && MO.isImm()) { 1922 // Pretty print the inline asm operand descriptor. 1923 OS << '$' << AsmOpCount++; 1924 unsigned Flag = MO.getImm(); 1925 switch (InlineAsm::getKind(Flag)) { 1926 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break; 1927 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break; 1928 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break; 1929 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break; 1930 case InlineAsm::Kind_Imm: OS << ":[imm"; break; 1931 case InlineAsm::Kind_Mem: OS << ":[mem"; break; 1932 default: OS << ":[??" << InlineAsm::getKind(Flag); break; 1933 } 1934 1935 unsigned RCID = 0; 1936 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) && 1937 InlineAsm::hasRegClassConstraint(Flag, RCID)) { 1938 if (TRI) { 1939 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID)); 1940 } else 1941 OS << ":RC" << RCID; 1942 } 1943 1944 if (InlineAsm::isMemKind(Flag)) { 1945 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag); 1946 switch (MCID) { 1947 case InlineAsm::Constraint_es: OS << ":es"; break; 1948 case InlineAsm::Constraint_i: OS << ":i"; break; 1949 case InlineAsm::Constraint_m: OS << ":m"; break; 1950 case InlineAsm::Constraint_o: OS << ":o"; break; 1951 case InlineAsm::Constraint_v: OS << ":v"; break; 1952 case InlineAsm::Constraint_Q: OS << ":Q"; break; 1953 case InlineAsm::Constraint_R: OS << ":R"; break; 1954 case InlineAsm::Constraint_S: OS << ":S"; break; 1955 case InlineAsm::Constraint_T: OS << ":T"; break; 1956 case InlineAsm::Constraint_Um: OS << ":Um"; break; 1957 case InlineAsm::Constraint_Un: OS << ":Un"; break; 1958 case InlineAsm::Constraint_Uq: OS << ":Uq"; break; 1959 case InlineAsm::Constraint_Us: OS << ":Us"; break; 1960 case InlineAsm::Constraint_Ut: OS << ":Ut"; break; 1961 case InlineAsm::Constraint_Uv: OS << ":Uv"; break; 1962 case InlineAsm::Constraint_Uy: OS << ":Uy"; break; 1963 case InlineAsm::Constraint_X: OS << ":X"; break; 1964 case InlineAsm::Constraint_Z: OS << ":Z"; break; 1965 case InlineAsm::Constraint_ZC: OS << ":ZC"; break; 1966 case InlineAsm::Constraint_Zy: OS << ":Zy"; break; 1967 default: OS << ":?"; break; 1968 } 1969 } 1970 1971 unsigned TiedTo = 0; 1972 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo)) 1973 OS << " tiedto:$" << TiedTo; 1974 1975 OS << ']'; 1976 1977 // Compute the index of the next operand descriptor. 1978 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag); 1979 } else 1980 MO.print(OS, MST, TRI); 1981 } 1982 1983 bool HaveSemi = false; 1984 const unsigned PrintableFlags = FrameSetup | FrameDestroy; 1985 if (Flags & PrintableFlags) { 1986 if (!HaveSemi) { 1987 OS << ";"; 1988 HaveSemi = true; 1989 } 1990 OS << " flags: "; 1991 1992 if (Flags & FrameSetup) 1993 OS << "FrameSetup"; 1994 1995 if (Flags & FrameDestroy) 1996 OS << "FrameDestroy"; 1997 } 1998 1999 if (!memoperands_empty()) { 2000 if (!HaveSemi) { 2001 OS << ";"; 2002 HaveSemi = true; 2003 } 2004 2005 OS << " mem:"; 2006 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 2007 i != e; ++i) { 2008 (*i)->print(OS, MST); 2009 if (std::next(i) != e) 2010 OS << " "; 2011 } 2012 } 2013 2014 // Print the regclass of any virtual registers encountered. 2015 if (MRI && !VirtRegs.empty()) { 2016 if (!HaveSemi) { 2017 OS << ";"; 2018 HaveSemi = true; 2019 } 2020 for (unsigned i = 0; i != VirtRegs.size(); ++i) { 2021 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]); 2022 if (!RC) 2023 continue; 2024 // Generic virtual registers do not have register classes. 2025 if (RC.is<const RegisterBank *>()) 2026 OS << " " << RC.get<const RegisterBank *>()->getName(); 2027 else 2028 OS << " " 2029 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>()); 2030 OS << ':' << PrintReg(VirtRegs[i]); 2031 for (unsigned j = i+1; j != VirtRegs.size();) { 2032 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) { 2033 ++j; 2034 continue; 2035 } 2036 if (VirtRegs[i] != VirtRegs[j]) 2037 OS << "," << PrintReg(VirtRegs[j]); 2038 VirtRegs.erase(VirtRegs.begin()+j); 2039 } 2040 } 2041 } 2042 2043 // Print debug location information. 2044 if (isDebugValue() && getOperand(e - 2).isMetadata()) { 2045 if (!HaveSemi) 2046 OS << ";"; 2047 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata()); 2048 OS << " line no:" << DV->getLine(); 2049 if (auto *InlinedAt = debugLoc->getInlinedAt()) { 2050 DebugLoc InlinedAtDL(InlinedAt); 2051 if (InlinedAtDL && MF) { 2052 OS << " inlined @[ "; 2053 InlinedAtDL.print(OS); 2054 OS << " ]"; 2055 } 2056 } 2057 if (isIndirectDebugValue()) 2058 OS << " indirect"; 2059 } else if (SkipDebugLoc) { 2060 return; 2061 } else if (debugLoc && MF) { 2062 if (!HaveSemi) 2063 OS << ";"; 2064 OS << " dbg:"; 2065 debugLoc.print(OS); 2066 } 2067 2068 OS << '\n'; 2069 } 2070 2071 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 2072 const TargetRegisterInfo *RegInfo, 2073 bool AddIfNotFound) { 2074 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 2075 bool hasAliases = isPhysReg && 2076 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid(); 2077 bool Found = false; 2078 SmallVector<unsigned,4> DeadOps; 2079 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 2080 MachineOperand &MO = getOperand(i); 2081 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 2082 continue; 2083 2084 // DEBUG_VALUE nodes do not contribute to code generation and should 2085 // always be ignored. Failure to do so may result in trying to modify 2086 // KILL flags on DEBUG_VALUE nodes. 2087 if (MO.isDebug()) 2088 continue; 2089 2090 unsigned Reg = MO.getReg(); 2091 if (!Reg) 2092 continue; 2093 2094 if (Reg == IncomingReg) { 2095 if (!Found) { 2096 if (MO.isKill()) 2097 // The register is already marked kill. 2098 return true; 2099 if (isPhysReg && isRegTiedToDefOperand(i)) 2100 // Two-address uses of physregs must not be marked kill. 2101 return true; 2102 MO.setIsKill(); 2103 Found = true; 2104 } 2105 } else if (hasAliases && MO.isKill() && 2106 TargetRegisterInfo::isPhysicalRegister(Reg)) { 2107 // A super-register kill already exists. 2108 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 2109 return true; 2110 if (RegInfo->isSubRegister(IncomingReg, Reg)) 2111 DeadOps.push_back(i); 2112 } 2113 } 2114 2115 // Trim unneeded kill operands. 2116 while (!DeadOps.empty()) { 2117 unsigned OpIdx = DeadOps.back(); 2118 if (getOperand(OpIdx).isImplicit()) 2119 RemoveOperand(OpIdx); 2120 else 2121 getOperand(OpIdx).setIsKill(false); 2122 DeadOps.pop_back(); 2123 } 2124 2125 // If not found, this means an alias of one of the operands is killed. Add a 2126 // new implicit operand if required. 2127 if (!Found && AddIfNotFound) { 2128 addOperand(MachineOperand::CreateReg(IncomingReg, 2129 false /*IsDef*/, 2130 true /*IsImp*/, 2131 true /*IsKill*/)); 2132 return true; 2133 } 2134 return Found; 2135 } 2136 2137 void MachineInstr::clearRegisterKills(unsigned Reg, 2138 const TargetRegisterInfo *RegInfo) { 2139 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) 2140 RegInfo = nullptr; 2141 for (MachineOperand &MO : operands()) { 2142 if (!MO.isReg() || !MO.isUse() || !MO.isKill()) 2143 continue; 2144 unsigned OpReg = MO.getReg(); 2145 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg) 2146 MO.setIsKill(false); 2147 } 2148 } 2149 2150 bool MachineInstr::addRegisterDead(unsigned Reg, 2151 const TargetRegisterInfo *RegInfo, 2152 bool AddIfNotFound) { 2153 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg); 2154 bool hasAliases = isPhysReg && 2155 MCRegAliasIterator(Reg, RegInfo, false).isValid(); 2156 bool Found = false; 2157 SmallVector<unsigned,4> DeadOps; 2158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 2159 MachineOperand &MO = getOperand(i); 2160 if (!MO.isReg() || !MO.isDef()) 2161 continue; 2162 unsigned MOReg = MO.getReg(); 2163 if (!MOReg) 2164 continue; 2165 2166 if (MOReg == Reg) { 2167 MO.setIsDead(); 2168 Found = true; 2169 } else if (hasAliases && MO.isDead() && 2170 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 2171 // There exists a super-register that's marked dead. 2172 if (RegInfo->isSuperRegister(Reg, MOReg)) 2173 return true; 2174 if (RegInfo->isSubRegister(Reg, MOReg)) 2175 DeadOps.push_back(i); 2176 } 2177 } 2178 2179 // Trim unneeded dead operands. 2180 while (!DeadOps.empty()) { 2181 unsigned OpIdx = DeadOps.back(); 2182 if (getOperand(OpIdx).isImplicit()) 2183 RemoveOperand(OpIdx); 2184 else 2185 getOperand(OpIdx).setIsDead(false); 2186 DeadOps.pop_back(); 2187 } 2188 2189 // If not found, this means an alias of one of the operands is dead. Add a 2190 // new implicit operand if required. 2191 if (Found || !AddIfNotFound) 2192 return Found; 2193 2194 addOperand(MachineOperand::CreateReg(Reg, 2195 true /*IsDef*/, 2196 true /*IsImp*/, 2197 false /*IsKill*/, 2198 true /*IsDead*/)); 2199 return true; 2200 } 2201 2202 void MachineInstr::clearRegisterDeads(unsigned Reg) { 2203 for (MachineOperand &MO : operands()) { 2204 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg) 2205 continue; 2206 MO.setIsDead(false); 2207 } 2208 } 2209 2210 void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) { 2211 for (MachineOperand &MO : operands()) { 2212 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0) 2213 continue; 2214 MO.setIsUndef(IsUndef); 2215 } 2216 } 2217 2218 void MachineInstr::addRegisterDefined(unsigned Reg, 2219 const TargetRegisterInfo *RegInfo) { 2220 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 2221 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo); 2222 if (MO) 2223 return; 2224 } else { 2225 for (const MachineOperand &MO : operands()) { 2226 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() && 2227 MO.getSubReg() == 0) 2228 return; 2229 } 2230 } 2231 addOperand(MachineOperand::CreateReg(Reg, 2232 true /*IsDef*/, 2233 true /*IsImp*/)); 2234 } 2235 2236 void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs, 2237 const TargetRegisterInfo &TRI) { 2238 bool HasRegMask = false; 2239 for (MachineOperand &MO : operands()) { 2240 if (MO.isRegMask()) { 2241 HasRegMask = true; 2242 continue; 2243 } 2244 if (!MO.isReg() || !MO.isDef()) continue; 2245 unsigned Reg = MO.getReg(); 2246 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue; 2247 // If there are no uses, including partial uses, the def is dead. 2248 if (llvm::none_of(UsedRegs, 2249 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); })) 2250 MO.setIsDead(); 2251 } 2252 2253 // This is a call with a register mask operand. 2254 // Mask clobbers are always dead, so add defs for the non-dead defines. 2255 if (HasRegMask) 2256 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end(); 2257 I != E; ++I) 2258 addRegisterDefined(*I, &TRI); 2259 } 2260 2261 unsigned 2262 MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) { 2263 // Build up a buffer of hash code components. 2264 SmallVector<size_t, 8> HashComponents; 2265 HashComponents.reserve(MI->getNumOperands() + 1); 2266 HashComponents.push_back(MI->getOpcode()); 2267 for (const MachineOperand &MO : MI->operands()) { 2268 if (MO.isReg() && MO.isDef() && 2269 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 2270 continue; // Skip virtual register defs. 2271 2272 HashComponents.push_back(hash_value(MO)); 2273 } 2274 return hash_combine_range(HashComponents.begin(), HashComponents.end()); 2275 } 2276 2277 void MachineInstr::emitError(StringRef Msg) const { 2278 // Find the source location cookie. 2279 unsigned LocCookie = 0; 2280 const MDNode *LocMD = nullptr; 2281 for (unsigned i = getNumOperands(); i != 0; --i) { 2282 if (getOperand(i-1).isMetadata() && 2283 (LocMD = getOperand(i-1).getMetadata()) && 2284 LocMD->getNumOperands() != 0) { 2285 if (const ConstantInt *CI = 2286 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) { 2287 LocCookie = CI->getZExtValue(); 2288 break; 2289 } 2290 } 2291 } 2292 2293 if (const MachineBasicBlock *MBB = getParent()) 2294 if (const MachineFunction *MF = MBB->getParent()) 2295 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg); 2296 report_fatal_error(Msg); 2297 } 2298 2299 MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL, 2300 const MCInstrDesc &MCID, bool IsIndirect, 2301 unsigned Reg, unsigned Offset, 2302 const MDNode *Variable, const MDNode *Expr) { 2303 assert(isa<DILocalVariable>(Variable) && "not a variable"); 2304 assert(cast<DIExpression>(Expr)->isValid() && "not an expression"); 2305 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) && 2306 "Expected inlined-at fields to agree"); 2307 if (IsIndirect) 2308 return BuildMI(MF, DL, MCID) 2309 .addReg(Reg, RegState::Debug) 2310 .addImm(Offset) 2311 .addMetadata(Variable) 2312 .addMetadata(Expr); 2313 else { 2314 assert(Offset == 0 && "A direct address cannot have an offset."); 2315 return BuildMI(MF, DL, MCID) 2316 .addReg(Reg, RegState::Debug) 2317 .addReg(0U, RegState::Debug) 2318 .addMetadata(Variable) 2319 .addMetadata(Expr); 2320 } 2321 } 2322 2323 MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB, 2324 MachineBasicBlock::iterator I, 2325 const DebugLoc &DL, const MCInstrDesc &MCID, 2326 bool IsIndirect, unsigned Reg, 2327 unsigned Offset, const MDNode *Variable, 2328 const MDNode *Expr) { 2329 assert(isa<DILocalVariable>(Variable) && "not a variable"); 2330 assert(cast<DIExpression>(Expr)->isValid() && "not an expression"); 2331 MachineFunction &MF = *BB.getParent(); 2332 MachineInstr *MI = 2333 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr); 2334 BB.insert(I, MI); 2335 return MachineInstrBuilder(MF, MI); 2336 } 2337 2338 MachineInstr *llvm::buildDbgValueForSpill(MachineBasicBlock &BB, 2339 MachineBasicBlock::iterator I, 2340 const MachineInstr &Orig, 2341 int FrameIndex) { 2342 const MDNode *Var = Orig.getDebugVariable(); 2343 const auto *Expr = cast_or_null<DIExpression>(Orig.getDebugExpression()); 2344 bool IsIndirect = Orig.isIndirectDebugValue(); 2345 uint64_t Offset = IsIndirect ? Orig.getOperand(1).getImm() : 0; 2346 DebugLoc DL = Orig.getDebugLoc(); 2347 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && 2348 "Expected inlined-at fields to agree"); 2349 // If the DBG_VALUE already was a memory location, add an extra 2350 // DW_OP_deref. Otherwise just turning this from a register into a 2351 // memory/indirect location is sufficient. 2352 if (IsIndirect) 2353 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref); 2354 return BuildMI(BB, I, DL, Orig.getDesc()) 2355 .addFrameIndex(FrameIndex) 2356 .addImm(Offset) 2357 .addMetadata(Var) 2358 .addMetadata(Expr); 2359 } 2360