Skip to content

[DAGCombine] Count leading ones: refine post DAG/Type Legalisation if promotion #102877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/CodeGen/SDPatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ template <typename Opnd> inline UnaryOpc_match<Opnd> m_FPToSI(const Opnd &Op) {
return UnaryOpc_match<Opnd>(ISD::FP_TO_SINT, Op);
}

template <typename Opnd> inline UnaryOpc_match<Opnd> m_Ctlz(const Opnd &Op) {
return UnaryOpc_match<Opnd>(ISD::CTLZ, Op);
}

// === Constants ===
struct ConstantInt_match {
APInt *BindVal;
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/VPIntrinsics.def
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ VP_PROPERTY_FUNCTIONAL_INTRINSIC(ctlz)
VP_PROPERTY_FUNCTIONAL_SDOPC(CTLZ)
END_REGISTER_VP_SDNODE(VP_CTLZ)
BEGIN_REGISTER_VP_SDNODE(VP_CTLZ_ZERO_UNDEF, -1, vp_ctlz_zero_undef, 1, 2)
VP_PROPERTY_FUNCTIONAL_SDOPC(CTLZ_ZERO_UNDEF)
END_REGISTER_VP_SDNODE(VP_CTLZ_ZERO_UNDEF)
END_REGISTER_VP_INTRINSIC(vp_ctlz)

Expand Down
78 changes: 78 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3755,6 +3755,79 @@ SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N, const SDLoc &DL) {
return SDValue();
}

// Refinement of DAG/Type Legalisation (promotion) when CTLZ is used for
// counting leading ones. Broadly, it replaces the substraction with a left
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a brief comment explaining what this function does, primarily the pattern?

Copy link
Contributor Author

@v01dXYZ v01dXYZ Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment. It's lengthy bc there are two patterns that are matched. And the replacing subDAG is slightly different (one any-extend when the other one does not).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping

// shift.
//
// * DAG Legalisation Pattern:
//
// (sub (ctlz (zeroextend (not Src)))
// BitWidthDiff)
//
// if BitWidthDiff == BitWidth(Node) - BitWidth(Src)
// -->
//
// (ctlz_zero_undef (not (shl (anyextend Src)
// BitWidthDiff)))
//
// * Type Legalisation Pattern:
//
// (sub (ctlz (and (xor Src XorMask)
// AndMask))
// BitWidthDiff)
//
// if AndMask has only trailing ones
// and MaskBitWidth(AndMask) == BitWidth(Node) - BitWidthDiff
// and XorMask has more trailing ones than AndMask
// -->
//
// (ctlz_zero_undef (not (shl Src BitWidthDiff)))
template <class MatchContextClass>
static SDValue foldSubCtlzNot(SDNode *N, SelectionDAG &DAG) {
const SDLoc DL(N);
SDValue N0 = N->getOperand(0);
EVT VT = N0.getValueType();
unsigned BitWidth = VT.getScalarSizeInBits();

MatchContextClass Matcher(DAG, DAG.getTargetLoweringInfo(), N);

APInt AndMask;
APInt XorMask;
APInt BitWidthDiff;

SDValue CtlzOp;
SDValue Src;

if (!sd_context_match(
N, Matcher, m_Sub(m_Ctlz(m_Value(CtlzOp)), m_ConstInt(BitWidthDiff))))
return SDValue();

if (sd_context_match(CtlzOp, Matcher, m_ZExt(m_Not(m_Value(Src))))) {
// DAG Legalisation Pattern:
// (sub (ctlz (zero_extend (not Op)) BitWidthDiff))
if ((BitWidth - Src.getValueType().getScalarSizeInBits()) != BitWidthDiff)
return SDValue();

Src = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Src);
} else if (sd_context_match(CtlzOp, Matcher,
m_And(m_Xor(m_Value(Src), m_ConstInt(XorMask)),
m_ConstInt(AndMask)))) {
// Type Legalisation Pattern:
// (sub (ctlz (and (xor Op XorMask) AndMask)) BitWidthDiff)
unsigned AndMaskWidth = BitWidth - BitWidthDiff.getZExtValue();
if (!(AndMask.isMask(AndMaskWidth) && XorMask.countr_one() >= AndMaskWidth))
return SDValue();
} else
return SDValue();

SDValue ShiftConst = DAG.getShiftAmountConstant(BitWidthDiff, VT, DL);
SDValue LShift = Matcher.getNode(ISD::SHL, DL, VT, Src, ShiftConst);
SDValue Not =
Matcher.getNode(ISD::XOR, DL, VT, LShift, DAG.getAllOnesConstant(DL, VT));

return Matcher.getNode(ISD::CTLZ_ZERO_UNDEF, DL, VT, Not);
}

// Since it may not be valid to emit a fold to zero for vector initializers
// check if we can before folding.
static SDValue tryFoldToZero(const SDLoc &DL, const TargetLowering &TLI, EVT VT,
Expand All @@ -3779,6 +3852,9 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
return N;
};

if (SDValue V = foldSubCtlzNot<EmptyMatchContext>(N, DAG))
return V;

// fold (sub x, x) -> 0
// FIXME: Refactor this and xor and other similar operations together.
if (PeekThroughFreeze(N0) == PeekThroughFreeze(N1))
Expand Down Expand Up @@ -26900,6 +26976,8 @@ SDValue DAGCombiner::visitVPOp(SDNode *N) {
return visitVP_SELECT(N);
case ISD::VP_MUL:
return visitMUL<VPMatchContext>(N);
case ISD::VP_SUB:
return foldSubCtlzNot<VPMatchContext>(N, DAG);
default:
break;
}
Expand Down
132 changes: 132 additions & 0 deletions llvm/test/CodeGen/AArch64/ctlo.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s --mtriple=aarch64 -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,CHECK-SD
; RUN: llc < %s --mtriple=aarch64 -global-isel -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,CHECK-GI

declare i8 @llvm.ctlz.i8(i8, i1)
declare i16 @llvm.ctlz.i16(i16, i1)
declare i32 @llvm.ctlz.i32(i32, i1)
declare i64 @llvm.ctlz.i64(i64, i1)

define i8 @ctlo_i8(i8 %x) {
; CHECK-SD-LABEL: ctlo_i8:
; CHECK-SD: // %bb.0:
; CHECK-SD-NEXT: mov w8, #-1 // =0xffffffff
; CHECK-SD-NEXT: eor w8, w8, w0, lsl #24
; CHECK-SD-NEXT: clz w0, w8
; CHECK-SD-NEXT: ret
;
; CHECK-GI-LABEL: ctlo_i8:
; CHECK-GI: // %bb.0:
; CHECK-GI-NEXT: mov w8, #255 // =0xff
; CHECK-GI-NEXT: bic w8, w8, w0
; CHECK-GI-NEXT: clz w8, w8
; CHECK-GI-NEXT: sub w0, w8, #24
; CHECK-GI-NEXT: ret
%tmp1 = xor i8 %x, -1
%tmp2 = call i8 @llvm.ctlz.i8( i8 %tmp1, i1 false )
ret i8 %tmp2
}

define i8 @ctlo_i8_undef(i8 %x) {
; CHECK-SD-LABEL: ctlo_i8_undef:
; CHECK-SD: // %bb.0:
; CHECK-SD-NEXT: mvn w8, w0
; CHECK-SD-NEXT: lsl w8, w8, #24
; CHECK-SD-NEXT: clz w0, w8
; CHECK-SD-NEXT: ret
;
; CHECK-GI-LABEL: ctlo_i8_undef:
; CHECK-GI: // %bb.0:
; CHECK-GI-NEXT: mov w8, #255 // =0xff
; CHECK-GI-NEXT: bic w8, w8, w0
; CHECK-GI-NEXT: clz w8, w8
; CHECK-GI-NEXT: sub w0, w8, #24
; CHECK-GI-NEXT: ret
%tmp1 = xor i8 %x, -1
%tmp2 = call i8 @llvm.ctlz.i8( i8 %tmp1, i1 true )
ret i8 %tmp2
}

define i16 @ctlo_i16(i16 %x) {
; CHECK-SD-LABEL: ctlo_i16:
; CHECK-SD: // %bb.0:
; CHECK-SD-NEXT: mov w8, #-1 // =0xffffffff
; CHECK-SD-NEXT: eor w8, w8, w0, lsl #16
; CHECK-SD-NEXT: clz w0, w8
; CHECK-SD-NEXT: ret
;
; CHECK-GI-LABEL: ctlo_i16:
; CHECK-GI: // %bb.0:
; CHECK-GI-NEXT: mov w8, #65535 // =0xffff
; CHECK-GI-NEXT: bic w8, w8, w0
; CHECK-GI-NEXT: clz w8, w8
; CHECK-GI-NEXT: sub w0, w8, #16
; CHECK-GI-NEXT: ret
%tmp1 = xor i16 %x, -1
%tmp2 = call i16 @llvm.ctlz.i16( i16 %tmp1, i1 false )
ret i16 %tmp2
}

define i16 @ctlo_i16_undef(i16 %x) {
; CHECK-SD-LABEL: ctlo_i16_undef:
; CHECK-SD: // %bb.0:
; CHECK-SD-NEXT: mvn w8, w0
; CHECK-SD-NEXT: lsl w8, w8, #16
; CHECK-SD-NEXT: clz w0, w8
; CHECK-SD-NEXT: ret
;
; CHECK-GI-LABEL: ctlo_i16_undef:
; CHECK-GI: // %bb.0:
; CHECK-GI-NEXT: mov w8, #65535 // =0xffff
; CHECK-GI-NEXT: bic w8, w8, w0
; CHECK-GI-NEXT: clz w8, w8
; CHECK-GI-NEXT: sub w0, w8, #16
; CHECK-GI-NEXT: ret
%tmp1 = xor i16 %x, -1
%tmp2 = call i16 @llvm.ctlz.i16( i16 %tmp1, i1 true )
ret i16 %tmp2
}

define i32 @ctlo_i32(i32 %x) {
; CHECK-LABEL: ctlo_i32:
; CHECK: // %bb.0:
; CHECK-NEXT: mvn w8, w0
; CHECK-NEXT: clz w0, w8
; CHECK-NEXT: ret
%tmp1 = xor i32 %x, -1
%tmp2 = call i32 @llvm.ctlz.i32( i32 %tmp1, i1 false )
ret i32 %tmp2
}

define i32 @ctlo_i32_undef(i32 %x) {
; CHECK-LABEL: ctlo_i32_undef:
; CHECK: // %bb.0:
; CHECK-NEXT: mvn w8, w0
; CHECK-NEXT: clz w0, w8
; CHECK-NEXT: ret
%tmp1 = xor i32 %x, -1
%tmp2 = call i32 @llvm.ctlz.i32( i32 %tmp1, i1 true )
ret i32 %tmp2
}

define i64 @ctlo_i64(i64 %x) {
; CHECK-LABEL: ctlo_i64:
; CHECK: // %bb.0:
; CHECK-NEXT: mvn x8, x0
; CHECK-NEXT: clz x0, x8
; CHECK-NEXT: ret
%tmp1 = xor i64 %x, -1
%tmp2 = call i64 @llvm.ctlz.i64( i64 %tmp1, i1 false )
ret i64 %tmp2
}

define i64 @ctlo_i64_undef(i64 %x) {
; CHECK-LABEL: ctlo_i64_undef:
; CHECK: // %bb.0:
; CHECK-NEXT: mvn x8, x0
; CHECK-NEXT: clz x0, x8
; CHECK-NEXT: ret
%tmp1 = xor i64 %x, -1
%tmp2 = call i64 @llvm.ctlz.i64( i64 %tmp1, i1 true )
ret i64 %tmp2
}
24 changes: 8 additions & 16 deletions llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,14 @@ define i64 @test_ctlz_i64(i64 %a) nounwind {
define i8 @test_not_ctlz_i8(i8 %a) nounwind {
; LA32-LABEL: test_not_ctlz_i8:
; LA32: # %bb.0:
; LA32-NEXT: ori $a1, $zero, 255
; LA32-NEXT: andn $a0, $a1, $a0
; LA32-NEXT: clz.w $a0, $a0
; LA32-NEXT: addi.w $a0, $a0, -24
; LA32-NEXT: slli.w $a0, $a0, 24
; LA32-NEXT: clo.w $a0, $a0
; LA32-NEXT: ret
;
; LA64-LABEL: test_not_ctlz_i8:
; LA64: # %bb.0:
; LA64-NEXT: ori $a1, $zero, 255
; LA64-NEXT: andn $a0, $a1, $a0
; LA64-NEXT: clz.d $a0, $a0
; LA64-NEXT: addi.d $a0, $a0, -56
; LA64-NEXT: slli.d $a0, $a0, 56
; LA64-NEXT: clo.d $a0, $a0
; LA64-NEXT: ret
%neg = xor i8 %a, -1
%tmp = call i8 @llvm.ctlz.i8(i8 %neg, i1 false)
Expand All @@ -110,18 +106,14 @@ define i8 @test_not_ctlz_i8(i8 %a) nounwind {
define i16 @test_not_ctlz_i16(i16 %a) nounwind {
; LA32-LABEL: test_not_ctlz_i16:
; LA32: # %bb.0:
; LA32-NEXT: nor $a0, $a0, $zero
; LA32-NEXT: bstrpick.w $a0, $a0, 15, 0
; LA32-NEXT: clz.w $a0, $a0
; LA32-NEXT: addi.w $a0, $a0, -16
; LA32-NEXT: slli.w $a0, $a0, 16
; LA32-NEXT: clo.w $a0, $a0
; LA32-NEXT: ret
;
; LA64-LABEL: test_not_ctlz_i16:
; LA64: # %bb.0:
; LA64-NEXT: nor $a0, $a0, $zero
; LA64-NEXT: bstrpick.d $a0, $a0, 15, 0
; LA64-NEXT: clz.d $a0, $a0
; LA64-NEXT: addi.d $a0, $a0, -48
; LA64-NEXT: slli.d $a0, $a0, 48
; LA64-NEXT: clo.d $a0, $a0
; LA64-NEXT: ret
%neg = xor i16 %a, -1
%tmp = call i16 @llvm.ctlz.i16(i16 %neg, i1 false)
Expand Down
Loading
Loading