Skip to content

Commit

Permalink
[X86] computeKnownBitsForTargetNode - add BEXTR support (PR39153)
Browse files Browse the repository at this point in the history
Add a KnownBits::extractBits helper
  • Loading branch information
RKSimon committed Feb 3, 2020
1 parent 1cc3db1 commit 8ead5df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions llvm/include/llvm/Support/KnownBits.h
Expand Up @@ -157,6 +157,13 @@ struct KnownBits {
return KnownBits(Zero.zextOrTrunc(BitWidth), One.zextOrTrunc(BitWidth));
}

/// Return a KnownBits with the extracted bits
/// [bitPosition,bitPosition+numBits).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
return KnownBits(Zero.extractBits(NumBits, BitPosition),
One.extractBits(NumBits, BitPosition));
}

/// Returns the minimum number of trailing zero bits.
unsigned countMinTrailingZeros() const {
return Zero.countTrailingOnes();
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Expand Up @@ -32721,6 +32721,28 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
Known.Zero &= Known2.Zero;
break;
}
case X86ISD::BEXTR: {
SDValue Op0 = Op.getOperand(0);
SDValue Op1 = Op.getOperand(1);

if (auto* Cst1 = dyn_cast<ConstantSDNode>(Op1)) {
unsigned Shift = Cst1->getAPIntValue().extractBitsAsZExtValue(8, 0);
unsigned Length = Cst1->getAPIntValue().extractBitsAsZExtValue(8, 8);

// If the length is 0, the result is 0.
if (Length == 0) {
Known.setAllZero();
break;
}

if ((Shift + Length) <= BitWidth) {
Known = DAG.computeKnownBits(Op0, Depth + 1);
Known = Known.extractBits(Length, Shift);
Known = Known.zextOrTrunc(BitWidth, true /* ExtBitsAreKnownZero */);
}
}
break;
}
}

// Handle target shuffles.
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/X86/combine-bextr.ll
Expand Up @@ -62,7 +62,7 @@ define float @bextr_uitofp(i32 %x, i32 %y) {
; X64: # %bb.0:
; X64-NEXT: movl $3855, %eax # imm = 0xF0F
; X64-NEXT: bextrl %eax, %edi, %eax
; X64-NEXT: cvtsi2ss %rax, %xmm0
; X64-NEXT: cvtsi2ss %eax, %xmm0
; X64-NEXT: retq
%1 = tail call i32 @llvm.x86.bmi.bextr.32(i32 %x, i32 3855)
%2 = uitofp i32 %1 to float
Expand Down

0 comments on commit 8ead5df

Please sign in to comment.