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