[llvm] 076bee1 - [DAG] getNode() - fold (zext (trunc (assertzext x))) -> (assertzext x)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 31 02:43:23 PDT 2023
Author: Simon Pilgrim
Date: 2023-07-31T10:43:11+01:00
New Revision: 076bee1020f7117758edf3bbc940951c10a20890
URL: https://github.com/llvm/llvm-project/commit/076bee1020f7117758edf3bbc940951c10a20890
DIFF: https://github.com/llvm/llvm-project/commit/076bee1020f7117758edf3bbc940951c10a20890.diff
LOG: [DAG] getNode() - fold (zext (trunc (assertzext x))) -> (assertzext x)
If the pre-truncated value was the same width as the extension, and the assertzext guarantees that the extended bits are already zero, then skip the zext/trunc 'zero_extend_inreg' pattern.
Addresses several regressions noticed in D155472
Added:
Modified:
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
llvm/test/CodeGen/RISCV/rvv/fold-vp-fadd-and-vp-fmul.ll
llvm/test/CodeGen/X86/extract-bits.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index ec84878f298854..c1963a1dc525fd 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -5691,6 +5691,22 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
if (OpOpcode == ISD::UNDEF)
// zext(undef) = 0, because the top bits will be zero.
return getConstant(0, DL, VT);
+
+ // Skip unnecessary zext_inreg pattern:
+ // (zext (trunc (assertzext x))) -> (assertzext x)
+ // TODO: Generalize to MaskedValueIsZero check?
+ if (OpOpcode == ISD::TRUNCATE) {
+ SDValue OpOp = N1.getOperand(0);
+ if (OpOp.getValueType() == VT) {
+ if (OpOp.getOpcode() == ISD::AssertZext && N1->hasOneUse()) {
+ EVT ExtVT = cast<VTSDNode>(OpOp.getOperand(1))->getVT();
+ if (N1.getScalarValueSizeInBits() >= ExtVT.getSizeInBits()) {
+ transferDbgValues(N1, OpOp);
+ return OpOp;
+ }
+ }
+ }
+ }
break;
case ISD::ANY_EXTEND:
assert(VT.isInteger() && N1.getValueType().isInteger() &&
diff --git a/llvm/test/CodeGen/RISCV/rvv/fold-vp-fadd-and-vp-fmul.ll b/llvm/test/CodeGen/RISCV/rvv/fold-vp-fadd-and-vp-fmul.ll
index f773de3b518c6d..1eccaaa26154f7 100644
--- a/llvm/test/CodeGen/RISCV/rvv/fold-vp-fadd-and-vp-fmul.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/fold-vp-fadd-and-vp-fmul.ll
@@ -62,9 +62,9 @@ define <vscale x 1 x double> @fma_reassociate(<vscale x 1 x double> %a, <vscale
; CHECK-LABEL: fma_reassociate:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, ma
-; CHECK-NEXT: vfmadd.vv v11, v10, v12, v0.t
-; CHECK-NEXT: vfmadd.vv v9, v8, v11, v0.t
-; CHECK-NEXT: vmv.v.v v8, v9
+; CHECK-NEXT: vfmadd.vv v9, v8, v12, v0.t
+; CHECK-NEXT: vfmadd.vv v11, v10, v9, v0.t
+; CHECK-NEXT: vmv.v.v v8, v11
; CHECK-NEXT: ret
%1 = call fast <vscale x 1 x double> @llvm.vp.fmul.nxv1f64(<vscale x 1 x double> %a, <vscale x 1 x double> %b, <vscale x 1 x i1> %m, i32 %vl)
%2 = call fast <vscale x 1 x double> @llvm.vp.fmul.nxv1f64(<vscale x 1 x double> %c, <vscale x 1 x double> %d, <vscale x 1 x i1> %m, i32 %vl)
diff --git a/llvm/test/CodeGen/X86/extract-bits.ll b/llvm/test/CodeGen/X86/extract-bits.ll
index 638c4db756b445..de826aba54ae24 100644
--- a/llvm/test/CodeGen/X86/extract-bits.ll
+++ b/llvm/test/CodeGen/X86/extract-bits.ll
@@ -210,9 +210,8 @@ define i32 @bextr32_a1_indexzext(i32 %val, i8 zeroext %numskipbits, i8 zeroext %
; X64-BMI1-LABEL: bextr32_a1_indexzext:
; X64-BMI1: # %bb.0:
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrl %eax, %edi, %eax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrl %edx, %edi, %eax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr32_a1_indexzext:
@@ -351,9 +350,8 @@ define i32 @bextr32_a3_load_indexzext(ptr %w, i8 zeroext %numskipbits, i8 zeroex
; X64-BMI1-LABEL: bextr32_a3_load_indexzext:
; X64-BMI1: # %bb.0:
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrl %eax, (%rdi), %eax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrl %edx, (%rdi), %eax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr32_a3_load_indexzext:
@@ -953,10 +951,10 @@ define i64 @bextr64_a1_indexzext(i64 %val, i8 zeroext %numskipbits, i8 zeroext %
;
; X64-BMI1-LABEL: bextr64_a1_indexzext:
; X64-BMI1: # %bb.0:
+; X64-BMI1-NEXT: # kill: def $edx killed $edx def $rdx
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrq %rax, %rdi, %rax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrq %rdx, %rdi, %rax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr64_a1_indexzext:
@@ -1250,10 +1248,10 @@ define i64 @bextr64_a3_load_indexzext(ptr %w, i8 zeroext %numskipbits, i8 zeroex
;
; X64-BMI1-LABEL: bextr64_a3_load_indexzext:
; X64-BMI1: # %bb.0:
+; X64-BMI1-NEXT: # kill: def $edx killed $edx def $rdx
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrq %rax, (%rdi), %rax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrq %rdx, (%rdi), %rax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr64_a3_load_indexzext:
@@ -2327,9 +2325,8 @@ define i32 @bextr32_b1_indexzext(i32 %val, i8 zeroext %numskipbits, i8 zeroext %
; X64-BMI1-LABEL: bextr32_b1_indexzext:
; X64-BMI1: # %bb.0:
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrl %eax, %edi, %eax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrl %edx, %edi, %eax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr32_b1_indexzext:
@@ -2468,9 +2465,8 @@ define i32 @bextr32_b3_load_indexzext(ptr %w, i8 zeroext %numskipbits, i8 zeroex
; X64-BMI1-LABEL: bextr32_b3_load_indexzext:
; X64-BMI1: # %bb.0:
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrl %eax, (%rdi), %eax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrl %edx, (%rdi), %eax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr32_b3_load_indexzext:
@@ -2916,10 +2912,10 @@ define i64 @bextr64_b1_indexzext(i64 %val, i8 zeroext %numskipbits, i8 zeroext %
;
; X64-BMI1-LABEL: bextr64_b1_indexzext:
; X64-BMI1: # %bb.0:
+; X64-BMI1-NEXT: # kill: def $edx killed $edx def $rdx
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrq %rax, %rdi, %rax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrq %rdx, %rdi, %rax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr64_b1_indexzext:
@@ -3205,10 +3201,10 @@ define i64 @bextr64_b3_load_indexzext(ptr %w, i8 zeroext %numskipbits, i8 zeroex
;
; X64-BMI1-LABEL: bextr64_b3_load_indexzext:
; X64-BMI1: # %bb.0:
+; X64-BMI1-NEXT: # kill: def $edx killed $edx def $rdx
; X64-BMI1-NEXT: shll $8, %edx
-; X64-BMI1-NEXT: movzbl %sil, %eax
-; X64-BMI1-NEXT: orl %edx, %eax
-; X64-BMI1-NEXT: bextrq %rax, (%rdi), %rax
+; X64-BMI1-NEXT: orl %esi, %edx
+; X64-BMI1-NEXT: bextrq %rdx, (%rdi), %rax
; X64-BMI1-NEXT: retq
;
; X64-BMI2-LABEL: bextr64_b3_load_indexzext:
More information about the llvm-commits
mailing list