[PM/AA] Hoist the AA counter pass into a header to match the analysis
[oota-llvm.git] / lib / Analysis / AliasAnalysisCounter.cpp
1 //===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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/AliasAnalysisCounter.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Pass.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"
23 using namespace llvm;
24
25 static cl::opt<bool>
26 PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true));
27 static cl::opt<bool>
28 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden);
29
30 char AliasAnalysisCounter::ID = 0;
31 INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa",
32                    "Count Alias Analysis Query Responses", false, true, false)
33
34 ModulePass *llvm::createAliasAnalysisCounterPass() {
35   return new AliasAnalysisCounter();
36 }
37
38 AliasResult AliasAnalysisCounter::alias(const MemoryLocation &LocA,
39                                         const MemoryLocation &LocB) {
40   AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB);
41
42   const char *AliasString = nullptr;
43   switch (R) {
44   case NoAlias:   No++;   AliasString = "No alias"; break;
45   case MayAlias:  May++;  AliasString = "May alias"; break;
46   case PartialAlias: Partial++; AliasString = "Partial alias"; break;
47   case MustAlias: Must++; AliasString = "Must alias"; break;
48   }
49
50   if (PrintAll || (PrintAllFailures && R == MayAlias)) {
51     errs() << AliasString << ":\t";
52     errs() << "[" << LocA.Size << "B] ";
53     LocA.Ptr->printAsOperand(errs(), true, M);
54     errs() << ", ";
55     errs() << "[" << LocB.Size << "B] ";
56     LocB.Ptr->printAsOperand(errs(), true, M);
57     errs() << "\n";
58   }
59
60   return R;
61 }
62
63 ModRefInfo AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
64                                                const MemoryLocation &Loc) {
65   ModRefInfo R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc);
66
67   const char *MRString = nullptr;
68   switch (R) {
69   case MRI_NoModRef:
70     NoMR++;
71     MRString = "MRI_NoModRef";
72     break;
73   case MRI_Ref:
74     JustRef++;
75     MRString = "JustRef";
76     break;
77   case MRI_Mod:
78     JustMod++;
79     MRString = "JustMod";
80     break;
81   case MRI_ModRef:
82     MR++;
83     MRString = "MRI_ModRef";
84     break;
85   }
86
87   if (PrintAll || (PrintAllFailures && R == MRI_ModRef)) {
88     errs() << MRString << ":  Ptr: ";
89     errs() << "[" << Loc.Size << "B] ";
90     Loc.Ptr->printAsOperand(errs(), true, M);
91     errs() << "\t<->" << *CS.getInstruction() << '\n';
92   }
93   return R;
94 }