blob: 1b86ec8e01be59579b50fcbb7ec9ca7be7823211 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the abstract ProfileInfo interface, and the default
11// "no profile" implementation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/Passes.h"
16#include "llvm/Analysis/ProfileInfo.h"
17#include "llvm/Pass.h"
18#include "llvm/Support/CFG.h"
19#include "llvm/Support/Compiler.h"
20#include <set>
21using namespace llvm;
22
23// Register the ProfileInfo interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000024static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025char ProfileInfo::ID = 0;
26
27ProfileInfo::~ProfileInfo() {}
28
Daniel Dunbar042521c2009-07-14 06:58:59 +000029unsigned ProfileInfo::getExecutionCount(const BasicBlock *BB) const {
Daniel Dunbar23505092009-08-05 21:51:16 +000030 if (BlockCounts.find(BB) != BlockCounts.end())
31 return BlockCounts.find(BB)->second;
32
Daniel Dunbar042521c2009-07-14 06:58:59 +000033 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034
35 // Are there zero predecessors of this block?
36 if (PI == PE) {
37 // If this is the entry block, look for the Null -> Entry edge.
38 if (BB == &BB->getParent()->getEntryBlock())
39 return getEdgeWeight(0, BB);
40 else
41 return 0; // Otherwise, this is a dead block.
42 }
43
44 // Otherwise, if there are predecessors, the execution count of this block is
45 // the sum of the edge frequencies from the incoming edges. Note that if
46 // there are multiple edges from a predecessor to this block that we don't
47 // want to count its weight multiple times. For this reason, we keep track of
48 // the predecessors we've seen and only count them if we haven't run into them
49 // yet.
50 //
51 // We don't want to create an std::set unless we are dealing with a block that
52 // has a LARGE number of in-edges. Handle the common case of having only a
53 // few in-edges with special code.
54 //
Daniel Dunbar042521c2009-07-14 06:58:59 +000055 const BasicBlock *FirstPred = *PI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 unsigned Count = getEdgeWeight(FirstPred, BB);
57 ++PI;
58 if (PI == PE) return Count; // Quick exit for single predecessor blocks
59
Daniel Dunbar042521c2009-07-14 06:58:59 +000060 const BasicBlock *SecondPred = *PI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
62 ++PI;
63 if (PI == PE) return Count; // Quick exit for two predecessor blocks
64
Daniel Dunbar042521c2009-07-14 06:58:59 +000065 const BasicBlock *ThirdPred = *PI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
67 Count += getEdgeWeight(ThirdPred, BB);
68 ++PI;
69 if (PI == PE) return Count; // Quick exit for three predecessor blocks
70
Daniel Dunbar042521c2009-07-14 06:58:59 +000071 std::set<const BasicBlock*> ProcessedPreds;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 ProcessedPreds.insert(FirstPred);
73 ProcessedPreds.insert(SecondPred);
74 ProcessedPreds.insert(ThirdPred);
75 for (; PI != PE; ++PI)
76 if (ProcessedPreds.insert(*PI).second)
77 Count += getEdgeWeight(*PI, BB);
78 return Count;
79}
80
Daniel Dunbar042521c2009-07-14 06:58:59 +000081unsigned ProfileInfo::getExecutionCount(const Function *F) const {
Daniel Dunbar23505092009-08-05 21:51:16 +000082 if (FunctionCounts.find(F) != FunctionCounts.end())
83 return FunctionCounts.find(F)->second;
84
Daniel Dunbar042521c2009-07-14 06:58:59 +000085 return getExecutionCount(&F->getEntryBlock());
86}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88
89//===----------------------------------------------------------------------===//
90// NoProfile ProfileInfo implementation
91//
92
93namespace {
94 struct VISIBILITY_HIDDEN NoProfileInfo
95 : public ImmutablePass, public ProfileInfo {
96 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000097 NoProfileInfo() : ImmutablePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099} // End of anonymous namespace
100
Dan Gohman089efff2008-05-13 00:00:25 +0000101char NoProfileInfo::ID = 0;
102// Register this pass...
103static RegisterPass<NoProfileInfo>
104X("no-profile", "No Profile Information", false, true);
105
106// Declare that we implement the ProfileInfo interface
107static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }