diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -12356,6 +12356,15 @@ if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, N0.getOperand(0)); + // fold (sext (sext_inreg x)) -> (sext (trunc x)) + if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) { + EVT ExtVT = cast(N0->getOperand(1))->getVT(); + if (!LegalOperations || TLI.isTypeLegal(ExtVT)) { + SDValue T = DAG.getNode(ISD::TRUNCATE, DL, ExtVT, N0.getOperand(0)); + return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, T); + } + } + if (N0.getOpcode() == ISD::TRUNCATE) { // fold (sext (truncate (load x))) -> (sext (smaller load x)) // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))