CodeMetrics.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements various weight measurements for code, helping
  15. // the Inliner and other passes decide whether to duplicate its contents.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_CODEMETRICS_H
  19. #define LLVM_ANALYSIS_CODEMETRICS_H
  20. #include "llvm/ADT/DenseMap.h"
  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. /// Utility to calculate the size and a few similar metrics for a set
  30. /// of basic blocks.
  31. struct CodeMetrics {
  32. /// True if this function contains a call to setjmp or other functions
  33. /// with attribute "returns twice" without having the attribute itself.
  34. bool exposesReturnsTwice = false;
  35. /// True if this function calls itself.
  36. bool isRecursive = false;
  37. /// True if this function cannot be duplicated.
  38. ///
  39. /// True if this function contains one or more indirect branches, or it contains
  40. /// one or more 'noduplicate' instructions.
  41. bool notDuplicatable = false;
  42. /// True if this function contains a call to a convergent function.
  43. bool convergent = false;
  44. /// True if this function calls alloca (in the C sense).
  45. bool usesDynamicAlloca = false;
  46. /// Number of instructions in the analyzed blocks.
  47. unsigned NumInsts = false;
  48. /// Number of analyzed blocks.
  49. unsigned NumBlocks = false;
  50. /// Keeps track of basic block code size estimates.
  51. DenseMap<const BasicBlock *, unsigned> NumBBInsts;
  52. /// Keep track of the number of calls to 'big' functions.
  53. unsigned NumCalls = false;
  54. /// The number of calls to internal functions with a single caller.
  55. ///
  56. /// These are likely targets for future inlining, likely exposed by
  57. /// interleaved devirtualization.
  58. unsigned NumInlineCandidates = 0;
  59. /// How many instructions produce vector values.
  60. ///
  61. /// The inliner is more aggressive with inlining vector kernels.
  62. unsigned NumVectorInsts = 0;
  63. /// How many 'ret' instructions the blocks contain.
  64. unsigned NumRets = 0;
  65. /// Add information about a block to the current state.
  66. void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
  67. const SmallPtrSetImpl<const Value *> &EphValues,
  68. bool PrepareForLTO = false);
  69. /// Collect a loop's ephemeral values (those used only by an assume
  70. /// or similar intrinsics in the loop).
  71. static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
  72. SmallPtrSetImpl<const Value *> &EphValues);
  73. /// Collect a functions's ephemeral values (those used only by an
  74. /// assume or similar intrinsics in the function).
  75. static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
  76. SmallPtrSetImpl<const Value *> &EphValues);
  77. };
  78. }
  79. #endif
  80. #ifdef __GNUC__
  81. #pragma GCC diagnostic pop
  82. #endif