blob: 53715519fe98c2876c00bb1ad8b6efc3ec85bf90 [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 Greeneba1ef8b2009-12-23 19:15:13 +000044 dbgs() << " " << 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 Greeneba1ef8b2009-12-23 19:15:13 +000051 dbgs() << "\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 Greeneba1ef8b2009-12-23 19:15:13 +000058 dbgs() << " 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 Greeneba1ef8b2009-12-23 19:15:13 +000062 dbgs() << " " << 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 Greeneba1ef8b2009-12-23 19:15:13 +000068 dbgs() << " 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
88 // FIXME: We could count these too...
89 bool pointsToConstantMemory(const Value *P) {
90 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
91 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 // Forwarding functions: just delegate to a real AA implementation, counting
94 // the number of responses...
95 AliasResult alias(const Value *V1, unsigned V1Size,
96 const Value *V2, unsigned V2Size);
97
98 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
99 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
100 return AliasAnalysis::getModRefInfo(CS1,CS2);
101 }
102 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103}
104
Dan Gohman089efff2008-05-13 00:00:25 +0000105char AliasAnalysisCounter::ID = 0;
106static RegisterPass<AliasAnalysisCounter>
107X("count-aa", "Count Alias Analysis Query Responses", false, true);
108static RegisterAnalysisGroup<AliasAnalysis> Y(X);
109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110ModulePass *llvm::createAliasAnalysisCounterPass() {
111 return new AliasAnalysisCounter();
112}
113
114AliasAnalysis::AliasResult
115AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
116 const Value *V2, unsigned V2Size) {
117 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
118
119 const char *AliasString;
120 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000121 default: llvm_unreachable("Unknown alias type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 case NoAlias: No++; AliasString = "No alias"; break;
123 case MayAlias: May++; AliasString = "May alias"; break;
124 case MustAlias: Must++; AliasString = "Must alias"; break;
125 }
126
127 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
David Greeneba1ef8b2009-12-23 19:15:13 +0000128 dbgs() << AliasString << ":\t";
129 dbgs() << "[" << V1Size << "B] ";
130 WriteAsOperand(dbgs(), V1, true, M);
131 dbgs() << ", ";
132 dbgs() << "[" << V2Size << "B] ";
133 WriteAsOperand(dbgs(), V2, true, M);
134 dbgs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 }
136
137 return R;
138}
139
140AliasAnalysis::ModRefResult
141AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
142 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
143
144 const char *MRString;
145 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000146 default: llvm_unreachable("Unknown mod/ref type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 case NoModRef: NoMR++; MRString = "NoModRef"; break;
148 case Ref: JustRef++; MRString = "JustRef"; break;
149 case Mod: JustMod++; MRString = "JustMod"; break;
150 case ModRef: MR++; MRString = "ModRef"; break;
151 }
152
153 if (PrintAll || (PrintAllFailures && R == ModRef)) {
David Greeneba1ef8b2009-12-23 19:15:13 +0000154 dbgs() << MRString << ": Ptr: ";
155 dbgs() << "[" << Size << "B] ";
156 WriteAsOperand(dbgs(), P, true, M);
157 dbgs() << "\t<->" << *CS.getInstruction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 }
159 return R;
160}