FunctionSummary.h revision cb0a5039c243f5b0c178e70f424adac334e5789b
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/DenseSet.h"
20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/ImmutableList.h"
22
23namespace clang {
24namespace ento {
25typedef llvm::ImmutableList<Decl*> SetOfDecls;
26typedef llvm::DenseSet<const Decl*> SetOfConstDecls;
27
28class FunctionSummariesTy {
29  struct FunctionSummary {
30    /// True if this function has reached a max block count while inlined from
31    /// at least one call site.
32    bool MayReachMaxBlockCount;
33
34    /// Total number of blocks in the function.
35    unsigned TotalBasicBlocks;
36
37    /// Marks the IDs of the basic blocks visited during the analyzes.
38    llvm::BitVector VisitedBasicBlocks;
39
40    FunctionSummary() :
41      MayReachMaxBlockCount(false),
42      TotalBasicBlocks(0),
43      VisitedBasicBlocks(0) {}
44  };
45
46  typedef llvm::DenseMap<const Decl*, FunctionSummary*> MapTy;
47  MapTy Map;
48
49public:
50  ~FunctionSummariesTy();
51
52  MapTy::iterator findOrInsertSummary(const Decl *D) {
53    MapTy::iterator I = Map.find(D);
54    if (I != Map.end())
55      return I;
56    FunctionSummary *DS = new FunctionSummary();
57    I = Map.insert(std::pair<const Decl*, FunctionSummary*>(D, DS)).first;
58    assert(I != Map.end());
59    return I;
60  }
61
62  void markReachedMaxBlockCount(const Decl* D) {
63    MapTy::iterator I = findOrInsertSummary(D);
64    I->second->MayReachMaxBlockCount = true;
65  }
66
67  bool hasReachedMaxBlockCount(const Decl* D) {
68  MapTy::const_iterator I = Map.find(D);
69    if (I != Map.end())
70      return I->second->MayReachMaxBlockCount;
71    return false;
72  }
73
74  void markVisitedBasicBlock(unsigned ID, const Decl* D, unsigned TotalIDs) {
75    MapTy::iterator I = findOrInsertSummary(D);
76    llvm::BitVector &Blocks = I->second->VisitedBasicBlocks;
77    assert(ID < TotalIDs);
78    if (TotalIDs > Blocks.size()) {
79      Blocks.resize(TotalIDs);
80      I->second->TotalBasicBlocks = TotalIDs;
81    }
82    Blocks[ID] = true;
83  }
84
85  unsigned getNumVisitedBasicBlocks(const Decl* D) {
86    MapTy::const_iterator I = Map.find(D);
87      if (I != Map.end())
88        return I->second->VisitedBasicBlocks.count();
89    return 0;
90  }
91
92  /// Get the percentage of the reachable blocks.
93  unsigned getPercentBlocksReachable(const Decl *D) {
94    MapTy::const_iterator I = Map.find(D);
95      if (I != Map.end())
96        return ((I->second->VisitedBasicBlocks.count() * 100) /
97                 I->second->TotalBasicBlocks);
98    return 0;
99  }
100
101  unsigned getTotalNumBasicBlocks();
102  unsigned getTotalNumVisitedBasicBlocks();
103
104};
105
106}} // end clang ento namespaces
107
108#endif
109