1 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a concrete implementation of profiling information that
11 // loads the information from a profile dump file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/BasicBlock.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/Module.h"
18 #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"
27 static cl::opt<std::string>
28 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
29 cl::value_desc("filename"),
30 cl::desc("Profile file loaded by -profile-loader"));
33 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
36 static char ID; // Class identification, replacement for typeinfo
37 explicit LoaderPass(const std::string &filename = "")
38 : ModulePass(&ID), Filename(filename) {
39 if (filename.empty()) Filename = ProfileInfoFilename;
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 virtual const char *getPassName() const {
47 return "Profiling information loader";
50 /// run - Load the profile information from the specified file.
51 virtual bool runOnModule(Module &M);
53 } // End of anonymous namespace
55 char LoaderPass::ID = 0;
56 static RegisterPass<LoaderPass>
57 X("profile-loader", "Load profile information from llvmprof.out", false, true);
59 static RegisterAnalysisGroup<ProfileInfo> Y(X);
61 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
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.
66 Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
67 return new LoaderPass(Filename);
70 bool LoaderPass::runOnModule(Module &M) {
71 ProfileInfoLoader PIL("profile-loader", Filename, M);
73 EdgeInformation.clear();
74 std::vector<unsigned> ECs = PIL.getRawEdgeCounts();
77 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
78 if (F->isDeclaration()) continue;
80 EdgeInformation[F][ProfileInfo::getEdge(0, &F->getEntryBlock())] +=
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) {
89 EdgeInformation[F][ProfileInfo::getEdge(BB, TI->getSuccessor(s))] +=
94 if (ei != ECs.size()) {
95 cerr << "WARNING: profile information is inconsistent with "
96 << "the current program!\n";
100 BlockInformation.clear();
101 std::vector<unsigned> BCs = PIL.getRawBlockCounts();
102 if (BCs.size() > 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)
108 BlockInformation[F][BB] = BCs[bi++];
110 if (bi != BCs.size()) {
111 cerr << "WARNING: profile information is inconsistent with "
112 << "the current program!\n";
116 FunctionInformation.clear();
117 std::vector<unsigned> FCs = PIL.getRawFunctionCounts();
118 if (FCs.size() > 0) {
120 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
121 if (F->isDeclaration()) continue;
123 FunctionInformation[F] = FCs[fi++];
125 if (fi != FCs.size()) {
126 cerr << "WARNING: profile information is inconsistent with "
127 << "the current program!\n";