diff options
author | Roger Sayle <roger@nextmovesoftware.com> | 2022-06-04 12:21:51 +0100 |
---|---|---|
committer | Roger Sayle <roger@nextmovesoftware.com> | 2022-06-04 12:21:51 +0100 |
commit | ed6fd2aed58f2cca99f15331bf68999c0e6df370 (patch) | |
tree | f45d2ecac85e6636ee9ec5ce1ecbe7b3fc771e5c /gcc/expr.cc | |
parent | 53718316afa45eb0d1c236fbbf2fc0959b08510f (diff) | |
download | gcc-ed6fd2aed58f2cca99f15331bf68999c0e6df370.zip gcc-ed6fd2aed58f2cca99f15331bf68999c0e6df370.tar.gz gcc-ed6fd2aed58f2cca99f15331bf68999c0e6df370.tar.bz2 |
PR middle-end/95126: Expand small const structs as immediate constants.
This patch resolves PR middle-end/95126 which is a code quality regression,
by teaching the RTL expander to emit small const structs/unions as integer
immediate constants.
The motivating example from the bugzilla PR is:
struct small{ short a,b; signed char c; };
extern int func(struct small X);
void call_func(void)
{
static struct small const s = { 1, 2, 0 };
func(s);
}
which on x86_64 is currently compiled to:
call_func:
movzwl s.0+2(%rip), %eax
movzwl s.0(%rip), %edx
movzwl s.0+4(%rip), %edi
salq $16, %rax
orq %rdx, %rax
salq $32, %rdi
orq %rax, %rdi
jmp func
but with this patch is now optimized to:
call_func:
movl $131073, %edi
jmp func
2022-06-04 Roger Sayle <roger@nextmovesoftware.com>
gcc/ChangeLog
PR middle-end/95126
* calls.cc (load_register_parameters): When loading a suitable
immediate_const_ctor_p VAR_DECL into a single word_mode register,
construct it directly in a pseudo rather than read it (by parts)
from memory.
* expr.cc (int_expr_size): Make tree argument a const_tree.
(immediate_const_ctor_p): Helper predicate. Return true for
simple constructors that may be materialized in a register.
(expand_expr_real_1) [VAR_DECL]: When expanding a constant
VAR_DECL with a suitable immediate_const_ctor_p constructor
use store_constructor to materialize it directly in a pseudo.
* expr.h (immediate_const_ctor_p): Prototype here.
* varasm.cc (initializer_constant_valid_for_bitfield_p): Change
VALUE argument from tree to const_tree.
* varasm.h (initializer_constant_valid_for_bitfield_p): Update
prototype.
gcc/testsuite/ChangeLog
PR middle-end/95126
* gcc.target/i386/pr95126-m32-1.c: New test case.
* gcc.target/i386/pr95126-m32-2.c: New test case.
* gcc.target/i386/pr95126-m32-3.c: New test case.
* gcc.target/i386/pr95126-m32-4.c: New test case.
* gcc.target/i386/pr95126-m64-1.c: New test case.
* gcc.target/i386/pr95126-m64-2.c: New test case.
* gcc.target/i386/pr95126-m64-3.c: New test case.
* gcc.target/i386/pr95126-m64-4.c: New test case.
Diffstat (limited to 'gcc/expr.cc')
-rw-r--r-- | gcc/expr.cc | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/gcc/expr.cc b/gcc/expr.cc index 7197996..fb062dc 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -100,7 +100,7 @@ static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, profile_probability); static rtx const_vector_from_tree (tree); static tree tree_expr_size (const_tree); -static HOST_WIDE_INT int_expr_size (tree); +static HOST_WIDE_INT int_expr_size (const_tree); static void convert_mode_scalar (rtx, rtx, int); @@ -4867,7 +4867,22 @@ emit_push_insn (rtx x, machine_mode mode, tree type, rtx size, return false; } } - emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM); + + /* If source is a constant VAR_DECL with a simple constructor, + store the constructor to the stack instead of moving it. */ + const_tree decl; + if (partial == 0 + && MEM_P (xinner) + && SYMBOL_REF_P (XEXP (xinner, 0)) + && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE + && VAR_P (decl) + && TREE_READONLY (decl) + && !TREE_SIDE_EFFECTS (decl) + && immediate_const_ctor_p (DECL_INITIAL (decl), 2)) + store_constructor (DECL_INITIAL (decl), target, 0, + int_expr_size (DECL_INITIAL (decl)), false); + else + emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM); } } else if (partial > 0) @@ -6576,6 +6591,25 @@ categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts, p_init_elts, p_complete); } +/* Return true if constructor CTOR is simple enough to be materialized + in an integer mode register. Limit the size to WORDS words, which + is 1 by default. */ + +bool +immediate_const_ctor_p (const_tree ctor, unsigned int words) +{ + /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */ + if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR) + return false; + + return TREE_CONSTANT (ctor) + && !TREE_ADDRESSABLE (ctor) + && CONSTRUCTOR_NELTS (ctor) + && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE + && int_expr_size (ctor) <= words * UNITS_PER_WORD + && initializer_constant_valid_for_bitfield_p (ctor); +} + /* TYPE is initialized by a constructor with NUM_ELTS elements, the last of which had type LAST_TYPE. Each element was itself a complete initializer, in the sense that every meaningful byte was explicitly @@ -10567,6 +10601,21 @@ expand_expr_real_1 (tree exp, rtx target, machine_mode tmode, if (temp) return temp; } + /* Expand const VAR_DECLs with CONSTRUCTOR initializers that + have scalar integer modes to a reg via store_constructor. */ + if (TREE_READONLY (exp) + && !TREE_SIDE_EFFECTS (exp) + && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM) + && immediate_const_ctor_p (DECL_INITIAL (exp)) + && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp))) + && crtl->emit.regno_pointer_align_length + && !target) + { + target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); + store_constructor (DECL_INITIAL (exp), target, 0, + int_expr_size (DECL_INITIAL (exp)), false); + return target; + } /* ... fall through ... */ case PARM_DECL: @@ -13161,7 +13210,7 @@ expr_size (tree exp) if the size can vary or is larger than an integer. */ static HOST_WIDE_INT -int_expr_size (tree exp) +int_expr_size (const_tree exp) { tree size; |