CodeMetrics.h 3.4 KB

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