diff options
author | Richard Sandiford <richard.sandiford@arm.com> | 2015-12-03 14:31:55 +0000 |
---|---|---|
committer | Richard Sandiford <rsandifo@gcc.gnu.org> | 2015-12-03 14:31:55 +0000 |
commit | ee62a5a60299fdc12c884337dd36767fd2230ac0 (patch) | |
tree | cb751abc1ebee76113ef8fcecacea64963774dfb /gcc/config | |
parent | 47e16c6a6aed01dde2774a5490a3e5c7f97e9f92 (diff) | |
download | gcc-ee62a5a60299fdc12c884337dd36767fd2230ac0.zip gcc-ee62a5a60299fdc12c884337dd36767fd2230ac0.tar.gz gcc-ee62a5a60299fdc12c884337dd36767fd2230ac0.tar.bz2 |
Add an rsqrt_optab and IFN_RSQRT internal function
All current uses of builtin_reciprocal convert 1.0/sqrt into rsqrt.
This patch adds an rsqrt optab and associated internal function for
that instead. We can then pick up the vector forms of rsqrt automatically,
fixing an AArch64 regression from my internal_fn patches.
With that change, builtin_reciprocal only needs to handle target-specific
built-in functions. I've restricted the hook to those since, if we need
a reciprocal of another standard function later, I think there should be
a strong preference for adding a new optab and internal function for it,
rather than hiding the code in a backend.
Three targets implement builtin_reciprocal: aarch64, i386 and rs6000.
i386 and rs6000 already used the obvious rsqrt<mode>2 pattern names
for the instructions, so they pick up the new code automatically.
aarch64 needs a slight rename.
mn10300 is unusual in that its native operation is rsqrt, and
sqrt is approximated as 1.0/rsqrt. The port also uses rsqrt<mode>2
for the rsqrt pattern, so after the patch we now pick it up as a native
operation.
Two other ports define rsqrt patterns: sh and v850. AFAICT these
patterns aren't currently used, but I think the patch does what the
authors of the patterns would have expected. There's obviously some
risk of fallout though.
Tested on x86_64-linux-gnu, aarch64-linux-gnu, arm-linux-gnueabihf
(as a target without the hooks) and powerpc64-linux-gnu.
gcc/
* internal-fn.def (RSQRT): New function.
* optabs.def (rsqrt_optab): New optab.
* doc/md.texi (rsqrtM2): Document.
* target.def (builtin_reciprocal): Replace gcall argument with
a function decl. Restrict hook to machine functions.
* doc/tm.texi: Regenerate.
* targhooks.h (default_builtin_reciprocal): Update prototype.
* targhooks.c (default_builtin_reciprocal): Likewise.
* tree-ssa-math-opts.c: Include internal-fn.h.
(internal_fn_reciprocal): New function.
(pass_cse_reciprocals::execute): Call it, and build a call to an
internal function on success. Only call targetm.builtin_reciprocal
for machine functions.
* config/aarch64/aarch64-protos.h (aarch64_builtin_rsqrt): Remove
second argument.
* config/aarch64/aarch64-builtins.c (aarch64_expand_builtin_rsqrt):
Rename aarch64_rsqrt_<mode>2 to rsqrt<mode>2.
(aarch64_builtin_rsqrt): Remove md_fn argument and only handle
machine functions.
* config/aarch64/aarch64.c (use_rsqrt_p): New function.
(aarch64_builtin_reciprocal): Replace gcall argument with a
function decl. Use use_rsqrt_p. Remove optimize_size check.
Only handle machine functions. Update call to aarch64_builtin_rsqrt.
(aarch64_optab_supported_p): New function.
(TARGET_OPTAB_SUPPORTED_P): Define.
* config/aarch64/aarch64-simd.md (aarch64_rsqrt_<mode>2): Rename to...
(rsqrt<mode>2): ...this.
* config/i386/i386.c (use_rsqrt_p): New function.
(ix86_builtin_reciprocal): Replace gcall argument with a
function decl. Use use_rsqrt_p. Remove optimize_insn_for_size_p
check. Only handle machine functions.
(ix86_optab_supported_p): Handle rsqrt_optab.
* config/rs6000/rs6000.c (TARGET_OPTAB_SUPPORTED_P): Define.
(rs6000_builtin_reciprocal): Replace gcall argument with a
function decl. Remove optimize_insn_for_size_p check.
Only handle machine functions.
(rs6000_optab_supported_p): New function.
From-SVN: r231229
Diffstat (limited to 'gcc/config')
-rw-r--r-- | gcc/config/aarch64/aarch64-builtins.c | 34 | ||||
-rw-r--r-- | gcc/config/aarch64/aarch64-protos.h | 2 | ||||
-rw-r--r-- | gcc/config/aarch64/aarch64-simd.md | 2 | ||||
-rw-r--r-- | gcc/config/aarch64/aarch64.c | 49 | ||||
-rw-r--r-- | gcc/config/i386/i386.c | 79 | ||||
-rw-r--r-- | gcc/config/rs6000/rs6000.c | 95 |
6 files changed, 106 insertions, 155 deletions
diff --git a/gcc/config/aarch64/aarch64-builtins.c b/gcc/config/aarch64/aarch64-builtins.c index 45011f6..b268a6a 100644 --- a/gcc/config/aarch64/aarch64-builtins.c +++ b/gcc/config/aarch64/aarch64-builtins.c @@ -1176,19 +1176,19 @@ aarch64_expand_builtin_rsqrt (int fcode, tree exp, rtx target) switch (fcode) { case AARCH64_BUILTIN_RSQRT_DF: - gen = gen_aarch64_rsqrt_df2; + gen = gen_rsqrtdf2; break; case AARCH64_BUILTIN_RSQRT_SF: - gen = gen_aarch64_rsqrt_sf2; + gen = gen_rsqrtsf2; break; case AARCH64_BUILTIN_RSQRT_V2DF: - gen = gen_aarch64_rsqrt_v2df2; + gen = gen_rsqrtv2df2; break; case AARCH64_BUILTIN_RSQRT_V2SF: - gen = gen_aarch64_rsqrt_v2sf2; + gen = gen_rsqrtv2sf2; break; case AARCH64_BUILTIN_RSQRT_V4SF: - gen = gen_aarch64_rsqrt_v4sf2; + gen = gen_rsqrtv4sf2; break; default: gcc_unreachable (); } @@ -1405,24 +1405,14 @@ aarch64_builtin_vectorized_function (unsigned int fn, tree type_out, /* Return builtin for reciprocal square root. */ tree -aarch64_builtin_rsqrt (unsigned int fn, bool md_fn) +aarch64_builtin_rsqrt (unsigned int fn) { - if (md_fn) - { - if (fn == AARCH64_SIMD_BUILTIN_UNOP_sqrtv2df) - return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_V2DF]; - if (fn == AARCH64_SIMD_BUILTIN_UNOP_sqrtv2sf) - return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_V2SF]; - if (fn == AARCH64_SIMD_BUILTIN_UNOP_sqrtv4sf) - return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_V4SF]; - } - else - { - if (fn == BUILT_IN_SQRT) - return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_DF]; - if (fn == BUILT_IN_SQRTF) - return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_SF]; - } + if (fn == AARCH64_SIMD_BUILTIN_UNOP_sqrtv2df) + return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_V2DF]; + if (fn == AARCH64_SIMD_BUILTIN_UNOP_sqrtv2sf) + return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_V2SF]; + if (fn == AARCH64_SIMD_BUILTIN_UNOP_sqrtv4sf) + return aarch64_builtin_decls[AARCH64_BUILTIN_RSQRT_V4SF]; return NULL_TREE; } diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h index e0a050c..e6bfe06 100644 --- a/gcc/config/aarch64/aarch64-protos.h +++ b/gcc/config/aarch64/aarch64-protos.h @@ -407,7 +407,7 @@ rtx aarch64_expand_builtin (tree exp, int ignore ATTRIBUTE_UNUSED); tree aarch64_builtin_decl (unsigned, bool ATTRIBUTE_UNUSED); -tree aarch64_builtin_rsqrt (unsigned int, bool); +tree aarch64_builtin_rsqrt (unsigned int); tree aarch64_builtin_vectorized_function (unsigned int, tree, tree); diff --git a/gcc/config/aarch64/aarch64-simd.md b/gcc/config/aarch64/aarch64-simd.md index ae1075c..030a101 100644 --- a/gcc/config/aarch64/aarch64-simd.md +++ b/gcc/config/aarch64/aarch64-simd.md @@ -399,7 +399,7 @@ "frsqrts\\t%<v>0<Vmtype>, %<v>1<Vmtype>, %<v>2<Vmtype>" [(set_attr "type" "neon_fp_rsqrts_<Vetype><q>")]) -(define_expand "aarch64_rsqrt_<mode>2" +(define_expand "rsqrt<mode>2" [(set (match_operand:VALLF 0 "register_operand" "=w") (unspec:VALLF [(match_operand:VALLF 1 "register_operand" "w")] UNSPEC_RSQRT))] diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c index 88dbe15..88f3ef8 100644 --- a/gcc/config/aarch64/aarch64.c +++ b/gcc/config/aarch64/aarch64.c @@ -7099,26 +7099,27 @@ aarch64_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED, return aarch64_tune_params.memmov_cost; } +/* Return true if it is safe and beneficial to use the rsqrt optabs to + optimize 1.0/sqrt. */ + +static bool +use_rsqrt_p (void) +{ + return (!flag_trapping_math + && flag_unsafe_math_optimizations + && (aarch64_tune_params.extra_tuning_flags + & AARCH64_EXTRA_TUNE_RECIP_SQRT)); +} + /* Function to decide when to use reciprocal square root builtins. */ static tree -aarch64_builtin_reciprocal (gcall *call) +aarch64_builtin_reciprocal (tree fndecl) { - if (flag_trapping_math - || !flag_unsafe_math_optimizations - || optimize_size - || ! (aarch64_tune_params.extra_tuning_flags - & AARCH64_EXTRA_TUNE_RECIP_SQRT)) + if (!use_rsqrt_p ()) return NULL_TREE; - - if (gimple_call_internal_p (call)) - return NULL_TREE; - - tree fndecl = gimple_call_fndecl (call); - enum built_in_function fn = DECL_FUNCTION_CODE (fndecl); - bool md_fn = DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD; - return aarch64_builtin_rsqrt (fn, md_fn); + return aarch64_builtin_rsqrt (DECL_FUNCTION_CODE (fndecl)); } typedef rtx (*rsqrte_type) (rtx, rtx); @@ -13546,6 +13547,23 @@ aarch64_promoted_type (const_tree t) return float_type_node; return NULL_TREE; } + +/* Implement the TARGET_OPTAB_SUPPORTED_P hook. */ + +static bool +aarch64_optab_supported_p (int op, machine_mode, machine_mode, + optimization_type opt_type) +{ + switch (op) + { + case rsqrt_optab: + return opt_type == OPTIMIZE_FOR_SPEED && use_rsqrt_p (); + + default: + return true; + } +} + #undef TARGET_ADDRESS_COST #define TARGET_ADDRESS_COST aarch64_address_cost @@ -13866,6 +13884,9 @@ aarch64_promoted_type (const_tree t) #undef TARGET_PRINT_OPERAND_ADDRESS #define TARGET_PRINT_OPERAND_ADDRESS aarch64_print_operand_address +#undef TARGET_OPTAB_SUPPORTED_P +#define TARGET_OPTAB_SUPPORTED_P aarch64_optab_supported_p + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-aarch64.h" diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 96d6c98..cd44375 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -42676,66 +42676,36 @@ ix86_vectorize_builtin_scatter (const_tree vectype, return ix86_builtins[code]; } +/* Return true if it is safe to use the rsqrt optabs to optimize + 1.0/sqrt. */ + +static bool +use_rsqrt_p () +{ + return (TARGET_SSE_MATH + && flag_finite_math_only + && !flag_trapping_math + && flag_unsafe_math_optimizations); +} + /* Returns a code for a target-specific builtin that implements reciprocal of the function, or NULL_TREE if not available. */ static tree -ix86_builtin_reciprocal (gcall *call) +ix86_builtin_reciprocal (tree fndecl) { - if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p () - && flag_finite_math_only && !flag_trapping_math - && flag_unsafe_math_optimizations)) - return NULL_TREE; - - if (gimple_call_internal_p (call)) - switch (gimple_call_internal_fn (call)) - { - tree type; - case IFN_SQRT: - type = TREE_TYPE (gimple_call_lhs (call)); - switch (TYPE_MODE (type)) - { - /* Vectorized version of sqrt to rsqrt conversion. */ - case V4SFmode: - return ix86_get_builtin (IX86_BUILTIN_RSQRTPS_NR); - - case V8SFmode: - return ix86_get_builtin (IX86_BUILTIN_RSQRTPS_NR256); - - default: - return NULL_TREE; - } - - default: - return NULL_TREE; - } - - tree fndecl = gimple_call_fndecl (call); - if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) - /* Machine dependent builtins. */ - switch (DECL_FUNCTION_CODE (fndecl)) - { - /* Vectorized version of sqrt to rsqrt conversion. */ - case IX86_BUILTIN_SQRTPS_NR: - return ix86_get_builtin (IX86_BUILTIN_RSQRTPS_NR); + switch (DECL_FUNCTION_CODE (fndecl)) + { + /* Vectorized version of sqrt to rsqrt conversion. */ + case IX86_BUILTIN_SQRTPS_NR: + return ix86_get_builtin (IX86_BUILTIN_RSQRTPS_NR); - case IX86_BUILTIN_SQRTPS_NR256: - return ix86_get_builtin (IX86_BUILTIN_RSQRTPS_NR256); + case IX86_BUILTIN_SQRTPS_NR256: + return ix86_get_builtin (IX86_BUILTIN_RSQRTPS_NR256); - default: - return NULL_TREE; - } - else - /* Normal builtins. */ - switch (DECL_FUNCTION_CODE (fndecl)) - { - /* Sqrt to rsqrt conversion. */ - case BUILT_IN_SQRTF: - return ix86_get_builtin (IX86_BUILTIN_RSQRTF); - - default: - return NULL_TREE; - } + default: + return NULL_TREE; + } } /* Helper for avx_vpermilps256_operand et al. This is also used by @@ -54139,6 +54109,9 @@ ix86_optab_supported_p (int op, machine_mode mode1, machine_mode, return true; return opt_type == OPTIMIZE_FOR_SPEED; + case rsqrt_optab: + return opt_type == OPTIMIZE_FOR_SPEED && use_rsqrt_p (); + default: return true; } diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c index 22eb0e5..2b2c170 100644 --- a/gcc/config/rs6000/rs6000.c +++ b/gcc/config/rs6000/rs6000.c @@ -1722,6 +1722,9 @@ static const struct attribute_spec rs6000_attribute_table[] = #undef TARGET_INVALID_BINARY_OP #define TARGET_INVALID_BINARY_OP rs6000_invalid_binary_op + +#undef TARGET_OPTAB_SUPPORTED_P +#define TARGET_OPTAB_SUPPORTED_P rs6000_optab_supported_p /* Processor table. */ @@ -32713,77 +32716,25 @@ rs6000_memory_move_cost (machine_mode mode, reg_class_t rclass, reciprocal of the function, or NULL_TREE if not available. */ static tree -rs6000_builtin_reciprocal (gcall *call) +rs6000_builtin_reciprocal (tree fndecl) { - if (optimize_insn_for_size_p ()) - return NULL_TREE; - - if (gimple_call_internal_p (call)) - switch (gimple_call_internal_fn (call)) - { - tree type; - case IFN_SQRT: - type = TREE_TYPE (gimple_call_lhs (call)); - switch (TYPE_MODE (type)) - { - case V2DFmode: - if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode)) - return NULL_TREE; - - return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF]; - - case V4SFmode: - if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode)) - return NULL_TREE; - - return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF]; - - default: - return NULL_TREE; - } - - default: + switch (DECL_FUNCTION_CODE (fndecl)) + { + case VSX_BUILTIN_XVSQRTDP: + if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode)) return NULL_TREE; - } - - tree fndecl = gimple_call_fndecl (call); - if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD) - switch (DECL_FUNCTION_CODE (fndecl)) - { - case VSX_BUILTIN_XVSQRTDP: - if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode)) - return NULL_TREE; - - return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF]; - case VSX_BUILTIN_XVSQRTSP: - if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode)) - return NULL_TREE; + return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF]; - return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF]; - - default: + case VSX_BUILTIN_XVSQRTSP: + if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode)) return NULL_TREE; - } - - else - switch (DECL_FUNCTION_CODE (fndecl)) - { - case BUILT_IN_SQRT: - if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode)) - return NULL_TREE; - - return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT]; - - case BUILT_IN_SQRTF: - if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode)) - return NULL_TREE; - return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF]; + return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF]; - default: - return NULL_TREE; - } + default: + return NULL_TREE; + } } /* Load up a constant. If the mode is a vector mode, splat the value across @@ -38007,6 +37958,22 @@ rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update) *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf); } +/* Implement the TARGET_OPTAB_SUPPORTED_P hook. */ + +static bool +rs6000_optab_supported_p (int op, machine_mode mode1, machine_mode, + optimization_type opt_type) +{ + switch (op) + { + case rsqrt_optab: + return (opt_type == OPTIMIZE_FOR_SPEED + && RS6000_RECIP_AUTO_RSQRTE_P (mode1)); + + default: + return true; + } +} struct gcc_target targetm = TARGET_INITIALIZER; |