clang 22.0.0git
FunctionSummary.h
Go to the documentation of this file.
1//===- FunctionSummary.h - Stores summaries of functions. -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a summary of a function gathered/used by static analysis.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
14#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
15
16#include "clang/AST/Decl.h"
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/SmallBitVector.h"
21#include <cassert>
22#include <deque>
23#include <optional>
24#include <utility>
25
26namespace clang {
27namespace ento {
28
29using SetOfDecls = std::deque<Decl *>;
30using SetOfConstDecls = llvm::DenseSet<const Decl *>;
31
33 class FunctionSummary {
34 public:
35 /// Marks the IDs of the basic blocks visited during the analyzes.
36 llvm::SmallBitVector VisitedBasicBlocks;
37
38 /// Total number of blocks in the function.
39 unsigned TotalBasicBlocks : 30;
40
41 /// True if this function has been checked against the rules for which
42 /// functions may be inlined.
43 unsigned InlineChecked : 1;
44
45 /// True if this function may be inlined.
46 unsigned MayInline : 1;
47
48 /// The number of times the function has been inlined.
49 unsigned TimesInlined : 32;
50
51 /// Running time for syntax-based AST analysis in milliseconds.
52 std::optional<unsigned> SyntaxRunningTime = std::nullopt;
53
54 FunctionSummary()
55 : TotalBasicBlocks(0), InlineChecked(0), MayInline(0),
56 TimesInlined(0) {}
57 };
58
59 using MapTy = llvm::DenseMap<const Decl *, FunctionSummary>;
60 MapTy Map;
61
62public:
63 MapTy::iterator findOrInsertSummary(const Decl *D) {
64 MapTy::iterator I = Map.find(D);
65 if (I != Map.end())
66 return I;
67
68 using KVPair = std::pair<const Decl *, FunctionSummary>;
69
70 I = Map.insert(KVPair(D, FunctionSummary())).first;
71 assert(I != Map.end());
72 return I;
73 }
74
75 FunctionSummary const *findSummary(const Decl *D) const {
76 auto I = Map.find(D);
77 return I == Map.end() ? nullptr : &I->second;
78 }
79
80 void markMayInline(const Decl *D) {
81 MapTy::iterator I = findOrInsertSummary(D);
82 I->second.InlineChecked = 1;
83 I->second.MayInline = 1;
84 }
85
86 void markShouldNotInline(const Decl *D) {
87 MapTy::iterator I = findOrInsertSummary(D);
88 I->second.InlineChecked = 1;
89 I->second.MayInline = 0;
90 }
91
92 std::optional<bool> mayInline(const Decl *D) {
93 MapTy::const_iterator I = Map.find(D);
94 if (I != Map.end() && I->second.InlineChecked)
95 return I->second.MayInline;
96 return std::nullopt;
97 }
98
99 void markVisitedBasicBlock(unsigned ID, const Decl* D, unsigned TotalIDs) {
100 MapTy::iterator I = findOrInsertSummary(D);
101 llvm::SmallBitVector &Blocks = I->second.VisitedBasicBlocks;
102 assert(ID < TotalIDs);
103 if (TotalIDs > Blocks.size()) {
104 Blocks.resize(TotalIDs);
105 I->second.TotalBasicBlocks = TotalIDs;
106 }
107 Blocks.set(ID);
108 }
109
110 unsigned getNumVisitedBasicBlocks(const Decl* D) {
111 MapTy::const_iterator I = Map.find(D);
112 if (I != Map.end())
113 return I->second.VisitedBasicBlocks.count();
114 return 0;
115 }
116
117 unsigned getNumTimesInlined(const Decl* D) {
118 MapTy::const_iterator I = Map.find(D);
119 if (I != Map.end())
120 return I->second.TimesInlined;
121 return 0;
122 }
123
124 void bumpNumTimesInlined(const Decl* D) {
125 MapTy::iterator I = findOrInsertSummary(D);
126 I->second.TimesInlined++;
127 }
128
129 /// Get the percentage of the reachable blocks.
130 unsigned getPercentBlocksReachable(const Decl *D) {
131 MapTy::const_iterator I = Map.find(D);
132 if (I != Map.end())
133 return ((I->second.VisitedBasicBlocks.count() * 100) /
134 I->second.TotalBasicBlocks);
135 return 0;
136 }
137
138 unsigned getTotalNumBasicBlocks();
140};
141
142} // namespace ento
143} // namespace clang
144
145#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_FUNCTIONSUMMARY_H
static UnsignedEPStat SyntaxRunningTime("SyntaxRunningTime")
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
std::optional< bool > mayInline(const Decl *D)
FunctionSummary const * findSummary(const Decl *D) const
void markVisitedBasicBlock(unsigned ID, const Decl *D, unsigned TotalIDs)
unsigned getPercentBlocksReachable(const Decl *D)
Get the percentage of the reachable blocks.
unsigned getNumTimesInlined(const Decl *D)
void markShouldNotInline(const Decl *D)
unsigned getNumVisitedBasicBlocks(const Decl *D)
MapTy::iterator findOrInsertSummary(const Decl *D)
void bumpNumTimesInlined(const Decl *D)
std::deque< Decl * > SetOfDecls
llvm::DenseSet< const Decl * > SetOfConstDecls
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...