LLVM  14.0.0git
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 
16 #include "llvm/IR/BasicBlock.h"
18 
19 namespace llvm {
20 
21 class AAResults;
22 class DataLayout;
23 class DominatorTree;
24 class Instruction;
25 class LoadInst;
26 class Loop;
27 class MemoryLocation;
28 class ScalarEvolution;
29 class TargetLibraryInfo;
30 
31 /// Return true if this is always a dereferenceable pointer. If the context
32 /// instruction is specified perform context-sensitive analysis and return true
33 /// if the pointer is dereferenceable at the specified instruction.
34 bool isDereferenceablePointer(const Value *V, Type *Ty,
35  const DataLayout &DL,
36  const Instruction *CtxI = nullptr,
37  const DominatorTree *DT = nullptr,
38  const TargetLibraryInfo *TLI = nullptr);
39 
40 /// Returns true if V is always a dereferenceable pointer with alignment
41 /// greater or equal than requested. If the context instruction is specified
42 /// performs context-sensitive analysis and returns true if the pointer is
43 /// dereferenceable at the specified instruction.
45  MaybeAlign Alignment,
46  const DataLayout &DL,
47  const Instruction *CtxI = nullptr,
48  const DominatorTree *DT = nullptr,
49  const TargetLibraryInfo *TLI = nullptr);
50 
51 /// Returns true if V is always dereferenceable for Size byte with alignment
52 /// greater or equal than requested. If the context instruction is specified
53 /// performs context-sensitive analysis and returns true if the pointer is
54 /// dereferenceable at the specified instruction.
55 bool isDereferenceableAndAlignedPointer(const Value *V, Align Alignment,
56  const APInt &Size, const DataLayout &DL,
57  const Instruction *CtxI = nullptr,
58  const DominatorTree *DT = nullptr,
59  const TargetLibraryInfo *TLI = nullptr);
60 
61 /// Return true if we know that executing a load from this value cannot trap.
62 ///
63 /// If DT and ScanFrom are specified this method performs context-sensitive
64 /// analysis and returns true if it is safe to load immediately before ScanFrom.
65 ///
66 /// If it is not obviously safe to load from the specified pointer, we do a
67 /// quick local scan of the basic block containing ScanFrom, to determine if
68 /// the address is already accessed.
69 bool isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size,
70  const DataLayout &DL,
71  Instruction *ScanFrom = nullptr,
72  const DominatorTree *DT = nullptr,
73  const TargetLibraryInfo *TLI = nullptr);
74 
75 /// Return true if we can prove that the given load (which is assumed to be
76 /// within the specified loop) would access only dereferenceable memory, and
77 /// be properly aligned on every iteration of the specified loop regardless of
78 /// its placement within the loop. (i.e. does not require predication beyond
79 /// that required by the the header itself and could be hoisted into the header
80 /// if desired.) This is more powerful than the variants above when the
81 /// address loaded from is analyzeable by SCEV.
82 bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
83  ScalarEvolution &SE,
84  DominatorTree &DT);
85 
86 /// Return true if we know that executing a load from this value cannot trap.
87 ///
88 /// If DT and ScanFrom are specified this method performs context-sensitive
89 /// analysis and returns true if it is safe to load immediately before ScanFrom.
90 ///
91 /// If it is not obviously safe to load from the specified pointer, we do a
92 /// quick local scan of the basic block containing ScanFrom, to determine if
93 /// the address is already accessed.
94 bool isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
95  const DataLayout &DL,
96  Instruction *ScanFrom = nullptr,
97  const DominatorTree *DT = nullptr,
98  const TargetLibraryInfo *TLI = nullptr);
99 
100 /// The default number of maximum instructions to scan in the block, used by
101 /// FindAvailableLoadedValue().
102 extern cl::opt<unsigned> DefMaxInstsToScan;
103 
104 /// Scan backwards to see if we have the value of the given load available
105 /// locally within a small number of instructions.
106 ///
107 /// You can use this function to scan across multiple blocks: after you call
108 /// this function, if ScanFrom points at the beginning of the block, it's safe
109 /// to continue scanning the predecessors.
110 ///
111 /// Note that performing load CSE requires special care to make sure the
112 /// metadata is set appropriately. In particular, aliasing metadata needs
113 /// to be merged. (This doesn't matter for store-to-load forwarding because
114 /// the only relevant load gets deleted.)
115 ///
116 /// \param Load The load we want to replace.
117 /// \param ScanBB The basic block to scan.
118 /// \param [in,out] ScanFrom The location to start scanning from. When this
119 /// function returns, it points at the last instruction scanned.
120 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
121 /// is zero, the whole block will be scanned.
122 /// \param AA Optional pointer to alias analysis, to make the scan more
123 /// precise.
124 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
125 /// location in memory, as opposed to the value operand of a store.
126 ///
127 /// \returns The found value, or nullptr if no value is found.
129  BasicBlock *ScanBB,
130  BasicBlock::iterator &ScanFrom,
131  unsigned MaxInstsToScan = DefMaxInstsToScan,
132  AAResults *AA = nullptr,
133  bool *IsLoadCSE = nullptr,
134  unsigned *NumScanedInst = nullptr);
135 
136 /// This overload provides a more efficient implementation of
137 /// FindAvailableLoadedValue() for the case where we are not interested in
138 /// finding the closest clobbering instruction if no available load is found.
139 /// This overload cannot be used to scan across multiple blocks.
140 Value *FindAvailableLoadedValue(LoadInst *Load, AAResults &AA, bool *IsLoadCSE,
141  unsigned MaxInstsToScan = DefMaxInstsToScan);
142 
143 /// Scan backwards to see if we have the value of the given pointer available
144 /// locally within a small number of instructions.
145 ///
146 /// You can use this function to scan across multiple blocks: after you call
147 /// this function, if ScanFrom points at the beginning of the block, it's safe
148 /// to continue scanning the predecessors.
149 ///
150 /// \param Loc The location we want the load and store to originate from.
151 /// \param AccessTy The access type of the pointer.
152 /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
153 /// case it is false, we can return an atomic or non-atomic load or store. In
154 /// case it is true, we need to return an atomic load or store.
155 /// \param ScanBB The basic block to scan.
156 /// \param [in,out] ScanFrom The location to start scanning from. When this
157 /// function returns, it points at the last instruction scanned.
158 /// \param MaxInstsToScan The maximum number of instructions to scan. If this
159 /// is zero, the whole block will be scanned.
160 /// \param AA Optional pointer to alias analysis, to make the scan more
161 /// precise.
162 /// \param [out] IsLoadCSE Whether the returned value is a load from the same
163 /// location in memory, as opposed to the value operand of a store.
164 ///
165 /// \returns The found value, or nullptr if no value is found.
166 Value *findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy,
167  bool AtLeastAtomic, BasicBlock *ScanBB,
168  BasicBlock::iterator &ScanFrom,
169  unsigned MaxInstsToScan, AAResults *AA,
170  bool *IsLoadCSE, unsigned *NumScanedInst);
171 
172 /// Returns true if a pointer value \p A can be replace with another pointer
173 /// value \B if they are deemed equal through some means (e.g. information from
174 /// conditions).
175 /// NOTE: the current implementations is incomplete and unsound. It does not
176 /// reject all invalid cases yet, but will be made stricter in the future. In
177 /// particular this means returning true means unknown if replacement is safe.
178 bool canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
179  Instruction *CtxI);
180 }
181 
182 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
llvm::isDereferenceableAndAlignedPointer
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, MaybeAlign Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:210
llvm::canReplacePointersIfEqual
bool canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL, Instruction *CtxI)
Returns true if a pointer value A can be replace with another pointer value \B if they are deemed equ...
Definition: Loads.cpp:653
CommandLine.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::isDereferenceablePointer
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:234
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::findAvailablePtrLoadStore
Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
Definition: Loads.cpp:524
BasicBlock.h
TemplateParamKind::Type
@ Type
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::isDereferenceableAndAlignedInLoop
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:273
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
A
* A
Definition: README_ALTIVEC.txt:89
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::FindAvailableLoadedValue
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AAResults *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:431
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::isSafeToLoadUnconditionally
bool isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size, const DataLayout &DL, Instruction *ScanFrom=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if we know that executing a load from this value cannot trap.
Definition: Loads.cpp:335
llvm::DefMaxInstsToScan
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().