Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:14

0001 //===- Loads.h - Local load analysis --------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares simple local analyses for load instructions.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_ANALYSIS_LOADS_H
0014 #define LLVM_ANALYSIS_LOADS_H
0015 
0016 #include "llvm/IR/BasicBlock.h"
0017 #include "llvm/Support/CommandLine.h"
0018 
0019 namespace llvm {
0020 
0021 class BatchAAResults;
0022 class AssumptionCache;
0023 class DataLayout;
0024 class DominatorTree;
0025 class Instruction;
0026 class LoadInst;
0027 class Loop;
0028 class MemoryLocation;
0029 class ScalarEvolution;
0030 class SCEVPredicate;
0031 template <typename T> class SmallVectorImpl;
0032 class TargetLibraryInfo;
0033 
0034 /// Return true if this is always a dereferenceable pointer. If the context
0035 /// instruction is specified perform context-sensitive analysis and return true
0036 /// if the pointer is dereferenceable at the specified instruction.
0037 bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL,
0038                               const Instruction *CtxI = nullptr,
0039                               AssumptionCache *AC = nullptr,
0040                               const DominatorTree *DT = nullptr,
0041                               const TargetLibraryInfo *TLI = nullptr);
0042 
0043 /// Returns true if V is always a dereferenceable pointer with alignment
0044 /// greater or equal than requested. If the context instruction is specified
0045 /// performs context-sensitive analysis and returns true if the pointer is
0046 /// dereferenceable at the specified instruction.
0047 bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
0048                                         Align Alignment, const DataLayout &DL,
0049                                         const Instruction *CtxI = nullptr,
0050                                         AssumptionCache *AC = nullptr,
0051                                         const DominatorTree *DT = nullptr,
0052                                         const TargetLibraryInfo *TLI = nullptr);
0053 
0054 /// Returns true if V is always dereferenceable for Size byte with alignment
0055 /// greater or equal than requested. If the context instruction is specified
0056 /// performs context-sensitive analysis and returns true if the pointer is
0057 /// dereferenceable at the specified instruction.
0058 bool isDereferenceableAndAlignedPointer(const Value *V, Align Alignment,
0059                                         const APInt &Size, const DataLayout &DL,
0060                                         const Instruction *CtxI = nullptr,
0061                                         AssumptionCache *AC = nullptr,
0062                                         const DominatorTree *DT = nullptr,
0063                                         const TargetLibraryInfo *TLI = nullptr);
0064 
0065 /// Return true if we know that executing a load from this value cannot trap.
0066 ///
0067 /// If DT and ScanFrom are specified this method performs context-sensitive
0068 /// analysis and returns true if it is safe to load immediately before ScanFrom.
0069 ///
0070 /// If it is not obviously safe to load from the specified pointer, we do a
0071 /// quick local scan of the basic block containing ScanFrom, to determine if
0072 /// the address is already accessed.
0073 bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size,
0074                                  const DataLayout &DL, Instruction *ScanFrom,
0075                                  AssumptionCache *AC = nullptr,
0076                                  const DominatorTree *DT = nullptr,
0077                                  const TargetLibraryInfo *TLI = nullptr);
0078 
0079 /// Return true if we can prove that the given load (which is assumed to be
0080 /// within the specified loop) would access only dereferenceable memory, and
0081 /// be properly aligned on every iteration of the specified loop regardless of
0082 /// its placement within the loop. (i.e. does not require predication beyond
0083 /// that required by the header itself and could be hoisted into the header
0084 /// if desired.)  This is more powerful than the variants above when the
0085 /// address loaded from is analyzeable by SCEV.
0086 bool isDereferenceableAndAlignedInLoop(
0087     LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT,
0088     AssumptionCache *AC = nullptr,
0089     SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
0090 
0091 /// Return true if the loop \p L cannot fault on any iteration and only
0092 /// contains read-only memory accesses.
0093 bool isDereferenceableReadOnlyLoop(
0094     Loop *L, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
0095     SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
0096 
0097 /// Return true if we know that executing a load from this value cannot trap.
0098 ///
0099 /// If DT and ScanFrom are specified this method performs context-sensitive
0100 /// analysis and returns true if it is safe to load immediately before ScanFrom.
0101 ///
0102 /// If it is not obviously safe to load from the specified pointer, we do a
0103 /// quick local scan of the basic block containing ScanFrom, to determine if
0104 /// the address is already accessed.
0105 bool isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
0106                                  const DataLayout &DL, Instruction *ScanFrom,
0107                                  AssumptionCache *AC = nullptr,
0108                                  const DominatorTree *DT = nullptr,
0109                                  const TargetLibraryInfo *TLI = nullptr);
0110 
0111 /// Return true if speculation of the given load must be suppressed to avoid
0112 /// ordering or interfering with an active sanitizer.  If not suppressed,
0113 /// dereferenceability and alignment must be proven separately.  Note: This
0114 /// is only needed for raw reasoning; if you use the interface below
0115 /// (isSafeToSpeculativelyExecute), this is handled internally.
0116 bool mustSuppressSpeculation(const LoadInst &LI);
0117 
0118 /// The default number of maximum instructions to scan in the block, used by
0119 /// FindAvailableLoadedValue().
0120 extern cl::opt<unsigned> DefMaxInstsToScan;
0121 
0122 /// Scan backwards to see if we have the value of the given load available
0123 /// locally within a small number of instructions.
0124 ///
0125 /// You can use this function to scan across multiple blocks: after you call
0126 /// this function, if ScanFrom points at the beginning of the block, it's safe
0127 /// to continue scanning the predecessors.
0128 ///
0129 /// Note that performing load CSE requires special care to make sure the
0130 /// metadata is set appropriately.  In particular, aliasing metadata needs
0131 /// to be merged.  (This doesn't matter for store-to-load forwarding because
0132 /// the only relevant load gets deleted.)
0133 ///
0134 /// \param Load The load we want to replace.
0135 /// \param ScanBB The basic block to scan.
0136 /// \param [in,out] ScanFrom The location to start scanning from. When this
0137 /// function returns, it points at the last instruction scanned.
0138 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
0139 /// is zero, the whole block will be scanned.
0140 /// \param AA Optional pointer to alias analysis, to make the scan more
0141 /// precise.
0142 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
0143 /// location in memory, as opposed to the value operand of a store.
0144 ///
0145 /// \returns The found value, or nullptr if no value is found.
0146 Value *FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
0147                                 BasicBlock::iterator &ScanFrom,
0148                                 unsigned MaxInstsToScan = DefMaxInstsToScan,
0149                                 BatchAAResults *AA = nullptr,
0150                                 bool *IsLoadCSE = nullptr,
0151                                 unsigned *NumScanedInst = nullptr);
0152 
0153 /// This overload provides a more efficient implementation of
0154 /// FindAvailableLoadedValue() for the case where we are not interested in
0155 /// finding the closest clobbering instruction if no available load is found.
0156 /// This overload cannot be used to scan across multiple blocks.
0157 Value *FindAvailableLoadedValue(LoadInst *Load, BatchAAResults &AA,
0158                                 bool *IsLoadCSE,
0159                                 unsigned MaxInstsToScan = DefMaxInstsToScan);
0160 
0161 /// Scan backwards to see if we have the value of the given pointer available
0162 /// locally within a small number of instructions.
0163 ///
0164 /// You can use this function to scan across multiple blocks: after you call
0165 /// this function, if ScanFrom points at the beginning of the block, it's safe
0166 /// to continue scanning the predecessors.
0167 ///
0168 /// \param Loc The location we want the load and store to originate from.
0169 /// \param AccessTy The access type of the pointer.
0170 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
0171 /// case it is false, we can return an atomic or non-atomic load or store. In
0172 /// case it is true, we need to return an atomic load or store.
0173 /// \param ScanBB The basic block to scan.
0174 /// \param [in,out] ScanFrom The location to start scanning from. When this
0175 /// function returns, it points at the last instruction scanned.
0176 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
0177 /// is zero, the whole block will be scanned.
0178 /// \param AA Optional pointer to alias analysis, to make the scan more
0179 /// precise.
0180 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
0181 /// location in memory, as opposed to the value operand of a store.
0182 ///
0183 /// \returns The found value, or nullptr if no value is found.
0184 Value *findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy,
0185                                  bool AtLeastAtomic, BasicBlock *ScanBB,
0186                                  BasicBlock::iterator &ScanFrom,
0187                                  unsigned MaxInstsToScan, BatchAAResults *AA,
0188                                  bool *IsLoadCSE, unsigned *NumScanedInst);
0189 
0190 /// Returns true if a pointer value \p From can be replaced with another pointer
0191 /// value \To if they are deemed equal through some means (e.g. information from
0192 /// conditions).
0193 /// NOTE: The current implementation allows replacement in Icmp and PtrToInt
0194 /// instructions, as well as when we are replacing with a null pointer.
0195 /// Additionally it also allows replacement of pointers when both pointers have
0196 /// the same underlying object.
0197 bool canReplacePointersIfEqual(const Value *From, const Value *To,
0198                                const DataLayout &DL);
0199 bool canReplacePointersInUseIfEqual(const Use &U, const Value *To,
0200                                     const DataLayout &DL);
0201 }
0202 
0203 #endif