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