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
, cl::init(true));
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
);
94 // Forwarding functions: just delegate to a real AA implementation, counting
95 // the number of responses...
96 AliasResult
alias(const Value
*V1
, unsigned V1Size
,
97 const Value
*V2
, unsigned V2Size
);
99 ModRefResult
getModRefInfo(CallSite CS
, Value
*P
, unsigned Size
);
100 ModRefResult
getModRefInfo(CallSite CS1
, CallSite CS2
) {
101 return AliasAnalysis::getModRefInfo(CS1
,CS2
);
106 char AliasAnalysisCounter::ID
= 0;
107 static RegisterPass
<AliasAnalysisCounter
>
108 X("count-aa", "Count Alias Analysis Query Responses", false, true);
109 static RegisterAnalysisGroup
<AliasAnalysis
> Y(X
);
111 ModulePass
*llvm::createAliasAnalysisCounterPass() {
112 return new AliasAnalysisCounter();
115 AliasAnalysis::AliasResult
116 AliasAnalysisCounter::alias(const Value
*V1
, unsigned V1Size
,
117 const Value
*V2
, unsigned V2Size
) {
118 AliasResult R
= getAnalysis
<AliasAnalysis
>().alias(V1
, V1Size
, V2
, V2Size
);
120 const char *AliasString
;
122 default: llvm_unreachable("Unknown alias type!");
123 case NoAlias
: No
++; AliasString
= "No alias"; break;
124 case MayAlias
: May
++; AliasString
= "May alias"; break;
125 case MustAlias
: Must
++; AliasString
= "Must alias"; break;
128 if (PrintAll
|| (PrintAllFailures
&& R
== MayAlias
)) {
129 errs() << AliasString
<< ":\t";
130 errs() << "[" << V1Size
<< "B] ";
131 WriteAsOperand(errs(), V1
, true, M
);
133 errs() << "[" << V2Size
<< "B] ";
134 WriteAsOperand(errs(), V2
, true, M
);
141 AliasAnalysis::ModRefResult
142 AliasAnalysisCounter::getModRefInfo(CallSite CS
, Value
*P
, unsigned Size
) {
143 ModRefResult R
= getAnalysis
<AliasAnalysis
>().getModRefInfo(CS
, P
, Size
);
145 const char *MRString
;
147 default: llvm_unreachable("Unknown mod/ref type!");
148 case NoModRef
: NoMR
++; MRString
= "NoModRef"; break;
149 case Ref
: JustRef
++; MRString
= "JustRef"; break;
150 case Mod
: JustMod
++; MRString
= "JustMod"; break;
151 case ModRef
: MR
++; MRString
= "ModRef"; break;
154 if (PrintAll
|| (PrintAllFailures
&& R
== ModRef
)) {
155 errs() << MRString
<< ": Ptr: ";
156 errs() << "[" << Size
<< "B] ";
157 WriteAsOperand(errs(), P
, true, M
);
158 errs() << "\t<->" << *CS
.getInstruction();