blob: a1f242476ef1aa0721319c585375dc7233c712c6 [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
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 Topperc73bc392012-05-02 08:03:44 +000083void DecodePSHUFHWMask(EVT VT, unsigned Imm,
84 SmallVectorImpl<int> &ShuffleMask) {
85 unsigned NumLanes = VT.getSizeInBits() / 128;
86 unsigned NumElts = 8 * NumLanes;
87
88 for (unsigned l = 0; l != NumElts; l += 8) {
89 unsigned NewImm = Imm;
90 for (unsigned i = 0, e = 4; i != e; ++i) {
91 ShuffleMask.push_back(l + i);
92 }
93 for (unsigned i = 4, e = 8; i != e; ++i) {
94 ShuffleMask.push_back(l + 4 + (NewImm & 3));
95 NewImm >>= 2;
96 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +000097 }
98}
99
Craig Topperc73bc392012-05-02 08:03:44 +0000100void DecodePSHUFLWMask(EVT VT, unsigned Imm,
101 SmallVectorImpl<int> &ShuffleMask) {
102 unsigned NumLanes = VT.getSizeInBits() / 128;
103 unsigned NumElts = 8 * NumLanes;
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 + (NewImm & 3));
109 NewImm >>= 2;
110 }
111 for (unsigned i = 4, e = 8; i != e; ++i) {
112 ShuffleMask.push_back(l + i);
113 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000114 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000115}
116
Craig Topper1f710572012-02-06 07:17:51 +0000117/// DecodeSHUFPMask - This decodes the shuffle masks for shufp*. VT indicates
118/// the type of the vector allowing it to handle different datatypes and vector
119/// widths.
Craig Toppercbc96a62012-03-20 06:42:26 +0000120void DecodeSHUFPMask(EVT VT, unsigned Imm, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperc16db842011-11-29 07:49:05 +0000121 unsigned NumElts = VT.getVectorNumElements();
122
123 unsigned NumLanes = VT.getSizeInBits() / 128;
124 unsigned NumLaneElts = NumElts / NumLanes;
125
Craig Topperc73bc392012-05-02 08:03:44 +0000126 unsigned NewImm = Imm;
Craig Topper1f710572012-02-06 07:17:51 +0000127 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
Craig Topperc16db842011-11-29 07:49:05 +0000128 // Part that reads from dest.
129 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
Craig Topper1f710572012-02-06 07:17:51 +0000130 ShuffleMask.push_back(NewImm % NumLaneElts + l);
Craig Topperc16db842011-11-29 07:49:05 +0000131 NewImm /= NumLaneElts;
132 }
133 // Part that reads from src.
134 for (unsigned i = 0; i != NumLaneElts/2; ++i) {
Craig Topper1f710572012-02-06 07:17:51 +0000135 ShuffleMask.push_back(NewImm % NumLaneElts + NumElts + l);
Craig Topperc16db842011-11-29 07:49:05 +0000136 NewImm /= NumLaneElts;
137 }
138 if (NumLaneElts == 4) NewImm = Imm; // reload imm
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000139 }
140}
141
Craig Topper1f710572012-02-06 07:17:51 +0000142/// DecodeUNPCKHMask - This decodes the shuffle masks for unpckhps/unpckhpd
143/// and punpckh*. VT indicates the type of the vector allowing it to handle
144/// different datatypes and vector widths.
Craig Toppercbc96a62012-03-20 06:42:26 +0000145void DecodeUNPCKHMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
Craig Topperccb70972011-11-22 01:57:35 +0000146 unsigned NumElts = VT.getVectorNumElements();
147
148 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
149 // independently on 128-bit lanes.
150 unsigned NumLanes = VT.getSizeInBits() / 128;
151 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
152 unsigned NumLaneElts = NumElts / NumLanes;
153
Craig Topper1f710572012-02-06 07:17:51 +0000154 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
155 for (unsigned i = l + NumLaneElts/2, e = l + NumLaneElts; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000156 ShuffleMask.push_back(i); // Reads from dest/src1
157 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
158 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000159 }
160}
161
Craig Topper3cb802c2011-12-06 05:31:16 +0000162/// DecodeUNPCKLMask - This decodes the shuffle masks for unpcklps/unpcklpd
Craig Topper1f710572012-02-06 07:17:51 +0000163/// and punpckl*. VT indicates the type of the vector allowing it to handle
164/// different datatypes and vector widths.
Craig Toppercbc96a62012-03-20 06:42:26 +0000165void DecodeUNPCKLMask(EVT VT, SmallVectorImpl<int> &ShuffleMask) {
David Greenedd567b22011-03-02 17:23:43 +0000166 unsigned NumElts = VT.getVectorNumElements();
David Greene20a1cbe2011-02-28 19:06:56 +0000167
Bruno Cardoso Lopesf8fe47b2011-07-26 22:03:40 +0000168 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
169 // independently on 128-bit lanes.
170 unsigned NumLanes = VT.getSizeInBits() / 128;
171 if (NumLanes == 0 ) NumLanes = 1; // Handle MMX
172 unsigned NumLaneElts = NumElts / NumLanes;
David Greene20a1cbe2011-02-28 19:06:56 +0000173
Craig Topper1f710572012-02-06 07:17:51 +0000174 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
175 for (unsigned i = l, e = l + NumLaneElts/2; i != e; ++i) {
Craig Topperccb70972011-11-22 01:57:35 +0000176 ShuffleMask.push_back(i); // Reads from dest/src1
177 ShuffleMask.push_back(i+NumElts); // Reads from src/src2
David Greenedd567b22011-03-02 17:23:43 +0000178 }
Bruno Cardoso Lopesc79f5012010-09-02 18:40:13 +0000179 }
180}
181
Craig Topper1f710572012-02-06 07:17:51 +0000182void DecodeVPERM2X128Mask(EVT VT, unsigned Imm,
Craig Toppercbc96a62012-03-20 06:42:26 +0000183 SmallVectorImpl<int> &ShuffleMask) {
Craig Topper354103d2012-04-17 05:54:54 +0000184 if (Imm & 0x88)
185 return; // Not a shuffle
186
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000187 unsigned HalfSize = VT.getVectorNumElements()/2;
188 unsigned FstHalfBegin = (Imm & 0x3) * HalfSize;
189 unsigned SndHalfBegin = ((Imm >> 4) & 0x3) * HalfSize;
190
Craig Topperc73bc392012-05-02 08:03:44 +0000191 for (unsigned i = FstHalfBegin, e = FstHalfBegin+HalfSize; i != e; ++i)
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000192 ShuffleMask.push_back(i);
Craig Topperc73bc392012-05-02 08:03:44 +0000193 for (unsigned i = SndHalfBegin, e = SndHalfBegin+HalfSize; i != e; ++i)
Bruno Cardoso Lopesf15dfe52011-08-12 21:48:26 +0000194 ShuffleMask.push_back(i);
195}
196
David Greene3a2b5082011-02-17 19:18:59 +0000197} // llvm namespace