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/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
25 PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
27 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
30 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
31 unsigned No, May, Must;
32 unsigned NoMR, JustRef, JustMod, MR;
36 static char ID; // Class identification, replacement for typeinfo
37 AliasAnalysisCounter() : ModulePass(&ID) {
39 NoMR = JustRef = JustMod = MR = 0;
42 void printLine(const char *Desc, unsigned Val, unsigned Sum) {
43 errs() << " " << Val << " " << Desc << " responses ("
44 << Val*100/Sum << "%)\n";
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...
50 errs() << "\n===== Alias Analysis Counter Report =====\n"
51 << " Analysis counted: " << Name << "\n"
52 << " " << AASum << " Total Alias Queries Performed\n";
54 printLine("no alias", No, AASum);
55 printLine("may alias", May, AASum);
56 printLine("must alias", Must, AASum);
57 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/"
58 << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n";
61 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
63 printLine("no mod/ref", NoMR, MRSum);
64 printLine("ref", JustRef, MRSum);
65 printLine("mod", JustMod, MRSum);
66 printLine("mod/ref", MR, MRSum);
67 errs() << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum
68 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum
69 << "%/" << MR*100/MRSum <<"%\n\n";
74 bool runOnModule(Module &M) {
76 InitializeAliasAnalysis(this);
77 Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName();
81 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
82 AliasAnalysis::getAnalysisUsage(AU);
83 AU.addRequired<AliasAnalysis>();
87 // FIXME: We could count these too...
88 bool pointsToConstantMemory(const Value *P) {
89 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
92 // Forwarding functions: just delegate to a real AA implementation, counting
93 // the number of responses...
94 AliasResult alias(const Value *V1, unsigned V1Size,
95 const Value *V2, unsigned V2Size);
97 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
98 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
99 return AliasAnalysis::getModRefInfo(CS1,CS2);
104 char AliasAnalysisCounter::ID = 0;
105 static RegisterPass<AliasAnalysisCounter>
106 X("count-aa", "Count Alias Analysis Query Responses", false, true);
107 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
109 ModulePass *llvm::createAliasAnalysisCounterPass() {
110 return new AliasAnalysisCounter();
113 AliasAnalysis::AliasResult
114 AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size,
115 const Value *V2, unsigned V2Size) {
116 AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
118 const char *AliasString;
120 default: llvm_unreachable("Unknown alias type!");
121 case NoAlias: No++; AliasString = "No alias"; break;
122 case MayAlias: May++; AliasString = "May alias"; break;
123 case MustAlias: Must++; AliasString = "Must alias"; break;
126 if (PrintAll || (PrintAllFailures && R == MayAlias)) {
127 errs() << AliasString << ":\t";
128 errs() << "[" << V1Size << "B] ";
129 WriteAsOperand(errs(), V1, true, M);
131 errs() << "[" << V2Size << "B] ";
132 WriteAsOperand(errs(), V2, true, M);
139 AliasAnalysis::ModRefResult
140 AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
141 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size);
143 const char *MRString;
145 default: llvm_unreachable("Unknown mod/ref type!");
146 case NoModRef: NoMR++; MRString = "NoModRef"; break;
147 case Ref: JustRef++; MRString = "JustRef"; break;
148 case Mod: JustMod++; MRString = "JustMod"; break;
149 case ModRef: MR++; MRString = "ModRef"; break;
152 if (PrintAll || (PrintAllFailures && R == ModRef)) {
153 errs() << MRString << ": Ptr: ";
154 errs() << "[" << Size << "B] ";
155 WriteAsOperand(errs(), P, true, M);
156 errs() << "\t<->" << *CS.getInstruction();