MemDepPrinter.cpp 5.0 KB

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