Add an initial version of the CompleteBUDataStructures class, which is currently
[oota-llvm.git] / lib / Analysis / DataStructure / CompleteBottomUp.cpp
1 //===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the exact same as the bottom-up graphs, but we use take a completed
11 // call graph and inline all indirect callees into their callers graphs, making
12 // the result more useful for things like pool allocation.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/DataStructure.h"
17 #include "llvm/Module.h"
18 #include "llvm/Analysis/DSGraph.h"
19 using namespace llvm;
20
21 namespace {
22   RegisterAnalysis<CompleteBUDataStructures>
23   X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis");
24 }
25
26 using namespace DS;
27
28 // run - Calculate the bottom up data structure graphs for each function in the
29 // program.
30 //
31 bool CompleteBUDataStructures::run(Module &M) {
32   BUDataStructures &BU = getAnalysis<BUDataStructures>();
33   GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
34   GlobalsGraph->setPrintAuxCalls();
35
36   // Our call graph is the same as the BU data structures call graph
37   ActualCallees = BU.getActualCallees();
38
39 #if 1   // REMOVE ME EVENTUALLY
40   // FIXME: TEMPORARY (remove once finalization of indirect call sites in the
41   // globals graph has been implemented in the BU pass)
42   TDDataStructures &TD = getAnalysis<TDDataStructures>();
43
44   // The call graph extractable from the TD pass is _much more complete_ and
45   // trustable than that generated by the BU pass so far.  Until this is fixed,
46   // we hack it like this:
47   for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
48     if (MI->isExternal()) continue;
49     const std::vector<DSCallSite> &CSs = TD.getDSGraph(*MI).getFunctionCalls();
50
51     for (unsigned CSi = 0, e = CSs.size(); CSi != e; ++CSi) {
52       if (CSs[CSi].isIndirectCall()) {
53         Instruction *TheCall = CSs[CSi].getCallSite().getInstruction();
54
55         const std::vector<GlobalValue*> &Callees =
56           CSs[CSi].getCalleeNode()->getGlobals();
57         for (unsigned i = 0, e = Callees.size(); i != e; ++i)
58           if (Function *F = dyn_cast<Function>(Callees[i]))
59             ActualCallees.insert(std::make_pair(TheCall, F));
60       }
61     }
62   }
63 #endif
64
65   // Start by copying all of the BU data structures graphs over, verbatim.
66   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
67     if (!I->isExternal()) {
68       DSInfo[I] = new DSGraph(BU.getDSGraph(*I));
69       DSInfo[I]->setGlobalsGraph(GlobalsGraph);
70       DSInfo[I]->setPrintAuxCalls();
71     }
72
73
74   return false;
75 }