blob: 5456c6c7795992701ebc1b19dd72ee0d534bb8b4 [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;
Nadav Rotem83c7c412013-04-20 06:13:47 +000030class Loop;
Nadav Rotem2d9dec32013-04-09 19:44:35 +000031
32/// Bottom Up SLP vectorization utility class.
33struct BoUpSLP {
34 typedef SmallVector<Value*, 8> ValueList;
35 typedef SmallPtrSet<Value*, 16> ValueSet;
36 typedef SmallVector<StoreInst*, 8> StoreList;
37 static const int max_cost = 1<<20;
38
39 // \brief C'tor.
40 BoUpSLP(BasicBlock *Bb, ScalarEvolution *Se, DataLayout *Dl,
Nadav Rotem83c7c412013-04-20 06:13:47 +000041 TargetTransformInfo *Tti, AliasAnalysis *Aa, Loop *Lp);
Nadav Rotem2d9dec32013-04-09 19:44:35 +000042
Nadav Rotem8543ba32013-04-12 21:16:54 +000043 /// \brief Take the pointer operand from the Load/Store instruction.
44 /// \returns NULL if this is not a valid Load/Store instruction.
45 static Value *getPointerOperand(Value *I);
46
47 /// \brief Take the address space operand from the Load/Store instruction.
48 /// \returns -1 if this is not a valid Load/Store instruction.
49 static unsigned getAddressSpaceOperand(Value *I);
50
Nadav Rotem2d9dec32013-04-09 19:44:35 +000051 /// \returns true if the memory operations A and B are consecutive.
52 bool isConsecutiveAccess(Value *A, Value *B);
53
54 /// \brief Vectorize the tree that starts with the elements in \p VL.
55 /// \returns the vectorized value.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000056 Value *vectorizeTree(ArrayRef<Value *> VL, int VF);
Nadav Rotem2d9dec32013-04-09 19:44:35 +000057
58 /// \returns the vectorization cost of the subtree that starts at \p VL.
59 /// A negative number means that this is profitable.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000060 int getTreeCost(ArrayRef<Value *> VL);
Nadav Rotem2d9dec32013-04-09 19:44:35 +000061
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000062 /// \returns the scalarization cost for this list of values. Assuming that
63 /// this subtree gets vectorized, we may need to extract the values from the
Nadav Rotem54b413d2013-04-14 05:15:53 +000064 /// roots. This method calculates the cost of extracting the values.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000065 int getScalarizationCost(ArrayRef<Value *> VL);
Nadav Rotem54b413d2013-04-14 05:15:53 +000066
Nadav Rotem2d9dec32013-04-09 19:44:35 +000067 /// \brief Attempts to order and vectorize a sequence of stores. This
68 /// function does a quadratic scan of the given stores.
69 /// \returns true if the basic block was modified.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000070 bool vectorizeStores(ArrayRef<StoreInst *> Stores, int costThreshold);
Nadav Rotem2d9dec32013-04-09 19:44:35 +000071
Nadav Rotem0b9cf852013-04-14 03:22:20 +000072 /// \brief Vectorize a group of scalars into a vector tree.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000073 void vectorizeArith(ArrayRef<Value *> Operands);
Nadav Rotem0b9cf852013-04-14 03:22:20 +000074
Nadav Rotemb9116e62013-04-15 22:00:26 +000075 /// \returns the list of new instructions that were added in order to collect
76 /// scalars into vectors. This list can be used to further optimize the gather
77 /// sequences.
78 ValueList &getGatherSeqInstructions() {return GatherInstructions; }
79
Nadav Rotem8543ba32013-04-12 21:16:54 +000080private:
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000081 /// \brief This method contains the recursive part of getTreeCost.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000082 int getTreeCost_rec(ArrayRef<Value *> VL, unsigned Depth);
Nadav Rotem8543ba32013-04-12 21:16:54 +000083
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000084 /// \brief This recursive method looks for vectorization hazards such as
Nadav Rotem8543ba32013-04-12 21:16:54 +000085 /// values that are used by multiple users and checks that values are used
86 /// by only one vector lane. It updates the variables LaneMap, MultiUserVals.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000087 void getTreeUses_rec(ArrayRef<Value *> VL, unsigned Depth);
Nadav Rotem8543ba32013-04-12 21:16:54 +000088
89 /// \brief This method contains the recursive part of vectorizeTree.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000090 Value *vectorizeTree_rec(ArrayRef<Value *> VL, int VF);
Nadav Rotem8543ba32013-04-12 21:16:54 +000091
Nadav Rotem2d9dec32013-04-09 19:44:35 +000092 /// \brief Number all of the instructions in the block.
93 void numberInstructions();
94
Nadav Rotem8543ba32013-04-12 21:16:54 +000095 /// \brief Vectorize a sorted sequence of stores.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +000096 bool vectorizeStoreChain(ArrayRef<Value *> Chain, int CostThreshold);
Nadav Rotem8543ba32013-04-12 21:16:54 +000097
Nadav Rotem2d9dec32013-04-09 19:44:35 +000098 /// \returns the scalarization cost for this type. Scalarization in this
99 /// context means the creation of vectors from a group of scalars.
100 int getScalarizationCost(Type *Ty);
101
102 /// \returns the AA location that is being access by the instruction.
103 AliasAnalysis::Location getLocation(Instruction *I);
104
105 /// \brief Checks if it is possible to sink an instruction from
106 /// \p Src to \p Dst.
107 /// \returns the pointer to the barrier instruction if we can't sink.
108 Value *isUnsafeToSink(Instruction *Src, Instruction *Dst);
109
110 /// \returns the instruction that appears last in the BB from \p VL.
111 /// Only consider the first \p VF elements.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +0000112 Instruction *GetLastInstr(ArrayRef<Value *> VL, unsigned VF);
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000113
114 /// \returns a vector from a collection of scalars in \p VL.
Benjamin Kramer4600bcc2013-04-20 09:49:10 +0000115 Value *Scalarize(ArrayRef<Value *> VL, VectorType *Ty);
Nadav Rotem83c7c412013-04-20 06:13:47 +0000116
Nadav Rotem8543ba32013-04-12 21:16:54 +0000117private:
Nadav Rotemb9116e62013-04-15 22:00:26 +0000118 /// Maps instructions to numbers and back.
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000119 SmallDenseMap<Value*, int> InstrIdx;
Nadav Rotemb9116e62013-04-15 22:00:26 +0000120 /// Maps integers to Instructions.
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000121 std::vector<Instruction*> InstrVec;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000122
123 // -- containers that are used during getTreeCost -- //
124
125 /// Contains values that must be scalarized because they are used
126 /// by multiple lanes, or by users outside the tree.
127 /// NOTICE: The vectorization methods also use this set.
128 ValueSet MustScalarize;
Nadav Rotem54b413d2013-04-14 05:15:53 +0000129
Nadav Rotemb9116e62013-04-15 22:00:26 +0000130 /// Contains a list of values that are used outside the current tree. This
131 /// set must be reset between runs.
Nadav Rotem8543ba32013-04-12 21:16:54 +0000132 ValueSet MultiUserVals;
Nadav Rotemb9116e62013-04-15 22:00:26 +0000133 /// Maps values in the tree to the vector lanes that uses them. This map must
134 /// be reset between runs of getCost.
Nadav Rotem8543ba32013-04-12 21:16:54 +0000135 std::map<Value*, int> LaneMap;
Nadav Rotemb9116e62013-04-15 22:00:26 +0000136 /// A list of instructions to ignore while sinking
137 /// memory instructions. This map must be reset between runs of getCost.
Benjamin Kramer7d62ea82013-04-14 09:33:08 +0000138 SmallPtrSet<Value *, 8> MemBarrierIgnoreList;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000139
Nadav Rotemb9116e62013-04-15 22:00:26 +0000140 // -- Containers that are used during vectorizeTree -- //
141
142 /// Maps between the first scalar to the vector. This map must be reset
143 ///between runs.
Nadav Rotem8543ba32013-04-12 21:16:54 +0000144 DenseMap<Value*, Value*> VectorizedValues;
145
Nadav Rotemb9116e62013-04-15 22:00:26 +0000146 // -- Containers that are used after vectorization by the caller -- //
147
148 /// A list of instructions that are used when gathering scalars into vectors.
149 /// In many cases these instructions can be hoisted outside of the BB.
150 /// Iterating over this list is faster than calling LICM.
151 ValueList GatherInstructions;
152
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000153 // Analysis and block reference.
154 BasicBlock *BB;
155 ScalarEvolution *SE;
156 DataLayout *DL;
157 TargetTransformInfo *TTI;
158 AliasAnalysis *AA;
Nadav Rotem83c7c412013-04-20 06:13:47 +0000159 Loop *L;
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000160};
161
162} // end of namespace
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000163
Benjamin Kramer7d62ea82013-04-14 09:33:08 +0000164#endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H