[gcc r14-12748] match.pd: Guard x+x -> x*2 simplification [PR126257]

Jakub Jelinek via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:097433757515c05313a6e26d1b87cf1b5758aebc

commit r14-12748-g097433757515c05313a6e26d1b87cf1b5758aebc
Author: Jakub Jelinek <[email protected]>
Date:   Wed Jul 15 10:27:56 2026 +0200

    match.pd: Guard x+x -> x*2 simplification [PR126257]
    
    The x+x -> x*2 simplication obviously requires that 2 is representable
    in type, so that rules out unsigned _BitInt(1) and signed _BitInt(2)
    (and 1 too in C2Y, ditto unsigned:1 and signed:2 and :1), otherwise
    we don't multiply by 2 but by 0 or -2.
    While perhaps we could transform in those cases x+x to x<<1, I'm not
    convinced it is worth it.
    
    2026-07-15  Jakub Jelinek  <[email protected]>
    
            PR tree-optimization/126257
            * match.pd (x+x -> x*2): Only optimize if 2 is representable in the
            type.
    
            * gcc.dg/torture/bitint-101.c: New test.
    
    Reviewed-by: Richard Biener <[email protected]>
    (cherry picked from commit d5059b35e03e63c7686288c3554c2922a2c37468)

Diff:
---
 gcc/match.pd                              |  3 ++-
 gcc/testsuite/gcc.dg/torture/bitint-101.c | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index d1b11483520e..2ab43a773b36 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4940,7 +4940,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (plus @0 @0)
  (if (SCALAR_FLOAT_TYPE_P (type))
   (mult @0 { build_real (type, dconst2); })
-  (if (INTEGRAL_TYPE_P (type))
+  (if (INTEGRAL_TYPE_P (type)
+       && TYPE_PRECISION (type) > (2 - TYPE_UNSIGNED (type)))
    (mult @0 { build_int_cst (type, 2); }))))
 
 /* 0 - X  ->  -X.  */
diff --git a/gcc/testsuite/gcc.dg/torture/bitint-101.c b/gcc/testsuite/gcc.dg/torture/bitint-101.c
new file mode 100644
index 000000000000..42f3998ccbfc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/bitint-101.c
@@ -0,0 +1,27 @@
+/* PR tree-optimization/126257 */
+/* { dg-do run } */
+/* { dg-additional-options "-std=c23" } */
+
+_BitInt(2) a;
+long long b, c, d;
+
+int
+main ()
+{
+  int e;
+  _BitInt(2) f;
+  bool g;
+  long long h = b;
+lab:
+  e = h;
+  if (e != 4)
+    f = -1;
+  f = f + f;
+  a = f;
+  g = d - 4;
+  if (!g)
+    goto lab;
+  c = a;
+  if (c != -2)
+    __builtin_abort ();
+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.