[gcc(refs/users/meissner/heads/work139)] PowerPC: Do not depend on an undefined shift

Michael Meissner meissner@gcc.gnu.org
Thu Oct 12 21:54:03 GMT 2023


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;


More information about the Gcc-cvs mailing list