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 | |
| 13 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 14 | #include "llvm/LLVMContext.h" |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
| 16 | #include "llvm/Assembly/Writer.h" |
| 17 | #include "llvm/Support/CallSite.h" |
| 18 | #include "llvm/Support/InstIterator.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 8945db7 | 2010-09-17 00:33:43 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | struct MemDepPrinter : public FunctionPass { |
| 26 | const Function *F; |
| 27 | |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 28 | enum DepType { |
| 29 | Clobber = 0, |
| 30 | Def, |
| 31 | NonFuncLocal, |
| 32 | Unknown |
| 33 | }; |
| 34 | |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 35 | static const char *const DepTypeStr[]; |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 36 | |
| 37 | typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair; |
| 38 | typedef std::pair<InstTypePair, const BasicBlock *> Dep; |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 39 | typedef SmallSetVector<Dep, 4> DepSet; |
| 40 | typedef DenseMap<const Instruction *, DepSet> DepSetMap; |
| 41 | DepSetMap Deps; |
| 42 | |
| 43 | static char ID; // Pass identifcation, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 44 | MemDepPrinter() : FunctionPass(ID) { |
| 45 | initializeMemDepPrinterPass(*PassRegistry::getPassRegistry()); |
| 46 | } |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 47 | |
| 48 | virtual bool runOnFunction(Function &F); |
| 49 | |
| 50 | void print(raw_ostream &OS, const Module * = 0) const; |
| 51 | |
| 52 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 53 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 54 | AU.addRequiredTransitive<MemoryDependenceAnalysis>(); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 55 | AU.setPreservesAll(); |
| 56 | } |
| 57 | |
| 58 | virtual void releaseMemory() { |
| 59 | Deps.clear(); |
| 60 | F = 0; |
| 61 | } |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | static InstTypePair getInstTypePair(MemDepResult dep) { |
| 65 | if (dep.isClobber()) |
| 66 | return InstTypePair(dep.getInst(), Clobber); |
| 67 | if (dep.isDef()) |
| 68 | return InstTypePair(dep.getInst(), Def); |
| 69 | if (dep.isNonFuncLocal()) |
| 70 | return InstTypePair(dep.getInst(), NonFuncLocal); |
| 71 | assert(dep.isUnknown() && "unexptected dependence type"); |
| 72 | return InstTypePair(dep.getInst(), Unknown); |
| 73 | } |
| 74 | static InstTypePair getInstTypePair(const Instruction* inst, DepType type) { |
| 75 | return InstTypePair(inst, type); |
| 76 | } |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 77 | }; |
| 78 | } |
| 79 | |
| 80 | char MemDepPrinter::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 81 | INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps", |
| 82 | "Print MemDeps of function", false, true) |
| 83 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis) |
| 84 | INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps", |
| 85 | "Print MemDeps of function", false, true) |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 86 | |
| 87 | FunctionPass *llvm::createMemDepPrinter() { |
| 88 | return new MemDepPrinter(); |
| 89 | } |
| 90 | |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 91 | const char *const MemDepPrinter::DepTypeStr[] |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 92 | = {"Clobber", "Def", "NonFuncLocal", "Unknown"}; |
| 93 | |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 94 | bool MemDepPrinter::runOnFunction(Function &F) { |
| 95 | this->F = &F; |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 96 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 97 | MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>(); |
| 98 | |
| 99 | // All this code uses non-const interfaces because MemDep is not |
| 100 | // const-friendly, though nothing is actually modified. |
| 101 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 102 | Instruction *Inst = &*I; |
| 103 | |
| 104 | if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory()) |
| 105 | continue; |
| 106 | |
| 107 | MemDepResult Res = MDA.getDependency(Inst); |
| 108 | if (!Res.isNonLocal()) { |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 109 | Deps[Inst].insert(std::make_pair(getInstTypePair(Res), |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 110 | static_cast<BasicBlock *>(0))); |
| 111 | } else if (CallSite CS = cast<Value>(Inst)) { |
| 112 | const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI = |
| 113 | MDA.getNonLocalCallDependency(CS); |
| 114 | |
| 115 | DepSet &InstDeps = Deps[Inst]; |
| 116 | for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator |
| 117 | I = NLDI.begin(), E = NLDI.end(); I != E; ++I) { |
| 118 | const MemDepResult &Res = I->getResult(); |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 119 | InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB())); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 120 | } |
| 121 | } else { |
| 122 | SmallVector<NonLocalDepResult, 4> NLDI; |
| 123 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 124 | if (!LI->isUnordered()) { |
| 125 | // FIXME: Handle atomic/volatile loads. |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 126 | Deps[Inst].insert(std::make_pair(getInstTypePair(0, Unknown), |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 127 | static_cast<BasicBlock *>(0))); |
| 128 | continue; |
| 129 | } |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 130 | AliasAnalysis::Location Loc = AA.getLocation(LI); |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 131 | MDA.getNonLocalPointerDependency(Loc, true, LI->getParent(), NLDI); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 132 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Eli Friedman | e610982 | 2011-12-14 02:54:39 +0000 | [diff] [blame] | 133 | if (!SI->isUnordered()) { |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 134 | // FIXME: Handle atomic/volatile stores. |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 135 | Deps[Inst].insert(std::make_pair(getInstTypePair(0, Unknown), |
Eli Friedman | 667ccf2 | 2011-08-15 20:54:19 +0000 | [diff] [blame] | 136 | static_cast<BasicBlock *>(0))); |
| 137 | continue; |
| 138 | } |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 139 | AliasAnalysis::Location Loc = AA.getLocation(SI); |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 140 | MDA.getNonLocalPointerDependency(Loc, false, SI->getParent(), NLDI); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 141 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) { |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 142 | AliasAnalysis::Location Loc = AA.getLocation(VI); |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame] | 143 | MDA.getNonLocalPointerDependency(Loc, false, VI->getParent(), NLDI); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 144 | } else { |
| 145 | llvm_unreachable("Unknown memory instruction!"); |
| 146 | } |
| 147 | |
| 148 | DepSet &InstDeps = Deps[Inst]; |
| 149 | for (SmallVectorImpl<NonLocalDepResult>::const_iterator |
| 150 | I = NLDI.begin(), E = NLDI.end(); I != E; ++I) { |
| 151 | const MemDepResult &Res = I->getResult(); |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 152 | InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB())); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | void MemDepPrinter::print(raw_ostream &OS, const Module *M) const { |
| 161 | for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) { |
| 162 | const Instruction *Inst = &*I; |
| 163 | |
Dan Gohman | a627e9b | 2010-09-16 22:50:09 +0000 | [diff] [blame] | 164 | DepSetMap::const_iterator DI = Deps.find(Inst); |
| 165 | if (DI == Deps.end()) |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 166 | continue; |
| 167 | |
Dan Gohman | a627e9b | 2010-09-16 22:50:09 +0000 | [diff] [blame] | 168 | const DepSet &InstDeps = DI->second; |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 169 | |
| 170 | for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end(); |
| 171 | I != E; ++I) { |
| 172 | const Instruction *DepInst = I->first.getPointer(); |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 173 | DepType type = I->first.getInt(); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 174 | const BasicBlock *DepBB = I->second; |
| 175 | |
Eli Friedman | a990e07 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 176 | OS << " "; |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 177 | OS << DepTypeStr[type]; |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 178 | if (DepBB) { |
| 179 | OS << " in block "; |
| 180 | WriteAsOperand(OS, DepBB, /*PrintType=*/false, M); |
| 181 | } |
Eli Friedman | a990e07 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 182 | if (DepInst) { |
| 183 | OS << " from: "; |
Eli Friedman | b414142 | 2011-10-13 22:14:57 +0000 | [diff] [blame] | 184 | DepInst->print(OS); |
Eli Friedman | a990e07 | 2011-06-15 00:47:34 +0000 | [diff] [blame] | 185 | } |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 186 | OS << "\n"; |
| 187 | } |
| 188 | |
| 189 | Inst->print(OS); |
| 190 | OS << "\n\n"; |
| 191 | } |
| 192 | } |