711607834921134f68e818bbbd87aaa2c46300ac
[oota-llvm.git] / include / llvm / Analysis / CodeMetrics.h
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
20 namespace llvm {
21   class BasicBlock;
22   class Function;
23   class Instruction;
24   class TargetData;
25   class Value;
26
27   /// \brief Check whether an instruction is likely to be "free" when lowered.
28   bool isInstructionFree(const Instruction *I, const TargetData *TD = 0);
29
30   /// \brief Check whether a call will lower to something small.
31   ///
32   /// This tests checks whether calls to this function will lower to something
33   /// significantly cheaper than a traditional call, often a single
34   /// instruction.
35   bool callIsSmall(const Function *F);
36
37   /// \brief Utility to calculate the size and a few similar metrics for a set
38   /// of basic blocks.
39   struct CodeMetrics {
40     /// \brief True if this function contains a call to setjmp or other functions
41     /// with attribute "returns twice" without having the attribute itself.
42     bool exposesReturnsTwice;
43
44     /// \brief True if this function calls itself.
45     bool isRecursive;
46
47     /// \brief True if this function contains one or more indirect branches.
48     bool containsIndirectBr;
49
50     /// \brief True if this function calls alloca (in the C sense).
51     bool usesDynamicAlloca;
52
53     /// \brief Number of instructions in the analyzed blocks.
54     unsigned NumInsts;
55
56     /// \brief Number of analyzed blocks.
57     unsigned NumBlocks;
58
59     /// \brief Keeps track of basic block code size estimates.
60     DenseMap<const BasicBlock *, unsigned> NumBBInsts;
61
62     /// \brief Keep track of the number of calls to 'big' functions.
63     unsigned NumCalls;
64
65     /// \brief The number of calls to internal functions with a single caller.
66     ///
67     /// These are likely targets for future inlining, likely exposed by
68     /// interleaved devirtualization.
69     unsigned NumInlineCandidates;
70
71     /// \brief How many instructions produce vector values.
72     ///
73     /// The inliner is more aggressive with inlining vector kernels.
74     unsigned NumVectorInsts;
75
76     /// \brief How many 'ret' instructions the blocks contain.
77     unsigned NumRets;
78
79     CodeMetrics() : exposesReturnsTwice(false), isRecursive(false),
80                     containsIndirectBr(false), usesDynamicAlloca(false),
81                     NumInsts(0), NumBlocks(0), NumCalls(0),
82                     NumInlineCandidates(0), NumVectorInsts(0),
83                     NumRets(0) {}
84
85     /// \brief Add information about a block to the current state.
86     void analyzeBasicBlock(const BasicBlock *BB, const TargetData *TD = 0);
87
88     /// \brief Add information about a function to the current state.
89     void analyzeFunction(Function *F, const TargetData *TD = 0);
90   };
91 }
92
93 #endif