blob: 6e1bb50e88936576227b13cc1500f84082037c69 [file] [log] [blame]
Dan Gohmanee744022010-09-16 22:08:32 +00001//===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohmanee744022010-09-16 22:08:32 +00006//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
Dan Gohmanee744022010-09-16 22:08:32 +000012#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Analysis/Passes.h"
Chandler Carruth83948572014-03-04 10:30:26 +000015#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/raw_ostream.h"
Dan Gohmanee744022010-09-16 22:08:32 +000019using namespace llvm;
20
21namespace {
22 struct MemDepPrinter : public FunctionPass {
23 const Function *F;
24
Eli Friedmanc1702c82011-10-13 22:14:57 +000025 enum DepType {
26 Clobber = 0,
27 Def,
28 NonFuncLocal,
29 Unknown
30 };
31
Craig Topper95207192012-05-24 06:35:32 +000032 static const char *const DepTypeStr[];
Eli Friedmanc1702c82011-10-13 22:14:57 +000033
34 typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
35 typedef std::pair<InstTypePair, const BasicBlock *> Dep;
Dan Gohmanee744022010-09-16 22:08:32 +000036 typedef SmallSetVector<Dep, 4> DepSet;
37 typedef DenseMap<const Instruction *, DepSet> DepSetMap;
38 DepSetMap Deps;
39
40 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000041 MemDepPrinter() : FunctionPass(ID) {
42 initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
43 }
Dan Gohmanee744022010-09-16 22:08:32 +000044
Craig Toppere9ba7592014-03-05 07:30:04 +000045 bool runOnFunction(Function &F) override;
Dan Gohmanee744022010-09-16 22:08:32 +000046
Craig Topper9f008862014-04-15 04:59:12 +000047 void print(raw_ostream &OS, const Module * = nullptr) const override;
Dan Gohmanee744022010-09-16 22:08:32 +000048
Craig Toppere9ba7592014-03-05 07:30:04 +000049 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth7b560d42015-09-09 17:55:00 +000050 AU.addRequiredTransitive<AAResultsWrapperPass>();
Chandler Carruth61440d22016-03-10 00:55:30 +000051 AU.addRequiredTransitive<MemoryDependenceWrapperPass>();
Dan Gohmanee744022010-09-16 22:08:32 +000052 AU.setPreservesAll();
53 }
54
Craig Toppere9ba7592014-03-05 07:30:04 +000055 void releaseMemory() override {
Dan Gohmanee744022010-09-16 22:08:32 +000056 Deps.clear();
Craig Topper9f008862014-04-15 04:59:12 +000057 F = nullptr;
Dan Gohmanee744022010-09-16 22:08:32 +000058 }
Eli Friedmanc1702c82011-10-13 22:14:57 +000059
60 private:
61 static InstTypePair getInstTypePair(MemDepResult dep) {
62 if (dep.isClobber())
63 return InstTypePair(dep.getInst(), Clobber);
64 if (dep.isDef())
65 return InstTypePair(dep.getInst(), Def);
66 if (dep.isNonFuncLocal())
67 return InstTypePair(dep.getInst(), NonFuncLocal);
Eric Christopher67c0bfe2013-12-04 23:55:09 +000068 assert(dep.isUnknown() && "unexpected dependence type");
Eli Friedmanc1702c82011-10-13 22:14:57 +000069 return InstTypePair(dep.getInst(), Unknown);
70 }
71 static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
72 return InstTypePair(inst, type);
73 }
Dan Gohmanee744022010-09-16 22:08:32 +000074 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000075}
Dan Gohmanee744022010-09-16 22:08:32 +000076
77char MemDepPrinter::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000078INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
79 "Print MemDeps of function", false, true)
Chandler Carruth61440d22016-03-10 00:55:30 +000080INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +000081INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
82 "Print MemDeps of function", false, true)
Dan Gohmanee744022010-09-16 22:08:32 +000083
84FunctionPass *llvm::createMemDepPrinter() {
85 return new MemDepPrinter();
86}
87
Craig Topper95207192012-05-24 06:35:32 +000088const char *const MemDepPrinter::DepTypeStr[]
Eli Friedmanc1702c82011-10-13 22:14:57 +000089 = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
90
Dan Gohmanee744022010-09-16 22:08:32 +000091bool MemDepPrinter::runOnFunction(Function &F) {
92 this->F = &F;
Chandler Carruth61440d22016-03-10 00:55:30 +000093 MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
Dan Gohmanee744022010-09-16 22:08:32 +000094
95 // All this code uses non-const interfaces because MemDep is not
96 // const-friendly, though nothing is actually modified.
Nico Rieck78199512015-08-06 19:10:45 +000097 for (auto &I : instructions(F)) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +000098 Instruction *Inst = &I;
Dan Gohmanee744022010-09-16 22:08:32 +000099
100 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
101 continue;
102
103 MemDepResult Res = MDA.getDependency(Inst);
104 if (!Res.isNonLocal()) {
Eli Friedmanc1702c82011-10-13 22:14:57 +0000105 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
Craig Topper9f008862014-04-15 04:59:12 +0000106 static_cast<BasicBlock *>(nullptr)));
Chandler Carruth363ac682019-01-07 05:42:51 +0000107 } else if (auto *Call = dyn_cast<CallBase>(Inst)) {
Chandler Carruth61440d22016-03-10 00:55:30 +0000108 const MemoryDependenceResults::NonLocalDepInfo &NLDI =
Chandler Carruth363ac682019-01-07 05:42:51 +0000109 MDA.getNonLocalCallDependency(Call);
Dan Gohmanee744022010-09-16 22:08:32 +0000110
111 DepSet &InstDeps = Deps[Inst];
Benjamin Krameraa209152016-06-26 17:27:42 +0000112 for (const NonLocalDepEntry &I : NLDI) {
113 const MemDepResult &Res = I.getResult();
114 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000115 }
116 } else {
117 SmallVector<NonLocalDepResult, 4> NLDI;
Philip Reames33d7f9d2015-01-09 00:26:45 +0000118 assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
Fangrui Songf78650a2018-07-30 19:41:25 +0000119 isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
Philip Reames33d7f9d2015-01-09 00:26:45 +0000120 MDA.getNonLocalPointerDependency(Inst, NLDI);
Dan Gohmanee744022010-09-16 22:08:32 +0000121
122 DepSet &InstDeps = Deps[Inst];
Benjamin Krameraa209152016-06-26 17:27:42 +0000123 for (const NonLocalDepResult &I : NLDI) {
124 const MemDepResult &Res = I.getResult();
125 InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000126 }
127 }
128 }
129
130 return false;
131}
132
133void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
Nico Rieck78199512015-08-06 19:10:45 +0000134 for (const auto &I : instructions(*F)) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000135 const Instruction *Inst = &I;
Dan Gohmanee744022010-09-16 22:08:32 +0000136
Dan Gohmanf4925062010-09-16 22:50:09 +0000137 DepSetMap::const_iterator DI = Deps.find(Inst);
138 if (DI == Deps.end())
Dan Gohmanee744022010-09-16 22:08:32 +0000139 continue;
140
Dan Gohmanf4925062010-09-16 22:50:09 +0000141 const DepSet &InstDeps = DI->second;
Dan Gohmanee744022010-09-16 22:08:32 +0000142
Ramkumar Ramachandraf8ea8472015-02-25 23:55:00 +0000143 for (const auto &I : InstDeps) {
Ramkumar Ramachandra010b77c2015-02-09 19:49:54 +0000144 const Instruction *DepInst = I.first.getPointer();
145 DepType type = I.first.getInt();
146 const BasicBlock *DepBB = I.second;
Dan Gohmanee744022010-09-16 22:08:32 +0000147
Eli Friedman7d58bc72011-06-15 00:47:34 +0000148 OS << " ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000149 OS << DepTypeStr[type];
Dan Gohmanee744022010-09-16 22:08:32 +0000150 if (DepBB) {
151 OS << " in block ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000152 DepBB->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohmanee744022010-09-16 22:08:32 +0000153 }
Eli Friedman7d58bc72011-06-15 00:47:34 +0000154 if (DepInst) {
155 OS << " from: ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000156 DepInst->print(OS);
Eli Friedman7d58bc72011-06-15 00:47:34 +0000157 }
Dan Gohmanee744022010-09-16 22:08:32 +0000158 OS << "\n";
159 }
160
161 Inst->print(OS);
162 OS << "\n\n";
163 }
164}