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