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