1 //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Analysis/Passes.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/InstIterator.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 using namespace llvm;
35
36 static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
37
38 static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
39 static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
40 static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden);
41 static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
42
43 static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
44 static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
45 static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
46 static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
47
48 static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
49
50 namespace {
51 class AAEval : public FunctionPass {
52 unsigned NoAliasCount, MayAliasCount, PartialAliasCount, MustAliasCount;
53 unsigned NoModRefCount, ModCount, RefCount, ModRefCount;
54
55 public:
56 static char ID; // Pass identification, replacement for typeid
AAEval()57 AAEval() : FunctionPass(ID) {
58 initializeAAEvalPass(*PassRegistry::getPassRegistry());
59 }
60
getAnalysisUsage(AnalysisUsage & AU) const61 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AU.addRequired<AAResultsWrapperPass>();
63 AU.setPreservesAll();
64 }
65
doInitialization(Module & M)66 bool doInitialization(Module &M) override {
67 NoAliasCount = MayAliasCount = PartialAliasCount = MustAliasCount = 0;
68 NoModRefCount = ModCount = RefCount = ModRefCount = 0;
69
70 if (PrintAll) {
71 PrintNoAlias = PrintMayAlias = true;
72 PrintPartialAlias = PrintMustAlias = true;
73 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
74 }
75 return false;
76 }
77
78 bool runOnFunction(Function &F) override;
79 bool doFinalization(Module &M) override;
80 };
81 }
82
83 char AAEval::ID = 0;
84 INITIALIZE_PASS_BEGIN(AAEval, "aa-eval",
85 "Exhaustive Alias Analysis Precision Evaluator", false, true)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)86 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
87 INITIALIZE_PASS_END(AAEval, "aa-eval",
88 "Exhaustive Alias Analysis Precision Evaluator", false, true)
89
90 FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
91
PrintResults(const char * Msg,bool P,const Value * V1,const Value * V2,const Module * M)92 static void PrintResults(const char *Msg, bool P, const Value *V1,
93 const Value *V2, const Module *M) {
94 if (P) {
95 std::string o1, o2;
96 {
97 raw_string_ostream os1(o1), os2(o2);
98 V1->printAsOperand(os1, true, M);
99 V2->printAsOperand(os2, true, M);
100 }
101
102 if (o2 < o1)
103 std::swap(o1, o2);
104 errs() << " " << Msg << ":\t"
105 << o1 << ", "
106 << o2 << "\n";
107 }
108 }
109
110 static inline void
PrintModRefResults(const char * Msg,bool P,Instruction * I,Value * Ptr,Module * M)111 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
112 Module *M) {
113 if (P) {
114 errs() << " " << Msg << ": Ptr: ";
115 Ptr->printAsOperand(errs(), true, M);
116 errs() << "\t<->" << *I << '\n';
117 }
118 }
119
120 static inline void
PrintModRefResults(const char * Msg,bool P,CallSite CSA,CallSite CSB,Module * M)121 PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
122 Module *M) {
123 if (P) {
124 errs() << " " << Msg << ": " << *CSA.getInstruction()
125 << " <-> " << *CSB.getInstruction() << '\n';
126 }
127 }
128
129 static inline void
PrintLoadStoreResults(const char * Msg,bool P,const Value * V1,const Value * V2,const Module * M)130 PrintLoadStoreResults(const char *Msg, bool P, const Value *V1,
131 const Value *V2, const Module *M) {
132 if (P) {
133 errs() << " " << Msg << ": " << *V1
134 << " <-> " << *V2 << '\n';
135 }
136 }
137
isInterestingPointer(Value * V)138 static inline bool isInterestingPointer(Value *V) {
139 return V->getType()->isPointerTy()
140 && !isa<ConstantPointerNull>(V);
141 }
142
runOnFunction(Function & F)143 bool AAEval::runOnFunction(Function &F) {
144 const DataLayout &DL = F.getParent()->getDataLayout();
145 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
146
147 SetVector<Value *> Pointers;
148 SmallSetVector<CallSite, 16> CallSites;
149 SetVector<Value *> Loads;
150 SetVector<Value *> Stores;
151
152 for (auto &I : F.args())
153 if (I.getType()->isPointerTy()) // Add all pointer arguments.
154 Pointers.insert(&I);
155
156 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
157 if (I->getType()->isPointerTy()) // Add all pointer instructions.
158 Pointers.insert(&*I);
159 if (EvalAAMD && isa<LoadInst>(&*I))
160 Loads.insert(&*I);
161 if (EvalAAMD && isa<StoreInst>(&*I))
162 Stores.insert(&*I);
163 Instruction &Inst = *I;
164 if (auto CS = CallSite(&Inst)) {
165 Value *Callee = CS.getCalledValue();
166 // Skip actual functions for direct function calls.
167 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
168 Pointers.insert(Callee);
169 // Consider formals.
170 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
171 AI != AE; ++AI)
172 if (isInterestingPointer(*AI))
173 Pointers.insert(*AI);
174 CallSites.insert(CS);
175 } else {
176 // Consider all operands.
177 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
178 OI != OE; ++OI)
179 if (isInterestingPointer(*OI))
180 Pointers.insert(*OI);
181 }
182 }
183
184 if (PrintNoAlias || PrintMayAlias || PrintPartialAlias || PrintMustAlias ||
185 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
186 errs() << "Function: " << F.getName() << ": " << Pointers.size()
187 << " pointers, " << CallSites.size() << " call sites\n";
188
189 // iterate over the worklist, and run the full (n^2)/2 disambiguations
190 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
191 I1 != E; ++I1) {
192 uint64_t I1Size = MemoryLocation::UnknownSize;
193 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
194 if (I1ElTy->isSized()) I1Size = DL.getTypeStoreSize(I1ElTy);
195
196 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
197 uint64_t I2Size = MemoryLocation::UnknownSize;
198 Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
199 if (I2ElTy->isSized()) I2Size = DL.getTypeStoreSize(I2ElTy);
200
201 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
202 case NoAlias:
203 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
204 ++NoAliasCount;
205 break;
206 case MayAlias:
207 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
208 ++MayAliasCount;
209 break;
210 case PartialAlias:
211 PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2,
212 F.getParent());
213 ++PartialAliasCount;
214 break;
215 case MustAlias:
216 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
217 ++MustAliasCount;
218 break;
219 }
220 }
221 }
222
223 if (EvalAAMD) {
224 // iterate over all pairs of load, store
225 for (SetVector<Value *>::iterator I1 = Loads.begin(), E = Loads.end();
226 I1 != E; ++I1) {
227 for (SetVector<Value *>::iterator I2 = Stores.begin(), E2 = Stores.end();
228 I2 != E2; ++I2) {
229 switch (AA.alias(MemoryLocation::get(cast<LoadInst>(*I1)),
230 MemoryLocation::get(cast<StoreInst>(*I2)))) {
231 case NoAlias:
232 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
233 F.getParent());
234 ++NoAliasCount;
235 break;
236 case MayAlias:
237 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
238 F.getParent());
239 ++MayAliasCount;
240 break;
241 case PartialAlias:
242 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
243 F.getParent());
244 ++PartialAliasCount;
245 break;
246 case MustAlias:
247 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
248 F.getParent());
249 ++MustAliasCount;
250 break;
251 }
252 }
253 }
254
255 // iterate over all pairs of store, store
256 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
257 I1 != E; ++I1) {
258 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
259 switch (AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
260 MemoryLocation::get(cast<StoreInst>(*I2)))) {
261 case NoAlias:
262 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
263 F.getParent());
264 ++NoAliasCount;
265 break;
266 case MayAlias:
267 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
268 F.getParent());
269 ++MayAliasCount;
270 break;
271 case PartialAlias:
272 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
273 F.getParent());
274 ++PartialAliasCount;
275 break;
276 case MustAlias:
277 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
278 F.getParent());
279 ++MustAliasCount;
280 break;
281 }
282 }
283 }
284 }
285
286 // Mod/ref alias analysis: compare all pairs of calls and values
287 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
288 Instruction *I = C->getInstruction();
289
290 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
291 V != Ve; ++V) {
292 uint64_t Size = MemoryLocation::UnknownSize;
293 Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
294 if (ElTy->isSized()) Size = DL.getTypeStoreSize(ElTy);
295
296 switch (AA.getModRefInfo(*C, *V, Size)) {
297 case MRI_NoModRef:
298 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
299 ++NoModRefCount;
300 break;
301 case MRI_Mod:
302 PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent());
303 ++ModCount;
304 break;
305 case MRI_Ref:
306 PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent());
307 ++RefCount;
308 break;
309 case MRI_ModRef:
310 PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent());
311 ++ModRefCount;
312 break;
313 }
314 }
315 }
316
317 // Mod/ref alias analysis: compare all pairs of calls
318 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
319 for (auto D = CallSites.begin(); D != Ce; ++D) {
320 if (D == C)
321 continue;
322 switch (AA.getModRefInfo(*C, *D)) {
323 case MRI_NoModRef:
324 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
325 ++NoModRefCount;
326 break;
327 case MRI_Mod:
328 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
329 ++ModCount;
330 break;
331 case MRI_Ref:
332 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
333 ++RefCount;
334 break;
335 case MRI_ModRef:
336 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
337 ++ModRefCount;
338 break;
339 }
340 }
341 }
342
343 return false;
344 }
345
PrintPercent(unsigned Num,unsigned Sum)346 static void PrintPercent(unsigned Num, unsigned Sum) {
347 errs() << "(" << Num*100ULL/Sum << "."
348 << ((Num*1000ULL/Sum) % 10) << "%)\n";
349 }
350
doFinalization(Module & M)351 bool AAEval::doFinalization(Module &M) {
352 unsigned AliasSum =
353 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
354 errs() << "===== Alias Analysis Evaluator Report =====\n";
355 if (AliasSum == 0) {
356 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
357 } else {
358 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
359 errs() << " " << NoAliasCount << " no alias responses ";
360 PrintPercent(NoAliasCount, AliasSum);
361 errs() << " " << MayAliasCount << " may alias responses ";
362 PrintPercent(MayAliasCount, AliasSum);
363 errs() << " " << PartialAliasCount << " partial alias responses ";
364 PrintPercent(PartialAliasCount, AliasSum);
365 errs() << " " << MustAliasCount << " must alias responses ";
366 PrintPercent(MustAliasCount, AliasSum);
367 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
368 << NoAliasCount * 100 / AliasSum << "%/"
369 << MayAliasCount * 100 / AliasSum << "%/"
370 << PartialAliasCount * 100 / AliasSum << "%/"
371 << MustAliasCount * 100 / AliasSum << "%\n";
372 }
373
374 // Display the summary for mod/ref analysis
375 unsigned ModRefSum = NoModRefCount + ModCount + RefCount + ModRefCount;
376 if (ModRefSum == 0) {
377 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
378 "mod/ref!\n";
379 } else {
380 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
381 errs() << " " << NoModRefCount << " no mod/ref responses ";
382 PrintPercent(NoModRefCount, ModRefSum);
383 errs() << " " << ModCount << " mod responses ";
384 PrintPercent(ModCount, ModRefSum);
385 errs() << " " << RefCount << " ref responses ";
386 PrintPercent(RefCount, ModRefSum);
387 errs() << " " << ModRefCount << " mod & ref responses ";
388 PrintPercent(ModRefCount, ModRefSum);
389 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
390 << NoModRefCount * 100 / ModRefSum << "%/"
391 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
392 << "%/" << ModRefCount * 100 / ModRefSum << "%\n";
393 }
394
395 return false;
396 }
397