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