1 //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/Loads.h" 10 #include "llvm/Analysis/Passes.h" 11 #include "llvm/IR/CallSite.h" 12 #include "llvm/IR/DataLayout.h" 13 #include "llvm/IR/InstIterator.h" 14 #include "llvm/IR/LLVMContext.h" 15 #include "llvm/IR/Module.h" 16 #include "llvm/Support/ErrorHandling.h" 17 #include "llvm/Support/raw_ostream.h" 18 using namespace llvm; 19 20 namespace { 21 struct MemDerefPrinter : public FunctionPass { 22 SmallVector<Value *, 4> Deref; 23 SmallPtrSet<Value *, 4> DerefAndAligned; 24 25 static char ID; // Pass identification, replacement for typeid 26 MemDerefPrinter() : FunctionPass(ID) { 27 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry()); 28 } 29 void getAnalysisUsage(AnalysisUsage &AU) const override { 30 AU.setPreservesAll(); 31 } 32 bool runOnFunction(Function &F) override; 33 void print(raw_ostream &OS, const Module * = nullptr) const override; 34 void releaseMemory() override { 35 Deref.clear(); 36 DerefAndAligned.clear(); 37 } 38 }; 39 } 40 41 char MemDerefPrinter::ID = 0; 42 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs", 43 "Memory Dereferenciblity of pointers in function", false, true) 44 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs", 45 "Memory Dereferenciblity of pointers in function", false, true) 46 47 FunctionPass *llvm::createMemDerefPrinter() { 48 return new MemDerefPrinter(); 49 } 50 51 bool MemDerefPrinter::runOnFunction(Function &F) { 52 const DataLayout &DL = F.getParent()->getDataLayout(); 53 for (auto &I: instructions(F)) { 54 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 55 Value *PO = LI->getPointerOperand(); 56 if (isDereferenceablePointer(PO, LI->getType(), DL)) 57 Deref.push_back(PO); 58 if (isDereferenceableAndAlignedPointer(PO, LI->getType(), 59 LI->getAlignment(), DL)) 60 DerefAndAligned.insert(PO); 61 } 62 } 63 return false; 64 } 65 66 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const { 67 OS << "The following are dereferenceable:\n"; 68 for (Value *V: Deref) { 69 V->print(OS); 70 if (DerefAndAligned.count(V)) 71 OS << "\t(aligned)"; 72 else 73 OS << "\t(unaligned)"; 74 OS << "\n\n"; 75 } 76 } 77