blob: 907b321b231a792950a9dc3c5b043f4d91b94a09 [file] [log] [blame]
Dan Gohmanead01092010-09-16 22:08:32 +00001//===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
Dan Gohmanead01092010-09-16 22:08:32 +000013#include "llvm/ADT/SetVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000015#include "llvm/Analysis/Passes.h"
Chandler Carruth876ac602014-03-04 10:30:26 +000016#include "llvm/IR/InstIterator.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/LLVMContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohmanead01092010-09-16 22:08:32 +000020using namespace llvm;
21
22namespace {
23 struct MemDepPrinter : public FunctionPass {
24 const Function *F;
25
Eli Friedmanb4141422011-10-13 22:14:57 +000026 enum DepType {
27 Clobber = 0,
28 Def,
29 NonFuncLocal,
30 Unknown
31 };
32
Craig Toppere3298102012-05-24 06:35:32 +000033 static const char *const DepTypeStr[];
Eli Friedmanb4141422011-10-13 22:14:57 +000034
35 typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
36 typedef std::pair<InstTypePair, const BasicBlock *> Dep;
Dan Gohmanead01092010-09-16 22:08:32 +000037 typedef SmallSetVector<Dep, 4> DepSet;
38 typedef DenseMap<const Instruction *, DepSet> DepSetMap;
39 DepSetMap Deps;
40
41 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000042 MemDepPrinter() : FunctionPass(ID) {
43 initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
44 }
Dan Gohmanead01092010-09-16 22:08:32 +000045
Craig Topperc37e6c02014-03-05 07:30:04 +000046 bool runOnFunction(Function &F) override;
Dan Gohmanead01092010-09-16 22:08:32 +000047
Craig Topper570e52c2014-04-15 04:59:12 +000048 void print(raw_ostream &OS, const Module * = nullptr) const override;
Dan Gohmanead01092010-09-16 22:08:32 +000049
Craig Topperc37e6c02014-03-05 07:30:04 +000050 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth91468332015-09-09 17:55:00 +000051 AU.addRequiredTransitive<AAResultsWrapperPass>();
Chandler Carruthc5266b52016-03-10 00:55:30 +000052 AU.addRequiredTransitive<MemoryDependenceWrapperPass>();
Dan Gohmanead01092010-09-16 22:08:32 +000053 AU.setPreservesAll();
54 }
55
Craig Topperc37e6c02014-03-05 07:30:04 +000056 void releaseMemory() override {
Dan Gohmanead01092010-09-16 22:08:32 +000057 Deps.clear();
Craig Topper570e52c2014-04-15 04:59:12 +000058 F = nullptr;
Dan Gohmanead01092010-09-16 22:08:32 +000059 }
Eli Friedmanb4141422011-10-13 22:14:57 +000060
61 private:
62 static InstTypePair getInstTypePair(MemDepResult dep) {
63 if (dep.isClobber())
64 return InstTypePair(dep.getInst(), Clobber);
65 if (dep.isDef())
66 return InstTypePair(dep.getInst(), Def);
67 if (dep.isNonFuncLocal())
68 return InstTypePair(dep.getInst(), NonFuncLocal);
Eric Christopher1cf9e7f2013-12-04 23:55:09 +000069 assert(dep.isUnknown() && "unexpected dependence type");
Eli Friedmanb4141422011-10-13 22:14:57 +000070 return InstTypePair(dep.getInst(), Unknown);
71 }
72 static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
73 return InstTypePair(inst, type);
74 }
Dan Gohmanead01092010-09-16 22:08:32 +000075 };
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +000076}
Dan Gohmanead01092010-09-16 22:08:32 +000077
78char MemDepPrinter::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000079INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
80 "Print MemDeps of function", false, true)
Chandler Carruthc5266b52016-03-10 00:55:30 +000081INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
Owen Anderson2ab36d32010-10-12 19:48:12 +000082INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
83 "Print MemDeps of function", false, true)
Dan Gohmanead01092010-09-16 22:08:32 +000084
85FunctionPass *llvm::createMemDepPrinter() {
86 return new MemDepPrinter();
87}
88
Craig Toppere3298102012-05-24 06:35:32 +000089const char *const MemDepPrinter::DepTypeStr[]
Eli Friedmanb4141422011-10-13 22:14:57 +000090 = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
91
Dan Gohmanead01092010-09-16 22:08:32 +000092bool MemDepPrinter::runOnFunction(Function &F) {
93 this->F = &F;
Chandler Carruthc5266b52016-03-10 00:55:30 +000094 MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
Dan Gohmanead01092010-09-16 22:08:32 +000095
96 // All this code uses non-const interfaces because MemDep is not
97 // const-friendly, though nothing is actually modified.
Nico Rieck3dd7bf52015-08-06 19:10:45 +000098 for (auto &I : instructions(F)) {
Ramkumar Ramachandra46b30b82015-02-09 19:49:54 +000099 Instruction *Inst = &I;
Dan Gohmanead01092010-09-16 22:08:32 +0000100
101 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
102 continue;
103
104 MemDepResult Res = MDA.getDependency(Inst);
105 if (!Res.isNonLocal()) {
Eli Friedmanb4141422011-10-13 22:14:57 +0000106 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
Craig Topper570e52c2014-04-15 04:59:12 +0000107 static_cast<BasicBlock *>(nullptr)));
Chandler Carruth81aa7122019-01-07 05:42:51 +0000108 } else if (auto *Call = dyn_cast<CallBase>(Inst)) {
Chandler Carruthc5266b52016-03-10 00:55:30 +0000109 const MemoryDependenceResults::NonLocalDepInfo &NLDI =
Chandler Carruth81aa7122019-01-07 05:42:51 +0000110 MDA.getNonLocalCallDependency(Call);
Dan Gohmanead01092010-09-16 22:08:32 +0000111
112 DepSet &InstDeps = Deps[Inst];
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +0000113 for (const NonLocalDepEntry &I : NLDI) {
114 const MemDepResult &Res = I.getResult();
115 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
Dan Gohmanead01092010-09-16 22:08:32 +0000116 }
117 } else {
118 SmallVector<NonLocalDepResult, 4> NLDI;
Philip Reamesce0e12f2015-01-09 00:26:45 +0000119 assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
Fangrui Songaf7b1832018-07-30 19:41:25 +0000120 isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
Philip Reamesce0e12f2015-01-09 00:26:45 +0000121 MDA.getNonLocalPointerDependency(Inst, NLDI);
Dan Gohmanead01092010-09-16 22:08:32 +0000122
123 DepSet &InstDeps = Deps[Inst];
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +0000124 for (const NonLocalDepResult &I : NLDI) {
125 const MemDepResult &Res = I.getResult();
126 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
Dan Gohmanead01092010-09-16 22:08:32 +0000127 }
128 }
129 }
130
131 return false;
132}
133
134void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
Nico Rieck3dd7bf52015-08-06 19:10:45 +0000135 for (const auto &I : instructions(*F)) {
Ramkumar Ramachandra46b30b82015-02-09 19:49:54 +0000136 const Instruction *Inst = &I;
Dan Gohmanead01092010-09-16 22:08:32 +0000137
Dan Gohmana627e9b2010-09-16 22:50:09 +0000138 DepSetMap::const_iterator DI = Deps.find(Inst);
139 if (DI == Deps.end())
Dan Gohmanead01092010-09-16 22:08:32 +0000140 continue;
141
Dan Gohmana627e9b2010-09-16 22:50:09 +0000142 const DepSet &InstDeps = DI->second;
Dan Gohmanead01092010-09-16 22:08:32 +0000143
Ramkumar Ramachandrae9a49d62015-02-25 23:55:00 +0000144 for (const auto &I : InstDeps) {
Ramkumar Ramachandra46b30b82015-02-09 19:49:54 +0000145 const Instruction *DepInst = I.first.getPointer();
146 DepType type = I.first.getInt();
147 const BasicBlock *DepBB = I.second;
Dan Gohmanead01092010-09-16 22:08:32 +0000148
Eli Friedmana990e0712011-06-15 00:47:34 +0000149 OS << " ";
Eli Friedmanb4141422011-10-13 22:14:57 +0000150 OS << DepTypeStr[type];
Dan Gohmanead01092010-09-16 22:08:32 +0000151 if (DepBB) {
152 OS << " in block ";
Chandler Carruth560e3952014-01-09 02:29:41 +0000153 DepBB->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohmanead01092010-09-16 22:08:32 +0000154 }
Eli Friedmana990e0712011-06-15 00:47:34 +0000155 if (DepInst) {
156 OS << " from: ";
Eli Friedmanb4141422011-10-13 22:14:57 +0000157 DepInst->print(OS);
Eli Friedmana990e0712011-06-15 00:47:34 +0000158 }
Dan Gohmanead01092010-09-16 22:08:32 +0000159 OS << "\n";
160 }
161
162 Inst->print(OS);
163 OS << "\n\n";
164 }
165}