blob: 5c0cbb26484c1b74b78d48a302f20fb34a168cbb [file] [log] [blame]
Dan Gohmanee744022010-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 Gohmanee744022010-09-16 22:08:32 +000013#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Analysis/Passes.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000016#include "llvm/IR/CallSite.h"
Chandler Carruth83948572014-03-04 10:30:26 +000017#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanee744022010-09-16 22:08:32 +000021using namespace llvm;
22
23namespace {
24 struct MemDepPrinter : public FunctionPass {
25 const Function *F;
26
Eli Friedmanc1702c82011-10-13 22:14:57 +000027 enum DepType {
28 Clobber = 0,
29 Def,
30 NonFuncLocal,
31 Unknown
32 };
33
Craig Topper95207192012-05-24 06:35:32 +000034 static const char *const DepTypeStr[];
Eli Friedmanc1702c82011-10-13 22:14:57 +000035
36 typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
37 typedef std::pair<InstTypePair, const BasicBlock *> Dep;
Dan Gohmanee744022010-09-16 22:08:32 +000038 typedef SmallSetVector<Dep, 4> DepSet;
39 typedef DenseMap<const Instruction *, DepSet> DepSetMap;
40 DepSetMap Deps;
41
42 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 MemDepPrinter() : FunctionPass(ID) {
44 initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
45 }
Dan Gohmanee744022010-09-16 22:08:32 +000046
Craig Toppere9ba7592014-03-05 07:30:04 +000047 bool runOnFunction(Function &F) override;
Dan Gohmanee744022010-09-16 22:08:32 +000048
Craig Topper9f008862014-04-15 04:59:12 +000049 void print(raw_ostream &OS, const Module * = nullptr) const override;
Dan Gohmanee744022010-09-16 22:08:32 +000050
Craig Toppere9ba7592014-03-05 07:30:04 +000051 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth7b560d42015-09-09 17:55:00 +000052 AU.addRequiredTransitive<AAResultsWrapperPass>();
Chandler Carruth61440d22016-03-10 00:55:30 +000053 AU.addRequiredTransitive<MemoryDependenceWrapperPass>();
Dan Gohmanee744022010-09-16 22:08:32 +000054 AU.setPreservesAll();
55 }
56
Craig Toppere9ba7592014-03-05 07:30:04 +000057 void releaseMemory() override {
Dan Gohmanee744022010-09-16 22:08:32 +000058 Deps.clear();
Craig Topper9f008862014-04-15 04:59:12 +000059 F = nullptr;
Dan Gohmanee744022010-09-16 22:08:32 +000060 }
Eli Friedmanc1702c82011-10-13 22:14:57 +000061
62 private:
63 static InstTypePair getInstTypePair(MemDepResult dep) {
64 if (dep.isClobber())
65 return InstTypePair(dep.getInst(), Clobber);
66 if (dep.isDef())
67 return InstTypePair(dep.getInst(), Def);
68 if (dep.isNonFuncLocal())
69 return InstTypePair(dep.getInst(), NonFuncLocal);
Eric Christopher67c0bfe2013-12-04 23:55:09 +000070 assert(dep.isUnknown() && "unexpected dependence type");
Eli Friedmanc1702c82011-10-13 22:14:57 +000071 return InstTypePair(dep.getInst(), Unknown);
72 }
73 static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
74 return InstTypePair(inst, type);
75 }
Dan Gohmanee744022010-09-16 22:08:32 +000076 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000077}
Dan Gohmanee744022010-09-16 22:08:32 +000078
79char MemDepPrinter::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000080INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
81 "Print MemDeps of function", false, true)
Chandler Carruth61440d22016-03-10 00:55:30 +000082INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +000083INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
84 "Print MemDeps of function", false, true)
Dan Gohmanee744022010-09-16 22:08:32 +000085
86FunctionPass *llvm::createMemDepPrinter() {
87 return new MemDepPrinter();
88}
89
Craig Topper95207192012-05-24 06:35:32 +000090const char *const MemDepPrinter::DepTypeStr[]
Eli Friedmanc1702c82011-10-13 22:14:57 +000091 = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
92
Dan Gohmanee744022010-09-16 22:08:32 +000093bool MemDepPrinter::runOnFunction(Function &F) {
94 this->F = &F;
Chandler Carruth61440d22016-03-10 00:55:30 +000095 MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
Dan Gohmanee744022010-09-16 22:08:32 +000096
97 // All this code uses non-const interfaces because MemDep is not
98 // const-friendly, though nothing is actually modified.
Nico Rieck78199512015-08-06 19:10:45 +000099 for (auto &I : instructions(F)) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000100 Instruction *Inst = &I;
Dan Gohmanee744022010-09-16 22:08:32 +0000101
102 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
103 continue;
104
105 MemDepResult Res = MDA.getDependency(Inst);
106 if (!Res.isNonLocal()) {
Eli Friedmanc1702c82011-10-13 22:14:57 +0000107 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
Craig Topper9f008862014-04-15 04:59:12 +0000108 static_cast<BasicBlock *>(nullptr)));
Benjamin Kramer3a09ef62015-04-10 14:50:08 +0000109 } else if (auto CS = CallSite(Inst)) {
Chandler Carruth61440d22016-03-10 00:55:30 +0000110 const MemoryDependenceResults::NonLocalDepInfo &NLDI =
Dan Gohmanee744022010-09-16 22:08:32 +0000111 MDA.getNonLocalCallDependency(CS);
112
113 DepSet &InstDeps = Deps[Inst];
Benjamin Krameraa209152016-06-26 17:27:42 +0000114 for (const NonLocalDepEntry &I : NLDI) {
115 const MemDepResult &Res = I.getResult();
116 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000117 }
118 } else {
119 SmallVector<NonLocalDepResult, 4> NLDI;
Philip Reames33d7f9d2015-01-09 00:26:45 +0000120 assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
121 isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
122 MDA.getNonLocalPointerDependency(Inst, NLDI);
Dan Gohmanee744022010-09-16 22:08:32 +0000123
124 DepSet &InstDeps = Deps[Inst];
Benjamin Krameraa209152016-06-26 17:27:42 +0000125 for (const NonLocalDepResult &I : NLDI) {
126 const MemDepResult &Res = I.getResult();
127 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000128 }
129 }
130 }
131
132 return false;
133}
134
135void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
Nico Rieck78199512015-08-06 19:10:45 +0000136 for (const auto &I : instructions(*F)) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000137 const Instruction *Inst = &I;
Dan Gohmanee744022010-09-16 22:08:32 +0000138
Dan Gohmanf4925062010-09-16 22:50:09 +0000139 DepSetMap::const_iterator DI = Deps.find(Inst);
140 if (DI == Deps.end())
Dan Gohmanee744022010-09-16 22:08:32 +0000141 continue;
142
Dan Gohmanf4925062010-09-16 22:50:09 +0000143 const DepSet &InstDeps = DI->second;
Dan Gohmanee744022010-09-16 22:08:32 +0000144
Ramkumar Ramachandraf8ea8472015-02-25 23:55:00 +0000145 for (const auto &I : InstDeps) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000146 const Instruction *DepInst = I.first.getPointer();
147 DepType type = I.first.getInt();
148 const BasicBlock *DepBB = I.second;
Dan Gohmanee744022010-09-16 22:08:32 +0000149
Eli Friedman7d58bc72011-06-15 00:47:34 +0000150 OS << " ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000151 OS << DepTypeStr[type];
Dan Gohmanee744022010-09-16 22:08:32 +0000152 if (DepBB) {
153 OS << " in block ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000154 DepBB->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohmanee744022010-09-16 22:08:32 +0000155 }
Eli Friedman7d58bc72011-06-15 00:47:34 +0000156 if (DepInst) {
157 OS << " from: ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000158 DepInst->print(OS);
Eli Friedman7d58bc72011-06-15 00:47:34 +0000159 }
Dan Gohmanee744022010-09-16 22:08:32 +0000160 OS << "\n";
161 }
162
163 Inst->print(OS);
164 OS << "\n\n";
165 }
166}