LLVM 20.0.0git
MemDerefPrinter.cpp
Go to the documentation of this file.
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
10#include "llvm/Analysis/Loads.h"
13#include "llvm/Pass.h"
15
16using namespace llvm;
17
20 OS << "Memory Dereferencibility of pointers in function '" << F.getName()
21 << "'\n";
22
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 }
49}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
An instruction for reading from memory.
Definition: Instructions.h:176
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:215
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:235