blob: e83703867e09a1180cf8dcb432bcb2d401bd8ee5 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner4fdb75f2003-02-06 21:29:49 +00008
Chandler Carruth4f846a52016-02-20 03:46:03 +00009#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000010#include "llvm/ADT/SetVector.h"
11#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/Constants.h"
Chandler Carruth50fee932015-08-06 02:05:46 +000013#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/DerivedTypes.h"
15#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000016#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Instructions.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/IR/Module.h"
Misha Brukmanbf28cf62004-03-12 06:15:08 +000019#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/CommandLine.h"
David Greene22819982009-12-23 19:21:19 +000021#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner8dee8412003-12-10 15:33:59 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Dan Gohmand78c4002008-05-13 00:00:25 +000025static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
26
27static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
28static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
Dan Gohman105d60a2010-12-10 19:52:40 +000029static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000030static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
31
32static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000033static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
Alina Sbirlea50db8a22017-12-21 21:41:53 +000034static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000035static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
Alina Sbirlea50db8a22017-12-21 21:41:53 +000036static cl::opt<bool> PrintMust("print-must", cl::ReallyHidden);
37static cl::opt<bool> PrintMustRef("print-mustref", cl::ReallyHidden);
38static cl::opt<bool> PrintMustMod("print-mustmod", cl::ReallyHidden);
39static cl::opt<bool> PrintMustModRef("print-mustmodref", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000040
Hal Finkelcc39b672014-07-24 12:16:19 +000041static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
Manman Ren0827e972013-03-22 22:34:41 +000042
George Burgess IVaa283d82018-06-14 19:55:53 +000043static void PrintResults(AliasResult AR, bool P, const Value *V1,
Chris Lattnerb1d782b2009-08-23 05:17:37 +000044 const Value *V2, const Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000045 if (PrintAll || P) {
Chris Lattnerb1d782b2009-08-23 05:17:37 +000046 std::string o1, o2;
47 {
48 raw_string_ostream os1(o1), os2(o2);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000049 V1->printAsOperand(os1, true, M);
50 V2->printAsOperand(os2, true, M);
Chris Lattnerb1d782b2009-08-23 05:17:37 +000051 }
George Burgess IVaa283d82018-06-14 19:55:53 +000052
Gabor Greiff77e6972008-02-28 08:38:45 +000053 if (o2 < o1)
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000054 std::swap(o1, o2);
George Burgess IVaa283d82018-06-14 19:55:53 +000055 errs() << " " << AR << ":\t" << o1 << ", " << o2 << "\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +000056 }
57}
58
George Burgess IVaa283d82018-06-14 19:55:53 +000059static inline void PrintModRefResults(const char *Msg, bool P, Instruction *I,
60 Value *Ptr, Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000061 if (PrintAll || P) {
David Greene0295ecf2009-12-23 22:49:57 +000062 errs() << " " << Msg << ": Ptr: ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000063 Ptr->printAsOperand(errs(), true, M);
David Greene0295ecf2009-12-23 22:49:57 +000064 errs() << "\t<->" << *I << '\n';
Chris Lattner2e8690b2004-07-17 06:43:20 +000065 }
66}
67
Chandler Carruth363ac682019-01-07 05:42:51 +000068static inline void PrintModRefResults(const char *Msg, bool P, CallBase *CallA,
69 CallBase *CallB, Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000070 if (PrintAll || P) {
Chandler Carruth363ac682019-01-07 05:42:51 +000071 errs() << " " << Msg << ": " << *CallA << " <-> " << *CallB << '\n';
Dan Gohmanbd33dab2010-08-04 22:56:29 +000072 }
73}
74
George Burgess IVaa283d82018-06-14 19:55:53 +000075static inline void PrintLoadStoreResults(AliasResult AR, bool P,
76 const Value *V1, const Value *V2,
77 const Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000078 if (PrintAll || P) {
George Burgess IVaa283d82018-06-14 19:55:53 +000079 errs() << " " << AR << ": " << *V1 << " <-> " << *V2 << '\n';
Manman Ren0827e972013-03-22 22:34:41 +000080 }
81}
82
Gabor Greif64d8d1a2010-04-08 16:46:24 +000083static inline bool isInterestingPointer(Value *V) {
84 return V->getType()->isPointerTy()
85 && !isa<ConstantPointerNull>(V);
86}
87
Sean Silva36e0d012016-08-09 00:28:15 +000088PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000089 runInternal(F, AM.getResult<AAManager>(F));
Chandler Carruth4f846a52016-02-20 03:46:03 +000090 return PreservedAnalyses::all();
91}
92
93void AAEvaluator::runInternal(Function &F, AAResults &AA) {
Chandler Carruth50fee932015-08-06 02:05:46 +000094 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruth4f846a52016-02-20 03:46:03 +000095
96 ++FunctionCount;
Dan Gohman00ef9322010-07-07 14:27:09 +000097
98 SetVector<Value *> Pointers;
Chandler Carruth363ac682019-01-07 05:42:51 +000099 SmallSetVector<CallBase *, 16> Calls;
Manman Ren0827e972013-03-22 22:34:41 +0000100 SetVector<Value *> Loads;
101 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000102
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000103 for (auto &I : F.args())
104 if (I.getType()->isPointerTy()) // Add all pointer arguments.
105 Pointers.insert(&I);
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000106
Chris Lattner83e21a02003-06-29 00:07:11 +0000107 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000108 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000109 Pointers.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000110 if (EvalAAMD && isa<LoadInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000111 Loads.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000112 if (EvalAAMD && isa<StoreInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000113 Stores.insert(&*I);
Chris Lattner9c9f68c2005-03-17 20:25:04 +0000114 Instruction &Inst = *I;
Chandler Carruth363ac682019-01-07 05:42:51 +0000115 if (auto *Call = dyn_cast<CallBase>(&Inst)) {
116 Value *Callee = Call->getCalledValue();
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000117 // Skip actual functions for direct function calls.
118 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
119 Pointers.insert(Callee);
120 // Consider formals.
Chandler Carruth363ac682019-01-07 05:42:51 +0000121 for (Use &DataOp : Call->data_ops())
David Majnemer2bc25382015-12-23 09:58:46 +0000122 if (isInterestingPointer(DataOp))
123 Pointers.insert(DataOp);
Chandler Carruth363ac682019-01-07 05:42:51 +0000124 Calls.insert(Call);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000125 } 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 Brukmanbf28cf62004-03-12 06:15:08 +0000132 }
133
Chandler Carruth4f846a52016-02-20 03:46:03 +0000134 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
135 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef)
Dan Gohman00ef9322010-07-07 14:27:09 +0000136 errs() << "Function: " << F.getName() << ": " << Pointers.size()
Chandler Carruth363ac682019-01-07 05:42:51 +0000137 << " pointers, " << Calls.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000138
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000139 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000140 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000141 I1 != E; ++I1) {
George Burgess IV685e7812018-12-23 02:39:58 +0000142 auto I1Size = LocationSize::unknown();
Chris Lattner229907c2011-07-18 04:54:35 +0000143 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
George Burgess IV685e7812018-12-23 02:39:58 +0000144 if (I1ElTy->isSized())
145 I1Size = LocationSize::precise(DL.getTypeStoreSize(I1ElTy));
Chris Lattnerf30656b2004-11-26 21:05:39 +0000146
Dan Gohman0672e922009-08-26 14:32:17 +0000147 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
George Burgess IV685e7812018-12-23 02:39:58 +0000148 auto I2Size = LocationSize::unknown();
149 Type *I2ElTy = cast<PointerType>((*I2)->getType())->getElementType();
150 if (I2ElTy->isSized())
151 I2Size = LocationSize::precise(DL.getTypeStoreSize(I2ElTy));
Chris Lattnerf30656b2004-11-26 21:05:39 +0000152
George Burgess IVaa283d82018-06-14 19:55:53 +0000153 AliasResult AR = AA.alias(*I1, I1Size, *I2, I2Size);
154 switch (AR) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000155 case NoAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000156 PrintResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000157 ++NoAliasCount;
158 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000159 case MayAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000160 PrintResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000161 ++MayAliasCount;
162 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000163 case PartialAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000164 PrintResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000165 ++PartialAliasCount;
166 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000167 case MustAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000168 PrintResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000169 ++MustAliasCount;
170 break;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000171 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000172 }
173 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000174
Hal Finkelcc39b672014-07-24 12:16:19 +0000175 if (EvalAAMD) {
Manman Ren0827e972013-03-22 22:34:41 +0000176 // iterate over all pairs of load, store
Benjamin Krameraa209152016-06-26 17:27:42 +0000177 for (Value *Load : Loads) {
178 for (Value *Store : Stores) {
George Burgess IVaa283d82018-06-14 19:55:53 +0000179 AliasResult AR = AA.alias(MemoryLocation::get(cast<LoadInst>(Load)),
180 MemoryLocation::get(cast<StoreInst>(Store)));
181 switch (AR) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000182 case NoAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000183 PrintLoadStoreResults(AR, PrintNoAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000184 ++NoAliasCount;
185 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000186 case MayAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000187 PrintLoadStoreResults(AR, PrintMayAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000188 ++MayAliasCount;
189 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000190 case PartialAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000191 PrintLoadStoreResults(AR, PrintPartialAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000192 ++PartialAliasCount;
193 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000194 case MustAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000195 PrintLoadStoreResults(AR, PrintMustAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000196 ++MustAliasCount;
197 break;
Manman Ren0827e972013-03-22 22:34:41 +0000198 }
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 IVaa283d82018-06-14 19:55:53 +0000206 AliasResult AR = AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
207 MemoryLocation::get(cast<StoreInst>(*I2)));
208 switch (AR) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000209 case NoAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000210 PrintLoadStoreResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000211 ++NoAliasCount;
212 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000213 case MayAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000214 PrintLoadStoreResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000215 ++MayAliasCount;
216 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000217 case PartialAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000218 PrintLoadStoreResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000219 ++PartialAliasCount;
220 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000221 case MustAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000222 PrintLoadStoreResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000223 ++MustAliasCount;
224 break;
Manman Ren0827e972013-03-22 22:34:41 +0000225 }
226 }
227 }
228 }
229
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000230 // Mod/ref alias analysis: compare all pairs of calls and values
Chandler Carruth363ac682019-01-07 05:42:51 +0000231 for (CallBase *Call : Calls) {
Benjamin Krameraa209152016-06-26 17:27:42 +0000232 for (auto Pointer : Pointers) {
George Burgess IV685e7812018-12-23 02:39:58 +0000233 auto Size = LocationSize::unknown();
Benjamin Krameraa209152016-06-26 17:27:42 +0000234 Type *ElTy = cast<PointerType>(Pointer->getType())->getElementType();
George Burgess IV685e7812018-12-23 02:39:58 +0000235 if (ElTy->isSized())
236 Size = LocationSize::precise(DL.getTypeStoreSize(ElTy));
Misha Brukman01808ca2005-04-21 21:13:18 +0000237
Chandler Carruth363ac682019-01-07 05:42:51 +0000238 switch (AA.getModRefInfo(Call, Pointer, Size)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000239 case ModRefInfo::NoModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000240 PrintModRefResults("NoModRef", PrintNoModRef, Call, Pointer,
Benjamin Krameraa209152016-06-26 17:27:42 +0000241 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000242 ++NoModRefCount;
243 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000244 case ModRefInfo::Mod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000245 PrintModRefResults("Just Mod", PrintMod, Call, Pointer, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000246 ++ModCount;
247 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000248 case ModRefInfo::Ref:
Chandler Carruth363ac682019-01-07 05:42:51 +0000249 PrintModRefResults("Just Ref", PrintRef, Call, Pointer, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000250 ++RefCount;
251 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000252 case ModRefInfo::ModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000253 PrintModRefResults("Both ModRef", PrintModRef, Call, Pointer,
Benjamin Krameraa209152016-06-26 17:27:42 +0000254 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000255 ++ModRefCount;
256 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000257 case ModRefInfo::Must:
Chandler Carruth363ac682019-01-07 05:42:51 +0000258 PrintModRefResults("Must", PrintMust, Call, Pointer, F.getParent());
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000259 ++MustCount;
260 break;
261 case ModRefInfo::MustMod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000262 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, Call, Pointer,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000263 F.getParent());
264 ++MustModCount;
265 break;
266 case ModRefInfo::MustRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000267 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, Call, Pointer,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000268 F.getParent());
269 ++MustRefCount;
270 break;
271 case ModRefInfo::MustModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000272 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, Call,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000273 Pointer, F.getParent());
274 ++MustModRefCount;
275 break;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000276 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000277 }
Chris Lattner3bbaaaa2004-07-17 07:40:34 +0000278 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000279
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000280 // Mod/ref alias analysis: compare all pairs of calls
Chandler Carruth363ac682019-01-07 05:42:51 +0000281 for (CallBase *CallA : Calls) {
282 for (CallBase *CallB : Calls) {
283 if (CallA == CallB)
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000284 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000285 switch (AA.getModRefInfo(CallA, CallB)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000286 case ModRefInfo::NoModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000287 PrintModRefResults("NoModRef", PrintNoModRef, CallA, CallB,
288 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000289 ++NoModRefCount;
290 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000291 case ModRefInfo::Mod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000292 PrintModRefResults("Just Mod", PrintMod, CallA, CallB, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000293 ++ModCount;
294 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000295 case ModRefInfo::Ref:
Chandler Carruth363ac682019-01-07 05:42:51 +0000296 PrintModRefResults("Just Ref", PrintRef, CallA, CallB, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000297 ++RefCount;
298 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000299 case ModRefInfo::ModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000300 PrintModRefResults("Both ModRef", PrintModRef, CallA, CallB,
301 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000302 ++ModRefCount;
303 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000304 case ModRefInfo::Must:
Chandler Carruth363ac682019-01-07 05:42:51 +0000305 PrintModRefResults("Must", PrintMust, CallA, CallB, F.getParent());
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000306 ++MustCount;
307 break;
308 case ModRefInfo::MustMod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000309 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, CallA, CallB,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000310 F.getParent());
311 ++MustModCount;
312 break;
313 case ModRefInfo::MustRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000314 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, CallA, CallB,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000315 F.getParent());
316 ++MustRefCount;
317 break;
318 case ModRefInfo::MustModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000319 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, CallA,
320 CallB, F.getParent());
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000321 ++MustModRefCount;
322 break;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000323 }
324 }
325 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000326}
327
Chandler Carruth4f846a52016-02-20 03:46:03 +0000328static void PrintPercent(int64_t Num, int64_t Sum) {
329 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
330 << "%)\n";
Chris Lattner3f08e782005-03-26 23:56:33 +0000331}
332
Chandler Carruth4f846a52016-02-20 03:46:03 +0000333AAEvaluator::~AAEvaluator() {
334 if (FunctionCount == 0)
335 return;
336
337 int64_t AliasSum =
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000338 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
David Greene0295ecf2009-12-23 22:49:57 +0000339 errs() << "===== Alias Analysis Evaluator Report =====\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000340 if (AliasSum == 0) {
David Greene0295ecf2009-12-23 22:49:57 +0000341 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Misha Brukman01808ca2005-04-21 21:13:18 +0000342 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000343 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000344 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 Greene0295ecf2009-12-23 22:49:57 +0000352 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000353 << NoAliasCount * 100 / AliasSum << "%/"
354 << MayAliasCount * 100 / AliasSum << "%/"
355 << PartialAliasCount * 100 / AliasSum << "%/"
356 << MustAliasCount * 100 / AliasSum << "%\n";
Chris Lattnereadcadc2003-02-08 23:04:50 +0000357 }
358
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000359 // Display the summary for mod/ref analysis
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000360 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount +
361 MustCount + MustRefCount + MustModCount + MustModRefCount;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000362 if (ModRefSum == 0) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000363 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
364 "mod/ref!\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000365 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000366 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000367 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 Sbirlea50db8a22017-12-21 21:41:53 +0000375 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 Greene0295ecf2009-12-23 22:49:57 +0000383 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000384 << NoModRefCount * 100 / ModRefSum << "%/"
385 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000386 << "%/" << ModRefCount * 100 / ModRefSum << "%/"
387 << MustCount * 100 / ModRefSum << "%/"
388 << MustRefCount * 100 / ModRefSum << "%/"
389 << MustModCount * 100 / ModRefSum << "%/"
390 << MustModRefCount * 100 / ModRefSum << "%\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000391 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000392}
Chandler Carruth4f846a52016-02-20 03:46:03 +0000393
394namespace llvm {
395class AAEvalLegacyPass : public FunctionPass {
396 std::unique_ptr<AAEvaluator> P;
397
398public:
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
425char AAEvalLegacyPass::ID = 0;
426INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval",
427 "Exhaustive Alias Analysis Precision Evaluator", false,
428 true)
429INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
430INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval",
431 "Exhaustive Alias Analysis Precision Evaluator", false,
432 true)
433
434FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); }