2015-02-06 09:46:42 +08:00
|
|
|
//===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
2016-02-24 20:49:04 +08:00
|
|
|
#include "llvm/Analysis/Loads.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2015-02-06 09:46:42 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2015-02-10 05:50:03 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-02-06 09:46:42 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2015-02-06 09:46:42 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MemDerefPrinter : public FunctionPass {
|
2015-08-17 23:54:26 +08:00
|
|
|
SmallVector<Value *, 4> Deref;
|
|
|
|
SmallPtrSet<Value *, 4> DerefAndAligned;
|
2015-02-06 09:46:42 +08:00
|
|
|
|
2015-05-21 19:57:38 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2015-02-06 09:46:42 +08:00
|
|
|
MemDerefPrinter() : FunctionPass(ID) {
|
|
|
|
initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2015-02-10 05:50:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2015-02-06 09:46:42 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void print(raw_ostream &OS, const Module * = nullptr) const override;
|
|
|
|
void releaseMemory() override {
|
2015-08-17 23:54:26 +08:00
|
|
|
Deref.clear();
|
|
|
|
DerefAndAligned.clear();
|
2015-02-06 09:46:42 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2015-02-06 09:46:42 +08:00
|
|
|
|
|
|
|
char MemDerefPrinter::ID = 0;
|
2015-02-10 05:50:03 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
|
|
|
|
"Memory Dereferenciblity of pointers in function", false, true)
|
|
|
|
INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
|
|
|
|
"Memory Dereferenciblity of pointers in function", false, true)
|
2015-02-06 09:46:42 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createMemDerefPrinter() {
|
|
|
|
return new MemDerefPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemDerefPrinter::runOnFunction(Function &F) {
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2015-08-07 03:10:45 +08:00
|
|
|
for (auto &I: instructions(F)) {
|
2015-02-06 09:46:42 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
|
|
|
|
Value *PO = LI->getPointerOperand();
|
2015-04-24 01:36:48 +08:00
|
|
|
if (isDereferenceablePointer(PO, DL))
|
2015-08-17 23:54:26 +08:00
|
|
|
Deref.push_back(PO);
|
|
|
|
if (isDereferenceableAndAlignedPointer(PO, LI->getAlignment(), DL))
|
|
|
|
DerefAndAligned.insert(PO);
|
2015-02-06 09:46:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
|
|
|
|
OS << "The following are dereferenceable:\n";
|
2015-08-17 23:54:26 +08:00
|
|
|
for (Value *V: Deref) {
|
2015-02-06 09:46:42 +08:00
|
|
|
V->print(OS);
|
2015-08-17 23:54:26 +08:00
|
|
|
if (DerefAndAligned.count(V))
|
|
|
|
OS << "\t(aligned)";
|
|
|
|
else
|
|
|
|
OS << "\t(unaligned)";
|
2015-02-06 09:46:42 +08:00
|
|
|
OS << "\n\n";
|
|
|
|
}
|
|
|
|
}
|