blob: 9e90c9435818bbd0d5c7c0c07d3ecf134c9ba8ea [file] [log] [blame]
Chris Lattner171de652004-02-10 22:11:42 +00001//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner171de652004-02-10 22:11:42 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner171de652004-02-10 22:11:42 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the abstract ProfileInfo interface, and the default
11// "no profile" implementation.
12//
13//===----------------------------------------------------------------------===//
14
Jeff Cohen534927d2005-01-08 22:01:16 +000015#include "llvm/Analysis/Passes.h"
Chris Lattner171de652004-02-10 22:11:42 +000016#include "llvm/Analysis/ProfileInfo.h"
17#include "llvm/Pass.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000018#include "llvm/Support/CFG.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000019#include "llvm/Support/Compiler.h"
Chris Lattner96ab5ca2004-03-08 22:04:08 +000020#include <set>
Chris Lattner171de652004-02-10 22:11:42 +000021using namespace llvm;
22
Chris Lattnerecefc962004-02-11 06:10:18 +000023// Register the ProfileInfo interface, providing a nice name to refer to.
Chris Lattner171de652004-02-10 22:11:42 +000024namespace {
25 RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
26}
Devang Patel19974732007-05-03 01:11:54 +000027char ProfileInfo::ID = 0;
Chris Lattner171de652004-02-10 22:11:42 +000028
29ProfileInfo::~ProfileInfo() {}
30
Chris Lattner96ab5ca2004-03-08 22:04:08 +000031unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
32 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
33
34 // Are there zero predecessors of this block?
35 if (PI == PE) {
36 // If this is the entry block, look for the Null -> Entry edge.
Dan Gohmanecb7a772007-03-22 16:38:57 +000037 if (BB == &BB->getParent()->getEntryBlock())
Chris Lattner96ab5ca2004-03-08 22:04:08 +000038 return getEdgeWeight(0, BB);
39 else
40 return 0; // Otherwise, this is a dead block.
41 }
42
43 // Otherwise, if there are predecessors, the execution count of this block is
44 // the sum of the edge frequencies from the incoming edges. Note that if
45 // there are multiple edges from a predecessor to this block that we don't
46 // want to count its weight multiple times. For this reason, we keep track of
47 // the predecessors we've seen and only count them if we haven't run into them
48 // yet.
49 //
50 // We don't want to create an std::set unless we are dealing with a block that
51 // has a LARGE number of in-edges. Handle the common case of having only a
52 // few in-edges with special code.
53 //
54 BasicBlock *FirstPred = *PI;
55 unsigned Count = getEdgeWeight(FirstPred, BB);
56 ++PI;
57 if (PI == PE) return Count; // Quick exit for single predecessor blocks
58
59 BasicBlock *SecondPred = *PI;
60 if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
61 ++PI;
62 if (PI == PE) return Count; // Quick exit for two predecessor blocks
63
64 BasicBlock *ThirdPred = *PI;
65 if (ThirdPred != FirstPred && ThirdPred != SecondPred)
66 Count += getEdgeWeight(ThirdPred, BB);
67 ++PI;
68 if (PI == PE) return Count; // Quick exit for three predecessor blocks
69
70 std::set<BasicBlock*> ProcessedPreds;
71 ProcessedPreds.insert(FirstPred);
72 ProcessedPreds.insert(SecondPred);
73 ProcessedPreds.insert(ThirdPred);
74 for (; PI != PE; ++PI)
75 if (ProcessedPreds.insert(*PI).second)
76 Count += getEdgeWeight(*PI, BB);
77 return Count;
78}
79
80
Chris Lattner171de652004-02-10 22:11:42 +000081
82//===----------------------------------------------------------------------===//
83// NoProfile ProfileInfo implementation
84//
85
86namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000087 struct VISIBILITY_HIDDEN NoProfileInfo
Devang Patel794fd752007-05-01 21:15:47 +000088 : public ImmutablePass, public ProfileInfo {
Devang Patel19974732007-05-03 01:11:54 +000089 static char ID; // Class identification, replacement for typeinfo
Devang Patel794fd752007-05-01 21:15:47 +000090 NoProfileInfo() : ImmutablePass((intptr_t)&ID) {}
91 };
Misha Brukman2b37d7c2005-04-21 21:13:18 +000092
Devang Patel19974732007-05-03 01:11:54 +000093 char NoProfileInfo::ID = 0;
Chris Lattner171de652004-02-10 22:11:42 +000094 // Register this pass...
Chris Lattner7f8897f2006-08-27 22:42:52 +000095 RegisterPass<NoProfileInfo>
Chris Lattner171de652004-02-10 22:11:42 +000096 X("no-profile", "No Profile Information");
97
Chris Lattnerecefc962004-02-11 06:10:18 +000098 // Declare that we implement the ProfileInfo interface
Chris Lattnera5370172006-08-28 00:42:29 +000099 RegisterAnalysisGroup<ProfileInfo, true> Y(X);
Chris Lattner171de652004-02-10 22:11:42 +0000100} // End of anonymous namespace
Jeff Cohen534927d2005-01-08 22:01:16 +0000101
102ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }