LLVM 24.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"
18#include "llvm/IR/BasicBlock.h"
22
23namespace llvm {
24
25class BatchAAResults;
26class AssumptionCache;
27class DataLayout;
28class DominatorTree;
29class Instruction;
30class LoadInst;
32class Loop;
33class MemoryLocation;
34class SCEV;
35class ScalarEvolution;
36class SCEVPredicate;
37class StoreInst;
38template <typename T> class SmallVectorImpl;
40
41/// Returns true if V is always a dereferenceable pointer with alignment
42/// greater or equal than requested. If the context instruction is specified
43/// performs context-sensitive analysis and returns true if the pointer is
44/// dereferenceable at the specified instruction.
45/// If \p IgnoreFree is set, ignore potential frees of the object.
47 Align Alignment,
48 const SimplifyQuery &Q,
49 bool IgnoreFree = false);
50
51/// Returns true if V is always dereferenceable for Size byte with alignment
52/// greater or equal than requested. If the context instruction is specified
53/// performs context-sensitive analysis and returns true if the pointer is
54/// dereferenceable at the specified instruction.
55/// If \p IgnoreFree is set, ignore potential frees of the object.
57 Align Alignment,
58 const APInt &Size,
59 const SimplifyQuery &Q,
60 bool IgnoreFree = false);
61
62/// Equivalent to isDereferenceableAndAlignedPointer with an alignment of 1.
64 const SimplifyQuery &Q,
65 bool IgnoreFree = false);
66
67/// Equivalent to isDereferenceableAndAlignedPointer with an alignment of 1.
69 const SimplifyQuery &Q,
70 bool IgnoreFree = false);
71
72/// Return true if we know that executing a load from this value cannot trap.
73///
74/// If SQ.CtxI is specified this method performs context-sensitive analysis
75/// and returns true if it is safe to load immediately before SQ.CtxI.
76///
77/// If it is not obviously safe to load from the specified pointer, we do a
78/// quick local scan of the basic block containing SQ.CtxI, to determine if
79/// the address is already accessed.
81 const APInt &Size,
82 const SimplifyQuery &SQ);
83
84/// Return true if we can prove that the given load (which is assumed to be
85/// within the specified loop) would access only dereferenceable memory, and
86/// be properly aligned on every iteration of the specified loop regardless of
87/// its placement within the loop. (i.e. does not require predication beyond
88/// that required by the header itself and could be hoisted into the header
89/// if desired.) This is more powerful than the variants above when the
90/// address loaded from is analyzeable by SCEV.
93 AssumptionCache *AC = nullptr,
94 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
95
96/// Overload for isDereferenceableAndAlignedInLoop taking the pointer and access
97/// size directly as SCEVs.
99 const SCEV *PtrSCEV, Align Alignment, const SCEV *EltSizeSCEV, Loop *L,
100 ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC = nullptr,
101 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
102
103/// Returns true if the loop contains read-only memory accesses and doesn't
104/// throw. Puts loads that may fault into \p NonDereferenceableAndAlignedLoads.
105LLVM_ABI bool
107 AssumptionCache *AC,
108 SmallVectorImpl<LoadInst *> &NonDereferenceableAndAlignedLoads,
109 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
110
111/// Return true if we know that executing a load from this value cannot trap.
112///
113/// If SQ.CtxI is specified this method performs context-sensitive analysis
114/// and returns true if it is safe to load immediately before SQ.CtxI.
115///
116/// If it is not obviously safe to load from the specified pointer, we do a
117/// quick local scan of the basic block containing SQ.CtxI, to determine if
118/// the address is already accessed.
119LLVM_ABI bool isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
120 const SimplifyQuery &SQ);
121
122/// Return true if speculation of the given load must be suppressed to avoid
123/// ordering or interfering with an active sanitizer. If not suppressed,
124/// dereferenceability and alignment must be proven separately. Note: This
125/// is only needed for raw reasoning; if you use the interface below
126/// (isSafeToSpeculativelyExecute), this is handled internally.
128
129/// The default number of maximum instructions to scan in the block, used by
130/// FindAvailableLoadedValue().
132
133/// Scan backwards to see if we have the value of the given load available
134/// locally within a small number of instructions.
135///
136/// You can use this function to scan across multiple blocks: after you call
137/// this function, if ScanFrom points at the beginning of the block, it's safe
138/// to continue scanning the predecessors.
139///
140/// Note that performing load CSE requires special care to make sure the
141/// metadata is set appropriately. In particular, aliasing metadata needs
142/// to be merged. (This doesn't matter for store-to-load forwarding because
143/// the only relevant load gets deleted.)
144///
145/// \param Load The load we want to replace.
146/// \param ScanBB The basic block to scan.
147/// \param [in,out] ScanFrom The location to start scanning from. When this
148/// function returns, it points at the last instruction scanned.
149/// \param MaxInstsToScan The maximum number of instructions to scan. If this
150/// is zero, the whole block will be scanned.
151/// \param AA Optional pointer to alias analysis, to make the scan more
152/// precise.
153/// \param [out] IsLoadCSE Whether the returned value is a load from the same
154/// location in memory, as opposed to the value operand of a store.
155///
156/// \returns The found value, or nullptr if no value is found.
158 LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom,
159 unsigned MaxInstsToScan = DefMaxInstsToScan, BatchAAResults *AA = nullptr,
160 bool *IsLoadCSE = nullptr, unsigned *NumScanedInst = nullptr);
161
162/// This overload provides a more efficient implementation of
163/// FindAvailableLoadedValue() for the case where we are not interested in
164/// finding the closest clobbering instruction if no available load is found.
165/// This overload cannot be used to scan across multiple blocks.
168 unsigned MaxInstsToScan = DefMaxInstsToScan);
169
170/// Check whether \p SI, which may alias \p MemLoc, can be safely skipped.
171/// This is possible when \p SI does only MustAlias or NoAlias \p MemLoc (no
172/// partial overlap possible), and it stores the value \p MemLoc currently
173/// holds (loaded before the store and not modified in between).
175 const MemoryLocation &MemLoc,
176 Align MemLocAlign,
178 unsigned ScanLimit);
179
180/// Scan backwards to see if we have the value of the given pointer available
181/// locally within a small number of instructions.
182///
183/// You can use this function to scan across multiple blocks: after you call
184/// this function, if ScanFrom points at the beginning of the block, it's safe
185/// to continue scanning the predecessors.
186///
187/// \param Loc The location we want the load and store to originate from.
188/// \param AccessTy The access type of the pointer.
189/// \param AccessProps The properties of the load we want to replace.
190/// \param ScanBB The basic block to scan.
191/// \param [in,out] ScanFrom The location to start scanning from. When this
192/// function returns, it points at the last instruction scanned.
193/// \param MaxInstsToScan The maximum number of instructions to scan. If this
194/// is zero, the whole block will be scanned.
195/// \param AA Optional pointer to alias analysis, to make the scan more
196/// precise.
197/// \param [out] IsLoadCSE Whether the returned value is a load from the same
198/// location in memory, as opposed to the value operand of a store.
199///
200/// \returns The found value, or nullptr if no value is found.
203 const LoadStoreInstProperties &AccessProps,
204 BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom,
205 unsigned MaxInstsToScan, BatchAAResults *AA,
206 bool *IsLoadCSE, unsigned *NumScanedInst);
207
208/// Returns true if a pointer value \p From can be replaced with another pointer
209/// value \To if they are deemed equal through some means (e.g. information from
210/// conditions).
211/// NOTE: The current implementation allows replacement in Icmp and PtrToInt
212/// instructions, as well as when we are replacing with a null pointer.
213/// Additionally it also allows replacement of pointers when both pointers have
214/// the same underlying object.
215LLVM_ABI bool canReplacePointersIfEqual(const Value *From, const Value *To,
216 const DataLayout &DL);
217LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To,
218 const DataLayout &DL);
219
220/// Linear expression BasePtr + Index * Scale + Offset.
221/// Index, Scale and Offset all have the same bit width, which matches the
222/// pointer index size of BasePtr.
223/// Index may be nullptr if Scale is 0.
234
235/// Decompose a pointer into a linear expression. This may look through
236/// multiple GEPs.
237LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL,
238 Value *Ptr);
239}
240
241#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:215
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:122
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...
An instruction for storing to memory.
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:46
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.
@ Load
The value being inserted comes from a load (InsertElement only).
LLVM_ABI bool isStorePreservingMemoryLocation(const StoreInst *SI, const MemoryLocation &MemLoc, Align MemLocAlign, BatchAAResults &AA, unsigned ScanLimit)
Check whether SI, which may alias MemLoc, can be safely skipped.
Definition Loads.cpp:843
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:452
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:552
LLVM_ABI bool canReplacePointersInUseIfEqual(const Use &U, const Value *To, const DataLayout &DL)
Definition Loads.cpp:924
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:944
LLVM_ABI LinearExpression decomposeLinearExpression(const DataLayout &DL, Value *Ptr)
Decompose a pointer into a linear expression.
Definition Loads.cpp:972
LLVM_ABI cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const SimplifyQuery &Q, bool IgnoreFree=false)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:244
constexpr unsigned BitWidth
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:954
LLVM_ABI bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size, const SimplifyQuery &SQ)
Return true if we know that executing a load from this value cannot trap.
Definition Loads.cpp:456
LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty, const SimplifyQuery &Q, bool IgnoreFree=false)
Equivalent to isDereferenceableAndAlignedPointer with an alignment of 1.
Definition Loads.cpp:264
LLVM_ABI Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, const LoadStoreInstProperties &AccessProps, 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:713
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:304
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:231
GEPNoWrapFlags Flags
Definition Loads.h:229
A structure representing the properties of a load or store instruction.