about summary refs log tree commit diff homepage
path: root/lib/Core/CallPathManager.cpp
blob: 03e751080e9068ef43648bbbed00c1b3e17e59e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===-- CallPathManager.cpp -----------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "CallPathManager.h"

#include "klee/Statistics.h"

#include <map>
#include <vector>
#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
#include "llvm/IR/Function.h"
#else
#include "llvm/Function.h"
#endif

#include "llvm/Support/raw_ostream.h"

using namespace llvm;
using namespace klee;

///

CallPathNode::CallPathNode(CallPathNode *_parent, 
                           Instruction *_callSite,
                           Function *_function)
  : parent(_parent),
    callSite(_callSite),
    function(_function),
    count(0) {
}

void CallPathNode::print() {
  llvm::errs() << "  (Function: " << this->function->getName() << ", "
               << "Callsite: " << callSite << ", "
               << "Count: " << this->count << ")";
  if (parent && parent->callSite) {
    llvm::errs() << ";\n";
    parent->print();
  }
  else llvm::errs() << "\n";
}

///

CallPathManager::CallPathManager() : root(0, 0, 0) {
}

CallPathManager::~CallPathManager() {
  for (std::vector<CallPathNode*>::iterator it = paths.begin(),
         ie = paths.end(); it != ie; ++it)
    delete *it;
}

void CallPathManager::getSummaryStatistics(CallSiteSummaryTable &results) {
  results.clear();

  for (std::vector<CallPathNode*>::iterator it = paths.begin(),
         ie = paths.end(); it != ie; ++it)
    (*it)->summaryStatistics = (*it)->statistics;

  // compute summary bottom up, while building result table
  for (std::vector<CallPathNode*>::reverse_iterator it = paths.rbegin(),
         ie = paths.rend(); it != ie; ++it) {
    CallPathNode *cp = *it;
    cp->parent->summaryStatistics += cp->summaryStatistics;

    CallSiteInfo &csi = results[cp->callSite][cp->function];
    csi.count += cp->count;
    csi.statistics += cp->summaryStatistics;
  }
}


CallPathNode *CallPathManager::computeCallPath(CallPathNode *parent, 
                                               Instruction *cs,
                                               Function *f) {
  for (CallPathNode *p=parent; p; p=p->parent)
    if (cs==p->callSite && f==p->function)
      return p;
  
  CallPathNode *cp = new CallPathNode(parent, cs, f);
  paths.push_back(cp);
  return cp;
}

CallPathNode *CallPathManager::getCallPath(CallPathNode *parent, 
                                           Instruction *cs,
                                           Function *f) {
  std::pair<Instruction*,Function*> key(cs, f);
  if (!parent)
    parent = &root;
  
  CallPathNode::children_ty::iterator it = parent->children.find(key);
  if (it==parent->children.end()) {
    CallPathNode *cp = computeCallPath(parent, cs, f);
    parent->children.insert(std::make_pair(key, cp));
    return cp;
  } else {
    return it->second;
  }
}