blob: 69dffac16aaa5afbb182103fc354393499ae03aa [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"
24#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000026#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instructions.h"
Misha Brukmanbf28cf62004-03-12 06:15:08 +000028#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/CommandLine.h"
David Greene22819982009-12-23 19:21:19 +000030#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattner8dee8412003-12-10 15:33:59 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Dan Gohmand78c4002008-05-13 00:00:25 +000034static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
35
36static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
37static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
Dan Gohman105d60a2010-12-10 19:52:40 +000038static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden);
Dan Gohmand78c4002008-05-13 00:00:25 +000039static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
40
41static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
42static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
43static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
44static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
45
Hal Finkelcc39b672014-07-24 12:16:19 +000046static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
Manman Ren0827e972013-03-22 22:34:41 +000047
Chris Lattner4fdb75f2003-02-06 21:29:49 +000048namespace {
Dan Gohman00ef9322010-07-07 14:27:09 +000049 class AAEval : public FunctionPass {
Chandler Carruthd1a130c2015-06-17 07:21:41 +000050 unsigned NoAliasCount, MayAliasCount, PartialAliasCount, MustAliasCount;
51 unsigned NoModRefCount, ModCount, RefCount, ModRefCount;
Chris Lattner4fdb75f2003-02-06 21:29:49 +000052
Dan Gohman00ef9322010-07-07 14:27:09 +000053 public:
54 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000055 AAEval() : FunctionPass(ID) {
56 initializeAAEvalPass(*PassRegistry::getPassRegistry());
57 }
Devang Patel09f162c2007-05-01 21:15:47 +000058
Craig Toppere9ba7592014-03-05 07:30:04 +000059 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner4fdb75f2003-02-06 21:29:49 +000060 AU.addRequired<AliasAnalysis>();
61 AU.setPreservesAll();
62 }
Misha Brukman01808ca2005-04-21 21:13:18 +000063
Craig Toppere9ba7592014-03-05 07:30:04 +000064 bool doInitialization(Module &M) override {
Chandler Carruthd1a130c2015-06-17 07:21:41 +000065 NoAliasCount = MayAliasCount = PartialAliasCount = MustAliasCount = 0;
66 NoModRefCount = ModCount = RefCount = ModRefCount = 0;
Chris Lattnereed1a6f2004-07-17 06:28:49 +000067
68 if (PrintAll) {
Dan Gohman105d60a2010-12-10 19:52:40 +000069 PrintNoAlias = PrintMayAlias = true;
70 PrintPartialAlias = PrintMustAlias = true;
Chris Lattnereed1a6f2004-07-17 06:28:49 +000071 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
72 }
Misha Brukman01808ca2005-04-21 21:13:18 +000073 return false;
Misha Brukmanbf28cf62004-03-12 06:15:08 +000074 }
75
Craig Toppere9ba7592014-03-05 07:30:04 +000076 bool runOnFunction(Function &F) override;
77 bool doFinalization(Module &M) override;
Chris Lattner4fdb75f2003-02-06 21:29:49 +000078 };
Alexander Kornienko70bc5f12015-06-19 15:57:42 +000079} // namespace
Chris Lattner4fdb75f2003-02-06 21:29:49 +000080
Dan Gohman00ef9322010-07-07 14:27:09 +000081char AAEval::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000082INITIALIZE_PASS_BEGIN(AAEval, "aa-eval",
83 "Exhaustive Alias Analysis Precision Evaluator", false, true)
84INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
85INITIALIZE_PASS_END(AAEval, "aa-eval",
Owen Andersondf7a4f22010-10-07 22:25:06 +000086 "Exhaustive Alias Analysis Precision Evaluator", false, true)
Dan Gohmand78c4002008-05-13 00:00:25 +000087
Dan Gohman00ef9322010-07-07 14:27:09 +000088FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
Jeff Cohencede1ce2005-01-08 22:01:16 +000089
Chris Lattnerb1d782b2009-08-23 05:17:37 +000090static void PrintResults(const char *Msg, bool P, const Value *V1,
91 const Value *V2, const Module *M) {
Chris Lattnerb0208e12003-02-09 20:40:13 +000092 if (P) {
Chris Lattnerb1d782b2009-08-23 05:17:37 +000093 std::string o1, o2;
94 {
95 raw_string_ostream os1(o1), os2(o2);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000096 V1->printAsOperand(os1, true, M);
97 V2->printAsOperand(os2, true, M);
Chris Lattnerb1d782b2009-08-23 05:17:37 +000098 }
99
Gabor Greiff77e6972008-02-28 08:38:45 +0000100 if (o2 < o1)
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000101 std::swap(o1, o2);
David Greene0295ecf2009-12-23 22:49:57 +0000102 errs() << " " << Msg << ":\t"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000103 << o1 << ", "
104 << o2 << "\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000105 }
106}
107
Misha Brukman01808ca2005-04-21 21:13:18 +0000108static inline void
Chris Lattner2e8690b2004-07-17 06:43:20 +0000109PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
110 Module *M) {
111 if (P) {
David Greene0295ecf2009-12-23 22:49:57 +0000112 errs() << " " << Msg << ": Ptr: ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000113 Ptr->printAsOperand(errs(), true, M);
David Greene0295ecf2009-12-23 22:49:57 +0000114 errs() << "\t<->" << *I << '\n';
Chris Lattner2e8690b2004-07-17 06:43:20 +0000115 }
116}
117
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000118static inline void
119PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
120 Module *M) {
121 if (P) {
122 errs() << " " << Msg << ": " << *CSA.getInstruction()
123 << " <-> " << *CSB.getInstruction() << '\n';
124 }
125}
126
Manman Ren0827e972013-03-22 22:34:41 +0000127static inline void
128PrintLoadStoreResults(const char *Msg, bool P, const Value *V1,
129 const Value *V2, const Module *M) {
130 if (P) {
131 errs() << " " << Msg << ": " << *V1
132 << " <-> " << *V2 << '\n';
133 }
134}
135
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000136static inline bool isInterestingPointer(Value *V) {
137 return V->getType()->isPointerTy()
138 && !isa<ConstantPointerNull>(V);
139}
140
Dan Gohman00ef9322010-07-07 14:27:09 +0000141bool AAEval::runOnFunction(Function &F) {
142 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
143
144 SetVector<Value *> Pointers;
145 SetVector<CallSite> CallSites;
Manman Ren0827e972013-03-22 22:34:41 +0000146 SetVector<Value *> Loads;
147 SetVector<Value *> Stores;
Dan Gohman00ef9322010-07-07 14:27:09 +0000148
Chris Lattner531f9e92005-03-15 04:54:21 +0000149 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000150 if (I->getType()->isPointerTy()) // Add all pointer arguments.
Chris Lattner83e21a02003-06-29 00:07:11 +0000151 Pointers.insert(I);
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000152
Chris Lattner83e21a02003-06-29 00:07:11 +0000153 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000154 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000155 Pointers.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000156 if (EvalAAMD && isa<LoadInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000157 Loads.insert(&*I);
Hal Finkelcc39b672014-07-24 12:16:19 +0000158 if (EvalAAMD && isa<StoreInst>(&*I))
Manman Ren0827e972013-03-22 22:34:41 +0000159 Stores.insert(&*I);
Chris Lattner9c9f68c2005-03-17 20:25:04 +0000160 Instruction &Inst = *I;
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000161 if (auto CS = CallSite(&Inst)) {
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000162 Value *Callee = CS.getCalledValue();
163 // Skip actual functions for direct function calls.
164 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
165 Pointers.insert(Callee);
166 // Consider formals.
167 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
168 AI != AE; ++AI)
169 if (isInterestingPointer(*AI))
170 Pointers.insert(*AI);
Gabor Greife497e5e2010-07-28 15:31:37 +0000171 CallSites.insert(CS);
Gabor Greif64d8d1a2010-04-08 16:46:24 +0000172 } else {
173 // Consider all operands.
174 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
175 OI != OE; ++OI)
176 if (isInterestingPointer(*OI))
177 Pointers.insert(*OI);
178 }
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000179 }
180
Dan Gohman105d60a2010-12-10 19:52:40 +0000181 if (PrintNoAlias || PrintMayAlias || PrintPartialAlias || PrintMustAlias ||
Dan Gohman00ef9322010-07-07 14:27:09 +0000182 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
183 errs() << "Function: " << F.getName() << ": " << Pointers.size()
184 << " pointers, " << CallSites.size() << " call sites\n";
Chris Lattnerb0208e12003-02-09 20:40:13 +0000185
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000186 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman0672e922009-08-26 14:32:17 +0000187 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Chris Lattnerf30656b2004-11-26 21:05:39 +0000188 I1 != E; ++I1) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000189 uint64_t I1Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000190 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Dan Gohman00ef9322010-07-07 14:27:09 +0000191 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000192
Dan Gohman0672e922009-08-26 14:32:17 +0000193 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000194 uint64_t I2Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000195 Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Dan Gohman00ef9322010-07-07 14:27:09 +0000196 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
Chris Lattnerf30656b2004-11-26 21:05:39 +0000197
Dan Gohman00ef9322010-07-07 14:27:09 +0000198 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000199 case NoAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000200 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000201 ++NoAliasCount;
202 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000203 case MayAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000204 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000205 ++MayAliasCount;
206 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000207 case PartialAlias:
Dan Gohman105d60a2010-12-10 19:52:40 +0000208 PrintResults("PartialAlias", PrintPartialAlias, *I1, *I2,
209 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000210 ++PartialAliasCount;
211 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000212 case MustAlias:
Dan Gohman00ef9322010-07-07 14:27:09 +0000213 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000214 ++MustAliasCount;
215 break;
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000216 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000217 }
218 }
Chris Lattner4fdb75f2003-02-06 21:29:49 +0000219
Hal Finkelcc39b672014-07-24 12:16:19 +0000220 if (EvalAAMD) {
Manman Ren0827e972013-03-22 22:34:41 +0000221 // iterate over all pairs of load, store
222 for (SetVector<Value *>::iterator I1 = Loads.begin(), E = Loads.end();
223 I1 != E; ++I1) {
224 for (SetVector<Value *>::iterator I2 = Stores.begin(), E2 = Stores.end();
225 I2 != E2; ++I2) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000226 switch (AA.alias(MemoryLocation::get(cast<LoadInst>(*I1)),
227 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000228 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000229 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
230 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000231 ++NoAliasCount;
232 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000233 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000234 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
235 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000236 ++MayAliasCount;
237 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000238 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000239 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
240 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000241 ++PartialAliasCount;
242 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000243 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000244 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
245 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000246 ++MustAliasCount;
247 break;
Manman Ren0827e972013-03-22 22:34:41 +0000248 }
249 }
250 }
251
252 // iterate over all pairs of store, store
253 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
254 I1 != E; ++I1) {
255 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
Chandler Carruth70c61c12015-06-04 02:03:15 +0000256 switch (AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
257 MemoryLocation::get(cast<StoreInst>(*I2)))) {
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000258 case NoAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000259 PrintLoadStoreResults("NoAlias", PrintNoAlias, *I1, *I2,
260 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000261 ++NoAliasCount;
262 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000263 case MayAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000264 PrintLoadStoreResults("MayAlias", PrintMayAlias, *I1, *I2,
265 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000266 ++MayAliasCount;
267 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000268 case PartialAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000269 PrintLoadStoreResults("PartialAlias", PrintPartialAlias, *I1, *I2,
270 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000271 ++PartialAliasCount;
272 break;
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000273 case MustAlias:
Manman Ren0827e972013-03-22 22:34:41 +0000274 PrintLoadStoreResults("MustAlias", PrintMustAlias, *I1, *I2,
275 F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000276 ++MustAliasCount;
277 break;
Manman Ren0827e972013-03-22 22:34:41 +0000278 }
279 }
280 }
281 }
282
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000283 // Mod/ref alias analysis: compare all pairs of calls and values
Dan Gohman0672e922009-08-26 14:32:17 +0000284 for (SetVector<CallSite>::iterator C = CallSites.begin(),
Chris Lattner68ee8f52005-03-26 22:16:44 +0000285 Ce = CallSites.end(); C != Ce; ++C) {
286 Instruction *I = C->getInstruction();
Misha Brukman01808ca2005-04-21 21:13:18 +0000287
Dan Gohman0672e922009-08-26 14:32:17 +0000288 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Chris Lattner68ee8f52005-03-26 22:16:44 +0000289 V != Ve; ++V) {
Chandler Carruthecbd1682015-06-17 07:21:38 +0000290 uint64_t Size = MemoryLocation::UnknownSize;
Chris Lattner229907c2011-07-18 04:54:35 +0000291 Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Dan Gohman00ef9322010-07-07 14:27:09 +0000292 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
Misha Brukman01808ca2005-04-21 21:13:18 +0000293
Dan Gohman00ef9322010-07-07 14:27:09 +0000294 switch (AA.getModRefInfo(*C, *V, Size)) {
Chris Lattnerf30656b2004-11-26 21:05:39 +0000295 case AliasAnalysis::NoModRef:
Dan Gohman00ef9322010-07-07 14:27:09 +0000296 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000297 ++NoModRefCount;
298 break;
Chris Lattnerf30656b2004-11-26 21:05:39 +0000299 case AliasAnalysis::Mod:
Dan Gohman10956182010-08-04 23:37:55 +0000300 PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000301 ++ModCount;
302 break;
Chris Lattnerf30656b2004-11-26 21:05:39 +0000303 case AliasAnalysis::Ref:
Dan Gohman10956182010-08-04 23:37:55 +0000304 PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000305 ++RefCount;
306 break;
Chris Lattnerf30656b2004-11-26 21:05:39 +0000307 case AliasAnalysis::ModRef:
Dan Gohman10956182010-08-04 23:37:55 +0000308 PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000309 ++ModRefCount;
310 break;
Misha Brukmanbf28cf62004-03-12 06:15:08 +0000311 }
Chris Lattnerf30656b2004-11-26 21:05:39 +0000312 }
Chris Lattner3bbaaaa2004-07-17 07:40:34 +0000313 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000314
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000315 // Mod/ref alias analysis: compare all pairs of calls
316 for (SetVector<CallSite>::iterator C = CallSites.begin(),
317 Ce = CallSites.end(); C != Ce; ++C) {
318 for (SetVector<CallSite>::iterator D = CallSites.begin(); D != Ce; ++D) {
319 if (D == C)
320 continue;
321 switch (AA.getModRefInfo(*C, *D)) {
322 case AliasAnalysis::NoModRef:
323 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
Chandler Carruthd1a130c2015-06-17 07:21:41 +0000324 ++NoModRefCount;
325 break;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000326 case AliasAnalysis::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;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000330 case AliasAnalysis::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;
Dan Gohmanbd33dab2010-08-04 22:56:29 +0000334 case AliasAnalysis::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}