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/Constants.h" 16 #include "llvm/Function.h" 17 #include "llvm/InlineAsm.h" 18 #include "llvm/Metadata.h" 19 #include "llvm/Type.h" 20 #include "llvm/Value.h" 21 #include "llvm/Assembly/Writer.h" 22 #include "llvm/CodeGen/MachineConstantPool.h" 23 #include "llvm/CodeGen/MachineFunction.h" 24 #include "llvm/CodeGen/MachineMemOperand.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/PseudoSourceValue.h" 27 #include "llvm/MC/MCSymbol.h" 28 #include "llvm/Target/TargetMachine.h" 29 #include "llvm/Target/TargetInstrInfo.h" 30 #include "llvm/Target/TargetInstrDesc.h" 31 #include "llvm/Target/TargetRegisterInfo.h" 32 #include "llvm/Analysis/AliasAnalysis.h" 33 #include "llvm/Analysis/DebugInfo.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/LeakDetector.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Support/raw_ostream.h" 39 #include "llvm/ADT/FoldingSet.h" 40 using namespace llvm; 41 42 //===----------------------------------------------------------------------===// 43 // MachineOperand Implementation 44 //===----------------------------------------------------------------------===// 45 46 /// AddRegOperandToRegInfo - Add this register operand to the specified 47 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 48 /// explicitly nulled out. 49 void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { 50 assert(isReg() && "Can only add reg operand to use lists"); 51 52 // If the reginfo pointer is null, just explicitly null out or next/prev 53 // pointers, to ensure they are not garbage. 54 if (RegInfo == 0) { 55 Contents.Reg.Prev = 0; 56 Contents.Reg.Next = 0; 57 return; 58 } 59 60 // Otherwise, add this operand to the head of the registers use/def list. 61 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); 62 63 // For SSA values, we prefer to keep the definition at the start of the list. 64 // we do this by skipping over the definition if it is at the head of the 65 // list. 66 if (*Head && (*Head)->isDef()) 67 Head = &(*Head)->Contents.Reg.Next; 68 69 Contents.Reg.Next = *Head; 70 if (Contents.Reg.Next) { 71 assert(getReg() == Contents.Reg.Next->getReg() && 72 "Different regs on the same list!"); 73 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; 74 } 75 76 Contents.Reg.Prev = Head; 77 *Head = this; 78 } 79 80 /// RemoveRegOperandFromRegInfo - Remove this register operand from the 81 /// MachineRegisterInfo it is linked with. 82 void MachineOperand::RemoveRegOperandFromRegInfo() { 83 assert(isOnRegUseList() && "Reg operand is not on a use list"); 84 // Unlink this from the doubly linked list of operands. 85 MachineOperand *NextOp = Contents.Reg.Next; 86 *Contents.Reg.Prev = NextOp; 87 if (NextOp) { 88 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); 89 NextOp->Contents.Reg.Prev = Contents.Reg.Prev; 90 } 91 Contents.Reg.Prev = 0; 92 Contents.Reg.Next = 0; 93 } 94 95 void MachineOperand::setReg(unsigned Reg) { 96 if (getReg() == Reg) return; // No change. 97 98 // Otherwise, we have to change the register. If this operand is embedded 99 // into a machine function, we need to update the old and new register's 100 // use/def lists. 101 if (MachineInstr *MI = getParent()) 102 if (MachineBasicBlock *MBB = MI->getParent()) 103 if (MachineFunction *MF = MBB->getParent()) { 104 RemoveRegOperandFromRegInfo(); 105 SmallContents.RegNo = Reg; 106 AddRegOperandToRegInfo(&MF->getRegInfo()); 107 return; 108 } 109 110 // Otherwise, just change the register, no problem. :) 111 SmallContents.RegNo = Reg; 112 } 113 114 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 115 const TargetRegisterInfo &TRI) { 116 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 117 if (SubIdx && getSubReg()) 118 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 119 setReg(Reg); 120 if (SubIdx) 121 setSubReg(SubIdx); 122 } 123 124 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 125 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 126 if (getSubReg()) { 127 Reg = TRI.getSubReg(Reg, getSubReg()); 128 assert(Reg && "Invalid SubReg for physical register"); 129 setSubReg(0); 130 } 131 setReg(Reg); 132 } 133 134 /// ChangeToImmediate - Replace this operand with a new immediate operand of 135 /// the specified value. If an operand is known to be an immediate already, 136 /// the setImm method should be used. 137 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 138 // If this operand is currently a register operand, and if this is in a 139 // function, deregister the operand from the register's use/def list. 140 if (isReg() && getParent() && getParent()->getParent() && 141 getParent()->getParent()->getParent()) 142 RemoveRegOperandFromRegInfo(); 143 144 OpKind = MO_Immediate; 145 Contents.ImmVal = ImmVal; 146 } 147 148 /// ChangeToRegister - Replace this operand with a new register operand of 149 /// the specified value. If an operand is known to be an register already, 150 /// the setReg method should be used. 151 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 152 bool isKill, bool isDead, bool isUndef, 153 bool isDebug) { 154 // If this operand is already a register operand, use setReg to update the 155 // register's use/def lists. 156 if (isReg()) { 157 assert(!isEarlyClobber()); 158 setReg(Reg); 159 } else { 160 // Otherwise, change this to a register and set the reg#. 161 OpKind = MO_Register; 162 SmallContents.RegNo = Reg; 163 164 // If this operand is embedded in a function, add the operand to the 165 // register's use/def list. 166 if (MachineInstr *MI = getParent()) 167 if (MachineBasicBlock *MBB = MI->getParent()) 168 if (MachineFunction *MF = MBB->getParent()) 169 AddRegOperandToRegInfo(&MF->getRegInfo()); 170 } 171 172 IsDef = isDef; 173 IsImp = isImp; 174 IsKill = isKill; 175 IsDead = isDead; 176 IsUndef = isUndef; 177 IsEarlyClobber = false; 178 IsDebug = isDebug; 179 SubReg = 0; 180 } 181 182 /// isIdenticalTo - Return true if this operand is identical to the specified 183 /// operand. 184 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 185 if (getType() != Other.getType() || 186 getTargetFlags() != Other.getTargetFlags()) 187 return false; 188 189 switch (getType()) { 190 default: llvm_unreachable("Unrecognized operand type"); 191 case MachineOperand::MO_Register: 192 return getReg() == Other.getReg() && isDef() == Other.isDef() && 193 getSubReg() == Other.getSubReg(); 194 case MachineOperand::MO_Immediate: 195 return getImm() == Other.getImm(); 196 case MachineOperand::MO_FPImmediate: 197 return getFPImm() == Other.getFPImm(); 198 case MachineOperand::MO_MachineBasicBlock: 199 return getMBB() == Other.getMBB(); 200 case MachineOperand::MO_FrameIndex: 201 return getIndex() == Other.getIndex(); 202 case MachineOperand::MO_ConstantPoolIndex: 203 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 204 case MachineOperand::MO_JumpTableIndex: 205 return getIndex() == Other.getIndex(); 206 case MachineOperand::MO_GlobalAddress: 207 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 208 case MachineOperand::MO_ExternalSymbol: 209 return !strcmp(getSymbolName(), Other.getSymbolName()) && 210 getOffset() == Other.getOffset(); 211 case MachineOperand::MO_BlockAddress: 212 return getBlockAddress() == Other.getBlockAddress(); 213 case MachineOperand::MO_MCSymbol: 214 return getMCSymbol() == Other.getMCSymbol(); 215 case MachineOperand::MO_Metadata: 216 return getMetadata() == Other.getMetadata(); 217 } 218 } 219 220 /// print - Print the specified machine operand. 221 /// 222 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 223 // If the instruction is embedded into a basic block, we can find the 224 // target info for the instruction. 225 if (!TM) 226 if (const MachineInstr *MI = getParent()) 227 if (const MachineBasicBlock *MBB = MI->getParent()) 228 if (const MachineFunction *MF = MBB->getParent()) 229 TM = &MF->getTarget(); 230 231 switch (getType()) { 232 case MachineOperand::MO_Register: 233 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { 234 OS << "%reg" << getReg(); 235 } else { 236 if (TM) 237 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; 238 else 239 OS << "%physreg" << getReg(); 240 } 241 242 if (getSubReg() != 0) { 243 if (TM) 244 OS << ':' << TM->getRegisterInfo()->getSubRegIndexName(getSubReg()); 245 else 246 OS << ':' << getSubReg(); 247 } 248 249 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 250 isEarlyClobber()) { 251 OS << '<'; 252 bool NeedComma = false; 253 if (isDef()) { 254 if (NeedComma) OS << ','; 255 if (isEarlyClobber()) 256 OS << "earlyclobber,"; 257 if (isImplicit()) 258 OS << "imp-"; 259 OS << "def"; 260 NeedComma = true; 261 } else if (isImplicit()) { 262 OS << "imp-use"; 263 NeedComma = true; 264 } 265 266 if (isKill() || isDead() || isUndef()) { 267 if (NeedComma) OS << ','; 268 if (isKill()) OS << "kill"; 269 if (isDead()) OS << "dead"; 270 if (isUndef()) { 271 if (isKill() || isDead()) 272 OS << ','; 273 OS << "undef"; 274 } 275 } 276 OS << '>'; 277 } 278 break; 279 case MachineOperand::MO_Immediate: 280 OS << getImm(); 281 break; 282 case MachineOperand::MO_FPImmediate: 283 if (getFPImm()->getType()->isFloatTy()) 284 OS << getFPImm()->getValueAPF().convertToFloat(); 285 else 286 OS << getFPImm()->getValueAPF().convertToDouble(); 287 break; 288 case MachineOperand::MO_MachineBasicBlock: 289 OS << "<BB#" << getMBB()->getNumber() << ">"; 290 break; 291 case MachineOperand::MO_FrameIndex: 292 OS << "<fi#" << getIndex() << '>'; 293 break; 294 case MachineOperand::MO_ConstantPoolIndex: 295 OS << "<cp#" << getIndex(); 296 if (getOffset()) OS << "+" << getOffset(); 297 OS << '>'; 298 break; 299 case MachineOperand::MO_JumpTableIndex: 300 OS << "<jt#" << getIndex() << '>'; 301 break; 302 case MachineOperand::MO_GlobalAddress: 303 OS << "<ga:"; 304 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false); 305 if (getOffset()) OS << "+" << getOffset(); 306 OS << '>'; 307 break; 308 case MachineOperand::MO_ExternalSymbol: 309 OS << "<es:" << getSymbolName(); 310 if (getOffset()) OS << "+" << getOffset(); 311 OS << '>'; 312 break; 313 case MachineOperand::MO_BlockAddress: 314 OS << '<'; 315 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false); 316 OS << '>'; 317 break; 318 case MachineOperand::MO_Metadata: 319 OS << '<'; 320 WriteAsOperand(OS, getMetadata(), /*PrintType=*/false); 321 OS << '>'; 322 break; 323 case MachineOperand::MO_MCSymbol: 324 OS << "<MCSym=" << *getMCSymbol() << '>'; 325 break; 326 default: 327 llvm_unreachable("Unrecognized operand type"); 328 } 329 330 if (unsigned TF = getTargetFlags()) 331 OS << "[TF=" << TF << ']'; 332 } 333 334 //===----------------------------------------------------------------------===// 335 // MachineMemOperand Implementation 336 //===----------------------------------------------------------------------===// 337 338 /// getAddrSpace - Return the LLVM IR address space number that this pointer 339 /// points into. 340 unsigned MachinePointerInfo::getAddrSpace() const { 341 if (V == 0) return 0; 342 return cast<PointerType>(V->getType())->getAddressSpace(); 343 } 344 345 /// getConstantPool - Return a MachinePointerInfo record that refers to the 346 /// constant pool. 347 MachinePointerInfo MachinePointerInfo::getConstantPool() { 348 return MachinePointerInfo(PseudoSourceValue::getConstantPool()); 349 } 350 351 /// getFixedStack - Return a MachinePointerInfo record that refers to the 352 /// the specified FrameIndex. 353 MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) { 354 return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset); 355 } 356 357 MachinePointerInfo MachinePointerInfo::getJumpTable() { 358 return MachinePointerInfo(PseudoSourceValue::getJumpTable()); 359 } 360 361 MachinePointerInfo MachinePointerInfo::getGOT() { 362 return MachinePointerInfo(PseudoSourceValue::getGOT()); 363 } 364 365 MachinePointerInfo MachinePointerInfo::getStack(int64_t Offset) { 366 return MachinePointerInfo(PseudoSourceValue::getStack(), Offset); 367 } 368 369 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f, 370 uint64_t s, unsigned int a) 371 : PtrInfo(ptrinfo), Size(s), 372 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)) { 373 assert((PtrInfo.V == 0 || isa<PointerType>(PtrInfo.V->getType())) && 374 "invalid pointer value"); 375 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 376 assert((isLoad() || isStore()) && "Not a load/store!"); 377 } 378 379 /// Profile - Gather unique data for the object. 380 /// 381 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 382 ID.AddInteger(getOffset()); 383 ID.AddInteger(Size); 384 ID.AddPointer(getValue()); 385 ID.AddInteger(Flags); 386 } 387 388 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 389 // The Value and Offset may differ due to CSE. But the flags and size 390 // should be the same. 391 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 392 assert(MMO->getSize() == getSize() && "Size mismatch!"); 393 394 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 395 // Update the alignment value. 396 Flags = (Flags & ((1 << MOMaxBits) - 1)) | 397 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits); 398 // Also update the base and offset, because the new alignment may 399 // not be applicable with the old ones. 400 PtrInfo = MMO->PtrInfo; 401 } 402 } 403 404 /// getAlignment - Return the minimum known alignment in bytes of the 405 /// actual memory reference. 406 uint64_t MachineMemOperand::getAlignment() const { 407 return MinAlign(getBaseAlignment(), getOffset()); 408 } 409 410 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { 411 assert((MMO.isLoad() || MMO.isStore()) && 412 "SV has to be a load, store or both."); 413 414 if (MMO.isVolatile()) 415 OS << "Volatile "; 416 417 if (MMO.isLoad()) 418 OS << "LD"; 419 if (MMO.isStore()) 420 OS << "ST"; 421 OS << MMO.getSize(); 422 423 // Print the address information. 424 OS << "["; 425 if (!MMO.getValue()) 426 OS << "<unknown>"; 427 else 428 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false); 429 430 // If the alignment of the memory reference itself differs from the alignment 431 // of the base pointer, print the base alignment explicitly, next to the base 432 // pointer. 433 if (MMO.getBaseAlignment() != MMO.getAlignment()) 434 OS << "(align=" << MMO.getBaseAlignment() << ")"; 435 436 if (MMO.getOffset() != 0) 437 OS << "+" << MMO.getOffset(); 438 OS << "]"; 439 440 // Print the alignment of the reference. 441 if (MMO.getBaseAlignment() != MMO.getAlignment() || 442 MMO.getBaseAlignment() != MMO.getSize()) 443 OS << "(align=" << MMO.getAlignment() << ")"; 444 445 return OS; 446 } 447 448 //===----------------------------------------------------------------------===// 449 // MachineInstr Implementation 450 //===----------------------------------------------------------------------===// 451 452 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 453 /// TID NULL and no operands. 454 MachineInstr::MachineInstr() 455 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), 456 Parent(0) { 457 // Make sure that we get added to a machine basicblock 458 LeakDetector::addGarbageObject(this); 459 } 460 461 void MachineInstr::addImplicitDefUseOperands() { 462 if (TID->ImplicitDefs) 463 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 464 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); 465 if (TID->ImplicitUses) 466 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 467 addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); 468 } 469 470 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the 471 /// implicit operands. It reserves space for the number of operands specified by 472 /// the TargetInstrDesc. 473 MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) 474 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), 475 MemRefs(0), MemRefsEnd(0), Parent(0) { 476 if (!NoImp) 477 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 478 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 479 if (!NoImp) 480 addImplicitDefUseOperands(); 481 // Make sure that we get added to a machine basicblock 482 LeakDetector::addGarbageObject(this); 483 } 484 485 /// MachineInstr ctor - As above, but with a DebugLoc. 486 MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, 487 bool NoImp) 488 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), 489 Parent(0), debugLoc(dl) { 490 if (!NoImp) 491 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 492 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 493 if (!NoImp) 494 addImplicitDefUseOperands(); 495 // Make sure that we get added to a machine basicblock 496 LeakDetector::addGarbageObject(this); 497 } 498 499 /// MachineInstr ctor - Work exactly the same as the ctor two above, except 500 /// that the MachineInstr is created and added to the end of the specified 501 /// basic block. 502 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) 503 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), 504 MemRefs(0), MemRefsEnd(0), Parent(0) { 505 assert(MBB && "Cannot use inserting ctor with null basic block!"); 506 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 507 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 508 addImplicitDefUseOperands(); 509 // Make sure that we get added to a machine basicblock 510 LeakDetector::addGarbageObject(this); 511 MBB->push_back(this); // Add instruction to end of basic block! 512 } 513 514 /// MachineInstr ctor - As above, but with a DebugLoc. 515 /// 516 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, 517 const TargetInstrDesc &tid) 518 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), 519 Parent(0), debugLoc(dl) { 520 assert(MBB && "Cannot use inserting ctor with null basic block!"); 521 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 522 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 523 addImplicitDefUseOperands(); 524 // Make sure that we get added to a machine basicblock 525 LeakDetector::addGarbageObject(this); 526 MBB->push_back(this); // Add instruction to end of basic block! 527 } 528 529 /// MachineInstr ctor - Copies MachineInstr arg exactly 530 /// 531 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 532 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0), 533 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd), 534 Parent(0), debugLoc(MI.getDebugLoc()) { 535 Operands.reserve(MI.getNumOperands()); 536 537 // Add operands 538 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 539 addOperand(MI.getOperand(i)); 540 NumImplicitOps = MI.NumImplicitOps; 541 542 // Set parent to null. 543 Parent = 0; 544 545 LeakDetector::addGarbageObject(this); 546 } 547 548 MachineInstr::~MachineInstr() { 549 LeakDetector::removeGarbageObject(this); 550 #ifndef NDEBUG 551 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 552 assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); 553 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && 554 "Reg operand def/use list corrupted"); 555 } 556 #endif 557 } 558 559 /// getRegInfo - If this instruction is embedded into a MachineFunction, 560 /// return the MachineRegisterInfo object for the current function, otherwise 561 /// return null. 562 MachineRegisterInfo *MachineInstr::getRegInfo() { 563 if (MachineBasicBlock *MBB = getParent()) 564 return &MBB->getParent()->getRegInfo(); 565 return 0; 566 } 567 568 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 569 /// this instruction from their respective use lists. This requires that the 570 /// operands already be on their use lists. 571 void MachineInstr::RemoveRegOperandsFromUseLists() { 572 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 573 if (Operands[i].isReg()) 574 Operands[i].RemoveRegOperandFromRegInfo(); 575 } 576 } 577 578 /// AddRegOperandsToUseLists - Add all of the register operands in 579 /// this instruction from their respective use lists. This requires that the 580 /// operands not be on their use lists yet. 581 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { 582 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 583 if (Operands[i].isReg()) 584 Operands[i].AddRegOperandToRegInfo(&RegInfo); 585 } 586 } 587 588 589 /// addOperand - Add the specified operand to the instruction. If it is an 590 /// implicit operand, it is added to the end of the operand list. If it is 591 /// an explicit operand it is added at the end of the explicit operand list 592 /// (before the first implicit operand). 593 void MachineInstr::addOperand(const MachineOperand &Op) { 594 bool isImpReg = Op.isReg() && Op.isImplicit(); 595 assert((isImpReg || !OperandsComplete()) && 596 "Trying to add an operand to a machine instr that is already done!"); 597 598 MachineRegisterInfo *RegInfo = getRegInfo(); 599 600 // If we are adding the operand to the end of the list, our job is simpler. 601 // This is true most of the time, so this is a reasonable optimization. 602 if (isImpReg || NumImplicitOps == 0) { 603 // We can only do this optimization if we know that the operand list won't 604 // reallocate. 605 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { 606 Operands.push_back(Op); 607 608 // Set the parent of the operand. 609 Operands.back().ParentMI = this; 610 611 // If the operand is a register, update the operand's use list. 612 if (Op.isReg()) { 613 Operands.back().AddRegOperandToRegInfo(RegInfo); 614 // If the register operand is flagged as early, mark the operand as such 615 unsigned OpNo = Operands.size() - 1; 616 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 617 Operands[OpNo].setIsEarlyClobber(true); 618 } 619 return; 620 } 621 } 622 623 // Otherwise, we have to insert a real operand before any implicit ones. 624 unsigned OpNo = Operands.size()-NumImplicitOps; 625 626 // If this instruction isn't embedded into a function, then we don't need to 627 // update any operand lists. 628 if (RegInfo == 0) { 629 // Simple insertion, no reginfo update needed for other register operands. 630 Operands.insert(Operands.begin()+OpNo, Op); 631 Operands[OpNo].ParentMI = this; 632 633 // Do explicitly set the reginfo for this operand though, to ensure the 634 // next/prev fields are properly nulled out. 635 if (Operands[OpNo].isReg()) { 636 Operands[OpNo].AddRegOperandToRegInfo(0); 637 // If the register operand is flagged as early, mark the operand as such 638 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 639 Operands[OpNo].setIsEarlyClobber(true); 640 } 641 642 } else if (Operands.size()+1 <= Operands.capacity()) { 643 // Otherwise, we have to remove register operands from their register use 644 // list, add the operand, then add the register operands back to their use 645 // list. This also must handle the case when the operand list reallocates 646 // to somewhere else. 647 648 // If insertion of this operand won't cause reallocation of the operand 649 // list, just remove the implicit operands, add the operand, then re-add all 650 // the rest of the operands. 651 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 652 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 653 Operands[i].RemoveRegOperandFromRegInfo(); 654 } 655 656 // Add the operand. If it is a register, add it to the reg list. 657 Operands.insert(Operands.begin()+OpNo, Op); 658 Operands[OpNo].ParentMI = this; 659 660 if (Operands[OpNo].isReg()) { 661 Operands[OpNo].AddRegOperandToRegInfo(RegInfo); 662 // If the register operand is flagged as early, mark the operand as such 663 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 664 Operands[OpNo].setIsEarlyClobber(true); 665 } 666 667 // Re-add all the implicit ops. 668 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { 669 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 670 Operands[i].AddRegOperandToRegInfo(RegInfo); 671 } 672 } else { 673 // Otherwise, we will be reallocating the operand list. Remove all reg 674 // operands from their list, then readd them after the operand list is 675 // reallocated. 676 RemoveRegOperandsFromUseLists(); 677 678 Operands.insert(Operands.begin()+OpNo, Op); 679 Operands[OpNo].ParentMI = this; 680 681 // Re-add all the operands. 682 AddRegOperandsToUseLists(*RegInfo); 683 684 // If the register operand is flagged as early, mark the operand as such 685 if (Operands[OpNo].isReg() 686 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 687 Operands[OpNo].setIsEarlyClobber(true); 688 } 689 } 690 691 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 692 /// fewer operand than it started with. 693 /// 694 void MachineInstr::RemoveOperand(unsigned OpNo) { 695 assert(OpNo < Operands.size() && "Invalid operand number"); 696 697 // Special case removing the last one. 698 if (OpNo == Operands.size()-1) { 699 // If needed, remove from the reg def/use list. 700 if (Operands.back().isReg() && Operands.back().isOnRegUseList()) 701 Operands.back().RemoveRegOperandFromRegInfo(); 702 703 Operands.pop_back(); 704 return; 705 } 706 707 // Otherwise, we are removing an interior operand. If we have reginfo to 708 // update, remove all operands that will be shifted down from their reg lists, 709 // move everything down, then re-add them. 710 MachineRegisterInfo *RegInfo = getRegInfo(); 711 if (RegInfo) { 712 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 713 if (Operands[i].isReg()) 714 Operands[i].RemoveRegOperandFromRegInfo(); 715 } 716 } 717 718 Operands.erase(Operands.begin()+OpNo); 719 720 if (RegInfo) { 721 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 722 if (Operands[i].isReg()) 723 Operands[i].AddRegOperandToRegInfo(RegInfo); 724 } 725 } 726 } 727 728 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 729 /// This function should be used only occasionally. The setMemRefs function 730 /// is the primary method for setting up a MachineInstr's MemRefs list. 731 void MachineInstr::addMemOperand(MachineFunction &MF, 732 MachineMemOperand *MO) { 733 mmo_iterator OldMemRefs = MemRefs; 734 mmo_iterator OldMemRefsEnd = MemRefsEnd; 735 736 size_t NewNum = (MemRefsEnd - MemRefs) + 1; 737 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 738 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum; 739 740 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs); 741 NewMemRefs[NewNum - 1] = MO; 742 743 MemRefs = NewMemRefs; 744 MemRefsEnd = NewMemRefsEnd; 745 } 746 747 bool MachineInstr::isIdenticalTo(const MachineInstr *Other, 748 MICheckType Check) const { 749 // If opcodes or number of operands are not the same then the two 750 // instructions are obviously not identical. 751 if (Other->getOpcode() != getOpcode() || 752 Other->getNumOperands() != getNumOperands()) 753 return false; 754 755 // Check operands to make sure they match. 756 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 757 const MachineOperand &MO = getOperand(i); 758 const MachineOperand &OMO = Other->getOperand(i); 759 // Clients may or may not want to ignore defs when testing for equality. 760 // For example, machine CSE pass only cares about finding common 761 // subexpressions, so it's safe to ignore virtual register defs. 762 if (Check != CheckDefs && MO.isReg() && MO.isDef()) { 763 if (Check == IgnoreDefs) 764 continue; 765 // Check == IgnoreVRegDefs 766 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || 767 TargetRegisterInfo::isPhysicalRegister(OMO.getReg())) 768 if (MO.getReg() != OMO.getReg()) 769 return false; 770 } else if (!MO.isIdenticalTo(OMO)) 771 return false; 772 } 773 return true; 774 } 775 776 /// removeFromParent - This method unlinks 'this' from the containing basic 777 /// block, and returns it, but does not delete it. 778 MachineInstr *MachineInstr::removeFromParent() { 779 assert(getParent() && "Not embedded in a basic block!"); 780 getParent()->remove(this); 781 return this; 782 } 783 784 785 /// eraseFromParent - This method unlinks 'this' from the containing basic 786 /// block, and deletes it. 787 void MachineInstr::eraseFromParent() { 788 assert(getParent() && "Not embedded in a basic block!"); 789 getParent()->erase(this); 790 } 791 792 793 /// OperandComplete - Return true if it's illegal to add a new operand 794 /// 795 bool MachineInstr::OperandsComplete() const { 796 unsigned short NumOperands = TID->getNumOperands(); 797 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) 798 return true; // Broken: we have all the operands of this instruction! 799 return false; 800 } 801 802 /// getNumExplicitOperands - Returns the number of non-implicit operands. 803 /// 804 unsigned MachineInstr::getNumExplicitOperands() const { 805 unsigned NumOperands = TID->getNumOperands(); 806 if (!TID->isVariadic()) 807 return NumOperands; 808 809 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 810 const MachineOperand &MO = getOperand(i); 811 if (!MO.isReg() || !MO.isImplicit()) 812 NumOperands++; 813 } 814 return NumOperands; 815 } 816 817 818 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 819 /// the specific register or -1 if it is not found. It further tightens 820 /// the search criteria to a use that kills the register if isKill is true. 821 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 822 const TargetRegisterInfo *TRI) const { 823 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 824 const MachineOperand &MO = getOperand(i); 825 if (!MO.isReg() || !MO.isUse()) 826 continue; 827 unsigned MOReg = MO.getReg(); 828 if (!MOReg) 829 continue; 830 if (MOReg == Reg || 831 (TRI && 832 TargetRegisterInfo::isPhysicalRegister(MOReg) && 833 TargetRegisterInfo::isPhysicalRegister(Reg) && 834 TRI->isSubRegister(MOReg, Reg))) 835 if (!isKill || MO.isKill()) 836 return i; 837 } 838 return -1; 839 } 840 841 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes) 842 /// indicating if this instruction reads or writes Reg. This also considers 843 /// partial defines. 844 std::pair<bool,bool> 845 MachineInstr::readsWritesVirtualRegister(unsigned Reg, 846 SmallVectorImpl<unsigned> *Ops) const { 847 bool PartDef = false; // Partial redefine. 848 bool FullDef = false; // Full define. 849 bool Use = false; 850 851 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 852 const MachineOperand &MO = getOperand(i); 853 if (!MO.isReg() || MO.getReg() != Reg) 854 continue; 855 if (Ops) 856 Ops->push_back(i); 857 if (MO.isUse()) 858 Use |= !MO.isUndef(); 859 else if (MO.getSubReg()) 860 PartDef = true; 861 else 862 FullDef = true; 863 } 864 // A partial redefine uses Reg unless there is also a full define. 865 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef); 866 } 867 868 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 869 /// the specified register or -1 if it is not found. If isDead is true, defs 870 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 871 /// also checks if there is a def of a super-register. 872 int 873 MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap, 874 const TargetRegisterInfo *TRI) const { 875 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg); 876 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 877 const MachineOperand &MO = getOperand(i); 878 if (!MO.isReg() || !MO.isDef()) 879 continue; 880 unsigned MOReg = MO.getReg(); 881 bool Found = (MOReg == Reg); 882 if (!Found && TRI && isPhys && 883 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 884 if (Overlap) 885 Found = TRI->regsOverlap(MOReg, Reg); 886 else 887 Found = TRI->isSubRegister(MOReg, Reg); 888 } 889 if (Found && (!isDead || MO.isDead())) 890 return i; 891 } 892 return -1; 893 } 894 895 /// findFirstPredOperandIdx() - Find the index of the first operand in the 896 /// operand list that is used to represent the predicate. It returns -1 if 897 /// none is found. 898 int MachineInstr::findFirstPredOperandIdx() const { 899 const TargetInstrDesc &TID = getDesc(); 900 if (TID.isPredicable()) { 901 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 902 if (TID.OpInfo[i].isPredicate()) 903 return i; 904 } 905 906 return -1; 907 } 908 909 /// isRegTiedToUseOperand - Given the index of a register def operand, 910 /// check if the register def is tied to a source operand, due to either 911 /// two-address elimination or inline assembly constraints. Returns the 912 /// first tied use operand index by reference is UseOpIdx is not null. 913 bool MachineInstr:: 914 isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const { 915 if (isInlineAsm()) { 916 assert(DefOpIdx >= 3); 917 const MachineOperand &MO = getOperand(DefOpIdx); 918 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) 919 return false; 920 // Determine the actual operand index that corresponds to this index. 921 unsigned DefNo = 0; 922 unsigned DefPart = 0; 923 for (unsigned i = 2, e = getNumOperands(); i < e; ) { 924 const MachineOperand &FMO = getOperand(i); 925 // After the normal asm operands there may be additional imp-def regs. 926 if (!FMO.isImm()) 927 return false; 928 // Skip over this def. 929 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm()); 930 unsigned PrevDef = i + 1; 931 i = PrevDef + NumOps; 932 if (i > DefOpIdx) { 933 DefPart = DefOpIdx - PrevDef; 934 break; 935 } 936 ++DefNo; 937 } 938 for (unsigned i = 2, e = getNumOperands(); i != e; ++i) { 939 const MachineOperand &FMO = getOperand(i); 940 if (!FMO.isImm()) 941 continue; 942 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse()) 943 continue; 944 unsigned Idx; 945 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) && 946 Idx == DefNo) { 947 if (UseOpIdx) 948 *UseOpIdx = (unsigned)i + 1 + DefPart; 949 return true; 950 } 951 } 952 return false; 953 } 954 955 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!"); 956 const TargetInstrDesc &TID = getDesc(); 957 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { 958 const MachineOperand &MO = getOperand(i); 959 if (MO.isReg() && MO.isUse() && 960 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) { 961 if (UseOpIdx) 962 *UseOpIdx = (unsigned)i; 963 return true; 964 } 965 } 966 return false; 967 } 968 969 /// isRegTiedToDefOperand - Return true if the operand of the specified index 970 /// is a register use and it is tied to an def operand. It also returns the def 971 /// operand index by reference. 972 bool MachineInstr:: 973 isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const { 974 if (isInlineAsm()) { 975 const MachineOperand &MO = getOperand(UseOpIdx); 976 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) 977 return false; 978 979 // Find the flag operand corresponding to UseOpIdx 980 unsigned FlagIdx, NumOps=0; 981 for (FlagIdx = 2; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) { 982 const MachineOperand &UFMO = getOperand(FlagIdx); 983 // After the normal asm operands there may be additional imp-def regs. 984 if (!UFMO.isImm()) 985 return false; 986 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm()); 987 assert(NumOps < getNumOperands() && "Invalid inline asm flag"); 988 if (UseOpIdx < FlagIdx+NumOps+1) 989 break; 990 } 991 if (FlagIdx >= UseOpIdx) 992 return false; 993 const MachineOperand &UFMO = getOperand(FlagIdx); 994 unsigned DefNo; 995 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) { 996 if (!DefOpIdx) 997 return true; 998 999 unsigned DefIdx = 2; 1000 // Remember to adjust the index. First operand is asm string, second is 1001 // the AlignStack bit, then there is a flag for each. 1002 while (DefNo) { 1003 const MachineOperand &FMO = getOperand(DefIdx); 1004 assert(FMO.isImm()); 1005 // Skip over this def. 1006 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1; 1007 --DefNo; 1008 } 1009 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx; 1010 return true; 1011 } 1012 return false; 1013 } 1014 1015 const TargetInstrDesc &TID = getDesc(); 1016 if (UseOpIdx >= TID.getNumOperands()) 1017 return false; 1018 const MachineOperand &MO = getOperand(UseOpIdx); 1019 if (!MO.isReg() || !MO.isUse()) 1020 return false; 1021 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO); 1022 if (DefIdx == -1) 1023 return false; 1024 if (DefOpIdx) 1025 *DefOpIdx = (unsigned)DefIdx; 1026 return true; 1027 } 1028 1029 /// clearKillInfo - Clears kill flags on all operands. 1030 /// 1031 void MachineInstr::clearKillInfo() { 1032 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1033 MachineOperand &MO = getOperand(i); 1034 if (MO.isReg() && MO.isUse()) 1035 MO.setIsKill(false); 1036 } 1037 } 1038 1039 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 1040 /// 1041 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { 1042 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1043 const MachineOperand &MO = MI->getOperand(i); 1044 if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) 1045 continue; 1046 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { 1047 MachineOperand &MOp = getOperand(j); 1048 if (!MOp.isIdenticalTo(MO)) 1049 continue; 1050 if (MO.isKill()) 1051 MOp.setIsKill(); 1052 else 1053 MOp.setIsDead(); 1054 break; 1055 } 1056 } 1057 } 1058 1059 /// copyPredicates - Copies predicate operand(s) from MI. 1060 void MachineInstr::copyPredicates(const MachineInstr *MI) { 1061 const TargetInstrDesc &TID = MI->getDesc(); 1062 if (!TID.isPredicable()) 1063 return; 1064 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1065 if (TID.OpInfo[i].isPredicate()) { 1066 // Predicated operands must be last operands. 1067 addOperand(MI->getOperand(i)); 1068 } 1069 } 1070 } 1071 1072 void MachineInstr::substituteRegister(unsigned FromReg, 1073 unsigned ToReg, 1074 unsigned SubIdx, 1075 const TargetRegisterInfo &RegInfo) { 1076 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 1077 if (SubIdx) 1078 ToReg = RegInfo.getSubReg(ToReg, SubIdx); 1079 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1080 MachineOperand &MO = getOperand(i); 1081 if (!MO.isReg() || MO.getReg() != FromReg) 1082 continue; 1083 MO.substPhysReg(ToReg, RegInfo); 1084 } 1085 } else { 1086 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1087 MachineOperand &MO = getOperand(i); 1088 if (!MO.isReg() || MO.getReg() != FromReg) 1089 continue; 1090 MO.substVirtReg(ToReg, SubIdx, RegInfo); 1091 } 1092 } 1093 } 1094 1095 /// isSafeToMove - Return true if it is safe to move this instruction. If 1096 /// SawStore is set to true, it means that there is a store (or call) between 1097 /// the instruction's location and its intended destination. 1098 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 1099 AliasAnalysis *AA, 1100 bool &SawStore) const { 1101 // Ignore stuff that we obviously can't move. 1102 if (TID->mayStore() || TID->isCall()) { 1103 SawStore = true; 1104 return false; 1105 } 1106 if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) 1107 return false; 1108 1109 // See if this instruction does a load. If so, we have to guarantee that the 1110 // loaded value doesn't change between the load and the its intended 1111 // destination. The check for isInvariantLoad gives the targe the chance to 1112 // classify the load as always returning a constant, e.g. a constant pool 1113 // load. 1114 if (TID->mayLoad() && !isInvariantLoad(AA)) 1115 // Otherwise, this is a real load. If there is a store between the load and 1116 // end of block, or if the load is volatile, we can't move it. 1117 return !SawStore && !hasVolatileMemoryRef(); 1118 1119 return true; 1120 } 1121 1122 /// isSafeToReMat - Return true if it's safe to rematerialize the specified 1123 /// instruction which defined the specified register instead of copying it. 1124 bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, 1125 AliasAnalysis *AA, 1126 unsigned DstReg) const { 1127 bool SawStore = false; 1128 if (!TII->isTriviallyReMaterializable(this, AA) || 1129 !isSafeToMove(TII, AA, SawStore)) 1130 return false; 1131 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1132 const MachineOperand &MO = getOperand(i); 1133 if (!MO.isReg()) 1134 continue; 1135 // FIXME: For now, do not remat any instruction with register operands. 1136 // Later on, we can loosen the restriction is the register operands have 1137 // not been modified between the def and use. Note, this is different from 1138 // MachineSink because the code is no longer in two-address form (at least 1139 // partially). 1140 if (MO.isUse()) 1141 return false; 1142 else if (!MO.isDead() && MO.getReg() != DstReg) 1143 return false; 1144 } 1145 return true; 1146 } 1147 1148 /// hasVolatileMemoryRef - Return true if this instruction may have a 1149 /// volatile memory reference, or if the information describing the 1150 /// memory reference is not available. Return false if it is known to 1151 /// have no volatile memory references. 1152 bool MachineInstr::hasVolatileMemoryRef() const { 1153 // An instruction known never to access memory won't have a volatile access. 1154 if (!TID->mayStore() && 1155 !TID->mayLoad() && 1156 !TID->isCall() && 1157 !TID->hasUnmodeledSideEffects()) 1158 return false; 1159 1160 // Otherwise, if the instruction has no memory reference information, 1161 // conservatively assume it wasn't preserved. 1162 if (memoperands_empty()) 1163 return true; 1164 1165 // Check the memory reference information for volatile references. 1166 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) 1167 if ((*I)->isVolatile()) 1168 return true; 1169 1170 return false; 1171 } 1172 1173 /// isInvariantLoad - Return true if this instruction is loading from a 1174 /// location whose value is invariant across the function. For example, 1175 /// loading a value from the constant pool or from the argument area 1176 /// of a function if it does not change. This should only return true of 1177 /// *all* loads the instruction does are invariant (if it does multiple loads). 1178 bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { 1179 // If the instruction doesn't load at all, it isn't an invariant load. 1180 if (!TID->mayLoad()) 1181 return false; 1182 1183 // If the instruction has lost its memoperands, conservatively assume that 1184 // it may not be an invariant load. 1185 if (memoperands_empty()) 1186 return false; 1187 1188 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); 1189 1190 for (mmo_iterator I = memoperands_begin(), 1191 E = memoperands_end(); I != E; ++I) { 1192 if ((*I)->isVolatile()) return false; 1193 if ((*I)->isStore()) return false; 1194 1195 if (const Value *V = (*I)->getValue()) { 1196 // A load from a constant PseudoSourceValue is invariant. 1197 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) 1198 if (PSV->isConstant(MFI)) 1199 continue; 1200 // If we have an AliasAnalysis, ask it whether the memory is constant. 1201 if (AA && AA->pointsToConstantMemory(V)) 1202 continue; 1203 } 1204 1205 // Otherwise assume conservatively. 1206 return false; 1207 } 1208 1209 // Everything checks out. 1210 return true; 1211 } 1212 1213 /// isConstantValuePHI - If the specified instruction is a PHI that always 1214 /// merges together the same virtual register, return the register, otherwise 1215 /// return 0. 1216 unsigned MachineInstr::isConstantValuePHI() const { 1217 if (!isPHI()) 1218 return 0; 1219 assert(getNumOperands() >= 3 && 1220 "It's illegal to have a PHI without source operands"); 1221 1222 unsigned Reg = getOperand(1).getReg(); 1223 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) 1224 if (getOperand(i).getReg() != Reg) 1225 return 0; 1226 return Reg; 1227 } 1228 1229 /// allDefsAreDead - Return true if all the defs of this instruction are dead. 1230 /// 1231 bool MachineInstr::allDefsAreDead() const { 1232 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) { 1233 const MachineOperand &MO = getOperand(i); 1234 if (!MO.isReg() || MO.isUse()) 1235 continue; 1236 if (!MO.isDead()) 1237 return false; 1238 } 1239 return true; 1240 } 1241 1242 void MachineInstr::dump() const { 1243 dbgs() << " " << *this; 1244 } 1245 1246 static void printDebugLoc(DebugLoc DL, const MachineFunction *MF, 1247 raw_ostream &CommentOS) { 1248 const LLVMContext &Ctx = MF->getFunction()->getContext(); 1249 if (!DL.isUnknown()) { // Print source line info. 1250 DIScope Scope(DL.getScope(Ctx)); 1251 // Omit the directory, because it's likely to be long and uninteresting. 1252 if (Scope.Verify()) 1253 CommentOS << Scope.getFilename(); 1254 else 1255 CommentOS << "<unknown>"; 1256 CommentOS << ':' << DL.getLine(); 1257 if (DL.getCol() != 0) 1258 CommentOS << ':' << DL.getCol(); 1259 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); 1260 if (!InlinedAtDL.isUnknown()) { 1261 CommentOS << " @[ "; 1262 printDebugLoc(InlinedAtDL, MF, CommentOS); 1263 CommentOS << " ]"; 1264 } 1265 } 1266 } 1267 1268 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { 1269 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction. 1270 const MachineFunction *MF = 0; 1271 const MachineRegisterInfo *MRI = 0; 1272 if (const MachineBasicBlock *MBB = getParent()) { 1273 MF = MBB->getParent(); 1274 if (!TM && MF) 1275 TM = &MF->getTarget(); 1276 if (MF) 1277 MRI = &MF->getRegInfo(); 1278 } 1279 1280 // Save a list of virtual registers. 1281 SmallVector<unsigned, 8> VirtRegs; 1282 1283 // Print explicitly defined operands on the left of an assignment syntax. 1284 unsigned StartOp = 0, e = getNumOperands(); 1285 for (; StartOp < e && getOperand(StartOp).isReg() && 1286 getOperand(StartOp).isDef() && 1287 !getOperand(StartOp).isImplicit(); 1288 ++StartOp) { 1289 if (StartOp != 0) OS << ", "; 1290 getOperand(StartOp).print(OS, TM); 1291 unsigned Reg = getOperand(StartOp).getReg(); 1292 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) 1293 VirtRegs.push_back(Reg); 1294 } 1295 1296 if (StartOp != 0) 1297 OS << " = "; 1298 1299 // Print the opcode name. 1300 OS << getDesc().getName(); 1301 1302 // Print the rest of the operands. 1303 bool OmittedAnyCallClobbers = false; 1304 bool FirstOp = true; 1305 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 1306 const MachineOperand &MO = getOperand(i); 1307 1308 if (MO.isReg() && MO.getReg() && 1309 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1310 VirtRegs.push_back(MO.getReg()); 1311 1312 // Omit call-clobbered registers which aren't used anywhere. This makes 1313 // call instructions much less noisy on targets where calls clobber lots 1314 // of registers. Don't rely on MO.isDead() because we may be called before 1315 // LiveVariables is run, or we may be looking at a non-allocatable reg. 1316 if (MF && getDesc().isCall() && 1317 MO.isReg() && MO.isImplicit() && MO.isDef()) { 1318 unsigned Reg = MO.getReg(); 1319 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { 1320 const MachineRegisterInfo &MRI = MF->getRegInfo(); 1321 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) { 1322 bool HasAliasLive = false; 1323 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg); 1324 unsigned AliasReg = *Alias; ++Alias) 1325 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) { 1326 HasAliasLive = true; 1327 break; 1328 } 1329 if (!HasAliasLive) { 1330 OmittedAnyCallClobbers = true; 1331 continue; 1332 } 1333 } 1334 } 1335 } 1336 1337 if (FirstOp) FirstOp = false; else OS << ","; 1338 OS << " "; 1339 if (i < getDesc().NumOperands) { 1340 const TargetOperandInfo &TOI = getDesc().OpInfo[i]; 1341 if (TOI.isPredicate()) 1342 OS << "pred:"; 1343 if (TOI.isOptionalDef()) 1344 OS << "opt:"; 1345 } 1346 if (isDebugValue() && MO.isMetadata()) { 1347 // Pretty print DBG_VALUE instructions. 1348 const MDNode *MD = MO.getMetadata(); 1349 if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2))) 1350 OS << "!\"" << MDS->getString() << '\"'; 1351 else 1352 MO.print(OS, TM); 1353 } else if (TM && (isInsertSubreg() || isRegSequence()) && MO.isImm()) { 1354 OS << TM->getRegisterInfo()->getSubRegIndexName(MO.getImm()); 1355 } else 1356 MO.print(OS, TM); 1357 } 1358 1359 // Briefly indicate whether any call clobbers were omitted. 1360 if (OmittedAnyCallClobbers) { 1361 if (!FirstOp) OS << ","; 1362 OS << " ..."; 1363 } 1364 1365 bool HaveSemi = false; 1366 if (!memoperands_empty()) { 1367 if (!HaveSemi) OS << ";"; HaveSemi = true; 1368 1369 OS << " mem:"; 1370 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 1371 i != e; ++i) { 1372 OS << **i; 1373 if (llvm::next(i) != e) 1374 OS << " "; 1375 } 1376 } 1377 1378 // Print the regclass of any virtual registers encountered. 1379 if (MRI && !VirtRegs.empty()) { 1380 if (!HaveSemi) OS << ";"; HaveSemi = true; 1381 for (unsigned i = 0; i != VirtRegs.size(); ++i) { 1382 const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]); 1383 OS << " " << RC->getName() << ":%reg" << VirtRegs[i]; 1384 for (unsigned j = i+1; j != VirtRegs.size();) { 1385 if (MRI->getRegClass(VirtRegs[j]) != RC) { 1386 ++j; 1387 continue; 1388 } 1389 if (VirtRegs[i] != VirtRegs[j]) 1390 OS << "," << VirtRegs[j]; 1391 VirtRegs.erase(VirtRegs.begin()+j); 1392 } 1393 } 1394 } 1395 1396 if (!debugLoc.isUnknown() && MF) { 1397 if (!HaveSemi) OS << ";"; 1398 OS << " dbg:"; 1399 printDebugLoc(debugLoc, MF, OS); 1400 } 1401 1402 OS << "\n"; 1403 } 1404 1405 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 1406 const TargetRegisterInfo *RegInfo, 1407 bool AddIfNotFound) { 1408 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1409 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1410 bool Found = false; 1411 SmallVector<unsigned,4> DeadOps; 1412 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1413 MachineOperand &MO = getOperand(i); 1414 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 1415 continue; 1416 unsigned Reg = MO.getReg(); 1417 if (!Reg) 1418 continue; 1419 1420 if (Reg == IncomingReg) { 1421 if (!Found) { 1422 if (MO.isKill()) 1423 // The register is already marked kill. 1424 return true; 1425 if (isPhysReg && isRegTiedToDefOperand(i)) 1426 // Two-address uses of physregs must not be marked kill. 1427 return true; 1428 MO.setIsKill(); 1429 Found = true; 1430 } 1431 } else if (hasAliases && MO.isKill() && 1432 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1433 // A super-register kill already exists. 1434 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1435 return true; 1436 if (RegInfo->isSubRegister(IncomingReg, Reg)) 1437 DeadOps.push_back(i); 1438 } 1439 } 1440 1441 // Trim unneeded kill operands. 1442 while (!DeadOps.empty()) { 1443 unsigned OpIdx = DeadOps.back(); 1444 if (getOperand(OpIdx).isImplicit()) 1445 RemoveOperand(OpIdx); 1446 else 1447 getOperand(OpIdx).setIsKill(false); 1448 DeadOps.pop_back(); 1449 } 1450 1451 // If not found, this means an alias of one of the operands is killed. Add a 1452 // new implicit operand if required. 1453 if (!Found && AddIfNotFound) { 1454 addOperand(MachineOperand::CreateReg(IncomingReg, 1455 false /*IsDef*/, 1456 true /*IsImp*/, 1457 true /*IsKill*/)); 1458 return true; 1459 } 1460 return Found; 1461 } 1462 1463 bool MachineInstr::addRegisterDead(unsigned IncomingReg, 1464 const TargetRegisterInfo *RegInfo, 1465 bool AddIfNotFound) { 1466 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1467 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1468 bool Found = false; 1469 SmallVector<unsigned,4> DeadOps; 1470 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1471 MachineOperand &MO = getOperand(i); 1472 if (!MO.isReg() || !MO.isDef()) 1473 continue; 1474 unsigned Reg = MO.getReg(); 1475 if (!Reg) 1476 continue; 1477 1478 if (Reg == IncomingReg) { 1479 if (!Found) { 1480 if (MO.isDead()) 1481 // The register is already marked dead. 1482 return true; 1483 MO.setIsDead(); 1484 Found = true; 1485 } 1486 } else if (hasAliases && MO.isDead() && 1487 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1488 // There exists a super-register that's marked dead. 1489 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1490 return true; 1491 if (RegInfo->getSubRegisters(IncomingReg) && 1492 RegInfo->getSuperRegisters(Reg) && 1493 RegInfo->isSubRegister(IncomingReg, Reg)) 1494 DeadOps.push_back(i); 1495 } 1496 } 1497 1498 // Trim unneeded dead operands. 1499 while (!DeadOps.empty()) { 1500 unsigned OpIdx = DeadOps.back(); 1501 if (getOperand(OpIdx).isImplicit()) 1502 RemoveOperand(OpIdx); 1503 else 1504 getOperand(OpIdx).setIsDead(false); 1505 DeadOps.pop_back(); 1506 } 1507 1508 // If not found, this means an alias of one of the operands is dead. Add a 1509 // new implicit operand if required. 1510 if (Found || !AddIfNotFound) 1511 return Found; 1512 1513 addOperand(MachineOperand::CreateReg(IncomingReg, 1514 true /*IsDef*/, 1515 true /*IsImp*/, 1516 false /*IsKill*/, 1517 true /*IsDead*/)); 1518 return true; 1519 } 1520 1521 void MachineInstr::addRegisterDefined(unsigned IncomingReg, 1522 const TargetRegisterInfo *RegInfo) { 1523 if (TargetRegisterInfo::isPhysicalRegister(IncomingReg)) { 1524 MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo); 1525 if (MO) 1526 return; 1527 } else { 1528 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1529 const MachineOperand &MO = getOperand(i); 1530 if (MO.isReg() && MO.getReg() == IncomingReg && MO.isDef() && 1531 MO.getSubReg() == 0) 1532 return; 1533 } 1534 } 1535 addOperand(MachineOperand::CreateReg(IncomingReg, 1536 true /*IsDef*/, 1537 true /*IsImp*/)); 1538 } 1539 1540 void MachineInstr::setPhysRegsDeadExcept(const SmallVectorImpl<unsigned> &UsedRegs, 1541 const TargetRegisterInfo &TRI) { 1542 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1543 MachineOperand &MO = getOperand(i); 1544 if (!MO.isReg() || !MO.isDef()) continue; 1545 unsigned Reg = MO.getReg(); 1546 if (Reg == 0) continue; 1547 bool Dead = true; 1548 for (SmallVectorImpl<unsigned>::const_iterator I = UsedRegs.begin(), 1549 E = UsedRegs.end(); I != E; ++I) 1550 if (TRI.regsOverlap(*I, Reg)) { 1551 Dead = false; 1552 break; 1553 } 1554 // If there are no uses, including partial uses, the def is dead. 1555 if (Dead) MO.setIsDead(); 1556 } 1557 } 1558 1559 unsigned 1560 MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) { 1561 unsigned Hash = MI->getOpcode() * 37; 1562 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1563 const MachineOperand &MO = MI->getOperand(i); 1564 uint64_t Key = (uint64_t)MO.getType() << 32; 1565 switch (MO.getType()) { 1566 default: break; 1567 case MachineOperand::MO_Register: 1568 if (MO.isDef() && MO.getReg() && 1569 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1570 continue; // Skip virtual register defs. 1571 Key |= MO.getReg(); 1572 break; 1573 case MachineOperand::MO_Immediate: 1574 Key |= MO.getImm(); 1575 break; 1576 case MachineOperand::MO_FrameIndex: 1577 case MachineOperand::MO_ConstantPoolIndex: 1578 case MachineOperand::MO_JumpTableIndex: 1579 Key |= MO.getIndex(); 1580 break; 1581 case MachineOperand::MO_MachineBasicBlock: 1582 Key |= DenseMapInfo<void*>::getHashValue(MO.getMBB()); 1583 break; 1584 case MachineOperand::MO_GlobalAddress: 1585 Key |= DenseMapInfo<void*>::getHashValue(MO.getGlobal()); 1586 break; 1587 case MachineOperand::MO_BlockAddress: 1588 Key |= DenseMapInfo<void*>::getHashValue(MO.getBlockAddress()); 1589 break; 1590 case MachineOperand::MO_MCSymbol: 1591 Key |= DenseMapInfo<void*>::getHashValue(MO.getMCSymbol()); 1592 break; 1593 } 1594 Key += ~(Key << 32); 1595 Key ^= (Key >> 22); 1596 Key += ~(Key << 13); 1597 Key ^= (Key >> 8); 1598 Key += (Key << 3); 1599 Key ^= (Key >> 15); 1600 Key += ~(Key << 27); 1601 Key ^= (Key >> 31); 1602 Hash = (unsigned)Key + Hash * 37; 1603 } 1604 return Hash; 1605 } 1606