MemDepPrinter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/ADT/SetVector.h"
  12. #include "llvm/Analysis/AliasAnalysis.h"
  13. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  14. #include "llvm/Analysis/Passes.h"
  15. #include "llvm/IR/InstIterator.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/InitializePasses.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<AAResultsWrapperPass>();
  44. AU.addRequiredTransitive<MemoryDependenceWrapperPass>();
  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. };
  63. }
  64. char MemDepPrinter::ID = 0;
  65. INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
  66. "Print MemDeps of function", false, true)
  67. INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
  68. INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
  69. "Print MemDeps of function", false, true)
  70. FunctionPass *llvm::createMemDepPrinter() {
  71. return new MemDepPrinter();
  72. }
  73. const char *const MemDepPrinter::DepTypeStr[]
  74. = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
  75. bool MemDepPrinter::runOnFunction(Function &F) {
  76. this->F = &F;
  77. MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
  78. // All this code uses non-const interfaces because MemDep is not
  79. // const-friendly, though nothing is actually modified.
  80. for (auto &I : instructions(F)) {
  81. Instruction *Inst = &I;
  82. if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
  83. continue;
  84. MemDepResult Res = MDA.getDependency(Inst);
  85. if (!Res.isNonLocal()) {
  86. Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
  87. static_cast<BasicBlock *>(nullptr)));
  88. } else if (auto *Call = dyn_cast<CallBase>(Inst)) {
  89. const MemoryDependenceResults::NonLocalDepInfo &NLDI =
  90. MDA.getNonLocalCallDependency(Call);
  91. DepSet &InstDeps = Deps[Inst];
  92. for (const NonLocalDepEntry &I : NLDI) {
  93. const MemDepResult &Res = I.getResult();
  94. InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
  95. }
  96. } else {
  97. SmallVector<NonLocalDepResult, 4> NLDI;
  98. assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
  99. isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
  100. MDA.getNonLocalPointerDependency(Inst, NLDI);
  101. DepSet &InstDeps = Deps[Inst];
  102. for (const NonLocalDepResult &I : NLDI) {
  103. const MemDepResult &Res = I.getResult();
  104. InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
  111. for (const auto &I : instructions(*F)) {
  112. const Instruction *Inst = &I;
  113. DepSetMap::const_iterator DI = Deps.find(Inst);
  114. if (DI == Deps.end())
  115. continue;
  116. const DepSet &InstDeps = DI->second;
  117. for (const auto &I : InstDeps) {
  118. const Instruction *DepInst = I.first.getPointer();
  119. DepType type = I.first.getInt();
  120. const BasicBlock *DepBB = I.second;
  121. OS << " ";
  122. OS << DepTypeStr[type];
  123. if (DepBB) {
  124. OS << " in block ";
  125. DepBB->printAsOperand(OS, /*PrintType=*/false, M);
  126. }
  127. if (DepInst) {
  128. OS << " from: ";
  129. DepInst->print(OS);
  130. }
  131. OS << "\n";
  132. }
  133. Inst->print(OS);
  134. OS << "\n\n";
  135. }
  136. }