blob: 761cd46783f648c1df294a4f290eff42b49d0beb [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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 Module *M;
35 public:
36 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000037 AliasAnalysisCounter() : ModulePass(&ID) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 No = May = Must = 0;
39 NoMR = JustRef = JustMod = MR = 0;
40 }
41
42 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
David Greene092316b2009-12-23 22:49:57 +000043 errs() << " " << Val << " " << Desc << " responses ("
Chris Lattnera7a9daa2009-08-23 05:17:37 +000044 << Val*100/Sum << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 }
46 ~AliasAnalysisCounter() {
47 unsigned AASum = No+May+Must;
48 unsigned MRSum = NoMR+JustRef+JustMod+MR;
49 if (AASum + MRSum) { // Print a report if any counted queries occurred...
David Greene092316b2009-12-23 22:49:57 +000050 errs() << "\n===== Alias Analysis Counter Report =====\n"
Chris Lattnerf8c29702010-01-22 05:52:51 +000051 << " Analysis counted:\n"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000052 << " " << AASum << " Total Alias Queries Performed\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 if (AASum) {
54 printLine("no alias", No, AASum);
55 printLine("may alias", May, AASum);
56 printLine("must alias", Must, AASum);
David Greene092316b2009-12-23 22:49:57 +000057 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000058 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 }
60
David Greene092316b2009-12-23 22:49:57 +000061 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 if (MRSum) {
63 printLine("no mod/ref", NoMR, MRSum);
64 printLine("ref", JustRef, MRSum);
65 printLine("mod", JustMod, MRSum);
66 printLine("mod/ref", MR, MRSum);
David Greene092316b2009-12-23 22:49:57 +000067 errs() << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
Chris Lattnera7a9daa2009-08-23 05:17:37 +000068 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
69 << "%/" << MR*100/MRSum <<"%\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 }
71 }
72 }
73
74 bool runOnModule(Module &M) {
75 this->M = &M;
76 InitializeAliasAnalysis(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 return false;
78 }
79
80 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
81 AliasAnalysis::getAnalysisUsage(AU);
82 AU.addRequired<AliasAnalysis>();
83 AU.setPreservesAll();
84 }
85
Chris Lattner55e98922010-01-20 20:09:02 +000086 /// getAdjustedAnalysisPointer - This method is used when a pass implements
87 /// an analysis interface through multiple inheritance. If needed, it
88 /// should override this to adjust the this pointer as needed for the
89 /// specified pass info.
90 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
91 if (PI->isPassID(&AliasAnalysis::ID))
92 return (AliasAnalysis*)this;
93 return this;
94 }
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // FIXME: We could count these too...
97 bool pointsToConstantMemory(const Value *P) {
98 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
99 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 // Forwarding functions: just delegate to a real AA implementation, counting
102 // the number of responses...
103 AliasResult alias(const Value *V1, unsigned V1Size,
104 const Value *V2, unsigned V2Size);
105
106 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
107 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
108 return AliasAnalysis::getModRefInfo(CS1,CS2);
109 }
110 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111}
112
Dan Gohman089efff2008-05-13 00:00:25 +0000113char AliasAnalysisCounter::ID = 0;
114static RegisterPass<AliasAnalysisCounter>
115X("count-aa", "Count Alias Analysis Query Responses", false, true);
116static RegisterAnalysisGroup<AliasAnalysis> Y(X);
117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118ModulePass *llvm::createAliasAnalysisCounterPass() {
119 return new AliasAnalysisCounter();
120}
121
122AliasAnalysis::AliasResult
123AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
124 const Value *V2, unsigned V2Size) {
125 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
126
127 const char *AliasString;
128 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000129 default: llvm_unreachable("Unknown alias type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 case NoAlias: No++; AliasString = "No alias"; break;
131 case MayAlias: May++; AliasString = "May alias"; break;
132 case MustAlias: Must++; AliasString = "Must alias"; break;
133 }
134
135 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
David Greene092316b2009-12-23 22:49:57 +0000136 errs() << AliasString << ":\t";
137 errs() << "[" << V1Size << "B] ";
138 WriteAsOperand(errs(), V1, true, M);
139 errs() << ", ";
140 errs() << "[" << V2Size << "B] ";
141 WriteAsOperand(errs(), V2, true, M);
142 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 }
144
145 return R;
146}
147
148AliasAnalysis::ModRefResult
149AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
150 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
151
152 const char *MRString;
153 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000154 default: llvm_unreachable("Unknown mod/ref type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 case NoModRef: NoMR++; MRString = "NoModRef"; break;
156 case Ref: JustRef++; MRString = "JustRef"; break;
157 case Mod: JustMod++; MRString = "JustMod"; break;
158 case ModRef: MR++; MRString = "ModRef"; break;
159 }
160
161 if (PrintAll || (PrintAllFailures && R == ModRef)) {
David Greene092316b2009-12-23 22:49:57 +0000162 errs() << MRString << ": Ptr: ";
163 errs() << "[" << Size << "B] ";
164 WriteAsOperand(errs(), P, true, M);
165 errs() << "\t<->" << *CS.getInstruction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 }
167 return R;
168}