LLVM 19.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"
23#include "llvm/IR/PassManager.h"
25#include "llvm/Pass.h"
29using namespace llvm;
30
32 "cost-kind", cl::desc("Target cost kind"),
35 "throughput", "Reciprocal throughput"),
37 "latency", "Instruction latency"),
39 "code-size", "Code size"),
41 "size-latency", "Code size and latency")));
42
43static cl::opt<bool> TypeBasedIntrinsicCost("type-based-intrinsic-cost",
44 cl::desc("Calculate intrinsics cost based only on argument types"),
45 cl::init(false));
46
47#define CM_NAME "cost-model"
48#define DEBUG_TYPE CM_NAME
49
52 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
53 OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";
54 for (BasicBlock &B : F) {
55 for (Instruction &Inst : B) {
56 // TODO: Use a pass parameter instead of cl::opt CostKind to determine
57 // which cost kind to print.
59 auto *II = dyn_cast<IntrinsicInst>(&Inst);
61 IntrinsicCostAttributes ICA(II->getIntrinsicID(), *II,
64 }
65 else {
67 }
68
69 if (auto CostVal = Cost.getValue())
70 OS << "Cost Model: Found an estimated cost of " << *CostVal;
71 else
72 OS << "Cost Model: Invalid cost";
73
74 OS << " for instruction: " << Inst << "\n";
75 }
76 }
78}
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))
#define F(x, y, z)
Definition: MD5.cpp:55
uint64_t IntrinsicInst * II
This header defines various interfaces for pass management in LLVM.
This pass exposes codegen information to IR-level passes.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:242
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:394
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: CostModel.cpp:50
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:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
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