blob: 3f6866407a606caf1aad64b08b985740a99121c3 [file] [log] [blame]
Nadav Rotemdfd8fcb2013-04-20 05:18:51 +00001//===- VecUtils.h - Vectorization Utilities -------------------------------===//
Nadav Rotem2d9dec32013-04-09 19:44:35 +00002//
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
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000015#ifndef LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
16#define LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
Nadav Rotem2d9dec32013-04-09 19:44:35 +000017
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallPtrSet.h"
Nadav Rotem2d9dec32013-04-09 19:44:35 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/Analysis/AliasAnalysis.h"
22#include <vector>
23
Nadav Rotem2d9dec32013-04-09 19:44:35 +000024namespace llvm {
25
26class BasicBlock; class Instruction; class Type;
27class VectorType; class StoreInst; class Value;
28class ScalarEvolution; class DataLayout;
29class TargetTransformInfo; class AliasAnalysis;
30
31/// Bottom Up SLP vectorization utility class.
32struct 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
Nadav Rotem8543ba32013-04-12 21:16:54 +000042 /// \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
Nadav Rotem2d9dec32013-04-09 19:44:35 +000050 /// \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.
Nadav Rotem8543ba32013-04-12 21:16:54 +000059 int getTreeCost(ValueList &VL);
Nadav Rotem2d9dec32013-04-09 19:44:35 +000060
Nadav Rotem54b413d2013-04-14 05:15:53 +000061 /// \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
Nadav Rotem2d9dec32013-04-09 19:44:35 +000066 /// \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
Nadav Rotem0b9cf852013-04-14 03:22:20 +000071 /// \brief Vectorize a group of scalars into a vector tree.
72 void vectorizeArith(ValueList &Operands);
73
Nadav Rotemb9116e62013-04-15 22:00:26 +000074 /// \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
Nadav Rotem8543ba32013-04-12 21:16:54 +000079private:
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000080 /// \brief This method contains the recursive part of getTreeCost.
Nadav Rotem8543ba32013-04-12 21:16:54 +000081 int getTreeCost_rec(ValueList &VL, unsigned Depth);
82
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000083 /// \brief This recursive method looks for vectorization hazards such as
Nadav Rotem8543ba32013-04-12 21:16:54 +000084 /// 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
Nadav Rotem2d9dec32013-04-09 19:44:35 +000091 /// \brief Number all of the instructions in the block.
92 void numberInstructions();
93
Nadav Rotem8543ba32013-04-12 21:16:54 +000094 /// \brief Vectorize a sorted sequence of stores.
95 bool vectorizeStoreChain(ValueList &Chain, int CostThreshold);
96
Nadav Rotem2d9dec32013-04-09 19:44:35 +000097 /// \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);
Nadav Rotemb9116e62013-04-15 22:00:26 +0000115
Nadav Rotem8543ba32013-04-12 21:16:54 +0000116private:
Nadav Rotemb9116e62013-04-15 22:00:26 +0000117 /// Maps instructions to numbers and back.
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000118 SmallDenseMap<Value*, int> InstrIdx;
Nadav Rotemb9116e62013-04-15 22:00:26 +0000119 /// Maps integers to Instructions.
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000120 std::vector<Instruction*> InstrVec;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000121
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;
Nadav Rotem54b413d2013-04-14 05:15:53 +0000128
Nadav Rotemb9116e62013-04-15 22:00:26 +0000129 /// Contains a list of values that are used outside the current tree. This
130 /// set must be reset between runs.
Nadav Rotem8543ba32013-04-12 21:16:54 +0000131 ValueSet MultiUserVals;
Nadav Rotemb9116e62013-04-15 22:00:26 +0000132 /// Maps values in the tree to the vector lanes that uses them. This map must
133 /// be reset between runs of getCost.
Nadav Rotem8543ba32013-04-12 21:16:54 +0000134 std::map<Value*, int> LaneMap;
Nadav Rotemb9116e62013-04-15 22:00:26 +0000135 /// A list of instructions to ignore while sinking
136 /// memory instructions. This map must be reset between runs of getCost.
Benjamin Kramer7d62ea82013-04-14 09:33:08 +0000137 SmallPtrSet<Value *, 8> MemBarrierIgnoreList;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000138
Nadav Rotemb9116e62013-04-15 22:00:26 +0000139 // -- Containers that are used during vectorizeTree -- //
140
141 /// Maps between the first scalar to the vector. This map must be reset
142 ///between runs.
Nadav Rotem8543ba32013-04-12 21:16:54 +0000143 DenseMap<Value*, Value*> VectorizedValues;
144
Nadav Rotemb9116e62013-04-15 22:00:26 +0000145 // -- 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
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000152 // 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
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000161
Benjamin Kramer7d62ea82013-04-14 09:33:08 +0000162#endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H