Bug 109342 - [13 Regression] Wrong code with -O2 since r13-5348-gc29d85359add80
Summary: [13 Regression] Wrong code with -O2 since r13-5348-gc29d85359add80
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 13.0
: P1 normal
Target Milestone: 13.0
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks: yarpgen
  Show dependency treegraph
 
Reported: 2023-03-30 07:12 UTC by Vsevolod Livinskii
Modified: 2023-03-30 11:02 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 12.2.1
Known to fail:
Last reconfirmed: 2023-03-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vsevolod Livinskii 2023-03-30 07:12:10 UTC
Link to the Compiler Explorer: https://godbolt.org/z/zKqcna3vn

Reproducer:
#include <stdio.h>

unsigned long long int var_4 = 42;
unsigned long long int var_14 = 10;
unsigned char var_16 = 1;
unsigned short var_18 = 0;
short var_75;

inline const int &foo(const int &b, const int &c) { return b < c ? c : b; }
inline unsigned long long &bar(unsigned long long &b, unsigned long long &c) {
  return !c ? c : b;
}

void test(unsigned long long var_4, unsigned long long var_14,
          unsigned char var_16, unsigned short var_18) __attribute__((noipa));
void test(unsigned long long var_4, unsigned long long var_14,
          unsigned char var_16, unsigned short var_18) {
  for (bool h = 0; h < (bool)foo(var_16 ? -7 : 4, var_4 ? var_4 : var_18);
       h = 2)
    var_75 = bar(var_4, var_14);
}

int main() {
  test(var_4, var_14, var_16, var_18);
  printf("%hd\n", var_75);
  if (var_75 != 42)
    __builtin_abort();
}

Error:
>$ g++ -O0 small.cpp && ./a.out 
42
>$ g++ -O2 small.cpp && ./a.out 
0
Aborted (core dumped)

gcc version 13.0.1 20230329 (3be4e43a6a0f429648ea188c8e110b74268fed27)
Comment 1 Richard Biener 2023-03-30 07:26:35 UTC
Confirmed.
Comment 2 Martin Liška 2023-03-30 07:41:04 UTC
Started with r13-5348-gc29d85359add80.
Comment 3 Richard Biener 2023-03-30 07:45:40 UTC
Mine then.
Comment 4 GCC Commits 2023-03-30 11:02:34 UTC
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:1d0ba4467dd9cad11eb9ff547442e3ce6292b892

commit r13-6942-g1d0ba4467dd9cad11eb9ff547442e3ce6292b892
Author: Richard Biener <rguenther@suse.de>
Date:   Thu Mar 30 11:32:19 2023 +0200

    tree-optimization/109342 - wrong code with edge equivalences in VN
    
    The following testcase shows a problem in how we query valitity for
    equivalences on edges when the edge is a backedge and thus refering
    to a block thats later in the iteration order we use for VN.  That
    causes the dominated_by_p_w_unex helper to look at edge executable
    state that's not yet computed and thus still at optimistic not
    executable state.
    
    The following makes sure to use a plain dominance check in these cases.
    
            PR tree-optimization/109342
            * tree-ssa-sccvn.cc (vn_nary_op_get_predicated_value): New
            overload for edge.  When that edge is a backedge use
            dominated_by_p directly.
    
            * g++.dg/torture/pr109342.C: New testcase.
Comment 5 Richard Biener 2023-03-30 11:02:48 UTC
Fixed.