public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-16 18:23 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-16 18:23 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:232f13ca9075f9ad33e6f62def03e3d6032462ce

commit 232f13ca9075f9ad33e6f62def03e3d6032462ce
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:07:36 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    Author: Jiufu Guo <guojiufu@linux.ibm.com>
    Date:   Tue Jan 10 20:52:33 2023 +0800
    
        rs6000: build constant via li/lis;rldicl/rldicr
    
        If a constant is possible left/right cleaned on a rotated value from
        a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
        to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Do not
            use a shift left by the number of bits in a host wide int.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index cc24dd5301e3..6ffd982c7bcf 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10371,7 +10371,17 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
 
+<<<<<<< HEAD
   /* If lz == 0, the left shift is undefined.  */
+=======
+  /* Different machines interpret num << shift differently if shift is at least
+     the number of bits in num's representation.  It is explicitly undefined
+     behavior in the C/C++ langauges.
+
+     In particular (-1 << 64) on an x86_64 produces -1 and (-1 << 64) on a
+     64-bit PowerPC produces 0.  This difference causes a cross compiler on
+     x86_64 to recurse until it runs out of stack.  */
+>>>>>>> 0016f87ad08 (PowerPC: Do not depend on an undefined shift)
   if (!lz)
     return false;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-16 18:24 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-16 18:24 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:6b89ca6ea6649b6a549077530f050c82245aded0

commit 6b89ca6ea6649b6a549077530f050c82245aded0
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:07:36 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    Author: Jiufu Guo <guojiufu@linux.ibm.com>
    Date:   Tue Jan 10 20:52:33 2023 +0800
    
        rs6000: build constant via li/lis;rldicl/rldicr
    
        If a constant is possible left/right cleaned on a rotated value from
        a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
        to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Do not
            use a shift left by the number of bits in a host wide int.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 21 ---------------------
 1 file changed, 21 deletions(-)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index c23ebbfa7347..8f06b37171a3 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10394,32 +10394,11 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
   /* Leading zeros may be cleaned by rldicl with a mask.  Change leading zeros
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
-<<<<<<< HEAD
-<<<<<<< HEAD
 
-<<<<<<< HEAD
   /* If lz == 0, the left shift is undefined.  */
-=======
-  /* Different machines interpret num << shift differently if shift is at least
-     the number of bits in num's representation.  It is explicitly undefined
-     behavior in the C/C++ langauges.
-
-     In particular (-1 << 64) on an x86_64 produces -1 and (-1 << 64) on a
-     64-bit PowerPC produces 0.  This difference causes a cross compiler on
-     x86_64 to recurse until it runs out of stack.  */
->>>>>>> 0016f87ad08 (PowerPC: Do not depend on an undefined shift)
   if (!lz)
     return false;
 
-=======
->>>>>>> ab63444d033 (Revert patches)
-=======
-
-  /* If lz == 0, the left shift is undefined.  */
-  if (!lz)
-    return false;
-
->>>>>>> 49fb3f85c90 (PowerPC: Do not depend on an undefined shift)
   HOST_WIDE_INT unmask_c
     = c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz));
   int n;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-16 18:23 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-16 18:23 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:36156709beefd312b27cabaa7602b87e1fae1c66

commit 36156709beefd312b27cabaa7602b87e1fae1c66
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:49:35 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    | commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    | Author: Jiufu Guo <guojiufu@linux.ibm.com>
    | Date:   Tue Jan 10 20:52:33 2023 +0800
    |
    |   rs6000: build constant via li/lis;rldicl/rldicr
    |
    |   If a constant is possible left/right cleaned on a rotated value from
    |   a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
    |   to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Protect
            code from shifts that are undefined.
            (can_be_built_by_li_lis_and_rldicr): Likewise.
            (can_be_built_by_li_and_rldic): Likewise.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index 2d1d379e1287..be6ae5c88bb4 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10371,6 +10371,7 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
 <<<<<<< HEAD
+<<<<<<< HEAD
 
 <<<<<<< HEAD
   /* If lz == 0, the left shift is undefined.  */
@@ -10388,6 +10389,13 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
 
 =======
 >>>>>>> ab63444d033 (Revert patches)
+=======
+
+  /* If lz == 0, the left shift is undefined.  */
+  if (!lz)
+    return false;
+
+>>>>>>> 49fb3f85c90 (PowerPC: Do not depend on an undefined shift)
   HOST_WIDE_INT unmask_c
     = c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz));
   int n;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-12 21:54 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-12 21:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5ba61cf9168a7f408a9af24d618a55ac44b3cb96

commit 5ba61cf9168a7f408a9af24d618a55ac44b3cb96
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:49:35 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    | commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    | Author: Jiufu Guo <guojiufu@linux.ibm.com>
    | Date:   Tue Jan 10 20:52:33 2023 +0800
    |
    |   rs6000: build constant via li/lis;rldicl/rldicr
    |
    |   If a constant is possible left/right cleaned on a rotated value from
    |   a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
    |   to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Protect
            code from shifts that are undefined.
            (can_be_built_by_li_lis_and_rldicr): Likewise.
            (can_be_built_by_li_and_rldic): Likewise.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index 2d1d379e128..be6ae5c88bb 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10371,6 +10371,7 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
 <<<<<<< HEAD
+<<<<<<< HEAD
 
 <<<<<<< HEAD
   /* If lz == 0, the left shift is undefined.  */
@@ -10388,6 +10389,13 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
 
 =======
 >>>>>>> ab63444d033 (Revert patches)
+=======
+
+  /* If lz == 0, the left shift is undefined.  */
+  if (!lz)
+    return false;
+
+>>>>>>> 49fb3f85c90 (PowerPC: Do not depend on an undefined shift)
   HOST_WIDE_INT unmask_c
     = c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz));
   int n;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-12 21:54 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-12 21:54 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:30673dbfa939bd034e12d7998a7c5a5d15ad3f27

commit 30673dbfa939bd034e12d7998a7c5a5d15ad3f27
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:07:36 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    Author: Jiufu Guo <guojiufu@linux.ibm.com>
    Date:   Tue Jan 10 20:52:33 2023 +0800
    
        rs6000: build constant via li/lis;rldicl/rldicr
    
        If a constant is possible left/right cleaned on a rotated value from
        a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
        to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Do not
            use a shift left by the number of bits in a host wide int.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index cc24dd5301e..6ffd982c7bc 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10371,7 +10371,17 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
 
+<<<<<<< HEAD
   /* If lz == 0, the left shift is undefined.  */
+=======
+  /* Different machines interpret num << shift differently if shift is at least
+     the number of bits in num's representation.  It is explicitly undefined
+     behavior in the C/C++ langauges.
+
+     In particular (-1 << 64) on an x86_64 produces -1 and (-1 << 64) on a
+     64-bit PowerPC produces 0.  This difference causes a cross compiler on
+     x86_64 to recurse until it runs out of stack.  */
+>>>>>>> 0016f87ad08 (PowerPC: Do not depend on an undefined shift)
   if (!lz)
     return false;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-12  4:49 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-12  4:49 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:49fb3f85c907777381cc5dde79dda2fc90c8dc9c

commit 49fb3f85c907777381cc5dde79dda2fc90c8dc9c
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:49:35 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    | commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    | Author: Jiufu Guo <guojiufu@linux.ibm.com>
    | Date:   Tue Jan 10 20:52:33 2023 +0800
    |
    |   rs6000: build constant via li/lis;rldicl/rldicr
    |
    |   If a constant is possible left/right cleaned on a rotated value from
    |   a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
    |   to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Protect
            code from shifts that are undefined.
            (can_be_built_by_li_lis_and_rldicr): Likewise.
            (can_be_built_by_li_and_rldic): Likewise.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index 2828f01413c..cc24dd5301e 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10370,6 +10370,11 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
   /* Leading zeros may be cleaned by rldicl with a mask.  Change leading zeros
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
+
+  /* If lz == 0, the left shift is undefined.  */
+  if (!lz)
+    return false;
+
   HOST_WIDE_INT unmask_c
     = c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz));
   int n;
@@ -10398,6 +10403,11 @@ can_be_built_by_li_lis_and_rldicr (HOST_WIDE_INT c, int *shift,
   /* Tailing zeros may be cleaned by rldicr with a mask.  Change tailing zeros
      to ones and then recheck it.  */
   int tz = ctz_hwi (c);
+
+  /* If tz == HOST_BITS_PER_WIDE_INT, the left shift is undefined.  */
+  if (tz >= HOST_BITS_PER_WIDE_INT)
+    return false;
+
   HOST_WIDE_INT unmask_c = c | ((HOST_WIDE_INT_1U << tz) - 1);
   int n;
   if (can_be_rotated_to_lowbits (~unmask_c, 15, &n)
@@ -10428,8 +10438,15 @@ can_be_built_by_li_and_rldic (HOST_WIDE_INT c, int *shift, HOST_WIDE_INT *mask)
      right bits are shifted as 0's, and left 1's(and x's) are cleaned.  */
   int tz = ctz_hwi (c);
   int lz = clz_hwi (c);
+
+  /* If lz == HOST_BITS_PER_WIDE_INT, the left shift is undefined.  */
+  if (lz >= HOST_BITS_PER_WIDE_INT)
+    return false;
+
   int middle_ones = clz_hwi (~(c << lz));
-  if (tz + lz + middle_ones >= ones)
+  if (tz + lz + middle_ones >= ones
+      && (tz - lz) < HOST_BITS_PER_WIDE_INT
+      && tz < HOST_BITS_PER_WIDE_INT)
     {
       *mask = ((1LL << (HOST_BITS_PER_WIDE_INT - tz - lz)) - 1LL) << tz;
       *shift = tz;
@@ -10440,7 +10457,8 @@ can_be_built_by_li_and_rldic (HOST_WIDE_INT c, int *shift, HOST_WIDE_INT *mask)
   int leading_ones = clz_hwi (~c);
   int tailing_ones = ctz_hwi (~c);
   int middle_zeros = ctz_hwi (c >> tailing_ones);
-  if (leading_ones + tailing_ones + middle_zeros >= ones)
+  if (leading_ones + tailing_ones + middle_zeros >= ones
+      && middle_zeros < HOST_BITS_PER_WIDE_INT)
     {
       *mask = ~(((1ULL << middle_zeros) - 1ULL) << tailing_ones);
       *shift = tailing_ones + middle_zeros;
@@ -10450,10 +10468,15 @@ can_be_built_by_li_and_rldic (HOST_WIDE_INT c, int *shift, HOST_WIDE_INT *mask)
   /* xx1..1xx: --> xx0..01..1xx: some 1's(following x's) are cleaned. */
   /* Get the position for the first bit of successive 1.
      The 24th bit would be in successive 0 or 1.  */
-  HOST_WIDE_INT low_mask = (1LL << 24) - 1LL;
+  HOST_WIDE_INT low_mask = (HOST_WIDE_INT_1U << 24) - HOST_WIDE_INT_1U;
   int pos_first_1 = ((c & (low_mask + 1)) == 0)
 		      ? clz_hwi (c & low_mask)
 		      : HOST_BITS_PER_WIDE_INT - ctz_hwi (~(c | low_mask));
+
+  /* Make sure the left and right shifts are defined.  */
+  if (!IN_RANGE (pos_first_1, 1, HOST_BITS_PER_WIDE_INT-1))
+    return false;
+
   middle_ones = clz_hwi (~c << pos_first_1);
   middle_zeros = ctz_hwi (c >> (HOST_BITS_PER_WIDE_INT - pos_first_1));
   if (pos_first_1 < HOST_BITS_PER_WIDE_INT

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift
@ 2023-10-12  4:07 Michael Meissner
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Meissner @ 2023-10-12  4:07 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0016f87ad08d0218a51c091334c9372ed6dfa497

commit 0016f87ad08d0218a51c091334c9372ed6dfa497
Author: Michael Meissner <meissner@linux.ibm.com>
Date:   Thu Oct 12 00:07:36 2023 -0400

    PowerPC: Do not depend on an undefined shift
    
    I was building a cross compiler to PowerPC on my x86_86 workstation with the
    latest version of GCC on October 11th.  I could not build the compiler on the
    x86_64 system as it died in building libgcc.  I looked into it, and I
    discovered the compiler was recursing until it ran out of stack space.  If I
    build a native compiler with the same sources on a PowerPC system, it builds
    fine.
    
    I traced this down to a change made around October 10th:
    
    commit 8f1a70a4fbcc6441c70da60d4ef6db1e5635e18a (HEAD)
    Author: Jiufu Guo <guojiufu@linux.ibm.com>
    Date:   Tue Jan 10 20:52:33 2023 +0800
    
        rs6000: build constant via li/lis;rldicl/rldicr
    
        If a constant is possible left/right cleaned on a rotated value from
        a negative value of "li/lis".  Then, using "li/lis ; rldicl/rldicr"
        to build the constant.
    
    The code was doing a -1 << 64 which is undefined behavior because different
    machines produce different results.  On the x86_64 system, (-1 << 64) produces
    -1 while on a PowerPC 64-bit system, (-1 << 64) produces 0.  The x86_64 then
    recurses until the stack runs out of space.
    
    If I apply this patch, the compiler builds fine on both x86_64 as a PowerPC
    crosss compiler and on a native PowerPC system.
    
    2023-10-12  Michael Meissner  <meissner@linux.ibm.com>
    
    gcc/
    
            PR target/111778
            * config/rs6000/rs6000.cc (can_be_built_by_li_lis_and_rldicl): Do not
            use a shift left by the number of bits in a host wide int.

Diff:
---
 gcc/config/rs6000/rs6000.cc | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index 2828f01413c..b03913fe71b 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10370,6 +10370,17 @@ can_be_built_by_li_lis_and_rldicl (HOST_WIDE_INT c, int *shift,
   /* Leading zeros may be cleaned by rldicl with a mask.  Change leading zeros
      to ones and then recheck it.  */
   int lz = clz_hwi (c);
+
+  /* Different machines interpret num << shift differently if shift is at least
+     the number of bits in num's representation.  It is explicitly undefined
+     behavior in the C/C++ langauges.
+
+     In particular (-1 << 64) on an x86_64 produces -1 and (-1 << 64) on a
+     64-bit PowerPC produces 0.  This difference causes a cross compiler on
+     x86_64 to recurse until it runs out of stack.  */
+  if (!lz)
+    return false;
+
   HOST_WIDE_INT unmask_c
     = c | (HOST_WIDE_INT_M1U << (HOST_BITS_PER_WIDE_INT - lz));
   int n;

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-10-16 18:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-16 18:23 [gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift Michael Meissner
  -- strict thread matches above, loose matches on Subject: below --
2023-10-16 18:24 Michael Meissner
2023-10-16 18:23 Michael Meissner
2023-10-12 21:54 Michael Meissner
2023-10-12 21:54 Michael Meissner
2023-10-12  4:49 Michael Meissner
2023-10-12  4:07 Michael Meissner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).