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