LLVM 23.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
21#include "llvm/IR/Function.h"
23#include "llvm/IR/PassManager.h"
24#include "llvm/Pass.h"
27
28using namespace llvm;
29
37
39 "cost-kind", cl::desc("Target cost kind"),
42 "Reciprocal throughput"),
44 "Instruction latency"),
45 clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"),
47 "Code size and latency"),
48 clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")));
49
55
57 "intrinsic-cost-strategy",
58 cl::desc("Costing strategy for intrinsic instructions"),
62 "Use TargetTransformInfo::getInstructionCost"),
64 "Use TargetTransformInfo::getIntrinsicInstrCost"),
67 "type-based-intrinsic-cost",
68 "Calculate the intrinsic cost based only on argument types")));
69
70#define CM_NAME "cost-model"
71#define DEBUG_TYPE CM_NAME
72
75 auto *II = dyn_cast<IntrinsicInst>(&Inst);
78 II->getIntrinsicID(), *II, InstructionCost::getInvalid(),
79 /*TypeBasedOnly=*/IntrinsicCost ==
81 return TTI.getIntrinsicInstrCost(ICA, CostKind);
82 }
83
84 return TTI.getInstructionCost(&Inst, CostKind);
85}
86
102
105 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
106 OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";
107 for (BasicBlock &B : F) {
108 for (Instruction &Inst : B) {
109 OS << "Cost Model: ";
111 OS << "Found costs of ";
116 if (RThru == CodeSize && RThru == Lat && RThru == SizeLat)
117 OS << RThru;
118 else
119 OS << "RThru:" << RThru << " CodeSize:" << CodeSize << " Lat:" << Lat
120 << " SizeLat:" << SizeLat;
121 OS << " for: " << Inst << "\n";
122 } else {
125 if (Cost.isValid())
126 OS << "Found an estimated cost of " << Cost.getValue();
127 else
128 OS << "Invalid cost";
129 OS << " for instruction: " << Inst << "\n";
130 }
131 }
132 }
133 return PreservedAnalyses::all();
134}
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:88
static InstructionCost getCost(Instruction &Inst, TTI::TargetCostKind CostKind, TargetTransformInfo &TTI)
Definition CostModel.cpp:73
IntrinsicCostStrategy
Definition CostModel.cpp:50
OutputCostKind
Definition CostModel.cpp:30
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
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.
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.
Definition Types.h:26
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
TargetTransformInfo TTI
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.