49 class X86InterleavedAccessGroup {
61 const unsigned Factor;
92 unsigned NumSubVecElems);
97 unsigned NumSubVecElems);
100 unsigned NumSubVecElems);
114 : Inst(I), Shuffles(Shuffs), Indices(Ind), Factor(F), Subtarget(STarget),
115 DL(Inst->getModule()->getDataLayout()), Builder(B) {}
119 bool isSupported()
const;
123 bool lowerIntoOptimizedSequence();
128 bool X86InterleavedAccessGroup::isSupported()
const {
129 VectorType *ShuffleVecTy = Shuffles[0]->getType();
131 unsigned ShuffleElemSize = DL.getTypeSizeInBits(ShuffleEltTy);
132 unsigned WideInstSize;
140 if (!Subtarget.hasAVX() || (Factor != 4 && Factor != 3))
143 if (isa<LoadInst>(Inst)) {
144 WideInstSize = DL.getTypeSizeInBits(Inst->getType());
145 if (cast<LoadInst>(Inst)->getPointerAddressSpace())
148 WideInstSize = DL.getTypeSizeInBits(Shuffles[0]->
getType());
152 if (ShuffleElemSize == 64 && WideInstSize == 1024 && Factor == 4)
155 if (ShuffleElemSize == 8 && isa<StoreInst>(Inst) && Factor == 4 &&
156 (WideInstSize == 256 || WideInstSize == 512 || WideInstSize == 1024 ||
157 WideInstSize == 2048))
160 if (ShuffleElemSize == 8 && Factor == 3 &&
161 (WideInstSize == 384 || WideInstSize == 768 || WideInstSize == 1536))
167 void X86InterleavedAccessGroup::decompose(
170 assert((isa<LoadInst>(VecInst) || isa<ShuffleVectorInst>(VecInst)) &&
171 "Expected Load or Shuffle");
176 DL.getTypeSizeInBits(VecWidth) >=
177 DL.getTypeSizeInBits(SubVecTy) * NumSubVectors &&
178 "Invalid Inst-size!!!");
180 if (
auto *SVI = dyn_cast<ShuffleVectorInst>(VecInst)) {
181 Value *Op0 = SVI->getOperand(0);
182 Value *Op1 = SVI->getOperand(1);
185 for (
unsigned i = 0; i < NumSubVectors; ++i)
187 cast<ShuffleVectorInst>(Builder.CreateShuffleVector(
195 LoadInst *LI = cast<LoadInst>(VecInst);
196 Type *VecBaseTy, *VecBasePtrTy;
198 unsigned int NumLoads = NumSubVectors;
202 unsigned VecLength = DL.getTypeSizeInBits(VecWidth);
203 if (VecLength == 768 || VecLength == 1536) {
207 NumLoads = NumSubVectors * (VecLength / 384);
209 VecBaseTy = SubVecTy;
214 for (
unsigned i = 0; i < NumLoads; i++) {
217 Builder.CreateGEP(VecBaseTy, VecBasePtr, Builder.getInt32(i));
219 Builder.CreateAlignedLoad(VecBaseTy, NewBasePtr, LI->
getAlignment());
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 };
258 "This function doesn't accept width smaller then 256");
260 for (
unsigned i = 0; i < Mask.
size(); i++)
262 for (
unsigned i = 0; i < Mask.
size(); i++)
263 Out.
push_back(Mask[i] + HighOffset + NumOfElm);
286 unsigned VecElems,
unsigned Stride,
289 if (VecElems == 16) {
290 for (
unsigned i = 0; i < Stride; i++)
299 for (
unsigned i = 0; i < (VecElems / 16) * Stride; i += 2) {
301 (i + 1) / Stride * 16);
303 Vec[i % Stride], Vec[(i + 1) % Stride], OptimizeShuf);
304 OptimizeShuf.
clear();
307 if (VecElems == 32) {
312 for (
unsigned i = 0; i < Stride; i++)
313 TransposedMatrix[i] =
317 void X86InterleavedAccessGroup::interleave8bitStride4VF8(
327 TransposedMatrix.
resize(2);
332 for (
unsigned i = 0; i < 8; ++i) {
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);
344 Builder.CreateShuffleVector(Matrix[0], Matrix[1], MaskLow);
346 Builder.CreateShuffleVector(Matrix[2], Matrix[3], MaskLow);
351 TransposedMatrix[0] =
352 Builder.CreateShuffleVector(IntrVec1Low, IntrVec2Low, MaskLowWord);
353 TransposedMatrix[1] =
354 Builder.CreateShuffleVector(IntrVec1Low, IntrVec2Low, MaskHighWord);
357 void X86InterleavedAccessGroup::interleave8bitStride4(
369 TransposedMatrix.
resize(4);
379 createUnpackShuffleMask<uint32_t>(VT, MaskLow,
true,
false);
380 createUnpackShuffleMask<uint32_t>(VT, MaskHigh,
false,
false);
385 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]);
396 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);
407 for (
int i = 0; i < 4; i++)
408 VecOut[i] = Builder.CreateShuffleVector(IntrVec[i / 2], IntrVec[i / 2 + 2],
422 NumOfElm, 4, Builder);
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);
452 for (
int i = 0, FirstGroupElement = 0; i < 3; i++) {
453 int GroupSize = std::ceil((VF - FirstGroupElement) / 3.0);
455 FirstGroupElement = ((GroupSize)*3 + FirstGroupElement) % VF;
474 bool AlignDirection =
true,
bool Unary =
false) {
477 unsigned NumLaneElts = NumElts / NumLanes;
479 Imm = AlignDirection ? Imm : (NumLaneElts - Imm);
482 for (
unsigned l = 0; l != NumElts; l += NumLaneElts) {
483 for (
unsigned i = 0; i != NumLaneElts; ++i) {
487 if (Base >= NumLaneElts)
488 Base = Unary ? Base % NumLaneElts : Base + NumElts - NumLaneElts;
523 if (VecElems == 16) {
524 for (
int i = 0; i < 3; i++)
529 for (
unsigned j = 0; j < VecElems / 32; j++)
530 for (
int i = 0; i < 3; i++)
532 InVec[j * 6 + i], InVec[j * 6 + i + 3],
makeArrayRef(Concat, 32));
537 for (
int i = 0; i < 3; i++)
541 void X86InterleavedAccessGroup::deinterleave8bitStride3(
549 TransposedMatrix.
resize(3);
555 Value *Vec[6], *TempVector[3];
562 for (
int i = 0; i < 2; i++)
573 for (
int i = 0; i < 3; i++)
574 Vec[i] = Builder.CreateShuffleVector(
581 for (
int i = 0; i < 3; i++)
583 Builder.CreateShuffleVector(Vec[(i + 2) % 3], Vec[i], VPAlign[0]);
589 for (
int i = 0; i < 3; i++)
590 Vec[i] = Builder.CreateShuffleVector(TempVector[(i + 1) % 3], TempVector[i],
597 Value *TempVec = Builder.CreateShuffleVector(
599 TransposedMatrix[0] = Builder.CreateShuffleVector(
601 TransposedMatrix[1] = VecElems == 8 ? Vec[2] : TempVec;
602 TransposedMatrix[2] = VecElems == 8 ? TempVec : Vec[2];
610 int IndexGroup[3] = {0, 0, 0};
615 int Lane = (VectorWidth / 128 > 0) ? VectorWidth / 128 : 1;
616 for (
int i = 0; i < 3; i++) {
617 IndexGroup[(Index * 3) % (VF / Lane)] =
Index;
621 for (
int i = 0; i < VF / Lane; i++) {
627 void X86InterleavedAccessGroup::interleave8bitStride3(
635 TransposedMatrix.
resize(3);
642 Value *Vec[3], *TempVector[3];
647 for (
int i = 0; i < 3; i++)
657 Vec[0] = Builder.CreateShuffleVector(
659 Vec[1] = Builder.CreateShuffleVector(
667 for (
int i = 0; i < 3; i++)
669 Builder.CreateShuffleVector(Vec[i], Vec[(i + 2) % 3], VPAlign[1]);
675 for (
int i = 0; i < 3; i++)
676 Vec[i] = Builder.CreateShuffleVector(TempVector[i], TempVector[(i + 1) % 3],
688 void X86InterleavedAccessGroup::transpose_4x4(
691 assert(Matrix.
size() == 4 &&
"Invalid matrix size");
692 TransposedMatrix.
resize(4);
697 Value *IntrVec1 = Builder.CreateShuffleVector(Matrix[0], Matrix[2], Mask);
698 Value *IntrVec2 = Builder.CreateShuffleVector(Matrix[1], Matrix[3], Mask);
703 Value *IntrVec3 = Builder.CreateShuffleVector(Matrix[0], Matrix[2], Mask);
704 Value *IntrVec4 = Builder.CreateShuffleVector(Matrix[1], Matrix[3], Mask);
709 TransposedMatrix[0] = Builder.CreateShuffleVector(IntrVec1, IntrVec2, Mask);
710 TransposedMatrix[2] = Builder.CreateShuffleVector(IntrVec3, IntrVec4, Mask);
715 TransposedMatrix[1] = Builder.CreateShuffleVector(IntrVec1, IntrVec2, Mask);
716 TransposedMatrix[3] = Builder.CreateShuffleVector(IntrVec3, IntrVec4, Mask);
721 bool X86InterleavedAccessGroup::lowerIntoOptimizedSequence() {
724 VectorType *ShuffleTy = Shuffles[0]->getType();
726 if (isa<LoadInst>(Inst)) {
728 decompose(Inst, Factor, ShuffleTy, DecomposedVectors);
730 Type *ShuffleEltTy = Inst->getType();
736 switch (NumSubVecElems) {
740 transpose_4x4(DecomposedVectors, TransposedVectors);
746 deinterleave8bitStride3(DecomposedVectors, TransposedVectors,
753 for (
unsigned i = 0,
e = Shuffles.size(); i <
e; ++i)
754 Shuffles[i]->replaceAllUsesWith(TransposedVectors[Indices[i]]);
765 decompose(Shuffles[0], Factor,
VectorType::get(ShuffleEltTy, NumSubVecElems),
770 switch (NumSubVecElems) {
772 transpose_4x4(DecomposedVectors, TransposedVectors);
775 interleave8bitStride4VF8(DecomposedVectors, TransposedVectors);
781 interleave8bitStride4(DecomposedVectors, TransposedVectors,
784 interleave8bitStride3(DecomposedVectors, TransposedVectors,
809 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
810 "Invalid interleave factor");
811 assert(!Shuffles.
empty() &&
"Empty shufflevector input");
813 "Unmatched number of shufflevectors and indices");
817 X86InterleavedAccessGroup Grp(LI, Shuffles, Indices, Factor, Subtarget,
820 return Grp.isSupported() && Grp.lowerIntoOptimizedSequence();
825 unsigned Factor)
const {
826 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
827 "Invalid interleave factor");
830 "Invalid interleaved store");
836 for (
unsigned i = 0; i < Factor; i++)
837 Indices.push_back(
Mask[i]);
843 X86InterleavedAccessGroup Grp(SI, Shuffles, Indices, Factor, Subtarget,
846 return Grp.isSupported() && Grp.lowerIntoOptimizedSequence();
static void setGroupSize(MVT VT, SmallVectorImpl< uint32_t > &SizeInfo)
Type * getVectorElementType() const
static MVT getIntegerVT(unsigned BitWidth)
A parsed version of the target data layout string in and methods for querying it. ...
This class represents lattice values for constants.
static MVT getVectorVT(MVT VT, unsigned NumElements)
void push_back(const T &Elt)
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
unsigned getVectorNumElements() const
This instruction constructs a fixed permutation of two input vectors.
LLVMContext & getContext() const
All values hold a context through their type.
An instruction for reading from memory.
static void genShuffleBland(MVT VT, ArrayRef< uint32_t > Mask, SmallVectorImpl< uint32_t > &Out, int LowOffset, int HighOffset)
bool isVectorTy() const
True if this is an instance of VectorType.
bool lowerInterleavedLoad(LoadInst *LI, ArrayRef< ShuffleVectorInst *> Shuffles, ArrayRef< unsigned > Indices, unsigned Factor) const override
Lower interleaved load(s) into target specific instructions/intrinsics.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Constant * createSequentialMask(IRBuilder<> &Builder, unsigned Start, unsigned NumInts, unsigned NumUndefs)
Create a sequential shuffle mask.
Type * getType() const
All values are typed, get the type of this value.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
An instruction for storing to memory.
MVT getVectorElementType() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Value * concatenateVectors(IRBuilder<> &Builder, ArrayRef< Value *> Vecs)
Concatenate a list of vectors.
The instances of the Type class are immutable: once they are created, they are never changed...
size_t size() const
size - Get the array size.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * getPointerOperand()
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static wasm::ValType getType(const TargetRegisterClass *RC)
static void concatSubVector(Value **Vec, ArrayRef< Instruction *> InVec, unsigned VecElems, IRBuilder<> Builder)
static void reorderSubVector(MVT VT, SmallVectorImpl< Value *> &TransposedMatrix, ArrayRef< Value *> Vec, ArrayRef< uint32_t > VPShuf, unsigned VecElems, unsigned Stride, IRBuilder<> Builder)
Align max(MaybeAlign Lhs, Align Rhs)
bool lowerInterleavedStore(StoreInst *SI, ShuffleVectorInst *SVI, unsigned Factor) const override
Lower interleaved store(s) into target specific instructions/intrinsics.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
static MVT scaleVectorType(MVT VT)
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
TypeSize getScalarSizeInBits() const
unsigned getVectorNumElements() const
Class to represent vector types.
unsigned getAlignment() const
Return the alignment of the access that is being performed.
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static void group2Shuffle(MVT VT, SmallVectorImpl< uint32_t > &Mask, SmallVectorImpl< uint32_t > &Output)
unsigned getAlignment() const
Return the alignment of the access that is being performed FIXME: Remove this function once transitio...
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
static void createShuffleStride(MVT VT, int Stride, SmallVectorImpl< uint32_t > &Mask)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
void DecodePALIGNRMask(unsigned NumElts, unsigned Imm, SmallVectorImpl< int > &ShuffleMask)
OutputIt copy(R &&Range, OutputIt Out)
VectorType * getType() const
Overload to return most specific vector type.
Value * getPointerOperand()
static IntegerType * getInt8Ty(LLVMContext &C)
bool empty() const
empty - Check if the array is empty.