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