blob: 12917b650e5eeba290b8c0bec8af3a37df728821 [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 {
Chandler Carruth7b560d42015-09-09 17:55:00 +000062 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner4fdb75f2003-02-06 21:29:49 +000063 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)
Chandler Carruth7b560d42015-09-09 17:55:00 +000086INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +000087INITIALIZE_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();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000145 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
Dan Gohman00ef9322010-07-07 14:27:09 +0000146
147 SetVector<Value *> Pointers;
Rafael Espindola55512f92015-11-18 06:52:18 +0000148 SmallSetVector<CallSite, 16> CallSites;
Manman Ren0827e972013-03-22 22:34:41 +0000149 SetVector<Value *> Loads;
150 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000151
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000152 for (auto &I : F.args())
153 if (I.getType()->isPointerTy()) // Add all pointer arguments.
154 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.
David Majnemer2bc25382015-12-23 09:58:46 +0000170 for (Use &DataOp : CS.data_ops())
171 if (isInterestingPointer(DataOp))
172 Pointers.insert(DataOp);
Gabor Greife497e5e2010-07-28 15:31:37 +0000173 CallSites.insert(CS);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000174 } else {
175 // Consider all operands.
176 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
177 OI != OE; ++OI)
178 if (isInterestingPointer(*OI))
179 Pointers.insert(*OI);
180 }
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000181 }
182
Dan Gohman105d60a2010-12-10 19:52:40 +0000183 if (PrintNoAlias || PrintMayAlias || PrintPartialAlias || PrintMustAlias ||
Dan Gohman00ef9322010-07-07 14:27:09 +0000184 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
185 errs() << "Function: " << F.getName() << ": " << Pointers.size()
186 << " pointers, " << CallSites.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000187
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000188 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000189 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000190 I1 != E; ++I1) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000191 uint64_t I1Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000192 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000193 if (I1ElTy->isSized()) I1Size = DL.getTypeStoreSize(I1ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000194
Dan Gohman0672e922009-08-26 14:32:17 +0000195 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000196 uint64_t I2Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000197 Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000198 if (I2ElTy->isSized()) I2Size = DL.getTypeStoreSize(I2ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000199
Dan Gohman00ef9322010-07-07 14:27:09 +0000200 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000201 case NoAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000202 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000203 ++NoAliasCount;
204 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000205 case MayAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000206 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000207 ++MayAliasCount;
208 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000209 case PartialAlias:
Dan Gohman105d60a2010-12-10 19:52:40 +0000210 PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2,
211 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000212 ++PartialAliasCount;
213 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000214 case MustAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000215 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000216 ++MustAliasCount;
217 break;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000218 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000219 }
220 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000221
Hal Finkelcc39b672014-07-24 12:16:19 +0000222 if (EvalAAMD) {
Manman Ren0827e972013-03-22 22:34:41 +0000223 // iterate over all pairs of load, store
224 for (SetVector<Value *>::iterator I1 = Loads.begin(), E = Loads.end();
225 I1 != E; ++I1) {
226 for (SetVector<Value *>::iterator I2 = Stores.begin(), E2 = Stores.end();
227 I2 != E2; ++I2) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000228 switch (AA.alias(MemoryLocation::get(cast<LoadInst>(*I1)),
229 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000230 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000231 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
232 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000233 ++NoAliasCount;
234 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000235 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000236 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
237 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000238 ++MayAliasCount;
239 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000240 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000241 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
242 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000243 ++PartialAliasCount;
244 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000245 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000246 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
247 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000248 ++MustAliasCount;
249 break;
Manman Ren0827e972013-03-22 22:34:41 +0000250 }
251 }
252 }
253
254 // iterate over all pairs of store, store
255 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
256 I1 != E; ++I1) {
257 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000258 switch (AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
259 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000260 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000261 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
262 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000263 ++NoAliasCount;
264 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000265 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000266 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
267 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000268 ++MayAliasCount;
269 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000270 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000271 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
272 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000273 ++PartialAliasCount;
274 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000275 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000276 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
277 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000278 ++MustAliasCount;
279 break;
Manman Ren0827e972013-03-22 22:34:41 +0000280 }
281 }
282 }
283 }
284
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000285 // Mod/ref alias analysis: compare all pairs of calls and values
Rafael Espindola55512f92015-11-18 06:52:18 +0000286 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
Chris Lattner68ee8f52005-03-26 22:16:44 +0000287 Instruction *I = C->getInstruction();
Misha Brukman01808ca2005-04-21 21:13:18 +0000288
Dan Gohman0672e922009-08-26 14:32:17 +0000289 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Chris Lattner68ee8f52005-03-26 22:16:44 +0000290 V != Ve; ++V) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000291 uint64_t Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000292 Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Chandler Carruth50fee932015-08-06 02:05:46 +0000293 if (ElTy->isSized()) Size = DL.getTypeStoreSize(ElTy);
Misha Brukman01808ca2005-04-21 21:13:18 +0000294
Dan Gohman00ef9322010-07-07 14:27:09 +0000295 switch (AA.getModRefInfo(*C, *V, Size)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000296 case MRI_NoModRef:
Dan Gohman00ef9322010-07-07 14:27:09 +0000297 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000298 ++NoModRefCount;
299 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000300 case MRI_Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000301 PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000302 ++ModCount;
303 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000304 case MRI_Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000305 PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000306 ++RefCount;
307 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000308 case MRI_ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000309 PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000310 ++ModRefCount;
311 break;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000312 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000313 }
Chris Lattner3bbaaaa2004-07-17 07:40:34 +0000314 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000315
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000316 // Mod/ref alias analysis: compare all pairs of calls
Rafael Espindola55512f92015-11-18 06:52:18 +0000317 for (auto C = CallSites.begin(), Ce = CallSites.end(); C != Ce; ++C) {
318 for (auto D = CallSites.begin(); D != Ce; ++D) {
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000319 if (D == C)
320 continue;
321 switch (AA.getModRefInfo(*C, *D)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000322 case MRI_NoModRef:
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000323 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000324 ++NoModRefCount;
325 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000326 case MRI_Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000327 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000328 ++ModCount;
329 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000330 case MRI_Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000331 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000332 ++RefCount;
333 break;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000334 case MRI_ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000335 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000336 ++ModRefCount;
337 break;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000338 }
339 }
340 }
341
Dan Gohman00ef9322010-07-07 14:27:09 +0000342 return false;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000343}
344
Chris Lattner3f08e782005-03-26 23:56:33 +0000345static void PrintPercent(unsigned Num, unsigned Sum) {
David Greene0295ecf2009-12-23 22:49:57 +0000346 errs() << "(" << Num*100ULL/Sum << "."
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000347 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Chris Lattner3f08e782005-03-26 23:56:33 +0000348}
349
Dan Gohman00ef9322010-07-07 14:27:09 +0000350bool AAEval::doFinalization(Module &M) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000351 unsigned AliasSum =
352 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
David Greene0295ecf2009-12-23 22:49:57 +0000353 errs() << "===== Alias Analysis Evaluator Report =====\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000354 if (AliasSum == 0) {
David Greene0295ecf2009-12-23 22:49:57 +0000355 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Misha Brukman01808ca2005-04-21 21:13:18 +0000356 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000357 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000358 errs() << " " << NoAliasCount << " no alias responses ";
359 PrintPercent(NoAliasCount, AliasSum);
360 errs() << " " << MayAliasCount << " may alias responses ";
361 PrintPercent(MayAliasCount, AliasSum);
362 errs() << " " << PartialAliasCount << " partial alias responses ";
363 PrintPercent(PartialAliasCount, AliasSum);
364 errs() << " " << MustAliasCount << " must alias responses ";
365 PrintPercent(MustAliasCount, AliasSum);
David Greene0295ecf2009-12-23 22:49:57 +0000366 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000367 << NoAliasCount * 100 / AliasSum << "%/"
368 << MayAliasCount * 100 / AliasSum << "%/"
369 << PartialAliasCount * 100 / AliasSum << "%/"
370 << MustAliasCount * 100 / AliasSum << "%\n";
Chris Lattnereadcadc2003-02-08 23:04:50 +0000371 }
372
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000373 // Display the summary for mod/ref analysis
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000374 unsigned ModRefSum = NoModRefCount + ModCount + RefCount + ModRefCount;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000375 if (ModRefSum == 0) {
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000376 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
377 "mod/ref!\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000378 } else {
David Greene0295ecf2009-12-23 22:49:57 +0000379 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000380 errs() << " " << NoModRefCount << " no mod/ref responses ";
381 PrintPercent(NoModRefCount, ModRefSum);
382 errs() << " " << ModCount << " mod responses ";
383 PrintPercent(ModCount, ModRefSum);
384 errs() << " " << RefCount << " ref responses ";
385 PrintPercent(RefCount, ModRefSum);
386 errs() << " " << ModRefCount << " mod & ref responses ";
387 PrintPercent(ModRefCount, ModRefSum);
David Greene0295ecf2009-12-23 22:49:57 +0000388 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000389 << NoModRefCount * 100 / ModRefSum << "%/"
390 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
391 << "%/" << ModRefCount * 100 / ModRefSum << "%\n";
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000392 }
Dan Gohman00ef9322010-07-07 14:27:09 +0000393
394 return false;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000395}