LLVM 24.0.0git
SLPVectorizer.h
Go to the documentation of this file.
1//===- SLPVectorizer.h ------------------------------------------*- 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// This pass implements the Bottom Up SLP vectorizer. It detects consecutive
9// stores that can be put together into vector-stores. Next, it attempts to
10// construct vectorizable tree using the use-def chains. If a profitable tree
11// was found, the SLP vectorizer performs vectorization on the tree.
12//
13// The pass is inspired by the work described in the paper:
14// "Loop-Aware SLP in GCC" by Ira Rosen, Dorit Nuzman, Ayal Zaks.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_TRANSFORMS_VECTORIZE_SLPVECTORIZER_H
19#define LLVM_TRANSFORMS_VECTORIZE_SLPVECTORIZER_H
20
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/MapVector.h"
23#include "llvm/ADT/SetVector.h"
25#include "llvm/IR/PassManager.h"
26
27namespace llvm {
28
29class AAResults;
30class AssumptionCache;
31class BasicBlock;
32class DataLayout;
33class DemandedBits;
34class DominatorTree;
35class Function;
38class InsertValueInst;
39class Instruction;
40class LoopInfo;
42class PHINode;
43class ScalarEvolution;
44class StoreInst;
47class Value;
48struct VectorizeOptions;
49class WeakTrackingVH;
50
51/// A private "module" namespace for types and utilities used by this pass.
52/// These are implementation details and should not be used by clients.
53namespace slpvectorizer {
54
55class BoUpSLP;
56
57} // end namespace slpvectorizer
58
59struct SLPVectorizerPass : public OptionalPassInfoMixin<SLPVectorizerPass> {
65
66 const VectorizeOptions *Opts = nullptr;
67 ScalarEvolution *SE = nullptr;
70 AAResults *AA = nullptr;
71 LoopInfo *LI = nullptr;
72 DominatorTree *DT = nullptr;
73 AssumptionCache *AC = nullptr;
74 DemandedBits *DB = nullptr;
75 const DataLayout *DL = nullptr;
76
77public:
79
80 // Glue for old PM.
83 AAResults *AA_, LoopInfo *LI_, DominatorTree *DT_,
86
87private:
88 /// Collect store and getelementptr instructions and organize them
89 /// according to the underlying object of their pointer operands. We sort the
90 /// instructions by their underlying objects to reduce the cost of
91 /// consecutive access queries.
92 ///
93 /// TODO: We can further reduce this cost if we flush the chain creation
94 /// every time we run into a memory barrier.
95 void collectSeedInstructions(BasicBlock *BB);
96
97 /// Try to vectorize a list of operands.
98 /// \param MaxVFOnly Vectorize only using maximal allowed register size.
99 /// \param StandaloneSeeds \p VL are the standalone seeds: the vector factor
100 /// is limited by a single register and the windows, overlapping with the
101 /// rejected ones, are not retried.
102 /// \returns true if a value was vectorized.
103 bool tryToVectorizeList(ArrayRef<Value *> VL, slpvectorizer::BoUpSLP &R,
104 bool MaxVFOnly = false, bool StandaloneSeeds = false);
105
106 /// Try to vectorize a chain that may start at the operands of \p I.
107 bool tryToVectorize(Instruction *I, slpvectorizer::BoUpSLP &R,
109 bool AllowFMACandidates = false);
110
111 /// Try to vectorize chains that may start at the operands of
112 /// instructions in \p Insts.
113 bool tryToVectorize(ArrayRef<WeakTrackingVH> Insts, slpvectorizer::BoUpSLP &R,
114 SmallSetVector<Instruction *, 8> &FMACandidates);
115
116 /// Vectorize the store instructions collected in Stores.
117 bool vectorizeStoreChains(slpvectorizer::BoUpSLP &R);
118
119 /// Vectorize the poor-throughput instruction groups of \p BB that the store
120 /// chains cannot cover completely.
121 bool vectorizeSplitStorePoorThroughputSeeds(BasicBlock *BB,
123
124 /// Try to vectorize the poor-throughput instructions \p Candidates as the
125 /// standalone seeds. If \p Filter is provided, only the groups accepted by
126 /// it are tried.
127 bool vectorizePoorThroughputSeeds(
129 function_ref<bool(ArrayRef<Value *>)> Filter = nullptr);
130
131 /// Try to vectorize the standalone seeds \p Seeds in the groups of the
132 /// compatible instructions, \p IsLessGroup orders the groups. If \p Filter
133 /// is provided, only the groups accepted by it are tried.
134 bool vectorizeSeeds(SmallVectorImpl<Value *> &Seeds,
135 function_ref<bool(Value *, Value *)> IsLessGroup,
137 function_ref<bool(ArrayRef<Value *>)> Filter = nullptr);
138
139 /// Try to vectorize the instructions with the single user in \p BB as the
140 /// standalone seeds.
141 bool vectorizeOnceUsedSeeds(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
142
143 /// Vectorize the index computations of the getelementptr instructions
144 /// collected in GEPs.
145 bool vectorizeGEPIndices(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
146
147 /// Try to find horizontal reduction or otherwise, collect instructions
148 /// for postponed vectorization attempts.
149 /// \a P if not null designates phi node the reduction is fed into
150 /// (with reduction operators \a Root or one of its operands, in a basic block
151 /// \a BB).
152 /// \returns true if a horizontal reduction was matched and reduced.
153 /// \returns false if \a V is null or not an instruction,
154 /// or a horizontal reduction was not matched or not possible.
155 bool vectorizeHorReduction(PHINode *P, Instruction *Root, BasicBlock *BB,
157 SmallVectorImpl<WeakTrackingVH> &PostponedInsts);
158
159 /// Tries to vectorize the horizontal reductions, rooted at the seed values
160 /// \p Vals (or at the incoming values of the non-header PHI nodes in \p
161 /// Vals), before the seeds themselves, if they are the reductions of the
162 /// loop accumulators, vectorizable along their own reduced values.
163 /// \returns true if any reduction was vectorized.
164 bool vectorizeSeedReductions(ArrayRef<Value *> Vals,
166
167 /// Make an attempt to vectorize reduction and then try to vectorize
168 /// postponed binary operations.
169 /// \returns true on any successfull vectorization.
170 bool
171 vectorizeRootInstruction(PHINode *P, Instruction *Root, BasicBlock *BB,
173 SmallSetVector<Instruction *, 8> &FMACandidates);
174
175 /// Try to vectorize trees that start at insertvalue instructions.
176 bool vectorizeInsertValueInst(InsertValueInst *IVI, BasicBlock *BB,
177 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
178
179 /// Try to vectorize trees that start at insertelement instructions.
180 bool vectorizeInsertElementInst(InsertElementInst *IEI, BasicBlock *BB,
181 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
182
183 /// Tries to vectorize \p CmpInts. \Returns true on success.
184 template <typename ItT>
185 bool vectorizeCmpInsts(iterator_range<ItT> CmpInsts, BasicBlock *BB,
187 SmallSetVector<Instruction *, 8> &FMACandidates);
188
189 /// Tries to vectorize the operand chains of the non-vectorizable
190 /// instructions in \p Insts.
191 template <typename ItT>
192 bool vectorizeNonVectorizableInsts(
194 SmallSetVector<Instruction *, 8> &FMACandidates);
195
196 /// Tries to vectorize constructs started from InsertValueInst or
197 /// InsertElementInst instructions.
198 bool vectorizeInserts(InstSetVector &Instructions, BasicBlock *BB,
200 SmallSetVector<Instruction *, 8> &FMACandidates);
201
202 /// Scan the basic block and look for patterns that are likely to start
203 /// a vectorization chain. The FMA candidates are collected in
204 /// \p FMACandidates for the retry after all the blocks of the function.
205 bool vectorizeChainsInBlock(BasicBlock *BB, slpvectorizer::BoUpSLP &R,
206 SmallSetVector<Instruction *, 8> &FMACandidates);
207
208 std::optional<bool> vectorizeStoreChain(ArrayRef<Value *> Chain,
210 unsigned Idx, unsigned MinVF,
211 unsigned &Size);
212
213 /// Single vectorization attempt for a store chain. \p vectorizeStoreChain
214 /// wraps this to retry once with runtime alias checks enabled when the
215 /// normal attempt is blocked only by runtime-checkable may-alias
216 /// dependencies.
217 std::optional<bool> vectorizeStoreChainImpl(ArrayRef<Value *> Chain,
219 unsigned Idx, unsigned MinVF,
220 unsigned &Size);
221
222 bool vectorizeStores(
224 DenseSet<std::tuple<Value *, Value *, Value *, Value *, unsigned>>
225 &Visited,
226 bool AllowMaskedStores = true);
227
228 /// Set by runImpl() when runtime alias check versioning changed the CFG, so
229 /// run() can drop CFG-analysis preservation only when necessary.
230 bool CFGChanged = false;
231
232 /// The store instructions in a basic block organized by base pointer.
233 StoreListMap Stores;
234
235 /// The getelementptr instructions in a basic block organized by base pointer.
236 GEPListMap GEPs;
237};
238
239} // end namespace llvm
240
241#endif // LLVM_TRANSFORMS_VECTORIZE_SLPVECTORIZER_H
#define LLVM_ABI
Definition Compiler.h:211
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
#define P(N)
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
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:62
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
This instruction inserts a single (scalar) element into a VectorType value.
This instruction inserts a struct field of array element value into an aggregate value.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
The optimization diagnostic interface.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
The main scalar evolution driver.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
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.
LLVM Value Representation.
Definition Value.h:75
Value handle that is nullable, but tries to track the Value.
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
Bottom Up SLP Vectorizer.
A private "module" namespace for types and utilities used by this pass.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in for passes that can be skipped.
MapVector< Value *, GEPList > GEPListMap
MapVector< Value *, StoreList > StoreListMap
ScalarEvolution * SE
TargetTransformInfo * TTI
AssumptionCache * AC
TargetLibraryInfo * TLI
SmallVector< StoreInst *, 8 > StoreList
SmallVector< GetElementPtrInst *, 8 > GEPList
const VectorizeOptions * Opts
const DataLayout * DL
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI bool runImpl(Function &F, ScalarEvolution *SE_, TargetTransformInfo *TTI_, TargetLibraryInfo *TLI_, AAResults *AA_, LoopInfo *LI_, DominatorTree *DT_, AssumptionCache *AC_, DemandedBits *DB_, OptimizationRemarkEmitter *ORE_)
SmallSetVector< Instruction *, 8 > InstSetVector