1 //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 // This is inspired and adapted from code by: Naveen Neelakantam, Francesco
16 // Spadini, and Wojciech Stryjewski.
18 //===----------------------------------------------------------------------===//
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"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/InstIterator.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/ADT/SetVector.h"
35 static cl::opt
<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden
);
37 static cl::opt
<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden
);
38 static cl::opt
<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden
);
39 static cl::opt
<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden
);
40 static cl::opt
<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden
);
42 static cl::opt
<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden
);
43 static cl::opt
<bool> PrintMod("print-mod", cl::ReallyHidden
);
44 static cl::opt
<bool> PrintRef("print-ref", cl::ReallyHidden
);
45 static cl::opt
<bool> PrintModRef("print-modref", cl::ReallyHidden
);
48 class AAEval
: public FunctionPass
{
49 unsigned NoAlias
, MayAlias
, PartialAlias
, MustAlias
;
50 unsigned NoModRef
, Mod
, Ref
, ModRef
;
53 static char ID
; // Pass identification, replacement for typeid
54 AAEval() : FunctionPass(ID
) {
55 initializeAAEvalPass(*PassRegistry::getPassRegistry());
58 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
59 AU
.addRequired
<AliasAnalysis
>();
63 bool doInitialization(Module
&M
) {
64 NoAlias
= MayAlias
= PartialAlias
= MustAlias
= 0;
65 NoModRef
= Mod
= Ref
= ModRef
= 0;
68 PrintNoAlias
= PrintMayAlias
= true;
69 PrintPartialAlias
= PrintMustAlias
= true;
70 PrintNoModRef
= PrintMod
= PrintRef
= PrintModRef
= true;
75 bool runOnFunction(Function
&F
);
76 bool doFinalization(Module
&M
);
81 INITIALIZE_PASS_BEGIN(AAEval
, "aa-eval",
82 "Exhaustive Alias Analysis Precision Evaluator", false, true)
83 INITIALIZE_AG_DEPENDENCY(AliasAnalysis
)
84 INITIALIZE_PASS_END(AAEval
, "aa-eval",
85 "Exhaustive Alias Analysis Precision Evaluator", false, true)
87 FunctionPass
*llvm::createAAEvalPass() { return new AAEval(); }
89 static void PrintResults(const char *Msg
, bool P
, const Value
*V1
,
90 const Value
*V2
, const Module
*M
) {
94 raw_string_ostream
os1(o1
), os2(o2
);
95 WriteAsOperand(os1
, V1
, true, M
);
96 WriteAsOperand(os2
, V2
, true, M
);
101 errs() << " " << Msg
<< ":\t"
108 PrintModRefResults(const char *Msg
, bool P
, Instruction
*I
, Value
*Ptr
,
111 errs() << " " << Msg
<< ": Ptr: ";
112 WriteAsOperand(errs(), Ptr
, true, M
);
113 errs() << "\t<->" << *I
<< '\n';
118 PrintModRefResults(const char *Msg
, bool P
, CallSite CSA
, CallSite CSB
,
121 errs() << " " << Msg
<< ": " << *CSA
.getInstruction()
122 << " <-> " << *CSB
.getInstruction() << '\n';
126 static inline bool isInterestingPointer(Value
*V
) {
127 return V
->getType()->isPointerTy()
128 && !isa
<ConstantPointerNull
>(V
);
131 bool AAEval::runOnFunction(Function
&F
) {
132 AliasAnalysis
&AA
= getAnalysis
<AliasAnalysis
>();
134 SetVector
<Value
*> Pointers
;
135 SetVector
<CallSite
> CallSites
;
137 for (Function::arg_iterator I
= F
.arg_begin(), E
= F
.arg_end(); I
!= E
; ++I
)
138 if (I
->getType()->isPointerTy()) // Add all pointer arguments.
141 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
142 if (I
->getType()->isPointerTy()) // Add all pointer instructions.
143 Pointers
.insert(&*I
);
144 Instruction
&Inst
= *I
;
145 if (CallSite CS
= cast
<Value
>(&Inst
)) {
146 Value
*Callee
= CS
.getCalledValue();
147 // Skip actual functions for direct function calls.
148 if (!isa
<Function
>(Callee
) && isInterestingPointer(Callee
))
149 Pointers
.insert(Callee
);
151 for (CallSite::arg_iterator AI
= CS
.arg_begin(), AE
= CS
.arg_end();
153 if (isInterestingPointer(*AI
))
154 Pointers
.insert(*AI
);
155 CallSites
.insert(CS
);
157 // Consider all operands.
158 for (Instruction::op_iterator OI
= Inst
.op_begin(), OE
= Inst
.op_end();
160 if (isInterestingPointer(*OI
))
161 Pointers
.insert(*OI
);
165 if (PrintNoAlias
|| PrintMayAlias
|| PrintPartialAlias
|| PrintMustAlias
||
166 PrintNoModRef
|| PrintMod
|| PrintRef
|| PrintModRef
)
167 errs() << "Function: " << F
.getName() << ": " << Pointers
.size()
168 << " pointers, " << CallSites
.size() << " call sites\n";
170 // iterate over the worklist, and run the full (n^2)/2 disambiguations
171 for (SetVector
<Value
*>::iterator I1
= Pointers
.begin(), E
= Pointers
.end();
173 uint64_t I1Size
= AliasAnalysis::UnknownSize
;
174 const Type
*I1ElTy
= cast
<PointerType
>((*I1
)->getType())->getElementType();
175 if (I1ElTy
->isSized()) I1Size
= AA
.getTypeStoreSize(I1ElTy
);
177 for (SetVector
<Value
*>::iterator I2
= Pointers
.begin(); I2
!= I1
; ++I2
) {
178 uint64_t I2Size
= AliasAnalysis::UnknownSize
;
179 const Type
*I2ElTy
=cast
<PointerType
>((*I2
)->getType())->getElementType();
180 if (I2ElTy
->isSized()) I2Size
= AA
.getTypeStoreSize(I2ElTy
);
182 switch (AA
.alias(*I1
, I1Size
, *I2
, I2Size
)) {
183 case AliasAnalysis::NoAlias
:
184 PrintResults("NoAlias", PrintNoAlias
, *I1
, *I2
, F
.getParent());
186 case AliasAnalysis::MayAlias
:
187 PrintResults("MayAlias", PrintMayAlias
, *I1
, *I2
, F
.getParent());
189 case AliasAnalysis::PartialAlias
:
190 PrintResults("PartialAlias", PrintPartialAlias
, *I1
, *I2
,
192 ++PartialAlias
; break;
193 case AliasAnalysis::MustAlias
:
194 PrintResults("MustAlias", PrintMustAlias
, *I1
, *I2
, F
.getParent());
197 errs() << "Unknown alias query result!\n";
202 // Mod/ref alias analysis: compare all pairs of calls and values
203 for (SetVector
<CallSite
>::iterator C
= CallSites
.begin(),
204 Ce
= CallSites
.end(); C
!= Ce
; ++C
) {
205 Instruction
*I
= C
->getInstruction();
207 for (SetVector
<Value
*>::iterator V
= Pointers
.begin(), Ve
= Pointers
.end();
209 uint64_t Size
= AliasAnalysis::UnknownSize
;
210 const Type
*ElTy
= cast
<PointerType
>((*V
)->getType())->getElementType();
211 if (ElTy
->isSized()) Size
= AA
.getTypeStoreSize(ElTy
);
213 switch (AA
.getModRefInfo(*C
, *V
, Size
)) {
214 case AliasAnalysis::NoModRef
:
215 PrintModRefResults("NoModRef", PrintNoModRef
, I
, *V
, F
.getParent());
217 case AliasAnalysis::Mod
:
218 PrintModRefResults("Just Mod", PrintMod
, I
, *V
, F
.getParent());
220 case AliasAnalysis::Ref
:
221 PrintModRefResults("Just Ref", PrintRef
, I
, *V
, F
.getParent());
223 case AliasAnalysis::ModRef
:
224 PrintModRefResults("Both ModRef", PrintModRef
, I
, *V
, F
.getParent());
227 errs() << "Unknown alias query result!\n";
232 // Mod/ref alias analysis: compare all pairs of calls
233 for (SetVector
<CallSite
>::iterator C
= CallSites
.begin(),
234 Ce
= CallSites
.end(); C
!= Ce
; ++C
) {
235 for (SetVector
<CallSite
>::iterator D
= CallSites
.begin(); D
!= Ce
; ++D
) {
238 switch (AA
.getModRefInfo(*C
, *D
)) {
239 case AliasAnalysis::NoModRef
:
240 PrintModRefResults("NoModRef", PrintNoModRef
, *C
, *D
, F
.getParent());
242 case AliasAnalysis::Mod
:
243 PrintModRefResults("Just Mod", PrintMod
, *C
, *D
, F
.getParent());
245 case AliasAnalysis::Ref
:
246 PrintModRefResults("Just Ref", PrintRef
, *C
, *D
, F
.getParent());
248 case AliasAnalysis::ModRef
:
249 PrintModRefResults("Both ModRef", PrintModRef
, *C
, *D
, F
.getParent());
258 static void PrintPercent(unsigned Num
, unsigned Sum
) {
259 errs() << "(" << Num
*100ULL/Sum
<< "."
260 << ((Num
*1000ULL/Sum
) % 10) << "%)\n";
263 bool AAEval::doFinalization(Module
&M
) {
264 unsigned AliasSum
= NoAlias
+ MayAlias
+ PartialAlias
+ MustAlias
;
265 errs() << "===== Alias Analysis Evaluator Report =====\n";
267 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
269 errs() << " " << AliasSum
<< " Total Alias Queries Performed\n";
270 errs() << " " << NoAlias
<< " no alias responses ";
271 PrintPercent(NoAlias
, AliasSum
);
272 errs() << " " << MayAlias
<< " may alias responses ";
273 PrintPercent(MayAlias
, AliasSum
);
274 errs() << " " << PartialAlias
<< " partial alias responses ";
275 PrintPercent(PartialAlias
, AliasSum
);
276 errs() << " " << MustAlias
<< " must alias responses ";
277 PrintPercent(MustAlias
, AliasSum
);
278 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
279 << NoAlias
*100/AliasSum
<< "%/" << MayAlias
*100/AliasSum
<< "%/"
280 << PartialAlias
*100/AliasSum
<< "%/"
281 << MustAlias
*100/AliasSum
<< "%\n";
284 // Display the summary for mod/ref analysis
285 unsigned ModRefSum
= NoModRef
+ Mod
+ Ref
+ ModRef
;
286 if (ModRefSum
== 0) {
287 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
289 errs() << " " << ModRefSum
<< " Total ModRef Queries Performed\n";
290 errs() << " " << NoModRef
<< " no mod/ref responses ";
291 PrintPercent(NoModRef
, ModRefSum
);
292 errs() << " " << Mod
<< " mod responses ";
293 PrintPercent(Mod
, ModRefSum
);
294 errs() << " " << Ref
<< " ref responses ";
295 PrintPercent(Ref
, ModRefSum
);
296 errs() << " " << ModRef
<< " mod & ref responses ";
297 PrintPercent(ModRef
, ModRefSum
);
298 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
299 << NoModRef
*100/ModRefSum
<< "%/" << Mod
*100/ModRefSum
<< "%/"
300 << Ref
*100/ModRefSum
<< "%/" << ModRef
*100/ModRefSum
<< "%\n";