~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Transforms/Instrumentation/EdgeProfiling.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===//
 
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 pass instruments the specified program with counters for edge profiling.
 
11
// Edge profiling can give a reasonable approximation of the hot paths through a
 
12
// program, and is used for a wide variety of program transformations.
 
13
//
 
14
// Note that this implementation is very naive.  We insert a counter for *every*
 
15
// edge in the program, instead of using control flow information to prune the
 
16
// number of counters inserted.
 
17
//
 
18
//===----------------------------------------------------------------------===//
 
19
#define DEBUG_TYPE "insert-edge-profiling"
 
20
#include "ProfilingUtils.h"
 
21
#include "llvm/Module.h"
 
22
#include "llvm/Pass.h"
 
23
#include "llvm/Support/raw_ostream.h"
 
24
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 
25
#include "llvm/Transforms/Instrumentation.h"
 
26
#include "llvm/ADT/Statistic.h"
 
27
#include <set>
 
28
using namespace llvm;
 
29
 
 
30
STATISTIC(NumEdgesInserted, "The # of edges inserted.");
 
31
 
 
32
namespace {
 
33
  class EdgeProfiler : public ModulePass {
 
34
    bool runOnModule(Module &M);
 
35
  public:
 
36
    static char ID; // Pass identification, replacement for typeid
 
37
    EdgeProfiler() : ModulePass(&ID) {}
 
38
 
 
39
    virtual const char *getPassName() const {
 
40
      return "Edge Profiler";
 
41
    }
 
42
  };
 
43
}
 
44
 
 
45
char EdgeProfiler::ID = 0;
 
46
static RegisterPass<EdgeProfiler>
 
47
X("insert-edge-profiling", "Insert instrumentation for edge profiling");
 
48
 
 
49
ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
 
50
 
 
51
bool EdgeProfiler::runOnModule(Module &M) {
 
52
  Function *Main = M.getFunction("main");
 
53
  if (Main == 0) {
 
54
    errs() << "WARNING: cannot insert edge profiling into a module"
 
55
           << " with no main function!\n";
 
56
    return false;  // No main, no instrumentation!
 
57
  }
 
58
 
 
59
  std::set<BasicBlock*> BlocksToInstrument;
 
60
  unsigned NumEdges = 0;
 
61
  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
 
62
    if (F->isDeclaration()) continue;
 
63
    // Reserve space for (0,entry) edge.
 
64
    ++NumEdges;
 
65
    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
 
66
      // Keep track of which blocks need to be instrumented.  We don't want to
 
67
      // instrument blocks that are added as the result of breaking critical
 
68
      // edges!
 
69
      BlocksToInstrument.insert(BB);
 
70
      NumEdges += BB->getTerminator()->getNumSuccessors();
 
71
    }
 
72
  }
 
73
 
 
74
  const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges);
 
75
  GlobalVariable *Counters =
 
76
    new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
 
77
                       Constant::getNullValue(ATy), "EdgeProfCounters");
 
78
  NumEdgesInserted = NumEdges;
 
79
 
 
80
  // Instrument all of the edges...
 
81
  unsigned i = 0;
 
82
  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
 
83
    if (F->isDeclaration()) continue;
 
84
    // Create counter for (0,entry) edge.
 
85
    IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters);
 
86
    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
 
87
      if (BlocksToInstrument.count(BB)) {  // Don't instrument inserted blocks
 
88
        // Okay, we have to add a counter of each outgoing edge.  If the
 
89
        // outgoing edge is not critical don't split it, just insert the counter
 
90
        // in the source or destination of the edge.
 
91
        TerminatorInst *TI = BB->getTerminator();
 
92
        for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
 
93
          // If the edge is critical, split it.
 
94
          SplitCriticalEdge(TI, s, this);
 
95
 
 
96
          // Okay, we are guaranteed that the edge is no longer critical.  If we
 
97
          // only have a single successor, insert the counter in this block,
 
98
          // otherwise insert it in the successor block.
 
99
          if (TI->getNumSuccessors() == 1) {
 
100
            // Insert counter at the start of the block
 
101
            IncrementCounterInBlock(BB, i++, Counters);
 
102
          } else {
 
103
            // Insert counter at the start of the block
 
104
            IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
 
105
          }
 
106
        }
 
107
      }
 
108
  }
 
109
 
 
110
  // Add the initialization call to main.
 
111
  InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
 
112
  return true;
 
113
}
 
114