1 //===-- MachineVerifier.cpp - Machine Code Verifier -------------*- C++ -*-===// 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 // Pass to verify generated machine code. The following is checked: 11 // 12 // Operand counts: All explicit operands must be present. 13 // 14 // Register classes: All physical and virtual register operands must be 15 // compatible with the register class required by the instruction descriptor. 16 // 17 // Register live intervals: Registers must be defined only once, and must be 18 // defined before use. 19 // 20 // The machine code verifier is enabled from LLVMTargetMachine.cpp with the 21 // command-line option -verify-machineinstrs, or by defining the environment 22 // variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive 23 // the verifier errors. 24 //===----------------------------------------------------------------------===// 25 26 #include "llvm/Function.h" 27 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 28 #include "llvm/CodeGen/LiveVariables.h" 29 #include "llvm/CodeGen/MachineFunctionPass.h" 30 #include "llvm/CodeGen/MachineFrameInfo.h" 31 #include "llvm/CodeGen/MachineMemOperand.h" 32 #include "llvm/CodeGen/MachineRegisterInfo.h" 33 #include "llvm/CodeGen/Passes.h" 34 #include "llvm/Target/TargetMachine.h" 35 #include "llvm/Target/TargetRegisterInfo.h" 36 #include "llvm/Target/TargetInstrInfo.h" 37 #include "llvm/ADT/DenseSet.h" 38 #include "llvm/ADT/SetOperations.h" 39 #include "llvm/ADT/SmallVector.h" 40 #include "llvm/Support/Debug.h" 41 #include "llvm/Support/ErrorHandling.h" 42 #include "llvm/Support/raw_ostream.h" 43 using namespace llvm; 44 45 namespace { 46 struct MachineVerifier { 47 48 MachineVerifier(Pass *pass) : 49 PASS(pass), 50 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS")) 51 {} 52 53 bool runOnMachineFunction(MachineFunction &MF); 54 55 Pass *const PASS; 56 const char *const OutFileName; 57 raw_ostream *OS; 58 const MachineFunction *MF; 59 const TargetMachine *TM; 60 const TargetRegisterInfo *TRI; 61 const MachineRegisterInfo *MRI; 62 63 unsigned foundErrors; 64 65 typedef SmallVector<unsigned, 16> RegVector; 66 typedef DenseSet<unsigned> RegSet; 67 typedef DenseMap<unsigned, const MachineInstr*> RegMap; 68 69 BitVector regsReserved; 70 RegSet regsLive; 71 RegVector regsDefined, regsDead, regsKilled; 72 RegSet regsLiveInButUnused; 73 74 // Add Reg and any sub-registers to RV 75 void addRegWithSubRegs(RegVector &RV, unsigned Reg) { 76 RV.push_back(Reg); 77 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 78 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++) 79 RV.push_back(*R); 80 } 81 82 struct BBInfo { 83 // Is this MBB reachable from the MF entry point? 84 bool reachable; 85 86 // Vregs that must be live in because they are used without being 87 // defined. Map value is the user. 88 RegMap vregsLiveIn; 89 90 // Regs killed in MBB. They may be defined again, and will then be in both 91 // regsKilled and regsLiveOut. 92 RegSet regsKilled; 93 94 // Regs defined in MBB and live out. Note that vregs passing through may 95 // be live out without being mentioned here. 96 RegSet regsLiveOut; 97 98 // Vregs that pass through MBB untouched. This set is disjoint from 99 // regsKilled and regsLiveOut. 100 RegSet vregsPassed; 101 102 // Vregs that must pass through MBB because they are needed by a successor 103 // block. This set is disjoint from regsLiveOut. 104 RegSet vregsRequired; 105 106 BBInfo() : reachable(false) {} 107 108 // Add register to vregsPassed if it belongs there. Return true if 109 // anything changed. 110 bool addPassed(unsigned Reg) { 111 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 112 return false; 113 if (regsKilled.count(Reg) || regsLiveOut.count(Reg)) 114 return false; 115 return vregsPassed.insert(Reg).second; 116 } 117 118 // Same for a full set. 119 bool addPassed(const RegSet &RS) { 120 bool changed = false; 121 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) 122 if (addPassed(*I)) 123 changed = true; 124 return changed; 125 } 126 127 // Add register to vregsRequired if it belongs there. Return true if 128 // anything changed. 129 bool addRequired(unsigned Reg) { 130 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 131 return false; 132 if (regsLiveOut.count(Reg)) 133 return false; 134 return vregsRequired.insert(Reg).second; 135 } 136 137 // Same for a full set. 138 bool addRequired(const RegSet &RS) { 139 bool changed = false; 140 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) 141 if (addRequired(*I)) 142 changed = true; 143 return changed; 144 } 145 146 // Same for a full map. 147 bool addRequired(const RegMap &RM) { 148 bool changed = false; 149 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I) 150 if (addRequired(I->first)) 151 changed = true; 152 return changed; 153 } 154 155 // Live-out registers are either in regsLiveOut or vregsPassed. 156 bool isLiveOut(unsigned Reg) const { 157 return regsLiveOut.count(Reg) || vregsPassed.count(Reg); 158 } 159 }; 160 161 // Extra register info per MBB. 162 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap; 163 164 bool isReserved(unsigned Reg) { 165 return Reg < regsReserved.size() && regsReserved.test(Reg); 166 } 167 168 // Analysis information if available 169 LiveVariables *LiveVars; 170 LiveIntervals *LiveInts; 171 172 void visitMachineFunctionBefore(); 173 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB); 174 void visitMachineInstrBefore(const MachineInstr *MI); 175 void visitMachineOperand(const MachineOperand *MO, unsigned MONum); 176 void visitMachineInstrAfter(const MachineInstr *MI); 177 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB); 178 void visitMachineFunctionAfter(); 179 180 void report(const char *msg, const MachineFunction *MF); 181 void report(const char *msg, const MachineBasicBlock *MBB); 182 void report(const char *msg, const MachineInstr *MI); 183 void report(const char *msg, const MachineOperand *MO, unsigned MONum); 184 185 void markReachable(const MachineBasicBlock *MBB); 186 void calcRegsPassed(); 187 void checkPHIOps(const MachineBasicBlock *MBB); 188 189 void calcRegsRequired(); 190 void verifyLiveVariables(); 191 }; 192 193 struct MachineVerifierPass : public MachineFunctionPass { 194 static char ID; // Pass ID, replacement for typeid 195 196 MachineVerifierPass() 197 : MachineFunctionPass(&ID) {} 198 199 void getAnalysisUsage(AnalysisUsage &AU) const { 200 AU.setPreservesAll(); 201 MachineFunctionPass::getAnalysisUsage(AU); 202 } 203 204 bool runOnMachineFunction(MachineFunction &MF) { 205 MF.verify(this); 206 return false; 207 } 208 }; 209 210 } 211 212 char MachineVerifierPass::ID = 0; 213 static RegisterPass<MachineVerifierPass> 214 MachineVer("machineverifier", "Verify generated machine code"); 215 static const PassInfo *const MachineVerifyID = &MachineVer; 216 217 FunctionPass *llvm::createMachineVerifierPass() { 218 return new MachineVerifierPass(); 219 } 220 221 void MachineFunction::verify(Pass *p) const { 222 MachineVerifier(p).runOnMachineFunction(const_cast<MachineFunction&>(*this)); 223 } 224 225 bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) { 226 raw_ostream *OutFile = 0; 227 if (OutFileName) { 228 std::string ErrorInfo; 229 OutFile = new raw_fd_ostream(OutFileName, ErrorInfo, 230 raw_fd_ostream::F_Append); 231 if (!ErrorInfo.empty()) { 232 errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n'; 233 exit(1); 234 } 235 236 OS = OutFile; 237 } else { 238 OS = &errs(); 239 } 240 241 foundErrors = 0; 242 243 this->MF = &MF; 244 TM = &MF.getTarget(); 245 TRI = TM->getRegisterInfo(); 246 MRI = &MF.getRegInfo(); 247 248 if (PASS) { 249 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>(); 250 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>(); 251 } else { 252 LiveVars = NULL; 253 LiveInts = NULL; 254 } 255 256 visitMachineFunctionBefore(); 257 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end(); 258 MFI!=MFE; ++MFI) { 259 visitMachineBasicBlockBefore(MFI); 260 for (MachineBasicBlock::const_iterator MBBI = MFI->begin(), 261 MBBE = MFI->end(); MBBI != MBBE; ++MBBI) { 262 visitMachineInstrBefore(MBBI); 263 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) 264 visitMachineOperand(&MBBI->getOperand(I), I); 265 visitMachineInstrAfter(MBBI); 266 } 267 visitMachineBasicBlockAfter(MFI); 268 } 269 visitMachineFunctionAfter(); 270 271 if (OutFile) 272 delete OutFile; 273 else if (foundErrors) 274 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors."); 275 276 // Clean up. 277 regsLive.clear(); 278 regsDefined.clear(); 279 regsDead.clear(); 280 regsKilled.clear(); 281 regsLiveInButUnused.clear(); 282 MBBInfoMap.clear(); 283 284 return false; // no changes 285 } 286 287 void MachineVerifier::report(const char *msg, const MachineFunction *MF) { 288 assert(MF); 289 *OS << '\n'; 290 if (!foundErrors++) 291 MF->print(*OS); 292 *OS << "*** Bad machine code: " << msg << " ***\n" 293 << "- function: " << MF->getFunction()->getNameStr() << "\n"; 294 } 295 296 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) { 297 assert(MBB); 298 report(msg, MBB->getParent()); 299 *OS << "- basic block: " << MBB->getName() 300 << " " << (void*)MBB 301 << " (BB#" << MBB->getNumber() << ")\n"; 302 } 303 304 void MachineVerifier::report(const char *msg, const MachineInstr *MI) { 305 assert(MI); 306 report(msg, MI->getParent()); 307 *OS << "- instruction: "; 308 MI->print(*OS, TM); 309 } 310 311 void MachineVerifier::report(const char *msg, 312 const MachineOperand *MO, unsigned MONum) { 313 assert(MO); 314 report(msg, MO->getParent()); 315 *OS << "- operand " << MONum << ": "; 316 MO->print(*OS, TM); 317 *OS << "\n"; 318 } 319 320 void MachineVerifier::markReachable(const MachineBasicBlock *MBB) { 321 BBInfo &MInfo = MBBInfoMap[MBB]; 322 if (!MInfo.reachable) { 323 MInfo.reachable = true; 324 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), 325 SuE = MBB->succ_end(); SuI != SuE; ++SuI) 326 markReachable(*SuI); 327 } 328 } 329 330 void MachineVerifier::visitMachineFunctionBefore() { 331 regsReserved = TRI->getReservedRegs(*MF); 332 333 // A sub-register of a reserved register is also reserved 334 for (int Reg = regsReserved.find_first(); Reg>=0; 335 Reg = regsReserved.find_next(Reg)) { 336 for (const unsigned *Sub = TRI->getSubRegisters(Reg); *Sub; ++Sub) { 337 // FIXME: This should probably be: 338 // assert(regsReserved.test(*Sub) && "Non-reserved sub-register"); 339 regsReserved.set(*Sub); 340 } 341 } 342 markReachable(&MF->front()); 343 } 344 345 // Does iterator point to a and b as the first two elements? 346 static bool matchPair(MachineBasicBlock::const_succ_iterator i, 347 const MachineBasicBlock *a, const MachineBasicBlock *b) { 348 if (*i == a) 349 return *++i == b; 350 if (*i == b) 351 return *++i == a; 352 return false; 353 } 354 355 void 356 MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) { 357 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 358 359 // Call AnalyzeBranch. If it succeeds, there several more conditions to check. 360 MachineBasicBlock *TBB = 0, *FBB = 0; 361 SmallVector<MachineOperand, 4> Cond; 362 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB), 363 TBB, FBB, Cond)) { 364 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's 365 // check whether its answers match up with reality. 366 if (!TBB && !FBB) { 367 // Block falls through to its successor. 368 MachineFunction::const_iterator MBBI = MBB; 369 ++MBBI; 370 if (MBBI == MF->end()) { 371 // It's possible that the block legitimately ends with a noreturn 372 // call or an unreachable, in which case it won't actually fall 373 // out the bottom of the function. 374 } else if (MBB->succ_empty()) { 375 // It's possible that the block legitimately ends with a noreturn 376 // call or an unreachable, in which case it won't actuall fall 377 // out of the block. 378 } else if (MBB->succ_size() != 1) { 379 report("MBB exits via unconditional fall-through but doesn't have " 380 "exactly one CFG successor!", MBB); 381 } else if (MBB->succ_begin()[0] != MBBI) { 382 report("MBB exits via unconditional fall-through but its successor " 383 "differs from its CFG successor!", MBB); 384 } 385 if (!MBB->empty() && MBB->back().getDesc().isBarrier() && 386 !TII->isPredicated(&MBB->back())) { 387 report("MBB exits via unconditional fall-through but ends with a " 388 "barrier instruction!", MBB); 389 } 390 if (!Cond.empty()) { 391 report("MBB exits via unconditional fall-through but has a condition!", 392 MBB); 393 } 394 } else if (TBB && !FBB && Cond.empty()) { 395 // Block unconditionally branches somewhere. 396 if (MBB->succ_size() != 1) { 397 report("MBB exits via unconditional branch but doesn't have " 398 "exactly one CFG successor!", MBB); 399 } else if (MBB->succ_begin()[0] != TBB) { 400 report("MBB exits via unconditional branch but the CFG " 401 "successor doesn't match the actual successor!", MBB); 402 } 403 if (MBB->empty()) { 404 report("MBB exits via unconditional branch but doesn't contain " 405 "any instructions!", MBB); 406 } else if (!MBB->back().getDesc().isBarrier()) { 407 report("MBB exits via unconditional branch but doesn't end with a " 408 "barrier instruction!", MBB); 409 } else if (!MBB->back().getDesc().isTerminator()) { 410 report("MBB exits via unconditional branch but the branch isn't a " 411 "terminator instruction!", MBB); 412 } 413 } else if (TBB && !FBB && !Cond.empty()) { 414 // Block conditionally branches somewhere, otherwise falls through. 415 MachineFunction::const_iterator MBBI = MBB; 416 ++MBBI; 417 if (MBBI == MF->end()) { 418 report("MBB conditionally falls through out of function!", MBB); 419 } if (MBB->succ_size() != 2) { 420 report("MBB exits via conditional branch/fall-through but doesn't have " 421 "exactly two CFG successors!", MBB); 422 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) { 423 report("MBB exits via conditional branch/fall-through but the CFG " 424 "successors don't match the actual successors!", MBB); 425 } 426 if (MBB->empty()) { 427 report("MBB exits via conditional branch/fall-through but doesn't " 428 "contain any instructions!", MBB); 429 } else if (MBB->back().getDesc().isBarrier()) { 430 report("MBB exits via conditional branch/fall-through but ends with a " 431 "barrier instruction!", MBB); 432 } else if (!MBB->back().getDesc().isTerminator()) { 433 report("MBB exits via conditional branch/fall-through but the branch " 434 "isn't a terminator instruction!", MBB); 435 } 436 } else if (TBB && FBB) { 437 // Block conditionally branches somewhere, otherwise branches 438 // somewhere else. 439 if (MBB->succ_size() != 2) { 440 report("MBB exits via conditional branch/branch but doesn't have " 441 "exactly two CFG successors!", MBB); 442 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) { 443 report("MBB exits via conditional branch/branch but the CFG " 444 "successors don't match the actual successors!", MBB); 445 } 446 if (MBB->empty()) { 447 report("MBB exits via conditional branch/branch but doesn't " 448 "contain any instructions!", MBB); 449 } else if (!MBB->back().getDesc().isBarrier()) { 450 report("MBB exits via conditional branch/branch but doesn't end with a " 451 "barrier instruction!", MBB); 452 } else if (!MBB->back().getDesc().isTerminator()) { 453 report("MBB exits via conditional branch/branch but the branch " 454 "isn't a terminator instruction!", MBB); 455 } 456 if (Cond.empty()) { 457 report("MBB exits via conditinal branch/branch but there's no " 458 "condition!", MBB); 459 } 460 } else { 461 report("AnalyzeBranch returned invalid data!", MBB); 462 } 463 } 464 465 regsLive.clear(); 466 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(), 467 E = MBB->livein_end(); I != E; ++I) { 468 if (!TargetRegisterInfo::isPhysicalRegister(*I)) { 469 report("MBB live-in list contains non-physical register", MBB); 470 continue; 471 } 472 regsLive.insert(*I); 473 for (const unsigned *R = TRI->getSubRegisters(*I); *R; R++) 474 regsLive.insert(*R); 475 } 476 regsLiveInButUnused = regsLive; 477 478 const MachineFrameInfo *MFI = MF->getFrameInfo(); 479 assert(MFI && "Function has no frame info"); 480 BitVector PR = MFI->getPristineRegs(MBB); 481 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) { 482 regsLive.insert(I); 483 for (const unsigned *R = TRI->getSubRegisters(I); *R; R++) 484 regsLive.insert(*R); 485 } 486 487 regsKilled.clear(); 488 regsDefined.clear(); 489 } 490 491 void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) { 492 const TargetInstrDesc &TI = MI->getDesc(); 493 if (MI->getNumOperands() < TI.getNumOperands()) { 494 report("Too few operands", MI); 495 *OS << TI.getNumOperands() << " operands expected, but " 496 << MI->getNumExplicitOperands() << " given.\n"; 497 } 498 499 // Check the MachineMemOperands for basic consistency. 500 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(), 501 E = MI->memoperands_end(); I != E; ++I) { 502 if ((*I)->isLoad() && !TI.mayLoad()) 503 report("Missing mayLoad flag", MI); 504 if ((*I)->isStore() && !TI.mayStore()) 505 report("Missing mayStore flag", MI); 506 } 507 508 // Debug values must not have a slot index. 509 // Other instructions must have one. 510 if (LiveInts) { 511 bool mapped = !LiveInts->isNotInMIMap(MI); 512 if (MI->isDebugValue()) { 513 if (mapped) 514 report("Debug instruction has a slot index", MI); 515 } else { 516 if (!mapped) 517 report("Missing slot index", MI); 518 } 519 } 520 521 } 522 523 void 524 MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) { 525 const MachineInstr *MI = MO->getParent(); 526 const TargetInstrDesc &TI = MI->getDesc(); 527 528 // The first TI.NumDefs operands must be explicit register defines 529 if (MONum < TI.getNumDefs()) { 530 if (!MO->isReg()) 531 report("Explicit definition must be a register", MO, MONum); 532 else if (!MO->isDef()) 533 report("Explicit definition marked as use", MO, MONum); 534 else if (MO->isImplicit()) 535 report("Explicit definition marked as implicit", MO, MONum); 536 } else if (MONum < TI.getNumOperands()) { 537 if (MO->isReg()) { 538 if (MO->isDef()) 539 report("Explicit operand marked as def", MO, MONum); 540 if (MO->isImplicit()) 541 report("Explicit operand marked as implicit", MO, MONum); 542 } 543 } else { 544 // ARM adds %reg0 operands to indicate predicates. We'll allow that. 545 if (MO->isReg() && !MO->isImplicit() && !TI.isVariadic() && MO->getReg()) 546 report("Extra explicit operand on non-variadic instruction", MO, MONum); 547 } 548 549 switch (MO->getType()) { 550 case MachineOperand::MO_Register: { 551 const unsigned Reg = MO->getReg(); 552 if (!Reg) 553 return; 554 555 // Check Live Variables. 556 if (MO->isUndef()) { 557 // An <undef> doesn't refer to any register, so just skip it. 558 } else if (MO->isUse()) { 559 regsLiveInButUnused.erase(Reg); 560 561 bool isKill = false; 562 unsigned defIdx; 563 if (MI->isRegTiedToDefOperand(MONum, &defIdx)) { 564 // A two-addr use counts as a kill if use and def are the same. 565 unsigned DefReg = MI->getOperand(defIdx).getReg(); 566 if (Reg == DefReg) { 567 isKill = true; 568 // ANd in that case an explicit kill flag is not allowed. 569 if (MO->isKill()) 570 report("Illegal kill flag on two-address instruction operand", 571 MO, MONum); 572 } else if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 573 report("Two-address instruction operands must be identical", 574 MO, MONum); 575 } 576 } else 577 isKill = MO->isKill(); 578 579 if (isKill) { 580 addRegWithSubRegs(regsKilled, Reg); 581 582 // Check that LiveVars knows this kill 583 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg)) { 584 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); 585 if (std::find(VI.Kills.begin(), 586 VI.Kills.end(), MI) == VI.Kills.end()) 587 report("Kill missing from LiveVariables", MO, MONum); 588 } 589 } 590 591 // Check LiveInts liveness and kill. 592 if (LiveInts && !LiveInts->isNotInMIMap(MI)) { 593 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getUseIndex(); 594 if (LiveInts->hasInterval(Reg)) { 595 const LiveInterval &LI = LiveInts->getInterval(Reg); 596 if (!LI.liveAt(UseIdx)) { 597 report("No live range at use", MO, MONum); 598 *OS << UseIdx << " is not live in " << LI << '\n'; 599 } 600 // TODO: Verify isKill == LI.killedAt. 601 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) { 602 report("Virtual register has no Live interval", MO, MONum); 603 } 604 } 605 606 // Use of a dead register. 607 if (!regsLive.count(Reg)) { 608 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 609 // Reserved registers may be used even when 'dead'. 610 if (!isReserved(Reg)) 611 report("Using an undefined physical register", MO, MONum); 612 } else { 613 BBInfo &MInfo = MBBInfoMap[MI->getParent()]; 614 // We don't know which virtual registers are live in, so only complain 615 // if vreg was killed in this MBB. Otherwise keep track of vregs that 616 // must be live in. PHI instructions are handled separately. 617 if (MInfo.regsKilled.count(Reg)) 618 report("Using a killed virtual register", MO, MONum); 619 else if (!MI->isPHI()) 620 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI)); 621 } 622 } 623 } else { 624 assert(MO->isDef()); 625 // Register defined. 626 // TODO: verify that earlyclobber ops are not used. 627 if (MO->isDead()) 628 addRegWithSubRegs(regsDead, Reg); 629 else 630 addRegWithSubRegs(regsDefined, Reg); 631 632 // Check LiveInts for a live range. 633 if (LiveInts && !LiveInts->isNotInMIMap(MI)) { 634 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI).getDefIndex(); 635 if (LiveInts->hasInterval(Reg)) { 636 const LiveInterval &LI = LiveInts->getInterval(Reg); 637 if (const LiveRange *LR = LI.getLiveRangeContaining(DefIdx)) { 638 assert(LR->valno && "NULL valno is not allowed"); 639 if (LR->valno->def != DefIdx) { 640 report("Inconsistent valno->def", MO, MONum); 641 *OS << "Valno " << LR->valno->id << " is not defined at " 642 << DefIdx << " in " << LI << '\n'; 643 } 644 if (LR->start != DefIdx) { 645 report("Live range doesn't start at def", MO, MONum); 646 LR->print(*OS); 647 *OS << " should start at " << DefIdx << " in " << LI << '\n'; 648 } 649 } else { 650 report("No live range at def", MO, MONum); 651 *OS << DefIdx << " is not live in " << LI << '\n'; 652 } 653 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) { 654 report("Virtual register has no Live interval", MO, MONum); 655 } 656 } 657 } 658 659 // Check register classes. 660 if (MONum < TI.getNumOperands() && !MO->isImplicit()) { 661 const TargetOperandInfo &TOI = TI.OpInfo[MONum]; 662 unsigned SubIdx = MO->getSubReg(); 663 664 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 665 unsigned sr = Reg; 666 if (SubIdx) { 667 unsigned s = TRI->getSubReg(Reg, SubIdx); 668 if (!s) { 669 report("Invalid subregister index for physical register", 670 MO, MONum); 671 return; 672 } 673 sr = s; 674 } 675 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) { 676 if (!DRC->contains(sr)) { 677 report("Illegal physical register for instruction", MO, MONum); 678 *OS << TRI->getName(sr) << " is not a " 679 << DRC->getName() << " register.\n"; 680 } 681 } 682 } else { 683 // Virtual register. 684 const TargetRegisterClass *RC = MRI->getRegClass(Reg); 685 if (SubIdx) { 686 const TargetRegisterClass *SRC = RC->getSubRegisterRegClass(SubIdx); 687 if (!SRC) { 688 report("Invalid subregister index for virtual register", MO, MONum); 689 *OS << "Register class " << RC->getName() 690 << " does not support subreg index " << SubIdx << "\n"; 691 return; 692 } 693 RC = SRC; 694 } 695 if (const TargetRegisterClass *DRC = TOI.getRegClass(TRI)) { 696 if (RC != DRC && !RC->hasSuperClass(DRC)) { 697 report("Illegal virtual register for instruction", MO, MONum); 698 *OS << "Expected a " << DRC->getName() << " register, but got a " 699 << RC->getName() << " register\n"; 700 } 701 } 702 } 703 } 704 break; 705 } 706 707 case MachineOperand::MO_MachineBasicBlock: 708 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent())) 709 report("PHI operand is not in the CFG", MO, MONum); 710 break; 711 712 default: 713 break; 714 } 715 } 716 717 void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) { 718 BBInfo &MInfo = MBBInfoMap[MI->getParent()]; 719 set_union(MInfo.regsKilled, regsKilled); 720 set_subtract(regsLive, regsKilled); regsKilled.clear(); 721 set_subtract(regsLive, regsDead); regsDead.clear(); 722 set_union(regsLive, regsDefined); regsDefined.clear(); 723 } 724 725 void 726 MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) { 727 MBBInfoMap[MBB].regsLiveOut = regsLive; 728 regsLive.clear(); 729 } 730 731 // Calculate the largest possible vregsPassed sets. These are the registers that 732 // can pass through an MBB live, but may not be live every time. It is assumed 733 // that all vregsPassed sets are empty before the call. 734 void MachineVerifier::calcRegsPassed() { 735 // First push live-out regs to successors' vregsPassed. Remember the MBBs that 736 // have any vregsPassed. 737 DenseSet<const MachineBasicBlock*> todo; 738 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); 739 MFI != MFE; ++MFI) { 740 const MachineBasicBlock &MBB(*MFI); 741 BBInfo &MInfo = MBBInfoMap[&MBB]; 742 if (!MInfo.reachable) 743 continue; 744 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(), 745 SuE = MBB.succ_end(); SuI != SuE; ++SuI) { 746 BBInfo &SInfo = MBBInfoMap[*SuI]; 747 if (SInfo.addPassed(MInfo.regsLiveOut)) 748 todo.insert(*SuI); 749 } 750 } 751 752 // Iteratively push vregsPassed to successors. This will converge to the same 753 // final state regardless of DenseSet iteration order. 754 while (!todo.empty()) { 755 const MachineBasicBlock *MBB = *todo.begin(); 756 todo.erase(MBB); 757 BBInfo &MInfo = MBBInfoMap[MBB]; 758 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), 759 SuE = MBB->succ_end(); SuI != SuE; ++SuI) { 760 if (*SuI == MBB) 761 continue; 762 BBInfo &SInfo = MBBInfoMap[*SuI]; 763 if (SInfo.addPassed(MInfo.vregsPassed)) 764 todo.insert(*SuI); 765 } 766 } 767 } 768 769 // Calculate the set of virtual registers that must be passed through each basic 770 // block in order to satisfy the requirements of successor blocks. This is very 771 // similar to calcRegsPassed, only backwards. 772 void MachineVerifier::calcRegsRequired() { 773 // First push live-in regs to predecessors' vregsRequired. 774 DenseSet<const MachineBasicBlock*> todo; 775 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); 776 MFI != MFE; ++MFI) { 777 const MachineBasicBlock &MBB(*MFI); 778 BBInfo &MInfo = MBBInfoMap[&MBB]; 779 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(), 780 PrE = MBB.pred_end(); PrI != PrE; ++PrI) { 781 BBInfo &PInfo = MBBInfoMap[*PrI]; 782 if (PInfo.addRequired(MInfo.vregsLiveIn)) 783 todo.insert(*PrI); 784 } 785 } 786 787 // Iteratively push vregsRequired to predecessors. This will converge to the 788 // same final state regardless of DenseSet iteration order. 789 while (!todo.empty()) { 790 const MachineBasicBlock *MBB = *todo.begin(); 791 todo.erase(MBB); 792 BBInfo &MInfo = MBBInfoMap[MBB]; 793 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), 794 PrE = MBB->pred_end(); PrI != PrE; ++PrI) { 795 if (*PrI == MBB) 796 continue; 797 BBInfo &SInfo = MBBInfoMap[*PrI]; 798 if (SInfo.addRequired(MInfo.vregsRequired)) 799 todo.insert(*PrI); 800 } 801 } 802 } 803 804 // Check PHI instructions at the beginning of MBB. It is assumed that 805 // calcRegsPassed has been run so BBInfo::isLiveOut is valid. 806 void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) { 807 for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end(); 808 BBI != BBE && BBI->isPHI(); ++BBI) { 809 DenseSet<const MachineBasicBlock*> seen; 810 811 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) { 812 unsigned Reg = BBI->getOperand(i).getReg(); 813 const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB(); 814 if (!Pre->isSuccessor(MBB)) 815 continue; 816 seen.insert(Pre); 817 BBInfo &PrInfo = MBBInfoMap[Pre]; 818 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg)) 819 report("PHI operand is not live-out from predecessor", 820 &BBI->getOperand(i), i); 821 } 822 823 // Did we see all predecessors? 824 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), 825 PrE = MBB->pred_end(); PrI != PrE; ++PrI) { 826 if (!seen.count(*PrI)) { 827 report("Missing PHI operand", BBI); 828 *OS << "BB#" << (*PrI)->getNumber() 829 << " is a predecessor according to the CFG.\n"; 830 } 831 } 832 } 833 } 834 835 void MachineVerifier::visitMachineFunctionAfter() { 836 calcRegsPassed(); 837 838 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); 839 MFI != MFE; ++MFI) { 840 BBInfo &MInfo = MBBInfoMap[MFI]; 841 842 // Skip unreachable MBBs. 843 if (!MInfo.reachable) 844 continue; 845 846 checkPHIOps(MFI); 847 } 848 849 // Now check LiveVariables info if available 850 if (LiveVars) { 851 calcRegsRequired(); 852 verifyLiveVariables(); 853 } 854 } 855 856 void MachineVerifier::verifyLiveVariables() { 857 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars"); 858 for (unsigned Reg = TargetRegisterInfo::FirstVirtualRegister, 859 RegE = MRI->getLastVirtReg()-1; Reg != RegE; ++Reg) { 860 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); 861 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); 862 MFI != MFE; ++MFI) { 863 BBInfo &MInfo = MBBInfoMap[MFI]; 864 865 // Our vregsRequired should be identical to LiveVariables' AliveBlocks 866 if (MInfo.vregsRequired.count(Reg)) { 867 if (!VI.AliveBlocks.test(MFI->getNumber())) { 868 report("LiveVariables: Block missing from AliveBlocks", MFI); 869 *OS << "Virtual register %reg" << Reg 870 << " must be live through the block.\n"; 871 } 872 } else { 873 if (VI.AliveBlocks.test(MFI->getNumber())) { 874 report("LiveVariables: Block should not be in AliveBlocks", MFI); 875 *OS << "Virtual register %reg" << Reg 876 << " is not needed live through the block.\n"; 877 } 878 } 879 } 880 } 881 } 882 883 884