this is failing on linux hosts, force a triple.
[llvm/avr.git] / lib / Analysis / AliasAnalysisEvaluator.cpp
blobbb95c01e2ea9ec7808f0f56add07b991a402cfb2
1 //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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.
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"
33 #include "llvm/ADT/SetVector.h"
34 using namespace llvm;
36 static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
38 static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
39 static cl::opt<bool> PrintMayAlias("print-may-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);
47 namespace {
48 class VISIBILITY_HIDDEN AAEval : public FunctionPass {
49 unsigned NoAlias, MayAlias, MustAlias;
50 unsigned NoModRef, Mod, Ref, ModRef;
52 public:
53 static char ID; // Pass identification, replacement for typeid
54 AAEval() : FunctionPass(&ID) {}
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.addRequired<AliasAnalysis>();
58 AU.setPreservesAll();
61 bool doInitialization(Module &M) {
62 NoAlias = MayAlias = MustAlias = 0;
63 NoModRef = Mod = Ref = ModRef = 0;
65 if (PrintAll) {
66 PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
67 PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
69 return false;
72 bool runOnFunction(Function &F);
73 bool doFinalization(Module &M);
77 char AAEval::ID = 0;
78 static RegisterPass<AAEval>
79 X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator", false, true);
81 FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
83 static void PrintResults(const char *Msg, bool P, const Value *V1,
84 const Value *V2, const Module *M) {
85 if (P) {
86 std::string o1, o2;
88 raw_string_ostream os1(o1), os2(o2);
89 WriteAsOperand(os1, V1, true, M);
90 WriteAsOperand(os2, V2, true, M);
93 if (o2 < o1)
94 std::swap(o1, o2);
95 errs() << " " << Msg << ":\t"
96 << o1 << ", "
97 << o2 << "\n";
101 static inline void
102 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
103 Module *M) {
104 if (P) {
105 errs() << " " << Msg << ": Ptr: ";
106 WriteAsOperand(errs(), Ptr, true, M);
107 errs() << "\t<->" << *I << '\n';
111 bool AAEval::runOnFunction(Function &F) {
112 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
114 SetVector<Value *> Pointers;
115 SetVector<CallSite> CallSites;
117 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
118 if (isa<PointerType>(I->getType())) // Add all pointer arguments
119 Pointers.insert(I);
121 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
122 if (isa<PointerType>(I->getType())) // Add all pointer instructions
123 Pointers.insert(&*I);
124 Instruction &Inst = *I;
125 User::op_iterator OI = Inst.op_begin();
126 CallSite CS = CallSite::get(&Inst);
127 if (CS.getInstruction() &&
128 isa<Function>(CS.getCalledValue()))
129 ++OI; // Skip actual functions for direct function calls.
130 for (; OI != Inst.op_end(); ++OI)
131 if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI))
132 Pointers.insert(*OI);
134 if (CS.getInstruction()) CallSites.insert(CS);
137 if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
138 PrintNoModRef || PrintMod || PrintRef || PrintModRef)
139 errs() << "Function: " << F.getName() << ": " << Pointers.size()
140 << " pointers, " << CallSites.size() << " call sites\n";
142 // iterate over the worklist, and run the full (n^2)/2 disambiguations
143 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
144 I1 != E; ++I1) {
145 unsigned I1Size = ~0u;
146 const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
147 if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
149 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
150 unsigned I2Size = ~0u;
151 const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
152 if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
154 switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
155 case AliasAnalysis::NoAlias:
156 PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
157 ++NoAlias; break;
158 case AliasAnalysis::MayAlias:
159 PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
160 ++MayAlias; break;
161 case AliasAnalysis::MustAlias:
162 PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
163 ++MustAlias; break;
164 default:
165 errs() << "Unknown alias query result!\n";
170 // Mod/ref alias analysis: compare all pairs of calls and values
171 for (SetVector<CallSite>::iterator C = CallSites.begin(),
172 Ce = CallSites.end(); C != Ce; ++C) {
173 Instruction *I = C->getInstruction();
175 for (SetVector<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
176 V != Ve; ++V) {
177 unsigned Size = ~0u;
178 const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
179 if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
181 switch (AA.getModRefInfo(*C, *V, Size)) {
182 case AliasAnalysis::NoModRef:
183 PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
184 ++NoModRef; break;
185 case AliasAnalysis::Mod:
186 PrintModRefResults(" Mod", PrintMod, I, *V, F.getParent());
187 ++Mod; break;
188 case AliasAnalysis::Ref:
189 PrintModRefResults(" Ref", PrintRef, I, *V, F.getParent());
190 ++Ref; break;
191 case AliasAnalysis::ModRef:
192 PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent());
193 ++ModRef; break;
194 default:
195 errs() << "Unknown alias query result!\n";
200 return false;
203 static void PrintPercent(unsigned Num, unsigned Sum) {
204 errs() << "(" << Num*100ULL/Sum << "."
205 << ((Num*1000ULL/Sum) % 10) << "%)\n";
208 bool AAEval::doFinalization(Module &M) {
209 unsigned AliasSum = NoAlias + MayAlias + MustAlias;
210 errs() << "===== Alias Analysis Evaluator Report =====\n";
211 if (AliasSum == 0) {
212 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
213 } else {
214 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
215 errs() << " " << NoAlias << " no alias responses ";
216 PrintPercent(NoAlias, AliasSum);
217 errs() << " " << MayAlias << " may alias responses ";
218 PrintPercent(MayAlias, AliasSum);
219 errs() << " " << MustAlias << " must alias responses ";
220 PrintPercent(MustAlias, AliasSum);
221 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
222 << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
223 << MustAlias*100/AliasSum << "%\n";
226 // Display the summary for mod/ref analysis
227 unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
228 if (ModRefSum == 0) {
229 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
230 } else {
231 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
232 errs() << " " << NoModRef << " no mod/ref responses ";
233 PrintPercent(NoModRef, ModRefSum);
234 errs() << " " << Mod << " mod responses ";
235 PrintPercent(Mod, ModRefSum);
236 errs() << " " << Ref << " ref responses ";
237 PrintPercent(Ref, ModRefSum);
238 errs() << " " << ModRef << " mod & ref responses ";
239 PrintPercent(ModRef, ModRefSum);
240 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
241 << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
242 << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
245 return false;