Fix batch of converting RegisterPass<> to INTIALIZE_PASS().
[oota-llvm.git] / lib / Analysis / AliasAnalysisEvaluator.cpp
1 //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
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 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/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Analysis/Passes.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/InstIterator.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/ADT/SetVector.h"
33 using namespace llvm;
34
35 static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
36
37 static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
38 static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
39 static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
40
41 static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
42 static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
43 static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
44 static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
45
46 namespace {
47   class AAEval : public FunctionPass {
48     unsigned NoAlias, MayAlias, MustAlias;
49     unsigned NoModRef, Mod, Ref, ModRef;
50
51   public:
52     static char ID; // Pass identification, replacement for typeid
53     AAEval() : FunctionPass(&ID) {}
54
55     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56       AU.addRequired<AliasAnalysis>();
57       AU.setPreservesAll();
58     }
59
60     bool doInitialization(Module &M) {
61       NoAlias = MayAlias = MustAlias = 0;
62       NoModRef = Mod = Ref = ModRef = 0;
63
64       if (PrintAll) {
65         PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
66         PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
67       }
68       return false;
69     }
70
71     bool runOnFunction(Function &F);
72     bool doFinalization(Module &M);
73   };
74 }
75
76 char AAEval::ID = 0;
77 INITIALIZE_PASS(AAEval, "aa-eval",
78                 "Exhaustive Alias Analysis Precision Evaluator", false, true);
79
80 FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
81
82 static void PrintResults(const char *Msg, bool P, const Value *V1,
83                          const Value *V2, const Module *M) {
84   if (P) {
85     std::string o1, o2;
86     {
87       raw_string_ostream os1(o1), os2(o2);
88       WriteAsOperand(os1, V1, true, M);
89       WriteAsOperand(os2, V2, true, M);
90     }
91     
92     if (o2 < o1)
93       std::swap(o1, o2);
94     errs() << "  " << Msg << ":\t"
95            << o1 << ", "
96            << o2 << "\n";
97   }
98 }
99
100 static inline void
101 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
102                    Module *M) {
103   if (P) {
104     errs() << "  " << Msg << ":  Ptr: ";
105     WriteAsOperand(errs(), Ptr, true, M);
106     errs() << "\t<->" << *I << '\n';
107   }
108 }
109
110 static inline bool isInterestingPointer(Value *V) {
111   return V->getType()->isPointerTy()
112       && !isa<ConstantPointerNull>(V);
113 }
114
115 bool AAEval::runOnFunction(Function &F) {
116   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
117
118   SetVector<Value *> Pointers;
119   SetVector<CallSite> CallSites;
120
121   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
122     if (I->getType()->isPointerTy())    // Add all pointer arguments.
123       Pointers.insert(I);
124
125   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
126     if (I->getType()->isPointerTy()) // Add all pointer instructions.
127       Pointers.insert(&*I);
128     Instruction &Inst = *I;
129     CallSite CS = CallSite::get(&Inst);
130     if (CS) {
131       Value *Callee = CS.getCalledValue();
132       // Skip actual functions for direct function calls.
133       if (!isa<Function>(Callee) && isInterestingPointer(Callee))
134         Pointers.insert(Callee);
135       // Consider formals.
136       for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
137            AI != AE; ++AI)
138         if (isInterestingPointer(*AI))
139           Pointers.insert(*AI);
140     } else {
141       // Consider all operands.
142       for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
143            OI != OE; ++OI)
144         if (isInterestingPointer(*OI))
145           Pointers.insert(*OI);
146     }
147
148     if (CS.getInstruction()) CallSites.insert(CS);
149   }
150
151   if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
152       PrintNoModRef || PrintMod || PrintRef || PrintModRef)
153     errs() << "Function: " << F.getName() << ": " << Pointers.size()
154            << " pointers, " << CallSites.size() << " call sites\n";
155
156   // iterate over the worklist, and run the full (n^2)/2 disambiguations
157   for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
158        I1 != E; ++I1) {
159     unsigned I1Size = ~0u;
160     const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
161     if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
162
163     for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
164       unsigned I2Size = ~0u;
165       const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
166       if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
167
168       switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
169       case AliasAnalysis::NoAlias:
170         PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
171         ++NoAlias; break;
172       case AliasAnalysis::MayAlias:
173         PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
174         ++MayAlias; break;
175       case AliasAnalysis::MustAlias:
176         PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
177         ++MustAlias; break;
178       default:
179         errs() << "Unknown alias query result!\n";
180       }
181     }
182   }
183
184   // Mod/ref alias analysis: compare all pairs of calls and values
185   for (SetVector<CallSite>::iterator C = CallSites.begin(),
186          Ce = CallSites.end(); C != Ce; ++C) {
187     Instruction *I = C->getInstruction();
188
189     for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
190          V != Ve; ++V) {
191       unsigned Size = ~0u;
192       const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
193       if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
194
195       switch (AA.getModRefInfo(*C, *V, Size)) {
196       case AliasAnalysis::NoModRef:
197         PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
198         ++NoModRef; break;
199       case AliasAnalysis::Mod:
200         PrintModRefResults("     Mod", PrintMod, I, *V, F.getParent());
201         ++Mod; break;
202       case AliasAnalysis::Ref:
203         PrintModRefResults("     Ref", PrintRef, I, *V, F.getParent());
204         ++Ref; break;
205       case AliasAnalysis::ModRef:
206         PrintModRefResults("  ModRef", PrintModRef, I, *V, F.getParent());
207         ++ModRef; break;
208       default:
209         errs() << "Unknown alias query result!\n";
210       }
211     }
212   }
213
214   return false;
215 }
216
217 static void PrintPercent(unsigned Num, unsigned Sum) {
218   errs() << "(" << Num*100ULL/Sum << "."
219          << ((Num*1000ULL/Sum) % 10) << "%)\n";
220 }
221
222 bool AAEval::doFinalization(Module &M) {
223   unsigned AliasSum = NoAlias + MayAlias + MustAlias;
224   errs() << "===== Alias Analysis Evaluator Report =====\n";
225   if (AliasSum == 0) {
226     errs() << "  Alias Analysis Evaluator Summary: No pointers!\n";
227   } else {
228     errs() << "  " << AliasSum << " Total Alias Queries Performed\n";
229     errs() << "  " << NoAlias << " no alias responses ";
230     PrintPercent(NoAlias, AliasSum);
231     errs() << "  " << MayAlias << " may alias responses ";
232     PrintPercent(MayAlias, AliasSum);
233     errs() << "  " << MustAlias << " must alias responses ";
234     PrintPercent(MustAlias, AliasSum);
235     errs() << "  Alias Analysis Evaluator Pointer Alias Summary: "
236            << NoAlias*100/AliasSum  << "%/" << MayAlias*100/AliasSum << "%/"
237            << MustAlias*100/AliasSum << "%\n";
238   }
239
240   // Display the summary for mod/ref analysis
241   unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
242   if (ModRefSum == 0) {
243     errs() << "  Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
244   } else {
245     errs() << "  " << ModRefSum << " Total ModRef Queries Performed\n";
246     errs() << "  " << NoModRef << " no mod/ref responses ";
247     PrintPercent(NoModRef, ModRefSum);
248     errs() << "  " << Mod << " mod responses ";
249     PrintPercent(Mod, ModRefSum);
250     errs() << "  " << Ref << " ref responses ";
251     PrintPercent(Ref, ModRefSum);
252     errs() << "  " << ModRef << " mod & ref responses ";
253     PrintPercent(ModRef, ModRefSum);
254     errs() << "  Alias Analysis Evaluator Mod/Ref Summary: "
255            << NoModRef*100/ModRefSum  << "%/" << Mod*100/ModRefSum << "%/"
256            << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
257   }
258
259   return false;
260 }