[PATCH] Match: Fold pow calls to ldexp when possible [PR57492]

Richard Biener rguenther@suse.de
Tue Oct 29 13:46:23 GMT 2024


On Mon, 28 Oct 2024, Soumya AR wrote:

> This patch transforms the following POW calls to equivalent LDEXP calls, as
> discussed in PR57492:
> 
> powi (2.0, i) -> ldexp (1.0, i)
> 
> a * powi (2.0, i) -> ldexp (a, i)
> 
> 2.0 * powi (2.0, i) -> ldexp (1.0, i + 1)
> 
> pow (powof2, i) -> ldexp (1.0, i * log2 (powof2))
> 
> powof2 * pow (2, i) -> ldexp (1.0, i + log2 (powof2))

For the multiplication cases why not handle powof2 * ldexp (1., i)
to ldexp (1., i + log2 (powof2)) and a * ldexp (1., i) -> ldexp (a, i)
instead?  exp2 * ldexp (1., i) is another candidate.

So please split out the multiplication parts.

+ /* Simplify pow (powof2, i) to ldexp (1, i * log2 (powof2)). */

the below pattern handles POWI, not POW.

+ (simplify 
+  (POWI REAL_CST@0 @1)
+  (with { HOST_WIDE_INT tmp = 0;
+         tree integer_arg1 = NULL_TREE; }
+  (if (integer_valued_real_p (@0)
+       && real_isinteger (&TREE_REAL_CST (@0), &tmp)
+       && integer_pow2p (integer_arg1 = build_int_cst (integer_type_node, 
tmp)))

  && tmp > 0
  && pow2p_hwi (tmp)

+    (LDEXP { build_one_cst (type); }
+          (mult @1 { build_int_cst (integer_type_node,
+                     tree_log2 (integer_arg1)); })))))

build_int_cst (integer_type_node, exact_log2 (tmp))

+ /* Simplify powi (2.0, i) to ldexp (1, i). */
+ (simplify
+  (POWI REAL_CST@0 @1)
+  (if (real_equal (TREE_REAL_CST_PTR (@0), &dconst2))
+   (LDEXP { build_one_cst (type); } @1)))
+

You'll have a duplicate pattern here, instead merge them.  2.0
is power-of-two so I wonder why the pattern is needed.

Richard.

> 
> This is especially helpful for SVE architectures as LDEXP calls can be
> implemented using the FSCALE instruction, as seen in the following patch:
> https://gcc.gnu.org/pipermail/gcc-patches/2024-September/664160.html
> 
> SPEC2017 was run with this patch, while there are no noticeable improvements,
> there are no non-noise regressions either.
> 
> The patch was bootstrapped and regtested on aarch64-linux-gnu, no regression.
> OK for mainline?
> 
> Signed-off-by: Soumya AR <soumyaa@nvidia.com>
> 
> gcc/ChangeLog:
> 	PR target/57492
> 	* match.pd: Added patterns to fold certain calls to pow to ldexp.
> 
> gcc/testsuite/ChangeLog:
> 	PR target/57492
> 	* gcc.dg/tree-ssa/pow-to-ldexp.c: New test.
> 
> 

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


More information about the Gcc-patches mailing list