1 //=- lib/Analysis/IPA/CallTargets.cpp - Resolve Call Targets --*- C++ -*-=====//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This pass uses DSA to map targets of all calls, and reports on if it
11 // thinks it knows all targets of a given call.
13 // Loop over all callsites, and lookup the DSNode for that site. Pull the
14 // Functions from the node as callees.
15 // This is essentially a utility pass to simplify later passes that only depend
16 // on call sites and callees to operate (such as a devirtualizer).
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Module.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Analysis/DataStructure/DataStructure.h"
23 #include "llvm/Analysis/DataStructure/DSGraph.h"
24 #include "llvm/Analysis/DataStructure/CallTargets.h"
25 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Constants.h"
32 Statistic<> DirCall("calltarget", "Number of direct calls");
33 Statistic<> IndCall("calltarget", "Number of indirect calls");
34 Statistic<> CompleteInd("calltarget", "Number of complete indirect calls");
35 Statistic<> CompleteEmpty("calltarget", "Number of complete empty calls");
37 RegisterPass<CallTargetFinder> X("calltarget","Find Call Targets (uses DSA)");
40 void CallTargetFinder::findIndTargets(Module &M)
42 TDDataStructures* T = &getAnalysis<TDDataStructures>();
43 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
45 for (Function::iterator F = I->begin(), FE = I->end(); F != FE; ++F)
46 for (BasicBlock::iterator B = F->begin(), BE = F->end(); B != BE; ++B)
47 if (isa<CallInst>(B) || isa<InvokeInst>(B)) {
48 CallSite cs = CallSite::get(B);
49 AllSites.push_back(cs);
50 if (!cs.getCalledFunction()) {
52 DSNode* N = T->getDSGraph(*cs.getCaller())
53 .getNodeForValue(cs.getCalledValue()).getNode();
54 N->addFullFunctionList(IndMap[cs]);
55 if (N->isComplete() && IndMap[cs].size()) {
56 CompleteSites.insert(cs);
59 if (N->isComplete() && !IndMap[cs].size()) {
61 std::cerr << "Call site empty: '"
62 << cs.getInstruction()->getName()
64 << cs.getInstruction()->getParent()->getParent()->getName()
69 IndMap[cs].push_back(cs.getCalledFunction());
70 CompleteSites.insert(cs);
75 void CallTargetFinder::print(std::ostream &O, const Module *M) const
78 O << "[* = incomplete] CS: func list\n";
79 for (std::map<CallSite, std::vector<Function*> >::const_iterator ii =
81 ee = IndMap.end(); ii != ee; ++ii) {
82 if (!ii->first.getCalledFunction()) { //only print indirect
83 if (!isComplete(ii->first)) {
85 CallSite cs = ii->first;
86 cs.getInstruction()->dump();
87 O << cs.getInstruction()->getParent()->getParent()->getName() << " "
88 << cs.getInstruction()->getName() << " ";
90 O << ii->first.getInstruction() << ":";
91 for (std::vector<Function*>::const_iterator i = ii->second.begin(),
92 e = ii->second.end(); i != e; ++i) {
93 O << " " << (*i)->getName();
100 bool CallTargetFinder::runOnModule(Module &M) {
105 void CallTargetFinder::getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.setPreservesAll();
107 AU.addRequired<TDDataStructures>();
110 std::vector<Function*>::iterator CallTargetFinder::begin(CallSite cs) {
111 return IndMap[cs].begin();
114 std::vector<Function*>::iterator CallTargetFinder::end(CallSite cs) {
115 return IndMap[cs].end();
118 bool CallTargetFinder::isComplete(CallSite cs) const {
119 return CompleteSites.find(cs) != CompleteSites.end();
122 std::list<CallSite>::iterator CallTargetFinder::cs_begin() {
123 return AllSites.begin();
126 std::list<CallSite>::iterator CallTargetFinder::cs_end() {
127 return AllSites.end();