blob: 4da448c9900b17ad632f9b2b53371e332ddd94ac [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file provides interfaces used to build and manipulate a call graph,
11/// which is a very useful tool for interprocedural optimization.
12///
13/// Every function in a module is represented as a node in the call graph. The
14/// callgraph node keeps track of which functions are called by the function
15/// corresponding to the node.
16///
17/// A call graph may contain nodes where the function that they correspond to
18/// is null. These 'external' nodes are used to represent control flow that is
19/// not represented (or analyzable) in the module. In particular, this
20/// analysis builds one external node such that:
21/// 1. All functions in the module without internal linkage will have edges
22/// from this external node, indicating that they could be called by
23/// functions outside of the module.
24/// 2. All functions whose address is used for something more than a direct
25/// call, for example being stored into a memory location will also have
26/// an edge from this external node. Since they may be called by an
27/// unknown caller later, they must be tracked as such.
28///
29/// There is a second external node added for calls that leave this module.
30/// Functions have a call edge to the external node iff:
31/// 1. The function is external, reflecting the fact that they could call
32/// anything without internal linkage or that has its address taken.
33/// 2. The function contains an indirect function call.
34///
35/// As an extension in the future, there may be multiple nodes with a null
36/// function. These will be used when we can prove (through pointer analysis)
37/// that an indirect call site can call only a specific set of functions.
38///
39/// Because of these properties, the CallGraph captures a conservative superset
40/// of all of the caller-callee relationships, which is useful for
41/// transformations.
42///
43//===----------------------------------------------------------------------===//
44
45#ifndef LLVM_ANALYSIS_CALLGRAPH_H
46#define LLVM_ANALYSIS_CALLGRAPH_H
47
48#include "llvm/ADT/GraphTraits.h"
49#include "llvm/ADT/STLExtras.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050#include "llvm/IR/Function.h"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010051#include "llvm/IR/InstrTypes.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052#include "llvm/IR/Intrinsics.h"
53#include "llvm/IR/PassManager.h"
54#include "llvm/IR/ValueHandle.h"
55#include "llvm/Pass.h"
56#include <cassert>
57#include <map>
58#include <memory>
59#include <utility>
60#include <vector>
61
62namespace llvm {
63
64class CallGraphNode;
65class Module;
66class raw_ostream;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068/// The basic data container for the call graph of a \c Module of IR.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069///
70/// This class exposes both the interface to the call graph for a module of IR.
71///
72/// The core call graph itself can also be updated to reflect changes to the IR.
73class CallGraph {
74 Module &M;
75
76 using FunctionMapTy =
77 std::map<const Function *, std::unique_ptr<CallGraphNode>>;
78
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 /// A map from \c Function* to \c CallGraphNode*.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080 FunctionMapTy FunctionMap;
81
Andrew Scullcdfcccc2018-10-05 20:58:37 +010082 /// This node has edges to all external functions and those internal
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083 /// functions that have their address taken.
84 CallGraphNode *ExternalCallingNode;
85
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 /// This node has edges to it from all functions making indirect calls
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 /// or calling an external function.
88 std::unique_ptr<CallGraphNode> CallsExternalNode;
89
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090public:
91 explicit CallGraph(Module &M);
92 CallGraph(CallGraph &&Arg);
93 ~CallGraph();
94
95 void print(raw_ostream &OS) const;
96 void dump() const;
97
98 using iterator = FunctionMapTy::iterator;
99 using const_iterator = FunctionMapTy::const_iterator;
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 /// Returns the module the call graph corresponds to.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102 Module &getModule() const { return M; }
103
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200104 bool invalidate(Module &, const PreservedAnalyses &PA,
105 ModuleAnalysisManager::Invalidator &);
106
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107 inline iterator begin() { return FunctionMap.begin(); }
108 inline iterator end() { return FunctionMap.end(); }
109 inline const_iterator begin() const { return FunctionMap.begin(); }
110 inline const_iterator end() const { return FunctionMap.end(); }
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 inline const CallGraphNode *operator[](const Function *F) const {
114 const_iterator I = FunctionMap.find(F);
115 assert(I != FunctionMap.end() && "Function not in callgraph!");
116 return I->second.get();
117 }
118
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100119 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120 inline CallGraphNode *operator[](const Function *F) {
121 const_iterator I = FunctionMap.find(F);
122 assert(I != FunctionMap.end() && "Function not in callgraph!");
123 return I->second.get();
124 }
125
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100126 /// Returns the \c CallGraphNode which is used to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127 /// undetermined calls into the callgraph.
128 CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
129
130 CallGraphNode *getCallsExternalNode() const {
131 return CallsExternalNode.get();
132 }
133
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200134 /// Old node has been deleted, and New is to be used in its place, update the
135 /// ExternalCallingNode.
136 void ReplaceExternalCallEdge(CallGraphNode *Old, CallGraphNode *New);
137
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100138 //===---------------------------------------------------------------------
139 // Functions to keep a call graph up to date with a function that has been
140 // modified.
141 //
142
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100143 /// Unlink the function from this module, returning it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144 ///
145 /// Because this removes the function from the module, the call graph node is
146 /// destroyed. This is only valid if the function does not call any other
147 /// functions (ie, there are no edges in it's CGN). The easiest way to do
148 /// this is to dropAllReferences before calling this.
149 Function *removeFunctionFromModule(CallGraphNode *CGN);
150
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100151 /// Similar to operator[], but this will insert a new CallGraphNode for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152 /// \c F if one does not already exist.
153 CallGraphNode *getOrInsertFunction(const Function *F);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200154
155 /// Populate \p CGN based on the calls inside the associated function.
156 void populateCallGraphNode(CallGraphNode *CGN);
157
158 /// Add a function to the call graph, and link the node to all of the
159 /// functions that it calls.
160 void addToCallGraph(Function *F);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161};
162
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100163/// A node in the call graph for a module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100164///
165/// Typically represents a function in the call graph. There are also special
166/// "null" nodes used to represent theoretical entries in the call graph.
167class CallGraphNode {
168public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100169 /// A pair of the calling instruction (a call or invoke)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 /// and the call graph node being called.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200171 /// Call graph node may have two types of call records which represent an edge
172 /// in the call graph - reference or a call edge. Reference edges are not
173 /// associated with any call instruction and are created with the first field
174 /// set to `None`, while real call edges have instruction address in this
175 /// field. Therefore, all real call edges are expected to have a value in the
176 /// first field and it is not supposed to be `nullptr`.
177 /// Reference edges, for example, are used for connecting broker function
178 /// caller to the callback function for callback call sites.
179 using CallRecord = std::pair<Optional<WeakTrackingVH>, CallGraphNode *>;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180
181public:
182 using CalledFunctionsVector = std::vector<CallRecord>;
183
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100184 /// Creates a node for the specified function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200185 inline CallGraphNode(CallGraph *CG, Function *F) : CG(CG), F(F) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186
187 CallGraphNode(const CallGraphNode &) = delete;
188 CallGraphNode &operator=(const CallGraphNode &) = delete;
189
190 ~CallGraphNode() {
191 assert(NumReferences == 0 && "Node deleted while references remain");
192 }
193
194 using iterator = std::vector<CallRecord>::iterator;
195 using const_iterator = std::vector<CallRecord>::const_iterator;
196
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100197 /// Returns the function that this call graph node represents.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100198 Function *getFunction() const { return F; }
199
200 inline iterator begin() { return CalledFunctions.begin(); }
201 inline iterator end() { return CalledFunctions.end(); }
202 inline const_iterator begin() const { return CalledFunctions.begin(); }
203 inline const_iterator end() const { return CalledFunctions.end(); }
204 inline bool empty() const { return CalledFunctions.empty(); }
205 inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
206
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100207 /// Returns the number of other CallGraphNodes in this CallGraph that
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100208 /// reference this node in their callee list.
209 unsigned getNumReferences() const { return NumReferences; }
210
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100211 /// Returns the i'th called function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100212 CallGraphNode *operator[](unsigned i) const {
213 assert(i < CalledFunctions.size() && "Invalid index");
214 return CalledFunctions[i].second;
215 }
216
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100217 /// Print out this call graph node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100218 void dump() const;
219 void print(raw_ostream &OS) const;
220
221 //===---------------------------------------------------------------------
222 // Methods to keep a call graph up to date with a function that has been
223 // modified
224 //
225
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100226 /// Removes all edges from this CallGraphNode to any functions it
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100227 /// calls.
228 void removeAllCalledFunctions() {
229 while (!CalledFunctions.empty()) {
230 CalledFunctions.back().second->DropRef();
231 CalledFunctions.pop_back();
232 }
233 }
234
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100235 /// Moves all the callee information from N to this node.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100236 void stealCalledFunctionsFrom(CallGraphNode *N) {
237 assert(CalledFunctions.empty() &&
238 "Cannot steal callsite information if I already have some");
239 std::swap(CalledFunctions, N->CalledFunctions);
240 }
241
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100242 /// Adds a function to the list of functions called by this one.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100243 void addCalledFunction(CallBase *Call, CallGraphNode *M) {
244 assert(!Call || !Call->getCalledFunction() ||
245 !Call->getCalledFunction()->isIntrinsic() ||
246 !Intrinsic::isLeaf(Call->getCalledFunction()->getIntrinsicID()));
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200247 CalledFunctions.emplace_back(
248 Call ? Optional<WeakTrackingVH>(Call) : Optional<WeakTrackingVH>(), M);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100249 M->AddRef();
250 }
251
252 void removeCallEdge(iterator I) {
253 I->second->DropRef();
254 *I = CalledFunctions.back();
255 CalledFunctions.pop_back();
256 }
257
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100258 /// Removes the edge in the node for the specified call site.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100259 ///
260 /// Note that this method takes linear time, so it should be used sparingly.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100261 void removeCallEdgeFor(CallBase &Call);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100262
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100263 /// Removes all call edges from this node to the specified callee
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100264 /// function.
265 ///
266 /// This takes more time to execute than removeCallEdgeTo, so it should not
267 /// be used unless necessary.
268 void removeAnyCallEdgeTo(CallGraphNode *Callee);
269
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100270 /// Removes one edge associated with a null callsite from this node to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100271 /// the specified callee function.
272 void removeOneAbstractEdgeTo(CallGraphNode *Callee);
273
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100274 /// Replaces the edge in the node for the specified call site with a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100275 /// new one.
276 ///
277 /// Note that this method takes linear time, so it should be used sparingly.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100278 void replaceCallEdge(CallBase &Call, CallBase &NewCall,
279 CallGraphNode *NewNode);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100280
281private:
282 friend class CallGraph;
283
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200284 CallGraph *CG;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100285 Function *F;
286
287 std::vector<CallRecord> CalledFunctions;
288
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100289 /// The number of times that this CallGraphNode occurs in the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100290 /// CalledFunctions array of this or other CallGraphNodes.
291 unsigned NumReferences = 0;
292
293 void DropRef() { --NumReferences; }
294 void AddRef() { ++NumReferences; }
295
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100296 /// A special function that should only be used by the CallGraph class.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100297 void allReferencesDropped() { NumReferences = 0; }
298};
299
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100300/// An analysis pass to compute the \c CallGraph for a \c Module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100301///
302/// This class implements the concept of an analysis pass used by the \c
303/// ModuleAnalysisManager to run an analysis over a module and cache the
304/// resulting data.
305class CallGraphAnalysis : public AnalysisInfoMixin<CallGraphAnalysis> {
306 friend AnalysisInfoMixin<CallGraphAnalysis>;
307
308 static AnalysisKey Key;
309
310public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100311 /// A formulaic type to inform clients of the result type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100312 using Result = CallGraph;
313
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100314 /// Compute the \c CallGraph for the module \c M.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100315 ///
316 /// The real work here is done in the \c CallGraph constructor.
317 CallGraph run(Module &M, ModuleAnalysisManager &) { return CallGraph(M); }
318};
319
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100320/// Printer pass for the \c CallGraphAnalysis results.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100321class CallGraphPrinterPass : public PassInfoMixin<CallGraphPrinterPass> {
322 raw_ostream &OS;
323
324public:
325 explicit CallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
326
327 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
328};
329
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100330/// The \c ModulePass which wraps up a \c CallGraph and the logic to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100331/// build it.
332///
333/// This class exposes both the interface to the call graph container and the
334/// module pass which runs over a module of IR and produces the call graph. The
335/// call graph interface is entirelly a wrapper around a \c CallGraph object
336/// which is stored internally for each module.
337class CallGraphWrapperPass : public ModulePass {
338 std::unique_ptr<CallGraph> G;
339
340public:
341 static char ID; // Class identification, replacement for typeinfo
342
343 CallGraphWrapperPass();
344 ~CallGraphWrapperPass() override;
345
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100346 /// The internal \c CallGraph around which the rest of this interface
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100347 /// is wrapped.
348 const CallGraph &getCallGraph() const { return *G; }
349 CallGraph &getCallGraph() { return *G; }
350
351 using iterator = CallGraph::iterator;
352 using const_iterator = CallGraph::const_iterator;
353
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100354 /// Returns the module the call graph corresponds to.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100355 Module &getModule() const { return G->getModule(); }
356
357 inline iterator begin() { return G->begin(); }
358 inline iterator end() { return G->end(); }
359 inline const_iterator begin() const { return G->begin(); }
360 inline const_iterator end() const { return G->end(); }
361
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100362 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100363 inline const CallGraphNode *operator[](const Function *F) const {
364 return (*G)[F];
365 }
366
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100367 /// Returns the call graph node for the provided function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100368 inline CallGraphNode *operator[](const Function *F) { return (*G)[F]; }
369
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100370 /// Returns the \c CallGraphNode which is used to represent
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100371 /// undetermined calls into the callgraph.
372 CallGraphNode *getExternalCallingNode() const {
373 return G->getExternalCallingNode();
374 }
375
376 CallGraphNode *getCallsExternalNode() const {
377 return G->getCallsExternalNode();
378 }
379
380 //===---------------------------------------------------------------------
381 // Functions to keep a call graph up to date with a function that has been
382 // modified.
383 //
384
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100385 /// Unlink the function from this module, returning it.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100386 ///
387 /// Because this removes the function from the module, the call graph node is
388 /// destroyed. This is only valid if the function does not call any other
389 /// functions (ie, there are no edges in it's CGN). The easiest way to do
390 /// this is to dropAllReferences before calling this.
391 Function *removeFunctionFromModule(CallGraphNode *CGN) {
392 return G->removeFunctionFromModule(CGN);
393 }
394
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100395 /// Similar to operator[], but this will insert a new CallGraphNode for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100396 /// \c F if one does not already exist.
397 CallGraphNode *getOrInsertFunction(const Function *F) {
398 return G->getOrInsertFunction(F);
399 }
400
401 //===---------------------------------------------------------------------
402 // Implementation of the ModulePass interface needed here.
403 //
404
405 void getAnalysisUsage(AnalysisUsage &AU) const override;
406 bool runOnModule(Module &M) override;
407 void releaseMemory() override;
408
409 void print(raw_ostream &o, const Module *) const override;
410 void dump() const;
411};
412
413//===----------------------------------------------------------------------===//
414// GraphTraits specializations for call graphs so that they can be treated as
415// graphs by the generic graph algorithms.
416//
417
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200418// Provide graph traits for traversing call graphs using standard graph
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100419// traversals.
420template <> struct GraphTraits<CallGraphNode *> {
421 using NodeRef = CallGraphNode *;
422 using CGNPairTy = CallGraphNode::CallRecord;
423
424 static NodeRef getEntryNode(CallGraphNode *CGN) { return CGN; }
425 static CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
426
427 using ChildIteratorType =
428 mapped_iterator<CallGraphNode::iterator, decltype(&CGNGetValue)>;
429
430 static ChildIteratorType child_begin(NodeRef N) {
431 return ChildIteratorType(N->begin(), &CGNGetValue);
432 }
433
434 static ChildIteratorType child_end(NodeRef N) {
435 return ChildIteratorType(N->end(), &CGNGetValue);
436 }
437};
438
439template <> struct GraphTraits<const CallGraphNode *> {
440 using NodeRef = const CallGraphNode *;
441 using CGNPairTy = CallGraphNode::CallRecord;
442 using EdgeRef = const CallGraphNode::CallRecord &;
443
444 static NodeRef getEntryNode(const CallGraphNode *CGN) { return CGN; }
445 static const CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
446
447 using ChildIteratorType =
448 mapped_iterator<CallGraphNode::const_iterator, decltype(&CGNGetValue)>;
449 using ChildEdgeIteratorType = CallGraphNode::const_iterator;
450
451 static ChildIteratorType child_begin(NodeRef N) {
452 return ChildIteratorType(N->begin(), &CGNGetValue);
453 }
454
455 static ChildIteratorType child_end(NodeRef N) {
456 return ChildIteratorType(N->end(), &CGNGetValue);
457 }
458
459 static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
460 return N->begin();
461 }
462 static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
463
464 static NodeRef edge_dest(EdgeRef E) { return E.second; }
465};
466
467template <>
468struct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> {
469 using PairTy =
470 std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
471
472 static NodeRef getEntryNode(CallGraph *CGN) {
473 return CGN->getExternalCallingNode(); // Start at the external node!
474 }
475
476 static CallGraphNode *CGGetValuePtr(const PairTy &P) {
477 return P.second.get();
478 }
479
480 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
481 using nodes_iterator =
482 mapped_iterator<CallGraph::iterator, decltype(&CGGetValuePtr)>;
483
484 static nodes_iterator nodes_begin(CallGraph *CG) {
485 return nodes_iterator(CG->begin(), &CGGetValuePtr);
486 }
487
488 static nodes_iterator nodes_end(CallGraph *CG) {
489 return nodes_iterator(CG->end(), &CGGetValuePtr);
490 }
491};
492
493template <>
494struct GraphTraits<const CallGraph *> : public GraphTraits<
495 const CallGraphNode *> {
496 using PairTy =
497 std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
498
499 static NodeRef getEntryNode(const CallGraph *CGN) {
500 return CGN->getExternalCallingNode(); // Start at the external node!
501 }
502
503 static const CallGraphNode *CGGetValuePtr(const PairTy &P) {
504 return P.second.get();
505 }
506
507 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
508 using nodes_iterator =
509 mapped_iterator<CallGraph::const_iterator, decltype(&CGGetValuePtr)>;
510
511 static nodes_iterator nodes_begin(const CallGraph *CG) {
512 return nodes_iterator(CG->begin(), &CGGetValuePtr);
513 }
514
515 static nodes_iterator nodes_end(const CallGraph *CG) {
516 return nodes_iterator(CG->end(), &CGGetValuePtr);
517 }
518};
519
520} // end namespace llvm
521
522#endif // LLVM_ANALYSIS_CALLGRAPH_H