1 //===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/LazyCallGraph.h"
11 #include "llvm/ADT/SCCIterator.h"
12 #include "llvm/IR/CallSite.h"
13 #include "llvm/IR/InstVisitor.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/PassManager.h"
16 #include "llvm/Support/raw_ostream.h"
20 static 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();
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))
43 for (Value *Op : C->operand_values())
44 if (Visited.insert(cast<Constant>(Op)))
45 Worklist.push_back(cast<Constant>(Op));
49 LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) : G(G), F(F) {
50 SmallVector<Constant *, 16> Worklist;
51 SmallPtrSet<Constant *, 16> Visited;
52 // Find all the potential callees in this function. First walk the
53 // instructions and add every operand which is a constant to the worklist.
54 for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
55 for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
57 for (Value *Op : II->operand_values())
58 if (Constant *C = dyn_cast<Constant>(Op))
59 if (Visited.insert(C))
60 Worklist.push_back(C);
62 // We've collected all the constant (and thus potentially function or
63 // function containing) operands to all of the instructions in the function.
64 // Process them (recursively) collecting every function found.
65 findCallees(Worklist, Visited, Callees, CalleeSet);
68 LazyCallGraph::Node::Node(LazyCallGraph &G, const Node &OtherN)
69 : G(G), F(OtherN.F), CalleeSet(OtherN.CalleeSet) {
70 // Loop over the other node's callees, adding the Function*s to our list
71 // directly, and recursing to add the Node*s.
72 Callees.reserve(OtherN.Callees.size());
73 for (NodeVectorImplT::iterator OI = OtherN.Callees.begin(),
74 OE = OtherN.Callees.end();
76 if (Function *Callee = OI->dyn_cast<Function *>())
77 Callees.push_back(Callee);
79 Callees.push_back(G.copyInto(*OI->get<Node *>()));
82 LazyCallGraph::Node::Node(LazyCallGraph &G, Node &&OtherN)
83 : G(G), F(OtherN.F), Callees(std::move(OtherN.Callees)),
84 CalleeSet(std::move(OtherN.CalleeSet)) {
85 // Loop over our Callees. They've been moved from another node, but we need
86 // to move the Node*s to live under our bump ptr allocator.
87 for (NodeVectorImplT::iterator CI = Callees.begin(), CE = Callees.end();
89 if (Node *ChildN = CI->dyn_cast<Node *>())
90 *CI = G.moveInto(std::move(*ChildN));
93 LazyCallGraph::LazyCallGraph(Module &M) : M(M) {
94 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
95 if (!FI->isDeclaration() && !FI->hasLocalLinkage())
96 if (EntryNodeSet.insert(&*FI))
97 EntryNodes.push_back(&*FI);
99 // Now add entry nodes for functions reachable via initializers to globals.
100 SmallVector<Constant *, 16> Worklist;
101 SmallPtrSet<Constant *, 16> Visited;
102 for (Module::global_iterator GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI)
103 if (GI->hasInitializer())
104 if (Visited.insert(GI->getInitializer()))
105 Worklist.push_back(GI->getInitializer());
107 findCallees(Worklist, Visited, EntryNodes, EntryNodeSet);
110 LazyCallGraph::LazyCallGraph(const LazyCallGraph &G)
111 : M(G.M), EntryNodeSet(G.EntryNodeSet) {
112 EntryNodes.reserve(G.EntryNodes.size());
113 for (NodeVectorImplT::const_iterator EI = G.EntryNodes.begin(),
114 EE = G.EntryNodes.end();
116 if (Function *Callee = EI->dyn_cast<Function *>())
117 EntryNodes.push_back(Callee);
119 EntryNodes.push_back(copyInto(*EI->get<Node *>()));
122 // FIXME: This would be crazy simpler if BumpPtrAllocator were movable without
123 // invalidating any of the allocated memory. We should make that be the case at
124 // some point and delete this.
125 LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
126 : M(G.M), EntryNodes(std::move(G.EntryNodes)),
127 EntryNodeSet(std::move(G.EntryNodeSet)) {
128 // Loop over our EntryNodes. They've been moved from another graph, so we
129 // need to move the Node*s to live under our bump ptr allocator. We can just
131 for (NodeVectorImplT::iterator EI = EntryNodes.begin(),
132 EE = EntryNodes.end();
134 if (Node *EntryN = EI->dyn_cast<Node *>())
135 *EI = moveInto(std::move(*EntryN));
138 LazyCallGraph::Node *LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
139 return new (MappedN = BPA.Allocate()) Node(*this, F);
142 LazyCallGraph::Node *LazyCallGraph::copyInto(const Node &OtherN) {
143 Node *&N = NodeMap[&OtherN.F];
147 return new (N = BPA.Allocate()) Node(*this, OtherN);
150 LazyCallGraph::Node *LazyCallGraph::moveInto(Node &&OtherN) {
151 Node *&N = NodeMap[&OtherN.F];
155 return new (N = BPA.Allocate()) Node(*this, std::move(OtherN));
158 char LazyCallGraphAnalysis::PassID;
160 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
162 static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N,
163 SmallPtrSetImpl<LazyCallGraph::Node *> &Printed) {
164 // Recurse depth first through the nodes.
165 for (LazyCallGraph::iterator I = N.begin(), E = N.end(); I != E; ++I)
166 if (Printed.insert(*I))
167 printNodes(OS, **I, Printed);
169 OS << " Call edges in function: " << N.getFunction().getName() << "\n";
170 for (LazyCallGraph::iterator I = N.begin(), E = N.end(); I != E; ++I)
171 OS << " -> " << I->getFunction().getName() << "\n";
176 PreservedAnalyses LazyCallGraphPrinterPass::run(Module *M, ModuleAnalysisManager *AM) {
177 LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M);
179 OS << "Printing the call graph for module: " << M->getModuleIdentifier() << "\n\n";
181 SmallPtrSet<LazyCallGraph::Node *, 16> Printed;
182 for (LazyCallGraph::iterator I = G.begin(), E = G.end(); I != E; ++I)
183 if (Printed.insert(*I))
184 printNodes(OS, **I, Printed);
186 return PreservedAnalyses::all();