1 //===- Loads.h - Local load analysis --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares simple local analyses for load instructions.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ANALYSIS_LOADS_H
14 #define LLVM_ANALYSIS_LOADS_H
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/Support/CommandLine.h"
25 class ScalarEvolution
;
27 /// Return true if this is always a dereferenceable pointer. If the context
28 /// instruction is specified perform context-sensitive analysis and return true
29 /// if the pointer is dereferenceable at the specified instruction.
30 bool isDereferenceablePointer(const Value
*V
, Type
*Ty
,
32 const Instruction
*CtxI
= nullptr,
33 const DominatorTree
*DT
= nullptr);
35 /// Returns true if V is always a dereferenceable pointer with alignment
36 /// greater or equal than requested. If the context instruction is specified
37 /// performs context-sensitive analysis and returns true if the pointer is
38 /// dereferenceable at the specified instruction.
39 bool isDereferenceableAndAlignedPointer(const Value
*V
, Type
*Ty
,
42 const Instruction
*CtxI
= nullptr,
43 const DominatorTree
*DT
= nullptr);
45 /// Returns true if V is always dereferenceable for Size byte with alignment
46 /// greater or equal than requested. If the context instruction is specified
47 /// performs context-sensitive analysis and returns true if the pointer is
48 /// dereferenceable at the specified instruction.
49 bool isDereferenceableAndAlignedPointer(const Value
*V
, Align Alignment
,
50 const APInt
&Size
, const DataLayout
&DL
,
51 const Instruction
*CtxI
= nullptr,
52 const DominatorTree
*DT
= nullptr);
54 /// Return true if we know that executing a load from this value cannot trap.
56 /// If DT and ScanFrom are specified this method performs context-sensitive
57 /// analysis and returns true if it is safe to load immediately before ScanFrom.
59 /// If it is not obviously safe to load from the specified pointer, we do a
60 /// quick local scan of the basic block containing ScanFrom, to determine if
61 /// the address is already accessed.
62 bool isSafeToLoadUnconditionally(Value
*V
, MaybeAlign Alignment
, APInt
&Size
,
64 Instruction
*ScanFrom
= nullptr,
65 const DominatorTree
*DT
= nullptr);
67 /// Return true if we can prove that the given load (which is assumed to be
68 /// within the specified loop) would access only dereferenceable memory, and
69 /// be properly aligned on every iteration of the specified loop regardless of
70 /// its placement within the loop. (i.e. does not require predication beyond
71 /// that required by the the header itself and could be hoisted into the header
72 /// if desired.) This is more powerful than the variants above when the
73 /// address loaded from is analyzeable by SCEV.
74 bool isDereferenceableAndAlignedInLoop(LoadInst
*LI
, Loop
*L
,
78 /// Return true if we know that executing a load from this value cannot trap.
80 /// If DT and ScanFrom are specified this method performs context-sensitive
81 /// analysis and returns true if it is safe to load immediately before ScanFrom.
83 /// If it is not obviously safe to load from the specified pointer, we do a
84 /// quick local scan of the basic block containing ScanFrom, to determine if
85 /// the address is already accessed.
86 bool isSafeToLoadUnconditionally(Value
*V
, Type
*Ty
, MaybeAlign Alignment
,
88 Instruction
*ScanFrom
= nullptr,
89 const DominatorTree
*DT
= nullptr);
91 /// The default number of maximum instructions to scan in the block, used by
92 /// FindAvailableLoadedValue().
93 extern cl::opt
<unsigned> DefMaxInstsToScan
;
95 /// Scan backwards to see if we have the value of the given load available
96 /// locally within a small number of instructions.
98 /// You can use this function to scan across multiple blocks: after you call
99 /// this function, if ScanFrom points at the beginning of the block, it's safe
100 /// to continue scanning the predecessors.
102 /// Note that performing load CSE requires special care to make sure the
103 /// metadata is set appropriately. In particular, aliasing metadata needs
104 /// to be merged. (This doesn't matter for store-to-load forwarding because
105 /// the only relevant load gets deleted.)
107 /// \param Load The load we want to replace.
108 /// \param ScanBB The basic block to scan.
109 /// \param [in,out] ScanFrom The location to start scanning from. When this
110 /// function returns, it points at the last instruction scanned.
111 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
112 /// is zero, the whole block will be scanned.
113 /// \param AA Optional pointer to alias analysis, to make the scan more
115 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
116 /// location in memory, as opposed to the value operand of a store.
118 /// \returns The found value, or nullptr if no value is found.
119 Value
*FindAvailableLoadedValue(LoadInst
*Load
,
121 BasicBlock::iterator
&ScanFrom
,
122 unsigned MaxInstsToScan
= DefMaxInstsToScan
,
123 AliasAnalysis
*AA
= nullptr,
124 bool *IsLoadCSE
= nullptr,
125 unsigned *NumScanedInst
= nullptr);
127 /// Scan backwards to see if we have the value of the given pointer available
128 /// locally within a small number of instructions.
130 /// You can use this function to scan across multiple blocks: after you call
131 /// this function, if ScanFrom points at the beginning of the block, it's safe
132 /// to continue scanning the predecessors.
134 /// \param Ptr The pointer we want the load and store to originate from.
135 /// \param AccessTy The access type of the pointer.
136 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
137 /// case it is false, we can return an atomic or non-atomic load or store. In
138 /// case it is true, we need to return an atomic load or store.
139 /// \param ScanBB The basic block to scan.
140 /// \param [in,out] ScanFrom The location to start scanning from. When this
141 /// function returns, it points at the last instruction scanned.
142 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
143 /// is zero, the whole block will be scanned.
144 /// \param AA Optional pointer to alias analysis, to make the scan more
146 /// \param [out] IsLoad Whether the returned value is a load from the same
147 /// location in memory, as opposed to the value operand of a store.
149 /// \returns The found value, or nullptr if no value is found.
150 Value
*FindAvailablePtrLoadStore(Value
*Ptr
, Type
*AccessTy
, bool AtLeastAtomic
,
152 BasicBlock::iterator
&ScanFrom
,
153 unsigned MaxInstsToScan
, AliasAnalysis
*AA
,
154 bool *IsLoad
, unsigned *NumScanedInst
);