Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
Line
Count
Source
1
//===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
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
// Helper methods for identifying profitable indirect call promotion
10
// candidates for an instruction when the indirect-call value profile metadata
11
// is available.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
16
#include "llvm/ADT/STLExtras.h"
17
#include "llvm/Analysis/IndirectCallVisitor.h"
18
#include "llvm/IR/CallSite.h"
19
#include "llvm/IR/InstIterator.h"
20
#include "llvm/IR/InstVisitor.h"
21
#include "llvm/IR/Instructions.h"
22
#include "llvm/IR/IntrinsicInst.h"
23
#include "llvm/ProfileData/InstrProf.h"
24
#include "llvm/Support/Debug.h"
25
#include <string>
26
#include <utility>
27
#include <vector>
28
29
using namespace llvm;
30
31
#define DEBUG_TYPE "pgo-icall-prom-analysis"
32
33
// The percent threshold for the direct-call target (this call site vs the
34
// remaining call count) for it to be considered as the promotion target.
35
static cl::opt<unsigned> ICPRemainingPercentThreshold(
36
    "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
37
    cl::desc("The percentage threshold against remaining unpromoted indirect "
38
             "call count for the promotion"));
39
40
// The percent threshold for the direct-call target (this call site vs the
41
// total call count) for it to be considered as the promotion target.
42
static cl::opt<unsigned>
43
    ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
44
                             cl::Hidden, cl::ZeroOrMore,
45
                             cl::desc("The percentage threshold against total "
46
                                      "count for the promotion"));
47
48
// Set the maximum number of targets to promote for a single indirect-call
49
// callsite.
50
static cl::opt<unsigned>
51
    MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
52
                     cl::desc("Max number of promotions for a single indirect "
53
                              "call callsite"));
54
55
1.66k
ICallPromotionAnalysis::ICallPromotionAnalysis() {
56
1.66k
  ValueDataArray = llvm::make_unique<InstrProfValueData[]>(MaxNumPromotions);
57
1.66k
}
58
59
bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
60
                                                   uint64_t TotalCount,
61
72
                                                   uint64_t RemainingCount) {
62
72
  return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
63
72
         Count * 100 >= ICPTotalPercentThreshold * TotalCount;
64
72
}
65
66
// Indirect-call promotion heuristic. The direct targets are sorted based on
67
// the count. Stop at the first target that is not promoted. Returns the
68
// number of candidates deemed profitable.
69
uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
70
51
    const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
71
51
  ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
72
51
73
51
  LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
74
51
                    << " Num_targets: " << NumVals << "\n");
75
51
76
51
  uint32_t I = 0;
77
51
  uint64_t RemainingCount = TotalCount;
78
119
  for (; I < MaxNumPromotions && 
I < NumVals116
;
I++68
) {
79
72
    uint64_t Count = ValueDataRef[I].Count;
80
72
    assert(Count <= RemainingCount);
81
72
    LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
82
72
                      << "  Target_func: " << ValueDataRef[I].Value << "\n");
83
72
84
72
    if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
85
4
      LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
86
4
      return I;
87
4
    }
88
68
    RemainingCount -= Count;
89
68
  }
90
51
  
return I47
;
91
51
}
92
93
ArrayRef<InstrProfValueData>
94
ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
95
    const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
96
115
    uint32_t &NumCandidates) {
97
115
  bool Res =
98
115
      getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
99
115
                               ValueDataArray.get(), NumVals, TotalCount);
100
115
  if (!Res) {
101
65
    NumCandidates = 0;
102
65
    return ArrayRef<InstrProfValueData>();
103
65
  }
104
50
  NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
105
50
  return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
106
50
}