| 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 | |
| 19 | #include "llvm/Analysis/CostModel.h" |
| 20 | #include "AnalysisOptions.h" |
| 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/IntrinsicInst.h" |
| 24 | #include "llvm/IR/PassManager.h" |
| 25 | #include "llvm/Pass.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | #define CM_NAME "cost-model" |
| 31 | #define DEBUG_TYPE CM_NAME |
| 32 | |
| 33 | static InstructionCost getCost(const AnalysisOptions &Opts, Instruction &Inst, |
| 34 | TTI::TargetCostKind CostKind, |
| 35 | TargetTransformInfo &TTI) { |
| 36 | auto *II = dyn_cast<IntrinsicInst>(Val: &Inst); |
| 37 | if (II && |
| 38 | Opts.intrinsic_cost_strategy != IntrinsicCostStrategy::InstructionCost) { |
| 39 | IntrinsicCostAttributes ICA( |
| 40 | II->getIntrinsicID(), *II, InstructionCost::getInvalid(), |
| 41 | /*TypeBasedOnly=*/Opts.intrinsic_cost_strategy == |
| 42 | IntrinsicCostStrategy::TypeBasedIntrinsicCost); |
| 43 | return TTI.getIntrinsicInstrCost(ICA, CostKind); |
| 44 | } |
| 45 | |
| 46 | return TTI.getInstructionCost(U: &Inst, CostKind); |
| 47 | } |
| 48 | |
| 49 | static TTI::TargetCostKind |
| 50 | OutputCostKindToTargetCostKind(OutputCostKind CostKind) { |
| 51 | switch (CostKind) { |
| 52 | case OutputCostKind::RecipThroughput: |
| 53 | return TTI::TCK_RecipThroughput; |
| 54 | case OutputCostKind::Latency: |
| 55 | return TTI::TCK_Latency; |
| 56 | case OutputCostKind::CodeSize: |
| 57 | return TTI::TCK_CodeSize; |
| 58 | case OutputCostKind::SizeAndLatency: |
| 59 | return TTI::TCK_SizeAndLatency; |
| 60 | default: |
| 61 | llvm_unreachable("Unexpected OutputCostKind!" ); |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | PreservedAnalyses CostModelPrinterPass::run(Function &F, |
| 66 | FunctionAnalysisManager &AM) { |
| 67 | auto &TTI = AM.getResult<TargetIRAnalysis>(IR&: 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 = |
| 76 | getCost(Opts, Inst, CostKind: TTI::TCK_RecipThroughput, TTI); |
| 77 | InstructionCost CodeSize = getCost(Opts, Inst, CostKind: TTI::TCK_CodeSize, TTI); |
| 78 | InstructionCost Lat = getCost(Opts, Inst, CostKind: TTI::TCK_Latency, TTI); |
| 79 | InstructionCost SizeLat = |
| 80 | getCost(Opts, Inst, CostKind: TTI::TCK_SizeAndLatency, TTI); |
| 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 { |
| 88 | InstructionCost Cost = getCost( |
| 89 | Opts, Inst, CostKind: OutputCostKindToTargetCostKind(CostKind: 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 | } |
| 98 | return PreservedAnalyses::all(); |
| 99 | } |
| 100 | |