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;
48class WeakTrackingVH;
49
50/// A private "module" namespace for types and utilities used by this pass.
51/// These are implementation details and should not be used by clients.
52namespace slpvectorizer {
53
54class BoUpSLP;
55
56} // end namespace slpvectorizer
57
58struct SLPVectorizerPass : public OptionalPassInfoMixin<SLPVectorizerPass> {
64
65 ScalarEvolution *SE = nullptr;
68 AAResults *AA = nullptr;
69 LoopInfo *LI = nullptr;
70 DominatorTree *DT = nullptr;
71 AssumptionCache *AC = nullptr;
72 DemandedBits *DB = nullptr;
73 const DataLayout *DL = nullptr;
74
75public:
77
78 // Glue for old PM.
81 AAResults *AA_, LoopInfo *LI_, DominatorTree *DT_,
84
85private:
86 /// Collect store and getelementptr instructions and organize them
87 /// according to the underlying object of their pointer operands. We sort the
88 /// instructions by their underlying objects to reduce the cost of
89 /// consecutive access queries.
90 ///
91 /// TODO: We can further reduce this cost if we flush the chain creation
92 /// every time we run into a memory barrier.
93 void collectSeedInstructions(BasicBlock *BB);
94
95 /// Try to vectorize a list of operands.
96 /// \param MaxVFOnly Vectorize only using maximal allowed register size.
97 /// \param StandaloneSeeds \p VL are the standalone seeds: the vector factor
98 /// is limited by a single register and the windows, overlapping with the
99 /// rejected ones, are not retried.
100 /// \returns true if a value was vectorized.
101 bool tryToVectorizeList(ArrayRef<Value *> VL, slpvectorizer::BoUpSLP &R,
102 bool MaxVFOnly = false, bool StandaloneSeeds = false);
103
104 /// Try to vectorize a chain that may start at the operands of \p I.
105 bool tryToVectorize(Instruction *I, slpvectorizer::BoUpSLP &R,
107 bool AllowFMACandidates = false);
108
109 /// Try to vectorize chains that may start at the operands of
110 /// instructions in \p Insts.
111 bool tryToVectorize(ArrayRef<WeakTrackingVH> Insts, slpvectorizer::BoUpSLP &R,
112 SmallSetVector<Instruction *, 8> &FMACandidates);
113
114 /// Vectorize the store instructions collected in Stores.
115 bool vectorizeStoreChains(slpvectorizer::BoUpSLP &R);
116
117 /// Try to vectorize the poor-throughput instructions \p Candidates as the
118 /// standalone seeds.
119 bool vectorizePoorThroughputSeeds(ArrayRef<Instruction *> Candidates,
121
122 /// Try to vectorize the standalone seeds \p Seeds in the groups of the
123 /// compatible instructions, \p IsLessGroup orders the groups.
124 bool vectorizeSeeds(SmallVectorImpl<Value *> &Seeds,
125 function_ref<bool(Value *, Value *)> IsLessGroup,
127
128 /// Try to vectorize the instructions with the single user in \p BB as the
129 /// standalone seeds.
130 bool vectorizeOnceUsedSeeds(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
131
132 /// Vectorize the index computations of the getelementptr instructions
133 /// collected in GEPs.
134 bool vectorizeGEPIndices(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
135
136 /// Try to find horizontal reduction or otherwise, collect instructions
137 /// for postponed vectorization attempts.
138 /// \a P if not null designates phi node the reduction is fed into
139 /// (with reduction operators \a Root or one of its operands, in a basic block
140 /// \a BB).
141 /// \returns true if a horizontal reduction was matched and reduced.
142 /// \returns false if \a V is null or not an instruction,
143 /// or a horizontal reduction was not matched or not possible.
144 bool vectorizeHorReduction(PHINode *P, Instruction *Root, BasicBlock *BB,
146 SmallVectorImpl<WeakTrackingVH> &PostponedInsts);
147
148 /// Make an attempt to vectorize reduction and then try to vectorize
149 /// postponed binary operations.
150 /// \returns true on any successfull vectorization.
151 bool
152 vectorizeRootInstruction(PHINode *P, Instruction *Root, BasicBlock *BB,
154 SmallSetVector<Instruction *, 8> &FMACandidates);
155
156 /// Try to vectorize trees that start at insertvalue instructions.
157 bool vectorizeInsertValueInst(InsertValueInst *IVI, BasicBlock *BB,
158 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
159
160 /// Try to vectorize trees that start at insertelement instructions.
161 bool vectorizeInsertElementInst(InsertElementInst *IEI, BasicBlock *BB,
162 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
163
164 /// Tries to vectorize \p CmpInts. \Returns true on success.
165 template <typename ItT>
166 bool vectorizeCmpInsts(iterator_range<ItT> CmpInsts, BasicBlock *BB,
168 SmallSetVector<Instruction *, 8> &FMACandidates);
169
170 /// Tries to vectorize the operand chains of the non-vectorizable
171 /// instructions in \p Insts.
172 template <typename ItT>
173 bool vectorizeNonVectorizableInsts(
175 SmallSetVector<Instruction *, 8> &FMACandidates);
176
177 /// Tries to vectorize constructs started from InsertValueInst or
178 /// InsertElementInst instructions.
179 bool vectorizeInserts(InstSetVector &Instructions, BasicBlock *BB,
181 SmallSetVector<Instruction *, 8> &FMACandidates);
182
183 /// Scan the basic block and look for patterns that are likely to start
184 /// a vectorization chain. The FMA candidates are collected in
185 /// \p FMACandidates for the retry after all the blocks of the function.
186 bool vectorizeChainsInBlock(BasicBlock *BB, slpvectorizer::BoUpSLP &R,
187 SmallSetVector<Instruction *, 8> &FMACandidates);
188
189 std::optional<bool> vectorizeStoreChain(ArrayRef<Value *> Chain,
191 unsigned Idx, unsigned MinVF,
192 unsigned &Size);
193
194 /// Single vectorization attempt for a store chain. \p vectorizeStoreChain
195 /// wraps this to retry once with runtime alias checks enabled when the
196 /// normal attempt is blocked only by runtime-checkable may-alias
197 /// dependencies.
198 std::optional<bool> vectorizeStoreChainImpl(ArrayRef<Value *> Chain,
200 unsigned Idx, unsigned MinVF,
201 unsigned &Size);
202
203 bool vectorizeStores(
205 DenseSet<std::tuple<Value *, Value *, Value *, Value *, unsigned>>
206 &Visited,
207 bool AllowMaskedStores = true);
208
209 /// Set by runImpl() when runtime alias check versioning changed the CFG, so
210 /// run() can drop CFG-analysis preservation only when necessary.
211 bool CFGChanged = false;
212
213 /// The store instructions in a basic block organized by base pointer.
214 StoreListMap Stores;
215
216 /// The getelementptr instructions in a basic block organized by base pointer.
217 GEPListMap GEPs;
218};
219
220} // end namespace llvm
221
222#endif // LLVM_TRANSFORMS_VECTORIZE_SLPVECTORIZER_H
#define LLVM_ABI
Definition Compiler.h:215
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 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