1 //===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/Analysis/CostModel.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/raw_ostream.h"
28 #include "llvm/IR/IntrinsicInst.h"
31 static cl::opt
<TargetTransformInfo::TargetCostKind
> CostKind(
32 "cost-kind", cl::desc("Target cost kind"),
33 cl::init(TargetTransformInfo::TCK_RecipThroughput
),
34 cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput
,
35 "throughput", "Reciprocal throughput"),
36 clEnumValN(TargetTransformInfo::TCK_Latency
,
37 "latency", "Instruction latency"),
38 clEnumValN(TargetTransformInfo::TCK_CodeSize
,
39 "code-size", "Code size"),
40 clEnumValN(TargetTransformInfo::TCK_SizeAndLatency
,
41 "size-latency", "Code size and latency")));
43 static cl::opt
<bool> TypeBasedIntrinsicCost("type-based-intrinsic-cost",
44 cl::desc("Calculate intrinsics cost based only on argument types"),
47 #define CM_NAME "cost-model"
48 #define DEBUG_TYPE CM_NAME
51 class CostModelAnalysis
: public FunctionPass
{
54 static char ID
; // Class identification, replacement for typeinfo
55 CostModelAnalysis() : FunctionPass(ID
) {
56 initializeCostModelAnalysisPass(
57 *PassRegistry::getPassRegistry());
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
= nullptr;
67 /// Target information.
68 const TargetTransformInfo
*TTI
= nullptr;
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();
83 CostModelAnalysis::getAnalysisUsage(AnalysisUsage
&AU
) const {
88 CostModelAnalysis::runOnFunction(Function
&F
) {
90 auto *TTIWP
= getAnalysisIfAvailable
<TargetTransformInfoWrapperPass
>();
91 TTI
= TTIWP
? &TTIWP
->getTTI(F
) : nullptr;
96 void CostModelAnalysis::print(raw_ostream
&OS
, const Module
*) const {
100 for (BasicBlock
&B
: *F
) {
101 for (Instruction
&Inst
: B
) {
102 InstructionCost Cost
;
103 auto *II
= dyn_cast
<IntrinsicInst
>(&Inst
);
104 if (II
&& TypeBasedIntrinsicCost
) {
105 IntrinsicCostAttributes
ICA(II
->getIntrinsicID(), *II
,
106 InstructionCost::getInvalid(), true);
107 Cost
= TTI
->getIntrinsicInstrCost(ICA
, CostKind
);
110 Cost
= TTI
->getInstructionCost(&Inst
, CostKind
);
113 if (auto CostVal
= Cost
.getValue())
114 OS
<< "Cost Model: Found an estimated cost of " << *CostVal
;
116 OS
<< "Cost Model: Invalid cost";
118 OS
<< " for instruction: " << Inst
<< "\n";
123 PreservedAnalyses
CostModelPrinterPass::run(Function
&F
,
124 FunctionAnalysisManager
&AM
) {
125 auto &TTI
= AM
.getResult
<TargetIRAnalysis
>(F
);
126 OS
<< "Printing analysis 'Cost Model Analysis' for function '" << F
.getName() << "':\n";
127 for (BasicBlock
&B
: F
) {
128 for (Instruction
&Inst
: B
) {
129 // TODO: Use a pass parameter instead of cl::opt CostKind to determine
130 // which cost kind to print.
131 InstructionCost Cost
;
132 auto *II
= dyn_cast
<IntrinsicInst
>(&Inst
);
133 if (II
&& TypeBasedIntrinsicCost
) {
134 IntrinsicCostAttributes
ICA(II
->getIntrinsicID(), *II
,
135 InstructionCost::getInvalid(), true);
136 Cost
= TTI
.getIntrinsicInstrCost(ICA
, CostKind
);
139 Cost
= TTI
.getInstructionCost(&Inst
, CostKind
);
142 if (auto CostVal
= Cost
.getValue())
143 OS
<< "Cost Model: Found an estimated cost of " << *CostVal
;
145 OS
<< "Cost Model: Invalid cost";
147 OS
<< " for instruction: " << Inst
<< "\n";
150 return PreservedAnalyses::all();