Index: llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -86,6 +86,10 @@ #define DEBUG_TYPE "dwarfdebug" STATISTIC(NumCSParams, "Number of dbg call site params created"); +STATISTIC(XXX_ExcludedRanges, "XXX Num DBG_VALUE ranges outside of the var scope"); +STATISTIC(XXX_ExcludeChecks, "XXX Range broken, exit checks"); + +static cl::opt DisableThing("disable-thing", cl::init(false)); static cl::opt DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, @@ -1623,6 +1627,69 @@ // open ranges and check if its location is valid for a single value // location. if (EI->isDbgValue()) { + // Return true if the var loc range being evaluated lives entirely outside + // its scope. + auto RangeExistsOutsideScope = [&]() -> bool { + XXX_ExcludeChecks++; + auto MBB = Instr->getParent(); + auto DL = Instr->getDebugLoc(); + LexicalScope *InstrScope = LScopes.findLexicalScope(DL); + // Scope doesn't exist? + if (!InstrScope) + return false; + + DbgValueHistoryMap::EntryIndex EndIndex = EI->getEndIndex(); + if (EndIndex == DbgValueHistoryMap::NoEntry) + // FIXME: I didn't want to think too hard about what to do here. + return false; + const DbgValueHistoryMap::Entry& End = Entries[EndIndex]; + auto *EndInstr = End.getInstr(); + // Make sure we're only looking in the same BB. + if (EndInstr->getParent() != Instr->getParent()) + return false; + + // FIXME: Is this necessary? + auto &LSRange = InstrScope->getRanges(); + if (LSRange.size() == 0) + return false; + + // Scan forwards; return false if we find any instruction which lives in + // a scope dominated by our DBG_VALUE's scope. + MachineBasicBlock::const_iterator Next(Instr); + MachineBasicBlock::const_iterator RangeEndInstr(EndInstr); + for (++Next; Next != MBB->end(); ++Next) { + auto NextDL = Next->getDebugLoc(); + if (!NextDL) + // We don't know anything about this instr, let's just bail. + return false; + LexicalScope *NextScope = LScopes.findLexicalScope(NextDL); + if (!NextScope) + // We don't know anything about this instr, let's just bail. + return false; + // scope(Next) is child of scope(Instr) so do not omit this range. + if (InstrScope->dominates(NextScope)) + // FIXME: This is too restrictive. If the range end is a DBG_VALUE + // in a scope dominated by Instr we'll exit here. Maybe we need + // a check like the one below (Next==RangeEndInstr) at the top + // of this loop? + // if(!End.isClobber() && Next==RangeEndInstr) + // return true; + return false; + if (Next == RangeEndInstr) { + XXX_ExcludedRanges++; + // We've got all the way to the range end without touching a + // dominated scope. + return true; + } + } + + XXX_ExcludedRanges++; + return true; + }; + + if (!DisableThing && RangeExistsOutsideScope()) + continue; + // Do not add undef debug values, as they are redundant information in // the location list entries. An undef debug results in an empty location // description. If there are any non-undef fragments then padding pieces @@ -1654,7 +1721,7 @@ LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n"); continue; } - + SmallVector Values; for (auto &R : OpenRanges) Values.push_back(R.second);