Statistics.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. //===-- Statistics.cpp - Debug Info quality metrics -----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm-dwarfdump.h"
  9. #include "llvm/ADT/DenseMap.h"
  10. #include "llvm/ADT/StringSet.h"
  11. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
  13. #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
  14. #include "llvm/Object/ObjectFile.h"
  15. #include "llvm/Support/JSON.h"
  16. #define DEBUG_TYPE "dwarfdump"
  17. using namespace llvm;
  18. using namespace llvm::dwarfdump;
  19. using namespace llvm::object;
  20. namespace {
  21. /// This represents the number of categories of debug location coverage being
  22. /// calculated. The first category is the number of variables with 0% location
  23. /// coverage, but the last category is the number of variables with 100%
  24. /// location coverage.
  25. constexpr int NumOfCoverageCategories = 12;
  26. /// This is used for zero location coverage bucket.
  27. constexpr unsigned ZeroCoverageBucket = 0;
  28. /// The UINT64_MAX is used as an indication of the overflow.
  29. constexpr uint64_t OverflowValue = std::numeric_limits<uint64_t>::max();
  30. /// This represents variables DIE offsets.
  31. using AbstractOriginVarsTy = llvm::SmallVector<uint64_t>;
  32. /// This maps function DIE offset to its variables.
  33. using AbstractOriginVarsTyMap = llvm::DenseMap<uint64_t, AbstractOriginVarsTy>;
  34. /// This represents function DIE offsets containing an abstract_origin.
  35. using FunctionsWithAbstractOriginTy = llvm::SmallVector<uint64_t>;
  36. /// This represents a data type for the stats and it helps us to
  37. /// detect an overflow.
  38. /// NOTE: This can be implemented as a template if there is an another type
  39. /// needing this.
  40. struct SaturatingUINT64 {
  41. /// Number that represents the stats.
  42. uint64_t Value;
  43. SaturatingUINT64(uint64_t Value_) : Value(Value_) {}
  44. void operator++(int) { return *this += 1; }
  45. void operator+=(uint64_t Value_) {
  46. if (Value != OverflowValue) {
  47. if (Value < OverflowValue - Value_)
  48. Value += Value_;
  49. else
  50. Value = OverflowValue;
  51. }
  52. }
  53. };
  54. /// Utility struct to store the full location of a DIE - its CU and offset.
  55. struct DIELocation {
  56. DWARFUnit *DwUnit;
  57. uint64_t DIEOffset;
  58. DIELocation(DWARFUnit *_DwUnit, uint64_t _DIEOffset)
  59. : DwUnit(_DwUnit), DIEOffset(_DIEOffset) {}
  60. };
  61. /// This represents DWARF locations of CrossCU referencing DIEs.
  62. using CrossCUReferencingDIELocationTy = llvm::SmallVector<DIELocation>;
  63. /// This maps function DIE offset to its DWARF CU.
  64. using FunctionDIECUTyMap = llvm::DenseMap<uint64_t, DWARFUnit *>;
  65. /// Holds statistics for one function (or other entity that has a PC range and
  66. /// contains variables, such as a compile unit).
  67. struct PerFunctionStats {
  68. /// Number of inlined instances of this function.
  69. uint64_t NumFnInlined = 0;
  70. /// Number of out-of-line instances of this function.
  71. uint64_t NumFnOutOfLine = 0;
  72. /// Number of inlined instances that have abstract origins.
  73. uint64_t NumAbstractOrigins = 0;
  74. /// Number of variables and parameters with location across all inlined
  75. /// instances.
  76. uint64_t TotalVarWithLoc = 0;
  77. /// Number of constants with location across all inlined instances.
  78. uint64_t ConstantMembers = 0;
  79. /// Number of arificial variables, parameters or members across all instances.
  80. uint64_t NumArtificial = 0;
  81. /// List of all Variables and parameters in this function.
  82. StringSet<> VarsInFunction;
  83. /// Compile units also cover a PC range, but have this flag set to false.
  84. bool IsFunction = false;
  85. /// Function has source location information.
  86. bool HasSourceLocation = false;
  87. /// Number of function parameters.
  88. uint64_t NumParams = 0;
  89. /// Number of function parameters with source location.
  90. uint64_t NumParamSourceLocations = 0;
  91. /// Number of function parameters with type.
  92. uint64_t NumParamTypes = 0;
  93. /// Number of function parameters with a DW_AT_location.
  94. uint64_t NumParamLocations = 0;
  95. /// Number of local variables.
  96. uint64_t NumLocalVars = 0;
  97. /// Number of local variables with source location.
  98. uint64_t NumLocalVarSourceLocations = 0;
  99. /// Number of local variables with type.
  100. uint64_t NumLocalVarTypes = 0;
  101. /// Number of local variables with DW_AT_location.
  102. uint64_t NumLocalVarLocations = 0;
  103. };
  104. /// Holds accumulated global statistics about DIEs.
  105. struct GlobalStats {
  106. /// Total number of PC range bytes covered by DW_AT_locations.
  107. SaturatingUINT64 TotalBytesCovered = 0;
  108. /// Total number of parent DIE PC range bytes covered by DW_AT_Locations.
  109. SaturatingUINT64 ScopeBytesCovered = 0;
  110. /// Total number of PC range bytes in each variable's enclosing scope.
  111. SaturatingUINT64 ScopeBytes = 0;
  112. /// Total number of PC range bytes covered by DW_AT_locations with
  113. /// the debug entry values (DW_OP_entry_value).
  114. SaturatingUINT64 ScopeEntryValueBytesCovered = 0;
  115. /// Total number of PC range bytes covered by DW_AT_locations of
  116. /// formal parameters.
  117. SaturatingUINT64 ParamScopeBytesCovered = 0;
  118. /// Total number of PC range bytes in each parameter's enclosing scope.
  119. SaturatingUINT64 ParamScopeBytes = 0;
  120. /// Total number of PC range bytes covered by DW_AT_locations with
  121. /// the debug entry values (DW_OP_entry_value) (only for parameters).
  122. SaturatingUINT64 ParamScopeEntryValueBytesCovered = 0;
  123. /// Total number of PC range bytes covered by DW_AT_locations (only for local
  124. /// variables).
  125. SaturatingUINT64 LocalVarScopeBytesCovered = 0;
  126. /// Total number of PC range bytes in each local variable's enclosing scope.
  127. SaturatingUINT64 LocalVarScopeBytes = 0;
  128. /// Total number of PC range bytes covered by DW_AT_locations with
  129. /// the debug entry values (DW_OP_entry_value) (only for local variables).
  130. SaturatingUINT64 LocalVarScopeEntryValueBytesCovered = 0;
  131. /// Total number of call site entries (DW_AT_call_file & DW_AT_call_line).
  132. SaturatingUINT64 CallSiteEntries = 0;
  133. /// Total number of call site DIEs (DW_TAG_call_site).
  134. SaturatingUINT64 CallSiteDIEs = 0;
  135. /// Total number of call site parameter DIEs (DW_TAG_call_site_parameter).
  136. SaturatingUINT64 CallSiteParamDIEs = 0;
  137. /// Total byte size of concrete functions. This byte size includes
  138. /// inline functions contained in the concrete functions.
  139. SaturatingUINT64 FunctionSize = 0;
  140. /// Total byte size of inlined functions. This is the total number of bytes
  141. /// for the top inline functions within concrete functions. This can help
  142. /// tune the inline settings when compiling to match user expectations.
  143. SaturatingUINT64 InlineFunctionSize = 0;
  144. };
  145. /// Holds accumulated debug location statistics about local variables and
  146. /// formal parameters.
  147. struct LocationStats {
  148. /// Map the scope coverage decile to the number of variables in the decile.
  149. /// The first element of the array (at the index zero) represents the number
  150. /// of variables with the no debug location at all, but the last element
  151. /// in the vector represents the number of fully covered variables within
  152. /// its scope.
  153. std::vector<SaturatingUINT64> VarParamLocStats{
  154. std::vector<SaturatingUINT64>(NumOfCoverageCategories, 0)};
  155. /// Map non debug entry values coverage.
  156. std::vector<SaturatingUINT64> VarParamNonEntryValLocStats{
  157. std::vector<SaturatingUINT64>(NumOfCoverageCategories, 0)};
  158. /// The debug location statistics for formal parameters.
  159. std::vector<SaturatingUINT64> ParamLocStats{
  160. std::vector<SaturatingUINT64>(NumOfCoverageCategories, 0)};
  161. /// Map non debug entry values coverage for formal parameters.
  162. std::vector<SaturatingUINT64> ParamNonEntryValLocStats{
  163. std::vector<SaturatingUINT64>(NumOfCoverageCategories, 0)};
  164. /// The debug location statistics for local variables.
  165. std::vector<SaturatingUINT64> LocalVarLocStats{
  166. std::vector<SaturatingUINT64>(NumOfCoverageCategories, 0)};
  167. /// Map non debug entry values coverage for local variables.
  168. std::vector<SaturatingUINT64> LocalVarNonEntryValLocStats{
  169. std::vector<SaturatingUINT64>(NumOfCoverageCategories, 0)};
  170. /// Total number of local variables and function parameters processed.
  171. SaturatingUINT64 NumVarParam = 0;
  172. /// Total number of formal parameters processed.
  173. SaturatingUINT64 NumParam = 0;
  174. /// Total number of local variables processed.
  175. SaturatingUINT64 NumVar = 0;
  176. };
  177. } // namespace
  178. /// Collect debug location statistics for one DIE.
  179. static void collectLocStats(uint64_t ScopeBytesCovered, uint64_t BytesInScope,
  180. std::vector<SaturatingUINT64> &VarParamLocStats,
  181. std::vector<SaturatingUINT64> &ParamLocStats,
  182. std::vector<SaturatingUINT64> &LocalVarLocStats,
  183. bool IsParam, bool IsLocalVar) {
  184. auto getCoverageBucket = [ScopeBytesCovered, BytesInScope]() -> unsigned {
  185. // No debug location at all for the variable.
  186. if (ScopeBytesCovered == 0)
  187. return 0;
  188. // Fully covered variable within its scope.
  189. if (ScopeBytesCovered >= BytesInScope)
  190. return NumOfCoverageCategories - 1;
  191. // Get covered range (e.g. 20%-29%).
  192. unsigned LocBucket = 100 * (double)ScopeBytesCovered / BytesInScope;
  193. LocBucket /= 10;
  194. return LocBucket + 1;
  195. };
  196. unsigned CoverageBucket = getCoverageBucket();
  197. VarParamLocStats[CoverageBucket].Value++;
  198. if (IsParam)
  199. ParamLocStats[CoverageBucket].Value++;
  200. else if (IsLocalVar)
  201. LocalVarLocStats[CoverageBucket].Value++;
  202. }
  203. /// Construct an identifier for a given DIE from its Prefix, Name, DeclFileName
  204. /// and DeclLine. The identifier aims to be unique for any unique entities,
  205. /// but keeping the same among different instances of the same entity.
  206. static std::string constructDieID(DWARFDie Die,
  207. StringRef Prefix = StringRef()) {
  208. std::string IDStr;
  209. llvm::raw_string_ostream ID(IDStr);
  210. ID << Prefix
  211. << Die.getName(DINameKind::LinkageName);
  212. // Prefix + Name is enough for local variables and parameters.
  213. if (!Prefix.empty() && !Prefix.equals("g"))
  214. return ID.str();
  215. auto DeclFile = Die.findRecursively(dwarf::DW_AT_decl_file);
  216. std::string File;
  217. if (DeclFile) {
  218. DWARFUnit *U = Die.getDwarfUnit();
  219. if (const auto *LT = U->getContext().getLineTableForUnit(U))
  220. if (LT->getFileNameByIndex(
  221. dwarf::toUnsigned(DeclFile, 0), U->getCompilationDir(),
  222. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File))
  223. File = std::string(sys::path::filename(File));
  224. }
  225. ID << ":" << (File.empty() ? "/" : File);
  226. ID << ":"
  227. << dwarf::toUnsigned(Die.findRecursively(dwarf::DW_AT_decl_line), 0);
  228. return ID.str();
  229. }
  230. /// Return the number of bytes in the overlap of ranges A and B.
  231. static uint64_t calculateOverlap(DWARFAddressRange A, DWARFAddressRange B) {
  232. uint64_t Lower = std::max(A.LowPC, B.LowPC);
  233. uint64_t Upper = std::min(A.HighPC, B.HighPC);
  234. if (Lower >= Upper)
  235. return 0;
  236. return Upper - Lower;
  237. }
  238. /// Collect debug info quality metrics for one DIE.
  239. static void collectStatsForDie(DWARFDie Die, const std::string &FnPrefix,
  240. const std::string &VarPrefix,
  241. uint64_t BytesInScope, uint32_t InlineDepth,
  242. StringMap<PerFunctionStats> &FnStatMap,
  243. GlobalStats &GlobalStats,
  244. LocationStats &LocStats,
  245. AbstractOriginVarsTy *AbstractOriginVariables) {
  246. const dwarf::Tag Tag = Die.getTag();
  247. // Skip CU node.
  248. if (Tag == dwarf::DW_TAG_compile_unit)
  249. return;
  250. bool HasLoc = false;
  251. bool HasSrcLoc = false;
  252. bool HasType = false;
  253. uint64_t TotalBytesCovered = 0;
  254. uint64_t ScopeBytesCovered = 0;
  255. uint64_t BytesEntryValuesCovered = 0;
  256. auto &FnStats = FnStatMap[FnPrefix];
  257. bool IsParam = Tag == dwarf::DW_TAG_formal_parameter;
  258. bool IsLocalVar = Tag == dwarf::DW_TAG_variable;
  259. bool IsConstantMember = Tag == dwarf::DW_TAG_member &&
  260. Die.find(dwarf::DW_AT_const_value);
  261. // For zero covered inlined variables the locstats will be
  262. // calculated later.
  263. bool DeferLocStats = false;
  264. if (Tag == dwarf::DW_TAG_call_site || Tag == dwarf::DW_TAG_GNU_call_site) {
  265. GlobalStats.CallSiteDIEs++;
  266. return;
  267. }
  268. if (Tag == dwarf::DW_TAG_call_site_parameter ||
  269. Tag == dwarf::DW_TAG_GNU_call_site_parameter) {
  270. GlobalStats.CallSiteParamDIEs++;
  271. return;
  272. }
  273. if (!IsParam && !IsLocalVar && !IsConstantMember) {
  274. // Not a variable or constant member.
  275. return;
  276. }
  277. // Ignore declarations of global variables.
  278. if (IsLocalVar && Die.find(dwarf::DW_AT_declaration))
  279. return;
  280. if (Die.findRecursively(dwarf::DW_AT_decl_file) &&
  281. Die.findRecursively(dwarf::DW_AT_decl_line))
  282. HasSrcLoc = true;
  283. if (Die.findRecursively(dwarf::DW_AT_type))
  284. HasType = true;
  285. if (Die.find(dwarf::DW_AT_abstract_origin)) {
  286. if (Die.find(dwarf::DW_AT_location) || Die.find(dwarf::DW_AT_const_value)) {
  287. if (AbstractOriginVariables) {
  288. auto Offset = Die.find(dwarf::DW_AT_abstract_origin);
  289. // Do not track this variable any more, since it has location
  290. // coverage.
  291. llvm::erase_value(*AbstractOriginVariables, (*Offset).getRawUValue());
  292. }
  293. } else {
  294. // The locstats will be handled at the end of
  295. // the collectStatsRecursive().
  296. DeferLocStats = true;
  297. }
  298. }
  299. auto IsEntryValue = [&](ArrayRef<uint8_t> D) -> bool {
  300. DWARFUnit *U = Die.getDwarfUnit();
  301. DataExtractor Data(toStringRef(D),
  302. Die.getDwarfUnit()->getContext().isLittleEndian(), 0);
  303. DWARFExpression Expression(Data, U->getAddressByteSize(),
  304. U->getFormParams().Format);
  305. // Consider the expression containing the DW_OP_entry_value as
  306. // an entry value.
  307. return llvm::any_of(Expression, [](const DWARFExpression::Operation &Op) {
  308. return Op.getCode() == dwarf::DW_OP_entry_value ||
  309. Op.getCode() == dwarf::DW_OP_GNU_entry_value;
  310. });
  311. };
  312. if (Die.find(dwarf::DW_AT_const_value)) {
  313. // This catches constant members *and* variables.
  314. HasLoc = true;
  315. ScopeBytesCovered = BytesInScope;
  316. TotalBytesCovered = BytesInScope;
  317. } else {
  318. // Handle variables and function arguments.
  319. Expected<std::vector<DWARFLocationExpression>> Loc =
  320. Die.getLocations(dwarf::DW_AT_location);
  321. if (!Loc) {
  322. consumeError(Loc.takeError());
  323. } else {
  324. HasLoc = true;
  325. // Get PC coverage.
  326. auto Default = find_if(
  327. *Loc, [](const DWARFLocationExpression &L) { return !L.Range; });
  328. if (Default != Loc->end()) {
  329. // Assume the entire range is covered by a single location.
  330. ScopeBytesCovered = BytesInScope;
  331. TotalBytesCovered = BytesInScope;
  332. } else {
  333. // Caller checks this Expected result already, it cannot fail.
  334. auto ScopeRanges = cantFail(Die.getParent().getAddressRanges());
  335. for (auto Entry : *Loc) {
  336. TotalBytesCovered += Entry.Range->HighPC - Entry.Range->LowPC;
  337. uint64_t ScopeBytesCoveredByEntry = 0;
  338. // Calculate how many bytes of the parent scope this entry covers.
  339. // FIXME: In section 2.6.2 of the DWARFv5 spec it says that "The
  340. // address ranges defined by the bounded location descriptions of a
  341. // location list may overlap". So in theory a variable can have
  342. // multiple simultaneous locations, which would make this calculation
  343. // misleading because we will count the overlapped areas
  344. // twice. However, clang does not currently emit DWARF like this.
  345. for (DWARFAddressRange R : ScopeRanges) {
  346. ScopeBytesCoveredByEntry += calculateOverlap(*Entry.Range, R);
  347. }
  348. ScopeBytesCovered += ScopeBytesCoveredByEntry;
  349. if (IsEntryValue(Entry.Expr))
  350. BytesEntryValuesCovered += ScopeBytesCoveredByEntry;
  351. }
  352. }
  353. }
  354. }
  355. // Calculate the debug location statistics.
  356. if (BytesInScope && !DeferLocStats) {
  357. LocStats.NumVarParam.Value++;
  358. if (IsParam)
  359. LocStats.NumParam.Value++;
  360. else if (IsLocalVar)
  361. LocStats.NumVar.Value++;
  362. collectLocStats(ScopeBytesCovered, BytesInScope, LocStats.VarParamLocStats,
  363. LocStats.ParamLocStats, LocStats.LocalVarLocStats, IsParam,
  364. IsLocalVar);
  365. // Non debug entry values coverage statistics.
  366. collectLocStats(ScopeBytesCovered - BytesEntryValuesCovered, BytesInScope,
  367. LocStats.VarParamNonEntryValLocStats,
  368. LocStats.ParamNonEntryValLocStats,
  369. LocStats.LocalVarNonEntryValLocStats, IsParam, IsLocalVar);
  370. }
  371. // Collect PC range coverage data.
  372. if (DWARFDie D =
  373. Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_abstract_origin))
  374. Die = D;
  375. std::string VarID = constructDieID(Die, VarPrefix);
  376. FnStats.VarsInFunction.insert(VarID);
  377. GlobalStats.TotalBytesCovered += TotalBytesCovered;
  378. if (BytesInScope) {
  379. GlobalStats.ScopeBytesCovered += ScopeBytesCovered;
  380. GlobalStats.ScopeBytes += BytesInScope;
  381. GlobalStats.ScopeEntryValueBytesCovered += BytesEntryValuesCovered;
  382. if (IsParam) {
  383. GlobalStats.ParamScopeBytesCovered += ScopeBytesCovered;
  384. GlobalStats.ParamScopeBytes += BytesInScope;
  385. GlobalStats.ParamScopeEntryValueBytesCovered += BytesEntryValuesCovered;
  386. } else if (IsLocalVar) {
  387. GlobalStats.LocalVarScopeBytesCovered += ScopeBytesCovered;
  388. GlobalStats.LocalVarScopeBytes += BytesInScope;
  389. GlobalStats.LocalVarScopeEntryValueBytesCovered +=
  390. BytesEntryValuesCovered;
  391. }
  392. assert(GlobalStats.ScopeBytesCovered.Value <= GlobalStats.ScopeBytes.Value);
  393. }
  394. if (IsConstantMember) {
  395. FnStats.ConstantMembers++;
  396. return;
  397. }
  398. FnStats.TotalVarWithLoc += (unsigned)HasLoc;
  399. if (Die.find(dwarf::DW_AT_artificial)) {
  400. FnStats.NumArtificial++;
  401. return;
  402. }
  403. if (IsParam) {
  404. FnStats.NumParams++;
  405. if (HasType)
  406. FnStats.NumParamTypes++;
  407. if (HasSrcLoc)
  408. FnStats.NumParamSourceLocations++;
  409. if (HasLoc)
  410. FnStats.NumParamLocations++;
  411. } else if (IsLocalVar) {
  412. FnStats.NumLocalVars++;
  413. if (HasType)
  414. FnStats.NumLocalVarTypes++;
  415. if (HasSrcLoc)
  416. FnStats.NumLocalVarSourceLocations++;
  417. if (HasLoc)
  418. FnStats.NumLocalVarLocations++;
  419. }
  420. }
  421. /// Recursively collect variables from subprogram with DW_AT_inline attribute.
  422. static void collectAbstractOriginFnInfo(
  423. DWARFDie Die, uint64_t SPOffset,
  424. AbstractOriginVarsTyMap &GlobalAbstractOriginFnInfo,
  425. AbstractOriginVarsTyMap &LocalAbstractOriginFnInfo) {
  426. DWARFDie Child = Die.getFirstChild();
  427. while (Child) {
  428. const dwarf::Tag ChildTag = Child.getTag();
  429. if (ChildTag == dwarf::DW_TAG_formal_parameter ||
  430. ChildTag == dwarf::DW_TAG_variable) {
  431. GlobalAbstractOriginFnInfo[SPOffset].push_back(Child.getOffset());
  432. LocalAbstractOriginFnInfo[SPOffset].push_back(Child.getOffset());
  433. } else if (ChildTag == dwarf::DW_TAG_lexical_block)
  434. collectAbstractOriginFnInfo(Child, SPOffset, GlobalAbstractOriginFnInfo,
  435. LocalAbstractOriginFnInfo);
  436. Child = Child.getSibling();
  437. }
  438. }
  439. /// Recursively collect debug info quality metrics.
  440. static void collectStatsRecursive(
  441. DWARFDie Die, std::string FnPrefix, std::string VarPrefix,
  442. uint64_t BytesInScope, uint32_t InlineDepth,
  443. StringMap<PerFunctionStats> &FnStatMap, GlobalStats &GlobalStats,
  444. LocationStats &LocStats, FunctionDIECUTyMap &AbstractOriginFnCUs,
  445. AbstractOriginVarsTyMap &GlobalAbstractOriginFnInfo,
  446. AbstractOriginVarsTyMap &LocalAbstractOriginFnInfo,
  447. FunctionsWithAbstractOriginTy &FnsWithAbstractOriginToBeProcessed,
  448. AbstractOriginVarsTy *AbstractOriginVarsPtr = nullptr) {
  449. // Skip NULL nodes.
  450. if (Die.isNULL())
  451. return;
  452. const dwarf::Tag Tag = Die.getTag();
  453. // Skip function types.
  454. if (Tag == dwarf::DW_TAG_subroutine_type)
  455. return;
  456. // Handle any kind of lexical scope.
  457. const bool HasAbstractOrigin =
  458. Die.find(dwarf::DW_AT_abstract_origin) != std::nullopt;
  459. const bool IsFunction = Tag == dwarf::DW_TAG_subprogram;
  460. const bool IsBlock = Tag == dwarf::DW_TAG_lexical_block;
  461. const bool IsInlinedFunction = Tag == dwarf::DW_TAG_inlined_subroutine;
  462. // We want to know how many variables (with abstract_origin) don't have
  463. // location info.
  464. const bool IsCandidateForZeroLocCovTracking =
  465. (IsInlinedFunction || (IsFunction && HasAbstractOrigin));
  466. AbstractOriginVarsTy AbstractOriginVars;
  467. // Get the vars of the inlined fn, so the locstats
  468. // reports the missing vars (with coverage 0%).
  469. if (IsCandidateForZeroLocCovTracking) {
  470. auto OffsetFn = Die.find(dwarf::DW_AT_abstract_origin);
  471. if (OffsetFn) {
  472. uint64_t OffsetOfInlineFnCopy = (*OffsetFn).getRawUValue();
  473. if (LocalAbstractOriginFnInfo.count(OffsetOfInlineFnCopy)) {
  474. AbstractOriginVars = LocalAbstractOriginFnInfo[OffsetOfInlineFnCopy];
  475. AbstractOriginVarsPtr = &AbstractOriginVars;
  476. } else {
  477. // This means that the DW_AT_inline fn copy is out of order
  478. // or that the abstract_origin references another CU,
  479. // so this abstract origin instance will be processed later.
  480. FnsWithAbstractOriginToBeProcessed.push_back(Die.getOffset());
  481. AbstractOriginVarsPtr = nullptr;
  482. }
  483. }
  484. }
  485. if (IsFunction || IsInlinedFunction || IsBlock) {
  486. // Reset VarPrefix when entering a new function.
  487. if (IsFunction || IsInlinedFunction)
  488. VarPrefix = "v";
  489. // Ignore forward declarations.
  490. if (Die.find(dwarf::DW_AT_declaration))
  491. return;
  492. // Check for call sites.
  493. if (Die.find(dwarf::DW_AT_call_file) && Die.find(dwarf::DW_AT_call_line))
  494. GlobalStats.CallSiteEntries++;
  495. // PC Ranges.
  496. auto RangesOrError = Die.getAddressRanges();
  497. if (!RangesOrError) {
  498. llvm::consumeError(RangesOrError.takeError());
  499. return;
  500. }
  501. auto Ranges = RangesOrError.get();
  502. uint64_t BytesInThisScope = 0;
  503. for (auto Range : Ranges)
  504. BytesInThisScope += Range.HighPC - Range.LowPC;
  505. // Count the function.
  506. if (!IsBlock) {
  507. // Skip over abstract origins, but collect variables
  508. // from it so it can be used for location statistics
  509. // for inlined instancies.
  510. if (Die.find(dwarf::DW_AT_inline)) {
  511. uint64_t SPOffset = Die.getOffset();
  512. AbstractOriginFnCUs[SPOffset] = Die.getDwarfUnit();
  513. collectAbstractOriginFnInfo(Die, SPOffset, GlobalAbstractOriginFnInfo,
  514. LocalAbstractOriginFnInfo);
  515. return;
  516. }
  517. std::string FnID = constructDieID(Die);
  518. // We've seen an instance of this function.
  519. auto &FnStats = FnStatMap[FnID];
  520. FnStats.IsFunction = true;
  521. if (IsInlinedFunction) {
  522. FnStats.NumFnInlined++;
  523. if (Die.findRecursively(dwarf::DW_AT_abstract_origin))
  524. FnStats.NumAbstractOrigins++;
  525. } else {
  526. FnStats.NumFnOutOfLine++;
  527. }
  528. if (Die.findRecursively(dwarf::DW_AT_decl_file) &&
  529. Die.findRecursively(dwarf::DW_AT_decl_line))
  530. FnStats.HasSourceLocation = true;
  531. // Update function prefix.
  532. FnPrefix = FnID;
  533. }
  534. if (BytesInThisScope) {
  535. BytesInScope = BytesInThisScope;
  536. if (IsFunction)
  537. GlobalStats.FunctionSize += BytesInThisScope;
  538. else if (IsInlinedFunction && InlineDepth == 0)
  539. GlobalStats.InlineFunctionSize += BytesInThisScope;
  540. }
  541. } else {
  542. // Not a scope, visit the Die itself. It could be a variable.
  543. collectStatsForDie(Die, FnPrefix, VarPrefix, BytesInScope, InlineDepth,
  544. FnStatMap, GlobalStats, LocStats, AbstractOriginVarsPtr);
  545. }
  546. // Set InlineDepth correctly for child recursion
  547. if (IsFunction)
  548. InlineDepth = 0;
  549. else if (IsInlinedFunction)
  550. ++InlineDepth;
  551. // Traverse children.
  552. unsigned LexicalBlockIndex = 0;
  553. unsigned FormalParameterIndex = 0;
  554. DWARFDie Child = Die.getFirstChild();
  555. while (Child) {
  556. std::string ChildVarPrefix = VarPrefix;
  557. if (Child.getTag() == dwarf::DW_TAG_lexical_block)
  558. ChildVarPrefix += toHex(LexicalBlockIndex++) + '.';
  559. if (Child.getTag() == dwarf::DW_TAG_formal_parameter)
  560. ChildVarPrefix += 'p' + toHex(FormalParameterIndex++) + '.';
  561. collectStatsRecursive(
  562. Child, FnPrefix, ChildVarPrefix, BytesInScope, InlineDepth, FnStatMap,
  563. GlobalStats, LocStats, AbstractOriginFnCUs, GlobalAbstractOriginFnInfo,
  564. LocalAbstractOriginFnInfo, FnsWithAbstractOriginToBeProcessed,
  565. AbstractOriginVarsPtr);
  566. Child = Child.getSibling();
  567. }
  568. if (!IsCandidateForZeroLocCovTracking)
  569. return;
  570. // After we have processed all vars of the inlined function (or function with
  571. // an abstract_origin), we want to know how many variables have no location.
  572. for (auto Offset : AbstractOriginVars) {
  573. LocStats.NumVarParam++;
  574. LocStats.VarParamLocStats[ZeroCoverageBucket]++;
  575. auto FnDie = Die.getDwarfUnit()->getDIEForOffset(Offset);
  576. if (!FnDie)
  577. continue;
  578. auto Tag = FnDie.getTag();
  579. if (Tag == dwarf::DW_TAG_formal_parameter) {
  580. LocStats.NumParam++;
  581. LocStats.ParamLocStats[ZeroCoverageBucket]++;
  582. } else if (Tag == dwarf::DW_TAG_variable) {
  583. LocStats.NumVar++;
  584. LocStats.LocalVarLocStats[ZeroCoverageBucket]++;
  585. }
  586. }
  587. }
  588. /// Print human-readable output.
  589. /// \{
  590. static void printDatum(json::OStream &J, const char *Key, json::Value Value) {
  591. if (Value == OverflowValue)
  592. J.attribute(Key, "overflowed");
  593. else
  594. J.attribute(Key, Value);
  595. LLVM_DEBUG(llvm::dbgs() << Key << ": " << Value << '\n');
  596. }
  597. static void printLocationStats(json::OStream &J, const char *Key,
  598. std::vector<SaturatingUINT64> &LocationStats) {
  599. if (LocationStats[0].Value == OverflowValue)
  600. J.attribute((Twine(Key) +
  601. " with (0%,10%) of parent scope covered by DW_AT_location")
  602. .str(),
  603. "overflowed");
  604. else
  605. J.attribute(
  606. (Twine(Key) + " with 0% of parent scope covered by DW_AT_location")
  607. .str(),
  608. LocationStats[0].Value);
  609. LLVM_DEBUG(
  610. llvm::dbgs() << Key
  611. << " with 0% of parent scope covered by DW_AT_location: \\"
  612. << LocationStats[0].Value << '\n');
  613. if (LocationStats[1].Value == OverflowValue)
  614. J.attribute((Twine(Key) +
  615. " with (0%,10%) of parent scope covered by DW_AT_location")
  616. .str(),
  617. "overflowed");
  618. else
  619. J.attribute((Twine(Key) +
  620. " with (0%,10%) of parent scope covered by DW_AT_location")
  621. .str(),
  622. LocationStats[1].Value);
  623. LLVM_DEBUG(llvm::dbgs()
  624. << Key
  625. << " with (0%,10%) of parent scope covered by DW_AT_location: "
  626. << LocationStats[1].Value << '\n');
  627. for (unsigned i = 2; i < NumOfCoverageCategories - 1; ++i) {
  628. if (LocationStats[i].Value == OverflowValue)
  629. J.attribute((Twine(Key) + " with [" + Twine((i - 1) * 10) + "%," +
  630. Twine(i * 10) +
  631. "%) of parent scope covered by DW_AT_location")
  632. .str(),
  633. "overflowed");
  634. else
  635. J.attribute((Twine(Key) + " with [" + Twine((i - 1) * 10) + "%," +
  636. Twine(i * 10) +
  637. "%) of parent scope covered by DW_AT_location")
  638. .str(),
  639. LocationStats[i].Value);
  640. LLVM_DEBUG(llvm::dbgs()
  641. << Key << " with [" << (i - 1) * 10 << "%," << i * 10
  642. << "%) of parent scope covered by DW_AT_location: "
  643. << LocationStats[i].Value);
  644. }
  645. if (LocationStats[NumOfCoverageCategories - 1].Value == OverflowValue)
  646. J.attribute(
  647. (Twine(Key) + " with 100% of parent scope covered by DW_AT_location")
  648. .str(),
  649. "overflowed");
  650. else
  651. J.attribute(
  652. (Twine(Key) + " with 100% of parent scope covered by DW_AT_location")
  653. .str(),
  654. LocationStats[NumOfCoverageCategories - 1].Value);
  655. LLVM_DEBUG(
  656. llvm::dbgs() << Key
  657. << " with 100% of parent scope covered by DW_AT_location: "
  658. << LocationStats[NumOfCoverageCategories - 1].Value);
  659. }
  660. static void printSectionSizes(json::OStream &J, const SectionSizes &Sizes) {
  661. for (const auto &It : Sizes.DebugSectionSizes)
  662. J.attribute((Twine("#bytes in ") + It.first).str(), int64_t(It.second));
  663. }
  664. /// Stop tracking variables that contain abstract_origin with a location.
  665. /// This is used for out-of-order DW_AT_inline subprograms only.
  666. static void updateVarsWithAbstractOriginLocCovInfo(
  667. DWARFDie FnDieWithAbstractOrigin,
  668. AbstractOriginVarsTy &AbstractOriginVars) {
  669. DWARFDie Child = FnDieWithAbstractOrigin.getFirstChild();
  670. while (Child) {
  671. const dwarf::Tag ChildTag = Child.getTag();
  672. if ((ChildTag == dwarf::DW_TAG_formal_parameter ||
  673. ChildTag == dwarf::DW_TAG_variable) &&
  674. (Child.find(dwarf::DW_AT_location) ||
  675. Child.find(dwarf::DW_AT_const_value))) {
  676. auto OffsetVar = Child.find(dwarf::DW_AT_abstract_origin);
  677. if (OffsetVar)
  678. llvm::erase_value(AbstractOriginVars, (*OffsetVar).getRawUValue());
  679. } else if (ChildTag == dwarf::DW_TAG_lexical_block)
  680. updateVarsWithAbstractOriginLocCovInfo(Child, AbstractOriginVars);
  681. Child = Child.getSibling();
  682. }
  683. }
  684. /// Collect zero location coverage for inlined variables which refer to
  685. /// a DW_AT_inline copy of subprogram that is out of order in the DWARF.
  686. /// Also cover the variables of a concrete function (represented with
  687. /// the DW_TAG_subprogram) with an abstract_origin attribute.
  688. static void collectZeroLocCovForVarsWithAbstractOrigin(
  689. DWARFUnit *DwUnit, GlobalStats &GlobalStats, LocationStats &LocStats,
  690. AbstractOriginVarsTyMap &LocalAbstractOriginFnInfo,
  691. FunctionsWithAbstractOriginTy &FnsWithAbstractOriginToBeProcessed) {
  692. // The next variable is used to filter out functions that have been processed,
  693. // leaving FnsWithAbstractOriginToBeProcessed with just CrossCU references.
  694. FunctionsWithAbstractOriginTy ProcessedFns;
  695. for (auto FnOffset : FnsWithAbstractOriginToBeProcessed) {
  696. DWARFDie FnDieWithAbstractOrigin = DwUnit->getDIEForOffset(FnOffset);
  697. auto FnCopy = FnDieWithAbstractOrigin.find(dwarf::DW_AT_abstract_origin);
  698. AbstractOriginVarsTy AbstractOriginVars;
  699. if (!FnCopy)
  700. continue;
  701. uint64_t FnCopyRawUValue = (*FnCopy).getRawUValue();
  702. // If there is no entry within LocalAbstractOriginFnInfo for the given
  703. // FnCopyRawUValue, function isn't out-of-order in DWARF. Rather, we have
  704. // CrossCU referencing.
  705. if (!LocalAbstractOriginFnInfo.count(FnCopyRawUValue))
  706. continue;
  707. AbstractOriginVars = LocalAbstractOriginFnInfo[FnCopyRawUValue];
  708. updateVarsWithAbstractOriginLocCovInfo(FnDieWithAbstractOrigin,
  709. AbstractOriginVars);
  710. for (auto Offset : AbstractOriginVars) {
  711. LocStats.NumVarParam++;
  712. LocStats.VarParamLocStats[ZeroCoverageBucket]++;
  713. auto Tag = DwUnit->getDIEForOffset(Offset).getTag();
  714. if (Tag == dwarf::DW_TAG_formal_parameter) {
  715. LocStats.NumParam++;
  716. LocStats.ParamLocStats[ZeroCoverageBucket]++;
  717. } else if (Tag == dwarf::DW_TAG_variable) {
  718. LocStats.NumVar++;
  719. LocStats.LocalVarLocStats[ZeroCoverageBucket]++;
  720. }
  721. }
  722. ProcessedFns.push_back(FnOffset);
  723. }
  724. for (auto ProcessedFn : ProcessedFns)
  725. llvm::erase_value(FnsWithAbstractOriginToBeProcessed, ProcessedFn);
  726. }
  727. /// Collect zero location coverage for inlined variables which refer to
  728. /// a DW_AT_inline copy of subprogram that is in a different CU.
  729. static void collectZeroLocCovForVarsWithCrossCUReferencingAbstractOrigin(
  730. LocationStats &LocStats, FunctionDIECUTyMap AbstractOriginFnCUs,
  731. AbstractOriginVarsTyMap &GlobalAbstractOriginFnInfo,
  732. CrossCUReferencingDIELocationTy &CrossCUReferencesToBeResolved) {
  733. for (const auto &CrossCUReferenceToBeResolved :
  734. CrossCUReferencesToBeResolved) {
  735. DWARFUnit *DwUnit = CrossCUReferenceToBeResolved.DwUnit;
  736. DWARFDie FnDIEWithCrossCUReferencing =
  737. DwUnit->getDIEForOffset(CrossCUReferenceToBeResolved.DIEOffset);
  738. auto FnCopy =
  739. FnDIEWithCrossCUReferencing.find(dwarf::DW_AT_abstract_origin);
  740. if (!FnCopy)
  741. continue;
  742. uint64_t FnCopyRawUValue = (*FnCopy).getRawUValue();
  743. AbstractOriginVarsTy AbstractOriginVars =
  744. GlobalAbstractOriginFnInfo[FnCopyRawUValue];
  745. updateVarsWithAbstractOriginLocCovInfo(FnDIEWithCrossCUReferencing,
  746. AbstractOriginVars);
  747. for (auto Offset : AbstractOriginVars) {
  748. LocStats.NumVarParam++;
  749. LocStats.VarParamLocStats[ZeroCoverageBucket]++;
  750. auto Tag = (AbstractOriginFnCUs[FnCopyRawUValue])
  751. ->getDIEForOffset(Offset)
  752. .getTag();
  753. if (Tag == dwarf::DW_TAG_formal_parameter) {
  754. LocStats.NumParam++;
  755. LocStats.ParamLocStats[ZeroCoverageBucket]++;
  756. } else if (Tag == dwarf::DW_TAG_variable) {
  757. LocStats.NumVar++;
  758. LocStats.LocalVarLocStats[ZeroCoverageBucket]++;
  759. }
  760. }
  761. }
  762. }
  763. /// \}
  764. /// Collect debug info quality metrics for an entire DIContext.
  765. ///
  766. /// Do the impossible and reduce the quality of the debug info down to a few
  767. /// numbers. The idea is to condense the data into numbers that can be tracked
  768. /// over time to identify trends in newer compiler versions and gauge the effect
  769. /// of particular optimizations. The raw numbers themselves are not particularly
  770. /// useful, only the delta between compiling the same program with different
  771. /// compilers is.
  772. bool dwarfdump::collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
  773. const Twine &Filename,
  774. raw_ostream &OS) {
  775. StringRef FormatName = Obj.getFileFormatName();
  776. GlobalStats GlobalStats;
  777. LocationStats LocStats;
  778. StringMap<PerFunctionStats> Statistics;
  779. // This variable holds variable information for functions with
  780. // abstract_origin globally, across all CUs.
  781. AbstractOriginVarsTyMap GlobalAbstractOriginFnInfo;
  782. // This variable holds information about the CU of a function with
  783. // abstract_origin.
  784. FunctionDIECUTyMap AbstractOriginFnCUs;
  785. CrossCUReferencingDIELocationTy CrossCUReferencesToBeResolved;
  786. for (const auto &CU : static_cast<DWARFContext *>(&DICtx)->compile_units()) {
  787. if (DWARFDie CUDie = CU->getNonSkeletonUnitDIE(false)) {
  788. // This variable holds variable information for functions with
  789. // abstract_origin, but just for the current CU.
  790. AbstractOriginVarsTyMap LocalAbstractOriginFnInfo;
  791. FunctionsWithAbstractOriginTy FnsWithAbstractOriginToBeProcessed;
  792. collectStatsRecursive(
  793. CUDie, "/", "g", 0, 0, Statistics, GlobalStats, LocStats,
  794. AbstractOriginFnCUs, GlobalAbstractOriginFnInfo,
  795. LocalAbstractOriginFnInfo, FnsWithAbstractOriginToBeProcessed);
  796. // collectZeroLocCovForVarsWithAbstractOrigin will filter out all
  797. // out-of-order DWARF functions that have been processed within it,
  798. // leaving FnsWithAbstractOriginToBeProcessed with only CrossCU
  799. // references.
  800. collectZeroLocCovForVarsWithAbstractOrigin(
  801. CUDie.getDwarfUnit(), GlobalStats, LocStats,
  802. LocalAbstractOriginFnInfo, FnsWithAbstractOriginToBeProcessed);
  803. // Collect all CrossCU references into CrossCUReferencesToBeResolved.
  804. for (auto CrossCUReferencingDIEOffset :
  805. FnsWithAbstractOriginToBeProcessed)
  806. CrossCUReferencesToBeResolved.push_back(
  807. DIELocation(CUDie.getDwarfUnit(), CrossCUReferencingDIEOffset));
  808. }
  809. }
  810. /// Resolve CrossCU references.
  811. collectZeroLocCovForVarsWithCrossCUReferencingAbstractOrigin(
  812. LocStats, AbstractOriginFnCUs, GlobalAbstractOriginFnInfo,
  813. CrossCUReferencesToBeResolved);
  814. /// Collect the sizes of debug sections.
  815. SectionSizes Sizes;
  816. calculateSectionSizes(Obj, Sizes, Filename);
  817. /// The version number should be increased every time the algorithm is changed
  818. /// (including bug fixes). New metrics may be added without increasing the
  819. /// version.
  820. unsigned Version = 9;
  821. SaturatingUINT64 VarParamTotal = 0;
  822. SaturatingUINT64 VarParamUnique = 0;
  823. SaturatingUINT64 VarParamWithLoc = 0;
  824. SaturatingUINT64 NumFunctions = 0;
  825. SaturatingUINT64 NumInlinedFunctions = 0;
  826. SaturatingUINT64 NumFuncsWithSrcLoc = 0;
  827. SaturatingUINT64 NumAbstractOrigins = 0;
  828. SaturatingUINT64 ParamTotal = 0;
  829. SaturatingUINT64 ParamWithType = 0;
  830. SaturatingUINT64 ParamWithLoc = 0;
  831. SaturatingUINT64 ParamWithSrcLoc = 0;
  832. SaturatingUINT64 LocalVarTotal = 0;
  833. SaturatingUINT64 LocalVarWithType = 0;
  834. SaturatingUINT64 LocalVarWithSrcLoc = 0;
  835. SaturatingUINT64 LocalVarWithLoc = 0;
  836. for (auto &Entry : Statistics) {
  837. PerFunctionStats &Stats = Entry.getValue();
  838. uint64_t TotalVars = Stats.VarsInFunction.size() *
  839. (Stats.NumFnInlined + Stats.NumFnOutOfLine);
  840. // Count variables in global scope.
  841. if (!Stats.IsFunction)
  842. TotalVars =
  843. Stats.NumLocalVars + Stats.ConstantMembers + Stats.NumArtificial;
  844. uint64_t Constants = Stats.ConstantMembers;
  845. VarParamWithLoc += Stats.TotalVarWithLoc + Constants;
  846. VarParamTotal += TotalVars;
  847. VarParamUnique += Stats.VarsInFunction.size();
  848. LLVM_DEBUG(for (auto &V
  849. : Stats.VarsInFunction) llvm::dbgs()
  850. << Entry.getKey() << ": " << V.getKey() << "\n");
  851. NumFunctions += Stats.IsFunction;
  852. NumFuncsWithSrcLoc += Stats.HasSourceLocation;
  853. NumInlinedFunctions += Stats.IsFunction * Stats.NumFnInlined;
  854. NumAbstractOrigins += Stats.IsFunction * Stats.NumAbstractOrigins;
  855. ParamTotal += Stats.NumParams;
  856. ParamWithType += Stats.NumParamTypes;
  857. ParamWithLoc += Stats.NumParamLocations;
  858. ParamWithSrcLoc += Stats.NumParamSourceLocations;
  859. LocalVarTotal += Stats.NumLocalVars;
  860. LocalVarWithType += Stats.NumLocalVarTypes;
  861. LocalVarWithLoc += Stats.NumLocalVarLocations;
  862. LocalVarWithSrcLoc += Stats.NumLocalVarSourceLocations;
  863. }
  864. // Print summary.
  865. OS.SetBufferSize(1024);
  866. json::OStream J(OS, 2);
  867. J.objectBegin();
  868. J.attribute("version", Version);
  869. LLVM_DEBUG(llvm::dbgs() << "Variable location quality metrics\n";
  870. llvm::dbgs() << "---------------------------------\n");
  871. printDatum(J, "file", Filename.str());
  872. printDatum(J, "format", FormatName);
  873. printDatum(J, "#functions", NumFunctions.Value);
  874. printDatum(J, "#functions with location", NumFuncsWithSrcLoc.Value);
  875. printDatum(J, "#inlined functions", NumInlinedFunctions.Value);
  876. printDatum(J, "#inlined functions with abstract origins",
  877. NumAbstractOrigins.Value);
  878. // This includes local variables and formal parameters.
  879. printDatum(J, "#unique source variables", VarParamUnique.Value);
  880. printDatum(J, "#source variables", VarParamTotal.Value);
  881. printDatum(J, "#source variables with location", VarParamWithLoc.Value);
  882. printDatum(J, "#call site entries", GlobalStats.CallSiteEntries.Value);
  883. printDatum(J, "#call site DIEs", GlobalStats.CallSiteDIEs.Value);
  884. printDatum(J, "#call site parameter DIEs",
  885. GlobalStats.CallSiteParamDIEs.Value);
  886. printDatum(J, "sum_all_variables(#bytes in parent scope)",
  887. GlobalStats.ScopeBytes.Value);
  888. printDatum(J,
  889. "sum_all_variables(#bytes in any scope covered by DW_AT_location)",
  890. GlobalStats.TotalBytesCovered.Value);
  891. printDatum(J,
  892. "sum_all_variables(#bytes in parent scope covered by "
  893. "DW_AT_location)",
  894. GlobalStats.ScopeBytesCovered.Value);
  895. printDatum(J,
  896. "sum_all_variables(#bytes in parent scope covered by "
  897. "DW_OP_entry_value)",
  898. GlobalStats.ScopeEntryValueBytesCovered.Value);
  899. printDatum(J, "sum_all_params(#bytes in parent scope)",
  900. GlobalStats.ParamScopeBytes.Value);
  901. printDatum(J,
  902. "sum_all_params(#bytes in parent scope covered by DW_AT_location)",
  903. GlobalStats.ParamScopeBytesCovered.Value);
  904. printDatum(J,
  905. "sum_all_params(#bytes in parent scope covered by "
  906. "DW_OP_entry_value)",
  907. GlobalStats.ParamScopeEntryValueBytesCovered.Value);
  908. printDatum(J, "sum_all_local_vars(#bytes in parent scope)",
  909. GlobalStats.LocalVarScopeBytes.Value);
  910. printDatum(J,
  911. "sum_all_local_vars(#bytes in parent scope covered by "
  912. "DW_AT_location)",
  913. GlobalStats.LocalVarScopeBytesCovered.Value);
  914. printDatum(J,
  915. "sum_all_local_vars(#bytes in parent scope covered by "
  916. "DW_OP_entry_value)",
  917. GlobalStats.LocalVarScopeEntryValueBytesCovered.Value);
  918. printDatum(J, "#bytes within functions", GlobalStats.FunctionSize.Value);
  919. printDatum(J, "#bytes within inlined functions",
  920. GlobalStats.InlineFunctionSize.Value);
  921. // Print the summary for formal parameters.
  922. printDatum(J, "#params", ParamTotal.Value);
  923. printDatum(J, "#params with source location", ParamWithSrcLoc.Value);
  924. printDatum(J, "#params with type", ParamWithType.Value);
  925. printDatum(J, "#params with binary location", ParamWithLoc.Value);
  926. // Print the summary for local variables.
  927. printDatum(J, "#local vars", LocalVarTotal.Value);
  928. printDatum(J, "#local vars with source location", LocalVarWithSrcLoc.Value);
  929. printDatum(J, "#local vars with type", LocalVarWithType.Value);
  930. printDatum(J, "#local vars with binary location", LocalVarWithLoc.Value);
  931. // Print the debug section sizes.
  932. printSectionSizes(J, Sizes);
  933. // Print the location statistics for variables (includes local variables
  934. // and formal parameters).
  935. printDatum(J, "#variables processed by location statistics",
  936. LocStats.NumVarParam.Value);
  937. printLocationStats(J, "#variables", LocStats.VarParamLocStats);
  938. printLocationStats(J, "#variables - entry values",
  939. LocStats.VarParamNonEntryValLocStats);
  940. // Print the location statistics for formal parameters.
  941. printDatum(J, "#params processed by location statistics",
  942. LocStats.NumParam.Value);
  943. printLocationStats(J, "#params", LocStats.ParamLocStats);
  944. printLocationStats(J, "#params - entry values",
  945. LocStats.ParamNonEntryValLocStats);
  946. // Print the location statistics for local variables.
  947. printDatum(J, "#local vars processed by location statistics",
  948. LocStats.NumVar.Value);
  949. printLocationStats(J, "#local vars", LocStats.LocalVarLocStats);
  950. printLocationStats(J, "#local vars - entry values",
  951. LocStats.LocalVarNonEntryValLocStats);
  952. J.objectEnd();
  953. OS << '\n';
  954. LLVM_DEBUG(
  955. llvm::dbgs() << "Total Availability: "
  956. << (VarParamTotal.Value
  957. ? (int)std::round((VarParamWithLoc.Value * 100.0) /
  958. VarParamTotal.Value)
  959. : 0)
  960. << "%\n";
  961. llvm::dbgs() << "PC Ranges covered: "
  962. << (GlobalStats.ScopeBytes.Value
  963. ? (int)std::round(
  964. (GlobalStats.ScopeBytesCovered.Value * 100.0) /
  965. GlobalStats.ScopeBytes.Value)
  966. : 0)
  967. << "%\n");
  968. return true;
  969. }