blob: 48f7a9f223fe5278b4c1039f84b9c6a32d89121d [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
Owen Anderson75693222010-08-06 18:33:48 +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.
Owen Anderson75693222010-08-06 18:33:48 +000090 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
91 if (PI == &AliasAnalysis::ID)
Chris Lattner55e98922010-01-20 20:09:02 +000092 return (AliasAnalysis*)this;
93 return this;
94 }
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // FIXME: We could count these too...
Dan Gohmancfa06ab2010-09-14 21:25:10 +000097 bool pointsToConstantMemory(const Location &Loc) {
98 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 // Forwarding functions: just delegate to a real AA implementation, counting
102 // the number of responses...
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000103 AliasResult alias(const Location &LocA, const Location &LocB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
Dan Gohman503e2042010-08-03 21:48:53 +0000105 ModRefResult getModRefInfo(ImmutableCallSite CS,
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000106 const Location &Loc);
Dan Gohman503e2042010-08-03 21:48:53 +0000107 ModRefResult getModRefInfo(ImmutableCallSite CS1,
108 ImmutableCallSite CS2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return AliasAnalysis::getModRefInfo(CS1,CS2);
110 }
111 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112}
113
Dan Gohman089efff2008-05-13 00:00:25 +0000114char AliasAnalysisCounter::ID = 0;
Owen Andersonb2127882010-07-21 23:07:00 +0000115INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa",
116 "Count Alias Analysis Query Responses", false, true, false);
Dan Gohman089efff2008-05-13 00:00:25 +0000117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118ModulePass *llvm::createAliasAnalysisCounterPass() {
119 return new AliasAnalysisCounter();
120}
121
122AliasAnalysis::AliasResult
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000123AliasAnalysisCounter::alias(const Location &LocA, const Location &LocB) {
124 AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125
126 const char *AliasString;
127 switch (R) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000128 default: llvm_unreachable("Unknown alias type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 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)) {
David Greene092316b2009-12-23 22:49:57 +0000135 errs() << AliasString << ":\t";
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000136 errs() << "[" << LocA.Size << "B] ";
137 WriteAsOperand(errs(), LocA.Ptr, true, M);
David Greene092316b2009-12-23 22:49:57 +0000138 errs() << ", ";
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000139 errs() << "[" << LocB.Size << "B] ";
140 WriteAsOperand(errs(), LocB.Ptr, true, M);
David Greene092316b2009-12-23 22:49:57 +0000141 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143
144 return R;
145}
146
147AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +0000148AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000149 const Location &Loc) {
150 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
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: ";
Dan Gohmancfa06ab2010-09-14 21:25:10 +0000163 errs() << "[" << Loc.Size << "B] ";
164 WriteAsOperand(errs(), Loc.Ptr, true, M);
Dan Gohmana4daf672010-03-01 17:42:55 +0000165 errs() << "\t<->" << *CS.getInstruction() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 }
167 return R;
168}