Hi, I saw this on llvm's review site (https://reviews.llvm.org/D34579) and I thought why not add it to GCC. I expanded more than what was done on the LLVM patch.
I added the following optimizations: Transform X * (X > 0 ? 1 : -1) into ABS(X). Transform X * (X >= 0 ? 1 : -1) into ABS(X). Transform X * (X > 0.0 ? 1.0 : -1.0) into ABS(X). Transform X * (X >= 0.0 ? 1.0 : -1.0) into ABS(X). Transform X * (X > 0 ? -1 : 1) into -ABS(X). Transform X * (X >= 0 ? -1 : 1) into -ABS(X). Transform X * (X > 0.0 ? -1.0 : 1.0) into -ABS(X). Transform X * (X >= 0.0 ? -1.0 : 1.0) into -ABS(X). Transform X * (X < 0 ? 1 : -1) into -ABS(X). Transform X * (X <= 0 ? 1 : -1) into -ABS(X). Transform X * (X < 0.0 ? 1.0 : -1.0) into -ABS(X). Transform X * (X <= 0.0 ? 1.0 : -1.0) into -ABS(X). Transform X * (X < 0 ? -1 : 1) into ABS(X). Transform X * (X <= 0 ? -1 : 1) into ABS(X). Transform X * (X < 0.0 ? -1.0 : 1.0) into ABS(X). Transform X * (X <= 0.0 ? -1.0 : 1.0) into ABS(X). The floating points ones only happen when not honoring SNANS and not honoring signed zeros. OK? Bootstrapped and tested on aarch64-linux-gnu with no regressions. Thanks, Andrew Pinski ChangeLog: * match.pd ( X * (X >/>=/</<= 0 ? 1 : -1)): New patterns. Testsuite/ChangeLog: * testsuite/gcc.dg/tree-ssa/mult-abs-1.c: New testcase. * testsuite/gcc.dg/tree-ssa/mult-abs-2.c: New testcase.