LLVM API Documentation

AliasAnalysisEvaluator.cpp

Go to the documentation of this file.
00001 //===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a simple N^2 alias analysis accuracy evaluator.
00011 // Basically, for each function in the program, it simply queries to see how the
00012 // alias analysis implementation answers alias queries between each pair of
00013 // pointers in the function.
00014 //
00015 // This is inspired and adapted from code by: Naveen Neelakantam, Francesco
00016 // Spadini, and Wojciech Stryjewski.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #include "llvm/Constants.h"
00021 #include "llvm/DerivedTypes.h"
00022 #include "llvm/Function.h"
00023 #include "llvm/Instructions.h"
00024 #include "llvm/Pass.h"
00025 #include "llvm/Analysis/Passes.h"
00026 #include "llvm/Analysis/AliasAnalysis.h"
00027 #include "llvm/Assembly/Writer.h"
00028 #include "llvm/Target/TargetData.h"
00029 #include "llvm/Support/InstIterator.h"
00030 #include "llvm/Support/CommandLine.h"
00031 #include <iostream>
00032 #include <set>
00033 
00034 using namespace llvm;
00035 
00036 namespace {
00037   cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
00038 
00039   cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
00040   cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
00041   cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
00042 
00043   cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
00044   cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
00045   cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
00046   cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
00047 
00048   class AAEval : public FunctionPass {
00049     unsigned NoAlias, MayAlias, MustAlias;
00050     unsigned NoModRef, Mod, Ref, ModRef;
00051 
00052   public:
00053     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00054       AU.addRequired<AliasAnalysis>();
00055       AU.setPreservesAll();
00056     }
00057 
00058     bool doInitialization(Module &M) {
00059       NoAlias = MayAlias = MustAlias = 0;
00060       NoModRef = Mod = Ref = ModRef = 0;
00061 
00062       if (PrintAll) {
00063         PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
00064         PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
00065       }
00066       return false;
00067     }
00068 
00069     bool runOnFunction(Function &F);
00070     bool doFinalization(Module &M);
00071   };
00072 
00073   RegisterOpt<AAEval>
00074   X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
00075 }
00076 
00077 FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
00078 
00079 static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
00080                                 Module *M) {
00081   if (P) {
00082     std::cerr << "  " << Msg << ":\t";
00083     WriteAsOperand(std::cerr, V1, true, true, M) << ", ";
00084     WriteAsOperand(std::cerr, V2, true, true, M) << "\n";
00085   }
00086 }
00087 
00088 static inline void
00089 PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
00090                    Module *M) {
00091   if (P) {
00092     std::cerr << "  " << Msg << ":  Ptr: ";
00093     WriteAsOperand(std::cerr, Ptr, true, true, M);
00094     std::cerr << "\t<->" << *I;
00095   }
00096 }
00097 
00098 bool AAEval::runOnFunction(Function &F) {
00099   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
00100 
00101   const TargetData &TD = AA.getTargetData();
00102 
00103   std::set<Value *> Pointers;
00104   std::set<CallSite> CallSites;
00105 
00106   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
00107     if (isa<PointerType>(I->getType()))    // Add all pointer arguments
00108       Pointers.insert(I);
00109 
00110   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
00111     if (isa<PointerType>(I->getType())) // Add all pointer instructions
00112       Pointers.insert(&*I);
00113     Instruction &Inst = *I;
00114     User::op_iterator OI = Inst.op_begin();
00115     if ((isa<InvokeInst>(Inst) || isa<CallInst>(Inst)) &&
00116         isa<Function>(Inst.getOperand(0)))
00117       ++OI;  // Skip actual functions for direct function calls.
00118     for (; OI != Inst.op_end(); ++OI)
00119       if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI))
00120         Pointers.insert(*OI);
00121 
00122     CallSite CS = CallSite::get(&*I);
00123     if (CS.getInstruction()) CallSites.insert(CS);
00124   }
00125 
00126   if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
00127       PrintNoModRef || PrintMod || PrintRef || PrintModRef)
00128     std::cerr << "Function: " << F.getName() << ": " << Pointers.size()
00129               << " pointers, " << CallSites.size() << " call sites\n";
00130 
00131   // iterate over the worklist, and run the full (n^2)/2 disambiguations
00132   for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
00133        I1 != E; ++I1) {
00134     unsigned I1Size = 0;
00135     const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
00136     if (I1ElTy->isSized()) I1Size = TD.getTypeSize(I1ElTy);
00137 
00138     for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
00139       unsigned I2Size = 0;
00140       const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
00141       if (I2ElTy->isSized()) I2Size = TD.getTypeSize(I2ElTy);
00142 
00143       switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
00144       case AliasAnalysis::NoAlias:
00145         PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
00146         ++NoAlias; break;
00147       case AliasAnalysis::MayAlias:
00148         PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
00149         ++MayAlias; break;
00150       case AliasAnalysis::MustAlias:
00151         PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
00152         ++MustAlias; break;
00153       default:
00154         std::cerr << "Unknown alias query result!\n";
00155       }
00156     }
00157   }
00158 
00159   // Mod/ref alias analysis: compare all pairs of calls and values
00160   for (std::set<CallSite>::iterator C = CallSites.begin(),
00161          Ce = CallSites.end(); C != Ce; ++C) {
00162     Instruction *I = C->getInstruction();
00163 
00164     for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
00165          V != Ve; ++V) {
00166       unsigned Size = 0;
00167       const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
00168       if (ElTy->isSized()) Size = TD.getTypeSize(ElTy);
00169 
00170       switch (AA.getModRefInfo(*C, *V, Size)) {
00171       case AliasAnalysis::NoModRef:
00172         PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
00173         ++NoModRef; break;
00174       case AliasAnalysis::Mod:
00175         PrintModRefResults("     Mod", PrintMod, I, *V, F.getParent());
00176         ++Mod; break;
00177       case AliasAnalysis::Ref:
00178         PrintModRefResults("     Ref", PrintRef, I, *V, F.getParent());
00179         ++Ref; break;
00180       case AliasAnalysis::ModRef:
00181         PrintModRefResults("  ModRef", PrintModRef, I, *V, F.getParent());
00182         ++ModRef; break;
00183       default:
00184         std::cerr << "Unknown alias query result!\n";
00185       }
00186     }
00187   }
00188 
00189   return false;
00190 }
00191 
00192 static void PrintPercent(unsigned Num, unsigned Sum) {
00193   std::cerr << "(" << Num*100ULL/Sum << "."
00194             << ((Num*1000ULL/Sum) % 10) << "%)\n";
00195 }
00196 
00197 bool AAEval::doFinalization(Module &M) {
00198   unsigned AliasSum = NoAlias + MayAlias + MustAlias;
00199   std::cerr << "===== Alias Analysis Evaluator Report =====\n";
00200   if (AliasSum == 0) {
00201     std::cerr << "  Alias Analysis Evaluator Summary: No pointers!\n";
00202   } else {
00203     std::cerr << "  " << AliasSum << " Total Alias Queries Performed\n";
00204     std::cerr << "  " << NoAlias << " no alias responses ";
00205     PrintPercent(NoAlias, AliasSum);
00206     std::cerr << "  " << MayAlias << " may alias responses ";
00207     PrintPercent(MayAlias, AliasSum);
00208     std::cerr << "  " << MustAlias << " must alias responses ";
00209     PrintPercent(MustAlias, AliasSum);
00210     std::cerr << "  Alias Analysis Evaluator Pointer Alias Summary: "
00211               << NoAlias*100/AliasSum  << "%/" << MayAlias*100/AliasSum << "%/"
00212               << MustAlias*100/AliasSum << "%\n";
00213   }
00214 
00215   // Display the summary for mod/ref analysis
00216   unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
00217   if (ModRefSum == 0) {
00218     std::cerr << "  Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
00219   } else {
00220     std::cerr << "  " << ModRefSum << " Total ModRef Queries Performed\n";
00221     std::cerr << "  " << NoModRef << " no mod/ref responses ";
00222     PrintPercent(NoModRef, ModRefSum);
00223     std::cerr << "  " << Mod << " mod responses ";
00224     PrintPercent(Mod, ModRefSum);
00225     std::cerr << "  " << Ref << " ref responses ";
00226     PrintPercent(Ref, ModRefSum);
00227     std::cerr << "  " << ModRef << " mod & ref responses ";
00228     PrintPercent(ModRef, ModRefSum);
00229     std::cerr << "  Alias Analysis Evaluator Mod/Ref Summary: "
00230               << NoModRef*100/ModRefSum  << "%/" << Mod*100/ModRefSum << "%/"
00231               << Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
00232   }
00233 
00234   return false;
00235 }