[yaml2obj][obj2yaml] - Do not create a symbol table by default.
[llvm-complete.git] / lib / Analysis / CostModel.cpp
blobbf0cdbfd0c8b5a545fbfbb34c0eab303512bd0ee
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.
17 //===----------------------------------------------------------------------===//
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/Pass.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace llvm;
29 static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
30 "cost-kind", cl::desc("Target cost kind"),
31 cl::init(TargetTransformInfo::TCK_RecipThroughput),
32 cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput,
33 "throughput", "Reciprocal throughput"),
34 clEnumValN(TargetTransformInfo::TCK_Latency,
35 "latency", "Instruction latency"),
36 clEnumValN(TargetTransformInfo::TCK_CodeSize,
37 "code-size", "Code size")));
39 #define CM_NAME "cost-model"
40 #define DEBUG_TYPE CM_NAME
42 namespace {
43 class CostModelAnalysis : public FunctionPass {
45 public:
46 static char ID; // Class identification, replacement for typeinfo
47 CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
48 initializeCostModelAnalysisPass(
49 *PassRegistry::getPassRegistry());
52 /// Returns the expected cost of the instruction.
53 /// Returns -1 if the cost is unknown.
54 /// Note, this method does not cache the cost calculation and it
55 /// can be expensive in some cases.
56 unsigned getInstructionCost(const Instruction *I) const {
57 return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput);
60 private:
61 void getAnalysisUsage(AnalysisUsage &AU) const override;
62 bool runOnFunction(Function &F) override;
63 void print(raw_ostream &OS, const Module*) const override;
65 /// The function that we analyze.
66 Function *F;
67 /// Target information.
68 const TargetTransformInfo *TTI;
70 } // End of anonymous namespace
72 // Register this pass.
73 char CostModelAnalysis::ID = 0;
74 static const char cm_name[] = "Cost Model Analysis";
75 INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
76 INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
78 FunctionPass *llvm::createCostModelAnalysisPass() {
79 return new CostModelAnalysis();
82 void
83 CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
84 AU.setPreservesAll();
87 bool
88 CostModelAnalysis::runOnFunction(Function &F) {
89 this->F = &F;
90 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
91 TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
93 return false;
96 void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
97 if (!F)
98 return;
100 for (BasicBlock &B : *F) {
101 for (Instruction &Inst : B) {
102 unsigned Cost = TTI->getInstructionCost(&Inst, CostKind);
103 if (Cost != (unsigned)-1)
104 OS << "Cost Model: Found an estimated cost of " << Cost;
105 else
106 OS << "Cost Model: Unknown cost";
108 OS << " for instruction: " << Inst << "\n";