Skip to content

Commit 4cd1c07

Browse files
committed
[DAG] SimplifyDemandedBits - if we're only demanding the msb, a UMIN/UMAX node can be simplified to a AND/OR node respectively.
Alive2: https://alive2.llvm.org/ce/z/qnvmc6
1 parent 5f8fd68 commit 4cd1c07

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,9 +2152,12 @@ bool TargetLowering::SimplifyDemandedBits(
21522152
break;
21532153
}
21542154
case ISD::UMIN: {
2155-
// Check if one arg is always less than (or equal) to the other arg.
21562155
SDValue Op0 = Op.getOperand(0);
21572156
SDValue Op1 = Op.getOperand(1);
2157+
// If we're only wanting the msb, then we can simplify to AND node.
2158+
if (OriginalDemandedBits.isSignMask())
2159+
return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, Op0, Op1));
2160+
// Check if one arg is always less than (or equal) to the other arg.
21582161
KnownBits Known0 = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth + 1);
21592162
KnownBits Known1 = TLO.DAG.computeKnownBits(Op1, DemandedElts, Depth + 1);
21602163
Known = KnownBits::umin(Known0, Known1);
@@ -2165,9 +2168,12 @@ bool TargetLowering::SimplifyDemandedBits(
21652168
break;
21662169
}
21672170
case ISD::UMAX: {
2168-
// Check if one arg is always greater than (or equal) to the other arg.
21692171
SDValue Op0 = Op.getOperand(0);
21702172
SDValue Op1 = Op.getOperand(1);
2173+
// If we're only wanting the msb, then we can simplify to OR node.
2174+
if (OriginalDemandedBits.isSignMask())
2175+
return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, VT, Op0, Op1));
2176+
// Check if one arg is always greater than (or equal) to the other arg.
21712177
KnownBits Known0 = TLO.DAG.computeKnownBits(Op0, DemandedElts, Depth + 1);
21722178
KnownBits Known1 = TLO.DAG.computeKnownBits(Op1, DemandedElts, Depth + 1);
21732179
Known = KnownBits::umax(Known0, Known1);

llvm/test/CodeGen/VE/Scalar/umax.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
define zeroext i1 @func_umax_var_i1(i1 noundef zeroext %0, i1 noundef zeroext %1) {
2929
; CHECK-LABEL: func_umax_var_i1:
3030
; CHECK: # %bb.0:
31-
; CHECK-NEXT: maxs.w.sx %s0, %s0, %s1
32-
; CHECK-NEXT: adds.w.zx %s0, %s0, (0)1
31+
; CHECK-NEXT: or %s0, %s0, %s1
3332
; CHECK-NEXT: b.l.t (, %s10)
3433
%3 = tail call i1 @llvm.umax.i1(i1 %0, i1 %1)
3534
ret i1 %3

llvm/test/CodeGen/VE/Scalar/umin.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
define zeroext i1 @func_umin_var_i1(i1 noundef zeroext %0, i1 noundef zeroext %1) {
2929
; CHECK-LABEL: func_umin_var_i1:
3030
; CHECK: # %bb.0:
31-
; CHECK-NEXT: mins.w.sx %s0, %s0, %s1
32-
; CHECK-NEXT: adds.w.zx %s0, %s0, (0)1
31+
; CHECK-NEXT: and %s0, %s0, %s1
3332
; CHECK-NEXT: b.l.t (, %s10)
3433
%3 = tail call i1 @llvm.umin.i1(i1 %0, i1 %1)
3534
ret i1 %3

llvm/test/CodeGen/X86/umax.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,9 +1287,7 @@ define i64 @test_signbits_i64(i64 %a, i64 %b) nounwind {
12871287
; X86-LABEL: test_signbits_i64:
12881288
; X86: # %bb.0:
12891289
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
1290-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
1291-
; X86-NEXT: cmpl %eax, %ecx
1292-
; X86-NEXT: cmoval %ecx, %eax
1290+
; X86-NEXT: orl {{[0-9]+}}(%esp), %eax
12931291
; X86-NEXT: movl %eax, %edx
12941292
; X86-NEXT: sarl $31, %edx
12951293
; X86-NEXT: retl
@@ -1304,8 +1302,7 @@ define i128 @test_signbits_i128(i128 %a, i128 %b) nounwind {
13041302
; X64: # %bb.0:
13051303
; X64-NEXT: movq %rcx, %rax
13061304
; X64-NEXT: sarq $28, %rax
1307-
; X64-NEXT: cmpq %rax, %rsi
1308-
; X64-NEXT: cmovaq %rsi, %rax
1305+
; X64-NEXT: orq %rsi, %rax
13091306
; X64-NEXT: movq %rax, %rdx
13101307
; X64-NEXT: sarq $63, %rdx
13111308
; X64-NEXT: retq

llvm/test/CodeGen/X86/umin.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,7 @@ define i64 @test_signbits_i64(i64 %a, i64 %b) nounwind {
702702
; X86-LABEL: test_signbits_i64:
703703
; X86: # %bb.0:
704704
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
705-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
706-
; X86-NEXT: cmpl %eax, %ecx
707-
; X86-NEXT: cmovbl %ecx, %eax
705+
; X86-NEXT: andl {{[0-9]+}}(%esp), %eax
708706
; X86-NEXT: movl %eax, %edx
709707
; X86-NEXT: sarl $31, %edx
710708
; X86-NEXT: retl
@@ -719,8 +717,7 @@ define i128 @test_signbits_i128(i128 %a, i128 %b) nounwind {
719717
; X64: # %bb.0:
720718
; X64-NEXT: movq %rcx, %rax
721719
; X64-NEXT: sarq $28, %rax
722-
; X64-NEXT: cmpq %rax, %rsi
723-
; X64-NEXT: cmovbq %rsi, %rax
720+
; X64-NEXT: andq %rsi, %rax
724721
; X64-NEXT: movq %rax, %rdx
725722
; X64-NEXT: sarq $63, %rdx
726723
; X64-NEXT: retq

0 commit comments

Comments
 (0)