blob: 867fa587bd950129a5df6154c54896a93e0b3849 [file] [log] [blame]
Nadav Rotema6b91ac2012-11-02 21:48:17 +00001//===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the cost model analysis. It provides a very basic cost
Nadav Rotem99868e42012-12-24 05:51:12 +000011// estimation for LLVM-IR. This analysis uses the services of the codegen
12// to approximate the cost of any IR instruction when lowered to machine
13// instructions. The cost results are unit-less and the cost number represents
14// the throughput of the machine assuming that all loads hit the cache, all
15// branches are predicted, etc. The cost numbers can be added in order to
16// compare two or more transformation alternatives.
Nadav Rotema6b91ac2012-11-02 21:48:17 +000017//
18//===----------------------------------------------------------------------===//
19
Arnold Schwaighofercae87352013-09-17 18:06:50 +000020#include "llvm/ADT/STLExtras.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000021#include "llvm/Analysis/Passes.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000022#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Function.h"
24#include "llvm/IR/Instructions.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000025#include "llvm/Pass.h"
Arnold Schwaighofercae87352013-09-17 18:06:50 +000026#include "llvm/Support/CommandLine.h"
Nadav Rotema6b91ac2012-11-02 21:48:17 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
29using namespace llvm;
Guozhi Wei62d64142017-09-08 22:29:17 +000030
31static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
32 "cost-kind", cl::desc("Target cost kind"),
33 cl::init(TargetTransformInfo::TCK_RecipThroughput),
34 cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput,
35 "throughput", "Reciprocal throughput"),
36 clEnumValN(TargetTransformInfo::TCK_Latency,
37 "latency", "Instruction latency"),
38 clEnumValN(TargetTransformInfo::TCK_CodeSize,
39 "code-size", "Code size")));
Nadav Rotema6b91ac2012-11-02 21:48:17 +000040
Chandler Carruthf1221bd2014-04-22 02:48:03 +000041#define CM_NAME "cost-model"
42#define DEBUG_TYPE CM_NAME
43
Nadav Rotema6b91ac2012-11-02 21:48:17 +000044namespace {
45 class CostModelAnalysis : public FunctionPass {
46
47 public:
48 static char ID; // Class identification, replacement for typeinfo
Craig Topper9f008862014-04-15 04:59:12 +000049 CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
Nadav Rotema6b91ac2012-11-02 21:48:17 +000050 initializeCostModelAnalysisPass(
51 *PassRegistry::getPassRegistry());
52 }
53
54 /// Returns the expected cost of the instruction.
55 /// Returns -1 if the cost is unknown.
56 /// Note, this method does not cache the cost calculation and it
57 /// can be expensive in some cases.
Guozhi Wei62d64142017-09-08 22:29:17 +000058 unsigned getInstructionCost(const Instruction *I) const {
59 return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput);
60 }
Nadav Rotema6b91ac2012-11-02 21:48:17 +000061
62 private:
Craig Toppere9ba7592014-03-05 07:30:04 +000063 void getAnalysisUsage(AnalysisUsage &AU) const override;
64 bool runOnFunction(Function &F) override;
65 void print(raw_ostream &OS, const Module*) const override;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000066
67 /// The function that we analyze.
68 Function *F;
Chandler Carruthcf569a82013-01-05 10:09:33 +000069 /// Target information.
70 const TargetTransformInfo *TTI;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000071 };
72} // End of anonymous namespace
73
74// Register this pass.
75char CostModelAnalysis::ID = 0;
76static const char cm_name[] = "Cost Model Analysis";
77INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
78INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
79
80FunctionPass *llvm::createCostModelAnalysisPass() {
81 return new CostModelAnalysis();
82}
83
84void
85CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.setPreservesAll();
87}
88
89bool
90CostModelAnalysis::runOnFunction(Function &F) {
91 this->F = &F;
Chandler Carruth705b1852015-01-31 03:43:40 +000092 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
Chandler Carruthfdb9c572015-02-01 12:01:35 +000093 TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
Nadav Rotema6b91ac2012-11-02 21:48:17 +000094
95 return false;
96}
97
Nadav Rotema6b91ac2012-11-02 21:48:17 +000098void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
99 if (!F)
100 return;
101
Benjamin Krameraa209152016-06-26 17:27:42 +0000102 for (BasicBlock &B : *F) {
103 for (Instruction &Inst : B) {
Guozhi Wei62d64142017-09-08 22:29:17 +0000104 unsigned Cost = TTI->getInstructionCost(&Inst, CostKind);
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000105 if (Cost != (unsigned)-1)
106 OS << "Cost Model: Found an estimated cost of " << Cost;
107 else
108 OS << "Cost Model: Unknown cost";
109
Benjamin Krameraa209152016-06-26 17:27:42 +0000110 OS << " for instruction: " << Inst << "\n";
Nadav Rotema6b91ac2012-11-02 21:48:17 +0000111 }
112 }
113}