aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Analysis/ProfileInfo.cpp
blob: c8dad662f19330f435d5f950cbaf7a56be40f89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the abstract ProfileInfo interface, and the default
// "no profile" implementation.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/ProfileInfo.h"
#include "llvm/Pass.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Compiler.h"
#include <set>
using namespace llvm;

// Register the ProfileInfo interface, providing a nice name to refer to.
namespace {
  RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
}
char ProfileInfo::ID = 0;

ProfileInfo::~ProfileInfo() {}

unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);

  // Are there zero predecessors of this block?
  if (PI == PE) {
    // If this is the entry block, look for the Null -> Entry edge.
    if (BB == &BB->getParent()->getEntryBlock())
      return getEdgeWeight(0, BB);
    else
      return 0;   // Otherwise, this is a dead block.
  }

  // Otherwise, if there are predecessors, the execution count of this block is
  // the sum of the edge frequencies from the incoming edges.  Note that if
  // there are multiple edges from a predecessor to this block that we don't
  // want to count its weight multiple times.  For this reason, we keep track of
  // the predecessors we've seen and only count them if we haven't run into them
  // yet.
  //
  // We don't want to create an std::set unless we are dealing with a block that
  // has a LARGE number of in-edges.  Handle the common case of having only a
  // few in-edges with special code.
  //
  BasicBlock *FirstPred = *PI;
  unsigned Count = getEdgeWeight(FirstPred, BB);
  ++PI;
  if (PI == PE) return Count;   // Quick exit for single predecessor blocks

  BasicBlock *SecondPred = *PI;
  if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
  ++PI;
  if (PI == PE) return Count;   // Quick exit for two predecessor blocks

  BasicBlock *ThirdPred = *PI;
  if (ThirdPred != FirstPred && ThirdPred != SecondPred)
    Count += getEdgeWeight(ThirdPred, BB);
  ++PI;
  if (PI == PE) return Count;   // Quick exit for three predecessor blocks

  std::set<BasicBlock*> ProcessedPreds;
  ProcessedPreds.insert(FirstPred);
  ProcessedPreds.insert(SecondPred);
  ProcessedPreds.insert(ThirdPred);
  for (; PI != PE; ++PI)
    if (ProcessedPreds.insert(*PI).second)
      Count += getEdgeWeight(*PI, BB);
  return Count;
}



//===----------------------------------------------------------------------===//
//  NoProfile ProfileInfo implementation
//

namespace {
  struct VISIBILITY_HIDDEN NoProfileInfo 
    : public ImmutablePass, public ProfileInfo {
    static char ID; // Class identification, replacement for typeinfo
    NoProfileInfo() : ImmutablePass((intptr_t)&ID) {}
  };

  char NoProfileInfo::ID = 0;
  // Register this pass...
  RegisterPass<NoProfileInfo>
  X("no-profile", "No Profile Information");

  // Declare that we implement the ProfileInfo interface
  RegisterAnalysisGroup<ProfileInfo, true> Y(X);
}  // End of anonymous namespace

ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }