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