blob: a802333002d80966e3c4ce3d96a06657bc85dfda [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 Toppercbc96a62012-03-20 06:42:26 +000067void DecodePSHUFMask(EVT 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
73 int NewImm = Imm;
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 Lopesc79f5012010-09-02 18:40:13 +000080 }
81}
82
Craig Toppercbc96a62012-03-20 06:42:26 +000083void DecodePSHUFHWMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000084 ShuffleMask.push_back(0);
85 ShuffleMask.push_back(1);
86 ShuffleMask.push_back(2);
87 ShuffleMask.push_back(3);
88 for (unsigned i = 0; i != 4; ++i) {
89 ShuffleMask.push_back(4+(Imm & 3));
90 Imm >>= 2;
91 }
92}
93
Craig Toppercbc96a62012-03-20 06:42:26 +000094void DecodePSHUFLWMask(unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000095 for (unsigned i = 0; i != 4; ++i) {
96 ShuffleMask.push_back((Imm & 3));
97 Imm >>= 2;
98 }
99 ShuffleMask.push_back(4);
100 ShuffleMask.push_back(5);
101 ShuffleMask.push_back(6);
102 ShuffleMask.push_back(7);
103}
104
Craig Topper1f710572012-02-06 07:17:51 +0000105/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
106/// the type of the vector allowing it to handle different datatypes and vector
107/// widths.
Craig Toppercbc96a62012-03-20 06:42:26 +0000108void DecodeSHUFPMask(EVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc16db842011-11-29 07:49:05 +0000109 unsigned NumElts = VT.getVectorNumElements();
110
111 unsigned NumLanes = VT.getSizeInBits() / 128;
112 unsigned NumLaneElts = NumElts / NumLanes;
113
114 int NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +0000115 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
Craig Topperc16db842011-11-29 07:49:05 +0000116 // Part that reads from dest.
117 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
Craig Topper1f710572012-02-06 07:17:51 +0000118 ShuffleMask.push_back(NewImm % NumLaneElts + l);
Craig Topperc16db842011-11-29 07:49:05 +0000119 NewImm /= NumLaneElts;
120 }
121 // Part that reads from src.
122 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
Craig Topper1f710572012-02-06 07:17:51 +0000123 ShuffleMask.push_back(NewImm % NumLaneElts + NumElts + l);
Craig Topperc16db842011-11-29 07:49:05 +0000124 NewImm /= NumLaneElts;
125 }
126 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000127 }
128}
129
Craig Topper1f710572012-02-06 07:17:51 +0000130/// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
131/// and punpckh*. VT indicates the type of the vector allowing it to handle
132/// different datatypes and vector widths.
Craig Toppercbc96a62012-03-20 06:42:26 +0000133void DecodeUNPCKHMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperccb70972011-11-22 01:57:35 +0000134 unsigned NumElts = VT.getVectorNumElements();
135
136 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
137 // independently on 128-bit lanes.
138 unsigned NumLanes = VT.getSizeInBits() / 128;
139 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
140 unsigned NumLaneElts = NumElts / NumLanes;
141
Craig Topper1f710572012-02-06 07:17:51 +0000142 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
143 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000144 ShuffleMask.push_back(i); // Reads from dest/src1
145 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
146 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000147 }
148}
149
Craig Topper3cb802c2011-12-06 05:31:16 +0000150/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
Craig Topper1f710572012-02-06 07:17:51 +0000151/// and punpckl*. VT indicates the type of the vector allowing it to handle
152/// different datatypes and vector widths.
Craig Toppercbc96a62012-03-20 06:42:26 +0000153void DecodeUNPCKLMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
David Greenedd567b22011-03-02 17:23:43 +0000154 unsigned NumElts = VT.getVectorNumElements();
David Greene20a1cbe2011-02-28 19:06:56 +0000155
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000156 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
157 // independently on 128-bit lanes.
158 unsigned NumLanes = VT.getSizeInBits() / 128;
159 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
160 unsigned NumLaneElts = NumElts / NumLanes;
David Greene20a1cbe2011-02-28 19:06:56 +0000161
Craig Topper1f710572012-02-06 07:17:51 +0000162 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
163 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000164 ShuffleMask.push_back(i); // Reads from dest/src1
165 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
David Greenedd567b22011-03-02 17:23:43 +0000166 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000167 }
168}
169
Craig Topper1f710572012-02-06 07:17:51 +0000170void DecodeVPERM2X128Mask(EVT VT, unsigned Imm,
Craig Toppercbc96a62012-03-20 06:42:26 +0000171 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper354103d2012-04-17 05:54:54 +0000172 if (Imm & 0x88)
173 return; // Not a shuffle
174
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000175 unsigned HalfSize = VT.getVectorNumElements()/2;
176 unsigned FstHalfBegin = (Imm & 0x3) * HalfSize;
177 unsigned SndHalfBegin = ((Imm >> 4) & 0x3) * HalfSize;
178
179 for (int i = FstHalfBegin, e = FstHalfBegin+HalfSize; i != e; ++i)
180 ShuffleMask.push_back(i);
181 for (int i = SndHalfBegin, e = SndHalfBegin+HalfSize; i != e; ++i)
182 ShuffleMask.push_back(i);
183}
184
David Greene3a2b5082011-02-17 19:18:59 +0000185} // llvm namespace