1 //===- Loads.h - Local load analysis --------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares simple local analyses for load instructions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_LOADS_H
15 #define LLVM_ANALYSIS_LOADS_H
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/Support/CommandLine.h"
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);
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,
39 const Instruction *CtxI = nullptr,
40 const DominatorTree *DT = nullptr);
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);
51 /// Return true if we know that executing a load from this value cannot trap.
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.
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,
61 Instruction *ScanFrom = nullptr,
62 const DominatorTree *DT = nullptr);
64 /// The default number of maximum instructions to scan in the block, used by
65 /// FindAvailableLoadedValue().
66 extern cl::opt<unsigned> DefMaxInstsToScan;
68 /// Scan backwards to see if we have the value of the given load available
69 /// locally within a small number of instructions.
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.
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.)
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
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.
91 /// \returns The found value, or nullptr if no value is found.
92 Value *FindAvailableLoadedValue(LoadInst *Load,
94 BasicBlock::iterator &ScanFrom,
95 unsigned MaxInstsToScan = DefMaxInstsToScan,
96 AliasAnalysis *AA = nullptr,
97 bool *IsLoadCSE = nullptr,
98 unsigned *NumScanedInst = nullptr);
100 /// Scan backwards to see if we have the value of the given pointer available
101 /// locally within a small number of instructions.
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.
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
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.
122 /// \returns The found value, or nullptr if no value is found.
123 Value *FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic,
125 BasicBlock::iterator &ScanFrom,
126 unsigned MaxInstsToScan, AliasAnalysis *AA,
127 bool *IsLoad, unsigned *NumScanedInst);