1 //===- VecUtils.h - Vectorization Utilities -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of classes and functions manipulate vectors and chains of
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
16 #define LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
26 class BasicBlock; class Instruction; class Type;
27 class VectorType; class StoreInst; class Value;
28 class ScalarEvolution; class DataLayout;
29 class TargetTransformInfo; class AliasAnalysis;
31 /// Bottom Up SLP vectorization utility class.
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;
39 BoUpSLP(BasicBlock *Bb, ScalarEvolution *Se, DataLayout *Dl,
40 TargetTransformInfo *Tti, AliasAnalysis *Aa);
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);
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);
50 /// \returns true if the memory operations A and B are consecutive.
51 bool isConsecutiveAccess(Value *A, Value *B);
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);
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);
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);
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);
71 /// \brief Vectorize a group of scalars into a vector tree.
72 void vectorizeArith(ValueList &Operands);
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
77 ValueList &getGatherSeqInstructions() {return GatherInstructions; }
80 /// \brief This method contains the recursive part of getTreeCost.
81 int getTreeCost_rec(ValueList &VL, unsigned Depth);
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);
88 /// \brief This method contains the recursive part of vectorizeTree.
89 Value *vectorizeTree_rec(ValueList &VL, int VF);
91 /// \brief Number all of the instructions in the block.
92 void numberInstructions();
94 /// \brief Vectorize a sorted sequence of stores.
95 bool vectorizeStoreChain(ValueList &Chain, int CostThreshold);
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);
101 /// \returns the AA location that is being access by the instruction.
102 AliasAnalysis::Location getLocation(Instruction *I);
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);
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);
113 /// \returns a vector from a collection of scalars in \p VL.
114 Value *Scalarize(ValueList &VL, VectorType *Ty);
117 /// Maps instructions to numbers and back.
118 SmallDenseMap<Value*, int> InstrIdx;
119 /// Maps integers to Instructions.
120 std::vector<Instruction*> InstrVec;
122 // -- containers that are used during getTreeCost -- //
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;
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;
139 // -- Containers that are used during vectorizeTree -- //
141 /// Maps between the first scalar to the vector. This map must be reset
143 DenseMap<Value*, Value*> VectorizedValues;
145 // -- Containers that are used after vectorization by the caller -- //
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;
152 // Analysis and block reference.
156 TargetTransformInfo *TTI;
160 } // end of namespace
162 #endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H