Skip to content

Commit

Permalink
[X86] getIntImmCostInst - reduce i64 imm costs of AND(X,CMASK) case t…
Browse files Browse the repository at this point in the history
…hat can fold to BEXT/BZHI

With BEXT/BZHI the i64 imm mask will be replaced with a i16/i8 control mask

Fixes llvm#111323
  • Loading branch information
RKSimon committed Oct 7, 2024
1 parent c978d05 commit 8b6e1dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
4 changes: 4 additions & 0 deletions llvm/lib/Target/X86/X86TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5828,6 +5828,10 @@ InstructionCost X86TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
// immediates here as the normal path expects bit 31 to be sign extended.
if (Idx == 1 && ImmBitWidth == 64 && Imm.isIntN(32))
return TTI::TCC_Free;
// If we have BMI then we can use BEXTR/BZHI to mask out upper i64 bits.
if (Idx == 1 && ImmBitWidth == 64 && ST->is64Bit() && ST->hasBMI() &&
Imm.isMask())
return X86TTIImpl::getIntImmCost(ST->hasBMI2() ? 255 : 65535);
ImmIdx = 1;
break;
case Instruction::Add:
Expand Down
27 changes: 12 additions & 15 deletions llvm/test/CodeGen/X86/extract-lowbits.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4508,7 +4508,7 @@ define i64 @bzhi64_constant_mask8_load(ptr %val) nounwind {
ret i64 %masked
}

; TODO: Ensure constant hoisting doesn't prevent BEXTR/BZHI instructions in both paths.
; Ensure constant hoisting doesn't prevent BEXTR/BZHI instructions in both paths.
define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64 noundef %y) nounwind {
; X86-LABEL: PR111323:
; X86: # %bb.0: # %entry
Expand Down Expand Up @@ -4555,9 +4555,9 @@ define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64
; X64-BMI1NOTBM-NEXT: testq %rdx, %rdx
; X64-BMI1NOTBM-NEXT: je .LBB68_2
; X64-BMI1NOTBM-NEXT: # %bb.1: # %if.end
; X64-BMI1NOTBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
; X64-BMI1NOTBM-NEXT: andq %rax, %rdx
; X64-BMI1NOTBM-NEXT: movq %rdx, 8(%rdi)
; X64-BMI1NOTBM-NEXT: movl $12288, %eax # imm = 0x3000
; X64-BMI1NOTBM-NEXT: bextrq %rax, %rdx, %rax
; X64-BMI1NOTBM-NEXT: movq %rax, 8(%rdi)
; X64-BMI1NOTBM-NEXT: .LBB68_2: # %return
; X64-BMI1NOTBM-NEXT: retq
;
Expand All @@ -4568,9 +4568,8 @@ define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64
; X64-BMI1TBM-NEXT: testq %rdx, %rdx
; X64-BMI1TBM-NEXT: je .LBB68_2
; X64-BMI1TBM-NEXT: # %bb.1: # %if.end
; X64-BMI1TBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
; X64-BMI1TBM-NEXT: andq %rax, %rdx
; X64-BMI1TBM-NEXT: movq %rdx, 8(%rdi)
; X64-BMI1TBM-NEXT: bextrq $12288, %rdx, %rax # imm = 0x3000
; X64-BMI1TBM-NEXT: movq %rax, 8(%rdi)
; X64-BMI1TBM-NEXT: .LBB68_2: # %return
; X64-BMI1TBM-NEXT: retq
;
Expand All @@ -4581,23 +4580,21 @@ define void @PR111323(ptr nocapture noundef writeonly %use, i64 noundef %x, i64
; X64-BMI2TBM-NEXT: testq %rdx, %rdx
; X64-BMI2TBM-NEXT: je .LBB68_2
; X64-BMI2TBM-NEXT: # %bb.1: # %if.end
; X64-BMI2TBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
; X64-BMI2TBM-NEXT: andq %rax, %rdx
; X64-BMI2TBM-NEXT: movq %rdx, 8(%rdi)
; X64-BMI2TBM-NEXT: bextrq $12288, %rdx, %rax # imm = 0x3000
; X64-BMI2TBM-NEXT: movq %rax, 8(%rdi)
; X64-BMI2TBM-NEXT: .LBB68_2: # %return
; X64-BMI2TBM-NEXT: retq
;
; X64-BMI2NOTBM-LABEL: PR111323:
; X64-BMI2NOTBM: # %bb.0: # %entry
; X64-BMI2NOTBM-NEXT: movb $48, %al
; X64-BMI2NOTBM-NEXT: bzhiq %rax, %rsi, %rax
; X64-BMI2NOTBM-NEXT: movq %rax, (%rdi)
; X64-BMI2NOTBM-NEXT: bzhiq %rax, %rsi, %rcx
; X64-BMI2NOTBM-NEXT: movq %rcx, (%rdi)
; X64-BMI2NOTBM-NEXT: testq %rdx, %rdx
; X64-BMI2NOTBM-NEXT: je .LBB68_2
; X64-BMI2NOTBM-NEXT: # %bb.1: # %if.end
; X64-BMI2NOTBM-NEXT: movabsq $281474976710655, %rax # imm = 0xFFFFFFFFFFFF
; X64-BMI2NOTBM-NEXT: andq %rax, %rdx
; X64-BMI2NOTBM-NEXT: movq %rdx, 8(%rdi)
; X64-BMI2NOTBM-NEXT: bzhiq %rax, %rdx, %rax
; X64-BMI2NOTBM-NEXT: movq %rax, 8(%rdi)
; X64-BMI2NOTBM-NEXT: .LBB68_2: # %return
; X64-BMI2NOTBM-NEXT: retq
entry:
Expand Down

0 comments on commit 8b6e1dc

Please sign in to comment.