https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121792

            Bug ID: 121792
           Summary: missing PRE due to exceptions
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
#include  <string>
struct optional
{
  bool t;
  int value;
  constexpr operator bool() const { return t; }
  int operator *() const { return value; }
};

optional get_pid();
void kill_pid(int);

int main()
{
  std::string s;

  volatile bool is_running = true;
  optional pid = is_running ? get_pid() : optional{};

  kill_pid(0);

  if (pid)
    kill_pid(*pid);
  if (pid)
    kill_pid(*pid);
}

```

Compile this at `-O2 -fno-tree-sra -fexceptions` and a missing pre happens:
```
  <bb 3> [local count: 354334800]:
  pid = get_pid ();

  <bb 16> [local count: 354334800]:
  goto <bb 5>; [100.00%]

  <bb 4> [local count: 719407024]:
  pid = {};

  <bb 5> [local count: 1073741824]:
  kill_pid (0);
  goto <bb 6>; [100.00%]

...

  <bb 6> [local count: 1073741824]:
  _13 = pid.t;
```

But add -fno-exceptions PRE then can catch it:
```
  if (is_running.0_1 != 0)
    goto <bb 3>; [33.00%]
  else
    goto <bb 4>; [67.00%]

  <bb 3> [local count: 354334800]:
  pid = get_pid ();
  pretmp_16 = pid.t;
  goto <bb 5>; [100.00%]

  <bb 4> [local count: 719407024]:
  pid = {};

  <bb 5> [local count: 1073741824]:
  # prephitmp_14 = PHI <pretmp_16(3), 0(4)>
```

I noticed this missing optimization when looking into PR 114253 but that does
not fix the issue there.

Reply via email to