LLVM 24.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
20#include "AnalysisOptions.h"
22#include "llvm/IR/Function.h"
24#include "llvm/IR/PassManager.h"
25#include "llvm/Pass.h"
27
28using namespace llvm;
29
30#define CM_NAME "cost-model"
31#define DEBUG_TYPE CM_NAME
32
33static InstructionCost getCost(const AnalysisOptions &Opts, Instruction &Inst,
34 TTI::TargetCostKind CostKind,
36 auto *II = dyn_cast<IntrinsicInst>(&Inst);
37 if (II &&
38 Opts.intrinsic_cost_strategy != IntrinsicCostStrategy::InstructionCost) {
40 II->getIntrinsicID(), *II, InstructionCost::getInvalid(),
41 /*TypeBasedOnly=*/Opts.intrinsic_cost_strategy ==
43 return TTI.getIntrinsicInstrCost(ICA, CostKind);
44 }
45
46 return TTI.getInstructionCost(&Inst, CostKind);
47}
48
51 switch (CostKind) {
55 return TTI::TCK_Latency;
57 return TTI::TCK_CodeSize;
60 default:
61 llvm_unreachable("Unexpected OutputCostKind!");
62 };
63}
64
67 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
68 const AnalysisOptions &Opts = getAnalysisOptions(F);
69 OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";
70 for (BasicBlock &B : F) {
71 for (Instruction &Inst : B) {
72 OS << "Cost Model: ";
73 if (Opts.cost_kind == OutputCostKind::All) {
74 OS << "Found costs of ";
75 InstructionCost RThru =
78 InstructionCost Lat = getCost(Opts, Inst, TTI::TCK_Latency, TTI);
79 InstructionCost SizeLat =
81 if (RThru == CodeSize && RThru == Lat && RThru == SizeLat)
82 OS << RThru;
83 else
84 OS << "RThru:" << RThru << " CodeSize:" << CodeSize << " Lat:" << Lat
85 << " SizeLat:" << SizeLat;
86 OS << " for: " << Inst << "\n";
87 } else {
89 Opts, Inst, OutputCostKindToTargetCostKind(Opts.cost_kind), TTI);
90 if (Cost.isValid())
91 OS << "Found an estimated cost of " << Cost;
92 else
93 OS << "Invalid cost";
94 OS << " for instruction: " << Inst << "\n";
95 }
96 }
97 }
99}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static TTI::TargetCostKind OutputCostKindToTargetCostKind(OutputCostKind CostKind)
Definition CostModel.cpp:50
static InstructionCost getCost(const AnalysisOptions &Opts, Instruction &Inst, TTI::TargetCostKind CostKind, TargetTransformInfo &TTI)
Definition CostModel.cpp:33
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
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition CostModel.cpp:65
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.
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:643
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
TargetTransformInfo TTI
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.