LLVM 23.0.0git
Loads.h
Go to the documentation of this file.
1//===- Loads.h - Local load analysis --------------------------------------===//
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// This file declares simple local analyses for load instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_LOADS_H
14#define LLVM_ANALYSIS_LOADS_H
15
16#include "llvm/ADT/APInt.h"
17#include "llvm/IR/BasicBlock.h"
21
22namespace llvm {
23
24class BatchAAResults;
25class AssumptionCache;
26class DataLayout;
27class DominatorTree;
28class Instruction;
29class LoadInst;
30class Loop;
31class MemoryLocation;
32class SCEV;
33class ScalarEvolution;
34class SCEVPredicate;
35template <typename T> class SmallVectorImpl;
37
38/// Return true if this is always a dereferenceable pointer. If the context
39/// instruction is specified perform context-sensitive analysis and return true
40/// if the pointer is dereferenceable at the specified instruction.
42 const DataLayout &DL,
43 const Instruction *CtxI = nullptr,
44 AssumptionCache *AC = nullptr,
45 const DominatorTree *DT = nullptr,
46 const TargetLibraryInfo *TLI = nullptr);
47
48/// Returns true if V is always a dereferenceable pointer with alignment
49/// greater or equal than requested. If the context instruction is specified
50/// performs context-sensitive analysis and returns true if the pointer is
51/// dereferenceable at the specified instruction.
53 const Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
54 const Instruction *CtxI = nullptr, AssumptionCache *AC = nullptr,
55 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
56
57/// Returns true if V is always dereferenceable for Size byte with alignment
58/// greater or equal than requested. If the context instruction is specified
59/// performs context-sensitive analysis and returns true if the pointer is
60/// dereferenceable at the specified instruction.
62 const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
63 const Instruction *CtxI = nullptr, AssumptionCache *AC = nullptr,
64 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
65
66/// Return true if we know that executing a load from this value cannot trap.
67///
68/// If DT and ScanFrom are specified this method performs context-sensitive
69/// analysis and returns true if it is safe to load immediately before ScanFrom.
70///
71/// If it is not obviously safe to load from the specified pointer, we do a
72/// quick local scan of the basic block containing ScanFrom, to determine if
73/// the address is already accessed.
75 Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
76 Instruction *ScanFrom, AssumptionCache *AC = nullptr,
77 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
78
79/// Return true if we can prove that the given load (which is assumed to be
80/// within the specified loop) would access only dereferenceable memory, and
81/// be properly aligned on every iteration of the specified loop regardless of
82/// its placement within the loop. (i.e. does not require predication beyond
83/// that required by the header itself and could be hoisted into the header
84/// if desired.) This is more powerful than the variants above when the
85/// address loaded from is analyzeable by SCEV.
88 AssumptionCache *AC = nullptr,
89 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
90
91/// Overload for isDereferenceableAndAlignedInLoop taking the pointer and access
92/// size directly as SCEVs.
94 const SCEV *PtrSCEV, Align Alignment, const SCEV *EltSizeSCEV, Loop *L,
95 ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC = nullptr,
96 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
97
98/// Returns true if the loop contains read-only memory accesses and doesn't
99/// throw. Puts loads that may fault into \p NonDereferenceableAndAlignedLoads.
100LLVM_ABI bool
102 AssumptionCache *AC,
103 SmallVectorImpl<LoadInst *> &NonDereferenceableAndAlignedLoads,
104 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
105
106/// Return true if we know that executing a load from this value cannot trap.
107///
108/// If DT and ScanFrom are specified this method performs context-sensitive
109/// analysis and returns true if it is safe to load immediately before ScanFrom.
110///
111/// If it is not obviously safe to load from the specified pointer, we do a
112/// quick local scan of the basic block containing ScanFrom, to determine if
113/// the address is already accessed.
115 Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
116 Instruction *ScanFrom, AssumptionCache *AC = nullptr,
117 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
118
119/// Return true if speculation of the given load must be suppressed to avoid
120/// ordering or interfering with an active sanitizer. If not suppressed,
121/// dereferenceability and alignment must be proven separately. Note: This
122/// is only needed for raw reasoning; if you use the interface below
123/// (isSafeToSpeculativelyExecute), this is handled internally.
125
126/// The default number of maximum instructions to scan in the block, used by
127/// FindAvailableLoadedValue().
129
130/// Scan backwards to see if we have the value of the given load available
131/// locally within a small number of instructions.
132///
133/// You can use this function to scan across multiple blocks: after you call
134/// this function, if ScanFrom points at the beginning of the block, it's safe
135/// to continue scanning the predecessors.
136///
137/// Note that performing load CSE requires special care to make sure the
138/// metadata is set appropriately. In particular, aliasing metadata needs
139/// to be merged. (This doesn't matter for store-to-load forwarding because
140/// the only relevant load gets deleted.)
141///
142/// \param Load The load we want to replace.
143/// \param ScanBB The basic block to scan.
144/// \param [in,out] ScanFrom The location to start scanning from. When this
145/// function returns, it points at the last instruction scanned.
146/// \param MaxInstsToScan The maximum number of instructions to scan. If this
147/// is zero, the whole block will be scanned.
148/// \param AA Optional pointer to alias analysis, to make the scan more
149/// precise.
150/// \param [out] IsLoadCSE Whether the returned value is a load from the same
151/// location in memory, as opposed to the value operand of a store.
152///
153/// \returns The found value, or nullptr if no value is found.
155 LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom,
156 unsigned MaxInstsToScan = DefMaxInstsToScan, BatchAAResults *AA = nullptr,
157 bool *IsLoadCSE = nullptr, unsigned *NumScanedInst = nullptr);
158
159/// This overload provides a more efficient implementation of
160/// FindAvailableLoadedValue() for the case where we are not interested in
161/// finding the closest clobbering instruction if no available load is found.
162/// This overload cannot be used to scan across multiple blocks.
164FindAvailableLoadedValue(LoadInst *Load, BatchAAResults &AA, bool *IsLoadCSE,
165 unsigned MaxInstsToScan = DefMaxInstsToScan);
166
167/// Scan backwards to see if we have the value of the given pointer available
168/// locally within a small number of instructions.
169///
170/// You can use this function to scan across multiple blocks: after you call
171/// this function, if ScanFrom points at the beginning of the block, it's safe
172/// to continue scanning the predecessors.
173///
174/// \param Loc The location we want the load and store to originate from.
175/// \param AccessTy The access type of the pointer.
176/// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
177/// case it is false, we can return an atomic or non-atomic load or store. In
178/// case it is true, we need to return an atomic load or store.
179/// \param ScanBB The basic block to scan.
180/// \param [in,out] ScanFrom The location to start scanning from. When this
181/// function returns, it points at the last instruction scanned.
182/// \param MaxInstsToScan The maximum number of instructions to scan. If this
183/// is zero, the whole block will be scanned.
184/// \param AA Optional pointer to alias analysis, to make the scan more
185/// precise.
186/// \param [out] IsLoadCSE Whether the returned value is a load from the same
187/// location in memory, as opposed to the value operand of a store.
188///
189/// \returns The found value, or nullptr if no value is found.
191 const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic,
192 BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan,
193 BatchAAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst);
194
195/// Returns true if a pointer value \p From can be replaced with another pointer
196/// value \To if they are deemed equal through some means (e.g. information from
197/// conditions).
198/// NOTE: The current implementation allows replacement in Icmp and PtrToInt
199/// instructions, as well as when we are replacing with a null pointer.
200/// Additionally it also allows replacement of pointers when both pointers have
201/// the same underlying object.
202LLVM_ABI bool canReplacePointersIfEqual(const Value *From, const Value *To,
203 const DataLayout &DL);
204LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To,
205 const DataLayout &DL);
206
207/// Linear expression BasePtr + Index * Scale + Offset.
208/// Index, Scale and Offset all have the same bit width, which matches the
209/// pointer index size of BasePtr.
210/// Index may be nullptr if Scale is 0.
221
222/// Decompose a pointer into a linear expression. This may look through
223/// multiple GEPs.
224LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL,
225 Value *Ptr);
226}
227
228#endif
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:213
Class for arbitrary precision integers.
Definition APInt.h:78
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags all()
An instruction for reading from memory.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Representation for a specific memory location.
This class represents an assumption made using SCEV expressions which can be checked at run-time.
This class represents an analyzed expression in the program.
The main scalar evolution driver.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Abstract Attribute helper functions.
Definition Attributor.h:165
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI 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:229
LLVM_ABI Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, BatchAAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
Definition Loads.cpp:686
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:431
LLVM_ABI Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, BatchAAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
Definition Loads.cpp:553
LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To, const DataLayout &DL)
Definition Loads.cpp:859
LLVM_ABI bool canReplacePointersIfEqual(const Value *From, const Value *To, const DataLayout &DL)
Returns true if a pointer value From can be replaced with another pointer value \To if they are deeme...
Definition Loads.cpp:879
LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL, Value *Ptr)
Decompose a pointer into a linear expression.
Definition Loads.cpp:907
LLVM_ABI bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size, const DataLayout &DL, Instruction *ScanFrom, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if we know that executing a load from this value cannot trap.
Definition Loads.cpp:446
LLVM_ABI cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
constexpr unsigned BitWidth
LLVM_ABI 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:249
LLVM_ABI bool isReadOnlyLoop(Loop *L, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, SmallVectorImpl< LoadInst * > &NonDereferenceableAndAlignedLoads, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Returns true if the loop contains read-only memory accesses and doesn't throw.
Definition Loads.cpp:889
LLVM_ABI bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC=nullptr, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Return true if we can prove that the given load (which is assumed to be within the specified loop) wo...
Definition Loads.cpp:289
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
LinearExpression(Value *BasePtr, unsigned BitWidth)
Definition Loads.h:218
GEPNoWrapFlags Flags
Definition Loads.h:216