-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[DAG] Fold (setcc ((x | x >> c0 | ...) & mask)) sequences #146054
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
Pierre-vh
merged 3 commits into
main
from
users/pierre-vh/dag-combine-workitems-intrinsics
Jul 30, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28982,13 +28982,100 @@ SDValue DAGCombiner::SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1, | |
return SDValue(); | ||
} | ||
|
||
static SDValue matchMergedBFX(SDValue Root, SelectionDAG &DAG, | ||
const TargetLowering &TLI) { | ||
// Match a pattern such as: | ||
// (X | (X >> C0) | (X >> C1) | ...) & Mask | ||
// This extracts contiguous parts of X and ORs them together before comparing. | ||
// We can optimize this so that we directly check (X & SomeMask) instead, | ||
// eliminating the shifts. | ||
|
||
EVT VT = Root.getValueType(); | ||
|
||
// TODO: Support vectors? | ||
if (!VT.isScalarInteger() || Root.getOpcode() != ISD::AND) | ||
return SDValue(); | ||
|
||
SDValue N0 = Root.getOperand(0); | ||
SDValue N1 = Root.getOperand(1); | ||
|
||
if (N0.getOpcode() != ISD::OR || !isa<ConstantSDNode>(N1)) | ||
return SDValue(); | ||
|
||
APInt RootMask = cast<ConstantSDNode>(N1)->getAsAPIntVal(); | ||
|
||
SDValue Src; | ||
const auto IsSrc = [&](SDValue V) { | ||
if (!Src) { | ||
Src = V; | ||
return true; | ||
} | ||
|
||
return Src == V; | ||
}; | ||
|
||
SmallVector<SDValue> Worklist = {N0}; | ||
APInt PartsMask(VT.getSizeInBits(), 0); | ||
while (!Worklist.empty()) { | ||
SDValue V = Worklist.pop_back_val(); | ||
if (!V.hasOneUse() && (Src && Src != V)) | ||
return SDValue(); | ||
|
||
if (V.getOpcode() == ISD::OR) { | ||
Worklist.push_back(V.getOperand(0)); | ||
Worklist.push_back(V.getOperand(1)); | ||
continue; | ||
} | ||
|
||
if (V.getOpcode() == ISD::SRL) { | ||
SDValue ShiftSrc = V.getOperand(0); | ||
SDValue ShiftAmt = V.getOperand(1); | ||
|
||
if (!IsSrc(ShiftSrc) || !isa<ConstantSDNode>(ShiftAmt)) | ||
return SDValue(); | ||
|
||
auto ShiftAmtVal = cast<ConstantSDNode>(ShiftAmt)->getAsZExtVal(); | ||
if (ShiftAmtVal > RootMask.getBitWidth()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Pierre-vh shouldn't this be |
||
return SDValue(); | ||
|
||
PartsMask |= (RootMask << ShiftAmtVal); | ||
continue; | ||
} | ||
|
||
if (IsSrc(V)) { | ||
PartsMask |= RootMask; | ||
continue; | ||
} | ||
|
||
return SDValue(); | ||
} | ||
|
||
if (!Src) | ||
return SDValue(); | ||
|
||
SDLoc DL(Root); | ||
return DAG.getNode(ISD::AND, DL, VT, | ||
{Src, DAG.getConstant(PartsMask, DL, VT)}); | ||
} | ||
|
||
/// This is a stub for TargetLowering::SimplifySetCC. | ||
SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0, SDValue N1, | ||
ISD::CondCode Cond, const SDLoc &DL, | ||
bool foldBooleans) { | ||
TargetLowering::DAGCombinerInfo | ||
DagCombineInfo(DAG, Level, false, this); | ||
return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL); | ||
if (SDValue C = | ||
TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL)) | ||
return C; | ||
|
||
if (ISD::isIntEqualitySetCC(Cond) && N0.getOpcode() == ISD::AND && | ||
isNullConstant(N1)) { | ||
|
||
if (SDValue Res = matchMergedBFX(N0, DAG, TLI)) | ||
return DAG.getSetCC(DL, VT, Res, N1, Cond); | ||
} | ||
|
||
return SDValue(); | ||
} | ||
|
||
/// Given an ISD::SDIV node expressing a divide by constant, return | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: llc -O3 -mtriple=amdgcn -mcpu=fiji %s -o - | FileCheck %s | ||
|
||
define i1 @basic_eq_i16_3x5(i16 %arg) { | ||
; CHECK-LABEL: basic_eq_i16_3x5: | ||
; CHECK: ; %bb.0: ; %entry | ||
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: v_and_b32_e32 v0, 0x7fff, v0 | ||
; CHECK-NEXT: v_cmp_eq_u16_e32 vcc, 0, v0 | ||
; CHECK-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
entry: | ||
%a = and i16 %arg, 31 | ||
%sh5 = lshr i16 %arg, 5 | ||
%b = and i16 %sh5, 31 | ||
%or = or i16 %a, %b | ||
%sh10 = lshr i16 %arg, 10 | ||
%c = and i16 %sh10, 31 | ||
%or1 = or i16 %or, %c | ||
%cmp = icmp eq i16 %or1, 0 | ||
ret i1 %cmp | ||
} | ||
|
||
define i1 @basic_eq_i32_3x5(i32 %arg) { | ||
; CHECK-LABEL: basic_eq_i32_3x5: | ||
; CHECK: ; %bb.0: ; %entry | ||
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: v_and_b32_e32 v0, 0x7fff, v0 | ||
; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 0, v0 | ||
; CHECK-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
entry: | ||
%a = and i32 %arg, 31 | ||
%sh5 = lshr i32 %arg, 5 | ||
%b = and i32 %sh5, 31 | ||
%or = or i32 %a, %b | ||
%sh10 = lshr i32 %arg, 10 | ||
%c = and i32 %sh10, 31 | ||
%or1 = or i32 %or, %c | ||
%cmp = icmp eq i32 %or1, 0 | ||
ret i1 %cmp | ||
} | ||
|
||
define i1 @basic_eq_i64_3x5(i64 %arg) { | ||
; CHECK-LABEL: basic_eq_i64_3x5: | ||
; CHECK: ; %bb.0: ; %entry | ||
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: v_and_b32_e32 v0, 0x7fff, v0 | ||
; CHECK-NEXT: v_cmp_eq_u32_e32 vcc, 0, v0 | ||
; CHECK-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
entry: | ||
%a = and i64 %arg, 31 | ||
%sh5 = lshr i64 %arg, 5 | ||
%b = and i64 %sh5, 31 | ||
%or = or i64 %a, %b | ||
%sh10 = lshr i64 %arg, 10 | ||
%c = and i64 %sh10, 31 | ||
%or1 = or i64 %or, %c | ||
%cmp = icmp eq i64 %or1, 0 | ||
ret i1 %cmp | ||
} | ||
|
||
define i1 @basic_ne_i32_3x5(i32 %arg) { | ||
; CHECK-LABEL: basic_ne_i32_3x5: | ||
; CHECK: ; %bb.0: ; %entry | ||
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: v_and_b32_e32 v0, 0x7fff, v0 | ||
; CHECK-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0 | ||
; CHECK-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
entry: | ||
%a = and i32 %arg, 31 | ||
%sh5 = lshr i32 %arg, 5 | ||
%b = and i32 %sh5, 31 | ||
%or = or i32 %a, %b | ||
%sh10 = lshr i32 %arg, 10 | ||
%c = and i32 %sh10, 31 | ||
%or1 = or i32 %or, %c | ||
%cmp = icmp ne i32 %or1, 0 | ||
ret i1 %cmp | ||
} | ||
|
||
define i1 @eq_i32_3x5_holes_in_mask(i32 %arg) { | ||
; CHECK-LABEL: eq_i32_3x5_holes_in_mask: | ||
; CHECK: ; %bb.0: ; %entry | ||
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: v_and_b32_e32 v0, 0x7f9f, v0 | ||
; CHECK-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0 | ||
; CHECK-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
entry: | ||
%a = and i32 %arg, 31 | ||
%sh5 = lshr i32 %arg, 7 | ||
%b = and i32 %sh5, 31 | ||
%or = or i32 %a, %b | ||
%sh10 = lshr i32 %arg, 10 | ||
%c = and i32 %sh10, 31 | ||
%or1 = or i32 %or, %c | ||
%cmp = icmp ne i32 %or1, 0 | ||
ret i1 %cmp | ||
} | ||
|
||
define i1 @eq_i32_3x5_all_shifted(i32 %arg) { | ||
; CHECK-LABEL: eq_i32_3x5_all_shifted: | ||
; CHECK: ; %bb.0: ; %entry | ||
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) | ||
; CHECK-NEXT: v_and_b32_e32 v0, 0x7ffc, v0 | ||
; CHECK-NEXT: v_cmp_ne_u32_e32 vcc, 0, v0 | ||
; CHECK-NEXT: v_cndmask_b32_e64 v0, 0, 1, vcc | ||
; CHECK-NEXT: s_setpc_b64 s[30:31] | ||
entry: | ||
%sh2 = lshr i32 %arg, 2 | ||
%a = and i32 %sh2, 31 | ||
%sh5 = lshr i32 %arg, 7 | ||
%b = and i32 %sh5, 31 | ||
%or = or i32 %a, %b | ||
%sh10 = lshr i32 %arg, 10 | ||
%c = and i32 %sh10, 31 | ||
%or1 = or i32 %or, %c | ||
%cmp = icmp ne i32 %or1, 0 | ||
ret i1 %cmp | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.