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/Support/raw_ostream.h"
20 #include "llvm/ADT/SetVector.h"
24 struct MemDepPrinter : public FunctionPass {
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;
33 static char ID; // Pass identifcation, replacement for typeid
34 MemDepPrinter() : FunctionPass(ID) {}
36 virtual bool runOnFunction(Function &F);
38 void print(raw_ostream &OS, const Module * = 0) const;
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.addRequired<MemoryDependenceAnalysis>();
45 virtual void releaseMemory() {
52 char MemDepPrinter::ID = 0;
53 INITIALIZE_PASS(MemDepPrinter, "print-memdeps", "Print MemDeps of function", false, true);
55 FunctionPass *llvm::createMemDepPrinter() {
56 return new MemDepPrinter();
59 bool MemDepPrinter::runOnFunction(Function &F) {
61 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
63 // All this code uses non-const interfaces because MemDep is not
64 // const-friendly, though nothing is actually modified.
65 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
66 Instruction *Inst = &*I;
68 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
71 MemDepResult Res = MDA.getDependency(Inst);
72 if (!Res.isNonLocal()) {
73 assert(Res.isClobber() != Res.isDef() &&
74 "Local dep should be def or clobber!");
75 Deps[Inst].insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
77 static_cast<BasicBlock *>(0)));
78 } else if (CallSite CS = cast<Value>(Inst)) {
79 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
80 MDA.getNonLocalCallDependency(CS);
82 DepSet &InstDeps = Deps[Inst];
83 for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
84 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
85 const MemDepResult &Res = I->getResult();
86 assert(Res.isClobber() != Res.isDef() &&
87 "Resolved non-local call dep should be def or clobber!");
88 InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
93 SmallVector<NonLocalDepResult, 4> NLDI;
94 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
95 // FIXME: Volatile is not handled properly here.
96 MDA.getNonLocalPointerDependency(LI->getPointerOperand(), !LI->isVolatile(),
97 LI->getParent(), NLDI);
98 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
99 // FIXME: Volatile is not handled properly here.
100 MDA.getNonLocalPointerDependency(SI->getPointerOperand(), false,
101 SI->getParent(), NLDI);
102 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) {
103 MDA.getNonLocalPointerDependency(VI->getPointerOperand(), false,
104 VI->getParent(), NLDI);
106 llvm_unreachable("Unknown memory instruction!");
109 DepSet &InstDeps = Deps[Inst];
110 for (SmallVectorImpl<NonLocalDepResult>::const_iterator
111 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
112 const MemDepResult &Res = I->getResult();
113 assert(Res.isClobber() != Res.isDef() &&
114 "Resolved non-local pointer dep should be def or clobber!");
115 InstDeps.insert(std::make_pair(InstAndClobberFlag(Res.getInst(),
125 void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
126 for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
127 const Instruction *Inst = &*I;
129 DepSetMap::const_iterator DI = Deps.find(Inst);
130 if (DI == Deps.end())
133 const DepSet &InstDeps = DI->second;
135 for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
137 const Instruction *DepInst = I->first.getPointer();
138 bool isClobber = I->first.getInt();
139 const BasicBlock *DepBB = I->second;
141 OS << " " << (isClobber ? "Clobber" : " Def");
144 WriteAsOperand(OS, DepBB, /*PrintType=*/false, M);