Skip to content

Commit a35ab50

Browse files
committed
[DAGCombine] Propagate nuw when evaluating sub with narrower types
1 parent 7460cbc commit a35ab50

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16317,7 +16317,15 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
1631716317
if (VT.isScalarInteger() || TLI.isOperationLegal(N0.getOpcode(), VT)) {
1631816318
SDValue NarrowL = DAG.getNode(ISD::TRUNCATE, DL, VT, N0.getOperand(0));
1631916319
SDValue NarrowR = DAG.getNode(ISD::TRUNCATE, DL, VT, N0.getOperand(1));
16320-
return DAG.getNode(N0.getOpcode(), DL, VT, NarrowL, NarrowR);
16320+
SDNodeFlags Flags;
16321+
// Propagate nuw for sub.
16322+
if (N0->getOpcode() == ISD::SUB && N0->getFlags().hasNoUnsignedWrap() &&
16323+
DAG.MaskedValueIsZero(
16324+
N0->getOperand(0),
16325+
APInt::getBitsSetFrom(SrcVT.getScalarSizeInBits(),
16326+
VT.getScalarSizeInBits())))
16327+
Flags.setNoUnsignedWrap(true);
16328+
return DAG.getNode(N0.getOpcode(), DL, VT, NarrowL, NarrowR, Flags);
1632116329
}
1632216330
}
1632316331
break;

llvm/test/CodeGen/X86/shift-i128.ll

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -951,46 +951,38 @@ define i128 @shift_i128_limited_shamt(i128 noundef %a, i32 noundef %b) {
951951
; i686-NEXT: pushl %edi
952952
; i686-NEXT: pushl %esi
953953
; i686-NEXT: andl $-16, %esp
954-
; i686-NEXT: subl $48, %esp
954+
; i686-NEXT: subl $16, %esp
955955
; i686-NEXT: .cfi_offset %esi, -20
956956
; i686-NEXT: .cfi_offset %edi, -16
957957
; i686-NEXT: .cfi_offset %ebx, -12
958-
; i686-NEXT: movzbl 40(%ebp), %eax
959-
; i686-NEXT: movl 24(%ebp), %ecx
960-
; i686-NEXT: movl 28(%ebp), %edx
961-
; i686-NEXT: movl 32(%ebp), %esi
962-
; i686-NEXT: movl 36(%ebp), %edi
963-
; i686-NEXT: movl %edi, {{[0-9]+}}(%esp)
964-
; i686-NEXT: movl %esi, {{[0-9]+}}(%esp)
965-
; i686-NEXT: movl %edx, {{[0-9]+}}(%esp)
966-
; i686-NEXT: movl %ecx, {{[0-9]+}}(%esp)
967-
; i686-NEXT: movb $6, %cl
968-
; i686-NEXT: subb %al, %cl
969-
; i686-NEXT: movl %ecx, %eax
970-
; i686-NEXT: shrb $3, %al
971-
; i686-NEXT: andb $12, %al
972-
; i686-NEXT: negb %al
973-
; i686-NEXT: movsbl %al, %eax
974-
; i686-NEXT: movl $0, {{[0-9]+}}(%esp)
975-
; i686-NEXT: movl $0, {{[0-9]+}}(%esp)
976-
; i686-NEXT: movl $0, {{[0-9]+}}(%esp)
977-
; i686-NEXT: movl $0, (%esp)
978-
; i686-NEXT: movl 20(%esp,%eax), %edx
979-
; i686-NEXT: movl 24(%esp,%eax), %ebx
980-
; i686-NEXT: movl %ebx, %edi
981-
; i686-NEXT: shldl %cl, %edx, %edi
982-
; i686-NEXT: movl 16(%esp,%eax), %esi
983-
; i686-NEXT: movl 28(%esp,%eax), %eax
984-
; i686-NEXT: shldl %cl, %ebx, %eax
985-
; i686-NEXT: movl 8(%ebp), %ebx
986-
; i686-NEXT: movl %eax, 12(%ebx)
987-
; i686-NEXT: movl %edi, 8(%ebx)
958+
; i686-NEXT: movl 28(%ebp), %esi
959+
; i686-NEXT: movl 32(%ebp), %eax
960+
; i686-NEXT: movb $6, %dl
961+
; i686-NEXT: subb 40(%ebp), %dl
962+
; i686-NEXT: movl %edx, %ecx
963+
; i686-NEXT: shll %cl, %eax
964+
; i686-NEXT: movl %esi, %ebx
965+
; i686-NEXT: movl %esi, %edi
966+
; i686-NEXT: shrl %ebx
967+
; i686-NEXT: notb %cl
968+
; i686-NEXT: shrl %cl, %ebx
969+
; i686-NEXT: orl %eax, %ebx
970+
; i686-NEXT: movl 24(%ebp), %esi
988971
; i686-NEXT: movl %esi, %eax
972+
; i686-NEXT: movl %edx, %ecx
989973
; i686-NEXT: shll %cl, %eax
990-
; i686-NEXT: shldl %cl, %esi, %edx
991-
; i686-NEXT: movl %edx, 4(%ebx)
992-
; i686-NEXT: movl %eax, (%ebx)
993-
; i686-NEXT: movl %ebx, %eax
974+
; i686-NEXT: shldl %cl, %esi, %edi
975+
; i686-NEXT: movl %edi, {{[-0-9]+}}(%e{{[sb]}}p) # 4-byte Spill
976+
; i686-NEXT: movl 8(%ebp), %edi
977+
; i686-NEXT: movl 36(%ebp), %esi
978+
; i686-NEXT: movl 32(%ebp), %edx
979+
; i686-NEXT: shldl %cl, %edx, %esi
980+
; i686-NEXT: movl %esi, 12(%edi)
981+
; i686-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %ecx # 4-byte Reload
982+
; i686-NEXT: movl %ecx, 4(%edi)
983+
; i686-NEXT: movl %eax, (%edi)
984+
; i686-NEXT: movl %ebx, 8(%edi)
985+
; i686-NEXT: movl %edi, %eax
994986
; i686-NEXT: leal -12(%ebp), %esp
995987
; i686-NEXT: popl %esi
996988
; i686-NEXT: popl %edi
@@ -1001,14 +993,11 @@ define i128 @shift_i128_limited_shamt(i128 noundef %a, i32 noundef %b) {
1001993
;
1002994
; x86_64-LABEL: shift_i128_limited_shamt:
1003995
; x86_64: # %bb.0: # %start
996+
; x86_64-NEXT: movq %rdi, %rax
1004997
; x86_64-NEXT: movb $6, %cl
1005998
; x86_64-NEXT: subb %dl, %cl
1006999
; x86_64-NEXT: shldq %cl, %rdi, %rsi
1007-
; x86_64-NEXT: shlq %cl, %rdi
1008-
; x86_64-NEXT: xorl %eax, %eax
1009-
; x86_64-NEXT: testb $64, %cl
1010-
; x86_64-NEXT: cmovneq %rdi, %rsi
1011-
; x86_64-NEXT: cmoveq %rdi, %rax
1000+
; x86_64-NEXT: shlq %cl, %rax
10121001
; x86_64-NEXT: movq %rsi, %rdx
10131002
; x86_64-NEXT: retq
10141003
start:

0 commit comments

Comments
 (0)