Now with fewer extraneous semicolons!
[oota-llvm.git] / lib / Analysis / MemDepPrinter.cpp
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"
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/Support/raw_ostream.h"
20 #include "llvm/ADT/SetVector.h"
21 using namespace llvm;
22
23 namespace {
24   struct MemDepPrinter : public FunctionPass {
25     const Function *F;
26
27     typedef PointerIntPair<const Instruction *, 1> InstAndClobberFlag;
28     typedef std::pair<InstAndClobberFlag, const BasicBlock *> Dep;
29     typedef SmallSetVector<Dep, 4> DepSet;
30     typedef DenseMap<const Instruction *, DepSet> DepSetMap;
31     DepSetMap Deps;
32
33     static char ID; // Pass identifcation, replacement for typeid
34     MemDepPrinter() : FunctionPass(ID) {}
35
36     virtual bool runOnFunction(Function &F);
37
38     void print(raw_ostream &OS, const Module * = 0) const;
39
40     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41       AU.addRequired<MemoryDependenceAnalysis>();
42       AU.setPreservesAll();
43     }
44
45     virtual void releaseMemory() {
46       Deps.clear();
47       F = 0;
48     }
49   };
50 }
51
52 char MemDepPrinter::ID = 0;
53 INITIALIZE_PASS(MemDepPrinter, "print-memdeps", "Print MemDeps of function", 
54                 false, true)
55
56 FunctionPass *llvm::createMemDepPrinter() {
57   return new MemDepPrinter();
58 }
59
60 bool MemDepPrinter::runOnFunction(Function &F) {
61   this->F = &F;
62   MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
63
64   // All this code uses non-const interfaces because MemDep is not
65   // const-friendly, though nothing is actually modified.
66   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
67     Instruction *Inst = &*I;
68
69     if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
70       continue;
71
72     MemDepResult Res = MDA.getDependency(Inst);
73     if (!Res.isNonLocal()) {
74       assert(Res.isClobber() != Res.isDef() &&
75              "Local dep should be def or clobber!");
76       Deps[Inst].insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
77                                                           Res.isClobber()),
78                                        static_cast<BasicBlock *>(0)));
79     } else if (CallSite CS = cast<Value>(Inst)) {
80       const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
81         MDA.getNonLocalCallDependency(CS);
82
83       DepSet &InstDeps = Deps[Inst];
84       for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
85            I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
86         const MemDepResult &Res = I->getResult();
87         assert(Res.isClobber() != Res.isDef() &&
88                "Resolved non-local call dep should be def or clobber!");
89         InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
90                                                           Res.isClobber()),
91                                        I->getBB()));
92       }
93     } else {
94       SmallVector<NonLocalDepResult, 4> NLDI;
95       if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
96         // FIXME: Volatile is not handled properly here.
97         MDA.getNonLocalPointerDependency(LI->getPointerOperand(), !LI->isVolatile(),
98                                          LI->getParent(), NLDI);
99       } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
100         // FIXME: Volatile is not handled properly here.
101         MDA.getNonLocalPointerDependency(SI->getPointerOperand(), false,
102                                          SI->getParent(), NLDI);
103       } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) {
104         MDA.getNonLocalPointerDependency(VI->getPointerOperand(), false,
105                                          VI->getParent(), NLDI);
106       } else {
107         llvm_unreachable("Unknown memory instruction!");
108       }
109
110       DepSet &InstDeps = Deps[Inst];
111       for (SmallVectorImpl<NonLocalDepResult>::const_iterator
112            I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
113         const MemDepResult &Res = I->getResult();
114         assert(Res.isClobber() != Res.isDef() &&
115                "Resolved non-local pointer dep should be def or clobber!");
116         InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
117                                                           Res.isClobber()),
118                                        I->getBB()));
119       }
120     }
121   }
122
123   return false;
124 }
125
126 void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
127   for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
128     const Instruction *Inst = &*I;
129
130     DepSetMap::const_iterator DI = Deps.find(Inst);
131     if (DI == Deps.end())
132       continue;
133
134     const DepSet &InstDeps = DI->second;
135
136     for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
137          I != E; ++I) {
138       const Instruction *DepInst = I->first.getPointer();
139       bool isClobber = I->first.getInt();
140       const BasicBlock *DepBB = I->second;
141
142       OS << "    " << (isClobber ? "Clobber" : "    Def");
143       if (DepBB) {
144         OS << " in block ";
145         WriteAsOperand(OS, DepBB, /*PrintType=*/false, M);
146       }
147       OS << " from: ";
148       DepInst->print(OS);
149       OS << "\n";
150     }
151
152     Inst->print(OS);
153     OS << "\n\n";
154   }
155 }