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/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
26 PrintAll("count-aa-print-all-queries", cl::ReallyHidden);
28 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
31 class VISIBILITY_HIDDEN AliasAnalysisCounter
32 : public ModulePass, public AliasAnalysis {
33 unsigned No, May, Must;
34 unsigned NoMR, JustRef, JustMod, MR;
38 static char ID; // Class identification, replacement for typeinfo
39 AliasAnalysisCounter() : ModulePass(&ID) {
41 NoMR = JustRef = JustMod = MR = 0;
44 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
45 errs() << " " << Val << " " << Desc << " responses ("
46 << Val*100/Sum << "%)\n";
48 ~AliasAnalysisCounter() {
49 unsigned AASum = No+May+Must;
50 unsigned MRSum = NoMR+JustRef+JustMod+MR;
51 if (AASum + MRSum) { // Print a report if any counted queries occurred...
52 errs() << "\n===== Alias Analysis Counter Report =====\n"
53 << " Analysis counted: " << Name << "\n"
54 << " " << AASum << " Total Alias Queries Performed\n";
56 printLine("no alias", No, AASum);
57 printLine("may alias", May, AASum);
58 printLine("must alias", Must, AASum);
59 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
60 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
63 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
65 printLine("no mod/ref", NoMR, MRSum);
66 printLine("ref", JustRef, MRSum);
67 printLine("mod", JustMod, MRSum);
68 printLine("mod/ref", MR, MRSum);
69 errs() << " 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) {
78 InitializeAliasAnalysis(this);
79 Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
83 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
84 AliasAnalysis::getAnalysisUsage(AU);
85 AU.addRequired<AliasAnalysis>();
89 // FIXME: We could count these too...
90 bool pointsToConstantMemory(const Value *P) {
91 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
93 bool doesNotAccessMemory(CallSite CS) {
94 return getAnalysis<AliasAnalysis>().doesNotAccessMemory(CS);
96 bool doesNotAccessMemory(Function *F) {
97 return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F);
99 bool onlyReadsMemory(CallSite CS) {
100 return getAnalysis<AliasAnalysis>().onlyReadsMemory(CS);
102 bool onlyReadsMemory(Function *F) {
103 return getAnalysis<AliasAnalysis>().onlyReadsMemory(F);
107 // Forwarding functions: just delegate to a real AA implementation, counting
108 // the number of responses...
109 AliasResult alias(const Value *V1, unsigned V1Size,
110 const Value *V2, unsigned V2Size);
112 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
113 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
114 return AliasAnalysis::getModRefInfo(CS1,CS2);
119 char AliasAnalysisCounter::ID = 0;
120 static RegisterPass<AliasAnalysisCounter>
121 X("count-aa", "Count Alias Analysis Query Responses", false, true);
122 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
124 ModulePass *llvm::createAliasAnalysisCounterPass() {
125 return new AliasAnalysisCounter();
128 AliasAnalysis::AliasResult
129 AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
130 const Value *V2, unsigned V2Size) {
131 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
133 const char *AliasString;
135 default: llvm_unreachable("Unknown alias type!");
136 case NoAlias: No++; AliasString = "No alias"; break;
137 case MayAlias: May++; AliasString = "May alias"; break;
138 case MustAlias: Must++; AliasString = "Must alias"; break;
141 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
142 errs() << AliasString << ":\t";
143 errs() << "[" << V1Size << "B] ";
144 WriteAsOperand(errs(), V1, true, M);
146 errs() << "[" << V2Size << "B] ";
147 WriteAsOperand(errs(), V2, true, M);
154 AliasAnalysis::ModRefResult
155 AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
156 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
158 const char *MRString;
160 default: llvm_unreachable("Unknown mod/ref type!");
161 case NoModRef: NoMR++; MRString = "NoModRef"; break;
162 case Ref: JustRef++; MRString = "JustRef"; break;
163 case Mod: JustMod++; MRString = "JustMod"; break;
164 case ModRef: MR++; MRString = "ModRef"; break;
167 if (PrintAll || (PrintAllFailures && R == ModRef)) {
168 errs() << MRString << ": Ptr: ";
169 errs() << "[" << Size << "B] ";
170 WriteAsOperand(errs(), P, true, M);
171 errs() << "\t<->" << *CS.getInstruction();