Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:10

0001 //===-- ImportedFunctionsInliningStatistics.h -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 // Generating inliner statistics for imported functions, mostly useful for
0009 // ThinLTO.
0010 //===----------------------------------------------------------------------===//
0011 
0012 #ifndef LLVM_ANALYSIS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H
0013 #define LLVM_ANALYSIS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H
0014 
0015 #include "llvm/ADT/SmallVector.h"
0016 #include "llvm/ADT/StringMap.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include <memory>
0019 #include <vector>
0020 
0021 namespace llvm {
0022 class Module;
0023 class Function;
0024 /// Calculate and dump ThinLTO specific inliner stats.
0025 /// The main statistics are:
0026 /// (1) Number of inlined imported functions,
0027 /// (2) Number of imported functions inlined into importing module (indirect),
0028 /// (3) Number of non imported functions inlined into importing module
0029 /// (indirect).
0030 /// The difference between first and the second is that first stat counts
0031 /// all performed inlines on imported functions, but the second one only the
0032 /// functions that have been eventually inlined to a function in the importing
0033 /// module (by a chain of inlines). Because llvm uses bottom-up inliner, it is
0034 /// possible to e.g. import function `A`, `B` and then inline `B` to `A`,
0035 /// and after this `A` might be too big to be inlined into some other function
0036 /// that calls it. It calculates this statistic by building graph, where
0037 /// the nodes are functions, and edges are performed inlines and then by marking
0038 /// the edges starting from not imported function.
0039 ///
0040 /// If `Verbose` is set to true, then it also dumps statistics
0041 /// per each inlined function, sorted by the greatest inlines count like
0042 /// - number of performed inlines
0043 /// - number of performed inlines to importing module
0044 class ImportedFunctionsInliningStatistics {
0045 private:
0046   /// InlineGraphNode represents node in graph of inlined functions.
0047   struct InlineGraphNode {
0048     // Default-constructible and movable.
0049     InlineGraphNode() = default;
0050     InlineGraphNode(InlineGraphNode &&) = default;
0051     InlineGraphNode &operator=(InlineGraphNode &&) = default;
0052 
0053     llvm::SmallVector<InlineGraphNode *, 8> InlinedCallees;
0054     /// Incremented every direct inline.
0055     int32_t NumberOfInlines = 0;
0056     /// Number of inlines into non imported function (possibly indirect via
0057     /// intermediate inlines). Computed based on graph search.
0058     int32_t NumberOfRealInlines = 0;
0059     bool Imported = false;
0060     bool Visited = false;
0061   };
0062 
0063 public:
0064   ImportedFunctionsInliningStatistics() = default;
0065   ImportedFunctionsInliningStatistics(
0066       const ImportedFunctionsInliningStatistics &) = delete;
0067 
0068   /// Set information like AllFunctions, ImportedFunctions, ModuleName.
0069   void setModuleInfo(const Module &M);
0070   /// Record inline of @param Callee to @param Caller for statistis.
0071   void recordInline(const Function &Caller, const Function &Callee);
0072   /// Dump stats computed with InlinerStatistics class.
0073   /// If @param Verbose is true then separate statistics for every inlined
0074   /// function will be printed.
0075   void dump(bool Verbose);
0076 
0077 private:
0078   /// Creates new Node in NodeMap and sets attributes, or returns existed one.
0079   InlineGraphNode &createInlineGraphNode(const Function &);
0080   void calculateRealInlines();
0081   void dfs(InlineGraphNode &GraphNode);
0082 
0083   using NodesMapTy =
0084       llvm::StringMap<std::unique_ptr<InlineGraphNode>>;
0085   using SortedNodesTy =
0086       std::vector<const NodesMapTy::MapEntryTy*>;
0087   /// Returns vector of elements sorted by
0088   /// (-NumberOfInlines, -NumberOfRealInlines, FunctionName).
0089   SortedNodesTy getSortedNodes();
0090 
0091 private:
0092   /// This map manage life of all InlineGraphNodes. Unique pointer to
0093   /// InlineGraphNode used since the node pointers are also saved in the
0094   /// InlinedCallees vector. If it would store InlineGraphNode instead then the
0095   /// address of the node would not be invariant.
0096   NodesMapTy NodesMap;
0097   /// Non external functions that have some other function inlined inside.
0098   std::vector<StringRef> NonImportedCallers;
0099   int AllFunctions = 0;
0100   int ImportedFunctions = 0;
0101   StringRef ModuleName;
0102 };
0103 
0104 enum class InlinerFunctionImportStatsOpts {
0105   No = 0,
0106   Basic = 1,
0107   Verbose = 2,
0108 };
0109 
0110 } // llvm
0111 
0112 #endif // LLVM_ANALYSIS_UTILS_IMPORTEDFUNCTIONSINLININGSTATISTICS_H