AliasAnalysisCounter.cpp revision b576c94c15af9a440f69d9d03c2afead7971118c
1//===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
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 pass which can be used to count how many alias queries
11// are being made and how the alias analysis implementation being used responds.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/AliasAnalysis.h"
16#include "llvm/Pass.h"
17#include <iostream>
18
19namespace {
20  class AliasAnalysisCounter : public Pass, public AliasAnalysis {
21    unsigned No, May, Must;
22    unsigned NoMR, JustRef, JustMod, MR;
23    const char *Name;
24  public:
25    AliasAnalysisCounter() {
26      No = May = Must = 0;
27      NoMR = JustRef = JustMod = MR = 0;
28    }
29
30    void printLine(const char *Desc, unsigned Val, unsigned Sum) {
31      std::cerr <<  "  " << Val << " " << Desc << " responses ("
32                << Val*100/Sum << "%)\n";
33    }
34    ~AliasAnalysisCounter() {
35      unsigned AASum = No+May+Must;
36      unsigned MRSum = NoMR+JustRef+JustMod+MR;
37      if (AASum + MRSum) { // Print a report if any counted queries occurred...
38        std::cerr
39          << "\n===== Alias Analysis Counter Report =====\n"
40          << "  Analysis counted: " << Name << "\n"
41          << "  " << AASum << " Total Alias Queries Performed\n";
42        if (AASum) {
43          printLine("no alias",     No, AASum);
44          printLine("may alias",   May, AASum);
45          printLine("must alias", Must, AASum);
46          std::cerr
47            << "  Alias Analysis Counter Summary: " << No*100/AASum << "%/"
48            << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
49        }
50
51        std::cerr
52          << "  " << MRSum    << " Total Mod/Ref Queries Performed\n";
53        if (MRSum) {
54          printLine("no mod/ref",    NoMR, MRSum);
55          printLine("ref",        JustRef, MRSum);
56          printLine("mod",        JustMod, MRSum);
57          printLine("mod/ref",         MR, MRSum);
58          std::cerr
59            << "  Mod/Ref Analysis Counter Summary: " << NoMR*100/MRSum<< "%/"
60            << JustRef*100/MRSum << "%/" << JustMod*100/MRSum << "%/"
61            << MR*100/MRSum <<"%\n\n";
62        }
63      }
64    }
65
66    bool run(Module &M) {
67      InitializeAliasAnalysis(this);
68      Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
69      return false;
70    }
71
72    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73      AliasAnalysis::getAnalysisUsage(AU);
74      AU.addRequired<AliasAnalysis>();
75      AU.setPreservesAll();
76    }
77
78    AliasResult count(AliasResult R) {
79      switch (R) {
80      default: assert(0 && "Unknown alias type!");
81      case NoAlias:   No++; return NoAlias;
82      case MayAlias:  May++; return MayAlias;
83      case MustAlias: Must++; return MustAlias;
84      }
85    }
86    ModRefResult count(ModRefResult R) {
87      switch (R) {
88      default:       assert(0 && "Unknown mod/ref type!");
89      case NoModRef: NoMR++;     return NoModRef;
90      case Ref:      JustRef++;  return Ref;
91      case Mod:      JustMod++;  return Mod;
92      case ModRef:   MR++;       return ModRef;
93      }
94    }
95
96    // Forwarding functions: just delegate to a real AA implementation, counting
97    // the number of responses...
98    AliasResult alias(const Value *V1, unsigned V1Size,
99                      const Value *V2, unsigned V2Size) {
100      return count(getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size));
101    }
102    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
103      return count(getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size));
104    }
105  };
106
107  RegisterOpt<AliasAnalysisCounter>
108  X("count-aa", "Count Alias Analysis Query Responses");
109  RegisterAnalysisGroup<AliasAnalysis, AliasAnalysisCounter> Y;
110}
111