Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/CostModel.cpp
Line
Count
Source (jump to first uncovered line)
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/ADT/STLExtras.h"
20
#include "llvm/Analysis/Passes.h"
21
#include "llvm/Analysis/TargetTransformInfo.h"
22
#include "llvm/IR/Function.h"
23
#include "llvm/Pass.h"
24
#include "llvm/Support/CommandLine.h"
25
#include "llvm/Support/Debug.h"
26
#include "llvm/Support/raw_ostream.h"
27
using namespace llvm;
28
29
static cl::opt<TargetTransformInfo::TargetCostKind> CostKind(
30
    "cost-kind", cl::desc("Target cost kind"),
31
    cl::init(TargetTransformInfo::TCK_RecipThroughput),
32
    cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput,
33
                          "throughput", "Reciprocal throughput"),
34
               clEnumValN(TargetTransformInfo::TCK_Latency,
35
                          "latency", "Instruction latency"),
36
               clEnumValN(TargetTransformInfo::TCK_CodeSize,
37
                          "code-size", "Code size")));
38
39
#define CM_NAME "cost-model"
40
#define DEBUG_TYPE CM_NAME
41
42
namespace {
43
  class CostModelAnalysis : public FunctionPass {
44
45
  public:
46
    static char ID; // Class identification, replacement for typeinfo
47
760
    CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
48
760
      initializeCostModelAnalysisPass(
49
760
        *PassRegistry::getPassRegistry());
50
760
    }
51
52
    /// Returns the expected cost of the instruction.
53
    /// Returns -1 if the cost is unknown.
54
    /// Note, this method does not cache the cost calculation and it
55
    /// can be expensive in some cases.
56
0
    unsigned getInstructionCost(const Instruction *I) const {
57
0
      return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput);
58
0
    }
59
60
  private:
61
    void getAnalysisUsage(AnalysisUsage &AU) const override;
62
    bool runOnFunction(Function &F) override;
63
    void print(raw_ostream &OS, const Module*) const override;
64
65
    /// The function that we analyze.
66
    Function *F;
67
    /// Target information.
68
    const TargetTransformInfo *TTI;
69
  };
70
}  // End of anonymous namespace
71
72
// Register this pass.
73
char CostModelAnalysis::ID = 0;
74
static const char cm_name[] = "Cost Model Analysis";
75
11.0k
INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
76
11.0k
INITIALIZE_PASS_END  (CostModelAnalysis, CM_NAME, cm_name, false, true)
77
78
0
FunctionPass *llvm::createCostModelAnalysisPass() {
79
0
  return new CostModelAnalysis();
80
0
}
81
82
void
83
760
CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
84
760
  AU.setPreservesAll();
85
760
}
86
87
bool
88
9.93k
CostModelAnalysis::runOnFunction(Function &F) {
89
9.93k
 this->F = &F;
90
9.93k
 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
91
9.93k
 TTI = TTIWP ? &TTIWP->getTTI(F) : 
nullptr0
;
92
9.93k
93
9.93k
 return false;
94
9.93k
}
95
96
9.93k
void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
97
9.93k
  if (!F)
98
0
    return;
99
9.93k
100
9.95k
  
for (BasicBlock &B : *F)9.93k
{
101
53.0k
    for (Instruction &Inst : B) {
102
53.0k
      unsigned Cost = TTI->getInstructionCost(&Inst, CostKind);
103
53.0k
      if (Cost != (unsigned)-1)
104
52.9k
        OS << "Cost Model: Found an estimated cost of " << Cost;
105
67
      else
106
67
        OS << "Cost Model: Unknown cost";
107
53.0k
108
53.0k
      OS << " for instruction: " << Inst << "\n";
109
53.0k
    }
110
9.95k
  }
111
9.93k
}