106c3fb27SDimitry Andric //===-- X86FixupVectorConstants.cpp - optimize constant generation -------===// 206c3fb27SDimitry Andric // 306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 606c3fb27SDimitry Andric // 706c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 806c3fb27SDimitry Andric // 906c3fb27SDimitry Andric // This file examines all full size vector constant pool loads and attempts to 1006c3fb27SDimitry Andric // replace them with smaller constant pool entries, including: 11*0fca6ea1SDimitry Andric // * Converting AVX512 memory-fold instructions to their broadcast-fold form. 12*0fca6ea1SDimitry Andric // * Using vzload scalar loads. 135f757f3fSDimitry Andric // * Broadcasting of full width loads. 14*0fca6ea1SDimitry Andric // * Sign/Zero extension of full width loads. 1506c3fb27SDimitry Andric // 1606c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 1706c3fb27SDimitry Andric 1806c3fb27SDimitry Andric #include "X86.h" 1906c3fb27SDimitry Andric #include "X86InstrFoldTables.h" 2006c3fb27SDimitry Andric #include "X86InstrInfo.h" 2106c3fb27SDimitry Andric #include "X86Subtarget.h" 2206c3fb27SDimitry Andric #include "llvm/ADT/Statistic.h" 2306c3fb27SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h" 2406c3fb27SDimitry Andric 2506c3fb27SDimitry Andric using namespace llvm; 2606c3fb27SDimitry Andric 2706c3fb27SDimitry Andric #define DEBUG_TYPE "x86-fixup-vector-constants" 2806c3fb27SDimitry Andric 2906c3fb27SDimitry Andric STATISTIC(NumInstChanges, "Number of instructions changes"); 3006c3fb27SDimitry Andric 3106c3fb27SDimitry Andric namespace { 3206c3fb27SDimitry Andric class X86FixupVectorConstantsPass : public MachineFunctionPass { 3306c3fb27SDimitry Andric public: 3406c3fb27SDimitry Andric static char ID; 3506c3fb27SDimitry Andric 3606c3fb27SDimitry Andric X86FixupVectorConstantsPass() : MachineFunctionPass(ID) {} 3706c3fb27SDimitry Andric 3806c3fb27SDimitry Andric StringRef getPassName() const override { 3906c3fb27SDimitry Andric return "X86 Fixup Vector Constants"; 4006c3fb27SDimitry Andric } 4106c3fb27SDimitry Andric 4206c3fb27SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 4306c3fb27SDimitry Andric bool processInstruction(MachineFunction &MF, MachineBasicBlock &MBB, 4406c3fb27SDimitry Andric MachineInstr &MI); 4506c3fb27SDimitry Andric 4606c3fb27SDimitry Andric // This pass runs after regalloc and doesn't support VReg operands. 4706c3fb27SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 4806c3fb27SDimitry Andric return MachineFunctionProperties().set( 4906c3fb27SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 5006c3fb27SDimitry Andric } 5106c3fb27SDimitry Andric 5206c3fb27SDimitry Andric private: 5306c3fb27SDimitry Andric const X86InstrInfo *TII = nullptr; 5406c3fb27SDimitry Andric const X86Subtarget *ST = nullptr; 5506c3fb27SDimitry Andric const MCSchedModel *SM = nullptr; 5606c3fb27SDimitry Andric }; 5706c3fb27SDimitry Andric } // end anonymous namespace 5806c3fb27SDimitry Andric 5906c3fb27SDimitry Andric char X86FixupVectorConstantsPass::ID = 0; 6006c3fb27SDimitry Andric 6106c3fb27SDimitry Andric INITIALIZE_PASS(X86FixupVectorConstantsPass, DEBUG_TYPE, DEBUG_TYPE, false, false) 6206c3fb27SDimitry Andric 6306c3fb27SDimitry Andric FunctionPass *llvm::createX86FixupVectorConstants() { 6406c3fb27SDimitry Andric return new X86FixupVectorConstantsPass(); 6506c3fb27SDimitry Andric } 6606c3fb27SDimitry Andric 67*0fca6ea1SDimitry Andric /// Normally, we only allow poison in vector splats. However, as this is part 68*0fca6ea1SDimitry Andric /// of the backend, and working with the DAG representation, which currently 69*0fca6ea1SDimitry Andric /// only natively represents undef values, we need to accept undefs here. 70*0fca6ea1SDimitry Andric static Constant *getSplatValueAllowUndef(const ConstantVector *C) { 71*0fca6ea1SDimitry Andric Constant *Res = nullptr; 72*0fca6ea1SDimitry Andric for (Value *Op : C->operands()) { 73*0fca6ea1SDimitry Andric Constant *OpC = cast<Constant>(Op); 74*0fca6ea1SDimitry Andric if (isa<UndefValue>(OpC)) 75*0fca6ea1SDimitry Andric continue; 76*0fca6ea1SDimitry Andric if (!Res) 77*0fca6ea1SDimitry Andric Res = OpC; 78*0fca6ea1SDimitry Andric else if (Res != OpC) 79*0fca6ea1SDimitry Andric return nullptr; 80*0fca6ea1SDimitry Andric } 81*0fca6ea1SDimitry Andric return Res; 82*0fca6ea1SDimitry Andric } 83*0fca6ea1SDimitry Andric 8406c3fb27SDimitry Andric // Attempt to extract the full width of bits data from the constant. 8506c3fb27SDimitry Andric static std::optional<APInt> extractConstantBits(const Constant *C) { 8606c3fb27SDimitry Andric unsigned NumBits = C->getType()->getPrimitiveSizeInBits(); 8706c3fb27SDimitry Andric 88*0fca6ea1SDimitry Andric if (isa<UndefValue>(C)) 89*0fca6ea1SDimitry Andric return APInt::getZero(NumBits); 90*0fca6ea1SDimitry Andric 9106c3fb27SDimitry Andric if (auto *CInt = dyn_cast<ConstantInt>(C)) 9206c3fb27SDimitry Andric return CInt->getValue(); 9306c3fb27SDimitry Andric 9406c3fb27SDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(C)) 9506c3fb27SDimitry Andric return CFP->getValue().bitcastToAPInt(); 9606c3fb27SDimitry Andric 9706c3fb27SDimitry Andric if (auto *CV = dyn_cast<ConstantVector>(C)) { 98*0fca6ea1SDimitry Andric if (auto *CVSplat = getSplatValueAllowUndef(CV)) { 9906c3fb27SDimitry Andric if (std::optional<APInt> Bits = extractConstantBits(CVSplat)) { 10006c3fb27SDimitry Andric assert((NumBits % Bits->getBitWidth()) == 0 && "Illegal splat"); 10106c3fb27SDimitry Andric return APInt::getSplat(NumBits, *Bits); 10206c3fb27SDimitry Andric } 10306c3fb27SDimitry Andric } 104*0fca6ea1SDimitry Andric 105*0fca6ea1SDimitry Andric APInt Bits = APInt::getZero(NumBits); 106*0fca6ea1SDimitry Andric for (unsigned I = 0, E = CV->getNumOperands(); I != E; ++I) { 107*0fca6ea1SDimitry Andric Constant *Elt = CV->getOperand(I); 108*0fca6ea1SDimitry Andric std::optional<APInt> SubBits = extractConstantBits(Elt); 109*0fca6ea1SDimitry Andric if (!SubBits) 110*0fca6ea1SDimitry Andric return std::nullopt; 111*0fca6ea1SDimitry Andric assert(NumBits == (E * SubBits->getBitWidth()) && 112*0fca6ea1SDimitry Andric "Illegal vector element size"); 113*0fca6ea1SDimitry Andric Bits.insertBits(*SubBits, I * SubBits->getBitWidth()); 114*0fca6ea1SDimitry Andric } 115*0fca6ea1SDimitry Andric return Bits; 11606c3fb27SDimitry Andric } 11706c3fb27SDimitry Andric 11806c3fb27SDimitry Andric if (auto *CDS = dyn_cast<ConstantDataSequential>(C)) { 11906c3fb27SDimitry Andric bool IsInteger = CDS->getElementType()->isIntegerTy(); 12006c3fb27SDimitry Andric bool IsFloat = CDS->getElementType()->isHalfTy() || 12106c3fb27SDimitry Andric CDS->getElementType()->isBFloatTy() || 12206c3fb27SDimitry Andric CDS->getElementType()->isFloatTy() || 12306c3fb27SDimitry Andric CDS->getElementType()->isDoubleTy(); 12406c3fb27SDimitry Andric if (IsInteger || IsFloat) { 12506c3fb27SDimitry Andric APInt Bits = APInt::getZero(NumBits); 12606c3fb27SDimitry Andric unsigned EltBits = CDS->getElementType()->getPrimitiveSizeInBits(); 12706c3fb27SDimitry Andric for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) { 12806c3fb27SDimitry Andric if (IsInteger) 12906c3fb27SDimitry Andric Bits.insertBits(CDS->getElementAsAPInt(I), I * EltBits); 13006c3fb27SDimitry Andric else 13106c3fb27SDimitry Andric Bits.insertBits(CDS->getElementAsAPFloat(I).bitcastToAPInt(), 13206c3fb27SDimitry Andric I * EltBits); 13306c3fb27SDimitry Andric } 13406c3fb27SDimitry Andric return Bits; 13506c3fb27SDimitry Andric } 13606c3fb27SDimitry Andric } 13706c3fb27SDimitry Andric 13806c3fb27SDimitry Andric return std::nullopt; 13906c3fb27SDimitry Andric } 14006c3fb27SDimitry Andric 141*0fca6ea1SDimitry Andric static std::optional<APInt> extractConstantBits(const Constant *C, 142*0fca6ea1SDimitry Andric unsigned NumBits) { 143*0fca6ea1SDimitry Andric if (std::optional<APInt> Bits = extractConstantBits(C)) 144*0fca6ea1SDimitry Andric return Bits->zextOrTrunc(NumBits); 145*0fca6ea1SDimitry Andric return std::nullopt; 146*0fca6ea1SDimitry Andric } 147*0fca6ea1SDimitry Andric 14806c3fb27SDimitry Andric // Attempt to compute the splat width of bits data by normalizing the splat to 14906c3fb27SDimitry Andric // remove undefs. 15006c3fb27SDimitry Andric static std::optional<APInt> getSplatableConstant(const Constant *C, 15106c3fb27SDimitry Andric unsigned SplatBitWidth) { 15206c3fb27SDimitry Andric const Type *Ty = C->getType(); 15306c3fb27SDimitry Andric assert((Ty->getPrimitiveSizeInBits() % SplatBitWidth) == 0 && 15406c3fb27SDimitry Andric "Illegal splat width"); 15506c3fb27SDimitry Andric 15606c3fb27SDimitry Andric if (std::optional<APInt> Bits = extractConstantBits(C)) 15706c3fb27SDimitry Andric if (Bits->isSplat(SplatBitWidth)) 15806c3fb27SDimitry Andric return Bits->trunc(SplatBitWidth); 15906c3fb27SDimitry Andric 16006c3fb27SDimitry Andric // Detect general splats with undefs. 16106c3fb27SDimitry Andric // TODO: Do we need to handle NumEltsBits > SplatBitWidth splitting? 16206c3fb27SDimitry Andric if (auto *CV = dyn_cast<ConstantVector>(C)) { 16306c3fb27SDimitry Andric unsigned NumOps = CV->getNumOperands(); 16406c3fb27SDimitry Andric unsigned NumEltsBits = Ty->getScalarSizeInBits(); 16506c3fb27SDimitry Andric unsigned NumScaleOps = SplatBitWidth / NumEltsBits; 16606c3fb27SDimitry Andric if ((SplatBitWidth % NumEltsBits) == 0) { 16706c3fb27SDimitry Andric // Collect the elements and ensure that within the repeated splat sequence 16806c3fb27SDimitry Andric // they either match or are undef. 16906c3fb27SDimitry Andric SmallVector<Constant *, 16> Sequence(NumScaleOps, nullptr); 17006c3fb27SDimitry Andric for (unsigned Idx = 0; Idx != NumOps; ++Idx) { 17106c3fb27SDimitry Andric if (Constant *Elt = CV->getAggregateElement(Idx)) { 17206c3fb27SDimitry Andric if (isa<UndefValue>(Elt)) 17306c3fb27SDimitry Andric continue; 17406c3fb27SDimitry Andric unsigned SplatIdx = Idx % NumScaleOps; 17506c3fb27SDimitry Andric if (!Sequence[SplatIdx] || Sequence[SplatIdx] == Elt) { 17606c3fb27SDimitry Andric Sequence[SplatIdx] = Elt; 17706c3fb27SDimitry Andric continue; 17806c3fb27SDimitry Andric } 17906c3fb27SDimitry Andric } 18006c3fb27SDimitry Andric return std::nullopt; 18106c3fb27SDimitry Andric } 18206c3fb27SDimitry Andric // Extract the constant bits forming the splat and insert into the bits 18306c3fb27SDimitry Andric // data, leave undef as zero. 18406c3fb27SDimitry Andric APInt SplatBits = APInt::getZero(SplatBitWidth); 18506c3fb27SDimitry Andric for (unsigned I = 0; I != NumScaleOps; ++I) { 18606c3fb27SDimitry Andric if (!Sequence[I]) 18706c3fb27SDimitry Andric continue; 18806c3fb27SDimitry Andric if (std::optional<APInt> Bits = extractConstantBits(Sequence[I])) { 18906c3fb27SDimitry Andric SplatBits.insertBits(*Bits, I * Bits->getBitWidth()); 19006c3fb27SDimitry Andric continue; 19106c3fb27SDimitry Andric } 19206c3fb27SDimitry Andric return std::nullopt; 19306c3fb27SDimitry Andric } 19406c3fb27SDimitry Andric return SplatBits; 19506c3fb27SDimitry Andric } 19606c3fb27SDimitry Andric } 19706c3fb27SDimitry Andric 19806c3fb27SDimitry Andric return std::nullopt; 19906c3fb27SDimitry Andric } 20006c3fb27SDimitry Andric 2017a6dacacSDimitry Andric // Split raw bits into a constant vector of elements of a specific bit width. 2027a6dacacSDimitry Andric // NOTE: We don't always bother converting to scalars if the vector length is 1. 2037a6dacacSDimitry Andric static Constant *rebuildConstant(LLVMContext &Ctx, Type *SclTy, 2047a6dacacSDimitry Andric const APInt &Bits, unsigned NumSclBits) { 2057a6dacacSDimitry Andric unsigned BitWidth = Bits.getBitWidth(); 2067a6dacacSDimitry Andric 2077a6dacacSDimitry Andric if (NumSclBits == 8) { 2087a6dacacSDimitry Andric SmallVector<uint8_t> RawBits; 2097a6dacacSDimitry Andric for (unsigned I = 0; I != BitWidth; I += 8) 2107a6dacacSDimitry Andric RawBits.push_back(Bits.extractBits(8, I).getZExtValue()); 2117a6dacacSDimitry Andric return ConstantDataVector::get(Ctx, RawBits); 2127a6dacacSDimitry Andric } 2137a6dacacSDimitry Andric 2147a6dacacSDimitry Andric if (NumSclBits == 16) { 2157a6dacacSDimitry Andric SmallVector<uint16_t> RawBits; 2167a6dacacSDimitry Andric for (unsigned I = 0; I != BitWidth; I += 16) 2177a6dacacSDimitry Andric RawBits.push_back(Bits.extractBits(16, I).getZExtValue()); 2187a6dacacSDimitry Andric if (SclTy->is16bitFPTy()) 2197a6dacacSDimitry Andric return ConstantDataVector::getFP(SclTy, RawBits); 2207a6dacacSDimitry Andric return ConstantDataVector::get(Ctx, RawBits); 2217a6dacacSDimitry Andric } 2227a6dacacSDimitry Andric 2237a6dacacSDimitry Andric if (NumSclBits == 32) { 2247a6dacacSDimitry Andric SmallVector<uint32_t> RawBits; 2257a6dacacSDimitry Andric for (unsigned I = 0; I != BitWidth; I += 32) 2267a6dacacSDimitry Andric RawBits.push_back(Bits.extractBits(32, I).getZExtValue()); 2277a6dacacSDimitry Andric if (SclTy->isFloatTy()) 2287a6dacacSDimitry Andric return ConstantDataVector::getFP(SclTy, RawBits); 2297a6dacacSDimitry Andric return ConstantDataVector::get(Ctx, RawBits); 2307a6dacacSDimitry Andric } 2317a6dacacSDimitry Andric 2327a6dacacSDimitry Andric assert(NumSclBits == 64 && "Unhandled vector element width"); 2337a6dacacSDimitry Andric 2347a6dacacSDimitry Andric SmallVector<uint64_t> RawBits; 2357a6dacacSDimitry Andric for (unsigned I = 0; I != BitWidth; I += 64) 2367a6dacacSDimitry Andric RawBits.push_back(Bits.extractBits(64, I).getZExtValue()); 2377a6dacacSDimitry Andric if (SclTy->isDoubleTy()) 2387a6dacacSDimitry Andric return ConstantDataVector::getFP(SclTy, RawBits); 2397a6dacacSDimitry Andric return ConstantDataVector::get(Ctx, RawBits); 2407a6dacacSDimitry Andric } 2417a6dacacSDimitry Andric 24206c3fb27SDimitry Andric // Attempt to rebuild a normalized splat vector constant of the requested splat 24306c3fb27SDimitry Andric // width, built up of potentially smaller scalar values. 244*0fca6ea1SDimitry Andric static Constant *rebuildSplatCst(const Constant *C, unsigned /*NumBits*/, 245*0fca6ea1SDimitry Andric unsigned /*NumElts*/, unsigned SplatBitWidth) { 246*0fca6ea1SDimitry Andric // TODO: Truncate to NumBits once ConvertToBroadcastAVX512 support this. 24706c3fb27SDimitry Andric std::optional<APInt> Splat = getSplatableConstant(C, SplatBitWidth); 24806c3fb27SDimitry Andric if (!Splat) 24906c3fb27SDimitry Andric return nullptr; 25006c3fb27SDimitry Andric 25106c3fb27SDimitry Andric // Determine scalar size to use for the constant splat vector, clamping as we 25206c3fb27SDimitry Andric // might have found a splat smaller than the original constant data. 253*0fca6ea1SDimitry Andric Type *SclTy = C->getType()->getScalarType(); 25406c3fb27SDimitry Andric unsigned NumSclBits = SclTy->getPrimitiveSizeInBits(); 25506c3fb27SDimitry Andric NumSclBits = std::min<unsigned>(NumSclBits, SplatBitWidth); 25606c3fb27SDimitry Andric 25706c3fb27SDimitry Andric // Fallback to i64 / double. 2587a6dacacSDimitry Andric NumSclBits = (NumSclBits == 8 || NumSclBits == 16 || NumSclBits == 32) 2597a6dacacSDimitry Andric ? NumSclBits 2607a6dacacSDimitry Andric : 64; 2617a6dacacSDimitry Andric 2627a6dacacSDimitry Andric // Extract per-element bits. 263*0fca6ea1SDimitry Andric return rebuildConstant(C->getContext(), SclTy, *Splat, NumSclBits); 264*0fca6ea1SDimitry Andric } 265*0fca6ea1SDimitry Andric 266*0fca6ea1SDimitry Andric static Constant *rebuildZeroUpperCst(const Constant *C, unsigned NumBits, 267*0fca6ea1SDimitry Andric unsigned /*NumElts*/, 268*0fca6ea1SDimitry Andric unsigned ScalarBitWidth) { 269*0fca6ea1SDimitry Andric Type *SclTy = C->getType()->getScalarType(); 270*0fca6ea1SDimitry Andric unsigned NumSclBits = SclTy->getPrimitiveSizeInBits(); 271*0fca6ea1SDimitry Andric LLVMContext &Ctx = C->getContext(); 272*0fca6ea1SDimitry Andric 273*0fca6ea1SDimitry Andric if (NumBits > ScalarBitWidth) { 274*0fca6ea1SDimitry Andric // Determine if the upper bits are all zero. 275*0fca6ea1SDimitry Andric if (std::optional<APInt> Bits = extractConstantBits(C, NumBits)) { 276*0fca6ea1SDimitry Andric if (Bits->countLeadingZeros() >= (NumBits - ScalarBitWidth)) { 277*0fca6ea1SDimitry Andric // If the original constant was made of smaller elements, try to retain 278*0fca6ea1SDimitry Andric // those types. 279*0fca6ea1SDimitry Andric if (ScalarBitWidth > NumSclBits && (ScalarBitWidth % NumSclBits) == 0) 280*0fca6ea1SDimitry Andric return rebuildConstant(Ctx, SclTy, *Bits, NumSclBits); 281*0fca6ea1SDimitry Andric 282*0fca6ea1SDimitry Andric // Fallback to raw integer bits. 283*0fca6ea1SDimitry Andric APInt RawBits = Bits->zextOrTrunc(ScalarBitWidth); 284*0fca6ea1SDimitry Andric return ConstantInt::get(Ctx, RawBits); 285*0fca6ea1SDimitry Andric } 286*0fca6ea1SDimitry Andric } 287*0fca6ea1SDimitry Andric } 288*0fca6ea1SDimitry Andric 289*0fca6ea1SDimitry Andric return nullptr; 290*0fca6ea1SDimitry Andric } 291*0fca6ea1SDimitry Andric 292*0fca6ea1SDimitry Andric static Constant *rebuildExtCst(const Constant *C, bool IsSExt, 293*0fca6ea1SDimitry Andric unsigned NumBits, unsigned NumElts, 294*0fca6ea1SDimitry Andric unsigned SrcEltBitWidth) { 295*0fca6ea1SDimitry Andric unsigned DstEltBitWidth = NumBits / NumElts; 296*0fca6ea1SDimitry Andric assert((NumBits % NumElts) == 0 && (NumBits % SrcEltBitWidth) == 0 && 297*0fca6ea1SDimitry Andric (DstEltBitWidth % SrcEltBitWidth) == 0 && 298*0fca6ea1SDimitry Andric (DstEltBitWidth > SrcEltBitWidth) && "Illegal extension width"); 299*0fca6ea1SDimitry Andric 300*0fca6ea1SDimitry Andric if (std::optional<APInt> Bits = extractConstantBits(C, NumBits)) { 301*0fca6ea1SDimitry Andric assert((Bits->getBitWidth() / DstEltBitWidth) == NumElts && 302*0fca6ea1SDimitry Andric (Bits->getBitWidth() % DstEltBitWidth) == 0 && 303*0fca6ea1SDimitry Andric "Unexpected constant extension"); 304*0fca6ea1SDimitry Andric 305*0fca6ea1SDimitry Andric // Ensure every vector element can be represented by the src bitwidth. 306*0fca6ea1SDimitry Andric APInt TruncBits = APInt::getZero(NumElts * SrcEltBitWidth); 307*0fca6ea1SDimitry Andric for (unsigned I = 0; I != NumElts; ++I) { 308*0fca6ea1SDimitry Andric APInt Elt = Bits->extractBits(DstEltBitWidth, I * DstEltBitWidth); 309*0fca6ea1SDimitry Andric if ((IsSExt && Elt.getSignificantBits() > SrcEltBitWidth) || 310*0fca6ea1SDimitry Andric (!IsSExt && Elt.getActiveBits() > SrcEltBitWidth)) 311*0fca6ea1SDimitry Andric return nullptr; 312*0fca6ea1SDimitry Andric TruncBits.insertBits(Elt.trunc(SrcEltBitWidth), I * SrcEltBitWidth); 313*0fca6ea1SDimitry Andric } 314*0fca6ea1SDimitry Andric 315*0fca6ea1SDimitry Andric Type *Ty = C->getType(); 316*0fca6ea1SDimitry Andric return rebuildConstant(Ty->getContext(), Ty->getScalarType(), TruncBits, 317*0fca6ea1SDimitry Andric SrcEltBitWidth); 318*0fca6ea1SDimitry Andric } 319*0fca6ea1SDimitry Andric 320*0fca6ea1SDimitry Andric return nullptr; 321*0fca6ea1SDimitry Andric } 322*0fca6ea1SDimitry Andric static Constant *rebuildSExtCst(const Constant *C, unsigned NumBits, 323*0fca6ea1SDimitry Andric unsigned NumElts, unsigned SrcEltBitWidth) { 324*0fca6ea1SDimitry Andric return rebuildExtCst(C, true, NumBits, NumElts, SrcEltBitWidth); 325*0fca6ea1SDimitry Andric } 326*0fca6ea1SDimitry Andric static Constant *rebuildZExtCst(const Constant *C, unsigned NumBits, 327*0fca6ea1SDimitry Andric unsigned NumElts, unsigned SrcEltBitWidth) { 328*0fca6ea1SDimitry Andric return rebuildExtCst(C, false, NumBits, NumElts, SrcEltBitWidth); 32906c3fb27SDimitry Andric } 33006c3fb27SDimitry Andric 33106c3fb27SDimitry Andric bool X86FixupVectorConstantsPass::processInstruction(MachineFunction &MF, 33206c3fb27SDimitry Andric MachineBasicBlock &MBB, 33306c3fb27SDimitry Andric MachineInstr &MI) { 33406c3fb27SDimitry Andric unsigned Opc = MI.getOpcode(); 33506c3fb27SDimitry Andric MachineConstantPool *CP = MI.getParent()->getParent()->getConstantPool(); 336*0fca6ea1SDimitry Andric bool HasSSE41 = ST->hasSSE41(); 3375f757f3fSDimitry Andric bool HasAVX2 = ST->hasAVX2(); 33806c3fb27SDimitry Andric bool HasDQI = ST->hasDQI(); 33906c3fb27SDimitry Andric bool HasBWI = ST->hasBWI(); 3405f757f3fSDimitry Andric bool HasVLX = ST->hasVLX(); 34106c3fb27SDimitry Andric 342*0fca6ea1SDimitry Andric struct FixupEntry { 343*0fca6ea1SDimitry Andric int Op; 344*0fca6ea1SDimitry Andric int NumCstElts; 345*0fca6ea1SDimitry Andric int MemBitWidth; 346*0fca6ea1SDimitry Andric std::function<Constant *(const Constant *, unsigned, unsigned, unsigned)> 347*0fca6ea1SDimitry Andric RebuildConstant; 348*0fca6ea1SDimitry Andric }; 349*0fca6ea1SDimitry Andric auto FixupConstant = [&](ArrayRef<FixupEntry> Fixups, unsigned RegBitWidth, 35006c3fb27SDimitry Andric unsigned OperandNo) { 351*0fca6ea1SDimitry Andric #ifdef EXPENSIVE_CHECKS 352*0fca6ea1SDimitry Andric assert(llvm::is_sorted(Fixups, 353*0fca6ea1SDimitry Andric [](const FixupEntry &A, const FixupEntry &B) { 354*0fca6ea1SDimitry Andric return (A.NumCstElts * A.MemBitWidth) < 355*0fca6ea1SDimitry Andric (B.NumCstElts * B.MemBitWidth); 356*0fca6ea1SDimitry Andric }) && 357*0fca6ea1SDimitry Andric "Constant fixup table not sorted in ascending constant size"); 358*0fca6ea1SDimitry Andric #endif 35906c3fb27SDimitry Andric assert(MI.getNumOperands() >= (OperandNo + X86::AddrNumOperands) && 36006c3fb27SDimitry Andric "Unexpected number of operands!"); 3617a6dacacSDimitry Andric if (auto *C = X86::getConstantFromPool(MI, OperandNo)) { 362*0fca6ea1SDimitry Andric RegBitWidth = 363*0fca6ea1SDimitry Andric RegBitWidth ? RegBitWidth : C->getType()->getPrimitiveSizeInBits(); 364*0fca6ea1SDimitry Andric for (const FixupEntry &Fixup : Fixups) { 365*0fca6ea1SDimitry Andric if (Fixup.Op) { 366*0fca6ea1SDimitry Andric // Construct a suitable constant and adjust the MI to use the new 367*0fca6ea1SDimitry Andric // constant pool entry. 368*0fca6ea1SDimitry Andric if (Constant *NewCst = Fixup.RebuildConstant( 369*0fca6ea1SDimitry Andric C, RegBitWidth, Fixup.NumCstElts, Fixup.MemBitWidth)) { 37006c3fb27SDimitry Andric unsigned NewCPI = 371*0fca6ea1SDimitry Andric CP->getConstantPoolIndex(NewCst, Align(Fixup.MemBitWidth / 8)); 372*0fca6ea1SDimitry Andric MI.setDesc(TII->get(Fixup.Op)); 3737a6dacacSDimitry Andric MI.getOperand(OperandNo + X86::AddrDisp).setIndex(NewCPI); 37406c3fb27SDimitry Andric return true; 37506c3fb27SDimitry Andric } 37606c3fb27SDimitry Andric } 37706c3fb27SDimitry Andric } 37806c3fb27SDimitry Andric } 37906c3fb27SDimitry Andric return false; 38006c3fb27SDimitry Andric }; 38106c3fb27SDimitry Andric 382*0fca6ea1SDimitry Andric // Attempt to detect a suitable vzload/broadcast/vextload from increasing 383*0fca6ea1SDimitry Andric // constant bitwidths. Prefer vzload/broadcast/vextload for same bitwidth: 384*0fca6ea1SDimitry Andric // - vzload shouldn't ever need a shuffle port to zero the upper elements and 385*0fca6ea1SDimitry Andric // the fp/int domain versions are equally available so we don't introduce a 386*0fca6ea1SDimitry Andric // domain crossing penalty. 387*0fca6ea1SDimitry Andric // - broadcast sometimes need a shuffle port (especially for 8/16-bit 388*0fca6ea1SDimitry Andric // variants), AVX1 only has fp domain broadcasts but AVX2+ have good fp/int 389*0fca6ea1SDimitry Andric // domain equivalents. 390*0fca6ea1SDimitry Andric // - vextload always needs a shuffle port and is only ever int domain. 39106c3fb27SDimitry Andric switch (Opc) { 39206c3fb27SDimitry Andric /* FP Loads */ 39306c3fb27SDimitry Andric case X86::MOVAPDrm: 39406c3fb27SDimitry Andric case X86::MOVAPSrm: 39506c3fb27SDimitry Andric case X86::MOVUPDrm: 39606c3fb27SDimitry Andric case X86::MOVUPSrm: 39706c3fb27SDimitry Andric // TODO: SSE3 MOVDDUP Handling 398*0fca6ea1SDimitry Andric return FixupConstant({{X86::MOVSSrm, 1, 32, rebuildZeroUpperCst}, 399*0fca6ea1SDimitry Andric {X86::MOVSDrm, 1, 64, rebuildZeroUpperCst}}, 400*0fca6ea1SDimitry Andric 128, 1); 40106c3fb27SDimitry Andric case X86::VMOVAPDrm: 40206c3fb27SDimitry Andric case X86::VMOVAPSrm: 40306c3fb27SDimitry Andric case X86::VMOVUPDrm: 40406c3fb27SDimitry Andric case X86::VMOVUPSrm: 405*0fca6ea1SDimitry Andric return FixupConstant({{X86::VMOVSSrm, 1, 32, rebuildZeroUpperCst}, 406*0fca6ea1SDimitry Andric {X86::VBROADCASTSSrm, 1, 32, rebuildSplatCst}, 407*0fca6ea1SDimitry Andric {X86::VMOVSDrm, 1, 64, rebuildZeroUpperCst}, 408*0fca6ea1SDimitry Andric {X86::VMOVDDUPrm, 1, 64, rebuildSplatCst}}, 409*0fca6ea1SDimitry Andric 128, 1); 41006c3fb27SDimitry Andric case X86::VMOVAPDYrm: 41106c3fb27SDimitry Andric case X86::VMOVAPSYrm: 41206c3fb27SDimitry Andric case X86::VMOVUPDYrm: 41306c3fb27SDimitry Andric case X86::VMOVUPSYrm: 414*0fca6ea1SDimitry Andric return FixupConstant({{X86::VBROADCASTSSYrm, 1, 32, rebuildSplatCst}, 415*0fca6ea1SDimitry Andric {X86::VBROADCASTSDYrm, 1, 64, rebuildSplatCst}, 416*0fca6ea1SDimitry Andric {X86::VBROADCASTF128rm, 1, 128, rebuildSplatCst}}, 417*0fca6ea1SDimitry Andric 256, 1); 41806c3fb27SDimitry Andric case X86::VMOVAPDZ128rm: 41906c3fb27SDimitry Andric case X86::VMOVAPSZ128rm: 42006c3fb27SDimitry Andric case X86::VMOVUPDZ128rm: 42106c3fb27SDimitry Andric case X86::VMOVUPSZ128rm: 422*0fca6ea1SDimitry Andric return FixupConstant({{X86::VMOVSSZrm, 1, 32, rebuildZeroUpperCst}, 423*0fca6ea1SDimitry Andric {X86::VBROADCASTSSZ128rm, 1, 32, rebuildSplatCst}, 424*0fca6ea1SDimitry Andric {X86::VMOVSDZrm, 1, 64, rebuildZeroUpperCst}, 425*0fca6ea1SDimitry Andric {X86::VMOVDDUPZ128rm, 1, 64, rebuildSplatCst}}, 426*0fca6ea1SDimitry Andric 128, 1); 42706c3fb27SDimitry Andric case X86::VMOVAPDZ256rm: 42806c3fb27SDimitry Andric case X86::VMOVAPSZ256rm: 42906c3fb27SDimitry Andric case X86::VMOVUPDZ256rm: 43006c3fb27SDimitry Andric case X86::VMOVUPSZ256rm: 431*0fca6ea1SDimitry Andric return FixupConstant( 432*0fca6ea1SDimitry Andric {{X86::VBROADCASTSSZ256rm, 1, 32, rebuildSplatCst}, 433*0fca6ea1SDimitry Andric {X86::VBROADCASTSDZ256rm, 1, 64, rebuildSplatCst}, 434*0fca6ea1SDimitry Andric {X86::VBROADCASTF32X4Z256rm, 1, 128, rebuildSplatCst}}, 435*0fca6ea1SDimitry Andric 256, 1); 43606c3fb27SDimitry Andric case X86::VMOVAPDZrm: 43706c3fb27SDimitry Andric case X86::VMOVAPSZrm: 43806c3fb27SDimitry Andric case X86::VMOVUPDZrm: 43906c3fb27SDimitry Andric case X86::VMOVUPSZrm: 440*0fca6ea1SDimitry Andric return FixupConstant({{X86::VBROADCASTSSZrm, 1, 32, rebuildSplatCst}, 441*0fca6ea1SDimitry Andric {X86::VBROADCASTSDZrm, 1, 64, rebuildSplatCst}, 442*0fca6ea1SDimitry Andric {X86::VBROADCASTF32X4rm, 1, 128, rebuildSplatCst}, 443*0fca6ea1SDimitry Andric {X86::VBROADCASTF64X4rm, 1, 256, rebuildSplatCst}}, 444*0fca6ea1SDimitry Andric 512, 1); 44506c3fb27SDimitry Andric /* Integer Loads */ 446*0fca6ea1SDimitry Andric case X86::MOVDQArm: 447*0fca6ea1SDimitry Andric case X86::MOVDQUrm: { 448*0fca6ea1SDimitry Andric FixupEntry Fixups[] = { 449*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVSXBQrm : 0, 2, 8, rebuildSExtCst}, 450*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVZXBQrm : 0, 2, 8, rebuildZExtCst}, 451*0fca6ea1SDimitry Andric {X86::MOVDI2PDIrm, 1, 32, rebuildZeroUpperCst}, 452*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVSXBDrm : 0, 4, 8, rebuildSExtCst}, 453*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVZXBDrm : 0, 4, 8, rebuildZExtCst}, 454*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVSXWQrm : 0, 2, 16, rebuildSExtCst}, 455*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVZXWQrm : 0, 2, 16, rebuildZExtCst}, 456*0fca6ea1SDimitry Andric {X86::MOVQI2PQIrm, 1, 64, rebuildZeroUpperCst}, 457*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVSXBWrm : 0, 8, 8, rebuildSExtCst}, 458*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVZXBWrm : 0, 8, 8, rebuildZExtCst}, 459*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVSXWDrm : 0, 4, 16, rebuildSExtCst}, 460*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVZXWDrm : 0, 4, 16, rebuildZExtCst}, 461*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVSXDQrm : 0, 2, 32, rebuildSExtCst}, 462*0fca6ea1SDimitry Andric {HasSSE41 ? X86::PMOVZXDQrm : 0, 2, 32, rebuildZExtCst}}; 463*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 128, 1); 464*0fca6ea1SDimitry Andric } 46506c3fb27SDimitry Andric case X86::VMOVDQArm: 466*0fca6ea1SDimitry Andric case X86::VMOVDQUrm: { 467*0fca6ea1SDimitry Andric FixupEntry Fixups[] = { 468*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTBrm : 0, 1, 8, rebuildSplatCst}, 469*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTWrm : 0, 1, 16, rebuildSplatCst}, 470*0fca6ea1SDimitry Andric {X86::VPMOVSXBQrm, 2, 8, rebuildSExtCst}, 471*0fca6ea1SDimitry Andric {X86::VPMOVZXBQrm, 2, 8, rebuildZExtCst}, 472*0fca6ea1SDimitry Andric {X86::VMOVDI2PDIrm, 1, 32, rebuildZeroUpperCst}, 473*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTDrm : X86::VBROADCASTSSrm, 1, 32, 474*0fca6ea1SDimitry Andric rebuildSplatCst}, 475*0fca6ea1SDimitry Andric {X86::VPMOVSXBDrm, 4, 8, rebuildSExtCst}, 476*0fca6ea1SDimitry Andric {X86::VPMOVZXBDrm, 4, 8, rebuildZExtCst}, 477*0fca6ea1SDimitry Andric {X86::VPMOVSXWQrm, 2, 16, rebuildSExtCst}, 478*0fca6ea1SDimitry Andric {X86::VPMOVZXWQrm, 2, 16, rebuildZExtCst}, 479*0fca6ea1SDimitry Andric {X86::VMOVQI2PQIrm, 1, 64, rebuildZeroUpperCst}, 480*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTQrm : X86::VMOVDDUPrm, 1, 64, 481*0fca6ea1SDimitry Andric rebuildSplatCst}, 482*0fca6ea1SDimitry Andric {X86::VPMOVSXBWrm, 8, 8, rebuildSExtCst}, 483*0fca6ea1SDimitry Andric {X86::VPMOVZXBWrm, 8, 8, rebuildZExtCst}, 484*0fca6ea1SDimitry Andric {X86::VPMOVSXWDrm, 4, 16, rebuildSExtCst}, 485*0fca6ea1SDimitry Andric {X86::VPMOVZXWDrm, 4, 16, rebuildZExtCst}, 486*0fca6ea1SDimitry Andric {X86::VPMOVSXDQrm, 2, 32, rebuildSExtCst}, 487*0fca6ea1SDimitry Andric {X86::VPMOVZXDQrm, 2, 32, rebuildZExtCst}}; 488*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 128, 1); 489*0fca6ea1SDimitry Andric } 49006c3fb27SDimitry Andric case X86::VMOVDQAYrm: 491*0fca6ea1SDimitry Andric case X86::VMOVDQUYrm: { 492*0fca6ea1SDimitry Andric FixupEntry Fixups[] = { 493*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTBYrm : 0, 1, 8, rebuildSplatCst}, 494*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTWYrm : 0, 1, 16, rebuildSplatCst}, 495*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTDYrm : X86::VBROADCASTSSYrm, 1, 32, 496*0fca6ea1SDimitry Andric rebuildSplatCst}, 497*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVSXBQYrm : 0, 4, 8, rebuildSExtCst}, 498*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVZXBQYrm : 0, 4, 8, rebuildZExtCst}, 499*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPBROADCASTQYrm : X86::VBROADCASTSDYrm, 1, 64, 500*0fca6ea1SDimitry Andric rebuildSplatCst}, 501*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVSXBDYrm : 0, 8, 8, rebuildSExtCst}, 502*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVZXBDYrm : 0, 8, 8, rebuildZExtCst}, 503*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVSXWQYrm : 0, 4, 16, rebuildSExtCst}, 504*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVZXWQYrm : 0, 4, 16, rebuildZExtCst}, 505*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VBROADCASTI128rm : X86::VBROADCASTF128rm, 1, 128, 506*0fca6ea1SDimitry Andric rebuildSplatCst}, 507*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVSXBWYrm : 0, 16, 8, rebuildSExtCst}, 508*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVZXBWYrm : 0, 16, 8, rebuildZExtCst}, 509*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVSXWDYrm : 0, 8, 16, rebuildSExtCst}, 510*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVZXWDYrm : 0, 8, 16, rebuildZExtCst}, 511*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVSXDQYrm : 0, 4, 32, rebuildSExtCst}, 512*0fca6ea1SDimitry Andric {HasAVX2 ? X86::VPMOVZXDQYrm : 0, 4, 32, rebuildZExtCst}}; 513*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 256, 1); 514*0fca6ea1SDimitry Andric } 51506c3fb27SDimitry Andric case X86::VMOVDQA32Z128rm: 51606c3fb27SDimitry Andric case X86::VMOVDQA64Z128rm: 51706c3fb27SDimitry Andric case X86::VMOVDQU32Z128rm: 518*0fca6ea1SDimitry Andric case X86::VMOVDQU64Z128rm: { 519*0fca6ea1SDimitry Andric FixupEntry Fixups[] = { 520*0fca6ea1SDimitry Andric {HasBWI ? X86::VPBROADCASTBZ128rm : 0, 1, 8, rebuildSplatCst}, 521*0fca6ea1SDimitry Andric {HasBWI ? X86::VPBROADCASTWZ128rm : 0, 1, 16, rebuildSplatCst}, 522*0fca6ea1SDimitry Andric {X86::VPMOVSXBQZ128rm, 2, 8, rebuildSExtCst}, 523*0fca6ea1SDimitry Andric {X86::VPMOVZXBQZ128rm, 2, 8, rebuildZExtCst}, 524*0fca6ea1SDimitry Andric {X86::VMOVDI2PDIZrm, 1, 32, rebuildZeroUpperCst}, 525*0fca6ea1SDimitry Andric {X86::VPBROADCASTDZ128rm, 1, 32, rebuildSplatCst}, 526*0fca6ea1SDimitry Andric {X86::VPMOVSXBDZ128rm, 4, 8, rebuildSExtCst}, 527*0fca6ea1SDimitry Andric {X86::VPMOVZXBDZ128rm, 4, 8, rebuildZExtCst}, 528*0fca6ea1SDimitry Andric {X86::VPMOVSXWQZ128rm, 2, 16, rebuildSExtCst}, 529*0fca6ea1SDimitry Andric {X86::VPMOVZXWQZ128rm, 2, 16, rebuildZExtCst}, 530*0fca6ea1SDimitry Andric {X86::VMOVQI2PQIZrm, 1, 64, rebuildZeroUpperCst}, 531*0fca6ea1SDimitry Andric {X86::VPBROADCASTQZ128rm, 1, 64, rebuildSplatCst}, 532*0fca6ea1SDimitry Andric {HasBWI ? X86::VPMOVSXBWZ128rm : 0, 8, 8, rebuildSExtCst}, 533*0fca6ea1SDimitry Andric {HasBWI ? X86::VPMOVZXBWZ128rm : 0, 8, 8, rebuildZExtCst}, 534*0fca6ea1SDimitry Andric {X86::VPMOVSXWDZ128rm, 4, 16, rebuildSExtCst}, 535*0fca6ea1SDimitry Andric {X86::VPMOVZXWDZ128rm, 4, 16, rebuildZExtCst}, 536*0fca6ea1SDimitry Andric {X86::VPMOVSXDQZ128rm, 2, 32, rebuildSExtCst}, 537*0fca6ea1SDimitry Andric {X86::VPMOVZXDQZ128rm, 2, 32, rebuildZExtCst}}; 538*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 128, 1); 539*0fca6ea1SDimitry Andric } 54006c3fb27SDimitry Andric case X86::VMOVDQA32Z256rm: 54106c3fb27SDimitry Andric case X86::VMOVDQA64Z256rm: 54206c3fb27SDimitry Andric case X86::VMOVDQU32Z256rm: 543*0fca6ea1SDimitry Andric case X86::VMOVDQU64Z256rm: { 544*0fca6ea1SDimitry Andric FixupEntry Fixups[] = { 545*0fca6ea1SDimitry Andric {HasBWI ? X86::VPBROADCASTBZ256rm : 0, 1, 8, rebuildSplatCst}, 546*0fca6ea1SDimitry Andric {HasBWI ? X86::VPBROADCASTWZ256rm : 0, 1, 16, rebuildSplatCst}, 547*0fca6ea1SDimitry Andric {X86::VPBROADCASTDZ256rm, 1, 32, rebuildSplatCst}, 548*0fca6ea1SDimitry Andric {X86::VPMOVSXBQZ256rm, 4, 8, rebuildSExtCst}, 549*0fca6ea1SDimitry Andric {X86::VPMOVZXBQZ256rm, 4, 8, rebuildZExtCst}, 550*0fca6ea1SDimitry Andric {X86::VPBROADCASTQZ256rm, 1, 64, rebuildSplatCst}, 551*0fca6ea1SDimitry Andric {X86::VPMOVSXBDZ256rm, 8, 8, rebuildSExtCst}, 552*0fca6ea1SDimitry Andric {X86::VPMOVZXBDZ256rm, 8, 8, rebuildZExtCst}, 553*0fca6ea1SDimitry Andric {X86::VPMOVSXWQZ256rm, 4, 16, rebuildSExtCst}, 554*0fca6ea1SDimitry Andric {X86::VPMOVZXWQZ256rm, 4, 16, rebuildZExtCst}, 555*0fca6ea1SDimitry Andric {X86::VBROADCASTI32X4Z256rm, 1, 128, rebuildSplatCst}, 556*0fca6ea1SDimitry Andric {HasBWI ? X86::VPMOVSXBWZ256rm : 0, 16, 8, rebuildSExtCst}, 557*0fca6ea1SDimitry Andric {HasBWI ? X86::VPMOVZXBWZ256rm : 0, 16, 8, rebuildZExtCst}, 558*0fca6ea1SDimitry Andric {X86::VPMOVSXWDZ256rm, 8, 16, rebuildSExtCst}, 559*0fca6ea1SDimitry Andric {X86::VPMOVZXWDZ256rm, 8, 16, rebuildZExtCst}, 560*0fca6ea1SDimitry Andric {X86::VPMOVSXDQZ256rm, 4, 32, rebuildSExtCst}, 561*0fca6ea1SDimitry Andric {X86::VPMOVZXDQZ256rm, 4, 32, rebuildZExtCst}}; 562*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 256, 1); 563*0fca6ea1SDimitry Andric } 56406c3fb27SDimitry Andric case X86::VMOVDQA32Zrm: 56506c3fb27SDimitry Andric case X86::VMOVDQA64Zrm: 56606c3fb27SDimitry Andric case X86::VMOVDQU32Zrm: 567*0fca6ea1SDimitry Andric case X86::VMOVDQU64Zrm: { 568*0fca6ea1SDimitry Andric FixupEntry Fixups[] = { 569*0fca6ea1SDimitry Andric {HasBWI ? X86::VPBROADCASTBZrm : 0, 1, 8, rebuildSplatCst}, 570*0fca6ea1SDimitry Andric {HasBWI ? X86::VPBROADCASTWZrm : 0, 1, 16, rebuildSplatCst}, 571*0fca6ea1SDimitry Andric {X86::VPBROADCASTDZrm, 1, 32, rebuildSplatCst}, 572*0fca6ea1SDimitry Andric {X86::VPBROADCASTQZrm, 1, 64, rebuildSplatCst}, 573*0fca6ea1SDimitry Andric {X86::VPMOVSXBQZrm, 8, 8, rebuildSExtCst}, 574*0fca6ea1SDimitry Andric {X86::VPMOVZXBQZrm, 8, 8, rebuildZExtCst}, 575*0fca6ea1SDimitry Andric {X86::VBROADCASTI32X4rm, 1, 128, rebuildSplatCst}, 576*0fca6ea1SDimitry Andric {X86::VPMOVSXBDZrm, 16, 8, rebuildSExtCst}, 577*0fca6ea1SDimitry Andric {X86::VPMOVZXBDZrm, 16, 8, rebuildZExtCst}, 578*0fca6ea1SDimitry Andric {X86::VPMOVSXWQZrm, 8, 16, rebuildSExtCst}, 579*0fca6ea1SDimitry Andric {X86::VPMOVZXWQZrm, 8, 16, rebuildZExtCst}, 580*0fca6ea1SDimitry Andric {X86::VBROADCASTI64X4rm, 1, 256, rebuildSplatCst}, 581*0fca6ea1SDimitry Andric {HasBWI ? X86::VPMOVSXBWZrm : 0, 32, 8, rebuildSExtCst}, 582*0fca6ea1SDimitry Andric {HasBWI ? X86::VPMOVZXBWZrm : 0, 32, 8, rebuildZExtCst}, 583*0fca6ea1SDimitry Andric {X86::VPMOVSXWDZrm, 16, 16, rebuildSExtCst}, 584*0fca6ea1SDimitry Andric {X86::VPMOVZXWDZrm, 16, 16, rebuildZExtCst}, 585*0fca6ea1SDimitry Andric {X86::VPMOVSXDQZrm, 8, 32, rebuildSExtCst}, 586*0fca6ea1SDimitry Andric {X86::VPMOVZXDQZrm, 8, 32, rebuildZExtCst}}; 587*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 512, 1); 588*0fca6ea1SDimitry Andric } 58906c3fb27SDimitry Andric } 59006c3fb27SDimitry Andric 5915f757f3fSDimitry Andric auto ConvertToBroadcastAVX512 = [&](unsigned OpSrc32, unsigned OpSrc64) { 59206c3fb27SDimitry Andric unsigned OpBcst32 = 0, OpBcst64 = 0; 59306c3fb27SDimitry Andric unsigned OpNoBcst32 = 0, OpNoBcst64 = 0; 5945f757f3fSDimitry Andric if (OpSrc32) { 5955f757f3fSDimitry Andric if (const X86FoldTableEntry *Mem2Bcst = 596*0fca6ea1SDimitry Andric llvm::lookupBroadcastFoldTableBySize(OpSrc32, 32)) { 59706c3fb27SDimitry Andric OpBcst32 = Mem2Bcst->DstOp; 59806c3fb27SDimitry Andric OpNoBcst32 = Mem2Bcst->Flags & TB_INDEX_MASK; 59906c3fb27SDimitry Andric } 6005f757f3fSDimitry Andric } 6015f757f3fSDimitry Andric if (OpSrc64) { 6025f757f3fSDimitry Andric if (const X86FoldTableEntry *Mem2Bcst = 603*0fca6ea1SDimitry Andric llvm::lookupBroadcastFoldTableBySize(OpSrc64, 64)) { 60406c3fb27SDimitry Andric OpBcst64 = Mem2Bcst->DstOp; 60506c3fb27SDimitry Andric OpNoBcst64 = Mem2Bcst->Flags & TB_INDEX_MASK; 60606c3fb27SDimitry Andric } 6075f757f3fSDimitry Andric } 60806c3fb27SDimitry Andric assert(((OpBcst32 == 0) || (OpBcst64 == 0) || (OpNoBcst32 == OpNoBcst64)) && 60906c3fb27SDimitry Andric "OperandNo mismatch"); 61006c3fb27SDimitry Andric 61106c3fb27SDimitry Andric if (OpBcst32 || OpBcst64) { 61206c3fb27SDimitry Andric unsigned OpNo = OpBcst32 == 0 ? OpNoBcst64 : OpNoBcst32; 613*0fca6ea1SDimitry Andric FixupEntry Fixups[] = {{(int)OpBcst32, 32, 32, rebuildSplatCst}, 614*0fca6ea1SDimitry Andric {(int)OpBcst64, 64, 64, rebuildSplatCst}}; 615*0fca6ea1SDimitry Andric // TODO: Add support for RegBitWidth, but currently rebuildSplatCst 616*0fca6ea1SDimitry Andric // doesn't require it (defaults to Constant::getPrimitiveSizeInBits). 617*0fca6ea1SDimitry Andric return FixupConstant(Fixups, 0, OpNo); 61806c3fb27SDimitry Andric } 6195f757f3fSDimitry Andric return false; 6205f757f3fSDimitry Andric }; 6215f757f3fSDimitry Andric 6225f757f3fSDimitry Andric // Attempt to find a AVX512 mapping from a full width memory-fold instruction 6235f757f3fSDimitry Andric // to a broadcast-fold instruction variant. 6245f757f3fSDimitry Andric if ((MI.getDesc().TSFlags & X86II::EncodingMask) == X86II::EVEX) 6255f757f3fSDimitry Andric return ConvertToBroadcastAVX512(Opc, Opc); 6265f757f3fSDimitry Andric 6275f757f3fSDimitry Andric // Reverse the X86InstrInfo::setExecutionDomainCustom EVEX->VEX logic 6285f757f3fSDimitry Andric // conversion to see if we can convert to a broadcasted (integer) logic op. 6295f757f3fSDimitry Andric if (HasVLX && !HasDQI) { 6305f757f3fSDimitry Andric unsigned OpSrc32 = 0, OpSrc64 = 0; 6315f757f3fSDimitry Andric switch (Opc) { 6325f757f3fSDimitry Andric case X86::VANDPDrm: 6335f757f3fSDimitry Andric case X86::VANDPSrm: 6345f757f3fSDimitry Andric case X86::VPANDrm: 6355f757f3fSDimitry Andric OpSrc32 = X86 ::VPANDDZ128rm; 6365f757f3fSDimitry Andric OpSrc64 = X86 ::VPANDQZ128rm; 6375f757f3fSDimitry Andric break; 6385f757f3fSDimitry Andric case X86::VANDPDYrm: 6395f757f3fSDimitry Andric case X86::VANDPSYrm: 6405f757f3fSDimitry Andric case X86::VPANDYrm: 6415f757f3fSDimitry Andric OpSrc32 = X86 ::VPANDDZ256rm; 6425f757f3fSDimitry Andric OpSrc64 = X86 ::VPANDQZ256rm; 6435f757f3fSDimitry Andric break; 6445f757f3fSDimitry Andric case X86::VANDNPDrm: 6455f757f3fSDimitry Andric case X86::VANDNPSrm: 6465f757f3fSDimitry Andric case X86::VPANDNrm: 6475f757f3fSDimitry Andric OpSrc32 = X86 ::VPANDNDZ128rm; 6485f757f3fSDimitry Andric OpSrc64 = X86 ::VPANDNQZ128rm; 6495f757f3fSDimitry Andric break; 6505f757f3fSDimitry Andric case X86::VANDNPDYrm: 6515f757f3fSDimitry Andric case X86::VANDNPSYrm: 6525f757f3fSDimitry Andric case X86::VPANDNYrm: 6535f757f3fSDimitry Andric OpSrc32 = X86 ::VPANDNDZ256rm; 6545f757f3fSDimitry Andric OpSrc64 = X86 ::VPANDNQZ256rm; 6555f757f3fSDimitry Andric break; 6565f757f3fSDimitry Andric case X86::VORPDrm: 6575f757f3fSDimitry Andric case X86::VORPSrm: 6585f757f3fSDimitry Andric case X86::VPORrm: 6595f757f3fSDimitry Andric OpSrc32 = X86 ::VPORDZ128rm; 6605f757f3fSDimitry Andric OpSrc64 = X86 ::VPORQZ128rm; 6615f757f3fSDimitry Andric break; 6625f757f3fSDimitry Andric case X86::VORPDYrm: 6635f757f3fSDimitry Andric case X86::VORPSYrm: 6645f757f3fSDimitry Andric case X86::VPORYrm: 6655f757f3fSDimitry Andric OpSrc32 = X86 ::VPORDZ256rm; 6665f757f3fSDimitry Andric OpSrc64 = X86 ::VPORQZ256rm; 6675f757f3fSDimitry Andric break; 6685f757f3fSDimitry Andric case X86::VXORPDrm: 6695f757f3fSDimitry Andric case X86::VXORPSrm: 6705f757f3fSDimitry Andric case X86::VPXORrm: 6715f757f3fSDimitry Andric OpSrc32 = X86 ::VPXORDZ128rm; 6725f757f3fSDimitry Andric OpSrc64 = X86 ::VPXORQZ128rm; 6735f757f3fSDimitry Andric break; 6745f757f3fSDimitry Andric case X86::VXORPDYrm: 6755f757f3fSDimitry Andric case X86::VXORPSYrm: 6765f757f3fSDimitry Andric case X86::VPXORYrm: 6775f757f3fSDimitry Andric OpSrc32 = X86 ::VPXORDZ256rm; 6785f757f3fSDimitry Andric OpSrc64 = X86 ::VPXORQZ256rm; 6795f757f3fSDimitry Andric break; 6805f757f3fSDimitry Andric } 6815f757f3fSDimitry Andric if (OpSrc32 || OpSrc64) 6825f757f3fSDimitry Andric return ConvertToBroadcastAVX512(OpSrc32, OpSrc64); 68306c3fb27SDimitry Andric } 68406c3fb27SDimitry Andric 68506c3fb27SDimitry Andric return false; 68606c3fb27SDimitry Andric } 68706c3fb27SDimitry Andric 68806c3fb27SDimitry Andric bool X86FixupVectorConstantsPass::runOnMachineFunction(MachineFunction &MF) { 68906c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "Start X86FixupVectorConstants\n";); 69006c3fb27SDimitry Andric bool Changed = false; 69106c3fb27SDimitry Andric ST = &MF.getSubtarget<X86Subtarget>(); 69206c3fb27SDimitry Andric TII = ST->getInstrInfo(); 69306c3fb27SDimitry Andric SM = &ST->getSchedModel(); 69406c3fb27SDimitry Andric 69506c3fb27SDimitry Andric for (MachineBasicBlock &MBB : MF) { 69606c3fb27SDimitry Andric for (MachineInstr &MI : MBB) { 69706c3fb27SDimitry Andric if (processInstruction(MF, MBB, MI)) { 69806c3fb27SDimitry Andric ++NumInstChanges; 69906c3fb27SDimitry Andric Changed = true; 70006c3fb27SDimitry Andric } 70106c3fb27SDimitry Andric } 70206c3fb27SDimitry Andric } 70306c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "End X86FixupVectorConstants\n";); 70406c3fb27SDimitry Andric return Changed; 70506c3fb27SDimitry Andric } 706