Re: RFA: Small PATCH to add pow2p_hwi to hwint.h
On 09/08/2016 02:59 PM, Jason Merrill wrote: On Thu, Sep 8, 2016 at 11:55 AM, Joseph Myers wrote: > On Thu, 8 Sep 2016, Jason Merrill wrote: > >> Various places in GCC use negate, bit-and and compare to test whether >> an integer is a power of 2, but I think it would be clearer for this >> test to be wrapped in a function. > > (x & -x) == x is also true for 0. Whatever the correct function semantics > for 0, the comment needs to reflect them. Yep, I was just realizing that, too. This much larger patch introduces: least_bit_hwi: (x & -x) pow2_or_zerop: (x & -x) == x pow2p_hwi: x && x == (x & -x) ctz_or_zero: floor_log2 (x & -x) and replaces these patterns accordingly. I'm not at all attached to the names. Tested x86_64-pc-linux-gnu. hwint.diff commit 4b76501b72f3953ac57cb077b4e25a90afb6d9a9 Author: Jason Merrill Date: Thu Sep 8 13:10:12 2016 -0400 Add inline functions for various bitwise operations. * hwint.h (least_bit_hwi, pow2_or_zerop, pow2p_hwi, ctz_or_zero): New. * hwint.c (exact_log2): Use pow2p_hwi. (ctz_hwi, ffs_hwi): Use least_bit_hwi. * alias.c (memrefs_conflict_p): Use pow2_or_zerop. * builtins.c (get_object_alignment_2, get_object_alignment) (get_pointer_alignment, fold_builtin_atomic_always_lock_free): Use least_bit_hwi. * calls.c (compute_argument_addresses, store_one_arg): Use least_bit_hwi. * cfgexpand.c (expand_one_stack_var_at): Use least_bit_hwi. * combine.c (force_to_mode): Use least_bit_hwi. * emit-rtl.c (set_mem_attributes_minus_bitpos, adjust_address_1): Use least_bit_hwi. * expmed.c (synth_mult, expand_divmod): Use ctz_or_zero, ctz_hwi. (init_expmed_one_conv): Use pow2p_hwi. * fold-const.c (round_up_loc, round_down_loc): Use pow2_or_zerop. (fold_binary_loc): Use pow2p_hwi. * function.c (assign_parm_find_stack_rtl): Use least_bit_hwi. * gimple-fold.c (gimple_fold_builtin_memory_op): Use pow2p_hwi. * gimple-ssa-strength-reduction.c (replace_ref): Use least_bit_hwi. * hsa-gen.c (gen_hsa_addr_with_align, hsa_bitmemref_alignment): Use least_bit_hwi. * ipa-cp.c (ipcp_alignment_lattice::meet_with_1): Use least_bit_hwi. * ipa-prop.c (ipa_modify_call_arguments): Use least_bit_hwi. * omp-low.c (oacc_loop_fixed_partitions) (oacc_loop_auto_partitions): Use least_bit_hwi. * rtlanal.c (nonzero_bits1): Use ctz_or_zero. * stor-layout.c (place_field): Use least_bit_hwi. * tree-pretty-print.c (dump_generic_node): Use pow2p_hwi. * tree-sra.c (build_ref_for_offset): Use least_bit_hwi. * tree-ssa-ccp.c (ccp_finalize): Use least_bit_hwi. * tree-ssa-math-opts.c (bswap_replace): Use least_bit_hwi. * tree-ssa-strlen.c (handle_builtin_memcmp): Use pow2p_hwi. * tree-vect-data-refs.c (vect_analyze_group_access_1) (vect_grouped_store_supported, vect_grouped_load_supported) (vect_permute_load_chain, vect_shift_permute_load_chain) (vect_transform_grouped_load): Use pow2p_hwi. * tree-vect-generic.c (expand_vector_divmod): Use ctz_or_zero. * tree-vect-patterns.c (vect_recog_divmod_pattern): Use ctz_or_zero. * tree-vect-stmts.c (vectorizable_mask_load_store): Use least_bit_hwi. * tsan.c (instrument_expr): Use least_bit_hwi. * var-tracking.c (negative_power_of_two_p): Use pow2_or_zerop. I was briefly worried about some of the expmed changes, but managed to convince myself they were correct. Ok for the trunk. Thanks, jeff
Re: RFA: Small PATCH to add pow2p_hwi to hwint.h
On Thu, Sep 8, 2016 at 11:55 AM, Joseph Myers wrote: > On Thu, 8 Sep 2016, Jason Merrill wrote: > >> Various places in GCC use negate, bit-and and compare to test whether >> an integer is a power of 2, but I think it would be clearer for this >> test to be wrapped in a function. > > (x & -x) == x is also true for 0. Whatever the correct function semantics > for 0, the comment needs to reflect them. Yep, I was just realizing that, too. This much larger patch introduces: least_bit_hwi: (x & -x) pow2_or_zerop: (x & -x) == x pow2p_hwi: x && x == (x & -x) ctz_or_zero: floor_log2 (x & -x) and replaces these patterns accordingly. I'm not at all attached to the names. Tested x86_64-pc-linux-gnu. commit 4b76501b72f3953ac57cb077b4e25a90afb6d9a9 Author: Jason Merrill Date: Thu Sep 8 13:10:12 2016 -0400 Add inline functions for various bitwise operations. * hwint.h (least_bit_hwi, pow2_or_zerop, pow2p_hwi, ctz_or_zero): New. * hwint.c (exact_log2): Use pow2p_hwi. (ctz_hwi, ffs_hwi): Use least_bit_hwi. * alias.c (memrefs_conflict_p): Use pow2_or_zerop. * builtins.c (get_object_alignment_2, get_object_alignment) (get_pointer_alignment, fold_builtin_atomic_always_lock_free): Use least_bit_hwi. * calls.c (compute_argument_addresses, store_one_arg): Use least_bit_hwi. * cfgexpand.c (expand_one_stack_var_at): Use least_bit_hwi. * combine.c (force_to_mode): Use least_bit_hwi. * emit-rtl.c (set_mem_attributes_minus_bitpos, adjust_address_1): Use least_bit_hwi. * expmed.c (synth_mult, expand_divmod): Use ctz_or_zero, ctz_hwi. (init_expmed_one_conv): Use pow2p_hwi. * fold-const.c (round_up_loc, round_down_loc): Use pow2_or_zerop. (fold_binary_loc): Use pow2p_hwi. * function.c (assign_parm_find_stack_rtl): Use least_bit_hwi. * gimple-fold.c (gimple_fold_builtin_memory_op): Use pow2p_hwi. * gimple-ssa-strength-reduction.c (replace_ref): Use least_bit_hwi. * hsa-gen.c (gen_hsa_addr_with_align, hsa_bitmemref_alignment): Use least_bit_hwi. * ipa-cp.c (ipcp_alignment_lattice::meet_with_1): Use least_bit_hwi. * ipa-prop.c (ipa_modify_call_arguments): Use least_bit_hwi. * omp-low.c (oacc_loop_fixed_partitions) (oacc_loop_auto_partitions): Use least_bit_hwi. * rtlanal.c (nonzero_bits1): Use ctz_or_zero. * stor-layout.c (place_field): Use least_bit_hwi. * tree-pretty-print.c (dump_generic_node): Use pow2p_hwi. * tree-sra.c (build_ref_for_offset): Use least_bit_hwi. * tree-ssa-ccp.c (ccp_finalize): Use least_bit_hwi. * tree-ssa-math-opts.c (bswap_replace): Use least_bit_hwi. * tree-ssa-strlen.c (handle_builtin_memcmp): Use pow2p_hwi. * tree-vect-data-refs.c (vect_analyze_group_access_1) (vect_grouped_store_supported, vect_grouped_load_supported) (vect_permute_load_chain, vect_shift_permute_load_chain) (vect_transform_grouped_load): Use pow2p_hwi. * tree-vect-generic.c (expand_vector_divmod): Use ctz_or_zero. * tree-vect-patterns.c (vect_recog_divmod_pattern): Use ctz_or_zero. * tree-vect-stmts.c (vectorizable_mask_load_store): Use least_bit_hwi. * tsan.c (instrument_expr): Use least_bit_hwi. * var-tracking.c (negative_power_of_two_p): Use pow2_or_zerop. diff --git a/gcc/alias.c b/gcc/alias.c index f4b5a92..277125e 100644 --- a/gcc/alias.c +++ b/gcc/alias.c @@ -2534,7 +2534,7 @@ memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c) { HOST_WIDE_INT sc = INTVAL (XEXP (x, 1)); unsigned HOST_WIDE_INT uc = sc; - if (sc < 0 && -uc == (uc & -uc)) + if (sc < 0 && pow2_or_zerop (-uc)) { if (xsize > 0) xsize = -xsize; @@ -2549,7 +2549,7 @@ memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c) { HOST_WIDE_INT sc = INTVAL (XEXP (y, 1)); unsigned HOST_WIDE_INT uc = sc; - if (sc < 0 && -uc == (uc & -uc)) + if (sc < 0 && pow2_or_zerop (-uc)) { if (ysize > 0) ysize = -ysize; diff --git a/gcc/builtins.c b/gcc/builtins.c index 1073e35..0ccba15 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -305,7 +305,7 @@ get_object_alignment_2 (tree exp, unsigned int *alignp, { ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)); ptr_bitmask *= BITS_PER_UNIT; - align = ptr_bitmask & -ptr_bitmask; + align = least_bit_hwi (ptr_bitmask); addr = TREE_OPERAND (addr, 0); } @@ -325,7 +325,7 @@ get_object_alignment_2 (tree exp, unsigned int *alignp, unsigned HOST_WIDE_INT step = 1;
Re: RFA: Small PATCH to add pow2p_hwi to hwint.h
On Thu, 8 Sep 2016, Jason Merrill wrote: > Various places in GCC use negate, bit-and and compare to test whether > an integer is a power of 2, but I think it would be clearer for this > test to be wrapped in a function. (x & -x) == x is also true for 0. Whatever the correct function semantics for 0, the comment needs to reflect them. -- Joseph S. Myers jos...@codesourcery.com
Re: RFA: Small PATCH to add pow2p_hwi to hwint.h
On Thu, Sep 08, 2016 at 08:53:26AM -0600, Jeff Law wrote: > On 09/07/2016 11:56 PM, Jason Merrill wrote: > > Various places in GCC use negate, bit-and and compare to test whether > > an integer is a power of 2, but I think it would be clearer for this > > test to be wrapped in a function. > > > > OK for trunk? > > > I think the canonical way we've written that is > > exact_log2 (x) != -1 We have integer_pow2p which works on trees (and is actually implemented by testing if popcnt is 1 on the wide-int). Calling exact_log2 for such a test if you don't need the value is a waste of compile time, ctz_hwi doesn't need to be computed. And, exact_log2 is -1 even on 0, which is not pow2p_hwi. I think having pow2p_hwi would be nice, if we really start using it where appropriate. Jakub
Re: RFA: Small PATCH to add pow2p_hwi to hwint.h
On 09/07/2016 11:56 PM, Jason Merrill wrote: Various places in GCC use negate, bit-and and compare to test whether an integer is a power of 2, but I think it would be clearer for this test to be wrapped in a function. OK for trunk? I think the canonical way we've written that is exact_log2 (x) != -1 Though you could argue that's far from obvious. Presumably you have a follow-up to start using pow2p_hwi? jeff