blob: cfb354915b6b0e7277b56660175961909d561b8b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/Pass.h"
17#include "llvm/Analysis/AliasAnalysis.h"
18#include "llvm/Assembly/Writer.h"
19#include "llvm/Support/CommandLine.h"
David Greeneba1ef8b2009-12-23 19:15:13 +000020#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner8a6411c2009-08-23 04:37:46 +000022#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
Dan Gohman089efff2008-05-13 00:00:25 +000025static cl::opt<bool>
Chris Lattner9da50552009-08-30 04:25:40 +000026PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
Dan Gohman089efff2008-05-13 00:00:25 +000027static cl::opt<bool>
28PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029
Dan Gohman089efff2008-05-13 00:00:25 +000030namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000031 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 unsigned No, May, Must;
33 unsigned NoMR, JustRef, JustMod, MR;
34 const char *Name;
35 Module *M;
36 public:
37 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000038 AliasAnalysisCounter() : ModulePass(&ID) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 No = May = Must = 0;
40 NoMR = JustRef = JustMod = MR = 0;
41 }
42
43 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
David Greene092316b2009-12-23 22:49:57 +000044 errs() << " " << Val << " " << Desc << " responses ("
Chris Lattnera7a9daa2009-08-23 05:17:37 +000045 << Val*100/Sum << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 }
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...
David Greene092316b2009-12-23 22:49:57 +000051 errs() << "\n===== Alias Analysis Counter Report =====\n"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000052 << " Analysis counted: " << Name << "\n"
53 << " " << AASum << " Total Alias Queries Performed\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 if (AASum) {
55 printLine("no alias", No, AASum);
56 printLine("may alias", May, AASum);
57 printLine("must alias", Must, AASum);
David Greene092316b2009-12-23 22:49:57 +000058 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000059 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 }
61
David Greene092316b2009-12-23 22:49:57 +000062 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 if (MRSum) {
64 printLine("no mod/ref", NoMR, MRSum);
65 printLine("ref", JustRef, MRSum);
66 printLine("mod", JustMod, MRSum);
67 printLine("mod/ref", MR, MRSum);
David Greene092316b2009-12-23 22:49:57 +000068 errs() << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
Chris Lattnera7a9daa2009-08-23 05:17:37 +000069 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
70 << "%/" << MR*100/MRSum <<"%\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 }
72 }
73 }
74
75 bool runOnModule(Module &M) {
76 this->M = &M;
77 InitializeAliasAnalysis(this);
78 Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
79 return false;
80 }
81
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83 AliasAnalysis::getAnalysisUsage(AU);
84 AU.addRequired<AliasAnalysis>();
85 AU.setPreservesAll();
86 }
87
Chris Lattner55e98922010-01-20 20:09:02 +000088 /// getAdjustedAnalysisPointer - This method is used when a pass implements
89 /// an analysis interface through multiple inheritance. If needed, it
90 /// should override this to adjust the this pointer as needed for the
91 /// specified pass info.
92 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
93 if (PI->isPassID(&AliasAnalysis::ID))
94 return (AliasAnalysis*)this;
95 return this;
96 }
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 // FIXME: We could count these too...
99 bool pointsToConstantMemory(const Value *P) {
100 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
101 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 // Forwarding functions: just delegate to a real AA implementation, counting
104 // the number of responses...
105 AliasResult alias(const Value *V1, unsigned V1Size,
106 const Value *V2, unsigned V2Size);
107
108 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
109 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
110 return AliasAnalysis::getModRefInfo(CS1,CS2);
111 }
112 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113}
114
Dan Gohman089efff2008-05-13 00:00:25 +0000115char AliasAnalysisCounter::ID = 0;
116static RegisterPass<AliasAnalysisCounter>
117X("count-aa", "Count Alias Analysis Query Responses", false, true);
118static RegisterAnalysisGroup<AliasAnalysis> Y(X);
119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120ModulePass *llvm::createAliasAnalysisCounterPass() {
121 return new AliasAnalysisCounter();
122}
123
124AliasAnalysis::AliasResult
125AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
126 const Value *V2, unsigned V2Size) {
127 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
128
129 const char *AliasString;
130 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000131 default: llvm_unreachable("Unknown alias type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 case NoAlias: No++; AliasString = "No alias"; break;
133 case MayAlias: May++; AliasString = "May alias"; break;
134 case MustAlias: Must++; AliasString = "Must alias"; break;
135 }
136
137 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
David Greene092316b2009-12-23 22:49:57 +0000138 errs() << AliasString << ":\t";
139 errs() << "[" << V1Size << "B] ";
140 WriteAsOperand(errs(), V1, true, M);
141 errs() << ", ";
142 errs() << "[" << V2Size << "B] ";
143 WriteAsOperand(errs(), V2, true, M);
144 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 }
146
147 return R;
148}
149
150AliasAnalysis::ModRefResult
151AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
152 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
153
154 const char *MRString;
155 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000156 default: llvm_unreachable("Unknown mod/ref type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 case NoModRef: NoMR++; MRString = "NoModRef"; break;
158 case Ref: JustRef++; MRString = "JustRef"; break;
159 case Mod: JustMod++; MRString = "JustMod"; break;
160 case ModRef: MR++; MRString = "ModRef"; break;
161 }
162
163 if (PrintAll || (PrintAllFailures && R == ModRef)) {
David Greene092316b2009-12-23 22:49:57 +0000164 errs() << MRString << ": Ptr: ";
165 errs() << "[" << Size << "B] ";
166 WriteAsOperand(errs(), P, true, M);
167 errs() << "\t<->" << *CS.getInstruction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 }
169 return R;
170}