blob: 75290223852265ba7d27c3e996fe5067b28be67c [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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
22namespace llvm {
23class AssumptionCache;
24class BasicBlock;
25class Loop;
26class Function;
27class Instruction;
28class DataLayout;
29class TargetTransformInfo;
30class Value;
31
Andrew Scullcdfcccc2018-10-05 20:58:37 +010032/// Check whether a call will lower to something small.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033///
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.
38bool callIsSmall(ImmutableCallSite CS);
39
Andrew Scullcdfcccc2018-10-05 20:58:37 +010040/// Utility to calculate the size and a few similar metrics for a set
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041/// of basic blocks.
42struct CodeMetrics {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010043 /// True if this function contains a call to setjmp or other functions
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 /// with attribute "returns twice" without having the attribute itself.
45 bool exposesReturnsTwice = false;
46
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 /// True if this function calls itself.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 bool isRecursive = false;
49
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// True if this function cannot be duplicated.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 ///
52 /// True if this function contains one or more indirect branches, or it contains
53 /// one or more 'noduplicate' instructions.
54 bool notDuplicatable = false;
55
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056 /// True if this function contains a call to a convergent function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 bool convergent = false;
58
Andrew Scullcdfcccc2018-10-05 20:58:37 +010059 /// True if this function calls alloca (in the C sense).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 bool usesDynamicAlloca = false;
61
Andrew Scullcdfcccc2018-10-05 20:58:37 +010062 /// Number of instructions in the analyzed blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063 unsigned NumInsts = false;
64
Andrew Scullcdfcccc2018-10-05 20:58:37 +010065 /// Number of analyzed blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 unsigned NumBlocks = false;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Keeps track of basic block code size estimates.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069 DenseMap<const BasicBlock *, unsigned> NumBBInsts;
70
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Keep track of the number of calls to 'big' functions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 unsigned NumCalls = false;
73
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 /// The number of calls to internal functions with a single caller.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 ///
76 /// These are likely targets for future inlining, likely exposed by
77 /// interleaved devirtualization.
78 unsigned NumInlineCandidates = 0;
79
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080 /// How many instructions produce vector values.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081 ///
82 /// The inliner is more aggressive with inlining vector kernels.
83 unsigned NumVectorInsts = 0;
84
Andrew Scullcdfcccc2018-10-05 20:58:37 +010085 /// How many 'ret' instructions the blocks contain.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086 unsigned NumRets = 0;
87
Andrew Scullcdfcccc2018-10-05 20:58:37 +010088 /// Add information about a block to the current state.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
90 const SmallPtrSetImpl<const Value*> &EphValues);
91
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092 /// Collect a loop's ephemeral values (those used only by an assume
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093 /// or similar intrinsics in the loop).
94 static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
95 SmallPtrSetImpl<const Value *> &EphValues);
96
Andrew Scullcdfcccc2018-10-05 20:58:37 +010097 /// Collect a functions's ephemeral values (those used only by an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098 /// assume or similar intrinsics in the function).
99 static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
100 SmallPtrSetImpl<const Value *> &EphValues);
101};
102
103}
104
105#endif