Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -6915,6 +6915,16 @@ return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1); } + // fold (sext_in_reg (trunc x)) -> (trunc x) if x's sign bits start from + // EVTBits. + if (N0.getOpcode() == ISD::TRUNCATE) { + unsigned N00VTBits = N0.getOperand(0).getValueType().getSizeInBits(); + unsigned N00NSB = DAG.ComputeNumSignBits(N0.getOperand(0)); + + if ((N00VTBits - N00NSB + 1) <= EVTBits) + return N0; + } + // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero. if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits))) return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);