LLVM 24.0.0git
SLPMemoryUtils.h
Go to the documentation of this file.
1//===- SLPMemoryUtils.h - SLP pointer/stride helpers -----------*- C++ -*-===//
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// Internal header used by SLPVectorizer.cpp. It declares free pointer and
10// stride helpers that do not depend on BoUpSLP or any other SLP-private type.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPMEMORYUTILS_H
15#define LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPMEMORYUTILS_H
16
17#include "SLPCostAnalysis.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
23
24#include <cstdint>
25#include <optional>
26#include <tuple>
27
28namespace llvm {
29class AssumptionCache;
30class BasicBlock;
31class DataLayout;
32class DominatorTree;
33class FixedVectorType;
34class IRBuilderBase;
35class SCEV;
36class ScalarEvolution;
38class Type;
39class Value;
40} // namespace llvm
41
42namespace llvm::slpvectorizer {
43
44/// Caches the results of the expensive analyses that are queried repeatedly
45/// for the same values while the vectorization trees are built and their costs
46/// are estimated. The results may change once new code replaces the scalars,
47/// so the cache is cleared each time the replaced scalars are erased.
49public:
50 /// Cached llvm::getPointersDiff(), a miss may require SCEV analysis. The
51 /// result for (\p PtrA, \p PtrB) also serves the query for the swapped
52 /// pointers.
53 std::optional<int64_t>
54 getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB, Value *PtrB,
55 const DataLayout &DL, ScalarEvolution &SE,
56 bool StrictCheck = false, bool CheckType = true);
57
58 /// llvm::sortPtrAccesses() with the cached pointer distances.
61 SmallVectorImpl<unsigned> &SortedIndices);
62
63 /// Cached llvm::isGuaranteedNotToBePoison().
65
66 void clear() {
67 PtrsDiffCache.clear();
68 NotPoisonCache.clear();
69 }
70
71private:
72 using PtrsDiffKeyT = std::tuple<Type *, Value *, Type *, Value *, unsigned>;
75};
76
77/// \p MaxDepth is the recursion limit for getUnderlyingObject.
78bool arePointersCompatible(Value *Ptr1, Value *Ptr2,
79 const TargetLibraryInfo &TLI, unsigned MaxDepth,
80 bool CompareOpcodes = true);
81
82/// Casts \p V between the widened strided access type and the entry vector
83/// type \p DstTy.
85 const DataLayout &DL);
86
87/// Calculates minimal alignment as a common alignment.
88template <typename T> Align computeCommonAlignment(ArrayRef<Value *> VL);
89
90/// Checks if the provided list of pointers \p Pointers represents the strided
91/// pointers for type ElemTy. If they are not, nullptr is returned.
92/// Otherwise, SCEV* of the stride value is returned.
93/// If `PointerOps` can be rearranged into the following sequence:
94/// ```
95/// %x + c_0 * stride,
96/// %x + c_1 * stride,
97/// %x + c_2 * stride
98/// ...
99/// ```
100/// where each `c_i` is constant. The SCEV of the `stride` will be returned.
102 const DataLayout &DL, ScalarEvolution &SE,
103 SmallVectorImpl<unsigned> &SortedIndices);
104
105/// Checks if the \p VL can be transformed to a (masked)load + compress or
106/// (masked) interleaved load.
111 const DominatorTree &DT, const TargetLibraryInfo &TLI,
113 const function_ref<bool(Value *)> AreAllUsersVectorized, bool ReVec,
114 CompressedLoadInfo &Info, SLPAnalysisCache &Cache);
115
120 const DominatorTree &DT, const TargetLibraryInfo &TLI,
122 const function_ref<bool(Value *)> AreAllUsersVectorized, bool ReVec,
123 SLPAnalysisCache &Cache);
124
125/// Checks if the simple loads of \p VL, mixed with the undef and poison lanes,
126/// can be replaced by a single load of the whole \p VL width. The loads must be
127/// consecutive and match their lanes, and the whole accessed range must be safe
128/// to load unconditionally.
131 const DominatorTree &DT,
132 const TargetLibraryInfo &TLI);
133
134/// Returns the distance in bytes from the start of the vector load to the
135/// first load of \p Scalars: the leading undef lanes are covered by the vector
136/// load.
138
139/// Checks if the stores \p VL with pointers \p PointerOps can be lowered as a
140/// single masked store. On success \p StoreVecTy is the widened store type and
141/// \p ReuseShuffleIndices is the expand mask that places each stored value at
142/// its element offset from the base (poison in the gaps).
144 ArrayRef<unsigned> Order,
145 const TargetTransformInfo &TTI, const DataLayout &DL,
146 ScalarEvolution &SE, Align CommonAlignment,
147 SmallVectorImpl<int> &ReuseShuffleIndices,
148 FixedVectorType *&StoreVecTy,
149 SLPAnalysisCache &Cache);
150
151/// Clusters \p VL pointers by (basic block, underlying object) pair and sorts
152/// each cluster by offset. Returns false and leaves \p SortedIndices empty if
153/// the accesses are not worth reordering.
155 Type *ElemTy, const DataLayout &DL,
156 ScalarEvolution &SE, unsigned MaxDepth,
157 SmallVectorImpl<unsigned> &SortedIndices,
158 SLPAnalysisCache &Cache);
159
160} // namespace llvm::slpvectorizer
161
162#endif // LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPMEMORYUTILS_H
const TargetLowering & TLI
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the DenseMap class.
This pass exposes codegen information to IR-level passes.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:61
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
Class to represent fixed width SIMD vectors.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:111
This class represents an analyzed expression in the program.
The main scalar evolution driver.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetCostKind
The kind of cost model.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
Caches the results of the expensive analyses that are queried repeatedly for the same values while th...
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC)
Cached llvm::isGuaranteedNotToBePoison().
bool sortPtrAccesses(ArrayRef< Value * > VL, Type *ElemTy, const DataLayout &DL, ScalarEvolution &SE, SmallVectorImpl< unsigned > &SortedIndices)
llvm::sortPtrAccesses() with the cached pointer distances.
std::optional< int64_t > getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB, Value *PtrB, const DataLayout &DL, ScalarEvolution &SE, bool StrictCheck=false, bool CheckType=true)
Cached llvm::getPointersDiff(), a miss may require SCEV analysis.
A private "module" namespace for types and utilities used by this pass.
bool isMaskedLoadCompress(ArrayRef< Value * > VL, ArrayRef< Value * > PointerOps, ArrayRef< unsigned > Order, const TargetTransformInfo &TTI, const DataLayout &DL, ScalarEvolution &SE, AssumptionCache &AC, const DominatorTree &DT, const TargetLibraryInfo &TLI, const TargetTransformInfo::TargetCostKind CostKind, const function_ref< bool(Value *)> AreAllUsersVectorized, bool ReVec, CompressedLoadInfo &Info, SLPAnalysisCache &Cache)
Checks if the VL can be transformed to a (masked)load + compress or (masked) interleaved load.
bool clusterSortPtrAccesses(ArrayRef< Value * > VL, ArrayRef< BasicBlock * > BBs, Type *ElemTy, const DataLayout &DL, ScalarEvolution &SE, unsigned MaxDepth, SmallVectorImpl< unsigned > &SortedIndices, SLPAnalysisCache &Cache)
Clusters VL pointers by (basic block, underlying object) pair and sorts each cluster by offset.
Value * createWidenedStridedCast(IRBuilderBase &Builder, Value *V, Type *DstTy, const DataLayout &DL)
Casts V between the widened strided access type and the entry vector type DstTy.
Align computeCommonAlignment(ArrayRef< Value * > VL)
Calculates minimal alignment as a common alignment.
const SCEV * calculateRtStride(ArrayRef< Value * > PointerOps, Type *ElemTy, const DataLayout &DL, ScalarEvolution &SE, SmallVectorImpl< unsigned > &SortedIndices)
Checks if the provided list of pointers Pointers represents the strided pointers for type ElemTy.
bool isMaskedStoreCompress(ArrayRef< Value * > VL, ArrayRef< Value * > PointerOps, ArrayRef< unsigned > Order, const TargetTransformInfo &TTI, const DataLayout &DL, ScalarEvolution &SE, Align CommonAlignment, SmallVectorImpl< int > &ReuseShuffleIndices, FixedVectorType *&StoreVecTy, SLPAnalysisCache &Cache)
Checks if the stores VL with pointers PointerOps can be lowered as a single masked store.
uint64_t getVectorLoadShift(ArrayRef< Value * > Scalars)
Returns the distance in bytes from the start of the vector load to the first load of Scalars: the lea...
bool canWidenLoadsOverUndefLanes(ArrayRef< Value * > VL, const DataLayout &DL, ScalarEvolution &SE, AssumptionCache &AC, const DominatorTree &DT, const TargetLibraryInfo &TLI)
Checks if the simple loads of VL, mixed with the undef and poison lanes, can be replaced by a single ...
bool arePointersCompatible(Value *Ptr1, Value *Ptr2, const TargetLibraryInfo &TLI, unsigned MaxDepth, bool CompareOpcodes)
MaxDepth is the recursion limit for getUnderlyingObject.
This is an optimization pass for GlobalISel generic memory operations.
TargetTransformInfo TTI
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39