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.
This is a reduced testcase from The huffman byte benchmark.
Still happens as of today on the trunk.
This isn't PRE but code hoisting which we have a dup for.
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.
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.
Well, it is not hoisting, either.
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?
(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 ...
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.
CLANG/LLVM does this while ICC peels off the first iteration of the loop.
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%]