blob: f737cecc43d1462668bf7f753b4a71fa404f3bc9 [file] [log] [blame]
Chris Lattner4fdb75f2003-02-06 21:29:49 +00001//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4fdb75f2003-02-06 21:29:49 +00009
Chandler Carruth4f846a52016-02-20 03:46:03 +000010#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/SetVector.h"
12#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/Constants.h"
Chandler Carruth50fee932015-08-06 02:05:46 +000014#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/DerivedTypes.h"
16#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000017#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Instructions.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/IR/Module.h"
Misha Brukmanbf28cf62004-03-12 06:15:08 +000020#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Support/CommandLine.h"
David Greene22819982009-12-23 19:21:19 +000022#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner8dee8412003-12-10 15:33:59 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Dan Gohmand78c4002008-05-13 00:00:25 +000026static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
27
28static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
29static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
Dan Gohman105d60a2010-12-10 19:52:40 +000030static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000031static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
32
33static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000034static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
Alina Sbirlea50db8a22017-12-21 21:41:53 +000035static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000036static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
Alina Sbirlea50db8a22017-12-21 21:41:53 +000037static cl::opt<bool> PrintMust("print-must", cl::ReallyHidden);
38static cl::opt<bool> PrintMustRef("print-mustref", cl::ReallyHidden);
39static cl::opt<bool> PrintMustMod("print-mustmod", cl::ReallyHidden);
40static cl::opt<bool> PrintMustModRef("print-mustmodref", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000041
Hal Finkelcc39b672014-07-24 12:16:19 +000042static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
Manman Ren0827e972013-03-22 22:34:41 +000043
Chris Lattnerb1d782b2009-08-23 05:17:37 +000044static void PrintResults(const char *Msg, bool P, const Value *V1,
45 const Value *V2, const Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000046 if (PrintAll || P) {
Chris Lattnerb1d782b2009-08-23 05:17:37 +000047 std::string o1, o2;
48 {
49 raw_string_ostream os1(o1), os2(o2);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000050 V1->printAsOperand(os1, true, M);
51 V2->printAsOperand(os2, true, M);
Chris Lattnerb1d782b2009-08-23 05:17:37 +000052 }
53
Gabor Greiff77e6972008-02-28 08:38:45 +000054 if (o2 < o1)
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000055 std::swap(o1, o2);
David Greene0295ecf2009-12-23 22:49:57 +000056 errs() << " " << Msg << ":\t"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000057 << o1 << ", "
58 << o2 << "\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +000059 }
60}
61
Misha Brukman01808ca2005-04-21 21:13:18 +000062static inline void
Chris Lattner2e8690b2004-07-17 06:43:20 +000063PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
64 Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000065 if (PrintAll || P) {
David Greene0295ecf2009-12-23 22:49:57 +000066 errs() << " " << Msg << ": Ptr: ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000067 Ptr->printAsOperand(errs(), true, M);
David Greene0295ecf2009-12-23 22:49:57 +000068 errs() << "\t<->" << *I << '\n';
Chris Lattner2e8690b2004-07-17 06:43:20 +000069 }
70}
71
Dan Gohmanbd33dab2010-08-04 22:56:29 +000072static inline void
73PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
74 Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000075 if (PrintAll || P) {
Dan Gohmanbd33dab2010-08-04 22:56:29 +000076 errs() << " " << Msg << ": " << *CSA.getInstruction()
77 << " <-> " << *CSB.getInstruction() << '\n';
78 }
79}
80
Manman Ren0827e972013-03-22 22:34:41 +000081static inline void
82PrintLoadStoreResults(const char *Msg, bool P, const Value *V1,
83 const Value *V2, const Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000084 if (PrintAll || P) {
Manman Ren0827e972013-03-22 22:34:41 +000085 errs() << " " << Msg << ": " << *V1
86 << " <-> " << *V2 << '\n';
87 }
88}
89
Gabor Greif64d8d1a2010-04-08 16:46:24 +000090static inline bool isInterestingPointer(Value *V) {
91 return V->getType()->isPointerTy()
92 && !isa<ConstantPointerNull>(V);
93}
94
Sean Silva36e0d012016-08-09 00:28:15 +000095PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000096 runInternal(F, AM.getResult<AAManager>(F));
Chandler Carruth4f846a52016-02-20 03:46:03 +000097 return PreservedAnalyses::all();
98}
99
100void AAEvaluator::runInternal(Function &F, AAResults &AA) {
Chandler Carruth50fee932015-08-06 02:05:46 +0000101 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruth4f846a52016-02-20 03:46:03 +0000102
103 ++FunctionCount;
Dan Gohman00ef9322010-07-07 14:27:09 +0000104
105 SetVector<Value *> Pointers;
Rafael Espindola55512f92015-11-18 06:52:18 +0000106 SmallSetVector<CallSite, 16> CallSites;
Manman Ren0827e972013-03-22 22:34:41 +0000107 SetVector<Value *> Loads;
108 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000109
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000110 for (auto &I : F.args())
111 if (I.getType()->isPointerTy()) // Add all pointer arguments.
112 Pointers.insert(&I);
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000113
Chris Lattner83e21a02003-06-29 00:07:11 +0000114 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000115 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000116 Pointers.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000117 if (EvalAAMD && isa<LoadInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000118 Loads.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000119 if (EvalAAMD && isa<StoreInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000120 Stores.insert(&*I);
Chris Lattner9c9f68c2005-03-17 20:25:04 +0000121 Instruction &Inst = *I;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000122 if (auto CS = CallSite(&Inst)) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000123 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 Majnemer2bc25382015-12-23 09:58:46 +0000128 for (Use &DataOp : CS.data_ops())
129 if (isInterestingPointer(DataOp))
130 Pointers.insert(DataOp);
Gabor Greife497e5e2010-07-28 15:31:37 +0000131 CallSites.insert(CS);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000132 } 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 Brukmanbf28cf62004-03-12 06:15:08 +0000139 }
140
Chandler Carruth4f846a52016-02-20 03:46:03 +0000141 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
142 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef)
Dan Gohman00ef9322010-07-07 14:27:09 +0000143 errs() << "Function: " << F.getName() << ": " << Pointers.size()
144 << " pointers, " << CallSites.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000145
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000146 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000147 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000148 I1 != E; ++I1) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000149 uint64_t I1Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000150 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000151 if (I1ElTy->isSized()) I1Size = DL.getTypeStoreSize(I1ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000152
Dan Gohman0672e922009-08-26 14:32:17 +0000153 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000154 uint64_t I2Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000155 Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000156 if (I2ElTy->isSized()) I2Size = DL.getTypeStoreSize(I2ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000157
Dan Gohman00ef9322010-07-07 14:27:09 +0000158 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000159 case NoAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000160 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000161 ++NoAliasCount;
162 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000163 case MayAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000164 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000165 ++MayAliasCount;
166 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000167 case PartialAlias:
Dan Gohman105d60a2010-12-10 19:52:40 +0000168 PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2,
169 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000170 ++PartialAliasCount;
171 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000172 case MustAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000173 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000174 ++MustAliasCount;
175 break;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000176 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000177 }
178 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000179
Hal Finkelcc39b672014-07-24 12:16:19 +0000180 if (EvalAAMD) {
Manman Ren0827e972013-03-22 22:34:41 +0000181 // iterate over all pairs of load, store
Benjamin Krameraa209152016-06-26 17:27:42 +0000182 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 Carruthc3f49eb2015-06-22 02:16:51 +0000186 case NoAlias:
Benjamin Krameraa209152016-06-26 17:27:42 +0000187 PrintLoadStoreResults("NoAlias", PrintNoAlias, Load, Store,
Manman Ren0827e972013-03-22 22:34:41 +0000188 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000189 ++NoAliasCount;
190 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000191 case MayAlias:
Benjamin Krameraa209152016-06-26 17:27:42 +0000192 PrintLoadStoreResults("MayAlias", PrintMayAlias, Load, Store,
Manman Ren0827e972013-03-22 22:34:41 +0000193 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000194 ++MayAliasCount;
195 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000196 case PartialAlias:
Benjamin Krameraa209152016-06-26 17:27:42 +0000197 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, Load, Store,
Manman Ren0827e972013-03-22 22:34:41 +0000198 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000199 ++PartialAliasCount;
200 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000201 case MustAlias:
Benjamin Krameraa209152016-06-26 17:27:42 +0000202 PrintLoadStoreResults("MustAlias", PrintMustAlias, Load, Store,
Manman Ren0827e972013-03-22 22:34:41 +0000203 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000204 ++MustAliasCount;
205 break;
Manman Ren0827e972013-03-22 22:34:41 +0000206 }
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 Carruth70c61c12015-06-04 02:03:15 +0000214 switch (AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
215 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000216 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000217 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
218 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000219 ++NoAliasCount;
220 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000221 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000222 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
223 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000224 ++MayAliasCount;
225 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000226 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000227 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
228 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000229 ++PartialAliasCount;
230 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000231 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000232 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
233 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000234 ++MustAliasCount;
235 break;
Manman Ren0827e972013-03-22 22:34:41 +0000236 }
237 }
238 }
239 }
240
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000241 // Mod/ref alias analysis: compare all pairs of calls and values
Benjamin Krameraa209152016-06-26 17:27:42 +0000242 for (CallSite C : CallSites) {
243 Instruction *I = C.getInstruction();
Misha Brukman01808ca2005-04-21 21:13:18 +0000244
Benjamin Krameraa209152016-06-26 17:27:42 +0000245 for (auto Pointer : Pointers) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000246 uint64_t Size = MemoryLocation::UnknownSize;
Benjamin Krameraa209152016-06-26 17:27:42 +0000247 Type *ElTy = cast<PointerType>(Pointer->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000248 if (ElTy->isSized()) Size = DL.getTypeStoreSize(ElTy);
Misha Brukman01808ca2005-04-21 21:13:18 +0000249
Benjamin Krameraa209152016-06-26 17:27:42 +0000250 switch (AA.getModRefInfo(C, Pointer, Size)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000251 case ModRefInfo::NoModRef:
Benjamin Krameraa209152016-06-26 17:27:42 +0000252 PrintModRefResults("NoModRef", PrintNoModRef, I, Pointer,
253 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000254 ++NoModRefCount;
255 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000256 case ModRefInfo::Mod:
Benjamin Krameraa209152016-06-26 17:27:42 +0000257 PrintModRefResults("Just Mod", PrintMod, I, Pointer, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000258 ++ModCount;
259 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000260 case ModRefInfo::Ref:
Benjamin Krameraa209152016-06-26 17:27:42 +0000261 PrintModRefResults("Just Ref", PrintRef, I, Pointer, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000262 ++RefCount;
263 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000264 case ModRefInfo::ModRef:
Benjamin Krameraa209152016-06-26 17:27:42 +0000265 PrintModRefResults("Both ModRef", PrintModRef, I, Pointer,
266 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000267 ++ModRefCount;
268 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000269 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 Brukmanbf28cf62004-03-12 06:15:08 +0000288 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000289 }
Chris Lattner3bbaaaa2004-07-17 07:40:34 +0000290 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000291
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000292 // Mod/ref alias analysis: compare all pairs of calls
Rafael Espindola55512f92015-11-18 06:52:18 +0000293 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
294 for (auto D = CallSites.begin(); D != Ce; ++D) {
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000295 if (D == C)
296 continue;
297 switch (AA.getModRefInfo(*C, *D)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000298 case ModRefInfo::NoModRef:
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000299 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000300 ++NoModRefCount;
301 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000302 case ModRefInfo::Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000303 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000304 ++ModCount;
305 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000306 case ModRefInfo::Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000307 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000308 ++RefCount;
309 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000310 case ModRefInfo::ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000311 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000312 ++ModRefCount;
313 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000314 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 Gohmanbd33dab2010-08-04 22:56:29 +0000333 }
334 }
335 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000336}
337
Chandler Carruth4f846a52016-02-20 03:46:03 +0000338static void PrintPercent(int64_t Num, int64_t Sum) {
339 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
340 << "%)\n";
Chris Lattner3f08e782005-03-26 23:56:33 +0000341}
342
Chandler Carruth4f846a52016-02-20 03:46:03 +0000343AAEvaluator::~AAEvaluator() {
344 if (FunctionCount == 0)
345 return;
346
347 int64_t AliasSum =
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000348 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
David Greene0295ecf2009-12-23 22:49:57 +0000349 errs() << "===== Alias Analysis Evaluator Report =====\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000350 if (AliasSum == 0) {
David Greene0295ecf2009-12-23 22:49:57 +0000351 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Misha Brukman01808ca2005-04-21 21:13:18 +0000352 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000353 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000354 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 Greene0295ecf2009-12-23 22:49:57 +0000362 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000363 << NoAliasCount * 100 / AliasSum << "%/"
364 << MayAliasCount * 100 / AliasSum << "%/"
365 << PartialAliasCount * 100 / AliasSum << "%/"
366 << MustAliasCount * 100 / AliasSum << "%\n";
Chris Lattnereadcadc2003-02-08 23:04:50 +0000367 }
368
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000369 // Display the summary for mod/ref analysis
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000370 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount +
371 MustCount + MustRefCount + MustModCount + MustModRefCount;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000372 if (ModRefSum == 0) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000373 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
374 "mod/ref!\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000375 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000376 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000377 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 Sbirlea50db8a22017-12-21 21:41:53 +0000385 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 Greene0295ecf2009-12-23 22:49:57 +0000393 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000394 << NoModRefCount * 100 / ModRefSum << "%/"
395 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000396 << "%/" << ModRefCount * 100 / ModRefSum << "%/"
397 << MustCount * 100 / ModRefSum << "%/"
398 << MustRefCount * 100 / ModRefSum << "%/"
399 << MustModCount * 100 / ModRefSum << "%/"
400 << MustModRefCount * 100 / ModRefSum << "%\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000401 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000402}
Chandler Carruth4f846a52016-02-20 03:46:03 +0000403
404namespace llvm {
405class AAEvalLegacyPass : public FunctionPass {
406 std::unique_ptr<AAEvaluator> P;
407
408public:
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
435char AAEvalLegacyPass::ID = 0;
436INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval",
437 "Exhaustive Alias Analysis Precision Evaluator", false,
438 true)
439INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
440INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval",
441 "Exhaustive Alias Analysis Precision Evaluator", false,
442 true)
443
444FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); }