Nadav Rotem | dfd8fcb | 2013-04-20 05:18:51 +0000 | [diff] [blame] | 1 | //===- VecUtils.h - Vectorization Utilities -------------------------------===// |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 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 Kramer | 7d62ea8 | 2013-04-14 09:33:08 +0000 | [diff] [blame] | 15 | #ifndef LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H |
| 16 | #define LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 17 | |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/SmallPtrSet.h" |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/Analysis/AliasAnalysis.h" |
| 22 | #include <vector> |
| 23 | |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 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; |
Nadav Rotem | 83c7c41 | 2013-04-20 06:13:47 +0000 | [diff] [blame] | 30 | class Loop; |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 31 | |
| 32 | /// Bottom Up SLP vectorization utility class. |
| 33 | struct 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 Rotem | 83c7c41 | 2013-04-20 06:13:47 +0000 | [diff] [blame] | 41 | TargetTransformInfo *Tti, AliasAnalysis *Aa, Loop *Lp); |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 42 | |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 43 | /// \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 Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 51 | /// \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 Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 56 | Value *vectorizeTree(ArrayRef<Value *> VL, int VF); |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 57 | |
| 58 | /// \returns the vectorization cost of the subtree that starts at \p VL. |
| 59 | /// A negative number means that this is profitable. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 60 | int getTreeCost(ArrayRef<Value *> VL); |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 61 | |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 62 | /// \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 Rotem | 54b413d | 2013-04-14 05:15:53 +0000 | [diff] [blame] | 64 | /// roots. This method calculates the cost of extracting the values. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 65 | int getScalarizationCost(ArrayRef<Value *> VL); |
Nadav Rotem | 54b413d | 2013-04-14 05:15:53 +0000 | [diff] [blame] | 66 | |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 67 | /// \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 Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 70 | bool vectorizeStores(ArrayRef<StoreInst *> Stores, int costThreshold); |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 71 | |
Nadav Rotem | 0b9cf85 | 2013-04-14 03:22:20 +0000 | [diff] [blame] | 72 | /// \brief Vectorize a group of scalars into a vector tree. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 73 | void vectorizeArith(ArrayRef<Value *> Operands); |
Nadav Rotem | 0b9cf85 | 2013-04-14 03:22:20 +0000 | [diff] [blame] | 74 | |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 75 | /// \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 Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 80 | private: |
Benjamin Kramer | 7d62ea8 | 2013-04-14 09:33:08 +0000 | [diff] [blame] | 81 | /// \brief This method contains the recursive part of getTreeCost. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 82 | int getTreeCost_rec(ArrayRef<Value *> VL, unsigned Depth); |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 83 | |
Benjamin Kramer | 7d62ea8 | 2013-04-14 09:33:08 +0000 | [diff] [blame] | 84 | /// \brief This recursive method looks for vectorization hazards such as |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 85 | /// 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 Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 87 | void getTreeUses_rec(ArrayRef<Value *> VL, unsigned Depth); |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 88 | |
| 89 | /// \brief This method contains the recursive part of vectorizeTree. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 90 | Value *vectorizeTree_rec(ArrayRef<Value *> VL, int VF); |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 91 | |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 92 | /// \brief Number all of the instructions in the block. |
| 93 | void numberInstructions(); |
| 94 | |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 95 | /// \brief Vectorize a sorted sequence of stores. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 96 | bool vectorizeStoreChain(ArrayRef<Value *> Chain, int CostThreshold); |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 97 | |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 98 | /// \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 Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 112 | Instruction *GetLastInstr(ArrayRef<Value *> VL, unsigned VF); |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 113 | |
| 114 | /// \returns a vector from a collection of scalars in \p VL. |
Benjamin Kramer | 4600bcc | 2013-04-20 09:49:10 +0000 | [diff] [blame] | 115 | Value *Scalarize(ArrayRef<Value *> VL, VectorType *Ty); |
Nadav Rotem | 83c7c41 | 2013-04-20 06:13:47 +0000 | [diff] [blame] | 116 | |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 117 | private: |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 118 | /// Maps instructions to numbers and back. |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 119 | SmallDenseMap<Value*, int> InstrIdx; |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 120 | /// Maps integers to Instructions. |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 121 | std::vector<Instruction*> InstrVec; |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 122 | |
| 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 Rotem | 54b413d | 2013-04-14 05:15:53 +0000 | [diff] [blame] | 129 | |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 130 | /// Contains a list of values that are used outside the current tree. This |
| 131 | /// set must be reset between runs. |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 132 | ValueSet MultiUserVals; |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 133 | /// Maps values in the tree to the vector lanes that uses them. This map must |
| 134 | /// be reset between runs of getCost. |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 135 | std::map<Value*, int> LaneMap; |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 136 | /// A list of instructions to ignore while sinking |
| 137 | /// memory instructions. This map must be reset between runs of getCost. |
Benjamin Kramer | 7d62ea8 | 2013-04-14 09:33:08 +0000 | [diff] [blame] | 138 | SmallPtrSet<Value *, 8> MemBarrierIgnoreList; |
Nadav Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 139 | |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 140 | // -- 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 Rotem | 8543ba3 | 2013-04-12 21:16:54 +0000 | [diff] [blame] | 144 | DenseMap<Value*, Value*> VectorizedValues; |
| 145 | |
Nadav Rotem | b9116e6 | 2013-04-15 22:00:26 +0000 | [diff] [blame] | 146 | // -- 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 Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 153 | // Analysis and block reference. |
| 154 | BasicBlock *BB; |
| 155 | ScalarEvolution *SE; |
| 156 | DataLayout *DL; |
| 157 | TargetTransformInfo *TTI; |
| 158 | AliasAnalysis *AA; |
Nadav Rotem | 83c7c41 | 2013-04-20 06:13:47 +0000 | [diff] [blame] | 159 | Loop *L; |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // end of namespace |
Nadav Rotem | 2d9dec3 | 2013-04-09 19:44:35 +0000 | [diff] [blame] | 163 | |
Benjamin Kramer | 7d62ea8 | 2013-04-14 09:33:08 +0000 | [diff] [blame] | 164 | #endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H |