1 //===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===// 2 // 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 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/Support/Compiler.h" 19 #include "llvm/Support/InstructionCost.h" 20 21 namespace llvm { 22 class AssumptionCache; 23 class BasicBlock; 24 class Loop; 25 class Function; 26 template <class T> class SmallPtrSetImpl; 27 class TargetTransformInfo; 28 class Value; 29 30 enum struct ConvergenceKind { None, Controlled, ExtendedLoop, Uncontrolled }; 31 32 /// Utility to calculate the size and a few similar metrics for a set 33 /// of basic blocks. 34 struct CodeMetrics { 35 /// True if this function contains a call to setjmp or other functions 36 /// with attribute "returns twice" without having the attribute itself. 37 bool exposesReturnsTwice = false; 38 39 /// True if this function calls itself. 40 bool isRecursive = false; 41 42 /// True if this function cannot be duplicated. 43 /// 44 /// True if this function contains one or more indirect branches, or it contains 45 /// one or more 'noduplicate' instructions. 46 bool notDuplicatable = false; 47 48 /// The kind of convergence specified in this function. 49 ConvergenceKind Convergence = ConvergenceKind::None; 50 51 /// True if this function calls alloca (in the C sense). 52 bool usesDynamicAlloca = false; 53 54 /// Code size cost of the analyzed blocks. 55 InstructionCost NumInsts = 0; 56 57 /// Number of analyzed blocks. 58 unsigned NumBlocks = false; 59 60 /// Keeps track of basic block code size estimates. 61 DenseMap<const BasicBlock *, InstructionCost> NumBBInsts; 62 63 /// Keep track of the number of calls to 'big' functions. 64 unsigned NumCalls = false; 65 66 /// The number of calls to internal functions with a single caller. 67 /// 68 /// These are likely targets for future inlining, likely exposed by 69 /// interleaved devirtualization. 70 unsigned NumInlineCandidates = 0; 71 72 /// How many instructions produce vector values. 73 /// 74 /// The inliner is more aggressive with inlining vector kernels. 75 unsigned NumVectorInsts = 0; 76 77 /// How many 'ret' instructions the blocks contain. 78 unsigned NumRets = 0; 79 80 /// Add information about a block to the current state. 81 LLVM_ABI void 82 analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI, 83 const SmallPtrSetImpl<const Value *> &EphValues, 84 bool PrepareForLTO = false, const Loop *L = nullptr); 85 86 /// Collect a loop's ephemeral values (those used only by an assume 87 /// or similar intrinsics in the loop). 88 LLVM_ABI static void 89 collectEphemeralValues(const Loop *L, AssumptionCache *AC, 90 SmallPtrSetImpl<const Value *> &EphValues); 91 92 /// Collect a functions's ephemeral values (those used only by an 93 /// assume or similar intrinsics in the function). 94 LLVM_ABI static void 95 collectEphemeralValues(const Function *L, AssumptionCache *AC, 96 SmallPtrSetImpl<const Value *> &EphValues); 97 }; 98 99 } 100 101 #endif 102