https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97690
--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>: https://gcc.gnu.org/g:3e190757fa332d327bee27495f37beb01155cfab commit r11-4717-g3e190757fa332d327bee27495f37beb01155cfab Author: Jakub Jelinek <ja...@redhat.com> Date: Wed Nov 4 11:55:29 2020 +0100 phiopt: Optimize x ? 1024 : 0 to (int) x << 10 [PR97690] The following patch generalizes the x ? 1 : 0 -> (int) x optimization to handle also left shifts by constant. During x86_64-linux and i686-linux bootstraps + regtests it triggered in 1514 unique non-LTO -m64 cases (sort -u on log mentioning filename, function name and shift count) and 1866 -m32 cases. Unfortunately, the patch regresses (before the tests have been adjusted): +FAIL: gcc.dg/tree-ssa/ssa-ccp-11.c scan-tree-dump-times optimized "if " 0 +FAIL: gcc.dg/vect/bb-slp-pattern-2.c -flto -ffat-lto-objects scan-tree-dump-times slp1 "optimized: basic block" 1 +FAIL: gcc.dg/vect/bb-slp-pattern-2.c scan-tree-dump-times slp1 "optimized: basic block" 1 and in both cases it actually results in worse code. > > We'd need some optimization that would go through all PHI edges and > > compute if some use of the phi results don't actually compute a constant > > across all the PHI edges - 1 & 0 and 0 & 1 is always 0. > PRE should do this, IMHO only optimizing it at -O2 is fine. > > Similarly, in the slp vectorization test there is: > > a[0] = b[0] ? 1 : 7; > note this, carefully avoiding the already "optimized" b[0] ? 1 : 0 ... > So the option is to put : 7 in the 2, 4 an 8 case as well. The testcase > wasn't added for any real-world case but is artificial I guess for > COND_EXPR handling of invariants. > But yeah, for things like SLP it means we eventually have to > implement reverse transforms for all of this to make the lanes > matching. But that's true anyway for things like x + 1 vs. x + 0 > or x / 3 vs. x / 2 or other simplifications we do. 2020-11-04 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/97690 * tree-ssa-phiopt.c (conditional_replacement): Also optimize cond ? pow2p_cst : 0 as ((type) cond) << cst. * gcc.dg/tree-ssa/phi-opt-22.c: New test. * gcc.dg/tree-ssa/ssa-ccp-11.c: Use -O2 instead of -O1. * gcc.dg/vect/bb-slp-pattern-2.c (foo): Use ? 2 : 7, ? 4 : 7 and ? 8 : 7 instead of ? 2 : 0, ? 4 : 0, ? 8 : 0.