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/InstIterator.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/InitializePasses.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 using namespace llvm;
20 
21 PreservedAnalyses MemDerefPrinterPass::run(Function &F,
22                                            FunctionAnalysisManager &AM) {
23   OS << "Memory Dereferencibility of pointers in function '" << F.getName()
24      << "'\n";
25 
26   SmallVector<Value *, 4> Deref;
27   SmallPtrSet<Value *, 4> DerefAndAligned;
28 
29   const DataLayout &DL = F.getParent()->getDataLayout();
30   for (auto &I : instructions(F)) {
31     if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
32       Value *PO = LI->getPointerOperand();
33       if (isDereferenceablePointer(PO, LI->getType(), DL))
34         Deref.push_back(PO);
35       if (isDereferenceableAndAlignedPointer(PO, LI->getType(), LI->getAlign(),
36                                              DL))
37         DerefAndAligned.insert(PO);
38     }
39   }
40 
41   OS << "The following are dereferenceable:\n";
42   for (Value *V : Deref) {
43     OS << "  ";
44     V->print(OS);
45     if (DerefAndAligned.count(V))
46       OS << "\t(aligned)";
47     else
48       OS << "\t(unaligned)";
49     OS << "\n";
50   }
51   return PreservedAnalyses::all();
52 }
53