2010-09-16 22:08:32 +00:00
|
|
|
//===- MemDepPrinter.cpp - Printer for MemoryDependenceAnalysis -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
|
2014-03-04 11:01:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2014-03-04 10:30:26 +00:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2010-09-16 22:08:32 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-09-17 00:33:43 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-09-16 22:08:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MemDepPrinter : public FunctionPass {
|
|
|
|
const Function *F;
|
|
|
|
|
2011-10-13 22:14:57 +00:00
|
|
|
enum DepType {
|
|
|
|
Clobber = 0,
|
|
|
|
Def,
|
|
|
|
NonFuncLocal,
|
|
|
|
Unknown
|
|
|
|
};
|
|
|
|
|
2012-05-24 06:35:32 +00:00
|
|
|
static const char *const DepTypeStr[];
|
2011-10-13 22:14:57 +00:00
|
|
|
|
|
|
|
typedef PointerIntPair<const Instruction *, 2, DepType> InstTypePair;
|
|
|
|
typedef std::pair<InstTypePair, const BasicBlock *> Dep;
|
2010-09-16 22:08:32 +00:00
|
|
|
typedef SmallSetVector<Dep, 4> DepSet;
|
|
|
|
typedef DenseMap<const Instruction *, DepSet> DepSetMap;
|
|
|
|
DepSetMap Deps;
|
|
|
|
|
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
MemDepPrinter() : FunctionPass(ID) {
|
|
|
|
initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-09-16 22:08:32 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2010-09-16 22:08:32 +00:00
|
|
|
|
2014-04-15 04:59:12 +00:00
|
|
|
void print(raw_ostream &OS, const Module * = nullptr) const override;
|
2010-09-16 22:08:32 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-11-10 20:37:15 +00:00
|
|
|
AU.addRequiredTransitive<AliasAnalysis>();
|
|
|
|
AU.addRequiredTransitive<MemoryDependenceAnalysis>();
|
2010-09-16 22:08:32 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void releaseMemory() override {
|
2010-09-16 22:08:32 +00:00
|
|
|
Deps.clear();
|
2014-04-15 04:59:12 +00:00
|
|
|
F = nullptr;
|
2010-09-16 22:08:32 +00:00
|
|
|
}
|
2011-10-13 22:14:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static InstTypePair getInstTypePair(MemDepResult dep) {
|
|
|
|
if (dep.isClobber())
|
|
|
|
return InstTypePair(dep.getInst(), Clobber);
|
|
|
|
if (dep.isDef())
|
|
|
|
return InstTypePair(dep.getInst(), Def);
|
|
|
|
if (dep.isNonFuncLocal())
|
|
|
|
return InstTypePair(dep.getInst(), NonFuncLocal);
|
2013-12-04 23:55:09 +00:00
|
|
|
assert(dep.isUnknown() && "unexpected dependence type");
|
2011-10-13 22:14:57 +00:00
|
|
|
return InstTypePair(dep.getInst(), Unknown);
|
|
|
|
}
|
|
|
|
static InstTypePair getInstTypePair(const Instruction* inst, DepType type) {
|
|
|
|
return InstTypePair(inst, type);
|
|
|
|
}
|
2010-09-16 22:08:32 +00:00
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2010-09-16 22:08:32 +00:00
|
|
|
|
|
|
|
char MemDepPrinter::ID = 0;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(MemDepPrinter, "print-memdeps",
|
|
|
|
"Print MemDeps of function", false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
|
|
|
|
INITIALIZE_PASS_END(MemDepPrinter, "print-memdeps",
|
|
|
|
"Print MemDeps of function", false, true)
|
2010-09-16 22:08:32 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createMemDepPrinter() {
|
|
|
|
return new MemDepPrinter();
|
|
|
|
}
|
|
|
|
|
2012-05-24 06:35:32 +00:00
|
|
|
const char *const MemDepPrinter::DepTypeStr[]
|
2011-10-13 22:14:57 +00:00
|
|
|
= {"Clobber", "Def", "NonFuncLocal", "Unknown"};
|
|
|
|
|
2010-09-16 22:08:32 +00:00
|
|
|
bool MemDepPrinter::runOnFunction(Function &F) {
|
|
|
|
this->F = &F;
|
|
|
|
MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
|
|
|
|
|
|
|
|
// All this code uses non-const interfaces because MemDep is not
|
|
|
|
// const-friendly, though nothing is actually modified.
|
2015-08-06 19:10:45 +00:00
|
|
|
for (auto &I : instructions(F)) {
|
2015-02-09 19:49:54 +00:00
|
|
|
Instruction *Inst = &I;
|
2010-09-16 22:08:32 +00:00
|
|
|
|
|
|
|
if (!Inst->mayReadFromMemory() && !Inst->mayWriteToMemory())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MemDepResult Res = MDA.getDependency(Inst);
|
|
|
|
if (!Res.isNonLocal()) {
|
2011-10-13 22:14:57 +00:00
|
|
|
Deps[Inst].insert(std::make_pair(getInstTypePair(Res),
|
2014-04-15 04:59:12 +00:00
|
|
|
static_cast<BasicBlock *>(nullptr)));
|
2015-04-10 14:50:08 +00:00
|
|
|
} else if (auto CS = CallSite(Inst)) {
|
2010-09-16 22:08:32 +00:00
|
|
|
const MemoryDependenceAnalysis::NonLocalDepInfo &NLDI =
|
|
|
|
MDA.getNonLocalCallDependency(CS);
|
|
|
|
|
|
|
|
DepSet &InstDeps = Deps[Inst];
|
|
|
|
for (MemoryDependenceAnalysis::NonLocalDepInfo::const_iterator
|
|
|
|
I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
|
|
|
|
const MemDepResult &Res = I->getResult();
|
2011-10-13 22:14:57 +00:00
|
|
|
InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
|
2010-09-16 22:08:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SmallVector<NonLocalDepResult, 4> NLDI;
|
2015-01-09 00:26:45 +00:00
|
|
|
assert( (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
|
|
|
|
isa<VAArgInst>(Inst)) && "Unknown memory instruction!");
|
|
|
|
MDA.getNonLocalPointerDependency(Inst, NLDI);
|
2010-09-16 22:08:32 +00:00
|
|
|
|
|
|
|
DepSet &InstDeps = Deps[Inst];
|
|
|
|
for (SmallVectorImpl<NonLocalDepResult>::const_iterator
|
|
|
|
I = NLDI.begin(), E = NLDI.end(); I != E; ++I) {
|
|
|
|
const MemDepResult &Res = I->getResult();
|
2011-10-13 22:14:57 +00:00
|
|
|
InstDeps.insert(std::make_pair(getInstTypePair(Res), I->getBB()));
|
2010-09-16 22:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemDepPrinter::print(raw_ostream &OS, const Module *M) const {
|
2015-08-06 19:10:45 +00:00
|
|
|
for (const auto &I : instructions(*F)) {
|
2015-02-09 19:49:54 +00:00
|
|
|
const Instruction *Inst = &I;
|
2010-09-16 22:08:32 +00:00
|
|
|
|
2010-09-16 22:50:09 +00:00
|
|
|
DepSetMap::const_iterator DI = Deps.find(Inst);
|
|
|
|
if (DI == Deps.end())
|
2010-09-16 22:08:32 +00:00
|
|
|
continue;
|
|
|
|
|
2010-09-16 22:50:09 +00:00
|
|
|
const DepSet &InstDeps = DI->second;
|
2010-09-16 22:08:32 +00:00
|
|
|
|
2015-02-25 23:55:00 +00:00
|
|
|
for (const auto &I : InstDeps) {
|
2015-02-09 19:49:54 +00:00
|
|
|
const Instruction *DepInst = I.first.getPointer();
|
|
|
|
DepType type = I.first.getInt();
|
|
|
|
const BasicBlock *DepBB = I.second;
|
2010-09-16 22:08:32 +00:00
|
|
|
|
2011-06-15 00:47:34 +00:00
|
|
|
OS << " ";
|
2011-10-13 22:14:57 +00:00
|
|
|
OS << DepTypeStr[type];
|
2010-09-16 22:08:32 +00:00
|
|
|
if (DepBB) {
|
|
|
|
OS << " in block ";
|
2014-01-09 02:29:41 +00:00
|
|
|
DepBB->printAsOperand(OS, /*PrintType=*/false, M);
|
2010-09-16 22:08:32 +00:00
|
|
|
}
|
2011-06-15 00:47:34 +00:00
|
|
|
if (DepInst) {
|
|
|
|
OS << " from: ";
|
2011-10-13 22:14:57 +00:00
|
|
|
DepInst->print(OS);
|
2011-06-15 00:47:34 +00:00
|
|
|
}
|
2010-09-16 22:08:32 +00:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
Inst->print(OS);
|
|
|
|
OS << "\n\n";
|
|
|
|
}
|
|
|
|
}
|