LLVM 22.0.0git
CostModel.cpp
Go to the documentation of this file.
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
22#include "llvm/IR/Function.h"
24#include "llvm/IR/PassManager.h"
25#include "llvm/Pass.h"
28
29using namespace llvm;
30
38
40 "cost-kind", cl::desc("Target cost kind"),
43 "Reciprocal throughput"),
45 "Instruction latency"),
46 clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"),
48 "Code size and latency"),
49 clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")));
50
56
58 "intrinsic-cost-strategy",
59 cl::desc("Costing strategy for intrinsic instructions"),
63 "Use TargetTransformInfo::getInstructionCost"),
65 "Use TargetTransformInfo::getIntrinsicInstrCost"),
68 "type-based-intrinsic-cost",
69 "Calculate the intrinsic cost based only on argument types")));
70
71#define CM_NAME "cost-model"
72#define DEBUG_TYPE CM_NAME
73
76 TargetLibraryInfo &TLI) {
77 auto *II = dyn_cast<IntrinsicInst>(&Inst);
80 II->getIntrinsicID(), *II, InstructionCost::getInvalid(),
81 /*TypeBasedOnly=*/IntrinsicCost ==
83 &TLI);
84 return TTI.getIntrinsicInstrCost(ICA, CostKind);
85 }
86
87 return TTI.getInstructionCost(&Inst, CostKind);
88}
89
105
108 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
109 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
110 OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";
111 for (BasicBlock &B : F) {
112 for (Instruction &Inst : B) {
113 OS << "Cost Model: ";
115 OS << "Found costs of ";
116 InstructionCost RThru =
119 InstructionCost Lat = getCost(Inst, TTI::TCK_Latency, TTI, TLI);
120 InstructionCost SizeLat =
122 if (RThru == CodeSize && RThru == Lat && RThru == SizeLat)
123 OS << RThru;
124 else
125 OS << "RThru:" << RThru << " CodeSize:" << CodeSize << " Lat:" << Lat
126 << " SizeLat:" << SizeLat;
127 OS << " for: " << Inst << "\n";
128 } else {
131 if (Cost.isValid())
132 OS << "Found an estimated cost of " << Cost.getValue();
133 else
134 OS << "Invalid cost";
135 OS << " for instruction: " << Inst << "\n";
136 }
137 }
138 }
139 return PreservedAnalyses::all();
140}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
static cl::opt< IntrinsicCostStrategy > IntrinsicCost("intrinsic-cost-strategy", cl::desc("Costing strategy for intrinsic instructions"), cl::init(IntrinsicCostStrategy::InstructionCost), cl::values(clEnumValN(IntrinsicCostStrategy::InstructionCost, "instruction-cost", "Use TargetTransformInfo::getInstructionCost"), clEnumValN(IntrinsicCostStrategy::IntrinsicCost, "intrinsic-cost", "Use TargetTransformInfo::getIntrinsicInstrCost"), clEnumValN(IntrinsicCostStrategy::TypeBasedIntrinsicCost, "type-based-intrinsic-cost", "Calculate the intrinsic cost based only on argument types")))
static TTI::TargetCostKind OutputCostKindToTargetCostKind(OutputCostKind CostKind)
Definition CostModel.cpp:91
IntrinsicCostStrategy
Definition CostModel.cpp:51
OutputCostKind
Definition CostModel.cpp:31
static InstructionCost getCost(Instruction &Inst, TTI::TargetCostKind CostKind, TargetTransformInfo &TTI, TargetLibraryInfo &TLI)
Definition CostModel.cpp:74
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:55
uint64_t IntrinsicInst * II
This pass exposes codegen information to IR-level passes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static InstructionCost getInvalid(CostType Val=0)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetCostKind
The kind of cost model.
@ TCK_RecipThroughput
Reciprocal throughput.
@ TCK_CodeSize
Instruction code size.
@ TCK_SizeAndLatency
The weighted sum of size and latency.
@ TCK_Latency
The latency of instruction.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
TargetTransformInfo TTI
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.