Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / Analysis / AliasAnalysisEvaluator.cpp
blobf42fde899737bdf4b076cc7d8e77b2b376de824b
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.
15 // This is inspired and adapted from code by: Naveen Neelakantam, Francesco
16 // Spadini, and Wojciech Stryjewski.
18 //===----------------------------------------------------------------------===//
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 <iostream>
32 #include <set>
34 using namespace llvm;
36 namespace {
37 cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
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);
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);
48 class AAEval : public FunctionPass {
49 unsigned NoAlias, MayAlias, MustAlias;
50 unsigned NoModRef, Mod, Ref, ModRef;
52 public:
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.addRequired<AliasAnalysis>();
55 AU.setPreservesAll();
58 bool doInitialization(Module &M) {
59 NoAlias = MayAlias = MustAlias = 0;
60 NoModRef = Mod = Ref = ModRef = 0;
62 if (PrintAll) {
63 PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
64 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
66 return false;
69 bool runOnFunction(Function &F);
70 bool doFinalization(Module &M);
73 RegisterOpt<AAEval>
74 X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
77 FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
79 static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
80 Module *M) {
81 if (P) {
82 std::cerr << " " << Msg << ":\t";
83 WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
84 WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
88 static inline void
89 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
90 Module *M) {
91 if (P) {
92 std::cerr << " " << Msg << ": Ptr: ";
93 WriteAsOperand(std::cerr, Ptr, true, true, M);
94 std::cerr << "\t<->" << *I;
98 bool AAEval::runOnFunction(Function &F) {
99 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
101 const TargetData &TD = AA.getTargetData();
103 std::set<Value *> Pointers;
104 std::set<CallSite> CallSites;
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);
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);
122 CallSite CS = CallSite::get(&*I);
123 if (CS.getInstruction()) CallSites.insert(CS);
126 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
127 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
128 std::cerr << "Function: " << F.getName() << ": " << Pointers.size()
129 << " pointers, " << CallSites.size() << " call sites\n";
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);
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);
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 std::cerr << "Unknown alias query result!\n";
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();
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);
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 std::cerr << "Unknown alias query result!\n";
189 return false;
192 static void PrintPercent(unsigned Num, unsigned Sum) {
193 std::cerr << "(" << Num*100ULL/Sum << "."
194 << ((Num*1000ULL/Sum) % 10) << "%)\n";
197 bool AAEval::doFinalization(Module &M) {
198 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
199 std::cerr << "===== Alias Analysis Evaluator Report =====\n";
200 if (AliasSum == 0) {
201 std::cerr << " Alias Analysis Evaluator Summary: No pointers!\n";
202 } else {
203 std::cerr << " " << AliasSum << " Total Alias Queries Performed\n";
204 std::cerr << " " << NoAlias << " no alias responses ";
205 PrintPercent(NoAlias, AliasSum);
206 std::cerr << " " << MayAlias << " may alias responses ";
207 PrintPercent(MayAlias, AliasSum);
208 std::cerr << " " << MustAlias << " must alias responses ";
209 PrintPercent(MustAlias, AliasSum);
210 std::cerr << " Alias Analysis Evaluator Pointer Alias Summary: "
211 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
212 << MustAlias*100/AliasSum << "%\n";
215 // Display the summary for mod/ref analysis
216 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
217 if (ModRefSum == 0) {
218 std::cerr << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
219 } else {
220 std::cerr << " " << ModRefSum << " Total ModRef Queries Performed\n";
221 std::cerr << " " << NoModRef << " no mod/ref responses ";
222 PrintPercent(NoModRef, ModRefSum);
223 std::cerr << " " << Mod << " mod responses ";
224 PrintPercent(Mod, ModRefSum);
225 std::cerr << " " << Ref << " ref responses ";
226 PrintPercent(Ref, ModRefSum);
227 std::cerr << " " << ModRef << " mod & ref responses ";
228 PrintPercent(ModRef, ModRefSum);
229 std::cerr << " Alias Analysis Evaluator Mod/Ref Summary: "
230 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
231 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
234 return false;