1 //===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Pass.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"
23 using namespace llvm;
24
25 static cl::opt<bool>
26 PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
27 static cl::opt<bool>
28 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
29
30 namespace {
31 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
32 unsigned No, May, Partial, Must;
33 unsigned NoMR, JustRef, JustMod, MR;
34 Module *M;
35 public:
36 static char ID; // Class identification, replacement for typeinfo
AliasAnalysisCounter()37 AliasAnalysisCounter() : ModulePass(ID) {
38 initializeAliasAnalysisCounterPass(*PassRegistry::getPassRegistry());
39 No = May = Partial = Must = 0;
40 NoMR = JustRef = JustMod = MR = 0;
41 }
42
printLine(const char * Desc,unsigned Val,unsigned Sum)43 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
44 errs() << " " << Val << " " << Desc << " responses ("
45 << Val*100/Sum << "%)\n";
46 }
~AliasAnalysisCounter()47 ~AliasAnalysisCounter() override {
48 unsigned AASum = No+May+Partial+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";
54 if (AASum) {
55 printLine("no alias", No, AASum);
56 printLine("may alias", May, AASum);
57 printLine("partial alias", Partial, AASum);
58 printLine("must alias", Must, AASum);
59 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
60 << May*100/AASum << "%/"
61 << Partial*100/AASum << "%/"
62 << Must*100/AASum<<"%\n\n";
63 }
64
65 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
66 if (MRSum) {
67 printLine("no mod/ref", NoMR, MRSum);
68 printLine("ref", JustRef, MRSum);
69 printLine("mod", JustMod, MRSum);
70 printLine("mod/ref", MR, MRSum);
71 errs() << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
72 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
73 << "%/" << MR*100/MRSum <<"%\n\n";
74 }
75 }
76 }
77
runOnModule(Module & M)78 bool runOnModule(Module &M) override {
79 this->M = &M;
80 InitializeAliasAnalysis(this, &M.getDataLayout());
81 return false;
82 }
83
getAnalysisUsage(AnalysisUsage & AU) const84 void getAnalysisUsage(AnalysisUsage &AU) const override {
85 AliasAnalysis::getAnalysisUsage(AU);
86 AU.addRequired<AliasAnalysis>();
87 AU.setPreservesAll();
88 }
89
90 /// getAdjustedAnalysisPointer - This method is used when a pass implements
91 /// an analysis interface through multiple inheritance. If needed, it
92 /// should override this to adjust the this pointer as needed for the
93 /// specified pass info.
getAdjustedAnalysisPointer(AnalysisID PI)94 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
95 if (PI == &AliasAnalysis::ID)
96 return (AliasAnalysis*)this;
97 return this;
98 }
99
100 // FIXME: We could count these too...
pointsToConstantMemory(const Location & Loc,bool OrLocal)101 bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override {
102 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal);
103 }
104
105 // Forwarding functions: just delegate to a real AA implementation, counting
106 // the number of responses...
107 AliasResult alias(const Location &LocA, const Location &LocB) override;
108
109 ModRefResult getModRefInfo(ImmutableCallSite CS,
110 const Location &Loc) override;
getModRefInfo(ImmutableCallSite CS1,ImmutableCallSite CS2)111 ModRefResult getModRefInfo(ImmutableCallSite CS1,
112 ImmutableCallSite CS2) override {
113 return AliasAnalysis::getModRefInfo(CS1,CS2);
114 }
115 };
116 }
117
118 char AliasAnalysisCounter::ID = 0;
119 INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa",
120 "Count Alias Analysis Query Responses", false, true, false)
121
createAliasAnalysisCounterPass()122 ModulePass *llvm::createAliasAnalysisCounterPass() {
123 return new AliasAnalysisCounter();
124 }
125
126 AliasAnalysis::AliasResult
alias(const Location & LocA,const Location & LocB)127 AliasAnalysisCounter::alias(const Location &LocA, const Location &LocB) {
128 AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB);
129
130 const char *AliasString = nullptr;
131 switch (R) {
132 case NoAlias: No++; AliasString = "No alias"; break;
133 case MayAlias: May++; AliasString = "May alias"; break;
134 case PartialAlias: Partial++; AliasString = "Partial alias"; break;
135 case MustAlias: Must++; AliasString = "Must alias"; break;
136 }
137
138 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
139 errs() << AliasString << ":\t";
140 errs() << "[" << LocA.Size << "B] ";
141 LocA.Ptr->printAsOperand(errs(), true, M);
142 errs() << ", ";
143 errs() << "[" << LocB.Size << "B] ";
144 LocB.Ptr->printAsOperand(errs(), true, M);
145 errs() << "\n";
146 }
147
148 return R;
149 }
150
151 AliasAnalysis::ModRefResult
getModRefInfo(ImmutableCallSite CS,const Location & Loc)152 AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
153 const Location &Loc) {
154 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc);
155
156 const char *MRString = nullptr;
157 switch (R) {
158 case NoModRef: NoMR++; MRString = "NoModRef"; break;
159 case Ref: JustRef++; MRString = "JustRef"; break;
160 case Mod: JustMod++; MRString = "JustMod"; break;
161 case ModRef: MR++; MRString = "ModRef"; break;
162 }
163
164 if (PrintAll || (PrintAllFailures && R == ModRef)) {
165 errs() << MRString << ": Ptr: ";
166 errs() << "[" << Loc.Size << "B] ";
167 Loc.Ptr->printAsOperand(errs(), true, M);
168 errs() << "\t<->" << *CS.getInstruction() << '\n';
169 }
170 return R;
171 }
172