From 29014ecbf253497c66116b0302159c7ec6801e16 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 24 Sep 2018 20:41:22 +0000 Subject: [PATCH] [InstCombine] add bitcast+extelt helper function; NFC We can handle patterns where the elements have different sizes, so refactoring ahead of trying to add another blob within these clauses. llvm-svn: 342918 --- .../InstCombine/InstCombineVectorOps.cpp | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/lib/Transforms/InstCombine/InstCombineVectorOps.cpp index 03580fc96d4..f6c6bab73a0 100644 --- a/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -166,6 +166,29 @@ Instruction *InstCombiner::scalarizePHI(ExtractElementInst &EI, PHINode *PN) { return &EI; } +static Instruction *foldBitcastExtElt(ExtractElementInst &Ext, + InstCombiner::BuilderTy &Builder) { + Value *X; + uint64_t ExtIndexC; + if (!match(Ext.getVectorOperand(), m_BitCast(m_Value(X))) || + !X->getType()->isVectorTy() || + !match(Ext.getIndexOperand(), m_ConstantInt(ExtIndexC))) + return nullptr; + + // If this extractelement is using a bitcast from a vector of the same number + // of elements, see if we can find the source element from the source vector: + // extelt (bitcast VecX), IndexC --> bitcast X[IndexC] + Type *SrcTy = X->getType(); + Type *DestTy = Ext.getType(); + unsigned NumSrcElts = SrcTy->getVectorNumElements(); + unsigned NumElts = Ext.getVectorOperandType()->getNumElements(); + if (NumSrcElts == NumElts) + if (Value *Elt = findScalarElement(X, ExtIndexC)) + return new BitCastInst(Elt, DestTy); + + return nullptr; +} + Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { if (Value *V = SimplifyExtractElementInst(EI.getVectorOperand(), EI.getIndexOperand(), @@ -187,15 +210,13 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { if (!IdxC->getValue().ule(NumElts)) return nullptr; - unsigned IndexVal = IdxC->getZExtValue(); - // This instruction only demands the single element from the input vector. // If the input vector has a single use, simplify it based on this use // property. if (EI.getOperand(0)->hasOneUse() && NumElts != 1) { APInt UndefElts(NumElts, 0); APInt DemandedMask(NumElts, 0); - DemandedMask.setBit(IndexVal); + DemandedMask.setBit(IdxC->getZExtValue()); if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), DemandedMask, UndefElts)) { EI.setOperand(0, V); @@ -203,17 +224,8 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { } } - Value *X; - if (match(EI.getVectorOperand(), m_BitCast(m_Value(X))) && - X->getType()->isVectorTy()) { - // If this extractelement is using a bitcast from a vector of the same - // number of elements, see if we can find the source element from the - // source vector: - // extelt (bitcast VecX), IdxC --> bitcast X[IdxC] - if (X->getType()->getVectorNumElements() == NumElts) - if (Value *Elt = findScalarElement(X, IndexVal)) - return new BitCastInst(Elt, EI.getType()); - } + if (Instruction *I = foldBitcastExtElt(EI, Builder)) + return I; // If there's a vector PHI feeding a scalar use through this extractelement // instruction, try to scalarize the PHI.