LLVM API Documentation
00001 //===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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/Pass.h" 00018 #include "llvm/Analysis/ProfileInfo.h" 00019 #include "llvm/Analysis/ProfileInfoLoader.h" 00020 #include "llvm/Support/CommandLine.h" 00021 #include <iostream> 00022 00023 using namespace llvm; 00024 00025 namespace { 00026 cl::opt<std::string> 00027 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"), 00028 cl::value_desc("filename"), 00029 cl::desc("Profile file loaded by -profile-loader")); 00030 00031 class LoaderPass : public ModulePass, public ProfileInfo { 00032 std::string Filename; 00033 public: 00034 LoaderPass(const std::string &filename = "") 00035 : Filename(filename) { 00036 if (filename.empty()) Filename = ProfileInfoFilename; 00037 } 00038 00039 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00040 AU.setPreservesAll(); 00041 } 00042 00043 virtual const char *getPassName() const { 00044 return "Profiling information loader"; 00045 } 00046 00047 /// run - Load the profile information from the specified file. 00048 virtual bool runOnModule(Module &M); 00049 }; 00050 00051 RegisterOpt<LoaderPass> 00052 X("profile-loader", "Load profile information from llvmprof.out"); 00053 00054 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y; 00055 } // End of anonymous namespace 00056 00057 00058 /// createProfileLoaderPass - This function returns a Pass that loads the 00059 /// profiling information for the module from the specified filename, making it 00060 /// available to the optimizers. 00061 Pass *llvm::createProfileLoaderPass(const std::string &Filename) { 00062 return new LoaderPass(Filename); 00063 } 00064 00065 bool LoaderPass::runOnModule(Module &M) { 00066 ProfileInfoLoader PIL("profile-loader", Filename, M); 00067 EdgeCounts.clear(); 00068 bool PrintedWarning = false; 00069 00070 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs; 00071 PIL.getEdgeCounts(ECs); 00072 for (unsigned i = 0, e = ECs.size(); i != e; ++i) { 00073 BasicBlock *BB = ECs[i].first.first; 00074 unsigned SuccNum = ECs[i].first.second; 00075 TerminatorInst *TI = BB->getTerminator(); 00076 if (SuccNum >= TI->getNumSuccessors()) { 00077 if (!PrintedWarning) { 00078 std::cerr << "WARNING: profile information is inconsistent with " 00079 << "the current program!\n"; 00080 PrintedWarning = true; 00081 } 00082 } else { 00083 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second; 00084 } 00085 } 00086 00087 return false; 00088 }