blob: 8b87c1f9c8ad2ad51a4160ffc8698157d8600090 [file] [log] [blame]
David Greene20a1cbe2011-02-28 19:06:56 +00001//===-- X86ShuffleDecode.cpp - X86 shuffle decode logic -------------------===//
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +00002//
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 Greene3a2b5082011-02-17 19:18:59 +000015#include "X86ShuffleDecode.h"
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000016
17//===----------------------------------------------------------------------===//
18// Vector Mask Decoding
19//===----------------------------------------------------------------------===//
20
David Greene3a2b5082011-02-17 19:18:59 +000021namespace llvm {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000022
Craig Toppercbc96a62012-03-20 06:42:26 +000023void DecodeINSERTPSMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopes02a05a62010-09-02 22:43:39 +000024 // 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 Lopes814a69c2010-09-02 21:51:11 +000046// <3,1> or <6,7,2,3>
Craig Toppercbc96a62012-03-20 06:42:26 +000047void DecodeMOVHLPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000048 for (unsigned i = NElts/2; i != NElts; ++i)
49 ShuffleMask.push_back(NElts+i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000050
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000051 for (unsigned i = NElts/2; i != NElts; ++i)
52 ShuffleMask.push_back(i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000053}
54
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000055// <0,2> or <0,1,4,5>
Craig Toppercbc96a62012-03-20 06:42:26 +000056void DecodeMOVLHPSMask(unsigned NElts, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000057 for (unsigned i = 0; i != NElts/2; ++i)
58 ShuffleMask.push_back(i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000059
Bruno Cardoso Lopes814a69c2010-09-02 21:51:11 +000060 for (unsigned i = 0; i != NElts/2; ++i)
61 ShuffleMask.push_back(NElts+i);
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000062}
63
Craig Topper1f710572012-02-06 07:17:51 +000064/// 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 Topper00a1e6d2012-05-06 19:46:21 +000067void DecodePSHUFMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topper1f710572012-02-06 07:17:51 +000068 unsigned NumElts = VT.getVectorNumElements();
69
70 unsigned NumLanes = VT.getSizeInBits() / 128;
71 unsigned NumLaneElts = NumElts / NumLanes;
72
Craig Topperc73bc392012-05-02 08:03:44 +000073 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +000074 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 Lopesc79f5012010-09-02 18:40:13 +000080 }
81}
82
Craig Topper00a1e6d2012-05-06 19:46:21 +000083void DecodePSHUFHWMask(MVT VT, unsigned Imm,
Craig Topperc73bc392012-05-02 08:03:44 +000084 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper315a5cc2012-05-03 07:12:59 +000085 unsigned NumElts = VT.getVectorNumElements();
Craig Topperc73bc392012-05-02 08:03:44 +000086
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 Lopesc79f5012010-09-02 18:40:13 +000096 }
97}
98
Craig Topper00a1e6d2012-05-06 19:46:21 +000099void DecodePSHUFLWMask(MVT VT, unsigned Imm,
Craig Topperc73bc392012-05-02 08:03:44 +0000100 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper315a5cc2012-05-03 07:12:59 +0000101 unsigned NumElts = VT.getVectorNumElements();
Craig Topperc73bc392012-05-02 08:03:44 +0000102
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 Lopesc79f5012010-09-02 18:40:13 +0000112 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000113}
114
Craig Topper1f710572012-02-06 07:17:51 +0000115/// 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 Topper00a1e6d2012-05-06 19:46:21 +0000118void DecodeSHUFPMask(MVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc16db842011-11-29 07:49:05 +0000119 unsigned NumElts = VT.getVectorNumElements();
120
121 unsigned NumLanes = VT.getSizeInBits() / 128;
122 unsigned NumLaneElts = NumElts / NumLanes;
123
Craig Topperc73bc392012-05-02 08:03:44 +0000124 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +0000125 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
Craig Topper00a1e6d2012-05-06 19:46:21 +0000126 // 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 Topperc16db842011-11-29 07:49:05 +0000132 }
133 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000134 }
135}
136
Craig Topper1f710572012-02-06 07:17:51 +0000137/// 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 Topper00a1e6d2012-05-06 19:46:21 +0000140void DecodeUNPCKHMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperccb70972011-11-22 01:57:35 +0000141 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 Topper1f710572012-02-06 07:17:51 +0000149 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
150 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000151 ShuffleMask.push_back(i); // Reads from dest/src1
152 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
153 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000154 }
155}
156
Craig Topper3cb802c2011-12-06 05:31:16 +0000157/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
Craig Topper1f710572012-02-06 07:17:51 +0000158/// and punpckl*. VT indicates the type of the vector allowing it to handle
159/// different datatypes and vector widths.
Craig Topper00a1e6d2012-05-06 19:46:21 +0000160void DecodeUNPCKLMask(MVT VT, SmallVectorImpl<int> &ShuffleMask) {
David Greenedd567b22011-03-02 17:23:43 +0000161 unsigned NumElts = VT.getVectorNumElements();
David Greene20a1cbe2011-02-28 19:06:56 +0000162
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000163 // 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 Greene20a1cbe2011-02-28 19:06:56 +0000168
Craig Topper1f710572012-02-06 07:17:51 +0000169 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
170 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000171 ShuffleMask.push_back(i); // Reads from dest/src1
172 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
David Greenedd567b22011-03-02 17:23:43 +0000173 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000174 }
175}
176
Craig Topper00a1e6d2012-05-06 19:46:21 +0000177void DecodeVPERM2X128Mask(MVT VT, unsigned Imm,
Craig Toppercbc96a62012-03-20 06:42:26 +0000178 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper354103d2012-04-17 05:54:54 +0000179 if (Imm & 0x88)
180 return; // Not a shuffle
181
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000182 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000183
Craig Topper00a1e6d2012-05-06 19:46:21 +0000184 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 Lopesf15dfe52011-08-12 21:48:26 +0000189}
190
Craig Topper54bdb352012-05-06 18:44:02 +0000191/// DecodeVPERMMask - this decodes the shuffle masks for VPERMQ/VPERMPD.
192/// No VT provided since it only works on 256-bit, 4 element vectors.
193void 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 Greene3a2b5082011-02-17 19:18:59 +0000199} // llvm namespace