Skip to content

Commit 8b6e1dc

Browse files
committed
[X86] getIntImmCostInst - reduce i64 imm costs of AND(X,CMASK) case that can fold to BEXT/BZHI
With BEXT/BZHI the i64 imm mask will be replaced with a i16/i8 control mask Fixes #111323
1 parent c978d05 commit 8b6e1dc

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

llvm/lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5828,6 +5828,10 @@ InstructionCost X86TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
58285828
// immediates here as the normal path expects bit 31 to be sign extended.
58295829
if (Idx == 1 && ImmBitWidth == 64 && Imm.isIntN(32))
58305830
return TTI::TCC_Free;
5831+
// If we have BMI then we can use BEXTR/BZHI to mask out upper i64 bits.
5832+
if (Idx == 1 && ImmBitWidth == 64 && ST->is64Bit() && ST->hasBMI() &&
5833+
Imm.isMask())
5834+
return X86TTIImpl::getIntImmCost(ST->hasBMI2() ? 255 : 65535);
58315835
ImmIdx = 1;
58325836
break;
58335837
case Instruction::Add:

llvm/test/CodeGen/X86/extract-lowbits.ll

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4508,7 +4508,7 @@ define i64 @bzhi64_constant_mask8_load(ptr %val) nounwind {
45084508
ret i64 %masked
45094509
}
45104510

4511-
; TODO: Ensure constant hoisting doesn't prevent BEXTR/BZHI instructions in both paths.
4511+
; Ensure constant hoisting doesn't prevent BEXTR/BZHI instructions in both paths.
45124512
define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64 noundef %y) nounwind {
45134513
; X86-LABEL: PR111323:
45144514
; X86: # %bb.0: # %entry
@@ -4555,9 +4555,9 @@ define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64
45554555
; X64-BMI1NOTBM-NEXT: testq %rdx, %rdx
45564556
; X64-BMI1NOTBM-NEXT: je .LBB68_2
45574557
; X64-BMI1NOTBM-NEXT: # %bb.1: # %if.end
4558-
; X64-BMI1NOTBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
4559-
; X64-BMI1NOTBM-NEXT: andq %rax, %rdx
4560-
; X64-BMI1NOTBM-NEXT: movq %rdx, 8(%rdi)
4558+
; X64-BMI1NOTBM-NEXT: movl $12288, %eax # imm = 0x3000
4559+
; X64-BMI1NOTBM-NEXT: bextrq %rax, %rdx, %rax
4560+
; X64-BMI1NOTBM-NEXT: movq %rax, 8(%rdi)
45614561
; X64-BMI1NOTBM-NEXT: .LBB68_2: # %return
45624562
; X64-BMI1NOTBM-NEXT: retq
45634563
;
@@ -4568,9 +4568,8 @@ define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64
45684568
; X64-BMI1TBM-NEXT: testq %rdx, %rdx
45694569
; X64-BMI1TBM-NEXT: je .LBB68_2
45704570
; X64-BMI1TBM-NEXT: # %bb.1: # %if.end
4571-
; X64-BMI1TBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
4572-
; X64-BMI1TBM-NEXT: andq %rax, %rdx
4573-
; X64-BMI1TBM-NEXT: movq %rdx, 8(%rdi)
4571+
; X64-BMI1TBM-NEXT: bextrq $12288, %rdx, %rax # imm = 0x3000
4572+
; X64-BMI1TBM-NEXT: movq %rax, 8(%rdi)
45744573
; X64-BMI1TBM-NEXT: .LBB68_2: # %return
45754574
; X64-BMI1TBM-NEXT: retq
45764575
;
@@ -4581,23 +4580,21 @@ define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64
45814580
; X64-BMI2TBM-NEXT: testq %rdx, %rdx
45824581
; X64-BMI2TBM-NEXT: je .LBB68_2
45834582
; X64-BMI2TBM-NEXT: # %bb.1: # %if.end
4584-
; X64-BMI2TBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
4585-
; X64-BMI2TBM-NEXT: andq %rax, %rdx
4586-
; X64-BMI2TBM-NEXT: movq %rdx, 8(%rdi)
4583+
; X64-BMI2TBM-NEXT: bextrq $12288, %rdx, %rax # imm = 0x3000
4584+
; X64-BMI2TBM-NEXT: movq %rax, 8(%rdi)
45874585
; X64-BMI2TBM-NEXT: .LBB68_2: # %return
45884586
; X64-BMI2TBM-NEXT: retq
45894587
;
45904588
; X64-BMI2NOTBM-LABEL: PR111323:
45914589
; X64-BMI2NOTBM: # %bb.0: # %entry
45924590
; X64-BMI2NOTBM-NEXT: movb $48, %al
4593-
; X64-BMI2NOTBM-NEXT: bzhiq %rax, %rsi, %rax
4594-
; X64-BMI2NOTBM-NEXT: movq %rax, (%rdi)
4591+
; X64-BMI2NOTBM-NEXT: bzhiq %rax, %rsi, %rcx
4592+
; X64-BMI2NOTBM-NEXT: movq %rcx, (%rdi)
45954593
; X64-BMI2NOTBM-NEXT: testq %rdx, %rdx
45964594
; X64-BMI2NOTBM-NEXT: je .LBB68_2
45974595
; X64-BMI2NOTBM-NEXT: # %bb.1: # %if.end
4598-
; X64-BMI2NOTBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
4599-
; X64-BMI2NOTBM-NEXT: andq %rax, %rdx
4600-
; X64-BMI2NOTBM-NEXT: movq %rdx, 8(%rdi)
4596+
; X64-BMI2NOTBM-NEXT: bzhiq %rax, %rdx, %rax
4597+
; X64-BMI2NOTBM-NEXT: movq %rax, 8(%rdi)
46014598
; X64-BMI2NOTBM-NEXT: .LBB68_2: # %return
46024599
; X64-BMI2NOTBM-NEXT: retq
46034600
entry:

0 commit comments

Comments
 (0)