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