Skip to content

Commit 5684156

Browse files
authored
[DAG] canCreateUndefOrPoison - add handling for CTTZ/CTLZ_ZERO_UNDEF nodes (#146501)
CTTZ/CTLZ_ZERO_UNDEF nodes can only create poison if the source value is zero - so check with isKnownNeverZero Pulled out of #146361 and reapplied now that #146490 has landed.
1 parent 4a2fa08 commit 5684156

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,6 +5627,12 @@ bool SelectionDAG::canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts,
56275627
PoisonOnly, Depth + 1) ||
56285628
!getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
56295629

5630+
case ISD::CTTZ_ZERO_UNDEF:
5631+
case ISD::CTLZ_ZERO_UNDEF:
5632+
// If the amount is zero then the result will be poison.
5633+
// TODO: Add isKnownNeverZero DemandedElts handling.
5634+
return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5635+
56305636
case ISD::SCALAR_TO_VECTOR:
56315637
// Check if we demand any upper (undef) elements.
56325638
return !PoisonOnly && DemandedElts.ugt(1);

llvm/test/CodeGen/X86/freeze-unary.ll

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,15 @@ define i32 @freeze_ctlz_undef_nonzero(i32 %a0) nounwind {
179179
; X86: # %bb.0:
180180
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
181181
; X86-NEXT: orl $1, %eax
182-
; X86-NEXT: bsrl %eax, %ecx
183-
; X86-NEXT: xorl $31, %ecx
184-
; X86-NEXT: testl %eax, %eax
185-
; X86-NEXT: movl $32, %eax
186-
; X86-NEXT: cmovnel %ecx, %eax
182+
; X86-NEXT: bsrl %eax, %eax
183+
; X86-NEXT: xorl $31, %eax
187184
; X86-NEXT: retl
188185
;
189186
; X64-LABEL: freeze_ctlz_undef_nonzero:
190187
; X64: # %bb.0:
191188
; X64-NEXT: orl $1, %edi
192-
; X64-NEXT: bsrl %edi, %ecx
193-
; X64-NEXT: xorl $31, %ecx
194-
; X64-NEXT: testl %edi, %edi
195-
; X64-NEXT: movl $32, %eax
196-
; X64-NEXT: cmovnel %ecx, %eax
189+
; X64-NEXT: bsrl %edi, %eax
190+
; X64-NEXT: xorl $31, %eax
197191
; X64-NEXT: retq
198192
%y = or i32 %a0, 1
199193
%x = call i32 @llvm.ctlz.i32(i32 %y, i1 -1)
@@ -250,17 +244,13 @@ define i32 @freeze_cttz_undef_nonzero(i32 %a0) nounwind {
250244
; X86: # %bb.0:
251245
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
252246
; X86-NEXT: orl $1, %eax
253-
; X86-NEXT: bsfl %eax, %ecx
254-
; X86-NEXT: movl $32, %eax
255-
; X86-NEXT: cmovnel %ecx, %eax
247+
; X86-NEXT: rep bsfl %eax, %eax
256248
; X86-NEXT: retl
257249
;
258250
; X64-LABEL: freeze_cttz_undef_nonzero:
259251
; X64: # %bb.0:
260252
; X64-NEXT: orl $1, %edi
261-
; X64-NEXT: bsfl %edi, %ecx
262-
; X64-NEXT: movl $32, %eax
263-
; X64-NEXT: cmovnel %ecx, %eax
253+
; X64-NEXT: rep bsfl %edi, %eax
264254
; X64-NEXT: retq
265255
%y = or i32 %a0, 1
266256
%x = call i32 @llvm.cttz.i32(i32 %y, i1 -1)

0 commit comments

Comments
 (0)