1 //===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 "llvm/Support/Compiler.h"
21 #include "llvm/Support/Streams.h"
25 PrintAll("count-aa-print-all-queries", cl::ReallyHidden);
27 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
30 class VISIBILITY_HIDDEN AliasAnalysisCounter
31 : public ModulePass, public AliasAnalysis {
32 unsigned No, May, Must;
33 unsigned NoMR, JustRef, JustMod, MR;
37 static char ID; // Class identification, replacement for typeinfo
38 AliasAnalysisCounter() : ModulePass(&ID) {
40 NoMR = JustRef = JustMod = MR = 0;
43 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
44 cerr << " " << Val << " " << Desc << " responses ("
45 << Val*100/Sum << "%)\n";
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 cerr << "\n===== Alias Analysis Counter Report =====\n"
52 << " Analysis counted: " << Name << "\n"
53 << " " << AASum << " Total Alias Queries Performed\n";
55 printLine("no alias", No, AASum);
56 printLine("may alias", May, AASum);
57 printLine("must alias", Must, AASum);
58 cerr << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
59 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
62 cerr << " " << MRSum << " Total Mod/Ref Queries Performed\n";
64 printLine("no mod/ref", NoMR, MRSum);
65 printLine("ref", JustRef, MRSum);
66 printLine("mod", JustMod, MRSum);
67 printLine("mod/ref", MR, MRSum);
68 cerr << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum<< "%/"
69 << JustRef*100/MRSum << "%/" << JustMod*100/MRSum << "%/"
70 << MR*100/MRSum <<"%\n\n";
75 bool runOnModule(Module &M) {
77 InitializeAliasAnalysis(this);
78 Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83 AliasAnalysis::getAnalysisUsage(AU);
84 AU.addRequired<AliasAnalysis>();
88 // FIXME: We could count these too...
89 bool pointsToConstantMemory(const Value *P) {
90 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
92 bool doesNotAccessMemory(CallSite CS) {
93 return getAnalysis<AliasAnalysis>().doesNotAccessMemory(CS);
95 bool doesNotAccessMemory(Function *F) {
96 return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F);
98 bool onlyReadsMemory(CallSite CS) {
99 return getAnalysis<AliasAnalysis>().onlyReadsMemory(CS);
101 bool onlyReadsMemory(Function *F) {
102 return getAnalysis<AliasAnalysis>().onlyReadsMemory(F);
106 // Forwarding functions: just delegate to a real AA implementation, counting
107 // the number of responses...
108 AliasResult alias(const Value *V1, unsigned V1Size,
109 const Value *V2, unsigned V2Size);
111 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
112 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
113 return AliasAnalysis::getModRefInfo(CS1,CS2);
118 char AliasAnalysisCounter::ID = 0;
119 static RegisterPass<AliasAnalysisCounter>
120 X("count-aa", "Count Alias Analysis Query Responses", false, true);
121 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
123 ModulePass *llvm::createAliasAnalysisCounterPass() {
124 return new AliasAnalysisCounter();
127 AliasAnalysis::AliasResult
128 AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
129 const Value *V2, unsigned V2Size) {
130 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
132 const char *AliasString;
134 default: assert(0 && "Unknown alias type!");
135 case NoAlias: No++; AliasString = "No alias"; break;
136 case MayAlias: May++; AliasString = "May alias"; break;
137 case MustAlias: Must++; AliasString = "Must alias"; break;
140 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
141 cerr << AliasString << ":\t";
142 cerr << "[" << V1Size << "B] ";
143 WriteAsOperand(*cerr.stream(), V1, true, M);
145 cerr << "[" << V2Size << "B] ";
146 WriteAsOperand(*cerr.stream(), V2, true, M);
153 AliasAnalysis::ModRefResult
154 AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
155 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
157 const char *MRString;
159 default: assert(0 && "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;
166 if (PrintAll || (PrintAllFailures && R == ModRef)) {
167 cerr << MRString << ": Ptr: ";
168 cerr << "[" << Size << "B] ";
169 WriteAsOperand(*cerr.stream(), P, true, M);
170 cerr << "\t<->" << *CS.getInstruction();