1 //===- CodeMetrics.h - Code cost measurements -------------------*- 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 implements various weight measurements for code, helping
10 // the Inliner and other passes decide whether to duplicate its contents.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_CODEMETRICS_H
15 #define LLVM_ANALYSIS_CODEMETRICS_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/Support/InstructionCost.h"
19 
20 namespace llvm {
21 class AssumptionCache;
22 class BasicBlock;
23 class Loop;
24 class Function;
25 template <class T> class SmallPtrSetImpl;
26 class TargetTransformInfo;
27 class Value;
28 
29 /// Utility to calculate the size and a few similar metrics for a set
30 /// of basic blocks.
31 struct CodeMetrics {
32   /// True if this function contains a call to setjmp or other functions
33   /// with attribute "returns twice" without having the attribute itself.
34   bool exposesReturnsTwice = false;
35 
36   /// True if this function calls itself.
37   bool isRecursive = false;
38 
39   /// True if this function cannot be duplicated.
40   ///
41   /// True if this function contains one or more indirect branches, or it contains
42   /// one or more 'noduplicate' instructions.
43   bool notDuplicatable = false;
44 
45   /// True if this function contains a call to a convergent function.
46   bool convergent = false;
47 
48   /// True if this function calls alloca (in the C sense).
49   bool usesDynamicAlloca = false;
50 
51   /// Code size cost of the analyzed blocks.
52   InstructionCost NumInsts = 0;
53 
54   /// Number of analyzed blocks.
55   unsigned NumBlocks = false;
56 
57   /// Keeps track of basic block code size estimates.
58   DenseMap<const BasicBlock *, InstructionCost> NumBBInsts;
59 
60   /// Keep track of the number of calls to 'big' functions.
61   unsigned NumCalls = false;
62 
63   /// The number of calls to internal functions with a single caller.
64   ///
65   /// These are likely targets for future inlining, likely exposed by
66   /// interleaved devirtualization.
67   unsigned NumInlineCandidates = 0;
68 
69   /// How many instructions produce vector values.
70   ///
71   /// The inliner is more aggressive with inlining vector kernels.
72   unsigned NumVectorInsts = 0;
73 
74   /// How many 'ret' instructions the blocks contain.
75   unsigned NumRets = 0;
76 
77   /// Add information about a block to the current state.
78   void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
79                          const SmallPtrSetImpl<const Value *> &EphValues,
80                          bool PrepareForLTO = false);
81 
82   /// Collect a loop's ephemeral values (those used only by an assume
83   /// or similar intrinsics in the loop).
84   static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
85                                      SmallPtrSetImpl<const Value *> &EphValues);
86 
87   /// Collect a functions's ephemeral values (those used only by an
88   /// assume or similar intrinsics in the function).
89   static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
90                                      SmallPtrSetImpl<const Value *> &EphValues);
91 };
92 
93 }
94 
95 #endif
96