LLVM API Documentation
00001 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a concrete implementation of profiling information that 00011 // loads the information from a profile dump file. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/BasicBlock.h" 00016 #include "llvm/InstrTypes.h" 00017 #include "llvm/Module.h" 00018 #include "llvm/Pass.h" 00019 #include "llvm/Analysis/Passes.h" 00020 #include "llvm/Analysis/ProfileInfo.h" 00021 #include "llvm/Analysis/ProfileInfoLoader.h" 00022 #include "llvm/Support/CommandLine.h" 00023 #include "llvm/Support/Compiler.h" 00024 #include "llvm/Support/Streams.h" 00025 using namespace llvm; 00026 00027 static cl::opt<std::string> 00028 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"), 00029 cl::value_desc("filename"), 00030 cl::desc("Profile file loaded by -profile-loader")); 00031 00032 namespace { 00033 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo { 00034 std::string Filename; 00035 public: 00036 static char ID; // Class identification, replacement for typeinfo 00037 explicit LoaderPass(const std::string &filename = "") 00038 : ModulePass(&ID), Filename(filename) { 00039 if (filename.empty()) Filename = ProfileInfoFilename; 00040 } 00041 00042 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00043 AU.setPreservesAll(); 00044 } 00045 00046 virtual const char *getPassName() const { 00047 return "Profiling information loader"; 00048 } 00049 00050 /// run - Load the profile information from the specified file. 00051 virtual bool runOnModule(Module &M); 00052 }; 00053 } // End of anonymous namespace 00054 00055 char LoaderPass::ID = 0; 00056 static RegisterPass<LoaderPass> 00057 X("profile-loader", "Load profile information from llvmprof.out", false, true); 00058 00059 static RegisterAnalysisGroup<ProfileInfo> Y(X); 00060 00061 ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); } 00062 00063 /// createProfileLoaderPass - This function returns a Pass that loads the 00064 /// profiling information for the module from the specified filename, making it 00065 /// available to the optimizers. 00066 Pass *llvm::createProfileLoaderPass(const std::string &Filename) { 00067 return new LoaderPass(Filename); 00068 } 00069 00070 bool LoaderPass::runOnModule(Module &M) { 00071 ProfileInfoLoader PIL("profile-loader", Filename, M); 00072 00073 EdgeInformation.clear(); 00074 std::vector<unsigned> ECs = PIL.getRawEdgeCounts(); 00075 if (ECs.size() > 0) { 00076 unsigned ei = 0; 00077 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { 00078 if (F->isDeclaration()) continue; 00079 if (ei < ECs.size()) 00080 EdgeInformation[F][ProfileInfo::getEdge(0, &F->getEntryBlock())] += 00081 ECs[ei++]; 00082 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { 00083 // Okay, we have to add a counter of each outgoing edge. If the 00084 // outgoing edge is not critical don't split it, just insert the counter 00085 // in the source or destination of the edge. 00086 TerminatorInst *TI = BB->getTerminator(); 00087 for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { 00088 if (ei < ECs.size()) 00089 EdgeInformation[F][ProfileInfo::getEdge(BB, TI->getSuccessor(s))] += 00090 ECs[ei++]; 00091 } 00092 } 00093 } 00094 if (ei != ECs.size()) { 00095 cerr << "WARNING: profile information is inconsistent with " 00096 << "the current program!\n"; 00097 } 00098 } 00099 00100 BlockInformation.clear(); 00101 std::vector<unsigned> BCs = PIL.getRawBlockCounts(); 00102 if (BCs.size() > 0) { 00103 unsigned bi = 0; 00104 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { 00105 if (F->isDeclaration()) continue; 00106 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 00107 if (bi < BCs.size()) 00108 BlockInformation[F][BB] = BCs[bi++]; 00109 } 00110 if (bi != BCs.size()) { 00111 cerr << "WARNING: profile information is inconsistent with " 00112 << "the current program!\n"; 00113 } 00114 } 00115 00116 FunctionInformation.clear(); 00117 std::vector<unsigned> FCs = PIL.getRawFunctionCounts(); 00118 if (FCs.size() > 0) { 00119 unsigned fi = 0; 00120 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { 00121 if (F->isDeclaration()) continue; 00122 if (fi < FCs.size()) 00123 FunctionInformation[F] = FCs[fi++]; 00124 } 00125 if (fi != FCs.size()) { 00126 cerr << "WARNING: profile information is inconsistent with " 00127 << "the current program!\n"; 00128 } 00129 } 00130 00131 return false; 00132 }