https://gcc.gnu.org/g:d08f45336781c4fb2244c48a3504da2a3c2fc13b
commit r17-6466-gd08f45336781c4fb2244c48a3504da2a3c2fc13b Author: Kyrylo Tkachov <[email protected]> Date: Mon Nov 9 13:59:47 2026 +0100 bitintlower: Lower the PHI copies of an edge as a parallel copy [PR121722] The PHIs of a block read their arguments from an incoming edge in parallel, but the lowering of large and huge _BitInt PHIs emits the copies for an edge one after another in the order of the PHIs. A copy can then read a partition that an earlier copy or constant store on the same edge has already overwritten. In the PR testcase # prevFib_14 = PHI <currentFib_15(5), 0(3)> # currentFib_15 = PHI <newFib_8(5), 1(3)> newFib_8 and prevFib_14 share the partition bitint.4, currentFib_15 has bitint.5, and the latch gets bitint.4 = bitint.5; bitint.5 = bitint.4; so fib (10) returns 2 instead of 55. A rotation through the loop PHIs, or a constant store before a copy that reads the old value, gives wrong code even with -fno-tree-coalesce-vars. Lower the PHI copies edge by edge. For each edge, queue the copies in the order of the PHIs and then the constant stores, which read no partition. If a copy reads a partition that an earlier copy on the edge writes, save the partition to a temporary before all the copies and end the storage of the temporary after them. The latch above becomes D.4439 = bitint.4; bitint.4 = bitint.5; bitint.5 = D.4439; D.4439 ={v} {CLOBBER(eos)}; An edge on which no copy reads an overwritten partition gets the same statements as before, with the constant stores after the copies. Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu. gcc/ChangeLog: PR tree-optimization/121722 * gimple-lower-bitint.cc (gimple_lower_bitint): Lower the PHI copies edge by edge, with the constant stores after the copies. Save a partition that an earlier copy on the edge writes to a temporary and let the later copies read the temporary. gcc/testsuite/ChangeLog: PR tree-optimization/121722 * gcc.dg/bitint-148.c: New test. * gcc.dg/torture/bitint-116.c: New test. * gcc.dg/torture/bitint-117.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/gimple-lower-bitint.cc | 119 ++++++++++++++++---------- gcc/testsuite/gcc.dg/bitint-148.c | 131 ++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/bitint-116.c | 138 ++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/torture/bitint-117.c | 5 ++ 4 files changed, 349 insertions(+), 44 deletions(-) diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc index dc838b7faf83..0958e82015c9 100644 --- a/gcc/gimple-lower-bitint.cc +++ b/gcc/gimple-lower-bitint.cc @@ -8663,22 +8663,33 @@ gimple_lower_bitint (void) large_huge.lower_stmt (stmt); } + /* The PHIs of BB read their arguments from an incoming edge in + parallel. For each edge, queue the copies in the order of the + PHIs and then the constant stores. If a copy reads a partition + that an earlier copy on the edge writes, save the partition to a + temporary before all the copies and let the copy read that. */ tree atype = NULL_TREE; - for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); - gsi_next (&gsi)) - { - gphi *phi = gsi.phi (); - tree lhs = gimple_phi_result (phi); - if (!BITINT_TYPE_P (TREE_TYPE (lhs)) - || bitint_precision_kind (TREE_TYPE (lhs)) < bitint_prec_large) - continue; - int p1 = var_to_partition (large_huge.m_map, lhs); - gcc_assert (large_huge.m_vars[p1] != NULL_TREE); - tree v1 = large_huge.m_vars[p1]; - for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i) - { + auto_bitmap written; + hash_map<int_hash<int, -1, -2>, tree> saved; + for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i) + { + gimple_seq saves = NULL, copies = NULL, consts = NULL; + gimple_seq clobbers = NULL; + bitmap_clear (written); + saved.empty (); + for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); + gsi_next (&gsi)) + { + gphi *phi = gsi.phi (); + tree lhs = gimple_phi_result (phi); + if (!BITINT_TYPE_P (TREE_TYPE (lhs)) + || (bitint_precision_kind (TREE_TYPE (lhs)) + < bitint_prec_large)) + continue; + int p1 = var_to_partition (large_huge.m_map, lhs); + gcc_assert (large_huge.m_vars[p1] != NULL_TREE); + tree v1 = large_huge.m_vars[p1]; tree arg = gimple_phi_arg_def (phi, i); - edge e = gimple_phi_arg_edge (phi, i); gimple *g; switch (TREE_CODE (arg)) { @@ -8687,8 +8698,7 @@ gimple_lower_bitint (void) { tree zero = build_zero_cst (TREE_TYPE (v1)); g = gimple_build_assign (v1, zero); - gsi_insert_on_edge (e, g); - edge_insertions = true; + gimple_seq_add_stmt (&consts, g); break; } int ext; @@ -8736,8 +8746,7 @@ gimple_lower_bitint (void) tree v2 = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (v1), c); g = gimple_build_assign (v1, v2); - gsi_insert_on_edge (e, g); - edge_insertions = true; + gimple_seq_add_stmt (&consts, g); break; } if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE) @@ -8786,12 +8795,9 @@ gimple_lower_bitint (void) build1 (VIEW_CONVERT_EXPR, vtype, c)); } - gsi_insert_on_edge (e, g); + gimple_seq_add_stmt (&consts, g); if (min_prec == prec) - { - edge_insertions = true; - break; - } + break; } if (ext == 0) { @@ -8836,8 +8842,7 @@ gimple_lower_bitint (void) build_int_cst (sizetype, nbytes)); } - gsi_insert_on_edge (e, g); - edge_insertions = true; + gimple_seq_add_stmt (&consts, g); break; default: gcc_unreachable (); @@ -8854,6 +8859,34 @@ gimple_lower_bitint (void) continue; gcc_assert (large_huge.m_vars[p2] != NULL_TREE); tree v2 = large_huge.m_vars[p2]; + if (atype == NULL_TREE + || !tree_int_cst_equal (TYPE_SIZE (atype), + TYPE_SIZE (TREE_TYPE (lhs)))) + { + unsigned HOST_WIDE_INT nelts + = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs))) + / limb_prec; + atype = build_array_type_nelts (large_huge.m_limb_type, + nelts); + } + if (bitmap_bit_p (written, p2)) + { + bool existed; + tree &tmp = saved.get_or_insert (p2, &existed); + if (!existed) + { + tmp = create_tmp_var (atype); + if (TREE_TYPE (v2) != atype) + v2 = build1 (VIEW_CONVERT_EXPR, atype, v2); + g = gimple_build_assign (tmp, v2); + gimple_seq_add_stmt (&saves, g); + tree clobber = build_clobber (atype, + CLOBBER_STORAGE_END); + g = gimple_build_assign (tmp, clobber); + gimple_seq_add_stmt (&clobbers, g); + } + v2 = tmp; + } if (VAR_P (v1) && VAR_P (v2)) g = gimple_build_assign (v1, v2); else if (VAR_P (v1)) @@ -8863,28 +8896,26 @@ gimple_lower_bitint (void) g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR, TREE_TYPE (v2), v1), v2); else - { - if (atype == NULL_TREE - || !tree_int_cst_equal (TYPE_SIZE (atype), - TYPE_SIZE (TREE_TYPE (lhs)))) - { - unsigned HOST_WIDE_INT nelts - = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs))) - / limb_prec; - atype - = build_array_type_nelts (large_huge.m_limb_type, - nelts); - } - g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR, - atype, v1), - build1 (VIEW_CONVERT_EXPR, - atype, v2)); - } - gsi_insert_on_edge (e, g); - edge_insertions = true; + g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR, + atype, v1), + build1 (VIEW_CONVERT_EXPR, + atype, v2)); + gimple_seq_add_stmt (&copies, g); + /* The live PHI results of BB conflict with each other, so + no two copies on the edge write the same partition. */ + bool fresh = bitmap_set_bit (written, p1); + gcc_checking_assert (fresh); break; } } + gimple_seq_add_seq (&saves, copies); + gimple_seq_add_seq (&saves, consts); + gimple_seq_add_seq (&saves, clobbers); + if (saves) + { + gsi_insert_seq_on_edge (EDGE_PRED (bb, i), saves); + edge_insertions = true; + } } } diff --git a/gcc/testsuite/gcc.dg/bitint-148.c b/gcc/testsuite/gcc.dg/bitint-148.c new file mode 100644 index 000000000000..1f917bb865bd --- /dev/null +++ b/gcc/testsuite/gcc.dg/bitint-148.c @@ -0,0 +1,131 @@ +/* PR tree-optimization/121722 */ +/* { dg-do run { target bitint575 } } */ +/* { dg-options "-O2 -fgimple" } */ + +typedef unsigned _BitInt(575) U; +U res; + +/* A swap of the parameters through the loop PHIs. */ + +void __GIMPLE (ssa,startwith("bitintlower")) __attribute__((noipa)) +swap (U a, U b, int n) +{ + U x; + U y; + int i; + U r; + U t; + + __BB(2): + goto __BB3; + + __BB(3): + x_1 = __PHI (__BB2: a_5(D), __BB4: y_2); + y_2 = __PHI (__BB2: b_6(D), __BB4: x_1); + i_3 = __PHI (__BB2: 0, __BB4: i_8); + if (i_3 < n_7(D)) + goto __BB4; + else + goto __BB5; + + __BB(4): + i_8 = i_3 + 1; + goto __BB3; + + __BB(5): + t_9 = x_1 * _Literal (U) 3; + r_10 = t_9 + y_2; + res = r_10; + return; +} + +/* A constant store on the latch before a copy that reads the old + value. */ + +void __GIMPLE (ssa,startwith("bitintlower")) __attribute__((noipa)) +cst (U a0, int n) +{ + U a; + U b; + U s; + U t; + int i; + + __BB(2): + goto __BB3; + + __BB(3): + a_1 = __PHI (__BB2: a0_5(D), __BB4: _Literal (U) 0); + b_2 = __PHI (__BB2: _Literal (U) 7, __BB4: a_1); + s_3 = __PHI (__BB2: _Literal (U) 0, __BB4: s_10); + i_4 = __PHI (__BB2: 0, __BB4: i_11); + if (i_4 < n_6(D)) + goto __BB4; + else + goto __BB5; + + __BB(4): + t_9 = a_1 + b_2; + s_10 = s_3 + t_9; + i_11 = i_4 + 1; + goto __BB3; + + __BB(5): + res = s_3; + return; +} + +/* A rotation whose PHIs read the old values in reverse order. */ + +void __GIMPLE (ssa,startwith("bitintlower")) __attribute__((noipa)) +rotr (int n) +{ + U a; + U b; + U c; + U s; + U t; + int i; + + __BB(2): + goto __BB3; + + __BB(3): + a_1 = __PHI (__BB2: _Literal (U) 1, __BB4: c_3); + b_2 = __PHI (__BB2: _Literal (U) 2, __BB4: a_1); + c_3 = __PHI (__BB2: _Literal (U) 3, __BB4: b_2); + s_4 = __PHI (__BB2: _Literal (U) 0, __BB4: s_10); + i_5 = __PHI (__BB2: 0, __BB4: i_11); + if (i_5 < n_6(D)) + goto __BB4; + else + goto __BB5; + + __BB(4): + t_9 = s_4 * _Literal (U) 10; + s_10 = t_9 + a_1; + i_11 = i_5 + 1; + goto __BB3; + + __BB(5): + res = s_4; + return; +} + +int +main () +{ + swap (1, 2, 3); + if (res != 7) + __builtin_abort (); + swap (1, 2, 2); + if (res != 5) + __builtin_abort (); + cst (5, 2); + if (res != 17) + __builtin_abort (); + rotr (4); + if (res != 1321) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/torture/bitint-116.c b/gcc/testsuite/gcc.dg/torture/bitint-116.c new file mode 100644 index 000000000000..c1f7825dc593 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/bitint-116.c @@ -0,0 +1,138 @@ +/* PR tree-optimization/121722 */ +/* { dg-do run { target bitint575 } } */ + +typedef unsigned _BitInt(129) U129; +typedef _BitInt(575) S575; +typedef unsigned _BitInt(575) U575; + +/* A swap through the loop PHIs. */ + +[[gnu::noipa]] U129 +fib (unsigned long n) +{ + U129 prev = 0, cur = 1; + for (unsigned long i = 2; i <= n; ++i) + { + U129 next = prev + cur; + prev = cur; + cur = next; + } + return cur; +} + +/* Copies that cross on one edge into a join. */ + +[[gnu::noipa]] S575 +cross (S575 a, S575 b, S575 p, S575 q) +{ + S575 x = a < b ? p : q; + S575 z = a < b ? q : a; + return x + z; +} + +/* A constant store on the latch edge and a copy that reads the old + value. */ + +#define CST(name, K) \ +[[gnu::noipa]] U575 \ +name (int n, U575 a0) \ +{ \ + U575 a = a0, b = 7, s = 0; \ + for (int i = 0; i < n; ++i) \ + { \ + s += a + b; \ + b = a; \ + a = K; \ + } \ + return s; \ +} + +#define W (~(U575) 0 / 3) + +CST (cst0, 0) +CST (cst5, 5) +CST (cstm1, -1) +CST (cstw, W) + +/* Rotations in both directions. */ + +[[gnu::noipa]] U575 +rot (int n) +{ + U575 a = 1, b = 2, c = 3, s = 0; + for (int i = 0; i < n; ++i) + { + s = s * 10 + a; + U575 t = a; + a = b; + b = c; + c = t; + } + return s; +} + +[[gnu::noipa]] U575 +rotr (int n) +{ + U575 a = 1, b = 2, c = 3, s = 0; + for (int i = 0; i < n; ++i) + { + s = s * 10 + a; + U575 t = c; + c = b; + b = a; + a = t; + } + return s; +} + +/* Two copies that read the old value of one partition. */ + +[[gnu::noipa]] U575 +share (int n) +{ + U575 a = 1, b = 0, c = 5, s = 0; + for (int i = 0; i < n; ++i) + { + s = s * 10 + a + b + c; + U575 t = a; + a = a + 1; + b = t; + c = t; + } + return s; +} + +/* A swap of the parameters. */ + +[[gnu::noipa]] S575 +pswap (S575 a, S575 b, int n) +{ + for (int i = 0; i < n; ++i) + { + S575 t = a; + a = b; + b = t; + } + return a * 3 + b; +} + +int +main () +{ + if (fib (10) != 55) + __builtin_abort (); + if (cross (1, 2, 3, 4) != 7 || cross (2, 1, 3, 4) != 6) + __builtin_abort (); + if (cst0 (2, 5) != 17 + || cst5 (2, 5) != 22 + || cstm1 (2, 5) != 16 + || cstw (2, 5) != 17 + W) + __builtin_abort (); + if (rot (4) != 1231 || rotr (4) != 1321) + __builtin_abort (); + if (share (3) != 647) + __builtin_abort (); + if (pswap (1, 2, 3) != 7 || pswap (1, 2, 2) != 5) + __builtin_abort (); +} diff --git a/gcc/testsuite/gcc.dg/torture/bitint-117.c b/gcc/testsuite/gcc.dg/torture/bitint-117.c new file mode 100644 index 000000000000..85d1fa8d3230 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/bitint-117.c @@ -0,0 +1,5 @@ +/* PR tree-optimization/121722 */ +/* { dg-do run { target bitint575 } } */ +/* { dg-additional-options "-fno-tree-coalesce-vars" } */ + +#include "bitint-116.c"
