//
// 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 is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include "llvm/Target/TargetData.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/CommandLine.h"
-#include <iostream>
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/raw_ostream.h"
#include <set>
-
+#include <sstream>
using namespace llvm;
-namespace {
- cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
+static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
- 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);
+static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
+static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
+static 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);
+static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
+static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
+static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
+static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
- class AAEval : public FunctionPass {
+namespace {
+ class VISIBILITY_HIDDEN AAEval : public FunctionPass {
unsigned NoAlias, MayAlias, MustAlias;
unsigned NoModRef, Mod, Ref, ModRef;
public:
+ static char ID; // Pass identification, replacement for typeid
+ AAEval() : FunctionPass(&ID) {}
+
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<AliasAnalysis>();
AU.setPreservesAll();
bool runOnFunction(Function &F);
bool doFinalization(Module &M);
};
-
- RegisterOpt<AAEval>
- X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
}
+char AAEval::ID = 0;
+static RegisterPass<AAEval>
+X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator", false, true);
+
FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
-static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
- Module *M) {
+static void PrintResults(const char *Msg, bool P, const Value *V1, const Value *V2,
+ const Module *M) {
if (P) {
- std::cerr << " " << Msg << ":\t";
- WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
- WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
+ std::stringstream s1, s2;
+ WriteAsOperand(s1, V1, true, M);
+ WriteAsOperand(s2, V2, true, M);
+ std::string o1(s1.str()), o2(s2.str());
+ if (o2 < o1)
+ std::swap(o1, o2);
+ errs() << " " << Msg << ":\t"
+ << o1 << ", "
+ << o2 << "\n";
}
}
PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
Module *M) {
if (P) {
- std::cerr << " " << Msg << ": Ptr: ";
- WriteAsOperand(std::cerr, Ptr, true, true, M);
- std::cerr << "\t<->" << *I;
+ errs() << " " << Msg << ": Ptr: ";
+ WriteAsOperand(errs(), Ptr, true, M);
+ errs() << "\t<->" << *I;
}
}
bool AAEval::runOnFunction(Function &F) {
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
- const TargetData &TD = AA.getTargetData();
-
std::set<Value *> Pointers;
std::set<CallSite> CallSites;
Pointers.insert(&*I);
Instruction &Inst = *I;
User::op_iterator OI = Inst.op_begin();
- if ((isa<InvokeInst>(Inst) || isa<CallInst>(Inst)) &&
- isa<Function>(Inst.getOperand(0)))
+ CallSite CS = CallSite::get(&Inst);
+ if (CS.getInstruction() &&
+ isa<Function>(CS.getCalledValue()))
++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);
if (CS.getInstruction()) CallSites.insert(CS);
}
if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
PrintNoModRef || PrintMod || PrintRef || PrintModRef)
- std::cerr << "Function: " << F.getName() << ": " << Pointers.size()
- << " pointers, " << CallSites.size() << " call sites\n";
+ errs() << "Function: " << F.getName() << ": " << Pointers.size()
+ << " pointers, " << CallSites.size() << " call sites\n";
// 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) {
- unsigned I1Size = 0;
+ unsigned I1Size = ~0u;
const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
- if (I1ElTy->isSized()) I1Size = TD.getTypeSize(I1ElTy);
+ if (I1ElTy->isSized()) I1Size = AA.getTypeStoreSize(I1ElTy);
for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
- unsigned I2Size = 0;
+ unsigned I2Size = ~0u;
const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
- if (I2ElTy->isSized()) I2Size = TD.getTypeSize(I2ElTy);
+ if (I2ElTy->isSized()) I2Size = AA.getTypeStoreSize(I2ElTy);
switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
case AliasAnalysis::NoAlias:
PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
++MustAlias; break;
default:
- std::cerr << "Unknown alias query result!\n";
+ errs() << "Unknown alias query result!\n";
}
}
}
for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
V != Ve; ++V) {
- unsigned Size = 0;
+ unsigned Size = ~0u;
const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
- if (ElTy->isSized()) Size = TD.getTypeSize(ElTy);
+ if (ElTy->isSized()) Size = AA.getTypeStoreSize(ElTy);
switch (AA.getModRefInfo(*C, *V, Size)) {
case AliasAnalysis::NoModRef:
PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent());
++ModRef; break;
default:
- std::cerr << "Unknown alias query result!\n";
+ errs() << "Unknown alias query result!\n";
}
}
}
}
static void PrintPercent(unsigned Num, unsigned Sum) {
- std::cerr << "(" << Num*100ULL/Sum << "."
- << ((Num*1000ULL/Sum) % 10) << "%)\n";
+ errs() << "(" << 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";
+ errs() << "===== Alias Analysis Evaluator Report =====\n";
if (AliasSum == 0) {
- std::cerr << " Alias Analysis Evaluator Summary: No pointers!\n";
+ errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
} else {
- std::cerr << " " << AliasSum << " Total Alias Queries Performed\n";
- std::cerr << " " << NoAlias << " no alias responses ";
+ errs() << " " << AliasSum << " Total Alias Queries Performed\n";
+ errs() << " " << NoAlias << " no alias responses ";
PrintPercent(NoAlias, AliasSum);
- std::cerr << " " << MayAlias << " may alias responses ";
+ errs() << " " << MayAlias << " may alias responses ";
PrintPercent(MayAlias, AliasSum);
- std::cerr << " " << MustAlias << " must alias responses ";
+ errs() << " " << 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";
+ errs() << " 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";
+ errs() << " 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 ";
+ errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
+ errs() << " " << NoModRef << " no mod/ref responses ";
PrintPercent(NoModRef, ModRefSum);
- std::cerr << " " << Mod << " mod responses ";
+ errs() << " " << Mod << " mod responses ";
PrintPercent(Mod, ModRefSum);
- std::cerr << " " << Ref << " ref responses ";
+ errs() << " " << Ref << " ref responses ";
PrintPercent(Ref, ModRefSum);
- std::cerr << " " << ModRef << " mod & ref responses ";
+ errs() << " " << 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";
+ errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
+ << NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
+ << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
}
return false;