blob: 8578a63bee1fefab148091b15a48763e5cc3b81f [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"
Dan Gohman075fb5d2010-11-10 20:37:15 +000014#include "llvm/LLVMContext.h"
Dan Gohmanead01092010-09-16 22:08:32 +000015#include "llvm/Analysis/Passes.h"
16#include "llvm/Assembly/Writer.h"
17#include "llvm/Support/CallSite.h"
18#include "llvm/Support/InstIterator.h"
19#include "llvm/Support/ErrorHandling.h"
Dan Gohman8945db72010-09-17 00:33:43 +000020#include "llvm/Support/raw_ostream.h"
Dan Gohmanead01092010-09-16 22:08:32 +000021#include "llvm/ADT/SetVector.h"
22using namespace llvm;
23
24namespace {
25 struct MemDepPrinter : public FunctionPass {
26 const Function *F;
27
Eli Friedmanb4141422011-10-13 22:14:57 +000028 enum DepType {
29 Clobber = 0,
30 Def,
31 NonFuncLocal,
32 Unknown
33 };
34
Craig Toppere3298102012-05-24 06:35:32 +000035 static const char *const DepTypeStr[];
Eli Friedmanb4141422011-10-13 22:14:57 +000036
37 typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
38 typedef std::pair<InstTypePair, const BasicBlock *> Dep;
Dan Gohmanead01092010-09-16 22:08:32 +000039 typedef SmallSetVector<Dep, 4> DepSet;
40 typedef DenseMap<const Instruction *, DepSet> DepSetMap;
41 DepSetMap Deps;
42
43 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000044 MemDepPrinter() : FunctionPass(ID) {
45 initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
46 }
Dan Gohmanead01092010-09-16 22:08:32 +000047
48 virtual bool runOnFunction(Function &F);
49
50 void print(raw_ostream &OS, const Module * = 0) const;
51
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman075fb5d2010-11-10 20:37:15 +000053 AU.addRequiredTransitive<AliasAnalysis>();
54 AU.addRequiredTransitive<MemoryDependenceAnalysis>();
Dan Gohmanead01092010-09-16 22:08:32 +000055 AU.setPreservesAll();
56 }
57
58 virtual void releaseMemory() {
59 Deps.clear();
60 F = 0;
61 }
Eli Friedmanb4141422011-10-13 22:14:57 +000062
63 private:
64 static InstTypePair getInstTypePair(MemDepResult dep) {
65 if (dep.isClobber())
66 return InstTypePair(dep.getInst(), Clobber);
67 if (dep.isDef())
68 return InstTypePair(dep.getInst(), Def);
69 if (dep.isNonFuncLocal())
70 return InstTypePair(dep.getInst(), NonFuncLocal);
71 assert(dep.isUnknown() && "unexptected dependence type");
72 return InstTypePair(dep.getInst(), Unknown);
73 }
74 static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
75 return InstTypePair(inst, type);
76 }
Dan Gohmanead01092010-09-16 22:08:32 +000077 };
78}
79
80char MemDepPrinter::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000081INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
82 "Print MemDeps of function", false, true)
83INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
84INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
85 "Print MemDeps of function", false, true)
Dan Gohmanead01092010-09-16 22:08:32 +000086
87FunctionPass *llvm::createMemDepPrinter() {
88 return new MemDepPrinter();
89}
90
Craig Toppere3298102012-05-24 06:35:32 +000091const char *const MemDepPrinter::DepTypeStr[]
Eli Friedmanb4141422011-10-13 22:14:57 +000092 = {"Clobber", "Def", "NonFuncLocal", "Unknown"};
93
Dan Gohmanead01092010-09-16 22:08:32 +000094bool MemDepPrinter::runOnFunction(Function &F) {
95 this->F = &F;
Dan Gohman075fb5d2010-11-10 20:37:15 +000096 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
Dan Gohmanead01092010-09-16 22:08:32 +000097 MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
98
99 // All this code uses non-const interfaces because MemDep is not
100 // const-friendly, though nothing is actually modified.
101 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
102 Instruction *Inst = &*I;
103
104 if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
105 continue;
106
107 MemDepResult Res = MDA.getDependency(Inst);
108 if (!Res.isNonLocal()) {
Eli Friedmanb4141422011-10-13 22:14:57 +0000109 Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
Dan Gohmanead01092010-09-16 22:08:32 +0000110 static_cast<BasicBlock *>(0)));
111 } else if (CallSite CS = cast<Value>(Inst)) {
112 const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
113 MDA.getNonLocalCallDependency(CS);
114
115 DepSet &InstDeps = Deps[Inst];
116 for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
117 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
118 const MemDepResult &Res = I->getResult();
Eli Friedmanb4141422011-10-13 22:14:57 +0000119 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
Dan Gohmanead01092010-09-16 22:08:32 +0000120 }
121 } else {
122 SmallVector<NonLocalDepResult, 4> NLDI;
123 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Eli Friedman667ccf22011-08-15 20:54:19 +0000124 if (!LI->isUnordered()) {
125 // FIXME: Handle atomic/volatile loads.
Eli Friedmanb4141422011-10-13 22:14:57 +0000126 Deps[Inst].insert(std::make_pair(getInstTypePair(0, Unknown),
Eli Friedman667ccf22011-08-15 20:54:19 +0000127 static_cast<BasicBlock *>(0)));
128 continue;
129 }
Dan Gohman6d8eb152010-11-11 21:50:19 +0000130 AliasAnalysis::Location Loc = AA.getLocation(LI);
Eli Friedman667ccf22011-08-15 20:54:19 +0000131 MDA.getNonLocalPointerDependency(Loc, true, LI->getParent(), NLDI);
Dan Gohmanead01092010-09-16 22:08:32 +0000132 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Eli Friedmane6109822011-12-14 02:54:39 +0000133 if (!SI->isUnordered()) {
Eli Friedman667ccf22011-08-15 20:54:19 +0000134 // FIXME: Handle atomic/volatile stores.
Eli Friedmanb4141422011-10-13 22:14:57 +0000135 Deps[Inst].insert(std::make_pair(getInstTypePair(0, Unknown),
Eli Friedman667ccf22011-08-15 20:54:19 +0000136 static_cast<BasicBlock *>(0)));
137 continue;
138 }
Dan Gohman6d8eb152010-11-11 21:50:19 +0000139 AliasAnalysis::Location Loc = AA.getLocation(SI);
Dan Gohman075fb5d2010-11-10 20:37:15 +0000140 MDA.getNonLocalPointerDependency(Loc, false, SI->getParent(), NLDI);
Dan Gohmanead01092010-09-16 22:08:32 +0000141 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(Inst)) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000142 AliasAnalysis::Location Loc = AA.getLocation(VI);
Dan Gohman075fb5d2010-11-10 20:37:15 +0000143 MDA.getNonLocalPointerDependency(Loc, false, VI->getParent(), NLDI);
Dan Gohmanead01092010-09-16 22:08:32 +0000144 } else {
145 llvm_unreachable("Unknown memory instruction!");
146 }
147
148 DepSet &InstDeps = Deps[Inst];
149 for (SmallVectorImpl<NonLocalDepResult>::const_iterator
150 I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
151 const MemDepResult &Res = I->getResult();
Eli Friedmanb4141422011-10-13 22:14:57 +0000152 InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
Dan Gohmanead01092010-09-16 22:08:32 +0000153 }
154 }
155 }
156
157 return false;
158}
159
160void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
161 for (const_inst_iterator I = inst_begin(*F), E = inst_end(*F); I != E; ++I) {
162 const Instruction *Inst = &*I;
163
Dan Gohmana627e9b2010-09-16 22:50:09 +0000164 DepSetMap::const_iterator DI = Deps.find(Inst);
165 if (DI == Deps.end())
Dan Gohmanead01092010-09-16 22:08:32 +0000166 continue;
167
Dan Gohmana627e9b2010-09-16 22:50:09 +0000168 const DepSet &InstDeps = DI->second;
Dan Gohmanead01092010-09-16 22:08:32 +0000169
170 for (DepSet::const_iterator I = InstDeps.begin(), E = InstDeps.end();
171 I != E; ++I) {
172 const Instruction *DepInst = I->first.getPointer();
Eli Friedmanb4141422011-10-13 22:14:57 +0000173 DepType type = I->first.getInt();
Dan Gohmanead01092010-09-16 22:08:32 +0000174 const BasicBlock *DepBB = I->second;
175
Eli Friedmana990e072011-06-15 00:47:34 +0000176 OS << " ";
Eli Friedmanb4141422011-10-13 22:14:57 +0000177 OS << DepTypeStr[type];
Dan Gohmanead01092010-09-16 22:08:32 +0000178 if (DepBB) {
179 OS << " in block ";
180 WriteAsOperand(OS, DepBB, /*PrintType=*/false, M);
181 }
Eli Friedmana990e072011-06-15 00:47:34 +0000182 if (DepInst) {
183 OS << " from: ";
Eli Friedmanb4141422011-10-13 22:14:57 +0000184 DepInst->print(OS);
Eli Friedmana990e072011-06-15 00:47:34 +0000185 }
Dan Gohmanead01092010-09-16 22:08:32 +0000186 OS << "\n";
187 }
188
189 Inst->print(OS);
190 OS << "\n\n";
191 }
192}