AliasAnalysisCounter.cpp revision 91d747569a87ed412622857521f0952d778f67fc
1//===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
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 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/Passes.h"
16#include "llvm/Pass.h"
17#include "llvm/Analysis/AliasAnalysis.h"
18#include "llvm/Assembly/Writer.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
25static cl::opt<bool>
26PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
27static cl::opt<bool>
28PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
29
30namespace {
31  class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
32    unsigned No, May, Partial, Must;
33    unsigned NoMR, JustRef, JustMod, MR;
34    Module *M;
35  public:
36    static char ID; // Class identification, replacement for typeinfo
37    AliasAnalysisCounter() : ModulePass(ID) {
38      initializeAliasAnalysisCounterPass(*PassRegistry::getPassRegistry());
39      No = May = Partial = Must = 0;
40      NoMR = JustRef = JustMod = MR = 0;
41    }
42
43    void printLine(const char *Desc, unsigned Val, unsigned Sum) {
44      errs() <<  "  " << Val << " " << Desc << " responses ("
45             << Val*100/Sum << "%)\n";
46    }
47    ~AliasAnalysisCounter() {
48      unsigned AASum = No+May+Partial+Must;
49      unsigned MRSum = NoMR+JustRef+JustMod+MR;
50      if (AASum + MRSum) { // Print a report if any counted queries occurred...
51        errs() << "\n===== Alias Analysis Counter Report =====\n"
52               << "  Analysis counted:\n"
53               << "  " << AASum << " Total Alias Queries Performed\n";
54        if (AASum) {
55          printLine("no alias",     No, AASum);
56          printLine("may alias",   May, AASum);
57          printLine("partial alias", Partial, AASum);
58          printLine("must alias", Must, AASum);
59          errs() << "  Alias Analysis Counter Summary: " << No*100/AASum << "%/"
60                 << May*100/AASum << "%/"
61                 << Partial*100/AASum << "%/"
62                 << Must*100/AASum<<"%\n\n";
63        }
64
65        errs() << "  " << MRSum    << " Total Mod/Ref Queries Performed\n";
66        if (MRSum) {
67          printLine("no mod/ref",    NoMR, MRSum);
68          printLine("ref",        JustRef, MRSum);
69          printLine("mod",        JustMod, MRSum);
70          printLine("mod/ref",         MR, MRSum);
71          errs() << "  Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
72                 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
73                 << "%/" << MR*100/MRSum <<"%\n\n";
74        }
75      }
76    }
77
78    bool runOnModule(Module &M) {
79      this->M = &M;
80      InitializeAliasAnalysis(this);
81      return false;
82    }
83
84    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85      AliasAnalysis::getAnalysisUsage(AU);
86      AU.addRequired<AliasAnalysis>();
87      AU.setPreservesAll();
88    }
89
90    /// getAdjustedAnalysisPointer - This method is used when a pass implements
91    /// an analysis interface through multiple inheritance.  If needed, it
92    /// should override this to adjust the this pointer as needed for the
93    /// specified pass info.
94    virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
95      if (PI == &AliasAnalysis::ID)
96        return (AliasAnalysis*)this;
97      return this;
98    }
99
100    // FIXME: We could count these too...
101    bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
102      return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal);
103    }
104
105    // Forwarding functions: just delegate to a real AA implementation, counting
106    // the number of responses...
107    AliasResult alias(const Location &LocA, const Location &LocB);
108
109    ModRefResult getModRefInfo(ImmutableCallSite CS,
110                               const Location &Loc);
111    ModRefResult getModRefInfo(ImmutableCallSite CS1,
112                               ImmutableCallSite CS2) {
113      return AliasAnalysis::getModRefInfo(CS1,CS2);
114    }
115  };
116}
117
118char AliasAnalysisCounter::ID = 0;
119INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa",
120                   "Count Alias Analysis Query Responses", false, true, false)
121
122ModulePass *llvm::createAliasAnalysisCounterPass() {
123  return new AliasAnalysisCounter();
124}
125
126AliasAnalysis::AliasResult
127AliasAnalysisCounter::alias(const Location &LocA, const Location &LocB) {
128  AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB);
129
130  const char *AliasString;
131  switch (R) {
132  default: llvm_unreachable("Unknown alias type!");
133  case NoAlias:   No++;   AliasString = "No alias"; break;
134  case MayAlias:  May++;  AliasString = "May alias"; break;
135  case PartialAlias: Partial++; AliasString = "Partial alias"; break;
136  case MustAlias: Must++; AliasString = "Must alias"; break;
137  }
138
139  if (PrintAll || (PrintAllFailures && R == MayAlias)) {
140    errs() << AliasString << ":\t";
141    errs() << "[" << LocA.Size << "B] ";
142    WriteAsOperand(errs(), LocA.Ptr, true, M);
143    errs() << ", ";
144    errs() << "[" << LocB.Size << "B] ";
145    WriteAsOperand(errs(), LocB.Ptr, true, M);
146    errs() << "\n";
147  }
148
149  return R;
150}
151
152AliasAnalysis::ModRefResult
153AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
154                                    const Location &Loc) {
155  ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc);
156
157  const char *MRString;
158  switch (R) {
159  default:       llvm_unreachable("Unknown mod/ref type!");
160  case NoModRef: NoMR++;     MRString = "NoModRef"; break;
161  case Ref:      JustRef++;  MRString = "JustRef"; break;
162  case Mod:      JustMod++;  MRString = "JustMod"; break;
163  case ModRef:   MR++;       MRString = "ModRef"; break;
164  }
165
166  if (PrintAll || (PrintAllFailures && R == ModRef)) {
167    errs() << MRString << ":  Ptr: ";
168    errs() << "[" << Loc.Size << "B] ";
169    WriteAsOperand(errs(), Loc.Ptr, true, M);
170    errs() << "\t<->" << *CS.getInstruction() << '\n';
171  }
172  return R;
173}
174