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/InlineAsm.h" 17 #include "llvm/Value.h" 18 #include "llvm/Assembly/Writer.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineMemOperand.h" 21 #include "llvm/CodeGen/MachineRegisterInfo.h" 22 #include "llvm/CodeGen/PseudoSourceValue.h" 23 #include "llvm/Target/TargetMachine.h" 24 #include "llvm/Target/TargetInstrInfo.h" 25 #include "llvm/Target/TargetInstrDesc.h" 26 #include "llvm/Target/TargetRegisterInfo.h" 27 #include "llvm/Analysis/DebugInfo.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include "llvm/Support/LeakDetector.h" 30 #include "llvm/Support/MathExtras.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include "llvm/ADT/FoldingSet.h" 33 using namespace llvm; 34 35 //===----------------------------------------------------------------------===// 36 // MachineOperand Implementation 37 //===----------------------------------------------------------------------===// 38 39 /// AddRegOperandToRegInfo - Add this register operand to the specified 40 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 41 /// explicitly nulled out. 42 void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { 43 assert(isReg() && "Can only add reg operand to use lists"); 44 45 // If the reginfo pointer is null, just explicitly null out or next/prev 46 // pointers, to ensure they are not garbage. 47 if (RegInfo == 0) { 48 Contents.Reg.Prev = 0; 49 Contents.Reg.Next = 0; 50 return; 51 } 52 53 // Otherwise, add this operand to the head of the registers use/def list. 54 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); 55 56 // For SSA values, we prefer to keep the definition at the start of the list. 57 // we do this by skipping over the definition if it is at the head of the 58 // list. 59 if (*Head && (*Head)->isDef()) 60 Head = &(*Head)->Contents.Reg.Next; 61 62 Contents.Reg.Next = *Head; 63 if (Contents.Reg.Next) { 64 assert(getReg() == Contents.Reg.Next->getReg() && 65 "Different regs on the same list!"); 66 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; 67 } 68 69 Contents.Reg.Prev = Head; 70 *Head = this; 71 } 72 73 /// RemoveRegOperandFromRegInfo - Remove this register operand from the 74 /// MachineRegisterInfo it is linked with. 75 void MachineOperand::RemoveRegOperandFromRegInfo() { 76 assert(isOnRegUseList() && "Reg operand is not on a use list"); 77 // Unlink this from the doubly linked list of operands. 78 MachineOperand *NextOp = Contents.Reg.Next; 79 *Contents.Reg.Prev = NextOp; 80 if (NextOp) { 81 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); 82 NextOp->Contents.Reg.Prev = Contents.Reg.Prev; 83 } 84 Contents.Reg.Prev = 0; 85 Contents.Reg.Next = 0; 86 } 87 88 void MachineOperand::setReg(unsigned Reg) { 89 if (getReg() == Reg) return; // No change. 90 91 // Otherwise, we have to change the register. If this operand is embedded 92 // into a machine function, we need to update the old and new register's 93 // use/def lists. 94 if (MachineInstr *MI = getParent()) 95 if (MachineBasicBlock *MBB = MI->getParent()) 96 if (MachineFunction *MF = MBB->getParent()) { 97 RemoveRegOperandFromRegInfo(); 98 Contents.Reg.RegNo = Reg; 99 AddRegOperandToRegInfo(&MF->getRegInfo()); 100 return; 101 } 102 103 // Otherwise, just change the register, no problem. :) 104 Contents.Reg.RegNo = Reg; 105 } 106 107 /// ChangeToImmediate - Replace this operand with a new immediate operand of 108 /// the specified value. If an operand is known to be an immediate already, 109 /// the setImm method should be used. 110 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 111 // If this operand is currently a register operand, and if this is in a 112 // function, deregister the operand from the register's use/def list. 113 if (isReg() && getParent() && getParent()->getParent() && 114 getParent()->getParent()->getParent()) 115 RemoveRegOperandFromRegInfo(); 116 117 OpKind = MO_Immediate; 118 Contents.ImmVal = ImmVal; 119 } 120 121 /// ChangeToRegister - Replace this operand with a new register operand of 122 /// the specified value. If an operand is known to be an register already, 123 /// the setReg method should be used. 124 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 125 bool isKill, bool isDead, bool isUndef) { 126 // If this operand is already a register operand, use setReg to update the 127 // register's use/def lists. 128 if (isReg()) { 129 assert(!isEarlyClobber()); 130 setReg(Reg); 131 } else { 132 // Otherwise, change this to a register and set the reg#. 133 OpKind = MO_Register; 134 Contents.Reg.RegNo = Reg; 135 136 // If this operand is embedded in a function, add the operand to the 137 // register's use/def list. 138 if (MachineInstr *MI = getParent()) 139 if (MachineBasicBlock *MBB = MI->getParent()) 140 if (MachineFunction *MF = MBB->getParent()) 141 AddRegOperandToRegInfo(&MF->getRegInfo()); 142 } 143 144 IsDef = isDef; 145 IsImp = isImp; 146 IsKill = isKill; 147 IsDead = isDead; 148 IsUndef = isUndef; 149 IsEarlyClobber = false; 150 SubReg = 0; 151 } 152 153 /// isIdenticalTo - Return true if this operand is identical to the specified 154 /// operand. 155 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 156 if (getType() != Other.getType() || 157 getTargetFlags() != Other.getTargetFlags()) 158 return false; 159 160 switch (getType()) { 161 default: llvm_unreachable("Unrecognized operand type"); 162 case MachineOperand::MO_Register: 163 return getReg() == Other.getReg() && isDef() == Other.isDef() && 164 getSubReg() == Other.getSubReg(); 165 case MachineOperand::MO_Immediate: 166 return getImm() == Other.getImm(); 167 case MachineOperand::MO_FPImmediate: 168 return getFPImm() == Other.getFPImm(); 169 case MachineOperand::MO_MachineBasicBlock: 170 return getMBB() == Other.getMBB(); 171 case MachineOperand::MO_FrameIndex: 172 return getIndex() == Other.getIndex(); 173 case MachineOperand::MO_ConstantPoolIndex: 174 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 175 case MachineOperand::MO_JumpTableIndex: 176 return getIndex() == Other.getIndex(); 177 case MachineOperand::MO_GlobalAddress: 178 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 179 case MachineOperand::MO_ExternalSymbol: 180 return !strcmp(getSymbolName(), Other.getSymbolName()) && 181 getOffset() == Other.getOffset(); 182 } 183 } 184 185 /// print - Print the specified machine operand. 186 /// 187 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 188 switch (getType()) { 189 case MachineOperand::MO_Register: 190 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { 191 OS << "%reg" << getReg(); 192 } else { 193 // If the instruction is embedded into a basic block, we can find the 194 // target info for the instruction. 195 if (TM == 0) 196 if (const MachineInstr *MI = getParent()) 197 if (const MachineBasicBlock *MBB = MI->getParent()) 198 if (const MachineFunction *MF = MBB->getParent()) 199 TM = &MF->getTarget(); 200 201 if (TM) 202 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; 203 else 204 OS << "%mreg" << getReg(); 205 } 206 207 if (getSubReg() != 0) 208 OS << ':' << getSubReg(); 209 210 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 211 isEarlyClobber()) { 212 OS << '<'; 213 bool NeedComma = false; 214 if (isImplicit()) { 215 if (NeedComma) OS << ','; 216 OS << (isDef() ? "imp-def" : "imp-use"); 217 NeedComma = true; 218 } else if (isDef()) { 219 if (NeedComma) OS << ','; 220 if (isEarlyClobber()) 221 OS << "earlyclobber,"; 222 OS << "def"; 223 NeedComma = true; 224 } 225 if (isKill() || isDead() || isUndef()) { 226 if (NeedComma) OS << ','; 227 if (isKill()) OS << "kill"; 228 if (isDead()) OS << "dead"; 229 if (isUndef()) { 230 if (isKill() || isDead()) 231 OS << ','; 232 OS << "undef"; 233 } 234 } 235 OS << '>'; 236 } 237 break; 238 case MachineOperand::MO_Immediate: 239 OS << getImm(); 240 break; 241 case MachineOperand::MO_FPImmediate: 242 if (getFPImm()->getType() == Type::getFloatTy(getFPImm()->getContext())) 243 OS << getFPImm()->getValueAPF().convertToFloat(); 244 else 245 OS << getFPImm()->getValueAPF().convertToDouble(); 246 break; 247 case MachineOperand::MO_MachineBasicBlock: 248 OS << "mbb<" 249 << ((Value*)getMBB()->getBasicBlock())->getName() 250 << "," << (void*)getMBB() << '>'; 251 break; 252 case MachineOperand::MO_FrameIndex: 253 OS << "<fi#" << getIndex() << '>'; 254 break; 255 case MachineOperand::MO_ConstantPoolIndex: 256 OS << "<cp#" << getIndex(); 257 if (getOffset()) OS << "+" << getOffset(); 258 OS << '>'; 259 break; 260 case MachineOperand::MO_JumpTableIndex: 261 OS << "<jt#" << getIndex() << '>'; 262 break; 263 case MachineOperand::MO_GlobalAddress: 264 OS << "<ga:" << ((Value*)getGlobal())->getName(); 265 if (getOffset()) OS << "+" << getOffset(); 266 OS << '>'; 267 break; 268 case MachineOperand::MO_ExternalSymbol: 269 OS << "<es:" << getSymbolName(); 270 if (getOffset()) OS << "+" << getOffset(); 271 OS << '>'; 272 break; 273 default: 274 llvm_unreachable("Unrecognized operand type"); 275 } 276 277 if (unsigned TF = getTargetFlags()) 278 OS << "[TF=" << TF << ']'; 279 } 280 281 //===----------------------------------------------------------------------===// 282 // MachineMemOperand Implementation 283 //===----------------------------------------------------------------------===// 284 285 MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f, 286 int64_t o, uint64_t s, unsigned int a) 287 : Offset(o), Size(s), V(v), 288 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) { 289 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 290 assert((isLoad() || isStore()) && "Not a load/store!"); 291 } 292 293 /// Profile - Gather unique data for the object. 294 /// 295 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 296 ID.AddInteger(Offset); 297 ID.AddInteger(Size); 298 ID.AddPointer(V); 299 ID.AddInteger(Flags); 300 } 301 302 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 303 // The Value and Offset may differ due to CSE. But the flags and size 304 // should be the same. 305 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 306 assert(MMO->getSize() == getSize() && "Size mismatch!"); 307 308 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 309 // Update the alignment value. 310 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3); 311 // Also update the base and offset, because the new alignment may 312 // not be applicable with the old ones. 313 V = MMO->getValue(); 314 Offset = MMO->getOffset(); 315 } 316 } 317 318 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { 319 assert((MMO.isLoad() || MMO.isStore()) && 320 "SV has to be a load, store or both."); 321 322 if (MMO.isVolatile()) 323 OS << "Volatile "; 324 325 if (MMO.isLoad()) 326 OS << "LD"; 327 if (MMO.isStore()) 328 OS << "ST"; 329 OS << MMO.getSize(); 330 331 // Print the address information. 332 OS << "["; 333 if (!MMO.getValue()) 334 OS << "<unknown>"; 335 else 336 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false); 337 338 // If the alignment of the memory reference itself differs from the alignment 339 // of the base pointer, print the base alignment explicitly, next to the base 340 // pointer. 341 if (MMO.getBaseAlignment() != MMO.getAlignment()) 342 OS << "(align=" << MMO.getBaseAlignment() << ")"; 343 344 if (MMO.getOffset() != 0) 345 OS << "+" << MMO.getOffset(); 346 OS << "]"; 347 348 // Print the alignment of the reference. 349 if (MMO.getBaseAlignment() != MMO.getAlignment() || 350 MMO.getBaseAlignment() != MMO.getSize()) 351 OS << "(align=" << MMO.getAlignment() << ")"; 352 353 return OS; 354 } 355 356 //===----------------------------------------------------------------------===// 357 // MachineInstr Implementation 358 //===----------------------------------------------------------------------===// 359 360 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 361 /// TID NULL and no operands. 362 MachineInstr::MachineInstr() 363 : TID(0), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), 364 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) { 365 // Make sure that we get added to a machine basicblock 366 LeakDetector::addGarbageObject(this); 367 } 368 369 void MachineInstr::addImplicitDefUseOperands() { 370 if (TID->ImplicitDefs) 371 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 372 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); 373 if (TID->ImplicitUses) 374 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 375 addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); 376 } 377 378 /// MachineInstr ctor - This constructor create a MachineInstr and add the 379 /// implicit operands. It reserves space for number of operands specified by 380 /// TargetInstrDesc or the numOperands if it is not zero. (for 381 /// instructions with variable number of operands). 382 MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) 383 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0), 384 debugLoc(DebugLoc::getUnknownLoc()) { 385 if (!NoImp && TID->getImplicitDefs()) 386 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 387 NumImplicitOps++; 388 if (!NoImp && TID->getImplicitUses()) 389 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 390 NumImplicitOps++; 391 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 392 if (!NoImp) 393 addImplicitDefUseOperands(); 394 // Make sure that we get added to a machine basicblock 395 LeakDetector::addGarbageObject(this); 396 } 397 398 /// MachineInstr ctor - As above, but with a DebugLoc. 399 MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, 400 bool NoImp) 401 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), 402 Parent(0), debugLoc(dl) { 403 if (!NoImp && TID->getImplicitDefs()) 404 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 405 NumImplicitOps++; 406 if (!NoImp && TID->getImplicitUses()) 407 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 408 NumImplicitOps++; 409 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 410 if (!NoImp) 411 addImplicitDefUseOperands(); 412 // Make sure that we get added to a machine basicblock 413 LeakDetector::addGarbageObject(this); 414 } 415 416 /// MachineInstr ctor - Work exactly the same as the ctor two above, except 417 /// that the MachineInstr is created and added to the end of the specified 418 /// basic block. 419 /// 420 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) 421 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0), 422 debugLoc(DebugLoc::getUnknownLoc()) { 423 assert(MBB && "Cannot use inserting ctor with null basic block!"); 424 if (TID->ImplicitDefs) 425 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 426 NumImplicitOps++; 427 if (TID->ImplicitUses) 428 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 429 NumImplicitOps++; 430 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 431 addImplicitDefUseOperands(); 432 // Make sure that we get added to a machine basicblock 433 LeakDetector::addGarbageObject(this); 434 MBB->push_back(this); // Add instruction to end of basic block! 435 } 436 437 /// MachineInstr ctor - As above, but with a DebugLoc. 438 /// 439 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, 440 const TargetInstrDesc &tid) 441 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), 442 Parent(0), debugLoc(dl) { 443 assert(MBB && "Cannot use inserting ctor with null basic block!"); 444 if (TID->ImplicitDefs) 445 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 446 NumImplicitOps++; 447 if (TID->ImplicitUses) 448 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 449 NumImplicitOps++; 450 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 451 addImplicitDefUseOperands(); 452 // Make sure that we get added to a machine basicblock 453 LeakDetector::addGarbageObject(this); 454 MBB->push_back(this); // Add instruction to end of basic block! 455 } 456 457 /// MachineInstr ctor - Copies MachineInstr arg exactly 458 /// 459 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 460 : TID(&MI.getDesc()), NumImplicitOps(0), 461 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd), 462 Parent(0), debugLoc(MI.getDebugLoc()) { 463 Operands.reserve(MI.getNumOperands()); 464 465 // Add operands 466 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 467 addOperand(MI.getOperand(i)); 468 NumImplicitOps = MI.NumImplicitOps; 469 470 // Set parent to null. 471 Parent = 0; 472 473 LeakDetector::addGarbageObject(this); 474 } 475 476 MachineInstr::~MachineInstr() { 477 LeakDetector::removeGarbageObject(this); 478 #ifndef NDEBUG 479 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 480 assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); 481 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && 482 "Reg operand def/use list corrupted"); 483 } 484 #endif 485 } 486 487 /// getRegInfo - If this instruction is embedded into a MachineFunction, 488 /// return the MachineRegisterInfo object for the current function, otherwise 489 /// return null. 490 MachineRegisterInfo *MachineInstr::getRegInfo() { 491 if (MachineBasicBlock *MBB = getParent()) 492 return &MBB->getParent()->getRegInfo(); 493 return 0; 494 } 495 496 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 497 /// this instruction from their respective use lists. This requires that the 498 /// operands already be on their use lists. 499 void MachineInstr::RemoveRegOperandsFromUseLists() { 500 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 501 if (Operands[i].isReg()) 502 Operands[i].RemoveRegOperandFromRegInfo(); 503 } 504 } 505 506 /// AddRegOperandsToUseLists - Add all of the register operands in 507 /// this instruction from their respective use lists. This requires that the 508 /// operands not be on their use lists yet. 509 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { 510 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 511 if (Operands[i].isReg()) 512 Operands[i].AddRegOperandToRegInfo(&RegInfo); 513 } 514 } 515 516 517 /// addOperand - Add the specified operand to the instruction. If it is an 518 /// implicit operand, it is added to the end of the operand list. If it is 519 /// an explicit operand it is added at the end of the explicit operand list 520 /// (before the first implicit operand). 521 void MachineInstr::addOperand(const MachineOperand &Op) { 522 bool isImpReg = Op.isReg() && Op.isImplicit(); 523 assert((isImpReg || !OperandsComplete()) && 524 "Trying to add an operand to a machine instr that is already done!"); 525 526 MachineRegisterInfo *RegInfo = getRegInfo(); 527 528 // If we are adding the operand to the end of the list, our job is simpler. 529 // This is true most of the time, so this is a reasonable optimization. 530 if (isImpReg || NumImplicitOps == 0) { 531 // We can only do this optimization if we know that the operand list won't 532 // reallocate. 533 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { 534 Operands.push_back(Op); 535 536 // Set the parent of the operand. 537 Operands.back().ParentMI = this; 538 539 // If the operand is a register, update the operand's use list. 540 if (Op.isReg()) 541 Operands.back().AddRegOperandToRegInfo(RegInfo); 542 return; 543 } 544 } 545 546 // Otherwise, we have to insert a real operand before any implicit ones. 547 unsigned OpNo = Operands.size()-NumImplicitOps; 548 549 // If this instruction isn't embedded into a function, then we don't need to 550 // update any operand lists. 551 if (RegInfo == 0) { 552 // Simple insertion, no reginfo update needed for other register operands. 553 Operands.insert(Operands.begin()+OpNo, Op); 554 Operands[OpNo].ParentMI = this; 555 556 // Do explicitly set the reginfo for this operand though, to ensure the 557 // next/prev fields are properly nulled out. 558 if (Operands[OpNo].isReg()) 559 Operands[OpNo].AddRegOperandToRegInfo(0); 560 561 } else if (Operands.size()+1 <= Operands.capacity()) { 562 // Otherwise, we have to remove register operands from their register use 563 // list, add the operand, then add the register operands back to their use 564 // list. This also must handle the case when the operand list reallocates 565 // to somewhere else. 566 567 // If insertion of this operand won't cause reallocation of the operand 568 // list, just remove the implicit operands, add the operand, then re-add all 569 // the rest of the operands. 570 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 571 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 572 Operands[i].RemoveRegOperandFromRegInfo(); 573 } 574 575 // Add the operand. If it is a register, add it to the reg list. 576 Operands.insert(Operands.begin()+OpNo, Op); 577 Operands[OpNo].ParentMI = this; 578 579 if (Operands[OpNo].isReg()) 580 Operands[OpNo].AddRegOperandToRegInfo(RegInfo); 581 582 // Re-add all the implicit ops. 583 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { 584 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 585 Operands[i].AddRegOperandToRegInfo(RegInfo); 586 } 587 } else { 588 // Otherwise, we will be reallocating the operand list. Remove all reg 589 // operands from their list, then readd them after the operand list is 590 // reallocated. 591 RemoveRegOperandsFromUseLists(); 592 593 Operands.insert(Operands.begin()+OpNo, Op); 594 Operands[OpNo].ParentMI = this; 595 596 // Re-add all the operands. 597 AddRegOperandsToUseLists(*RegInfo); 598 } 599 } 600 601 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 602 /// fewer operand than it started with. 603 /// 604 void MachineInstr::RemoveOperand(unsigned OpNo) { 605 assert(OpNo < Operands.size() && "Invalid operand number"); 606 607 // Special case removing the last one. 608 if (OpNo == Operands.size()-1) { 609 // If needed, remove from the reg def/use list. 610 if (Operands.back().isReg() && Operands.back().isOnRegUseList()) 611 Operands.back().RemoveRegOperandFromRegInfo(); 612 613 Operands.pop_back(); 614 return; 615 } 616 617 // Otherwise, we are removing an interior operand. If we have reginfo to 618 // update, remove all operands that will be shifted down from their reg lists, 619 // move everything down, then re-add them. 620 MachineRegisterInfo *RegInfo = getRegInfo(); 621 if (RegInfo) { 622 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 623 if (Operands[i].isReg()) 624 Operands[i].RemoveRegOperandFromRegInfo(); 625 } 626 } 627 628 Operands.erase(Operands.begin()+OpNo); 629 630 if (RegInfo) { 631 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 632 if (Operands[i].isReg()) 633 Operands[i].AddRegOperandToRegInfo(RegInfo); 634 } 635 } 636 } 637 638 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 639 /// This function should be used only occasionally. The setMemRefs function 640 /// is the primary method for setting up a MachineInstr's MemRefs list. 641 void MachineInstr::addMemOperand(MachineFunction &MF, 642 MachineMemOperand *MO) { 643 mmo_iterator OldMemRefs = MemRefs; 644 mmo_iterator OldMemRefsEnd = MemRefsEnd; 645 646 size_t NewNum = (MemRefsEnd - MemRefs) + 1; 647 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 648 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum; 649 650 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs); 651 NewMemRefs[NewNum - 1] = MO; 652 653 MemRefs = NewMemRefs; 654 MemRefsEnd = NewMemRefsEnd; 655 } 656 657 /// removeFromParent - This method unlinks 'this' from the containing basic 658 /// block, and returns it, but does not delete it. 659 MachineInstr *MachineInstr::removeFromParent() { 660 assert(getParent() && "Not embedded in a basic block!"); 661 getParent()->remove(this); 662 return this; 663 } 664 665 666 /// eraseFromParent - This method unlinks 'this' from the containing basic 667 /// block, and deletes it. 668 void MachineInstr::eraseFromParent() { 669 assert(getParent() && "Not embedded in a basic block!"); 670 getParent()->erase(this); 671 } 672 673 674 /// OperandComplete - Return true if it's illegal to add a new operand 675 /// 676 bool MachineInstr::OperandsComplete() const { 677 unsigned short NumOperands = TID->getNumOperands(); 678 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) 679 return true; // Broken: we have all the operands of this instruction! 680 return false; 681 } 682 683 /// getNumExplicitOperands - Returns the number of non-implicit operands. 684 /// 685 unsigned MachineInstr::getNumExplicitOperands() const { 686 unsigned NumOperands = TID->getNumOperands(); 687 if (!TID->isVariadic()) 688 return NumOperands; 689 690 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 691 const MachineOperand &MO = getOperand(i); 692 if (!MO.isReg() || !MO.isImplicit()) 693 NumOperands++; 694 } 695 return NumOperands; 696 } 697 698 699 /// isLabel - Returns true if the MachineInstr represents a label. 700 /// 701 bool MachineInstr::isLabel() const { 702 return getOpcode() == TargetInstrInfo::DBG_LABEL || 703 getOpcode() == TargetInstrInfo::EH_LABEL || 704 getOpcode() == TargetInstrInfo::GC_LABEL; 705 } 706 707 /// isDebugLabel - Returns true if the MachineInstr represents a debug label. 708 /// 709 bool MachineInstr::isDebugLabel() const { 710 return getOpcode() == TargetInstrInfo::DBG_LABEL; 711 } 712 713 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 714 /// the specific register or -1 if it is not found. It further tightens 715 /// the search criteria to a use that kills the register if isKill is true. 716 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 717 const TargetRegisterInfo *TRI) const { 718 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 719 const MachineOperand &MO = getOperand(i); 720 if (!MO.isReg() || !MO.isUse()) 721 continue; 722 unsigned MOReg = MO.getReg(); 723 if (!MOReg) 724 continue; 725 if (MOReg == Reg || 726 (TRI && 727 TargetRegisterInfo::isPhysicalRegister(MOReg) && 728 TargetRegisterInfo::isPhysicalRegister(Reg) && 729 TRI->isSubRegister(MOReg, Reg))) 730 if (!isKill || MO.isKill()) 731 return i; 732 } 733 return -1; 734 } 735 736 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 737 /// the specified register or -1 if it is not found. If isDead is true, defs 738 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 739 /// also checks if there is a def of a super-register. 740 int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, 741 const TargetRegisterInfo *TRI) const { 742 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 743 const MachineOperand &MO = getOperand(i); 744 if (!MO.isReg() || !MO.isDef()) 745 continue; 746 unsigned MOReg = MO.getReg(); 747 if (MOReg == Reg || 748 (TRI && 749 TargetRegisterInfo::isPhysicalRegister(MOReg) && 750 TargetRegisterInfo::isPhysicalRegister(Reg) && 751 TRI->isSubRegister(MOReg, Reg))) 752 if (!isDead || MO.isDead()) 753 return i; 754 } 755 return -1; 756 } 757 758 /// findFirstPredOperandIdx() - Find the index of the first operand in the 759 /// operand list that is used to represent the predicate. It returns -1 if 760 /// none is found. 761 int MachineInstr::findFirstPredOperandIdx() const { 762 const TargetInstrDesc &TID = getDesc(); 763 if (TID.isPredicable()) { 764 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 765 if (TID.OpInfo[i].isPredicate()) 766 return i; 767 } 768 769 return -1; 770 } 771 772 /// isRegTiedToUseOperand - Given the index of a register def operand, 773 /// check if the register def is tied to a source operand, due to either 774 /// two-address elimination or inline assembly constraints. Returns the 775 /// first tied use operand index by reference is UseOpIdx is not null. 776 bool MachineInstr:: 777 isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const { 778 if (getOpcode() == TargetInstrInfo::INLINEASM) { 779 assert(DefOpIdx >= 2); 780 const MachineOperand &MO = getOperand(DefOpIdx); 781 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) 782 return false; 783 // Determine the actual operand index that corresponds to this index. 784 unsigned DefNo = 0; 785 unsigned DefPart = 0; 786 for (unsigned i = 1, e = getNumOperands(); i < e; ) { 787 const MachineOperand &FMO = getOperand(i); 788 // After the normal asm operands there may be additional imp-def regs. 789 if (!FMO.isImm()) 790 return false; 791 // Skip over this def. 792 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm()); 793 unsigned PrevDef = i + 1; 794 i = PrevDef + NumOps; 795 if (i > DefOpIdx) { 796 DefPart = DefOpIdx - PrevDef; 797 break; 798 } 799 ++DefNo; 800 } 801 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 802 const MachineOperand &FMO = getOperand(i); 803 if (!FMO.isImm()) 804 continue; 805 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse()) 806 continue; 807 unsigned Idx; 808 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) && 809 Idx == DefNo) { 810 if (UseOpIdx) 811 *UseOpIdx = (unsigned)i + 1 + DefPart; 812 return true; 813 } 814 } 815 return false; 816 } 817 818 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!"); 819 const TargetInstrDesc &TID = getDesc(); 820 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { 821 const MachineOperand &MO = getOperand(i); 822 if (MO.isReg() && MO.isUse() && 823 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) { 824 if (UseOpIdx) 825 *UseOpIdx = (unsigned)i; 826 return true; 827 } 828 } 829 return false; 830 } 831 832 /// isRegTiedToDefOperand - Return true if the operand of the specified index 833 /// is a register use and it is tied to an def operand. It also returns the def 834 /// operand index by reference. 835 bool MachineInstr:: 836 isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const { 837 if (getOpcode() == TargetInstrInfo::INLINEASM) { 838 const MachineOperand &MO = getOperand(UseOpIdx); 839 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) 840 return false; 841 842 // Find the flag operand corresponding to UseOpIdx 843 unsigned FlagIdx, NumOps=0; 844 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) { 845 const MachineOperand &UFMO = getOperand(FlagIdx); 846 // After the normal asm operands there may be additional imp-def regs. 847 if (!UFMO.isImm()) 848 return false; 849 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm()); 850 assert(NumOps < getNumOperands() && "Invalid inline asm flag"); 851 if (UseOpIdx < FlagIdx+NumOps+1) 852 break; 853 } 854 if (FlagIdx >= UseOpIdx) 855 return false; 856 const MachineOperand &UFMO = getOperand(FlagIdx); 857 unsigned DefNo; 858 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) { 859 if (!DefOpIdx) 860 return true; 861 862 unsigned DefIdx = 1; 863 // Remember to adjust the index. First operand is asm string, then there 864 // is a flag for each. 865 while (DefNo) { 866 const MachineOperand &FMO = getOperand(DefIdx); 867 assert(FMO.isImm()); 868 // Skip over this def. 869 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1; 870 --DefNo; 871 } 872 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx; 873 return true; 874 } 875 return false; 876 } 877 878 const TargetInstrDesc &TID = getDesc(); 879 if (UseOpIdx >= TID.getNumOperands()) 880 return false; 881 const MachineOperand &MO = getOperand(UseOpIdx); 882 if (!MO.isReg() || !MO.isUse()) 883 return false; 884 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO); 885 if (DefIdx == -1) 886 return false; 887 if (DefOpIdx) 888 *DefOpIdx = (unsigned)DefIdx; 889 return true; 890 } 891 892 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 893 /// 894 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { 895 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 896 const MachineOperand &MO = MI->getOperand(i); 897 if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) 898 continue; 899 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { 900 MachineOperand &MOp = getOperand(j); 901 if (!MOp.isIdenticalTo(MO)) 902 continue; 903 if (MO.isKill()) 904 MOp.setIsKill(); 905 else 906 MOp.setIsDead(); 907 break; 908 } 909 } 910 } 911 912 /// copyPredicates - Copies predicate operand(s) from MI. 913 void MachineInstr::copyPredicates(const MachineInstr *MI) { 914 const TargetInstrDesc &TID = MI->getDesc(); 915 if (!TID.isPredicable()) 916 return; 917 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 918 if (TID.OpInfo[i].isPredicate()) { 919 // Predicated operands must be last operands. 920 addOperand(MI->getOperand(i)); 921 } 922 } 923 } 924 925 /// isSafeToMove - Return true if it is safe to move this instruction. If 926 /// SawStore is set to true, it means that there is a store (or call) between 927 /// the instruction's location and its intended destination. 928 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 929 bool &SawStore) const { 930 // Ignore stuff that we obviously can't move. 931 if (TID->mayStore() || TID->isCall()) { 932 SawStore = true; 933 return false; 934 } 935 if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) 936 return false; 937 938 // See if this instruction does a load. If so, we have to guarantee that the 939 // loaded value doesn't change between the load and the its intended 940 // destination. The check for isInvariantLoad gives the targe the chance to 941 // classify the load as always returning a constant, e.g. a constant pool 942 // load. 943 if (TID->mayLoad() && !TII->isInvariantLoad(this)) 944 // Otherwise, this is a real load. If there is a store between the load and 945 // end of block, or if the load is volatile, we can't move it. 946 return !SawStore && !hasVolatileMemoryRef(); 947 948 return true; 949 } 950 951 /// isSafeToReMat - Return true if it's safe to rematerialize the specified 952 /// instruction which defined the specified register instead of copying it. 953 bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, 954 unsigned DstReg) const { 955 bool SawStore = false; 956 if (!getDesc().isRematerializable() || 957 !TII->isTriviallyReMaterializable(this) || 958 !isSafeToMove(TII, SawStore)) 959 return false; 960 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 961 const MachineOperand &MO = getOperand(i); 962 if (!MO.isReg()) 963 continue; 964 // FIXME: For now, do not remat any instruction with register operands. 965 // Later on, we can loosen the restriction is the register operands have 966 // not been modified between the def and use. Note, this is different from 967 // MachineSink because the code is no longer in two-address form (at least 968 // partially). 969 if (MO.isUse()) 970 return false; 971 else if (!MO.isDead() && MO.getReg() != DstReg) 972 return false; 973 } 974 return true; 975 } 976 977 /// hasVolatileMemoryRef - Return true if this instruction may have a 978 /// volatile memory reference, or if the information describing the 979 /// memory reference is not available. Return false if it is known to 980 /// have no volatile memory references. 981 bool MachineInstr::hasVolatileMemoryRef() const { 982 // An instruction known never to access memory won't have a volatile access. 983 if (!TID->mayStore() && 984 !TID->mayLoad() && 985 !TID->isCall() && 986 !TID->hasUnmodeledSideEffects()) 987 return false; 988 989 // Otherwise, if the instruction has no memory reference information, 990 // conservatively assume it wasn't preserved. 991 if (memoperands_empty()) 992 return true; 993 994 // Check the memory reference information for volatile references. 995 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) 996 if ((*I)->isVolatile()) 997 return true; 998 999 return false; 1000 } 1001 1002 void MachineInstr::dump() const { 1003 errs() << " " << *this; 1004 } 1005 1006 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { 1007 // Specialize printing if op#0 is definition 1008 unsigned StartOp = 0; 1009 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) { 1010 getOperand(0).print(OS, TM); 1011 OS << " = "; 1012 ++StartOp; // Don't print this operand again! 1013 } 1014 1015 OS << getDesc().getName(); 1016 1017 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 1018 if (i != StartOp) 1019 OS << ","; 1020 OS << " "; 1021 getOperand(i).print(OS, TM); 1022 } 1023 1024 if (!memoperands_empty()) { 1025 OS << ", Mem:"; 1026 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 1027 i != e; ++i) { 1028 OS << **i; 1029 if (next(i) != e) 1030 OS << " "; 1031 } 1032 } 1033 1034 if (!debugLoc.isUnknown()) { 1035 const MachineFunction *MF = getParent()->getParent(); 1036 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc); 1037 DICompileUnit CU(DLT.CompileUnit); 1038 std::string Dir, Fn; 1039 OS << " [dbg: " 1040 << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << "," 1041 << DLT.Line << "," 1042 << DLT.Col << "]"; 1043 } 1044 1045 OS << "\n"; 1046 } 1047 1048 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 1049 const TargetRegisterInfo *RegInfo, 1050 bool AddIfNotFound) { 1051 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1052 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1053 bool Found = false; 1054 SmallVector<unsigned,4> DeadOps; 1055 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1056 MachineOperand &MO = getOperand(i); 1057 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 1058 continue; 1059 unsigned Reg = MO.getReg(); 1060 if (!Reg) 1061 continue; 1062 1063 if (Reg == IncomingReg) { 1064 if (!Found) { 1065 if (MO.isKill()) 1066 // The register is already marked kill. 1067 return true; 1068 if (isPhysReg && isRegTiedToDefOperand(i)) 1069 // Two-address uses of physregs must not be marked kill. 1070 return true; 1071 MO.setIsKill(); 1072 Found = true; 1073 } 1074 } else if (hasAliases && MO.isKill() && 1075 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1076 // A super-register kill already exists. 1077 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1078 return true; 1079 if (RegInfo->isSubRegister(IncomingReg, Reg)) 1080 DeadOps.push_back(i); 1081 } 1082 } 1083 1084 // Trim unneeded kill operands. 1085 while (!DeadOps.empty()) { 1086 unsigned OpIdx = DeadOps.back(); 1087 if (getOperand(OpIdx).isImplicit()) 1088 RemoveOperand(OpIdx); 1089 else 1090 getOperand(OpIdx).setIsKill(false); 1091 DeadOps.pop_back(); 1092 } 1093 1094 // If not found, this means an alias of one of the operands is killed. Add a 1095 // new implicit operand if required. 1096 if (!Found && AddIfNotFound) { 1097 addOperand(MachineOperand::CreateReg(IncomingReg, 1098 false /*IsDef*/, 1099 true /*IsImp*/, 1100 true /*IsKill*/)); 1101 return true; 1102 } 1103 return Found; 1104 } 1105 1106 bool MachineInstr::addRegisterDead(unsigned IncomingReg, 1107 const TargetRegisterInfo *RegInfo, 1108 bool AddIfNotFound) { 1109 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1110 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1111 bool Found = false; 1112 SmallVector<unsigned,4> DeadOps; 1113 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1114 MachineOperand &MO = getOperand(i); 1115 if (!MO.isReg() || !MO.isDef()) 1116 continue; 1117 unsigned Reg = MO.getReg(); 1118 if (!Reg) 1119 continue; 1120 1121 if (Reg == IncomingReg) { 1122 if (!Found) { 1123 if (MO.isDead()) 1124 // The register is already marked dead. 1125 return true; 1126 MO.setIsDead(); 1127 Found = true; 1128 } 1129 } else if (hasAliases && MO.isDead() && 1130 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1131 // There exists a super-register that's marked dead. 1132 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1133 return true; 1134 if (RegInfo->getSubRegisters(IncomingReg) && 1135 RegInfo->getSuperRegisters(Reg) && 1136 RegInfo->isSubRegister(IncomingReg, Reg)) 1137 DeadOps.push_back(i); 1138 } 1139 } 1140 1141 // Trim unneeded dead operands. 1142 while (!DeadOps.empty()) { 1143 unsigned OpIdx = DeadOps.back(); 1144 if (getOperand(OpIdx).isImplicit()) 1145 RemoveOperand(OpIdx); 1146 else 1147 getOperand(OpIdx).setIsDead(false); 1148 DeadOps.pop_back(); 1149 } 1150 1151 // If not found, this means an alias of one of the operands is dead. Add a 1152 // new implicit operand if required. 1153 if (Found || !AddIfNotFound) 1154 return Found; 1155 1156 addOperand(MachineOperand::CreateReg(IncomingReg, 1157 true /*IsDef*/, 1158 true /*IsImp*/, 1159 false /*IsKill*/, 1160 true /*IsDead*/)); 1161 return true; 1162 } 1163