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