LLVM API Documentation
00001 //===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Pass to verify generated machine code. The following is checked: 00011 // 00012 // Operand counts: All explicit operands must be present. 00013 // 00014 // Register classes: All physical and virtual register operands must be 00015 // compatible with the register class required by the instruction descriptor. 00016 // 00017 // Register live intervals: Registers must be defined only once, and must be 00018 // defined before use. 00019 // 00020 // The machine code verifier is enabled from LLVMTargetMachine.cpp with the 00021 // command-line option -verify-machineinstrs, or by defining the environment 00022 // variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive 00023 // the verifier errors. 00024 //===----------------------------------------------------------------------===// 00025 00026 #include "llvm/CodeGen/Passes.h" 00027 #include "llvm/ADT/DenseSet.h" 00028 #include "llvm/ADT/DepthFirstIterator.h" 00029 #include "llvm/ADT/SetOperations.h" 00030 #include "llvm/ADT/SmallVector.h" 00031 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00032 #include "llvm/CodeGen/LiveStackAnalysis.h" 00033 #include "llvm/CodeGen/LiveVariables.h" 00034 #include "llvm/CodeGen/MachineFrameInfo.h" 00035 #include "llvm/CodeGen/MachineFunctionPass.h" 00036 #include "llvm/CodeGen/MachineMemOperand.h" 00037 #include "llvm/CodeGen/MachineRegisterInfo.h" 00038 #include "llvm/IR/BasicBlock.h" 00039 #include "llvm/IR/InlineAsm.h" 00040 #include "llvm/IR/Instructions.h" 00041 #include "llvm/MC/MCAsmInfo.h" 00042 #include "llvm/Support/Debug.h" 00043 #include "llvm/Support/ErrorHandling.h" 00044 #include "llvm/Support/FileSystem.h" 00045 #include "llvm/Support/raw_ostream.h" 00046 #include "llvm/Target/TargetInstrInfo.h" 00047 #include "llvm/Target/TargetMachine.h" 00048 #include "llvm/Target/TargetRegisterInfo.h" 00049 #include "llvm/Target/TargetSubtargetInfo.h" 00050 using namespace llvm; 00051 00052 namespace { 00053 struct MachineVerifier { 00054 00055 MachineVerifier(Pass *pass, const char *b) : 00056 PASS(pass), 00057 Banner(b), 00058 OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS")) 00059 {} 00060 00061 bool runOnMachineFunction(MachineFunction &MF); 00062 00063 Pass *const PASS; 00064 const char *Banner; 00065 const char *const OutFileName; 00066 raw_ostream *OS; 00067 const MachineFunction *MF; 00068 const TargetMachine *TM; 00069 const TargetInstrInfo *TII; 00070 const TargetRegisterInfo *TRI; 00071 const MachineRegisterInfo *MRI; 00072 00073 unsigned foundErrors; 00074 00075 typedef SmallVector<unsigned, 16> RegVector; 00076 typedef SmallVector<const uint32_t*, 4> RegMaskVector; 00077 typedef DenseSet<unsigned> RegSet; 00078 typedef DenseMap<unsigned, const MachineInstr*> RegMap; 00079 typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet; 00080 00081 const MachineInstr *FirstTerminator; 00082 BlockSet FunctionBlocks; 00083 00084 BitVector regsReserved; 00085 RegSet regsLive; 00086 RegVector regsDefined, regsDead, regsKilled; 00087 RegMaskVector regMasks; 00088 RegSet regsLiveInButUnused; 00089 00090 SlotIndex lastIndex; 00091 00092 // Add Reg and any sub-registers to RV 00093 void addRegWithSubRegs(RegVector &RV, unsigned Reg) { 00094 RV.push_back(Reg); 00095 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 00096 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) 00097 RV.push_back(*SubRegs); 00098 } 00099 00100 struct BBInfo { 00101 // Is this MBB reachable from the MF entry point? 00102 bool reachable; 00103 00104 // Vregs that must be live in because they are used without being 00105 // defined. Map value is the user. 00106 RegMap vregsLiveIn; 00107 00108 // Regs killed in MBB. They may be defined again, and will then be in both 00109 // regsKilled and regsLiveOut. 00110 RegSet regsKilled; 00111 00112 // Regs defined in MBB and live out. Note that vregs passing through may 00113 // be live out without being mentioned here. 00114 RegSet regsLiveOut; 00115 00116 // Vregs that pass through MBB untouched. This set is disjoint from 00117 // regsKilled and regsLiveOut. 00118 RegSet vregsPassed; 00119 00120 // Vregs that must pass through MBB because they are needed by a successor 00121 // block. This set is disjoint from regsLiveOut. 00122 RegSet vregsRequired; 00123 00124 // Set versions of block's predecessor and successor lists. 00125 BlockSet Preds, Succs; 00126 00127 BBInfo() : reachable(false) {} 00128 00129 // Add register to vregsPassed if it belongs there. Return true if 00130 // anything changed. 00131 bool addPassed(unsigned Reg) { 00132 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 00133 return false; 00134 if (regsKilled.count(Reg) || regsLiveOut.count(Reg)) 00135 return false; 00136 return vregsPassed.insert(Reg).second; 00137 } 00138 00139 // Same for a full set. 00140 bool addPassed(const RegSet &RS) { 00141 bool changed = false; 00142 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) 00143 if (addPassed(*I)) 00144 changed = true; 00145 return changed; 00146 } 00147 00148 // Add register to vregsRequired if it belongs there. Return true if 00149 // anything changed. 00150 bool addRequired(unsigned Reg) { 00151 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 00152 return false; 00153 if (regsLiveOut.count(Reg)) 00154 return false; 00155 return vregsRequired.insert(Reg).second; 00156 } 00157 00158 // Same for a full set. 00159 bool addRequired(const RegSet &RS) { 00160 bool changed = false; 00161 for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) 00162 if (addRequired(*I)) 00163 changed = true; 00164 return changed; 00165 } 00166 00167 // Same for a full map. 00168 bool addRequired(const RegMap &RM) { 00169 bool changed = false; 00170 for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I) 00171 if (addRequired(I->first)) 00172 changed = true; 00173 return changed; 00174 } 00175 00176 // Live-out registers are either in regsLiveOut or vregsPassed. 00177 bool isLiveOut(unsigned Reg) const { 00178 return regsLiveOut.count(Reg) || vregsPassed.count(Reg); 00179 } 00180 }; 00181 00182 // Extra register info per MBB. 00183 DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap; 00184 00185 bool isReserved(unsigned Reg) { 00186 return Reg < regsReserved.size() && regsReserved.test(Reg); 00187 } 00188 00189 bool isAllocatable(unsigned Reg) { 00190 return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg); 00191 } 00192 00193 // Analysis information if available 00194 LiveVariables *LiveVars; 00195 LiveIntervals *LiveInts; 00196 LiveStacks *LiveStks; 00197 SlotIndexes *Indexes; 00198 00199 void visitMachineFunctionBefore(); 00200 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB); 00201 void visitMachineBundleBefore(const MachineInstr *MI); 00202 void visitMachineInstrBefore(const MachineInstr *MI); 00203 void visitMachineOperand(const MachineOperand *MO, unsigned MONum); 00204 void visitMachineInstrAfter(const MachineInstr *MI); 00205 void visitMachineBundleAfter(const MachineInstr *MI); 00206 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB); 00207 void visitMachineFunctionAfter(); 00208 00209 void report(const char *msg, const MachineFunction *MF); 00210 void report(const char *msg, const MachineBasicBlock *MBB); 00211 void report(const char *msg, const MachineInstr *MI); 00212 void report(const char *msg, const MachineOperand *MO, unsigned MONum); 00213 void report(const char *msg, const MachineFunction *MF, 00214 const LiveInterval &LI); 00215 void report(const char *msg, const MachineBasicBlock *MBB, 00216 const LiveInterval &LI); 00217 void report(const char *msg, const MachineFunction *MF, 00218 const LiveRange &LR); 00219 void report(const char *msg, const MachineBasicBlock *MBB, 00220 const LiveRange &LR); 00221 00222 void verifyInlineAsm(const MachineInstr *MI); 00223 00224 void checkLiveness(const MachineOperand *MO, unsigned MONum); 00225 void markReachable(const MachineBasicBlock *MBB); 00226 void calcRegsPassed(); 00227 void checkPHIOps(const MachineBasicBlock *MBB); 00228 00229 void calcRegsRequired(); 00230 void verifyLiveVariables(); 00231 void verifyLiveIntervals(); 00232 void verifyLiveInterval(const LiveInterval&); 00233 void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned); 00234 void verifyLiveRangeSegment(const LiveRange&, 00235 const LiveRange::const_iterator I, unsigned); 00236 void verifyLiveRange(const LiveRange&, unsigned); 00237 00238 void verifyStackFrame(); 00239 }; 00240 00241 struct MachineVerifierPass : public MachineFunctionPass { 00242 static char ID; // Pass ID, replacement for typeid 00243 const char *const Banner; 00244 00245 MachineVerifierPass(const char *b = nullptr) 00246 : MachineFunctionPass(ID), Banner(b) { 00247 initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry()); 00248 } 00249 00250 void getAnalysisUsage(AnalysisUsage &AU) const override { 00251 AU.setPreservesAll(); 00252 MachineFunctionPass::getAnalysisUsage(AU); 00253 } 00254 00255 bool runOnMachineFunction(MachineFunction &MF) override { 00256 MF.verify(this, Banner); 00257 return false; 00258 } 00259 }; 00260 00261 } 00262 00263 char MachineVerifierPass::ID = 0; 00264 INITIALIZE_PASS(MachineVerifierPass, "machineverifier", 00265 "Verify generated machine code", false, false) 00266 00267 FunctionPass *llvm::createMachineVerifierPass(const char *Banner) { 00268 return new MachineVerifierPass(Banner); 00269 } 00270 00271 void MachineFunction::verify(Pass *p, const char *Banner) const { 00272 MachineVerifier(p, Banner) 00273 .runOnMachineFunction(const_cast<MachineFunction&>(*this)); 00274 } 00275 00276 bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) { 00277 raw_ostream *OutFile = nullptr; 00278 if (OutFileName) { 00279 std::error_code EC; 00280 OutFile = new raw_fd_ostream(OutFileName, EC, 00281 sys::fs::F_Append | sys::fs::F_Text); 00282 if (EC) { 00283 errs() << "Error opening '" << OutFileName << "': " << EC.message() 00284 << '\n'; 00285 exit(1); 00286 } 00287 00288 OS = OutFile; 00289 } else { 00290 OS = &errs(); 00291 } 00292 00293 foundErrors = 0; 00294 00295 this->MF = &MF; 00296 TM = &MF.getTarget(); 00297 TII = TM->getSubtargetImpl()->getInstrInfo(); 00298 TRI = TM->getSubtargetImpl()->getRegisterInfo(); 00299 MRI = &MF.getRegInfo(); 00300 00301 LiveVars = nullptr; 00302 LiveInts = nullptr; 00303 LiveStks = nullptr; 00304 Indexes = nullptr; 00305 if (PASS) { 00306 LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>(); 00307 // We don't want to verify LiveVariables if LiveIntervals is available. 00308 if (!LiveInts) 00309 LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>(); 00310 LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>(); 00311 Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>(); 00312 } 00313 00314 visitMachineFunctionBefore(); 00315 for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end(); 00316 MFI!=MFE; ++MFI) { 00317 visitMachineBasicBlockBefore(MFI); 00318 // Keep track of the current bundle header. 00319 const MachineInstr *CurBundle = nullptr; 00320 // Do we expect the next instruction to be part of the same bundle? 00321 bool InBundle = false; 00322 00323 for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(), 00324 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) { 00325 if (MBBI->getParent() != MFI) { 00326 report("Bad instruction parent pointer", MFI); 00327 *OS << "Instruction: " << *MBBI; 00328 continue; 00329 } 00330 00331 // Check for consistent bundle flags. 00332 if (InBundle && !MBBI->isBundledWithPred()) 00333 report("Missing BundledPred flag, " 00334 "BundledSucc was set on predecessor", MBBI); 00335 if (!InBundle && MBBI->isBundledWithPred()) 00336 report("BundledPred flag is set, " 00337 "but BundledSucc not set on predecessor", MBBI); 00338 00339 // Is this a bundle header? 00340 if (!MBBI->isInsideBundle()) { 00341 if (CurBundle) 00342 visitMachineBundleAfter(CurBundle); 00343 CurBundle = MBBI; 00344 visitMachineBundleBefore(CurBundle); 00345 } else if (!CurBundle) 00346 report("No bundle header", MBBI); 00347 visitMachineInstrBefore(MBBI); 00348 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) 00349 visitMachineOperand(&MBBI->getOperand(I), I); 00350 visitMachineInstrAfter(MBBI); 00351 00352 // Was this the last bundled instruction? 00353 InBundle = MBBI->isBundledWithSucc(); 00354 } 00355 if (CurBundle) 00356 visitMachineBundleAfter(CurBundle); 00357 if (InBundle) 00358 report("BundledSucc flag set on last instruction in block", &MFI->back()); 00359 visitMachineBasicBlockAfter(MFI); 00360 } 00361 visitMachineFunctionAfter(); 00362 00363 if (OutFile) 00364 delete OutFile; 00365 else if (foundErrors) 00366 report_fatal_error("Found "+Twine(foundErrors)+" machine code errors."); 00367 00368 // Clean up. 00369 regsLive.clear(); 00370 regsDefined.clear(); 00371 regsDead.clear(); 00372 regsKilled.clear(); 00373 regMasks.clear(); 00374 regsLiveInButUnused.clear(); 00375 MBBInfoMap.clear(); 00376 00377 return false; // no changes 00378 } 00379 00380 void MachineVerifier::report(const char *msg, const MachineFunction *MF) { 00381 assert(MF); 00382 *OS << '\n'; 00383 if (!foundErrors++) { 00384 if (Banner) 00385 *OS << "# " << Banner << '\n'; 00386 MF->print(*OS, Indexes); 00387 } 00388 *OS << "*** Bad machine code: " << msg << " ***\n" 00389 << "- function: " << MF->getName() << "\n"; 00390 } 00391 00392 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) { 00393 assert(MBB); 00394 report(msg, MBB->getParent()); 00395 *OS << "- basic block: BB#" << MBB->getNumber() 00396 << ' ' << MBB->getName() 00397 << " (" << (const void*)MBB << ')'; 00398 if (Indexes) 00399 *OS << " [" << Indexes->getMBBStartIdx(MBB) 00400 << ';' << Indexes->getMBBEndIdx(MBB) << ')'; 00401 *OS << '\n'; 00402 } 00403 00404 void MachineVerifier::report(const char *msg, const MachineInstr *MI) { 00405 assert(MI); 00406 report(msg, MI->getParent()); 00407 *OS << "- instruction: "; 00408 if (Indexes && Indexes->hasIndex(MI)) 00409 *OS << Indexes->getInstructionIndex(MI) << '\t'; 00410 MI->print(*OS, TM); 00411 } 00412 00413 void MachineVerifier::report(const char *msg, 00414 const MachineOperand *MO, unsigned MONum) { 00415 assert(MO); 00416 report(msg, MO->getParent()); 00417 *OS << "- operand " << MONum << ": "; 00418 MO->print(*OS, TM); 00419 *OS << "\n"; 00420 } 00421 00422 void MachineVerifier::report(const char *msg, const MachineFunction *MF, 00423 const LiveInterval &LI) { 00424 report(msg, MF); 00425 *OS << "- interval: " << LI << '\n'; 00426 } 00427 00428 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB, 00429 const LiveInterval &LI) { 00430 report(msg, MBB); 00431 *OS << "- interval: " << LI << '\n'; 00432 } 00433 00434 void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB, 00435 const LiveRange &LR) { 00436 report(msg, MBB); 00437 *OS << "- liverange: " << LR << "\n"; 00438 } 00439 00440 void MachineVerifier::report(const char *msg, const MachineFunction *MF, 00441 const LiveRange &LR) { 00442 report(msg, MF); 00443 *OS << "- liverange: " << LR << "\n"; 00444 } 00445 00446 void MachineVerifier::markReachable(const MachineBasicBlock *MBB) { 00447 BBInfo &MInfo = MBBInfoMap[MBB]; 00448 if (!MInfo.reachable) { 00449 MInfo.reachable = true; 00450 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), 00451 SuE = MBB->succ_end(); SuI != SuE; ++SuI) 00452 markReachable(*SuI); 00453 } 00454 } 00455 00456 void MachineVerifier::visitMachineFunctionBefore() { 00457 lastIndex = SlotIndex(); 00458 regsReserved = MRI->getReservedRegs(); 00459 00460 // A sub-register of a reserved register is also reserved 00461 for (int Reg = regsReserved.find_first(); Reg>=0; 00462 Reg = regsReserved.find_next(Reg)) { 00463 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { 00464 // FIXME: This should probably be: 00465 // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register"); 00466 regsReserved.set(*SubRegs); 00467 } 00468 } 00469 00470 markReachable(&MF->front()); 00471 00472 // Build a set of the basic blocks in the function. 00473 FunctionBlocks.clear(); 00474 for (const auto &MBB : *MF) { 00475 FunctionBlocks.insert(&MBB); 00476 BBInfo &MInfo = MBBInfoMap[&MBB]; 00477 00478 MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end()); 00479 if (MInfo.Preds.size() != MBB.pred_size()) 00480 report("MBB has duplicate entries in its predecessor list.", &MBB); 00481 00482 MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end()); 00483 if (MInfo.Succs.size() != MBB.succ_size()) 00484 report("MBB has duplicate entries in its successor list.", &MBB); 00485 } 00486 00487 // Check that the register use lists are sane. 00488 MRI->verifyUseLists(); 00489 00490 verifyStackFrame(); 00491 } 00492 00493 // Does iterator point to a and b as the first two elements? 00494 static bool matchPair(MachineBasicBlock::const_succ_iterator i, 00495 const MachineBasicBlock *a, const MachineBasicBlock *b) { 00496 if (*i == a) 00497 return *++i == b; 00498 if (*i == b) 00499 return *++i == a; 00500 return false; 00501 } 00502 00503 void 00504 MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) { 00505 FirstTerminator = nullptr; 00506 00507 if (MRI->isSSA()) { 00508 // If this block has allocatable physical registers live-in, check that 00509 // it is an entry block or landing pad. 00510 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(), 00511 LE = MBB->livein_end(); 00512 LI != LE; ++LI) { 00513 unsigned reg = *LI; 00514 if (isAllocatable(reg) && !MBB->isLandingPad() && 00515 MBB != MBB->getParent()->begin()) { 00516 report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB); 00517 } 00518 } 00519 } 00520 00521 // Count the number of landing pad successors. 00522 SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs; 00523 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), 00524 E = MBB->succ_end(); I != E; ++I) { 00525 if ((*I)->isLandingPad()) 00526 LandingPadSuccs.insert(*I); 00527 if (!FunctionBlocks.count(*I)) 00528 report("MBB has successor that isn't part of the function.", MBB); 00529 if (!MBBInfoMap[*I].Preds.count(MBB)) { 00530 report("Inconsistent CFG", MBB); 00531 *OS << "MBB is not in the predecessor list of the successor BB#" 00532 << (*I)->getNumber() << ".\n"; 00533 } 00534 } 00535 00536 // Check the predecessor list. 00537 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(), 00538 E = MBB->pred_end(); I != E; ++I) { 00539 if (!FunctionBlocks.count(*I)) 00540 report("MBB has predecessor that isn't part of the function.", MBB); 00541 if (!MBBInfoMap[*I].Succs.count(MBB)) { 00542 report("Inconsistent CFG", MBB); 00543 *OS << "MBB is not in the successor list of the predecessor BB#" 00544 << (*I)->getNumber() << ".\n"; 00545 } 00546 } 00547 00548 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo(); 00549 const BasicBlock *BB = MBB->getBasicBlock(); 00550 if (LandingPadSuccs.size() > 1 && 00551 !(AsmInfo && 00552 AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj && 00553 BB && isa<SwitchInst>(BB->getTerminator()))) 00554 report("MBB has more than one landing pad successor", MBB); 00555 00556 // Call AnalyzeBranch. If it succeeds, there several more conditions to check. 00557 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 00558 SmallVector<MachineOperand, 4> Cond; 00559 if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB), 00560 TBB, FBB, Cond)) { 00561 // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's 00562 // check whether its answers match up with reality. 00563 if (!TBB && !FBB) { 00564 // Block falls through to its successor. 00565 MachineFunction::const_iterator MBBI = MBB; 00566 ++MBBI; 00567 if (MBBI == MF->end()) { 00568 // It's possible that the block legitimately ends with a noreturn 00569 // call or an unreachable, in which case it won't actually fall 00570 // out the bottom of the function. 00571 } else if (MBB->succ_size() == LandingPadSuccs.size()) { 00572 // It's possible that the block legitimately ends with a noreturn 00573 // call or an unreachable, in which case it won't actuall fall 00574 // out of the block. 00575 } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) { 00576 report("MBB exits via unconditional fall-through but doesn't have " 00577 "exactly one CFG successor!", MBB); 00578 } else if (!MBB->isSuccessor(MBBI)) { 00579 report("MBB exits via unconditional fall-through but its successor " 00580 "differs from its CFG successor!", MBB); 00581 } 00582 if (!MBB->empty() && MBB->back().isBarrier() && 00583 !TII->isPredicated(&MBB->back())) { 00584 report("MBB exits via unconditional fall-through but ends with a " 00585 "barrier instruction!", MBB); 00586 } 00587 if (!Cond.empty()) { 00588 report("MBB exits via unconditional fall-through but has a condition!", 00589 MBB); 00590 } 00591 } else if (TBB && !FBB && Cond.empty()) { 00592 // Block unconditionally branches somewhere. 00593 if (MBB->succ_size() != 1+LandingPadSuccs.size()) { 00594 report("MBB exits via unconditional branch but doesn't have " 00595 "exactly one CFG successor!", MBB); 00596 } else if (!MBB->isSuccessor(TBB)) { 00597 report("MBB exits via unconditional branch but the CFG " 00598 "successor doesn't match the actual successor!", MBB); 00599 } 00600 if (MBB->empty()) { 00601 report("MBB exits via unconditional branch but doesn't contain " 00602 "any instructions!", MBB); 00603 } else if (!MBB->back().isBarrier()) { 00604 report("MBB exits via unconditional branch but doesn't end with a " 00605 "barrier instruction!", MBB); 00606 } else if (!MBB->back().isTerminator()) { 00607 report("MBB exits via unconditional branch but the branch isn't a " 00608 "terminator instruction!", MBB); 00609 } 00610 } else if (TBB && !FBB && !Cond.empty()) { 00611 // Block conditionally branches somewhere, otherwise falls through. 00612 MachineFunction::const_iterator MBBI = MBB; 00613 ++MBBI; 00614 if (MBBI == MF->end()) { 00615 report("MBB conditionally falls through out of function!", MBB); 00616 } else if (MBB->succ_size() == 1) { 00617 // A conditional branch with only one successor is weird, but allowed. 00618 if (&*MBBI != TBB) 00619 report("MBB exits via conditional branch/fall-through but only has " 00620 "one CFG successor!", MBB); 00621 else if (TBB != *MBB->succ_begin()) 00622 report("MBB exits via conditional branch/fall-through but the CFG " 00623 "successor don't match the actual successor!", MBB); 00624 } else if (MBB->succ_size() != 2) { 00625 report("MBB exits via conditional branch/fall-through but doesn't have " 00626 "exactly two CFG successors!", MBB); 00627 } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) { 00628 report("MBB exits via conditional branch/fall-through but the CFG " 00629 "successors don't match the actual successors!", MBB); 00630 } 00631 if (MBB->empty()) { 00632 report("MBB exits via conditional branch/fall-through but doesn't " 00633 "contain any instructions!", MBB); 00634 } else if (MBB->back().isBarrier()) { 00635 report("MBB exits via conditional branch/fall-through but ends with a " 00636 "barrier instruction!", MBB); 00637 } else if (!MBB->back().isTerminator()) { 00638 report("MBB exits via conditional branch/fall-through but the branch " 00639 "isn't a terminator instruction!", MBB); 00640 } 00641 } else if (TBB && FBB) { 00642 // Block conditionally branches somewhere, otherwise branches 00643 // somewhere else. 00644 if (MBB->succ_size() == 1) { 00645 // A conditional branch with only one successor is weird, but allowed. 00646 if (FBB != TBB) 00647 report("MBB exits via conditional branch/branch through but only has " 00648 "one CFG successor!", MBB); 00649 else if (TBB != *MBB->succ_begin()) 00650 report("MBB exits via conditional branch/branch through but the CFG " 00651 "successor don't match the actual successor!", MBB); 00652 } else if (MBB->succ_size() != 2) { 00653 report("MBB exits via conditional branch/branch but doesn't have " 00654 "exactly two CFG successors!", MBB); 00655 } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) { 00656 report("MBB exits via conditional branch/branch but the CFG " 00657 "successors don't match the actual successors!", MBB); 00658 } 00659 if (MBB->empty()) { 00660 report("MBB exits via conditional branch/branch but doesn't " 00661 "contain any instructions!", MBB); 00662 } else if (!MBB->back().isBarrier()) { 00663 report("MBB exits via conditional branch/branch but doesn't end with a " 00664 "barrier instruction!", MBB); 00665 } else if (!MBB->back().isTerminator()) { 00666 report("MBB exits via conditional branch/branch but the branch " 00667 "isn't a terminator instruction!", MBB); 00668 } 00669 if (Cond.empty()) { 00670 report("MBB exits via conditinal branch/branch but there's no " 00671 "condition!", MBB); 00672 } 00673 } else { 00674 report("AnalyzeBranch returned invalid data!", MBB); 00675 } 00676 } 00677 00678 regsLive.clear(); 00679 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(), 00680 E = MBB->livein_end(); I != E; ++I) { 00681 if (!TargetRegisterInfo::isPhysicalRegister(*I)) { 00682 report("MBB live-in list contains non-physical register", MBB); 00683 continue; 00684 } 00685 for (MCSubRegIterator SubRegs(*I, TRI, /*IncludeSelf=*/true); 00686 SubRegs.isValid(); ++SubRegs) 00687 regsLive.insert(*SubRegs); 00688 } 00689 regsLiveInButUnused = regsLive; 00690 00691 const MachineFrameInfo *MFI = MF->getFrameInfo(); 00692 assert(MFI && "Function has no frame info"); 00693 BitVector PR = MFI->getPristineRegs(MBB); 00694 for (int I = PR.find_first(); I>0; I = PR.find_next(I)) { 00695 for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true); 00696 SubRegs.isValid(); ++SubRegs) 00697 regsLive.insert(*SubRegs); 00698 } 00699 00700 regsKilled.clear(); 00701 regsDefined.clear(); 00702 00703 if (Indexes) 00704 lastIndex = Indexes->getMBBStartIdx(MBB); 00705 } 00706 00707 // This function gets called for all bundle headers, including normal 00708 // stand-alone unbundled instructions. 00709 void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) { 00710 if (Indexes && Indexes->hasIndex(MI)) { 00711 SlotIndex idx = Indexes->getInstructionIndex(MI); 00712 if (!(idx > lastIndex)) { 00713 report("Instruction index out of order", MI); 00714 *OS << "Last instruction was at " << lastIndex << '\n'; 00715 } 00716 lastIndex = idx; 00717 } 00718 00719 // Ensure non-terminators don't follow terminators. 00720 // Ignore predicated terminators formed by if conversion. 00721 // FIXME: If conversion shouldn't need to violate this rule. 00722 if (MI->isTerminator() && !TII->isPredicated(MI)) { 00723 if (!FirstTerminator) 00724 FirstTerminator = MI; 00725 } else if (FirstTerminator) { 00726 report("Non-terminator instruction after the first terminator", MI); 00727 *OS << "First terminator was:\t" << *FirstTerminator; 00728 } 00729 } 00730 00731 // The operands on an INLINEASM instruction must follow a template. 00732 // Verify that the flag operands make sense. 00733 void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) { 00734 // The first two operands on INLINEASM are the asm string and global flags. 00735 if (MI->getNumOperands() < 2) { 00736 report("Too few operands on inline asm", MI); 00737 return; 00738 } 00739 if (!MI->getOperand(0).isSymbol()) 00740 report("Asm string must be an external symbol", MI); 00741 if (!MI->getOperand(1).isImm()) 00742 report("Asm flags must be an immediate", MI); 00743 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2, 00744 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16. 00745 if (!isUInt<5>(MI->getOperand(1).getImm())) 00746 report("Unknown asm flags", &MI->getOperand(1), 1); 00747 00748 assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed"); 00749 00750 unsigned OpNo = InlineAsm::MIOp_FirstOperand; 00751 unsigned NumOps; 00752 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) { 00753 const MachineOperand &MO = MI->getOperand(OpNo); 00754 // There may be implicit ops after the fixed operands. 00755 if (!MO.isImm()) 00756 break; 00757 NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm()); 00758 } 00759 00760 if (OpNo > MI->getNumOperands()) 00761 report("Missing operands in last group", MI); 00762 00763 // An optional MDNode follows the groups. 00764 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata()) 00765 ++OpNo; 00766 00767 // All trailing operands must be implicit registers. 00768 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) { 00769 const MachineOperand &MO = MI->getOperand(OpNo); 00770 if (!MO.isReg() || !MO.isImplicit()) 00771 report("Expected implicit register after groups", &MO, OpNo); 00772 } 00773 } 00774 00775 void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) { 00776 const MCInstrDesc &MCID = MI->getDesc(); 00777 if (MI->getNumOperands() < MCID.getNumOperands()) { 00778 report("Too few operands", MI); 00779 *OS << MCID.getNumOperands() << " operands expected, but " 00780 << MI->getNumOperands() << " given.\n"; 00781 } 00782 00783 // Check the tied operands. 00784 if (MI->isInlineAsm()) 00785 verifyInlineAsm(MI); 00786 00787 // Check the MachineMemOperands for basic consistency. 00788 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(), 00789 E = MI->memoperands_end(); I != E; ++I) { 00790 if ((*I)->isLoad() && !MI->mayLoad()) 00791 report("Missing mayLoad flag", MI); 00792 if ((*I)->isStore() && !MI->mayStore()) 00793 report("Missing mayStore flag", MI); 00794 } 00795 00796 // Debug values must not have a slot index. 00797 // Other instructions must have one, unless they are inside a bundle. 00798 if (LiveInts) { 00799 bool mapped = !LiveInts->isNotInMIMap(MI); 00800 if (MI->isDebugValue()) { 00801 if (mapped) 00802 report("Debug instruction has a slot index", MI); 00803 } else if (MI->isInsideBundle()) { 00804 if (mapped) 00805 report("Instruction inside bundle has a slot index", MI); 00806 } else { 00807 if (!mapped) 00808 report("Missing slot index", MI); 00809 } 00810 } 00811 00812 StringRef ErrorInfo; 00813 if (!TII->verifyInstruction(MI, ErrorInfo)) 00814 report(ErrorInfo.data(), MI); 00815 } 00816 00817 void 00818 MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) { 00819 const MachineInstr *MI = MO->getParent(); 00820 const MCInstrDesc &MCID = MI->getDesc(); 00821 00822 // The first MCID.NumDefs operands must be explicit register defines 00823 if (MONum < MCID.getNumDefs()) { 00824 const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; 00825 if (!MO->isReg()) 00826 report("Explicit definition must be a register", MO, MONum); 00827 else if (!MO->isDef() && !MCOI.isOptionalDef()) 00828 report("Explicit definition marked as use", MO, MONum); 00829 else if (MO->isImplicit()) 00830 report("Explicit definition marked as implicit", MO, MONum); 00831 } else if (MONum < MCID.getNumOperands()) { 00832 const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; 00833 // Don't check if it's the last operand in a variadic instruction. See, 00834 // e.g., LDM_RET in the arm back end. 00835 if (MO->isReg() && 00836 !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) { 00837 if (MO->isDef() && !MCOI.isOptionalDef()) 00838 report("Explicit operand marked as def", MO, MONum); 00839 if (MO->isImplicit()) 00840 report("Explicit operand marked as implicit", MO, MONum); 00841 } 00842 00843 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO); 00844 if (TiedTo != -1) { 00845 if (!MO->isReg()) 00846 report("Tied use must be a register", MO, MONum); 00847 else if (!MO->isTied()) 00848 report("Operand should be tied", MO, MONum); 00849 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum)) 00850 report("Tied def doesn't match MCInstrDesc", MO, MONum); 00851 } else if (MO->isReg() && MO->isTied()) 00852 report("Explicit operand should not be tied", MO, MONum); 00853 } else { 00854 // ARM adds %reg0 operands to indicate predicates. We'll allow that. 00855 if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg()) 00856 report("Extra explicit operand on non-variadic instruction", MO, MONum); 00857 } 00858 00859 switch (MO->getType()) { 00860 case MachineOperand::MO_Register: { 00861 const unsigned Reg = MO->getReg(); 00862 if (!Reg) 00863 return; 00864 if (MRI->tracksLiveness() && !MI->isDebugValue()) 00865 checkLiveness(MO, MONum); 00866 00867 // Verify the consistency of tied operands. 00868 if (MO->isTied()) { 00869 unsigned OtherIdx = MI->findTiedOperandIdx(MONum); 00870 const MachineOperand &OtherMO = MI->getOperand(OtherIdx); 00871 if (!OtherMO.isReg()) 00872 report("Must be tied to a register", MO, MONum); 00873 if (!OtherMO.isTied()) 00874 report("Missing tie flags on tied operand", MO, MONum); 00875 if (MI->findTiedOperandIdx(OtherIdx) != MONum) 00876 report("Inconsistent tie links", MO, MONum); 00877 if (MONum < MCID.getNumDefs()) { 00878 if (OtherIdx < MCID.getNumOperands()) { 00879 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO)) 00880 report("Explicit def tied to explicit use without tie constraint", 00881 MO, MONum); 00882 } else { 00883 if (!OtherMO.isImplicit()) 00884 report("Explicit def should be tied to implicit use", MO, MONum); 00885 } 00886 } 00887 } 00888 00889 // Verify two-address constraints after leaving SSA form. 00890 unsigned DefIdx; 00891 if (!MRI->isSSA() && MO->isUse() && 00892 MI->isRegTiedToDefOperand(MONum, &DefIdx) && 00893 Reg != MI->getOperand(DefIdx).getReg()) 00894 report("Two-address instruction operands must be identical", MO, MONum); 00895 00896 // Check register classes. 00897 if (MONum < MCID.getNumOperands() && !MO->isImplicit()) { 00898 unsigned SubIdx = MO->getSubReg(); 00899 00900 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 00901 if (SubIdx) { 00902 report("Illegal subregister index for physical register", MO, MONum); 00903 return; 00904 } 00905 if (const TargetRegisterClass *DRC = 00906 TII->getRegClass(MCID, MONum, TRI, *MF)) { 00907 if (!DRC->contains(Reg)) { 00908 report("Illegal physical register for instruction", MO, MONum); 00909 *OS << TRI->getName(Reg) << " is not a " 00910 << DRC->getName() << " register.\n"; 00911 } 00912 } 00913 } else { 00914 // Virtual register. 00915 const TargetRegisterClass *RC = MRI->getRegClass(Reg); 00916 if (SubIdx) { 00917 const TargetRegisterClass *SRC = 00918 TRI->getSubClassWithSubReg(RC, SubIdx); 00919 if (!SRC) { 00920 report("Invalid subregister index for virtual register", MO, MONum); 00921 *OS << "Register class " << RC->getName() 00922 << " does not support subreg index " << SubIdx << "\n"; 00923 return; 00924 } 00925 if (RC != SRC) { 00926 report("Invalid register class for subregister index", MO, MONum); 00927 *OS << "Register class " << RC->getName() 00928 << " does not fully support subreg index " << SubIdx << "\n"; 00929 return; 00930 } 00931 } 00932 if (const TargetRegisterClass *DRC = 00933 TII->getRegClass(MCID, MONum, TRI, *MF)) { 00934 if (SubIdx) { 00935 const TargetRegisterClass *SuperRC = 00936 TRI->getLargestLegalSuperClass(RC); 00937 if (!SuperRC) { 00938 report("No largest legal super class exists.", MO, MONum); 00939 return; 00940 } 00941 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx); 00942 if (!DRC) { 00943 report("No matching super-reg register class.", MO, MONum); 00944 return; 00945 } 00946 } 00947 if (!RC->hasSuperClassEq(DRC)) { 00948 report("Illegal virtual register for instruction", MO, MONum); 00949 *OS << "Expected a " << DRC->getName() << " register, but got a " 00950 << RC->getName() << " register\n"; 00951 } 00952 } 00953 } 00954 } 00955 break; 00956 } 00957 00958 case MachineOperand::MO_RegisterMask: 00959 regMasks.push_back(MO->getRegMask()); 00960 break; 00961 00962 case MachineOperand::MO_MachineBasicBlock: 00963 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent())) 00964 report("PHI operand is not in the CFG", MO, MONum); 00965 break; 00966 00967 case MachineOperand::MO_FrameIndex: 00968 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) && 00969 LiveInts && !LiveInts->isNotInMIMap(MI)) { 00970 LiveInterval &LI = LiveStks->getInterval(MO->getIndex()); 00971 SlotIndex Idx = LiveInts->getInstructionIndex(MI); 00972 if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) { 00973 report("Instruction loads from dead spill slot", MO, MONum); 00974 *OS << "Live stack: " << LI << '\n'; 00975 } 00976 if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) { 00977 report("Instruction stores to dead spill slot", MO, MONum); 00978 *OS << "Live stack: " << LI << '\n'; 00979 } 00980 } 00981 break; 00982 00983 default: 00984 break; 00985 } 00986 } 00987 00988 void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) { 00989 const MachineInstr *MI = MO->getParent(); 00990 const unsigned Reg = MO->getReg(); 00991 00992 // Both use and def operands can read a register. 00993 if (MO->readsReg()) { 00994 regsLiveInButUnused.erase(Reg); 00995 00996 if (MO->isKill()) 00997 addRegWithSubRegs(regsKilled, Reg); 00998 00999 // Check that LiveVars knows this kill. 01000 if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) && 01001 MO->isKill()) { 01002 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); 01003 if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end()) 01004 report("Kill missing from LiveVariables", MO, MONum); 01005 } 01006 01007 // Check LiveInts liveness and kill. 01008 if (LiveInts && !LiveInts->isNotInMIMap(MI)) { 01009 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI); 01010 // Check the cached regunit intervals. 01011 if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) { 01012 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) { 01013 if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) { 01014 LiveQueryResult LRQ = LR->Query(UseIdx); 01015 if (!LRQ.valueIn()) { 01016 report("No live segment at use", MO, MONum); 01017 *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI) 01018 << ' ' << *LR << '\n'; 01019 } 01020 if (MO->isKill() && !LRQ.isKill()) { 01021 report("Live range continues after kill flag", MO, MONum); 01022 *OS << PrintRegUnit(*Units, TRI) << ' ' << *LR << '\n'; 01023 } 01024 } 01025 } 01026 } 01027 01028 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 01029 if (LiveInts->hasInterval(Reg)) { 01030 // This is a virtual register interval. 01031 const LiveInterval &LI = LiveInts->getInterval(Reg); 01032 LiveQueryResult LRQ = LI.Query(UseIdx); 01033 if (!LRQ.valueIn()) { 01034 report("No live segment at use", MO, MONum); 01035 *OS << UseIdx << " is not live in " << LI << '\n'; 01036 } 01037 // Check for extra kill flags. 01038 // Note that we allow missing kill flags for now. 01039 if (MO->isKill() && !LRQ.isKill()) { 01040 report("Live range continues after kill flag", MO, MONum); 01041 *OS << "Live range: " << LI << '\n'; 01042 } 01043 } else { 01044 report("Virtual register has no live interval", MO, MONum); 01045 } 01046 } 01047 } 01048 01049 // Use of a dead register. 01050 if (!regsLive.count(Reg)) { 01051 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 01052 // Reserved registers may be used even when 'dead'. 01053 if (!isReserved(Reg)) 01054 report("Using an undefined physical register", MO, MONum); 01055 } else if (MRI->def_empty(Reg)) { 01056 report("Reading virtual register without a def", MO, MONum); 01057 } else { 01058 BBInfo &MInfo = MBBInfoMap[MI->getParent()]; 01059 // We don't know which virtual registers are live in, so only complain 01060 // if vreg was killed in this MBB. Otherwise keep track of vregs that 01061 // must be live in. PHI instructions are handled separately. 01062 if (MInfo.regsKilled.count(Reg)) 01063 report("Using a killed virtual register", MO, MONum); 01064 else if (!MI->isPHI()) 01065 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI)); 01066 } 01067 } 01068 } 01069 01070 if (MO->isDef()) { 01071 // Register defined. 01072 // TODO: verify that earlyclobber ops are not used. 01073 if (MO->isDead()) 01074 addRegWithSubRegs(regsDead, Reg); 01075 else 01076 addRegWithSubRegs(regsDefined, Reg); 01077 01078 // Verify SSA form. 01079 if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) && 01080 std::next(MRI->def_begin(Reg)) != MRI->def_end()) 01081 report("Multiple virtual register defs in SSA form", MO, MONum); 01082 01083 // Check LiveInts for a live segment, but only for virtual registers. 01084 if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) && 01085 !LiveInts->isNotInMIMap(MI)) { 01086 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI); 01087 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber()); 01088 if (LiveInts->hasInterval(Reg)) { 01089 const LiveInterval &LI = LiveInts->getInterval(Reg); 01090 if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) { 01091 assert(VNI && "NULL valno is not allowed"); 01092 if (VNI->def != DefIdx) { 01093 report("Inconsistent valno->def", MO, MONum); 01094 *OS << "Valno " << VNI->id << " is not defined at " 01095 << DefIdx << " in " << LI << '\n'; 01096 } 01097 } else { 01098 report("No live segment at def", MO, MONum); 01099 *OS << DefIdx << " is not live in " << LI << '\n'; 01100 } 01101 // Check that, if the dead def flag is present, LiveInts agree. 01102 if (MO->isDead()) { 01103 LiveQueryResult LRQ = LI.Query(DefIdx); 01104 if (!LRQ.isDeadDef()) { 01105 report("Live range continues after dead def flag", MO, MONum); 01106 *OS << "Live range: " << LI << '\n'; 01107 } 01108 } 01109 } else { 01110 report("Virtual register has no Live interval", MO, MONum); 01111 } 01112 } 01113 } 01114 } 01115 01116 void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) { 01117 } 01118 01119 // This function gets called after visiting all instructions in a bundle. The 01120 // argument points to the bundle header. 01121 // Normal stand-alone instructions are also considered 'bundles', and this 01122 // function is called for all of them. 01123 void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) { 01124 BBInfo &MInfo = MBBInfoMap[MI->getParent()]; 01125 set_union(MInfo.regsKilled, regsKilled); 01126 set_subtract(regsLive, regsKilled); regsKilled.clear(); 01127 // Kill any masked registers. 01128 while (!regMasks.empty()) { 01129 const uint32_t *Mask = regMasks.pop_back_val(); 01130 for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I) 01131 if (TargetRegisterInfo::isPhysicalRegister(*I) && 01132 MachineOperand::clobbersPhysReg(Mask, *I)) 01133 regsDead.push_back(*I); 01134 } 01135 set_subtract(regsLive, regsDead); regsDead.clear(); 01136 set_union(regsLive, regsDefined); regsDefined.clear(); 01137 } 01138 01139 void 01140 MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) { 01141 MBBInfoMap[MBB].regsLiveOut = regsLive; 01142 regsLive.clear(); 01143 01144 if (Indexes) { 01145 SlotIndex stop = Indexes->getMBBEndIdx(MBB); 01146 if (!(stop > lastIndex)) { 01147 report("Block ends before last instruction index", MBB); 01148 *OS << "Block ends at " << stop 01149 << " last instruction was at " << lastIndex << '\n'; 01150 } 01151 lastIndex = stop; 01152 } 01153 } 01154 01155 // Calculate the largest possible vregsPassed sets. These are the registers that 01156 // can pass through an MBB live, but may not be live every time. It is assumed 01157 // that all vregsPassed sets are empty before the call. 01158 void MachineVerifier::calcRegsPassed() { 01159 // First push live-out regs to successors' vregsPassed. Remember the MBBs that 01160 // have any vregsPassed. 01161 SmallPtrSet<const MachineBasicBlock*, 8> todo; 01162 for (const auto &MBB : *MF) { 01163 BBInfo &MInfo = MBBInfoMap[&MBB]; 01164 if (!MInfo.reachable) 01165 continue; 01166 for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(), 01167 SuE = MBB.succ_end(); SuI != SuE; ++SuI) { 01168 BBInfo &SInfo = MBBInfoMap[*SuI]; 01169 if (SInfo.addPassed(MInfo.regsLiveOut)) 01170 todo.insert(*SuI); 01171 } 01172 } 01173 01174 // Iteratively push vregsPassed to successors. This will converge to the same 01175 // final state regardless of DenseSet iteration order. 01176 while (!todo.empty()) { 01177 const MachineBasicBlock *MBB = *todo.begin(); 01178 todo.erase(MBB); 01179 BBInfo &MInfo = MBBInfoMap[MBB]; 01180 for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), 01181 SuE = MBB->succ_end(); SuI != SuE; ++SuI) { 01182 if (*SuI == MBB) 01183 continue; 01184 BBInfo &SInfo = MBBInfoMap[*SuI]; 01185 if (SInfo.addPassed(MInfo.vregsPassed)) 01186 todo.insert(*SuI); 01187 } 01188 } 01189 } 01190 01191 // Calculate the set of virtual registers that must be passed through each basic 01192 // block in order to satisfy the requirements of successor blocks. This is very 01193 // similar to calcRegsPassed, only backwards. 01194 void MachineVerifier::calcRegsRequired() { 01195 // First push live-in regs to predecessors' vregsRequired. 01196 SmallPtrSet<const MachineBasicBlock*, 8> todo; 01197 for (const auto &MBB : *MF) { 01198 BBInfo &MInfo = MBBInfoMap[&MBB]; 01199 for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(), 01200 PrE = MBB.pred_end(); PrI != PrE; ++PrI) { 01201 BBInfo &PInfo = MBBInfoMap[*PrI]; 01202 if (PInfo.addRequired(MInfo.vregsLiveIn)) 01203 todo.insert(*PrI); 01204 } 01205 } 01206 01207 // Iteratively push vregsRequired to predecessors. This will converge to the 01208 // same final state regardless of DenseSet iteration order. 01209 while (!todo.empty()) { 01210 const MachineBasicBlock *MBB = *todo.begin(); 01211 todo.erase(MBB); 01212 BBInfo &MInfo = MBBInfoMap[MBB]; 01213 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), 01214 PrE = MBB->pred_end(); PrI != PrE; ++PrI) { 01215 if (*PrI == MBB) 01216 continue; 01217 BBInfo &SInfo = MBBInfoMap[*PrI]; 01218 if (SInfo.addRequired(MInfo.vregsRequired)) 01219 todo.insert(*PrI); 01220 } 01221 } 01222 } 01223 01224 // Check PHI instructions at the beginning of MBB. It is assumed that 01225 // calcRegsPassed has been run so BBInfo::isLiveOut is valid. 01226 void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) { 01227 SmallPtrSet<const MachineBasicBlock*, 8> seen; 01228 for (const auto &BBI : *MBB) { 01229 if (!BBI.isPHI()) 01230 break; 01231 seen.clear(); 01232 01233 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) { 01234 unsigned Reg = BBI.getOperand(i).getReg(); 01235 const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB(); 01236 if (!Pre->isSuccessor(MBB)) 01237 continue; 01238 seen.insert(Pre); 01239 BBInfo &PrInfo = MBBInfoMap[Pre]; 01240 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg)) 01241 report("PHI operand is not live-out from predecessor", 01242 &BBI.getOperand(i), i); 01243 } 01244 01245 // Did we see all predecessors? 01246 for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), 01247 PrE = MBB->pred_end(); PrI != PrE; ++PrI) { 01248 if (!seen.count(*PrI)) { 01249 report("Missing PHI operand", &BBI); 01250 *OS << "BB#" << (*PrI)->getNumber() 01251 << " is a predecessor according to the CFG.\n"; 01252 } 01253 } 01254 } 01255 } 01256 01257 void MachineVerifier::visitMachineFunctionAfter() { 01258 calcRegsPassed(); 01259 01260 for (const auto &MBB : *MF) { 01261 BBInfo &MInfo = MBBInfoMap[&MBB]; 01262 01263 // Skip unreachable MBBs. 01264 if (!MInfo.reachable) 01265 continue; 01266 01267 checkPHIOps(&MBB); 01268 } 01269 01270 // Now check liveness info if available 01271 calcRegsRequired(); 01272 01273 // Check for killed virtual registers that should be live out. 01274 for (const auto &MBB : *MF) { 01275 BBInfo &MInfo = MBBInfoMap[&MBB]; 01276 for (RegSet::iterator 01277 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; 01278 ++I) 01279 if (MInfo.regsKilled.count(*I)) { 01280 report("Virtual register killed in block, but needed live out.", &MBB); 01281 *OS << "Virtual register " << PrintReg(*I) 01282 << " is used after the block.\n"; 01283 } 01284 } 01285 01286 if (!MF->empty()) { 01287 BBInfo &MInfo = MBBInfoMap[&MF->front()]; 01288 for (RegSet::iterator 01289 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; 01290 ++I) 01291 report("Virtual register def doesn't dominate all uses.", 01292 MRI->getVRegDef(*I)); 01293 } 01294 01295 if (LiveVars) 01296 verifyLiveVariables(); 01297 if (LiveInts) 01298 verifyLiveIntervals(); 01299 } 01300 01301 void MachineVerifier::verifyLiveVariables() { 01302 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars"); 01303 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 01304 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 01305 LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); 01306 for (const auto &MBB : *MF) { 01307 BBInfo &MInfo = MBBInfoMap[&MBB]; 01308 01309 // Our vregsRequired should be identical to LiveVariables' AliveBlocks 01310 if (MInfo.vregsRequired.count(Reg)) { 01311 if (!VI.AliveBlocks.test(MBB.getNumber())) { 01312 report("LiveVariables: Block missing from AliveBlocks", &MBB); 01313 *OS << "Virtual register " << PrintReg(Reg) 01314 << " must be live through the block.\n"; 01315 } 01316 } else { 01317 if (VI.AliveBlocks.test(MBB.getNumber())) { 01318 report("LiveVariables: Block should not be in AliveBlocks", &MBB); 01319 *OS << "Virtual register " << PrintReg(Reg) 01320 << " is not needed live through the block.\n"; 01321 } 01322 } 01323 } 01324 } 01325 } 01326 01327 void MachineVerifier::verifyLiveIntervals() { 01328 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts"); 01329 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 01330 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 01331 01332 // Spilling and splitting may leave unused registers around. Skip them. 01333 if (MRI->reg_nodbg_empty(Reg)) 01334 continue; 01335 01336 if (!LiveInts->hasInterval(Reg)) { 01337 report("Missing live interval for virtual register", MF); 01338 *OS << PrintReg(Reg, TRI) << " still has defs or uses\n"; 01339 continue; 01340 } 01341 01342 const LiveInterval &LI = LiveInts->getInterval(Reg); 01343 assert(Reg == LI.reg && "Invalid reg to interval mapping"); 01344 verifyLiveInterval(LI); 01345 } 01346 01347 // Verify all the cached regunit intervals. 01348 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) 01349 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i)) 01350 verifyLiveRange(*LR, i); 01351 } 01352 01353 void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR, 01354 const VNInfo *VNI, 01355 unsigned Reg) { 01356 if (VNI->isUnused()) 01357 return; 01358 01359 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def); 01360 01361 if (!DefVNI) { 01362 report("Valno not live at def and not marked unused", MF, LR); 01363 *OS << "Valno #" << VNI->id << '\n'; 01364 return; 01365 } 01366 01367 if (DefVNI != VNI) { 01368 report("Live segment at def has different valno", MF, LR); 01369 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def 01370 << " where valno #" << DefVNI->id << " is live\n"; 01371 return; 01372 } 01373 01374 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def); 01375 if (!MBB) { 01376 report("Invalid definition index", MF, LR); 01377 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def 01378 << " in " << LR << '\n'; 01379 return; 01380 } 01381 01382 if (VNI->isPHIDef()) { 01383 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) { 01384 report("PHIDef value is not defined at MBB start", MBB, LR); 01385 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def 01386 << ", not at the beginning of BB#" << MBB->getNumber() << '\n'; 01387 } 01388 return; 01389 } 01390 01391 // Non-PHI def. 01392 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); 01393 if (!MI) { 01394 report("No instruction at def index", MBB, LR); 01395 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n'; 01396 return; 01397 } 01398 01399 if (Reg != 0) { 01400 bool hasDef = false; 01401 bool isEarlyClobber = false; 01402 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { 01403 if (!MOI->isReg() || !MOI->isDef()) 01404 continue; 01405 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 01406 if (MOI->getReg() != Reg) 01407 continue; 01408 } else { 01409 if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) || 01410 !TRI->hasRegUnit(MOI->getReg(), Reg)) 01411 continue; 01412 } 01413 hasDef = true; 01414 if (MOI->isEarlyClobber()) 01415 isEarlyClobber = true; 01416 } 01417 01418 if (!hasDef) { 01419 report("Defining instruction does not modify register", MI); 01420 *OS << "Valno #" << VNI->id << " in " << LR << '\n'; 01421 } 01422 01423 // Early clobber defs begin at USE slots, but other defs must begin at 01424 // DEF slots. 01425 if (isEarlyClobber) { 01426 if (!VNI->def.isEarlyClobber()) { 01427 report("Early clobber def must be at an early-clobber slot", MBB, LR); 01428 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n'; 01429 } 01430 } else if (!VNI->def.isRegister()) { 01431 report("Non-PHI, non-early clobber def must be at a register slot", 01432 MBB, LR); 01433 *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n'; 01434 } 01435 } 01436 } 01437 01438 void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR, 01439 const LiveRange::const_iterator I, 01440 unsigned Reg) { 01441 const LiveRange::Segment &S = *I; 01442 const VNInfo *VNI = S.valno; 01443 assert(VNI && "Live segment has no valno"); 01444 01445 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) { 01446 report("Foreign valno in live segment", MF, LR); 01447 *OS << S << " has a bad valno\n"; 01448 } 01449 01450 if (VNI->isUnused()) { 01451 report("Live segment valno is marked unused", MF, LR); 01452 *OS << S << '\n'; 01453 } 01454 01455 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start); 01456 if (!MBB) { 01457 report("Bad start of live segment, no basic block", MF, LR); 01458 *OS << S << '\n'; 01459 return; 01460 } 01461 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB); 01462 if (S.start != MBBStartIdx && S.start != VNI->def) { 01463 report("Live segment must begin at MBB entry or valno def", MBB, LR); 01464 *OS << S << '\n'; 01465 } 01466 01467 const MachineBasicBlock *EndMBB = 01468 LiveInts->getMBBFromIndex(S.end.getPrevSlot()); 01469 if (!EndMBB) { 01470 report("Bad end of live segment, no basic block", MF, LR); 01471 *OS << S << '\n'; 01472 return; 01473 } 01474 01475 // No more checks for live-out segments. 01476 if (S.end == LiveInts->getMBBEndIdx(EndMBB)) 01477 return; 01478 01479 // RegUnit intervals are allowed dead phis. 01480 if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() && 01481 S.start == VNI->def && S.end == VNI->def.getDeadSlot()) 01482 return; 01483 01484 // The live segment is ending inside EndMBB 01485 const MachineInstr *MI = 01486 LiveInts->getInstructionFromIndex(S.end.getPrevSlot()); 01487 if (!MI) { 01488 report("Live segment doesn't end at a valid instruction", EndMBB, LR); 01489 *OS << S << '\n'; 01490 return; 01491 } 01492 01493 // The block slot must refer to a basic block boundary. 01494 if (S.end.isBlock()) { 01495 report("Live segment ends at B slot of an instruction", EndMBB, LR); 01496 *OS << S << '\n'; 01497 } 01498 01499 if (S.end.isDead()) { 01500 // Segment ends on the dead slot. 01501 // That means there must be a dead def. 01502 if (!SlotIndex::isSameInstr(S.start, S.end)) { 01503 report("Live segment ending at dead slot spans instructions", EndMBB, LR); 01504 *OS << S << '\n'; 01505 } 01506 } 01507 01508 // A live segment can only end at an early-clobber slot if it is being 01509 // redefined by an early-clobber def. 01510 if (S.end.isEarlyClobber()) { 01511 if (I+1 == LR.end() || (I+1)->start != S.end) { 01512 report("Live segment ending at early clobber slot must be " 01513 "redefined by an EC def in the same instruction", EndMBB, LR); 01514 *OS << S << '\n'; 01515 } 01516 } 01517 01518 // The following checks only apply to virtual registers. Physreg liveness 01519 // is too weird to check. 01520 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 01521 // A live segment can end with either a redefinition, a kill flag on a 01522 // use, or a dead flag on a def. 01523 bool hasRead = false; 01524 for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { 01525 if (!MOI->isReg() || MOI->getReg() != Reg) 01526 continue; 01527 if (MOI->readsReg()) 01528 hasRead = true; 01529 } 01530 if (!S.end.isDead()) { 01531 if (!hasRead) { 01532 report("Instruction ending live segment doesn't read the register", MI); 01533 *OS << S << " in " << LR << '\n'; 01534 } 01535 } 01536 } 01537 01538 // Now check all the basic blocks in this live segment. 01539 MachineFunction::const_iterator MFI = MBB; 01540 // Is this live segment the beginning of a non-PHIDef VN? 01541 if (S.start == VNI->def && !VNI->isPHIDef()) { 01542 // Not live-in to any blocks. 01543 if (MBB == EndMBB) 01544 return; 01545 // Skip this block. 01546 ++MFI; 01547 } 01548 for (;;) { 01549 assert(LiveInts->isLiveInToMBB(LR, MFI)); 01550 // We don't know how to track physregs into a landing pad. 01551 if (!TargetRegisterInfo::isVirtualRegister(Reg) && 01552 MFI->isLandingPad()) { 01553 if (&*MFI == EndMBB) 01554 break; 01555 ++MFI; 01556 continue; 01557 } 01558 01559 // Is VNI a PHI-def in the current block? 01560 bool IsPHI = VNI->isPHIDef() && 01561 VNI->def == LiveInts->getMBBStartIdx(MFI); 01562 01563 // Check that VNI is live-out of all predecessors. 01564 for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(), 01565 PE = MFI->pred_end(); PI != PE; ++PI) { 01566 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI); 01567 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd); 01568 01569 // All predecessors must have a live-out value. 01570 if (!PVNI) { 01571 report("Register not marked live out of predecessor", *PI, LR); 01572 *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber() 01573 << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before " 01574 << PEnd << '\n'; 01575 continue; 01576 } 01577 01578 // Only PHI-defs can take different predecessor values. 01579 if (!IsPHI && PVNI != VNI) { 01580 report("Different value live out of predecessor", *PI, LR); 01581 *OS << "Valno #" << PVNI->id << " live out of BB#" 01582 << (*PI)->getNumber() << '@' << PEnd 01583 << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber() 01584 << '@' << LiveInts->getMBBStartIdx(MFI) << '\n'; 01585 } 01586 } 01587 if (&*MFI == EndMBB) 01588 break; 01589 ++MFI; 01590 } 01591 } 01592 01593 void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg) { 01594 for (LiveRange::const_vni_iterator I = LR.vni_begin(), E = LR.vni_end(); 01595 I != E; ++I) 01596 verifyLiveRangeValue(LR, *I, Reg); 01597 01598 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I) 01599 verifyLiveRangeSegment(LR, I, Reg); 01600 } 01601 01602 void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) { 01603 verifyLiveRange(LI, LI.reg); 01604 01605 // Check the LI only has one connected component. 01606 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { 01607 ConnectedVNInfoEqClasses ConEQ(*LiveInts); 01608 unsigned NumComp = ConEQ.Classify(&LI); 01609 if (NumComp > 1) { 01610 report("Multiple connected components in live interval", MF, LI); 01611 for (unsigned comp = 0; comp != NumComp; ++comp) { 01612 *OS << comp << ": valnos"; 01613 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), 01614 E = LI.vni_end(); I!=E; ++I) 01615 if (comp == ConEQ.getEqClass(*I)) 01616 *OS << ' ' << (*I)->id; 01617 *OS << '\n'; 01618 } 01619 } 01620 } 01621 } 01622 01623 namespace { 01624 // FrameSetup and FrameDestroy can have zero adjustment, so using a single 01625 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the 01626 // value is zero. 01627 // We use a bool plus an integer to capture the stack state. 01628 struct StackStateOfBB { 01629 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false), 01630 ExitIsSetup(false) { } 01631 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) : 01632 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup), 01633 ExitIsSetup(ExitSetup) { } 01634 // Can be negative, which means we are setting up a frame. 01635 int EntryValue; 01636 int ExitValue; 01637 bool EntryIsSetup; 01638 bool ExitIsSetup; 01639 }; 01640 } 01641 01642 /// Make sure on every path through the CFG, a FrameSetup <n> is always followed 01643 /// by a FrameDestroy <n>, stack adjustments are identical on all 01644 /// CFG edges to a merge point, and frame is destroyed at end of a return block. 01645 void MachineVerifier::verifyStackFrame() { 01646 int FrameSetupOpcode = TII->getCallFrameSetupOpcode(); 01647 int FrameDestroyOpcode = TII->getCallFrameDestroyOpcode(); 01648 01649 SmallVector<StackStateOfBB, 8> SPState; 01650 SPState.resize(MF->getNumBlockIDs()); 01651 SmallPtrSet<const MachineBasicBlock*, 8> Reachable; 01652 01653 // Visit the MBBs in DFS order. 01654 for (df_ext_iterator<const MachineFunction*, 01655 SmallPtrSet<const MachineBasicBlock*, 8> > 01656 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable); 01657 DFI != DFE; ++DFI) { 01658 const MachineBasicBlock *MBB = *DFI; 01659 01660 StackStateOfBB BBState; 01661 // Check the exit state of the DFS stack predecessor. 01662 if (DFI.getPathLength() >= 2) { 01663 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2); 01664 assert(Reachable.count(StackPred) && 01665 "DFS stack predecessor is already visited.\n"); 01666 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue; 01667 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup; 01668 BBState.ExitValue = BBState.EntryValue; 01669 BBState.ExitIsSetup = BBState.EntryIsSetup; 01670 } 01671 01672 // Update stack state by checking contents of MBB. 01673 for (const auto &I : *MBB) { 01674 if (I.getOpcode() == FrameSetupOpcode) { 01675 // The first operand of a FrameOpcode should be i32. 01676 int Size = I.getOperand(0).getImm(); 01677 assert(Size >= 0 && 01678 "Value should be non-negative in FrameSetup and FrameDestroy.\n"); 01679 01680 if (BBState.ExitIsSetup) 01681 report("FrameSetup is after another FrameSetup", &I); 01682 BBState.ExitValue -= Size; 01683 BBState.ExitIsSetup = true; 01684 } 01685 01686 if (I.getOpcode() == FrameDestroyOpcode) { 01687 // The first operand of a FrameOpcode should be i32. 01688 int Size = I.getOperand(0).getImm(); 01689 assert(Size >= 0 && 01690 "Value should be non-negative in FrameSetup and FrameDestroy.\n"); 01691 01692 if (!BBState.ExitIsSetup) 01693 report("FrameDestroy is not after a FrameSetup", &I); 01694 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue : 01695 BBState.ExitValue; 01696 if (BBState.ExitIsSetup && AbsSPAdj != Size) { 01697 report("FrameDestroy <n> is after FrameSetup <m>", &I); 01698 *OS << "FrameDestroy <" << Size << "> is after FrameSetup <" 01699 << AbsSPAdj << ">.\n"; 01700 } 01701 BBState.ExitValue += Size; 01702 BBState.ExitIsSetup = false; 01703 } 01704 } 01705 SPState[MBB->getNumber()] = BBState; 01706 01707 // Make sure the exit state of any predecessor is consistent with the entry 01708 // state. 01709 for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(), 01710 E = MBB->pred_end(); I != E; ++I) { 01711 if (Reachable.count(*I) && 01712 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue || 01713 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) { 01714 report("The exit stack state of a predecessor is inconsistent.", MBB); 01715 *OS << "Predecessor BB#" << (*I)->getNumber() << " has exit state (" 01716 << SPState[(*I)->getNumber()].ExitValue << ", " 01717 << SPState[(*I)->getNumber()].ExitIsSetup 01718 << "), while BB#" << MBB->getNumber() << " has entry state (" 01719 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n"; 01720 } 01721 } 01722 01723 // Make sure the entry state of any successor is consistent with the exit 01724 // state. 01725 for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), 01726 E = MBB->succ_end(); I != E; ++I) { 01727 if (Reachable.count(*I) && 01728 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue || 01729 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) { 01730 report("The entry stack state of a successor is inconsistent.", MBB); 01731 *OS << "Successor BB#" << (*I)->getNumber() << " has entry state (" 01732 << SPState[(*I)->getNumber()].EntryValue << ", " 01733 << SPState[(*I)->getNumber()].EntryIsSetup 01734 << "), while BB#" << MBB->getNumber() << " has exit state (" 01735 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n"; 01736 } 01737 } 01738 01739 // Make sure a basic block with return ends with zero stack adjustment. 01740 if (!MBB->empty() && MBB->back().isReturn()) { 01741 if (BBState.ExitIsSetup) 01742 report("A return block ends with a FrameSetup.", MBB); 01743 if (BBState.ExitValue) 01744 report("A return block ends with a nonzero stack adjustment.", MBB); 01745 } 01746 } 01747 }