Index: include/llvm/Analysis/VectorUtils.h =================================================================== --- include/llvm/Analysis/VectorUtils.h +++ include/llvm/Analysis/VectorUtils.h @@ -79,6 +79,11 @@ /// from the vector. Value *findScalarElement(Value *V, unsigned EltNo); +/// \brief Get splat value if the input is a splat vector or return nullptr. +/// The value may be extracted from a splat constants vector or from +/// a sequence of instructions that broadcast a single value into a vector. +Value *getSplatValue(Value *V); + } // llvm namespace #endif Index: lib/Analysis/VectorUtils.cpp =================================================================== --- lib/Analysis/VectorUtils.cpp +++ lib/Analysis/VectorUtils.cpp @@ -18,6 +18,7 @@ #include "llvm/IR/GetElementPtrTypeIterator.h" #include "llvm/IR/PatternMatch.h" #include "llvm/IR/Value.h" +#include "llvm/IR/Constants.h" /// \brief Identify if the intrinsic is trivially vectorizable. /// This method returns true if the intrinsic's argument types are all @@ -409,3 +410,15 @@ // Otherwise, we don't know. return nullptr; } + +llvm::Value *llvm::getSplatValue(Value *V) { + llvm::ConstantDataVector *CV = dyn_cast(V); + if (CV) + return CV->getSplatValue(); + llvm::ShuffleVectorInst *ShuffleInst = dyn_cast(V); + if (!ShuffleInst || !ShuffleInst->getMask()->isNullValue() || + !isa(ShuffleInst->getOperand(0))) + return nullptr; + + return cast(ShuffleInst->getOperand(0))->getOperand(1); +} Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -22,6 +22,7 @@ #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/ValueTracking.h" +#include "llvm/Analysis/VectorUtils.h" #include "llvm/CodeGen/FastISel.h" #include "llvm/CodeGen/FunctionLoweringInfo.h" #include "llvm/CodeGen/GCMetadata.h" @@ -3091,28 +3092,23 @@ GetElementPtrInst *Gep = dyn_cast(Ptr); if (!Gep || Gep->getNumOperands() > 2) return false; - ShuffleVectorInst *ShuffleInst = - dyn_cast(Gep->getPointerOperand()); - if (!ShuffleInst || !ShuffleInst->getMask()->isNullValue() || - cast(ShuffleInst->getOperand(0))->getOpcode() != - Instruction::InsertElement) + Value *GepPtrs = Gep->getPointerOperand(); + if (!(Ptr = getSplatValue(GepPtrs))) return false; - Ptr = cast(ShuffleInst->getOperand(0))->getOperand(1); - SelectionDAG& DAG = SDB->DAG; const TargetLowering &TLI = DAG.getTargetLoweringInfo(); // Check is the Ptr is inside current basic block // If not, look for the shuffle instruction if (SDB->findValue(Ptr)) Base = SDB->getValue(Ptr); - else if (SDB->findValue(ShuffleInst)) { - SDValue ShuffleNode = SDB->getValue(ShuffleInst); - SDLoc sdl = ShuffleNode; - Base = DAG.getNode( - ISD::EXTRACT_VECTOR_ELT, sdl, - ShuffleNode.getValueType().getScalarType(), ShuffleNode, - DAG.getConstant(0, sdl, TLI.getVectorIdxTy(DAG.getDataLayout()))); + else if (SDB->findValue(GepPtrs)) { + SDValue GepPtrsVal = SDB->getValue(GepPtrs); + SDLoc sdl = GepPtrsVal; + EVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout()); + Base = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, + GepPtrsVal.getValueType().getScalarType(), GepPtrsVal, + DAG.getConstant(0, sdl, IdxVT)); SDB->setValue(Ptr, Base); } else