blob: cd3f5753559847674a0c0c0045ff72ce90032236 [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"
18#include "llvm/Analysis/ProfileInfo.h"
19#include "llvm/Analysis/ProfileInfoLoader.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Reid Spencer954da372004-07-04 12:19:56 +000021#include <iostream>
22
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 Lattner927fec32004-02-11 06:10:05 +000031 class LoaderPass : public Pass, public ProfileInfo {
32 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.
48 virtual bool run(Module &M);
Chris Lattner927fec32004-02-11 06:10:05 +000049 };
50
51 RegisterOpt<LoaderPass>
52 X("profile-loader", "Load profile information from llvmprof.out");
53
54 RegisterAnalysisGroup<ProfileInfo, LoaderPass> Y;
55} // End of anonymous namespace
56
57
58/// createProfileLoaderPass - This function returns a Pass that loads the
59/// profiling information for the module from the specified filename, making it
60/// available to the optimizers.
61Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
62 return new LoaderPass(Filename);
63}
64
65bool LoaderPass::run(Module &M) {
Chris Lattner62e84f32004-03-08 21:30:35 +000066 ProfileInfoLoader PIL("profile-loader", Filename, M);
Chris Lattner96ab5ca2004-03-08 22:04:08 +000067 EdgeCounts.clear();
68 bool PrintedWarning = false;
69
70 std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
71 PIL.getEdgeCounts(ECs);
72 for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
73 BasicBlock *BB = ECs[i].first.first;
74 unsigned SuccNum = ECs[i].first.second;
75 TerminatorInst *TI = BB->getTerminator();
76 if (SuccNum >= TI->getNumSuccessors()) {
77 if (!PrintedWarning) {
78 std::cerr << "WARNING: profile information is inconsistent with "
79 << "the current program!\n";
80 PrintedWarning = true;
81 }
82 } else {
83 EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
84 }
Chris Lattner945871d2004-02-11 18:21:05 +000085 }
Chris Lattner96ab5ca2004-03-08 22:04:08 +000086
Chris Lattner927fec32004-02-11 06:10:05 +000087 return false;
88}