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