[llvm] 17dcbd8 - [SDAG] don't hoist div/rem through a select with neutral constant
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 4 10:21:27 PDT 2022
Author: Sanjay Patel
Date: 2022-10-04T13:15:01-04:00
New Revision: 17dcbd8165479d5b2d7f827bfcb271b50ee03872
URL: https://github.com/llvm/llvm-project/commit/17dcbd8165479d5b2d7f827bfcb271b50ee03872
DIFF: https://github.com/llvm/llvm-project/commit/17dcbd8165479d5b2d7f827bfcb271b50ee03872.diff
LOG: [SDAG] don't hoist div/rem through a select with neutral constant
This bug was introduced with D134966.
Added:
Modified:
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
llvm/test/CodeGen/X86/vector-bo-select.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index db9c857f0fd9d..e5c267e5c3ae8 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -2196,7 +2196,12 @@ static SDValue foldSelectWithIdentityConstant(SDNode *N, SelectionDAG &DAG,
if (N1.getOpcode() != ISD::VSELECT || !N1.hasOneUse())
return SDValue();
+ // We can't hoist div/rem because of immediate UB (not speculatable).
unsigned Opcode = N->getOpcode();
+ if (Opcode == ISD::SDIV || Opcode == ISD::UDIV ||
+ Opcode == ISD::SREM || Opcode == ISD::UREM)
+ return SDValue();
+
EVT VT = N->getValueType(0);
SDValue Cond = N1.getOperand(0);
SDValue TVal = N1.getOperand(1);
diff --git a/llvm/test/CodeGen/X86/vector-bo-select.ll b/llvm/test/CodeGen/X86/vector-bo-select.ll
index 3e125b60af152..8653835ca917b 100644
--- a/llvm/test/CodeGen/X86/vector-bo-select.ll
+++ b/llvm/test/CodeGen/X86/vector-bo-select.ll
@@ -1845,6 +1845,7 @@ define <8 x i64> @select_sdiv_neutral_constant_v8i64(<8 x i1> %b, <8 x i64> %x,
; AVX512F-NEXT: vpmovsxwq %xmm0, %zmm0
; AVX512F-NEXT: vpsllq $63, %zmm0, %zmm0
; AVX512F-NEXT: vptestmq %zmm0, %zmm0, %k1
+; AVX512F-NEXT: vpbroadcastq {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %zmm2 {%k1}
; AVX512F-NEXT: vextracti32x4 $3, %zmm2, %xmm0
; AVX512F-NEXT: vpextrq $1, %xmm0, %rcx
; AVX512F-NEXT: vextracti32x4 $3, %zmm1, %xmm3
@@ -1894,11 +1895,10 @@ define <8 x i64> @select_sdiv_neutral_constant_v8i64(<8 x i1> %b, <8 x i64> %x,
; AVX512F-NEXT: vmovq %xmm1, %rax
; AVX512F-NEXT: cqto
; AVX512F-NEXT: idivq %rcx
-; AVX512F-NEXT: vmovq %rax, %xmm2
-; AVX512F-NEXT: vpunpcklqdq {{.*#+}} xmm2 = xmm2[0],xmm4[0]
-; AVX512F-NEXT: vinserti128 $1, %xmm3, %ymm2, %ymm2
-; AVX512F-NEXT: vinserti64x4 $1, %ymm0, %zmm2, %zmm0
-; AVX512F-NEXT: vmovdqa64 %zmm1, %zmm0 {%k1}
+; AVX512F-NEXT: vmovq %rax, %xmm1
+; AVX512F-NEXT: vpunpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm4[0]
+; AVX512F-NEXT: vinserti128 $1, %xmm3, %ymm1, %ymm1
+; AVX512F-NEXT: vinserti64x4 $1, %ymm0, %zmm1, %zmm0
; AVX512F-NEXT: retq
;
; AVX512VL-LABEL: select_sdiv_neutral_constant_v8i64:
@@ -1906,6 +1906,7 @@ define <8 x i64> @select_sdiv_neutral_constant_v8i64(<8 x i1> %b, <8 x i64> %x,
; AVX512VL-NEXT: vpmovsxwd %xmm0, %ymm0
; AVX512VL-NEXT: vpslld $31, %ymm0, %ymm0
; AVX512VL-NEXT: vptestmd %ymm0, %ymm0, %k1
+; AVX512VL-NEXT: vpbroadcastq {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %zmm2 {%k1}
; AVX512VL-NEXT: vextracti32x4 $3, %zmm2, %xmm0
; AVX512VL-NEXT: vpextrq $1, %xmm0, %rcx
; AVX512VL-NEXT: vextracti32x4 $3, %zmm1, %xmm3
@@ -1955,11 +1956,10 @@ define <8 x i64> @select_sdiv_neutral_constant_v8i64(<8 x i1> %b, <8 x i64> %x,
; AVX512VL-NEXT: vmovq %xmm1, %rax
; AVX512VL-NEXT: cqto
; AVX512VL-NEXT: idivq %rcx
-; AVX512VL-NEXT: vmovq %rax, %xmm2
-; AVX512VL-NEXT: vpunpcklqdq {{.*#+}} xmm2 = xmm2[0],xmm4[0]
-; AVX512VL-NEXT: vinserti128 $1, %xmm3, %ymm2, %ymm2
-; AVX512VL-NEXT: vinserti64x4 $1, %ymm0, %zmm2, %zmm0
-; AVX512VL-NEXT: vmovdqa64 %zmm1, %zmm0 {%k1}
+; AVX512VL-NEXT: vmovq %rax, %xmm1
+; AVX512VL-NEXT: vpunpcklqdq {{.*#+}} xmm1 = xmm1[0],xmm4[0]
+; AVX512VL-NEXT: vinserti128 $1, %xmm3, %ymm1, %ymm1
+; AVX512VL-NEXT: vinserti64x4 $1, %ymm0, %zmm1, %zmm0
; AVX512VL-NEXT: retq
%sel = select <8 x i1> %b, <8 x i64> <i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1, i64 1>, <8 x i64> %y
%r = sdiv <8 x i64> %x, %sel
More information about the llvm-commits
mailing list