1 //===- Loads.h - Local load analysis --------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares simple local analyses for load instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_ANALYSIS_LOADS_H 15 #define LLVM_ANALYSIS_LOADS_H 16 17 #include "llvm/Analysis/AliasAnalysis.h" 18 #include "llvm/IR/BasicBlock.h" 19 #include "llvm/Support/CommandLine.h" 20 21 namespace llvm { 22 23 class DataLayout; 24 class MDNode; 25 26 /// Return true if this is always a dereferenceable pointer. If the context 27 /// instruction is specified perform context-sensitive analysis and return true 28 /// if the pointer is dereferenceable at the specified instruction. 29 bool isDereferenceablePointer(const Value *V, const DataLayout &DL, 30 const Instruction *CtxI = nullptr, 31 const DominatorTree *DT = nullptr); 32 33 /// Returns true if V is always a dereferenceable pointer with alignment 34 /// greater or equal than requested. If the context instruction is specified 35 /// performs context-sensitive analysis and returns true if the pointer is 36 /// dereferenceable at the specified instruction. 37 bool isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, 38 const DataLayout &DL, 39 const Instruction *CtxI = nullptr, 40 const DominatorTree *DT = nullptr); 41 42 /// Returns true if V is always dereferenceable for Size byte with alignment 43 /// greater or equal than requested. If the context instruction is specified 44 /// performs context-sensitive analysis and returns true if the pointer is 45 /// dereferenceable at the specified instruction. 46 bool isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, 47 const APInt &Size, const DataLayout &DL, 48 const Instruction *CtxI = nullptr, 49 const DominatorTree *DT = nullptr); 50 51 /// Return true if we know that executing a load from this value cannot trap. 52 /// 53 /// If DT and ScanFrom are specified this method performs context-sensitive 54 /// analysis and returns true if it is safe to load immediately before ScanFrom. 55 /// 56 /// If it is not obviously safe to load from the specified pointer, we do a 57 /// quick local scan of the basic block containing ScanFrom, to determine if 58 /// the address is already accessed. 59 bool isSafeToLoadUnconditionally(Value *V, unsigned Align, 60 const DataLayout &DL, 61 Instruction *ScanFrom = nullptr, 62 const DominatorTree *DT = nullptr); 63 64 /// The default number of maximum instructions to scan in the block, used by 65 /// FindAvailableLoadedValue(). 66 extern cl::opt<unsigned> DefMaxInstsToScan; 67 68 /// Scan backwards to see if we have the value of the given load available 69 /// locally within a small number of instructions. 70 /// 71 /// You can use this function to scan across multiple blocks: after you call 72 /// this function, if ScanFrom points at the beginning of the block, it's safe 73 /// to continue scanning the predecessors. 74 /// 75 /// Note that performing load CSE requires special care to make sure the 76 /// metadata is set appropriately. In particular, aliasing metadata needs 77 /// to be merged. (This doesn't matter for store-to-load forwarding because 78 /// the only relevant load gets deleted.) 79 /// 80 /// \param Load The load we want to replace. 81 /// \param ScanBB The basic block to scan. 82 /// \param [in,out] ScanFrom The location to start scanning from. When this 83 /// function returns, it points at the last instruction scanned. 84 /// \param MaxInstsToScan The maximum number of instructions to scan. If this 85 /// is zero, the whole block will be scanned. 86 /// \param AA Optional pointer to alias analysis, to make the scan more 87 /// precise. 88 /// \param [out] IsLoadCSE Whether the returned value is a load from the same 89 /// location in memory, as opposed to the value operand of a store. 90 /// 91 /// \returns The found value, or nullptr if no value is found. 92 Value *FindAvailableLoadedValue(LoadInst *Load, 93 BasicBlock *ScanBB, 94 BasicBlock::iterator &ScanFrom, 95 unsigned MaxInstsToScan = DefMaxInstsToScan, 96 AliasAnalysis *AA = nullptr, 97 bool *IsLoadCSE = nullptr, 98 unsigned *NumScanedInst = nullptr); 99 100 /// Scan backwards to see if we have the value of the given pointer available 101 /// locally within a small number of instructions. 102 /// 103 /// You can use this function to scan across multiple blocks: after you call 104 /// this function, if ScanFrom points at the beginning of the block, it's safe 105 /// to continue scanning the predecessors. 106 /// 107 /// \param Ptr The pointer we want the load and store to originate from. 108 /// \param AccessTy The access type of the pointer. 109 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In 110 /// case it is false, we can return an atomic or non-atomic load or store. In 111 /// case it is true, we need to return an atomic load or store. 112 /// \param ScanBB The basic block to scan. 113 /// \param [in,out] ScanFrom The location to start scanning from. When this 114 /// function returns, it points at the last instruction scanned. 115 /// \param MaxInstsToScan The maximum number of instructions to scan. If this 116 /// is zero, the whole block will be scanned. 117 /// \param AA Optional pointer to alias analysis, to make the scan more 118 /// precise. 119 /// \param [out] IsLoad Whether the returned value is a load from the same 120 /// location in memory, as opposed to the value operand of a store. 121 /// 122 /// \returns The found value, or nullptr if no value is found. 123 Value *FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic, 124 BasicBlock *ScanBB, 125 BasicBlock::iterator &ScanFrom, 126 unsigned MaxInstsToScan, AliasAnalysis *AA, 127 bool *IsLoad, unsigned *NumScanedInst); 128 } 129 130 #endif 131