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