David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 1 | //===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===// |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +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 | // Define several functions to decode x86 specific shuffle semantics into a |
| 11 | // generic vector mask. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 15 | #include "X86ShuffleDecode.h" |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 16 | |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | // Vector Mask Decoding |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 21 | namespace llvm { |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 22 | |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 23 | void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Bruno Cardoso Lopes | 02a05a6 | 2010-09-02 22:43:39 +0000 | [diff] [blame] | 24 | // Defaults the copying the dest value. |
| 25 | ShuffleMask.push_back(0); |
| 26 | ShuffleMask.push_back(1); |
| 27 | ShuffleMask.push_back(2); |
| 28 | ShuffleMask.push_back(3); |
| 29 | |
| 30 | // Decode the immediate. |
| 31 | unsigned ZMask = Imm & 15; |
| 32 | unsigned CountD = (Imm >> 4) & 3; |
| 33 | unsigned CountS = (Imm >> 6) & 3; |
| 34 | |
| 35 | // CountS selects which input element to use. |
| 36 | unsigned InVal = 4+CountS; |
| 37 | // CountD specifies which element of destination to update. |
| 38 | ShuffleMask[CountD] = InVal; |
| 39 | // ZMask zaps values, potentially overriding the CountD elt. |
| 40 | if (ZMask & 1) ShuffleMask[0] = SM_SentinelZero; |
| 41 | if (ZMask & 2) ShuffleMask[1] = SM_SentinelZero; |
| 42 | if (ZMask & 4) ShuffleMask[2] = SM_SentinelZero; |
| 43 | if (ZMask & 8) ShuffleMask[3] = SM_SentinelZero; |
| 44 | } |
| 45 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 46 | // <3,1> or <6,7,2,3> |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 47 | void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 48 | for (unsigned i = NElts/2; i != NElts; ++i) |
| 49 | ShuffleMask.push_back(NElts+i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 50 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 51 | for (unsigned i = NElts/2; i != NElts; ++i) |
| 52 | ShuffleMask.push_back(i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 55 | // <0,2> or <0,1,4,5> |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 56 | void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) { |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 57 | for (unsigned i = 0; i != NElts/2; ++i) |
| 58 | ShuffleMask.push_back(i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 59 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 60 | for (unsigned i = 0; i != NElts/2; ++i) |
| 61 | ShuffleMask.push_back(NElts+i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Craig Topper | 8fb09f0 | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 64 | void DecodePALIGNRMask(MVT VT, unsigned Imm, |
| 65 | SmallVectorImpl<int> &ShuffleMask) { |
Benjamin Kramer | 6a93596 | 2013-01-26 13:31:37 +0000 | [diff] [blame] | 66 | unsigned NumElts = VT.getVectorNumElements(); |
| 67 | unsigned Offset = Imm * (VT.getVectorElementType().getSizeInBits() / 8); |
| 68 | |
Craig Topper | 5c68397 | 2013-01-28 07:41:18 +0000 | [diff] [blame^] | 69 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 70 | unsigned NumLaneElts = NumElts / NumLanes; |
| 71 | |
| 72 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 73 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 74 | unsigned Base = i + Offset; |
| 75 | // if i+offset is out of this lane then we actually need the other source |
| 76 | if (Base >= NumLaneElts) Base += NumElts - NumLaneElts; |
| 77 | ShuffleMask.push_back(Base + l); |
| 78 | } |
| 79 | } |
Benjamin Kramer | 6a93596 | 2013-01-26 13:31:37 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 82 | /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*. |
| 83 | /// VT indicates the type of the vector allowing it to handle different |
| 84 | /// datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 85 | void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 86 | unsigned NumElts = VT.getVectorNumElements(); |
| 87 | |
| 88 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 89 | unsigned NumLaneElts = NumElts / NumLanes; |
| 90 | |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 91 | unsigned NewImm = Imm; |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 92 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 93 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 94 | ShuffleMask.push_back(NewImm % NumLaneElts + l); |
| 95 | NewImm /= NumLaneElts; |
| 96 | } |
| 97 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 101 | void DecodePSHUFHWMask(MVT VT, unsigned Imm, |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 102 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 315a5cc | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 103 | unsigned NumElts = VT.getVectorNumElements(); |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 104 | |
| 105 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 106 | unsigned NewImm = Imm; |
| 107 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 108 | ShuffleMask.push_back(l + i); |
| 109 | } |
| 110 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 111 | ShuffleMask.push_back(l + 4 + (NewImm & 3)); |
| 112 | NewImm >>= 2; |
| 113 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 117 | void DecodePSHUFLWMask(MVT VT, unsigned Imm, |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 118 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 315a5cc | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 119 | unsigned NumElts = VT.getVectorNumElements(); |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 120 | |
| 121 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 122 | unsigned NewImm = Imm; |
| 123 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 124 | ShuffleMask.push_back(l + (NewImm & 3)); |
| 125 | NewImm >>= 2; |
| 126 | } |
| 127 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 128 | ShuffleMask.push_back(l + i); |
| 129 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 130 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 133 | /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates |
| 134 | /// the type of the vector allowing it to handle different datatypes and vector |
| 135 | /// widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 136 | void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 137 | unsigned NumElts = VT.getVectorNumElements(); |
| 138 | |
| 139 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 140 | unsigned NumLaneElts = NumElts / NumLanes; |
| 141 | |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 142 | unsigned NewImm = Imm; |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 143 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 144 | // each half of a lane comes from different source |
| 145 | for (unsigned s = 0; s != NumElts*2; s += NumElts) { |
| 146 | for (unsigned i = 0; i != NumLaneElts/2; ++i) { |
| 147 | ShuffleMask.push_back(NewImm % NumLaneElts + s + l); |
| 148 | NewImm /= NumLaneElts; |
| 149 | } |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 150 | } |
| 151 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 155 | /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd |
| 156 | /// and punpckh*. VT indicates the type of the vector allowing it to handle |
| 157 | /// different datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 158 | void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 159 | unsigned NumElts = VT.getVectorNumElements(); |
| 160 | |
| 161 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 162 | // independently on 128-bit lanes. |
| 163 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 164 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 165 | unsigned NumLaneElts = NumElts / NumLanes; |
| 166 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 167 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 168 | for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 169 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 170 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
| 171 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Craig Topper | 3cb802c | 2011-12-06 05:31:16 +0000 | [diff] [blame] | 175 | /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 176 | /// and punpckl*. VT indicates the type of the vector allowing it to handle |
| 177 | /// different datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 178 | void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 179 | unsigned NumElts = VT.getVectorNumElements(); |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 180 | |
Bruno Cardoso Lopes | f8fe47b | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 181 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 182 | // independently on 128-bit lanes. |
| 183 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 184 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 185 | unsigned NumLaneElts = NumElts / NumLanes; |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 186 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 187 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 188 | for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 189 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 190 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 191 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 195 | void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 196 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 354103d | 2012-04-17 05:54:54 +0000 | [diff] [blame] | 197 | if (Imm & 0x88) |
| 198 | return; // Not a shuffle |
| 199 | |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 200 | unsigned HalfSize = VT.getVectorNumElements()/2; |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 201 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 202 | for (unsigned l = 0; l != 2; ++l) { |
| 203 | unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize; |
| 204 | for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i) |
| 205 | ShuffleMask.push_back(i); |
| 206 | } |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Craig Topper | 54bdb35 | 2012-05-06 18:44:02 +0000 | [diff] [blame] | 209 | /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. |
| 210 | /// No VT provided since it only works on 256-bit, 4 element vectors. |
| 211 | void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 212 | for (unsigned i = 0; i != 4; ++i) { |
| 213 | ShuffleMask.push_back((Imm >> (2*i)) & 3); |
| 214 | } |
| 215 | } |
| 216 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 217 | } // llvm namespace |