LCOV - code coverage report
Current view: top level - lib/Analysis - AliasAnalysisEvaluator.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 253 263 96.2 %
Date: 2017-09-14 15:23:50 Functions: 18 20 90.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13