1 //===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
26 PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
28 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
31 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
32 unsigned No, May, Partial, Must;
33 unsigned NoMR, JustRef, JustMod, MR;
36 static char ID; // Class identification, replacement for typeinfo
37 AliasAnalysisCounter() : ModulePass(ID) {
38 initializeAliasAnalysisCounterPass(*PassRegistry::getPassRegistry());
39 No = May = Partial = Must = 0;
40 NoMR = JustRef = JustMod = MR = 0;
43 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
44 errs() << " " << Val << " " << Desc << " responses ("
45 << Val*100/Sum << "%)\n";
47 ~AliasAnalysisCounter() {
48 unsigned AASum = No+May+Partial+Must;
49 unsigned MRSum = NoMR+JustRef+JustMod+MR;
50 if (AASum + MRSum) { // Print a report if any counted queries occurred...
51 errs() << "\n===== Alias Analysis Counter Report =====\n"
52 << " Analysis counted:\n"
53 << " " << AASum << " Total Alias Queries Performed\n";
55 printLine("no alias", No, AASum);
56 printLine("may alias", May, AASum);
57 printLine("partial alias", Partial, AASum);
58 printLine("must alias", Must, AASum);
59 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
60 << May*100/AASum << "%/"
61 << Partial*100/AASum << "%/"
62 << Must*100/AASum<<"%\n\n";
65 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
67 printLine("no mod/ref", NoMR, MRSum);
68 printLine("ref", JustRef, MRSum);
69 printLine("mod", JustMod, MRSum);
70 printLine("mod/ref", MR, MRSum);
71 errs() << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
72 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
73 << "%/" << MR*100/MRSum <<"%\n\n";
78 bool runOnModule(Module &M) {
80 InitializeAliasAnalysis(this);
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 AliasAnalysis::getAnalysisUsage(AU);
86 AU.addRequired<AliasAnalysis>();
90 /// getAdjustedAnalysisPointer - This method is used when a pass implements
91 /// an analysis interface through multiple inheritance. If needed, it
92 /// should override this to adjust the this pointer as needed for the
93 /// specified pass info.
94 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
95 if (PI == &AliasAnalysis::ID)
96 return (AliasAnalysis*)this;
100 // FIXME: We could count these too...
101 bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
102 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal);
105 // Forwarding functions: just delegate to a real AA implementation, counting
106 // the number of responses...
107 AliasResult alias(const Location &LocA, const Location &LocB);
109 ModRefResult getModRefInfo(ImmutableCallSite CS,
110 const Location &Loc);
111 ModRefResult getModRefInfo(ImmutableCallSite CS1,
112 ImmutableCallSite CS2) {
113 return AliasAnalysis::getModRefInfo(CS1,CS2);
118 char AliasAnalysisCounter::ID = 0;
119 INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa",
120 "Count Alias Analysis Query Responses", false, true, false)
122 ModulePass *llvm::createAliasAnalysisCounterPass() {
123 return new AliasAnalysisCounter();
126 AliasAnalysis::AliasResult
127 AliasAnalysisCounter::alias(const Location &LocA, const Location &LocB) {
128 AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB);
130 const char *AliasString;
132 default: llvm_unreachable("Unknown alias type!");
133 case NoAlias: No++; AliasString = "No alias"; break;
134 case MayAlias: May++; AliasString = "May alias"; break;
135 case PartialAlias: Partial++; AliasString = "Partial alias"; break;
136 case MustAlias: Must++; AliasString = "Must alias"; break;
139 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
140 errs() << AliasString << ":\t";
141 errs() << "[" << LocA.Size << "B] ";
142 WriteAsOperand(errs(), LocA.Ptr, true, M);
144 errs() << "[" << LocB.Size << "B] ";
145 WriteAsOperand(errs(), LocB.Ptr, true, M);
152 AliasAnalysis::ModRefResult
153 AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
154 const Location &Loc) {
155 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc);
157 const char *MRString;
159 default: llvm_unreachable("Unknown mod/ref type!");
160 case NoModRef: NoMR++; MRString = "NoModRef"; break;
161 case Ref: JustRef++; MRString = "JustRef"; break;
162 case Mod: JustMod++; MRString = "JustMod"; break;
163 case ModRef: MR++; MRString = "ModRef"; break;
166 if (PrintAll || (PrintAllFailures && R == ModRef)) {
167 errs() << MRString << ": Ptr: ";
168 errs() << "[" << Loc.Size << "B] ";
169 WriteAsOperand(errs(), Loc.Ptr, true, M);
170 errs() << "\t<->" << *CS.getInstruction() << '\n';