1 //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
26 static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
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);
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);
38 static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
40 static void PrintResults(const char *Msg, bool P, const Value *V1,
41 const Value *V2, const Module *M) {
45 raw_string_ostream os1(o1), os2(o2);
46 V1->printAsOperand(os1, true, M);
47 V2->printAsOperand(os2, true, M);
52 errs() << " " << Msg << ":\t"
59 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
62 errs() << " " << Msg << ": Ptr: ";
63 Ptr->printAsOperand(errs(), true, M);
64 errs() << "\t<->" << *I << '\n';
69 PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
72 errs() << " " << Msg << ": " << *CSA.getInstruction()
73 << " <-> " << *CSB.getInstruction() << '\n';
78 PrintLoadStoreResults(const char *Msg, bool P, const Value *V1,
79 const Value *V2, const Module *M) {
81 errs() << " " << Msg << ": " << *V1
82 << " <-> " << *V2 << '\n';
86 static inline bool isInterestingPointer(Value *V) {
87 return V->getType()->isPointerTy()
88 && !isa<ConstantPointerNull>(V);
91 PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) {
92 runInternal(F, AM.getResult<AAManager>(F));
93 return PreservedAnalyses::all();
96 void AAEvaluator::runInternal(Function &F, AAResults &AA) {
97 const DataLayout &DL = F.getParent()->getDataLayout();
101 SetVector<Value *> Pointers;
102 SmallSetVector<CallSite, 16> CallSites;
103 SetVector<Value *> Loads;
104 SetVector<Value *> Stores;
106 for (auto &I : F.args())
107 if (I.getType()->isPointerTy()) // Add all pointer arguments.
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))
115 if (EvalAAMD && isa<StoreInst>(&*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);
124 for (Use &DataOp : CS.data_ops())
125 if (isInterestingPointer(DataOp))
126 Pointers.insert(DataOp);
127 CallSites.insert(CS);
129 // Consider all operands.
130 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
132 if (isInterestingPointer(*OI))
133 Pointers.insert(*OI);
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";
142 // iterate over the worklist, and run the full (n^2)/2 disambiguations
143 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
145 uint64_t I1Size = MemoryLocation::UnknownSize;
146 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
147 if (I1ElTy->isSized()) I1Size = DL.getTypeStoreSize(I1ElTy);
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);
154 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
156 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
160 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
164 PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2,
169 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
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)))) {
183 PrintLoadStoreResults("NoAlias", PrintNoAlias, Load, Store,
188 PrintLoadStoreResults("MayAlias", PrintMayAlias, Load, Store,
193 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, Load, Store,
198 PrintLoadStoreResults("MustAlias", PrintMustAlias, Load, Store,
206 // iterate over all pairs of store, store
207 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
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)))) {
213 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
218 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
223 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
228 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
237 // Mod/ref alias analysis: compare all pairs of calls and values
238 for (CallSite C : CallSites) {
239 Instruction *I = C.getInstruction();
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);
246 switch (AA.getModRefInfo(C, Pointer, Size)) {
248 PrintModRefResults("NoModRef", PrintNoModRef, I, Pointer,
253 PrintModRefResults("Just Mod", PrintMod, I, Pointer, F.getParent());
257 PrintModRefResults("Just Ref", PrintRef, I, Pointer, F.getParent());
261 PrintModRefResults("Both ModRef", PrintModRef, I, Pointer,
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) {
274 switch (AA.getModRefInfo(*C, *D)) {
276 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
280 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
284 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
288 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
296 static void PrintPercent(int64_t Num, int64_t Sum) {
297 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
301 AAEvaluator::~AAEvaluator() {
302 if (FunctionCount == 0)
306 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
307 errs() << "===== Alias Analysis Evaluator Report =====\n";
309 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
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";
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 "
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";
350 class AAEvalLegacyPass : public FunctionPass {
351 std::unique_ptr<AAEvaluator> P;
354 static char ID; // Pass identification, replacement for typeid
355 AAEvalLegacyPass() : FunctionPass(ID) {
356 initializeAAEvalLegacyPassPass(*PassRegistry::getPassRegistry());
359 void getAnalysisUsage(AnalysisUsage &AU) const override {
360 AU.addRequired<AAResultsWrapperPass>();
361 AU.setPreservesAll();
364 bool doInitialization(Module &M) override {
365 P.reset(new AAEvaluator());
369 bool runOnFunction(Function &F) override {
370 P->runInternal(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
373 bool doFinalization(Module &M) override {
380 char AAEvalLegacyPass::ID = 0;
381 INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval",
382 "Exhaustive Alias Analysis Precision Evaluator", false,
384 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
385 INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval",
386 "Exhaustive Alias Analysis Precision Evaluator", false,
389 FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); }