xref: /llvm-project/llvm/lib/Analysis/MemDerefPrinter.cpp (revision 236fda550d36d35a00785938c3e38b0f402aeda6)
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 
16 using namespace llvm;
17 
18 PreservedAnalyses 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>(&I)) {
29       Value *PO = LI->getPointerOperand();
30       if (isDereferenceablePointer(PO, LI->getType(), DL, LI))
31         Deref.push_back(PO);
32       if (isDereferenceableAndAlignedPointer(PO, LI->getType(), LI->getAlign(),
33                                              DL, LI))
34         DerefAndAligned.insert(PO);
35     }
36   }
37 
38   OS << "The following are dereferenceable:\n";
39   for (Value *V : Deref) {
40     OS << "  ";
41     V->print(OS);
42     if (DerefAndAligned.count(V))
43       OS << "\t(aligned)";
44     else
45       OS << "\t(unaligned)";
46     OS << "\n";
47   }
48   return PreservedAnalyses::all();
49 }
50