blob: e1b7b4b8509df99642f70fc8f563f75645365e99 [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/Analysis/Passes.h"
Dan Gohmanee744022010-09-16 22:08:32 +000014#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Analysis/MemoryDependenceAnalysis.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 {
Dan Gohman0a6021a2010-11-10 20:37:15 +000052 AU.addRequiredTransitive<AliasAnalysis>();
53 AU.addRequiredTransitive<MemoryDependenceAnalysis>();
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 };
77}
78
79char MemDepPrinter::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000080INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
81 "Print MemDeps of function", false, true)
82INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
83INITIALIZE_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;
95 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
96
97 // All this code uses non-const interfaces because MemDep is not
98 // const-friendly, though nothing is actually modified.
Ramkumar Ramachandraf8ea8472015-02-25 23:55:00 +000099 for (auto &I : inst_range(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)));
Dan Gohmanee744022010-09-16 22:08:32 +0000109 } else if (CallSite CS = cast<Value>(Inst)) {
110 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
111 MDA.getNonLocalCallDependency(CS);
112
113 DepSet &InstDeps = Deps[Inst];
114 for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
115 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
116 const MemDepResult &Res = I->getResult();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000117 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000118 }
119 } else {
120 SmallVector<NonLocalDepResult, 4> NLDI;
Philip Reames33d7f9d2015-01-09 00:26:45 +0000121 assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
122 isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
123 MDA.getNonLocalPointerDependency(Inst, NLDI);
Dan Gohmanee744022010-09-16 22:08:32 +0000124
125 DepSet &InstDeps = Deps[Inst];
126 for (SmallVectorImpl<NonLocalDepResult>::const_iterator
127 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
128 const MemDepResult &Res = I->getResult();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000129 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000130 }
131 }
132 }
133
134 return false;
135}
136
137void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
Ramkumar Ramachandraf8ea8472015-02-25 23:55:00 +0000138 for (const auto &I : inst_range(*F)) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000139 const Instruction *Inst = &I;
Dan Gohmanee744022010-09-16 22:08:32 +0000140
Dan Gohmanf4925062010-09-16 22:50:09 +0000141 DepSetMap::const_iterator DI = Deps.find(Inst);
142 if (DI == Deps.end())
Dan Gohmanee744022010-09-16 22:08:32 +0000143 continue;
144
Dan Gohmanf4925062010-09-16 22:50:09 +0000145 const DepSet &InstDeps = DI->second;
Dan Gohmanee744022010-09-16 22:08:32 +0000146
Ramkumar Ramachandraf8ea8472015-02-25 23:55:00 +0000147 for (const auto &I : InstDeps) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000148 const Instruction *DepInst = I.first.getPointer();
149 DepType type = I.first.getInt();
150 const BasicBlock *DepBB = I.second;
Dan Gohmanee744022010-09-16 22:08:32 +0000151
Eli Friedman7d58bc72011-06-15 00:47:34 +0000152 OS << " ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000153 OS << DepTypeStr[type];
Dan Gohmanee744022010-09-16 22:08:32 +0000154 if (DepBB) {
155 OS << " in block ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000156 DepBB->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohmanee744022010-09-16 22:08:32 +0000157 }
Eli Friedman7d58bc72011-06-15 00:47:34 +0000158 if (DepInst) {
159 OS << " from: ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000160 DepInst->print(OS);
Eli Friedman7d58bc72011-06-15 00:47:34 +0000161 }
Dan Gohmanee744022010-09-16 22:08:32 +0000162 OS << "\n";
163 }
164
165 Inst->print(OS);
166 OS << "\n\n";
167 }
168}