From 6e6b0da30316b522702c869840ba95db2cfa709e Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 31 Mar 2006 23:01:56 +0000 Subject: [PATCH] If we can look through vector operations to find the scalar version of an extract_element'd value, do so. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27323 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index f69d869c12e..e101aeb0ff0 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -6654,7 +6654,41 @@ static bool CheapToScalarize(Value *V, bool isConstant) { return false; } +/// FindScalarElement - Given a vector and an element number, see if the scalar +/// value is already around as a register, for example if it were inserted then +/// extracted from the vector. +static Value *FindScalarElement(Value *V, unsigned EltNo) { + assert(isa(V->getType()) && "Not looking at a vector?"); + const PackedType *PTy = cast(V->getType()); + if (EltNo >= PTy->getNumElements()) // Out of range access. + return UndefValue::get(PTy->getElementType()); + + if (isa(V)) + return UndefValue::get(PTy->getElementType()); + else if (isa(V)) + return Constant::getNullValue(PTy->getElementType()); + else if (ConstantPacked *CP = dyn_cast(V)) + return CP->getOperand(EltNo); + else if (InsertElementInst *III = dyn_cast(V)) { + // If this is an insert to a variable element, we don't know what it is. + if (!isa(III->getOperand(2))) return 0; + unsigned IIElt = cast(III->getOperand(2))->getValue(); + + // If this is an insert to the element we are looking for, return the + // inserted value. + if (EltNo == IIElt) return III->getOperand(1); + + // Otherwise, the insertelement doesn't modify the value, recurse on its + // vector input. + return FindScalarElement(III->getOperand(0), EltNo); + } + + // Otherwise, we don't know. + return 0; +} + Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { + // If packed val is undef, replace extract with scalar undef. if (isa(EI.getOperand(0))) return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); @@ -6676,6 +6710,12 @@ Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { return ReplaceInstUsesWith(EI, op0); } + // If extracting a specified index from the vector, see if we can recursively + // find a previously computed scalar that was inserted into the vector. + if (ConstantUInt *IdxC = dyn_cast(EI.getOperand(1))) + if (Value *Elt = FindScalarElement(EI.getOperand(0), IdxC->getValue())) + return ReplaceInstUsesWith(EI, Elt); + if (Instruction *I = dyn_cast(EI.getOperand(0))) if (I->hasOneUse()) { // Push extractelement into predecessor operation if legal and -- 2.34.1