Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -5560,12 +5560,12 @@ SDLoc(N)); } -// tryToFoldExtendOfConstant - Try to fold a sext/zext/aext -// dag node into a ConstantSDNode or a build_vector of constants. -// This function is called by the DAGCombiner when visiting sext/zext/aext -// dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND). -// Vector extends are not folded if operations are legal; this is to -// avoid introducing illegal build_vector dag nodes. +/// Try to fold a sext/zext/aext dag node into a ConstantSDNode or +/// a build_vector of constants. +/// This function is called by the DAGCombiner when visiting sext/zext/aext +/// dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND). +/// Vector extends are not folded if operations are legal; this is to +/// avoid introducing illegal build_vector dag nodes. static SDNode *tryToFoldExtendOfConstant(SDNode *N, const TargetLowering &TLI, SelectionDAG &DAG, bool LegalTypes, bool LegalOperations) { @@ -5594,8 +5594,6 @@ // We can fold this node into a build_vector. unsigned VTBits = SVT.getSizeInBits(); - unsigned EVTBits = N0->getValueType(0).getScalarType().getSizeInBits(); - unsigned ShAmt = VTBits - EVTBits; SmallVector Elts; unsigned NumElts = VT.getVectorNumElements(); SDLoc DL(N); @@ -5608,14 +5606,11 @@ } SDLoc DL(Op); - ConstantSDNode *CurrentND = cast(Op); - const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue()); + const APInt &C = cast(Op)->getAPIntValue(); if (Opcode == ISD::SIGN_EXTEND || Opcode == ISD::SIGN_EXTEND_VECTOR_INREG) - Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(), - DL, SVT)); + Elts.push_back(DAG.getConstant(C.sextOrTrunc(VTBits), DL, SVT)); else - Elts.push_back(DAG.getConstant(C.shl(ShAmt).lshr(ShAmt).getZExtValue(), - DL, SVT)); + Elts.push_back(DAG.getConstant(C.zextOrTrunc(VTBits), DL, SVT)); } return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Elts).getNode(); Index: test/CodeGen/X86/fold-vector-sext-crash2.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/fold-vector-sext-crash2.ll @@ -0,0 +1,17 @@ +; RUN: llc < %s | FileCheck %s + +; DAGCombiner crashes during sext folding + +; CHECK-LABEL: test_sext1 +define <8 x i256> @test_sext1() { + %Se = sext <8 x i8> to <8 x i256> + %Shuff = shufflevector <8 x i256> zeroinitializer, <8 x i256> %Se, <8 x i32> + ret <8 x i256> %Shuff +} + +; CHECK-LABEL: test_sext2 +define <8 x i256> @test_sext2() { + %Se = sext <8 x i128> to <8 x i256> + %Shuff = shufflevector <8 x i256> zeroinitializer, <8 x i256> %Se, <8 x i32> + ret <8 x i256> %Shuff +}