Skip to content

JIT: Transform 'SELECT(cond, cns, cns)' to 'cns'#127915

Open
BoyBaykiller wants to merge 2 commits intodotnet:mainfrom
BoyBaykiller:select-cond-cns-cns-to-x
Open

JIT: Transform 'SELECT(cond, cns, cns)' to 'cns'#127915
BoyBaykiller wants to merge 2 commits intodotnet:mainfrom
BoyBaykiller:select-cond-cns-cns-to-x

Conversation

@BoyBaykiller
Copy link
Copy Markdown
Contributor

@BoyBaykiller BoyBaykiller commented May 7, 2026

Example:

static bool IsDefaultValue(double value)
{
    return 0.0.Equals(value);
}
SELECT created:
               [000076] -----------                         *  SELECT    int   
N003 (  5,  8) [000056] J----+-N---                         +--*  EQ        int    $141
N001 (  1,  2) [000034] -----+-----                         |  +--*  LCL_VAR   double V00 arg0         u:1 $80
N002 (  1,  2) [000055] -----+-----                         |  \--*  LCL_VAR   double V00 arg0         u:1 (last use) $80
N001 (  1,  1) [000041] -----+-----                         +--*  CNS_INT   int    0 $40
N001 (  1,  1) [000075] -----------                         \--*  CNS_INT   int    0 $40

Note: gtFoldExprConditional can also handle this...

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 7, 2026
@BoyBaykiller BoyBaykiller changed the title JIT: 'SELECT(cond, cns, cns)' to 'cns' JIT: Transform 'SELECT(cond, cns, cns)' to 'cns' May 7, 2026
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 7, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/jit/ifconversion.cpp Outdated
{
return m_compiler->gtReverseCond(cond);
}
else if (trueVal == falseVal)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why here instead of gtFoldExpr so that we avoid ending up with such conditional select nodes in the first place?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how I am supposed to improve gtFoldExpr to handle this.
We enter if-conversion with IR equivalent to:

if (a == b)
{
    result = false;
}
else
{
    result = false;
}

return result;

where a and b are of type double so can't be folded. I am folding based on the true/false paths storing the same values, but that first requires the necessary control flow simplification which if conversion does to collapse this into a single SELECT which can then be analysed. Doesn't look like a gtFoldExpr job to me. Although there might be some opportunity very early on. Anyway I think we should have this either way.

@BoyBaykiller BoyBaykiller force-pushed the select-cond-cns-cns-to-x branch from d046532 to cca783e Compare May 8, 2026 12:31
@BoyBaykiller BoyBaykiller requested a review from jakobbotsch May 8, 2026 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants