blob: 764ae9160350586781141ccb8f28005dbe64fe8c [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
George Burgess IVaa283d82018-06-14 19:55:53 +000044static void PrintResults(AliasResult AR, bool P, const Value *V1,
Chris Lattnerb1d782b2009-08-23 05:17:37 +000045 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 }
George Burgess IVaa283d82018-06-14 19:55:53 +000053
Gabor Greiff77e6972008-02-28 08:38:45 +000054 if (o2 < o1)
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000055 std::swap(o1, o2);
George Burgess IVaa283d82018-06-14 19:55:53 +000056 errs() << " " << AR << ":\t" << o1 << ", " << o2 << "\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +000057 }
58}
59
George Burgess IVaa283d82018-06-14 19:55:53 +000060static inline void PrintModRefResults(const char *Msg, bool P, Instruction *I,
61 Value *Ptr, Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000062 if (PrintAll || P) {
David Greene0295ecf2009-12-23 22:49:57 +000063 errs() << " " << Msg << ": Ptr: ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000064 Ptr->printAsOperand(errs(), true, M);
David Greene0295ecf2009-12-23 22:49:57 +000065 errs() << "\t<->" << *I << '\n';
Chris Lattner2e8690b2004-07-17 06:43:20 +000066 }
67}
68
George Burgess IVaa283d82018-06-14 19:55:53 +000069static inline void PrintModRefResults(const char *Msg, bool P, CallSite CSA,
70 CallSite CSB, Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000071 if (PrintAll || P) {
George Burgess IVaa283d82018-06-14 19:55:53 +000072 errs() << " " << Msg << ": " << *CSA.getInstruction() << " <-> "
73 << *CSB.getInstruction() << '\n';
Dan Gohmanbd33dab2010-08-04 22:56:29 +000074 }
75}
76
George Burgess IVaa283d82018-06-14 19:55:53 +000077static inline void PrintLoadStoreResults(AliasResult AR, bool P,
78 const Value *V1, const Value *V2,
79 const Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000080 if (PrintAll || P) {
George Burgess IVaa283d82018-06-14 19:55:53 +000081 errs() << " " << AR << ": " << *V1 << " <-> " << *V2 << '\n';
Manman Ren0827e972013-03-22 22:34:41 +000082 }
83}
84
Gabor Greif64d8d1a2010-04-08 16:46:24 +000085static inline bool isInterestingPointer(Value *V) {
86 return V->getType()->isPointerTy()
87 && !isa<ConstantPointerNull>(V);
88}
89
Sean Silva36e0d012016-08-09 00:28:15 +000090PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000091 runInternal(F, AM.getResult<AAManager>(F));
Chandler Carruth4f846a52016-02-20 03:46:03 +000092 return PreservedAnalyses::all();
93}
94
95void AAEvaluator::runInternal(Function &F, AAResults &AA) {
Chandler Carruth50fee932015-08-06 02:05:46 +000096 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruth4f846a52016-02-20 03:46:03 +000097
98 ++FunctionCount;
Dan Gohman00ef9322010-07-07 14:27:09 +000099
100 SetVector<Value *> Pointers;
Rafael Espindola55512f92015-11-18 06:52:18 +0000101 SmallSetVector<CallSite, 16> CallSites;
Manman Ren0827e972013-03-22 22:34:41 +0000102 SetVector<Value *> Loads;
103 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000104
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000105 for (auto &I : F.args())
106 if (I.getType()->isPointerTy()) // Add all pointer arguments.
107 Pointers.insert(&I);
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000108
Chris Lattner83e21a02003-06-29 00:07:11 +0000109 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000110 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000111 Pointers.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000112 if (EvalAAMD && isa<LoadInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000113 Loads.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000114 if (EvalAAMD && isa<StoreInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000115 Stores.insert(&*I);
Chris Lattner9c9f68c2005-03-17 20:25:04 +0000116 Instruction &Inst = *I;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000117 if (auto CS = CallSite(&Inst)) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000118 Value *Callee = CS.getCalledValue();
119 // Skip actual functions for direct function calls.
120 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
121 Pointers.insert(Callee);
122 // Consider formals.
David Majnemer2bc25382015-12-23 09:58:46 +0000123 for (Use &DataOp : CS.data_ops())
124 if (isInterestingPointer(DataOp))
125 Pointers.insert(DataOp);
Gabor Greife497e5e2010-07-28 15:31:37 +0000126 CallSites.insert(CS);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000127 } else {
128 // Consider all operands.
129 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
130 OI != OE; ++OI)
131 if (isInterestingPointer(*OI))
132 Pointers.insert(*OI);
133 }
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000134 }
135
Chandler Carruth4f846a52016-02-20 03:46:03 +0000136 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
137 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef)
Dan Gohman00ef9322010-07-07 14:27:09 +0000138 errs() << "Function: " << F.getName() << ": " << Pointers.size()
139 << " pointers, " << CallSites.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000140
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000141 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000142 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000143 I1 != E; ++I1) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000144 uint64_t I1Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000145 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000146 if (I1ElTy->isSized()) I1Size = DL.getTypeStoreSize(I1ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000147
Dan Gohman0672e922009-08-26 14:32:17 +0000148 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000149 uint64_t I2Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000150 Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000151 if (I2ElTy->isSized()) I2Size = 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
Benjamin Krameraa209152016-06-26 17:27:42 +0000231 for (CallSite C : CallSites) {
232 Instruction *I = C.getInstruction();
Misha Brukman01808ca2005-04-21 21:13:18 +0000233
Benjamin Krameraa209152016-06-26 17:27:42 +0000234 for (auto Pointer : Pointers) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000235 uint64_t Size = MemoryLocation::UnknownSize;
Benjamin Krameraa209152016-06-26 17:27:42 +0000236 Type *ElTy = cast<PointerType>(Pointer->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000237 if (ElTy->isSized()) Size = DL.getTypeStoreSize(ElTy);
Misha Brukman01808ca2005-04-21 21:13:18 +0000238
Benjamin Krameraa209152016-06-26 17:27:42 +0000239 switch (AA.getModRefInfo(C, Pointer, Size)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000240 case ModRefInfo::NoModRef:
Benjamin Krameraa209152016-06-26 17:27:42 +0000241 PrintModRefResults("NoModRef", PrintNoModRef, I, Pointer,
242 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000243 ++NoModRefCount;
244 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000245 case ModRefInfo::Mod:
Benjamin Krameraa209152016-06-26 17:27:42 +0000246 PrintModRefResults("Just Mod", PrintMod, I, Pointer, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000247 ++ModCount;
248 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000249 case ModRefInfo::Ref:
Benjamin Krameraa209152016-06-26 17:27:42 +0000250 PrintModRefResults("Just Ref", PrintRef, I, Pointer, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000251 ++RefCount;
252 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000253 case ModRefInfo::ModRef:
Benjamin Krameraa209152016-06-26 17:27:42 +0000254 PrintModRefResults("Both ModRef", PrintModRef, I, Pointer,
255 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000256 ++ModRefCount;
257 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000258 case ModRefInfo::Must:
259 PrintModRefResults("Must", PrintMust, I, Pointer, F.getParent());
260 ++MustCount;
261 break;
262 case ModRefInfo::MustMod:
263 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, I, Pointer,
264 F.getParent());
265 ++MustModCount;
266 break;
267 case ModRefInfo::MustRef:
268 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, I, Pointer,
269 F.getParent());
270 ++MustRefCount;
271 break;
272 case ModRefInfo::MustModRef:
273 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, I,
274 Pointer, F.getParent());
275 ++MustModRefCount;
276 break;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000277 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000278 }
Chris Lattner3bbaaaa2004-07-17 07:40:34 +0000279 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000280
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000281 // Mod/ref alias analysis: compare all pairs of calls
Rafael Espindola55512f92015-11-18 06:52:18 +0000282 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
283 for (auto D = CallSites.begin(); D != Ce; ++D) {
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000284 if (D == C)
285 continue;
286 switch (AA.getModRefInfo(*C, *D)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000287 case ModRefInfo::NoModRef:
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000288 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000289 ++NoModRefCount;
290 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000291 case ModRefInfo::Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000292 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000293 ++ModCount;
294 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000295 case ModRefInfo::Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000296 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000297 ++RefCount;
298 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000299 case ModRefInfo::ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000300 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000301 ++ModRefCount;
302 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000303 case ModRefInfo::Must:
304 PrintModRefResults("Must", PrintMust, *C, *D, F.getParent());
305 ++MustCount;
306 break;
307 case ModRefInfo::MustMod:
308 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, *C, *D,
309 F.getParent());
310 ++MustModCount;
311 break;
312 case ModRefInfo::MustRef:
313 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, *C, *D,
314 F.getParent());
315 ++MustRefCount;
316 break;
317 case ModRefInfo::MustModRef:
318 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, *C, *D,
319 F.getParent());
320 ++MustModRefCount;
321 break;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000322 }
323 }
324 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000325}
326
Chandler Carruth4f846a52016-02-20 03:46:03 +0000327static void PrintPercent(int64_t Num, int64_t Sum) {
328 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
329 << "%)\n";
Chris Lattner3f08e782005-03-26 23:56:33 +0000330}
331
Chandler Carruth4f846a52016-02-20 03:46:03 +0000332AAEvaluator::~AAEvaluator() {
333 if (FunctionCount == 0)
334 return;
335
336 int64_t AliasSum =
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000337 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
David Greene0295ecf2009-12-23 22:49:57 +0000338 errs() << "===== Alias Analysis Evaluator Report =====\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000339 if (AliasSum == 0) {
David Greene0295ecf2009-12-23 22:49:57 +0000340 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Misha Brukman01808ca2005-04-21 21:13:18 +0000341 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000342 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000343 errs() << " " << NoAliasCount << " no alias responses ";
344 PrintPercent(NoAliasCount, AliasSum);
345 errs() << " " << MayAliasCount << " may alias responses ";
346 PrintPercent(MayAliasCount, AliasSum);
347 errs() << " " << PartialAliasCount << " partial alias responses ";
348 PrintPercent(PartialAliasCount, AliasSum);
349 errs() << " " << MustAliasCount << " must alias responses ";
350 PrintPercent(MustAliasCount, AliasSum);
David Greene0295ecf2009-12-23 22:49:57 +0000351 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000352 << NoAliasCount * 100 / AliasSum << "%/"
353 << MayAliasCount * 100 / AliasSum << "%/"
354 << PartialAliasCount * 100 / AliasSum << "%/"
355 << MustAliasCount * 100 / AliasSum << "%\n";
Chris Lattnereadcadc2003-02-08 23:04:50 +0000356 }
357
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000358 // Display the summary for mod/ref analysis
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000359 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount +
360 MustCount + MustRefCount + MustModCount + MustModRefCount;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000361 if (ModRefSum == 0) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000362 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
363 "mod/ref!\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000364 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000365 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000366 errs() << " " << NoModRefCount << " no mod/ref responses ";
367 PrintPercent(NoModRefCount, ModRefSum);
368 errs() << " " << ModCount << " mod responses ";
369 PrintPercent(ModCount, ModRefSum);
370 errs() << " " << RefCount << " ref responses ";
371 PrintPercent(RefCount, ModRefSum);
372 errs() << " " << ModRefCount << " mod & ref responses ";
373 PrintPercent(ModRefCount, ModRefSum);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000374 errs() << " " << MustCount << " must responses ";
375 PrintPercent(MustCount, ModRefSum);
376 errs() << " " << MustModCount << " must mod responses ";
377 PrintPercent(MustModCount, ModRefSum);
378 errs() << " " << MustRefCount << " must ref responses ";
379 PrintPercent(MustRefCount, ModRefSum);
380 errs() << " " << MustModRefCount << " must mod & ref responses ";
381 PrintPercent(MustModRefCount, ModRefSum);
David Greene0295ecf2009-12-23 22:49:57 +0000382 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000383 << NoModRefCount * 100 / ModRefSum << "%/"
384 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000385 << "%/" << ModRefCount * 100 / ModRefSum << "%/"
386 << MustCount * 100 / ModRefSum << "%/"
387 << MustRefCount * 100 / ModRefSum << "%/"
388 << MustModCount * 100 / ModRefSum << "%/"
389 << MustModRefCount * 100 / ModRefSum << "%\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000390 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000391}
Chandler Carruth4f846a52016-02-20 03:46:03 +0000392
393namespace llvm {
394class AAEvalLegacyPass : public FunctionPass {
395 std::unique_ptr<AAEvaluator> P;
396
397public:
398 static char ID; // Pass identification, replacement for typeid
399 AAEvalLegacyPass() : FunctionPass(ID) {
400 initializeAAEvalLegacyPassPass(*PassRegistry::getPassRegistry());
401 }
402
403 void getAnalysisUsage(AnalysisUsage &AU) const override {
404 AU.addRequired<AAResultsWrapperPass>();
405 AU.setPreservesAll();
406 }
407
408 bool doInitialization(Module &M) override {
409 P.reset(new AAEvaluator());
410 return false;
411 }
412
413 bool runOnFunction(Function &F) override {
414 P->runInternal(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
415 return false;
416 }
417 bool doFinalization(Module &M) override {
418 P.reset();
419 return false;
420 }
421};
422}
423
424char AAEvalLegacyPass::ID = 0;
425INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval",
426 "Exhaustive Alias Analysis Precision Evaluator", false,
427 true)
428INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
429INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval",
430 "Exhaustive Alias Analysis Precision Evaluator", false,
431 true)
432
433FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); }