1 //===- FunctionSummary.h - Stores summaries of functions. -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a summary of a function gathered/used by static analysis.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/SmallBitVector.h"
31 using SetOfDecls = std::deque<Decl *>;
32 using SetOfConstDecls = llvm::DenseSet<const Decl *>;
34 class FunctionSummariesTy {
35 class FunctionSummary {
37 /// Marks the IDs of the basic blocks visited during the analyzes.
38 llvm::SmallBitVector VisitedBasicBlocks;
40 /// Total number of blocks in the function.
41 unsigned TotalBasicBlocks : 30;
43 /// True if this function has been checked against the rules for which
44 /// functions may be inlined.
45 unsigned InlineChecked : 1;
47 /// True if this function may be inlined.
48 unsigned MayInline : 1;
50 /// The number of times the function has been inlined.
51 unsigned TimesInlined : 32;
54 : TotalBasicBlocks(0), InlineChecked(0), MayInline(0),
58 using MapTy = llvm::DenseMap<const Decl *, FunctionSummary>;
62 MapTy::iterator findOrInsertSummary(const Decl *D) {
63 MapTy::iterator I = Map.find(D);
67 using KVPair = std::pair<const Decl *, FunctionSummary>;
69 I = Map.insert(KVPair(D, FunctionSummary())).first;
70 assert(I != Map.end());
74 void markMayInline(const Decl *D) {
75 MapTy::iterator I = findOrInsertSummary(D);
76 I->second.InlineChecked = 1;
77 I->second.MayInline = 1;
80 void markShouldNotInline(const Decl *D) {
81 MapTy::iterator I = findOrInsertSummary(D);
82 I->second.InlineChecked = 1;
83 I->second.MayInline = 0;
86 void markReachedMaxBlockCount(const Decl *D) {
87 markShouldNotInline(D);
90 Optional<bool> mayInline(const Decl *D) {
91 MapTy::const_iterator I = Map.find(D);
92 if (I != Map.end() && I->second.InlineChecked)
93 return I->second.MayInline;
97 void markVisitedBasicBlock(unsigned ID, const Decl* D, unsigned TotalIDs) {
98 MapTy::iterator I = findOrInsertSummary(D);
99 llvm::SmallBitVector &Blocks = I->second.VisitedBasicBlocks;
100 assert(ID < TotalIDs);
101 if (TotalIDs > Blocks.size()) {
102 Blocks.resize(TotalIDs);
103 I->second.TotalBasicBlocks = TotalIDs;
108 unsigned getNumVisitedBasicBlocks(const Decl* D) {
109 MapTy::const_iterator I = Map.find(D);
111 return I->second.VisitedBasicBlocks.count();
115 unsigned getNumTimesInlined(const Decl* D) {
116 MapTy::const_iterator I = Map.find(D);
118 return I->second.TimesInlined;
122 void bumpNumTimesInlined(const Decl* D) {
123 MapTy::iterator I = findOrInsertSummary(D);
124 I->second.TimesInlined++;
127 /// Get the percentage of the reachable blocks.
128 unsigned getPercentBlocksReachable(const Decl *D) {
129 MapTy::const_iterator I = Map.find(D);
131 return ((I->second.VisitedBasicBlocks.count() * 100) /
132 I->second.TotalBasicBlocks);
136 unsigned getTotalNumBasicBlocks();
137 unsigned getTotalNumVisitedBasicBlocks();
143 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H