Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/Instructions.h" |
| 24 | #include "llvm/Pass.h" |
| 25 | #include "llvm/Analysis/Passes.h" |
| 26 | #include "llvm/Analysis/AliasAnalysis.h" |
| 27 | #include "llvm/Assembly/Writer.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Support/InstIterator.h" |
| 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | #include <set> |
Gabor Greif | cc322ca | 2008-02-28 08:38:45 +0000 | [diff] [blame] | 34 | #include <sstream> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 37 | static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden); |
| 38 | |
| 39 | static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden); |
| 40 | static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden); |
| 41 | static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden); |
| 42 | |
| 43 | static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden); |
| 44 | static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden); |
| 45 | static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden); |
| 46 | static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden); |
| 47 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | class VISIBILITY_HIDDEN AAEval : public FunctionPass { |
| 50 | unsigned NoAlias, MayAlias, MustAlias; |
| 51 | unsigned NoModRef, Mod, Ref, ModRef; |
| 52 | |
| 53 | public: |
| 54 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 55 | AAEval() : FunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.addRequired<AliasAnalysis>(); |
| 59 | AU.setPreservesAll(); |
| 60 | } |
| 61 | |
| 62 | bool doInitialization(Module &M) { |
| 63 | NoAlias = MayAlias = MustAlias = 0; |
| 64 | NoModRef = Mod = Ref = ModRef = 0; |
| 65 | |
| 66 | if (PrintAll) { |
| 67 | PrintNoAlias = PrintMayAlias = PrintMustAlias = true; |
| 68 | PrintNoModRef = PrintMod = PrintRef = PrintModRef = true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool runOnFunction(Function &F); |
| 74 | bool doFinalization(Module &M); |
| 75 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 78 | char AAEval::ID = 0; |
| 79 | static RegisterPass<AAEval> |
| 80 | X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator", false, true); |
| 81 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | FunctionPass *llvm::createAAEvalPass() { return new AAEval(); } |
| 83 | |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 84 | static void PrintResults(const char *Msg, bool P, const Value *V1, |
| 85 | const Value *V2, const Module *M) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | if (P) { |
Chris Lattner | a7a9daa | 2009-08-23 05:17:37 +0000 | [diff] [blame^] | 87 | std::string o1, o2; |
| 88 | { |
| 89 | raw_string_ostream os1(o1), os2(o2); |
| 90 | WriteAsOperand(os1, V1, true, M); |
| 91 | WriteAsOperand(os2, V2, true, M); |
| 92 | } |
| 93 | |
Gabor Greif | cc322ca | 2008-02-28 08:38:45 +0000 | [diff] [blame] | 94 | if (o2 < o1) |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 95 | std::swap(o1, o2); |
| 96 | errs() << " " << Msg << ":\t" |
| 97 | << o1 << ", " |
| 98 | << o2 << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | static inline void |
| 103 | PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr, |
| 104 | Module *M) { |
| 105 | if (P) { |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 106 | errs() << " " << Msg << ": Ptr: "; |
| 107 | WriteAsOperand(errs(), Ptr, true, M); |
| 108 | errs() << "\t<->" << *I; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | bool AAEval::runOnFunction(Function &F) { |
| 113 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 114 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 115 | std::set<Value *> Pointers; |
| 116 | std::set<CallSite> CallSites; |
| 117 | |
| 118 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) |
| 119 | if (isa<PointerType>(I->getType())) // Add all pointer arguments |
| 120 | Pointers.insert(I); |
| 121 | |
| 122 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 123 | if (isa<PointerType>(I->getType())) // Add all pointer instructions |
| 124 | Pointers.insert(&*I); |
| 125 | Instruction &Inst = *I; |
| 126 | User::op_iterator OI = Inst.op_begin(); |
Gabor Greif | 2161a7f | 2009-03-24 19:28:39 +0000 | [diff] [blame] | 127 | CallSite CS = CallSite::get(&Inst); |
| 128 | if (CS.getInstruction() && |
| 129 | isa<Function>(CS.getCalledValue())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | ++OI; // Skip actual functions for direct function calls. |
| 131 | for (; OI != Inst.op_end(); ++OI) |
| 132 | if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI)) |
| 133 | Pointers.insert(*OI); |
| 134 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | if (CS.getInstruction()) CallSites.insert(CS); |
| 136 | } |
| 137 | |
| 138 | if (PrintNoAlias || PrintMayAlias || PrintMustAlias || |
| 139 | PrintNoModRef || PrintMod || PrintRef || PrintModRef) |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 140 | errs() << "Function: " << F.getName() << ": " << Pointers.size() |
| 141 | << " pointers, " << CallSites.size() << " call sites\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | |
| 143 | // iterate over the worklist, and run the full (n^2)/2 disambiguations |
| 144 | for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end(); |
| 145 | I1 != E; ++I1) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 146 | unsigned I1Size = ~0u; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 147 | const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType(); |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 148 | if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 149 | |
| 150 | for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 151 | unsigned I2Size = ~0u; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType(); |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 153 | if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 154 | |
| 155 | switch (AA.alias(*I1, I1Size, *I2, I2Size)) { |
| 156 | case AliasAnalysis::NoAlias: |
| 157 | PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent()); |
| 158 | ++NoAlias; break; |
| 159 | case AliasAnalysis::MayAlias: |
| 160 | PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent()); |
| 161 | ++MayAlias; break; |
| 162 | case AliasAnalysis::MustAlias: |
| 163 | PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent()); |
| 164 | ++MustAlias; break; |
| 165 | default: |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 166 | errs() << "Unknown alias query result!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Mod/ref alias analysis: compare all pairs of calls and values |
| 172 | for (std::set<CallSite>::iterator C = CallSites.begin(), |
| 173 | Ce = CallSites.end(); C != Ce; ++C) { |
| 174 | Instruction *I = C->getInstruction(); |
| 175 | |
| 176 | for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end(); |
| 177 | V != Ve; ++V) { |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 178 | unsigned Size = ~0u; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 179 | const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType(); |
Dan Gohman | 624f961 | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 180 | if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 181 | |
| 182 | switch (AA.getModRefInfo(*C, *V, Size)) { |
| 183 | case AliasAnalysis::NoModRef: |
| 184 | PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent()); |
| 185 | ++NoModRef; break; |
| 186 | case AliasAnalysis::Mod: |
| 187 | PrintModRefResults(" Mod", PrintMod, I, *V, F.getParent()); |
| 188 | ++Mod; break; |
| 189 | case AliasAnalysis::Ref: |
| 190 | PrintModRefResults(" Ref", PrintRef, I, *V, F.getParent()); |
| 191 | ++Ref; break; |
| 192 | case AliasAnalysis::ModRef: |
| 193 | PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent()); |
| 194 | ++ModRef; break; |
| 195 | default: |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 196 | errs() << "Unknown alias query result!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | static void PrintPercent(unsigned Num, unsigned Sum) { |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 205 | errs() << "(" << Num*100ULL/Sum << "." |
| 206 | << ((Num*1000ULL/Sum) % 10) << "%)\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool AAEval::doFinalization(Module &M) { |
| 210 | unsigned AliasSum = NoAlias + MayAlias + MustAlias; |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 211 | errs() << "===== Alias Analysis Evaluator Report =====\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 212 | if (AliasSum == 0) { |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 213 | errs() << " Alias Analysis Evaluator Summary: No pointers!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 214 | } else { |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 215 | errs() << " " << AliasSum << " Total Alias Queries Performed\n"; |
| 216 | errs() << " " << NoAlias << " no alias responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 217 | PrintPercent(NoAlias, AliasSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 218 | errs() << " " << MayAlias << " may alias responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 219 | PrintPercent(MayAlias, AliasSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 220 | errs() << " " << MustAlias << " must alias responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | PrintPercent(MustAlias, AliasSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 222 | errs() << " Alias Analysis Evaluator Pointer Alias Summary: " |
| 223 | << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/" |
| 224 | << MustAlias*100/AliasSum << "%\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Display the summary for mod/ref analysis |
| 228 | unsigned ModRefSum = NoModRef + Mod + Ref + ModRef; |
| 229 | if (ModRefSum == 0) { |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 230 | errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | } else { |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 232 | errs() << " " << ModRefSum << " Total ModRef Queries Performed\n"; |
| 233 | errs() << " " << NoModRef << " no mod/ref responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 234 | PrintPercent(NoModRef, ModRefSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 235 | errs() << " " << Mod << " mod responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 236 | PrintPercent(Mod, ModRefSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 237 | errs() << " " << Ref << " ref responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 238 | PrintPercent(Ref, ModRefSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 239 | errs() << " " << ModRef << " mod & ref responses "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 240 | PrintPercent(ModRef, ModRefSum); |
Daniel Dunbar | 005975c | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 241 | errs() << " Alias Analysis Evaluator Mod/Ref Summary: " |
| 242 | << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/" |
| 243 | << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return false; |
| 247 | } |