2015-02-06 09:46:42 +08:00
|
|
|
//===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-02-06 09:46:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-24 20:49:04 +08:00
|
|
|
#include "llvm/Analysis/Loads.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2015-02-10 05:50:03 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-02-06 09:46:42 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2020-06-24 20:17:44 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-02-06 09:46:42 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2020-06-24 20:17:44 +08:00
|
|
|
#include "llvm/Pass.h"
|
2015-02-06 09:46:42 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MemDerefPrinter : public FunctionPass {
|
2015-08-17 23:54:26 +08:00
|
|
|
SmallVector<Value *, 4> Deref;
|
|
|
|
SmallPtrSet<Value *, 4> DerefAndAligned;
|
2015-02-06 09:46:42 +08:00
|
|
|
|
2015-05-21 19:57:38 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2015-02-06 09:46:42 +08:00
|
|
|
MemDerefPrinter() : FunctionPass(ID) {
|
|
|
|
initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2015-02-10 05:50:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2015-02-06 09:46:42 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void print(raw_ostream &OS, const Module * = nullptr) const override;
|
|
|
|
void releaseMemory() override {
|
2015-08-17 23:54:26 +08:00
|
|
|
Deref.clear();
|
|
|
|
DerefAndAligned.clear();
|
2015-02-06 09:46:42 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2015-02-06 09:46:42 +08:00
|
|
|
|
|
|
|
char MemDerefPrinter::ID = 0;
|
2015-02-10 05:50:03 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
|
|
|
|
"Memory Dereferenciblity of pointers in function", false, true)
|
|
|
|
INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
|
|
|
|
"Memory Dereferenciblity of pointers in function", false, true)
|
2015-02-06 09:46:42 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createMemDerefPrinter() {
|
|
|
|
return new MemDerefPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemDerefPrinter::runOnFunction(Function &F) {
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2015-08-07 03:10:45 +08:00
|
|
|
for (auto &I: instructions(F)) {
|
2015-02-06 09:46:42 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
|
|
|
|
Value *PO = LI->getPointerOperand();
|
2019-07-09 19:35:35 +08:00
|
|
|
if (isDereferenceablePointer(PO, LI->getType(), DL))
|
2015-08-17 23:54:26 +08:00
|
|
|
Deref.push_back(PO);
|
2019-10-21 23:10:26 +08:00
|
|
|
if (isDereferenceableAndAlignedPointer(
|
|
|
|
PO, LI->getType(), MaybeAlign(LI->getAlignment()), DL))
|
2015-08-17 23:54:26 +08:00
|
|
|
DerefAndAligned.insert(PO);
|
2015-02-06 09:46:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
|
|
|
|
OS << "The following are dereferenceable:\n";
|
2015-08-17 23:54:26 +08:00
|
|
|
for (Value *V: Deref) {
|
2015-02-06 09:46:42 +08:00
|
|
|
V->print(OS);
|
2015-08-17 23:54:26 +08:00
|
|
|
if (DerefAndAligned.count(V))
|
|
|
|
OS << "\t(aligned)";
|
|
|
|
else
|
|
|
|
OS << "\t(unaligned)";
|
2015-02-06 09:46:42 +08:00
|
|
|
OS << "\n\n";
|
|
|
|
}
|
|
|
|
}
|