Loads.h revision 360784
1//===- Loads.h - Local load analysis --------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares simple local analyses for load instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_LOADS_H
14#define LLVM_ANALYSIS_LOADS_H
15
16#include "llvm/Analysis/AliasAnalysis.h"
17#include "llvm/IR/BasicBlock.h"
18#include "llvm/Support/CommandLine.h"
19
20namespace llvm {
21
22class DataLayout;
23class Loop;
24class MDNode;
25class ScalarEvolution;
26
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.
30bool isDereferenceablePointer(const Value *V, Type *Ty,
31                              const DataLayout &DL,
32                              const Instruction *CtxI = nullptr,
33                              const DominatorTree *DT = nullptr);
34
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.
39bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
40                                        MaybeAlign Alignment,
41                                        const DataLayout &DL,
42                                        const Instruction *CtxI = nullptr,
43                                        const DominatorTree *DT = nullptr);
44
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.
49bool isDereferenceableAndAlignedPointer(const Value *V, Align Alignment,
50                                        const APInt &Size, const DataLayout &DL,
51                                        const Instruction *CtxI = nullptr,
52                                        const DominatorTree *DT = nullptr);
53
54/// Return true if we know that executing a load from this value cannot trap.
55///
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.
58///
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.
62bool isSafeToLoadUnconditionally(Value *V, MaybeAlign Alignment, APInt &Size,
63                                 const DataLayout &DL,
64                                 Instruction *ScanFrom = nullptr,
65                                 const DominatorTree *DT = nullptr);
66
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.
74bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
75                                       ScalarEvolution &SE,
76                                       DominatorTree &DT);
77
78/// Return true if we know that executing a load from this value cannot trap.
79///
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.
82///
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.
86bool isSafeToLoadUnconditionally(Value *V, Type *Ty, MaybeAlign Alignment,
87                                 const DataLayout &DL,
88                                 Instruction *ScanFrom = nullptr,
89                                 const DominatorTree *DT = nullptr);
90
91/// The default number of maximum instructions to scan in the block, used by
92/// FindAvailableLoadedValue().
93extern cl::opt<unsigned> DefMaxInstsToScan;
94
95/// Scan backwards to see if we have the value of the given load available
96/// locally within a small number of instructions.
97///
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.
101///
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.)
106///
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
114/// precise.
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.
117///
118/// \returns The found value, or nullptr if no value is found.
119Value *FindAvailableLoadedValue(LoadInst *Load,
120                                BasicBlock *ScanBB,
121                                BasicBlock::iterator &ScanFrom,
122                                unsigned MaxInstsToScan = DefMaxInstsToScan,
123                                AliasAnalysis *AA = nullptr,
124                                bool *IsLoadCSE = nullptr,
125                                unsigned *NumScanedInst = nullptr);
126
127/// Scan backwards to see if we have the value of the given pointer available
128/// locally within a small number of instructions.
129///
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.
133///
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
145/// precise.
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.
148///
149/// \returns The found value, or nullptr if no value is found.
150Value *FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic,
151                                 BasicBlock *ScanBB,
152                                 BasicBlock::iterator &ScanFrom,
153                                 unsigned MaxInstsToScan, AliasAnalysis *AA,
154                                 bool *IsLoad, unsigned *NumScanedInst);
155}
156
157#endif
158