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