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 standalone seeds \p Seeds in the groups of the
118 /// compatible instructions, \p IsLessGroup orders the groups.
119 bool vectorizeSeeds(SmallVectorImpl<Value *> &Seeds,
120 function_ref<bool(Value *, Value *)> IsLessGroup,
122
123 /// Try to vectorize the instructions with the single user in \p BB as the
124 /// standalone seeds.
125 bool vectorizeOnceUsedSeeds(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
126
127 /// Vectorize the index computations of the getelementptr instructions
128 /// collected in GEPs.
129 bool vectorizeGEPIndices(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
130
131 /// Try to find horizontal reduction or otherwise, collect instructions
132 /// for postponed vectorization attempts.
133 /// \a P if not null designates phi node the reduction is fed into
134 /// (with reduction operators \a Root or one of its operands, in a basic block
135 /// \a BB).
136 /// \returns true if a horizontal reduction was matched and reduced.
137 /// \returns false if \a V is null or not an instruction,
138 /// or a horizontal reduction was not matched or not possible.
139 bool vectorizeHorReduction(PHINode *P, Instruction *Root, BasicBlock *BB,
141 SmallVectorImpl<WeakTrackingVH> &PostponedInsts);
142
143 /// Make an attempt to vectorize reduction and then try to vectorize
144 /// postponed binary operations.
145 /// \returns true on any successfull vectorization.
146 bool
147 vectorizeRootInstruction(PHINode *P, Instruction *Root, BasicBlock *BB,
149 SmallSetVector<Instruction *, 8> &FMACandidates);
150
151 /// Try to vectorize trees that start at insertvalue instructions.
152 bool vectorizeInsertValueInst(InsertValueInst *IVI, BasicBlock *BB,
153 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
154
155 /// Try to vectorize trees that start at insertelement instructions.
156 bool vectorizeInsertElementInst(InsertElementInst *IEI, BasicBlock *BB,
157 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
158
159 /// Tries to vectorize \p CmpInts. \Returns true on success.
160 template <typename ItT>
161 bool vectorizeCmpInsts(iterator_range<ItT> CmpInsts, BasicBlock *BB,
163 SmallSetVector<Instruction *, 8> &FMACandidates);
164
165 /// Tries to vectorize the operand chains of the non-vectorizable
166 /// instructions in \p Insts.
167 template <typename ItT>
168 bool vectorizeNonVectorizableInsts(
170 SmallSetVector<Instruction *, 8> &FMACandidates);
171
172 /// Tries to vectorize constructs started from InsertValueInst or
173 /// InsertElementInst instructions.
174 bool vectorizeInserts(InstSetVector &Instructions, BasicBlock *BB,
176 SmallSetVector<Instruction *, 8> &FMACandidates);
177
178 /// Scan the basic block and look for patterns that are likely to start
179 /// a vectorization chain.
180 bool vectorizeChainsInBlock(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
181
182 std::optional<bool> vectorizeStoreChain(ArrayRef<Value *> Chain,
184 unsigned Idx, unsigned MinVF,
185 unsigned &Size);
186
187 /// Single vectorization attempt for a store chain. \p vectorizeStoreChain
188 /// wraps this to retry once with runtime alias checks enabled when the
189 /// normal attempt is blocked only by runtime-checkable may-alias
190 /// dependencies.
191 std::optional<bool> vectorizeStoreChainImpl(ArrayRef<Value *> Chain,
193 unsigned Idx, unsigned MinVF,
194 unsigned &Size);
195
196 bool vectorizeStores(
198 DenseSet<std::tuple<Value *, Value *, Value *, Value *, unsigned>>
199 &Visited,
200 bool AllowMaskedStores = true);
201
202 /// Set by runImpl() when runtime alias check versioning changed the CFG, so
203 /// run() can drop CFG-analysis preservation only when necessary.
204 bool CFGChanged = false;
205
206 /// The store instructions in a basic block organized by base pointer.
207 StoreListMap Stores;
208
209 /// The getelementptr instructions in a basic block organized by base pointer.
210 GEPListMap GEPs;
211};
212
213} // end namespace llvm
214
215#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