LLVM API Documentation

AliasDebugger.cpp
Go to the documentation of this file.
00001 //===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This simple pass checks alias analysis users to ensure that if they
00011 // create a new value, they do not query AA without informing it of the value.
00012 // It acts as a shim over any other AA pass you want.
00013 //
00014 // Yes keeping track of every value in the program is expensive, but this is 
00015 // a debugging pass.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Analysis/Passes.h"
00020 #include "llvm/Analysis/AliasAnalysis.h"
00021 #include "llvm/IR/Constants.h"
00022 #include "llvm/IR/DerivedTypes.h"
00023 #include "llvm/IR/Instructions.h"
00024 #include "llvm/IR/Module.h"
00025 #include "llvm/Pass.h"
00026 #include <set>
00027 using namespace llvm;
00028 
00029 namespace {
00030   
00031   class AliasDebugger : public ModulePass, public AliasAnalysis {
00032 
00033     //What we do is simple.  Keep track of every value the AA could
00034     //know about, and verify that queries are one of those.
00035     //A query to a value that didn't exist when the AA was created
00036     //means someone forgot to update the AA when creating new values
00037 
00038     std::set<const Value*> Vals;
00039     
00040   public:
00041     static char ID; // Class identification, replacement for typeinfo
00042     AliasDebugger() : ModulePass(ID) {
00043       initializeAliasDebuggerPass(*PassRegistry::getPassRegistry());
00044     }
00045 
00046     bool runOnModule(Module &M) override {
00047       InitializeAliasAnalysis(this);                 // set up super class
00048 
00049       for(Module::global_iterator I = M.global_begin(),
00050             E = M.global_end(); I != E; ++I) {
00051         Vals.insert(&*I);
00052         for (User::const_op_iterator OI = I->op_begin(),
00053              OE = I->op_end(); OI != OE; ++OI)
00054           Vals.insert(*OI);
00055       }
00056 
00057       for(Module::iterator I = M.begin(),
00058             E = M.end(); I != E; ++I){
00059         Vals.insert(&*I);
00060         if(!I->isDeclaration()) {
00061           for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
00062                AI != AE; ++AI) 
00063             Vals.insert(&*AI);     
00064           for (Function::const_iterator FI = I->begin(), FE = I->end();
00065                FI != FE; ++FI) 
00066             for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
00067                  BI != BE; ++BI) {
00068               Vals.insert(&*BI);
00069               for (User::const_op_iterator OI = BI->op_begin(),
00070                    OE = BI->op_end(); OI != OE; ++OI)
00071                 Vals.insert(*OI);
00072             }
00073         }
00074         
00075       }
00076       return false;
00077     }
00078 
00079     void getAnalysisUsage(AnalysisUsage &AU) const override {
00080       AliasAnalysis::getAnalysisUsage(AU);
00081       AU.setPreservesAll();                         // Does not transform code
00082     }
00083 
00084     /// getAdjustedAnalysisPointer - This method is used when a pass implements
00085     /// an analysis interface through multiple inheritance.  If needed, it
00086     /// should override this to adjust the this pointer as needed for the
00087     /// specified pass info.
00088     void *getAdjustedAnalysisPointer(AnalysisID PI) override {
00089       if (PI == &AliasAnalysis::ID)
00090         return (AliasAnalysis*)this;
00091       return this;
00092     }
00093     
00094     //------------------------------------------------
00095     // Implement the AliasAnalysis API
00096     //
00097     AliasResult alias(const Location &LocA, const Location &LocB) override {
00098       assert(Vals.find(LocA.Ptr) != Vals.end() &&
00099              "Never seen value in AA before");
00100       assert(Vals.find(LocB.Ptr) != Vals.end() &&
00101              "Never seen value in AA before");
00102       return AliasAnalysis::alias(LocA, LocB);
00103     }
00104 
00105     ModRefResult getModRefInfo(ImmutableCallSite CS,
00106                                const Location &Loc) override {
00107       assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
00108       return AliasAnalysis::getModRefInfo(CS, Loc);
00109     }
00110 
00111     ModRefResult getModRefInfo(ImmutableCallSite CS1,
00112                                ImmutableCallSite CS2) override {
00113       return AliasAnalysis::getModRefInfo(CS1,CS2);
00114     }
00115 
00116     bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override {
00117       assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
00118       return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00119     }
00120 
00121     void deleteValue(Value *V) override {
00122       assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
00123       AliasAnalysis::deleteValue(V);
00124     }
00125     void copyValue(Value *From, Value *To) override {
00126       Vals.insert(To);
00127       AliasAnalysis::copyValue(From, To);
00128     }
00129 
00130   };
00131 }
00132 
00133 char AliasDebugger::ID = 0;
00134 INITIALIZE_AG_PASS(AliasDebugger, AliasAnalysis, "debug-aa",
00135                    "AA use debugger", false, true, false)
00136 
00137 Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
00138