On Mon, 28 Oct 2024, Soumya AR wrote:

> This patch implements transformations for the following optimizations.
> 
> logN(x) CMP CST -> x CMP expN(CST)
> expN(x) CMP CST -> x CMP logN(CST)
> 
> For example:
> 
> int
> foo (float x)
> {
>   return __builtin_logf (x) < 0.0f;
> }
> 
> can just be:
> 
> int
> foo (float x)
> {
>   return x < 1.0f;
> } 
> 
> The patch was bootstrapped and regtested on aarch64-linux-gnu, no regression.
> OK for mainline?

+   (for cmp (lt le gt ge eq ne)
+    (for logs (LOG LOG2 LOG10)
+        exps (EXP EXP2 EXP10)
+    /* Simplify logN (x) CMP CST into x CMP expN (CST) */
+    (simplify
+    (cmp:c (logs:s @0) @1)
+     (cmp @0 (exps @1)))
+
+    /* Simplify expN (x) CMP CST into x CMP logN (CST) */
+    (simplify
+    (cmp:c (exps:s @0) @1)
+     (cmp @0 (logs @1))))))

this doesn't restrict @1 to be constant.  You should use

 (cmp:c (exps:s @0) REAL_CST@1)

I think this transform is also very susceptible to rounding
issues - esp. using it for eq and ne looks very dangerous
to me.  Unless you check a roundtrip through exp/log gets
you back exactly the original constant.

I think the compare kinds "most safe" would be le and ge.

You can look at fold-const-call.cc:do_mpfr_arg1, mpfr gets
you the information on whether the result is exact for example.

Richard.


> Signed-off-by: Soumya AR <soum...@nvidia.com>
> 
> gcc/ChangeLog:
> 
>       * match.pd: Fold logN(x) CMP CST -> x CMP expN(CST)
>       and expN(x) CMP CST -> x CMP logN(CST)
> 
> gcc/testsuite/ChangeLog:
> 
>       * gcc.dg/tree-ssa/log_exp.c: New test.
> 
> 

-- 
Richard Biener <rguent...@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)

Reply via email to