Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 1 | //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===// |
| 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 | #include "llvm/Analysis/Passes.h" |
| 11 | #include "llvm/ADT/SetVector.h" |
| 12 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/ValueTracking.h" |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 14 | #include "llvm/IR/CallSite.h" |
Ramkumar Ramachandra | 82ab65c | 2015-02-09 21:50:03 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DataLayout.h" |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 16 | #include "llvm/IR/InstIterator.h" |
| 17 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | struct MemDerefPrinter : public FunctionPass { |
| 25 | SmallVector<Value *, 4> Vec; |
| 26 | |
Artur Pilipenko | 0f90c86 | 2015-05-21 11:57:38 +0000 | [diff] [blame] | 27 | static char ID; // Pass identification, replacement for typeid |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 28 | MemDerefPrinter() : FunctionPass(ID) { |
| 29 | initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry()); |
| 30 | } |
Ramkumar Ramachandra | 82ab65c | 2015-02-09 21:50:03 +0000 | [diff] [blame] | 31 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Ramkumar Ramachandra | 82ab65c | 2015-02-09 21:50:03 +0000 | [diff] [blame] | 32 | AU.setPreservesAll(); |
| 33 | } |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 34 | bool runOnFunction(Function &F) override; |
| 35 | void print(raw_ostream &OS, const Module * = nullptr) const override; |
| 36 | void releaseMemory() override { |
| 37 | Vec.clear(); |
| 38 | } |
| 39 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 40 | } |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 41 | |
| 42 | char MemDerefPrinter::ID = 0; |
Ramkumar Ramachandra | 82ab65c | 2015-02-09 21:50:03 +0000 | [diff] [blame] | 43 | INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs", |
| 44 | "Memory Dereferenciblity of pointers in function", false, true) |
Ramkumar Ramachandra | 82ab65c | 2015-02-09 21:50:03 +0000 | [diff] [blame] | 45 | INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs", |
| 46 | "Memory Dereferenciblity of pointers in function", false, true) |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 47 | |
| 48 | FunctionPass *llvm::createMemDerefPrinter() { |
| 49 | return new MemDerefPrinter(); |
| 50 | } |
| 51 | |
| 52 | bool MemDerefPrinter::runOnFunction(Function &F) { |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 53 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame^] | 54 | for (auto &I: instructions(F)) { |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 55 | if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { |
| 56 | Value *PO = LI->getPointerOperand(); |
Philip Reames | 5461d45 | 2015-04-23 17:36:48 +0000 | [diff] [blame] | 57 | if (isDereferenceablePointer(PO, DL)) |
Ramkumar Ramachandra | 8378ac3 | 2015-02-06 01:46:42 +0000 | [diff] [blame] | 58 | Vec.push_back(PO); |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const { |
| 65 | OS << "The following are dereferenceable:\n"; |
| 66 | for (auto &V: Vec) { |
| 67 | V->print(OS); |
| 68 | OS << "\n\n"; |
| 69 | } |
| 70 | } |