Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 1 | //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===// |
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 | // |
Chris Lattner | 4ee451d | 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. |
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 | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Passes.h" |
| 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
| 23 | #include "llvm/Assembly/Writer.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Function.h" |
| 27 | #include "llvm/IR/Instructions.h" |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
David Greene | 44d98a7 | 2009-12-23 19:21:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 31 | #include "llvm/Support/InstIterator.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 9a4f8ef | 2003-12-10 15:33:59 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 35 | static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden); |
| 36 | |
| 37 | static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden); |
| 38 | static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden); |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 39 | static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 40 | static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden); |
| 41 | |
| 42 | static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden); |
| 43 | static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden); |
| 44 | static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden); |
| 45 | static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden); |
| 46 | |
Manman Ren | a2e3834 | 2013-03-22 22:34:41 +0000 | [diff] [blame^] | 47 | static cl::opt<bool> EvalTBAA("evaluate-tbaa", cl::ReallyHidden); |
| 48 | |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 49 | namespace { |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 50 | class AAEval : public FunctionPass { |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 51 | unsigned NoAlias, MayAlias, PartialAlias, MustAlias; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 52 | unsigned NoModRef, Mod, Ref, ModRef; |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 53 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 54 | public: |
| 55 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 56 | AAEval() : FunctionPass(ID) { |
| 57 | initializeAAEvalPass(*PassRegistry::getPassRegistry()); |
| 58 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 59 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 60 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 61 | AU.addRequired<AliasAnalysis>(); |
| 62 | AU.setPreservesAll(); |
| 63 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 64 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 65 | bool doInitialization(Module &M) { |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 66 | NoAlias = MayAlias = PartialAlias = MustAlias = 0; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 67 | NoModRef = Mod = Ref = ModRef = 0; |
Chris Lattner | 0e872cb | 2004-07-17 06:28:49 +0000 | [diff] [blame] | 68 | |
| 69 | if (PrintAll) { |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 70 | PrintNoAlias = PrintMayAlias = true; |
| 71 | PrintPartialAlias = PrintMustAlias = true; |
Chris Lattner | 0e872cb | 2004-07-17 06:28:49 +0000 | [diff] [blame] | 72 | PrintNoModRef = PrintMod = PrintRef = PrintModRef = true; |
| 73 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 74 | return false; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 77 | bool runOnFunction(Function &F); |
| 78 | bool doFinalization(Module &M); |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 79 | }; |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 82 | char AAEval::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 83 | INITIALIZE_PASS_BEGIN(AAEval, "aa-eval", |
| 84 | "Exhaustive Alias Analysis Precision Evaluator", false, true) |
| 85 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 86 | INITIALIZE_PASS_END(AAEval, "aa-eval", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 87 | "Exhaustive Alias Analysis Precision Evaluator", false, true) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 88 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 89 | FunctionPass *llvm::createAAEvalPass() { return new AAEval(); } |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 91 | static void PrintResults(const char *Msg, bool P, const Value *V1, |
| 92 | const Value *V2, const Module *M) { |
Chris Lattner | 638b381 | 2003-02-09 20:40:13 +0000 | [diff] [blame] | 93 | if (P) { |
Chris Lattner | 791102f | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 94 | std::string o1, o2; |
| 95 | { |
| 96 | raw_string_ostream os1(o1), os2(o2); |
| 97 | WriteAsOperand(os1, V1, true, M); |
| 98 | WriteAsOperand(os2, V2, true, M); |
| 99 | } |
| 100 | |
Gabor Greif | c0734e3 | 2008-02-28 08:38:45 +0000 | [diff] [blame] | 101 | if (o2 < o1) |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 102 | std::swap(o1, o2); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 103 | errs() << " " << Msg << ":\t" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 104 | << o1 << ", " |
| 105 | << o2 << "\n"; |
Chris Lattner | 638b381 | 2003-02-09 20:40:13 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 109 | static inline void |
Chris Lattner | ad48cc7 | 2004-07-17 06:43:20 +0000 | [diff] [blame] | 110 | PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr, |
| 111 | Module *M) { |
| 112 | if (P) { |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 113 | errs() << " " << Msg << ": Ptr: "; |
| 114 | WriteAsOperand(errs(), Ptr, true, M); |
| 115 | errs() << "\t<->" << *I << '\n'; |
Chris Lattner | ad48cc7 | 2004-07-17 06:43:20 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Dan Gohman | 3dcc91e | 2010-08-04 22:56:29 +0000 | [diff] [blame] | 119 | static inline void |
| 120 | PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB, |
| 121 | Module *M) { |
| 122 | if (P) { |
| 123 | errs() << " " << Msg << ": " << *CSA.getInstruction() |
| 124 | << " <-> " << *CSB.getInstruction() << '\n'; |
| 125 | } |
| 126 | } |
| 127 | |
Manman Ren | a2e3834 | 2013-03-22 22:34:41 +0000 | [diff] [blame^] | 128 | static inline void |
| 129 | PrintLoadStoreResults(const char *Msg, bool P, const Value *V1, |
| 130 | const Value *V2, const Module *M) { |
| 131 | if (P) { |
| 132 | errs() << " " << Msg << ": " << *V1 |
| 133 | << " <-> " << *V2 << '\n'; |
| 134 | } |
| 135 | } |
| 136 | |
Gabor Greif | fcf0f08 | 2010-04-08 16:46:24 +0000 | [diff] [blame] | 137 | static inline bool isInterestingPointer(Value *V) { |
| 138 | return V->getType()->isPointerTy() |
| 139 | && !isa<ConstantPointerNull>(V); |
| 140 | } |
| 141 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 142 | bool AAEval::runOnFunction(Function &F) { |
| 143 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
| 144 | |
| 145 | SetVector<Value *> Pointers; |
| 146 | SetVector<CallSite> CallSites; |
Manman Ren | a2e3834 | 2013-03-22 22:34:41 +0000 | [diff] [blame^] | 147 | SetVector<Value *> Loads; |
| 148 | SetVector<Value *> Stores; |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 149 | |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 150 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) |
Gabor Greif | fcf0f08 | 2010-04-08 16:46:24 +0000 | [diff] [blame] | 151 | if (I->getType()->isPointerTy()) // Add all pointer arguments. |
Chris Lattner | 1842a90 | 2003-06-29 00:07:11 +0000 | [diff] [blame] | 152 | Pointers.insert(I); |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 1842a90 | 2003-06-29 00:07:11 +0000 | [diff] [blame] | 154 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
Gabor Greif | fcf0f08 | 2010-04-08 16:46:24 +0000 | [diff] [blame] | 155 | if (I->getType()->isPointerTy()) // Add all pointer instructions. |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 156 | Pointers.insert(&*I); |
Manman Ren | a2e3834 | 2013-03-22 22:34:41 +0000 | [diff] [blame^] | 157 | if (EvalTBAA && isa<LoadInst>(&*I)) |
| 158 | Loads.insert(&*I); |
| 159 | if (EvalTBAA && isa<StoreInst>(&*I)) |
| 160 | Stores.insert(&*I); |
Chris Lattner | ddc77c4 | 2005-03-17 20:25:04 +0000 | [diff] [blame] | 161 | Instruction &Inst = *I; |
Gabor Greif | 5d4b32e | 2010-07-28 15:31:37 +0000 | [diff] [blame] | 162 | if (CallSite CS = cast<Value>(&Inst)) { |
Gabor Greif | fcf0f08 | 2010-04-08 16:46:24 +0000 | [diff] [blame] | 163 | Value *Callee = CS.getCalledValue(); |
| 164 | // Skip actual functions for direct function calls. |
| 165 | if (!isa<Function>(Callee) && isInterestingPointer(Callee)) |
| 166 | Pointers.insert(Callee); |
| 167 | // Consider formals. |
| 168 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 169 | AI != AE; ++AI) |
| 170 | if (isInterestingPointer(*AI)) |
| 171 | Pointers.insert(*AI); |
Gabor Greif | 5d4b32e | 2010-07-28 15:31:37 +0000 | [diff] [blame] | 172 | CallSites.insert(CS); |
Gabor Greif | fcf0f08 | 2010-04-08 16:46:24 +0000 | [diff] [blame] | 173 | } else { |
| 174 | // Consider all operands. |
| 175 | for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end(); |
| 176 | OI != OE; ++OI) |
| 177 | if (isInterestingPointer(*OI)) |
| 178 | Pointers.insert(*OI); |
| 179 | } |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 182 | if (PrintNoAlias || PrintMayAlias || PrintPartialAlias || PrintMustAlias || |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 183 | PrintNoModRef || PrintMod || PrintRef || PrintModRef) |
| 184 | errs() << "Function: " << F.getName() << ": " << Pointers.size() |
| 185 | << " pointers, " << CallSites.size() << " call sites\n"; |
Chris Lattner | 638b381 | 2003-02-09 20:40:13 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 187 | // iterate over the worklist, and run the full (n^2)/2 disambiguations |
Dan Gohman | a8c711c | 2009-08-26 14:32:17 +0000 | [diff] [blame] | 188 | for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end(); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 189 | I1 != E; ++I1) { |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 190 | uint64_t I1Size = AliasAnalysis::UnknownSize; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 191 | Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType(); |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 192 | if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 193 | |
Dan Gohman | a8c711c | 2009-08-26 14:32:17 +0000 | [diff] [blame] | 194 | for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) { |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 195 | uint64_t I2Size = AliasAnalysis::UnknownSize; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 196 | Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType(); |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 197 | if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 198 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 199 | switch (AA.alias(*I1, I1Size, *I2, I2Size)) { |
Chris Lattner | 638b381 | 2003-02-09 20:40:13 +0000 | [diff] [blame] | 200 | case AliasAnalysis::NoAlias: |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 201 | PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent()); |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 202 | ++NoAlias; break; |
Chris Lattner | 638b381 | 2003-02-09 20:40:13 +0000 | [diff] [blame] | 203 | case AliasAnalysis::MayAlias: |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 204 | PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent()); |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 205 | ++MayAlias; break; |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 206 | case AliasAnalysis::PartialAlias: |
| 207 | PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2, |
| 208 | F.getParent()); |
| 209 | ++PartialAlias; break; |
Chris Lattner | 638b381 | 2003-02-09 20:40:13 +0000 | [diff] [blame] | 210 | case AliasAnalysis::MustAlias: |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 211 | PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent()); |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 212 | ++MustAlias; break; |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 216 | |
Manman Ren | a2e3834 | 2013-03-22 22:34:41 +0000 | [diff] [blame^] | 217 | if (EvalTBAA) { |
| 218 | // iterate over all pairs of load, store |
| 219 | for (SetVector<Value *>::iterator I1 = Loads.begin(), E = Loads.end(); |
| 220 | I1 != E; ++I1) { |
| 221 | for (SetVector<Value *>::iterator I2 = Stores.begin(), E2 = Stores.end(); |
| 222 | I2 != E2; ++I2) { |
| 223 | switch (AA.alias(AA.getLocation(cast<LoadInst>(*I1)), |
| 224 | AA.getLocation(cast<StoreInst>(*I2)))) { |
| 225 | case AliasAnalysis::NoAlias: |
| 226 | PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2, |
| 227 | F.getParent()); |
| 228 | ++NoAlias; break; |
| 229 | case AliasAnalysis::MayAlias: |
| 230 | PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2, |
| 231 | F.getParent()); |
| 232 | ++MayAlias; break; |
| 233 | case AliasAnalysis::PartialAlias: |
| 234 | PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2, |
| 235 | F.getParent()); |
| 236 | ++PartialAlias; break; |
| 237 | case AliasAnalysis::MustAlias: |
| 238 | PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2, |
| 239 | F.getParent()); |
| 240 | ++MustAlias; break; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // iterate over all pairs of store, store |
| 246 | for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end(); |
| 247 | I1 != E; ++I1) { |
| 248 | for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) { |
| 249 | switch (AA.alias(AA.getLocation(cast<StoreInst>(*I1)), |
| 250 | AA.getLocation(cast<StoreInst>(*I2)))) { |
| 251 | case AliasAnalysis::NoAlias: |
| 252 | PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2, |
| 253 | F.getParent()); |
| 254 | ++NoAlias; break; |
| 255 | case AliasAnalysis::MayAlias: |
| 256 | PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2, |
| 257 | F.getParent()); |
| 258 | ++MayAlias; break; |
| 259 | case AliasAnalysis::PartialAlias: |
| 260 | PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2, |
| 261 | F.getParent()); |
| 262 | ++PartialAlias; break; |
| 263 | case AliasAnalysis::MustAlias: |
| 264 | PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2, |
| 265 | F.getParent()); |
| 266 | ++MustAlias; break; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 272 | // Mod/ref alias analysis: compare all pairs of calls and values |
Dan Gohman | a8c711c | 2009-08-26 14:32:17 +0000 | [diff] [blame] | 273 | for (SetVector<CallSite>::iterator C = CallSites.begin(), |
Chris Lattner | 0772e78 | 2005-03-26 22:16:44 +0000 | [diff] [blame] | 274 | Ce = CallSites.end(); C != Ce; ++C) { |
| 275 | Instruction *I = C->getInstruction(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 276 | |
Dan Gohman | a8c711c | 2009-08-26 14:32:17 +0000 | [diff] [blame] | 277 | for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end(); |
Chris Lattner | 0772e78 | 2005-03-26 22:16:44 +0000 | [diff] [blame] | 278 | V != Ve; ++V) { |
Dan Gohman | 3da848b | 2010-10-19 22:54:46 +0000 | [diff] [blame] | 279 | uint64_t Size = AliasAnalysis::UnknownSize; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 280 | Type *ElTy = cast<PointerType>((*V)->getType())->getElementType(); |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 281 | if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 282 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 283 | switch (AA.getModRefInfo(*C, *V, Size)) { |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 284 | case AliasAnalysis::NoModRef: |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 285 | PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent()); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 286 | ++NoModRef; break; |
| 287 | case AliasAnalysis::Mod: |
Dan Gohman | 907857d | 2010-08-04 23:37:55 +0000 | [diff] [blame] | 288 | PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent()); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 289 | ++Mod; break; |
| 290 | case AliasAnalysis::Ref: |
Dan Gohman | 907857d | 2010-08-04 23:37:55 +0000 | [diff] [blame] | 291 | PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent()); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 292 | ++Ref; break; |
| 293 | case AliasAnalysis::ModRef: |
Dan Gohman | 907857d | 2010-08-04 23:37:55 +0000 | [diff] [blame] | 294 | PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent()); |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 295 | ++ModRef; break; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 296 | } |
Chris Lattner | 1ed80b6 | 2004-11-26 21:05:39 +0000 | [diff] [blame] | 297 | } |
Chris Lattner | cb19d67 | 2004-07-17 07:40:34 +0000 | [diff] [blame] | 298 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 299 | |
Dan Gohman | 3dcc91e | 2010-08-04 22:56:29 +0000 | [diff] [blame] | 300 | // Mod/ref alias analysis: compare all pairs of calls |
| 301 | for (SetVector<CallSite>::iterator C = CallSites.begin(), |
| 302 | Ce = CallSites.end(); C != Ce; ++C) { |
| 303 | for (SetVector<CallSite>::iterator D = CallSites.begin(); D != Ce; ++D) { |
| 304 | if (D == C) |
| 305 | continue; |
| 306 | switch (AA.getModRefInfo(*C, *D)) { |
| 307 | case AliasAnalysis::NoModRef: |
| 308 | PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent()); |
| 309 | ++NoModRef; break; |
| 310 | case AliasAnalysis::Mod: |
Dan Gohman | 907857d | 2010-08-04 23:37:55 +0000 | [diff] [blame] | 311 | PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent()); |
Dan Gohman | 3dcc91e | 2010-08-04 22:56:29 +0000 | [diff] [blame] | 312 | ++Mod; break; |
| 313 | case AliasAnalysis::Ref: |
Dan Gohman | 907857d | 2010-08-04 23:37:55 +0000 | [diff] [blame] | 314 | PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent()); |
Dan Gohman | 3dcc91e | 2010-08-04 22:56:29 +0000 | [diff] [blame] | 315 | ++Ref; break; |
| 316 | case AliasAnalysis::ModRef: |
Dan Gohman | 907857d | 2010-08-04 23:37:55 +0000 | [diff] [blame] | 317 | PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent()); |
Dan Gohman | 3dcc91e | 2010-08-04 22:56:29 +0000 | [diff] [blame] | 318 | ++ModRef; break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 323 | return false; |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 326 | static void PrintPercent(unsigned Num, unsigned Sum) { |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 327 | errs() << "(" << Num*100ULL/Sum << "." |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 328 | << ((Num*1000ULL/Sum) % 10) << "%)\n"; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 331 | bool AAEval::doFinalization(Module &M) { |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 332 | unsigned AliasSum = NoAlias + MayAlias + PartialAlias + MustAlias; |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 333 | errs() << "===== Alias Analysis Evaluator Report =====\n"; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 334 | if (AliasSum == 0) { |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 335 | errs() << " Alias Analysis Evaluator Summary: No pointers!\n"; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 336 | } else { |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 337 | errs() << " " << AliasSum << " Total Alias Queries Performed\n"; |
| 338 | errs() << " " << NoAlias << " no alias responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 339 | PrintPercent(NoAlias, AliasSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 340 | errs() << " " << MayAlias << " may alias responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 341 | PrintPercent(MayAlias, AliasSum); |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 342 | errs() << " " << PartialAlias << " partial alias responses "; |
| 343 | PrintPercent(PartialAlias, AliasSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 344 | errs() << " " << MustAlias << " must alias responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 345 | PrintPercent(MustAlias, AliasSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 346 | errs() << " Alias Analysis Evaluator Pointer Alias Summary: " |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 347 | << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/" |
Dan Gohman | 3d9f1ca | 2010-12-10 19:52:40 +0000 | [diff] [blame] | 348 | << PartialAlias*100/AliasSum << "%/" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 349 | << MustAlias*100/AliasSum << "%\n"; |
Chris Lattner | 2c1d7cf | 2003-02-08 23:04:50 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 352 | // Display the summary for mod/ref analysis |
| 353 | unsigned ModRefSum = NoModRef + Mod + Ref + ModRef; |
| 354 | if (ModRefSum == 0) { |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 355 | errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n"; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 356 | } else { |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 357 | errs() << " " << ModRefSum << " Total ModRef Queries Performed\n"; |
| 358 | errs() << " " << NoModRef << " no mod/ref responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 359 | PrintPercent(NoModRef, ModRefSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 360 | errs() << " " << Mod << " mod responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 361 | PrintPercent(Mod, ModRefSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 362 | errs() << " " << Ref << " ref responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 363 | PrintPercent(Ref, ModRefSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 364 | errs() << " " << ModRef << " mod & ref responses "; |
Chris Lattner | e70492d | 2005-03-26 23:56:33 +0000 | [diff] [blame] | 365 | PrintPercent(ModRef, ModRefSum); |
David Greene | 4850a89 | 2009-12-23 22:49:57 +0000 | [diff] [blame] | 366 | errs() << " Alias Analysis Evaluator Mod/Ref Summary: " |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 367 | << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/" |
| 368 | << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n"; |
Misha Brukman | bc1dbe9 | 2004-03-12 06:15:08 +0000 | [diff] [blame] | 369 | } |
Dan Gohman | 9e86f43 | 2010-07-07 14:27:09 +0000 | [diff] [blame] | 370 | |
| 371 | return false; |
Chris Lattner | 9798ca5 | 2003-02-06 21:29:49 +0000 | [diff] [blame] | 372 | } |