Skip to content

Commit 71d2de8

Browse files
authored
[X86] Promote cttz_i32(x) -> cttz_i64((i64)x | (1 << 32)) (#102900)
On 64bit targets we can promote i32 CTTZ nodes to i64 CTTZ_ZERO_UNDEF by setting the 32nd bit. #57811 also queried about whether we should use BTS instead of MOVABS+OR to avoid a i64 immediate - I'm willing to tweak the DAGToDAG isel peephole for these cases if reviewers think it worthwhile. But most recent CPUs can actually handle MOVABS faster than BTS/C/R....... Fixes #57811
1 parent b7ebb67 commit 71d2de8

File tree

5 files changed

+154
-240
lines changed

5 files changed

+154
-240
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
412412
setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
413413
setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Legal);
414414
if (Subtarget.is64Bit()) {
415+
setOperationPromotedToType(ISD::CTTZ , MVT::i32, MVT::i64);
415416
setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
416417
setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Legal);
417418
}
@@ -3237,9 +3238,10 @@ bool X86TargetLowering::shouldFormOverflowOp(unsigned Opcode, EVT VT,
32373238
}
32383239

32393240
bool X86TargetLowering::isCheapToSpeculateCttz(Type *Ty) const {
3240-
// Speculate cttz only if we can directly use TZCNT or can promote to i32.
3241+
// Speculate cttz only if we can directly use TZCNT or can promote to i32/i64.
32413242
return Subtarget.hasBMI() ||
3242-
(!Ty->isVectorTy() && Ty->getScalarSizeInBits() < 32);
3243+
(!Ty->isVectorTy() &&
3244+
Ty->getScalarSizeInBits() < (Subtarget.is64Bit() ? 64u : 32u));
32433245
}
32443246

32453247
bool X86TargetLowering::isCheapToSpeculateCtlz(Type *Ty) const {

llvm/test/CodeGen/X86/cttz.ll

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,11 @@ define i32 @cttz_i32_zero_test(i32 %n) {
317317
;
318318
; X64-LABEL: cttz_i32_zero_test:
319319
; X64: # %bb.0:
320-
; X64-NEXT: testl %edi, %edi
321-
; X64-NEXT: je .LBB6_1
322-
; X64-NEXT: # %bb.2: # %cond.false
323-
; X64-NEXT: rep bsfl %edi, %eax
324-
; X64-NEXT: retq
325-
; X64-NEXT: .LBB6_1:
326-
; X64-NEXT: movl $32, %eax
320+
; X64-NEXT: # kill: def $edi killed $edi def $rdi
321+
; X64-NEXT: movabsq $4294967296, %rax # imm = 0x100000000
322+
; X64-NEXT: orq %rdi, %rax
323+
; X64-NEXT: rep bsfq %rax, %rax
324+
; X64-NEXT: # kill: def $eax killed $eax killed $rax
327325
; X64-NEXT: retq
328326
;
329327
; X86-CLZ-LABEL: cttz_i32_zero_test:

0 commit comments

Comments
 (0)