~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Analysis/ProfileInfoLoaderPass.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//===- ProfileInfoLoaderPass.cpp - LLVM Pass to load profile info ---------===//
2
 
//
3
 
//                     The LLVM Compiler Infrastructure
4
 
//
5
 
// This file is distributed under the University of Illinois Open Source
6
 
// 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
 
#define DEBUG_TYPE "profile-loader"
15
 
#include "llvm/BasicBlock.h"
16
 
#include "llvm/InstrTypes.h"
17
 
#include "llvm/Module.h"
18
 
#include "llvm/Pass.h"
19
 
#include "llvm/Analysis/Passes.h"
20
 
#include "llvm/Analysis/ProfileInfo.h"
21
 
#include "llvm/Analysis/ProfileInfoLoader.h"
22
 
#include "llvm/Support/CommandLine.h"
23
 
#include "llvm/Support/CFG.h"
24
 
#include "llvm/Support/Debug.h"
25
 
#include "llvm/Support/raw_ostream.h"
26
 
#include "llvm/Support/Format.h"
27
 
#include "llvm/ADT/Statistic.h"
28
 
#include "llvm/ADT/SmallSet.h"
29
 
#include <set>
30
 
using namespace llvm;
31
 
 
32
 
STATISTIC(NumEdgesRead, "The # of edges read.");
33
 
 
34
 
static cl::opt<std::string>
35
 
ProfileInfoFilename("profile-info-file", cl::init("llvmprof.out"),
36
 
                    cl::value_desc("filename"),
37
 
                    cl::desc("Profile file loaded by -profile-loader"));
38
 
 
39
 
namespace {
40
 
  class LoaderPass : public ModulePass, public ProfileInfo {
41
 
    std::string Filename;
42
 
    std::set<Edge> SpanningTree;
43
 
    std::set<const BasicBlock*> BBisUnvisited;
44
 
    unsigned ReadCount;
45
 
  public:
46
 
    static char ID; // Class identification, replacement for typeinfo
47
 
    explicit LoaderPass(const std::string &filename = "")
48
 
      : ModulePass(ID), Filename(filename) {
49
 
      if (filename.empty()) Filename = ProfileInfoFilename;
50
 
    }
51
 
 
52
 
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53
 
      AU.setPreservesAll();
54
 
    }
55
 
 
56
 
    virtual const char *getPassName() const {
57
 
      return "Profiling information loader";
58
 
    }
59
 
 
60
 
    // recurseBasicBlock() - Calculates the edge weights for as much basic
61
 
    // blocks as possbile.
62
 
    virtual void recurseBasicBlock(const BasicBlock *BB);
63
 
    virtual void readEdgeOrRemember(Edge, Edge&, unsigned &, double &);
64
 
    virtual void readEdge(ProfileInfo::Edge, std::vector<unsigned>&);
65
 
 
66
 
    /// getAdjustedAnalysisPointer - This method is used when a pass implements
67
 
    /// an analysis interface through multiple inheritance.  If needed, it
68
 
    /// should override this to adjust the this pointer as needed for the
69
 
    /// specified pass info.
70
 
    virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
71
 
      if (PI == &ProfileInfo::ID)
72
 
        return (ProfileInfo*)this;
73
 
      return this;
74
 
    }
75
 
    
76
 
    /// run - Load the profile information from the specified file.
77
 
    virtual bool runOnModule(Module &M);
78
 
  };
79
 
}  // End of anonymous namespace
80
 
 
81
 
char LoaderPass::ID = 0;
82
 
INITIALIZE_AG_PASS(LoaderPass, ProfileInfo, "profile-loader",
83
 
              "Load profile information from llvmprof.out", false, true, false);
84
 
 
85
 
char &llvm::ProfileLoaderPassID = LoaderPass::ID;
86
 
 
87
 
ModulePass *llvm::createProfileLoaderPass() { return new LoaderPass(); }
88
 
 
89
 
/// createProfileLoaderPass - This function returns a Pass that loads the
90
 
/// profiling information for the module from the specified filename, making it
91
 
/// available to the optimizers.
92
 
Pass *llvm::createProfileLoaderPass(const std::string &Filename) {
93
 
  return new LoaderPass(Filename);
94
 
}
95
 
 
96
 
void LoaderPass::readEdgeOrRemember(Edge edge, Edge &tocalc, 
97
 
                                    unsigned &uncalc, double &count) {
98
 
  double w;
99
 
  if ((w = getEdgeWeight(edge)) == MissingValue) {
100
 
    tocalc = edge;
101
 
    uncalc++;
102
 
  } else {
103
 
    count+=w;
104
 
  }
105
 
}
106
 
 
107
 
// recurseBasicBlock - Visits all neighbours of a block and then tries to
108
 
// calculate the missing edge values.
109
 
void LoaderPass::recurseBasicBlock(const BasicBlock *BB) {
110
 
 
111
 
  // break recursion if already visited
112
 
  if (BBisUnvisited.find(BB) == BBisUnvisited.end()) return;
113
 
  BBisUnvisited.erase(BB);
114
 
  if (!BB) return;
115
 
 
116
 
  for (succ_const_iterator bbi = succ_begin(BB), bbe = succ_end(BB);
117
 
       bbi != bbe; ++bbi) {
118
 
    recurseBasicBlock(*bbi);
119
 
  }
120
 
  for (const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
121
 
       bbi != bbe; ++bbi) {
122
 
    recurseBasicBlock(*bbi);
123
 
  }
124
 
 
125
 
  Edge tocalc;
126
 
  if (CalculateMissingEdge(BB, tocalc)) {
127
 
    SpanningTree.erase(tocalc);
128
 
  }
129
 
}
130
 
 
131
 
void LoaderPass::readEdge(ProfileInfo::Edge e,
132
 
                          std::vector<unsigned> &ECs) {
133
 
  if (ReadCount < ECs.size()) {
134
 
    double weight = ECs[ReadCount++];
135
 
    if (weight != ProfileInfoLoader::Uncounted) {
136
 
      // Here the data realm changes from the unsigned of the file to the
137
 
      // double of the ProfileInfo. This conversion is save because we know
138
 
      // that everything thats representable in unsinged is also representable
139
 
      // in double.
140
 
      EdgeInformation[getFunction(e)][e] += (double)weight;
141
 
 
142
 
      DEBUG(dbgs() << "--Read Edge Counter for " << e
143
 
                   << " (# "<< (ReadCount-1) << "): "
144
 
                   << (unsigned)getEdgeWeight(e) << "\n");
145
 
    } else {
146
 
      // This happens only if reading optimal profiling information, not when
147
 
      // reading regular profiling information.
148
 
      SpanningTree.insert(e);
149
 
    }
150
 
  }
151
 
}
152
 
 
153
 
bool LoaderPass::runOnModule(Module &M) {
154
 
  ProfileInfoLoader PIL("profile-loader", Filename, M);
155
 
 
156
 
  EdgeInformation.clear();
157
 
  std::vector<unsigned> Counters = PIL.getRawEdgeCounts();
158
 
  if (Counters.size() > 0) {
159
 
    ReadCount = 0;
160
 
    for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
161
 
      if (F->isDeclaration()) continue;
162
 
      DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");
163
 
      readEdge(getEdge(0,&F->getEntryBlock()), Counters);
164
 
      for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
165
 
        TerminatorInst *TI = BB->getTerminator();
166
 
        for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
167
 
          readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
168
 
        }
169
 
      }
170
 
    }
171
 
    if (ReadCount != Counters.size()) {
172
 
      errs() << "WARNING: profile information is inconsistent with "
173
 
             << "the current program!\n";
174
 
    }
175
 
    NumEdgesRead = ReadCount;
176
 
  }
177
 
 
178
 
  Counters = PIL.getRawOptimalEdgeCounts();
179
 
  if (Counters.size() > 0) {
180
 
    ReadCount = 0;
181
 
    for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
182
 
      if (F->isDeclaration()) continue;
183
 
      DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n");
184
 
      readEdge(getEdge(0,&F->getEntryBlock()), Counters);
185
 
      for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
186
 
        TerminatorInst *TI = BB->getTerminator();
187
 
        if (TI->getNumSuccessors() == 0) {
188
 
          readEdge(getEdge(BB,0), Counters);
189
 
        }
190
 
        for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
191
 
          readEdge(getEdge(BB,TI->getSuccessor(s)), Counters);
192
 
        }
193
 
      }
194
 
      while (SpanningTree.size() > 0) {
195
 
 
196
 
        unsigned size = SpanningTree.size();
197
 
 
198
 
        BBisUnvisited.clear();
199
 
        for (std::set<Edge>::iterator ei = SpanningTree.begin(),
200
 
             ee = SpanningTree.end(); ei != ee; ++ei) {
201
 
          BBisUnvisited.insert(ei->first);
202
 
          BBisUnvisited.insert(ei->second);
203
 
        }
204
 
        while (BBisUnvisited.size() > 0) {
205
 
          recurseBasicBlock(*BBisUnvisited.begin());
206
 
        }
207
 
 
208
 
        if (SpanningTree.size() == size) {
209
 
          DEBUG(dbgs()<<"{");
210
 
          for (std::set<Edge>::iterator ei = SpanningTree.begin(),
211
 
               ee = SpanningTree.end(); ei != ee; ++ei) {
212
 
            DEBUG(dbgs()<< *ei <<",");
213
 
          }
214
 
          assert(0 && "No edge calculated!");
215
 
        }
216
 
 
217
 
      }
218
 
    }
219
 
    if (ReadCount != Counters.size()) {
220
 
      errs() << "WARNING: profile information is inconsistent with "
221
 
             << "the current program!\n";
222
 
    }
223
 
    NumEdgesRead = ReadCount;
224
 
  }
225
 
 
226
 
  BlockInformation.clear();
227
 
  Counters = PIL.getRawBlockCounts();
228
 
  if (Counters.size() > 0) {
229
 
    ReadCount = 0;
230
 
    for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
231
 
      if (F->isDeclaration()) continue;
232
 
      for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
233
 
        if (ReadCount < Counters.size())
234
 
          // Here the data realm changes from the unsigned of the file to the
235
 
          // double of the ProfileInfo. This conversion is save because we know
236
 
          // that everything thats representable in unsinged is also
237
 
          // representable in double.
238
 
          BlockInformation[F][BB] = (double)Counters[ReadCount++];
239
 
    }
240
 
    if (ReadCount != Counters.size()) {
241
 
      errs() << "WARNING: profile information is inconsistent with "
242
 
             << "the current program!\n";
243
 
    }
244
 
  }
245
 
 
246
 
  FunctionInformation.clear();
247
 
  Counters = PIL.getRawFunctionCounts();
248
 
  if (Counters.size() > 0) {
249
 
    ReadCount = 0;
250
 
    for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
251
 
      if (F->isDeclaration()) continue;
252
 
      if (ReadCount < Counters.size())
253
 
        // Here the data realm changes from the unsigned of the file to the
254
 
        // double of the ProfileInfo. This conversion is save because we know
255
 
        // that everything thats representable in unsinged is also
256
 
        // representable in double.
257
 
        FunctionInformation[F] = (double)Counters[ReadCount++];
258
 
    }
259
 
    if (ReadCount != Counters.size()) {
260
 
      errs() << "WARNING: profile information is inconsistent with "
261
 
             << "the current program!\n";
262
 
    }
263
 
  }
264
 
 
265
 
  return false;
266
 
}