//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
-//
+//
// 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.
-//
+//
//===----------------------------------------------------------------------===//
//
// This file implements a simple N^2 alias analysis accuracy evaluator.
//
//===----------------------------------------------------------------------===//
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
-#include "llvm/iOther.h"
-#include "llvm/iTerminators.h"
+#include "llvm/Instructions.h"
#include "llvm/Pass.h"
-#include "llvm/Type.h"
+#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Assembly/Writer.h"
+#include "llvm/Target/TargetData.h"
#include "llvm/Support/InstIterator.h"
-#include "Support/CommandLine.h"
+#include "llvm/Support/CommandLine.h"
#include <iostream>
#include <set>
AU.addRequired<AliasAnalysis>();
AU.setPreservesAll();
}
-
- bool doInitialization(Module &M) {
- NoAlias = MayAlias = MustAlias = 0;
+
+ bool doInitialization(Module &M) {
+ NoAlias = MayAlias = MustAlias = 0;
NoModRef = Mod = Ref = ModRef = 0;
if (PrintAll) {
PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
}
- return false;
+ return false;
}
bool runOnFunction(Function &F);
X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
}
+FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
+
static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
Module *M) {
if (P) {
}
}
-static inline void
+static inline void
PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
Module *M) {
if (P) {
bool AAEval::runOnFunction(Function &F) {
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
-
+
+ const TargetData &TD = AA.getTargetData();
+
std::set<Value *> Pointers;
std::set<CallSite> CallSites;
- for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
+ for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
if (isa<PointerType>(I->getType())) // Add all pointer arguments
Pointers.insert(I);
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
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)
- if (isa<PointerType>((*OI)->getType()))
+ Instruction &Inst = *I;
+ User::op_iterator OI = Inst.op_begin();
+ if ((isa<InvokeInst>(Inst) || isa<CallInst>(Inst)) &&
+ isa<Function>(Inst.getOperand(0)))
+ ++OI; // Skip actual functions for direct function calls.
+ for (; OI != Inst.op_end(); ++OI)
+ if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI))
Pointers.insert(*OI);
CallSite CS = CallSite::get(&*I);
// iterate over the worklist, and run the full (n^2)/2 disambiguations
for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
- I1 != E; ++I1)
- for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2)
- switch (AA.alias(*I1, 0, *I2, 0)) {
+ I1 != E; ++I1) {
+ unsigned I1Size = 0;
+ const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
+ if (I1ElTy->isSized()) I1Size = TD.getTypeSize(I1ElTy);
+
+ for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
+ unsigned I2Size = 0;
+ const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
+ if (I2ElTy->isSized()) I2Size = TD.getTypeSize(I2ElTy);
+
+ switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
case AliasAnalysis::NoAlias:
PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
++NoAlias; break;
default:
std::cerr << "Unknown alias query result!\n";
}
+ }
+ }
// 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())) {
+ for (std::set<CallSite>::iterator C = CallSites.begin(),
+ Ce = CallSites.end(); C != Ce; ++C) {
+ Instruction *I = C->getInstruction();
+
+ for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
+ V != Ve; ++V) {
+ unsigned Size = 0;
+ const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
+ if (ElTy->isSized()) Size = TD.getTypeSize(ElTy);
+
+ switch (AA.getModRefInfo(*C, *V, Size)) {
case AliasAnalysis::NoModRef:
PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
++NoModRef; break;
std::cerr << "Unknown alias query result!\n";
}
}
+ }
return false;
}
+static void PrintPercent(unsigned Num, unsigned Sum) {
+ std::cerr << "(" << Num*100ULL/Sum << "."
+ << ((Num*1000ULL/Sum) % 10) << "%)\n";
+}
+
bool AAEval::doFinalization(Module &M) {
unsigned AliasSum = NoAlias + MayAlias + MustAlias;
std::cerr << "===== Alias Analysis Evaluator Report =====\n";
if (AliasSum == 0) {
std::cerr << " Alias Analysis Evaluator Summary: No pointers!\n";
- } else {
+ } 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 << "%/"
+ std::cerr << " " << NoAlias << " no alias responses ";
+ PrintPercent(NoAlias, AliasSum);
+ std::cerr << " " << MayAlias << " may alias responses ";
+ PrintPercent(MayAlias, AliasSum);
+ std::cerr << " " << MustAlias << " must alias responses ";
+ PrintPercent(MustAlias, AliasSum);
+ std::cerr << " Alias Analysis Evaluator Pointer Alias Summary: "
+ << NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
<< MustAlias*100/AliasSum << "%\n";
}
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 << "%/"
+ std::cerr << " " << NoModRef << " no mod/ref responses ";
+ PrintPercent(NoModRef, ModRefSum);
+ std::cerr << " " << Mod << " mod responses ";
+ PrintPercent(Mod, ModRefSum);
+ std::cerr << " " << Ref << " ref responses ";
+ PrintPercent(Ref, ModRefSum);
+ std::cerr << " " << ModRef << " mod & ref responses ";
+ PrintPercent(ModRef, ModRefSum);
+ std::cerr << " Alias Analysis Evaluator Mod/Ref Summary: "
+ << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
<< Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
}