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