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/MemDerefPrinter.h" 10 #include "llvm/Analysis/Loads.h" 11 #include "llvm/Analysis/Passes.h" 12 #include "llvm/IR/DataLayout.h" 13 #include "llvm/IR/InstIterator.h" 14 #include "llvm/IR/Instructions.h" 15 #include "llvm/IR/LLVMContext.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/InitializePasses.h" 18 #include "llvm/Pass.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace llvm; 23 24 namespace { 25 struct MemDerefPrinter : public FunctionPass { 26 SmallVector<Value *, 4> Deref; 27 SmallPtrSet<Value *, 4> DerefAndAligned; 28 29 static char ID; // Pass identification, replacement for typeid 30 MemDerefPrinter() : FunctionPass(ID) { 31 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry()); 32 } 33 void getAnalysisUsage(AnalysisUsage &AU) const override { 34 AU.setPreservesAll(); 35 } 36 bool runOnFunction(Function &F) override; 37 void print(raw_ostream &OS, const Module * = nullptr) const override; 38 void releaseMemory() override { 39 Deref.clear(); 40 DerefAndAligned.clear(); 41 } 42 }; 43 } 44 45 char MemDerefPrinter::ID = 0; 46 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs", 47 "Memory Dereferenciblity of pointers in function", false, true) 48 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs", 49 "Memory Dereferenciblity of pointers in function", false, true) 50 51 FunctionPass *llvm::createMemDerefPrinter() { 52 return new MemDerefPrinter(); 53 } 54 55 bool MemDerefPrinter::runOnFunction(Function &F) { 56 const DataLayout &DL = F.getParent()->getDataLayout(); 57 for (auto &I: instructions(F)) { 58 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 59 Value *PO = LI->getPointerOperand(); 60 if (isDereferenceablePointer(PO, LI->getType(), DL)) 61 Deref.push_back(PO); 62 if (isDereferenceableAndAlignedPointer(PO, LI->getType(), LI->getAlign(), 63 DL)) 64 DerefAndAligned.insert(PO); 65 } 66 } 67 return false; 68 } 69 70 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const { 71 OS << "The following are dereferenceable:\n"; 72 for (Value *V: Deref) { 73 OS << " "; 74 V->print(OS); 75 if (DerefAndAligned.count(V)) 76 OS << "\t(aligned)"; 77 else 78 OS << "\t(unaligned)"; 79 OS << "\n"; 80 } 81 } 82 83 PreservedAnalyses MemDerefPrinterPass::run(Function &F, 84 FunctionAnalysisManager &AM) { 85 OS << "Memory Dereferencibility of pointers in function '" << F.getName() 86 << "'\n"; 87 88 SmallVector<Value *, 4> Deref; 89 SmallPtrSet<Value *, 4> DerefAndAligned; 90 91 const DataLayout &DL = F.getParent()->getDataLayout(); 92 for (auto &I : instructions(F)) { 93 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 94 Value *PO = LI->getPointerOperand(); 95 if (isDereferenceablePointer(PO, LI->getType(), DL)) 96 Deref.push_back(PO); 97 if (isDereferenceableAndAlignedPointer(PO, LI->getType(), LI->getAlign(), 98 DL)) 99 DerefAndAligned.insert(PO); 100 } 101 } 102 103 OS << "The following are dereferenceable:\n"; 104 for (Value *V : Deref) { 105 OS << " "; 106 V->print(OS); 107 if (DerefAndAligned.count(V)) 108 OS << "\t(aligned)"; 109 else 110 OS << "\t(unaligned)"; 111 OS << "\n"; 112 } 113 return PreservedAnalyses::all(); 114 } 115