CodeMetrics.h 3.2 KB

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