LLVM API Documentation

MemDepPrinter.cpp
Go to the documentation of this file.
00001 //===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
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 //
00011 //===----------------------------------------------------------------------===//
00012 
00013 #include "llvm/Analysis/Passes.h"
00014 #include "llvm/ADT/SetVector.h"
00015 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
00016 #include "llvm/IR/CallSite.h"
00017 #include "llvm/IR/InstIterator.h"
00018 #include "llvm/IR/LLVMContext.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 using namespace llvm;
00022 
00023 namespace {
00024   struct MemDepPrinter : public FunctionPass {
00025     const Function *F;
00026 
00027     enum DepType {
00028       Clobber = 0,
00029       Def,
00030       NonFuncLocal,
00031       Unknown
00032     };
00033 
00034     static const char *const DepTypeStr[];
00035 
00036     typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
00037     typedef std::pair<InstTypePair, const BasicBlock *> Dep;
00038     typedef SmallSetVector<Dep, 4> DepSet;
00039     typedef DenseMap<const Instruction *, DepSet> DepSetMap;
00040     DepSetMap Deps;
00041 
00042     static char ID; // Pass identifcation, replacement for typeid
00043     MemDepPrinter() : FunctionPass(ID) {
00044       initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
00045     }
00046 
00047     bool runOnFunction(Function &F) override;
00048 
00049     void print(raw_ostream &OS, const Module * = nullptr) const override;
00050 
00051     void getAnalysisUsage(AnalysisUsage &AU) const override {
00052       AU.addRequiredTransitive<AliasAnalysis>();
00053       AU.addRequiredTransitive<MemoryDependenceAnalysis>();
00054       AU.setPreservesAll();
00055     }
00056 
00057     void releaseMemory() override {
00058       Deps.clear();
00059       F = nullptr;
00060     }
00061 
00062   private:
00063     static InstTypePair getInstTypePair(MemDepResult dep) {
00064       if (dep.isClobber())
00065         return InstTypePair(dep.getInst(), Clobber);
00066       if (dep.isDef())
00067         return InstTypePair(dep.getInst(), Def);
00068       if (dep.isNonFuncLocal())
00069         return InstTypePair(dep.getInst(), NonFuncLocal);
00070       assert(dep.isUnknown() && "unexpected dependence type");
00071       return InstTypePair(dep.getInst(), Unknown);
00072     }
00073     static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
00074       return InstTypePair(inst, type);
00075     }
00076   };
00077 }
00078 
00079 char MemDepPrinter::ID = 0;
00080 INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
00081                       "Print MemDeps of function", false, true)
00082 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
00083 INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
00084                       "Print MemDeps of function", false, true)
00085 
00086 FunctionPass *llvm::createMemDepPrinter() {
00087   return new MemDepPrinter();
00088 }
00089 
00090 const char *const MemDepPrinter::DepTypeStr[]
00091   = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
00092 
00093 bool MemDepPrinter::runOnFunction(Function &F) {
00094   this->F = &F;
00095   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
00096   MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
00097 
00098   // All this code uses non-const interfaces because MemDep is not
00099   // const-friendly, though nothing is actually modified.
00100   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
00101     Instruction *Inst = &*I;
00102 
00103     if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
00104       continue;
00105 
00106     MemDepResult Res = MDA.getDependency(Inst);
00107     if (!Res.isNonLocal()) {
00108       Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
00109                                        static_cast<BasicBlock *>(nullptr)));
00110     } else if (CallSite CS = cast<Value>(Inst)) {
00111       const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
00112         MDA.getNonLocalCallDependency(CS);
00113 
00114       DepSet &InstDeps = Deps[Inst];
00115       for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
00116            I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
00117         const MemDepResult &Res = I->getResult();
00118         InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
00119       }
00120     } else {
00121       SmallVector<NonLocalDepResult, 4> NLDI;
00122       if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
00123         if (!LI->isUnordered()) {
00124           // FIXME: Handle atomic/volatile loads.
00125           Deps[Inst].insert(std::make_pair(getInstTypePair(nullptr, Unknown),
00126                                            static_cast<BasicBlock *>(nullptr)));
00127           continue;
00128         }
00129         AliasAnalysis::Location Loc = AA.getLocation(LI);
00130         MDA.getNonLocalPointerDependency(Loc, true, LI->getParent(), NLDI);
00131       } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
00132         if (!SI->isUnordered()) {
00133           // FIXME: Handle atomic/volatile stores.
00134           Deps[Inst].insert(std::make_pair(getInstTypePair(nullptr, Unknown),
00135                                            static_cast<BasicBlock *>(nullptr)));
00136           continue;
00137         }
00138         AliasAnalysis::Location Loc = AA.getLocation(SI);
00139         MDA.getNonLocalPointerDependency(Loc, false, SI->getParent(), NLDI);
00140       } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) {
00141         AliasAnalysis::Location Loc = AA.getLocation(VI);
00142         MDA.getNonLocalPointerDependency(Loc, false, VI->getParent(), NLDI);
00143       } else {
00144         llvm_unreachable("Unknown memory instruction!");
00145       }
00146 
00147       DepSet &InstDeps = Deps[Inst];
00148       for (SmallVectorImpl<NonLocalDepResult>::const_iterator
00149            I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
00150         const MemDepResult &Res = I->getResult();
00151         InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
00152       }
00153     }
00154   }
00155 
00156   return false;
00157 }
00158 
00159 void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
00160   for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
00161     const Instruction *Inst = &*I;
00162 
00163     DepSetMap::const_iterator DI = Deps.find(Inst);
00164     if (DI == Deps.end())
00165       continue;
00166 
00167     const DepSet &InstDeps = DI->second;
00168 
00169     for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
00170          I != E; ++I) {
00171       const Instruction *DepInst = I->first.getPointer();
00172       DepType type = I->first.getInt();
00173       const BasicBlock *DepBB = I->second;
00174 
00175       OS << "    ";
00176       OS << DepTypeStr[type];
00177       if (DepBB) {
00178         OS << " in block ";
00179         DepBB->printAsOperand(OS, /*PrintType=*/false, M);
00180       }
00181       if (DepInst) {
00182         OS << " from: ";
00183         DepInst->print(OS);
00184       }
00185       OS << "\n";
00186     }
00187 
00188     Inst->print(OS);
00189     OS << "\n\n";
00190   }
00191 }