blob: 03512bf8c34fb84f8401e22c84cfb8783639d997 [file] [log] [blame]
Nadav Rotem2d9dec32013-04-09 19:44:35 +00001//===- 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
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 Rotem8543ba32013-04-12 21:16:54 +000074private:
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000075 /// \brief This method contains the recursive part of getTreeCost.
Nadav Rotem8543ba32013-04-12 21:16:54 +000076 int getTreeCost_rec(ValueList &VL, unsigned Depth);
77
Benjamin Kramer7d62ea82013-04-14 09:33:08 +000078 /// \brief This recursive method looks for vectorization hazards such as
Nadav Rotem8543ba32013-04-12 21:16:54 +000079 /// values that are used by multiple users and checks that values are used
80 /// by only one vector lane. It updates the variables LaneMap, MultiUserVals.
81 void getTreeUses_rec(ValueList &VL, unsigned Depth);
82
83 /// \brief This method contains the recursive part of vectorizeTree.
84 Value *vectorizeTree_rec(ValueList &VL, int VF);
85
Nadav Rotem2d9dec32013-04-09 19:44:35 +000086 /// \brief Number all of the instructions in the block.
87 void numberInstructions();
88
Nadav Rotem8543ba32013-04-12 21:16:54 +000089 /// \brief Vectorize a sorted sequence of stores.
90 bool vectorizeStoreChain(ValueList &Chain, int CostThreshold);
91
Nadav Rotem2d9dec32013-04-09 19:44:35 +000092 /// \returns the scalarization cost for this type. Scalarization in this
93 /// context means the creation of vectors from a group of scalars.
94 int getScalarizationCost(Type *Ty);
95
96 /// \returns the AA location that is being access by the instruction.
97 AliasAnalysis::Location getLocation(Instruction *I);
98
99 /// \brief Checks if it is possible to sink an instruction from
100 /// \p Src to \p Dst.
101 /// \returns the pointer to the barrier instruction if we can't sink.
102 Value *isUnsafeToSink(Instruction *Src, Instruction *Dst);
103
104 /// \returns the instruction that appears last in the BB from \p VL.
105 /// Only consider the first \p VF elements.
106 Instruction *GetLastInstr(ValueList &VL, unsigned VF);
107
108 /// \returns a vector from a collection of scalars in \p VL.
109 Value *Scalarize(ValueList &VL, VectorType *Ty);
110
Nadav Rotem8543ba32013-04-12 21:16:54 +0000111private:
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000112 // Maps instructions to numbers and back.
113 SmallDenseMap<Value*, int> InstrIdx;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000114 // Maps integers to Instructions.
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000115 std::vector<Instruction*> InstrVec;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000116
117 // -- containers that are used during getTreeCost -- //
118
119 /// Contains values that must be scalarized because they are used
120 /// by multiple lanes, or by users outside the tree.
121 /// NOTICE: The vectorization methods also use this set.
122 ValueSet MustScalarize;
Nadav Rotem54b413d2013-04-14 05:15:53 +0000123
Nadav Rotem8543ba32013-04-12 21:16:54 +0000124 // Contains a list of values that are used outside the current tree. This
125 // set must be reset between runs.
126 ValueSet MultiUserVals;
127 // Maps values in the tree to the vector lanes that uses them. This map must
128 // be reset between runs of getCost.
129 std::map<Value*, int> LaneMap;
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000130 // A list of instructions to ignore while sinking
Nadav Rotem8543ba32013-04-12 21:16:54 +0000131 // memory instructions. This map must be reset between runs of getCost.
Benjamin Kramer7d62ea82013-04-14 09:33:08 +0000132 SmallPtrSet<Value *, 8> MemBarrierIgnoreList;
Nadav Rotem8543ba32013-04-12 21:16:54 +0000133
134 // -- containers that are used during vectorizeTree -- //
135 // Maps between the first scalar to the vector. This map must be reset between
136 // runs.
137 DenseMap<Value*, Value*> VectorizedValues;
138
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000139 // Analysis and block reference.
140 BasicBlock *BB;
141 ScalarEvolution *SE;
142 DataLayout *DL;
143 TargetTransformInfo *TTI;
144 AliasAnalysis *AA;
145};
146
147} // end of namespace
Nadav Rotem2d9dec32013-04-09 19:44:35 +0000148
Benjamin Kramer7d62ea82013-04-14 09:33:08 +0000149#endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H