jan@localhost:/tmp> cat tt.c int array[1000]; void test (int a) { if (__builtin_expect (a > 3, 1)) return; for (int i = 0; i < a; i++) array[i]=i; } void test2 (int a) { if (__builtin_expect (a > 10, 1)) return; for (int i = 0; i < a; i++) array[i]=i; } int main() { test(1); test(2); test(3); test2(10); if (array[9] != 9) __builtin_abort (); return 0; } jan@localhost:/tmp> gcc -O2 tt.c ; ./a.out jan@localhost:/tmp> gcc -O3 tt.c ; ./a.out Aborted (core dumped) The problem here is that we do not match value ranges and thus we can end up with different estimates on number of iterations.
Reproduces on 14 and trunk. GCC 12 is not able to determine the loop bound during early optimizations
(In reply to Jan Hubicka from comment #1) > Reproduces on 14 and trunk. GCC 12 is not able to determine the loop bound > during early optimizations What about gcc 13?
> What about gcc 13? GCC 13 also misoptimizes. Honza
The master branch has been updated by Jan Hubicka <hubicka@gcc.gnu.org>: https://gcc.gnu.org/g:0d19fbc7b0760ce665fa6a88cd40cfa0311358d7 commit r15-2200-g0d19fbc7b0760ce665fa6a88cd40cfa0311358d7 Author: Jan Hubicka <hubicka@ucw.cz> Date: Mon Jul 22 18:01:57 2024 +0200 Compare loop bounds in ipa-icf Hi, this testcase shows another poblem with missing comparators for metadata in ICF. With value ranges available to loop optimizations during early opts we can estimate number of iterations based on guarding condition that can be split away by the fnsplit pass. This patch disables ICF when number of iteraitons does not match. Bootstrapped/regtesed x86_64-linux, will commit it shortly gcc/ChangeLog: PR ipa/115277 * ipa-icf-gimple.cc (func_checker::compare_loops): compare loop bounds. gcc/testsuite/ChangeLog: * gcc.c-torture/compile/pr115277.c: New test.
honza, should that be in execute/ instead?
The releases/gcc-14 branch has been updated by Jan Hubicka <hubicka@gcc.gnu.org>: https://gcc.gnu.org/g:c5397d343ff1365fcebcf3ebabe140608874aac3 commit r14-10487-gc5397d343ff1365fcebcf3ebabe140608874aac3 Author: Jan Hubicka <hubicka@ucw.cz> Date: Mon Jul 22 18:01:57 2024 +0200 Compare loop bounds in ipa-icf Hi, this testcase shows another poblem with missing comparators for metadata in ICF. With value ranges available to loop optimizations during early opts we can estimate number of iterations based on guarding condition that can be split away by the fnsplit pass. This patch disables ICF when number of iteraitons does not match. Bootstrapped/regtesed x86_64-linux, will commit it shortly gcc/ChangeLog: PR ipa/115277 * ipa-icf-gimple.cc (func_checker::compare_loops): compare loop bounds. gcc/testsuite/ChangeLog: * gcc.c-torture/compile/pr115277.c: New test. (cherry picked from commit 0d19fbc7b0760ce665fa6a88cd40cfa0311358d7)
Fixed on 14/15 so far
The master branch has been updated by Sam James <sjames@gcc.gnu.org>: https://gcc.gnu.org/g:ca255ca2760a5e6176031ea62a9c29c7bb92c212 commit r15-2383-gca255ca2760a5e6176031ea62a9c29c7bb92c212 Author: Sam James <sam@gentoo.org> Date: Mon Jul 29 16:47:09 2024 +0100 testsuite: make PR115277 test an execute one PR middle-end/115277 * gcc.c-torture/compile/pr115277.c: Rename to... * gcc.c-torture/execute/pr115277.c: ...this.
The releases/gcc-13 branch has been updated by Martin Jambor <jamborm@gcc.gnu.org>: https://gcc.gnu.org/g:e469654e5e7bdd823c5aa996075e903c6b4d47e2 commit r13-8982-ge469654e5e7bdd823c5aa996075e903c6b4d47e2 Author: Jan Hubicka <hubicka@ucw.cz> Date: Mon Aug 19 17:10:25 2024 +0200 Compare loop bounds in ipa-icf Hi, this testcase shows another poblem with missing comparators for metadata in ICF. With value ranges available to loop optimizations during early opts we can estimate number of iterations based on guarding condition that can be split away by the fnsplit pass. This patch disables ICF when number of iteraitons does not match. Bootstrapped/regtesed x86_64-linux, will commit it shortly gcc/ChangeLog: PR ipa/115277 * ipa-icf-gimple.cc (func_checker::compare_loops): compare loop bounds. gcc/testsuite/ChangeLog: * gcc.c-torture/compile/pr115277.c: New test. (cherry picked from commit 0d19fbc7b0760ce665fa6a88cd40cfa0311358d7)
Fixed backported to the last branch where it was needed.