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