blob: e18625f8e76514ee814b2299ecdec06363f44dd0 [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"
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"
Reid Spencerd7d83db2007-02-05 23:42:17 +000022#include "llvm/Support/Compiler.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000023#include "llvm/Support/Streams.h"
Chris Lattner927fec32004-02-11 06:10:05 +000024using namespace llvm;
25
26namespace {
Chris Lattner945871d2004-02-11 18:21:05 +000027 cl::opt<std::string>
28 ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
Chris Lattnerd6a556b2004-02-11 19:14:04 +000029 cl::value_desc("filename"),
30 cl::desc("Profile file loaded by -profile-loader"));
Chris Lattner945871d2004-02-11 18:21:05 +000031
Reid Spencerd7d83db2007-02-05 23:42:17 +000032 class VISIBILITY_HIDDEN LoaderPass : public ModulePass, public ProfileInfo {
Chris Lattner927fec32004-02-11 06:10:05 +000033 std::string Filename;
34 public:
Devang Patel19974732007-05-03 01:11:54 +000035 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000036 explicit LoaderPass(const std::string &filename = "")
Devang Patel794fd752007-05-01 21:15:47 +000037 : ModulePass((intptr_t)&ID), Filename(filename) {
Chris Lattner945871d2004-02-11 18:21:05 +000038 if (filename.empty()) Filename = ProfileInfoFilename;
39 }
Chris Lattner927fec32004-02-11 06:10:05 +000040
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.setPreservesAll();
43 }
44
45 virtual const char *getPassName() const {
46 return "Profiling information loader";
47 }
48
Devang Patel1cee94f2008-03-18 00:39:19 +000049 /// isAnalysis - Return true if this pass is implementing an analysis pass.
50 virtual bool isAnalysis() const { return true; }
51
Chris Lattner927fec32004-02-11 06:10:05 +000052 /// run - Load the profile information from the specified file.
Chris Lattnerb12914b2004-09-20 04:48:05 +000053 virtual bool runOnModule(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000054 };
Misha Brukman2b37d7c2005-04-21 21:13:18 +000055
Devang Patel19974732007-05-03 01:11:54 +000056 char LoaderPass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000057 RegisterPass<LoaderPass>
Chris Lattner3dd965c2006-08-21 17:20:01 +000058 X("profile-loader", "Load profile information from llvmprof.out");
Chris Lattner927fec32004-02-11 06:10:05 +000059
Chris Lattnera5370172006-08-28 00:42:29 +000060 RegisterAnalysisGroup<ProfileInfo> Y(X);
Chris Lattner927fec32004-02-11 06:10:05 +000061} // End of anonymous namespace
62
Jeff Cohen6e400f72005-01-10 03:56:27 +000063ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
Chris Lattner927fec32004-02-11 06:10:05 +000064
65/// createProfileLoaderPass - This function returns a Pass that loads the
66/// profiling information for the module from the specified filename, making it
67/// available to the optimizers.
68Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
69 return new LoaderPass(Filename);
70}
71
Chris Lattnerb12914b2004-09-20 04:48:05 +000072bool LoaderPass::runOnModule(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000073 ProfileInfoLoader PIL("profile-loader", Filename, M);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000074 EdgeCounts.clear();
75 bool PrintedWarning = false;
Misha Brukman2b37d7c2005-04-21 21:13:18 +000076
Chris Lattner96ab5ca2004-03-08 22:04:08 +000077 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
78 PIL.getEdgeCounts(ECs);
79 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
80 BasicBlock *BB = ECs[i].first.first;
81 unsigned SuccNum = ECs[i].first.second;
82 TerminatorInst *TI = BB->getTerminator();
83 if (SuccNum >= TI->getNumSuccessors()) {
84 if (!PrintedWarning) {
Bill Wendlinge8156192006-12-07 01:30:32 +000085 cerr << "WARNING: profile information is inconsistent with "
86 << "the current program!\n";
Chris Lattner96ab5ca2004-03-08 22:04:08 +000087 PrintedWarning = true;
88 }
89 } else {
90 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
91 }
Chris Lattner945871d2004-02-11 18:21:05 +000092 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +000093
Chris Lattner927fec32004-02-11 06:10:05 +000094 return false;
95}