1 //===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Assembly/Writer.h"
16 #include "llvm/Support/CallSite.h"
17 #include "llvm/Support/InstIterator.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/ADT/SetVector.h"
23 struct MemDepPrinter : public FunctionPass {
26 typedef PointerIntPair<const Instruction *, 1> InstAndClobberFlag;
27 typedef std::pair<InstAndClobberFlag, const BasicBlock *> Dep;
28 typedef SmallSetVector<Dep, 4> DepSet;
29 typedef DenseMap<const Instruction *, DepSet> DepSetMap;
32 static char ID; // Pass identifcation, replacement for typeid
33 MemDepPrinter() : FunctionPass(ID) {}
35 virtual bool runOnFunction(Function &F);
37 void print(raw_ostream &OS, const Module * = 0) const;
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<MemoryDependenceAnalysis>();
44 virtual void releaseMemory() {
51 char MemDepPrinter::ID = 0;
52 INITIALIZE_PASS(MemDepPrinter, "print-memdeps", "Print MemDeps of function", false, true);
54 FunctionPass *llvm::createMemDepPrinter() {
55 return new MemDepPrinter();
58 bool MemDepPrinter::runOnFunction(Function &F) {
60 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
62 // All this code uses non-const interfaces because MemDep is not
63 // const-friendly, though nothing is actually modified.
64 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
65 Instruction *Inst = &*I;
67 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
70 MemDepResult Res = MDA.getDependency(Inst);
71 if (!Res.isNonLocal()) {
72 assert(Res.isClobber() != Res.isDef() &&
73 "Local dep should be def or clobber!");
74 Deps[Inst].insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
76 static_cast<BasicBlock *>(0)));
77 } else if (CallSite CS = cast<Value>(Inst)) {
78 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
79 MDA.getNonLocalCallDependency(CS);
81 DepSet &InstDeps = Deps[Inst];
82 for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
83 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
84 const MemDepResult &Res = I->getResult();
85 assert(Res.isClobber() != Res.isDef() &&
86 "Resolved non-local call dep should be def or clobber!");
87 InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
92 SmallVector<NonLocalDepResult, 4> NLDI;
93 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
94 // FIXME: Volatile is not handled properly here.
95 MDA.getNonLocalPointerDependency(LI->getPointerOperand(), !LI->isVolatile(),
96 LI->getParent(), NLDI);
97 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
98 // FIXME: Volatile is not handled properly here.
99 MDA.getNonLocalPointerDependency(SI->getPointerOperand(), false,
100 SI->getParent(), NLDI);
101 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) {
102 MDA.getNonLocalPointerDependency(VI->getPointerOperand(), false,
103 VI->getParent(), NLDI);
105 llvm_unreachable("Unknown memory instruction!");
108 DepSet &InstDeps = Deps[Inst];
109 for (SmallVectorImpl<NonLocalDepResult>::const_iterator
110 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
111 const MemDepResult &Res = I->getResult();
112 assert(Res.isClobber() != Res.isDef() &&
113 "Resolved non-local pointer dep should be def or clobber!");
114 InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
124 void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
125 for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
126 const Instruction *Inst = &*I;
128 DepSetMap::const_iterator DI = Deps.find(Inst);
129 if (DI == Deps.end())
132 const DepSet &InstDeps = DI->second;
134 for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
136 const Instruction *DepInst = I->first.getPointer();
137 bool isClobber = I->first.getInt();
138 const BasicBlock *DepBB = I->second;
140 OS << " " << (isClobber ? "Clobber" : " Def");
143 WriteAsOperand(OS, DepBB, /*PrintType=*/false, M);