1 //===- CodeMetrics.h - Code cost measurements -------------------*- 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 implements various weight measurements for code, helping
11 // the Inliner and other passes decide whether to duplicate its contents.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_CODEMETRICS_H
16 #define LLVM_ANALYSIS_CODEMETRICS_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/IR/CallSite.h"
23 class AssumptionCache;
29 class TargetTransformInfo;
32 /// \brief Check whether a call will lower to something small.
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);
40 /// \brief Utility to calculate the size and a few similar metrics for a set
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 = false;
47 /// \brief True if this function calls itself.
48 bool isRecursive = false;
50 /// \brief True if this function cannot be duplicated.
52 /// True if this function contains one or more indirect branches, or it contains
53 /// one or more 'noduplicate' instructions.
54 bool notDuplicatable = false;
56 /// \brief True if this function contains a call to a convergent function.
57 bool convergent = false;
59 /// \brief True if this function calls alloca (in the C sense).
60 bool usesDynamicAlloca = false;
62 /// \brief Number of instructions in the analyzed blocks.
63 unsigned NumInsts = false;
65 /// \brief Number of analyzed blocks.
66 unsigned NumBlocks = false;
68 /// \brief Keeps track of basic block code size estimates.
69 DenseMap<const BasicBlock *, unsigned> NumBBInsts;
71 /// \brief Keep track of the number of calls to 'big' functions.
72 unsigned NumCalls = false;
74 /// \brief The number of calls to internal functions with a single caller.
76 /// These are likely targets for future inlining, likely exposed by
77 /// interleaved devirtualization.
78 unsigned NumInlineCandidates = 0;
80 /// \brief How many instructions produce vector values.
82 /// The inliner is more aggressive with inlining vector kernels.
83 unsigned NumVectorInsts = 0;
85 /// \brief How many 'ret' instructions the blocks contain.
88 /// \brief Add information about a block to the current state.
89 void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
90 const SmallPtrSetImpl<const Value*> &EphValues);
92 /// \brief Collect a loop's ephemeral values (those used only by an assume
93 /// or similar intrinsics in the loop).
94 static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
95 SmallPtrSetImpl<const Value *> &EphValues);
97 /// \brief Collect a functions's ephemeral values (those used only by an
98 /// assume or similar intrinsics in the function).
99 static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
100 SmallPtrSetImpl<const Value *> &EphValues);