MemDepPrinter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Analysis/Passes.h"
  13. #include "llvm/ADT/SetVector.h"
  14. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  15. #include "llvm/IR/CallSite.h"
  16. #include "llvm/IR/InstIterator.h"
  17. #include "llvm/IR/LLVMContext.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. namespace {
  22. struct MemDepPrinter : public FunctionPass {
  23. const Function *F;
  24. enum DepType {
  25. Clobber = 0,
  26. Def,
  27. NonFuncLocal,
  28. Unknown
  29. };
  30. static const char *const DepTypeStr[];
  31. typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
  32. typedef std::pair<InstTypePair, const BasicBlock *> Dep;
  33. typedef SmallSetVector<Dep, 4> DepSet;
  34. typedef DenseMap<const Instruction *, DepSet> DepSetMap;
  35. DepSetMap Deps;
  36. static char ID; // Pass identifcation, replacement for typeid
  37. MemDepPrinter() : FunctionPass(ID) {
  38. initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
  39. }
  40. bool runOnFunction(Function &F) override;
  41. void print(raw_ostream &OS, const Module * = nullptr) const override;
  42. void getAnalysisUsage(AnalysisUsage &AU) const override {
  43. AU.addRequiredTransitive<AliasAnalysis>();
  44. AU.addRequiredTransitive<MemoryDependenceAnalysis>();
  45. AU.setPreservesAll();
  46. }
  47. void releaseMemory() override {
  48. Deps.clear();
  49. F = nullptr;
  50. }
  51. private:
  52. static InstTypePair getInstTypePair(MemDepResult dep) {
  53. if (dep.isClobber())
  54. return InstTypePair(dep.getInst(), Clobber);
  55. if (dep.isDef())
  56. return InstTypePair(dep.getInst(), Def);
  57. if (dep.isNonFuncLocal())
  58. return InstTypePair(dep.getInst(), NonFuncLocal);
  59. assert(dep.isUnknown() && "unexpected dependence type");
  60. return InstTypePair(dep.getInst(), Unknown);
  61. }
  62. static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
  63. return InstTypePair(inst, type);
  64. }
  65. };
  66. }
  67. char MemDepPrinter::ID = 0;
  68. INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
  69. "Print MemDeps of function", false, true)
  70. INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
  71. INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
  72. "Print MemDeps of function", false, true)
  73. FunctionPass *llvm::createMemDepPrinter() {
  74. return new MemDepPrinter();
  75. }
  76. const char *const MemDepPrinter::DepTypeStr[]
  77. = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
  78. bool MemDepPrinter::runOnFunction(Function &F) {
  79. this->F = &F;
  80. MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
  81. // All this code uses non-const interfaces because MemDep is not
  82. // const-friendly, though nothing is actually modified.
  83. for (auto &I : inst_range(F)) {
  84. Instruction *Inst = &I;
  85. if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
  86. continue;
  87. MemDepResult Res = MDA.getDependency(Inst);
  88. if (!Res.isNonLocal()) {
  89. Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
  90. static_cast<BasicBlock *>(nullptr)));
  91. } else if (auto CS = CallSite(Inst)) {
  92. const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
  93. MDA.getNonLocalCallDependency(CS);
  94. DepSet &InstDeps = Deps[Inst];
  95. for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
  96. I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
  97. const MemDepResult &Res = I->getResult();
  98. InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
  99. }
  100. } else {
  101. SmallVector<NonLocalDepResult, 4> NLDI;
  102. assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
  103. isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
  104. MDA.getNonLocalPointerDependency(Inst, NLDI);
  105. DepSet &InstDeps = Deps[Inst];
  106. for (SmallVectorImpl<NonLocalDepResult>::const_iterator
  107. I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
  108. const MemDepResult &Res = I->getResult();
  109. InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
  110. }
  111. }
  112. }
  113. return false;
  114. }
  115. void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
  116. for (const auto &I : inst_range(*F)) {
  117. const Instruction *Inst = &I;
  118. DepSetMap::const_iterator DI = Deps.find(Inst);
  119. if (DI == Deps.end())
  120. continue;
  121. const DepSet &InstDeps = DI->second;
  122. for (const auto &I : InstDeps) {
  123. const Instruction *DepInst = I.first.getPointer();
  124. DepType type = I.first.getInt();
  125. const BasicBlock *DepBB = I.second;
  126. OS << " ";
  127. OS << DepTypeStr[type];
  128. if (DepBB) {
  129. OS << " in block ";
  130. DepBB->printAsOperand(OS, /*PrintType=*/false, M);
  131. }
  132. if (DepInst) {
  133. OS << " from: ";
  134. DepInst->print(OS);
  135. }
  136. OS << "\n";
  137. }
  138. Inst->print(OS);
  139. OS << "\n\n";
  140. }
  141. }