LLVM 22.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 ScalarEvolution;
33class SCEVPredicate;
34template <typename T> class SmallVectorImpl;
36
37/// Return true if this is always a dereferenceable pointer. If the context
38/// instruction is specified perform context-sensitive analysis and return true
39/// if the pointer is dereferenceable at the specified instruction.
41 const DataLayout &DL,
42 const Instruction *CtxI = nullptr,
43 AssumptionCache *AC = nullptr,
44 const DominatorTree *DT = nullptr,
45 const TargetLibraryInfo *TLI = nullptr);
46
47/// Returns true if V is always a dereferenceable pointer with alignment
48/// greater or equal than requested. If the context instruction is specified
49/// performs context-sensitive analysis and returns true if the pointer is
50/// dereferenceable at the specified instruction.
52 const Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
53 const Instruction *CtxI = nullptr, AssumptionCache *AC = nullptr,
54 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
55
56/// Returns true if V is always dereferenceable for Size byte with alignment
57/// greater or equal than requested. If the context instruction is specified
58/// performs context-sensitive analysis and returns true if the pointer is
59/// dereferenceable at the specified instruction.
61 const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
62 const Instruction *CtxI = nullptr, AssumptionCache *AC = nullptr,
63 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
64
65/// Return true if we know that executing a load from this value cannot trap.
66///
67/// If DT and ScanFrom are specified this method performs context-sensitive
68/// analysis and returns true if it is safe to load immediately before ScanFrom.
69///
70/// If it is not obviously safe to load from the specified pointer, we do a
71/// quick local scan of the basic block containing ScanFrom, to determine if
72/// the address is already accessed.
74 Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
75 Instruction *ScanFrom, AssumptionCache *AC = nullptr,
76 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
77
78/// Return true if we can prove that the given load (which is assumed to be
79/// within the specified loop) would access only dereferenceable memory, and
80/// be properly aligned on every iteration of the specified loop regardless of
81/// its placement within the loop. (i.e. does not require predication beyond
82/// that required by the header itself and could be hoisted into the header
83/// if desired.) This is more powerful than the variants above when the
84/// address loaded from is analyzeable by SCEV.
87 AssumptionCache *AC = nullptr,
88 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
89
90/// Returns true if the loop contains read-only memory accesses and doesn't
91/// throw. Puts loads that may fault into \p NonDereferenceableAndAlignedLoads.
92LLVM_ABI bool
95 SmallVectorImpl<LoadInst *> &NonDereferenceableAndAlignedLoads,
96 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
97
98/// Return true if we know that executing a load from this value cannot trap.
99///
100/// If DT and ScanFrom are specified this method performs context-sensitive
101/// analysis and returns true if it is safe to load immediately before ScanFrom.
102///
103/// If it is not obviously safe to load from the specified pointer, we do a
104/// quick local scan of the basic block containing ScanFrom, to determine if
105/// the address is already accessed.
107 Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
108 Instruction *ScanFrom, AssumptionCache *AC = nullptr,
109 const DominatorTree *DT = nullptr, const TargetLibraryInfo *TLI = nullptr);
110
111/// Return true if speculation of the given load must be suppressed to avoid
112/// ordering or interfering with an active sanitizer. If not suppressed,
113/// dereferenceability and alignment must be proven separately. Note: This
114/// is only needed for raw reasoning; if you use the interface below
115/// (isSafeToSpeculativelyExecute), this is handled internally.
117
118/// The default number of maximum instructions to scan in the block, used by
119/// FindAvailableLoadedValue().
121
122/// Scan backwards to see if we have the value of the given load available
123/// locally within a small number of instructions.
124///
125/// You can use this function to scan across multiple blocks: after you call
126/// this function, if ScanFrom points at the beginning of the block, it's safe
127/// to continue scanning the predecessors.
128///
129/// Note that performing load CSE requires special care to make sure the
130/// metadata is set appropriately. In particular, aliasing metadata needs
131/// to be merged. (This doesn't matter for store-to-load forwarding because
132/// the only relevant load gets deleted.)
133///
134/// \param Load The load we want to replace.
135/// \param ScanBB The basic block to scan.
136/// \param [in,out] ScanFrom The location to start scanning from. When this
137/// function returns, it points at the last instruction scanned.
138/// \param MaxInstsToScan The maximum number of instructions to scan. If this
139/// is zero, the whole block will be scanned.
140/// \param AA Optional pointer to alias analysis, to make the scan more
141/// precise.
142/// \param [out] IsLoadCSE Whether the returned value is a load from the same
143/// location in memory, as opposed to the value operand of a store.
144///
145/// \returns The found value, or nullptr if no value is found.
147 LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom,
148 unsigned MaxInstsToScan = DefMaxInstsToScan, BatchAAResults *AA = nullptr,
149 bool *IsLoadCSE = nullptr, unsigned *NumScanedInst = nullptr);
150
151/// This overload provides a more efficient implementation of
152/// FindAvailableLoadedValue() for the case where we are not interested in
153/// finding the closest clobbering instruction if no available load is found.
154/// This overload cannot be used to scan across multiple blocks.
156FindAvailableLoadedValue(LoadInst *Load, BatchAAResults &AA, bool *IsLoadCSE,
157 unsigned MaxInstsToScan = DefMaxInstsToScan);
158
159/// Scan backwards to see if we have the value of the given pointer available
160/// locally within a small number of instructions.
161///
162/// You can use this function to scan across multiple blocks: after you call
163/// this function, if ScanFrom points at the beginning of the block, it's safe
164/// to continue scanning the predecessors.
165///
166/// \param Loc The location we want the load and store to originate from.
167/// \param AccessTy The access type of the pointer.
168/// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
169/// case it is false, we can return an atomic or non-atomic load or store. In
170/// case it is true, we need to return an atomic load or store.
171/// \param ScanBB The basic block to scan.
172/// \param [in,out] ScanFrom The location to start scanning from. When this
173/// function returns, it points at the last instruction scanned.
174/// \param MaxInstsToScan The maximum number of instructions to scan. If this
175/// is zero, the whole block will be scanned.
176/// \param AA Optional pointer to alias analysis, to make the scan more
177/// precise.
178/// \param [out] IsLoadCSE Whether the returned value is a load from the same
179/// location in memory, as opposed to the value operand of a store.
180///
181/// \returns The found value, or nullptr if no value is found.
183 const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic,
184 BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan,
185 BatchAAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst);
186
187/// Returns true if a pointer value \p From can be replaced with another pointer
188/// value \To if they are deemed equal through some means (e.g. information from
189/// conditions).
190/// NOTE: The current implementation allows replacement in Icmp and PtrToInt
191/// instructions, as well as when we are replacing with a null pointer.
192/// Additionally it also allows replacement of pointers when both pointers have
193/// the same underlying object.
194LLVM_ABI bool canReplacePointersIfEqual(const Value *From, const Value *To,
195 const DataLayout &DL);
196LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To,
197 const DataLayout &DL);
198
199/// Linear expression BasePtr + Index * Scale + Offset.
200/// Index, Scale and Offset all have the same bit width, which matches the
201/// pointer index size of BasePtr.
202/// Index may be nullptr if Scale is 0.
213
214/// Decompose a pointer into a linear expression. This may look through
215/// multiple GEPs.
216LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL,
217 Value *Ptr);
218}
219
220#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:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
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.
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.
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:671
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:416
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:538
LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To, const DataLayout &DL)
Definition Loads.cpp:837
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:853
LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL, Value *Ptr)
Decompose a pointer into a linear expression.
Definition Loads.cpp:881
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:431
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:863
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:210
GEPNoWrapFlags Flags
Definition Loads.h:208