blob: 15d511ac9345a2c4b7af9a340e7ef434e3ea277e [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"
David Greene879027a2009-12-23 19:21:19 +000029#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/InstIterator.h"
31#include "llvm/Support/CommandLine.h"
Daniel Dunbar005975c2009-07-25 00:23:56 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman84c793a2009-08-26 14:32:17 +000033#include "llvm/ADT/SetVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
Dan Gohman089efff2008-05-13 00:00:25 +000036static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
37
38static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
39static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
40static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
41
42static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
43static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
44static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
45static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
46
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000048 class AAEval : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 unsigned NoAlias, MayAlias, MustAlias;
50 unsigned NoModRef, Mod, Ref, ModRef;
51
52 public:
53 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000054 AAEval() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.addRequired<AliasAnalysis>();
58 AU.setPreservesAll();
59 }
60
61 bool doInitialization(Module &M) {
62 NoAlias = MayAlias = MustAlias = 0;
63 NoModRef = Mod = Ref = ModRef = 0;
64
65 if (PrintAll) {
66 PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
67 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
68 }
69 return false;
70 }
71
72 bool runOnFunction(Function &F);
73 bool doFinalization(Module &M);
74 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
Dan Gohman089efff2008-05-13 00:00:25 +000077char AAEval::ID = 0;
78static RegisterPass<AAEval>
79X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator", false, true);
80
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
82
Chris Lattnera7a9daa2009-08-23 05:17:37 +000083static void PrintResults(const char *Msg, bool P, const Value *V1,
84 const Value *V2, const Module *M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 if (P) {
Chris Lattnera7a9daa2009-08-23 05:17:37 +000086 std::string o1, o2;
87 {
88 raw_string_ostream os1(o1), os2(o2);
89 WriteAsOperand(os1, V1, true, M);
90 WriteAsOperand(os2, V2, true, M);
91 }
92
Gabor Greifcc322ca2008-02-28 08:38:45 +000093 if (o2 < o1)
Daniel Dunbar005975c2009-07-25 00:23:56 +000094 std::swap(o1, o2);
David Greene879027a2009-12-23 19:21:19 +000095 dbgs() << " " << Msg << ":\t"
Daniel Dunbar005975c2009-07-25 00:23:56 +000096 << o1 << ", "
97 << o2 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 }
99}
100
101static inline void
102PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
103 Module *M) {
104 if (P) {
David Greene879027a2009-12-23 19:21:19 +0000105 dbgs() << " " << Msg << ": Ptr: ";
106 WriteAsOperand(dbgs(), Ptr, true, M);
107 dbgs() << "\t<->" << *I << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 }
109}
110
111bool AAEval::runOnFunction(Function &F) {
112 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
113
Dan Gohman84c793a2009-08-26 14:32:17 +0000114 SetVector<Value *> Pointers;
115 SetVector<CallSite> CallSites;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
118 if (isa<PointerType>(I->getType())) // Add all pointer arguments
119 Pointers.insert(I);
120
121 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
122 if (isa<PointerType>(I->getType())) // Add all pointer instructions
123 Pointers.insert(&*I);
124 Instruction &Inst = *I;
125 User::op_iterator OI = Inst.op_begin();
Gabor Greif2161a7f2009-03-24 19:28:39 +0000126 CallSite CS = CallSite::get(&Inst);
127 if (CS.getInstruction() &&
128 isa<Function>(CS.getCalledValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 ++OI; // Skip actual functions for direct function calls.
130 for (; OI != Inst.op_end(); ++OI)
131 if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI))
132 Pointers.insert(*OI);
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 if (CS.getInstruction()) CallSites.insert(CS);
135 }
136
137 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
138 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
David Greene879027a2009-12-23 19:21:19 +0000139 dbgs() << "Function: " << F.getName() << ": " << Pointers.size()
Daniel Dunbar005975c2009-07-25 00:23:56 +0000140 << " pointers, " << CallSites.size() << " call sites\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
142 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman84c793a2009-08-26 14:32:17 +0000143 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 I1 != E; ++I1) {
Dan Gohman624f9612009-07-25 00:48:42 +0000145 unsigned I1Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000147 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
Dan Gohman84c793a2009-08-26 14:32:17 +0000149 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Dan Gohman624f9612009-07-25 00:48:42 +0000150 unsigned I2Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000152 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
154 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
155 case AliasAnalysis::NoAlias:
156 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
157 ++NoAlias; break;
158 case AliasAnalysis::MayAlias:
159 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
160 ++MayAlias; break;
161 case AliasAnalysis::MustAlias:
162 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
163 ++MustAlias; break;
164 default:
David Greene879027a2009-12-23 19:21:19 +0000165 dbgs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 }
167 }
168 }
169
170 // Mod/ref alias analysis: compare all pairs of calls and values
Dan Gohman84c793a2009-08-26 14:32:17 +0000171 for (SetVector<CallSite>::iterator C = CallSites.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 Ce = CallSites.end(); C != Ce; ++C) {
173 Instruction *I = C->getInstruction();
174
Dan Gohman84c793a2009-08-26 14:32:17 +0000175 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 V != Ve; ++V) {
Dan Gohman624f9612009-07-25 00:48:42 +0000177 unsigned Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000179 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180
181 switch (AA.getModRefInfo(*C, *V, Size)) {
182 case AliasAnalysis::NoModRef:
183 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
184 ++NoModRef; break;
185 case AliasAnalysis::Mod:
186 PrintModRefResults(" Mod", PrintMod, I, *V, F.getParent());
187 ++Mod; break;
188 case AliasAnalysis::Ref:
189 PrintModRefResults(" Ref", PrintRef, I, *V, F.getParent());
190 ++Ref; break;
191 case AliasAnalysis::ModRef:
192 PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent());
193 ++ModRef; break;
194 default:
David Greene879027a2009-12-23 19:21:19 +0000195 dbgs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 }
197 }
198 }
199
200 return false;
201}
202
203static void PrintPercent(unsigned Num, unsigned Sum) {
David Greene879027a2009-12-23 19:21:19 +0000204 dbgs() << "(" << Num*100ULL/Sum << "."
Daniel Dunbar005975c2009-07-25 00:23:56 +0000205 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206}
207
208bool AAEval::doFinalization(Module &M) {
209 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
David Greene879027a2009-12-23 19:21:19 +0000210 dbgs() << "===== Alias Analysis Evaluator Report =====\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 if (AliasSum == 0) {
David Greene879027a2009-12-23 19:21:19 +0000212 dbgs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 } else {
David Greene879027a2009-12-23 19:21:19 +0000214 dbgs() << " " << AliasSum << " Total Alias Queries Performed\n";
215 dbgs() << " " << NoAlias << " no alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 PrintPercent(NoAlias, AliasSum);
David Greene879027a2009-12-23 19:21:19 +0000217 dbgs() << " " << MayAlias << " may alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 PrintPercent(MayAlias, AliasSum);
David Greene879027a2009-12-23 19:21:19 +0000219 dbgs() << " " << MustAlias << " must alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 PrintPercent(MustAlias, AliasSum);
David Greene879027a2009-12-23 19:21:19 +0000221 dbgs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000222 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
223 << MustAlias*100/AliasSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 }
225
226 // Display the summary for mod/ref analysis
227 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
228 if (ModRefSum == 0) {
David Greene879027a2009-12-23 19:21:19 +0000229 dbgs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 } else {
David Greene879027a2009-12-23 19:21:19 +0000231 dbgs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
232 dbgs() << " " << NoModRef << " no mod/ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 PrintPercent(NoModRef, ModRefSum);
David Greene879027a2009-12-23 19:21:19 +0000234 dbgs() << " " << Mod << " mod responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 PrintPercent(Mod, ModRefSum);
David Greene879027a2009-12-23 19:21:19 +0000236 dbgs() << " " << Ref << " ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 PrintPercent(Ref, ModRefSum);
David Greene879027a2009-12-23 19:21:19 +0000238 dbgs() << " " << ModRef << " mod & ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 PrintPercent(ModRef, ModRefSum);
David Greene879027a2009-12-23 19:21:19 +0000240 dbgs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000241 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
242 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
244
245 return false;
246}