Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / Analysis / AliasAnalysisCounter.cpp
blob8c8cb3e1d48dc942e3cc624d66fdf34159cf4573
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.
13 //===----------------------------------------------------------------------===//
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 <iostream>
21 using namespace llvm;
23 namespace {
24 cl::opt<bool>
25 PrintAll("count-aa-print-all-queries", cl::ReallyHidden);
26 cl::opt<bool>
27 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
29 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
30 unsigned No, May, Must;
31 unsigned NoMR, JustRef, JustMod, MR;
32 const char *Name;
33 Module *M;
34 public:
35 AliasAnalysisCounter() {
36 No = May = Must = 0;
37 NoMR = JustRef = JustMod = MR = 0;
40 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
41 std::cerr << " " << Val << " " << Desc << " responses ("
42 << Val*100/Sum << "%)\n";
44 ~AliasAnalysisCounter() {
45 unsigned AASum = No+May+Must;
46 unsigned MRSum = NoMR+JustRef+JustMod+MR;
47 if (AASum + MRSum) { // Print a report if any counted queries occurred...
48 std::cerr
49 << "\n===== Alias Analysis Counter Report =====\n"
50 << " Analysis counted: " << Name << "\n"
51 << " " << AASum << " Total Alias Queries Performed\n";
52 if (AASum) {
53 printLine("no alias", No, AASum);
54 printLine("may alias", May, AASum);
55 printLine("must alias", Must, AASum);
56 std::cerr
57 << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
58 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
61 std::cerr
62 << " " << 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 std::cerr
69 << " Mod/Ref Analysis Counter Summary: " << NoMR*100/MRSum<< "%/"
70 << JustRef*100/MRSum << "%/" << JustMod*100/MRSum << "%/"
71 << MR*100/MRSum <<"%\n\n";
76 bool runOnModule(Module &M) {
77 this->M = &M;
78 InitializeAliasAnalysis(this);
79 Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
80 return false;
83 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
84 AliasAnalysis::getAnalysisUsage(AU);
85 AU.addRequired<AliasAnalysis>();
86 AU.setPreservesAll();
89 // FIXME: We could count these too...
90 bool pointsToConstantMemory(const Value *P) {
91 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
93 bool doesNotAccessMemory(Function *F) {
94 return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F);
96 bool onlyReadsMemory(Function *F) {
97 return getAnalysis<AliasAnalysis>().onlyReadsMemory(F);
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);
106 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
107 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
108 return AliasAnalysis::getModRefInfo(CS1,CS2);
112 RegisterOpt<AliasAnalysisCounter>
113 X("count-aa", "Count Alias Analysis Query Responses");
114 RegisterAnalysisGroup<AliasAnalysis, AliasAnalysisCounter> Y;
117 ModulePass *llvm::createAliasAnalysisCounterPass() {
118 return new AliasAnalysisCounter();
121 AliasAnalysis::AliasResult
122 AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
123 const Value *V2, unsigned V2Size) {
124 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
126 const char *AliasString;
127 switch (R) {
128 default: assert(0 && "Unknown alias type!");
129 case NoAlias: No++; AliasString = "No alias"; break;
130 case MayAlias: May++; AliasString = "May alias"; break;
131 case MustAlias: Must++; AliasString = "Must alias"; break;
134 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
135 std::cerr << AliasString << ":\t";
136 std::cerr << "[" << V1Size << "B] ";
137 WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
138 std::cerr << "[" << V2Size << "B] ";
139 WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
142 return R;
145 AliasAnalysis::ModRefResult
146 AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
147 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
149 const char *MRString;
150 switch (R) {
151 default: assert(0 && "Unknown mod/ref type!");
152 case NoModRef: NoMR++; MRString = "NoModRef"; break;
153 case Ref: JustRef++; MRString = "JustRef"; break;
154 case Mod: JustMod++; MRString = "JustMod"; break;
155 case ModRef: MR++; MRString = "ModRef"; break;
158 if (PrintAll || (PrintAllFailures && R == ModRef)) {
159 std::cerr << MRString << ": Ptr: ";
160 std::cerr << "[" << Size << "B] ";
161 WriteAsOperand(std::cerr, P, true, true, M);
162 std::cerr << "\t<->" << *CS.getInstruction();
164 return R;