SLPVectorizer: Make it a function pass and add code for hoisting the vector-gather...
[oota-llvm.git] / lib / Transforms / Vectorize / VecUtils.h
1 //===- VecUtils.cpp - Vectorization Utilities -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of classes and functions manipulate vectors and chains of
11 // vectors.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
16 #define LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include <vector>
23
24 namespace llvm {
25
26 class BasicBlock; class Instruction; class Type;
27 class VectorType; class StoreInst; class Value;
28 class ScalarEvolution; class DataLayout;
29 class TargetTransformInfo; class AliasAnalysis;
30
31 /// Bottom Up SLP vectorization utility class.
32 struct BoUpSLP  {
33   typedef SmallVector<Value*, 8> ValueList;
34   typedef SmallPtrSet<Value*, 16> ValueSet;
35   typedef SmallVector<StoreInst*, 8> StoreList;
36   static const int max_cost = 1<<20;
37
38   // \brief C'tor.
39   BoUpSLP(BasicBlock *Bb, ScalarEvolution *Se, DataLayout *Dl,
40          TargetTransformInfo *Tti, AliasAnalysis *Aa);
41
42   /// \brief Take the pointer operand from the Load/Store instruction.
43   /// \returns NULL if this is not a valid Load/Store instruction.
44   static Value *getPointerOperand(Value *I);
45
46   /// \brief Take the address space operand from the Load/Store instruction.
47   /// \returns -1 if this is not a valid Load/Store instruction.
48   static unsigned getAddressSpaceOperand(Value *I);
49
50   /// \returns true if the memory operations A and B are consecutive.
51   bool isConsecutiveAccess(Value *A, Value *B);
52
53   /// \brief Vectorize the tree that starts with the elements in \p VL.
54   /// \returns the vectorized value.
55   Value *vectorizeTree(ValueList &VL, int VF);
56
57   /// \returns the vectorization cost of the subtree that starts at \p VL.
58   /// A negative number means that this is profitable.
59   int getTreeCost(ValueList &VL);
60
61   /// \returns the scalarization cost for this ValueList. Assuming that this
62   /// subtree gets vectorized, we may need to extract the values from the
63   /// roots. This method calculates the cost of extracting the values.
64   int getScalarizationCost(ValueList &VL);
65
66   /// \brief Attempts to order and vectorize a sequence of stores. This
67   /// function does a quadratic scan of the given stores.
68   /// \returns true if the basic block was modified.
69   bool vectorizeStores(StoreList &Stores, int costThreshold);
70
71   /// \brief Vectorize a group of scalars into a vector tree.
72   void vectorizeArith(ValueList &Operands);
73
74   /// \returns the list of new instructions that were added in order to collect
75   /// scalars into vectors. This list can be used to further optimize the gather
76   /// sequences.
77   ValueList &getGatherSeqInstructions() {return GatherInstructions; }
78
79 private:
80   /// \brief This method contains the recursive part of getTreeCost.
81   int getTreeCost_rec(ValueList &VL, unsigned Depth);
82
83   /// \brief This recursive method looks for vectorization hazards such as
84   /// values that are used by multiple users and checks that values are used
85   /// by only one vector lane. It updates the variables LaneMap, MultiUserVals.
86   void getTreeUses_rec(ValueList &VL, unsigned Depth);
87
88   /// \brief This method contains the recursive part of vectorizeTree.
89   Value *vectorizeTree_rec(ValueList &VL, int VF);
90
91   /// \brief Number all of the instructions in the block.
92   void numberInstructions();
93
94   ///  \brief Vectorize a sorted sequence of stores.
95   bool vectorizeStoreChain(ValueList &Chain, int CostThreshold);
96
97   /// \returns the scalarization cost for this type. Scalarization in this
98   /// context means the creation of vectors from a group of scalars.
99   int getScalarizationCost(Type *Ty);
100
101   /// \returns the AA location that is being access by the instruction.
102   AliasAnalysis::Location getLocation(Instruction *I);
103
104   /// \brief Checks if it is possible to sink an instruction from
105   /// \p Src to \p Dst.
106   /// \returns the pointer to the barrier instruction if we can't sink.
107   Value *isUnsafeToSink(Instruction *Src, Instruction *Dst);
108
109   /// \returns the instruction that appears last in the BB from \p VL.
110   /// Only consider the first \p VF elements.
111   Instruction *GetLastInstr(ValueList &VL, unsigned VF);
112
113   /// \returns a vector from a collection of scalars in \p VL.
114   Value *Scalarize(ValueList &VL, VectorType *Ty);
115   
116 private:
117   /// Maps instructions to numbers and back.
118   SmallDenseMap<Value*, int> InstrIdx;
119   /// Maps integers to Instructions.
120   std::vector<Instruction*> InstrVec;
121
122   // -- containers that are used during getTreeCost -- //
123
124   /// Contains values that must be scalarized because they are used
125   /// by multiple lanes, or by users outside the tree.
126   /// NOTICE: The vectorization methods also use this set.
127   ValueSet MustScalarize;
128
129   /// Contains a list of values that are used outside the current tree. This
130   /// set must be reset between runs.
131   ValueSet MultiUserVals;
132   /// Maps values in the tree to the vector lanes that uses them. This map must
133   /// be reset between runs of getCost.
134   std::map<Value*, int> LaneMap;
135   /// A list of instructions to ignore while sinking
136   /// memory instructions. This map must be reset between runs of getCost.
137   SmallPtrSet<Value *, 8> MemBarrierIgnoreList;
138
139   // -- Containers that are used during vectorizeTree -- //
140
141   /// Maps between the first scalar to the vector. This map must be reset
142   ///between runs.
143   DenseMap<Value*, Value*> VectorizedValues;
144
145   // -- Containers that are used after vectorization by the caller -- //
146
147   /// A list of instructions that are used when gathering scalars into vectors.
148   /// In many cases these instructions can be hoisted outside of the BB.
149   /// Iterating over this list is faster than calling LICM.
150   ValueList GatherInstructions;
151
152   // Analysis and block reference.
153   BasicBlock *BB;
154   ScalarEvolution *SE;
155   DataLayout *DL;
156   TargetTransformInfo *TTI;
157   AliasAnalysis *AA;
158 };
159
160 } // end of namespace
161
162 #endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H