[llvm-branch-commits] [llvm] 6ce52ac - [DAG] LowerShiftByScalarImmediate - early out for shift-by-zero cases (#237365)
Tobias Hieta via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 8 06:42:49 PST 2026
Author: Simon Pilgrim
Date: 2026-11-08T15:42:07+01:00
New Revision: 6ce52ac1698e871ad5218c01170f0d61d90bab2c
URL: https://github.com/llvm/llvm-project/commit/6ce52ac1698e871ad5218c01170f0d61d90bab2c
DIFF: https://github.com/llvm/llvm-project/commit/6ce52ac1698e871ad5218c01170f0d61d90bab2c.diff
LOG: [DAG] LowerShiftByScalarImmediate - early out for shift-by-zero cases (#237365)
Also, add assertions to getGFNICtrlImm that we don't try to shift by
zero
Fixes #235663
(cherry picked from commit 4eff20caf9ca079c6c779f1a0029e7a8f86f4790)
Added:
Modified:
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/pr235663.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 1d3b66ca27ab5f..ec4dbe7b1e94eb 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -29374,15 +29374,21 @@ uint64_t getGFNICtrlImm(unsigned Opcode, unsigned Amt = 0) {
case ISD::CTTZ:
// Special case - only works for zero/single bit input.
return 0xAACCF0FF00000000ULL;
- case ISD::SHL:
+ case ISD::SHL: {
+ assert(Amt != 0 && "Shift-by-zero is unsupported");
return ((0x0102040810204080ULL >> (Amt)) &
(0x0101010101010101ULL * (0xFF >> (Amt))));
- case ISD::SRL:
+ }
+ case ISD::SRL: {
+ assert(Amt != 0 && "Shift-by-zero is unsupported");
return ((0x0102040810204080ULL << (Amt)) &
(0x0101010101010101ULL * ((0xFF << (Amt)) & 0xFF)));
- case ISD::SRA:
+ }
+ case ISD::SRA: {
+ assert(Amt != 0 && "Shift-by-zero is unsupported");
return (getGFNICtrlImm(ISD::SRL, Amt) |
(0x8080808080808080ULL >> (64 - (8 * Amt))));
+ }
case ISD::ROTL:
return getGFNICtrlImm(ISD::SRL, 8 - Amt) | getGFNICtrlImm(ISD::SHL, Amt);
case ISD::ROTR:
@@ -30995,6 +31001,10 @@ static SDValue LowerShiftByScalarImmediate(SDValue Op, SelectionDAG &DAG,
uint64_t ShiftAmt = APIntShiftAmt.getZExtValue();
+ // During legalization we can sometimes end up with shift-by-zero.
+ if (ShiftAmt == 0)
+ return R;
+
if (supportedVectorShiftWithImm(VT, Subtarget, Op.getOpcode()))
return getTargetVShiftByConstNode(X86Opc, dl, VT, R, ShiftAmt, DAG);
diff --git a/llvm/test/CodeGen/X86/pr235663.ll b/llvm/test/CodeGen/X86/pr235663.ll
index bae308f6cc4445..0fe54bad3e88ee 100644
--- a/llvm/test/CodeGen/X86/pr235663.ll
+++ b/llvm/test/CodeGen/X86/pr235663.ll
@@ -1,15 +1,12 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+sse2 | FileCheck %s --check-prefixes=SSE2
; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+sse4.1 | FileCheck %s --check-prefixes=SSE41
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+avx2 | FileCheck %s --check-prefixes=AVX,AVX2
-; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+avx2,+gfni | FileCheck %s --check-prefixes=AVX,GFNI
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+avx2 | FileCheck %s --check-prefixes=AVX
+; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+avx2,+gfni | FileCheck %s --check-prefixes=AVX
define <16 x i8> @PR235663(<16 x i8> %x) {
; SSE2-LABEL: PR235663:
; SSE2: # %bb.0:
-; SSE2-NEXT: movdqa {{.*#+}} xmm1 = [128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128]
-; SSE2-NEXT: pxor %xmm1, %xmm0
-; SSE2-NEXT: psubb %xmm1, %xmm0
; SSE2-NEXT: pxor %xmm1, %xmm1
; SSE2-NEXT: movdqa %xmm0, %xmm2
; SSE2-NEXT: punpcklbw {{.*#+}} xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3],xmm2[4],xmm1[4],xmm2[5],xmm1[5],xmm2[6],xmm1[6],xmm2[7],xmm1[7]
@@ -36,28 +33,14 @@ define <16 x i8> @PR235663(<16 x i8> %x) {
;
; SSE41-LABEL: PR235663:
; SSE41: # %bb.0:
-; SSE41-NEXT: movdqa {{.*#+}} xmm1 = [128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128]
; SSE41-NEXT: pshufb {{.*#+}} xmm0 = xmm0[5,0,7,11,0,9,11,3,2,1,4,6,10,12,13,14]
-; SSE41-NEXT: pxor %xmm1, %xmm0
-; SSE41-NEXT: psubb %xmm1, %xmm0
; SSE41-NEXT: retq
;
-; AVX2-LABEL: PR235663:
-; AVX2: # %bb.0:
-; AVX2-NEXT: vpbroadcastb {{.*#+}} xmm1 = [128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128]
-; AVX2-NEXT: vpshufb {{.*#+}} xmm0 = xmm0[5,0,7,11,0,9,11,3,2,1,4,6,10,12,13,14]
-; AVX2-NEXT: vpxor %xmm1, %xmm0, %xmm0
-; AVX2-NEXT: vpsubb %xmm1, %xmm0, %xmm0
-; AVX2-NEXT: retq
-;
-; GFNI-LABEL: PR235663:
-; GFNI: # %bb.0:
-; GFNI-NEXT: vgf2p8affineqb $0, {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0 # [128,192,160,144,136,132,130,129,128,192,160,144,136,132,130,129]
-; GFNI-NEXT: vpshufb {{.*#+}} xmm0 = xmm0[5,0,7,11,0,9,11,3,2,1,4,6,10,12,13,14]
-; GFNI-NEXT: retq
+; AVX-LABEL: PR235663:
+; AVX: # %bb.0:
+; AVX-NEXT: vpshufb {{.*#+}} xmm0 = xmm0[5,0,7,11,0,9,11,3,2,1,4,6,10,12,13,14]
+; AVX-NEXT: retq
%div = sdiv <16 x i8> %x, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 7, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%shuffle = shufflevector <16 x i8> %div, <16 x i8> zeroinitializer, <16 x i32> <i32 5, i32 0, i32 7, i32 11, i32 0, i32 9, i32 11, i32 3, i32 2, i32 1, i32 4, i32 6, i32 10, i32 12, i32 13, i32 14>
ret <16 x i8> %shuffle
}
-;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
-; AVX: {{.*}}
More information about the llvm-branch-commits
mailing list