LLVM 23.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.
80 TargetLibraryInfo *TLI_, AAResults *AA_, LoopInfo *LI_,
83
84private:
85 /// Collect store and getelementptr instructions and organize them
86 /// according to the underlying object of their pointer operands. We sort the
87 /// instructions by their underlying objects to reduce the cost of
88 /// consecutive access queries.
89 ///
90 /// TODO: We can further reduce this cost if we flush the chain creation
91 /// every time we run into a memory barrier.
92 void collectSeedInstructions(BasicBlock *BB);
93
94 /// Try to vectorize a list of operands.
95 /// \param MaxVFOnly Vectorize only using maximal allowed register size.
96 /// \returns true if a value was vectorized.
97 bool tryToVectorizeList(ArrayRef<Value *> VL, slpvectorizer::BoUpSLP &R,
98 bool MaxVFOnly = false);
99
100 /// Try to vectorize a chain that may start at the operands of \p I.
101 bool tryToVectorize(Instruction *I, slpvectorizer::BoUpSLP &R,
103 bool AllowFMACandidates = false);
104
105 /// Try to vectorize chains that may start at the operands of
106 /// instructions in \p Insts.
107 bool tryToVectorize(ArrayRef<WeakTrackingVH> Insts, slpvectorizer::BoUpSLP &R,
108 SmallSetVector<Instruction *, 8> &FMACandidates);
109
110 /// Vectorize the store instructions collected in Stores.
111 bool vectorizeStoreChains(slpvectorizer::BoUpSLP &R);
112
113 /// Vectorize the index computations of the getelementptr instructions
114 /// collected in GEPs.
115 bool vectorizeGEPIndices(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
116
117 /// Try to find horizontal reduction or otherwise, collect instructions
118 /// for postponed vectorization attempts.
119 /// \a P if not null designates phi node the reduction is fed into
120 /// (with reduction operators \a Root or one of its operands, in a basic block
121 /// \a BB).
122 /// \returns true if a horizontal reduction was matched and reduced.
123 /// \returns false if \a V is null or not an instruction,
124 /// or a horizontal reduction was not matched or not possible.
125 bool vectorizeHorReduction(PHINode *P, Instruction *Root, BasicBlock *BB,
127 SmallVectorImpl<WeakTrackingVH> &PostponedInsts);
128
129 /// Make an attempt to vectorize reduction and then try to vectorize
130 /// postponed binary operations.
131 /// \returns true on any successfull vectorization.
132 bool
133 vectorizeRootInstruction(PHINode *P, Instruction *Root, BasicBlock *BB,
135 SmallSetVector<Instruction *, 8> &FMACandidates);
136
137 /// Try to vectorize trees that start at insertvalue instructions.
138 bool vectorizeInsertValueInst(InsertValueInst *IVI, BasicBlock *BB,
139 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
140
141 /// Try to vectorize trees that start at insertelement instructions.
142 bool vectorizeInsertElementInst(InsertElementInst *IEI, BasicBlock *BB,
143 slpvectorizer::BoUpSLP &R, bool MaxVFOnly);
144
145 /// Tries to vectorize \p CmpInts. \Returns true on success.
146 template <typename ItT>
147 bool vectorizeCmpInsts(iterator_range<ItT> CmpInsts, BasicBlock *BB,
149 SmallSetVector<Instruction *, 8> &FMACandidates);
150
151 /// Tries to vectorize the operand chains of the non-vectorizable
152 /// instructions in \p Insts.
153 template <typename ItT>
154 bool vectorizeNonVectorizableInsts(
156 SmallSetVector<Instruction *, 8> &FMACandidates);
157
158 /// Tries to vectorize constructs started from InsertValueInst or
159 /// InsertElementInst instructions.
160 bool vectorizeInserts(InstSetVector &Instructions, BasicBlock *BB,
162 SmallSetVector<Instruction *, 8> &FMACandidates);
163
164 /// Scan the basic block and look for patterns that are likely to start
165 /// a vectorization chain.
166 bool vectorizeChainsInBlock(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
167
168 std::optional<bool> vectorizeStoreChain(ArrayRef<Value *> Chain,
170 unsigned Idx, unsigned MinVF,
171 unsigned &Size);
172
173 bool vectorizeStores(
175 DenseSet<std::tuple<Value *, Value *, Value *, Value *, unsigned>>
176 &Visited);
177
178 /// The store instructions in a basic block organized by base pointer.
179 StoreListMap Stores;
180
181 /// The getelementptr instructions in a basic block organized by base pointer.
182 GEPListMap GEPs;
183};
184
185} // end namespace llvm
186
187#endif // LLVM_TRANSFORMS_VECTORIZE_SLPVECTORIZER_H
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:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
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:339
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.
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
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
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