LLVM  3.7.0
AliasAnalysisCounter.cpp
Go to the documentation of this file.
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/Passes.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Pass.h"
20 #include "llvm/Support/Debug.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 namespace {
31  class AliasAnalysisCounter : public ModulePass, public AliasAnalysis {
32  unsigned No, May, Partial, Must;
33  unsigned NoMR, JustRef, JustMod, MR;
34  Module *M;
35  public:
36  static char ID; // Class identification, replacement for typeinfo
37  AliasAnalysisCounter() : ModulePass(ID) {
39  No = May = Partial = Must = 0;
40  NoMR = JustRef = JustMod = MR = 0;
41  }
42 
43  void printLine(const char *Desc, unsigned Val, unsigned Sum) {
44  errs() << " " << Val << " " << Desc << " responses ("
45  << Val*100/Sum << "%)\n";
46  }
47  ~AliasAnalysisCounter() override {
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";
54  if (AASum) {
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";
63  }
64 
65  errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n";
66  if (MRSum) {
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";
74  }
75  }
76  }
77 
78  bool runOnModule(Module &M) override {
79  this->M = &M;
80  InitializeAliasAnalysis(this, &M.getDataLayout());
81  return false;
82  }
83 
84  void getAnalysisUsage(AnalysisUsage &AU) const override {
87  AU.setPreservesAll();
88  }
89 
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  void *getAdjustedAnalysisPointer(AnalysisID PI) override {
95  if (PI == &AliasAnalysis::ID)
96  return (AliasAnalysis*)this;
97  return this;
98  }
99 
100  // FIXME: We could count these too...
101  bool pointsToConstantMemory(const MemoryLocation &Loc,
102  bool OrLocal) override {
103  return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal);
104  }
105 
106  // Forwarding functions: just delegate to a real AA implementation, counting
107  // the number of responses...
108  AliasResult alias(const MemoryLocation &LocA,
109  const MemoryLocation &LocB) override;
110 
111  ModRefResult getModRefInfo(ImmutableCallSite CS,
112  const MemoryLocation &Loc) override;
113  ModRefResult getModRefInfo(ImmutableCallSite CS1,
114  ImmutableCallSite CS2) override {
115  return AliasAnalysis::getModRefInfo(CS1,CS2);
116  }
117  };
118 }
119 
120 char AliasAnalysisCounter::ID = 0;
121 INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa",
122  "Count Alias Analysis Query Responses", false, true, false)
123 
125  return new AliasAnalysisCounter();
126 }
127 
128 AliasResult AliasAnalysisCounter::alias(const MemoryLocation &LocA,
129  const MemoryLocation &LocB) {
130  AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB);
131 
132  const char *AliasString = nullptr;
133  switch (R) {
134  case NoAlias: No++; AliasString = "No alias"; break;
135  case MayAlias: May++; AliasString = "May alias"; break;
136  case PartialAlias: Partial++; AliasString = "Partial alias"; break;
137  case MustAlias: Must++; AliasString = "Must alias"; break;
138  }
139 
140  if (PrintAll || (PrintAllFailures && R == MayAlias)) {
141  errs() << AliasString << ":\t";
142  errs() << "[" << LocA.Size << "B] ";
143  LocA.Ptr->printAsOperand(errs(), true, M);
144  errs() << ", ";
145  errs() << "[" << LocB.Size << "B] ";
146  LocB.Ptr->printAsOperand(errs(), true, M);
147  errs() << "\n";
148  }
149 
150  return R;
151 }
152 
154 AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS,
155  const MemoryLocation &Loc) {
156  ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc);
157 
158  const char *MRString = nullptr;
159  switch (R) {
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;
164  }
165 
166  if (PrintAll || (PrintAllFailures && R == ModRef)) {
167  errs() << MRString << ": Ptr: ";
168  errs() << "[" << Loc.Size << "B] ";
169  Loc.Ptr->printAsOperand(errs(), true, M);
170  errs() << "\t<->" << *CS.getInstruction() << '\n';
171  }
172  return R;
173 }
The two locations precisely alias each other.
Definition: AliasAnalysis.h:84
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
InstrTy * getInstruction() const
Definition: CallSite.h:82
The two locations alias, but only due to a partial overlap.
Definition: AliasAnalysis.h:82
The two locations do not alias at all.
Definition: AliasAnalysis.h:78
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:80
AnalysisUsage & addRequired()
INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis,"count-aa","Count Alias Analysis Query Responses", false, true, false) ModulePass *llvm
ModulePass * createAliasAnalysisCounterPass()
void initializeAliasAnalysisCounterPass(PassRegistry &)
static cl::opt< bool > PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden)
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:3287
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
static cl::opt< bool > PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true))
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill, StringRef Suffix)
Module.h This file contains the declarations for the Module class.
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
const void * AnalysisID
Definition: Pass.h:47
ModRefResult
Simple mod/ref information...
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...
uint64_t Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...