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