blob: b76c34a8bb833770753d2ee5c0e5ffd48a719e92 [file] [log] [blame]
Chris Lattner927fec32004-02-11 06:10:05 +00001//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Spencer954da372004-07-04 12:19:56 +000022#include <iostream>
23
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
Chris Lattnerb12914b2004-09-20 04:48:05 +000032 class LoaderPass : public ModulePass, public ProfileInfo {
Chris Lattner927fec32004-02-11 06:10:05 +000033 std::string Filename;
34 public:
Chris Lattner945871d2004-02-11 18:21:05 +000035 LoaderPass(const std::string &filename = "")
36 : Filename(filename) {
37 if (filename.empty()) Filename = ProfileInfoFilename;
38 }
Chris Lattner927fec32004-02-11 06:10:05 +000039
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesAll();
42 }
43
44 virtual const char *getPassName() const {
45 return "Profiling information loader";
46 }
47
48 /// run - Load the profile information from the specified file.
Chris Lattnerb12914b2004-09-20 04:48:05 +000049 virtual bool runOnModule(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000050 };
51
52 RegisterOpt<LoaderPass>
53 X("profile-loader", "Load profile information from llvmprof.out");
54
55 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
56} // End of anonymous namespace
57
Jeff Cohen6e400f72005-01-10 03:56:27 +000058ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
Chris Lattner927fec32004-02-11 06:10:05 +000059
60/// createProfileLoaderPass - This function returns a Pass that loads the
61/// profiling information for the module from the specified filename, making it
62/// available to the optimizers.
63Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
64 return new LoaderPass(Filename);
65}
66
Chris Lattnerb12914b2004-09-20 04:48:05 +000067bool LoaderPass::runOnModule(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000068 ProfileInfoLoader PIL("profile-loader", Filename, M);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000069 EdgeCounts.clear();
70 bool PrintedWarning = false;
71
72 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
73 PIL.getEdgeCounts(ECs);
74 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
75 BasicBlock *BB = ECs[i].first.first;
76 unsigned SuccNum = ECs[i].first.second;
77 TerminatorInst *TI = BB->getTerminator();
78 if (SuccNum >= TI->getNumSuccessors()) {
79 if (!PrintedWarning) {
80 std::cerr << "WARNING: profile information is inconsistent with "
81 << "the current program!\n";
82 PrintedWarning = true;
83 }
84 } else {
85 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
86 }
Chris Lattner945871d2004-02-11 18:21:05 +000087 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +000088
Chris Lattner927fec32004-02-11 06:10:05 +000089 return false;
90}