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 | |
Bruno Cardoso Lopes | 02a05a6 | 2010-09-02 22:43:39 +0000 | [diff] [blame] | 23 | void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<unsigned> &ShuffleMask) { |
| 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> |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 47 | void DecodeMOVHLPSMask(unsigned NElts, |
| 48 | SmallVectorImpl<unsigned> &ShuffleMask) { |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 49 | for (unsigned i = NElts/2; i != NElts; ++i) |
| 50 | ShuffleMask.push_back(NElts+i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 51 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 52 | for (unsigned i = NElts/2; i != NElts; ++i) |
| 53 | ShuffleMask.push_back(i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 56 | // <0,2> or <0,1,4,5> |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 57 | void DecodeMOVLHPSMask(unsigned NElts, |
| 58 | SmallVectorImpl<unsigned> &ShuffleMask) { |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 59 | for (unsigned i = 0; i != NElts/2; ++i) |
| 60 | ShuffleMask.push_back(i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 61 | |
Bruno Cardoso Lopes | 814a69c | 2010-09-02 21:51:11 +0000 | [diff] [blame] | 62 | for (unsigned i = 0; i != NElts/2; ++i) |
| 63 | ShuffleMask.push_back(NElts+i); |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 66 | void DecodePSHUFMask(unsigned NElts, unsigned Imm, |
| 67 | SmallVectorImpl<unsigned> &ShuffleMask) { |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 68 | for (unsigned i = 0; i != NElts; ++i) { |
| 69 | ShuffleMask.push_back(Imm % NElts); |
| 70 | Imm /= NElts; |
| 71 | } |
| 72 | } |
| 73 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 74 | void DecodePSHUFHWMask(unsigned Imm, |
| 75 | SmallVectorImpl<unsigned> &ShuffleMask) { |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 76 | ShuffleMask.push_back(0); |
| 77 | ShuffleMask.push_back(1); |
| 78 | ShuffleMask.push_back(2); |
| 79 | ShuffleMask.push_back(3); |
| 80 | for (unsigned i = 0; i != 4; ++i) { |
| 81 | ShuffleMask.push_back(4+(Imm & 3)); |
| 82 | Imm >>= 2; |
| 83 | } |
| 84 | } |
| 85 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 86 | void DecodePSHUFLWMask(unsigned Imm, |
| 87 | SmallVectorImpl<unsigned> &ShuffleMask) { |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 88 | for (unsigned i = 0; i != 4; ++i) { |
| 89 | ShuffleMask.push_back((Imm & 3)); |
| 90 | Imm >>= 2; |
| 91 | } |
| 92 | ShuffleMask.push_back(4); |
| 93 | ShuffleMask.push_back(5); |
| 94 | ShuffleMask.push_back(6); |
| 95 | ShuffleMask.push_back(7); |
| 96 | } |
| 97 | |
Craig Topper | c16db84 | 2011-11-29 07:49:05 +0000 | [diff] [blame] | 98 | void DecodeSHUFPMask(EVT VT, unsigned Imm, |
| 99 | SmallVectorImpl<unsigned> &ShuffleMask) { |
| 100 | unsigned NumElts = VT.getVectorNumElements(); |
| 101 | |
| 102 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 103 | unsigned NumLaneElts = NumElts / NumLanes; |
| 104 | |
| 105 | int NewImm = Imm; |
| 106 | for (unsigned l = 0; l < NumLanes; ++l) { |
| 107 | unsigned LaneStart = l * NumLaneElts; |
| 108 | // Part that reads from dest. |
| 109 | for (unsigned i = 0; i != NumLaneElts/2; ++i) { |
| 110 | ShuffleMask.push_back(NewImm % NumLaneElts + LaneStart); |
| 111 | NewImm /= NumLaneElts; |
| 112 | } |
| 113 | // Part that reads from src. |
| 114 | for (unsigned i = 0; i != NumLaneElts/2; ++i) { |
| 115 | ShuffleMask.push_back(NewImm % NumLaneElts + NumElts + LaneStart); |
| 116 | NewImm /= NumLaneElts; |
| 117 | } |
| 118 | if (NumLaneElts == 4) NewImm = Imm; // reload imm |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Craig Topper | 3cb802c | 2011-12-06 05:31:16 +0000 | [diff] [blame^] | 122 | void DecodeUNPCKHMask(EVT VT, SmallVectorImpl<unsigned> &ShuffleMask) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 123 | unsigned NumElts = VT.getVectorNumElements(); |
| 124 | |
| 125 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 126 | // independently on 128-bit lanes. |
| 127 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 128 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 129 | unsigned NumLaneElts = NumElts / NumLanes; |
| 130 | |
| 131 | for (unsigned s = 0; s < NumLanes; ++s) { |
| 132 | unsigned Start = s * NumLaneElts + NumLaneElts/2; |
| 133 | unsigned End = s * NumLaneElts + NumLaneElts; |
| 134 | for (unsigned i = Start; i != End; ++i) { |
| 135 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 136 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
| 137 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Craig Topper | 3cb802c | 2011-12-06 05:31:16 +0000 | [diff] [blame^] | 141 | /// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 142 | /// etc. VT indicates the type of the vector allowing it to handle different |
| 143 | /// datatypes and vector widths. |
Craig Topper | 3cb802c | 2011-12-06 05:31:16 +0000 | [diff] [blame^] | 144 | void DecodeUNPCKLMask(EVT VT, SmallVectorImpl<unsigned> &ShuffleMask) { |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 145 | unsigned NumElts = VT.getVectorNumElements(); |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 146 | |
Bruno Cardoso Lopes | f8fe47b | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 147 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 148 | // independently on 128-bit lanes. |
| 149 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 150 | if (NumLanes == 0 ) NumLanes = 1; // Handle MMX |
| 151 | unsigned NumLaneElts = NumElts / NumLanes; |
David Greene | 20a1cbe | 2011-02-28 19:06:56 +0000 | [diff] [blame] | 152 | |
Bruno Cardoso Lopes | f8fe47b | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 153 | for (unsigned s = 0; s < NumLanes; ++s) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 154 | unsigned Start = s * NumLaneElts; |
| 155 | unsigned End = s * NumLaneElts + NumLaneElts/2; |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 156 | for (unsigned i = Start; i != End; ++i) { |
Craig Topper | ccb7097 | 2011-11-22 01:57:35 +0000 | [diff] [blame] | 157 | ShuffleMask.push_back(i); // Reads from dest/src1 |
| 158 | ShuffleMask.push_back(i+NumElts); // Reads from src/src2 |
David Greene | dd567b2 | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 159 | } |
Bruno Cardoso Lopes | c79f501 | 2010-09-02 18:40:13 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Craig Topper | bafd224 | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 163 | // DecodeVPERMILPMask - Decodes VPERMILPS/ VPERMILPD permutes for any 128-bit |
| 164 | // 32-bit or 64-bit elements. For 256-bit vectors, it's considered as two 128 |
| 165 | // lanes. For VPERMILPS, referenced elements can't cross lanes and the mask of |
| 166 | // the first lane must be the same of the second. |
| 167 | void DecodeVPERMILPMask(EVT VT, unsigned Imm, |
| 168 | SmallVectorImpl<unsigned> &ShuffleMask) { |
| 169 | unsigned NumElts = VT.getVectorNumElements(); |
| 170 | |
| 171 | unsigned NumLanes = VT.getSizeInBits() / 128; |
| 172 | unsigned NumLaneElts = NumElts / NumLanes; |
Bruno Cardoso Lopes | b878caa | 2011-07-21 01:55:47 +0000 | [diff] [blame] | 173 | |
| 174 | for (unsigned l = 0; l != NumLanes; ++l) { |
Craig Topper | bafd224 | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 175 | unsigned LaneStart = l*NumLaneElts; |
| 176 | for (unsigned i = 0; i != NumLaneElts; ++i) { |
| 177 | unsigned Idx = NumLaneElts == 4 ? (Imm >> (i*2)) & 0x3 |
| 178 | : (Imm >> (i+LaneStart)) & 0x1; |
| 179 | ShuffleMask.push_back(Idx+LaneStart); |
Bruno Cardoso Lopes | b878caa | 2011-07-21 01:55:47 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Bruno Cardoso Lopes | f15dfe5 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 184 | void DecodeVPERM2F128Mask(EVT VT, unsigned Imm, |
| 185 | SmallVectorImpl<unsigned> &ShuffleMask) { |
| 186 | unsigned HalfSize = VT.getVectorNumElements()/2; |
| 187 | unsigned FstHalfBegin = (Imm & 0x3) * HalfSize; |
| 188 | unsigned SndHalfBegin = ((Imm >> 4) & 0x3) * HalfSize; |
| 189 | |
| 190 | for (int i = FstHalfBegin, e = FstHalfBegin+HalfSize; i != e; ++i) |
| 191 | ShuffleMask.push_back(i); |
| 192 | for (int i = SndHalfBegin, e = SndHalfBegin+HalfSize; i != e; ++i) |
| 193 | ShuffleMask.push_back(i); |
| 194 | } |
| 195 | |
| 196 | void DecodeVPERM2F128Mask(unsigned Imm, |
| 197 | SmallVectorImpl<unsigned> &ShuffleMask) { |
| 198 | // VPERM2F128 is used by any 256-bit EVT, but X86InstComments only |
| 199 | // has information about the instruction and not the types. So for |
| 200 | // instruction comments purpose, assume the 256-bit vector is v4i64. |
| 201 | return DecodeVPERM2F128Mask(MVT::v4i64, Imm, ShuffleMask); |
| 202 | } |
| 203 | |
David Greene | 3a2b508 | 2011-02-17 19:18:59 +0000 | [diff] [blame] | 204 | } // llvm namespace |