blob: 1cbacff02d2492757f92f6adf5690d110bb2c7fe [file] [log] [blame]
Dan Gohmanead01092010-09-16 22:08:32 +00001//===- 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/ADT/SetVector.h"
20using namespace llvm;
21
22namespace {
23 struct MemDepPrinter : public FunctionPass {
24 const Function *F;
25
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;
30 DepSetMap Deps;
31
32 static char ID; // Pass identifcation, replacement for typeid
33 MemDepPrinter() : FunctionPass(ID) {}
34
35 virtual bool runOnFunction(Function &F);
36
37 void print(raw_ostream &OS, const Module * = 0) const;
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<MemoryDependenceAnalysis>();
41 AU.setPreservesAll();
42 }
43
44 virtual void releaseMemory() {
45 Deps.clear();
46 F = 0;
47 }
48 };
49}
50
51char MemDepPrinter::ID = 0;
52INITIALIZE_PASS(MemDepPrinter, "print-memdeps", "Print MemDeps of function", false, true);
53
54FunctionPass *llvm::createMemDepPrinter() {
55 return new MemDepPrinter();
56}
57
58bool MemDepPrinter::runOnFunction(Function &F) {
59 this->F = &F;
60 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
61
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;
66
67 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
68 continue;
69
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(),
75 Res.isClobber()),
76 static_cast<BasicBlock *>(0)));
77 } else if (CallSite CS = cast<Value>(Inst)) {
78 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
79 MDA.getNonLocalCallDependency(CS);
80
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(),
88 Res.isClobber()),
89 I->getBB()));
90 }
91 } else {
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);
104 } else {
105 llvm_unreachable("Unknown memory instruction!");
106 }
107
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(),
115 Res.isClobber()),
116 I->getBB()));
117 }
118 }
119 }
120
121 return false;
122}
123
124void 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;
127
128 DepSetMap::const_iterator I = Deps.find(Inst);
129 if (I == Deps.end())
130 continue;
131
132 const DepSet &InstDeps = I->second;
133
134 for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
135 I != E; ++I) {
136 const Instruction *DepInst = I->first.getPointer();
137 bool isClobber = I->first.getInt();
138 const BasicBlock *DepBB = I->second;
139
140 OS << " " << (isClobber ? "Clobber" : " Def");
141 if (DepBB) {
142 OS << " in block ";
143 WriteAsOperand(OS, DepBB, /*PrintType=*/false, M);
144 }
145 OS << " from: ";
146 DepInst->print(OS);
147 OS << "\n";
148 }
149
150 Inst->print(OS);
151 OS << "\n\n";
152 }
153}