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( 63 PO, LI->getType(), MaybeAlign(LI->getAlignment()), 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 V->print(OS); 74 if (DerefAndAligned.count(V)) 75 OS << "\t(aligned)"; 76 else 77 OS << "\t(unaligned)"; 78 OS << "\n\n"; 79 } 80 } 81 82 PreservedAnalyses MemDerefPrinterPass::run(Function &F, 83 FunctionAnalysisManager &AM) { 84 OS << "Memory Dereferencibility of pointers in function '" << F.getName() 85 << "'\n"; 86 87 SmallVector<Value *, 4> Deref; 88 SmallPtrSet<Value *, 4> DerefAndAligned; 89 90 const DataLayout &DL = F.getParent()->getDataLayout(); 91 for (auto &I : instructions(F)) { 92 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) { 93 Value *PO = LI->getPointerOperand(); 94 if (isDereferenceablePointer(PO, LI->getType(), DL)) 95 Deref.push_back(PO); 96 if (isDereferenceableAndAlignedPointer( 97 PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL)) 98 DerefAndAligned.insert(PO); 99 } 100 } 101 102 OS << "The following are dereferenceable:\n"; 103 for (Value *V : Deref) { 104 V->print(OS); 105 if (DerefAndAligned.count(V)) 106 OS << "\t(aligned)"; 107 else 108 OS << "\t(unaligned)"; 109 OS << "\n\n"; 110 } 111 return PreservedAnalyses::all(); 112 } 113