[LCG] Add support for building persistent and connected SCCs to the
[oota-llvm.git] / lib / Analysis / LazyCallGraph.cpp
1 //===- 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/STLExtras.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"
17
18 using namespace llvm;
19
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();
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 (Value *Op : C->operand_values())
44       if (Visited.insert(cast<Constant>(Op)))
45         Worklist.push_back(cast<Constant>(Op));
46   }
47 }
48
49 LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
50     : G(&G), F(F), DFSNumber(0), LowLink(0) {
51   SmallVector<Constant *, 16> Worklist;
52   SmallPtrSet<Constant *, 16> Visited;
53   // Find all the potential callees in this function. First walk the
54   // instructions and add every operand which is a constant to the worklist.
55   for (BasicBlock &BB : F)
56     for (Instruction &I : BB)
57       for (Value *Op : I.operand_values())
58         if (Constant *C = dyn_cast<Constant>(Op))
59           if (Visited.insert(C))
60             Worklist.push_back(C);
61
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);
66 }
67
68 LazyCallGraph::Node::Node(LazyCallGraph &G, const Node &OtherN)
69     : G(&G), F(OtherN.F), DFSNumber(0), LowLink(0), 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 (const auto &OtherCallee : OtherN.Callees)
74     if (Function *Callee = OtherCallee.dyn_cast<Function *>())
75       Callees.push_back(Callee);
76     else
77       Callees.push_back(G.copyInto(*OtherCallee.get<Node *>()));
78 }
79
80 LazyCallGraph::LazyCallGraph(Module &M) {
81   for (Function &F : M)
82     if (!F.isDeclaration() && !F.hasLocalLinkage())
83       if (EntryNodeSet.insert(&F))
84         EntryNodes.push_back(&F);
85
86   // Now add entry nodes for functions reachable via initializers to globals.
87   SmallVector<Constant *, 16> Worklist;
88   SmallPtrSet<Constant *, 16> Visited;
89   for (GlobalVariable &GV : M.globals())
90     if (GV.hasInitializer())
91       if (Visited.insert(GV.getInitializer()))
92         Worklist.push_back(GV.getInitializer());
93
94   findCallees(Worklist, Visited, EntryNodes, EntryNodeSet);
95
96   for (auto &Entry : EntryNodes)
97     if (Function *F = Entry.dyn_cast<Function *>())
98       SCCEntryNodes.insert(F);
99     else
100       SCCEntryNodes.insert(&Entry.get<Node *>()->getFunction());
101 }
102
103 LazyCallGraph::LazyCallGraph(const LazyCallGraph &G)
104     : EntryNodeSet(G.EntryNodeSet) {
105   EntryNodes.reserve(G.EntryNodes.size());
106   for (const auto &EntryNode : G.EntryNodes)
107     if (Function *Callee = EntryNode.dyn_cast<Function *>())
108       EntryNodes.push_back(Callee);
109     else
110       EntryNodes.push_back(copyInto(*EntryNode.get<Node *>()));
111
112   // Just re-populate the SCCEntryNodes structure so we recompute the SCCs if
113   // needed.
114   for (auto &Entry : EntryNodes)
115     if (Function *F = Entry.dyn_cast<Function *>())
116       SCCEntryNodes.insert(F);
117     else
118       SCCEntryNodes.insert(&Entry.get<Node *>()->getFunction());
119 }
120
121 LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
122     : BPA(std::move(G.BPA)), EntryNodes(std::move(G.EntryNodes)),
123       EntryNodeSet(std::move(G.EntryNodeSet)), SCCBPA(std::move(G.SCCBPA)),
124       SCCMap(std::move(G.SCCMap)), LeafSCCs(std::move(G.LeafSCCs)),
125       DFSStack(std::move(G.DFSStack)),
126       SCCEntryNodes(std::move(G.SCCEntryNodes)) {
127   // Process all nodes updating the graph pointers.
128   SmallVector<Node *, 16> Worklist;
129   for (auto &Entry : EntryNodes)
130     if (Node *EntryN = Entry.dyn_cast<Node *>())
131       Worklist.push_back(EntryN);
132
133   while (!Worklist.empty()) {
134     Node *N = Worklist.pop_back_val();
135     N->G = this;
136     for (auto &Callee : N->Callees)
137       if (Node *CalleeN = Callee.dyn_cast<Node *>())
138         Worklist.push_back(CalleeN);
139   }
140 }
141
142 LazyCallGraph::Node *LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
143   return new (MappedN = BPA.Allocate()) Node(*this, F);
144 }
145
146 LazyCallGraph::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
154 LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() {
155   // When the stack is empty, there are no more SCCs to walk in this graph.
156   if (DFSStack.empty()) {
157     // If we've handled all candidate entry nodes to the SCC forest, we're done.
158     if (SCCEntryNodes.empty())
159       return nullptr;
160
161     Node *N = get(*SCCEntryNodes.pop_back_val());
162     DFSStack.push_back(std::make_pair(N, N->begin()));
163   }
164
165   Node *N = DFSStack.back().first;
166   if (N->DFSNumber == 0) {
167     // This node hasn't been visited before, assign it a DFS number and remove
168     // it from the entry set.
169     N->LowLink = N->DFSNumber = NextDFSNumber++;
170     SCCEntryNodes.remove(&N->getFunction());
171   }
172
173   for (auto I = DFSStack.back().second, E = N->end(); I != E; ++I) {
174     Node *ChildN = *I;
175     if (ChildN->DFSNumber == 0) {
176       // Mark that we should start at this child when next this node is the
177       // top of the stack. We don't start at the next child to ensure this
178       // child's lowlink is reflected.
179       // FIXME: I don't actually think this is required, and we could start
180       // at the next child.
181       DFSStack.back().second = I;
182
183       // Recurse onto this node via a tail call.
184       DFSStack.push_back(std::make_pair(ChildN, ChildN->begin()));
185       return LazyCallGraph::getNextSCCInPostOrder();
186     }
187
188     // Track the lowest link of the childen, if any are still in the stack.
189     if (ChildN->LowLink < N->LowLink && !SCCMap.count(&ChildN->getFunction()))
190       N->LowLink = ChildN->LowLink;
191   }
192
193   // The tail of the stack is the new SCC. Allocate the SCC and pop the stack
194   // into it.
195   SCC *NewSCC = new (SCCBPA.Allocate()) SCC();
196
197   // Because we don't follow the strict Tarjan recursive formulation, walk
198   // from the top of the stack down, propagating the lowest link and stopping
199   // when the DFS number is the lowest link.
200   int LowestLink = N->LowLink;
201   do {
202     Node *SCCN = DFSStack.pop_back_val().first;
203     SCCMap.insert(std::make_pair(&SCCN->getFunction(), NewSCC));
204     NewSCC->Nodes.push_back(SCCN);
205     LowestLink = std::min(LowestLink, SCCN->LowLink);
206     bool Inserted =
207         NewSCC->NodeSet.insert(&SCCN->getFunction());
208     (void)Inserted;
209     assert(Inserted && "Cannot have duplicates in the DFSStack!");
210   } while (!DFSStack.empty() && LowestLink <= DFSStack.back().first->DFSNumber);
211   assert(LowestLink == NewSCC->Nodes.back()->DFSNumber &&
212          "Cannot stop with a DFS number greater than the lowest link!");
213
214   // A final pass over all edges in the SCC (this remains linear as we only
215   // do this once when we build the SCC) to connect it to the parent sets of
216   // its children.
217   bool IsLeafSCC = true;
218   for (Node *SCCN : NewSCC->Nodes)
219     for (Node *SCCChildN : *SCCN) {
220       if (NewSCC->NodeSet.count(&SCCChildN->getFunction()))
221         continue;
222       SCC *ChildSCC = SCCMap.lookup(&SCCChildN->getFunction());
223       assert(ChildSCC &&
224              "Must have all child SCCs processed when building a new SCC!");
225       ChildSCC->ParentSCCs.insert(NewSCC);
226       IsLeafSCC = false;
227     }
228
229   // For the SCCs where we fine no child SCCs, add them to the leaf list.
230   if (IsLeafSCC)
231     LeafSCCs.push_back(NewSCC);
232
233   return NewSCC;
234 }
235
236 char LazyCallGraphAnalysis::PassID;
237
238 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
239
240 static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N,
241                        SmallPtrSetImpl<LazyCallGraph::Node *> &Printed) {
242   // Recurse depth first through the nodes.
243   for (LazyCallGraph::Node *ChildN : N)
244     if (Printed.insert(ChildN))
245       printNodes(OS, *ChildN, Printed);
246
247   OS << "  Call edges in function: " << N.getFunction().getName() << "\n";
248   for (LazyCallGraph::iterator I = N.begin(), E = N.end(); I != E; ++I)
249     OS << "    -> " << I->getFunction().getName() << "\n";
250
251   OS << "\n";
252 }
253
254 static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &SCC) {
255   ptrdiff_t SCCSize = std::distance(SCC.begin(), SCC.end());
256   OS << "  SCC with " << SCCSize << " functions:\n";
257
258   for (LazyCallGraph::Node *N : SCC)
259     OS << "    " << N->getFunction().getName() << "\n";
260
261   OS << "\n";
262 }
263
264 PreservedAnalyses LazyCallGraphPrinterPass::run(Module *M,
265                                                 ModuleAnalysisManager *AM) {
266   LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M);
267
268   OS << "Printing the call graph for module: " << M->getModuleIdentifier()
269      << "\n\n";
270
271   SmallPtrSet<LazyCallGraph::Node *, 16> Printed;
272   for (LazyCallGraph::Node *N : G)
273     if (Printed.insert(N))
274       printNodes(OS, *N, Printed);
275
276   for (LazyCallGraph::SCC *SCC : G.postorder_sccs())
277     printSCC(OS, *SCC);
278
279   return PreservedAnalyses::all();
280
281 }