blob: 24a05610e68d8010426f6e3a7aa1f2adef9a3958 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Loads.h - Local load analysis --------------------------------------===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016#include "llvm/IR/BasicBlock.h"
17#include "llvm/Support/CommandLine.h"
18
19namespace llvm {
20
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020021class AAResults;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022class DataLayout;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020023class DominatorTree;
24class Instruction;
25class LoadInst;
26class Loop;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027class MDNode;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020028class ScalarEvolution;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029
30/// Return true if this is always a dereferenceable pointer. If the context
31/// instruction is specified perform context-sensitive analysis and return true
32/// if the pointer is dereferenceable at the specified instruction.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010033bool isDereferenceablePointer(const Value *V, Type *Ty,
34 const DataLayout &DL,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035 const Instruction *CtxI = nullptr,
36 const DominatorTree *DT = nullptr);
37
38/// Returns true if V is always a dereferenceable pointer with alignment
39/// greater or equal than requested. If the context instruction is specified
40/// performs context-sensitive analysis and returns true if the pointer is
41/// dereferenceable at the specified instruction.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010042bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020043 MaybeAlign Alignment,
44 const DataLayout &DL,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045 const Instruction *CtxI = nullptr,
46 const DominatorTree *DT = nullptr);
47
48/// Returns true if V is always dereferenceable for Size byte with alignment
49/// greater or equal than requested. If the context instruction is specified
50/// performs context-sensitive analysis and returns true if the pointer is
51/// dereferenceable at the specified instruction.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020052bool isDereferenceableAndAlignedPointer(const Value *V, Align Alignment,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 const APInt &Size, const DataLayout &DL,
54 const Instruction *CtxI = nullptr,
55 const DominatorTree *DT = nullptr);
56
57/// Return true if we know that executing a load from this value cannot trap.
58///
59/// If DT and ScanFrom are specified this method performs context-sensitive
60/// analysis and returns true if it is safe to load immediately before ScanFrom.
61///
62/// If it is not obviously safe to load from the specified pointer, we do a
63/// quick local scan of the basic block containing ScanFrom, to determine if
64/// the address is already accessed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020065bool isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010066 const DataLayout &DL,
67 Instruction *ScanFrom = nullptr,
68 const DominatorTree *DT = nullptr);
69
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020070/// Return true if we can prove that the given load (which is assumed to be
71/// within the specified loop) would access only dereferenceable memory, and
72/// be properly aligned on every iteration of the specified loop regardless of
73/// its placement within the loop. (i.e. does not require predication beyond
74/// that required by the the header itself and could be hoisted into the header
75/// if desired.) This is more powerful than the variants above when the
76/// address loaded from is analyzeable by SCEV.
77bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
78 ScalarEvolution &SE,
79 DominatorTree &DT);
80
Andrew Walbran3d2c1972020-04-07 12:24:26 +010081/// Return true if we know that executing a load from this value cannot trap.
82///
83/// If DT and ScanFrom are specified this method performs context-sensitive
84/// analysis and returns true if it is safe to load immediately before ScanFrom.
85///
86/// If it is not obviously safe to load from the specified pointer, we do a
87/// quick local scan of the basic block containing ScanFrom, to determine if
88/// the address is already accessed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020089bool isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090 const DataLayout &DL,
91 Instruction *ScanFrom = nullptr,
92 const DominatorTree *DT = nullptr);
93
94/// The default number of maximum instructions to scan in the block, used by
95/// FindAvailableLoadedValue().
96extern cl::opt<unsigned> DefMaxInstsToScan;
97
98/// Scan backwards to see if we have the value of the given load available
99/// locally within a small number of instructions.
100///
101/// You can use this function to scan across multiple blocks: after you call
102/// this function, if ScanFrom points at the beginning of the block, it's safe
103/// to continue scanning the predecessors.
104///
105/// Note that performing load CSE requires special care to make sure the
106/// metadata is set appropriately. In particular, aliasing metadata needs
107/// to be merged. (This doesn't matter for store-to-load forwarding because
108/// the only relevant load gets deleted.)
109///
110/// \param Load The load we want to replace.
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
117/// precise.
118/// \param [out] IsLoadCSE Whether the returned value is a load from the same
119/// location in memory, as opposed to the value operand of a store.
120///
121/// \returns The found value, or nullptr if no value is found.
122Value *FindAvailableLoadedValue(LoadInst *Load,
123 BasicBlock *ScanBB,
124 BasicBlock::iterator &ScanFrom,
125 unsigned MaxInstsToScan = DefMaxInstsToScan,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200126 AAResults *AA = nullptr,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127 bool *IsLoadCSE = nullptr,
128 unsigned *NumScanedInst = nullptr);
129
130/// Scan backwards to see if we have the value of the given pointer available
131/// locally within a small number of instructions.
132///
133/// You can use this function to scan across multiple blocks: after you call
134/// this function, if ScanFrom points at the beginning of the block, it's safe
135/// to continue scanning the predecessors.
136///
137/// \param Ptr The pointer we want the load and store to originate from.
138/// \param AccessTy The access type of the pointer.
139/// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
140/// case it is false, we can return an atomic or non-atomic load or store. In
141/// case it is true, we need to return an atomic load or store.
142/// \param ScanBB The basic block to scan.
143/// \param [in,out] ScanFrom The location to start scanning from. When this
144/// function returns, it points at the last instruction scanned.
145/// \param MaxInstsToScan The maximum number of instructions to scan. If this
146/// is zero, the whole block will be scanned.
147/// \param AA Optional pointer to alias analysis, to make the scan more
148/// precise.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200149/// \param [out] IsLoadCSE Whether the returned value is a load from the same
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100150/// location in memory, as opposed to the value operand of a store.
151///
152/// \returns The found value, or nullptr if no value is found.
153Value *FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, bool AtLeastAtomic,
154 BasicBlock *ScanBB,
155 BasicBlock::iterator &ScanFrom,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200156 unsigned MaxInstsToScan, AAResults *AA,
157 bool *IsLoadCSE, unsigned *NumScanedInst);
158
159/// Returns true if a pointer value \p A can be replace with another pointer
160/// value \B if they are deemed equal through some means (e.g. information from
161/// conditions).
162/// NOTE: the current implementations is incomplete and unsound. It does not
163/// reject all invalid cases yet, but will be made stricter in the future. In
164/// particular this means returning true means unknown if replacement is safe.
165bool canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
166 Instruction *CtxI);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167}
168
169#endif