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/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
MemDepPrinter__anonb542e3df0111::MemDepPrinter44 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
getAnalysisUsage__anonb542e3df0111::MemDepPrinter52 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 AU.addRequiredTransitive<AAResultsWrapperPass>();
54 AU.addRequiredTransitive<MemoryDependenceWrapperPass>();
55 AU.setPreservesAll();
56 }
57
releaseMemory__anonb542e3df0111::MemDepPrinter58 void releaseMemory() override {
59 Deps.clear();
60 F = nullptr;
61 }
62
63 private:
getInstTypePair__anonb542e3df0111::MemDepPrinter64 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 };
75 }
76
77 char MemDepPrinter::ID = 0;
78 INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
79 "Print MemDeps of function", false, true)
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)80 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
81 INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
82 "Print MemDeps of function", false, true)
83
84 FunctionPass *llvm::createMemDepPrinter() {
85 return new MemDepPrinter();
86 }
87
88 const char *const MemDepPrinter::DepTypeStr[]
89 = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
90
runOnFunction(Function & F)91 bool MemDepPrinter::runOnFunction(Function &F) {
92 this->F = &F;
93 MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
94
95 // All this code uses non-const interfaces because MemDep is not
96 // const-friendly, though nothing is actually modified.
97 for (auto &I : instructions(F)) {
98 Instruction *Inst = &I;
99
100 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
101 continue;
102
103 MemDepResult Res = MDA.getDependency(Inst);
104 if (!Res.isNonLocal()) {
105 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
106 static_cast<BasicBlock *>(nullptr)));
107 } else if (auto *Call = dyn_cast<CallBase>(Inst)) {
108 const MemoryDependenceResults::NonLocalDepInfo &NLDI =
109 MDA.getNonLocalCallDependency(Call);
110
111 DepSet &InstDeps = Deps[Inst];
112 for (const NonLocalDepEntry &I : NLDI) {
113 const MemDepResult &Res = I.getResult();
114 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
115 }
116 } else {
117 SmallVector<NonLocalDepResult, 4> NLDI;
118 assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
119 isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
120 MDA.getNonLocalPointerDependency(Inst, NLDI);
121
122 DepSet &InstDeps = Deps[Inst];
123 for (const NonLocalDepResult &I : NLDI) {
124 const MemDepResult &Res = I.getResult();
125 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
126 }
127 }
128 }
129
130 return false;
131 }
132
print(raw_ostream & OS,const Module * M) const133 void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
134 for (const auto &I : instructions(*F)) {
135 const Instruction *Inst = &I;
136
137 DepSetMap::const_iterator DI = Deps.find(Inst);
138 if (DI == Deps.end())
139 continue;
140
141 const DepSet &InstDeps = DI->second;
142
143 for (const auto &I : InstDeps) {
144 const Instruction *DepInst = I.first.getPointer();
145 DepType type = I.first.getInt();
146 const BasicBlock *DepBB = I.second;
147
148 OS << " ";
149 OS << DepTypeStr[type];
150 if (DepBB) {
151 OS << " in block ";
152 DepBB->printAsOperand(OS, /*PrintType=*/false, M);
153 }
154 if (DepInst) {
155 OS << " from: ";
156 DepInst->print(OS);
157 }
158 OS << "\n";
159 }
160
161 Inst->print(OS);
162 OS << "\n\n";
163 }
164 }
165