<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64900>64900</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            AMDGPU materializes avoidable constant infinities
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          arsenm
      </td>
    </tr>
</table>

<pre>
    These are a few different ways of expressing the same thing, but the first one saves the cost of a constant materialize. We should be able to recognize the tested value is already an infinity and use that in the nonoptimal cases

```
float avoids_inf_materialize(float ax, float y, float other) {
    float ay = __builtin_fabsf(y);

    if (__builtin_isinf(ay)) {
        bool y_is_neg_inf = y != ay;
 return ax == 1.0f ? ax : ((ax < 1.0f) ^ y_is_neg_inf ? 0.0f : ay);
 }

    return other;
}

float avoidable_materialize_1(float ax, float y, float other) {
    float ay = __builtin_fabsf(y);

 if (__builtin_isinf(ay)) {
        bool y_is_neg_inf = y == -__builtin_inff();
        return ax == 1.0f ? ax : ((ax < 1.0f) ^ y_is_neg_inf ? 0.0f : ay);
    }

    return other;
}

float avoidable_materialize_2(float ax, float y, float other) {
    float ay = __builtin_fabsf(y);

    if (__builtin_isinf(ay)) {
        bool y_is_neg_inf = y != __builtin_inff();
        return ax == 1.0f ? ax : ((ax < 1.0f) ^ y_is_neg_inf ? 0.0f : ay);
    }

    return other;
}

// __builtin_isinf should emit a compare instead of a class
float avoidable_materialize_3(float ax, float y, float other) {
    float ay = __builtin_fabsf(y);
    if (ay == __builtin_inff()) {
        bool y_is_neg_inf = y == -__builtin_inff();
        return ax == 1.0f ? ax : ((ax < 1.0f) ^ y_is_neg_inf ? 0.0f : ay);
    }

    return other;
}

```

An additional problem is the isinf check is getting emitted as llvm.is.fpclass, and not canonicalized to compare of fabs infinity
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVsGO4zYM_Rr5QkzgyEkmOfiQmTQ9FeihRY8BbVM2u7IUmPLseL6-kOydZAcFeukusFgjcGSRIsVHUk8owq0jKtX2SW1PGY6h80OJg5Drs8o3U_lHR0KAAwGCoc_QsDE0kAvwGScBb4BerwOJsGshdASCPUHo2LVKP0M1hjRreJAA3kX5C0maq32cMoBQeycBXYAeAw2Mlt9oBX8RSOdH20BFgJUlCB4Gqn3r-I2SiUASqIEXtCMBC6AdCJsJ0AE7w45DHDcwStTHAOzSOuedvwbu0UKNQqLyk8qPy3uXL7_0aazHAPjiuZELO3O526PS-0X8GoOdx9Nt6ENHg9IHUI9PszUAWGQ4gSpOcLlUI9vA7mKwEqP0flL6oIqn-y3FVWxA6f1NnYVdVMek_9FHfCrvLUwXloujNm49OZxA6XUc4PTuBQYK4-AAX6NKFK5XeVQ_z1PH6Dr6ih_PSZg8bn_5aP8M-bzyCPhVIKAeTx9DWrzOKL1H_LXeHfyxBO7Rv6y_G_7_I_gzvg93tpwxCd47sJbnO6QF4FtkRv_QnfHj50bps9Jn-IDJl-OUeg7p1O2v8WBnJ4GwWY5iiyL_neDi2yf4llp8b5t_zczP2X4faGp-Hx1g03Bg79DCdfCVpT4SYyS9uQbqjupPcaqlECJpx2qIJIoC1r70K5aVuc5loJ8TezofoEbnHdcp-00k4i_V4w3E9L3zbdaURXMoDphRud4d9ON6Wxx2WVdutjrf7mrc4V5rQ9XabKrCHIp1s8VHY6qMS53rIt9rvd5sivVuRfsat4h1tUNd1U2tNjn1yHaV9umHNmORkcrd5pDnmcWKrKSrjNYV1p_INao4Hn87_fr7n0prpZ-V1j2LUPOQ2J_fMCIVZdtTNpTR7EM1tqI2uWUJcnMUOFgqZ1v3txS59cftFrNAwSTZONiyC-EqqliasuXQjdWq9r3S52h_-Xu4Dv5vqoPS5xSVKH1Ogf0TAAD__zzR4hY">