[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / Analysis / MemDerefPrinter.cpp
blob1b16e1a9bcb2915e59f8cc0af9fa64e6e4f1612b
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/MemDerefPrinter.h"
10 #include "llvm/Analysis/Loads.h"
11 #include "llvm/Analysis/Passes.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/InstIterator.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
24 namespace {
25 struct MemDerefPrinter : public FunctionPass {
26 SmallVector<Value *, 4> Deref;
27 SmallPtrSet<Value *, 4> DerefAndAligned;
29 static char ID; // Pass identification, replacement for typeid
30 MemDerefPrinter() : FunctionPass(ID) {
31 initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
33 void getAnalysisUsage(AnalysisUsage &AU) const override {
34 AU.setPreservesAll();
36 bool runOnFunction(Function &F) override;
37 void print(raw_ostream &OS, const Module * = nullptr) const override;
38 void releaseMemory() override {
39 Deref.clear();
40 DerefAndAligned.clear();
45 char MemDerefPrinter::ID = 0;
46 INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
47 "Memory Dereferenciblity of pointers in function", false, true)
48 INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
49 "Memory Dereferenciblity of pointers in function", false, true)
51 FunctionPass *llvm::createMemDerefPrinter() {
52 return new MemDerefPrinter();
55 bool MemDerefPrinter::runOnFunction(Function &F) {
56 const DataLayout &DL = F.getParent()->getDataLayout();
57 for (auto &I: instructions(F)) {
58 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
59 Value *PO = LI->getPointerOperand();
60 if (isDereferenceablePointer(PO, LI->getType(), DL))
61 Deref.push_back(PO);
62 if (isDereferenceableAndAlignedPointer(
63 PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL))
64 DerefAndAligned.insert(PO);
67 return false;
70 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
71 OS << "The following are dereferenceable:\n";
72 for (Value *V: Deref) {
73 OS << " ";
74 V->print(OS);
75 if (DerefAndAligned.count(V))
76 OS << "\t(aligned)";
77 else
78 OS << "\t(unaligned)";
79 OS << "\n";
83 PreservedAnalyses MemDerefPrinterPass::run(Function &F,
84 FunctionAnalysisManager &AM) {
85 OS << "Memory Dereferencibility of pointers in function '" << F.getName()
86 << "'\n";
88 SmallVector<Value *, 4> Deref;
89 SmallPtrSet<Value *, 4> DerefAndAligned;
91 const DataLayout &DL = F.getParent()->getDataLayout();
92 for (auto &I : instructions(F)) {
93 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
94 Value *PO = LI->getPointerOperand();
95 if (isDereferenceablePointer(PO, LI->getType(), DL))
96 Deref.push_back(PO);
97 if (isDereferenceableAndAlignedPointer(
98 PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL))
99 DerefAndAligned.insert(PO);
103 OS << "The following are dereferenceable:\n";
104 for (Value *V : Deref) {
105 OS << " ";
106 V->print(OS);
107 if (DerefAndAligned.count(V))
108 OS << "\t(aligned)";
109 else
110 OS << "\t(unaligned)";
111 OS << "\n";
113 return PreservedAnalyses::all();