This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] fold a * (a > 0 ? 1 : -1) to abs(a) and related optimizations
- From: Andrew Pinski <pinskia at gmail dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 23 Jun 2017 20:59:09 -0700
- Subject: [PATCH] fold a * (a > 0 ? 1 : -1) to abs(a) and related optimizations
- Authentication-results: sourceware.org; auth=none
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.