Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 1 | //===- 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 Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/Passes.h" |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 16 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 17 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | struct MemDepPrinter : public FunctionPass { |
| 25 | const Function *F; |
| 26 | |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 27 | enum DepType { |
| 28 | Clobber = 0, |
| 29 | Def, |
| 30 | NonFuncLocal, |
| 31 | Unknown |
| 32 | }; |
| 33 | |
Craig Topper | 9520719 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 34 | static const char *const DepTypeStr[]; |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 35 | |
| 36 | typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair; |
| 37 | typedef std::pair<InstTypePair, const BasicBlock *> Dep; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 38 | 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 Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 43 | MemDepPrinter() : FunctionPass(ID) { |
| 44 | initializeMemDepPrinterPass(*PassRegistry::getPassRegistry()); |
| 45 | } |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 46 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 47 | bool runOnFunction(Function &F) override; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 48 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 49 | void print(raw_ostream &OS, const Module * = nullptr) const override; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 50 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 51 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 52 | AU.addRequiredTransitive<AAResultsWrapperPass>(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 53 | AU.addRequiredTransitive<MemoryDependenceWrapperPass>(); |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 54 | AU.setPreservesAll(); |
| 55 | } |
| 56 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 57 | void releaseMemory() override { |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 58 | Deps.clear(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 59 | F = nullptr; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 60 | } |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 61 | |
| 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 Christopher | 67c0bfe | 2013-12-04 23:55:09 +0000 | [diff] [blame] | 70 | assert(dep.isUnknown() && "unexpected dependence type"); |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 71 | return InstTypePair(dep.getInst(), Unknown); |
| 72 | } |
| 73 | static InstTypePair getInstTypePair(const Instruction* inst, DepType type) { |
| 74 | return InstTypePair(inst, type); |
| 75 | } |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 76 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 77 | } |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 78 | |
| 79 | char MemDepPrinter::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 80 | INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps", |
| 81 | "Print MemDeps of function", false, true) |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 82 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 83 | INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps", |
| 84 | "Print MemDeps of function", false, true) |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 85 | |
| 86 | FunctionPass *llvm::createMemDepPrinter() { |
| 87 | return new MemDepPrinter(); |
| 88 | } |
| 89 | |
Craig Topper | 9520719 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 90 | const char *const MemDepPrinter::DepTypeStr[] |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 91 | = {"Clobber", "Def", "NonFuncLocal", "Unknown"}; |
| 92 | |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 93 | bool MemDepPrinter::runOnFunction(Function &F) { |
| 94 | this->F = &F; |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 95 | MemoryDependenceResults &MDA = getAnalysis<MemoryDependenceWrapperPass>().getMemDep(); |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 96 | |
| 97 | // All this code uses non-const interfaces because MemDep is not |
| 98 | // const-friendly, though nothing is actually modified. |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 99 | for (auto &I : instructions(F)) { |
Ramkumar Ramachandra | 010b77c | 2015-02-09 19:49:54 +0000 | [diff] [blame] | 100 | Instruction *Inst = &I; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 101 | |
| 102 | if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory()) |
| 103 | continue; |
| 104 | |
| 105 | MemDepResult Res = MDA.getDependency(Inst); |
| 106 | if (!Res.isNonLocal()) { |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 107 | Deps[Inst].insert(std::make_pair(getInstTypePair(Res), |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 108 | static_cast<BasicBlock *>(nullptr))); |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 109 | } else if (auto CS = CallSite(Inst)) { |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 110 | const MemoryDependenceResults::NonLocalDepInfo &NLDI = |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 111 | MDA.getNonLocalCallDependency(CS); |
| 112 | |
| 113 | DepSet &InstDeps = Deps[Inst]; |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 114 | for (const NonLocalDepEntry &I : NLDI) { |
| 115 | const MemDepResult &Res = I.getResult(); |
| 116 | InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB())); |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 117 | } |
| 118 | } else { |
| 119 | SmallVector<NonLocalDepResult, 4> NLDI; |
Philip Reames | 33d7f9d | 2015-01-09 00:26:45 +0000 | [diff] [blame] | 120 | assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) || |
| 121 | isa<VAArgInst>(Inst)) && "Unknown memory instruction!"); |
| 122 | MDA.getNonLocalPointerDependency(Inst, NLDI); |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 123 | |
| 124 | DepSet &InstDeps = Deps[Inst]; |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 125 | for (const NonLocalDepResult &I : NLDI) { |
| 126 | const MemDepResult &Res = I.getResult(); |
| 127 | InstDeps.insert(std::make_pair(getInstTypePair(Res), I.getBB())); |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | void MemDepPrinter::print(raw_ostream &OS, const Module *M) const { |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 136 | for (const auto &I : instructions(*F)) { |
Ramkumar Ramachandra | 010b77c | 2015-02-09 19:49:54 +0000 | [diff] [blame] | 137 | const Instruction *Inst = &I; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 138 | |
Dan Gohman | f492506 | 2010-09-16 22:50:09 +0000 | [diff] [blame] | 139 | DepSetMap::const_iterator DI = Deps.find(Inst); |
| 140 | if (DI == Deps.end()) |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 141 | continue; |
| 142 | |
Dan Gohman | f492506 | 2010-09-16 22:50:09 +0000 | [diff] [blame] | 143 | const DepSet &InstDeps = DI->second; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 144 | |
Ramkumar Ramachandra | f8ea847 | 2015-02-25 23:55:00 +0000 | [diff] [blame] | 145 | for (const auto &I : InstDeps) { |
Ramkumar Ramachandra | 010b77c | 2015-02-09 19:49:54 +0000 | [diff] [blame] | 146 | const Instruction *DepInst = I.first.getPointer(); |
| 147 | DepType type = I.first.getInt(); |
| 148 | const BasicBlock *DepBB = I.second; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 149 | |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 150 | OS << " "; |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 151 | OS << DepTypeStr[type]; |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 152 | if (DepBB) { |
| 153 | OS << " in block "; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 154 | DepBB->printAsOperand(OS, /*PrintType=*/false, M); |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 155 | } |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 156 | if (DepInst) { |
| 157 | OS << " from: "; |
Eli Friedman | c1702c8 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 158 | DepInst->print(OS); |
Eli Friedman | 7d58bc7 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 159 | } |
Dan Gohman | ee74402 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 160 | OS << "\n"; |
| 161 | } |
| 162 | |
| 163 | Inst->print(OS); |
| 164 | OS << "\n\n"; |
| 165 | } |
| 166 | } |