Skip to content

Position of constant folded operand in or-statement affects codegen #123621

@ovska

Description

@ovska

In the code below, placing the comparison with Environment.NewLine inline after a non-runtime constant check produces extra code compared to hoisting the newline check to a local, or placing it first.

x86: https://godbolt.org/z/oeMWrEWje
arm: https://godbolt.org/z/EY3f8cscG

using System;

static class B
{
    public static bool Hoisted(byte v)
    {
        bool isUnix = Environment.NewLine != "\r\n";
        return (v == 2 && isUnix);
    }

    public static bool Inline_Before(byte v)
    {
        return (Environment.NewLine != "\r\n" && v == 2);
    }

    public static bool Inline_After(byte v)
    {
        return (v == 2 && Environment.NewLine != "\r\n");
    }
}

x86 example:

// expected
       cmp      dil, 2
       sete     al
       movzx    rax, al
       ret      

// actual
       cmp      dil, 2
       je       SHORT G_M4495_IG05
       xor      eax, eax
       ret      
G_M4495_IG05:  ;; offset=0x0009
       mov      eax, 1
       ret      

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIhelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions