blob: 5194e494e623ee7eb5231e1e376b82ece8c86ca5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/Compiler.h"
Daniel Dunbar005975c2009-07-25 00:23:56 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include <set>
Gabor Greifcc322ca2008-02-28 08:38:45 +000034#include <sstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace llvm;
36
Dan Gohman089efff2008-05-13 00:00:25 +000037static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
38
39static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
40static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
41static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
42
43static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
44static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
45static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
46static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
47
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 class VISIBILITY_HIDDEN AAEval : public FunctionPass {
50 unsigned NoAlias, MayAlias, MustAlias;
51 unsigned NoModRef, Mod, Ref, ModRef;
52
53 public:
54 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000055 AAEval() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.addRequired<AliasAnalysis>();
59 AU.setPreservesAll();
60 }
61
62 bool doInitialization(Module &M) {
63 NoAlias = MayAlias = MustAlias = 0;
64 NoModRef = Mod = Ref = ModRef = 0;
65
66 if (PrintAll) {
67 PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
68 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
69 }
70 return false;
71 }
72
73 bool runOnFunction(Function &F);
74 bool doFinalization(Module &M);
75 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076}
77
Dan Gohman089efff2008-05-13 00:00:25 +000078char AAEval::ID = 0;
79static RegisterPass<AAEval>
80X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator", false, true);
81
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
83
Chris Lattnera7a9daa2009-08-23 05:17:37 +000084static void PrintResults(const char *Msg, bool P, const Value *V1,
85 const Value *V2, const Module *M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 if (P) {
Chris Lattnera7a9daa2009-08-23 05:17:37 +000087 std::string o1, o2;
88 {
89 raw_string_ostream os1(o1), os2(o2);
90 WriteAsOperand(os1, V1, true, M);
91 WriteAsOperand(os2, V2, true, M);
92 }
93
Gabor Greifcc322ca2008-02-28 08:38:45 +000094 if (o2 < o1)
Daniel Dunbar005975c2009-07-25 00:23:56 +000095 std::swap(o1, o2);
96 errs() << " " << Msg << ":\t"
97 << o1 << ", "
98 << o2 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 }
100}
101
102static inline void
103PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
104 Module *M) {
105 if (P) {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000106 errs() << " " << Msg << ": Ptr: ";
107 WriteAsOperand(errs(), Ptr, true, M);
108 errs() << "\t<->" << *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 }
110}
111
112bool AAEval::runOnFunction(Function &F) {
113 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 std::set<Value *> Pointers;
116 std::set<CallSite> CallSites;
117
118 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
119 if (isa<PointerType>(I->getType())) // Add all pointer arguments
120 Pointers.insert(I);
121
122 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
123 if (isa<PointerType>(I->getType())) // Add all pointer instructions
124 Pointers.insert(&*I);
125 Instruction &Inst = *I;
126 User::op_iterator OI = Inst.op_begin();
Gabor Greif2161a7f2009-03-24 19:28:39 +0000127 CallSite CS = CallSite::get(&Inst);
128 if (CS.getInstruction() &&
129 isa<Function>(CS.getCalledValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 ++OI; // Skip actual functions for direct function calls.
131 for (; OI != Inst.op_end(); ++OI)
132 if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI))
133 Pointers.insert(*OI);
134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 if (CS.getInstruction()) CallSites.insert(CS);
136 }
137
138 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
139 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
Daniel Dunbar005975c2009-07-25 00:23:56 +0000140 errs() << "Function: " << F.getName() << ": " << Pointers.size()
141 << " pointers, " << CallSites.size() << " call sites\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142
143 // iterate over the worklist, and run the full (n^2)/2 disambiguations
144 for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
145 I1 != E; ++I1) {
Dan Gohman624f9612009-07-25 00:48:42 +0000146 unsigned I1Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000148 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149
150 for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Dan Gohman624f9612009-07-25 00:48:42 +0000151 unsigned I2Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000153 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
155 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
156 case AliasAnalysis::NoAlias:
157 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
158 ++NoAlias; break;
159 case AliasAnalysis::MayAlias:
160 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
161 ++MayAlias; break;
162 case AliasAnalysis::MustAlias:
163 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
164 ++MustAlias; break;
165 default:
Daniel Dunbar005975c2009-07-25 00:23:56 +0000166 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 }
168 }
169 }
170
171 // Mod/ref alias analysis: compare all pairs of calls and values
172 for (std::set<CallSite>::iterator C = CallSites.begin(),
173 Ce = CallSites.end(); C != Ce; ++C) {
174 Instruction *I = C->getInstruction();
175
176 for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
177 V != Ve; ++V) {
Dan Gohman624f9612009-07-25 00:48:42 +0000178 unsigned Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000180 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 switch (AA.getModRefInfo(*C, *V, Size)) {
183 case AliasAnalysis::NoModRef:
184 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
185 ++NoModRef; break;
186 case AliasAnalysis::Mod:
187 PrintModRefResults(" Mod", PrintMod, I, *V, F.getParent());
188 ++Mod; break;
189 case AliasAnalysis::Ref:
190 PrintModRefResults(" Ref", PrintRef, I, *V, F.getParent());
191 ++Ref; break;
192 case AliasAnalysis::ModRef:
193 PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent());
194 ++ModRef; break;
195 default:
Daniel Dunbar005975c2009-07-25 00:23:56 +0000196 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 }
198 }
199 }
200
201 return false;
202}
203
204static void PrintPercent(unsigned Num, unsigned Sum) {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000205 errs() << "(" << Num*100ULL/Sum << "."
206 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207}
208
209bool AAEval::doFinalization(Module &M) {
210 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
Daniel Dunbar005975c2009-07-25 00:23:56 +0000211 errs() << "===== Alias Analysis Evaluator Report =====\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 if (AliasSum == 0) {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000213 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 } else {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000215 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
216 errs() << " " << NoAlias << " no alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 PrintPercent(NoAlias, AliasSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000218 errs() << " " << MayAlias << " may alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 PrintPercent(MayAlias, AliasSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000220 errs() << " " << MustAlias << " must alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 PrintPercent(MustAlias, AliasSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000222 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
223 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
224 << MustAlias*100/AliasSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 }
226
227 // Display the summary for mod/ref analysis
228 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
229 if (ModRefSum == 0) {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000230 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 } else {
Daniel Dunbar005975c2009-07-25 00:23:56 +0000232 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
233 errs() << " " << NoModRef << " no mod/ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 PrintPercent(NoModRef, ModRefSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000235 errs() << " " << Mod << " mod responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 PrintPercent(Mod, ModRefSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000237 errs() << " " << Ref << " ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 PrintPercent(Ref, ModRefSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000239 errs() << " " << ModRef << " mod & ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 PrintPercent(ModRef, ModRefSum);
Daniel Dunbar005975c2009-07-25 00:23:56 +0000241 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
242 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
243 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 }
245
246 return false;
247}