CodeMetrics.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements various weight measurements for code, helping
  11. // the Inliner and other passes decide whether to duplicate its contents.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ANALYSIS_CODEMETRICS_H
  15. #define LLVM_ANALYSIS_CODEMETRICS_H
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/IR/CallSite.h"
  19. namespace llvm {
  20. class AssumptionCache;
  21. class BasicBlock;
  22. class Loop;
  23. class Function;
  24. class Instruction;
  25. class DataLayout;
  26. class TargetTransformInfo;
  27. class Value;
  28. /// \brief Check whether a call will lower to something small.
  29. ///
  30. /// This tests checks whether this callsite will lower to something
  31. /// significantly cheaper than a traditional call, often a single
  32. /// instruction. Note that if isInstructionFree(CS.getInstruction()) would
  33. /// return true, so will this function.
  34. bool callIsSmall(ImmutableCallSite CS);
  35. /// \brief Utility to calculate the size and a few similar metrics for a set
  36. /// of basic blocks.
  37. struct CodeMetrics {
  38. /// \brief True if this function contains a call to setjmp or other functions
  39. /// with attribute "returns twice" without having the attribute itself.
  40. bool exposesReturnsTwice;
  41. /// \brief True if this function calls itself.
  42. bool isRecursive;
  43. /// \brief True if this function cannot be duplicated.
  44. ///
  45. /// True if this function contains one or more indirect branches, or it contains
  46. /// one or more 'noduplicate' instructions.
  47. bool notDuplicatable;
  48. /// \brief True if this function calls alloca (in the C sense).
  49. bool usesDynamicAlloca;
  50. /// \brief Number of instructions in the analyzed blocks.
  51. unsigned NumInsts;
  52. /// \brief Number of analyzed blocks.
  53. unsigned NumBlocks;
  54. /// \brief Keeps track of basic block code size estimates.
  55. DenseMap<const BasicBlock *, unsigned> NumBBInsts;
  56. /// \brief Keep track of the number of calls to 'big' functions.
  57. unsigned NumCalls;
  58. /// \brief The number of calls to internal functions with a single caller.
  59. ///
  60. /// These are likely targets for future inlining, likely exposed by
  61. /// interleaved devirtualization.
  62. unsigned NumInlineCandidates;
  63. /// \brief How many instructions produce vector values.
  64. ///
  65. /// The inliner is more aggressive with inlining vector kernels.
  66. unsigned NumVectorInsts;
  67. /// \brief How many 'ret' instructions the blocks contain.
  68. unsigned NumRets;
  69. CodeMetrics()
  70. : exposesReturnsTwice(false), isRecursive(false), notDuplicatable(false),
  71. usesDynamicAlloca(false), NumInsts(0), NumBlocks(0), NumCalls(0),
  72. NumInlineCandidates(0), NumVectorInsts(0), NumRets(0) {}
  73. /// \brief Add information about a block to the current state.
  74. void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI,
  75. SmallPtrSetImpl<const Value*> &EphValues);
  76. /// \brief Collect a loop's ephemeral values (those used only by an assume
  77. /// or similar intrinsics in the loop).
  78. static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
  79. SmallPtrSetImpl<const Value *> &EphValues);
  80. /// \brief Collect a functions's ephemeral values (those used only by an
  81. /// assume or similar intrinsics in the function).
  82. static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
  83. SmallPtrSetImpl<const Value *> &EphValues);
  84. };
  85. }
  86. #endif