FunctionSummary.h revision 6a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3
1//== FunctionSummary.h - Stores summaries of functions. ------------*- C++ -*-//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a summary of a function gathered/used by static analyzes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_GR_FUNCTIONSUMMARY_H
15#define LLVM_CLANG_GR_FUNCTIONSUMMARY_H
16
17#include "clang/AST/Decl.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/BitVector.h"
21
22namespace clang {
23namespace ento {
24typedef llvm::SmallPtrSet<Decl*, 24> SetOfDecls;
25typedef llvm::SmallPtrSet<const Decl*, 24> SetOfConstDecls;
26
27class FunctionSummariesTy {
28  struct FunctionSummary {
29    /// True if this function has reached a max block count while inlined from
30    /// at least one call site.
31    bool MayReachMaxBlockCount;
32
33    /// Total number of blocks in the function.
34    unsigned TotalBasicBlocks;
35
36    /// Marks the IDs of the basic blocks visited during the analyzes.
37    llvm::BitVector VisitedBasicBlocks;
38
39    FunctionSummary() :
40      MayReachMaxBlockCount(false),
41      TotalBasicBlocks(0),
42      VisitedBasicBlocks(0) {}
43  };
44
45  typedef llvm::DenseMap<const Decl*, FunctionSummary*> MapTy;
46  MapTy Map;
47
48public:
49  ~FunctionSummariesTy();
50
51  MapTy::iterator findOrInsertSummary(const Decl *D) {
52    MapTy::iterator I = Map.find(D);
53    if (I != Map.end())
54      return I;
55    FunctionSummary *DS = new FunctionSummary();
56    I = Map.insert(std::pair<const Decl*, FunctionSummary*>(D, DS)).first;
57    assert(I != Map.end());
58    return I;
59  }
60
61  void markReachedMaxBlockCount(const Decl* D) {
62    MapTy::iterator I = findOrInsertSummary(D);
63    I->second->MayReachMaxBlockCount = true;
64  }
65
66  bool hasReachedMaxBlockCount(const Decl* D) {
67  MapTy::const_iterator I = Map.find(D);
68    if (I != Map.end())
69      return I->second->MayReachMaxBlockCount;
70    return false;
71  }
72
73  void markVisitedBasicBlock(unsigned ID, const Decl* D, unsigned TotalIDs) {
74    MapTy::iterator I = findOrInsertSummary(D);
75    llvm::BitVector &Blocks = I->second->VisitedBasicBlocks;
76    assert(ID < TotalIDs);
77    if (TotalIDs > Blocks.size()) {
78      Blocks.resize(TotalIDs);
79      I->second->TotalBasicBlocks = TotalIDs;
80    }
81    Blocks[ID] = true;
82  }
83
84  unsigned getNumVisitedBasicBlocks(const Decl* D) {
85    MapTy::const_iterator I = Map.find(D);
86      if (I != Map.end())
87        return I->second->VisitedBasicBlocks.count();
88    return 0;
89  }
90
91  /// Get the percentage of the reachable blocks.
92  unsigned getPercentBlocksReachable(const Decl *D) {
93    MapTy::const_iterator I = Map.find(D);
94      if (I != Map.end())
95        return ((I->second->VisitedBasicBlocks.count() * 100) /
96                 I->second->TotalBasicBlocks);
97    return 0;
98  }
99
100  unsigned getTotalNumBasicBlocks();
101  unsigned getTotalNumVisitedBasicBlocks();
102
103};
104
105}} // end clang ento namespaces
106
107#endif
108