blob: 3e75f0274b285d1328341f0e9cddb12a6b0d0721 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
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 a concrete implementation of profiling information that
11// loads the information from a profile dump file.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/BasicBlock.h"
16#include "llvm/InstrTypes.h"
Daniel Dunbar13021f12009-08-05 15:55:56 +000017#include "llvm/Module.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Pass.h"
19#include "llvm/Analysis/Passes.h"
20#include "llvm/Analysis/ProfileInfo.h"
21#include "llvm/Analysis/ProfileInfoLoader.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Streams.h"
25using namespace llvm;
26
Dan Gohman089efff2008-05-13 00:00:25 +000027static cl::opt<std::string>
28ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
29 cl::value_desc("filename"),
30 cl::desc("Profile file loaded by -profile-loader"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
Dan Gohman089efff2008-05-13 00:00:25 +000032namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
34 std::string Filename;
35 public:
36 static char ID; // Class identification, replacement for typeinfo
Dan Gohman34c280e2007-08-01 15:32:29 +000037 explicit LoaderPass(const std::string &filename = "")
Dan Gohman26f8c272008-09-04 17:05:41 +000038 : ModulePass(&ID), Filename(filename) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 if (filename.empty()) Filename = ProfileInfoFilename;
40 }
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesAll();
44 }
45
46 virtual const char *getPassName() const {
47 return "Profiling information loader";
48 }
49
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 /// run - Load the profile information from the specified file.
51 virtual bool runOnModule(Module &M);
52 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053} // End of anonymous namespace
54
Dan Gohman089efff2008-05-13 00:00:25 +000055char LoaderPass::ID = 0;
56static RegisterPass<LoaderPass>
57X("profile-loader", "Load profile information from llvmprof.out", false, true);
58
59static RegisterAnalysisGroup<ProfileInfo> Y(X);
60
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
62
63/// createProfileLoaderPass - This function returns a Pass that loads the
64/// profiling information for the module from the specified filename, making it
65/// available to the optimizers.
66Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
67 return new LoaderPass(Filename);
68}
69
70bool LoaderPass::runOnModule(Module &M) {
71 ProfileInfoLoader PIL("profile-loader", Filename, M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
Daniel Dunbar4ae20272009-08-08 17:43:09 +000073 EdgeInformation.clear();
Daniel Dunbar13021f12009-08-05 15:55:56 +000074 std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
Daniel Dunbar4ae20272009-08-08 17:43:09 +000075 if (ECs.size() > 0) {
76 unsigned ei = 0;
77 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
78 if (F->isDeclaration()) continue;
79 if (ei < ECs.size())
Daniel Dunbar6d07ef12009-08-08 18:59:03 +000080 EdgeInformation[F][ProfileInfo::getEdge(0, &F->getEntryBlock())] +=
Daniel Dunbar4ae20272009-08-08 17:43:09 +000081 ECs[ei++];
82 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
83 // Okay, we have to add a counter of each outgoing edge. If the
84 // outgoing edge is not critical don't split it, just insert the counter
85 // in the source or destination of the edge.
86 TerminatorInst *TI = BB->getTerminator();
87 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
88 if (ei < ECs.size())
89 EdgeInformation[F][ProfileInfo::getEdge(BB, TI->getSuccessor(s))] +=
90 ECs[ei++];
91 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +000094 if (ei != ECs.size()) {
95 cerr << "WARNING: profile information is inconsistent with "
96 << "the current program!\n";
97 }
Daniel Dunbar23505092009-08-05 21:51:16 +000098 }
Daniel Dunbar4ae20272009-08-08 17:43:09 +000099
100 BlockInformation.clear();
101 std::vector<unsigned> BCs = PIL.getRawBlockCounts();
102 if (BCs.size() > 0) {
103 unsigned bi = 0;
104 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
105 if (F->isDeclaration()) continue;
106 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
107 if (bi < BCs.size())
108 BlockInformation[F][BB] = BCs[bi++];
109 }
110 if (bi != BCs.size()) {
111 cerr << "WARNING: profile information is inconsistent with "
112 << "the current program!\n";
113 }
114 }
115
116 FunctionInformation.clear();
117 std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
118 if (FCs.size() > 0) {
119 unsigned fi = 0;
120 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
121 if (F->isDeclaration()) continue;
122 if (fi < FCs.size())
123 FunctionInformation[F] = FCs[fi++];
124 }
125 if (fi != FCs.size()) {
126 cerr << "WARNING: profile information is inconsistent with "
127 << "the current program!\n";
128 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 }
130
131 return false;
132}