https://gcc.gnu.org/g:bf6efbbad14e46f97bcc36c531000d8d4740c863
commit r15-6086-gbf6efbbad14e46f97bcc36c531000d8d4740c863 Author: Wilco Dijkstra <wilco.dijks...@arm.com> Date: Tue Oct 1 16:51:14 2024 +0000 AArch64: Cleanup alignment macros Change the AARCH64_EXPAND_ALIGNMENT macro into proper function calls to make future changes easier. Use the existing alignment settings, however avoid overaligning small array's or structs to 64 bits when there is no benefit. The lower alignment gives a small reduction in data and stack size. Using 32-bit alignment for small char arrays still improves performance of string functions since it can be loaded in full by the first 8/16-byte load. gcc: * config/aarch64/aarch64.h (AARCH64_EXPAND_ALIGNMENT): Remove. (DATA_ALIGNMENT): Use aarch64_data_alignment. (LOCAL_ALIGNMENT): Use aarch64_stack_alignment. * config/aarch64/aarch64.cc (aarch64_data_alignment): New function. (aarch64_stack_alignment): Likewise. * config/aarch64/aarch64-protos.h (aarch64_data_alignment): New prototype. (aarch64_stack_alignment): Likewise. Diff: --- gcc/config/aarch64/aarch64-protos.h | 3 +++ gcc/config/aarch64/aarch64.cc | 54 +++++++++++++++++++++++++++++++++++++ gcc/config/aarch64/aarch64.h | 23 ++++------------ 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h index 51951ae80023..db2baca58665 100644 --- a/gcc/config/aarch64/aarch64-protos.h +++ b/gcc/config/aarch64/aarch64-protos.h @@ -1236,4 +1236,7 @@ void aarch64_expand_reversed_crc_using_pmull (scalar_mode, scalar_mode, rtx *); extern bool aarch64_gcs_enabled (); +extern unsigned aarch64_data_alignment (const_tree exp, unsigned align); +extern unsigned aarch64_stack_alignment (const_tree exp, unsigned align); + #endif /* GCC_AARCH64_PROTOS_H */ diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index 29b034a25c0e..3606dc174c2f 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -2678,6 +2678,60 @@ aarch64_constant_alignment (const_tree exp, HOST_WIDE_INT align) return align; } +/* Align definitions of arrays, unions and structures so that + initializations and copies can be made more efficient. This is not + ABI-changing, so it only affects places where we can see the + definition. Increasing the alignment tends to introduce padding, + so don't do this when optimizing for size/conserving stack space. */ + +unsigned +aarch64_data_alignment (const_tree type, unsigned align) +{ + if (optimize_size) + return align; + + if (AGGREGATE_TYPE_P (type)) + { + unsigned HOST_WIDE_INT size = 0; + + if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST + && tree_fits_uhwi_p (TYPE_SIZE (type))) + size = tree_to_uhwi (TYPE_SIZE (type)); + + /* Align small structs/arrays to 32 bits, or 64 bits if larger. */ + if (align < 32 && size <= 32) + align = 32; + else if (align < 64) + align = 64; + } + + return align; +} + +unsigned +aarch64_stack_alignment (const_tree type, unsigned align) +{ + if (flag_conserve_stack) + return align; + + if (AGGREGATE_TYPE_P (type)) + { + unsigned HOST_WIDE_INT size = 0; + + if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST + && tree_fits_uhwi_p (TYPE_SIZE (type))) + size = tree_to_uhwi (TYPE_SIZE (type)); + + /* Align small structs/arrays to 32 bits, or 64 bits if larger. */ + if (align < 32 && size <= 32) + align = 32; + else if (align < 64) + align = 64; + } + + return align; +} + /* Return true if calls to DECL should be treated as long-calls (ie called via a register). */ static bool diff --git a/gcc/config/aarch64/aarch64.h b/gcc/config/aarch64/aarch64.h index b1c694e143f2..f1251f67c74e 100644 --- a/gcc/config/aarch64/aarch64.h +++ b/gcc/config/aarch64/aarch64.h @@ -121,24 +121,11 @@ of LSE instructions. */ #define TARGET_OUTLINE_ATOMICS (aarch64_flag_outline_atomics) -/* Align definitions of arrays, unions and structures so that - initializations and copies can be made more efficient. This is not - ABI-changing, so it only affects places where we can see the - definition. Increasing the alignment tends to introduce padding, - so don't do this when optimizing for size/conserving stack space. */ -#define AARCH64_EXPAND_ALIGNMENT(COND, EXP, ALIGN) \ - (((COND) && ((ALIGN) < BITS_PER_WORD) \ - && (TREE_CODE (EXP) == ARRAY_TYPE \ - || TREE_CODE (EXP) == UNION_TYPE \ - || TREE_CODE (EXP) == RECORD_TYPE)) ? BITS_PER_WORD : (ALIGN)) - -/* Align global data. */ -#define DATA_ALIGNMENT(EXP, ALIGN) \ - AARCH64_EXPAND_ALIGNMENT (!optimize_size, EXP, ALIGN) - -/* Similarly, make sure that objects on the stack are sensibly aligned. */ -#define LOCAL_ALIGNMENT(EXP, ALIGN) \ - AARCH64_EXPAND_ALIGNMENT (!flag_conserve_stack, EXP, ALIGN) +/* Align global data as an optimization. */ +#define DATA_ALIGNMENT(EXP, ALIGN) aarch64_data_alignment (EXP, ALIGN) + +/* Align stack data as an optimization. */ +#define LOCAL_ALIGNMENT(EXP, ALIGN) aarch64_stack_alignment (EXP, ALIGN) #define STRUCTURE_SIZE_BOUNDARY 8