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 RegisterAnalysis<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: '" << cs.getInstruction()->getName()
62 << "' In '" << cs.getInstruction()->getParent()->getParent()->getName()
67 IndMap[cs].push_back(cs.getCalledFunction());
68 CompleteSites.insert(cs);
73 void CallTargetFinder::print(std::ostream &O, const Module *M) const
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)) {
82 CallSite cs = ii->first;
83 cs.getInstruction()->dump();
84 O << cs.getInstruction()->getParent()->getParent()->getName() << " "
85 << cs.getInstruction()->getName() << " ";
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();
97 bool CallTargetFinder::runOnModule(Module &M) {
102 void CallTargetFinder::getAnalysisUsage(AnalysisUsage &AU) const {
103 AU.setPreservesAll();
104 AU.addRequired<TDDataStructures>();
107 std::vector<Function*>::iterator CallTargetFinder::begin(CallSite cs) {
108 return IndMap[cs].begin();
111 std::vector<Function*>::iterator CallTargetFinder::end(CallSite cs) {
112 return IndMap[cs].end();
115 bool CallTargetFinder::isComplete(CallSite cs) const {
116 return CompleteSites.find(cs) != CompleteSites.end();
119 std::list<CallSite>::iterator CallTargetFinder::cs_begin() {
120 return AllSites.begin();
123 std::list<CallSite>::iterator CallTargetFinder::cs_end() {
124 return AllSites.end();