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