adopt getAdjustedAnalysisPointer in a few more passes.
[oota-llvm.git] / lib / Analysis / AliasAnalysisCounter.cpp
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"
23 using namespace llvm;
24
25 static cl::opt<bool>
26 PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
27 static cl::opt<bool>
28 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
29
30 namespace {
31   class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
32     unsigned No, May, Must;
33     unsigned NoMR, JustRef, JustMod, MR;
34     const char *Name;
35     Module *M;
36   public:
37     static char ID; // Class identification, replacement for typeinfo
38     AliasAnalysisCounter() : ModulePass(&ID) {
39       No = May = 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+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: " << Name << "\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("must alias", Must, AASum);
58           errs() << "  Alias Analysis Counter Summary: " << No*100/AASum << "%/"
59                  << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
60         }
61
62         errs() << "  " << MRSum    << " Total Mod/Ref Queries Performed\n";
63         if (MRSum) {
64           printLine("no mod/ref",    NoMR, MRSum);
65           printLine("ref",        JustRef, MRSum);
66           printLine("mod",        JustMod, MRSum);
67           printLine("mod/ref",         MR, MRSum);
68           errs() << "  Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
69                  << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
70                  << "%/" << MR*100/MRSum <<"%\n\n";
71         }
72       }
73     }
74
75     bool runOnModule(Module &M) {
76       this->M = &M;
77       InitializeAliasAnalysis(this);
78       Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
79       return false;
80     }
81
82     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83       AliasAnalysis::getAnalysisUsage(AU);
84       AU.addRequired<AliasAnalysis>();
85       AU.setPreservesAll();
86     }
87
88     /// getAdjustedAnalysisPointer - This method is used when a pass implements
89     /// an analysis interface through multiple inheritance.  If needed, it
90     /// should override this to adjust the this pointer as needed for the
91     /// specified pass info.
92     virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
93       if (PI->isPassID(&AliasAnalysis::ID))
94         return (AliasAnalysis*)this;
95       return this;
96     }
97     
98     // FIXME: We could count these too...
99     bool pointsToConstantMemory(const Value *P) {
100       return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
101     }
102
103     // Forwarding functions: just delegate to a real AA implementation, counting
104     // the number of responses...
105     AliasResult alias(const Value *V1, unsigned V1Size,
106                       const Value *V2, unsigned V2Size);
107
108     ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
109     ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
110       return AliasAnalysis::getModRefInfo(CS1,CS2);
111     }
112   };
113 }
114
115 char AliasAnalysisCounter::ID = 0;
116 static RegisterPass<AliasAnalysisCounter>
117 X("count-aa", "Count Alias Analysis Query Responses", false, true);
118 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
119
120 ModulePass *llvm::createAliasAnalysisCounterPass() {
121   return new AliasAnalysisCounter();
122 }
123
124 AliasAnalysis::AliasResult
125 AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
126                             const Value *V2, unsigned V2Size) {
127   AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
128
129   const char *AliasString;
130   switch (R) {
131   default: llvm_unreachable("Unknown alias type!");
132   case NoAlias:   No++;   AliasString = "No alias"; break;
133   case MayAlias:  May++;  AliasString = "May alias"; break;
134   case MustAlias: Must++; AliasString = "Must alias"; break;
135   }
136
137   if (PrintAll || (PrintAllFailures && R == MayAlias)) {
138     errs() << AliasString << ":\t";
139     errs() << "[" << V1Size << "B] ";
140     WriteAsOperand(errs(), V1, true, M);
141     errs() << ", ";
142     errs() << "[" << V2Size << "B] ";
143     WriteAsOperand(errs(), V2, true, M);
144     errs() << "\n";
145   }
146
147   return R;
148 }
149
150 AliasAnalysis::ModRefResult
151 AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
152   ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
153
154   const char *MRString;
155   switch (R) {
156   default:       llvm_unreachable("Unknown mod/ref type!");
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() << "[" << Size << "B] ";
166     WriteAsOperand(errs(), P, true, M);
167     errs() << "\t<->" << *CS.getInstruction();
168   }
169   return R;
170 }