blob: 76a452e2e7daf09c5ff1b605d623533d28235a41 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
23#include "llvm/Instructions.h"
24#include "llvm/Pass.h"
25#include "llvm/Analysis/Passes.h"
26#include "llvm/Analysis/AliasAnalysis.h"
27#include "llvm/Assembly/Writer.h"
David Greene879027a2009-12-23 19:21:19 +000028#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/InstIterator.h"
30#include "llvm/Support/CommandLine.h"
Daniel Dunbar005975c2009-07-25 00:23:56 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohman84c793a2009-08-26 14:32:17 +000032#include "llvm/ADT/SetVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
Dan Gohman089efff2008-05-13 00:00:25 +000035static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
36
37static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
38static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
39static 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046namespace {
Dan Gohman83a01ad22010-07-07 14:27:09 +000047 class AAEval : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 unsigned NoAlias, MayAlias, MustAlias;
49 unsigned NoModRef, Mod, Ref, ModRef;
50
Dan Gohman83a01ad22010-07-07 14:27:09 +000051 public:
52 static char ID; // Pass identification, replacement for typeid
53 AAEval() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
Dan Gohman83a01ad22010-07-07 14:27:09 +000055 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 AU.addRequired<AliasAnalysis>();
57 AU.setPreservesAll();
58 }
59
Dan Gohman83a01ad22010-07-07 14:27:09 +000060 bool doInitialization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 NoAlias = MayAlias = MustAlias = 0;
62 NoModRef = Mod = Ref = ModRef = 0;
63
64 if (PrintAll) {
65 PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
66 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
67 }
68 return false;
69 }
70
Dan Gohman83a01ad22010-07-07 14:27:09 +000071 bool runOnFunction(Function &F);
72 bool doFinalization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074}
75
Dan Gohman83a01ad22010-07-07 14:27:09 +000076char AAEval::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +000077INITIALIZE_PASS(AAEval, "aa-eval",
78 "Exhaustive Alias Analysis Precision Evaluator", false, true);
Dan Gohman089efff2008-05-13 00:00:25 +000079
Dan Gohman83a01ad22010-07-07 14:27:09 +000080FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
Chris Lattnera7a9daa2009-08-23 05:17:37 +000082static void PrintResults(const char *Msg, bool P, const Value *V1,
83 const Value *V2, const Module *M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 if (P) {
Chris Lattnera7a9daa2009-08-23 05:17:37 +000085 std::string o1, o2;
86 {
87 raw_string_ostream os1(o1), os2(o2);
88 WriteAsOperand(os1, V1, true, M);
89 WriteAsOperand(os2, V2, true, M);
90 }
91
Gabor Greifcc322ca2008-02-28 08:38:45 +000092 if (o2 < o1)
Daniel Dunbar005975c2009-07-25 00:23:56 +000093 std::swap(o1, o2);
David Greene092316b2009-12-23 22:49:57 +000094 errs() << " " << Msg << ":\t"
Daniel Dunbar005975c2009-07-25 00:23:56 +000095 << o1 << ", "
96 << o2 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 }
98}
99
100static inline void
101PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
102 Module *M) {
103 if (P) {
David Greene092316b2009-12-23 22:49:57 +0000104 errs() << " " << Msg << ": Ptr: ";
105 WriteAsOperand(errs(), Ptr, true, M);
106 errs() << "\t<->" << *I << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 }
108}
109
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000110static inline void
111PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
112 Module *M) {
113 if (P) {
114 errs() << " " << Msg << ": " << *CSA.getInstruction()
115 << " <-> " << *CSB.getInstruction() << '\n';
116 }
117}
118
Gabor Greif5dac0f62010-04-08 16:46:24 +0000119static inline bool isInterestingPointer(Value *V) {
120 return V->getType()->isPointerTy()
121 && !isa<ConstantPointerNull>(V);
122}
123
Dan Gohman83a01ad22010-07-07 14:27:09 +0000124bool AAEval::runOnFunction(Function &F) {
125 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
126
127 SetVector<Value *> Pointers;
128 SetVector<CallSite> CallSites;
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Gabor Greif5dac0f62010-04-08 16:46:24 +0000131 if (I->getType()->isPointerTy()) // Add all pointer arguments.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 Pointers.insert(I);
133
134 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif5dac0f62010-04-08 16:46:24 +0000135 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 Pointers.insert(&*I);
137 Instruction &Inst = *I;
Gabor Greif11b03892010-07-28 15:31:37 +0000138 if (CallSite CS = cast<Value>(&Inst)) {
Gabor Greif5dac0f62010-04-08 16:46:24 +0000139 Value *Callee = CS.getCalledValue();
140 // Skip actual functions for direct function calls.
141 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
142 Pointers.insert(Callee);
143 // Consider formals.
144 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
145 AI != AE; ++AI)
146 if (isInterestingPointer(*AI))
147 Pointers.insert(*AI);
Gabor Greif11b03892010-07-28 15:31:37 +0000148 CallSites.insert(CS);
Gabor Greif5dac0f62010-04-08 16:46:24 +0000149 } else {
150 // Consider all operands.
151 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
152 OI != OE; ++OI)
153 if (isInterestingPointer(*OI))
154 Pointers.insert(*OI);
155 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 }
157
Dan Gohman83a01ad22010-07-07 14:27:09 +0000158 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
159 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
160 errs() << "Function: " << F.getName() << ": " << Pointers.size()
161 << " pointers, " << CallSites.size() << " call sites\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
163 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman84c793a2009-08-26 14:32:17 +0000164 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 I1 != E; ++I1) {
Dan Gohman624f9612009-07-25 00:48:42 +0000166 unsigned I1Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Dan Gohman83a01ad22010-07-07 14:27:09 +0000168 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
Dan Gohman84c793a2009-08-26 14:32:17 +0000170 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Dan Gohman624f9612009-07-25 00:48:42 +0000171 unsigned I2Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Dan Gohman83a01ad22010-07-07 14:27:09 +0000173 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174
Dan Gohman83a01ad22010-07-07 14:27:09 +0000175 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 case AliasAnalysis::NoAlias:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000177 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 ++NoAlias; break;
179 case AliasAnalysis::MayAlias:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000180 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 ++MayAlias; break;
182 case AliasAnalysis::MustAlias:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000183 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 ++MustAlias; break;
185 default:
David Greene092316b2009-12-23 22:49:57 +0000186 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 }
188 }
189 }
190
191 // Mod/ref alias analysis: compare all pairs of calls and values
Dan Gohman84c793a2009-08-26 14:32:17 +0000192 for (SetVector<CallSite>::iterator C = CallSites.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 Ce = CallSites.end(); C != Ce; ++C) {
194 Instruction *I = C->getInstruction();
195
Dan Gohman84c793a2009-08-26 14:32:17 +0000196 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 V != Ve; ++V) {
Dan Gohman624f9612009-07-25 00:48:42 +0000198 unsigned Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Dan Gohman83a01ad22010-07-07 14:27:09 +0000200 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
Dan Gohman83a01ad22010-07-07 14:27:09 +0000202 switch (AA.getModRefInfo(*C, *V, Size)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 case AliasAnalysis::NoModRef:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000204 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 ++NoModRef; break;
206 case AliasAnalysis::Mod:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000207 PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 ++Mod; break;
209 case AliasAnalysis::Ref:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000210 PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 ++Ref; break;
212 case AliasAnalysis::ModRef:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000213 PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 ++ModRef; break;
215 default:
David Greene092316b2009-12-23 22:49:57 +0000216 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
218 }
219 }
220
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000221 // Mod/ref alias analysis: compare all pairs of calls
222 for (SetVector<CallSite>::iterator C = CallSites.begin(),
223 Ce = CallSites.end(); C != Ce; ++C) {
224 for (SetVector<CallSite>::iterator D = CallSites.begin(); D != Ce; ++D) {
225 if (D == C)
226 continue;
227 switch (AA.getModRefInfo(*C, *D)) {
228 case AliasAnalysis::NoModRef:
229 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
230 ++NoModRef; break;
231 case AliasAnalysis::Mod:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000232 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000233 ++Mod; break;
234 case AliasAnalysis::Ref:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000235 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000236 ++Ref; break;
237 case AliasAnalysis::ModRef:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000238 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000239 ++ModRef; break;
240 }
241 }
242 }
243
Dan Gohman83a01ad22010-07-07 14:27:09 +0000244 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245}
246
247static void PrintPercent(unsigned Num, unsigned Sum) {
David Greene092316b2009-12-23 22:49:57 +0000248 errs() << "(" << Num*100ULL/Sum << "."
Daniel Dunbar005975c2009-07-25 00:23:56 +0000249 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250}
251
Dan Gohman83a01ad22010-07-07 14:27:09 +0000252bool AAEval::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
David Greene092316b2009-12-23 22:49:57 +0000254 errs() << "===== Alias Analysis Evaluator Report =====\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (AliasSum == 0) {
David Greene092316b2009-12-23 22:49:57 +0000256 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 } else {
David Greene092316b2009-12-23 22:49:57 +0000258 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
259 errs() << " " << NoAlias << " no alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 PrintPercent(NoAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000261 errs() << " " << MayAlias << " may alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 PrintPercent(MayAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000263 errs() << " " << MustAlias << " must alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 PrintPercent(MustAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000265 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000266 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
267 << MustAlias*100/AliasSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 }
269
270 // Display the summary for mod/ref analysis
271 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
272 if (ModRefSum == 0) {
David Greene092316b2009-12-23 22:49:57 +0000273 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 } else {
David Greene092316b2009-12-23 22:49:57 +0000275 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
276 errs() << " " << NoModRef << " no mod/ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 PrintPercent(NoModRef, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000278 errs() << " " << Mod << " mod responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 PrintPercent(Mod, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000280 errs() << " " << Ref << " ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 PrintPercent(Ref, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000282 errs() << " " << ModRef << " mod & ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 PrintPercent(ModRef, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000284 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000285 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
286 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 }
Dan Gohman83a01ad22010-07-07 14:27:09 +0000288
289 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290}