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
,
40 unsigned Align
, const DataLayout
&DL
,
41 const Instruction
*CtxI
= nullptr,
42 const DominatorTree
*DT
= nullptr);
44 /// Returns true if V is always dereferenceable for Size byte with alignment
45 /// greater or equal than requested. If the context instruction is specified
46 /// performs context-sensitive analysis and returns true if the pointer is
47 /// dereferenceable at the specified instruction.
48 bool isDereferenceableAndAlignedPointer(const Value
*V
, unsigned Align
,
49 const APInt
&Size
, const DataLayout
&DL
,
50 const Instruction
*CtxI
= nullptr,
51 const DominatorTree
*DT
= nullptr);
53 /// Return true if we know that executing a load from this value cannot trap.
55 /// If DT and ScanFrom are specified this method performs context-sensitive
56 /// analysis and returns true if it is safe to load immediately before ScanFrom.
58 /// If it is not obviously safe to load from the specified pointer, we do a
59 /// quick local scan of the basic block containing ScanFrom, to determine if
60 /// the address is already accessed.
61 bool isSafeToLoadUnconditionally(Value
*V
, unsigned Align
, APInt
&Size
,
63 Instruction
*ScanFrom
= nullptr,
64 const DominatorTree
*DT
= nullptr);
66 /// Return true if we can prove that the given load (which is assumed to be
67 /// within the specified loop) would access only dereferenceable memory, and
68 /// be properly aligned on every iteration of the specified loop regardless of
69 /// its placement within the loop. (i.e. does not require predication beyond
70 /// that required by the the header itself and could be hoisted into the header
71 /// if desired.) This is more powerful than the variants above when the
72 /// address loaded from is analyzeable by SCEV.
73 bool isDereferenceableAndAlignedInLoop(LoadInst
*LI
, Loop
*L
,
77 /// Return true if we know that executing a load from this value cannot trap.
79 /// If DT and ScanFrom are specified this method performs context-sensitive
80 /// analysis and returns true if it is safe to load immediately before ScanFrom.
82 /// If it is not obviously safe to load from the specified pointer, we do a
83 /// quick local scan of the basic block containing ScanFrom, to determine if
84 /// the address is already accessed.
85 bool isSafeToLoadUnconditionally(Value
*V
, Type
*Ty
, unsigned Align
,
87 Instruction
*ScanFrom
= nullptr,
88 const DominatorTree
*DT
= nullptr);
90 /// The default number of maximum instructions to scan in the block, used by
91 /// FindAvailableLoadedValue().
92 extern cl::opt
<unsigned> DefMaxInstsToScan
;
94 /// Scan backwards to see if we have the value of the given load available
95 /// locally within a small number of instructions.
97 /// You can use this function to scan across multiple blocks: after you call
98 /// this function, if ScanFrom points at the beginning of the block, it's safe
99 /// to continue scanning the predecessors.
101 /// Note that performing load CSE requires special care to make sure the
102 /// metadata is set appropriately. In particular, aliasing metadata needs
103 /// to be merged. (This doesn't matter for store-to-load forwarding because
104 /// the only relevant load gets deleted.)
106 /// \param Load The load we want to replace.
107 /// \param ScanBB The basic block to scan.
108 /// \param [in,out] ScanFrom The location to start scanning from. When this
109 /// function returns, it points at the last instruction scanned.
110 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
111 /// is zero, the whole block will be scanned.
112 /// \param AA Optional pointer to alias analysis, to make the scan more
114 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
115 /// location in memory, as opposed to the value operand of a store.
117 /// \returns The found value, or nullptr if no value is found.
118 Value
*FindAvailableLoadedValue(LoadInst
*Load
,
120 BasicBlock::iterator
&ScanFrom
,
121 unsigned MaxInstsToScan
= DefMaxInstsToScan
,
122 AliasAnalysis
*AA
= nullptr,
123 bool *IsLoadCSE
= nullptr,
124 unsigned *NumScanedInst
= nullptr);
126 /// Scan backwards to see if we have the value of the given pointer available
127 /// locally within a small number of instructions.
129 /// You can use this function to scan across multiple blocks: after you call
130 /// this function, if ScanFrom points at the beginning of the block, it's safe
131 /// to continue scanning the predecessors.
133 /// \param Ptr The pointer we want the load and store to originate from.
134 /// \param AccessTy The access type of the pointer.
135 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
136 /// case it is false, we can return an atomic or non-atomic load or store. In
137 /// case it is true, we need to return an atomic load or store.
138 /// \param ScanBB The basic block to scan.
139 /// \param [in,out] ScanFrom The location to start scanning from. When this
140 /// function returns, it points at the last instruction scanned.
141 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
142 /// is zero, the whole block will be scanned.
143 /// \param AA Optional pointer to alias analysis, to make the scan more
145 /// \param [out] IsLoad Whether the returned value is a load from the same
146 /// location in memory, as opposed to the value operand of a store.
148 /// \returns The found value, or nullptr if no value is found.
149 Value
*FindAvailablePtrLoadStore(Value
*Ptr
, Type
*AccessTy
, bool AtLeastAtomic
,
151 BasicBlock::iterator
&ScanFrom
,
152 unsigned MaxInstsToScan
, AliasAnalysis
*AA
,
153 bool *IsLoad
, unsigned *NumScanedInst
);