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/Debug.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 AliasAnalysisCounter
: public ModulePass
, public AliasAnalysis
{
32 unsigned No
, May
, Must
;
33 unsigned NoMR
, JustRef
, JustMod
, MR
;
36 static char ID
; // Class identification, replacement for typeinfo
37 AliasAnalysisCounter() : ModulePass(ID
) {
38 initializeAliasAnalysisCounterPass(*PassRegistry::getPassRegistry());
40 NoMR
= JustRef
= JustMod
= MR
= 0;
43 void printLine(const char *Desc
, unsigned Val
, unsigned Sum
) {
44 errs() << " " << 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 errs() << "\n===== Alias Analysis Counter Report =====\n"
52 << " Analysis counted:\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 errs() << " Alias Analysis Counter Summary: " << No
*100/AASum
<< "%/"
59 << May
*100/AASum
<< "%/" << Must
*100/AASum
<<"%\n\n";
62 errs() << " " << 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 errs() << " 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);
81 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
82 AliasAnalysis::getAnalysisUsage(AU
);
83 AU
.addRequired
<AliasAnalysis
>();
87 /// getAdjustedAnalysisPointer - This method is used when a pass implements
88 /// an analysis interface through multiple inheritance. If needed, it
89 /// should override this to adjust the this pointer as needed for the
90 /// specified pass info.
91 virtual void *getAdjustedAnalysisPointer(AnalysisID PI
) {
92 if (PI
== &AliasAnalysis::ID
)
93 return (AliasAnalysis
*)this;
97 // FIXME: We could count these too...
98 bool pointsToConstantMemory(const Location
&Loc
) {
99 return getAnalysis
<AliasAnalysis
>().pointsToConstantMemory(Loc
);
102 // Forwarding functions: just delegate to a real AA implementation, counting
103 // the number of responses...
104 AliasResult
alias(const Location
&LocA
, const Location
&LocB
);
106 ModRefResult
getModRefInfo(ImmutableCallSite CS
,
107 const Location
&Loc
);
108 ModRefResult
getModRefInfo(ImmutableCallSite CS1
,
109 ImmutableCallSite CS2
) {
110 return AliasAnalysis::getModRefInfo(CS1
,CS2
);
115 char AliasAnalysisCounter::ID
= 0;
116 INITIALIZE_AG_PASS(AliasAnalysisCounter
, AliasAnalysis
, "count-aa",
117 "Count Alias Analysis Query Responses", false, true, false)
119 ModulePass
*llvm::createAliasAnalysisCounterPass() {
120 return new AliasAnalysisCounter();
123 AliasAnalysis::AliasResult
124 AliasAnalysisCounter::alias(const Location
&LocA
, const Location
&LocB
) {
125 AliasResult R
= getAnalysis
<AliasAnalysis
>().alias(LocA
, LocB
);
127 const char *AliasString
;
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;
135 if (PrintAll
|| (PrintAllFailures
&& R
== MayAlias
)) {
136 errs() << AliasString
<< ":\t";
137 errs() << "[" << LocA
.Size
<< "B] ";
138 WriteAsOperand(errs(), LocA
.Ptr
, true, M
);
140 errs() << "[" << LocB
.Size
<< "B] ";
141 WriteAsOperand(errs(), LocB
.Ptr
, true, M
);
148 AliasAnalysis::ModRefResult
149 AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS
,
150 const Location
&Loc
) {
151 ModRefResult R
= getAnalysis
<AliasAnalysis
>().getModRefInfo(CS
, Loc
);
153 const char *MRString
;
155 default: llvm_unreachable("Unknown mod/ref type!");
156 case NoModRef
: NoMR
++; MRString
= "NoModRef"; break;
157 case Ref
: JustRef
++; MRString
= "JustRef"; break;
158 case Mod
: JustMod
++; MRString
= "JustMod"; break;
159 case ModRef
: MR
++; MRString
= "ModRef"; break;
162 if (PrintAll
|| (PrintAllFailures
&& R
== ModRef
)) {
163 errs() << MRString
<< ": Ptr: ";
164 errs() << "[" << Loc
.Size
<< "B] ";
165 WriteAsOperand(errs(), Loc
.Ptr
, true, M
);
166 errs() << "\t<->" << *CS
.getInstruction() << '\n';