From 7bf13fe81218b17d079cc5d61ced6b09077a913c Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Thu, 11 Jan 2024 09:39:30 +0000 Subject: [PATCH] [DAG] Fold (sext (sext_inreg x)) -> (sext (trunc x)) if the trunc is free (#77616) --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 4 ++-- llvm/test/CodeGen/X86/abds.ll | 8 ++++---- llvm/test/CodeGen/X86/pr14088.ll | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 54732237c91aa6..ecdf9ab9e989f0 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -13381,9 +13381,9 @@ SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) { if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) { SDValue N00 = N0.getOperand(0); EVT ExtVT = cast(N0->getOperand(1))->getVT(); - if (N00.getOpcode() == ISD::TRUNCATE && + if ((N00.getOpcode() == ISD::TRUNCATE || TLI.isTruncateFree(N00, ExtVT)) && (!LegalTypes || TLI.isTypeLegal(ExtVT))) { - SDValue T = DAG.getNode(ISD::TRUNCATE, DL, ExtVT, N00.getOperand(0)); + SDValue T = DAG.getNode(ISD::TRUNCATE, DL, ExtVT, N00); return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, T); } } diff --git a/llvm/test/CodeGen/X86/abds.ll b/llvm/test/CodeGen/X86/abds.ll index 39ac47e99e6e98..a80339427984a8 100644 --- a/llvm/test/CodeGen/X86/abds.ll +++ b/llvm/test/CodeGen/X86/abds.ll @@ -141,9 +141,9 @@ define i16 @abd_ext_i16_i32(i16 %a, i32 %b) nounwind { ; ; X64-LABEL: abd_ext_i16_i32: ; X64: # %bb.0: +; X64-NEXT: # kill: def $edi killed $edi def $rdi +; X64-NEXT: movswq %di, %rcx ; X64-NEXT: movslq %esi, %rax -; X64-NEXT: movswl %di, %ecx -; X64-NEXT: movslq %ecx, %rcx ; X64-NEXT: subq %rax, %rcx ; X64-NEXT: movq %rcx, %rax ; X64-NEXT: negq %rax @@ -232,9 +232,9 @@ define i32 @abd_ext_i32_i16(i32 %a, i16 %b) nounwind { ; ; X64-LABEL: abd_ext_i32_i16: ; X64: # %bb.0: +; X64-NEXT: # kill: def $esi killed $esi def $rsi +; X64-NEXT: movswq %si, %rax ; X64-NEXT: movslq %edi, %rcx -; X64-NEXT: movswl %si, %eax -; X64-NEXT: cltq ; X64-NEXT: subq %rax, %rcx ; X64-NEXT: movq %rcx, %rax ; X64-NEXT: negq %rax diff --git a/llvm/test/CodeGen/X86/pr14088.ll b/llvm/test/CodeGen/X86/pr14088.ll index 18c73f00571ca8..83bf13280f94ae 100644 --- a/llvm/test/CodeGen/X86/pr14088.ll +++ b/llvm/test/CodeGen/X86/pr14088.ll @@ -26,8 +26,7 @@ define i32 @f(i1 %foo, ptr %tm_year2, ptr %bar, i16 %zed, i32 %zed2) { ; CHECK-NEXT: imull $100, %ecx, %ecx ; CHECK-NEXT: subl %ecx, %eax ; CHECK-NEXT: movw %ax, (%rsi) -; CHECK-NEXT: cwtl -; CHECK-NEXT: cltq +; CHECK-NEXT: movswq %ax, %rax ; CHECK-NEXT: imulq $1717986919, %rax, %rax # imm = 0x66666667 ; CHECK-NEXT: movq %rax, %rcx ; CHECK-NEXT: shrq $63, %rcx