52 struct MachineVerifier {
54 MachineVerifier(
Pass *
pass,
const char *b) :
57 OutFileName(
getenv(
"LLVM_VERIFY_MACHINEINSTRS"))
64 const char *
const OutFileName;
81 BlockSet FunctionBlocks;
85 RegVector regsDefined, regsDead, regsKilled;
86 RegMaskVector regMasks;
87 RegSet regsLiveInButUnused;
92 void addRegWithSubRegs(RegVector &RV,
unsigned Reg) {
96 RV.push_back(*SubRegs);
121 RegSet vregsRequired;
124 BlockSet Preds, Succs;
126 BBInfo() : reachable(
false) {}
130 bool addPassed(
unsigned Reg) {
133 if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
135 return vregsPassed.insert(Reg).second;
139 bool addPassed(
const RegSet &RS) {
140 bool changed =
false;
141 for (RegSet::const_iterator
I = RS.begin(), E = RS.end();
I != E; ++
I)
149 bool addRequired(
unsigned Reg) {
152 if (regsLiveOut.count(Reg))
154 return vregsRequired.insert(Reg).second;
158 bool addRequired(
const RegSet &RS) {
159 bool changed =
false;
160 for (RegSet::const_iterator
I = RS.begin(), E = RS.end();
I != E; ++
I)
167 bool addRequired(
const RegMap &
RM) {
168 bool changed =
false;
169 for (RegMap::const_iterator
I = RM.begin(), E = RM.end();
I != E; ++
I)
170 if (addRequired(
I->first))
176 bool isLiveOut(
unsigned Reg)
const {
177 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
184 bool isReserved(
unsigned Reg) {
185 return Reg < regsReserved.size() && regsReserved.test(Reg);
188 bool isAllocatable(
unsigned Reg) {
189 return Reg < TRI->getNumRegs() &&
MRI->isAllocatable(Reg);
198 void visitMachineFunctionBefore();
202 void visitMachineOperand(
const MachineOperand *MO,
unsigned MONum);
206 void visitMachineFunctionAfter();
211 void report(
const char *msg,
const MachineOperand *MO,
unsigned MONum);
225 void calcRegsPassed();
228 void calcRegsRequired();
229 void verifyLiveVariables();
230 void verifyLiveIntervals();
233 void verifyLiveRangeSegment(
const LiveRange&,
235 void verifyLiveRange(
const LiveRange&,
unsigned);
237 void verifyStackFrame();
242 const char *
const Banner;
244 MachineVerifierPass(
const char *b = 0)
264 "Verify generated machine code",
false,
false)
267 return new MachineVerifierPass(Banner);
271 MachineVerifier(p, Banner)
272 .runOnMachineFunction(const_cast<MachineFunction&>(*
this));
278 std::string ErrorInfo;
280 if (!ErrorInfo.empty()) {
281 errs() <<
"Error opening '" << OutFileName <<
"': " << ErrorInfo <<
'\n';
294 TII =
TM->getInstrInfo();
295 TRI =
TM->getRegisterInfo();
311 visitMachineFunctionBefore();
314 visitMachineBasicBlockBefore(MFI);
318 bool InBundle =
false;
321 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
322 if (MBBI->getParent() != MFI) {
323 report(
"Bad instruction parent pointer", MFI);
324 *OS <<
"Instruction: " << *MBBI;
329 if (InBundle && !MBBI->isBundledWithPred())
330 report(
"Missing BundledPred flag, "
331 "BundledSucc was set on predecessor", MBBI);
332 if (!InBundle && MBBI->isBundledWithPred())
333 report(
"BundledPred flag is set, "
334 "but BundledSucc not set on predecessor", MBBI);
337 if (!MBBI->isInsideBundle()) {
339 visitMachineBundleAfter(CurBundle);
341 visitMachineBundleBefore(CurBundle);
342 }
else if (!CurBundle)
343 report(
"No bundle header", MBBI);
344 visitMachineInstrBefore(MBBI);
345 for (
unsigned I = 0, E = MBBI->getNumOperands();
I != E; ++
I)
346 visitMachineOperand(&MBBI->getOperand(
I),
I);
347 visitMachineInstrAfter(MBBI);
350 InBundle = MBBI->isBundledWithSucc();
353 visitMachineBundleAfter(CurBundle);
355 report(
"BundledSucc flag set on last instruction in block", &MFI->back());
356 visitMachineBasicBlockAfter(MFI);
358 visitMachineFunctionAfter();
362 else if (foundErrors)
371 regsLiveInButUnused.clear();
377 void MachineVerifier::report(
const char *msg,
const MachineFunction *MF) {
380 if (!foundErrors++) {
382 *OS <<
"# " << Banner <<
'\n';
383 MF->
print(*OS, Indexes);
385 *OS <<
"*** Bad machine code: " << msg <<
" ***\n"
386 <<
"- function: " << MF->
getName() <<
"\n";
392 *OS <<
"- basic block: BB#" << MBB->
getNumber()
394 <<
" (" << (
const void*)MBB <<
')';
396 *OS <<
" [" << Indexes->getMBBStartIdx(MBB)
397 <<
';' << Indexes->getMBBEndIdx(MBB) <<
')';
401 void MachineVerifier::report(
const char *msg,
const MachineInstr *
MI) {
404 *OS <<
"- instruction: ";
405 if (Indexes && Indexes->hasIndex(MI))
406 *OS << Indexes->getInstructionIndex(MI) <<
'\t';
410 void MachineVerifier::report(
const char *msg,
414 *OS <<
"- operand " << MONum <<
": ";
419 void MachineVerifier::report(
const char *msg,
const MachineFunction *MF,
422 *OS <<
"- interval: " << LI <<
'\n';
428 *OS <<
"- interval: " << LI <<
'\n';
434 *OS <<
"- liverange: " << LR <<
"\n";
437 void MachineVerifier::report(
const char *msg,
const MachineFunction *MF,
440 *OS <<
"- liverange: " << LR <<
"\n";
444 BBInfo &MInfo = MBBInfoMap[MBB];
445 if (!MInfo.reachable) {
446 MInfo.reachable =
true;
448 SuE = MBB->
succ_end(); SuI != SuE; ++SuI)
453 void MachineVerifier::visitMachineFunctionBefore() {
455 regsReserved =
MRI->getReservedRegs();
458 for (
int Reg = regsReserved.find_first(); Reg>=0;
459 Reg = regsReserved.find_next(Reg)) {
463 regsReserved.set(*SubRegs);
467 markReachable(&MF->
front());
470 FunctionBlocks.clear();
473 FunctionBlocks.insert(
I);
474 BBInfo &MInfo = MBBInfoMap[
I];
476 MInfo.Preds.insert(
I->pred_begin(),
I->pred_end());
477 if (MInfo.Preds.size() !=
I->pred_size())
478 report(
"MBB has duplicate entries in its predecessor list.",
I);
480 MInfo.Succs.insert(
I->succ_begin(),
I->succ_end());
481 if (MInfo.Succs.size() !=
I->succ_size())
482 report(
"MBB has duplicate entries in its successor list.",
I);
486 MRI->verifyUseLists();
514 report(
"MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
523 if ((*I)->isLandingPad())
525 if (!FunctionBlocks.count(*
I))
526 report(
"MBB has successor that isn't part of the function.", MBB);
527 if (!MBBInfoMap[*
I].Preds.count(MBB)) {
528 report(
"Inconsistent CFG", MBB);
529 *OS <<
"MBB is not in the predecessor list of the successor BB#"
530 << (*I)->getNumber() <<
".\n";
537 if (!FunctionBlocks.count(*
I))
538 report(
"MBB has predecessor that isn't part of the function.", MBB);
539 if (!MBBInfoMap[*
I].Succs.count(MBB)) {
540 report(
"Inconsistent CFG", MBB);
541 *OS <<
"MBB is not in the successor list of the predecessor BB#"
542 << (*I)->getNumber() <<
".\n";
548 if (LandingPadSuccs.
size() > 1 &&
552 report(
"MBB has more than one landing pad successor", MBB);
565 if (MBBI == MF->
end()) {
574 report(
"MBB exits via unconditional fall-through but doesn't have "
575 "exactly one CFG successor!", MBB);
577 report(
"MBB exits via unconditional fall-through but its successor "
578 "differs from its CFG successor!", MBB);
582 report(
"MBB exits via unconditional fall-through but ends with a "
583 "barrier instruction!", MBB);
586 report(
"MBB exits via unconditional fall-through but has a condition!",
589 }
else if (TBB && !FBB && Cond.
empty()) {
592 report(
"MBB exits via unconditional branch but doesn't have "
593 "exactly one CFG successor!", MBB);
595 report(
"MBB exits via unconditional branch but the CFG "
596 "successor doesn't match the actual successor!", MBB);
599 report(
"MBB exits via unconditional branch but doesn't contain "
600 "any instructions!", MBB);
602 report(
"MBB exits via unconditional branch but doesn't end with a "
603 "barrier instruction!", MBB);
605 report(
"MBB exits via unconditional branch but the branch isn't a "
606 "terminator instruction!", MBB);
608 }
else if (TBB && !FBB && !Cond.
empty()) {
612 if (MBBI == MF->
end()) {
613 report(
"MBB conditionally falls through out of function!", MBB);
617 report(
"MBB exits via conditional branch/fall-through but only has "
618 "one CFG successor!", MBB);
620 report(
"MBB exits via conditional branch/fall-through but the CFG "
621 "successor don't match the actual successor!", MBB);
623 report(
"MBB exits via conditional branch/fall-through but doesn't have "
624 "exactly two CFG successors!", MBB);
626 report(
"MBB exits via conditional branch/fall-through but the CFG "
627 "successors don't match the actual successors!", MBB);
630 report(
"MBB exits via conditional branch/fall-through but doesn't "
631 "contain any instructions!", MBB);
633 report(
"MBB exits via conditional branch/fall-through but ends with a "
634 "barrier instruction!", MBB);
636 report(
"MBB exits via conditional branch/fall-through but the branch "
637 "isn't a terminator instruction!", MBB);
639 }
else if (TBB && FBB) {
645 report(
"MBB exits via conditional branch/branch through but only has "
646 "one CFG successor!", MBB);
648 report(
"MBB exits via conditional branch/branch through but the CFG "
649 "successor don't match the actual successor!", MBB);
651 report(
"MBB exits via conditional branch/branch but doesn't have "
652 "exactly two CFG successors!", MBB);
654 report(
"MBB exits via conditional branch/branch but the CFG "
655 "successors don't match the actual successors!", MBB);
658 report(
"MBB exits via conditional branch/branch but doesn't "
659 "contain any instructions!", MBB);
661 report(
"MBB exits via conditional branch/branch but doesn't end with a "
662 "barrier instruction!", MBB);
664 report(
"MBB exits via conditional branch/branch but the branch "
665 "isn't a terminator instruction!", MBB);
668 report(
"MBB exits via conditinal branch/branch but there's no "
672 report(
"AnalyzeBranch returned invalid data!", MBB);
680 report(
"MBB live-in list contains non-physical register", MBB);
685 regsLive.insert(*SubRegs);
687 regsLiveInButUnused = regsLive;
690 assert(MFI &&
"Function has no frame info");
695 regsLive.insert(*SubRegs);
702 lastIndex = Indexes->getMBBStartIdx(MBB);
707 void MachineVerifier::visitMachineBundleBefore(
const MachineInstr *MI) {
708 if (Indexes && Indexes->hasIndex(MI)) {
709 SlotIndex idx = Indexes->getInstructionIndex(MI);
710 if (!(idx > lastIndex)) {
711 report(
"Instruction index out of order", MI);
712 *OS <<
"Last instruction was at " << lastIndex <<
'\n';
721 if (!FirstTerminator)
722 FirstTerminator =
MI;
723 }
else if (FirstTerminator) {
724 report(
"Non-terminator instruction after the first terminator", MI);
725 *OS <<
"First terminator was:\t" << *FirstTerminator;
731 void MachineVerifier::verifyInlineAsm(
const MachineInstr *MI) {
734 report(
"Too few operands on inline asm", MI);
738 report(
"Asm string must be an external symbol", MI);
740 report(
"Asm flags must be an immediate", MI);
744 report(
"Unknown asm flags", &MI->
getOperand(1), 1);
746 assert(InlineAsm::MIOp_FirstOperand == 2 &&
"Asm format changed");
748 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
750 for (
unsigned e = MI->
getNumOperands(); OpNo < e; OpNo += NumOps) {
759 report(
"Missing operands in last group", MI);
769 report(
"Expected implicit register after groups", &MO, OpNo);
773 void MachineVerifier::visitMachineInstrBefore(
const MachineInstr *MI) {
776 report(
"Too few operands", MI);
788 if ((*I)->isLoad() && !MI->
mayLoad())
789 report(
"Missing mayLoad flag", MI);
790 if ((*I)->isStore() && !MI->
mayStore())
791 report(
"Missing mayStore flag", MI);
797 bool mapped = !LiveInts->isNotInMIMap(MI);
800 report(
"Debug instruction has a slot index", MI);
803 report(
"Instruction inside bundle has a slot index", MI);
806 report(
"Missing slot index", MI);
811 if (!
TII->verifyInstruction(MI, ErrorInfo))
812 report(ErrorInfo.
data(),
MI);
816 MachineVerifier::visitMachineOperand(
const MachineOperand *MO,
unsigned MONum) {
824 report(
"Explicit definition must be a register", MO, MONum);
826 report(
"Explicit definition marked as use", MO, MONum);
828 report(
"Explicit definition marked as implicit", MO, MONum);
836 report(
"Explicit operand marked as def", MO, MONum);
838 report(
"Explicit operand marked as implicit", MO, MONum);
844 report(
"Tied use must be a register", MO, MONum);
846 report(
"Operand should be tied", MO, MONum);
848 report(
"Tied def doesn't match MCInstrDesc", MO, MONum);
850 report(
"Explicit operand should not be tied", MO, MONum);
854 report(
"Extra explicit operand on non-variadic instruction", MO, MONum);
859 const unsigned Reg = MO->
getReg();
863 checkLiveness(MO, MONum);
869 if (!OtherMO.
isReg())
870 report(
"Must be tied to a register", MO, MONum);
872 report(
"Missing tie flags on tied operand", MO, MONum);
874 report(
"Inconsistent tie links", MO, MONum);
878 report(
"Explicit def tied to explicit use without tie constraint",
882 report(
"Explicit def should be tied to implicit use", MO, MONum);
892 report(
"Two-address instruction operands must be identical", MO, MONum);
900 report(
"Illegal subregister index for physical register", MO, MONum);
904 TII->getRegClass(MCID, MONum, TRI, *MF)) {
905 if (!DRC->contains(Reg)) {
906 report(
"Illegal physical register for instruction", MO, MONum);
907 *OS << TRI->getName(Reg) <<
" is not a "
908 << DRC->getName() <<
" register.\n";
916 TRI->getSubClassWithSubReg(RC, SubIdx);
918 report(
"Invalid subregister index for virtual register", MO, MONum);
919 *OS <<
"Register class " << RC->
getName()
920 <<
" does not support subreg index " << SubIdx <<
"\n";
924 report(
"Invalid register class for subregister index", MO, MONum);
925 *OS <<
"Register class " << RC->
getName()
926 <<
" does not fully support subreg index " << SubIdx <<
"\n";
931 TII->getRegClass(MCID, MONum, TRI, *MF)) {
934 TRI->getLargestLegalSuperClass(RC);
936 report(
"No largest legal super class exists.", MO, MONum);
939 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
941 report(
"No matching super-reg register class.", MO, MONum);
946 report(
"Illegal virtual register for instruction", MO, MONum);
947 *OS <<
"Expected a " << DRC->getName() <<
" register, but got a "
948 << RC->
getName() <<
" register\n";
962 report(
"PHI operand is not in the CFG", MO, MONum);
966 if (LiveStks && LiveStks->hasInterval(MO->
getIndex()) &&
967 LiveInts && !LiveInts->isNotInMIMap(MI)) {
969 SlotIndex Idx = LiveInts->getInstructionIndex(MI);
971 report(
"Instruction loads from dead spill slot", MO, MONum);
972 *OS <<
"Live stack: " << LI <<
'\n';
975 report(
"Instruction stores to dead spill slot", MO, MONum);
976 *OS <<
"Live stack: " << LI <<
'\n';
986 void MachineVerifier::checkLiveness(
const MachineOperand *MO,
unsigned MONum) {
988 const unsigned Reg = MO->
getReg();
992 regsLiveInButUnused.erase(Reg);
995 addRegWithSubRegs(regsKilled, Reg);
1002 report(
"Kill missing from LiveVariables", MO, MONum);
1006 if (LiveInts && !LiveInts->isNotInMIMap(MI)) {
1007 SlotIndex UseIdx = LiveInts->getInstructionIndex(MI);
1011 if (
const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) {
1014 report(
"No live segment at use", MO, MONum);
1015 *OS << UseIdx <<
" is not live in " <<
PrintRegUnit(*Units, TRI)
1016 <<
' ' << *LR <<
'\n';
1019 report(
"Live range continues after kill flag", MO, MONum);
1020 *OS <<
PrintRegUnit(*Units, TRI) <<
' ' << *LR <<
'\n';
1027 if (LiveInts->hasInterval(Reg)) {
1032 report(
"No live segment at use", MO, MONum);
1033 *OS << UseIdx <<
" is not live in " << LI <<
'\n';
1038 report(
"Live range continues after kill flag", MO, MONum);
1039 *OS <<
"Live range: " << LI <<
'\n';
1042 report(
"Virtual register has no live interval", MO, MONum);
1048 if (!regsLive.count(Reg)) {
1051 if (!isReserved(Reg))
1052 report(
"Using an undefined physical register", MO, MONum);
1053 }
else if (
MRI->def_empty(Reg)) {
1054 report(
"Reading virtual register without a def", MO, MONum);
1056 BBInfo &MInfo = MBBInfoMap[MI->
getParent()];
1060 if (MInfo.regsKilled.count(Reg))
1061 report(
"Using a killed virtual register", MO, MONum);
1062 else if (!MI->
isPHI())
1063 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1072 addRegWithSubRegs(regsDead, Reg);
1074 addRegWithSubRegs(regsDefined, Reg);
1079 report(
"Multiple virtual register defs in SSA form", MO, MONum);
1083 !LiveInts->isNotInMIMap(MI)) {
1084 SlotIndex DefIdx = LiveInts->getInstructionIndex(MI);
1086 if (LiveInts->hasInterval(Reg)) {
1089 assert(VNI &&
"NULL valno is not allowed");
1090 if (VNI->def != DefIdx) {
1091 report(
"Inconsistent valno->def", MO, MONum);
1092 *OS <<
"Valno " << VNI->id <<
" is not defined at "
1093 << DefIdx <<
" in " << LI <<
'\n';
1096 report(
"No live segment at def", MO, MONum);
1097 *OS << DefIdx <<
" is not live in " << LI <<
'\n';
1103 report(
"Live range continues after dead def flag", MO, MONum);
1104 *OS <<
"Live range: " << LI <<
'\n';
1108 report(
"Virtual register has no Live interval", MO, MONum);
1114 void MachineVerifier::visitMachineInstrAfter(
const MachineInstr *MI) {
1121 void MachineVerifier::visitMachineBundleAfter(
const MachineInstr *MI) {
1122 BBInfo &MInfo = MBBInfoMap[MI->
getParent()];
1123 set_union(MInfo.regsKilled, regsKilled);
1124 set_subtract(regsLive, regsKilled); regsKilled.clear();
1126 while (!regMasks.empty()) {
1127 const uint32_t *Mask = regMasks.pop_back_val();
1128 for (RegSet::iterator
I = regsLive.begin(), E = regsLive.end();
I != E; ++
I)
1131 regsDead.push_back(*
I);
1134 set_union(regsLive, regsDefined); regsDefined.clear();
1139 MBBInfoMap[MBB].regsLiveOut = regsLive;
1143 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1144 if (!(stop > lastIndex)) {
1145 report(
"Block ends before last instruction index", MBB);
1146 *OS <<
"Block ends at " << stop
1147 <<
" last instruction was at " << lastIndex <<
'\n';
1156 void MachineVerifier::calcRegsPassed() {
1161 MFI != MFE; ++MFI) {
1163 BBInfo &MInfo = MBBInfoMap[&MBB];
1164 if (!MInfo.reachable)
1167 SuE = MBB.
succ_end(); SuI != SuE; ++SuI) {
1168 BBInfo &SInfo = MBBInfoMap[*SuI];
1169 if (SInfo.addPassed(MInfo.regsLiveOut))
1176 while (!todo.
empty()) {
1179 BBInfo &MInfo = MBBInfoMap[MBB];
1181 SuE = MBB->
succ_end(); SuI != SuE; ++SuI) {
1184 BBInfo &SInfo = MBBInfoMap[*SuI];
1185 if (SInfo.addPassed(MInfo.vregsPassed))
1194 void MachineVerifier::calcRegsRequired() {
1198 MFI != MFE; ++MFI) {
1200 BBInfo &MInfo = MBBInfoMap[&MBB];
1202 PrE = MBB.
pred_end(); PrI != PrE; ++PrI) {
1203 BBInfo &PInfo = MBBInfoMap[*PrI];
1204 if (PInfo.addRequired(MInfo.vregsLiveIn))
1211 while (!todo.
empty()) {
1214 BBInfo &MInfo = MBBInfoMap[MBB];
1216 PrE = MBB->
pred_end(); PrI != PrE; ++PrI) {
1219 BBInfo &SInfo = MBBInfoMap[*PrI];
1220 if (SInfo.addRequired(MInfo.vregsRequired))
1231 BBI != BBE && BBI->isPHI(); ++BBI) {
1234 for (
unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
1235 unsigned Reg = BBI->getOperand(i).getReg();
1240 BBInfo &PrInfo = MBBInfoMap[Pre];
1241 if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1242 report(
"PHI operand is not live-out from predecessor",
1243 &BBI->getOperand(i), i);
1248 PrE = MBB->
pred_end(); PrI != PrE; ++PrI) {
1249 if (!seen.
count(*PrI)) {
1250 report(
"Missing PHI operand", BBI);
1251 *OS <<
"BB#" << (*PrI)->getNumber()
1252 <<
" is a predecessor according to the CFG.\n";
1258 void MachineVerifier::visitMachineFunctionAfter() {
1262 MFI != MFE; ++MFI) {
1263 BBInfo &MInfo = MBBInfoMap[MFI];
1266 if (!MInfo.reachable)
1277 MFI != MFE; ++MFI) {
1278 BBInfo &MInfo = MBBInfoMap[MFI];
1279 for (RegSet::iterator
1280 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end();
I != E;
1282 if (MInfo.regsKilled.count(*
I)) {
1283 report(
"Virtual register killed in block, but needed live out.", MFI);
1284 *OS <<
"Virtual register " <<
PrintReg(*
I)
1285 <<
" is used after the block.\n";
1290 BBInfo &MInfo = MBBInfoMap[&MF->
front()];
1291 for (RegSet::iterator
1292 I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end();
I != E;
1294 report(
"Virtual register def doesn't dominate all uses.",
1295 MRI->getVRegDef(*
I));
1299 verifyLiveVariables();
1301 verifyLiveIntervals();
1304 void MachineVerifier::verifyLiveVariables() {
1305 assert(LiveVars &&
"Don't call verifyLiveVariables without LiveVars");
1306 for (
unsigned i = 0, e =
MRI->getNumVirtRegs(); i != e; ++i) {
1310 MFI != MFE; ++MFI) {
1311 BBInfo &MInfo = MBBInfoMap[MFI];
1314 if (MInfo.vregsRequired.count(Reg)) {
1316 report(
"LiveVariables: Block missing from AliveBlocks", MFI);
1317 *OS <<
"Virtual register " <<
PrintReg(Reg)
1318 <<
" must be live through the block.\n";
1322 report(
"LiveVariables: Block should not be in AliveBlocks", MFI);
1323 *OS <<
"Virtual register " <<
PrintReg(Reg)
1324 <<
" is not needed live through the block.\n";
1331 void MachineVerifier::verifyLiveIntervals() {
1332 assert(LiveInts &&
"Don't call verifyLiveIntervals without LiveInts");
1333 for (
unsigned i = 0, e =
MRI->getNumVirtRegs(); i != e; ++i) {
1337 if (
MRI->reg_nodbg_empty(Reg))
1340 if (!LiveInts->hasInterval(Reg)) {
1341 report(
"Missing live interval for virtual register", MF);
1342 *OS <<
PrintReg(Reg, TRI) <<
" still has defs or uses\n";
1347 assert(Reg == LI.
reg &&
"Invalid reg to interval mapping");
1348 verifyLiveInterval(LI);
1352 for (
unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1353 if (
const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1354 verifyLiveRange(*LR, i);
1357 void MachineVerifier::verifyLiveRangeValue(
const LiveRange &LR,
1366 report(
"Valno not live at def and not marked unused", MF, LR);
1367 *OS <<
"Valno #" << VNI->
id <<
'\n';
1371 if (DefVNI != VNI) {
1372 report(
"Live segment at def has different valno", MF, LR);
1373 *OS <<
"Valno #" << VNI->
id <<
" is defined at " << VNI->
def
1374 <<
" where valno #" << DefVNI->
id <<
" is live\n";
1380 report(
"Invalid definition index", MF, LR);
1381 *OS <<
"Valno #" << VNI->
id <<
" is defined at " << VNI->
def
1382 <<
" in " << LR <<
'\n';
1387 if (VNI->
def != LiveInts->getMBBStartIdx(MBB)) {
1388 report(
"PHIDef value is not defined at MBB start", MBB, LR);
1389 *OS <<
"Valno #" << VNI->
id <<
" is defined at " << VNI->
def
1390 <<
", not at the beginning of BB#" << MBB->
getNumber() <<
'\n';
1398 report(
"No instruction at def index", MBB, LR);
1399 *OS <<
"Valno #" << VNI->
id <<
" is defined at " << VNI->
def <<
'\n';
1404 bool hasDef =
false;
1405 bool isEarlyClobber =
false;
1407 if (!MOI->isReg() || !MOI->isDef())
1410 if (MOI->getReg() !=
Reg)
1414 !TRI->hasRegUnit(MOI->getReg(),
Reg))
1418 if (MOI->isEarlyClobber())
1419 isEarlyClobber =
true;
1423 report(
"Defining instruction does not modify register", MI);
1424 *OS <<
"Valno #" << VNI->
id <<
" in " << LR <<
'\n';
1429 if (isEarlyClobber) {
1431 report(
"Early clobber def must be at an early-clobber slot", MBB, LR);
1432 *OS <<
"Valno #" << VNI->
id <<
" is defined at " << VNI->
def <<
'\n';
1435 report(
"Non-PHI, non-early clobber def must be at a register slot",
1437 *OS <<
"Valno #" << VNI->
id <<
" is defined at " << VNI->
def <<
'\n';
1442 void MachineVerifier::verifyLiveRangeSegment(
const LiveRange &LR,
1447 assert(VNI &&
"Live segment has no valno");
1450 report(
"Foreign valno in live segment", MF, LR);
1451 *OS << S <<
" has a bad valno\n";
1455 report(
"Live segment valno is marked unused", MF, LR);
1461 report(
"Bad start of live segment, no basic block", MF, LR);
1465 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1467 report(
"Live segment must begin at MBB entry or valno def", MBB, LR);
1474 report(
"Bad end of live segment, no basic block", MF, LR);
1480 if (S.
end == LiveInts->getMBBEndIdx(EndMBB))
1492 report(
"Live segment doesn't end at a valid instruction", EndMBB, LR);
1499 report(
"Live segment ends at B slot of an instruction", EndMBB, LR);
1507 report(
"Live segment ending at dead slot spans instructions", EndMBB, LR);
1515 if (I+1 == LR.
end() || (I+1)->start != S.
end) {
1516 report(
"Live segment ending at early clobber slot must be "
1517 "redefined by an EC def in the same instruction", EndMBB, LR);
1527 bool hasRead =
false;
1529 if (!MOI->isReg() || MOI->getReg() !=
Reg)
1531 if (MOI->readsReg())
1536 report(
"Instruction ending live segment doesn't read the register", MI);
1537 *OS << S <<
" in " << LR <<
'\n';
1553 assert(LiveInts->isLiveInToMBB(LR, MFI));
1556 MFI->isLandingPad()) {
1557 if (&*MFI == EndMBB)
1565 VNI->
def == LiveInts->getMBBStartIdx(MFI);
1569 PE = MFI->pred_end(); PI != PE; ++PI) {
1570 SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1575 report(
"Register not marked live out of predecessor", *PI, LR);
1576 *OS <<
"Valno #" << VNI->
id <<
" live into BB#" << MFI->getNumber()
1577 <<
'@' << LiveInts->getMBBStartIdx(MFI) <<
", not live before "
1583 if (!IsPHI && PVNI != VNI) {
1584 report(
"Different value live out of predecessor", *PI, LR);
1585 *OS <<
"Valno #" << PVNI->
id <<
" live out of BB#"
1586 << (*PI)->getNumber() <<
'@' << PEnd
1587 <<
"\nValno #" << VNI->
id <<
" live into BB#" << MFI->getNumber()
1588 <<
'@' << LiveInts->getMBBStartIdx(MFI) <<
'\n';
1591 if (&*MFI == EndMBB)
1597 void MachineVerifier::verifyLiveRange(
const LiveRange &LR,
unsigned Reg) {
1600 verifyLiveRangeValue(LR, *I, Reg);
1603 verifyLiveRangeSegment(LR, I, Reg);
1606 void MachineVerifier::verifyLiveInterval(
const LiveInterval &LI) {
1607 verifyLiveRange(LI, LI.
reg);
1612 unsigned NumComp = ConEQ.Classify(&LI);
1614 report(
"Multiple connected components in live interval", MF, LI);
1615 for (
unsigned comp = 0; comp != NumComp; ++comp) {
1616 *OS << comp <<
": valnos";
1619 if (comp == ConEQ.getEqClass(*I))
1620 *OS <<
' ' << (*I)->id;
1632 struct StackStateOfBB {
1633 StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(
false),
1634 ExitIsSetup(
false) { }
1635 StackStateOfBB(
int EntryVal,
int ExitVal,
bool EntrySetup,
bool ExitSetup) :
1636 EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1637 ExitIsSetup(ExitSetup) { }
1649 void MachineVerifier::verifyStackFrame() {
1650 int FrameSetupOpcode =
TII->getCallFrameSetupOpcode();
1651 int FrameDestroyOpcode =
TII->getCallFrameDestroyOpcode();
1661 DFI != DFE; ++DFI) {
1664 StackStateOfBB BBState;
1666 if (DFI.getPathLength() >= 2) {
1668 assert(Reachable.
count(StackPred) &&
1669 "DFS stack predecessor is already visited.\n");
1670 BBState.EntryValue = SPState[StackPred->
getNumber()].ExitValue;
1671 BBState.EntryIsSetup = SPState[StackPred->
getNumber()].ExitIsSetup;
1672 BBState.ExitValue = BBState.EntryValue;
1673 BBState.ExitIsSetup = BBState.EntryIsSetup;
1679 if (I->getOpcode() == FrameSetupOpcode) {
1681 int Size = I->getOperand(0).getImm();
1683 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1685 if (BBState.ExitIsSetup)
1686 report(
"FrameSetup is after another FrameSetup", I);
1687 BBState.ExitValue -= Size;
1688 BBState.ExitIsSetup =
true;
1691 if (I->getOpcode() == FrameDestroyOpcode) {
1693 int Size = I->getOperand(0).getImm();
1695 "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1697 if (!BBState.ExitIsSetup)
1698 report(
"FrameDestroy is not after a FrameSetup", I);
1699 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1701 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
1702 report(
"FrameDestroy <n> is after FrameSetup <m>", I);
1703 *OS <<
"FrameDestroy <" << Size <<
"> is after FrameSetup <"
1704 << AbsSPAdj <<
">.\n";
1706 BBState.ExitValue += Size;
1707 BBState.ExitIsSetup =
false;
1716 if (Reachable.
count(*I) &&
1717 (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
1718 SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
1719 report(
"The exit stack state of a predecessor is inconsistent.", MBB);
1720 *OS <<
"Predecessor BB#" << (*I)->getNumber() <<
" has exit state ("
1721 << SPState[(*I)->getNumber()].ExitValue <<
", "
1722 << SPState[(*I)->getNumber()].ExitIsSetup
1723 <<
"), while BB#" << MBB->
getNumber() <<
" has entry state ("
1724 << BBState.EntryValue <<
", " << BBState.EntryIsSetup <<
").\n";
1732 if (Reachable.
count(*I) &&
1733 (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
1734 SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
1735 report(
"The entry stack state of a successor is inconsistent.", MBB);
1736 *OS <<
"Successor BB#" << (*I)->getNumber() <<
" has entry state ("
1737 << SPState[(*I)->getNumber()].EntryValue <<
", "
1738 << SPState[(*I)->getNumber()].EntryIsSetup
1739 <<
"), while BB#" << MBB->
getNumber() <<
" has exit state ("
1740 << BBState.ExitValue <<
", " << BBState.ExitIsSetup <<
").\n";
1746 if (BBState.ExitIsSetup)
1747 report(
"A return block ends with a FrameSetup.", MBB);
1748 if (BBState.ExitValue)
1749 report(
"A return block ends with a nonzero stack adjustment.", MBB);
bool isInsideBundle() const
unsigned succ_size() const
const MachineFunction * getParent() const
bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx=0) const
MachineInstr * getParent()
static PassRegistry * getPassRegistry()
SlotIndex def
The index of the defining instruction.
virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
MachineBasicBlock * getMBB() const
static unsigned index2VirtReg(unsigned Index)
void verify(Pass *p=NULL, const char *Banner=NULL) const
void print(raw_ostream &OS, SlotIndexes *=0) const
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions. Register definitions always occur...
int find_next(unsigned Prev) const
std::vector< unsigned >::const_iterator livein_iterator
bool mayStore(QueryType Type=AnyInBundle) const
static bool isVirtualRegister(unsigned Reg)
static bool matchPair(MachineBasicBlock::const_succ_iterator i, const MachineBasicBlock *a, const MachineBasicBlock *b)
const MCInstrDesc & getDesc() const
ExceptionHandling::ExceptionsType getExceptionHandlingType() const
bool hasSuperClassEq(const TargetRegisterClass *RC) const
virtual bool isPredicated(const MachineInstr *MI) const
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
unsigned getNumValNums() const
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
bool isTerminator(QueryType Type=AnyInBundle) const
bool isEarlyClobber() const
isEarlyClobber - Returns true if this is an early-clobber slot.
LoopInfoBase< BlockT, LoopT > * LI
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
void initializeMachineVerifierPassPass(PassRegistry &)
livein_iterator livein_begin() const
void clear()
clear - Clear all bits.
unsigned getNumBlockIDs() const
const HexagonInstrInfo * TII
const char * getName() const
AnalysisType * getAnalysisIfAvailable() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool mayLoad(QueryType Type=AnyInBundle) const
Abstract Stack Frame Information.
bool isBlock() const
isBlock - Returns true if this is a block boundary slot.
ID
LLVM Calling Convention Representation.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
SparseBitVector AliveBlocks
unsigned getNumOperands() const
bool isUnused() const
Returns true if this value is unused.
const MachineBasicBlock & front() const
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
const char * data() const
VNInfoList::const_iterator const_vni_iterator
INITIALIZE_PASS(MachineVerifierPass,"machineverifier","Verify generated machine code", false, false) FunctionPass *llvm
const BasicBlock * getBasicBlock() const
SlotIndex getPrevSlot() const
const MachineBasicBlock * getParent() const
bool isDebugValue() const
mmo_iterator memoperands_end() const
bool isDeadDef() const
Return true if this instruction has a dead def.
bool isEarlyClobber() const
unsigned findTiedOperandIdx(unsigned OpIdx) const
bool isReturn(QueryType Type=AnyInBundle) const
LiveQueryResult Query(SlotIndex Idx) const
LLVM Basic Block Representation.
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
bool isOptionalDef() const
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value. See class MCOperandInfo.
livein_iterator livein_end() const
const MachineOperand & getOperand(unsigned i) const
Abstract Stack Frame Index.
static unsigned getNumOperandRegisters(unsigned Flag)
Two Address instruction pass
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
ItTy next(ItTy it, Dist n)
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isVariadic(QueryType Type=IgnoreBundle) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
succ_iterator succ_begin()
unsigned getSubReg() const
pred_iterator pred_begin()
bool liveAt(SlotIndex index) const
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specific constraint if it is set. Returns -1 if it is not set...
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
BitVector getPristineRegs(const MachineBasicBlock *MBB) const
std::vector< MachineInstr * > Kills
unsigned id
The ID number of this value.
const uint32_t * getRegMask() const
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
FunctionPass * createMachineVerifierPass(const char *Banner=0)
bool isSuccessor(const MachineBasicBlock *MBB) const
MachineFrameInfo * getFrameInfo()
void print(raw_ostream &OS, const TargetMachine *TM=0, bool SkipOpers=false) const
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
StringRef getName() const
MachineInstr * getBundleStart(MachineInstr *MI)
VNInfo * getValNumInfo(unsigned ValNo)
void set_subtract(S1Ty &S1, const S2Ty &S2)
MachineOperandType getType() const
bundle_iterator< const MachineInstr, const_instr_iterator > const_iterator
static bool isPhysicalRegister(unsigned Reg)
bool isLandingPad() const
MachineRegisterInfo & getRegInfo()
virtual void getAnalysisUsage(AnalysisUsage &AU) const
void print(raw_ostream &os, const TargetMachine *TM=0) const
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
const TargetMachine & getTarget() const
Mask of preserved registers.
SlotIndex getRegSlot(bool EC=false) const
unsigned getReg() const
getReg - Returns the register number.
bool isValid() const
isValid - Returns true until all the operands have been visited.
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction. Note that variadic (isVari...
const MCOperandInfo * OpInfo
const MCRegisterInfo & MRI
StringRef getName() const
SlotIndex - An opaque wrapper around machine indexes.
char *getenv(const char *name);
bool set_union(S1Ty &S1, const S2Ty &S2)
MachineBasicBlock reference.
VNInfo * getVNInfoBefore(SlotIndex Idx) const
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.