blob: 9242318f65ff3364f0ae8e0c23381396b8c8f50c [file] [log] [blame]
Dan Gohmanee744022010-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
Dan Gohmanee744022010-09-16 22:08:32 +000013#include "llvm/Analysis/Passes.h"
Dan Gohmanee744022010-09-16 22:08:32 +000014#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chandler Carruth83948572014-03-04 10:30:26 +000016#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/CallSite.h"
19#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanee744022010-09-16 22:08:32 +000021using namespace llvm;
22
23namespace {
24 struct MemDepPrinter : public FunctionPass {
25 const Function *F;
26
Eli Friedmanc1702c82011-10-13 22:14:57 +000027 enum DepType {
28 Clobber = 0,
29 Def,
30 NonFuncLocal,
31 Unknown
32 };
33
Craig Topper95207192012-05-24 06:35:32 +000034 static const char *const DepTypeStr[];
Eli Friedmanc1702c82011-10-13 22:14:57 +000035
36 typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
37 typedef std::pair<InstTypePair, const BasicBlock *> Dep;
Dan Gohmanee744022010-09-16 22:08:32 +000038 typedef SmallSetVector<Dep, 4> DepSet;
39 typedef DenseMap<const Instruction *, DepSet> DepSetMap;
40 DepSetMap Deps;
41
42 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 MemDepPrinter() : FunctionPass(ID) {
44 initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
45 }
Dan Gohmanee744022010-09-16 22:08:32 +000046
47 virtual bool runOnFunction(Function &F);
48
49 void print(raw_ostream &OS, const Module * = 0) const;
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman0a6021a2010-11-10 20:37:15 +000052 AU.addRequiredTransitive<AliasAnalysis>();
53 AU.addRequiredTransitive<MemoryDependenceAnalysis>();
Dan Gohmanee744022010-09-16 22:08:32 +000054 AU.setPreservesAll();
55 }
56
57 virtual void releaseMemory() {
58 Deps.clear();
59 F = 0;
60 }
Eli Friedmanc1702c82011-10-13 22:14:57 +000061
62 private:
63 static InstTypePair getInstTypePair(MemDepResult dep) {
64 if (dep.isClobber())
65 return InstTypePair(dep.getInst(), Clobber);
66 if (dep.isDef())
67 return InstTypePair(dep.getInst(), Def);
68 if (dep.isNonFuncLocal())
69 return InstTypePair(dep.getInst(), NonFuncLocal);
Eric Christopher67c0bfe2013-12-04 23:55:09 +000070 assert(dep.isUnknown() && "unexpected dependence type");
Eli Friedmanc1702c82011-10-13 22:14:57 +000071 return InstTypePair(dep.getInst(), Unknown);
72 }
73 static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
74 return InstTypePair(inst, type);
75 }
Dan Gohmanee744022010-09-16 22:08:32 +000076 };
77}
78
79char MemDepPrinter::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000080INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
81 "Print MemDeps of function", false, true)
82INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
83INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
84 "Print MemDeps of function", false, true)
Dan Gohmanee744022010-09-16 22:08:32 +000085
86FunctionPass *llvm::createMemDepPrinter() {
87 return new MemDepPrinter();
88}
89
Craig Topper95207192012-05-24 06:35:32 +000090const char *const MemDepPrinter::DepTypeStr[]
Eli Friedmanc1702c82011-10-13 22:14:57 +000091 = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
92
Dan Gohmanee744022010-09-16 22:08:32 +000093bool MemDepPrinter::runOnFunction(Function &F) {
94 this->F = &F;
Dan Gohman0a6021a2010-11-10 20:37:15 +000095 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Dan Gohmanee744022010-09-16 22:08:32 +000096 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
97
98 // All this code uses non-const interfaces because MemDep is not
99 // const-friendly, though nothing is actually modified.
100 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
101 Instruction *Inst = &*I;
102
103 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
104 continue;
105
106 MemDepResult Res = MDA.getDependency(Inst);
107 if (!Res.isNonLocal()) {
Eli Friedmanc1702c82011-10-13 22:14:57 +0000108 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
Dan Gohmanee744022010-09-16 22:08:32 +0000109 static_cast<BasicBlock *>(0)));
110 } else if (CallSite CS = cast<Value>(Inst)) {
111 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
112 MDA.getNonLocalCallDependency(CS);
113
114 DepSet &InstDeps = Deps[Inst];
115 for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
116 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
117 const MemDepResult &Res = I->getResult();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000118 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000119 }
120 } else {
121 SmallVector<NonLocalDepResult, 4> NLDI;
122 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000123 if (!LI->isUnordered()) {
124 // FIXME: Handle atomic/volatile loads.
Eli Friedmanc1702c82011-10-13 22:14:57 +0000125 Deps[Inst].insert(std::make_pair(getInstTypePair(0, Unknown),
Eli Friedman5494ada2011-08-15 20:54:19 +0000126 static_cast<BasicBlock *>(0)));
127 continue;
128 }
Dan Gohman65316d62010-11-11 21:50:19 +0000129 AliasAnalysis::Location Loc = AA.getLocation(LI);
Eli Friedman5494ada2011-08-15 20:54:19 +0000130 MDA.getNonLocalPointerDependency(Loc, true, LI->getParent(), NLDI);
Dan Gohmanee744022010-09-16 22:08:32 +0000131 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Eli Friedmanfdeaf252011-12-14 02:54:39 +0000132 if (!SI->isUnordered()) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000133 // FIXME: Handle atomic/volatile stores.
Eli Friedmanc1702c82011-10-13 22:14:57 +0000134 Deps[Inst].insert(std::make_pair(getInstTypePair(0, Unknown),
Eli Friedman5494ada2011-08-15 20:54:19 +0000135 static_cast<BasicBlock *>(0)));
136 continue;
137 }
Dan Gohman65316d62010-11-11 21:50:19 +0000138 AliasAnalysis::Location Loc = AA.getLocation(SI);
Dan Gohman0a6021a2010-11-10 20:37:15 +0000139 MDA.getNonLocalPointerDependency(Loc, false, SI->getParent(), NLDI);
Dan Gohmanee744022010-09-16 22:08:32 +0000140 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) {
Dan Gohman65316d62010-11-11 21:50:19 +0000141 AliasAnalysis::Location Loc = AA.getLocation(VI);
Dan Gohman0a6021a2010-11-10 20:37:15 +0000142 MDA.getNonLocalPointerDependency(Loc, false, VI->getParent(), NLDI);
Dan Gohmanee744022010-09-16 22:08:32 +0000143 } else {
144 llvm_unreachable("Unknown memory instruction!");
145 }
146
147 DepSet &InstDeps = Deps[Inst];
148 for (SmallVectorImpl<NonLocalDepResult>::const_iterator
149 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
150 const MemDepResult &Res = I->getResult();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000151 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
Dan Gohmanee744022010-09-16 22:08:32 +0000152 }
153 }
154 }
155
156 return false;
157}
158
159void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
160 for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
161 const Instruction *Inst = &*I;
162
Dan Gohmanf4925062010-09-16 22:50:09 +0000163 DepSetMap::const_iterator DI = Deps.find(Inst);
164 if (DI == Deps.end())
Dan Gohmanee744022010-09-16 22:08:32 +0000165 continue;
166
Dan Gohmanf4925062010-09-16 22:50:09 +0000167 const DepSet &InstDeps = DI->second;
Dan Gohmanee744022010-09-16 22:08:32 +0000168
169 for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
170 I != E; ++I) {
171 const Instruction *DepInst = I->first.getPointer();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000172 DepType type = I->first.getInt();
Dan Gohmanee744022010-09-16 22:08:32 +0000173 const BasicBlock *DepBB = I->second;
174
Eli Friedman7d58bc72011-06-15 00:47:34 +0000175 OS << " ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000176 OS << DepTypeStr[type];
Dan Gohmanee744022010-09-16 22:08:32 +0000177 if (DepBB) {
178 OS << " in block ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000179 DepBB->printAsOperand(OS, /*PrintType=*/false, M);
Dan Gohmanee744022010-09-16 22:08:32 +0000180 }
Eli Friedman7d58bc72011-06-15 00:47:34 +0000181 if (DepInst) {
182 OS << " from: ";
Eli Friedmanc1702c82011-10-13 22:14:57 +0000183 DepInst->print(OS);
Eli Friedman7d58bc72011-06-15 00:47:34 +0000184 }
Dan Gohmanee744022010-09-16 22:08:32 +0000185 OS << "\n";
186 }
187
188 Inst->print(OS);
189 OS << "\n\n";
190 }
191}