LLVM 20.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"
27using namespace llvm;
28
30 "cost-kind", cl::desc("Target cost kind"),
33 "throughput", "Reciprocal throughput"),
35 "latency", "Instruction latency"),
37 "code-size", "Code size"),
39 "size-latency", "Code size and latency")));
40
41static cl::opt<bool> TypeBasedIntrinsicCost("type-based-intrinsic-cost",
42 cl::desc("Calculate intrinsics cost based only on argument types"),
43 cl::init(false));
44
45#define CM_NAME "cost-model"
46#define DEBUG_TYPE CM_NAME
47
50 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
51 OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";
52 for (BasicBlock &B : F) {
53 for (Instruction &Inst : B) {
54 // TODO: Use a pass parameter instead of cl::opt CostKind to determine
55 // which cost kind to print.
57 auto *II = dyn_cast<IntrinsicInst>(&Inst);
59 IntrinsicCostAttributes ICA(II->getIntrinsicID(), *II,
62 }
63 else {
65 }
66
67 if (auto CostVal = Cost.getValue())
68 OS << "Cost Model: Found an estimated cost of " << *CostVal;
69 else
70 OS << "Cost Model: Invalid cost";
71
72 OS << " for instruction: " << Inst << "\n";
73 }
74 }
76}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:686
static cl::opt< TargetTransformInfo::TargetCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(TargetTransformInfo::TCK_RecipThroughput), cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(TargetTransformInfo::TCK_Latency, "latency", "Instruction latency"), clEnumValN(TargetTransformInfo::TCK_CodeSize, "code-size", "Code size"), clEnumValN(TargetTransformInfo::TCK_SizeAndLatency, "size-latency", "Code size and latency")))
static cl::opt< bool > TypeBasedIntrinsicCost("type-based-intrinsic-cost", cl::desc("Calculate intrinsics cost based only on argument types"), cl::init(false))
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.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: CostModel.cpp:48
static InstructionCost getInvalid(CostType Val=0)
std::optional< CostType > getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind) const
@ TCK_RecipThroughput
Reciprocal throughput.
@ TCK_CodeSize
Instruction code size.
@ TCK_SizeAndLatency
The weighted sum of size and latency.
@ TCK_Latency
The latency of instruction.
InstructionCost getInstructionCost(const User *U, ArrayRef< const Value * > Operands, TargetCostKind CostKind) const
Estimate the cost of a given IR user when lowered.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:711
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
InstructionCost Cost