]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
bitintlower: Fix up ?ROTATE_EXPR lowering [PR117847]
authorJakub Jelinek <jakub@redhat.com>
Tue, 3 Dec 2024 10:16:37 +0000 (11:16 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 9 Jan 2025 09:05:01 +0000 (10:05 +0100)
In the ?ROTATE_EXPR lowering I forgot to handle rotation by 0 correctly.
INTEGER_CST 0 is very unlikely, it would be probably folded away, but
a non-constant count can't use just p - n because then the shift count
is out of bounds for zero.

In the FE I use n == 0 ? x : (x << n) | (x >> (p - n)) but bitintlower
here isn't prepared at this point to have bb split and am not sure if
using COND_EXPR is a good idea either, so the patch uses (p - n) % p.
Perhaps I should just disable lowering the rotate in the FE for the
non-mode precision BITINT_TYPEs too.

2024-12-03  Jakub Jelinek  <jakub@redhat.com>

PR middle-end/117847
* gimple-lower-bitint.cc (gimple_lower_bitint) <case LROTATE_EXPR>:
Use m = (p - n) % p instead of m = p - n for the other shift count.

(cherry picked from commit 0b89341f124eadc689682d01193309225adfec23)

gcc/gimple-lower-bitint.cc

index e4e628b786b9a311e13d5f4a9bb15999293052e2..2072e54db7720af58f3c94a6317612b152893e47 100644 (file)
@@ -6227,11 +6227,20 @@ gimple_lower_bitint (void)
                  tree p = build_int_cst (TREE_TYPE (n),
                                          TYPE_PRECISION (type));
                  if (TREE_CODE (n) == INTEGER_CST)
-                   m = fold_build2 (MINUS_EXPR, TREE_TYPE (n), p, n);
+                   {
+                     if (integer_zerop (n))
+                       m = n;
+                     else
+                       m = fold_build2 (MINUS_EXPR, TREE_TYPE (n), p, n);
+                   }
                  else
                    {
+                     tree tem = make_ssa_name (TREE_TYPE (n));
+                     g = gimple_build_assign (tem, MINUS_EXPR, p, n);
+                     gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+                     gimple_set_location (g, loc);
                      m = make_ssa_name (TREE_TYPE (n));
-                     g = gimple_build_assign (m, MINUS_EXPR, p, n);
+                     g = gimple_build_assign (m, TRUNC_MOD_EXPR, tem, p);
                      gsi_insert_before (&gsi, g, GSI_SAME_STMT);
                      gimple_set_location (g, loc);
                    }