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 | |
| 28 | typedef PointerIntPair<const Instruction *, 1> InstAndClobberFlag; |
| 29 | typedef std::pair<InstAndClobberFlag, const BasicBlock *> Dep; |
| 30 | typedef SmallSetVector<Dep, 4> DepSet; |
| 31 | typedef DenseMap<const Instruction *, DepSet> DepSetMap; |
| 32 | DepSetMap Deps; |
| 33 | |
| 34 | static char ID; // Pass identifcation, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 35 | MemDepPrinter() : FunctionPass(ID) { |
| 36 | initializeMemDepPrinterPass(*PassRegistry::getPassRegistry()); |
| 37 | } |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 38 | |
| 39 | virtual bool runOnFunction(Function &F); |
| 40 | |
| 41 | void print(raw_ostream &OS, const Module * = 0) const; |
| 42 | |
| 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame^] | 44 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 45 | AU.addRequiredTransitive<MemoryDependenceAnalysis>(); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 46 | AU.setPreservesAll(); |
| 47 | } |
| 48 | |
| 49 | virtual void releaseMemory() { |
| 50 | Deps.clear(); |
| 51 | F = 0; |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | char MemDepPrinter::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 57 | INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps", |
| 58 | "Print MemDeps of function", false, true) |
| 59 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis) |
| 60 | INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps", |
| 61 | "Print MemDeps of function", false, true) |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 62 | |
| 63 | FunctionPass *llvm::createMemDepPrinter() { |
| 64 | return new MemDepPrinter(); |
| 65 | } |
| 66 | |
| 67 | bool MemDepPrinter::runOnFunction(Function &F) { |
| 68 | this->F = &F; |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame^] | 69 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 70 | MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>(); |
| 71 | |
| 72 | // All this code uses non-const interfaces because MemDep is not |
| 73 | // const-friendly, though nothing is actually modified. |
| 74 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { |
| 75 | Instruction *Inst = &*I; |
| 76 | |
| 77 | if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory()) |
| 78 | continue; |
| 79 | |
| 80 | MemDepResult Res = MDA.getDependency(Inst); |
| 81 | if (!Res.isNonLocal()) { |
| 82 | assert(Res.isClobber() != Res.isDef() && |
| 83 | "Local dep should be def or clobber!"); |
| 84 | Deps[Inst].insert(std::make_pair(InstAndClobberFlag(Res.getInst(), |
| 85 | Res.isClobber()), |
| 86 | static_cast<BasicBlock *>(0))); |
| 87 | } else if (CallSite CS = cast<Value>(Inst)) { |
| 88 | const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI = |
| 89 | MDA.getNonLocalCallDependency(CS); |
| 90 | |
| 91 | DepSet &InstDeps = Deps[Inst]; |
| 92 | for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator |
| 93 | I = NLDI.begin(), E = NLDI.end(); I != E; ++I) { |
| 94 | const MemDepResult &Res = I->getResult(); |
| 95 | assert(Res.isClobber() != Res.isDef() && |
| 96 | "Resolved non-local call dep should be def or clobber!"); |
| 97 | InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(), |
| 98 | Res.isClobber()), |
| 99 | I->getBB())); |
| 100 | } |
| 101 | } else { |
| 102 | SmallVector<NonLocalDepResult, 4> NLDI; |
| 103 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
| 104 | // FIXME: Volatile is not handled properly here. |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame^] | 105 | AliasAnalysis::Location Loc(LI->getPointerOperand(), |
| 106 | AA.getTypeStoreSize(LI->getType()), |
| 107 | LI->getMetadata(LLVMContext::MD_tbaa)); |
| 108 | MDA.getNonLocalPointerDependency(Loc, !LI->isVolatile(), |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 109 | LI->getParent(), NLDI); |
| 110 | } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 111 | // FIXME: Volatile is not handled properly here. |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame^] | 112 | AliasAnalysis::Location Loc(SI->getPointerOperand(), |
| 113 | AA.getTypeStoreSize(SI->getValueOperand() |
| 114 | ->getType()), |
| 115 | SI->getMetadata(LLVMContext::MD_tbaa)); |
| 116 | MDA.getNonLocalPointerDependency(Loc, false, SI->getParent(), NLDI); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 117 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) { |
Dan Gohman | 075fb5d | 2010-11-10 20:37:15 +0000 | [diff] [blame^] | 118 | AliasAnalysis::Location Loc(SI->getPointerOperand(), |
| 119 | AliasAnalysis::UnknownSize, |
| 120 | SI->getMetadata(LLVMContext::MD_tbaa)); |
| 121 | MDA.getNonLocalPointerDependency(Loc, false, VI->getParent(), NLDI); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 122 | } else { |
| 123 | llvm_unreachable("Unknown memory instruction!"); |
| 124 | } |
| 125 | |
| 126 | DepSet &InstDeps = Deps[Inst]; |
| 127 | for (SmallVectorImpl<NonLocalDepResult>::const_iterator |
| 128 | I = NLDI.begin(), E = NLDI.end(); I != E; ++I) { |
| 129 | const MemDepResult &Res = I->getResult(); |
| 130 | assert(Res.isClobber() != Res.isDef() && |
| 131 | "Resolved non-local pointer dep should be def or clobber!"); |
| 132 | InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(), |
| 133 | Res.isClobber()), |
| 134 | I->getBB())); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | void MemDepPrinter::print(raw_ostream &OS, const Module *M) const { |
| 143 | for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) { |
| 144 | const Instruction *Inst = &*I; |
| 145 | |
Dan Gohman | a627e9b | 2010-09-16 22:50:09 +0000 | [diff] [blame] | 146 | DepSetMap::const_iterator DI = Deps.find(Inst); |
| 147 | if (DI == Deps.end()) |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 148 | continue; |
| 149 | |
Dan Gohman | a627e9b | 2010-09-16 22:50:09 +0000 | [diff] [blame] | 150 | const DepSet &InstDeps = DI->second; |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 151 | |
| 152 | for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end(); |
| 153 | I != E; ++I) { |
| 154 | const Instruction *DepInst = I->first.getPointer(); |
| 155 | bool isClobber = I->first.getInt(); |
| 156 | const BasicBlock *DepBB = I->second; |
| 157 | |
| 158 | OS << " " << (isClobber ? "Clobber" : " Def"); |
| 159 | if (DepBB) { |
| 160 | OS << " in block "; |
| 161 | WriteAsOperand(OS, DepBB, /*PrintType=*/false, M); |
| 162 | } |
| 163 | OS << " from: "; |
Dan Gohman | adb30d2 | 2010-10-20 22:37:41 +0000 | [diff] [blame] | 164 | if (DepInst == Inst) |
| 165 | OS << "<unspecified>"; |
| 166 | else |
| 167 | DepInst->print(OS); |
Dan Gohman | ead0109 | 2010-09-16 22:08:32 +0000 | [diff] [blame] | 168 | OS << "\n"; |
| 169 | } |
| 170 | |
| 171 | Inst->print(OS); |
| 172 | OS << "\n\n"; |
| 173 | } |
| 174 | } |