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