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
, const DataLayout
&DL
,
29 const Instruction
*CtxI
= nullptr,
30 const DominatorTree
*DT
= nullptr);
32 /// Returns true if V is always a dereferenceable pointer with alignment
33 /// greater or equal than requested. If the context instruction is specified
34 /// performs context-sensitive analysis and returns true if the pointer is
35 /// dereferenceable at the specified instruction.
36 bool isDereferenceableAndAlignedPointer(const Value
*V
, unsigned Align
,
38 const Instruction
*CtxI
= nullptr,
39 const DominatorTree
*DT
= nullptr);
41 /// Returns true if V is always dereferenceable for Size byte with alignment
42 /// greater or equal than requested. If the context instruction is specified
43 /// performs context-sensitive analysis and returns true if the pointer is
44 /// dereferenceable at the specified instruction.
45 bool isDereferenceableAndAlignedPointer(const Value
*V
, unsigned Align
,
46 const APInt
&Size
, const DataLayout
&DL
,
47 const Instruction
*CtxI
= nullptr,
48 const DominatorTree
*DT
= nullptr);
50 /// Return true if we know that executing a load from this value cannot trap.
52 /// If DT and ScanFrom are specified this method performs context-sensitive
53 /// analysis and returns true if it is safe to load immediately before ScanFrom.
55 /// If it is not obviously safe to load from the specified pointer, we do a
56 /// quick local scan of the basic block containing ScanFrom, to determine if
57 /// the address is already accessed.
58 bool isSafeToLoadUnconditionally(Value
*V
, unsigned Align
,
60 Instruction
*ScanFrom
= nullptr,
61 const DominatorTree
*DT
= nullptr);
63 /// The default number of maximum instructions to scan in the block, used by
64 /// FindAvailableLoadedValue().
65 extern cl::opt
<unsigned> DefMaxInstsToScan
;
67 /// Scan backwards to see if we have the value of the given load available
68 /// locally within a small number of instructions.
70 /// You can use this function to scan across multiple blocks: after you call
71 /// this function, if ScanFrom points at the beginning of the block, it's safe
72 /// to continue scanning the predecessors.
74 /// Note that performing load CSE requires special care to make sure the
75 /// metadata is set appropriately. In particular, aliasing metadata needs
76 /// to be merged. (This doesn't matter for store-to-load forwarding because
77 /// the only relevant load gets deleted.)
79 /// \param Load The load we want to replace.
80 /// \param ScanBB The basic block to scan.
81 /// \param [in,out] ScanFrom The location to start scanning from. When this
82 /// function returns, it points at the last instruction scanned.
83 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
84 /// is zero, the whole block will be scanned.
85 /// \param AA Optional pointer to alias analysis, to make the scan more
87 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
88 /// location in memory, as opposed to the value operand of a store.
90 /// \returns The found value, or nullptr if no value is found.
91 Value
*FindAvailableLoadedValue(LoadInst
*Load
,
93 BasicBlock::iterator
&ScanFrom
,
94 unsigned MaxInstsToScan
= DefMaxInstsToScan
,
95 AliasAnalysis
*AA
= nullptr,
96 bool *IsLoadCSE
= nullptr,
97 unsigned *NumScanedInst
= nullptr);
99 /// Scan backwards to see if we have the value of the given pointer available
100 /// locally within a small number of instructions.
102 /// You can use this function to scan across multiple blocks: after you call
103 /// this function, if ScanFrom points at the beginning of the block, it's safe
104 /// to continue scanning the predecessors.
106 /// \param Ptr The pointer we want the load and store to originate from.
107 /// \param AccessTy The access type of the pointer.
108 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
109 /// case it is false, we can return an atomic or non-atomic load or store. In
110 /// case it is true, we need to return an atomic load or store.
111 /// \param ScanBB The basic block to scan.
112 /// \param [in,out] ScanFrom The location to start scanning from. When this
113 /// function returns, it points at the last instruction scanned.
114 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
115 /// is zero, the whole block will be scanned.
116 /// \param AA Optional pointer to alias analysis, to make the scan more
118 /// \param [out] IsLoad Whether the returned value is a load from the same
119 /// location in memory, as opposed to the value operand of a store.
121 /// \returns The found value, or nullptr if no value is found.
122 Value
*FindAvailablePtrLoadStore(Value
*Ptr
, Type
*AccessTy
, bool AtLeastAtomic
,
124 BasicBlock::iterator
&ScanFrom
,
125 unsigned MaxInstsToScan
, AliasAnalysis
*AA
,
126 bool *IsLoad
, unsigned *NumScanedInst
);