blob: 1482b66a3080d9a76050e6c9c882e9535b0c4211 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements various weight measurements for code, helping
10// the Inliner and other passes decide whether to duplicate its contents.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_CODEMETRICS_H
15#define LLVM_ANALYSIS_CODEMETRICS_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/SmallPtrSet.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019
20namespace llvm {
21class AssumptionCache;
22class BasicBlock;
23class Loop;
24class Function;
25class Instruction;
26class DataLayout;
27class TargetTransformInfo;
28class Value;
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// Utility to calculate the size and a few similar metrics for a set
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031/// of basic blocks.
32struct CodeMetrics {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033 /// True if this function contains a call to setjmp or other functions
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034 /// with attribute "returns twice" without having the attribute itself.
35 bool exposesReturnsTwice = false;
36
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037 /// True if this function calls itself.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 bool isRecursive = false;
39
Andrew Scullcdfcccc2018-10-05 20:58:37 +010040 /// True if this function cannot be duplicated.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041 ///
42 /// True if this function contains one or more indirect branches, or it contains
43 /// one or more 'noduplicate' instructions.
44 bool notDuplicatable = false;
45
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046 /// True if this function contains a call to a convergent function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047 bool convergent = false;
48
Andrew Scullcdfcccc2018-10-05 20:58:37 +010049 /// True if this function calls alloca (in the C sense).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 bool usesDynamicAlloca = false;
51
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 /// Number of instructions in the analyzed blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 unsigned NumInsts = false;
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055 /// Number of analyzed blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 unsigned NumBlocks = false;
57
Andrew Scullcdfcccc2018-10-05 20:58:37 +010058 /// Keeps track of basic block code size estimates.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059 DenseMap<const BasicBlock *, unsigned> NumBBInsts;
60
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061 /// Keep track of the number of calls to 'big' functions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062 unsigned NumCalls = false;
63
Andrew Scullcdfcccc2018-10-05 20:58:37 +010064 /// The number of calls to internal functions with a single caller.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065 ///
66 /// These are likely targets for future inlining, likely exposed by
67 /// interleaved devirtualization.
68 unsigned NumInlineCandidates = 0;
69
Andrew Scullcdfcccc2018-10-05 20:58:37 +010070 /// How many instructions produce vector values.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071 ///
72 /// The inliner is more aggressive with inlining vector kernels.
73 unsigned NumVectorInsts = 0;
74
Andrew Scullcdfcccc2018-10-05 20:58:37 +010075 /// How many 'ret' instructions the blocks contain.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010076 unsigned NumRets = 0;
77
Andrew Scullcdfcccc2018-10-05 20:58:37 +010078 /// Add information about a block to the current state.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
80 const SmallPtrSetImpl<const Value*> &EphValues);
81
Andrew Scullcdfcccc2018-10-05 20:58:37 +010082 /// Collect a loop's ephemeral values (those used only by an assume
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083 /// or similar intrinsics in the loop).
84 static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
85 SmallPtrSetImpl<const Value *> &EphValues);
86
Andrew Scullcdfcccc2018-10-05 20:58:37 +010087 /// Collect a functions's ephemeral values (those used only by an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088 /// assume or similar intrinsics in the function).
89 static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
90 SmallPtrSetImpl<const Value *> &EphValues);
91};
92
93}
94
95#endif