blob: 323174b9ce35ce0cf6ac7a3c4665b43abe1a446e [file] [log] [blame]
Chris Lattner927fec32004-02-11 06:10:05 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner927fec32004-02-11 06:10:05 +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 Lattner927fec32004-02-11 06:10:05 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements a concrete implementation of profiling information that
11// loads the information from a profile dump file.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner96ab5ca2004-03-08 22:04:08 +000015#include "llvm/BasicBlock.h"
16#include "llvm/InstrTypes.h"
Daniel Dunbaree166382009-08-05 15:55:56 +000017#include "llvm/Module.h"
Chris Lattner927fec32004-02-11 06:10:05 +000018#include "llvm/Pass.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000019#include "llvm/Analysis/Passes.h"
Chris Lattner927fec32004-02-11 06:10:05 +000020#include "llvm/Analysis/ProfileInfo.h"
21#include "llvm/Analysis/ProfileInfoLoader.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000023#include "llvm/Support/Compiler.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000024#include "llvm/Support/Streams.h"
Chris Lattner927fec32004-02-11 06:10:05 +000025using namespace llvm;
26
Dan Gohman844731a2008-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"));
Chris Lattner945871d2004-02-11 18:21:05 +000031
Dan Gohman844731a2008-05-13 00:00:25 +000032namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +000033 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
Chris Lattner927fec32004-02-11 06:10:05 +000034 std::string Filename;
35 public:
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000037 explicit LoaderPass(const std::string &filename = "")
Dan Gohmanae73dc12008-09-04 17:05:41 +000038 : ModulePass(&ID), Filename(filename) {
Chris Lattner945871d2004-02-11 18:21:05 +000039 if (filename.empty()) Filename = ProfileInfoFilename;
40 }
Chris Lattner927fec32004-02-11 06:10:05 +000041
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
50 /// run - Load the profile information from the specified file.
Chris Lattnerb12914b2004-09-20 04:48:05 +000051 virtual bool runOnModule(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000052 };
Chris Lattner927fec32004-02-11 06:10:05 +000053} // End of anonymous namespace
54
Dan Gohman844731a2008-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
Jeff Cohen6e400f72005-01-10 03:56:27 +000061ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
Chris Lattner927fec32004-02-11 06:10:05 +000062
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
Chris Lattnerb12914b2004-09-20 04:48:05 +000070bool LoaderPass::runOnModule(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000071 ProfileInfoLoader PIL("profile-loader", Filename, M);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000072 EdgeCounts.clear();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000073
Daniel Dunbaree166382009-08-05 15:55:56 +000074 std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
Daniel Dunbarc9008c52009-08-05 21:51:16 +000075 std::vector<unsigned> BCs = PIL.getRawBlockCounts();
76 std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
Daniel Dunbaree166382009-08-05 15:55:56 +000077 // Instrument all of the edges...
Daniel Dunbarc9008c52009-08-05 21:51:16 +000078 unsigned ei = 0;
79 unsigned bi = 0;
80 unsigned fi = 0;
81 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
82 if (F->isDeclaration()) continue;
83 if (fi<FCs.size()) FunctionCounts[F] = FCs[fi++];
Daniel Dunbaree166382009-08-05 15:55:56 +000084 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
Daniel Dunbarc9008c52009-08-05 21:51:16 +000085 if (bi<BCs.size()) BlockCounts[BB] = BCs[bi++];
Daniel Dunbaree166382009-08-05 15:55:56 +000086 // Okay, we have to add a counter of each outgoing edge. If the
87 // outgoing edge is not critical don't split it, just insert the counter
88 // in the source or destination of the edge.
89 TerminatorInst *TI = BB->getTerminator();
90 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
Daniel Dunbarc9008c52009-08-05 21:51:16 +000091 if (ei < ECs.size())
92 EdgeCounts[std::make_pair(BB, TI->getSuccessor(s))]+= ECs[ei++];
Chris Lattner96ab5ca2004-03-08 22:04:08 +000093 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +000094 }
Daniel Dunbarc9008c52009-08-05 21:51:16 +000095 }
Daniel Dunbaree166382009-08-05 15:55:56 +000096
Daniel Dunbarc9008c52009-08-05 21:51:16 +000097 if (ei != ECs.size()) {
Daniel Dunbaree166382009-08-05 15:55:56 +000098 cerr << "WARNING: profile information is inconsistent with "
99 << "the current program!\n";
Chris Lattner945871d2004-02-11 18:21:05 +0000100 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +0000101
Chris Lattner927fec32004-02-11 06:10:05 +0000102 return false;
103}