Skip to content

Commit 0303974

Browse files
committed
expr: Fix up emit_push_insn [PR114552]
r13-990 added optimizations in multiple spots to optimize during expansion storing of constant initializers into targets. In the load_register_parameters and expand_expr_real_1 cases, it checks it has a tree as the source and so knows we are reading that whole decl's value, so the code is fine as is, but in the emit_push_insn case it checks for a MEM from which something is pushed and checks for SYMBOL_REF as the MEM's address, but still assumes the whole object is copied, which as the following testcase shows might not always be the case. In the testcase, k is 6 bytes, then 2 bytes of padding, then another 4 bytes, while the emit_push_insn wants to store just the 6 bytes. The following patch simply verifies it is the whole initializer that is being stored, I think that is best thing to do so late in GCC 14 cycle as well for backporting. For GCC 15, perhaps the code could stop requiring it must be at offset zero, nor that the size is equal, but could use get_symbol_constant_value/fold_ctor_reference gimple-fold APIs to actually extract just part of the initializer if we e.g. push just some subset (of course, still verify that it is a subset). For sizes which are power of two bytes and we have some integer modes, we could use as type for fold_ctor_reference corresponding integral types, otherwise dunno, punt or use some structure (e.g. try to find one in the initializer?), whatever. But even in the other spots it could perhaps handle loading of COMPONENT_REFs or MEM_REFs from the .rodata vars. 2024-04-03 Jakub Jelinek <[email protected]> PR middle-end/114552 * expr.cc (emit_push_insn): Only use store_constructor for immediate_const_ctor_p if int_expr_size matches size. * gcc.c-torture/execute/pr114552.c: New test.
1 parent e7b7188 commit 0303974

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

gcc/expr.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5466,16 +5466,19 @@ emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
54665466
/* If source is a constant VAR_DECL with a simple constructor,
54675467
store the constructor to the stack instead of moving it. */
54685468
const_tree decl;
5469+
HOST_WIDE_INT sz;
54695470
if (partial == 0
54705471
&& MEM_P (xinner)
54715472
&& SYMBOL_REF_P (XEXP (xinner, 0))
54725473
&& (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
54735474
&& VAR_P (decl)
54745475
&& TREE_READONLY (decl)
54755476
&& !TREE_SIDE_EFFECTS (decl)
5476-
&& immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5477-
store_constructor (DECL_INITIAL (decl), target, 0,
5478-
int_expr_size (DECL_INITIAL (decl)), false);
5477+
&& immediate_const_ctor_p (DECL_INITIAL (decl), 2)
5478+
&& (sz = int_expr_size (DECL_INITIAL (decl))) > 0
5479+
&& CONST_INT_P (size)
5480+
&& INTVAL (size) == sz)
5481+
store_constructor (DECL_INITIAL (decl), target, 0, sz, false);
54795482
else
54805483
emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
54815484
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* PR middle-end/114552 */
2+
3+
struct __attribute__((packed)) S { short b; int c; };
4+
struct T { struct S b; int e; };
5+
static const struct T k = { { 1, 0 }, 0 };
6+
7+
__attribute__((noinline)) void
8+
foo (void)
9+
{
10+
asm volatile ("" : : : "memory");
11+
}
12+
13+
__attribute__((noinline)) void
14+
bar (struct S n)
15+
{
16+
foo ();
17+
}
18+
19+
int
20+
main ()
21+
{
22+
bar (k.b);
23+
return 0;
24+
}

0 commit comments

Comments
 (0)