1 //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
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
7 //===----------------------------------------------------------------------===//
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"
21 PreservedAnalyses
MemDerefPrinterPass::run(Function
&F
,
22 FunctionAnalysisManager
&AM
) {
23 OS
<< "Memory Dereferencibility of pointers in function '" << F
.getName()
26 SmallVector
<Value
*, 4> Deref
;
27 SmallPtrSet
<Value
*, 4> DerefAndAligned
;
29 const DataLayout
&DL
= F
.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
))
35 if (isDereferenceableAndAlignedPointer(PO
, LI
->getType(), LI
->getAlign(),
37 DerefAndAligned
.insert(PO
);
41 OS
<< "The following are dereferenceable:\n";
42 for (Value
*V
: Deref
) {
45 if (DerefAndAligned
.count(V
))
48 OS
<< "\t(unaligned)";
51 return PreservedAnalyses::all();