blob: 85dd4fe95b33dd7e5326c69618934584dda28bb7 [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
Chandler Carruth363ac682019-01-07 05:42:51 +000069static inline void PrintModRefResults(const char *Msg, bool P, CallBase *CallA,
70 CallBase *CallB, Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000071 if (PrintAll || P) {
Chandler Carruth363ac682019-01-07 05:42:51 +000072 errs() << " " << Msg << ": " << *CallA << " <-> " << *CallB << '\n';
Dan Gohmanbd33dab2010-08-04 22:56:29 +000073 }
74}
75
George Burgess IVaa283d82018-06-14 19:55:53 +000076static inline void PrintLoadStoreResults(AliasResult AR, bool P,
77 const Value *V1, const Value *V2,
78 const Module *M) {
Chandler Carruth4f846a52016-02-20 03:46:03 +000079 if (PrintAll || P) {
George Burgess IVaa283d82018-06-14 19:55:53 +000080 errs() << " " << AR << ": " << *V1 << " <-> " << *V2 << '\n';
Manman Ren0827e972013-03-22 22:34:41 +000081 }
82}
83
Gabor Greif64d8d1a2010-04-08 16:46:24 +000084static inline bool isInterestingPointer(Value *V) {
85 return V->getType()->isPointerTy()
86 && !isa<ConstantPointerNull>(V);
87}
88
Sean Silva36e0d012016-08-09 00:28:15 +000089PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000090 runInternal(F, AM.getResult<AAManager>(F));
Chandler Carruth4f846a52016-02-20 03:46:03 +000091 return PreservedAnalyses::all();
92}
93
94void AAEvaluator::runInternal(Function &F, AAResults &AA) {
Chandler Carruth50fee932015-08-06 02:05:46 +000095 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruth4f846a52016-02-20 03:46:03 +000096
97 ++FunctionCount;
Dan Gohman00ef9322010-07-07 14:27:09 +000098
99 SetVector<Value *> Pointers;
Chandler Carruth363ac682019-01-07 05:42:51 +0000100 SmallSetVector<CallBase *, 16> Calls;
Manman Ren0827e972013-03-22 22:34:41 +0000101 SetVector<Value *> Loads;
102 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000103
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000104 for (auto &I : F.args())
105 if (I.getType()->isPointerTy()) // Add all pointer arguments.
106 Pointers.insert(&I);
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000107
Chris Lattner83e21a02003-06-29 00:07:11 +0000108 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000109 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000110 Pointers.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000111 if (EvalAAMD && isa<LoadInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000112 Loads.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000113 if (EvalAAMD && isa<StoreInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000114 Stores.insert(&*I);
Chris Lattner9c9f68c2005-03-17 20:25:04 +0000115 Instruction &Inst = *I;
Chandler Carruth363ac682019-01-07 05:42:51 +0000116 if (auto *Call = dyn_cast<CallBase>(&Inst)) {
117 Value *Callee = Call->getCalledValue();
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000118 // Skip actual functions for direct function calls.
119 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
120 Pointers.insert(Callee);
121 // Consider formals.
Chandler Carruth363ac682019-01-07 05:42:51 +0000122 for (Use &DataOp : Call->data_ops())
David Majnemer2bc25382015-12-23 09:58:46 +0000123 if (isInterestingPointer(DataOp))
124 Pointers.insert(DataOp);
Chandler Carruth363ac682019-01-07 05:42:51 +0000125 Calls.insert(Call);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000126 } else {
127 // Consider all operands.
128 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
129 OI != OE; ++OI)
130 if (isInterestingPointer(*OI))
131 Pointers.insert(*OI);
132 }
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000133 }
134
Chandler Carruth4f846a52016-02-20 03:46:03 +0000135 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
136 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef)
Dan Gohman00ef9322010-07-07 14:27:09 +0000137 errs() << "Function: " << F.getName() << ": " << Pointers.size()
Chandler Carruth363ac682019-01-07 05:42:51 +0000138 << " pointers, " << Calls.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000139
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000140 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000141 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000142 I1 != E; ++I1) {
George Burgess IV685e7812018-12-23 02:39:58 +0000143 auto I1Size = LocationSize::unknown();
Chris Lattner229907c2011-07-18 04:54:35 +0000144 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
George Burgess IV685e7812018-12-23 02:39:58 +0000145 if (I1ElTy->isSized())
146 I1Size = LocationSize::precise(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) {
George Burgess IV685e7812018-12-23 02:39:58 +0000149 auto I2Size = LocationSize::unknown();
150 Type *I2ElTy = cast<PointerType>((*I2)->getType())->getElementType();
151 if (I2ElTy->isSized())
152 I2Size = LocationSize::precise(DL.getTypeStoreSize(I2ElTy));
Chris Lattnerf30656b2004-11-26 21:05:39 +0000153
George Burgess IVaa283d82018-06-14 19:55:53 +0000154 AliasResult AR = AA.alias(*I1, I1Size, *I2, I2Size);
155 switch (AR) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000156 case NoAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000157 PrintResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000158 ++NoAliasCount;
159 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000160 case MayAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000161 PrintResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000162 ++MayAliasCount;
163 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000164 case PartialAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000165 PrintResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000166 ++PartialAliasCount;
167 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000168 case MustAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000169 PrintResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000170 ++MustAliasCount;
171 break;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000172 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000173 }
174 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000175
Hal Finkelcc39b672014-07-24 12:16:19 +0000176 if (EvalAAMD) {
Manman Ren0827e972013-03-22 22:34:41 +0000177 // iterate over all pairs of load, store
Benjamin Krameraa209152016-06-26 17:27:42 +0000178 for (Value *Load : Loads) {
179 for (Value *Store : Stores) {
George Burgess IVaa283d82018-06-14 19:55:53 +0000180 AliasResult AR = AA.alias(MemoryLocation::get(cast<LoadInst>(Load)),
181 MemoryLocation::get(cast<StoreInst>(Store)));
182 switch (AR) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000183 case NoAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000184 PrintLoadStoreResults(AR, PrintNoAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000185 ++NoAliasCount;
186 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000187 case MayAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000188 PrintLoadStoreResults(AR, PrintMayAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000189 ++MayAliasCount;
190 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000191 case PartialAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000192 PrintLoadStoreResults(AR, PrintPartialAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000193 ++PartialAliasCount;
194 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000195 case MustAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000196 PrintLoadStoreResults(AR, PrintMustAlias, Load, Store, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000197 ++MustAliasCount;
198 break;
Manman Ren0827e972013-03-22 22:34:41 +0000199 }
200 }
201 }
202
203 // iterate over all pairs of store, store
204 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
205 I1 != E; ++I1) {
206 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
George Burgess IVaa283d82018-06-14 19:55:53 +0000207 AliasResult AR = AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
208 MemoryLocation::get(cast<StoreInst>(*I2)));
209 switch (AR) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000210 case NoAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000211 PrintLoadStoreResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000212 ++NoAliasCount;
213 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000214 case MayAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000215 PrintLoadStoreResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000216 ++MayAliasCount;
217 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000218 case PartialAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000219 PrintLoadStoreResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000220 ++PartialAliasCount;
221 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000222 case MustAlias:
George Burgess IVaa283d82018-06-14 19:55:53 +0000223 PrintLoadStoreResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000224 ++MustAliasCount;
225 break;
Manman Ren0827e972013-03-22 22:34:41 +0000226 }
227 }
228 }
229 }
230
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000231 // Mod/ref alias analysis: compare all pairs of calls and values
Chandler Carruth363ac682019-01-07 05:42:51 +0000232 for (CallBase *Call : Calls) {
Benjamin Krameraa209152016-06-26 17:27:42 +0000233 for (auto Pointer : Pointers) {
George Burgess IV685e7812018-12-23 02:39:58 +0000234 auto Size = LocationSize::unknown();
Benjamin Krameraa209152016-06-26 17:27:42 +0000235 Type *ElTy = cast<PointerType>(Pointer->getType())->getElementType();
George Burgess IV685e7812018-12-23 02:39:58 +0000236 if (ElTy->isSized())
237 Size = LocationSize::precise(DL.getTypeStoreSize(ElTy));
Misha Brukman01808ca2005-04-21 21:13:18 +0000238
Chandler Carruth363ac682019-01-07 05:42:51 +0000239 switch (AA.getModRefInfo(Call, Pointer, Size)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000240 case ModRefInfo::NoModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000241 PrintModRefResults("NoModRef", PrintNoModRef, Call, Pointer,
Benjamin Krameraa209152016-06-26 17:27:42 +0000242 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000243 ++NoModRefCount;
244 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000245 case ModRefInfo::Mod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000246 PrintModRefResults("Just Mod", PrintMod, Call, 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:
Chandler Carruth363ac682019-01-07 05:42:51 +0000250 PrintModRefResults("Just Ref", PrintRef, Call, 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:
Chandler Carruth363ac682019-01-07 05:42:51 +0000254 PrintModRefResults("Both ModRef", PrintModRef, Call, Pointer,
Benjamin Krameraa209152016-06-26 17:27:42 +0000255 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000256 ++ModRefCount;
257 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000258 case ModRefInfo::Must:
Chandler Carruth363ac682019-01-07 05:42:51 +0000259 PrintModRefResults("Must", PrintMust, Call, Pointer, F.getParent());
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000260 ++MustCount;
261 break;
262 case ModRefInfo::MustMod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000263 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, Call, Pointer,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000264 F.getParent());
265 ++MustModCount;
266 break;
267 case ModRefInfo::MustRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000268 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, Call, Pointer,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000269 F.getParent());
270 ++MustRefCount;
271 break;
272 case ModRefInfo::MustModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000273 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, Call,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000274 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
Chandler Carruth363ac682019-01-07 05:42:51 +0000282 for (CallBase *CallA : Calls) {
283 for (CallBase *CallB : Calls) {
284 if (CallA == CallB)
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000285 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000286 switch (AA.getModRefInfo(CallA, CallB)) {
Alina Sbirlea193429f2017-12-07 22:41:34 +0000287 case ModRefInfo::NoModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000288 PrintModRefResults("NoModRef", PrintNoModRef, CallA, CallB,
289 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000290 ++NoModRefCount;
291 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000292 case ModRefInfo::Mod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000293 PrintModRefResults("Just Mod", PrintMod, CallA, CallB, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000294 ++ModCount;
295 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000296 case ModRefInfo::Ref:
Chandler Carruth363ac682019-01-07 05:42:51 +0000297 PrintModRefResults("Just Ref", PrintRef, CallA, CallB, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000298 ++RefCount;
299 break;
Alina Sbirlea193429f2017-12-07 22:41:34 +0000300 case ModRefInfo::ModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000301 PrintModRefResults("Both ModRef", PrintModRef, CallA, CallB,
302 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000303 ++ModRefCount;
304 break;
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000305 case ModRefInfo::Must:
Chandler Carruth363ac682019-01-07 05:42:51 +0000306 PrintModRefResults("Must", PrintMust, CallA, CallB, F.getParent());
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000307 ++MustCount;
308 break;
309 case ModRefInfo::MustMod:
Chandler Carruth363ac682019-01-07 05:42:51 +0000310 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, CallA, CallB,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000311 F.getParent());
312 ++MustModCount;
313 break;
314 case ModRefInfo::MustRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000315 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, CallA, CallB,
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000316 F.getParent());
317 ++MustRefCount;
318 break;
319 case ModRefInfo::MustModRef:
Chandler Carruth363ac682019-01-07 05:42:51 +0000320 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, CallA,
321 CallB, F.getParent());
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000322 ++MustModRefCount;
323 break;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000324 }
325 }
326 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000327}
328
Chandler Carruth4f846a52016-02-20 03:46:03 +0000329static void PrintPercent(int64_t Num, int64_t Sum) {
330 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
331 << "%)\n";
Chris Lattner3f08e782005-03-26 23:56:33 +0000332}
333
Chandler Carruth4f846a52016-02-20 03:46:03 +0000334AAEvaluator::~AAEvaluator() {
335 if (FunctionCount == 0)
336 return;
337
338 int64_t AliasSum =
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000339 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
David Greene0295ecf2009-12-23 22:49:57 +0000340 errs() << "===== Alias Analysis Evaluator Report =====\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000341 if (AliasSum == 0) {
David Greene0295ecf2009-12-23 22:49:57 +0000342 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Misha Brukman01808ca2005-04-21 21:13:18 +0000343 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000344 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000345 errs() << " " << NoAliasCount << " no alias responses ";
346 PrintPercent(NoAliasCount, AliasSum);
347 errs() << " " << MayAliasCount << " may alias responses ";
348 PrintPercent(MayAliasCount, AliasSum);
349 errs() << " " << PartialAliasCount << " partial alias responses ";
350 PrintPercent(PartialAliasCount, AliasSum);
351 errs() << " " << MustAliasCount << " must alias responses ";
352 PrintPercent(MustAliasCount, AliasSum);
David Greene0295ecf2009-12-23 22:49:57 +0000353 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000354 << NoAliasCount * 100 / AliasSum << "%/"
355 << MayAliasCount * 100 / AliasSum << "%/"
356 << PartialAliasCount * 100 / AliasSum << "%/"
357 << MustAliasCount * 100 / AliasSum << "%\n";
Chris Lattnereadcadc2003-02-08 23:04:50 +0000358 }
359
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000360 // Display the summary for mod/ref analysis
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000361 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount +
362 MustCount + MustRefCount + MustModCount + MustModRefCount;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000363 if (ModRefSum == 0) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000364 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
365 "mod/ref!\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000366 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000367 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000368 errs() << " " << NoModRefCount << " no mod/ref responses ";
369 PrintPercent(NoModRefCount, ModRefSum);
370 errs() << " " << ModCount << " mod responses ";
371 PrintPercent(ModCount, ModRefSum);
372 errs() << " " << RefCount << " ref responses ";
373 PrintPercent(RefCount, ModRefSum);
374 errs() << " " << ModRefCount << " mod & ref responses ";
375 PrintPercent(ModRefCount, ModRefSum);
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000376 errs() << " " << MustCount << " must responses ";
377 PrintPercent(MustCount, ModRefSum);
378 errs() << " " << MustModCount << " must mod responses ";
379 PrintPercent(MustModCount, ModRefSum);
380 errs() << " " << MustRefCount << " must ref responses ";
381 PrintPercent(MustRefCount, ModRefSum);
382 errs() << " " << MustModRefCount << " must mod & ref responses ";
383 PrintPercent(MustModRefCount, ModRefSum);
David Greene0295ecf2009-12-23 22:49:57 +0000384 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000385 << NoModRefCount * 100 / ModRefSum << "%/"
386 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
Alina Sbirlea50db8a22017-12-21 21:41:53 +0000387 << "%/" << ModRefCount * 100 / ModRefSum << "%/"
388 << MustCount * 100 / ModRefSum << "%/"
389 << MustRefCount * 100 / ModRefSum << "%/"
390 << MustModCount * 100 / ModRefSum << "%/"
391 << MustModRefCount * 100 / ModRefSum << "%\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000392 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000393}
Chandler Carruth4f846a52016-02-20 03:46:03 +0000394
395namespace llvm {
396class AAEvalLegacyPass : public FunctionPass {
397 std::unique_ptr<AAEvaluator> P;
398
399public:
400 static char ID; // Pass identification, replacement for typeid
401 AAEvalLegacyPass() : FunctionPass(ID) {
402 initializeAAEvalLegacyPassPass(*PassRegistry::getPassRegistry());
403 }
404
405 void getAnalysisUsage(AnalysisUsage &AU) const override {
406 AU.addRequired<AAResultsWrapperPass>();
407 AU.setPreservesAll();
408 }
409
410 bool doInitialization(Module &M) override {
411 P.reset(new AAEvaluator());
412 return false;
413 }
414
415 bool runOnFunction(Function &F) override {
416 P->runInternal(F, getAnalysis<AAResultsWrapperPass>().getAAResults());
417 return false;
418 }
419 bool doFinalization(Module &M) override {
420 P.reset();
421 return false;
422 }
423};
424}
425
426char AAEvalLegacyPass::ID = 0;
427INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval",
428 "Exhaustive Alias Analysis Precision Evaluator", false,
429 true)
430INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
431INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval",
432 "Exhaustive Alias Analysis Precision Evaluator", false,
433 true)
434
435FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); }