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 | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 64 | /// DecodePSHUFMask - This decodes the shuffle masks for pshufd, and vpermilp*. |
| 65 | /// VT indicates the type of the vector allowing it to handle different |
| 66 | /// datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 67 | void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 68 | unsigned NumElts = VT.getVectorNumElements(); |
| 69 | |
| 70 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 71 | unsigned NumLaneElts = NumElts / NumLanes; |
| 72 | |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 73 | unsigned NewImm = Imm; |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 74 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 75 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 76 | ShuffleMask.push_back(NewImm % NumLaneElts + l); |
| 77 | NewImm /= NumLaneElts; |
| 78 | } |
| 79 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 83 | void DecodePSHUFHWMask(MVT VT, unsigned Imm, |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 84 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 315a5cc | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 85 | unsigned NumElts = VT.getVectorNumElements(); |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 86 | |
| 87 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 88 | unsigned NewImm = Imm; |
| 89 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 90 | ShuffleMask.push_back(l + i); |
| 91 | } |
| 92 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 93 | ShuffleMask.push_back(l + 4 + (NewImm & 3)); |
| 94 | NewImm >>= 2; |
| 95 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 99 | void DecodePSHUFLWMask(MVT VT, unsigned Imm, |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 100 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 315a5cc | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 101 | unsigned NumElts = VT.getVectorNumElements(); |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 102 | |
| 103 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 104 | unsigned NewImm = Imm; |
| 105 | for (unsigned i = 0, e = 4; i != e; ++i) { |
| 106 | ShuffleMask.push_back(l + (NewImm & 3)); |
| 107 | NewImm >>= 2; |
| 108 | } |
| 109 | for (unsigned i = 4, e = 8; i != e; ++i) { |
| 110 | ShuffleMask.push_back(l + i); |
| 111 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 112 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 115 | /// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates |
| 116 | /// the type of the vector allowing it to handle different datatypes and vector |
| 117 | /// widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 118 | void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 119 | unsigned NumElts = VT.getVectorNumElements(); |
| 120 | |
| 121 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 122 | unsigned NumLaneElts = NumElts / NumLanes; |
| 123 | |
Craig Topper | c73bc39 | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 124 | unsigned NewImm = Imm; |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 125 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 126 | // each half of a lane comes from different source |
| 127 | for (unsigned s = 0; s != NumElts*2; s += NumElts) { |
| 128 | for (unsigned i = 0; i != NumLaneElts/2; ++i) { |
| 129 | ShuffleMask.push_back(NewImm % NumLaneElts + s + l); |
| 130 | NewImm /= NumLaneElts; |
| 131 | } |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 132 | } |
| 133 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 137 | /// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd |
| 138 | /// and punpckh*. VT indicates the type of the vector allowing it to handle |
| 139 | /// different datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 140 | void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 141 | unsigned NumElts = VT.getVectorNumElements(); |
| 142 | |
| 143 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 144 | // independently on 128-bit lanes. |
| 145 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 146 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 147 | unsigned NumLaneElts = NumElts / NumLanes; |
| 148 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 149 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 150 | for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 151 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 152 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
| 153 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Craig Topper | 3cb802c | 2011-12-06 05:31:16 +0000 | [diff] [blame] | 157 | /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 158 | /// and punpckl*. VT indicates the type of the vector allowing it to handle |
| 159 | /// different datatypes and vector widths. |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 160 | void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) { |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 161 | unsigned NumElts = VT.getVectorNumElements(); |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 162 | |
Bruno Cardoso Lopes | f8fe47b | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 163 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 164 | // independently on 128-bit lanes. |
| 165 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 166 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 167 | unsigned NumLaneElts = NumElts / NumLanes; |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 168 | |
Craig Topper | 1f71057 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 169 | for (unsigned l = 0; l != NumElts; l += NumLaneElts) { |
| 170 | for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 171 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 172 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 173 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 177 | void DecodeVPERM2X128Mask(MVT VT, unsigned Imm, |
Craig Topper | cbc96a6 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 178 | SmallVectorImpl<int> &ShuffleMask) { |
Craig Topper | 354103d | 2012-04-17 05:54:54 +0000 | [diff] [blame] | 179 | if (Imm & 0x88) |
| 180 | return; // Not a shuffle |
| 181 | |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 182 | unsigned HalfSize = VT.getVectorNumElements()/2; |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 183 | |
Craig Topper | 00a1e6d | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 184 | for (unsigned l = 0; l != 2; ++l) { |
| 185 | unsigned HalfBegin = ((Imm >> (l*4)) & 0x3) * HalfSize; |
| 186 | for (unsigned i = HalfBegin, e = HalfBegin+HalfSize; i != e; ++i) |
| 187 | ShuffleMask.push_back(i); |
| 188 | } |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Craig Topper | 54bdb35 | 2012-05-06 18:44:02 +0000 | [diff] [blame] | 191 | /// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD. |
| 192 | /// No VT provided since it only works on 256-bit, 4 element vectors. |
| 193 | void DecodeVPERMMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) { |
| 194 | for (unsigned i = 0; i != 4; ++i) { |
| 195 | ShuffleMask.push_back((Imm >> (2*i)) & 3); |
| 196 | } |
| 197 | } |
| 198 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 199 | } // llvm namespace |