blob: 1bb1d0d6589fdded9ad5c9e0465fa4cc239f9fd2 [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
Owen Anderson75693222010-08-06 18:33:48 +000053 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 Andersoncdb0c032010-10-12 19:48:12 +000077INITIALIZE_PASS_BEGIN(AAEval, "aa-eval",
78 "Exhaustive Alias Analysis Precision Evaluator", false, true)
79INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
80INITIALIZE_PASS_END(AAEval, "aa-eval",
Owen Anderson1434dfa2010-10-07 22:25:06 +000081 "Exhaustive Alias Analysis Precision Evaluator", false, true)
Dan Gohman089efff2008-05-13 00:00:25 +000082
Dan Gohman83a01ad22010-07-07 14:27:09 +000083FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
Chris Lattnera7a9daa2009-08-23 05:17:37 +000085static void PrintResults(const char *Msg, bool P, const Value *V1,
86 const Value *V2, const Module *M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 if (P) {
Chris Lattnera7a9daa2009-08-23 05:17:37 +000088 std::string o1, o2;
89 {
90 raw_string_ostream os1(o1), os2(o2);
91 WriteAsOperand(os1, V1, true, M);
92 WriteAsOperand(os2, V2, true, M);
93 }
94
Gabor Greifcc322ca2008-02-28 08:38:45 +000095 if (o2 < o1)
Daniel Dunbar005975c2009-07-25 00:23:56 +000096 std::swap(o1, o2);
David Greene092316b2009-12-23 22:49:57 +000097 errs() << " " << Msg << ":\t"
Daniel Dunbar005975c2009-07-25 00:23:56 +000098 << o1 << ", "
99 << o2 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 }
101}
102
103static inline void
104PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
105 Module *M) {
106 if (P) {
David Greene092316b2009-12-23 22:49:57 +0000107 errs() << " " << Msg << ": Ptr: ";
108 WriteAsOperand(errs(), Ptr, true, M);
109 errs() << "\t<->" << *I << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 }
111}
112
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000113static inline void
114PrintModRefResults(const char *Msg, bool P, CallSite CSA, CallSite CSB,
115 Module *M) {
116 if (P) {
117 errs() << " " << Msg << ": " << *CSA.getInstruction()
118 << " <-> " << *CSB.getInstruction() << '\n';
119 }
120}
121
Gabor Greif5dac0f62010-04-08 16:46:24 +0000122static inline bool isInterestingPointer(Value *V) {
123 return V->getType()->isPointerTy()
124 && !isa<ConstantPointerNull>(V);
125}
126
Dan Gohman83a01ad22010-07-07 14:27:09 +0000127bool AAEval::runOnFunction(Function &F) {
128 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
129
130 SetVector<Value *> Pointers;
131 SetVector<CallSite> CallSites;
132
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Gabor Greif5dac0f62010-04-08 16:46:24 +0000134 if (I->getType()->isPointerTy()) // Add all pointer arguments.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 Pointers.insert(I);
136
137 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Gabor Greif5dac0f62010-04-08 16:46:24 +0000138 if (I->getType()->isPointerTy()) // Add all pointer instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 Pointers.insert(&*I);
140 Instruction &Inst = *I;
Gabor Greif11b03892010-07-28 15:31:37 +0000141 if (CallSite CS = cast<Value>(&Inst)) {
Gabor Greif5dac0f62010-04-08 16:46:24 +0000142 Value *Callee = CS.getCalledValue();
143 // Skip actual functions for direct function calls.
144 if (!isa<Function>(Callee) && isInterestingPointer(Callee))
145 Pointers.insert(Callee);
146 // Consider formals.
147 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
148 AI != AE; ++AI)
149 if (isInterestingPointer(*AI))
150 Pointers.insert(*AI);
Gabor Greif11b03892010-07-28 15:31:37 +0000151 CallSites.insert(CS);
Gabor Greif5dac0f62010-04-08 16:46:24 +0000152 } else {
153 // Consider all operands.
154 for (Instruction::op_iterator OI = Inst.op_begin(), OE = Inst.op_end();
155 OI != OE; ++OI)
156 if (isInterestingPointer(*OI))
157 Pointers.insert(*OI);
158 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 }
160
Dan Gohman83a01ad22010-07-07 14:27:09 +0000161 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
162 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
163 errs() << "Function: " << F.getName() << ": " << Pointers.size()
164 << " pointers, " << CallSites.size() << " call sites\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
166 // iterate over the worklist, and run the full (n^2)/2 disambiguations
Dan Gohman84c793a2009-08-26 14:32:17 +0000167 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 I1 != E; ++I1) {
Dan Gohman624f9612009-07-25 00:48:42 +0000169 unsigned I1Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
Dan Gohman83a01ad22010-07-07 14:27:09 +0000171 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172
Dan Gohman84c793a2009-08-26 14:32:17 +0000173 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
Dan Gohman624f9612009-07-25 00:48:42 +0000174 unsigned I2Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
Dan Gohman83a01ad22010-07-07 14:27:09 +0000176 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Dan Gohman83a01ad22010-07-07 14:27:09 +0000178 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 case AliasAnalysis::NoAlias:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000180 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 ++NoAlias; break;
182 case AliasAnalysis::MayAlias:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000183 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 ++MayAlias; break;
185 case AliasAnalysis::MustAlias:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000186 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 ++MustAlias; break;
188 default:
David Greene092316b2009-12-23 22:49:57 +0000189 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 }
191 }
192 }
193
194 // Mod/ref alias analysis: compare all pairs of calls and values
Dan Gohman84c793a2009-08-26 14:32:17 +0000195 for (SetVector<CallSite>::iterator C = CallSites.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 Ce = CallSites.end(); C != Ce; ++C) {
197 Instruction *I = C->getInstruction();
198
Dan Gohman84c793a2009-08-26 14:32:17 +0000199 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 V != Ve; ++V) {
Dan Gohman624f9612009-07-25 00:48:42 +0000201 unsigned Size = ~0u;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
Dan Gohman83a01ad22010-07-07 14:27:09 +0000203 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
Dan Gohman83a01ad22010-07-07 14:27:09 +0000205 switch (AA.getModRefInfo(*C, *V, Size)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 case AliasAnalysis::NoModRef:
Dan Gohman83a01ad22010-07-07 14:27:09 +0000207 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 ++NoModRef; break;
209 case AliasAnalysis::Mod:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000210 PrintModRefResults("Just Mod", PrintMod, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 ++Mod; break;
212 case AliasAnalysis::Ref:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000213 PrintModRefResults("Just Ref", PrintRef, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 ++Ref; break;
215 case AliasAnalysis::ModRef:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000216 PrintModRefResults("Both ModRef", PrintModRef, I, *V, F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 ++ModRef; break;
218 default:
David Greene092316b2009-12-23 22:49:57 +0000219 errs() << "Unknown alias query result!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 }
221 }
222 }
223
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000224 // Mod/ref alias analysis: compare all pairs of calls
225 for (SetVector<CallSite>::iterator C = CallSites.begin(),
226 Ce = CallSites.end(); C != Ce; ++C) {
227 for (SetVector<CallSite>::iterator D = CallSites.begin(); D != Ce; ++D) {
228 if (D == C)
229 continue;
230 switch (AA.getModRefInfo(*C, *D)) {
231 case AliasAnalysis::NoModRef:
232 PrintModRefResults("NoModRef", PrintNoModRef, *C, *D, F.getParent());
233 ++NoModRef; break;
234 case AliasAnalysis::Mod:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000235 PrintModRefResults("Just Mod", PrintMod, *C, *D, F.getParent());
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000236 ++Mod; break;
237 case AliasAnalysis::Ref:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000238 PrintModRefResults("Just Ref", PrintRef, *C, *D, F.getParent());
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000239 ++Ref; break;
240 case AliasAnalysis::ModRef:
Dan Gohman3d9867f2010-08-04 23:37:55 +0000241 PrintModRefResults("Both ModRef", PrintModRef, *C, *D, F.getParent());
Dan Gohmanfa1c49b2010-08-04 22:56:29 +0000242 ++ModRef; break;
243 }
244 }
245 }
246
Dan Gohman83a01ad22010-07-07 14:27:09 +0000247 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248}
249
250static void PrintPercent(unsigned Num, unsigned Sum) {
David Greene092316b2009-12-23 22:49:57 +0000251 errs() << "(" << Num*100ULL/Sum << "."
Daniel Dunbar005975c2009-07-25 00:23:56 +0000252 << ((Num*1000ULL/Sum) % 10) << "%)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253}
254
Dan Gohman83a01ad22010-07-07 14:27:09 +0000255bool AAEval::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
David Greene092316b2009-12-23 22:49:57 +0000257 errs() << "===== Alias Analysis Evaluator Report =====\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 if (AliasSum == 0) {
David Greene092316b2009-12-23 22:49:57 +0000259 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 } else {
David Greene092316b2009-12-23 22:49:57 +0000261 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
262 errs() << " " << NoAlias << " no alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 PrintPercent(NoAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000264 errs() << " " << MayAlias << " may alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 PrintPercent(MayAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000266 errs() << " " << MustAlias << " must alias responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 PrintPercent(MustAlias, AliasSum);
David Greene092316b2009-12-23 22:49:57 +0000268 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000269 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
270 << MustAlias*100/AliasSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 }
272
273 // Display the summary for mod/ref analysis
274 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
275 if (ModRefSum == 0) {
David Greene092316b2009-12-23 22:49:57 +0000276 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 } else {
David Greene092316b2009-12-23 22:49:57 +0000278 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
279 errs() << " " << NoModRef << " no mod/ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 PrintPercent(NoModRef, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000281 errs() << " " << Mod << " mod responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 PrintPercent(Mod, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000283 errs() << " " << Ref << " ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 PrintPercent(Ref, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000285 errs() << " " << ModRef << " mod & ref responses ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 PrintPercent(ModRef, ModRefSum);
David Greene092316b2009-12-23 22:49:57 +0000287 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
Daniel Dunbar005975c2009-07-25 00:23:56 +0000288 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
289 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 }
Dan Gohman83a01ad22010-07-07 14:27:09 +0000291
292 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293}