blob: 9e0559829dec065d3c04203c4d60e8579821f4e5 [file] [log] [blame]
Chandler Carruthbf71a342014-02-06 04:37:03 +00001//===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/LazyCallGraph.h"
11#include "llvm/ADT/SCCIterator.h"
12#include "llvm/IR/Instructions.h"
13#include "llvm/IR/PassManager.h"
14#include "llvm/Support/CallSite.h"
15#include "llvm/Support/raw_ostream.h"
16#include "llvm/InstVisitor.h"
17
18using namespace llvm;
19
20static void findCallees(
21 SmallVectorImpl<Constant *> &Worklist, SmallPtrSetImpl<Constant *> &Visited,
22 SmallVectorImpl<PointerUnion<Function *, LazyCallGraph::Node *> > &Callees,
23 SmallPtrSetImpl<Function *> &CalleeSet) {
24 while (!Worklist.empty()) {
25 Constant *C = Worklist.pop_back_val();
26
27 if (Function *F = dyn_cast<Function>(C)) {
28 // Note that we consider *any* function with a definition to be a viable
29 // edge. Even if the function's definition is subject to replacement by
30 // some other module (say, a weak definition) there may still be
31 // optimizations which essentially speculate based on the definition and
32 // a way to check that the specific definition is in fact the one being
33 // used. For example, this could be done by moving the weak definition to
34 // a strong (internal) definition and making the weak definition be an
35 // alias. Then a test of the address of the weak function against the new
36 // strong definition's address would be an effective way to determine the
37 // safety of optimizing a direct call edge.
38 if (!F->isDeclaration() && CalleeSet.insert(F))
39 Callees.push_back(F);
40 continue;
41 }
42
43 for (User::value_op_iterator OI = C->value_op_begin(),
44 OE = C->value_op_end();
45 OI != OE; ++OI)
46 if (Visited.insert(cast<Constant>(*OI)))
47 Worklist.push_back(cast<Constant>(*OI));
48 }
49}
50
51LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) : G(G), F(F) {
52 SmallVector<Constant *, 16> Worklist;
53 SmallPtrSet<Constant *, 16> Visited;
54 // Find all the potential callees in this function. First walk the
55 // instructions and add every operand which is a constant to the worklist.
56 for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
57 for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
58 ++II)
59 for (User::value_op_iterator OI = II->value_op_begin(),
60 OE = II->value_op_end();
61 OI != OE; ++OI)
62 if (Constant *C = dyn_cast<Constant>(*OI))
63 if (Visited.insert(C))
64 Worklist.push_back(C);
65
66 // We've collected all the constant (and thus potentially function or
67 // function containing) operands to all of the instructions in the function.
68 // Process them (recursively) collecting every function found.
69 findCallees(Worklist, Visited, Callees, CalleeSet);
70}
71
72LazyCallGraph::Node::Node(LazyCallGraph &G, const Node &OtherN)
73 : G(G), F(OtherN.F), CalleeSet(OtherN.CalleeSet) {
74 // Loop over the other node's callees, adding the Function*s to our list
75 // directly, and recursing to add the Node*s.
76 Callees.reserve(OtherN.Callees.size());
77 for (NodeVectorImplT::iterator OI = OtherN.Callees.begin(),
78 OE = OtherN.Callees.end();
79 OI != OE; ++OI)
80 if (Function *Callee = OI->dyn_cast<Function *>())
81 Callees.push_back(Callee);
82 else
83 Callees.push_back(G.copyInto(*OI->get<Node *>()));
84}
85
Chandler Carruthbf71a342014-02-06 04:37:03 +000086LazyCallGraph::Node::Node(LazyCallGraph &G, Node &&OtherN)
87 : G(G), F(OtherN.F), Callees(std::move(OtherN.Callees)),
88 CalleeSet(std::move(OtherN.CalleeSet)) {
89 // Loop over our Callees. They've been moved from another node, but we need
90 // to move the Node*s to live under our bump ptr allocator.
91 for (NodeVectorImplT::iterator CI = Callees.begin(), CE = Callees.end();
92 CI != CE; ++CI)
93 if (Node *ChildN = CI->dyn_cast<Node *>())
94 *CI = G.moveInto(std::move(*ChildN));
95}
Chandler Carruthbf71a342014-02-06 04:37:03 +000096
97LazyCallGraph::LazyCallGraph(Module &M) : M(M) {
98 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
99 if (!FI->isDeclaration() && !FI->hasLocalLinkage())
100 if (EntryNodeSet.insert(&*FI))
101 EntryNodes.push_back(&*FI);
102
103 // Now add entry nodes for functions reachable via initializers to globals.
104 SmallVector<Constant *, 16> Worklist;
105 SmallPtrSet<Constant *, 16> Visited;
106 for (Module::global_iterator GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI)
107 if (GI->hasInitializer())
108 if (Visited.insert(GI->getInitializer()))
109 Worklist.push_back(GI->getInitializer());
110
111 findCallees(Worklist, Visited, EntryNodes, EntryNodeSet);
112}
113
114LazyCallGraph::LazyCallGraph(const LazyCallGraph &G)
115 : M(G.M), EntryNodeSet(G.EntryNodeSet) {
Chandler Carruthd1ba2ef2014-02-06 05:17:02 +0000116 EntryNodes.reserve(G.EntryNodes.size());
117 for (NodeVectorImplT::const_iterator EI = G.EntryNodes.begin(),
118 EE = G.EntryNodes.end();
Chandler Carruthbf71a342014-02-06 04:37:03 +0000119 EI != EE; ++EI)
120 if (Function *Callee = EI->dyn_cast<Function *>())
121 EntryNodes.push_back(Callee);
122 else
123 EntryNodes.push_back(copyInto(*EI->get<Node *>()));
124}
125
Chandler Carruthbf71a342014-02-06 04:37:03 +0000126// FIXME: This would be crazy simpler if BumpPtrAllocator were movable without
127// invalidating any of the allocated memory. We should make that be the case at
128// some point and delete this.
129LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
130 : M(G.M), EntryNodes(std::move(G.EntryNodes)),
131 EntryNodeSet(std::move(G.EntryNodeSet)) {
Chandler Carruthd1ba2ef2014-02-06 05:17:02 +0000132 // Loop over our EntryNodes. They've been moved from another graph, so we
133 // need to move the Node*s to live under our bump ptr allocator. We can just
134 // do this in-place.
135 for (NodeVectorImplT::iterator EI = EntryNodes.begin(),
136 EE = EntryNodes.end();
Chandler Carruthbf71a342014-02-06 04:37:03 +0000137 EI != EE; ++EI)
138 if (Node *EntryN = EI->dyn_cast<Node *>())
Chandler Carruthd1ba2ef2014-02-06 05:17:02 +0000139 *EI = moveInto(std::move(*EntryN));
Chandler Carruthbf71a342014-02-06 04:37:03 +0000140}
Chandler Carruthbf71a342014-02-06 04:37:03 +0000141
142LazyCallGraph::Node *LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
143 return new (MappedN = BPA.Allocate()) Node(*this, F);
144}
145
146LazyCallGraph::Node *LazyCallGraph::copyInto(const Node &OtherN) {
147 Node *&N = NodeMap[&OtherN.F];
148 if (N)
149 return N;
150
151 return new (N = BPA.Allocate()) Node(*this, OtherN);
152}
153
Chandler Carruthbf71a342014-02-06 04:37:03 +0000154LazyCallGraph::Node *LazyCallGraph::moveInto(Node &&OtherN) {
155 Node *&N = NodeMap[&OtherN.F];
156 if (N)
157 return N;
158
159 return new (N = BPA.Allocate()) Node(*this, std::move(OtherN));
160}
Chandler Carruthbf71a342014-02-06 04:37:03 +0000161
162char LazyCallGraphAnalysis::PassID;
163
164LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
165
166static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N,
167 SmallPtrSetImpl<LazyCallGraph::Node *> &Printed) {
168 // Recurse depth first through the nodes.
169 for (LazyCallGraph::iterator I = N.begin(), E = N.end(); I != E; ++I)
170 if (Printed.insert(*I))
171 printNodes(OS, **I, Printed);
172
173 OS << " Call edges in function: " << N.getFunction().getName() << "\n";
174 for (LazyCallGraph::iterator I = N.begin(), E = N.end(); I != E; ++I)
175 OS << " -> " << I->getFunction().getName() << "\n";
176
177 OS << "\n";
178}
179
180PreservedAnalyses LazyCallGraphPrinterPass::run(Module *M, ModuleAnalysisManager *AM) {
181 LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M);
182
183 OS << "Printing the call graph for module: " << M->getModuleIdentifier() << "\n\n";
184
185 SmallPtrSet<LazyCallGraph::Node *, 16> Printed;
186 for (LazyCallGraph::iterator I = G.begin(), E = G.end(); I != E; ++I)
187 if (Printed.insert(*I))
188 printNodes(OS, **I, Printed);
189
190 return PreservedAnalyses::all();
191}