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/Target/TargetData.h"
29 #include "llvm/Support/InstIterator.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/raw_ostream.h"
37 static cl::opt
<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden
);
39 static cl::opt
<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden
);
40 static cl::opt
<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden
);
41 static cl::opt
<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden
);
43 static cl::opt
<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden
);
44 static cl::opt
<bool> PrintMod("print-mod", cl::ReallyHidden
);
45 static cl::opt
<bool> PrintRef("print-ref", cl::ReallyHidden
);
46 static cl::opt
<bool> PrintModRef("print-modref", cl::ReallyHidden
);
49 class VISIBILITY_HIDDEN AAEval
: public FunctionPass
{
50 unsigned NoAlias
, MayAlias
, MustAlias
;
51 unsigned NoModRef
, Mod
, Ref
, ModRef
;
54 static char ID
; // Pass identification, replacement for typeid
55 AAEval() : FunctionPass(&ID
) {}
57 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
58 AU
.addRequired
<AliasAnalysis
>();
62 bool doInitialization(Module
&M
) {
63 NoAlias
= MayAlias
= MustAlias
= 0;
64 NoModRef
= Mod
= Ref
= ModRef
= 0;
67 PrintNoAlias
= PrintMayAlias
= PrintMustAlias
= true;
68 PrintNoModRef
= PrintMod
= PrintRef
= PrintModRef
= true;
73 bool runOnFunction(Function
&F
);
74 bool doFinalization(Module
&M
);
79 static RegisterPass
<AAEval
>
80 X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator", false, true);
82 FunctionPass
*llvm::createAAEvalPass() { return new AAEval(); }
84 static void PrintResults(const char *Msg
, bool P
, const Value
*V1
, const Value
*V2
,
87 std::stringstream s1
, s2
;
88 WriteAsOperand(s1
, V1
, true, M
);
89 WriteAsOperand(s2
, V2
, true, M
);
90 std::string
o1(s1
.str()), o2(s2
.str());
93 errs() << " " << Msg
<< ":\t"
100 PrintModRefResults(const char *Msg
, bool P
, Instruction
*I
, Value
*Ptr
,
103 errs() << " " << Msg
<< ": Ptr: ";
104 WriteAsOperand(errs(), Ptr
, true, M
);
105 errs() << "\t<->" << *I
;
109 bool AAEval::runOnFunction(Function
&F
) {
110 AliasAnalysis
&AA
= getAnalysis
<AliasAnalysis
>();
112 std::set
<Value
*> Pointers
;
113 std::set
<CallSite
> CallSites
;
115 for (Function::arg_iterator I
= F
.arg_begin(), E
= F
.arg_end(); I
!= E
; ++I
)
116 if (isa
<PointerType
>(I
->getType())) // Add all pointer arguments
119 for (inst_iterator I
= inst_begin(F
), E
= inst_end(F
); I
!= E
; ++I
) {
120 if (isa
<PointerType
>(I
->getType())) // Add all pointer instructions
121 Pointers
.insert(&*I
);
122 Instruction
&Inst
= *I
;
123 User::op_iterator OI
= Inst
.op_begin();
124 CallSite CS
= CallSite::get(&Inst
);
125 if (CS
.getInstruction() &&
126 isa
<Function
>(CS
.getCalledValue()))
127 ++OI
; // Skip actual functions for direct function calls.
128 for (; OI
!= Inst
.op_end(); ++OI
)
129 if (isa
<PointerType
>((*OI
)->getType()) && !isa
<ConstantPointerNull
>(*OI
))
130 Pointers
.insert(*OI
);
132 if (CS
.getInstruction()) CallSites
.insert(CS
);
135 if (PrintNoAlias
|| PrintMayAlias
|| PrintMustAlias
||
136 PrintNoModRef
|| PrintMod
|| PrintRef
|| PrintModRef
)
137 errs() << "Function: " << F
.getName() << ": " << Pointers
.size()
138 << " pointers, " << CallSites
.size() << " call sites\n";
140 // iterate over the worklist, and run the full (n^2)/2 disambiguations
141 for (std::set
<Value
*>::iterator I1
= Pointers
.begin(), E
= Pointers
.end();
143 unsigned I1Size
= ~0u;
144 const Type
*I1ElTy
= cast
<PointerType
>((*I1
)->getType())->getElementType();
145 if (I1ElTy
->isSized()) I1Size
= AA
.getTypeStoreSize(I1ElTy
);
147 for (std::set
<Value
*>::iterator I2
= Pointers
.begin(); I2
!= I1
; ++I2
) {
148 unsigned I2Size
= ~0u;
149 const Type
*I2ElTy
=cast
<PointerType
>((*I2
)->getType())->getElementType();
150 if (I2ElTy
->isSized()) I2Size
= AA
.getTypeStoreSize(I2ElTy
);
152 switch (AA
.alias(*I1
, I1Size
, *I2
, I2Size
)) {
153 case AliasAnalysis::NoAlias
:
154 PrintResults("NoAlias", PrintNoAlias
, *I1
, *I2
, F
.getParent());
156 case AliasAnalysis::MayAlias
:
157 PrintResults("MayAlias", PrintMayAlias
, *I1
, *I2
, F
.getParent());
159 case AliasAnalysis::MustAlias
:
160 PrintResults("MustAlias", PrintMustAlias
, *I1
, *I2
, F
.getParent());
163 errs() << "Unknown alias query result!\n";
168 // Mod/ref alias analysis: compare all pairs of calls and values
169 for (std::set
<CallSite
>::iterator C
= CallSites
.begin(),
170 Ce
= CallSites
.end(); C
!= Ce
; ++C
) {
171 Instruction
*I
= C
->getInstruction();
173 for (std::set
<Value
*>::iterator V
= Pointers
.begin(), Ve
= Pointers
.end();
176 const Type
*ElTy
= cast
<PointerType
>((*V
)->getType())->getElementType();
177 if (ElTy
->isSized()) Size
= AA
.getTypeStoreSize(ElTy
);
179 switch (AA
.getModRefInfo(*C
, *V
, Size
)) {
180 case AliasAnalysis::NoModRef
:
181 PrintModRefResults("NoModRef", PrintNoModRef
, I
, *V
, F
.getParent());
183 case AliasAnalysis::Mod
:
184 PrintModRefResults(" Mod", PrintMod
, I
, *V
, F
.getParent());
186 case AliasAnalysis::Ref
:
187 PrintModRefResults(" Ref", PrintRef
, I
, *V
, F
.getParent());
189 case AliasAnalysis::ModRef
:
190 PrintModRefResults(" ModRef", PrintModRef
, I
, *V
, F
.getParent());
193 errs() << "Unknown alias query result!\n";
201 static void PrintPercent(unsigned Num
, unsigned Sum
) {
202 errs() << "(" << Num
*100ULL/Sum
<< "."
203 << ((Num
*1000ULL/Sum
) % 10) << "%)\n";
206 bool AAEval::doFinalization(Module
&M
) {
207 unsigned AliasSum
= NoAlias
+ MayAlias
+ MustAlias
;
208 errs() << "===== Alias Analysis Evaluator Report =====\n";
210 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
212 errs() << " " << AliasSum
<< " Total Alias Queries Performed\n";
213 errs() << " " << NoAlias
<< " no alias responses ";
214 PrintPercent(NoAlias
, AliasSum
);
215 errs() << " " << MayAlias
<< " may alias responses ";
216 PrintPercent(MayAlias
, AliasSum
);
217 errs() << " " << MustAlias
<< " must alias responses ";
218 PrintPercent(MustAlias
, AliasSum
);
219 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
220 << NoAlias
*100/AliasSum
<< "%/" << MayAlias
*100/AliasSum
<< "%/"
221 << MustAlias
*100/AliasSum
<< "%\n";
224 // Display the summary for mod/ref analysis
225 unsigned ModRefSum
= NoModRef
+ Mod
+ Ref
+ ModRef
;
226 if (ModRefSum
== 0) {
227 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
229 errs() << " " << ModRefSum
<< " Total ModRef Queries Performed\n";
230 errs() << " " << NoModRef
<< " no mod/ref responses ";
231 PrintPercent(NoModRef
, ModRefSum
);
232 errs() << " " << Mod
<< " mod responses ";
233 PrintPercent(Mod
, ModRefSum
);
234 errs() << " " << Ref
<< " ref responses ";
235 PrintPercent(Ref
, ModRefSum
);
236 errs() << " " << ModRef
<< " mod & ref responses ";
237 PrintPercent(ModRef
, ModRefSum
);
238 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
239 << NoModRef
*100/ModRefSum
<< "%/" << Mod
*100/ModRefSum
<< "%/"
240 << Ref
*100/ModRefSum
<< "%/" << ModRef
*100/ModRefSum
<< "%\n";