blob: 30d7d48334df96e304d02f758efad5a268617d3a [file] [log] [blame]
Andrew Lenharth632cd522006-05-29 22:58:38 +00001//=- lib/Analysis/IPA/CallTargets.cpp - Resolve Call Targets --*- C++ -*-=====//
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 pass uses DSA to map targets of all calls, and reports on if it
11// thinks it knows all targets of a given call.
12//
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).
17//
18//===----------------------------------------------------------------------===//
19
20#include "llvm/Module.h"
21#include "llvm/Instructions.h"
22#include "llvm/Analysis/DataStructure/DataStructure.h"
23#include "llvm/Analysis/DataStructure/DSGraph.h"
Andrew Lenharth73a38a92006-06-16 14:33:53 +000024#include "llvm/Analysis/DataStructure/CallTargets.h"
Andrew Lenharth632cd522006-05-29 22:58:38 +000025#include "llvm/ADT/Statistic.h"
26#include <iostream>
27#include "llvm/Constants.h"
28
29using namespace llvm;
30
31namespace {
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");
36
37 RegisterAnalysis<CallTargetFinder> X("calltarget", "Find Call Targets (uses DSA)");
38}
39
40void CallTargetFinder::findIndTargets(Module &M)
41{
42 TDDataStructures* T = &getAnalysis<TDDataStructures>();
43 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
44 if (!I->isExternal())
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()) {
51 IndCall++;
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);
57 ++CompleteInd;
58 }
59 if (N->isComplete() && !IndMap[cs].size()) {
60 ++CompleteEmpty;
61 std::cerr << "Call site empty: '" << cs.getInstruction()->getName()
62 << "' In '" << cs.getInstruction()->getParent()->getParent()->getName()
63 << "'\n";
64 }
65 } else {
66 ++DirCall;
67 IndMap[cs].push_back(cs.getCalledFunction());
68 CompleteSites.insert(cs);
69 }
70 }
71}
72
73void CallTargetFinder::print(std::ostream &O, const Module *M) const
74{
75 return;
76 O << "[* = incomplete] CS: func list\n";
77 for (std::map<CallSite, std::vector<Function*> >::const_iterator ii = IndMap.begin(),
78 ee = IndMap.end(); ii != ee; ++ii) {
79 if (!ii->first.getCalledFunction()) { //only print indirect
80 if (!isComplete(ii->first)) {
81 O << "* ";
82 CallSite cs = ii->first;
83 cs.getInstruction()->dump();
84 O << cs.getInstruction()->getParent()->getParent()->getName() << " "
85 << cs.getInstruction()->getName() << " ";
86 }
87 O << ii->first.getInstruction() << ":";
88 for (std::vector<Function*>::const_iterator i = ii->second.begin(),
89 e = ii->second.end(); i != e; ++i) {
90 O << " " << (*i)->getName();
91 }
92 O << "\n";
93 }
94 }
95}
96
97bool CallTargetFinder::runOnModule(Module &M) {
98 findIndTargets(M);
99 return false;
100}
101
102void CallTargetFinder::getAnalysisUsage(AnalysisUsage &AU) const {
103 AU.setPreservesAll();
104 AU.addRequired<TDDataStructures>();
105}
106
107std::vector<Function*>::iterator CallTargetFinder::begin(CallSite cs) {
108 return IndMap[cs].begin();
109}
110
111std::vector<Function*>::iterator CallTargetFinder::end(CallSite cs) {
112 return IndMap[cs].end();
113}
114
115bool CallTargetFinder::isComplete(CallSite cs) const {
116 return CompleteSites.find(cs) != CompleteSites.end();
117}
118
119std::list<CallSite>::iterator CallTargetFinder::cs_begin() {
120 return AllSites.begin();
121}
122
123std::list<CallSite>::iterator CallTargetFinder::cs_end() {
124 return AllSites.end();
125}