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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
We see

  <bb 2> [local count: 3508266]:
  if (c_4(D) != 0)
    goto <bb 3>; [33.00%]
  else
    goto <bb 10>; [67.00%]

  <bb 10> [local count: 2350538]:
  goto <bb 4>; [100.00%]

  <bb 4> [local count: 3508266]:
  # v_10 = PHI <v_5(D)(10), v_8(3)>
  _3 = (unsigned int) v_10;
  _12 = _3 * 2;
  _1 = (int) _12;

  <bb 5> [local count: 354334800]:
  if (c_4(D) != 0)
    goto <bb 7>; [66.33%]
  else
    goto <bb 8>; [33.67%]

it's loop invariant motion that hoists the v + v compute out of the loop
and thus outside of its controlling condition.  You can see it's careful
to not introduce undefined overflow that is possibly conditionally
executed only but it fails to consider the case of 'v' being conditionally
uninitialized.

It's very difficult to do the right thing here - it might be tempting to
hoist the compute as

  if (c)
    tem = v+v;
  while (1)
    if (c)
      f(tem);

but apart from the technical problems in invariant motion this would
cause it does introduce another variable that's only conditionally
initialized and thus might be prone to false positive diagnostics.
Not to mention the hoisted if (c) branch having a cost.

Maybe the simplest thing would be to never hoist v + v, or only
hoist it when the controlling branch is not loop invariant.

The original testcase is probably more "sensible", does it still have
a loop invariant controlling condition and a loop invariant computation
under that control?

Reply via email to