Skip to content

Commit 547dc46

Browse files
committed
[DAG] SimplifyDemandedBits - ensure we drop NSW/NUW flags when we simplify a SHL node's input
We already do this for variable shifts, but we missed it for constant shifts Fixes llvm#69965
1 parent 12dfcc0 commit 547dc46

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1786,8 +1786,17 @@ bool TargetLowering::SimplifyDemandedBits(
17861786

17871787
APInt InDemandedMask = DemandedBits.lshr(ShAmt);
17881788
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
1789-
Depth + 1))
1789+
Depth + 1)) {
1790+
SDNodeFlags Flags = Op.getNode()->getFlags();
1791+
if (Flags.hasNoSignedWrap() || Flags.hasNoUnsignedWrap()) {
1792+
// Disable the nsw and nuw flags. We can no longer guarantee that we
1793+
// won't wrap after simplification.
1794+
Flags.setNoSignedWrap(false);
1795+
Flags.setNoUnsignedWrap(false);
1796+
Op->setFlags(Flags);
1797+
}
17901798
return true;
1799+
}
17911800
assert(!Known.hasConflict() && "Bits known to be one AND zero?");
17921801
Known.Zero <<= ShAmt;
17931802
Known.One <<= ShAmt;

llvm/test/CodeGen/X86/pr69965.ll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ define i64 @PR69965(ptr %input_ptrs, ptr %output_ptrs) {
1010
; X86-NEXT: movl (%eax), %eax
1111
; X86-NEXT: movzbl (%eax), %eax
1212
; X86-NEXT: notl %eax
13-
; X86-NEXT: movzbl %al, %edx
14-
; X86-NEXT: shll $8, %eax
13+
; X86-NEXT: movl %eax, %edx
14+
; X86-NEXT: shll $8, %edx
1515
; X86-NEXT: movl (%ecx), %ecx
16-
; X86-NEXT: leal (%eax,%edx,2), %eax
16+
; X86-NEXT: addb %al, %al
17+
; X86-NEXT: movzbl %al, %eax
18+
; X86-NEXT: orl %edx, %eax
1719
; X86-NEXT: orl $32768, %eax # imm = 0x8000
1820
; X86-NEXT: movw %ax, (%ecx)
1921
; X86-NEXT: xorl %eax, %eax
@@ -25,13 +27,14 @@ define i64 @PR69965(ptr %input_ptrs, ptr %output_ptrs) {
2527
; X64-NEXT: movq (%rdi), %rax
2628
; X64-NEXT: movzbl (%rax), %eax
2729
; X64-NEXT: notl %eax
28-
; X64-NEXT: movzbl %al, %ecx
29-
; X64-NEXT: # kill: def $eax killed $eax def $rax
30+
; X64-NEXT: leal (%rax,%rax), %ecx
31+
; X64-NEXT: # kill: def $eax killed $eax killed $rax
3032
; X64-NEXT: shll $8, %eax
3133
; X64-NEXT: movq (%rsi), %rdx
32-
; X64-NEXT: leal (%rax,%rcx,2), %eax
33-
; X64-NEXT: orl $32768, %eax # imm = 0x8000
34-
; X64-NEXT: movw %ax, (%rdx)
34+
; X64-NEXT: movzbl %cl, %ecx
35+
; X64-NEXT: orl %eax, %ecx
36+
; X64-NEXT: orl $32768, %ecx # imm = 0x8000
37+
; X64-NEXT: movw %cx, (%rdx)
3538
; X64-NEXT: xorl %eax, %eax
3639
; X64-NEXT: retq
3740
entry:

0 commit comments

Comments
 (0)