Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/CostModel.cpp
35233 views
//===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the cost model analysis. It provides a very basic cost9// estimation for LLVM-IR. This analysis uses the services of the codegen10// to approximate the cost of any IR instruction when lowered to machine11// instructions. The cost results are unit-less and the cost number represents12// the throughput of the machine assuming that all loads hit the cache, all13// branches are predicted, etc. The cost numbers can be added in order to14// compare two or more transformation alternatives.15//16//===----------------------------------------------------------------------===//1718#include "llvm/Analysis/CostModel.h"19#include "llvm/Analysis/Passes.h"20#include "llvm/Analysis/TargetTransformInfo.h"21#include "llvm/IR/Function.h"22#include "llvm/IR/PassManager.h"23#include "llvm/InitializePasses.h"24#include "llvm/Pass.h"25#include "llvm/Support/CommandLine.h"26#include "llvm/Support/raw_ostream.h"27#include "llvm/IR/IntrinsicInst.h"28using namespace llvm;2930static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(31"cost-kind", cl::desc("Target cost kind"),32cl::init(TargetTransformInfo::TCK_RecipThroughput),33cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput,34"throughput", "Reciprocal throughput"),35clEnumValN(TargetTransformInfo::TCK_Latency,36"latency", "Instruction latency"),37clEnumValN(TargetTransformInfo::TCK_CodeSize,38"code-size", "Code size"),39clEnumValN(TargetTransformInfo::TCK_SizeAndLatency,40"size-latency", "Code size and latency")));4142static cl::opt<bool> TypeBasedIntrinsicCost("type-based-intrinsic-cost",43cl::desc("Calculate intrinsics cost based only on argument types"),44cl::init(false));4546#define CM_NAME "cost-model"47#define DEBUG_TYPE CM_NAME4849PreservedAnalyses CostModelPrinterPass::run(Function &F,50FunctionAnalysisManager &AM) {51auto &TTI = AM.getResult<TargetIRAnalysis>(F);52OS << "Printing analysis 'Cost Model Analysis' for function '" << F.getName() << "':\n";53for (BasicBlock &B : F) {54for (Instruction &Inst : B) {55// TODO: Use a pass parameter instead of cl::opt CostKind to determine56// which cost kind to print.57InstructionCost Cost;58auto *II = dyn_cast<IntrinsicInst>(&Inst);59if (II && TypeBasedIntrinsicCost) {60IntrinsicCostAttributes ICA(II->getIntrinsicID(), *II,61InstructionCost::getInvalid(), true);62Cost = TTI.getIntrinsicInstrCost(ICA, CostKind);63}64else {65Cost = TTI.getInstructionCost(&Inst, CostKind);66}6768if (auto CostVal = Cost.getValue())69OS << "Cost Model: Found an estimated cost of " << *CostVal;70else71OS << "Cost Model: Invalid cost";7273OS << " for instruction: " << Inst << "\n";74}75}76return PreservedAnalyses::all();77}787980