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