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