Bug 38497 - PRE missing a load PRE which causes a loop to have two BBs
Summary: PRE missing a load PRE which causes a loop to have two BBs
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.2.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-12-11 23:21 UTC by Drea Pinski
Modified: 2025-10-08 00:41 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 14.1.0
Known to fail: 13.1.0
Last reconfirmed: 2021-08-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Drea Pinski 2008-12-11 23:21:11 UTC
Testcase:
void DoHuffIteration(int);
int f(int *a)
{
  int i;
  int plaintextlen=*a;
  for(i = 0; i< 10000; i++)
     DoHuffIteration(*a);
return *a - plaintextlen;
}

--- CUT ---
There is a load of *a after that loop even though we could have placed the load inside the loop after the call.
Comment 1 Drea Pinski 2008-12-11 23:24:18 UTC
This is a reduced testcase from The huffman byte benchmark.
Comment 2 Drea Pinski 2010-03-02 19:01:15 UTC
Still happens as of today on the trunk.
Comment 3 Richard Biener 2010-03-03 10:43:30 UTC
This isn't PRE but code hoisting which we have a dup for.
Comment 4 Steven Bosscher 2010-03-03 10:57:32 UTC
I think pinskia means we could transform the test case of comment #0 to:
 
void DoHuffIteration(int);
int f(int *a)
{
  int i;
  int plaintextlen=*a;
  pretmp = plaintextlen;
  for(i = 0; i< 10000; i++)
     {
       DoHuffIteration(pretmp);
       pretmp = *a;
      }
  return pretmp - plaintextlen;
}

which makes this PRE, not hoisting.
Comment 5 Richard Biener 2010-03-03 11:03:21 UTC
I'm not so sure.  We start with

<bb 2>:
  plaintextlen_3 = *a_2(D);

<bb 3>:
  # i_13 = PHI <i_6(5), 0(2)>
  D.2725_5 = *a_2(D);
  DoHuffIteration (D.2725_5);
  i_6 = i_13 + 1;
  if (i_6 <= 9999)
    goto <bb 5>;
  else
    goto <bb 4>;

<bb 5>:
  goto <bb 3>;

<bb 4>:
  D.2725_7 = *a_2(D);

so *a_2 (4) is antic-out in bb3 but dies in bb3 (thus it's not antic-in there).
*a_2 (3) is antic-in in bb5 and available in bb2.  Thus we insert in bb5:

<bb 2>:
  plaintextlen_3 = *a_2(D);

<bb 3>:
  # i_13 = PHI <i_6(5), 0(2)>
  # prephitmp.3_14 = PHI <pretmp.2_1(5), plaintextlen_3(2)>
  D.2725_5 = prephitmp.3_14;
  DoHuffIteration (D.2725_5);
  i_6 = i_13 + 1;
  if (i_6 <= 9999)
    goto <bb 5>;
  else
    goto <bb 4>;

<bb 5>:
  pretmp.2_1 = *a_2(D);
  goto <bb 3>;

<bb 4>:
  D.2725_7 = *a_2(D);

now what is left is hoisting *a_2(D) to after the call.  But that's not PRE.
Comment 6 Steven Bosscher 2010-03-03 14:26:36 UTC
Well, it is not hoisting, either.
Comment 7 Richard Biener 2010-05-05 10:30:55 UTC
As *a_2(D) is ANTIC_OUT in bb3 (but not ANTIC_IN in bb3 because it dies in there)
can we insert in bb3 instead of in bb5?
Comment 8 Drea Pinski 2017-06-26 04:39:38 UTC
(In reply to Richard Biener from comment #5)
> now what is left is hoisting *a_2(D) to after the call.  But that's not PRE.

Yes I agree with this but hoisting was added to PRE and we still don't optimize it ...
Comment 9 Richard Biener 2017-06-26 08:28:12 UTC
I think the issue is we are not able to value-number both loads of *a to the
same value because DoHuffIteration clobbers *a.  So the *a in the block
leading to exit and the *a inserted in the latch do not have the same value.

I don't see how we can fix that easily inside PRE - we'd have to somehow
fix the value-numbering done during PHI translation to catch this case.

Iterating VN/PRE makes the value-numbering part work but hoisting still
fails because we look at ANTIC_IN & ~AVAIL_OUT but *a_6(D) is cleared
from ANTIC_OUT(3) as DoHuffIteration clobbers it.  So we really want
to look at ANTIC_OUT & ~AVAIL_OUT in do_hoist_insertion (ANTIC_IN is
a good approximation but it doesn't work here).  Of course ANTIC_OUT
we'd have to re-compute as we don't retain it.  There's the slight
complication that hoist insertion possibly inserts before the last
stmt though (that makes using ANTIC_IN a "fix" for that stmt possibly
clobbering the value).  Anyway, w/o fixing the value-numbering part
fixing hoisting is pointless of course.
Comment 10 Drea Pinski 2021-08-22 04:04:32 UTC
CLANG/LLVM does this while ICC peels off the first iteration of the loop.
Comment 11 Drea Pinski 2025-10-08 00:41:28 UTC
Looks to be fixed by r14-5095-g2266f7cbc78dfc for GCC 14 or an earlier one for PR 109849.

Either way fixed for GCC 14:
  <bb 3> [local count: 1063004408]:
  # i_13 = PHI <i_10(3), 0(2)>
  # prephitmp_4 = PHI <pretmp_12(3), plaintextlen_7(2)>
  DoHuffIteration (prephitmp_4);
  i_10 = i_13 + 1;
  pretmp_12 = *a_6(D);
  if (i_10 != 10000)
    goto <bb 3>; [98.99%]
  else
    goto <bb 4>; [1.01%]