AliasAnalysisEvaluator.cpp revision 954da37bb492b519f5c31dc360f2a142567e08b4
1//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a simple N^2 alias analysis accuracy evaluator.
11// Basically, for each function in the program, it simply queries to see how the
12// alias analysis implementation answers alias queries between each pair of
13// pointers in the function.
14//
15// This is inspired and adapted from code by: Naveen Neelakantam, Francesco
16// Spadini, and Wojciech Stryjewski.
17//
18//===----------------------------------------------------------------------===//
19
20#include "llvm/Function.h"
21#include "llvm/iOther.h"
22#include "llvm/iTerminators.h"
23#include "llvm/Pass.h"
24#include "llvm/Type.h"
25#include "llvm/Analysis/AliasAnalysis.h"
26#include "llvm/Assembly/Writer.h"
27#include "llvm/Support/InstIterator.h"
28#include "Support/CommandLine.h"
29#include <iostream>
30#include <set>
31
32using namespace llvm;
33
34namespace {
35  cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
36  cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
37  cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
38
39  cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
40  cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
41  cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
42  cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
43
44  class AAEval : public FunctionPass {
45    unsigned NoAlias, MayAlias, MustAlias;
46    unsigned NoModRef, Mod, Ref, ModRef;
47
48  public:
49    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50      AU.addRequired<AliasAnalysis>();
51      AU.setPreservesAll();
52    }
53
54    bool doInitialization(Module &M) {
55      NoAlias = MayAlias = MustAlias = 0;
56      NoModRef = Mod = Ref = ModRef = 0;
57      return false;
58    }
59
60    bool runOnFunction(Function &F);
61    bool doFinalization(Module &M);
62  };
63
64  RegisterOpt<AAEval>
65  X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
66}
67
68static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
69                                Module *M) {
70  if (P) {
71    std::cerr << "  " << Msg << ":\t";
72    WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
73    WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
74  }
75}
76
77bool AAEval::runOnFunction(Function &F) {
78  AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
79
80  std::set<Value *> Pointers;
81  std::set<CallSite> CallSites;
82
83  for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
84    if (isa<PointerType>(I->getType()))    // Add all pointer arguments
85      Pointers.insert(I);
86
87  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
88    if (isa<PointerType>(I->getType())) // Add all pointer instructions
89      Pointers.insert(&*I);
90    for (User::op_iterator OI = (*I).op_begin(); OI != (*I).op_end(); ++OI)
91      if (isa<PointerType>((*OI)->getType()))
92        Pointers.insert(*OI);
93  }
94
95  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
96    CallSite CS = CallSite::get(&*I);
97    if (CS.getInstruction()) CallSites.insert(CS);
98  }
99
100  if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
101      PrintNoModRef || PrintMod || PrintRef || PrintModRef)
102    std::cerr << "Function: " << F.getName() << "\n";
103
104  // iterate over the worklist, and run the full (n^2)/2 disambiguations
105  for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
106       I1 != E; ++I1)
107    for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2)
108      switch (AA.alias(*I1, 0, *I2, 0)) {
109      case AliasAnalysis::NoAlias:
110        PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
111        ++NoAlias; break;
112      case AliasAnalysis::MayAlias:
113        PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
114        ++MayAlias; break;
115      case AliasAnalysis::MustAlias:
116        PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
117        ++MustAlias; break;
118      default:
119        std::cerr << "Unknown alias query result!\n";
120      }
121
122  // Mod/ref alias analysis: compare all pairs of calls and values
123  for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
124       V != Ve; ++V)
125    for (std::set<CallSite>::iterator C = CallSites.begin(),
126           Ce = CallSites.end(); C != Ce; ++C) {
127      Instruction *I = C->getInstruction();
128      switch (AA.getModRefInfo(*C, *V, (*V)->getType()->getPrimitiveSize())) {
129      case AliasAnalysis::NoModRef:
130        PrintResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
131        ++NoModRef; break;
132      case AliasAnalysis::Mod:
133        PrintResults("Mod", PrintMod, I, *V, F.getParent());
134        ++Mod; break;
135      case AliasAnalysis::Ref:
136        PrintResults("Ref", PrintRef, I, *V, F.getParent());
137        ++Ref; break;
138      case AliasAnalysis::ModRef:
139        PrintResults("ModRef", PrintModRef, I, *V, F.getParent());
140        ++ModRef; break;
141      default:
142        std::cerr << "Unknown alias query result!\n";
143      }
144    }
145
146  return false;
147}
148
149bool AAEval::doFinalization(Module &M) {
150  unsigned AliasSum = NoAlias + MayAlias + MustAlias;
151  std::cerr << "===== Alias Analysis Evaluator Report =====\n";
152  if (AliasSum == 0) {
153    std::cerr << "  Alias Analysis Evaluator Summary: No pointers!\n";
154  } else {
155    std::cerr << "  " << AliasSum << " Total Alias Queries Performed\n";
156    std::cerr << "  " << NoAlias << " no alias responses ("
157              << NoAlias*100/AliasSum << "%)\n";
158    std::cerr << "  " << MayAlias << " may alias responses ("
159              << MayAlias*100/AliasSum << "%)\n";
160    std::cerr << "  " << MustAlias << " must alias responses ("
161              << MustAlias*100/AliasSum <<"%)\n";
162    std::cerr << "  Alias Analysis Evaluator Pointer Alias Summary: "
163              << NoAlias*100/AliasSum  << "%/" << MayAlias*100/AliasSum << "%/"
164              << MustAlias*100/AliasSum << "%\n";
165  }
166
167  // Display the summary for mod/ref analysis
168  unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
169  if (ModRefSum == 0) {
170    std::cerr << "  Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
171  } else {
172    std::cerr << "  " << ModRefSum << " Total ModRef Queries Performed\n";
173    std::cerr << "  " << NoModRef << " no mod/ref responses ("
174              << NoModRef*100/ModRefSum << "%)\n";
175    std::cerr << "  " << Mod << " mod responses ("
176              << Mod*100/ModRefSum << "%)\n";
177    std::cerr << "  " << Ref << " ref responses ("
178              << Ref*100/ModRefSum <<"%)\n";
179    std::cerr << "  " << ModRef << " mod & ref responses ("
180              << ModRef*100/ModRefSum <<"%)\n";
181    std::cerr << "  Alias Analysis Evaluator Mod/Ref Summary: "
182              << NoModRef*100/ModRefSum  << "%/" << Mod*100/ModRefSum << "%/"
183              << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
184  }
185
186  return false;
187}
188