blob: bfa3ff1f9e6c0672ea592638dcba6149752dbf5f [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 Greene092316b2009-12-23 22:49:57 +000095 errs() << " " << 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 Greene092316b2009-12-23 22:49:57 +0000105 errs() << " " << Msg << ": Ptr: ";
106 WriteAsOperand(errs(), Ptr, true, M);
107 errs() << "\t<->" << *I << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 }
109}
110
Gabor Greif5dac0f62010-04-08 16:46:24 +0000111static inline bool isInterestingPointer(Value *V) {
112 return V->getType()->isPointerTy()
113 && !isa<ConstantPointerNull>(V);
114}
115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116bool AAEval::runOnFunction(Function &F) {
117 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
118
Dan Gohman84c793a2009-08-26 14:32:17 +0000119 SetVector<Value *> Pointers;
120 SetVector<CallSite> CallSites;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Gabor Greif5dac0f62010-04-08 16:46:24 +0000123 if (I->getType()->isPointerTy()) // Add all pointer arguments.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 Pointers.insert(I);
125
126 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif5dac0f62010-04-08 16:46:24 +0000127 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 Pointers.insert(&*I);
129 Instruction &Inst = *I;
Gabor Greif2161a7f2009-03-24 19:28:39 +0000130 CallSite CS = CallSite::get(&Inst);
Gabor Greif5dac0f62010-04-08 16:46:24 +0000131 if (CS) {
132 Value *Callee = CS.getCalledValue();
133 // Skip actual functions for direct function calls.
134 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
135 Pointers.insert(Callee);
136 // Consider formals.
137 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
138 AI != AE; ++AI)
139 if (isInterestingPointer(*AI))
140 Pointers.insert(*AI);
141 } else {
142 // Consider all operands.
143 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
144 OI != OE; ++OI)
145 if (isInterestingPointer(*OI))
146 Pointers.insert(*OI);
147 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 if (CS.getInstruction()) CallSites.insert(CS);
150 }
151
152 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
153 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
David Greene092316b2009-12-23 22:49:57 +0000154 errs() << "Function: " << F.getName() << ": " << Pointers.size()
Daniel Dunbar005975c2009-07-25 00:23:56 +0000155 << " pointers, " << CallSites.size() << " call sites\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
157 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman84c793a2009-08-26 14:32:17 +0000158 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 I1 != E; ++I1) {
Dan Gohman624f9612009-07-25 00:48:42 +0000160 unsigned I1Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000162 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Dan Gohman84c793a2009-08-26 14:32:17 +0000164 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Dan Gohman624f9612009-07-25 00:48:42 +0000165 unsigned I2Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000167 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
169 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
170 case AliasAnalysis::NoAlias:
171 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
172 ++NoAlias; break;
173 case AliasAnalysis::MayAlias:
174 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
175 ++MayAlias; break;
176 case AliasAnalysis::MustAlias:
177 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
178 ++MustAlias; break;
179 default:
David Greene092316b2009-12-23 22:49:57 +0000180 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182 }
183 }
184
185 // Mod/ref alias analysis: compare all pairs of calls and values
Dan Gohman84c793a2009-08-26 14:32:17 +0000186 for (SetVector<CallSite>::iterator C = CallSites.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 Ce = CallSites.end(); C != Ce; ++C) {
188 Instruction *I = C->getInstruction();
189
Dan Gohman84c793a2009-08-26 14:32:17 +0000190 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 V != Ve; ++V) {
Dan Gohman624f9612009-07-25 00:48:42 +0000192 unsigned Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Dan Gohman624f9612009-07-25 00:48:42 +0000194 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
196 switch (AA.getModRefInfo(*C, *V, Size)) {
197 case AliasAnalysis::NoModRef:
198 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
199 ++NoModRef; break;
200 case AliasAnalysis::Mod:
201 PrintModRefResults(" Mod", PrintMod, I, *V, F.getParent());
202 ++Mod; break;
203 case AliasAnalysis::Ref:
204 PrintModRefResults(" Ref", PrintRef, I, *V, F.getParent());
205 ++Ref; break;
206 case AliasAnalysis::ModRef:
207 PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent());
208 ++ModRef; break;
209 default:
David Greene092316b2009-12-23 22:49:57 +0000210 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 }
212 }
213 }
214
215 return false;
216}
217
218static void PrintPercent(unsigned Num, unsigned Sum) {
David Greene092316b2009-12-23 22:49:57 +0000219 errs() << "(" << Num*100ULL/Sum << "."
Daniel Dunbar005975c2009-07-25 00:23:56 +0000220 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221}
222
223bool AAEval::doFinalization(Module &M) {
224 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
David Greene092316b2009-12-23 22:49:57 +0000225 errs() << "===== Alias Analysis Evaluator Report =====\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 if (AliasSum == 0) {
David Greene092316b2009-12-23 22:49:57 +0000227 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 } else {
David Greene092316b2009-12-23 22:49:57 +0000229 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
230 errs() << " " << NoAlias << " no alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 PrintPercent(NoAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000232 errs() << " " << MayAlias << " may alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 PrintPercent(MayAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000234 errs() << " " << MustAlias << " must alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 PrintPercent(MustAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000236 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000237 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
238 << MustAlias*100/AliasSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 }
240
241 // Display the summary for mod/ref analysis
242 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
243 if (ModRefSum == 0) {
David Greene092316b2009-12-23 22:49:57 +0000244 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 } else {
David Greene092316b2009-12-23 22:49:57 +0000246 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
247 errs() << " " << NoModRef << " no mod/ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 PrintPercent(NoModRef, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000249 errs() << " " << Mod << " mod responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 PrintPercent(Mod, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000251 errs() << " " << Ref << " ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 PrintPercent(Ref, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000253 errs() << " " << ModRef << " mod & ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 PrintPercent(ModRef, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000255 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000256 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
257 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 }
259
260 return false;
261}