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 /// Return true if this is always a dereferenceable pointer. If the context
26 /// instruction is specified perform context-sensitive analysis and return true
27 /// if the pointer is dereferenceable at the specified instruction.
28 bool isDereferenceablePointer(const Value
*V
, Type
*Ty
,
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
, Type
*Ty
,
38 unsigned Align
, const DataLayout
&DL
,
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
, APInt
&Size
,
61 Instruction
*ScanFrom
= nullptr,
62 const DominatorTree
*DT
= nullptr);
64 /// Return true if we know that executing a load from this value cannot trap.
66 /// If DT and ScanFrom are specified this method performs context-sensitive
67 /// analysis and returns true if it is safe to load immediately before ScanFrom.
69 /// If it is not obviously safe to load from the specified pointer, we do a
70 /// quick local scan of the basic block containing ScanFrom, to determine if
71 /// the address is already accessed.
72 bool isSafeToLoadUnconditionally(Value
*V
, Type
*Ty
, unsigned Align
,
74 Instruction
*ScanFrom
= nullptr,
75 const DominatorTree
*DT
= nullptr);
77 /// The default number of maximum instructions to scan in the block, used by
78 /// FindAvailableLoadedValue().
79 extern cl::opt
<unsigned> DefMaxInstsToScan
;
81 /// Scan backwards to see if we have the value of the given load available
82 /// locally within a small number of instructions.
84 /// You can use this function to scan across multiple blocks: after you call
85 /// this function, if ScanFrom points at the beginning of the block, it's safe
86 /// to continue scanning the predecessors.
88 /// Note that performing load CSE requires special care to make sure the
89 /// metadata is set appropriately. In particular, aliasing metadata needs
90 /// to be merged. (This doesn't matter for store-to-load forwarding because
91 /// the only relevant load gets deleted.)
93 /// \param Load The load we want to replace.
94 /// \param ScanBB The basic block to scan.
95 /// \param [in,out] ScanFrom The location to start scanning from. When this
96 /// function returns, it points at the last instruction scanned.
97 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
98 /// is zero, the whole block will be scanned.
99 /// \param AA Optional pointer to alias analysis, to make the scan more
101 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
102 /// location in memory, as opposed to the value operand of a store.
104 /// \returns The found value, or nullptr if no value is found.
105 Value
*FindAvailableLoadedValue(LoadInst
*Load
,
107 BasicBlock::iterator
&ScanFrom
,
108 unsigned MaxInstsToScan
= DefMaxInstsToScan
,
109 AliasAnalysis
*AA
= nullptr,
110 bool *IsLoadCSE
= nullptr,
111 unsigned *NumScanedInst
= nullptr);
113 /// Scan backwards to see if we have the value of the given pointer available
114 /// locally within a small number of instructions.
116 /// You can use this function to scan across multiple blocks: after you call
117 /// this function, if ScanFrom points at the beginning of the block, it's safe
118 /// to continue scanning the predecessors.
120 /// \param Ptr The pointer we want the load and store to originate from.
121 /// \param AccessTy The access type of the pointer.
122 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
123 /// case it is false, we can return an atomic or non-atomic load or store. In
124 /// case it is true, we need to return an atomic load or store.
125 /// \param ScanBB The basic block to scan.
126 /// \param [in,out] ScanFrom The location to start scanning from. When this
127 /// function returns, it points at the last instruction scanned.
128 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
129 /// is zero, the whole block will be scanned.
130 /// \param AA Optional pointer to alias analysis, to make the scan more
132 /// \param [out] IsLoad Whether the returned value is a load from the same
133 /// location in memory, as opposed to the value operand of a store.
135 /// \returns The found value, or nullptr if no value is found.
136 Value
*FindAvailablePtrLoadStore(Value
*Ptr
, Type
*AccessTy
, bool AtLeastAtomic
,
138 BasicBlock::iterator
&ScanFrom
,
139 unsigned MaxInstsToScan
, AliasAnalysis
*AA
,
140 bool *IsLoad
, unsigned *NumScanedInst
);