1e8d8bef9SDimitry Andric //===-- X86InstCombineIntrinsic.cpp - X86 specific InstCombine pass -------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric /// \file 9e8d8bef9SDimitry Andric /// This file implements a TargetTransformInfo analysis pass specific to the 10e8d8bef9SDimitry Andric /// X86 target machine. It uses the target's detailed information to provide 11e8d8bef9SDimitry Andric /// more precise answers to certain TTI queries, while letting the target 12e8d8bef9SDimitry Andric /// independent and default TTI implementations handle the rest. 13e8d8bef9SDimitry Andric /// 14e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 15e8d8bef9SDimitry Andric 16e8d8bef9SDimitry Andric #include "X86TargetTransformInfo.h" 17e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 18e8d8bef9SDimitry Andric #include "llvm/IR/IntrinsicsX86.h" 19e8d8bef9SDimitry Andric #include "llvm/Support/KnownBits.h" 20e8d8bef9SDimitry Andric #include "llvm/Transforms/InstCombine/InstCombiner.h" 21e8d8bef9SDimitry Andric 22e8d8bef9SDimitry Andric using namespace llvm; 23e8d8bef9SDimitry Andric 24e8d8bef9SDimitry Andric #define DEBUG_TYPE "x86tti" 25e8d8bef9SDimitry Andric 26e8d8bef9SDimitry Andric /// Return a constant boolean vector that has true elements in all positions 27e8d8bef9SDimitry Andric /// where the input constant data vector has an element with the sign bit set. 28e8d8bef9SDimitry Andric static Constant *getNegativeIsTrueBoolVec(Constant *V) { 29e8d8bef9SDimitry Andric VectorType *IntTy = VectorType::getInteger(cast<VectorType>(V->getType())); 30e8d8bef9SDimitry Andric V = ConstantExpr::getBitCast(V, IntTy); 31e8d8bef9SDimitry Andric V = ConstantExpr::getICmp(CmpInst::ICMP_SGT, Constant::getNullValue(IntTy), 32e8d8bef9SDimitry Andric V); 33e8d8bef9SDimitry Andric return V; 34e8d8bef9SDimitry Andric } 35e8d8bef9SDimitry Andric 36e8d8bef9SDimitry Andric /// Convert the x86 XMM integer vector mask to a vector of bools based on 37e8d8bef9SDimitry Andric /// each element's most significant bit (the sign bit). 38e8d8bef9SDimitry Andric static Value *getBoolVecFromMask(Value *Mask) { 39e8d8bef9SDimitry Andric // Fold Constant Mask. 40e8d8bef9SDimitry Andric if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) 41e8d8bef9SDimitry Andric return getNegativeIsTrueBoolVec(ConstantMask); 42e8d8bef9SDimitry Andric 43e8d8bef9SDimitry Andric // Mask was extended from a boolean vector. 44e8d8bef9SDimitry Andric Value *ExtMask; 45e8d8bef9SDimitry Andric if (PatternMatch::match( 46e8d8bef9SDimitry Andric Mask, PatternMatch::m_SExt(PatternMatch::m_Value(ExtMask))) && 47e8d8bef9SDimitry Andric ExtMask->getType()->isIntOrIntVectorTy(1)) 48e8d8bef9SDimitry Andric return ExtMask; 49e8d8bef9SDimitry Andric 50e8d8bef9SDimitry Andric return nullptr; 51e8d8bef9SDimitry Andric } 52e8d8bef9SDimitry Andric 53e8d8bef9SDimitry Andric // TODO: If the x86 backend knew how to convert a bool vector mask back to an 54e8d8bef9SDimitry Andric // XMM register mask efficiently, we could transform all x86 masked intrinsics 55e8d8bef9SDimitry Andric // to LLVM masked intrinsics and remove the x86 masked intrinsic defs. 56e8d8bef9SDimitry Andric static Instruction *simplifyX86MaskedLoad(IntrinsicInst &II, InstCombiner &IC) { 57e8d8bef9SDimitry Andric Value *Ptr = II.getOperand(0); 58e8d8bef9SDimitry Andric Value *Mask = II.getOperand(1); 59e8d8bef9SDimitry Andric Constant *ZeroVec = Constant::getNullValue(II.getType()); 60e8d8bef9SDimitry Andric 61e8d8bef9SDimitry Andric // Zero Mask - masked load instruction creates a zero vector. 62e8d8bef9SDimitry Andric if (isa<ConstantAggregateZero>(Mask)) 63e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ZeroVec); 64e8d8bef9SDimitry Andric 65e8d8bef9SDimitry Andric // The mask is constant or extended from a bool vector. Convert this x86 66e8d8bef9SDimitry Andric // intrinsic to the LLVM intrinsic to allow target-independent optimizations. 67e8d8bef9SDimitry Andric if (Value *BoolMask = getBoolVecFromMask(Mask)) { 68e8d8bef9SDimitry Andric // First, cast the x86 intrinsic scalar pointer to a vector pointer to match 69e8d8bef9SDimitry Andric // the LLVM intrinsic definition for the pointer argument. 70e8d8bef9SDimitry Andric unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace(); 71e8d8bef9SDimitry Andric PointerType *VecPtrTy = PointerType::get(II.getType(), AddrSpace); 72e8d8bef9SDimitry Andric Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec"); 73e8d8bef9SDimitry Andric 74e8d8bef9SDimitry Andric // The pass-through vector for an x86 masked load is a zero vector. 75*fe6060f1SDimitry Andric CallInst *NewMaskedLoad = IC.Builder.CreateMaskedLoad( 76*fe6060f1SDimitry Andric II.getType(), PtrCast, Align(1), BoolMask, ZeroVec); 77e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, NewMaskedLoad); 78e8d8bef9SDimitry Andric } 79e8d8bef9SDimitry Andric 80e8d8bef9SDimitry Andric return nullptr; 81e8d8bef9SDimitry Andric } 82e8d8bef9SDimitry Andric 83e8d8bef9SDimitry Andric // TODO: If the x86 backend knew how to convert a bool vector mask back to an 84e8d8bef9SDimitry Andric // XMM register mask efficiently, we could transform all x86 masked intrinsics 85e8d8bef9SDimitry Andric // to LLVM masked intrinsics and remove the x86 masked intrinsic defs. 86e8d8bef9SDimitry Andric static bool simplifyX86MaskedStore(IntrinsicInst &II, InstCombiner &IC) { 87e8d8bef9SDimitry Andric Value *Ptr = II.getOperand(0); 88e8d8bef9SDimitry Andric Value *Mask = II.getOperand(1); 89e8d8bef9SDimitry Andric Value *Vec = II.getOperand(2); 90e8d8bef9SDimitry Andric 91e8d8bef9SDimitry Andric // Zero Mask - this masked store instruction does nothing. 92e8d8bef9SDimitry Andric if (isa<ConstantAggregateZero>(Mask)) { 93e8d8bef9SDimitry Andric IC.eraseInstFromFunction(II); 94e8d8bef9SDimitry Andric return true; 95e8d8bef9SDimitry Andric } 96e8d8bef9SDimitry Andric 97e8d8bef9SDimitry Andric // The SSE2 version is too weird (eg, unaligned but non-temporal) to do 98e8d8bef9SDimitry Andric // anything else at this level. 99e8d8bef9SDimitry Andric if (II.getIntrinsicID() == Intrinsic::x86_sse2_maskmov_dqu) 100e8d8bef9SDimitry Andric return false; 101e8d8bef9SDimitry Andric 102e8d8bef9SDimitry Andric // The mask is constant or extended from a bool vector. Convert this x86 103e8d8bef9SDimitry Andric // intrinsic to the LLVM intrinsic to allow target-independent optimizations. 104e8d8bef9SDimitry Andric if (Value *BoolMask = getBoolVecFromMask(Mask)) { 105e8d8bef9SDimitry Andric unsigned AddrSpace = cast<PointerType>(Ptr->getType())->getAddressSpace(); 106e8d8bef9SDimitry Andric PointerType *VecPtrTy = PointerType::get(Vec->getType(), AddrSpace); 107e8d8bef9SDimitry Andric Value *PtrCast = IC.Builder.CreateBitCast(Ptr, VecPtrTy, "castvec"); 108e8d8bef9SDimitry Andric 109e8d8bef9SDimitry Andric IC.Builder.CreateMaskedStore(Vec, PtrCast, Align(1), BoolMask); 110e8d8bef9SDimitry Andric 111e8d8bef9SDimitry Andric // 'Replace uses' doesn't work for stores. Erase the original masked store. 112e8d8bef9SDimitry Andric IC.eraseInstFromFunction(II); 113e8d8bef9SDimitry Andric return true; 114e8d8bef9SDimitry Andric } 115e8d8bef9SDimitry Andric 116e8d8bef9SDimitry Andric return false; 117e8d8bef9SDimitry Andric } 118e8d8bef9SDimitry Andric 119e8d8bef9SDimitry Andric static Value *simplifyX86immShift(const IntrinsicInst &II, 120e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 121e8d8bef9SDimitry Andric bool LogicalShift = false; 122e8d8bef9SDimitry Andric bool ShiftLeft = false; 123e8d8bef9SDimitry Andric bool IsImm = false; 124e8d8bef9SDimitry Andric 125e8d8bef9SDimitry Andric switch (II.getIntrinsicID()) { 126e8d8bef9SDimitry Andric default: 127e8d8bef9SDimitry Andric llvm_unreachable("Unexpected intrinsic!"); 128e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrai_d: 129e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrai_w: 130e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrai_d: 131e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrai_w: 132e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_q_128: 133e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_q_256: 134e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_d_512: 135e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_q_512: 136e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_w_512: 137e8d8bef9SDimitry Andric IsImm = true; 138e8d8bef9SDimitry Andric LLVM_FALLTHROUGH; 139e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psra_d: 140e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psra_w: 141e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psra_d: 142e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psra_w: 143e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_q_128: 144e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_q_256: 145e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_d_512: 146e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_q_512: 147e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_w_512: 148e8d8bef9SDimitry Andric LogicalShift = false; 149e8d8bef9SDimitry Andric ShiftLeft = false; 150e8d8bef9SDimitry Andric break; 151e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrli_d: 152e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrli_q: 153e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrli_w: 154e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrli_d: 155e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrli_q: 156e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrli_w: 157e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrli_d_512: 158e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrli_q_512: 159e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrli_w_512: 160e8d8bef9SDimitry Andric IsImm = true; 161e8d8bef9SDimitry Andric LLVM_FALLTHROUGH; 162e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrl_d: 163e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrl_q: 164e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrl_w: 165e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrl_d: 166e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrl_q: 167e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrl_w: 168e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrl_d_512: 169e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrl_q_512: 170e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrl_w_512: 171e8d8bef9SDimitry Andric LogicalShift = true; 172e8d8bef9SDimitry Andric ShiftLeft = false; 173e8d8bef9SDimitry Andric break; 174e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pslli_d: 175e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pslli_q: 176e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pslli_w: 177e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pslli_d: 178e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pslli_q: 179e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pslli_w: 180e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pslli_d_512: 181e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pslli_q_512: 182e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pslli_w_512: 183e8d8bef9SDimitry Andric IsImm = true; 184e8d8bef9SDimitry Andric LLVM_FALLTHROUGH; 185e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psll_d: 186e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psll_q: 187e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psll_w: 188e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psll_d: 189e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psll_q: 190e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psll_w: 191e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psll_d_512: 192e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psll_q_512: 193e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psll_w_512: 194e8d8bef9SDimitry Andric LogicalShift = true; 195e8d8bef9SDimitry Andric ShiftLeft = true; 196e8d8bef9SDimitry Andric break; 197e8d8bef9SDimitry Andric } 198e8d8bef9SDimitry Andric assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left"); 199e8d8bef9SDimitry Andric 200*fe6060f1SDimitry Andric Value *Vec = II.getArgOperand(0); 201*fe6060f1SDimitry Andric Value *Amt = II.getArgOperand(1); 202*fe6060f1SDimitry Andric auto *VT = cast<FixedVectorType>(Vec->getType()); 203*fe6060f1SDimitry Andric Type *SVT = VT->getElementType(); 204*fe6060f1SDimitry Andric Type *AmtVT = Amt->getType(); 205e8d8bef9SDimitry Andric unsigned VWidth = VT->getNumElements(); 206e8d8bef9SDimitry Andric unsigned BitWidth = SVT->getPrimitiveSizeInBits(); 207e8d8bef9SDimitry Andric 208e8d8bef9SDimitry Andric // If the shift amount is guaranteed to be in-range we can replace it with a 209e8d8bef9SDimitry Andric // generic shift. If its guaranteed to be out of range, logical shifts combine 210e8d8bef9SDimitry Andric // to zero and arithmetic shifts are clamped to (BitWidth - 1). 211e8d8bef9SDimitry Andric if (IsImm) { 212e8d8bef9SDimitry Andric assert(AmtVT->isIntegerTy(32) && "Unexpected shift-by-immediate type"); 213e8d8bef9SDimitry Andric KnownBits KnownAmtBits = 214e8d8bef9SDimitry Andric llvm::computeKnownBits(Amt, II.getModule()->getDataLayout()); 215e8d8bef9SDimitry Andric if (KnownAmtBits.getMaxValue().ult(BitWidth)) { 216e8d8bef9SDimitry Andric Amt = Builder.CreateZExtOrTrunc(Amt, SVT); 217e8d8bef9SDimitry Andric Amt = Builder.CreateVectorSplat(VWidth, Amt); 218e8d8bef9SDimitry Andric return (LogicalShift ? (ShiftLeft ? Builder.CreateShl(Vec, Amt) 219e8d8bef9SDimitry Andric : Builder.CreateLShr(Vec, Amt)) 220e8d8bef9SDimitry Andric : Builder.CreateAShr(Vec, Amt)); 221e8d8bef9SDimitry Andric } 222e8d8bef9SDimitry Andric if (KnownAmtBits.getMinValue().uge(BitWidth)) { 223e8d8bef9SDimitry Andric if (LogicalShift) 224e8d8bef9SDimitry Andric return ConstantAggregateZero::get(VT); 225e8d8bef9SDimitry Andric Amt = ConstantInt::get(SVT, BitWidth - 1); 226e8d8bef9SDimitry Andric return Builder.CreateAShr(Vec, Builder.CreateVectorSplat(VWidth, Amt)); 227e8d8bef9SDimitry Andric } 228e8d8bef9SDimitry Andric } else { 229e8d8bef9SDimitry Andric // Ensure the first element has an in-range value and the rest of the 230e8d8bef9SDimitry Andric // elements in the bottom 64 bits are zero. 231e8d8bef9SDimitry Andric assert(AmtVT->isVectorTy() && AmtVT->getPrimitiveSizeInBits() == 128 && 232e8d8bef9SDimitry Andric cast<VectorType>(AmtVT)->getElementType() == SVT && 233e8d8bef9SDimitry Andric "Unexpected shift-by-scalar type"); 234e8d8bef9SDimitry Andric unsigned NumAmtElts = cast<FixedVectorType>(AmtVT)->getNumElements(); 235e8d8bef9SDimitry Andric APInt DemandedLower = APInt::getOneBitSet(NumAmtElts, 0); 236e8d8bef9SDimitry Andric APInt DemandedUpper = APInt::getBitsSet(NumAmtElts, 1, NumAmtElts / 2); 237e8d8bef9SDimitry Andric KnownBits KnownLowerBits = llvm::computeKnownBits( 238e8d8bef9SDimitry Andric Amt, DemandedLower, II.getModule()->getDataLayout()); 239e8d8bef9SDimitry Andric KnownBits KnownUpperBits = llvm::computeKnownBits( 240e8d8bef9SDimitry Andric Amt, DemandedUpper, II.getModule()->getDataLayout()); 241e8d8bef9SDimitry Andric if (KnownLowerBits.getMaxValue().ult(BitWidth) && 242e8d8bef9SDimitry Andric (DemandedUpper.isNullValue() || KnownUpperBits.isZero())) { 243e8d8bef9SDimitry Andric SmallVector<int, 16> ZeroSplat(VWidth, 0); 244e8d8bef9SDimitry Andric Amt = Builder.CreateShuffleVector(Amt, ZeroSplat); 245e8d8bef9SDimitry Andric return (LogicalShift ? (ShiftLeft ? Builder.CreateShl(Vec, Amt) 246e8d8bef9SDimitry Andric : Builder.CreateLShr(Vec, Amt)) 247e8d8bef9SDimitry Andric : Builder.CreateAShr(Vec, Amt)); 248e8d8bef9SDimitry Andric } 249e8d8bef9SDimitry Andric } 250e8d8bef9SDimitry Andric 251e8d8bef9SDimitry Andric // Simplify if count is constant vector. 252*fe6060f1SDimitry Andric auto *CDV = dyn_cast<ConstantDataVector>(Amt); 253e8d8bef9SDimitry Andric if (!CDV) 254e8d8bef9SDimitry Andric return nullptr; 255e8d8bef9SDimitry Andric 256e8d8bef9SDimitry Andric // SSE2/AVX2 uses all the first 64-bits of the 128-bit vector 257e8d8bef9SDimitry Andric // operand to compute the shift amount. 258e8d8bef9SDimitry Andric assert(AmtVT->isVectorTy() && AmtVT->getPrimitiveSizeInBits() == 128 && 259e8d8bef9SDimitry Andric cast<VectorType>(AmtVT)->getElementType() == SVT && 260e8d8bef9SDimitry Andric "Unexpected shift-by-scalar type"); 261e8d8bef9SDimitry Andric 262e8d8bef9SDimitry Andric // Concatenate the sub-elements to create the 64-bit value. 263e8d8bef9SDimitry Andric APInt Count(64, 0); 264e8d8bef9SDimitry Andric for (unsigned i = 0, NumSubElts = 64 / BitWidth; i != NumSubElts; ++i) { 265e8d8bef9SDimitry Andric unsigned SubEltIdx = (NumSubElts - 1) - i; 266*fe6060f1SDimitry Andric auto *SubElt = cast<ConstantInt>(CDV->getElementAsConstant(SubEltIdx)); 267e8d8bef9SDimitry Andric Count <<= BitWidth; 268e8d8bef9SDimitry Andric Count |= SubElt->getValue().zextOrTrunc(64); 269e8d8bef9SDimitry Andric } 270e8d8bef9SDimitry Andric 271e8d8bef9SDimitry Andric // If shift-by-zero then just return the original value. 272e8d8bef9SDimitry Andric if (Count.isNullValue()) 273e8d8bef9SDimitry Andric return Vec; 274e8d8bef9SDimitry Andric 275e8d8bef9SDimitry Andric // Handle cases when Shift >= BitWidth. 276e8d8bef9SDimitry Andric if (Count.uge(BitWidth)) { 277e8d8bef9SDimitry Andric // If LogicalShift - just return zero. 278e8d8bef9SDimitry Andric if (LogicalShift) 279e8d8bef9SDimitry Andric return ConstantAggregateZero::get(VT); 280e8d8bef9SDimitry Andric 281e8d8bef9SDimitry Andric // If ArithmeticShift - clamp Shift to (BitWidth - 1). 282e8d8bef9SDimitry Andric Count = APInt(64, BitWidth - 1); 283e8d8bef9SDimitry Andric } 284e8d8bef9SDimitry Andric 285e8d8bef9SDimitry Andric // Get a constant vector of the same type as the first operand. 286e8d8bef9SDimitry Andric auto ShiftAmt = ConstantInt::get(SVT, Count.zextOrTrunc(BitWidth)); 287e8d8bef9SDimitry Andric auto ShiftVec = Builder.CreateVectorSplat(VWidth, ShiftAmt); 288e8d8bef9SDimitry Andric 289e8d8bef9SDimitry Andric if (ShiftLeft) 290e8d8bef9SDimitry Andric return Builder.CreateShl(Vec, ShiftVec); 291e8d8bef9SDimitry Andric 292e8d8bef9SDimitry Andric if (LogicalShift) 293e8d8bef9SDimitry Andric return Builder.CreateLShr(Vec, ShiftVec); 294e8d8bef9SDimitry Andric 295e8d8bef9SDimitry Andric return Builder.CreateAShr(Vec, ShiftVec); 296e8d8bef9SDimitry Andric } 297e8d8bef9SDimitry Andric 298e8d8bef9SDimitry Andric // Attempt to simplify AVX2 per-element shift intrinsics to a generic IR shift. 299e8d8bef9SDimitry Andric // Unlike the generic IR shifts, the intrinsics have defined behaviour for out 300e8d8bef9SDimitry Andric // of range shift amounts (logical - set to zero, arithmetic - splat sign bit). 301e8d8bef9SDimitry Andric static Value *simplifyX86varShift(const IntrinsicInst &II, 302e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 303e8d8bef9SDimitry Andric bool LogicalShift = false; 304e8d8bef9SDimitry Andric bool ShiftLeft = false; 305e8d8bef9SDimitry Andric 306e8d8bef9SDimitry Andric switch (II.getIntrinsicID()) { 307e8d8bef9SDimitry Andric default: 308e8d8bef9SDimitry Andric llvm_unreachable("Unexpected intrinsic!"); 309e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrav_d: 310e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrav_d_256: 311e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_q_128: 312e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_q_256: 313e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_d_512: 314e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_q_512: 315e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_w_128: 316e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_w_256: 317e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_w_512: 318e8d8bef9SDimitry Andric LogicalShift = false; 319e8d8bef9SDimitry Andric ShiftLeft = false; 320e8d8bef9SDimitry Andric break; 321e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_d: 322e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_d_256: 323e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_q: 324e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_q_256: 325e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_d_512: 326e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_q_512: 327e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_w_128: 328e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_w_256: 329e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_w_512: 330e8d8bef9SDimitry Andric LogicalShift = true; 331e8d8bef9SDimitry Andric ShiftLeft = false; 332e8d8bef9SDimitry Andric break; 333e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_d: 334e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_d_256: 335e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_q: 336e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_q_256: 337e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_d_512: 338e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_q_512: 339e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_w_128: 340e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_w_256: 341e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_w_512: 342e8d8bef9SDimitry Andric LogicalShift = true; 343e8d8bef9SDimitry Andric ShiftLeft = true; 344e8d8bef9SDimitry Andric break; 345e8d8bef9SDimitry Andric } 346e8d8bef9SDimitry Andric assert((LogicalShift || !ShiftLeft) && "Only logical shifts can shift left"); 347e8d8bef9SDimitry Andric 348*fe6060f1SDimitry Andric Value *Vec = II.getArgOperand(0); 349*fe6060f1SDimitry Andric Value *Amt = II.getArgOperand(1); 350*fe6060f1SDimitry Andric auto *VT = cast<FixedVectorType>(II.getType()); 351*fe6060f1SDimitry Andric Type *SVT = VT->getElementType(); 352e8d8bef9SDimitry Andric int NumElts = VT->getNumElements(); 353e8d8bef9SDimitry Andric int BitWidth = SVT->getIntegerBitWidth(); 354e8d8bef9SDimitry Andric 355e8d8bef9SDimitry Andric // If the shift amount is guaranteed to be in-range we can replace it with a 356e8d8bef9SDimitry Andric // generic shift. 357e8d8bef9SDimitry Andric APInt UpperBits = 358e8d8bef9SDimitry Andric APInt::getHighBitsSet(BitWidth, BitWidth - Log2_32(BitWidth)); 359e8d8bef9SDimitry Andric if (llvm::MaskedValueIsZero(Amt, UpperBits, 360e8d8bef9SDimitry Andric II.getModule()->getDataLayout())) { 361e8d8bef9SDimitry Andric return (LogicalShift ? (ShiftLeft ? Builder.CreateShl(Vec, Amt) 362e8d8bef9SDimitry Andric : Builder.CreateLShr(Vec, Amt)) 363e8d8bef9SDimitry Andric : Builder.CreateAShr(Vec, Amt)); 364e8d8bef9SDimitry Andric } 365e8d8bef9SDimitry Andric 366e8d8bef9SDimitry Andric // Simplify if all shift amounts are constant/undef. 367e8d8bef9SDimitry Andric auto *CShift = dyn_cast<Constant>(Amt); 368e8d8bef9SDimitry Andric if (!CShift) 369e8d8bef9SDimitry Andric return nullptr; 370e8d8bef9SDimitry Andric 371e8d8bef9SDimitry Andric // Collect each element's shift amount. 372e8d8bef9SDimitry Andric // We also collect special cases: UNDEF = -1, OUT-OF-RANGE = BitWidth. 373e8d8bef9SDimitry Andric bool AnyOutOfRange = false; 374e8d8bef9SDimitry Andric SmallVector<int, 8> ShiftAmts; 375e8d8bef9SDimitry Andric for (int I = 0; I < NumElts; ++I) { 376e8d8bef9SDimitry Andric auto *CElt = CShift->getAggregateElement(I); 377e8d8bef9SDimitry Andric if (isa_and_nonnull<UndefValue>(CElt)) { 378e8d8bef9SDimitry Andric ShiftAmts.push_back(-1); 379e8d8bef9SDimitry Andric continue; 380e8d8bef9SDimitry Andric } 381e8d8bef9SDimitry Andric 382e8d8bef9SDimitry Andric auto *COp = dyn_cast_or_null<ConstantInt>(CElt); 383e8d8bef9SDimitry Andric if (!COp) 384e8d8bef9SDimitry Andric return nullptr; 385e8d8bef9SDimitry Andric 386e8d8bef9SDimitry Andric // Handle out of range shifts. 387e8d8bef9SDimitry Andric // If LogicalShift - set to BitWidth (special case). 388e8d8bef9SDimitry Andric // If ArithmeticShift - set to (BitWidth - 1) (sign splat). 389e8d8bef9SDimitry Andric APInt ShiftVal = COp->getValue(); 390e8d8bef9SDimitry Andric if (ShiftVal.uge(BitWidth)) { 391e8d8bef9SDimitry Andric AnyOutOfRange = LogicalShift; 392e8d8bef9SDimitry Andric ShiftAmts.push_back(LogicalShift ? BitWidth : BitWidth - 1); 393e8d8bef9SDimitry Andric continue; 394e8d8bef9SDimitry Andric } 395e8d8bef9SDimitry Andric 396e8d8bef9SDimitry Andric ShiftAmts.push_back((int)ShiftVal.getZExtValue()); 397e8d8bef9SDimitry Andric } 398e8d8bef9SDimitry Andric 399e8d8bef9SDimitry Andric // If all elements out of range or UNDEF, return vector of zeros/undefs. 400e8d8bef9SDimitry Andric // ArithmeticShift should only hit this if they are all UNDEF. 401e8d8bef9SDimitry Andric auto OutOfRange = [&](int Idx) { return (Idx < 0) || (BitWidth <= Idx); }; 402e8d8bef9SDimitry Andric if (llvm::all_of(ShiftAmts, OutOfRange)) { 403e8d8bef9SDimitry Andric SmallVector<Constant *, 8> ConstantVec; 404e8d8bef9SDimitry Andric for (int Idx : ShiftAmts) { 405e8d8bef9SDimitry Andric if (Idx < 0) { 406e8d8bef9SDimitry Andric ConstantVec.push_back(UndefValue::get(SVT)); 407e8d8bef9SDimitry Andric } else { 408e8d8bef9SDimitry Andric assert(LogicalShift && "Logical shift expected"); 409e8d8bef9SDimitry Andric ConstantVec.push_back(ConstantInt::getNullValue(SVT)); 410e8d8bef9SDimitry Andric } 411e8d8bef9SDimitry Andric } 412e8d8bef9SDimitry Andric return ConstantVector::get(ConstantVec); 413e8d8bef9SDimitry Andric } 414e8d8bef9SDimitry Andric 415e8d8bef9SDimitry Andric // We can't handle only some out of range values with generic logical shifts. 416e8d8bef9SDimitry Andric if (AnyOutOfRange) 417e8d8bef9SDimitry Andric return nullptr; 418e8d8bef9SDimitry Andric 419e8d8bef9SDimitry Andric // Build the shift amount constant vector. 420e8d8bef9SDimitry Andric SmallVector<Constant *, 8> ShiftVecAmts; 421e8d8bef9SDimitry Andric for (int Idx : ShiftAmts) { 422e8d8bef9SDimitry Andric if (Idx < 0) 423e8d8bef9SDimitry Andric ShiftVecAmts.push_back(UndefValue::get(SVT)); 424e8d8bef9SDimitry Andric else 425e8d8bef9SDimitry Andric ShiftVecAmts.push_back(ConstantInt::get(SVT, Idx)); 426e8d8bef9SDimitry Andric } 427e8d8bef9SDimitry Andric auto ShiftVec = ConstantVector::get(ShiftVecAmts); 428e8d8bef9SDimitry Andric 429e8d8bef9SDimitry Andric if (ShiftLeft) 430e8d8bef9SDimitry Andric return Builder.CreateShl(Vec, ShiftVec); 431e8d8bef9SDimitry Andric 432e8d8bef9SDimitry Andric if (LogicalShift) 433e8d8bef9SDimitry Andric return Builder.CreateLShr(Vec, ShiftVec); 434e8d8bef9SDimitry Andric 435e8d8bef9SDimitry Andric return Builder.CreateAShr(Vec, ShiftVec); 436e8d8bef9SDimitry Andric } 437e8d8bef9SDimitry Andric 438e8d8bef9SDimitry Andric static Value *simplifyX86pack(IntrinsicInst &II, 439e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder, bool IsSigned) { 440e8d8bef9SDimitry Andric Value *Arg0 = II.getArgOperand(0); 441e8d8bef9SDimitry Andric Value *Arg1 = II.getArgOperand(1); 442e8d8bef9SDimitry Andric Type *ResTy = II.getType(); 443e8d8bef9SDimitry Andric 444e8d8bef9SDimitry Andric // Fast all undef handling. 445e8d8bef9SDimitry Andric if (isa<UndefValue>(Arg0) && isa<UndefValue>(Arg1)) 446e8d8bef9SDimitry Andric return UndefValue::get(ResTy); 447e8d8bef9SDimitry Andric 448e8d8bef9SDimitry Andric auto *ArgTy = cast<FixedVectorType>(Arg0->getType()); 449e8d8bef9SDimitry Andric unsigned NumLanes = ResTy->getPrimitiveSizeInBits() / 128; 450e8d8bef9SDimitry Andric unsigned NumSrcElts = ArgTy->getNumElements(); 451e8d8bef9SDimitry Andric assert(cast<FixedVectorType>(ResTy)->getNumElements() == (2 * NumSrcElts) && 452e8d8bef9SDimitry Andric "Unexpected packing types"); 453e8d8bef9SDimitry Andric 454e8d8bef9SDimitry Andric unsigned NumSrcEltsPerLane = NumSrcElts / NumLanes; 455e8d8bef9SDimitry Andric unsigned DstScalarSizeInBits = ResTy->getScalarSizeInBits(); 456e8d8bef9SDimitry Andric unsigned SrcScalarSizeInBits = ArgTy->getScalarSizeInBits(); 457e8d8bef9SDimitry Andric assert(SrcScalarSizeInBits == (2 * DstScalarSizeInBits) && 458e8d8bef9SDimitry Andric "Unexpected packing types"); 459e8d8bef9SDimitry Andric 460e8d8bef9SDimitry Andric // Constant folding. 461e8d8bef9SDimitry Andric if (!isa<Constant>(Arg0) || !isa<Constant>(Arg1)) 462e8d8bef9SDimitry Andric return nullptr; 463e8d8bef9SDimitry Andric 464e8d8bef9SDimitry Andric // Clamp Values - signed/unsigned both use signed clamp values, but they 465e8d8bef9SDimitry Andric // differ on the min/max values. 466e8d8bef9SDimitry Andric APInt MinValue, MaxValue; 467e8d8bef9SDimitry Andric if (IsSigned) { 468e8d8bef9SDimitry Andric // PACKSS: Truncate signed value with signed saturation. 469e8d8bef9SDimitry Andric // Source values less than dst minint are saturated to minint. 470e8d8bef9SDimitry Andric // Source values greater than dst maxint are saturated to maxint. 471e8d8bef9SDimitry Andric MinValue = 472e8d8bef9SDimitry Andric APInt::getSignedMinValue(DstScalarSizeInBits).sext(SrcScalarSizeInBits); 473e8d8bef9SDimitry Andric MaxValue = 474e8d8bef9SDimitry Andric APInt::getSignedMaxValue(DstScalarSizeInBits).sext(SrcScalarSizeInBits); 475e8d8bef9SDimitry Andric } else { 476e8d8bef9SDimitry Andric // PACKUS: Truncate signed value with unsigned saturation. 477e8d8bef9SDimitry Andric // Source values less than zero are saturated to zero. 478e8d8bef9SDimitry Andric // Source values greater than dst maxuint are saturated to maxuint. 479e8d8bef9SDimitry Andric MinValue = APInt::getNullValue(SrcScalarSizeInBits); 480e8d8bef9SDimitry Andric MaxValue = APInt::getLowBitsSet(SrcScalarSizeInBits, DstScalarSizeInBits); 481e8d8bef9SDimitry Andric } 482e8d8bef9SDimitry Andric 483e8d8bef9SDimitry Andric auto *MinC = Constant::getIntegerValue(ArgTy, MinValue); 484e8d8bef9SDimitry Andric auto *MaxC = Constant::getIntegerValue(ArgTy, MaxValue); 485e8d8bef9SDimitry Andric Arg0 = Builder.CreateSelect(Builder.CreateICmpSLT(Arg0, MinC), MinC, Arg0); 486e8d8bef9SDimitry Andric Arg1 = Builder.CreateSelect(Builder.CreateICmpSLT(Arg1, MinC), MinC, Arg1); 487e8d8bef9SDimitry Andric Arg0 = Builder.CreateSelect(Builder.CreateICmpSGT(Arg0, MaxC), MaxC, Arg0); 488e8d8bef9SDimitry Andric Arg1 = Builder.CreateSelect(Builder.CreateICmpSGT(Arg1, MaxC), MaxC, Arg1); 489e8d8bef9SDimitry Andric 490e8d8bef9SDimitry Andric // Shuffle clamped args together at the lane level. 491e8d8bef9SDimitry Andric SmallVector<int, 32> PackMask; 492e8d8bef9SDimitry Andric for (unsigned Lane = 0; Lane != NumLanes; ++Lane) { 493e8d8bef9SDimitry Andric for (unsigned Elt = 0; Elt != NumSrcEltsPerLane; ++Elt) 494e8d8bef9SDimitry Andric PackMask.push_back(Elt + (Lane * NumSrcEltsPerLane)); 495e8d8bef9SDimitry Andric for (unsigned Elt = 0; Elt != NumSrcEltsPerLane; ++Elt) 496e8d8bef9SDimitry Andric PackMask.push_back(Elt + (Lane * NumSrcEltsPerLane) + NumSrcElts); 497e8d8bef9SDimitry Andric } 498e8d8bef9SDimitry Andric auto *Shuffle = Builder.CreateShuffleVector(Arg0, Arg1, PackMask); 499e8d8bef9SDimitry Andric 500e8d8bef9SDimitry Andric // Truncate to dst size. 501e8d8bef9SDimitry Andric return Builder.CreateTrunc(Shuffle, ResTy); 502e8d8bef9SDimitry Andric } 503e8d8bef9SDimitry Andric 504e8d8bef9SDimitry Andric static Value *simplifyX86movmsk(const IntrinsicInst &II, 505e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 506e8d8bef9SDimitry Andric Value *Arg = II.getArgOperand(0); 507e8d8bef9SDimitry Andric Type *ResTy = II.getType(); 508e8d8bef9SDimitry Andric 509e8d8bef9SDimitry Andric // movmsk(undef) -> zero as we must ensure the upper bits are zero. 510e8d8bef9SDimitry Andric if (isa<UndefValue>(Arg)) 511e8d8bef9SDimitry Andric return Constant::getNullValue(ResTy); 512e8d8bef9SDimitry Andric 513e8d8bef9SDimitry Andric auto *ArgTy = dyn_cast<FixedVectorType>(Arg->getType()); 514e8d8bef9SDimitry Andric // We can't easily peek through x86_mmx types. 515e8d8bef9SDimitry Andric if (!ArgTy) 516e8d8bef9SDimitry Andric return nullptr; 517e8d8bef9SDimitry Andric 518e8d8bef9SDimitry Andric // Expand MOVMSK to compare/bitcast/zext: 519e8d8bef9SDimitry Andric // e.g. PMOVMSKB(v16i8 x): 520e8d8bef9SDimitry Andric // %cmp = icmp slt <16 x i8> %x, zeroinitializer 521e8d8bef9SDimitry Andric // %int = bitcast <16 x i1> %cmp to i16 522e8d8bef9SDimitry Andric // %res = zext i16 %int to i32 523e8d8bef9SDimitry Andric unsigned NumElts = ArgTy->getNumElements(); 524e8d8bef9SDimitry Andric Type *IntegerVecTy = VectorType::getInteger(ArgTy); 525e8d8bef9SDimitry Andric Type *IntegerTy = Builder.getIntNTy(NumElts); 526e8d8bef9SDimitry Andric 527e8d8bef9SDimitry Andric Value *Res = Builder.CreateBitCast(Arg, IntegerVecTy); 528e8d8bef9SDimitry Andric Res = Builder.CreateICmpSLT(Res, Constant::getNullValue(IntegerVecTy)); 529e8d8bef9SDimitry Andric Res = Builder.CreateBitCast(Res, IntegerTy); 530e8d8bef9SDimitry Andric Res = Builder.CreateZExtOrTrunc(Res, ResTy); 531e8d8bef9SDimitry Andric return Res; 532e8d8bef9SDimitry Andric } 533e8d8bef9SDimitry Andric 534e8d8bef9SDimitry Andric static Value *simplifyX86addcarry(const IntrinsicInst &II, 535e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 536e8d8bef9SDimitry Andric Value *CarryIn = II.getArgOperand(0); 537e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 538e8d8bef9SDimitry Andric Value *Op2 = II.getArgOperand(2); 539e8d8bef9SDimitry Andric Type *RetTy = II.getType(); 540e8d8bef9SDimitry Andric Type *OpTy = Op1->getType(); 541e8d8bef9SDimitry Andric assert(RetTy->getStructElementType(0)->isIntegerTy(8) && 542e8d8bef9SDimitry Andric RetTy->getStructElementType(1) == OpTy && OpTy == Op2->getType() && 543e8d8bef9SDimitry Andric "Unexpected types for x86 addcarry"); 544e8d8bef9SDimitry Andric 545e8d8bef9SDimitry Andric // If carry-in is zero, this is just an unsigned add with overflow. 546e8d8bef9SDimitry Andric if (match(CarryIn, PatternMatch::m_ZeroInt())) { 547e8d8bef9SDimitry Andric Value *UAdd = Builder.CreateIntrinsic(Intrinsic::uadd_with_overflow, OpTy, 548e8d8bef9SDimitry Andric {Op1, Op2}); 549e8d8bef9SDimitry Andric // The types have to be adjusted to match the x86 call types. 550e8d8bef9SDimitry Andric Value *UAddResult = Builder.CreateExtractValue(UAdd, 0); 551e8d8bef9SDimitry Andric Value *UAddOV = Builder.CreateZExt(Builder.CreateExtractValue(UAdd, 1), 552e8d8bef9SDimitry Andric Builder.getInt8Ty()); 553e8d8bef9SDimitry Andric Value *Res = UndefValue::get(RetTy); 554e8d8bef9SDimitry Andric Res = Builder.CreateInsertValue(Res, UAddOV, 0); 555e8d8bef9SDimitry Andric return Builder.CreateInsertValue(Res, UAddResult, 1); 556e8d8bef9SDimitry Andric } 557e8d8bef9SDimitry Andric 558e8d8bef9SDimitry Andric return nullptr; 559e8d8bef9SDimitry Andric } 560e8d8bef9SDimitry Andric 561e8d8bef9SDimitry Andric static Value *simplifyX86insertps(const IntrinsicInst &II, 562e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 563e8d8bef9SDimitry Andric auto *CInt = dyn_cast<ConstantInt>(II.getArgOperand(2)); 564e8d8bef9SDimitry Andric if (!CInt) 565e8d8bef9SDimitry Andric return nullptr; 566e8d8bef9SDimitry Andric 567e8d8bef9SDimitry Andric auto *VecTy = cast<FixedVectorType>(II.getType()); 568e8d8bef9SDimitry Andric assert(VecTy->getNumElements() == 4 && "insertps with wrong vector type"); 569e8d8bef9SDimitry Andric 570e8d8bef9SDimitry Andric // The immediate permute control byte looks like this: 571e8d8bef9SDimitry Andric // [3:0] - zero mask for each 32-bit lane 572e8d8bef9SDimitry Andric // [5:4] - select one 32-bit destination lane 573e8d8bef9SDimitry Andric // [7:6] - select one 32-bit source lane 574e8d8bef9SDimitry Andric 575e8d8bef9SDimitry Andric uint8_t Imm = CInt->getZExtValue(); 576e8d8bef9SDimitry Andric uint8_t ZMask = Imm & 0xf; 577e8d8bef9SDimitry Andric uint8_t DestLane = (Imm >> 4) & 0x3; 578e8d8bef9SDimitry Andric uint8_t SourceLane = (Imm >> 6) & 0x3; 579e8d8bef9SDimitry Andric 580e8d8bef9SDimitry Andric ConstantAggregateZero *ZeroVector = ConstantAggregateZero::get(VecTy); 581e8d8bef9SDimitry Andric 582e8d8bef9SDimitry Andric // If all zero mask bits are set, this was just a weird way to 583e8d8bef9SDimitry Andric // generate a zero vector. 584e8d8bef9SDimitry Andric if (ZMask == 0xf) 585e8d8bef9SDimitry Andric return ZeroVector; 586e8d8bef9SDimitry Andric 587e8d8bef9SDimitry Andric // Initialize by passing all of the first source bits through. 588e8d8bef9SDimitry Andric int ShuffleMask[4] = {0, 1, 2, 3}; 589e8d8bef9SDimitry Andric 590e8d8bef9SDimitry Andric // We may replace the second operand with the zero vector. 591e8d8bef9SDimitry Andric Value *V1 = II.getArgOperand(1); 592e8d8bef9SDimitry Andric 593e8d8bef9SDimitry Andric if (ZMask) { 594e8d8bef9SDimitry Andric // If the zero mask is being used with a single input or the zero mask 595e8d8bef9SDimitry Andric // overrides the destination lane, this is a shuffle with the zero vector. 596e8d8bef9SDimitry Andric if ((II.getArgOperand(0) == II.getArgOperand(1)) || 597e8d8bef9SDimitry Andric (ZMask & (1 << DestLane))) { 598e8d8bef9SDimitry Andric V1 = ZeroVector; 599e8d8bef9SDimitry Andric // We may still move 32-bits of the first source vector from one lane 600e8d8bef9SDimitry Andric // to another. 601e8d8bef9SDimitry Andric ShuffleMask[DestLane] = SourceLane; 602e8d8bef9SDimitry Andric // The zero mask may override the previous insert operation. 603e8d8bef9SDimitry Andric for (unsigned i = 0; i < 4; ++i) 604e8d8bef9SDimitry Andric if ((ZMask >> i) & 0x1) 605e8d8bef9SDimitry Andric ShuffleMask[i] = i + 4; 606e8d8bef9SDimitry Andric } else { 607e8d8bef9SDimitry Andric // TODO: Model this case as 2 shuffles or a 'logical and' plus shuffle? 608e8d8bef9SDimitry Andric return nullptr; 609e8d8bef9SDimitry Andric } 610e8d8bef9SDimitry Andric } else { 611e8d8bef9SDimitry Andric // Replace the selected destination lane with the selected source lane. 612e8d8bef9SDimitry Andric ShuffleMask[DestLane] = SourceLane + 4; 613e8d8bef9SDimitry Andric } 614e8d8bef9SDimitry Andric 615e8d8bef9SDimitry Andric return Builder.CreateShuffleVector(II.getArgOperand(0), V1, ShuffleMask); 616e8d8bef9SDimitry Andric } 617e8d8bef9SDimitry Andric 618e8d8bef9SDimitry Andric /// Attempt to simplify SSE4A EXTRQ/EXTRQI instructions using constant folding 619e8d8bef9SDimitry Andric /// or conversion to a shuffle vector. 620e8d8bef9SDimitry Andric static Value *simplifyX86extrq(IntrinsicInst &II, Value *Op0, 621e8d8bef9SDimitry Andric ConstantInt *CILength, ConstantInt *CIIndex, 622e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 623e8d8bef9SDimitry Andric auto LowConstantHighUndef = [&](uint64_t Val) { 624e8d8bef9SDimitry Andric Type *IntTy64 = Type::getInt64Ty(II.getContext()); 625e8d8bef9SDimitry Andric Constant *Args[] = {ConstantInt::get(IntTy64, Val), 626e8d8bef9SDimitry Andric UndefValue::get(IntTy64)}; 627e8d8bef9SDimitry Andric return ConstantVector::get(Args); 628e8d8bef9SDimitry Andric }; 629e8d8bef9SDimitry Andric 630e8d8bef9SDimitry Andric // See if we're dealing with constant values. 631*fe6060f1SDimitry Andric auto *C0 = dyn_cast<Constant>(Op0); 632*fe6060f1SDimitry Andric auto *CI0 = 633e8d8bef9SDimitry Andric C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0)) 634e8d8bef9SDimitry Andric : nullptr; 635e8d8bef9SDimitry Andric 636e8d8bef9SDimitry Andric // Attempt to constant fold. 637e8d8bef9SDimitry Andric if (CILength && CIIndex) { 638e8d8bef9SDimitry Andric // From AMD documentation: "The bit index and field length are each six 639e8d8bef9SDimitry Andric // bits in length other bits of the field are ignored." 640e8d8bef9SDimitry Andric APInt APIndex = CIIndex->getValue().zextOrTrunc(6); 641e8d8bef9SDimitry Andric APInt APLength = CILength->getValue().zextOrTrunc(6); 642e8d8bef9SDimitry Andric 643e8d8bef9SDimitry Andric unsigned Index = APIndex.getZExtValue(); 644e8d8bef9SDimitry Andric 645e8d8bef9SDimitry Andric // From AMD documentation: "a value of zero in the field length is 646e8d8bef9SDimitry Andric // defined as length of 64". 647e8d8bef9SDimitry Andric unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue(); 648e8d8bef9SDimitry Andric 649e8d8bef9SDimitry Andric // From AMD documentation: "If the sum of the bit index + length field 650e8d8bef9SDimitry Andric // is greater than 64, the results are undefined". 651e8d8bef9SDimitry Andric unsigned End = Index + Length; 652e8d8bef9SDimitry Andric 653e8d8bef9SDimitry Andric // Note that both field index and field length are 8-bit quantities. 654e8d8bef9SDimitry Andric // Since variables 'Index' and 'Length' are unsigned values 655e8d8bef9SDimitry Andric // obtained from zero-extending field index and field length 656e8d8bef9SDimitry Andric // respectively, their sum should never wrap around. 657e8d8bef9SDimitry Andric if (End > 64) 658e8d8bef9SDimitry Andric return UndefValue::get(II.getType()); 659e8d8bef9SDimitry Andric 660e8d8bef9SDimitry Andric // If we are inserting whole bytes, we can convert this to a shuffle. 661e8d8bef9SDimitry Andric // Lowering can recognize EXTRQI shuffle masks. 662e8d8bef9SDimitry Andric if ((Length % 8) == 0 && (Index % 8) == 0) { 663e8d8bef9SDimitry Andric // Convert bit indices to byte indices. 664e8d8bef9SDimitry Andric Length /= 8; 665e8d8bef9SDimitry Andric Index /= 8; 666e8d8bef9SDimitry Andric 667e8d8bef9SDimitry Andric Type *IntTy8 = Type::getInt8Ty(II.getContext()); 668e8d8bef9SDimitry Andric auto *ShufTy = FixedVectorType::get(IntTy8, 16); 669e8d8bef9SDimitry Andric 670e8d8bef9SDimitry Andric SmallVector<int, 16> ShuffleMask; 671e8d8bef9SDimitry Andric for (int i = 0; i != (int)Length; ++i) 672e8d8bef9SDimitry Andric ShuffleMask.push_back(i + Index); 673e8d8bef9SDimitry Andric for (int i = Length; i != 8; ++i) 674e8d8bef9SDimitry Andric ShuffleMask.push_back(i + 16); 675e8d8bef9SDimitry Andric for (int i = 8; i != 16; ++i) 676e8d8bef9SDimitry Andric ShuffleMask.push_back(-1); 677e8d8bef9SDimitry Andric 678e8d8bef9SDimitry Andric Value *SV = Builder.CreateShuffleVector( 679e8d8bef9SDimitry Andric Builder.CreateBitCast(Op0, ShufTy), 680e8d8bef9SDimitry Andric ConstantAggregateZero::get(ShufTy), ShuffleMask); 681e8d8bef9SDimitry Andric return Builder.CreateBitCast(SV, II.getType()); 682e8d8bef9SDimitry Andric } 683e8d8bef9SDimitry Andric 684e8d8bef9SDimitry Andric // Constant Fold - shift Index'th bit to lowest position and mask off 685e8d8bef9SDimitry Andric // Length bits. 686e8d8bef9SDimitry Andric if (CI0) { 687e8d8bef9SDimitry Andric APInt Elt = CI0->getValue(); 688e8d8bef9SDimitry Andric Elt.lshrInPlace(Index); 689e8d8bef9SDimitry Andric Elt = Elt.zextOrTrunc(Length); 690e8d8bef9SDimitry Andric return LowConstantHighUndef(Elt.getZExtValue()); 691e8d8bef9SDimitry Andric } 692e8d8bef9SDimitry Andric 693e8d8bef9SDimitry Andric // If we were an EXTRQ call, we'll save registers if we convert to EXTRQI. 694e8d8bef9SDimitry Andric if (II.getIntrinsicID() == Intrinsic::x86_sse4a_extrq) { 695e8d8bef9SDimitry Andric Value *Args[] = {Op0, CILength, CIIndex}; 696e8d8bef9SDimitry Andric Module *M = II.getModule(); 697e8d8bef9SDimitry Andric Function *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_extrqi); 698e8d8bef9SDimitry Andric return Builder.CreateCall(F, Args); 699e8d8bef9SDimitry Andric } 700e8d8bef9SDimitry Andric } 701e8d8bef9SDimitry Andric 702e8d8bef9SDimitry Andric // Constant Fold - extraction from zero is always {zero, undef}. 703e8d8bef9SDimitry Andric if (CI0 && CI0->isZero()) 704e8d8bef9SDimitry Andric return LowConstantHighUndef(0); 705e8d8bef9SDimitry Andric 706e8d8bef9SDimitry Andric return nullptr; 707e8d8bef9SDimitry Andric } 708e8d8bef9SDimitry Andric 709e8d8bef9SDimitry Andric /// Attempt to simplify SSE4A INSERTQ/INSERTQI instructions using constant 710e8d8bef9SDimitry Andric /// folding or conversion to a shuffle vector. 711e8d8bef9SDimitry Andric static Value *simplifyX86insertq(IntrinsicInst &II, Value *Op0, Value *Op1, 712e8d8bef9SDimitry Andric APInt APLength, APInt APIndex, 713e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 714e8d8bef9SDimitry Andric // From AMD documentation: "The bit index and field length are each six bits 715e8d8bef9SDimitry Andric // in length other bits of the field are ignored." 716e8d8bef9SDimitry Andric APIndex = APIndex.zextOrTrunc(6); 717e8d8bef9SDimitry Andric APLength = APLength.zextOrTrunc(6); 718e8d8bef9SDimitry Andric 719e8d8bef9SDimitry Andric // Attempt to constant fold. 720e8d8bef9SDimitry Andric unsigned Index = APIndex.getZExtValue(); 721e8d8bef9SDimitry Andric 722e8d8bef9SDimitry Andric // From AMD documentation: "a value of zero in the field length is 723e8d8bef9SDimitry Andric // defined as length of 64". 724e8d8bef9SDimitry Andric unsigned Length = APLength == 0 ? 64 : APLength.getZExtValue(); 725e8d8bef9SDimitry Andric 726e8d8bef9SDimitry Andric // From AMD documentation: "If the sum of the bit index + length field 727e8d8bef9SDimitry Andric // is greater than 64, the results are undefined". 728e8d8bef9SDimitry Andric unsigned End = Index + Length; 729e8d8bef9SDimitry Andric 730e8d8bef9SDimitry Andric // Note that both field index and field length are 8-bit quantities. 731e8d8bef9SDimitry Andric // Since variables 'Index' and 'Length' are unsigned values 732e8d8bef9SDimitry Andric // obtained from zero-extending field index and field length 733e8d8bef9SDimitry Andric // respectively, their sum should never wrap around. 734e8d8bef9SDimitry Andric if (End > 64) 735e8d8bef9SDimitry Andric return UndefValue::get(II.getType()); 736e8d8bef9SDimitry Andric 737e8d8bef9SDimitry Andric // If we are inserting whole bytes, we can convert this to a shuffle. 738e8d8bef9SDimitry Andric // Lowering can recognize INSERTQI shuffle masks. 739e8d8bef9SDimitry Andric if ((Length % 8) == 0 && (Index % 8) == 0) { 740e8d8bef9SDimitry Andric // Convert bit indices to byte indices. 741e8d8bef9SDimitry Andric Length /= 8; 742e8d8bef9SDimitry Andric Index /= 8; 743e8d8bef9SDimitry Andric 744e8d8bef9SDimitry Andric Type *IntTy8 = Type::getInt8Ty(II.getContext()); 745e8d8bef9SDimitry Andric auto *ShufTy = FixedVectorType::get(IntTy8, 16); 746e8d8bef9SDimitry Andric 747e8d8bef9SDimitry Andric SmallVector<int, 16> ShuffleMask; 748e8d8bef9SDimitry Andric for (int i = 0; i != (int)Index; ++i) 749e8d8bef9SDimitry Andric ShuffleMask.push_back(i); 750e8d8bef9SDimitry Andric for (int i = 0; i != (int)Length; ++i) 751e8d8bef9SDimitry Andric ShuffleMask.push_back(i + 16); 752e8d8bef9SDimitry Andric for (int i = Index + Length; i != 8; ++i) 753e8d8bef9SDimitry Andric ShuffleMask.push_back(i); 754e8d8bef9SDimitry Andric for (int i = 8; i != 16; ++i) 755e8d8bef9SDimitry Andric ShuffleMask.push_back(-1); 756e8d8bef9SDimitry Andric 757e8d8bef9SDimitry Andric Value *SV = Builder.CreateShuffleVector(Builder.CreateBitCast(Op0, ShufTy), 758e8d8bef9SDimitry Andric Builder.CreateBitCast(Op1, ShufTy), 759e8d8bef9SDimitry Andric ShuffleMask); 760e8d8bef9SDimitry Andric return Builder.CreateBitCast(SV, II.getType()); 761e8d8bef9SDimitry Andric } 762e8d8bef9SDimitry Andric 763e8d8bef9SDimitry Andric // See if we're dealing with constant values. 764*fe6060f1SDimitry Andric auto *C0 = dyn_cast<Constant>(Op0); 765*fe6060f1SDimitry Andric auto *C1 = dyn_cast<Constant>(Op1); 766*fe6060f1SDimitry Andric auto *CI00 = 767e8d8bef9SDimitry Andric C0 ? dyn_cast_or_null<ConstantInt>(C0->getAggregateElement((unsigned)0)) 768e8d8bef9SDimitry Andric : nullptr; 769*fe6060f1SDimitry Andric auto *CI10 = 770e8d8bef9SDimitry Andric C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0)) 771e8d8bef9SDimitry Andric : nullptr; 772e8d8bef9SDimitry Andric 773e8d8bef9SDimitry Andric // Constant Fold - insert bottom Length bits starting at the Index'th bit. 774e8d8bef9SDimitry Andric if (CI00 && CI10) { 775e8d8bef9SDimitry Andric APInt V00 = CI00->getValue(); 776e8d8bef9SDimitry Andric APInt V10 = CI10->getValue(); 777e8d8bef9SDimitry Andric APInt Mask = APInt::getLowBitsSet(64, Length).shl(Index); 778e8d8bef9SDimitry Andric V00 = V00 & ~Mask; 779e8d8bef9SDimitry Andric V10 = V10.zextOrTrunc(Length).zextOrTrunc(64).shl(Index); 780e8d8bef9SDimitry Andric APInt Val = V00 | V10; 781e8d8bef9SDimitry Andric Type *IntTy64 = Type::getInt64Ty(II.getContext()); 782e8d8bef9SDimitry Andric Constant *Args[] = {ConstantInt::get(IntTy64, Val.getZExtValue()), 783e8d8bef9SDimitry Andric UndefValue::get(IntTy64)}; 784e8d8bef9SDimitry Andric return ConstantVector::get(Args); 785e8d8bef9SDimitry Andric } 786e8d8bef9SDimitry Andric 787e8d8bef9SDimitry Andric // If we were an INSERTQ call, we'll save demanded elements if we convert to 788e8d8bef9SDimitry Andric // INSERTQI. 789e8d8bef9SDimitry Andric if (II.getIntrinsicID() == Intrinsic::x86_sse4a_insertq) { 790e8d8bef9SDimitry Andric Type *IntTy8 = Type::getInt8Ty(II.getContext()); 791e8d8bef9SDimitry Andric Constant *CILength = ConstantInt::get(IntTy8, Length, false); 792e8d8bef9SDimitry Andric Constant *CIIndex = ConstantInt::get(IntTy8, Index, false); 793e8d8bef9SDimitry Andric 794e8d8bef9SDimitry Andric Value *Args[] = {Op0, Op1, CILength, CIIndex}; 795e8d8bef9SDimitry Andric Module *M = II.getModule(); 796e8d8bef9SDimitry Andric Function *F = Intrinsic::getDeclaration(M, Intrinsic::x86_sse4a_insertqi); 797e8d8bef9SDimitry Andric return Builder.CreateCall(F, Args); 798e8d8bef9SDimitry Andric } 799e8d8bef9SDimitry Andric 800e8d8bef9SDimitry Andric return nullptr; 801e8d8bef9SDimitry Andric } 802e8d8bef9SDimitry Andric 803e8d8bef9SDimitry Andric /// Attempt to convert pshufb* to shufflevector if the mask is constant. 804e8d8bef9SDimitry Andric static Value *simplifyX86pshufb(const IntrinsicInst &II, 805e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 806*fe6060f1SDimitry Andric auto *V = dyn_cast<Constant>(II.getArgOperand(1)); 807e8d8bef9SDimitry Andric if (!V) 808e8d8bef9SDimitry Andric return nullptr; 809e8d8bef9SDimitry Andric 810e8d8bef9SDimitry Andric auto *VecTy = cast<FixedVectorType>(II.getType()); 811e8d8bef9SDimitry Andric unsigned NumElts = VecTy->getNumElements(); 812e8d8bef9SDimitry Andric assert((NumElts == 16 || NumElts == 32 || NumElts == 64) && 813e8d8bef9SDimitry Andric "Unexpected number of elements in shuffle mask!"); 814e8d8bef9SDimitry Andric 815e8d8bef9SDimitry Andric // Construct a shuffle mask from constant integers or UNDEFs. 816e8d8bef9SDimitry Andric int Indexes[64]; 817e8d8bef9SDimitry Andric 818e8d8bef9SDimitry Andric // Each byte in the shuffle control mask forms an index to permute the 819e8d8bef9SDimitry Andric // corresponding byte in the destination operand. 820e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumElts; ++I) { 821e8d8bef9SDimitry Andric Constant *COp = V->getAggregateElement(I); 822e8d8bef9SDimitry Andric if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) 823e8d8bef9SDimitry Andric return nullptr; 824e8d8bef9SDimitry Andric 825e8d8bef9SDimitry Andric if (isa<UndefValue>(COp)) { 826e8d8bef9SDimitry Andric Indexes[I] = -1; 827e8d8bef9SDimitry Andric continue; 828e8d8bef9SDimitry Andric } 829e8d8bef9SDimitry Andric 830e8d8bef9SDimitry Andric int8_t Index = cast<ConstantInt>(COp)->getValue().getZExtValue(); 831e8d8bef9SDimitry Andric 832e8d8bef9SDimitry Andric // If the most significant bit (bit[7]) of each byte of the shuffle 833e8d8bef9SDimitry Andric // control mask is set, then zero is written in the result byte. 834e8d8bef9SDimitry Andric // The zero vector is in the right-hand side of the resulting 835e8d8bef9SDimitry Andric // shufflevector. 836e8d8bef9SDimitry Andric 837e8d8bef9SDimitry Andric // The value of each index for the high 128-bit lane is the least 838e8d8bef9SDimitry Andric // significant 4 bits of the respective shuffle control byte. 839e8d8bef9SDimitry Andric Index = ((Index < 0) ? NumElts : Index & 0x0F) + (I & 0xF0); 840e8d8bef9SDimitry Andric Indexes[I] = Index; 841e8d8bef9SDimitry Andric } 842e8d8bef9SDimitry Andric 843e8d8bef9SDimitry Andric auto V1 = II.getArgOperand(0); 844e8d8bef9SDimitry Andric auto V2 = Constant::getNullValue(VecTy); 845e8d8bef9SDimitry Andric return Builder.CreateShuffleVector(V1, V2, makeArrayRef(Indexes, NumElts)); 846e8d8bef9SDimitry Andric } 847e8d8bef9SDimitry Andric 848e8d8bef9SDimitry Andric /// Attempt to convert vpermilvar* to shufflevector if the mask is constant. 849e8d8bef9SDimitry Andric static Value *simplifyX86vpermilvar(const IntrinsicInst &II, 850e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 851*fe6060f1SDimitry Andric auto *V = dyn_cast<Constant>(II.getArgOperand(1)); 852e8d8bef9SDimitry Andric if (!V) 853e8d8bef9SDimitry Andric return nullptr; 854e8d8bef9SDimitry Andric 855e8d8bef9SDimitry Andric auto *VecTy = cast<FixedVectorType>(II.getType()); 856e8d8bef9SDimitry Andric unsigned NumElts = VecTy->getNumElements(); 857e8d8bef9SDimitry Andric bool IsPD = VecTy->getScalarType()->isDoubleTy(); 858e8d8bef9SDimitry Andric unsigned NumLaneElts = IsPD ? 2 : 4; 859e8d8bef9SDimitry Andric assert(NumElts == 16 || NumElts == 8 || NumElts == 4 || NumElts == 2); 860e8d8bef9SDimitry Andric 861e8d8bef9SDimitry Andric // Construct a shuffle mask from constant integers or UNDEFs. 862e8d8bef9SDimitry Andric int Indexes[16]; 863e8d8bef9SDimitry Andric 864e8d8bef9SDimitry Andric // The intrinsics only read one or two bits, clear the rest. 865e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumElts; ++I) { 866e8d8bef9SDimitry Andric Constant *COp = V->getAggregateElement(I); 867e8d8bef9SDimitry Andric if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) 868e8d8bef9SDimitry Andric return nullptr; 869e8d8bef9SDimitry Andric 870e8d8bef9SDimitry Andric if (isa<UndefValue>(COp)) { 871e8d8bef9SDimitry Andric Indexes[I] = -1; 872e8d8bef9SDimitry Andric continue; 873e8d8bef9SDimitry Andric } 874e8d8bef9SDimitry Andric 875e8d8bef9SDimitry Andric APInt Index = cast<ConstantInt>(COp)->getValue(); 876e8d8bef9SDimitry Andric Index = Index.zextOrTrunc(32).getLoBits(2); 877e8d8bef9SDimitry Andric 878e8d8bef9SDimitry Andric // The PD variants uses bit 1 to select per-lane element index, so 879e8d8bef9SDimitry Andric // shift down to convert to generic shuffle mask index. 880e8d8bef9SDimitry Andric if (IsPD) 881e8d8bef9SDimitry Andric Index.lshrInPlace(1); 882e8d8bef9SDimitry Andric 883e8d8bef9SDimitry Andric // The _256 variants are a bit trickier since the mask bits always index 884e8d8bef9SDimitry Andric // into the corresponding 128 half. In order to convert to a generic 885e8d8bef9SDimitry Andric // shuffle, we have to make that explicit. 886e8d8bef9SDimitry Andric Index += APInt(32, (I / NumLaneElts) * NumLaneElts); 887e8d8bef9SDimitry Andric 888e8d8bef9SDimitry Andric Indexes[I] = Index.getZExtValue(); 889e8d8bef9SDimitry Andric } 890e8d8bef9SDimitry Andric 891e8d8bef9SDimitry Andric auto V1 = II.getArgOperand(0); 892e8d8bef9SDimitry Andric return Builder.CreateShuffleVector(V1, makeArrayRef(Indexes, NumElts)); 893e8d8bef9SDimitry Andric } 894e8d8bef9SDimitry Andric 895e8d8bef9SDimitry Andric /// Attempt to convert vpermd/vpermps to shufflevector if the mask is constant. 896e8d8bef9SDimitry Andric static Value *simplifyX86vpermv(const IntrinsicInst &II, 897e8d8bef9SDimitry Andric InstCombiner::BuilderTy &Builder) { 898e8d8bef9SDimitry Andric auto *V = dyn_cast<Constant>(II.getArgOperand(1)); 899e8d8bef9SDimitry Andric if (!V) 900e8d8bef9SDimitry Andric return nullptr; 901e8d8bef9SDimitry Andric 902e8d8bef9SDimitry Andric auto *VecTy = cast<FixedVectorType>(II.getType()); 903e8d8bef9SDimitry Andric unsigned Size = VecTy->getNumElements(); 904e8d8bef9SDimitry Andric assert((Size == 4 || Size == 8 || Size == 16 || Size == 32 || Size == 64) && 905e8d8bef9SDimitry Andric "Unexpected shuffle mask size"); 906e8d8bef9SDimitry Andric 907e8d8bef9SDimitry Andric // Construct a shuffle mask from constant integers or UNDEFs. 908e8d8bef9SDimitry Andric int Indexes[64]; 909e8d8bef9SDimitry Andric 910e8d8bef9SDimitry Andric for (unsigned I = 0; I < Size; ++I) { 911e8d8bef9SDimitry Andric Constant *COp = V->getAggregateElement(I); 912e8d8bef9SDimitry Andric if (!COp || (!isa<UndefValue>(COp) && !isa<ConstantInt>(COp))) 913e8d8bef9SDimitry Andric return nullptr; 914e8d8bef9SDimitry Andric 915e8d8bef9SDimitry Andric if (isa<UndefValue>(COp)) { 916e8d8bef9SDimitry Andric Indexes[I] = -1; 917e8d8bef9SDimitry Andric continue; 918e8d8bef9SDimitry Andric } 919e8d8bef9SDimitry Andric 920e8d8bef9SDimitry Andric uint32_t Index = cast<ConstantInt>(COp)->getZExtValue(); 921e8d8bef9SDimitry Andric Index &= Size - 1; 922e8d8bef9SDimitry Andric Indexes[I] = Index; 923e8d8bef9SDimitry Andric } 924e8d8bef9SDimitry Andric 925e8d8bef9SDimitry Andric auto V1 = II.getArgOperand(0); 926e8d8bef9SDimitry Andric return Builder.CreateShuffleVector(V1, makeArrayRef(Indexes, Size)); 927e8d8bef9SDimitry Andric } 928e8d8bef9SDimitry Andric 929e8d8bef9SDimitry Andric Optional<Instruction *> 930e8d8bef9SDimitry Andric X86TTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const { 931e8d8bef9SDimitry Andric auto SimplifyDemandedVectorEltsLow = [&IC](Value *Op, unsigned Width, 932e8d8bef9SDimitry Andric unsigned DemandedWidth) { 933e8d8bef9SDimitry Andric APInt UndefElts(Width, 0); 934e8d8bef9SDimitry Andric APInt DemandedElts = APInt::getLowBitsSet(Width, DemandedWidth); 935e8d8bef9SDimitry Andric return IC.SimplifyDemandedVectorElts(Op, DemandedElts, UndefElts); 936e8d8bef9SDimitry Andric }; 937e8d8bef9SDimitry Andric 938e8d8bef9SDimitry Andric Intrinsic::ID IID = II.getIntrinsicID(); 939e8d8bef9SDimitry Andric switch (IID) { 940e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_bextr_32: 941e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_bextr_64: 942e8d8bef9SDimitry Andric case Intrinsic::x86_tbm_bextri_u32: 943e8d8bef9SDimitry Andric case Intrinsic::x86_tbm_bextri_u64: 944e8d8bef9SDimitry Andric // If the RHS is a constant we can try some simplifications. 945e8d8bef9SDimitry Andric if (auto *C = dyn_cast<ConstantInt>(II.getArgOperand(1))) { 946e8d8bef9SDimitry Andric uint64_t Shift = C->getZExtValue(); 947e8d8bef9SDimitry Andric uint64_t Length = (Shift >> 8) & 0xff; 948e8d8bef9SDimitry Andric Shift &= 0xff; 949e8d8bef9SDimitry Andric unsigned BitWidth = II.getType()->getIntegerBitWidth(); 950e8d8bef9SDimitry Andric // If the length is 0 or the shift is out of range, replace with zero. 951e8d8bef9SDimitry Andric if (Length == 0 || Shift >= BitWidth) { 952e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), 0)); 953e8d8bef9SDimitry Andric } 954e8d8bef9SDimitry Andric // If the LHS is also a constant, we can completely constant fold this. 955e8d8bef9SDimitry Andric if (auto *InC = dyn_cast<ConstantInt>(II.getArgOperand(0))) { 956e8d8bef9SDimitry Andric uint64_t Result = InC->getZExtValue() >> Shift; 957e8d8bef9SDimitry Andric if (Length > BitWidth) 958e8d8bef9SDimitry Andric Length = BitWidth; 959e8d8bef9SDimitry Andric Result &= maskTrailingOnes<uint64_t>(Length); 960e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, 961e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), Result)); 962e8d8bef9SDimitry Andric } 963e8d8bef9SDimitry Andric // TODO should we turn this into 'and' if shift is 0? Or 'shl' if we 964e8d8bef9SDimitry Andric // are only masking bits that a shift already cleared? 965e8d8bef9SDimitry Andric } 966e8d8bef9SDimitry Andric break; 967e8d8bef9SDimitry Andric 968e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_bzhi_32: 969e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_bzhi_64: 970e8d8bef9SDimitry Andric // If the RHS is a constant we can try some simplifications. 971e8d8bef9SDimitry Andric if (auto *C = dyn_cast<ConstantInt>(II.getArgOperand(1))) { 972e8d8bef9SDimitry Andric uint64_t Index = C->getZExtValue() & 0xff; 973e8d8bef9SDimitry Andric unsigned BitWidth = II.getType()->getIntegerBitWidth(); 974e8d8bef9SDimitry Andric if (Index >= BitWidth) { 975e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, II.getArgOperand(0)); 976e8d8bef9SDimitry Andric } 977e8d8bef9SDimitry Andric if (Index == 0) { 978e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), 0)); 979e8d8bef9SDimitry Andric } 980e8d8bef9SDimitry Andric // If the LHS is also a constant, we can completely constant fold this. 981e8d8bef9SDimitry Andric if (auto *InC = dyn_cast<ConstantInt>(II.getArgOperand(0))) { 982e8d8bef9SDimitry Andric uint64_t Result = InC->getZExtValue(); 983e8d8bef9SDimitry Andric Result &= maskTrailingOnes<uint64_t>(Index); 984e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, 985e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), Result)); 986e8d8bef9SDimitry Andric } 987e8d8bef9SDimitry Andric // TODO should we convert this to an AND if the RHS is constant? 988e8d8bef9SDimitry Andric } 989e8d8bef9SDimitry Andric break; 990e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_pext_32: 991e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_pext_64: 992e8d8bef9SDimitry Andric if (auto *MaskC = dyn_cast<ConstantInt>(II.getArgOperand(1))) { 993e8d8bef9SDimitry Andric if (MaskC->isNullValue()) { 994e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), 0)); 995e8d8bef9SDimitry Andric } 996e8d8bef9SDimitry Andric if (MaskC->isAllOnesValue()) { 997e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, II.getArgOperand(0)); 998e8d8bef9SDimitry Andric } 999e8d8bef9SDimitry Andric 1000e8d8bef9SDimitry Andric if (MaskC->getValue().isShiftedMask()) { 1001e8d8bef9SDimitry Andric // any single contingous sequence of 1s anywhere in the mask simply 1002e8d8bef9SDimitry Andric // describes a subset of the input bits shifted to the appropriate 1003e8d8bef9SDimitry Andric // position. Replace with the straight forward IR. 1004e8d8bef9SDimitry Andric unsigned ShiftAmount = MaskC->getValue().countTrailingZeros(); 1005e8d8bef9SDimitry Andric Value *Input = II.getArgOperand(0); 1006e8d8bef9SDimitry Andric Value *Masked = IC.Builder.CreateAnd(Input, II.getArgOperand(1)); 1007e8d8bef9SDimitry Andric Value *Shifted = IC.Builder.CreateLShr(Masked, 1008e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), 1009e8d8bef9SDimitry Andric ShiftAmount)); 1010e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Shifted); 1011e8d8bef9SDimitry Andric } 1012e8d8bef9SDimitry Andric 1013e8d8bef9SDimitry Andric 1014e8d8bef9SDimitry Andric if (auto *SrcC = dyn_cast<ConstantInt>(II.getArgOperand(0))) { 1015e8d8bef9SDimitry Andric uint64_t Src = SrcC->getZExtValue(); 1016e8d8bef9SDimitry Andric uint64_t Mask = MaskC->getZExtValue(); 1017e8d8bef9SDimitry Andric uint64_t Result = 0; 1018e8d8bef9SDimitry Andric uint64_t BitToSet = 1; 1019e8d8bef9SDimitry Andric 1020e8d8bef9SDimitry Andric while (Mask) { 1021e8d8bef9SDimitry Andric // Isolate lowest set bit. 1022e8d8bef9SDimitry Andric uint64_t BitToTest = Mask & -Mask; 1023e8d8bef9SDimitry Andric if (BitToTest & Src) 1024e8d8bef9SDimitry Andric Result |= BitToSet; 1025e8d8bef9SDimitry Andric 1026e8d8bef9SDimitry Andric BitToSet <<= 1; 1027e8d8bef9SDimitry Andric // Clear lowest set bit. 1028e8d8bef9SDimitry Andric Mask &= Mask - 1; 1029e8d8bef9SDimitry Andric } 1030e8d8bef9SDimitry Andric 1031e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, 1032e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), Result)); 1033e8d8bef9SDimitry Andric } 1034e8d8bef9SDimitry Andric } 1035e8d8bef9SDimitry Andric break; 1036e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_pdep_32: 1037e8d8bef9SDimitry Andric case Intrinsic::x86_bmi_pdep_64: 1038e8d8bef9SDimitry Andric if (auto *MaskC = dyn_cast<ConstantInt>(II.getArgOperand(1))) { 1039e8d8bef9SDimitry Andric if (MaskC->isNullValue()) { 1040e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, ConstantInt::get(II.getType(), 0)); 1041e8d8bef9SDimitry Andric } 1042e8d8bef9SDimitry Andric if (MaskC->isAllOnesValue()) { 1043e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, II.getArgOperand(0)); 1044e8d8bef9SDimitry Andric } 1045e8d8bef9SDimitry Andric if (MaskC->getValue().isShiftedMask()) { 1046e8d8bef9SDimitry Andric // any single contingous sequence of 1s anywhere in the mask simply 1047e8d8bef9SDimitry Andric // describes a subset of the input bits shifted to the appropriate 1048e8d8bef9SDimitry Andric // position. Replace with the straight forward IR. 1049e8d8bef9SDimitry Andric unsigned ShiftAmount = MaskC->getValue().countTrailingZeros(); 1050e8d8bef9SDimitry Andric Value *Input = II.getArgOperand(0); 1051e8d8bef9SDimitry Andric Value *Shifted = IC.Builder.CreateShl(Input, 1052e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), 1053e8d8bef9SDimitry Andric ShiftAmount)); 1054e8d8bef9SDimitry Andric Value *Masked = IC.Builder.CreateAnd(Shifted, II.getArgOperand(1)); 1055e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Masked); 1056e8d8bef9SDimitry Andric } 1057e8d8bef9SDimitry Andric 1058e8d8bef9SDimitry Andric if (auto *SrcC = dyn_cast<ConstantInt>(II.getArgOperand(0))) { 1059e8d8bef9SDimitry Andric uint64_t Src = SrcC->getZExtValue(); 1060e8d8bef9SDimitry Andric uint64_t Mask = MaskC->getZExtValue(); 1061e8d8bef9SDimitry Andric uint64_t Result = 0; 1062e8d8bef9SDimitry Andric uint64_t BitToTest = 1; 1063e8d8bef9SDimitry Andric 1064e8d8bef9SDimitry Andric while (Mask) { 1065e8d8bef9SDimitry Andric // Isolate lowest set bit. 1066e8d8bef9SDimitry Andric uint64_t BitToSet = Mask & -Mask; 1067e8d8bef9SDimitry Andric if (BitToTest & Src) 1068e8d8bef9SDimitry Andric Result |= BitToSet; 1069e8d8bef9SDimitry Andric 1070e8d8bef9SDimitry Andric BitToTest <<= 1; 1071e8d8bef9SDimitry Andric // Clear lowest set bit; 1072e8d8bef9SDimitry Andric Mask &= Mask - 1; 1073e8d8bef9SDimitry Andric } 1074e8d8bef9SDimitry Andric 1075e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, 1076e8d8bef9SDimitry Andric ConstantInt::get(II.getType(), Result)); 1077e8d8bef9SDimitry Andric } 1078e8d8bef9SDimitry Andric } 1079e8d8bef9SDimitry Andric break; 1080e8d8bef9SDimitry Andric 1081e8d8bef9SDimitry Andric case Intrinsic::x86_sse_cvtss2si: 1082e8d8bef9SDimitry Andric case Intrinsic::x86_sse_cvtss2si64: 1083e8d8bef9SDimitry Andric case Intrinsic::x86_sse_cvttss2si: 1084e8d8bef9SDimitry Andric case Intrinsic::x86_sse_cvttss2si64: 1085e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_cvtsd2si: 1086e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_cvtsd2si64: 1087e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_cvttsd2si: 1088e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_cvttsd2si64: 1089e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtss2si32: 1090e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtss2si64: 1091e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtss2usi32: 1092e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtss2usi64: 1093e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtsd2si32: 1094e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtsd2si64: 1095e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtsd2usi32: 1096e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcvtsd2usi64: 1097e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttss2si: 1098e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttss2si64: 1099e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttss2usi: 1100e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttss2usi64: 1101e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttsd2si: 1102e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttsd2si64: 1103e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttsd2usi: 1104e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_cvttsd2usi64: { 1105e8d8bef9SDimitry Andric // These intrinsics only demand the 0th element of their input vectors. If 1106e8d8bef9SDimitry Andric // we can simplify the input based on that, do so now. 1107e8d8bef9SDimitry Andric Value *Arg = II.getArgOperand(0); 1108e8d8bef9SDimitry Andric unsigned VWidth = cast<FixedVectorType>(Arg->getType())->getNumElements(); 1109e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Arg, VWidth, 1)) { 1110e8d8bef9SDimitry Andric return IC.replaceOperand(II, 0, V); 1111e8d8bef9SDimitry Andric } 1112e8d8bef9SDimitry Andric break; 1113e8d8bef9SDimitry Andric } 1114e8d8bef9SDimitry Andric 1115e8d8bef9SDimitry Andric case Intrinsic::x86_mmx_pmovmskb: 1116e8d8bef9SDimitry Andric case Intrinsic::x86_sse_movmsk_ps: 1117e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_movmsk_pd: 1118e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pmovmskb_128: 1119e8d8bef9SDimitry Andric case Intrinsic::x86_avx_movmsk_pd_256: 1120e8d8bef9SDimitry Andric case Intrinsic::x86_avx_movmsk_ps_256: 1121e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pmovmskb: 1122e8d8bef9SDimitry Andric if (Value *V = simplifyX86movmsk(II, IC.Builder)) { 1123e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1124e8d8bef9SDimitry Andric } 1125e8d8bef9SDimitry Andric break; 1126e8d8bef9SDimitry Andric 1127e8d8bef9SDimitry Andric case Intrinsic::x86_sse_comieq_ss: 1128e8d8bef9SDimitry Andric case Intrinsic::x86_sse_comige_ss: 1129e8d8bef9SDimitry Andric case Intrinsic::x86_sse_comigt_ss: 1130e8d8bef9SDimitry Andric case Intrinsic::x86_sse_comile_ss: 1131e8d8bef9SDimitry Andric case Intrinsic::x86_sse_comilt_ss: 1132e8d8bef9SDimitry Andric case Intrinsic::x86_sse_comineq_ss: 1133e8d8bef9SDimitry Andric case Intrinsic::x86_sse_ucomieq_ss: 1134e8d8bef9SDimitry Andric case Intrinsic::x86_sse_ucomige_ss: 1135e8d8bef9SDimitry Andric case Intrinsic::x86_sse_ucomigt_ss: 1136e8d8bef9SDimitry Andric case Intrinsic::x86_sse_ucomile_ss: 1137e8d8bef9SDimitry Andric case Intrinsic::x86_sse_ucomilt_ss: 1138e8d8bef9SDimitry Andric case Intrinsic::x86_sse_ucomineq_ss: 1139e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_comieq_sd: 1140e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_comige_sd: 1141e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_comigt_sd: 1142e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_comile_sd: 1143e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_comilt_sd: 1144e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_comineq_sd: 1145e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_ucomieq_sd: 1146e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_ucomige_sd: 1147e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_ucomigt_sd: 1148e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_ucomile_sd: 1149e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_ucomilt_sd: 1150e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_ucomineq_sd: 1151e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcomi_ss: 1152e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vcomi_sd: 1153e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_cmp_ss: 1154e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_cmp_sd: { 1155e8d8bef9SDimitry Andric // These intrinsics only demand the 0th element of their input vectors. If 1156e8d8bef9SDimitry Andric // we can simplify the input based on that, do so now. 1157e8d8bef9SDimitry Andric bool MadeChange = false; 1158e8d8bef9SDimitry Andric Value *Arg0 = II.getArgOperand(0); 1159e8d8bef9SDimitry Andric Value *Arg1 = II.getArgOperand(1); 1160e8d8bef9SDimitry Andric unsigned VWidth = cast<FixedVectorType>(Arg0->getType())->getNumElements(); 1161e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Arg0, VWidth, 1)) { 1162e8d8bef9SDimitry Andric IC.replaceOperand(II, 0, V); 1163e8d8bef9SDimitry Andric MadeChange = true; 1164e8d8bef9SDimitry Andric } 1165e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, 1)) { 1166e8d8bef9SDimitry Andric IC.replaceOperand(II, 1, V); 1167e8d8bef9SDimitry Andric MadeChange = true; 1168e8d8bef9SDimitry Andric } 1169e8d8bef9SDimitry Andric if (MadeChange) { 1170e8d8bef9SDimitry Andric return &II; 1171e8d8bef9SDimitry Andric } 1172e8d8bef9SDimitry Andric break; 1173e8d8bef9SDimitry Andric } 1174e8d8bef9SDimitry Andric 1175e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_add_ps_512: 1176e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_div_ps_512: 1177e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mul_ps_512: 1178e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_sub_ps_512: 1179e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_add_pd_512: 1180e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_div_pd_512: 1181e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mul_pd_512: 1182e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_sub_pd_512: 1183e8d8bef9SDimitry Andric // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular 1184e8d8bef9SDimitry Andric // IR operations. 1185e8d8bef9SDimitry Andric if (auto *R = dyn_cast<ConstantInt>(II.getArgOperand(2))) { 1186e8d8bef9SDimitry Andric if (R->getValue() == 4) { 1187e8d8bef9SDimitry Andric Value *Arg0 = II.getArgOperand(0); 1188e8d8bef9SDimitry Andric Value *Arg1 = II.getArgOperand(1); 1189e8d8bef9SDimitry Andric 1190e8d8bef9SDimitry Andric Value *V; 1191e8d8bef9SDimitry Andric switch (IID) { 1192e8d8bef9SDimitry Andric default: 1193e8d8bef9SDimitry Andric llvm_unreachable("Case stmts out of sync!"); 1194e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_add_ps_512: 1195e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_add_pd_512: 1196e8d8bef9SDimitry Andric V = IC.Builder.CreateFAdd(Arg0, Arg1); 1197e8d8bef9SDimitry Andric break; 1198e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_sub_ps_512: 1199e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_sub_pd_512: 1200e8d8bef9SDimitry Andric V = IC.Builder.CreateFSub(Arg0, Arg1); 1201e8d8bef9SDimitry Andric break; 1202e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mul_ps_512: 1203e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mul_pd_512: 1204e8d8bef9SDimitry Andric V = IC.Builder.CreateFMul(Arg0, Arg1); 1205e8d8bef9SDimitry Andric break; 1206e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_div_ps_512: 1207e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_div_pd_512: 1208e8d8bef9SDimitry Andric V = IC.Builder.CreateFDiv(Arg0, Arg1); 1209e8d8bef9SDimitry Andric break; 1210e8d8bef9SDimitry Andric } 1211e8d8bef9SDimitry Andric 1212e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1213e8d8bef9SDimitry Andric } 1214e8d8bef9SDimitry Andric } 1215e8d8bef9SDimitry Andric break; 1216e8d8bef9SDimitry Andric 1217e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_add_ss_round: 1218e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_div_ss_round: 1219e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_mul_ss_round: 1220e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_sub_ss_round: 1221e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_add_sd_round: 1222e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_div_sd_round: 1223e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_mul_sd_round: 1224e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_sub_sd_round: 1225e8d8bef9SDimitry Andric // If the rounding mode is CUR_DIRECTION(4) we can turn these into regular 1226e8d8bef9SDimitry Andric // IR operations. 1227e8d8bef9SDimitry Andric if (auto *R = dyn_cast<ConstantInt>(II.getArgOperand(4))) { 1228e8d8bef9SDimitry Andric if (R->getValue() == 4) { 1229e8d8bef9SDimitry Andric // Extract the element as scalars. 1230e8d8bef9SDimitry Andric Value *Arg0 = II.getArgOperand(0); 1231e8d8bef9SDimitry Andric Value *Arg1 = II.getArgOperand(1); 1232e8d8bef9SDimitry Andric Value *LHS = IC.Builder.CreateExtractElement(Arg0, (uint64_t)0); 1233e8d8bef9SDimitry Andric Value *RHS = IC.Builder.CreateExtractElement(Arg1, (uint64_t)0); 1234e8d8bef9SDimitry Andric 1235e8d8bef9SDimitry Andric Value *V; 1236e8d8bef9SDimitry Andric switch (IID) { 1237e8d8bef9SDimitry Andric default: 1238e8d8bef9SDimitry Andric llvm_unreachable("Case stmts out of sync!"); 1239e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_add_ss_round: 1240e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_add_sd_round: 1241e8d8bef9SDimitry Andric V = IC.Builder.CreateFAdd(LHS, RHS); 1242e8d8bef9SDimitry Andric break; 1243e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_sub_ss_round: 1244e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_sub_sd_round: 1245e8d8bef9SDimitry Andric V = IC.Builder.CreateFSub(LHS, RHS); 1246e8d8bef9SDimitry Andric break; 1247e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_mul_ss_round: 1248e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_mul_sd_round: 1249e8d8bef9SDimitry Andric V = IC.Builder.CreateFMul(LHS, RHS); 1250e8d8bef9SDimitry Andric break; 1251e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_div_ss_round: 1252e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_div_sd_round: 1253e8d8bef9SDimitry Andric V = IC.Builder.CreateFDiv(LHS, RHS); 1254e8d8bef9SDimitry Andric break; 1255e8d8bef9SDimitry Andric } 1256e8d8bef9SDimitry Andric 1257e8d8bef9SDimitry Andric // Handle the masking aspect of the intrinsic. 1258e8d8bef9SDimitry Andric Value *Mask = II.getArgOperand(3); 1259e8d8bef9SDimitry Andric auto *C = dyn_cast<ConstantInt>(Mask); 1260e8d8bef9SDimitry Andric // We don't need a select if we know the mask bit is a 1. 1261e8d8bef9SDimitry Andric if (!C || !C->getValue()[0]) { 1262e8d8bef9SDimitry Andric // Cast the mask to an i1 vector and then extract the lowest element. 1263e8d8bef9SDimitry Andric auto *MaskTy = FixedVectorType::get( 1264e8d8bef9SDimitry Andric IC.Builder.getInt1Ty(), 1265e8d8bef9SDimitry Andric cast<IntegerType>(Mask->getType())->getBitWidth()); 1266e8d8bef9SDimitry Andric Mask = IC.Builder.CreateBitCast(Mask, MaskTy); 1267e8d8bef9SDimitry Andric Mask = IC.Builder.CreateExtractElement(Mask, (uint64_t)0); 1268e8d8bef9SDimitry Andric // Extract the lowest element from the passthru operand. 1269e8d8bef9SDimitry Andric Value *Passthru = 1270e8d8bef9SDimitry Andric IC.Builder.CreateExtractElement(II.getArgOperand(2), (uint64_t)0); 1271e8d8bef9SDimitry Andric V = IC.Builder.CreateSelect(Mask, V, Passthru); 1272e8d8bef9SDimitry Andric } 1273e8d8bef9SDimitry Andric 1274e8d8bef9SDimitry Andric // Insert the result back into the original argument 0. 1275e8d8bef9SDimitry Andric V = IC.Builder.CreateInsertElement(Arg0, V, (uint64_t)0); 1276e8d8bef9SDimitry Andric 1277e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1278e8d8bef9SDimitry Andric } 1279e8d8bef9SDimitry Andric } 1280e8d8bef9SDimitry Andric break; 1281e8d8bef9SDimitry Andric 1282e8d8bef9SDimitry Andric // Constant fold ashr( <A x Bi>, Ci ). 1283e8d8bef9SDimitry Andric // Constant fold lshr( <A x Bi>, Ci ). 1284e8d8bef9SDimitry Andric // Constant fold shl( <A x Bi>, Ci ). 1285e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrai_d: 1286e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrai_w: 1287e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrai_d: 1288e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrai_w: 1289e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_q_128: 1290e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_q_256: 1291e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_d_512: 1292e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_q_512: 1293e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrai_w_512: 1294e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrli_d: 1295e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrli_q: 1296e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrli_w: 1297e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrli_d: 1298e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrli_q: 1299e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrli_w: 1300e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrli_d_512: 1301e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrli_q_512: 1302e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrli_w_512: 1303e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pslli_d: 1304e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pslli_q: 1305e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pslli_w: 1306e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pslli_d: 1307e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pslli_q: 1308e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pslli_w: 1309e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pslli_d_512: 1310e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pslli_q_512: 1311e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pslli_w_512: 1312e8d8bef9SDimitry Andric if (Value *V = simplifyX86immShift(II, IC.Builder)) { 1313e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1314e8d8bef9SDimitry Andric } 1315e8d8bef9SDimitry Andric break; 1316e8d8bef9SDimitry Andric 1317e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psra_d: 1318e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psra_w: 1319e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psra_d: 1320e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psra_w: 1321e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_q_128: 1322e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_q_256: 1323e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_d_512: 1324e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_q_512: 1325e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psra_w_512: 1326e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrl_d: 1327e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrl_q: 1328e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psrl_w: 1329e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrl_d: 1330e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrl_q: 1331e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrl_w: 1332e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrl_d_512: 1333e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrl_q_512: 1334e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrl_w_512: 1335e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psll_d: 1336e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psll_q: 1337e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_psll_w: 1338e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psll_d: 1339e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psll_q: 1340e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psll_w: 1341e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psll_d_512: 1342e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psll_q_512: 1343e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psll_w_512: { 1344e8d8bef9SDimitry Andric if (Value *V = simplifyX86immShift(II, IC.Builder)) { 1345e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1346e8d8bef9SDimitry Andric } 1347e8d8bef9SDimitry Andric 1348e8d8bef9SDimitry Andric // SSE2/AVX2 uses only the first 64-bits of the 128-bit vector 1349e8d8bef9SDimitry Andric // operand to compute the shift amount. 1350e8d8bef9SDimitry Andric Value *Arg1 = II.getArgOperand(1); 1351e8d8bef9SDimitry Andric assert(Arg1->getType()->getPrimitiveSizeInBits() == 128 && 1352e8d8bef9SDimitry Andric "Unexpected packed shift size"); 1353e8d8bef9SDimitry Andric unsigned VWidth = cast<FixedVectorType>(Arg1->getType())->getNumElements(); 1354e8d8bef9SDimitry Andric 1355e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Arg1, VWidth, VWidth / 2)) { 1356e8d8bef9SDimitry Andric return IC.replaceOperand(II, 1, V); 1357e8d8bef9SDimitry Andric } 1358e8d8bef9SDimitry Andric break; 1359e8d8bef9SDimitry Andric } 1360e8d8bef9SDimitry Andric 1361e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_d: 1362e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_d_256: 1363e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_q: 1364e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psllv_q_256: 1365e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_d_512: 1366e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_q_512: 1367e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_w_128: 1368e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_w_256: 1369e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psllv_w_512: 1370e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrav_d: 1371e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrav_d_256: 1372e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_q_128: 1373e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_q_256: 1374e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_d_512: 1375e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_q_512: 1376e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_w_128: 1377e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_w_256: 1378e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrav_w_512: 1379e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_d: 1380e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_d_256: 1381e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_q: 1382e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_psrlv_q_256: 1383e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_d_512: 1384e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_q_512: 1385e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_w_128: 1386e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_w_256: 1387e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_psrlv_w_512: 1388e8d8bef9SDimitry Andric if (Value *V = simplifyX86varShift(II, IC.Builder)) { 1389e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1390e8d8bef9SDimitry Andric } 1391e8d8bef9SDimitry Andric break; 1392e8d8bef9SDimitry Andric 1393e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_packssdw_128: 1394e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_packsswb_128: 1395e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packssdw: 1396e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packsswb: 1397e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packssdw_512: 1398e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packsswb_512: 1399e8d8bef9SDimitry Andric if (Value *V = simplifyX86pack(II, IC.Builder, true)) { 1400e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1401e8d8bef9SDimitry Andric } 1402e8d8bef9SDimitry Andric break; 1403e8d8bef9SDimitry Andric 1404e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_packuswb_128: 1405e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_packusdw: 1406e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packusdw: 1407e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packuswb: 1408e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packusdw_512: 1409e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packuswb_512: 1410e8d8bef9SDimitry Andric if (Value *V = simplifyX86pack(II, IC.Builder, false)) { 1411e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1412e8d8bef9SDimitry Andric } 1413e8d8bef9SDimitry Andric break; 1414e8d8bef9SDimitry Andric 1415e8d8bef9SDimitry Andric case Intrinsic::x86_pclmulqdq: 1416e8d8bef9SDimitry Andric case Intrinsic::x86_pclmulqdq_256: 1417e8d8bef9SDimitry Andric case Intrinsic::x86_pclmulqdq_512: { 1418e8d8bef9SDimitry Andric if (auto *C = dyn_cast<ConstantInt>(II.getArgOperand(2))) { 1419e8d8bef9SDimitry Andric unsigned Imm = C->getZExtValue(); 1420e8d8bef9SDimitry Andric 1421e8d8bef9SDimitry Andric bool MadeChange = false; 1422e8d8bef9SDimitry Andric Value *Arg0 = II.getArgOperand(0); 1423e8d8bef9SDimitry Andric Value *Arg1 = II.getArgOperand(1); 1424e8d8bef9SDimitry Andric unsigned VWidth = 1425e8d8bef9SDimitry Andric cast<FixedVectorType>(Arg0->getType())->getNumElements(); 1426e8d8bef9SDimitry Andric 1427e8d8bef9SDimitry Andric APInt UndefElts1(VWidth, 0); 1428e8d8bef9SDimitry Andric APInt DemandedElts1 = 1429e8d8bef9SDimitry Andric APInt::getSplat(VWidth, APInt(2, (Imm & 0x01) ? 2 : 1)); 1430e8d8bef9SDimitry Andric if (Value *V = 1431e8d8bef9SDimitry Andric IC.SimplifyDemandedVectorElts(Arg0, DemandedElts1, UndefElts1)) { 1432e8d8bef9SDimitry Andric IC.replaceOperand(II, 0, V); 1433e8d8bef9SDimitry Andric MadeChange = true; 1434e8d8bef9SDimitry Andric } 1435e8d8bef9SDimitry Andric 1436e8d8bef9SDimitry Andric APInt UndefElts2(VWidth, 0); 1437e8d8bef9SDimitry Andric APInt DemandedElts2 = 1438e8d8bef9SDimitry Andric APInt::getSplat(VWidth, APInt(2, (Imm & 0x10) ? 2 : 1)); 1439e8d8bef9SDimitry Andric if (Value *V = 1440e8d8bef9SDimitry Andric IC.SimplifyDemandedVectorElts(Arg1, DemandedElts2, UndefElts2)) { 1441e8d8bef9SDimitry Andric IC.replaceOperand(II, 1, V); 1442e8d8bef9SDimitry Andric MadeChange = true; 1443e8d8bef9SDimitry Andric } 1444e8d8bef9SDimitry Andric 1445e8d8bef9SDimitry Andric // If either input elements are undef, the result is zero. 1446e8d8bef9SDimitry Andric if (DemandedElts1.isSubsetOf(UndefElts1) || 1447e8d8bef9SDimitry Andric DemandedElts2.isSubsetOf(UndefElts2)) { 1448e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, 1449e8d8bef9SDimitry Andric ConstantAggregateZero::get(II.getType())); 1450e8d8bef9SDimitry Andric } 1451e8d8bef9SDimitry Andric 1452e8d8bef9SDimitry Andric if (MadeChange) { 1453e8d8bef9SDimitry Andric return &II; 1454e8d8bef9SDimitry Andric } 1455e8d8bef9SDimitry Andric } 1456e8d8bef9SDimitry Andric break; 1457e8d8bef9SDimitry Andric } 1458e8d8bef9SDimitry Andric 1459e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_insertps: 1460e8d8bef9SDimitry Andric if (Value *V = simplifyX86insertps(II, IC.Builder)) { 1461e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1462e8d8bef9SDimitry Andric } 1463e8d8bef9SDimitry Andric break; 1464e8d8bef9SDimitry Andric 1465e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_extrq: { 1466e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1467e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 1468e8d8bef9SDimitry Andric unsigned VWidth0 = cast<FixedVectorType>(Op0->getType())->getNumElements(); 1469e8d8bef9SDimitry Andric unsigned VWidth1 = cast<FixedVectorType>(Op1->getType())->getNumElements(); 1470e8d8bef9SDimitry Andric assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && 1471e8d8bef9SDimitry Andric Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 && 1472e8d8bef9SDimitry Andric VWidth1 == 16 && "Unexpected operand sizes"); 1473e8d8bef9SDimitry Andric 1474e8d8bef9SDimitry Andric // See if we're dealing with constant values. 1475*fe6060f1SDimitry Andric auto *C1 = dyn_cast<Constant>(Op1); 1476*fe6060f1SDimitry Andric auto *CILength = 1477e8d8bef9SDimitry Andric C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)0)) 1478e8d8bef9SDimitry Andric : nullptr; 1479*fe6060f1SDimitry Andric auto *CIIndex = 1480e8d8bef9SDimitry Andric C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1)) 1481e8d8bef9SDimitry Andric : nullptr; 1482e8d8bef9SDimitry Andric 1483e8d8bef9SDimitry Andric // Attempt to simplify to a constant, shuffle vector or EXTRQI call. 1484e8d8bef9SDimitry Andric if (Value *V = simplifyX86extrq(II, Op0, CILength, CIIndex, IC.Builder)) { 1485e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1486e8d8bef9SDimitry Andric } 1487e8d8bef9SDimitry Andric 1488e8d8bef9SDimitry Andric // EXTRQ only uses the lowest 64-bits of the first 128-bit vector 1489e8d8bef9SDimitry Andric // operands and the lowest 16-bits of the second. 1490e8d8bef9SDimitry Andric bool MadeChange = false; 1491e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) { 1492e8d8bef9SDimitry Andric IC.replaceOperand(II, 0, V); 1493e8d8bef9SDimitry Andric MadeChange = true; 1494e8d8bef9SDimitry Andric } 1495e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 2)) { 1496e8d8bef9SDimitry Andric IC.replaceOperand(II, 1, V); 1497e8d8bef9SDimitry Andric MadeChange = true; 1498e8d8bef9SDimitry Andric } 1499e8d8bef9SDimitry Andric if (MadeChange) { 1500e8d8bef9SDimitry Andric return &II; 1501e8d8bef9SDimitry Andric } 1502e8d8bef9SDimitry Andric break; 1503e8d8bef9SDimitry Andric } 1504e8d8bef9SDimitry Andric 1505e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_extrqi: { 1506e8d8bef9SDimitry Andric // EXTRQI: Extract Length bits starting from Index. Zero pad the remaining 1507e8d8bef9SDimitry Andric // bits of the lower 64-bits. The upper 64-bits are undefined. 1508e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1509e8d8bef9SDimitry Andric unsigned VWidth = cast<FixedVectorType>(Op0->getType())->getNumElements(); 1510e8d8bef9SDimitry Andric assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 && 1511e8d8bef9SDimitry Andric "Unexpected operand size"); 1512e8d8bef9SDimitry Andric 1513e8d8bef9SDimitry Andric // See if we're dealing with constant values. 1514*fe6060f1SDimitry Andric auto *CILength = dyn_cast<ConstantInt>(II.getArgOperand(1)); 1515*fe6060f1SDimitry Andric auto *CIIndex = dyn_cast<ConstantInt>(II.getArgOperand(2)); 1516e8d8bef9SDimitry Andric 1517e8d8bef9SDimitry Andric // Attempt to simplify to a constant or shuffle vector. 1518e8d8bef9SDimitry Andric if (Value *V = simplifyX86extrq(II, Op0, CILength, CIIndex, IC.Builder)) { 1519e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1520e8d8bef9SDimitry Andric } 1521e8d8bef9SDimitry Andric 1522e8d8bef9SDimitry Andric // EXTRQI only uses the lowest 64-bits of the first 128-bit vector 1523e8d8bef9SDimitry Andric // operand. 1524e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) { 1525e8d8bef9SDimitry Andric return IC.replaceOperand(II, 0, V); 1526e8d8bef9SDimitry Andric } 1527e8d8bef9SDimitry Andric break; 1528e8d8bef9SDimitry Andric } 1529e8d8bef9SDimitry Andric 1530e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_insertq: { 1531e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1532e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 1533e8d8bef9SDimitry Andric unsigned VWidth = cast<FixedVectorType>(Op0->getType())->getNumElements(); 1534e8d8bef9SDimitry Andric assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && 1535e8d8bef9SDimitry Andric Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth == 2 && 1536e8d8bef9SDimitry Andric cast<FixedVectorType>(Op1->getType())->getNumElements() == 2 && 1537e8d8bef9SDimitry Andric "Unexpected operand size"); 1538e8d8bef9SDimitry Andric 1539e8d8bef9SDimitry Andric // See if we're dealing with constant values. 1540*fe6060f1SDimitry Andric auto *C1 = dyn_cast<Constant>(Op1); 1541*fe6060f1SDimitry Andric auto *CI11 = 1542e8d8bef9SDimitry Andric C1 ? dyn_cast_or_null<ConstantInt>(C1->getAggregateElement((unsigned)1)) 1543e8d8bef9SDimitry Andric : nullptr; 1544e8d8bef9SDimitry Andric 1545e8d8bef9SDimitry Andric // Attempt to simplify to a constant, shuffle vector or INSERTQI call. 1546e8d8bef9SDimitry Andric if (CI11) { 1547e8d8bef9SDimitry Andric const APInt &V11 = CI11->getValue(); 1548e8d8bef9SDimitry Andric APInt Len = V11.zextOrTrunc(6); 1549e8d8bef9SDimitry Andric APInt Idx = V11.lshr(8).zextOrTrunc(6); 1550e8d8bef9SDimitry Andric if (Value *V = simplifyX86insertq(II, Op0, Op1, Len, Idx, IC.Builder)) { 1551e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1552e8d8bef9SDimitry Andric } 1553e8d8bef9SDimitry Andric } 1554e8d8bef9SDimitry Andric 1555e8d8bef9SDimitry Andric // INSERTQ only uses the lowest 64-bits of the first 128-bit vector 1556e8d8bef9SDimitry Andric // operand. 1557e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth, 1)) { 1558e8d8bef9SDimitry Andric return IC.replaceOperand(II, 0, V); 1559e8d8bef9SDimitry Andric } 1560e8d8bef9SDimitry Andric break; 1561e8d8bef9SDimitry Andric } 1562e8d8bef9SDimitry Andric 1563e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_insertqi: { 1564e8d8bef9SDimitry Andric // INSERTQI: Extract lowest Length bits from lower half of second source and 1565e8d8bef9SDimitry Andric // insert over first source starting at Index bit. The upper 64-bits are 1566e8d8bef9SDimitry Andric // undefined. 1567e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1568e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 1569e8d8bef9SDimitry Andric unsigned VWidth0 = cast<FixedVectorType>(Op0->getType())->getNumElements(); 1570e8d8bef9SDimitry Andric unsigned VWidth1 = cast<FixedVectorType>(Op1->getType())->getNumElements(); 1571e8d8bef9SDimitry Andric assert(Op0->getType()->getPrimitiveSizeInBits() == 128 && 1572e8d8bef9SDimitry Andric Op1->getType()->getPrimitiveSizeInBits() == 128 && VWidth0 == 2 && 1573e8d8bef9SDimitry Andric VWidth1 == 2 && "Unexpected operand sizes"); 1574e8d8bef9SDimitry Andric 1575e8d8bef9SDimitry Andric // See if we're dealing with constant values. 1576*fe6060f1SDimitry Andric auto *CILength = dyn_cast<ConstantInt>(II.getArgOperand(2)); 1577*fe6060f1SDimitry Andric auto *CIIndex = dyn_cast<ConstantInt>(II.getArgOperand(3)); 1578e8d8bef9SDimitry Andric 1579e8d8bef9SDimitry Andric // Attempt to simplify to a constant or shuffle vector. 1580e8d8bef9SDimitry Andric if (CILength && CIIndex) { 1581e8d8bef9SDimitry Andric APInt Len = CILength->getValue().zextOrTrunc(6); 1582e8d8bef9SDimitry Andric APInt Idx = CIIndex->getValue().zextOrTrunc(6); 1583e8d8bef9SDimitry Andric if (Value *V = simplifyX86insertq(II, Op0, Op1, Len, Idx, IC.Builder)) { 1584e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1585e8d8bef9SDimitry Andric } 1586e8d8bef9SDimitry Andric } 1587e8d8bef9SDimitry Andric 1588e8d8bef9SDimitry Andric // INSERTQI only uses the lowest 64-bits of the first two 128-bit vector 1589e8d8bef9SDimitry Andric // operands. 1590e8d8bef9SDimitry Andric bool MadeChange = false; 1591e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Op0, VWidth0, 1)) { 1592e8d8bef9SDimitry Andric IC.replaceOperand(II, 0, V); 1593e8d8bef9SDimitry Andric MadeChange = true; 1594e8d8bef9SDimitry Andric } 1595e8d8bef9SDimitry Andric if (Value *V = SimplifyDemandedVectorEltsLow(Op1, VWidth1, 1)) { 1596e8d8bef9SDimitry Andric IC.replaceOperand(II, 1, V); 1597e8d8bef9SDimitry Andric MadeChange = true; 1598e8d8bef9SDimitry Andric } 1599e8d8bef9SDimitry Andric if (MadeChange) { 1600e8d8bef9SDimitry Andric return &II; 1601e8d8bef9SDimitry Andric } 1602e8d8bef9SDimitry Andric break; 1603e8d8bef9SDimitry Andric } 1604e8d8bef9SDimitry Andric 1605e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_pblendvb: 1606e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_blendvps: 1607e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_blendvpd: 1608e8d8bef9SDimitry Andric case Intrinsic::x86_avx_blendv_ps_256: 1609e8d8bef9SDimitry Andric case Intrinsic::x86_avx_blendv_pd_256: 1610e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pblendvb: { 1611e8d8bef9SDimitry Andric // fold (blend A, A, Mask) -> A 1612e8d8bef9SDimitry Andric Value *Op0 = II.getArgOperand(0); 1613e8d8bef9SDimitry Andric Value *Op1 = II.getArgOperand(1); 1614e8d8bef9SDimitry Andric Value *Mask = II.getArgOperand(2); 1615e8d8bef9SDimitry Andric if (Op0 == Op1) { 1616e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Op0); 1617e8d8bef9SDimitry Andric } 1618e8d8bef9SDimitry Andric 1619e8d8bef9SDimitry Andric // Zero Mask - select 1st argument. 1620e8d8bef9SDimitry Andric if (isa<ConstantAggregateZero>(Mask)) { 1621e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, Op0); 1622e8d8bef9SDimitry Andric } 1623e8d8bef9SDimitry Andric 1624e8d8bef9SDimitry Andric // Constant Mask - select 1st/2nd argument lane based on top bit of mask. 1625e8d8bef9SDimitry Andric if (auto *ConstantMask = dyn_cast<ConstantDataVector>(Mask)) { 1626e8d8bef9SDimitry Andric Constant *NewSelector = getNegativeIsTrueBoolVec(ConstantMask); 1627e8d8bef9SDimitry Andric return SelectInst::Create(NewSelector, Op1, Op0, "blendv"); 1628e8d8bef9SDimitry Andric } 1629e8d8bef9SDimitry Andric 1630e8d8bef9SDimitry Andric // Convert to a vector select if we can bypass casts and find a boolean 1631e8d8bef9SDimitry Andric // vector condition value. 1632e8d8bef9SDimitry Andric Value *BoolVec; 1633e8d8bef9SDimitry Andric Mask = InstCombiner::peekThroughBitcast(Mask); 1634e8d8bef9SDimitry Andric if (match(Mask, PatternMatch::m_SExt(PatternMatch::m_Value(BoolVec))) && 1635e8d8bef9SDimitry Andric BoolVec->getType()->isVectorTy() && 1636e8d8bef9SDimitry Andric BoolVec->getType()->getScalarSizeInBits() == 1) { 1637e8d8bef9SDimitry Andric assert(Mask->getType()->getPrimitiveSizeInBits() == 1638e8d8bef9SDimitry Andric II.getType()->getPrimitiveSizeInBits() && 1639e8d8bef9SDimitry Andric "Not expecting mask and operands with different sizes"); 1640e8d8bef9SDimitry Andric 1641e8d8bef9SDimitry Andric unsigned NumMaskElts = 1642e8d8bef9SDimitry Andric cast<FixedVectorType>(Mask->getType())->getNumElements(); 1643e8d8bef9SDimitry Andric unsigned NumOperandElts = 1644e8d8bef9SDimitry Andric cast<FixedVectorType>(II.getType())->getNumElements(); 1645e8d8bef9SDimitry Andric if (NumMaskElts == NumOperandElts) { 1646e8d8bef9SDimitry Andric return SelectInst::Create(BoolVec, Op1, Op0); 1647e8d8bef9SDimitry Andric } 1648e8d8bef9SDimitry Andric 1649e8d8bef9SDimitry Andric // If the mask has less elements than the operands, each mask bit maps to 1650e8d8bef9SDimitry Andric // multiple elements of the operands. Bitcast back and forth. 1651e8d8bef9SDimitry Andric if (NumMaskElts < NumOperandElts) { 1652e8d8bef9SDimitry Andric Value *CastOp0 = IC.Builder.CreateBitCast(Op0, Mask->getType()); 1653e8d8bef9SDimitry Andric Value *CastOp1 = IC.Builder.CreateBitCast(Op1, Mask->getType()); 1654e8d8bef9SDimitry Andric Value *Sel = IC.Builder.CreateSelect(BoolVec, CastOp1, CastOp0); 1655e8d8bef9SDimitry Andric return new BitCastInst(Sel, II.getType()); 1656e8d8bef9SDimitry Andric } 1657e8d8bef9SDimitry Andric } 1658e8d8bef9SDimitry Andric 1659e8d8bef9SDimitry Andric break; 1660e8d8bef9SDimitry Andric } 1661e8d8bef9SDimitry Andric 1662e8d8bef9SDimitry Andric case Intrinsic::x86_ssse3_pshuf_b_128: 1663e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pshuf_b: 1664e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pshuf_b_512: 1665e8d8bef9SDimitry Andric if (Value *V = simplifyX86pshufb(II, IC.Builder)) { 1666e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1667e8d8bef9SDimitry Andric } 1668e8d8bef9SDimitry Andric break; 1669e8d8bef9SDimitry Andric 1670e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_ps: 1671e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_ps_256: 1672e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vpermilvar_ps_512: 1673e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_pd: 1674e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_pd_256: 1675e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vpermilvar_pd_512: 1676e8d8bef9SDimitry Andric if (Value *V = simplifyX86vpermilvar(II, IC.Builder)) { 1677e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1678e8d8bef9SDimitry Andric } 1679e8d8bef9SDimitry Andric break; 1680e8d8bef9SDimitry Andric 1681e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_permd: 1682e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_permps: 1683e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_df_256: 1684e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_df_512: 1685e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_di_256: 1686e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_di_512: 1687e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_hi_128: 1688e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_hi_256: 1689e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_hi_512: 1690e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_qi_128: 1691e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_qi_256: 1692e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_qi_512: 1693e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_sf_512: 1694e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_permvar_si_512: 1695e8d8bef9SDimitry Andric if (Value *V = simplifyX86vpermv(II, IC.Builder)) { 1696e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1697e8d8bef9SDimitry Andric } 1698e8d8bef9SDimitry Andric break; 1699e8d8bef9SDimitry Andric 1700e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskload_ps: 1701e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskload_pd: 1702e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskload_ps_256: 1703e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskload_pd_256: 1704e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskload_d: 1705e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskload_q: 1706e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskload_d_256: 1707e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskload_q_256: 1708e8d8bef9SDimitry Andric if (Instruction *I = simplifyX86MaskedLoad(II, IC)) { 1709e8d8bef9SDimitry Andric return I; 1710e8d8bef9SDimitry Andric } 1711e8d8bef9SDimitry Andric break; 1712e8d8bef9SDimitry Andric 1713e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_maskmov_dqu: 1714e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskstore_ps: 1715e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskstore_pd: 1716e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskstore_ps_256: 1717e8d8bef9SDimitry Andric case Intrinsic::x86_avx_maskstore_pd_256: 1718e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskstore_d: 1719e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskstore_q: 1720e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskstore_d_256: 1721e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_maskstore_q_256: 1722e8d8bef9SDimitry Andric if (simplifyX86MaskedStore(II, IC)) { 1723e8d8bef9SDimitry Andric return nullptr; 1724e8d8bef9SDimitry Andric } 1725e8d8bef9SDimitry Andric break; 1726e8d8bef9SDimitry Andric 1727e8d8bef9SDimitry Andric case Intrinsic::x86_addcarry_32: 1728e8d8bef9SDimitry Andric case Intrinsic::x86_addcarry_64: 1729e8d8bef9SDimitry Andric if (Value *V = simplifyX86addcarry(II, IC.Builder)) { 1730e8d8bef9SDimitry Andric return IC.replaceInstUsesWith(II, V); 1731e8d8bef9SDimitry Andric } 1732e8d8bef9SDimitry Andric break; 1733e8d8bef9SDimitry Andric 1734e8d8bef9SDimitry Andric default: 1735e8d8bef9SDimitry Andric break; 1736e8d8bef9SDimitry Andric } 1737e8d8bef9SDimitry Andric return None; 1738e8d8bef9SDimitry Andric } 1739e8d8bef9SDimitry Andric 1740e8d8bef9SDimitry Andric Optional<Value *> X86TTIImpl::simplifyDemandedUseBitsIntrinsic( 1741e8d8bef9SDimitry Andric InstCombiner &IC, IntrinsicInst &II, APInt DemandedMask, KnownBits &Known, 1742e8d8bef9SDimitry Andric bool &KnownBitsComputed) const { 1743e8d8bef9SDimitry Andric switch (II.getIntrinsicID()) { 1744e8d8bef9SDimitry Andric default: 1745e8d8bef9SDimitry Andric break; 1746e8d8bef9SDimitry Andric case Intrinsic::x86_mmx_pmovmskb: 1747e8d8bef9SDimitry Andric case Intrinsic::x86_sse_movmsk_ps: 1748e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_movmsk_pd: 1749e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_pmovmskb_128: 1750e8d8bef9SDimitry Andric case Intrinsic::x86_avx_movmsk_ps_256: 1751e8d8bef9SDimitry Andric case Intrinsic::x86_avx_movmsk_pd_256: 1752e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pmovmskb: { 1753e8d8bef9SDimitry Andric // MOVMSK copies the vector elements' sign bits to the low bits 1754e8d8bef9SDimitry Andric // and zeros the high bits. 1755e8d8bef9SDimitry Andric unsigned ArgWidth; 1756e8d8bef9SDimitry Andric if (II.getIntrinsicID() == Intrinsic::x86_mmx_pmovmskb) { 1757e8d8bef9SDimitry Andric ArgWidth = 8; // Arg is x86_mmx, but treated as <8 x i8>. 1758e8d8bef9SDimitry Andric } else { 1759*fe6060f1SDimitry Andric auto *ArgType = cast<FixedVectorType>(II.getArgOperand(0)->getType()); 1760e8d8bef9SDimitry Andric ArgWidth = ArgType->getNumElements(); 1761e8d8bef9SDimitry Andric } 1762e8d8bef9SDimitry Andric 1763e8d8bef9SDimitry Andric // If we don't need any of low bits then return zero, 1764e8d8bef9SDimitry Andric // we know that DemandedMask is non-zero already. 1765e8d8bef9SDimitry Andric APInt DemandedElts = DemandedMask.zextOrTrunc(ArgWidth); 1766e8d8bef9SDimitry Andric Type *VTy = II.getType(); 1767e8d8bef9SDimitry Andric if (DemandedElts.isNullValue()) { 1768e8d8bef9SDimitry Andric return ConstantInt::getNullValue(VTy); 1769e8d8bef9SDimitry Andric } 1770e8d8bef9SDimitry Andric 1771e8d8bef9SDimitry Andric // We know that the upper bits are set to zero. 1772e8d8bef9SDimitry Andric Known.Zero.setBitsFrom(ArgWidth); 1773e8d8bef9SDimitry Andric KnownBitsComputed = true; 1774e8d8bef9SDimitry Andric break; 1775e8d8bef9SDimitry Andric } 1776e8d8bef9SDimitry Andric } 1777e8d8bef9SDimitry Andric return None; 1778e8d8bef9SDimitry Andric } 1779e8d8bef9SDimitry Andric 1780e8d8bef9SDimitry Andric Optional<Value *> X86TTIImpl::simplifyDemandedVectorEltsIntrinsic( 1781e8d8bef9SDimitry Andric InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, 1782e8d8bef9SDimitry Andric APInt &UndefElts2, APInt &UndefElts3, 1783e8d8bef9SDimitry Andric std::function<void(Instruction *, unsigned, APInt, APInt &)> 1784e8d8bef9SDimitry Andric simplifyAndSetOp) const { 1785e8d8bef9SDimitry Andric unsigned VWidth = cast<FixedVectorType>(II.getType())->getNumElements(); 1786e8d8bef9SDimitry Andric switch (II.getIntrinsicID()) { 1787e8d8bef9SDimitry Andric default: 1788e8d8bef9SDimitry Andric break; 1789e8d8bef9SDimitry Andric case Intrinsic::x86_xop_vfrcz_ss: 1790e8d8bef9SDimitry Andric case Intrinsic::x86_xop_vfrcz_sd: 1791e8d8bef9SDimitry Andric // The instructions for these intrinsics are speced to zero upper bits not 1792e8d8bef9SDimitry Andric // pass them through like other scalar intrinsics. So we shouldn't just 1793e8d8bef9SDimitry Andric // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics. 1794e8d8bef9SDimitry Andric // Instead we should return a zero vector. 1795e8d8bef9SDimitry Andric if (!DemandedElts[0]) { 1796e8d8bef9SDimitry Andric IC.addToWorklist(&II); 1797e8d8bef9SDimitry Andric return ConstantAggregateZero::get(II.getType()); 1798e8d8bef9SDimitry Andric } 1799e8d8bef9SDimitry Andric 1800e8d8bef9SDimitry Andric // Only the lower element is used. 1801e8d8bef9SDimitry Andric DemandedElts = 1; 1802e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 0, DemandedElts, UndefElts); 1803e8d8bef9SDimitry Andric 1804e8d8bef9SDimitry Andric // Only the lower element is undefined. The high elements are zero. 1805e8d8bef9SDimitry Andric UndefElts = UndefElts[0]; 1806e8d8bef9SDimitry Andric break; 1807e8d8bef9SDimitry Andric 1808e8d8bef9SDimitry Andric // Unary scalar-as-vector operations that work column-wise. 1809e8d8bef9SDimitry Andric case Intrinsic::x86_sse_rcp_ss: 1810e8d8bef9SDimitry Andric case Intrinsic::x86_sse_rsqrt_ss: 1811e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 0, DemandedElts, UndefElts); 1812e8d8bef9SDimitry Andric 1813e8d8bef9SDimitry Andric // If lowest element of a scalar op isn't used then use Arg0. 1814e8d8bef9SDimitry Andric if (!DemandedElts[0]) { 1815e8d8bef9SDimitry Andric IC.addToWorklist(&II); 1816e8d8bef9SDimitry Andric return II.getArgOperand(0); 1817e8d8bef9SDimitry Andric } 1818e8d8bef9SDimitry Andric // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions 1819e8d8bef9SDimitry Andric // checks). 1820e8d8bef9SDimitry Andric break; 1821e8d8bef9SDimitry Andric 1822e8d8bef9SDimitry Andric // Binary scalar-as-vector operations that work column-wise. The high 1823e8d8bef9SDimitry Andric // elements come from operand 0. The low element is a function of both 1824e8d8bef9SDimitry Andric // operands. 1825e8d8bef9SDimitry Andric case Intrinsic::x86_sse_min_ss: 1826e8d8bef9SDimitry Andric case Intrinsic::x86_sse_max_ss: 1827e8d8bef9SDimitry Andric case Intrinsic::x86_sse_cmp_ss: 1828e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_min_sd: 1829e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_max_sd: 1830e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_cmp_sd: { 1831e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 0, DemandedElts, UndefElts); 1832e8d8bef9SDimitry Andric 1833e8d8bef9SDimitry Andric // If lowest element of a scalar op isn't used then use Arg0. 1834e8d8bef9SDimitry Andric if (!DemandedElts[0]) { 1835e8d8bef9SDimitry Andric IC.addToWorklist(&II); 1836e8d8bef9SDimitry Andric return II.getArgOperand(0); 1837e8d8bef9SDimitry Andric } 1838e8d8bef9SDimitry Andric 1839e8d8bef9SDimitry Andric // Only lower element is used for operand 1. 1840e8d8bef9SDimitry Andric DemandedElts = 1; 1841e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 1, DemandedElts, UndefElts2); 1842e8d8bef9SDimitry Andric 1843e8d8bef9SDimitry Andric // Lower element is undefined if both lower elements are undefined. 1844e8d8bef9SDimitry Andric // Consider things like undef&0. The result is known zero, not undef. 1845e8d8bef9SDimitry Andric if (!UndefElts2[0]) 1846e8d8bef9SDimitry Andric UndefElts.clearBit(0); 1847e8d8bef9SDimitry Andric 1848e8d8bef9SDimitry Andric break; 1849e8d8bef9SDimitry Andric } 1850e8d8bef9SDimitry Andric 1851e8d8bef9SDimitry Andric // Binary scalar-as-vector operations that work column-wise. The high 1852e8d8bef9SDimitry Andric // elements come from operand 0 and the low element comes from operand 1. 1853e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_round_ss: 1854e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_round_sd: { 1855e8d8bef9SDimitry Andric // Don't use the low element of operand 0. 1856e8d8bef9SDimitry Andric APInt DemandedElts2 = DemandedElts; 1857e8d8bef9SDimitry Andric DemandedElts2.clearBit(0); 1858e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 0, DemandedElts2, UndefElts); 1859e8d8bef9SDimitry Andric 1860e8d8bef9SDimitry Andric // If lowest element of a scalar op isn't used then use Arg0. 1861e8d8bef9SDimitry Andric if (!DemandedElts[0]) { 1862e8d8bef9SDimitry Andric IC.addToWorklist(&II); 1863e8d8bef9SDimitry Andric return II.getArgOperand(0); 1864e8d8bef9SDimitry Andric } 1865e8d8bef9SDimitry Andric 1866e8d8bef9SDimitry Andric // Only lower element is used for operand 1. 1867e8d8bef9SDimitry Andric DemandedElts = 1; 1868e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 1, DemandedElts, UndefElts2); 1869e8d8bef9SDimitry Andric 1870e8d8bef9SDimitry Andric // Take the high undef elements from operand 0 and take the lower element 1871e8d8bef9SDimitry Andric // from operand 1. 1872e8d8bef9SDimitry Andric UndefElts.clearBit(0); 1873e8d8bef9SDimitry Andric UndefElts |= UndefElts2[0]; 1874e8d8bef9SDimitry Andric break; 1875e8d8bef9SDimitry Andric } 1876e8d8bef9SDimitry Andric 1877e8d8bef9SDimitry Andric // Three input scalar-as-vector operations that work column-wise. The high 1878e8d8bef9SDimitry Andric // elements come from operand 0 and the low element is a function of all 1879e8d8bef9SDimitry Andric // three inputs. 1880e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_add_ss_round: 1881e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_div_ss_round: 1882e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_mul_ss_round: 1883e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_sub_ss_round: 1884e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_max_ss_round: 1885e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_min_ss_round: 1886e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_add_sd_round: 1887e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_div_sd_round: 1888e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_mul_sd_round: 1889e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_sub_sd_round: 1890e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_max_sd_round: 1891e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_mask_min_sd_round: 1892e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 0, DemandedElts, UndefElts); 1893e8d8bef9SDimitry Andric 1894e8d8bef9SDimitry Andric // If lowest element of a scalar op isn't used then use Arg0. 1895e8d8bef9SDimitry Andric if (!DemandedElts[0]) { 1896e8d8bef9SDimitry Andric IC.addToWorklist(&II); 1897e8d8bef9SDimitry Andric return II.getArgOperand(0); 1898e8d8bef9SDimitry Andric } 1899e8d8bef9SDimitry Andric 1900e8d8bef9SDimitry Andric // Only lower element is used for operand 1 and 2. 1901e8d8bef9SDimitry Andric DemandedElts = 1; 1902e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 1, DemandedElts, UndefElts2); 1903e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 2, DemandedElts, UndefElts3); 1904e8d8bef9SDimitry Andric 1905e8d8bef9SDimitry Andric // Lower element is undefined if all three lower elements are undefined. 1906e8d8bef9SDimitry Andric // Consider things like undef&0. The result is known zero, not undef. 1907e8d8bef9SDimitry Andric if (!UndefElts2[0] || !UndefElts3[0]) 1908e8d8bef9SDimitry Andric UndefElts.clearBit(0); 1909e8d8bef9SDimitry Andric break; 1910e8d8bef9SDimitry Andric 1911e8d8bef9SDimitry Andric // TODO: Add fmaddsub support? 1912e8d8bef9SDimitry Andric case Intrinsic::x86_sse3_addsub_pd: 1913e8d8bef9SDimitry Andric case Intrinsic::x86_sse3_addsub_ps: 1914e8d8bef9SDimitry Andric case Intrinsic::x86_avx_addsub_pd_256: 1915e8d8bef9SDimitry Andric case Intrinsic::x86_avx_addsub_ps_256: { 1916e8d8bef9SDimitry Andric // If none of the even or none of the odd lanes are required, turn this 1917e8d8bef9SDimitry Andric // into a generic FP math instruction. 1918e8d8bef9SDimitry Andric APInt SubMask = APInt::getSplat(VWidth, APInt(2, 0x1)); 1919e8d8bef9SDimitry Andric APInt AddMask = APInt::getSplat(VWidth, APInt(2, 0x2)); 1920e8d8bef9SDimitry Andric bool IsSubOnly = DemandedElts.isSubsetOf(SubMask); 1921e8d8bef9SDimitry Andric bool IsAddOnly = DemandedElts.isSubsetOf(AddMask); 1922e8d8bef9SDimitry Andric if (IsSubOnly || IsAddOnly) { 1923e8d8bef9SDimitry Andric assert((IsSubOnly ^ IsAddOnly) && "Can't be both add-only and sub-only"); 1924e8d8bef9SDimitry Andric IRBuilderBase::InsertPointGuard Guard(IC.Builder); 1925e8d8bef9SDimitry Andric IC.Builder.SetInsertPoint(&II); 1926e8d8bef9SDimitry Andric Value *Arg0 = II.getArgOperand(0), *Arg1 = II.getArgOperand(1); 1927e8d8bef9SDimitry Andric return IC.Builder.CreateBinOp( 1928e8d8bef9SDimitry Andric IsSubOnly ? Instruction::FSub : Instruction::FAdd, Arg0, Arg1); 1929e8d8bef9SDimitry Andric } 1930e8d8bef9SDimitry Andric 1931e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 0, DemandedElts, UndefElts); 1932e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 1, DemandedElts, UndefElts2); 1933e8d8bef9SDimitry Andric UndefElts &= UndefElts2; 1934e8d8bef9SDimitry Andric break; 1935e8d8bef9SDimitry Andric } 1936e8d8bef9SDimitry Andric 1937e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_packssdw_128: 1938e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_packsswb_128: 1939e8d8bef9SDimitry Andric case Intrinsic::x86_sse2_packuswb_128: 1940e8d8bef9SDimitry Andric case Intrinsic::x86_sse41_packusdw: 1941e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packssdw: 1942e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packsswb: 1943e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packusdw: 1944e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_packuswb: 1945e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packssdw_512: 1946e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packsswb_512: 1947e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packusdw_512: 1948e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_packuswb_512: { 1949e8d8bef9SDimitry Andric auto *Ty0 = II.getArgOperand(0)->getType(); 1950e8d8bef9SDimitry Andric unsigned InnerVWidth = cast<FixedVectorType>(Ty0)->getNumElements(); 1951e8d8bef9SDimitry Andric assert(VWidth == (InnerVWidth * 2) && "Unexpected input size"); 1952e8d8bef9SDimitry Andric 1953e8d8bef9SDimitry Andric unsigned NumLanes = Ty0->getPrimitiveSizeInBits() / 128; 1954e8d8bef9SDimitry Andric unsigned VWidthPerLane = VWidth / NumLanes; 1955e8d8bef9SDimitry Andric unsigned InnerVWidthPerLane = InnerVWidth / NumLanes; 1956e8d8bef9SDimitry Andric 1957e8d8bef9SDimitry Andric // Per lane, pack the elements of the first input and then the second. 1958e8d8bef9SDimitry Andric // e.g. 1959e8d8bef9SDimitry Andric // v8i16 PACK(v4i32 X, v4i32 Y) - (X[0..3],Y[0..3]) 1960e8d8bef9SDimitry Andric // v32i8 PACK(v16i16 X, v16i16 Y) - (X[0..7],Y[0..7]),(X[8..15],Y[8..15]) 1961e8d8bef9SDimitry Andric for (int OpNum = 0; OpNum != 2; ++OpNum) { 1962e8d8bef9SDimitry Andric APInt OpDemandedElts(InnerVWidth, 0); 1963e8d8bef9SDimitry Andric for (unsigned Lane = 0; Lane != NumLanes; ++Lane) { 1964e8d8bef9SDimitry Andric unsigned LaneIdx = Lane * VWidthPerLane; 1965e8d8bef9SDimitry Andric for (unsigned Elt = 0; Elt != InnerVWidthPerLane; ++Elt) { 1966e8d8bef9SDimitry Andric unsigned Idx = LaneIdx + Elt + InnerVWidthPerLane * OpNum; 1967e8d8bef9SDimitry Andric if (DemandedElts[Idx]) 1968e8d8bef9SDimitry Andric OpDemandedElts.setBit((Lane * InnerVWidthPerLane) + Elt); 1969e8d8bef9SDimitry Andric } 1970e8d8bef9SDimitry Andric } 1971e8d8bef9SDimitry Andric 1972e8d8bef9SDimitry Andric // Demand elements from the operand. 1973e8d8bef9SDimitry Andric APInt OpUndefElts(InnerVWidth, 0); 1974e8d8bef9SDimitry Andric simplifyAndSetOp(&II, OpNum, OpDemandedElts, OpUndefElts); 1975e8d8bef9SDimitry Andric 1976e8d8bef9SDimitry Andric // Pack the operand's UNDEF elements, one lane at a time. 1977e8d8bef9SDimitry Andric OpUndefElts = OpUndefElts.zext(VWidth); 1978e8d8bef9SDimitry Andric for (unsigned Lane = 0; Lane != NumLanes; ++Lane) { 1979e8d8bef9SDimitry Andric APInt LaneElts = OpUndefElts.lshr(InnerVWidthPerLane * Lane); 1980e8d8bef9SDimitry Andric LaneElts = LaneElts.getLoBits(InnerVWidthPerLane); 1981e8d8bef9SDimitry Andric LaneElts <<= InnerVWidthPerLane * (2 * Lane + OpNum); 1982e8d8bef9SDimitry Andric UndefElts |= LaneElts; 1983e8d8bef9SDimitry Andric } 1984e8d8bef9SDimitry Andric } 1985e8d8bef9SDimitry Andric break; 1986e8d8bef9SDimitry Andric } 1987e8d8bef9SDimitry Andric 1988e8d8bef9SDimitry Andric // PSHUFB 1989e8d8bef9SDimitry Andric case Intrinsic::x86_ssse3_pshuf_b_128: 1990e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_pshuf_b: 1991e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_pshuf_b_512: 1992e8d8bef9SDimitry Andric // PERMILVAR 1993e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_ps: 1994e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_ps_256: 1995e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vpermilvar_ps_512: 1996e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_pd: 1997e8d8bef9SDimitry Andric case Intrinsic::x86_avx_vpermilvar_pd_256: 1998e8d8bef9SDimitry Andric case Intrinsic::x86_avx512_vpermilvar_pd_512: 1999e8d8bef9SDimitry Andric // PERMV 2000e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_permd: 2001e8d8bef9SDimitry Andric case Intrinsic::x86_avx2_permps: { 2002e8d8bef9SDimitry Andric simplifyAndSetOp(&II, 1, DemandedElts, UndefElts); 2003e8d8bef9SDimitry Andric break; 2004e8d8bef9SDimitry Andric } 2005e8d8bef9SDimitry Andric 2006e8d8bef9SDimitry Andric // SSE4A instructions leave the upper 64-bits of the 128-bit result 2007e8d8bef9SDimitry Andric // in an undefined state. 2008e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_extrq: 2009e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_extrqi: 2010e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_insertq: 2011e8d8bef9SDimitry Andric case Intrinsic::x86_sse4a_insertqi: 2012e8d8bef9SDimitry Andric UndefElts.setHighBits(VWidth / 2); 2013e8d8bef9SDimitry Andric break; 2014e8d8bef9SDimitry Andric } 2015e8d8bef9SDimitry Andric return None; 2016e8d8bef9SDimitry Andric } 2017