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/None.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/IR/PassManager.h"
26 
27 namespace llvm {
28 
29 class AAResults;
30 class AssumptionCache;
31 class BasicBlock;
32 class CmpInst;
33 class DataLayout;
34 class DemandedBits;
35 class DominatorTree;
36 class Function;
37 class GetElementPtrInst;
38 class InsertElementInst;
39 class InsertValueInst;
40 class Instruction;
41 class LoopInfo;
42 class OptimizationRemarkEmitter;
43 class PHINode;
44 class ScalarEvolution;
45 class StoreInst;
46 class TargetLibraryInfo;
47 class TargetTransformInfo;
48 class Value;
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.
52 namespace slpvectorizer {
53 
54 class BoUpSLP;
55 
56 } // end namespace slpvectorizer
57 
58 struct SLPVectorizerPass : public PassInfoMixin<SLPVectorizerPass> {
59   using StoreList = SmallVector<StoreInst *, 8>;
60   using StoreListMap = MapVector<Value *, StoreList>;
61   using GEPList = SmallVector<GetElementPtrInst *, 8>;
62   using GEPListMap = MapVector<Value *, GEPList>;
63 
64   ScalarEvolution *SE = nullptr;
65   TargetTransformInfo *TTI = nullptr;
66   TargetLibraryInfo *TLI = nullptr;
67   AAResults *AA = nullptr;
68   LoopInfo *LI = nullptr;
69   DominatorTree *DT = nullptr;
70   AssumptionCache *AC = nullptr;
71   DemandedBits *DB = nullptr;
72   const DataLayout *DL = nullptr;
73 
74 public:
75   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
76 
77   // Glue for old PM.
78   bool runImpl(Function &F, ScalarEvolution *SE_, TargetTransformInfo *TTI_,
79                TargetLibraryInfo *TLI_, AAResults *AA_, LoopInfo *LI_,
80                DominatorTree *DT_, AssumptionCache *AC_, DemandedBits *DB_,
81                OptimizationRemarkEmitter *ORE_);
82 
83 private:
84   /// Collect store and getelementptr instructions and organize them
85   /// according to the underlying object of their pointer operands. We sort the
86   /// instructions by their underlying objects to reduce the cost of
87   /// consecutive access queries.
88   ///
89   /// TODO: We can further reduce this cost if we flush the chain creation
90   ///       every time we run into a memory barrier.
91   void collectSeedInstructions(BasicBlock *BB);
92 
93   /// Try to vectorize a chain that starts at two arithmetic instrs.
94   bool tryToVectorizePair(Value *A, Value *B, slpvectorizer::BoUpSLP &R);
95 
96   /// Try to vectorize a list of operands.
97   /// When \p InsertUses is provided and its entries are non-zero
98   /// then users of \p VL are known to be InsertElement instructions
99   /// each associated with same VL entry index. Their cost is then
100   /// used to adjust cost of the vectorization assuming instcombine pass
101   /// then optimizes ExtractElement-InsertElement sequence.
102   /// \returns true if a value was vectorized.
103   bool tryToVectorizeList(ArrayRef<Value *> VL, slpvectorizer::BoUpSLP &R,
104                           bool AllowReorder = false,
105                           ArrayRef<Value *> InsertUses = None);
106 
107   /// Try to vectorize a chain that may start at the operands of \p I.
108   bool tryToVectorize(Instruction *I, slpvectorizer::BoUpSLP &R);
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 vectorize a chain of binary
118   /// operators.
119   bool vectorizeRootInstruction(PHINode *P, Value *V, BasicBlock *BB,
120                                 slpvectorizer::BoUpSLP &R,
121                                 TargetTransformInfo *TTI);
122 
123   /// Try to vectorize trees that start at insertvalue instructions.
124   bool vectorizeInsertValueInst(InsertValueInst *IVI, BasicBlock *BB,
125                                 slpvectorizer::BoUpSLP &R);
126 
127   /// Try to vectorize trees that start at insertelement instructions.
128   bool vectorizeInsertElementInst(InsertElementInst *IEI, BasicBlock *BB,
129                                   slpvectorizer::BoUpSLP &R);
130 
131   /// Try to vectorize trees that start at compare instructions.
132   bool vectorizeCmpInst(CmpInst *CI, BasicBlock *BB, slpvectorizer::BoUpSLP &R);
133 
134   /// Tries to vectorize constructs started from CmpInst, InsertValueInst or
135   /// InsertElementInst instructions.
136   bool vectorizeSimpleInstructions(SmallVectorImpl<Instruction *> &Instructions,
137                                    BasicBlock *BB, slpvectorizer::BoUpSLP &R);
138 
139   /// Scan the basic block and look for patterns that are likely to start
140   /// a vectorization chain.
141   bool vectorizeChainsInBlock(BasicBlock *BB, slpvectorizer::BoUpSLP &R);
142 
143   bool vectorizeStoreChain(ArrayRef<Value *> Chain, slpvectorizer::BoUpSLP &R,
144                            unsigned Idx);
145 
146   bool vectorizeStores(ArrayRef<StoreInst *> Stores, slpvectorizer::BoUpSLP &R);
147 
148   /// The store instructions in a basic block organized by base pointer.
149   StoreListMap Stores;
150 
151   /// The getelementptr instructions in a basic block organized by base pointer.
152   GEPListMap GEPs;
153 };
154 
155 } // end namespace llvm
156 
157 #endif // LLVM_TRANSFORMS_VECTORIZE_SLPVECTORIZER_H
158