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