[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / lib / Analysis / MemDerefPrinter.cpp
blob5cf516a538b55cf582fbdd8482b5a29fac11ce50
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/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"
18 using namespace llvm;
20 namespace {
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 {
30 AU.setPreservesAll();
32 bool runOnFunction(Function &F) override;
33 void print(raw_ostream &OS, const Module * = nullptr) const override;
34 void releaseMemory() override {
35 Deref.clear();
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))
57 Deref.push_back(PO);
58 if (isDereferenceableAndAlignedPointer(
59 PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL))
60 DerefAndAligned.insert(PO);
63 return false;
66 void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
67 OS << "The following are dereferenceable:\n";
68 for (Value *V: Deref) {
69 V->print(OS);
70 if (DerefAndAligned.count(V))
71 OS << "\t(aligned)";
72 else
73 OS << "\t(unaligned)";
74 OS << "\n\n";