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/Loads.h"
10 #include "llvm/Analysis/Passes.h"
11 #include "llvm/IR/CallSite.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
21 struct MemDerefPrinter
: public FunctionPass
{
22 SmallVector
<Value
*, 4> Deref
;
23 SmallPtrSet
<Value
*, 4> DerefAndAligned
;
25 static char ID
; // Pass identification, replacement for typeid
26 MemDerefPrinter() : FunctionPass(ID
) {
27 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
29 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
32 bool runOnFunction(Function
&F
) override
;
33 void print(raw_ostream
&OS
, const Module
* = nullptr) const override
;
34 void releaseMemory() override
{
36 DerefAndAligned
.clear();
41 char MemDerefPrinter::ID
= 0;
42 INITIALIZE_PASS_BEGIN(MemDerefPrinter
, "print-memderefs",
43 "Memory Dereferenciblity of pointers in function", false, true)
44 INITIALIZE_PASS_END(MemDerefPrinter
, "print-memderefs",
45 "Memory Dereferenciblity of pointers in function", false, true)
47 FunctionPass
*llvm::createMemDerefPrinter() {
48 return new MemDerefPrinter();
51 bool MemDerefPrinter::runOnFunction(Function
&F
) {
52 const DataLayout
&DL
= F
.getParent()->getDataLayout();
53 for (auto &I
: instructions(F
)) {
54 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(&I
)) {
55 Value
*PO
= LI
->getPointerOperand();
56 if (isDereferenceablePointer(PO
, LI
->getType(), DL
))
58 if (isDereferenceableAndAlignedPointer(
59 PO
, LI
->getType(), MaybeAlign(LI
->getAlignment()), DL
))
60 DerefAndAligned
.insert(PO
);
66 void MemDerefPrinter::print(raw_ostream
&OS
, const Module
*M
) const {
67 OS
<< "The following are dereferenceable:\n";
68 for (Value
*V
: Deref
) {
70 if (DerefAndAligned
.count(V
))
73 OS
<< "\t(unaligned)";