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/IR/BasicBlock.h" 17 #include "llvm/Support/CommandLine.h" 18 19 namespace llvm { 20 21 class AAResults; 22 class DataLayout; 23 class DominatorTree; 24 class Instruction; 25 class LoadInst; 26 class Loop; 27 class MemoryLocation; 28 class ScalarEvolution; 29 class TargetLibraryInfo; 30 31 /// Return true if this is always a dereferenceable pointer. If the context 32 /// instruction is specified perform context-sensitive analysis and return true 33 /// if the pointer is dereferenceable at the specified instruction. 34 bool isDereferenceablePointer(const Value *V, Type *Ty, 35 const DataLayout &DL, 36 const Instruction *CtxI = nullptr, 37 const DominatorTree *DT = nullptr, 38 const TargetLibraryInfo *TLI = nullptr); 39 40 /// Returns true if V is always a dereferenceable pointer with alignment 41 /// greater or equal than requested. If the context instruction is specified 42 /// performs context-sensitive analysis and returns true if the pointer is 43 /// dereferenceable at the specified instruction. 44 bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, 45 Align Alignment, const DataLayout &DL, 46 const Instruction *CtxI = nullptr, 47 const DominatorTree *DT = nullptr, 48 const TargetLibraryInfo *TLI = nullptr); 49 50 /// Returns true if V is always dereferenceable for Size byte with alignment 51 /// greater or equal than requested. If the context instruction is specified 52 /// performs context-sensitive analysis and returns true if the pointer is 53 /// dereferenceable at the specified instruction. 54 bool isDereferenceableAndAlignedPointer(const Value *V, Align Alignment, 55 const APInt &Size, const DataLayout &DL, 56 const Instruction *CtxI = nullptr, 57 const DominatorTree *DT = nullptr, 58 const TargetLibraryInfo *TLI = nullptr); 59 60 /// Return true if we know that executing a load from this value cannot trap. 61 /// 62 /// If DT and ScanFrom are specified this method performs context-sensitive 63 /// analysis and returns true if it is safe to load immediately before ScanFrom. 64 /// 65 /// If it is not obviously safe to load from the specified pointer, we do a 66 /// quick local scan of the basic block containing ScanFrom, to determine if 67 /// the address is already accessed. 68 bool isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size, 69 const DataLayout &DL, 70 Instruction *ScanFrom = nullptr, 71 const DominatorTree *DT = nullptr, 72 const TargetLibraryInfo *TLI = nullptr); 73 74 /// Return true if we can prove that the given load (which is assumed to be 75 /// within the specified loop) would access only dereferenceable memory, and 76 /// be properly aligned on every iteration of the specified loop regardless of 77 /// its placement within the loop. (i.e. does not require predication beyond 78 /// that required by the header itself and could be hoisted into the header 79 /// if desired.) This is more powerful than the variants above when the 80 /// address loaded from is analyzeable by SCEV. 81 bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, 82 ScalarEvolution &SE, 83 DominatorTree &DT); 84 85 /// Return true if we know that executing a load from this value cannot trap. 86 /// 87 /// If DT and ScanFrom are specified this method performs context-sensitive 88 /// analysis and returns true if it is safe to load immediately before ScanFrom. 89 /// 90 /// If it is not obviously safe to load from the specified pointer, we do a 91 /// quick local scan of the basic block containing ScanFrom, to determine if 92 /// the address is already accessed. 93 bool isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment, 94 const DataLayout &DL, 95 Instruction *ScanFrom = nullptr, 96 const DominatorTree *DT = nullptr, 97 const TargetLibraryInfo *TLI = nullptr); 98 99 /// The default number of maximum instructions to scan in the block, used by 100 /// FindAvailableLoadedValue(). 101 extern cl::opt<unsigned> DefMaxInstsToScan; 102 103 /// Scan backwards to see if we have the value of the given load available 104 /// locally within a small number of instructions. 105 /// 106 /// You can use this function to scan across multiple blocks: after you call 107 /// this function, if ScanFrom points at the beginning of the block, it's safe 108 /// to continue scanning the predecessors. 109 /// 110 /// Note that performing load CSE requires special care to make sure the 111 /// metadata is set appropriately. In particular, aliasing metadata needs 112 /// to be merged. (This doesn't matter for store-to-load forwarding because 113 /// the only relevant load gets deleted.) 114 /// 115 /// \param Load The load we want to replace. 116 /// \param ScanBB The basic block to scan. 117 /// \param [in,out] ScanFrom The location to start scanning from. When this 118 /// function returns, it points at the last instruction scanned. 119 /// \param MaxInstsToScan The maximum number of instructions to scan. If this 120 /// is zero, the whole block will be scanned. 121 /// \param AA Optional pointer to alias analysis, to make the scan more 122 /// precise. 123 /// \param [out] IsLoadCSE Whether the returned value is a load from the same 124 /// location in memory, as opposed to the value operand of a store. 125 /// 126 /// \returns The found value, or nullptr if no value is found. 127 Value *FindAvailableLoadedValue(LoadInst *Load, 128 BasicBlock *ScanBB, 129 BasicBlock::iterator &ScanFrom, 130 unsigned MaxInstsToScan = DefMaxInstsToScan, 131 AAResults *AA = nullptr, 132 bool *IsLoadCSE = nullptr, 133 unsigned *NumScanedInst = nullptr); 134 135 /// This overload provides a more efficient implementation of 136 /// FindAvailableLoadedValue() for the case where we are not interested in 137 /// finding the closest clobbering instruction if no available load is found. 138 /// This overload cannot be used to scan across multiple blocks. 139 Value *FindAvailableLoadedValue(LoadInst *Load, AAResults &AA, bool *IsLoadCSE, 140 unsigned MaxInstsToScan = DefMaxInstsToScan); 141 142 /// Scan backwards to see if we have the value of the given pointer available 143 /// locally within a small number of instructions. 144 /// 145 /// You can use this function to scan across multiple blocks: after you call 146 /// this function, if ScanFrom points at the beginning of the block, it's safe 147 /// to continue scanning the predecessors. 148 /// 149 /// \param Loc The location we want the load and store to originate from. 150 /// \param AccessTy The access type of the pointer. 151 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In 152 /// case it is false, we can return an atomic or non-atomic load or store. In 153 /// case it is true, we need to return an atomic load or store. 154 /// \param ScanBB The basic block to scan. 155 /// \param [in,out] ScanFrom The location to start scanning from. When this 156 /// function returns, it points at the last instruction scanned. 157 /// \param MaxInstsToScan The maximum number of instructions to scan. If this 158 /// is zero, the whole block will be scanned. 159 /// \param AA Optional pointer to alias analysis, to make the scan more 160 /// precise. 161 /// \param [out] IsLoadCSE Whether the returned value is a load from the same 162 /// location in memory, as opposed to the value operand of a store. 163 /// 164 /// \returns The found value, or nullptr if no value is found. 165 Value *findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, 166 bool AtLeastAtomic, BasicBlock *ScanBB, 167 BasicBlock::iterator &ScanFrom, 168 unsigned MaxInstsToScan, AAResults *AA, 169 bool *IsLoadCSE, unsigned *NumScanedInst); 170 171 /// Returns true if a pointer value \p A can be replace with another pointer 172 /// value \B if they are deemed equal through some means (e.g. information from 173 /// conditions). 174 /// NOTE: the current implementations is incomplete and unsound. It does not 175 /// reject all invalid cases yet, but will be made stricter in the future. In 176 /// particular this means returning true means unknown if replacement is safe. 177 bool canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL, 178 Instruction *CtxI); 179 } 180 181 #endif 182