Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
| 21 | class AssumptionCache; |
| 22 | class BasicBlock; |
| 23 | class Loop; |
| 24 | class Function; |
| 25 | class Instruction; |
| 26 | class DataLayout; |
| 27 | class TargetTransformInfo; |
| 28 | class Value; |
| 29 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 30 | /// Utility to calculate the size and a few similar metrics for a set |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | /// of basic blocks. |
| 32 | struct CodeMetrics { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 33 | /// True if this function contains a call to setjmp or other functions |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | /// with attribute "returns twice" without having the attribute itself. |
| 35 | bool exposesReturnsTwice = false; |
| 36 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 37 | /// True if this function calls itself. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | bool isRecursive = false; |
| 39 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | /// True if this function cannot be duplicated. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | /// |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 46 | /// True if this function contains a call to a convergent function. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | bool convergent = false; |
| 48 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 49 | /// True if this function calls alloca (in the C sense). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | bool usesDynamicAlloca = false; |
| 51 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 52 | /// Number of instructions in the analyzed blocks. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 53 | unsigned NumInsts = false; |
| 54 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 55 | /// Number of analyzed blocks. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 56 | unsigned NumBlocks = false; |
| 57 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 58 | /// Keeps track of basic block code size estimates. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | DenseMap<const BasicBlock *, unsigned> NumBBInsts; |
| 60 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 61 | /// Keep track of the number of calls to 'big' functions. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 62 | unsigned NumCalls = false; |
| 63 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 64 | /// The number of calls to internal functions with a single caller. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | /// |
| 66 | /// These are likely targets for future inlining, likely exposed by |
| 67 | /// interleaved devirtualization. |
| 68 | unsigned NumInlineCandidates = 0; |
| 69 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 70 | /// How many instructions produce vector values. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | /// |
| 72 | /// The inliner is more aggressive with inlining vector kernels. |
| 73 | unsigned NumVectorInsts = 0; |
| 74 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 75 | /// How many 'ret' instructions the blocks contain. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | unsigned NumRets = 0; |
| 77 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 78 | /// Add information about a block to the current state. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI, |
| 80 | const SmallPtrSetImpl<const Value*> &EphValues); |
| 81 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 82 | /// Collect a loop's ephemeral values (those used only by an assume |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | /// or similar intrinsics in the loop). |
| 84 | static void collectEphemeralValues(const Loop *L, AssumptionCache *AC, |
| 85 | SmallPtrSetImpl<const Value *> &EphValues); |
| 86 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 87 | /// Collect a functions's ephemeral values (those used only by an |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | /// 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 |