CostModel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//
  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 defines the cost model analysis. It provides a very basic cost
  10. // estimation for LLVM-IR. This analysis uses the services of the codegen
  11. // to approximate the cost of any IR instruction when lowered to machine
  12. // instructions. The cost results are unit-less and the cost number represents
  13. // the throughput of the machine assuming that all loads hit the cache, all
  14. // branches are predicted, etc. The cost numbers can be added in order to
  15. // compare two or more transformation alternatives.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Analysis/CostModel.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/Analysis/Passes.h"
  21. #include "llvm/Analysis/TargetTransformInfo.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/PassManager.h"
  24. #include "llvm/InitializePasses.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. using namespace llvm;
  30. static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
  31. "cost-kind", cl::desc("Target cost kind"),
  32. cl::init(TargetTransformInfo::TCK_RecipThroughput),
  33. cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput,
  34. "throughput", "Reciprocal throughput"),
  35. clEnumValN(TargetTransformInfo::TCK_Latency,
  36. "latency", "Instruction latency"),
  37. clEnumValN(TargetTransformInfo::TCK_CodeSize,
  38. "code-size", "Code size"),
  39. clEnumValN(TargetTransformInfo::TCK_SizeAndLatency,
  40. "size-latency", "Code size and latency")));
  41. #define CM_NAME "cost-model"
  42. #define DEBUG_TYPE CM_NAME
  43. namespace {
  44. class CostModelAnalysis : public FunctionPass {
  45. public:
  46. static char ID; // Class identification, replacement for typeinfo
  47. CostModelAnalysis() : FunctionPass(ID) {
  48. initializeCostModelAnalysisPass(
  49. *PassRegistry::getPassRegistry());
  50. }
  51. /// Returns the expected cost of the instruction.
  52. /// Returns -1 if the cost is unknown.
  53. /// Note, this method does not cache the cost calculation and it
  54. /// can be expensive in some cases.
  55. InstructionCost getInstructionCost(const Instruction *I) const {
  56. return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput);
  57. }
  58. private:
  59. void getAnalysisUsage(AnalysisUsage &AU) const override;
  60. bool runOnFunction(Function &F) override;
  61. void print(raw_ostream &OS, const Module*) const override;
  62. /// The function that we analyze.
  63. Function *F = nullptr;
  64. /// Target information.
  65. const TargetTransformInfo *TTI = nullptr;
  66. };
  67. } // End of anonymous namespace
  68. // Register this pass.
  69. char CostModelAnalysis::ID = 0;
  70. static const char cm_name[] = "Cost Model Analysis";
  71. INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
  72. INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
  73. FunctionPass *llvm::createCostModelAnalysisPass() {
  74. return new CostModelAnalysis();
  75. }
  76. void
  77. CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  78. AU.setPreservesAll();
  79. }
  80. bool
  81. CostModelAnalysis::runOnFunction(Function &F) {
  82. this->F = &F;
  83. auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
  84. TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
  85. return false;
  86. }
  87. void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
  88. if (!F)
  89. return;
  90. for (BasicBlock &B : *F) {
  91. for (Instruction &Inst : B) {
  92. InstructionCost Cost = TTI->getInstructionCost(&Inst, CostKind);
  93. if (auto CostVal = Cost.getValue())
  94. OS << "Cost Model: Found an estimated cost of " << *CostVal;
  95. else
  96. OS << "Cost Model: Invalid cost";
  97. OS << " for instruction: " << Inst << "\n";
  98. }
  99. }
  100. }
  101. PreservedAnalyses CostModelPrinterPass::run(Function &F,
  102. FunctionAnalysisManager &AM) {
  103. auto &TTI = AM.getResult<TargetIRAnalysis>(F);
  104. OS << "Cost Model for function '" << F.getName() << "'\n";
  105. for (BasicBlock &B : F) {
  106. for (Instruction &Inst : B) {
  107. // TODO: Use a pass parameter instead of cl::opt CostKind to determine
  108. // which cost kind to print.
  109. InstructionCost Cost = TTI.getInstructionCost(&Inst, CostKind);
  110. if (auto CostVal = Cost.getValue())
  111. OS << "Cost Model: Found an estimated cost of " << *CostVal;
  112. else
  113. OS << "Cost Model: Invalid cost";
  114. OS << " for instruction: " << Inst << "\n";
  115. }
  116. }
  117. return PreservedAnalyses::all();
  118. }