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