blob: 4540823dbeda2f1ca9b0e1419f6b6ac1feed280c [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//
10// This file implements a simple N^2 alias analysis accuracy evaluator.
11// Basically, for each function in the program, it simply queries to see how the
12// alias analysis implementation answers alias queries between each pair of
13// pointers in the function.
14//
15// This is inspired and adapted from code by: Naveen Neelakantam, Francesco
16// Spadini, and Wojciech Stryjewski.
17//
18//===----------------------------------------------------------------------===//
19
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Analysis/Passes.h"
21#include "llvm/ADT/SetVector.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
Chandler Carruth50fee932015-08-06 02:05:46 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
Chandler Carruth50fee932015-08-06 02:05:46 +000027#include "llvm/IR/Module.h"
Chandler Carruth83948572014-03-04 10:30:26 +000028#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Instructions.h"
Misha Brukmanbf28cf62004-03-12 06:15:08 +000030#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/CommandLine.h"
David Greene22819982009-12-23 19:21:19 +000032#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattner8dee8412003-12-10 15:33:59 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Dan Gohmand78c4002008-05-13 00:00:25 +000036static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
37
38static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
39static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
Dan Gohman105d60a2010-12-10 19:52:40 +000040static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000041static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
42
43static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
44static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
45static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
46static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
47
Hal Finkelcc39b672014-07-24 12:16:19 +000048static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
Manman Ren0827e972013-03-22 22:34:41 +000049
Chris Lattner4fdb75f2003-02-06 21:29:49 +000050namespace {
Dan Gohman00ef9322010-07-07 14:27:09 +000051 class AAEval : public FunctionPass {
Chandler Carruthd1a130c2015-06-17 07:21:41 +000052 unsigned NoAliasCount, MayAliasCount, PartialAliasCount, MustAliasCount;
53 unsigned NoModRefCount, ModCount, RefCount, ModRefCount;
Chris Lattner4fdb75f2003-02-06 21:29:49 +000054
Dan Gohman00ef9322010-07-07 14:27:09 +000055 public:
56 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000057 AAEval() : FunctionPass(ID) {
58 initializeAAEvalPass(*PassRegistry::getPassRegistry());
59 }
Devang Patel09f162c2007-05-01 21:15:47 +000060
Craig Toppere9ba7592014-03-05 07:30:04 +000061 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner4fdb75f2003-02-06 21:29:49 +000062 AU.addRequired<AliasAnalysis>();
63 AU.setPreservesAll();
64 }
Misha Brukman01808ca2005-04-21 21:13:18 +000065
Craig Toppere9ba7592014-03-05 07:30:04 +000066 bool doInitialization(Module &M) override {
Chandler Carruthd1a130c2015-06-17 07:21:41 +000067 NoAliasCount = MayAliasCount = PartialAliasCount = MustAliasCount = 0;
68 NoModRefCount = ModCount = RefCount = ModRefCount = 0;
Chris Lattnereed1a6f2004-07-17 06:28:49 +000069
70 if (PrintAll) {
Dan Gohman105d60a2010-12-10 19:52:40 +000071 PrintNoAlias = PrintMayAlias = true;
72 PrintPartialAlias = PrintMustAlias = true;
Chris Lattnereed1a6f2004-07-17 06:28:49 +000073 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
74 }
Misha Brukman01808ca2005-04-21 21:13:18 +000075 return false;
Misha Brukmanbf28cf62004-03-12 06:15:08 +000076 }
77
Craig Toppere9ba7592014-03-05 07:30:04 +000078 bool runOnFunction(Function &F) override;
79 bool doFinalization(Module &M) override;
Chris Lattner4fdb75f2003-02-06 21:29:49 +000080 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081}
Chris Lattner4fdb75f2003-02-06 21:29:49 +000082
Dan Gohman00ef9322010-07-07 14:27:09 +000083char AAEval::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000084INITIALIZE_PASS_BEGIN(AAEval, "aa-eval",
85 "Exhaustive Alias Analysis Precision Evaluator", false, true)
86INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
87INITIALIZE_PASS_END(AAEval, "aa-eval",
Owen Andersondf7a4f22010-10-07 22:25:06 +000088 "Exhaustive Alias Analysis Precision Evaluator", false, true)
Dan Gohmand78c4002008-05-13 00:00:25 +000089
Dan Gohman00ef9322010-07-07 14:27:09 +000090FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
Jeff Cohencede1ce2005-01-08 22:01:16 +000091
Chris Lattnerb1d782b2009-08-23 05:17:37 +000092static void PrintResults(const char *Msg, bool P, const Value *V1,
93 const Value *V2, const Module *M) {
Chris Lattnerb0208e12003-02-09 20:40:13 +000094 if (P) {
Chris Lattnerb1d782b2009-08-23 05:17:37 +000095 std::string o1, o2;
96 {
97 raw_string_ostream os1(o1), os2(o2);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000098 V1->printAsOperand(os1, true, M);
99 V2->printAsOperand(os2, true, M);
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000100 }
101
Gabor Greiff77e6972008-02-28 08:38:45 +0000102 if (o2 < o1)
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000103 std::swap(o1, o2);
David Greene0295ecf2009-12-23 22:49:57 +0000104 errs() << " " << Msg << ":\t"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000105 << o1 << ", "
106 << o2 << "\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000107 }
108}
109
Misha Brukman01808ca2005-04-21 21:13:18 +0000110static inline void
Chris Lattner2e8690b2004-07-17 06:43:20 +0000111PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
112 Module *M) {
113 if (P) {
David Greene0295ecf2009-12-23 22:49:57 +0000114 errs() << " " << Msg << ": Ptr: ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000115 Ptr->printAsOperand(errs(), true, M);
David Greene0295ecf2009-12-23 22:49:57 +0000116 errs() << "\t<->" << *I << '\n';
Chris Lattner2e8690b2004-07-17 06:43:20 +0000117 }
118}
119
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000120static inline void
121PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
122 Module *M) {
123 if (P) {
124 errs() << " " << Msg << ": " << *CSA.getInstruction()
125 << " <-> " << *CSB.getInstruction() << '\n';
126 }
127}
128
Manman Ren0827e972013-03-22 22:34:41 +0000129static inline void
130PrintLoadStoreResults(const char *Msg, bool P, const Value *V1,
131 const Value *V2, const Module *M) {
132 if (P) {
133 errs() << " " << Msg << ": " << *V1
134 << " <-> " << *V2 << '\n';
135 }
136}
137
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000138static inline bool isInterestingPointer(Value *V) {
139 return V->getType()->isPointerTy()
140 && !isa<ConstantPointerNull>(V);
141}
142
Dan Gohman00ef9322010-07-07 14:27:09 +0000143bool AAEval::runOnFunction(Function &F) {
Chandler Carruth50fee932015-08-06 02:05:46 +0000144 const DataLayout &DL = F.getParent()->getDataLayout();
Dan Gohman00ef9322010-07-07 14:27:09 +0000145 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
146
147 SetVector<Value *> Pointers;
148 SetVector<CallSite> CallSites;
Manman Ren0827e972013-03-22 22:34:41 +0000149 SetVector<Value *> Loads;
150 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000151
Chris Lattner531f9e92005-03-15 04:54:21 +0000152 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000153 if (I->getType()->isPointerTy()) // Add all pointer arguments.
Chris Lattner83e21a02003-06-29 00:07:11 +0000154 Pointers.insert(I);
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000155
Chris Lattner83e21a02003-06-29 00:07:11 +0000156 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000157 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000158 Pointers.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000159 if (EvalAAMD && isa<LoadInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000160 Loads.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000161 if (EvalAAMD && isa<StoreInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000162 Stores.insert(&*I);
Chris Lattner9c9f68c2005-03-17 20:25:04 +0000163 Instruction &Inst = *I;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000164 if (auto CS = CallSite(&Inst)) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000165 Value *Callee = CS.getCalledValue();
166 // Skip actual functions for direct function calls.
167 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
168 Pointers.insert(Callee);
169 // Consider formals.
170 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
171 AI != AE; ++AI)
172 if (isInterestingPointer(*AI))
173 Pointers.insert(*AI);
Gabor Greife497e5e2010-07-28 15:31:37 +0000174 CallSites.insert(CS);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000175 } else {
176 // Consider all operands.
177 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
178 OI != OE; ++OI)
179 if (isInterestingPointer(*OI))
180 Pointers.insert(*OI);
181 }
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000182 }
183
Dan Gohman105d60a2010-12-10 19:52:40 +0000184 if (PrintNoAlias || PrintMayAlias || PrintPartialAlias || PrintMustAlias ||
Dan Gohman00ef9322010-07-07 14:27:09 +0000185 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
186 errs() << "Function: " << F.getName() << ": " << Pointers.size()
187 << " pointers, " << CallSites.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000188
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000189 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000190 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000191 I1 != E; ++I1) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000192 uint64_t I1Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000193 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000194 if (I1ElTy->isSized()) I1Size = DL.getTypeStoreSize(I1ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000195
Dan Gohman0672e922009-08-26 14:32:17 +0000196 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000197 uint64_t I2Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000198 Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000199 if (I2ElTy->isSized()) I2Size = DL.getTypeStoreSize(I2ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000200
Dan Gohman00ef9322010-07-07 14:27:09 +0000201 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000202 case NoAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000203 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000204 ++NoAliasCount;
205 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000206 case MayAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000207 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000208 ++MayAliasCount;
209 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000210 case PartialAlias:
Dan Gohman105d60a2010-12-10 19:52:40 +0000211 PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2,
212 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000213 ++PartialAliasCount;
214 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000215 case MustAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000216 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000217 ++MustAliasCount;
218 break;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000219 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000220 }
221 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000222
Hal Finkelcc39b672014-07-24 12:16:19 +0000223 if (EvalAAMD) {
Manman Ren0827e972013-03-22 22:34:41 +0000224 // iterate over all pairs of load, store
225 for (SetVector<Value *>::iterator I1 = Loads.begin(), E = Loads.end();
226 I1 != E; ++I1) {
227 for (SetVector<Value *>::iterator I2 = Stores.begin(), E2 = Stores.end();
228 I2 != E2; ++I2) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000229 switch (AA.alias(MemoryLocation::get(cast<LoadInst>(*I1)),
230 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000231 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000232 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
233 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000234 ++NoAliasCount;
235 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000236 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000237 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
238 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000239 ++MayAliasCount;
240 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000241 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000242 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
243 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000244 ++PartialAliasCount;
245 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000246 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000247 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
248 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000249 ++MustAliasCount;
250 break;
Manman Ren0827e972013-03-22 22:34:41 +0000251 }
252 }
253 }
254
255 // iterate over all pairs of store, store
256 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
257 I1 != E; ++I1) {
258 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000259 switch (AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
260 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000261 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000262 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
263 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000264 ++NoAliasCount;
265 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000266 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000267 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
268 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000269 ++MayAliasCount;
270 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000271 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000272 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
273 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000274 ++PartialAliasCount;
275 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000276 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000277 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
278 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000279 ++MustAliasCount;
280 break;
Manman Ren0827e972013-03-22 22:34:41 +0000281 }
282 }
283 }
284 }
285
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000286 // Mod/ref alias analysis: compare all pairs of calls and values
Dan Gohman0672e922009-08-26 14:32:17 +0000287 for (SetVector<CallSite>::iterator C = CallSites.begin(),
Chris Lattner68ee8f52005-03-26 22:16:44 +0000288 Ce = CallSites.end(); C != Ce; ++C) {
289 Instruction *I = C->getInstruction();
Misha Brukman01808ca2005-04-21 21:13:18 +0000290
Dan Gohman0672e922009-08-26 14:32:17 +0000291 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Chris Lattner68ee8f52005-03-26 22:16:44 +0000292 V != Ve; ++V) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000293 uint64_t Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000294 Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000295 if (ElTy->isSized()) Size = DL.getTypeStoreSize(ElTy);
Misha Brukman01808ca2005-04-21 21:13:18 +0000296
Dan Gohman00ef9322010-07-07 14:27:09 +0000297 switch (AA.getModRefInfo(*C, *V, Size)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000298 case MRI_NoModRef:
Dan Gohman00ef9322010-07-07 14:27:09 +0000299 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000300 ++NoModRefCount;
301 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000302 case MRI_Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000303 PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000304 ++ModCount;
305 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000306 case MRI_Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000307 PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000308 ++RefCount;
309 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000310 case MRI_ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000311 PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000312 ++ModRefCount;
313 break;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000314 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000315 }
Chris Lattner3bbaaaa2004-07-17 07:40:34 +0000316 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000317
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000318 // Mod/ref alias analysis: compare all pairs of calls
319 for (SetVector<CallSite>::iterator C = CallSites.begin(),
320 Ce = CallSites.end(); C != Ce; ++C) {
321 for (SetVector<CallSite>::iterator D = CallSites.begin(); D != Ce; ++D) {
322 if (D == C)
323 continue;
324 switch (AA.getModRefInfo(*C, *D)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000325 case MRI_NoModRef:
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000326 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000327 ++NoModRefCount;
328 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000329 case MRI_Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000330 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000331 ++ModCount;
332 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000333 case MRI_Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000334 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000335 ++RefCount;
336 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000337 case MRI_ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000338 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000339 ++ModRefCount;
340 break;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000341 }
342 }
343 }
344
Dan Gohman00ef9322010-07-07 14:27:09 +0000345 return false;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000346}
347
Chris Lattner3f08e782005-03-26 23:56:33 +0000348static void PrintPercent(unsigned Num, unsigned Sum) {
David Greene0295ecf2009-12-23 22:49:57 +0000349 errs() << "(" << Num*100ULL/Sum << "."
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000350 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Chris Lattner3f08e782005-03-26 23:56:33 +0000351}
352
Dan Gohman00ef9322010-07-07 14:27:09 +0000353bool AAEval::doFinalization(Module &M) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000354 unsigned AliasSum =
355 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
David Greene0295ecf2009-12-23 22:49:57 +0000356 errs() << "===== Alias Analysis Evaluator Report =====\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000357 if (AliasSum == 0) {
David Greene0295ecf2009-12-23 22:49:57 +0000358 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Misha Brukman01808ca2005-04-21 21:13:18 +0000359 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000360 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000361 errs() << " " << NoAliasCount << " no alias responses ";
362 PrintPercent(NoAliasCount, AliasSum);
363 errs() << " " << MayAliasCount << " may alias responses ";
364 PrintPercent(MayAliasCount, AliasSum);
365 errs() << " " << PartialAliasCount << " partial alias responses ";
366 PrintPercent(PartialAliasCount, AliasSum);
367 errs() << " " << MustAliasCount << " must alias responses ";
368 PrintPercent(MustAliasCount, AliasSum);
David Greene0295ecf2009-12-23 22:49:57 +0000369 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000370 << NoAliasCount * 100 / AliasSum << "%/"
371 << MayAliasCount * 100 / AliasSum << "%/"
372 << PartialAliasCount * 100 / AliasSum << "%/"
373 << MustAliasCount * 100 / AliasSum << "%\n";
Chris Lattnereadcadc2003-02-08 23:04:50 +0000374 }
375
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000376 // Display the summary for mod/ref analysis
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000377 unsigned ModRefSum = NoModRefCount + ModCount + RefCount + ModRefCount;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000378 if (ModRefSum == 0) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000379 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
380 "mod/ref!\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000381 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000382 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000383 errs() << " " << NoModRefCount << " no mod/ref responses ";
384 PrintPercent(NoModRefCount, ModRefSum);
385 errs() << " " << ModCount << " mod responses ";
386 PrintPercent(ModCount, ModRefSum);
387 errs() << " " << RefCount << " ref responses ";
388 PrintPercent(RefCount, ModRefSum);
389 errs() << " " << ModRefCount << " mod & ref responses ";
390 PrintPercent(ModRefCount, ModRefSum);
David Greene0295ecf2009-12-23 22:49:57 +0000391 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000392 << NoModRefCount * 100 / ModRefSum << "%/"
393 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
394 << "%/" << ModRefCount * 100 / ModRefSum << "%\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000395 }
Dan Gohman00ef9322010-07-07 14:27:09 +0000396
397 return false;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000398}