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

            Bug ID: 121740
           Summary: missed load fre/pre with std::optional
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: missed-optimization, needs-reduction
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
#include <optional>
#include <vector>
#include <string>

struct O
{
  std::optional <int> i;
  std::optional <std::string> s;
};

std::vector <O> a;
O a1;
void f0 ()
{
  for (auto &i: a)
    i = { };
}
void f1 ()
{
  a1 = {};
}
```

In f0, in .optimized we still have:
```
  D.58046 = {};
  SR.83_44->i = {};
  _34 = (void *) ivtmp.132_94;
  _17 = MEM[(bool *)_34 + 16B];
  pretmp_41 = MEM[(struct _Optional_payload_base &)&D.58046 + 8]._M_engaged;
```


That is pretmp_41 should be 0 here as far as I can tell. In fre3 we have:
```
  D.58030 = {};
  SR.84_7->i = {};
  _11 = &MEM[(struct _Optional_payload *)SR.84_7 + 8B].D.55976;
  _17 = MEM[(struct _Optional_payload_base *)SR.84_7 + 8B]._M_engaged;
  if (_17 != 0)
    goto <bb 4>; [50.00%]
  else
    goto <bb 16>; [50.00%]

  <bb 4> [local count: 477815112]:
  _18 = MEM[(struct _Optional_payload_base &)&D.58030 + 8]._M_engaged;
```

The store to `SR.84_7->i` should not alias with the other optional field so the
load from `MEM[(struct _Optional_payload_base &)&D.58030 + 8]._M_engaged` in
<bb 4> is still 0 (well and the store to SR.84_7->i is zeros anyways.

f1 has a similar issue but pre is able to figure out later on.

Reply via email to