blob: 08a45c62c55b08038e6393ccc7148dcf1852fed1 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattner927fec32004-02-11 06:10:05 +000017#include "llvm/Pass.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000018#include "llvm/Analysis/Passes.h"
Chris Lattner927fec32004-02-11 06:10:05 +000019#include "llvm/Analysis/ProfileInfo.h"
20#include "llvm/Analysis/ProfileInfoLoader.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000022#include "llvm/Support/Streams.h"
Chris Lattner927fec32004-02-11 06:10:05 +000023using namespace llvm;
24
25namespace {
Chris Lattner945871d2004-02-11 18:21:05 +000026 cl::opt<std::string>
27 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
Chris Lattnerd6a556b2004-02-11 19:14:04 +000028 cl::value_desc("filename"),
29 cl::desc("Profile file loaded by -profile-loader"));
Chris Lattner945871d2004-02-11 18:21:05 +000030
Chris Lattnerb12914b2004-09-20 04:48:05 +000031 class LoaderPass : public ModulePass, public ProfileInfo {
Chris Lattner927fec32004-02-11 06:10:05 +000032 std::string Filename;
33 public:
Chris Lattner945871d2004-02-11 18:21:05 +000034 LoaderPass(const std::string &filename = "")
35 : Filename(filename) {
36 if (filename.empty()) Filename = ProfileInfoFilename;
37 }
Chris Lattner927fec32004-02-11 06:10:05 +000038
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesAll();
41 }
42
43 virtual const char *getPassName() const {
44 return "Profiling information loader";
45 }
46
47 /// run - Load the profile information from the specified file.
Chris Lattnerb12914b2004-09-20 04:48:05 +000048 virtual bool runOnModule(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000049 };
Misha Brukman2b37d7c2005-04-21 21:13:18 +000050
Chris Lattner7f8897f2006-08-27 22:42:52 +000051 RegisterPass<LoaderPass>
Chris Lattner3dd965c2006-08-21 17:20:01 +000052 X("profile-loader", "Load profile information from llvmprof.out");
Chris Lattner927fec32004-02-11 06:10:05 +000053
Chris Lattnera5370172006-08-28 00:42:29 +000054 RegisterAnalysisGroup<ProfileInfo> Y(X);
Chris Lattner927fec32004-02-11 06:10:05 +000055} // End of anonymous namespace
56
Jeff Cohen6e400f72005-01-10 03:56:27 +000057ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
Chris Lattner927fec32004-02-11 06:10:05 +000058
59/// createProfileLoaderPass - This function returns a Pass that loads the
60/// profiling information for the module from the specified filename, making it
61/// available to the optimizers.
62Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
63 return new LoaderPass(Filename);
64}
65
Chris Lattnerb12914b2004-09-20 04:48:05 +000066bool LoaderPass::runOnModule(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000067 ProfileInfoLoader PIL("profile-loader", Filename, M);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000068 EdgeCounts.clear();
69 bool PrintedWarning = false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000070
Chris Lattner96ab5ca2004-03-08 22:04:08 +000071 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
72 PIL.getEdgeCounts(ECs);
73 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
74 BasicBlock *BB = ECs[i].first.first;
75 unsigned SuccNum = ECs[i].first.second;
76 TerminatorInst *TI = BB->getTerminator();
77 if (SuccNum >= TI->getNumSuccessors()) {
78 if (!PrintedWarning) {
Bill Wendling6f81b512006-11-28 22:46:12 +000079 llvm_cerr << "WARNING: profile information is inconsistent with "
Chris Lattner96ab5ca2004-03-08 22:04:08 +000080 << "the current program!\n";
81 PrintedWarning = true;
82 }
83 } else {
84 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
85 }
Chris Lattner945871d2004-02-11 18:21:05 +000086 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +000087
Chris Lattner927fec32004-02-11 06:10:05 +000088 return false;
89}