ImportedFunctionsInliningStatistics.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- ImportedFunctionsInliningStatistics.h -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. // Generating inliner statistics for imported functions, mostly useful for
  14. // ThinLTO.
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_TRANSFORMS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H
  17. #define LLVM_TRANSFORMS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include <string>
  22. #include <vector>
  23. namespace llvm {
  24. class Module;
  25. class Function;
  26. /// Calculate and dump ThinLTO specific inliner stats.
  27. /// The main statistics are:
  28. /// (1) Number of inlined imported functions,
  29. /// (2) Number of imported functions inlined into importing module (indirect),
  30. /// (3) Number of non imported functions inlined into importing module
  31. /// (indirect).
  32. /// The difference between first and the second is that first stat counts
  33. /// all performed inlines on imported functions, but the second one only the
  34. /// functions that have been eventually inlined to a function in the importing
  35. /// module (by a chain of inlines). Because llvm uses bottom-up inliner, it is
  36. /// possible to e.g. import function `A`, `B` and then inline `B` to `A`,
  37. /// and after this `A` might be too big to be inlined into some other function
  38. /// that calls it. It calculates this statistic by building graph, where
  39. /// the nodes are functions, and edges are performed inlines and then by marking
  40. /// the edges starting from not imported function.
  41. ///
  42. /// If `Verbose` is set to true, then it also dumps statistics
  43. /// per each inlined function, sorted by the greatest inlines count like
  44. /// - number of performed inlines
  45. /// - number of performed inlines to importing module
  46. class ImportedFunctionsInliningStatistics {
  47. private:
  48. /// InlineGraphNode represents node in graph of inlined functions.
  49. struct InlineGraphNode {
  50. // Default-constructible and movable.
  51. InlineGraphNode() = default;
  52. InlineGraphNode(InlineGraphNode &&) = default;
  53. InlineGraphNode &operator=(InlineGraphNode &&) = default;
  54. llvm::SmallVector<InlineGraphNode *, 8> InlinedCallees;
  55. /// Incremented every direct inline.
  56. int32_t NumberOfInlines = 0;
  57. /// Number of inlines into non imported function (possibly indirect via
  58. /// intermediate inlines). Computed based on graph search.
  59. int32_t NumberOfRealInlines = 0;
  60. bool Imported = false;
  61. bool Visited = false;
  62. };
  63. public:
  64. ImportedFunctionsInliningStatistics() = default;
  65. ImportedFunctionsInliningStatistics(
  66. const ImportedFunctionsInliningStatistics &) = delete;
  67. /// Set information like AllFunctions, ImportedFunctions, ModuleName.
  68. void setModuleInfo(const Module &M);
  69. /// Record inline of @param Callee to @param Caller for statistis.
  70. void recordInline(const Function &Caller, const Function &Callee);
  71. /// Dump stats computed with InlinerStatistics class.
  72. /// If @param Verbose is true then separate statistics for every inlined
  73. /// function will be printed.
  74. void dump(bool Verbose);
  75. private:
  76. /// Creates new Node in NodeMap and sets attributes, or returns existed one.
  77. InlineGraphNode &createInlineGraphNode(const Function &);
  78. void calculateRealInlines();
  79. void dfs(InlineGraphNode &GraphNode);
  80. using NodesMapTy =
  81. llvm::StringMap<std::unique_ptr<InlineGraphNode>>;
  82. using SortedNodesTy =
  83. std::vector<const NodesMapTy::MapEntryTy*>;
  84. /// Returns vector of elements sorted by
  85. /// (-NumberOfInlines, -NumberOfRealInlines, FunctionName).
  86. SortedNodesTy getSortedNodes();
  87. private:
  88. /// This map manage life of all InlineGraphNodes. Unique pointer to
  89. /// InlineGraphNode used since the node pointers are also saved in the
  90. /// InlinedCallees vector. If it would store InlineGraphNode instead then the
  91. /// address of the node would not be invariant.
  92. NodesMapTy NodesMap;
  93. /// Non external functions that have some other function inlined inside.
  94. std::vector<StringRef> NonImportedCallers;
  95. int AllFunctions = 0;
  96. int ImportedFunctions = 0;
  97. StringRef ModuleName;
  98. };
  99. enum class InlinerFunctionImportStatsOpts {
  100. No = 0,
  101. Basic = 1,
  102. Verbose = 2,
  103. };
  104. } // llvm
  105. #endif // LLVM_TRANSFORMS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif