2003-02-06 21:29:49 +00:00
|
|
|
//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-02-06 21:29:49 +00:00
|
|
|
//
|
|
|
|
// This file implements a simple N^2 alias analysis accuracy evaluator.
|
|
|
|
// Basically, for each function in the program, it simply queries to see how the
|
|
|
|
// alias analysis implementation answers alias queries between each pair of
|
|
|
|
// pointers in the function.
|
|
|
|
//
|
|
|
|
// This is inspired and adapted from code by: Naveen Neelakantam, Francesco
|
|
|
|
// Spadini, and Wojciech Stryjewski.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Function.h"
|
2004-03-12 06:15:08 +00:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/iTerminators.h"
|
|
|
|
#include "llvm/Pass.h"
|
2003-02-06 21:29:49 +00:00
|
|
|
#include "llvm/Type.h"
|
2004-03-12 06:15:08 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2003-02-09 20:40:13 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2004-03-12 06:15:08 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2003-02-09 20:40:13 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2004-07-04 12:19:56 +00:00
|
|
|
#include <iostream>
|
2003-06-29 00:07:11 +00:00
|
|
|
#include <set>
|
2004-07-04 12:19:56 +00:00
|
|
|
|
2003-12-10 15:33:59 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
namespace {
|
2004-03-12 06:15:08 +00:00
|
|
|
cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
|
|
|
|
|
|
|
|
cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
|
2003-02-09 20:40:13 +00:00
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
class AAEval : public FunctionPass {
|
2004-03-12 06:15:08 +00:00
|
|
|
unsigned NoAlias, MayAlias, MustAlias;
|
|
|
|
unsigned NoModRef, Mod, Ref, ModRef;
|
2003-02-06 21:29:49 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2004-03-12 06:15:08 +00:00
|
|
|
bool doInitialization(Module &M) {
|
|
|
|
NoAlias = MayAlias = MustAlias = 0;
|
|
|
|
NoModRef = Mod = Ref = ModRef = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
bool doFinalization(Module &M);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<AAEval>
|
|
|
|
X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
|
|
|
|
}
|
|
|
|
|
2003-12-10 15:33:59 +00:00
|
|
|
static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
|
|
|
|
Module *M) {
|
2003-02-09 20:40:13 +00:00
|
|
|
if (P) {
|
|
|
|
std::cerr << " " << Msg << ":\t";
|
2003-12-10 15:33:59 +00:00
|
|
|
WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
|
|
|
|
WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
|
2003-02-09 20:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
bool AAEval::runOnFunction(Function &F) {
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
|
2003-06-29 00:07:11 +00:00
|
|
|
std::set<Value *> Pointers;
|
2004-03-12 06:15:08 +00:00
|
|
|
std::set<CallSite> CallSites;
|
2003-02-06 21:29:49 +00:00
|
|
|
|
|
|
|
for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
|
|
|
|
if (isa<PointerType>(I->getType())) // Add all pointer arguments
|
2003-06-29 00:07:11 +00:00
|
|
|
Pointers.insert(I);
|
2003-02-06 21:29:49 +00:00
|
|
|
|
2003-06-29 00:07:11 +00:00
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
2004-04-27 15:13:33 +00:00
|
|
|
if (isa<PointerType>(I->getType())) // Add all pointer instructions
|
|
|
|
Pointers.insert(&*I);
|
|
|
|
for (User::op_iterator OI = (*I).op_begin(); OI != (*I).op_end(); ++OI)
|
2003-06-29 00:07:11 +00:00
|
|
|
if (isa<PointerType>((*OI)->getType()))
|
|
|
|
Pointers.insert(*OI);
|
|
|
|
}
|
2003-02-06 21:29:49 +00:00
|
|
|
|
2004-03-12 06:15:08 +00:00
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
2004-04-27 15:13:33 +00:00
|
|
|
CallSite CS = CallSite::get(&*I);
|
2004-03-12 16:20:49 +00:00
|
|
|
if (CS.getInstruction()) CallSites.insert(CS);
|
2004-03-12 06:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
|
|
|
|
PrintNoModRef || PrintMod || PrintRef || PrintModRef)
|
2003-02-09 20:40:13 +00:00
|
|
|
std::cerr << "Function: " << F.getName() << "\n";
|
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
// iterate over the worklist, and run the full (n^2)/2 disambiguations
|
2003-06-29 00:07:11 +00:00
|
|
|
for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
|
2003-02-06 21:29:49 +00:00
|
|
|
I1 != E; ++I1)
|
2003-06-29 00:07:11 +00:00
|
|
|
for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2)
|
2003-02-26 19:29:16 +00:00
|
|
|
switch (AA.alias(*I1, 0, *I2, 0)) {
|
2003-02-09 20:40:13 +00:00
|
|
|
case AliasAnalysis::NoAlias:
|
2004-03-12 06:15:08 +00:00
|
|
|
PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
|
|
|
|
++NoAlias; break;
|
2003-02-09 20:40:13 +00:00
|
|
|
case AliasAnalysis::MayAlias:
|
2004-03-12 06:15:08 +00:00
|
|
|
PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
|
|
|
|
++MayAlias; break;
|
2003-02-09 20:40:13 +00:00
|
|
|
case AliasAnalysis::MustAlias:
|
2004-03-12 06:15:08 +00:00
|
|
|
PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
|
|
|
|
++MustAlias; break;
|
2003-02-06 21:29:49 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "Unknown alias query result!\n";
|
|
|
|
}
|
|
|
|
|
2004-03-12 06:15:08 +00:00
|
|
|
// Mod/ref alias analysis: compare all pairs of calls and values
|
|
|
|
for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
|
|
|
|
V != Ve; ++V)
|
|
|
|
for (std::set<CallSite>::iterator C = CallSites.begin(),
|
|
|
|
Ce = CallSites.end(); C != Ce; ++C) {
|
|
|
|
Instruction *I = C->getInstruction();
|
|
|
|
switch (AA.getModRefInfo(*C, *V, (*V)->getType()->getPrimitiveSize())) {
|
|
|
|
case AliasAnalysis::NoModRef:
|
|
|
|
PrintResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
|
|
|
|
++NoModRef; break;
|
|
|
|
case AliasAnalysis::Mod:
|
|
|
|
PrintResults("Mod", PrintMod, I, *V, F.getParent());
|
|
|
|
++Mod; break;
|
|
|
|
case AliasAnalysis::Ref:
|
|
|
|
PrintResults("Ref", PrintRef, I, *V, F.getParent());
|
|
|
|
++Ref; break;
|
|
|
|
case AliasAnalysis::ModRef:
|
|
|
|
PrintResults("ModRef", PrintModRef, I, *V, F.getParent());
|
|
|
|
++ModRef; break;
|
|
|
|
default:
|
|
|
|
std::cerr << "Unknown alias query result!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AAEval::doFinalization(Module &M) {
|
2004-03-12 06:15:08 +00:00
|
|
|
unsigned AliasSum = NoAlias + MayAlias + MustAlias;
|
2003-02-06 21:29:49 +00:00
|
|
|
std::cerr << "===== Alias Analysis Evaluator Report =====\n";
|
2004-03-12 06:15:08 +00:00
|
|
|
if (AliasSum == 0) {
|
2003-02-08 23:04:50 +00:00
|
|
|
std::cerr << " Alias Analysis Evaluator Summary: No pointers!\n";
|
2004-03-12 06:15:08 +00:00
|
|
|
} else {
|
|
|
|
std::cerr << " " << AliasSum << " Total Alias Queries Performed\n";
|
|
|
|
std::cerr << " " << NoAlias << " no alias responses ("
|
|
|
|
<< NoAlias*100/AliasSum << "%)\n";
|
|
|
|
std::cerr << " " << MayAlias << " may alias responses ("
|
|
|
|
<< MayAlias*100/AliasSum << "%)\n";
|
|
|
|
std::cerr << " " << MustAlias << " must alias responses ("
|
|
|
|
<< MustAlias*100/AliasSum <<"%)\n";
|
|
|
|
std::cerr << " Alias Analysis Evaluator Pointer Alias Summary: "
|
|
|
|
<< NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
|
|
|
|
<< MustAlias*100/AliasSum << "%\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the summary for mod/ref analysis
|
|
|
|
unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
|
|
|
|
if (ModRefSum == 0) {
|
|
|
|
std::cerr << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
|
|
|
|
} else {
|
|
|
|
std::cerr << " " << ModRefSum << " Total ModRef Queries Performed\n";
|
|
|
|
std::cerr << " " << NoModRef << " no mod/ref responses ("
|
|
|
|
<< NoModRef*100/ModRefSum << "%)\n";
|
|
|
|
std::cerr << " " << Mod << " mod responses ("
|
|
|
|
<< Mod*100/ModRefSum << "%)\n";
|
|
|
|
std::cerr << " " << Ref << " ref responses ("
|
|
|
|
<< Ref*100/ModRefSum <<"%)\n";
|
|
|
|
std::cerr << " " << ModRef << " mod & ref responses ("
|
|
|
|
<< ModRef*100/ModRefSum <<"%)\n";
|
|
|
|
std::cerr << " Alias Analysis Evaluator Mod/Ref Summary: "
|
|
|
|
<< NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
|
|
|
|
<< Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
|
2003-02-08 23:04:50 +00:00
|
|
|
}
|
|
|
|
|
2003-02-06 21:29:49 +00:00
|
|
|
return false;
|
|
|
|
}
|