LLVM API Documentation
00001 //===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a pass which can be used to count how many alias queries 00011 // are being made and how the alias analysis implementation being used responds. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Analysis/Passes.h" 00016 #include "llvm/Analysis/AliasAnalysis.h" 00017 #include "llvm/Pass.h" 00018 #include "llvm/Support/CommandLine.h" 00019 #include "llvm/Support/Debug.h" 00020 #include "llvm/Support/ErrorHandling.h" 00021 #include "llvm/Support/raw_ostream.h" 00022 using namespace llvm; 00023 00024 static cl::opt<bool> 00025 PrintAll("count-aa-print-all-queries", cl::ReallyHidden, cl::init(true)); 00026 static cl::opt<bool> 00027 PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden); 00028 00029 namespace { 00030 class AliasAnalysisCounter : public ModulePass, public AliasAnalysis { 00031 unsigned No, May, Partial, Must; 00032 unsigned NoMR, JustRef, JustMod, MR; 00033 Module *M; 00034 public: 00035 static char ID; // Class identification, replacement for typeinfo 00036 AliasAnalysisCounter() : ModulePass(ID) { 00037 initializeAliasAnalysisCounterPass(*PassRegistry::getPassRegistry()); 00038 No = May = Partial = Must = 0; 00039 NoMR = JustRef = JustMod = MR = 0; 00040 } 00041 00042 void printLine(const char *Desc, unsigned Val, unsigned Sum) { 00043 errs() << " " << Val << " " << Desc << " responses (" 00044 << Val*100/Sum << "%)\n"; 00045 } 00046 ~AliasAnalysisCounter() { 00047 unsigned AASum = No+May+Partial+Must; 00048 unsigned MRSum = NoMR+JustRef+JustMod+MR; 00049 if (AASum + MRSum) { // Print a report if any counted queries occurred... 00050 errs() << "\n===== Alias Analysis Counter Report =====\n" 00051 << " Analysis counted:\n" 00052 << " " << AASum << " Total Alias Queries Performed\n"; 00053 if (AASum) { 00054 printLine("no alias", No, AASum); 00055 printLine("may alias", May, AASum); 00056 printLine("partial alias", Partial, AASum); 00057 printLine("must alias", Must, AASum); 00058 errs() << " Alias Analysis Counter Summary: " << No*100/AASum << "%/" 00059 << May*100/AASum << "%/" 00060 << Partial*100/AASum << "%/" 00061 << Must*100/AASum<<"%\n\n"; 00062 } 00063 00064 errs() << " " << MRSum << " Total Mod/Ref Queries Performed\n"; 00065 if (MRSum) { 00066 printLine("no mod/ref", NoMR, MRSum); 00067 printLine("ref", JustRef, MRSum); 00068 printLine("mod", JustMod, MRSum); 00069 printLine("mod/ref", MR, MRSum); 00070 errs() << " Mod/Ref Analysis Counter Summary: " <<NoMR*100/MRSum 00071 << "%/" << JustRef*100/MRSum << "%/" << JustMod*100/MRSum 00072 << "%/" << MR*100/MRSum <<"%\n\n"; 00073 } 00074 } 00075 } 00076 00077 bool runOnModule(Module &M) override { 00078 this->M = &M; 00079 InitializeAliasAnalysis(this); 00080 return false; 00081 } 00082 00083 void getAnalysisUsage(AnalysisUsage &AU) const override { 00084 AliasAnalysis::getAnalysisUsage(AU); 00085 AU.addRequired<AliasAnalysis>(); 00086 AU.setPreservesAll(); 00087 } 00088 00089 /// getAdjustedAnalysisPointer - This method is used when a pass implements 00090 /// an analysis interface through multiple inheritance. If needed, it 00091 /// should override this to adjust the this pointer as needed for the 00092 /// specified pass info. 00093 void *getAdjustedAnalysisPointer(AnalysisID PI) override { 00094 if (PI == &AliasAnalysis::ID) 00095 return (AliasAnalysis*)this; 00096 return this; 00097 } 00098 00099 // FIXME: We could count these too... 00100 bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override { 00101 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal); 00102 } 00103 00104 // Forwarding functions: just delegate to a real AA implementation, counting 00105 // the number of responses... 00106 AliasResult alias(const Location &LocA, const Location &LocB) override; 00107 00108 ModRefResult getModRefInfo(ImmutableCallSite CS, 00109 const Location &Loc) override; 00110 ModRefResult getModRefInfo(ImmutableCallSite CS1, 00111 ImmutableCallSite CS2) override { 00112 return AliasAnalysis::getModRefInfo(CS1,CS2); 00113 } 00114 }; 00115 } 00116 00117 char AliasAnalysisCounter::ID = 0; 00118 INITIALIZE_AG_PASS(AliasAnalysisCounter, AliasAnalysis, "count-aa", 00119 "Count Alias Analysis Query Responses", false, true, false) 00120 00121 ModulePass *llvm::createAliasAnalysisCounterPass() { 00122 return new AliasAnalysisCounter(); 00123 } 00124 00125 AliasAnalysis::AliasResult 00126 AliasAnalysisCounter::alias(const Location &LocA, const Location &LocB) { 00127 AliasResult R = getAnalysis<AliasAnalysis>().alias(LocA, LocB); 00128 00129 const char *AliasString = nullptr; 00130 switch (R) { 00131 case NoAlias: No++; AliasString = "No alias"; break; 00132 case MayAlias: May++; AliasString = "May alias"; break; 00133 case PartialAlias: Partial++; AliasString = "Partial alias"; break; 00134 case MustAlias: Must++; AliasString = "Must alias"; break; 00135 } 00136 00137 if (PrintAll || (PrintAllFailures && R == MayAlias)) { 00138 errs() << AliasString << ":\t"; 00139 errs() << "[" << LocA.Size << "B] "; 00140 LocA.Ptr->printAsOperand(errs(), true, M); 00141 errs() << ", "; 00142 errs() << "[" << LocB.Size << "B] "; 00143 LocB.Ptr->printAsOperand(errs(), true, M); 00144 errs() << "\n"; 00145 } 00146 00147 return R; 00148 } 00149 00150 AliasAnalysis::ModRefResult 00151 AliasAnalysisCounter::getModRefInfo(ImmutableCallSite CS, 00152 const Location &Loc) { 00153 ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, Loc); 00154 00155 const char *MRString = nullptr; 00156 switch (R) { 00157 case NoModRef: NoMR++; MRString = "NoModRef"; break; 00158 case Ref: JustRef++; MRString = "JustRef"; break; 00159 case Mod: JustMod++; MRString = "JustMod"; break; 00160 case ModRef: MR++; MRString = "ModRef"; break; 00161 } 00162 00163 if (PrintAll || (PrintAllFailures && R == ModRef)) { 00164 errs() << MRString << ": Ptr: "; 00165 errs() << "[" << Loc.Size << "B] "; 00166 Loc.Ptr->printAsOperand(errs(), true, M); 00167 errs() << "\t<->" << *CS.getInstruction() << '\n'; 00168 } 00169 return R; 00170 }