blob: 25ace732e2dd4d7c7583cc256af97057a6d168cf [file] [log] [blame]
Chris Lattner70e332d2003-02-07 19:46:03 +00001//===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner70e332d2003-02-07 19:46:03 +00009//
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
Jeff Cohen534927d2005-01-08 22:01:16 +000015#include "llvm/Analysis/Passes.h"
Chris Lattner70e332d2003-02-07 19:46:03 +000016#include "llvm/Pass.h"
Chris Lattnercf9f2012005-03-23 21:59:07 +000017#include "llvm/Analysis/AliasAnalysis.h"
18#include "llvm/Assembly/Writer.h"
19#include "llvm/Support/CommandLine.h"
Chris Lattner70e332d2003-02-07 19:46:03 +000020#include <iostream>
Chris Lattner381bf792003-12-10 15:34:03 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner70e332d2003-02-07 19:46:03 +000023namespace {
Chris Lattnercf9f2012005-03-23 21:59:07 +000024 cl::opt<bool>
25 PrintAll("count-aa-print-all-queries", cl::ReallyHidden);
26 cl::opt<bool>
27 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
28
Chris Lattnerb12914b2004-09-20 04:48:05 +000029 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
Chris Lattnera89feb52003-02-07 20:39:17 +000030 unsigned No, May, Must;
Chris Lattnerd80651d2003-02-26 19:27:05 +000031 unsigned NoMR, JustRef, JustMod, MR;
Chris Lattnera89feb52003-02-07 20:39:17 +000032 const char *Name;
Chris Lattnercf9f2012005-03-23 21:59:07 +000033 Module *M;
Chris Lattnera89feb52003-02-07 20:39:17 +000034 public:
Chris Lattnerd80651d2003-02-26 19:27:05 +000035 AliasAnalysisCounter() {
36 No = May = Must = 0;
37 NoMR = JustRef = JustMod = MR = 0;
38 }
39
40 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
41 std::cerr << " " << Val << " " << Desc << " responses ("
42 << Val*100/Sum << "%)\n";
43 }
Chris Lattnera89feb52003-02-07 20:39:17 +000044 ~AliasAnalysisCounter() {
Chris Lattnerd80651d2003-02-26 19:27:05 +000045 unsigned AASum = No+May+Must;
46 unsigned MRSum = NoMR+JustRef+JustMod+MR;
47 if (AASum + MRSum) { // Print a report if any counted queries occurred...
Chris Lattnera89feb52003-02-07 20:39:17 +000048 std::cerr
49 << "\n===== Alias Analysis Counter Report =====\n"
50 << " Analysis counted: " << Name << "\n"
Chris Lattnerd80651d2003-02-26 19:27:05 +000051 << " " << AASum << " Total Alias Queries Performed\n";
Chris Lattnerefa095e2003-06-17 15:07:20 +000052 if (AASum) {
53 printLine("no alias", No, AASum);
54 printLine("may alias", May, AASum);
55 printLine("must alias", Must, AASum);
56 std::cerr
57 << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
58 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
59 }
Chris Lattnerd80651d2003-02-26 19:27:05 +000060
61 std::cerr
62 << " " << MRSum << " Total Mod/Ref Queries Performed\n";
Chris Lattnerefa095e2003-06-17 15:07:20 +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);
68 std::cerr
69 << " Mod/Ref Analysis Counter Summary: " << NoMR*100/MRSum<< "%/"
Misha Brukman2b37d7c2005-04-21 21:13:18 +000070 << JustRef*100/MRSum << "%/" << JustMod*100/MRSum << "%/"
Chris Lattnerefa095e2003-06-17 15:07:20 +000071 << MR*100/MRSum <<"%\n\n";
72 }
Chris Lattnera89feb52003-02-07 20:39:17 +000073 }
74 }
Chris Lattner70e332d2003-02-07 19:46:03 +000075
Chris Lattnerb12914b2004-09-20 04:48:05 +000076 bool runOnModule(Module &M) {
Chris Lattnercf9f2012005-03-23 21:59:07 +000077 this->M = &M;
Chris Lattnerd80651d2003-02-26 19:27:05 +000078 InitializeAliasAnalysis(this);
Chris Lattnera89feb52003-02-07 20:39:17 +000079 Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
80 return false;
81 }
82
Chris Lattner70e332d2003-02-07 19:46:03 +000083 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerd80651d2003-02-26 19:27:05 +000084 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner70e332d2003-02-07 19:46:03 +000085 AU.addRequired<AliasAnalysis>();
86 AU.setPreservesAll();
87 }
88
Chris Lattnerb79d7922004-03-15 04:06:46 +000089 // FIXME: We could count these too...
90 bool pointsToConstantMemory(const Value *P) {
91 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
92 }
93 bool doesNotAccessMemory(Function *F) {
94 return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F);
95 }
96 bool onlyReadsMemory(Function *F) {
97 return getAnalysis<AliasAnalysis>().onlyReadsMemory(F);
98 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000099
100
Chris Lattner70e332d2003-02-07 19:46:03 +0000101 // Forwarding functions: just delegate to a real AA implementation, counting
102 // the number of responses...
Chris Lattnerd80651d2003-02-26 19:27:05 +0000103 AliasResult alias(const Value *V1, unsigned V1Size,
Chris Lattnercf9f2012005-03-23 21:59:07 +0000104 const Value *V2, unsigned V2Size);
105
106 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Reid Spencer4a7ebfa2004-12-07 08:11:24 +0000107 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
108 return AliasAnalysis::getModRefInfo(CS1,CS2);
109 }
Chris Lattner70e332d2003-02-07 19:46:03 +0000110 };
111
Chris Lattner7f8897f2006-08-27 22:42:52 +0000112 RegisterPass<AliasAnalysisCounter>
Chris Lattner70e332d2003-02-07 19:46:03 +0000113 X("count-aa", "Count Alias Analysis Query Responses");
Chris Lattnera5370172006-08-28 00:42:29 +0000114 RegisterAnalysisGroup<AliasAnalysis> Y(X);
Chris Lattner70e332d2003-02-07 19:46:03 +0000115}
Jeff Cohen534927d2005-01-08 22:01:16 +0000116
117ModulePass *llvm::createAliasAnalysisCounterPass() {
118 return new AliasAnalysisCounter();
119}
Chris Lattnercf9f2012005-03-23 21:59:07 +0000120
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000121AliasAnalysis::AliasResult
Chris Lattnercf9f2012005-03-23 21:59:07 +0000122AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
123 const Value *V2, unsigned V2Size) {
124 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000125
Chris Lattnercf9f2012005-03-23 21:59:07 +0000126 const char *AliasString;
127 switch (R) {
128 default: assert(0 && "Unknown alias type!");
129 case NoAlias: No++; AliasString = "No alias"; break;
130 case MayAlias: May++; AliasString = "May alias"; break;
131 case MustAlias: Must++; AliasString = "Must alias"; break;
132 }
133
134 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
135 std::cerr << AliasString << ":\t";
136 std::cerr << "[" << V1Size << "B] ";
137 WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
138 std::cerr << "[" << V2Size << "B] ";
139 WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
140 }
141
142 return R;
143}
144
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000145AliasAnalysis::ModRefResult
Chris Lattnercf9f2012005-03-23 21:59:07 +0000146AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
147 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
148
149 const char *MRString;
150 switch (R) {
151 default: assert(0 && "Unknown mod/ref type!");
152 case NoModRef: NoMR++; MRString = "NoModRef"; break;
153 case Ref: JustRef++; MRString = "JustRef"; break;
154 case Mod: JustMod++; MRString = "JustMod"; break;
155 case ModRef: MR++; MRString = "ModRef"; break;
156 }
157
158 if (PrintAll || (PrintAllFailures && R == ModRef)) {
159 std::cerr << MRString << ": Ptr: ";
160 std::cerr << "[" << Size << "B] ";
161 WriteAsOperand(std::cerr, P, true, true, M);
162 std::cerr << "\t<->" << *CS.getInstruction();
163 }
164 return R;
165}