LLVM  10.0.0svn
Loads.h
Go to the documentation of this file.
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 
17 #include "llvm/IR/BasicBlock.h"
19 
20 namespace llvm {
21 
22 class DataLayout;
23 class Loop;
24 class MDNode;
25 class 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.
30 bool 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.
40  unsigned Align, const DataLayout &DL,
41  const Instruction *CtxI = nullptr,
42  const DominatorTree *DT = nullptr);
43 
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);
52 
53 /// Return true if we know that executing a load from this value cannot trap.
54 ///
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.
57 ///
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,
62  const DataLayout &DL,
63  Instruction *ScanFrom = nullptr,
64  const DominatorTree *DT = nullptr);
65 
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,
74  ScalarEvolution &SE,
75  DominatorTree &DT);
76 
77 /// Return true if we know that executing a load from this value cannot trap.
78 ///
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.
81 ///
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,
86  const DataLayout &DL,
87  Instruction *ScanFrom = nullptr,
88  const DominatorTree *DT = nullptr);
89 
90 /// The default number of maximum instructions to scan in the block, used by
91 /// FindAvailableLoadedValue().
92 extern cl::opt<unsigned> DefMaxInstsToScan;
93 
94 /// Scan backwards to see if we have the value of the given load available
95 /// locally within a small number of instructions.
96 ///
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.
100 ///
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.)
105 ///
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
113 /// precise.
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.
116 ///
117 /// \returns The found value, or nullptr if no value is found.
119  BasicBlock *ScanBB,
120  BasicBlock::iterator &ScanFrom,
121  unsigned MaxInstsToScan = DefMaxInstsToScan,
122  AliasAnalysis *AA = nullptr,
123  bool *IsLoadCSE = nullptr,
124  unsigned *NumScanedInst = nullptr);
125 
126 /// Scan backwards to see if we have the value of the given pointer available
127 /// locally within a small number of instructions.
128 ///
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.
132 ///
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
144 /// precise.
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.
147 ///
148 /// \returns The found value, or nullptr if no value is found.
149 Value *FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic,
150  BasicBlock *ScanBB,
151  BasicBlock::iterator &ScanFrom,
152  unsigned MaxInstsToScan, AliasAnalysis *AA,
153  bool *IsLoad, unsigned *NumScanedInst);
154 }
155 
156 #endif
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Various leaf nodes.
Definition: ISDOpcodes.h:59
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
bool isSafeToLoadUnconditionally(Value *V, unsigned Align, APInt &Size, const DataLayout &DL, Instruction *ScanFrom=nullptr, const DominatorTree *DT=nullptr)
Return true if we know that executing a load from this value cannot trap.
Definition: Loads.cpp:262
Value * FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, AliasAnalysis *AA, bool *IsLoad, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
Definition: Loads.cpp:376
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, unsigned Align, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested...
Definition: Loads.cpp:135
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AliasAnalysis *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
Definition: Loads.cpp:361
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:158
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:89
bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT)
Return true if we can prove that the given load (which is assumed to be within the specified loop) wo...
Definition: Loads.cpp:196
uint32_t Size
Definition: Profile.cpp:46