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