Bug 98640

Summary: [10 Regression] GCC produces incorrect code with -O1 and higher since r10-2711-g3ed01d5408045d80
Product: gcc Reporter: Vsevolod Livinskii <vsevolod.livinskiy>
Component: tree-optimizationAssignee: Richard Biener <rguenth>
Status: RESOLVED FIXED    
Severity: normal CC: babokin, marxin, regehr, rguenth, vsevolod.livinskiy
Priority: P2 Keywords: wrong-code
Version: 11.0   
Target Milestone: 10.3   
Host: Target:
Build: Known to work: 10.2.1, 9.3.0
Known to fail: 10.2.0, 11.0 Last reconfirmed: 2021-01-12 00:00:00
Bug Depends on:    
Bug Blocks: 103035    

Description Vsevolod Livinskii 2021-01-12 17:01:39 UTC
Reproducer:
#include <stdio.h>

unsigned long long int var_0 = 18128133247277979402ULL;
long long int var_14 = 6557021550272328915LL;
unsigned long long int var_83 = 10966786425750692026ULL;

void test() {
  var_14 = var_0 + (bool)7;
  var_83 = 1 + (int)var_0; // 1 + 888395530
}

int main() {
    test();
    printf("%llu\n", var_83);
}

https://godbolt.org/z/axYx1f

>$ g++ -O0 driver.cpp && ./a.out 
888395531
>$ g++ -O1 driver.cpp && ./a.out 
18128133247277979403

gcc version 11.0.0 20210112 (cf2ac1c30af0fa783c8d72e527904dda5d8cc330)
Comment 1 Martin Liška 2021-01-12 18:45:21 UTC
Thanks for the report, started with r10-2711-g3ed01d5408045d80.
Comment 2 Richard Biener 2021-01-13 08:18:37 UTC
I will have a look.
Comment 3 GCC Commits 2021-01-13 10:13:55 UTC
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:ffd28c265e6d611983cd27e9332dc799039a3f04

commit r11-6632-gffd28c265e6d611983cd27e9332dc799039a3f04
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Jan 13 09:43:52 2021 +0100

    tree-optimization/98640 - fix bogus sign-extension with VN
    
    VN tried to express a sign extension from int to long of
    a trucated quantity with a plain conversion but that loses the
    truncation.  Since there's no single operand doing truncate plus
    sign extend (there was a proposed SEXT_EXPR to do that at some
    point mapping to RTL sign_extract) don't bother to appropriately
    model this with two ops (which the VN insert machinery doesn't
    handle and which is unlikely to CSE fully).
    
    2021-01-13  Richard Biener  <rguenther@suse.de>
    
            PR tree-optimization/98640
            * tree-ssa-sccvn.c (visit_nary_op): Do not try to
            handle plus or minus from a truncated operand to be
            sign-extended.
    
            * gcc.dg/torture/pr98640.c: New testcase.
Comment 4 GCC Commits 2021-03-03 10:36:26 UTC
The releases/gcc-10 branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:268b54382e5248ee1a5e3f4a0841358e03290d17

commit r10-9407-g268b54382e5248ee1a5e3f4a0841358e03290d17
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Jan 13 09:43:52 2021 +0100

    tree-optimization/98640 - fix bogus sign-extension with VN
    
    VN tried to express a sign extension from int to long of
    a trucated quantity with a plain conversion but that loses the
    truncation.  Since there's no single operand doing truncate plus
    sign extend (there was a proposed SEXT_EXPR to do that at some
    point mapping to RTL sign_extract) don't bother to appropriately
    model this with two ops (which the VN insert machinery doesn't
    handle and which is unlikely to CSE fully).
    
    2021-01-13  Richard Biener  <rguenther@suse.de>
    
            PR tree-optimization/98640
            * tree-ssa-sccvn.c (visit_nary_op): Do not try to
            handle plus or minus from a truncated operand to be
            sign-extended.
    
            * gcc.dg/torture/pr98640.c: New testcase.
    
    (cherry picked from commit ffd28c265e6d611983cd27e9332dc799039a3f04)
Comment 5 Richard Biener 2021-03-03 10:37:37 UTC
Fixed.