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

    <tr>
        <th>Summary</th>
        <td>
            Absolute value of negative maximum in an array becomes 0 at -O2
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    When compiling this source code
`
    long long  a[2]={-9223372036854775807, -9223372036854775808};
    for(int i=0;i<2;i++)
    {
        long long int tmp = a[i] >> 63;
        long long int tmp1 = a[i] ^ tmp;
        long long int ret = tmp1 - tmp;
        printf("%lld\n", ret);
    }
`

With -O2 -target x86_64-linux, clang12.0.1 generates the following assembly (only showing relevant part)
`
; Function Attrs: nofree nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 9223372036854775807)
  %2 = tail call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 undef)
  ret i32 0
}
`

With -O2 -target x86_64-linux, clang10.0.0
`
; Function Attrs: nofree nounwind
define dso_local i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 9223372036854775807)
  %2 = tail call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 -9223372036854775808)
  ret i32 0
}
`

With LLVM12.0.1, this program gives a correct result at -O0, but at -O2 the value of negative maximum becomes 0.
`
$ clang test2.c 
$ ./a.out 
9223372036854775807
-9223372036854775808
$ clang test2.c -O2
$ ./a.out 
9223372036854775807
0
`

Does not reproduce with LLVM10.0.0:
`
$ clang test2.c
$ ./a.out 
9223372036854775807
-9223372036854775808
$ clang test2.c -O2
$ ./a.out 
9223372036854775807
-9223372036854775808
`


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztVkuP0zAQ_jXJxWrkOu9DDu12OYG4wXHlJJPUyLErP_bx7xm7u9sWCkjLBRBVGj_nm2_Gnx33enzqPu9BkUEvByGFmonbC0us9mYA7B0hobuEbpKKHisEf1LjvPgiPCm3LCl3Sb5L6u2qZSzPa0bzqimLui4bWifshlzpb5IajbYn0EmbhDVCOSIQjOIYljcslmwbn_Y0G52dGpecAoRbDgRRIj2B9LBxiw-p8gufVw3X31iWt6H3F3YGXDSL9qurBgeDEyeMMWEsYaWUY1LeqNi4CfYhvnObkKDL3B_fn4Xbk9VHRlaOmxn9PjbVXVWscPX8Y8AaJFfzmmU0W5MZFBjuwOK6AuZYSv0QVplbC0svnwjy0QpLuz8OGJBwzzGiAzfuNeUnCvmWvPNqcEIrsnHO2CTfEKUnA4CFVwgyEv_geC-fpTPCJBSQ0eo7qQcuicgZSQq6cKFiNloS---8UnyB8Y6Po0FeOT1fZczYcWEcF5Lg9GcclAxCbELcWZYFMIR-TXUcQ15KeTQYwcCEfzVApMeadTDAHGLQCyh3cAZXs8cwRhugUQMVeSSIggpHD5ftTXCVWWfCkKgKQs8q7Uv96pZoz8Jif2VYiAXTWSBhAwTmLzr5PfFSFC99i_T-S-7fldzVr8jbFPj-_acPxxMygMdP3sHo2fCFzOIeT0uO3z5jYAgnu_XSEe5QtpFW759bLJ6p91x6IHoiCmbu0Jgs_FEsfiE94FcVsWj2LRFWHHVO8GB2LBvIqR8T_45nGn0c-66tZRy4mo0fwCPXN3j4bgPG905jSEqHxGDKRo_XhIfXlB73bb75VcB_ZLw_8XCZghS6dVW2VUOrukjHLh_bvOWpE05Ct-mtlt79TBhCEY6PMfzpJJJnTaXeyG7v3CGcbsgcnxnT6_sM52FDyvuXYoX5_4ISxaaw1oPFSlm1RZ3uu6adgHNGocnrmjFerKtqoOUaqnZsiomlkvcgbYf7EG8gCh5IhAi3kXKXio5RxmhDS1qxIs-zKa_KgcIwDaxizdDjdgU8TGUWeGTazKnpIqXezxYHpbDOngbxtiFmBRDdIT73bq9Nd7v36zT67SLvr_n75uY">