blob: eab24c8ab179d0a4933097164baea00e61212838 [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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018
19namespace llvm {
20class AssumptionCache;
21class BasicBlock;
22class Loop;
23class Function;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020024template <class T> class SmallPtrSetImpl;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025class TargetTransformInfo;
26class Value;
27
Andrew Scullcdfcccc2018-10-05 20:58:37 +010028/// Utility to calculate the size and a few similar metrics for a set
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029/// of basic blocks.
30struct CodeMetrics {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010031 /// True if this function contains a call to setjmp or other functions
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010032 /// with attribute "returns twice" without having the attribute itself.
33 bool exposesReturnsTwice = false;
34
Andrew Scullcdfcccc2018-10-05 20:58:37 +010035 /// True if this function calls itself.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010036 bool isRecursive = false;
37
Andrew Scullcdfcccc2018-10-05 20:58:37 +010038 /// True if this function cannot be duplicated.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010039 ///
40 /// True if this function contains one or more indirect branches, or it contains
41 /// one or more 'noduplicate' instructions.
42 bool notDuplicatable = false;
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044 /// True if this function contains a call to a convergent function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045 bool convergent = false;
46
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 /// True if this function calls alloca (in the C sense).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 bool usesDynamicAlloca = false;
49
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// Number of instructions in the analyzed blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 unsigned NumInsts = false;
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053 /// Number of analyzed blocks.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054 unsigned NumBlocks = false;
55
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056 /// Keeps track of basic block code size estimates.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 DenseMap<const BasicBlock *, unsigned> NumBBInsts;
58
Andrew Scullcdfcccc2018-10-05 20:58:37 +010059 /// Keep track of the number of calls to 'big' functions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 unsigned NumCalls = false;
61
Andrew Scullcdfcccc2018-10-05 20:58:37 +010062 /// The number of calls to internal functions with a single caller.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063 ///
64 /// These are likely targets for future inlining, likely exposed by
65 /// interleaved devirtualization.
66 unsigned NumInlineCandidates = 0;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// How many instructions produce vector values.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069 ///
70 /// The inliner is more aggressive with inlining vector kernels.
71 unsigned NumVectorInsts = 0;
72
Andrew Scullcdfcccc2018-10-05 20:58:37 +010073 /// How many 'ret' instructions the blocks contain.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 unsigned NumRets = 0;
75
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076 /// Add information about a block to the current state.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077 void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
78 const SmallPtrSetImpl<const Value*> &EphValues);
79
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080 /// Collect a loop's ephemeral values (those used only by an assume
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081 /// or similar intrinsics in the loop).
82 static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
83 SmallPtrSetImpl<const Value *> &EphValues);
84
Andrew Scullcdfcccc2018-10-05 20:58:37 +010085 /// Collect a functions's ephemeral values (those used only by an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086 /// assume or similar intrinsics in the function).
87 static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
88 SmallPtrSetImpl<const Value *> &EphValues);
89};
90
91}
92
93#endif