1 //===- bolt/Core/CallGraph.h ----------------------------------*- C++ -*-===//
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
9 #ifndef BOLT_PASSES_BINARY_FUNCTION_CALLGRAPH_H
10 #define BOLT_PASSES_BINARY_FUNCTION_CALLGRAPH_H
11
12 #include "bolt/Core/CallGraph.h"
13 #include <deque>
14 #include <functional>
15 #include <unordered_map>
16
17 namespace llvm {
18 namespace bolt {
19
20 class BinaryFunction;
21 class BinaryContext;
22
23 class BinaryFunctionCallGraph : public CallGraph {
24 public:
maybeGetNodeId(const BinaryFunction * BF)25 NodeId maybeGetNodeId(const BinaryFunction *BF) const {
26 auto Itr = FuncToNodeId.find(BF);
27 return Itr != FuncToNodeId.end() ? Itr->second : InvalidId;
28 }
getNodeId(const BinaryFunction * BF)29 NodeId getNodeId(const BinaryFunction *BF) const {
30 auto Itr = FuncToNodeId.find(BF);
31 assert(Itr != FuncToNodeId.end());
32 return Itr->second;
33 }
nodeIdToFunc(NodeId Id)34 BinaryFunction *nodeIdToFunc(NodeId Id) {
35 assert(Id < Funcs.size());
36 return Funcs[Id];
37 }
nodeIdToFunc(NodeId Id)38 const BinaryFunction *nodeIdToFunc(NodeId Id) const {
39 assert(Id < Funcs.size());
40 return Funcs[Id];
41 }
42 NodeId addNode(BinaryFunction *BF, uint32_t Size, uint64_t Samples = 0);
43
44 /// Compute a DFS traversal of the call graph.
45 std::deque<BinaryFunction *> buildTraversalOrder();
46
47 private:
48 std::unordered_map<const BinaryFunction *, NodeId> FuncToNodeId;
49 std::vector<BinaryFunction *> Funcs;
50 };
51
52 using CgFilterFunction = std::function<bool(const BinaryFunction &BF)>;
NoFilter(const BinaryFunction &)53 inline bool NoFilter(const BinaryFunction &) { return false; }
54
55 /// Builds a call graph from the map of BinaryFunctions provided in BC.
56 /// The arguments control how the graph is constructed.
57 /// Filter is called on each function, any function that it returns true for
58 /// is omitted from the graph.
59 /// If IncludeSplitCalls is true, then calls from cold BBs are considered for
60 /// the graph, otherwise they are ignored. UseFunctionHotSize controls whether
61 /// the hot size of a function is used when filling in the Size attribute of new
62 /// Nodes. UseEdgeCounts is used to control if the Weight attribute on Arcs is
63 /// computed using the number of calls.
64 BinaryFunctionCallGraph
65 buildCallGraph(BinaryContext &BC, CgFilterFunction Filter = NoFilter,
66 bool CgFromPerfData = false, bool IncludeSplitCalls = true,
67 bool UseFunctionHotSize = false, bool UseSplitHotSize = false,
68 bool UseEdgeCounts = false, bool IgnoreRecursiveCalls = false);
69
70 } // namespace bolt
71 } // namespace llvm
72
73 #endif
74