blob: a95eeffd94dd94a733d924a2b50a0531adaf33e8 [file] [log] [blame]
Eugene Zelenko60433b62017-10-05 00:33:50 +00001//===- X86InterleavedAccess.cpp -------------------------------------------===//
David L Kreitzer01a057a2016-10-14 18:20:41 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David L Kreitzer01a057a2016-10-14 18:20:41 +00006//
Eugene Zelenko60433b62017-10-05 00:33:50 +00007//===----------------------------------------------------------------------===//
8//
David L Kreitzer0e3ae302016-12-01 19:56:39 +00009/// \file
10/// This file contains the X86 implementation of the interleaved accesses
11/// optimization generating X86-specific instructions/intrinsics for
12/// interleaved access groups.
Eugene Zelenko60433b62017-10-05 00:33:50 +000013//
14//===----------------------------------------------------------------------===//
David L Kreitzer01a057a2016-10-14 18:20:41 +000015
Eugene Zelenko60433b62017-10-05 00:33:50 +000016#include "X86ISelLowering.h"
17#include "X86Subtarget.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/SmallVector.h"
Farhana Aleen4b652a52017-06-22 22:59:04 +000020#include "llvm/Analysis/VectorUtils.h"
Eugene Zelenko60433b62017-10-05 00:33:50 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Instruction.h"
26#include "llvm/IR/Instructions.h"
27#include "llvm/IR/Module.h"
28#include "llvm/IR/Type.h"
29#include "llvm/IR/Value.h"
30#include "llvm/Support/Casting.h"
David Blaikie13e77db2018-03-23 23:58:25 +000031#include "llvm/Support/MachineValueType.h"
Eugene Zelenko60433b62017-10-05 00:33:50 +000032#include <algorithm>
33#include <cassert>
34#include <cmath>
35#include <cstdint>
David L Kreitzer01a057a2016-10-14 18:20:41 +000036
37using namespace llvm;
38
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000039namespace {
Eugene Zelenko60433b62017-10-05 00:33:50 +000040
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000041/// This class holds necessary information to represent an interleaved
David L Kreitzer0e3ae302016-12-01 19:56:39 +000042/// access group and supports utilities to lower the group into
43/// X86-specific instructions/intrinsics.
44/// E.g. A group of interleaving access loads (Factor = 2; accessing every
45/// other element)
46/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr
47/// %v0 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <0, 2, 4, 6>
48/// %v1 = shuffle <8 x i32> %wide.vec, <8 x i32> undef, <1, 3, 5, 7>
David L Kreitzer0e3ae302016-12-01 19:56:39 +000049class X86InterleavedAccessGroup {
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000050 /// Reference to the wide-load instruction of an interleaved access
David L Kreitzer0e3ae302016-12-01 19:56:39 +000051 /// group.
52 Instruction *const Inst;
53
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000054 /// Reference to the shuffle(s), consumer(s) of the (load) 'Inst'.
David L Kreitzer0e3ae302016-12-01 19:56:39 +000055 ArrayRef<ShuffleVectorInst *> Shuffles;
56
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000057 /// Reference to the starting index of each user-shuffle.
David L Kreitzer0e3ae302016-12-01 19:56:39 +000058 ArrayRef<unsigned> Indices;
59
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000060 /// Reference to the interleaving stride in terms of elements.
David L Kreitzer0e3ae302016-12-01 19:56:39 +000061 const unsigned Factor;
62
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000063 /// Reference to the underlying target.
David L Kreitzer0e3ae302016-12-01 19:56:39 +000064 const X86Subtarget &Subtarget;
65
66 const DataLayout &DL;
67
68 IRBuilder<> &Builder;
69
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000070 /// Breaks down a vector \p 'Inst' of N elements into \p NumSubVectors
Farhana Aleen4b652a52017-06-22 22:59:04 +000071 /// sub vectors of type \p T. Returns the sub-vectors in \p DecomposedVectors.
72 void decompose(Instruction *Inst, unsigned NumSubVectors, VectorType *T,
David L Kreitzer0e3ae302016-12-01 19:56:39 +000073 SmallVectorImpl<Instruction *> &DecomposedVectors);
74
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000075 /// Performs matrix transposition on a 4x4 matrix \p InputVectors and
David L Kreitzer0e3ae302016-12-01 19:56:39 +000076 /// returns the transposed-vectors in \p TransposedVectors.
77 /// E.g.
78 /// InputVectors:
79 /// In-V0 = p1, p2, p3, p4
80 /// In-V1 = q1, q2, q3, q4
81 /// In-V2 = r1, r2, r3, r4
82 /// In-V3 = s1, s2, s3, s4
83 /// OutputVectors:
84 /// Out-V0 = p1, q1, r1, s1
85 /// Out-V1 = p2, q2, r2, s2
86 /// Out-V2 = p3, q3, r3, s3
87 /// Out-V3 = P4, q4, r4, s4
88 void transpose_4x4(ArrayRef<Instruction *> InputVectors,
Michael Zuckermanc1918ad2017-07-26 08:10:14 +000089 SmallVectorImpl<Value *> &TransposedMatrix);
Michael Zuckerman680ac102017-08-07 13:22:39 +000090 void interleave8bitStride4(ArrayRef<Instruction *> InputVectors,
91 SmallVectorImpl<Value *> &TransposedMatrix,
92 unsigned NumSubVecElems);
Michael Zuckerman4a97df02017-09-25 14:50:38 +000093 void interleave8bitStride4VF8(ArrayRef<Instruction *> InputVectors,
94 SmallVectorImpl<Value *> &TransposedMatrix);
Michael Zuckerman645f7772017-09-26 18:49:11 +000095 void interleave8bitStride3(ArrayRef<Instruction *> InputVectors,
96 SmallVectorImpl<Value *> &TransposedMatrix,
97 unsigned NumSubVecElems);
Michael Zuckerman5a385942017-09-07 14:02:13 +000098 void deinterleave8bitStride3(ArrayRef<Instruction *> InputVectors,
99 SmallVectorImpl<Value *> &TransposedMatrix,
100 unsigned NumSubVecElems);
Michael Zuckerman680ac102017-08-07 13:22:39 +0000101
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000102public:
103 /// In order to form an interleaved access group X86InterleavedAccessGroup
104 /// requires a wide-load instruction \p 'I', a group of interleaved-vectors
105 /// \p Shuffs, reference to the first indices of each interleaved-vector
106 /// \p 'Ind' and the interleaving stride factor \p F. In order to generate
107 /// X86-specific instructions/intrinsics it also requires the underlying
108 /// target information \p STarget.
109 explicit X86InterleavedAccessGroup(Instruction *I,
110 ArrayRef<ShuffleVectorInst *> Shuffs,
Farhana Aleen4b652a52017-06-22 22:59:04 +0000111 ArrayRef<unsigned> Ind, const unsigned F,
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000112 const X86Subtarget &STarget,
113 IRBuilder<> &B)
114 : Inst(I), Shuffles(Shuffs), Indices(Ind), Factor(F), Subtarget(STarget),
115 DL(Inst->getModule()->getDataLayout()), Builder(B) {}
116
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000117 /// Returns true if this interleaved access group can be lowered into
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000118 /// x86-specific instructions/intrinsics, false otherwise.
119 bool isSupported() const;
120
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000121 /// Lowers this interleaved access group into X86-specific
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000122 /// instructions/intrinsics.
123 bool lowerIntoOptimizedSequence();
124};
Eugene Zelenko60433b62017-10-05 00:33:50 +0000125
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000126} // end anonymous namespace
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000127
128bool X86InterleavedAccessGroup::isSupported() const {
David L Kreitzer01a057a2016-10-14 18:20:41 +0000129 VectorType *ShuffleVecTy = Shuffles[0]->getType();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000130 Type *ShuffleEltTy = ShuffleVecTy->getVectorElementType();
Farhana Aleene4a89a62017-07-21 21:35:00 +0000131 unsigned ShuffleElemSize = DL.getTypeSizeInBits(ShuffleEltTy);
Farhana Aleene4a89a62017-07-21 21:35:00 +0000132 unsigned WideInstSize;
David L Kreitzer01a057a2016-10-14 18:20:41 +0000133
Michael Zuckerman5a385942017-09-07 14:02:13 +0000134 // Currently, lowering is supported for the following vectors:
135 // Stride 4:
136 // 1. Store and load of 4-element vectors of 64 bits on AVX.
137 // 2. Store of 16/32-element vectors of 8 bits on AVX.
138 // Stride 3:
Craig Topper21531142017-11-14 16:14:00 +0000139 // 1. Load of 16/32-element vectors of 8 bits on AVX.
Michael Zuckerman5a385942017-09-07 14:02:13 +0000140 if (!Subtarget.hasAVX() || (Factor != 4 && Factor != 3))
Michael Zuckerman680ac102017-08-07 13:22:39 +0000141 return false;
142
Farhana Aleene4a89a62017-07-21 21:35:00 +0000143 if (isa<LoadInst>(Inst)) {
Farhana Aleene4a89a62017-07-21 21:35:00 +0000144 WideInstSize = DL.getTypeSizeInBits(Inst->getType());
Michael Zuckerman72a6f892017-10-18 08:04:31 +0000145 if (cast<LoadInst>(Inst)->getPointerAddressSpace())
146 return false;
Farhana Aleene4a89a62017-07-21 21:35:00 +0000147 } else
148 WideInstSize = DL.getTypeSizeInBits(Shuffles[0]->getType());
149
Michael Zuckerman680ac102017-08-07 13:22:39 +0000150 // We support shuffle represents stride 4 for byte type with size of
151 // WideInstSize.
Michael Zuckerman5a385942017-09-07 14:02:13 +0000152 if (ShuffleElemSize == 64 && WideInstSize == 1024 && Factor == 4)
153 return true;
154
155 if (ShuffleElemSize == 8 && isa<StoreInst>(Inst) && Factor == 4 &&
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000156 (WideInstSize == 256 || WideInstSize == 512 || WideInstSize == 1024 ||
157 WideInstSize == 2048))
158 return true;
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000159
Michael Zuckerman645f7772017-09-26 18:49:11 +0000160 if (ShuffleElemSize == 8 && Factor == 3 &&
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000161 (WideInstSize == 384 || WideInstSize == 768 || WideInstSize == 1536))
Michael Zuckerman645f7772017-09-26 18:49:11 +0000162 return true;
David L Kreitzer01a057a2016-10-14 18:20:41 +0000163
Michael Zuckerman5a385942017-09-07 14:02:13 +0000164 return false;
David L Kreitzer01a057a2016-10-14 18:20:41 +0000165}
166
Farhana Aleen4b652a52017-06-22 22:59:04 +0000167void X86InterleavedAccessGroup::decompose(
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000168 Instruction *VecInst, unsigned NumSubVectors, VectorType *SubVecTy,
169 SmallVectorImpl<Instruction *> &DecomposedVectors) {
Farhana Aleen4b652a52017-06-22 22:59:04 +0000170 assert((isa<LoadInst>(VecInst) || isa<ShuffleVectorInst>(VecInst)) &&
171 "Expected Load or Shuffle");
172
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000173 Type *VecWidth = VecInst->getType();
174 (void)VecWidth;
175 assert(VecWidth->isVectorTy() &&
176 DL.getTypeSizeInBits(VecWidth) >=
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000177 DL.getTypeSizeInBits(SubVecTy) * NumSubVectors &&
178 "Invalid Inst-size!!!");
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000179
Farhana Aleen4b652a52017-06-22 22:59:04 +0000180 if (auto *SVI = dyn_cast<ShuffleVectorInst>(VecInst)) {
181 Value *Op0 = SVI->getOperand(0);
182 Value *Op1 = SVI->getOperand(1);
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000183
Farhana Aleen4b652a52017-06-22 22:59:04 +0000184 // Generate N(= NumSubVectors) shuffles of T(= SubVecTy) type.
185 for (unsigned i = 0; i < NumSubVectors; ++i)
186 DecomposedVectors.push_back(
187 cast<ShuffleVectorInst>(Builder.CreateShuffleVector(
Farhana Aleene4a89a62017-07-21 21:35:00 +0000188 Op0, Op1,
189 createSequentialMask(Builder, Indices[i],
190 SubVecTy->getVectorNumElements(), 0))));
Farhana Aleen4b652a52017-06-22 22:59:04 +0000191 return;
192 }
193
194 // Decompose the load instruction.
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000195 LoadInst *LI = cast<LoadInst>(VecInst);
James Y Knight14359ef2019-02-01 20:44:24 +0000196 Type *VecBaseTy, *VecBasePtrTy;
Michael Zuckerman5a385942017-09-07 14:02:13 +0000197 Value *VecBasePtr;
198 unsigned int NumLoads = NumSubVectors;
199 // In the case of stride 3 with a vector of 32 elements load the information
200 // in the following way:
201 // [0,1...,VF/2-1,VF/2+VF,VF/2+VF+1,...,2VF-1]
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000202 unsigned VecLength = DL.getTypeSizeInBits(VecWidth);
203 if (VecLength == 768 || VecLength == 1536) {
James Y Knight14359ef2019-02-01 20:44:24 +0000204 VecBaseTy = VectorType::get(Type::getInt8Ty(LI->getContext()), 16);
205 VecBasePtrTy = VecBaseTy->getPointerTo(LI->getPointerAddressSpace());
Michael Zuckerman5a385942017-09-07 14:02:13 +0000206 VecBasePtr = Builder.CreateBitCast(LI->getPointerOperand(), VecBasePtrTy);
James Y Knight14359ef2019-02-01 20:44:24 +0000207 NumLoads = NumSubVectors * (VecLength / 384);
208 } else {
209 VecBaseTy = SubVecTy;
210 VecBasePtrTy = VecBaseTy->getPointerTo(LI->getPointerAddressSpace());
211 VecBasePtr = Builder.CreateBitCast(LI->getPointerOperand(), VecBasePtrTy);
212 }
Farhana Aleen4b652a52017-06-22 22:59:04 +0000213 // Generate N loads of T type.
Michael Zuckerman5a385942017-09-07 14:02:13 +0000214 for (unsigned i = 0; i < NumLoads; i++) {
Farhana Aleen4b652a52017-06-22 22:59:04 +0000215 // TODO: Support inbounds GEP.
James Y Knight77160752019-02-01 20:44:47 +0000216 Value *NewBasePtr =
217 Builder.CreateGEP(VecBaseTy, VecBasePtr, Builder.getInt32(i));
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000218 Instruction *NewLoad =
James Y Knight14359ef2019-02-01 20:44:24 +0000219 Builder.CreateAlignedLoad(VecBaseTy, NewBasePtr, LI->getAlignment());
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000220 DecomposedVectors.push_back(NewLoad);
221 }
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000222}
223
Michael Zuckerman80d3649f2017-09-13 18:28:09 +0000224// Changing the scale of the vector type by reducing the number of elements and
225// doubling the scalar size.
226static MVT scaleVectorType(MVT VT) {
227 unsigned ScalarSize = VT.getVectorElementType().getScalarSizeInBits() * 2;
228 return MVT::getVectorVT(MVT::getIntegerVT(ScalarSize),
229 VT.getVectorNumElements() / 2);
230}
231
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000232static uint32_t Concat[] = {
233 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
234 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
235 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
236 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 };
237
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000238// genShuffleBland - Creates shuffle according to two vectors.This function is
239// only works on instructions with lane inside 256 registers. According to
240// the mask 'Mask' creates a new Mask 'Out' by the offset of the mask. The
241// offset amount depends on the two integer, 'LowOffset' and 'HighOffset'.
242// Where the 'LowOffset' refers to the first vector and the highOffset refers to
243// the second vector.
244// |a0....a5,b0....b4,c0....c4|a16..a21,b16..b20,c16..c20|
245// |c5...c10,a5....a9,b5....b9|c21..c26,a22..a26,b21..b25|
246// |b10..b15,c11..c15,a10..a15|b26..b31,c27..c31,a27..a31|
247// For the sequence to work as a mirror to the load.
248// We must consider the elements order as above.
249// In this function we are combining two types of shuffles.
250// The first one is vpshufed and the second is a type of "blend" shuffle.
251// By computing the shuffle on a sequence of 16 elements(one lane) and add the
252// correct offset. We are creating a vpsuffed + blend sequence between two
253// shuffles.
254static void genShuffleBland(MVT VT, ArrayRef<uint32_t> Mask,
255 SmallVectorImpl<uint32_t> &Out, int LowOffset,
256 int HighOffset) {
257 assert(VT.getSizeInBits() >= 256 &&
258 "This function doesn't accept width smaller then 256");
259 unsigned NumOfElm = VT.getVectorNumElements();
260 for (unsigned i = 0; i < Mask.size(); i++)
261 Out.push_back(Mask[i] + LowOffset);
262 for (unsigned i = 0; i < Mask.size(); i++)
263 Out.push_back(Mask[i] + HighOffset + NumOfElm);
264}
265
Craig Topper21531142017-11-14 16:14:00 +0000266// reorderSubVector returns the data to is the original state. And de-facto is
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000267// the opposite of the function concatSubVector.
268
269// For VecElems = 16
270// Invec[0] - |0| TransposedMatrix[0] - |0|
271// Invec[1] - |1| => TransposedMatrix[1] - |1|
272// Invec[2] - |2| TransposedMatrix[2] - |2|
273
274// For VecElems = 32
275// Invec[0] - |0|3| TransposedMatrix[0] - |0|1|
276// Invec[1] - |1|4| => TransposedMatrix[1] - |2|3|
277// Invec[2] - |2|5| TransposedMatrix[2] - |4|5|
278
279// For VecElems = 64
280// Invec[0] - |0|3|6|9 | TransposedMatrix[0] - |0|1|2 |3 |
281// Invec[1] - |1|4|7|10| => TransposedMatrix[1] - |4|5|6 |7 |
282// Invec[2] - |2|5|8|11| TransposedMatrix[2] - |8|9|10|11|
283
284static void reorderSubVector(MVT VT, SmallVectorImpl<Value *> &TransposedMatrix,
285 ArrayRef<Value *> Vec, ArrayRef<uint32_t> VPShuf,
286 unsigned VecElems, unsigned Stride,
287 IRBuilder<> Builder) {
288
289 if (VecElems == 16) {
290 for (unsigned i = 0; i < Stride; i++)
291 TransposedMatrix[i] = Builder.CreateShuffleVector(
292 Vec[i], UndefValue::get(Vec[i]->getType()), VPShuf);
293 return;
294 }
295
296 SmallVector<uint32_t, 32> OptimizeShuf;
297 Value *Temp[8];
298
299 for (unsigned i = 0; i < (VecElems / 16) * Stride; i += 2) {
300 genShuffleBland(VT, VPShuf, OptimizeShuf, (i / Stride) * 16,
301 (i + 1) / Stride * 16);
302 Temp[i / 2] = Builder.CreateShuffleVector(
303 Vec[i % Stride], Vec[(i + 1) % Stride], OptimizeShuf);
304 OptimizeShuf.clear();
305 }
306
307 if (VecElems == 32) {
308 std::copy(Temp, Temp + Stride, TransposedMatrix.begin());
309 return;
310 }
311 else
312 for (unsigned i = 0; i < Stride; i++)
313 TransposedMatrix[i] =
314 Builder.CreateShuffleVector(Temp[2 * i], Temp[2 * i + 1], Concat);
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000315}
316
Michael Zuckerman4a97df02017-09-25 14:50:38 +0000317void X86InterleavedAccessGroup::interleave8bitStride4VF8(
318 ArrayRef<Instruction *> Matrix,
319 SmallVectorImpl<Value *> &TransposedMatrix) {
320 // Assuming we start from the following vectors:
321 // Matrix[0]= c0 c1 c2 c3 c4 ... c7
322 // Matrix[1]= m0 m1 m2 m3 m4 ... m7
323 // Matrix[2]= y0 y1 y2 y3 y4 ... y7
324 // Matrix[3]= k0 k1 k2 k3 k4 ... k7
325
326 MVT VT = MVT::v8i16;
327 TransposedMatrix.resize(2);
328 SmallVector<uint32_t, 16> MaskLow;
329 SmallVector<uint32_t, 32> MaskLowTemp1, MaskLowWord;
330 SmallVector<uint32_t, 32> MaskHighTemp1, MaskHighWord;
331
332 for (unsigned i = 0; i < 8; ++i) {
333 MaskLow.push_back(i);
334 MaskLow.push_back(i + 8);
335 }
336
337 createUnpackShuffleMask<uint32_t>(VT, MaskLowTemp1, true, false);
338 createUnpackShuffleMask<uint32_t>(VT, MaskHighTemp1, false, false);
339 scaleShuffleMask<uint32_t>(2, MaskHighTemp1, MaskHighWord);
340 scaleShuffleMask<uint32_t>(2, MaskLowTemp1, MaskLowWord);
341 // IntrVec1Low = c0 m0 c1 m1 c2 m2 c3 m3 c4 m4 c5 m5 c6 m6 c7 m7
342 // IntrVec2Low = y0 k0 y1 k1 y2 k2 y3 k3 y4 k4 y5 k5 y6 k6 y7 k7
343 Value *IntrVec1Low =
344 Builder.CreateShuffleVector(Matrix[0], Matrix[1], MaskLow);
345 Value *IntrVec2Low =
346 Builder.CreateShuffleVector(Matrix[2], Matrix[3], MaskLow);
347
348 // TransposedMatrix[0] = c0 m0 y0 k0 c1 m1 y1 k1 c2 m2 y2 k2 c3 m3 y3 k3
349 // TransposedMatrix[1] = c4 m4 y4 k4 c5 m5 y5 k5 c6 m6 y6 k6 c7 m7 y7 k7
350
351 TransposedMatrix[0] =
352 Builder.CreateShuffleVector(IntrVec1Low, IntrVec2Low, MaskLowWord);
353 TransposedMatrix[1] =
354 Builder.CreateShuffleVector(IntrVec1Low, IntrVec2Low, MaskHighWord);
355}
356
Michael Zuckerman680ac102017-08-07 13:22:39 +0000357void X86InterleavedAccessGroup::interleave8bitStride4(
358 ArrayRef<Instruction *> Matrix, SmallVectorImpl<Value *> &TransposedMatrix,
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000359 unsigned NumOfElm) {
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000360 // Example: Assuming we start from the following vectors:
361 // Matrix[0]= c0 c1 c2 c3 c4 ... c31
362 // Matrix[1]= m0 m1 m2 m3 m4 ... m31
363 // Matrix[2]= y0 y1 y2 y3 y4 ... y31
364 // Matrix[3]= k0 k1 k2 k3 k4 ... k31
365
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000366 MVT VT = MVT::getVectorVT(MVT::i8, NumOfElm);
Michael Zuckerman80d3649f2017-09-13 18:28:09 +0000367 MVT HalfVT = scaleVectorType(VT);
Michael Zuckerman680ac102017-08-07 13:22:39 +0000368
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000369 TransposedMatrix.resize(4);
Michael Zuckerman80d3649f2017-09-13 18:28:09 +0000370 SmallVector<uint32_t, 32> MaskHigh;
371 SmallVector<uint32_t, 32> MaskLow;
Michael Zuckermanb92b6d42017-09-30 14:55:03 +0000372 SmallVector<uint32_t, 32> LowHighMask[2];
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000373 SmallVector<uint32_t, 32> MaskHighTemp;
374 SmallVector<uint32_t, 32> MaskLowTemp;
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000375
376 // MaskHighTemp and MaskLowTemp built in the vpunpckhbw and vpunpcklbw X86
377 // shuffle pattern.
378
Michael Zuckerman80d3649f2017-09-13 18:28:09 +0000379 createUnpackShuffleMask<uint32_t>(VT, MaskLow, true, false);
Michael Zuckermanb92b6d42017-09-30 14:55:03 +0000380 createUnpackShuffleMask<uint32_t>(VT, MaskHigh, false, false);
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000381
382 // MaskHighTemp1 and MaskLowTemp1 built in the vpunpckhdw and vpunpckldw X86
383 // shuffle pattern.
384
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000385 createUnpackShuffleMask<uint32_t>(HalfVT, MaskLowTemp, true, false);
386 createUnpackShuffleMask<uint32_t>(HalfVT, MaskHighTemp, false, false);
387 scaleShuffleMask<uint32_t>(2, MaskLowTemp, LowHighMask[0]);
388 scaleShuffleMask<uint32_t>(2, MaskHighTemp, LowHighMask[1]);
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000389
390 // IntrVec1Low = c0 m0 c1 m1 ... c7 m7 | c16 m16 c17 m17 ... c23 m23
391 // IntrVec1High = c8 m8 c9 m9 ... c15 m15 | c24 m24 c25 m25 ... c31 m31
392 // IntrVec2Low = y0 k0 y1 k1 ... y7 k7 | y16 k16 y17 k17 ... y23 k23
393 // IntrVec2High = y8 k8 y9 k9 ... y15 k15 | y24 k24 y25 k25 ... y31 k31
Michael Zuckermanb92b6d42017-09-30 14:55:03 +0000394 Value *IntrVec[4];
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000395
Michael Zuckermanb92b6d42017-09-30 14:55:03 +0000396 IntrVec[0] = Builder.CreateShuffleVector(Matrix[0], Matrix[1], MaskLow);
397 IntrVec[1] = Builder.CreateShuffleVector(Matrix[0], Matrix[1], MaskHigh);
398 IntrVec[2] = Builder.CreateShuffleVector(Matrix[2], Matrix[3], MaskLow);
399 IntrVec[3] = Builder.CreateShuffleVector(Matrix[2], Matrix[3], MaskHigh);
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000400
401 // cmyk4 cmyk5 cmyk6 cmyk7 | cmyk20 cmyk21 cmyk22 cmyk23
402 // cmyk12 cmyk13 cmyk14 cmyk15 | cmyk28 cmyk29 cmyk30 cmyk31
403 // cmyk0 cmyk1 cmyk2 cmyk3 | cmyk16 cmyk17 cmyk18 cmyk19
404 // cmyk8 cmyk9 cmyk10 cmyk11 | cmyk24 cmyk25 cmyk26 cmyk27
405
Michael Zuckermanb92b6d42017-09-30 14:55:03 +0000406 Value *VecOut[4];
407 for (int i = 0; i < 4; i++)
408 VecOut[i] = Builder.CreateShuffleVector(IntrVec[i / 2], IntrVec[i / 2 + 2],
409 LowHighMask[i % 2]);
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000410
Michael Zuckerman80d3649f2017-09-13 18:28:09 +0000411 // cmyk0 cmyk1 cmyk2 cmyk3 | cmyk4 cmyk5 cmyk6 cmyk7
412 // cmyk8 cmyk9 cmyk10 cmyk11 | cmyk12 cmyk13 cmyk14 cmyk15
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000413 // cmyk16 cmyk17 cmyk18 cmyk19 | cmyk20 cmyk21 cmyk22 cmyk23
414 // cmyk24 cmyk25 cmyk26 cmyk27 | cmyk28 cmyk29 cmyk30 cmyk31
415
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000416 if (VT == MVT::v16i8) {
417 std::copy(VecOut, VecOut + 4, TransposedMatrix.begin());
418 return;
419 }
Michael Zuckerman80d3649f2017-09-13 18:28:09 +0000420
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000421 reorderSubVector(VT, TransposedMatrix, VecOut, makeArrayRef(Concat, 16),
422 NumOfElm, 4, Builder);
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000423}
424
Michael Zuckerman5a385942017-09-07 14:02:13 +0000425// createShuffleStride returns shuffle mask of size N.
426// The shuffle pattern is as following :
427// {0, Stride%(VF/Lane), (2*Stride%(VF/Lane))...(VF*Stride/Lane)%(VF/Lane),
428// (VF/ Lane) ,(VF / Lane)+Stride%(VF/Lane),...,
429// (VF / Lane)+(VF*Stride/Lane)%(VF/Lane)}
430// Where Lane is the # of lanes in a register:
431// VectorSize = 128 => Lane = 1
432// VectorSize = 256 => Lane = 2
433// For example shuffle pattern for VF 16 register size 256 -> lanes = 2
434// {<[0|3|6|1|4|7|2|5]-[8|11|14|9|12|15|10|13]>}
435static void createShuffleStride(MVT VT, int Stride,
436 SmallVectorImpl<uint32_t> &Mask) {
437 int VectorSize = VT.getSizeInBits();
438 int VF = VT.getVectorNumElements();
439 int LaneCount = std::max(VectorSize / 128, 1);
440 for (int Lane = 0; Lane < LaneCount; Lane++)
441 for (int i = 0, LaneSize = VF / LaneCount; i != LaneSize; ++i)
442 Mask.push_back((i * Stride) % LaneSize + LaneSize * Lane);
443}
444
445// setGroupSize sets 'SizeInfo' to the size(number of elements) of group
446// inside mask a shuffleMask. A mask contains exactly 3 groups, where
447// each group is a monotonically increasing sequence with stride 3.
448// For example shuffleMask {0,3,6,1,4,7,2,5} => {3,3,2}
449static void setGroupSize(MVT VT, SmallVectorImpl<uint32_t> &SizeInfo) {
450 int VectorSize = VT.getSizeInBits();
451 int VF = VT.getVectorNumElements() / std::max(VectorSize / 128, 1);
452 for (int i = 0, FirstGroupElement = 0; i < 3; i++) {
453 int GroupSize = std::ceil((VF - FirstGroupElement) / 3.0);
454 SizeInfo.push_back(GroupSize);
455 FirstGroupElement = ((GroupSize)*3 + FirstGroupElement) % VF;
456 }
457}
458
459// DecodePALIGNRMask returns the shuffle mask of vpalign instruction.
460// vpalign works according to lanes
461// Where Lane is the # of lanes in a register:
462// VectorWide = 128 => Lane = 1
463// VectorWide = 256 => Lane = 2
464// For Lane = 1 shuffle pattern is: {DiffToJump,...,DiffToJump+VF-1}.
465// For Lane = 2 shuffle pattern is:
466// {DiffToJump,...,VF/2-1,VF,...,DiffToJump+VF-1}.
467// Imm variable sets the offset amount. The result of the
468// function is stored inside ShuffleMask vector and it built as described in
Craig Toppereaa1cf52018-10-25 05:00:20 +0000469// the begin of the description. AlignDirection is a boolean that indicates the
Michael Zuckerman5a385942017-09-07 14:02:13 +0000470// direction of the alignment. (false - align to the "right" side while true -
471// align to the "left" side)
472static void DecodePALIGNRMask(MVT VT, unsigned Imm,
473 SmallVectorImpl<uint32_t> &ShuffleMask,
474 bool AlignDirection = true, bool Unary = false) {
Michael Zuckerman5a385942017-09-07 14:02:13 +0000475 unsigned NumElts = VT.getVectorNumElements();
476 unsigned NumLanes = std::max((int)VT.getSizeInBits() / 128, 1);
477 unsigned NumLaneElts = NumElts / NumLanes;
478
479 Imm = AlignDirection ? Imm : (NumLaneElts - Imm);
480 unsigned Offset = Imm * (VT.getScalarSizeInBits() / 8);
481
482 for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
483 for (unsigned i = 0; i != NumLaneElts; ++i) {
484 unsigned Base = i + Offset;
485 // if i+offset is out of this lane then we actually need the other source
486 // If Unary the other source is the first source.
487 if (Base >= NumLaneElts)
488 Base = Unary ? Base % NumLaneElts : Base + NumElts - NumLaneElts;
489 ShuffleMask.push_back(Base + l);
490 }
491 }
492}
493
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000494// concatSubVector - The function rebuilds the data to a correct expected
495// order. An assumption(The shape of the matrix) was taken for the
496// deinterleaved to work with lane's instructions like 'vpalign' or 'vphuf'.
497// This function ensures that the data is built in correct way for the lane
498// instructions. Each lane inside the vector is a 128-bit length.
499//
500// The 'InVec' argument contains the data in increasing order. In InVec[0] You
501// can find the first 128 bit data. The number of different lanes inside a
502// vector depends on the 'VecElems'.In general, the formula is
503// VecElems * type / 128. The size of the array 'InVec' depends and equal to
504// 'VecElems'.
505
506// For VecElems = 16
507// Invec[0] - |0| Vec[0] - |0|
508// Invec[1] - |1| => Vec[1] - |1|
509// Invec[2] - |2| Vec[2] - |2|
510
511// For VecElems = 32
512// Invec[0] - |0|1| Vec[0] - |0|3|
513// Invec[1] - |2|3| => Vec[1] - |1|4|
514// Invec[2] - |4|5| Vec[2] - |2|5|
515
516// For VecElems = 64
517// Invec[0] - |0|1|2 |3 | Vec[0] - |0|3|6|9 |
518// Invec[1] - |4|5|6 |7 | => Vec[1] - |1|4|7|10|
519// Invec[2] - |8|9|10|11| Vec[2] - |2|5|8|11|
520
521static void concatSubVector(Value **Vec, ArrayRef<Instruction *> InVec,
522 unsigned VecElems, IRBuilder<> Builder) {
523 if (VecElems == 16) {
524 for (int i = 0; i < 3; i++)
525 Vec[i] = InVec[i];
526 return;
527 }
528
529 for (unsigned j = 0; j < VecElems / 32; j++)
530 for (int i = 0; i < 3; i++)
531 Vec[i + j * 3] = Builder.CreateShuffleVector(
532 InVec[j * 6 + i], InVec[j * 6 + i + 3], makeArrayRef(Concat, 32));
533
534 if (VecElems == 32)
535 return;
536
537 for (int i = 0; i < 3; i++)
538 Vec[i] = Builder.CreateShuffleVector(Vec[i], Vec[i + 3], Concat);
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000539}
540
Michael Zuckerman5a385942017-09-07 14:02:13 +0000541void X86InterleavedAccessGroup::deinterleave8bitStride3(
542 ArrayRef<Instruction *> InVec, SmallVectorImpl<Value *> &TransposedMatrix,
543 unsigned VecElems) {
Michael Zuckerman5a385942017-09-07 14:02:13 +0000544 // Example: Assuming we start from the following vectors:
545 // Matrix[0]= a0 b0 c0 a1 b1 c1 a2 b2
546 // Matrix[1]= c2 a3 b3 c3 a4 b4 c4 a5
547 // Matrix[2]= b5 c5 a6 b6 c6 a7 b7 c7
548
549 TransposedMatrix.resize(3);
Michael Zuckerman5a385942017-09-07 14:02:13 +0000550 SmallVector<uint32_t, 32> VPShuf;
551 SmallVector<uint32_t, 32> VPAlign[2];
552 SmallVector<uint32_t, 32> VPAlign2;
553 SmallVector<uint32_t, 32> VPAlign3;
554 SmallVector<uint32_t, 3> GroupSize;
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000555 Value *Vec[6], *TempVector[3];
Michael Zuckerman5a385942017-09-07 14:02:13 +0000556
557 MVT VT = MVT::getVT(Shuffles[0]->getType());
558
Michael Zuckerman5a385942017-09-07 14:02:13 +0000559 createShuffleStride(VT, 3, VPShuf);
560 setGroupSize(VT, GroupSize);
561
562 for (int i = 0; i < 2; i++)
563 DecodePALIGNRMask(VT, GroupSize[2 - i], VPAlign[i], false);
564
565 DecodePALIGNRMask(VT, GroupSize[2] + GroupSize[1], VPAlign2, true, true);
566 DecodePALIGNRMask(VT, GroupSize[1], VPAlign3, true, true);
567
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000568 concatSubVector(Vec, InVec, VecElems, Builder);
Michael Zuckerman5a385942017-09-07 14:02:13 +0000569 // Vec[0]= a0 a1 a2 b0 b1 b2 c0 c1
570 // Vec[1]= c2 c3 c4 a3 a4 a5 b3 b4
571 // Vec[2]= b5 b6 b7 c5 c6 c7 a6 a7
572
573 for (int i = 0; i < 3; i++)
574 Vec[i] = Builder.CreateShuffleVector(
575 Vec[i], UndefValue::get(Vec[0]->getType()), VPShuf);
576
577 // TempVector[0]= a6 a7 a0 a1 a2 b0 b1 b2
578 // TempVector[1]= c0 c1 c2 c3 c4 a3 a4 a5
579 // TempVector[2]= b3 b4 b5 b6 b7 c5 c6 c7
580
581 for (int i = 0; i < 3; i++)
582 TempVector[i] =
583 Builder.CreateShuffleVector(Vec[(i + 2) % 3], Vec[i], VPAlign[0]);
584
585 // Vec[0]= a3 a4 a5 a6 a7 a0 a1 a2
586 // Vec[1]= c5 c6 c7 c0 c1 c2 c3 c4
587 // Vec[2]= b0 b1 b2 b3 b4 b5 b6 b7
588
589 for (int i = 0; i < 3; i++)
590 Vec[i] = Builder.CreateShuffleVector(TempVector[(i + 1) % 3], TempVector[i],
591 VPAlign[1]);
592
593 // TransposedMatrix[0]= a0 a1 a2 a3 a4 a5 a6 a7
594 // TransposedMatrix[1]= b0 b1 b2 b3 b4 b5 b6 b7
595 // TransposedMatrix[2]= c0 c1 c2 c3 c4 c5 c6 c7
596
597 Value *TempVec = Builder.CreateShuffleVector(
598 Vec[1], UndefValue::get(Vec[1]->getType()), VPAlign3);
599 TransposedMatrix[0] = Builder.CreateShuffleVector(
600 Vec[0], UndefValue::get(Vec[1]->getType()), VPAlign2);
601 TransposedMatrix[1] = VecElems == 8 ? Vec[2] : TempVec;
602 TransposedMatrix[2] = VecElems == 8 ? TempVec : Vec[2];
Michael Zuckerman5a385942017-09-07 14:02:13 +0000603}
604
Michael Zuckerman645f7772017-09-26 18:49:11 +0000605// group2Shuffle reorder the shuffle stride back into continuous order.
606// For example For VF16 with Mask1 = {0,3,6,9,12,15,2,5,8,11,14,1,4,7,10,13} =>
607// MaskResult = {0,11,6,1,12,7,2,13,8,3,14,9,4,15,10,5}.
608static void group2Shuffle(MVT VT, SmallVectorImpl<uint32_t> &Mask,
609 SmallVectorImpl<uint32_t> &Output) {
610 int IndexGroup[3] = {0, 0, 0};
611 int Index = 0;
612 int VectorWidth = VT.getSizeInBits();
613 int VF = VT.getVectorNumElements();
614 // Find the index of the different groups.
615 int Lane = (VectorWidth / 128 > 0) ? VectorWidth / 128 : 1;
616 for (int i = 0; i < 3; i++) {
617 IndexGroup[(Index * 3) % (VF / Lane)] = Index;
618 Index += Mask[i];
619 }
620 // According to the index compute the convert mask.
621 for (int i = 0; i < VF / Lane; i++) {
622 Output.push_back(IndexGroup[i % 3]);
623 IndexGroup[i % 3]++;
624 }
625}
626
Michael Zuckerman645f7772017-09-26 18:49:11 +0000627void X86InterleavedAccessGroup::interleave8bitStride3(
628 ArrayRef<Instruction *> InVec, SmallVectorImpl<Value *> &TransposedMatrix,
629 unsigned VecElems) {
Michael Zuckerman645f7772017-09-26 18:49:11 +0000630 // Example: Assuming we start from the following vectors:
631 // Matrix[0]= a0 a1 a2 a3 a4 a5 a6 a7
632 // Matrix[1]= b0 b1 b2 b3 b4 b5 b6 b7
633 // Matrix[2]= c0 c1 c2 c3 c3 a7 b7 c7
634
635 TransposedMatrix.resize(3);
636 SmallVector<uint32_t, 3> GroupSize;
637 SmallVector<uint32_t, 32> VPShuf;
638 SmallVector<uint32_t, 32> VPAlign[3];
639 SmallVector<uint32_t, 32> VPAlign2;
640 SmallVector<uint32_t, 32> VPAlign3;
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000641
Michael Zuckerman645f7772017-09-26 18:49:11 +0000642 Value *Vec[3], *TempVector[3];
643 MVT VT = MVT::getVectorVT(MVT::i8, VecElems);
644
645 setGroupSize(VT, GroupSize);
646
647 for (int i = 0; i < 3; i++)
648 DecodePALIGNRMask(VT, GroupSize[i], VPAlign[i]);
649
650 DecodePALIGNRMask(VT, GroupSize[1] + GroupSize[2], VPAlign2, false, true);
651 DecodePALIGNRMask(VT, GroupSize[1], VPAlign3, false, true);
652
653 // Vec[0]= a3 a4 a5 a6 a7 a0 a1 a2
654 // Vec[1]= c5 c6 c7 c0 c1 c2 c3 c4
655 // Vec[2]= b0 b1 b2 b3 b4 b5 b6 b7
656
657 Vec[0] = Builder.CreateShuffleVector(
658 InVec[0], UndefValue::get(InVec[0]->getType()), VPAlign2);
659 Vec[1] = Builder.CreateShuffleVector(
660 InVec[1], UndefValue::get(InVec[1]->getType()), VPAlign3);
661 Vec[2] = InVec[2];
662
663 // Vec[0]= a6 a7 a0 a1 a2 b0 b1 b2
664 // Vec[1]= c0 c1 c2 c3 c4 a3 a4 a5
665 // Vec[2]= b3 b4 b5 b6 b7 c5 c6 c7
666
667 for (int i = 0; i < 3; i++)
668 TempVector[i] =
669 Builder.CreateShuffleVector(Vec[i], Vec[(i + 2) % 3], VPAlign[1]);
670
671 // Vec[0]= a0 a1 a2 b0 b1 b2 c0 c1
672 // Vec[1]= c2 c3 c4 a3 a4 a5 b3 b4
673 // Vec[2]= b5 b6 b7 c5 c6 c7 a6 a7
674
675 for (int i = 0; i < 3; i++)
676 Vec[i] = Builder.CreateShuffleVector(TempVector[i], TempVector[(i + 1) % 3],
677 VPAlign[2]);
678
679 // TransposedMatrix[0] = a0 b0 c0 a1 b1 c1 a2 b2
680 // TransposedMatrix[1] = c2 a3 b3 c3 a4 b4 c4 a5
681 // TransposedMatrix[2] = b5 c5 a6 b6 c6 a7 b7 c7
682
Michael Zuckerman645f7772017-09-26 18:49:11 +0000683 unsigned NumOfElm = VT.getVectorNumElements();
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000684 group2Shuffle(VT, GroupSize, VPShuf);
685 reorderSubVector(VT, TransposedMatrix, Vec, VPShuf, NumOfElm,3, Builder);
Michael Zuckerman645f7772017-09-26 18:49:11 +0000686}
687
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000688void X86InterleavedAccessGroup::transpose_4x4(
689 ArrayRef<Instruction *> Matrix,
690 SmallVectorImpl<Value *> &TransposedMatrix) {
691 assert(Matrix.size() == 4 && "Invalid matrix size");
692 TransposedMatrix.resize(4);
693
694 // dst = src1[0,1],src2[0,1]
695 uint32_t IntMask1[] = {0, 1, 4, 5};
696 ArrayRef<uint32_t> Mask = makeArrayRef(IntMask1, 4);
697 Value *IntrVec1 = Builder.CreateShuffleVector(Matrix[0], Matrix[2], Mask);
698 Value *IntrVec2 = Builder.CreateShuffleVector(Matrix[1], Matrix[3], Mask);
699
700 // dst = src1[2,3],src2[2,3]
701 uint32_t IntMask2[] = {2, 3, 6, 7};
702 Mask = makeArrayRef(IntMask2, 4);
703 Value *IntrVec3 = Builder.CreateShuffleVector(Matrix[0], Matrix[2], Mask);
704 Value *IntrVec4 = Builder.CreateShuffleVector(Matrix[1], Matrix[3], Mask);
705
706 // dst = src1[0],src2[0],src1[2],src2[2]
707 uint32_t IntMask3[] = {0, 4, 2, 6};
708 Mask = makeArrayRef(IntMask3, 4);
709 TransposedMatrix[0] = Builder.CreateShuffleVector(IntrVec1, IntrVec2, Mask);
710 TransposedMatrix[2] = Builder.CreateShuffleVector(IntrVec3, IntrVec4, Mask);
711
712 // dst = src1[1],src2[1],src1[3],src2[3]
713 uint32_t IntMask4[] = {1, 5, 3, 7};
714 Mask = makeArrayRef(IntMask4, 4);
715 TransposedMatrix[1] = Builder.CreateShuffleVector(IntrVec1, IntrVec2, Mask);
716 TransposedMatrix[3] = Builder.CreateShuffleVector(IntrVec3, IntrVec4, Mask);
717}
718
719// Lowers this interleaved access group into X86-specific
720// instructions/intrinsics.
721bool X86InterleavedAccessGroup::lowerIntoOptimizedSequence() {
722 SmallVector<Instruction *, 4> DecomposedVectors;
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000723 SmallVector<Value *, 4> TransposedVectors;
Farhana Aleen4b652a52017-06-22 22:59:04 +0000724 VectorType *ShuffleTy = Shuffles[0]->getType();
725
726 if (isa<LoadInst>(Inst)) {
727 // Try to generate target-sized register(/instruction).
728 decompose(Inst, Factor, ShuffleTy, DecomposedVectors);
729
Michael Zuckerman5a385942017-09-07 14:02:13 +0000730 Type *ShuffleEltTy = Inst->getType();
731 unsigned NumSubVecElems = ShuffleEltTy->getVectorNumElements() / Factor;
Farhana Aleen4b652a52017-06-22 22:59:04 +0000732 // Perform matrix-transposition in order to compute interleaved
733 // results by generating some sort of (optimized) target-specific
734 // instructions.
Michael Zuckerman5a385942017-09-07 14:02:13 +0000735
736 switch (NumSubVecElems) {
737 default:
738 return false;
739 case 4:
740 transpose_4x4(DecomposedVectors, TransposedVectors);
741 break;
742 case 8:
743 case 16:
744 case 32:
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000745 case 64:
Michael Zuckerman5a385942017-09-07 14:02:13 +0000746 deinterleave8bitStride3(DecomposedVectors, TransposedVectors,
747 NumSubVecElems);
748 break;
749 }
Farhana Aleen4b652a52017-06-22 22:59:04 +0000750
751 // Now replace the unoptimized-interleaved-vectors with the
752 // transposed-interleaved vectors.
753 for (unsigned i = 0, e = Shuffles.size(); i < e; ++i)
754 Shuffles[i]->replaceAllUsesWith(TransposedVectors[Indices[i]]);
755
756 return true;
757 }
758
759 Type *ShuffleEltTy = ShuffleTy->getVectorElementType();
760 unsigned NumSubVecElems = ShuffleTy->getVectorNumElements() / Factor;
761
762 // Lower the interleaved stores:
763 // 1. Decompose the interleaved wide shuffle into individual shuffle
764 // vectors.
Farhana Aleene4a89a62017-07-21 21:35:00 +0000765 decompose(Shuffles[0], Factor, VectorType::get(ShuffleEltTy, NumSubVecElems),
766 DecomposedVectors);
Farhana Aleen4b652a52017-06-22 22:59:04 +0000767
768 // 2. Transpose the interleaved-vectors into vectors of contiguous
769 // elements.
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000770 switch (NumSubVecElems) {
771 case 4:
772 transpose_4x4(DecomposedVectors, TransposedVectors);
773 break;
Michael Zuckerman4a97df02017-09-25 14:50:38 +0000774 case 8:
775 interleave8bitStride4VF8(DecomposedVectors, TransposedVectors);
776 break;
Michael Zuckerman680ac102017-08-07 13:22:39 +0000777 case 16:
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000778 case 32:
Michael Zuckermane4084f6b2017-10-02 07:35:25 +0000779 case 64:
Michael Zuckerman645f7772017-09-26 18:49:11 +0000780 if (Factor == 4)
781 interleave8bitStride4(DecomposedVectors, TransposedVectors,
782 NumSubVecElems);
783 if (Factor == 3)
784 interleave8bitStride3(DecomposedVectors, TransposedVectors,
785 NumSubVecElems);
Michael Zuckermanc1918ad2017-07-26 08:10:14 +0000786 break;
787 default:
788 return false;
789 }
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000790
Farhana Aleen4b652a52017-06-22 22:59:04 +0000791 // 3. Concatenate the contiguous-vectors back into a wide vector.
792 Value *WideVec = concatenateVectors(Builder, TransposedVectors);
793
794 // 4. Generate a store instruction for wide-vec.
795 StoreInst *SI = cast<StoreInst>(Inst);
796 Builder.CreateAlignedStore(WideVec, SI->getPointerOperand(),
797 SI->getAlignment());
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000798
799 return true;
800}
801
802// Lower interleaved load(s) into target specific instructions/
803// intrinsics. Lowering sequence varies depending on the vector-types, factor,
804// number of shuffles and ISA.
805// Currently, lowering is supported for 4x64 bits with Factor = 4 on AVX.
David L Kreitzer01a057a2016-10-14 18:20:41 +0000806bool X86TargetLowering::lowerInterleavedLoad(
807 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
808 ArrayRef<unsigned> Indices, unsigned Factor) const {
809 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
810 "Invalid interleave factor");
811 assert(!Shuffles.empty() && "Empty shufflevector input");
812 assert(Shuffles.size() == Indices.size() &&
813 "Unmatched number of shufflevectors and indices");
814
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000815 // Create an interleaved access group.
David L Kreitzer01a057a2016-10-14 18:20:41 +0000816 IRBuilder<> Builder(LI);
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000817 X86InterleavedAccessGroup Grp(LI, Shuffles, Indices, Factor, Subtarget,
818 Builder);
David L Kreitzer01a057a2016-10-14 18:20:41 +0000819
David L Kreitzer0e3ae302016-12-01 19:56:39 +0000820 return Grp.isSupported() && Grp.lowerIntoOptimizedSequence();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000821}
Farhana Aleen4b652a52017-06-22 22:59:04 +0000822
823bool X86TargetLowering::lowerInterleavedStore(StoreInst *SI,
824 ShuffleVectorInst *SVI,
825 unsigned Factor) const {
826 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
827 "Invalid interleave factor");
828
Farhana Aleen9bd593e2017-06-22 23:56:31 +0000829 assert(SVI->getType()->getVectorNumElements() % Factor == 0 &&
Farhana Aleen4b652a52017-06-22 22:59:04 +0000830 "Invalid interleaved store");
831
832 // Holds the indices of SVI that correspond to the starting index of each
833 // interleaved shuffle.
834 SmallVector<unsigned, 4> Indices;
835 auto Mask = SVI->getShuffleMask();
836 for (unsigned i = 0; i < Factor; i++)
837 Indices.push_back(Mask[i]);
838
839 ArrayRef<ShuffleVectorInst *> Shuffles = makeArrayRef(SVI);
840
841 // Create an interleaved access group.
842 IRBuilder<> Builder(SI);
843 X86InterleavedAccessGroup Grp(SI, Shuffles, Indices, Factor, Subtarget,
844 Builder);
845
846 return Grp.isSupported() && Grp.lowerIntoOptimizedSequence();
847}