Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 1 | //===- AliasAnalysisCounter.cpp - Alias Analysis Query Counter ------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements a pass which can be used to count how many alias queries |
| 11 | // are being made and how the alias analysis implementation being used responds. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AliasAnalysis.h" |
| 18 | #include "llvm/Assembly/Writer.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 20 | #include <iostream> |
Chris Lattner | 381bf79 | 2003-12-10 15:34:03 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 23 | namespace { |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 24 | cl::opt<bool> |
| 25 | PrintAll("count-aa-print-all-queries", cl::ReallyHidden); |
| 26 | cl::opt<bool> |
| 27 | PrintAllFailures("count-aa-print-all-failed-queries", cl::ReallyHidden); |
| 28 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 29 | class AliasAnalysisCounter : public ModulePass, public AliasAnalysis { |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 30 | unsigned No, May, Must; |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 31 | unsigned NoMR, JustRef, JustMod, MR; |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 32 | const char *Name; |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 33 | Module *M; |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 34 | public: |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 35 | AliasAnalysisCounter() { |
| 36 | No = May = Must = 0; |
| 37 | NoMR = JustRef = JustMod = MR = 0; |
| 38 | } |
| 39 | |
| 40 | void printLine(const char *Desc, unsigned Val, unsigned Sum) { |
| 41 | std::cerr << " " << Val << " " << Desc << " responses (" |
| 42 | << Val*100/Sum << "%)\n"; |
| 43 | } |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 44 | ~AliasAnalysisCounter() { |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 45 | unsigned AASum = No+May+Must; |
| 46 | unsigned MRSum = NoMR+JustRef+JustMod+MR; |
| 47 | if (AASum + MRSum) { // Print a report if any counted queries occurred... |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 48 | std::cerr |
| 49 | << "\n===== Alias Analysis Counter Report =====\n" |
| 50 | << " Analysis counted: " << Name << "\n" |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 51 | << " " << AASum << " Total Alias Queries Performed\n"; |
Chris Lattner | efa095e | 2003-06-17 15:07:20 +0000 | [diff] [blame] | 52 | if (AASum) { |
| 53 | printLine("no alias", No, AASum); |
| 54 | printLine("may alias", May, AASum); |
| 55 | printLine("must alias", Must, AASum); |
| 56 | std::cerr |
| 57 | << " Alias Analysis Counter Summary: " << No*100/AASum << "%/" |
| 58 | << May*100/AASum << "%/" << Must*100/AASum<<"%\n\n"; |
| 59 | } |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 60 | |
| 61 | std::cerr |
| 62 | << " " << MRSum << " Total Mod/Ref Queries Performed\n"; |
Chris Lattner | efa095e | 2003-06-17 15:07:20 +0000 | [diff] [blame] | 63 | if (MRSum) { |
| 64 | printLine("no mod/ref", NoMR, MRSum); |
| 65 | printLine("ref", JustRef, MRSum); |
| 66 | printLine("mod", JustMod, MRSum); |
| 67 | printLine("mod/ref", MR, MRSum); |
| 68 | std::cerr |
| 69 | << " Mod/Ref Analysis Counter Summary: " << NoMR*100/MRSum<< "%/" |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 70 | << JustRef*100/MRSum << "%/" << JustMod*100/MRSum << "%/" |
Chris Lattner | efa095e | 2003-06-17 15:07:20 +0000 | [diff] [blame] | 71 | << MR*100/MRSum <<"%\n\n"; |
| 72 | } |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 75 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 76 | bool runOnModule(Module &M) { |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 77 | this->M = &M; |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 78 | InitializeAliasAnalysis(this); |
Chris Lattner | a89feb5 | 2003-02-07 20:39:17 +0000 | [diff] [blame] | 79 | Name = dynamic_cast<Pass*>(&getAnalysis<AliasAnalysis>())->getPassName(); |
| 80 | return false; |
| 81 | } |
| 82 | |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 83 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 84 | AliasAnalysis::getAnalysisUsage(AU); |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 85 | AU.addRequired<AliasAnalysis>(); |
| 86 | AU.setPreservesAll(); |
| 87 | } |
| 88 | |
Chris Lattner | b79d792 | 2004-03-15 04:06:46 +0000 | [diff] [blame] | 89 | // FIXME: We could count these too... |
| 90 | bool pointsToConstantMemory(const Value *P) { |
| 91 | return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P); |
| 92 | } |
| 93 | bool doesNotAccessMemory(Function *F) { |
| 94 | return getAnalysis<AliasAnalysis>().doesNotAccessMemory(F); |
| 95 | } |
| 96 | bool onlyReadsMemory(Function *F) { |
| 97 | return getAnalysis<AliasAnalysis>().onlyReadsMemory(F); |
| 98 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 99 | |
| 100 | |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 101 | // Forwarding functions: just delegate to a real AA implementation, counting |
| 102 | // the number of responses... |
Chris Lattner | d80651d | 2003-02-26 19:27:05 +0000 | [diff] [blame] | 103 | AliasResult alias(const Value *V1, unsigned V1Size, |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 104 | const Value *V2, unsigned V2Size); |
| 105 | |
| 106 | ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); |
Reid Spencer | 4a7ebfa | 2004-12-07 08:11:24 +0000 | [diff] [blame] | 107 | ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { |
| 108 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 109 | } |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 112 | RegisterPass<AliasAnalysisCounter> |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 113 | X("count-aa", "Count Alias Analysis Query Responses"); |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 114 | RegisterAnalysisGroup<AliasAnalysis> Y(X); |
Chris Lattner | 70e332d | 2003-02-07 19:46:03 +0000 | [diff] [blame] | 115 | } |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 116 | |
| 117 | ModulePass *llvm::createAliasAnalysisCounterPass() { |
| 118 | return new AliasAnalysisCounter(); |
| 119 | } |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 120 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 121 | AliasAnalysis::AliasResult |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 122 | AliasAnalysisCounter::alias(const Value *V1, unsigned V1Size, |
| 123 | const Value *V2, unsigned V2Size) { |
| 124 | AliasResult R = getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 125 | |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 126 | const char *AliasString; |
| 127 | switch (R) { |
| 128 | default: assert(0 && "Unknown alias type!"); |
| 129 | case NoAlias: No++; AliasString = "No alias"; break; |
| 130 | case MayAlias: May++; AliasString = "May alias"; break; |
| 131 | case MustAlias: Must++; AliasString = "Must alias"; break; |
| 132 | } |
| 133 | |
| 134 | if (PrintAll || (PrintAllFailures && R == MayAlias)) { |
| 135 | std::cerr << AliasString << ":\t"; |
| 136 | std::cerr << "[" << V1Size << "B] "; |
| 137 | WriteAsOperand(std::cerr, V1, true, true, M) << ", "; |
| 138 | std::cerr << "[" << V2Size << "B] "; |
| 139 | WriteAsOperand(std::cerr, V2, true, true, M) << "\n"; |
| 140 | } |
| 141 | |
| 142 | return R; |
| 143 | } |
| 144 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 145 | AliasAnalysis::ModRefResult |
Chris Lattner | cf9f201 | 2005-03-23 21:59:07 +0000 | [diff] [blame] | 146 | AliasAnalysisCounter::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 147 | ModRefResult R = getAnalysis<AliasAnalysis>().getModRefInfo(CS, P, Size); |
| 148 | |
| 149 | const char *MRString; |
| 150 | switch (R) { |
| 151 | default: assert(0 && "Unknown mod/ref type!"); |
| 152 | case NoModRef: NoMR++; MRString = "NoModRef"; break; |
| 153 | case Ref: JustRef++; MRString = "JustRef"; break; |
| 154 | case Mod: JustMod++; MRString = "JustMod"; break; |
| 155 | case ModRef: MR++; MRString = "ModRef"; break; |
| 156 | } |
| 157 | |
| 158 | if (PrintAll || (PrintAllFailures && R == ModRef)) { |
| 159 | std::cerr << MRString << ": Ptr: "; |
| 160 | std::cerr << "[" << Size << "B] "; |
| 161 | WriteAsOperand(std::cerr, P, true, true, M); |
| 162 | std::cerr << "\t<->" << *CS.getInstruction(); |
| 163 | } |
| 164 | return R; |
| 165 | } |