gcc/gcc/multiple_target.c

561 lines
15 KiB
C
Raw Normal View History

/* Pass for parsing functions with multiple target attributes.
Contributed by Evgeny Stupachenko <evstupac@gmail.com>
Copyright (C) 2015-2019 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "stringpool.h"
#include "gimple.h"
#include "diagnostic-core.h"
#include "gimple-ssa.h"
#include "cgraph.h"
#include "tree-pass.h"
#include "target.h"
#include "attribs.h"
#include "pretty-print.h"
#include "gimple-iterator.h"
#include "gimple-walk.h"
#include "tree-inline.h"
#include "intl.h"
/* Walker callback that replaces all FUNCTION_DECL of a function that's
going to be versioned. */
static tree
replace_function_decl (tree *op, int *walk_subtrees, void *data)
{
struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
if (TREE_CODE (*op) == FUNCTION_DECL
&& info->this_node->decl == *op)
{
*op = info->dispatcher_resolver;
*walk_subtrees = 0;
}
return NULL;
}
/* If the call in NODE has multiple target attribute with multiple fields,
replace it with dispatcher call and create dispatcher (once). */
static void
create_dispatcher_calls (struct cgraph_node *node)
{
ipa_ref *ref;
if (!DECL_FUNCTION_VERSIONED (node->decl)
|| !is_function_default_version (node->decl))
return;
if (!targetm.has_ifunc_p ())
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"the call requires ifunc, which is not"
" supported by this target");
return;
}
else if (!targetm.get_function_versions_dispatcher)
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"target does not support function version dispatcher");
return;
}
tree idecl = targetm.get_function_versions_dispatcher (node->decl);
if (!idecl)
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"default %<target_clones%> attribute was not set");
return;
}
cgraph_node *inode = cgraph_node::get (idecl);
gcc_assert (inode);
tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
/* Update aliases. */
inode->alias = true;
inode->alias_target = resolver_decl;
if (!inode->analyzed)
inode->resolve_alias (cgraph_node::get (resolver_decl));
auto_vec<cgraph_edge *> edges_to_redirect;
auto_vec<ipa_ref *> references_to_redirect;
for (unsigned i = 0; node->iterate_referring (i, ref); i++)
references_to_redirect.safe_push (ref);
/* We need to remember NEXT_CALLER as it could be modified in the loop. */
for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
edges_to_redirect.safe_push (e);
if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
{
/* Redirect edges. */
unsigned i;
cgraph_edge *e;
FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
{
e->redirect_callee (inode);
e->redirect_call_stmt_to_callee ();
}
/* Redirect references. */
FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
{
if (ref->use == IPA_REF_ADDR)
{
struct walk_stmt_info wi;
memset (&wi, 0, sizeof (wi));
wi.info = (void *)node->function_version ();
if (dyn_cast<varpool_node *> (ref->referring))
{
hash_set<tree> visited_nodes;
walk_tree (&DECL_INITIAL (ref->referring->decl),
replace_function_decl, &wi, &visited_nodes);
}
else
{
gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
if (ref->referring->decl != resolver_decl)
walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
}
symtab_node *source = ref->referring;
ref->remove_reference ();
source->create_reference (inode, IPA_REF_ADDR);
}
else if (ref->use == IPA_REF_ALIAS)
{
symtab_node *source = ref->referring;
ref->remove_reference ();
source->create_reference (inode, IPA_REF_ALIAS);
source->add_to_same_comdat_group (inode);
}
else
gcc_unreachable ();
}
}
symtab->change_decl_assembler_name (node->decl,
Avoid unnecessarily numbering cloned symbols. 2018-10-30 Michael Ploujnikov <michael.ploujnikov@oracle.com> * cgraph.h (clone_function_name_1): Replaced by new clone_function_name_numbered that takes name as string; for privatize_symbol_name_1 use only. (clone_function_name): Renamed to clone_function_name_numbered to be explicit about numbering. (clone_function_name): New two-argument function that does not number its output. (clone_function_name): New three-argument function that takes a number to append to its output. * cgraphclones.c (duplicate_thunk_for_node): (clone_function_name_1): Renamed. (clone_function_name_numbered): Two new functions. (clone_function_name): Improved documentation. (cgraph_node::create_virtual_clone): Use clone_function_name_numbered. * config/rs6000/rs6000.c (make_resolver_func): Ditto. * final.c (final_scan_insn_1): Use the new clone_function_name without numbering. * multiple_target.c (create_dispatcher_calls): Ditto. (create_target_clone): Ditto. * omp-expand.c (grid_expand_target_grid_body): Ditto. * omp-low.c (create_omp_child_function_name): Ditto. * omp-simd-clone.c (simd_clone_create): Ditto. * symtab.c (simd_symtab_node::noninterposable_alias): Use the new clone_function_name without numbering. 2018-10-30 Michael Ploujnikov <michael.ploujnikov@oracle.com> * lto-partition.c (privatize_symbol_name_1): Use clone_function_name_numbered. 2018-10-30 Michael Ploujnikov <michael.ploujnikov@oracle.com> * gcc.dg/tree-prof/cold_partition_label.c: Update for cold section names without numbers. * gcc.dg/tree-prof/section-attr-1.c: Ditto. * gcc.dg/tree-prof/section-attr-2.c: Ditto. * gcc.dg/tree-prof/section-attr-3.c: Ditto. From-SVN: r265621
2018-10-30 12:39:29 +01:00
clone_function_name_numbered (
node->decl, "default"));
/* FIXME: copy of cgraph_node::make_local that should be cleaned up
in next stage1. */
node->make_decl_local ();
node->set_section (NULL);
node->set_comdat_group (NULL);
node->externally_visible = false;
node->forced_by_abi = false;
node->set_section (NULL);
node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
|| node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
&& !flag_incremental_link);
node->resolution = LDPR_PREVAILING_DEF_IRONLY;
DECL_ARTIFICIAL (node->decl) = 1;
node->force_output = true;
}
/* Return length of attribute names string,
if arglist chain > 1, -1 otherwise. */
static int
get_attr_len (tree arglist)
{
tree arg;
int str_len_sum = 0;
int argnum = 0;
for (arg = arglist; arg; arg = TREE_CHAIN (arg))
{
const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
size_t len = strlen (str);
str_len_sum += len + 1;
for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
argnum++;
argnum++;
}
if (argnum <= 1)
return -1;
return str_len_sum;
}
/* Create string with attributes separated by comma.
Return number of attributes. */
static int
get_attr_str (tree arglist, char *attr_str)
{
tree arg;
size_t str_len_sum = 0;
int argnum = 0;
for (arg = arglist; arg; arg = TREE_CHAIN (arg))
{
const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
size_t len = strlen (str);
for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
argnum++;
memcpy (attr_str + str_len_sum, str, len);
attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
str_len_sum += len + 1;
argnum++;
}
return argnum;
}
/* Return number of attributes separated by comma and put them into ARGS.
If there is no DEFAULT attribute return -1. If there is an empty
string in attribute return -2. */
static int
separate_attrs (char *attr_str, char **attrs, int attrnum)
{
int i = 0;
int default_count = 0;
for (char *attr = strtok (attr_str, ",");
attr != NULL; attr = strtok (NULL, ","))
{
if (strcmp (attr, "default") == 0)
{
default_count++;
continue;
}
attrs[i++] = attr;
}
if (default_count == 0)
return -1;
else if (i + default_count < attrnum)
return -2;
return i;
}
/* Return true if symbol is valid in assembler name. */
static bool
is_valid_asm_symbol (char c)
{
if ('a' <= c && c <= 'z')
return true;
if ('A' <= c && c <= 'Z')
return true;
if ('0' <= c && c <= '9')
return true;
if (c == '_')
return true;
return false;
}
/* Replace all not valid assembler symbols with '_'. */
static void
create_new_asm_name (char *old_asm_name, char *new_asm_name)
{
int i;
int old_name_len = strlen (old_asm_name);
/* Replace all not valid assembler symbols with '_'. */
for (i = 0; i < old_name_len; i++)
if (!is_valid_asm_symbol (old_asm_name[i]))
new_asm_name[i] = '_';
else
new_asm_name[i] = old_asm_name[i];
new_asm_name[old_name_len] = '\0';
}
/* Creates target clone of NODE. */
static cgraph_node *
create_target_clone (cgraph_node *node, bool definition, char *name)
{
cgraph_node *new_node;
if (definition)
{
new_node = node->create_version_clone_with_body (vNULL, NULL,
NULL, false,
NULL, NULL,
name);
new_node->force_output = true;
}
else
{
tree new_decl = copy_node (node->decl);
new_node = cgraph_node::get_create (new_decl);
/* Generate a new name for the new version. */
symtab->change_decl_assembler_name (new_node->decl,
Avoid unnecessarily numbering cloned symbols. 2018-10-30 Michael Ploujnikov <michael.ploujnikov@oracle.com> * cgraph.h (clone_function_name_1): Replaced by new clone_function_name_numbered that takes name as string; for privatize_symbol_name_1 use only. (clone_function_name): Renamed to clone_function_name_numbered to be explicit about numbering. (clone_function_name): New two-argument function that does not number its output. (clone_function_name): New three-argument function that takes a number to append to its output. * cgraphclones.c (duplicate_thunk_for_node): (clone_function_name_1): Renamed. (clone_function_name_numbered): Two new functions. (clone_function_name): Improved documentation. (cgraph_node::create_virtual_clone): Use clone_function_name_numbered. * config/rs6000/rs6000.c (make_resolver_func): Ditto. * final.c (final_scan_insn_1): Use the new clone_function_name without numbering. * multiple_target.c (create_dispatcher_calls): Ditto. (create_target_clone): Ditto. * omp-expand.c (grid_expand_target_grid_body): Ditto. * omp-low.c (create_omp_child_function_name): Ditto. * omp-simd-clone.c (simd_clone_create): Ditto. * symtab.c (simd_symtab_node::noninterposable_alias): Use the new clone_function_name without numbering. 2018-10-30 Michael Ploujnikov <michael.ploujnikov@oracle.com> * lto-partition.c (privatize_symbol_name_1): Use clone_function_name_numbered. 2018-10-30 Michael Ploujnikov <michael.ploujnikov@oracle.com> * gcc.dg/tree-prof/cold_partition_label.c: Update for cold section names without numbers. * gcc.dg/tree-prof/section-attr-1.c: Ditto. * gcc.dg/tree-prof/section-attr-2.c: Ditto. * gcc.dg/tree-prof/section-attr-3.c: Ditto. From-SVN: r265621
2018-10-30 12:39:29 +01:00
clone_function_name_numbered (
node->decl, name));
}
return new_node;
}
/* If the function in NODE has multiple target attributes
create the appropriate clone for each valid target attribute. */
static bool
expand_target_clones (struct cgraph_node *node, bool definition)
{
int i;
/* Parsing target attributes separated by comma. */
tree attr_target = lookup_attribute ("target_clones",
DECL_ATTRIBUTES (node->decl));
/* No targets specified. */
if (!attr_target)
return false;
tree arglist = TREE_VALUE (attr_target);
int attr_len = get_attr_len (arglist);
/* No need to clone for 1 target attribute. */
if (attr_len == -1)
{
warning_at (DECL_SOURCE_LOCATION (node->decl),
0,
"single %<target_clones%> attribute is ignored");
return false;
}
if (node->definition
&& !tree_versionable_function_p (node->decl))
{
Add support for grouping of related diagnostics (PR other/84889) We often emit logically-related groups of diagnostics. A relatively simple case is this: if (warning_at (body_loc, OPT_Wmultistatement_macros, "macro expands to multiple statements")) inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); where the "note" diagnostic issued by the "inform" call is guarded by the -Wmultistatement_macros warning. More complicated examples can be seen in the C++ frontend, where e.g. print_z_candidates can lead to numerous "note" diagnostics being emitted. I'm looking at various ways to improve how we handle such related diagnostics, but, prior to this patch, there was no explicit relationship between these diagnostics: the diagnostics subsystem had no way of "knowing" that these were related. This patch introduces a simple way to group the diagnostics: an auto_diagnostic_group class: all diagnostics emitted within the lifetime of an auto_diagnostic_group instance are logically grouped. Hence in the above example, the two diagnostics can be grouped by simply adding an auto_diagnostic_group instance: auto_diagnostic_group d; if (warning_at (body_loc, OPT_Wmultistatement_macros, "macro expands to multiple statements")) inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); Some more awkard cases are of the form: if (some_condition && warning_at (...) && more_conditions) inform (...); which thus need restructuring to: if (some_condition) { auto_diagnostic_group d; warning_at (...); if (more_conditions) inform (...); } so that the lifetime of the auto_diagnostic_group groups the warning_at and the inform call. Nesting is handled by simply tracking a nesting depth within the diagnostic_context.: all diagnostics are treated as grouped until the final auto_diagnostic_group is popped. diagnostic.c uses this internally, so that all diagnostics are part of a group - those that are "by themselves" are treated as being part of a group with one element. The diagnostic_context gains optional callbacks for displaying the start of a group (when the first diagnostic is emitted within it), and the end of a group (for when the group was non-empty); these callbacks are unused by default, but a test plugin demonstrates them (and verifies that the machinery is working). As noted above, I'm looking at various ways to use the grouping to improve how we output the diagnostics. FWIW, I experimented with a more involved implementation, of the form: diagnostic_group d; if (d.warning_at (body_loc, OPT_Wmultistatement_macros, "macro expands to multiple statements")) d.inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); which had the advantage of allowing auto-detection of the places where groups were needed (by converting ::warning_at's return type to bool), but it was a much more invasive patch, especially when dealing with the places in the C++ frontend that can emit numerous notes after an error or warning (and thus having to pass the group around) Hence I went with this simpler approach. gcc/c-family/ChangeLog: PR other/84889 * c-attribs.c (common_handle_aligned_attribute): Add auto_diagnostic_group instance. * c-indentation.c (warn_for_misleading_indentation): Likewise. * c-opts.c (c_common_post_options): Likewise. * c-warn.c (warn_logical_not_parentheses): Likewise. (warn_duplicated_cond_add_or_warn): Likewise. (warn_for_multistatement_macros): Likewise. gcc/c/ChangeLog: PR other/84889 * c-decl.c (pushtag): Add auto_diagnostic_group instance. (diagnose_mismatched_decls): Likewise, in various places. (warn_if_shadowing): Likewise. (implicit_decl_warning): Likewise. (implicitly_declare): Likewise. (undeclared_variable): Likewise. (declare_label): Likewise. (grokdeclarator): Likewise. (start_function): Likewise. * c-parser.c (c_parser_declaration_or_fndef): Likewise. (c_parser_parameter_declaration): Likewise. (c_parser_binary_expression): Likewise. * c-typeck.c (c_expr_sizeof_expr): Likewise. (parser_build_binary_op): Likewise. (build_unary_op): Likewise. (error_init): Likewise. (pedwarn_init): Likewise. (warning_init): Likewise. (convert_for_assignment): Likewise. gcc/cp/ChangeLog: PR other/84889 * call.c (build_user_type_conversion_1): Add auto_diagnostic_group instance(s). (print_error_for_call_failure): Likewise. (build_op_call_1): Likewise. (build_conditional_expr_1): Likewise. (build_new_op_1): Likewise. (build_op_delete_call): Likewise. (convert_like_real): Likewise. (build_over_call): Likewise. (build_new_method_call_1): Likewise. (joust): Likewise. * class.c (check_tag): Likewise. (finish_struct_anon_r): Likewise. (one_inherited_ctor): Likewise. (finalize_literal_type_property): Likewise. (explain_non_literal_class): Likewise. (find_flexarrays): Likewise. (resolve_address_of_overloaded_function): Likewise. * constexpr.c (ensure_literal_type_for_constexpr_object): Likewise. (is_valid_constexpr_fn): Likewise. (cx_check_missing_mem_inits): Likewise. * cp-gimplify.c (cp_genericize_r): Likewise. * cvt.c (maybe_warn_nodiscard): Likewise. * decl.c (warn_extern_redeclared_static): Likewise. (check_redeclaration_exception_specification): Likewise. (check_no_redeclaration_friend_default_args): Likewise. (duplicate_decls): Likewise. (redeclaration_error_message): Likewise. (warn_misplaced_attr_for_class_type): Likewise. * decl2.c (finish_static_data_member_decl): Likewise. (no_linkage_error): Likewise. (cp_warn_deprecated_use): Likewise. * error.c (qualified_name_lookup_error): Likewise. * friend.c (make_friend_class): Likewise. (do_friend): Likewise. * init.c (perform_member_init): Likewise. (build_new_1): Likewise. (build_vec_delete_1): Likewise. (build_delete): Likewise. * lex.c (unqualified_name_lookup_error): Likewise. * name-lookup.c (check_extern_c_conflict): Likewise. (inform_shadowed): New function. (check_local_shadow): Add auto_diagnostic_group instances, replacing goto "inform_shadowed" label with call to subroutine. (set_local_extern_decl_linkage): Add auto_diagnostic_group instance(s). * parser.c (cp_parser_diagnose_invalid_type_name): Likewise. (cp_parser_namespace_name): Likewise. * pt.c (check_specialization_namespace): Likewise. (check_template_variable): Likewise. (warn_spec_missing_attributes): Likewise. (check_explicit_specialization): Likewise. (process_partial_specialization): Likewise. (lookup_template_class_1): Likewise. (finish_template_variable): Likewise. (do_auto_deduction): Likewise. * search.c (check_final_overrider): Likewise. (look_for_overrides_r): Likewise. * tree.c (maybe_warn_parm_abi): Likewise. * typeck.c (cxx_sizeof_expr): Likewise. (cp_build_function_call_vec): Likewise. (cp_build_binary_op): Likewise. (convert_for_assignment): Likewise. (maybe_warn_about_returning_address_of_local): Likewise. * typeck2.c (abstract_virtuals_error_sfinae): Likewise. (check_narrowing): Likewise. gcc/ChangeLog: PR other/84889 * attribs.c (diag_attr_exclusions): Add auto_diagnostic_group instance. (decl_attributes): Likewise. * calls.c (maybe_warn_nonstring_arg): Add auto_diagnostic_group instance. * cgraphunit.c (maybe_diag_incompatible_alias): Likewise. * diagnostic-core.h (class auto_diagnostic_group): New class. * diagnostic.c (diagnostic_initialize): Initialize the new fields. (diagnostic_report_diagnostic): Handle the first diagnostics within a group. (emit_diagnostic): Add auto_diagnostic_group instance. (inform): Likewise. (inform_n): Likewise. (warning): Likewise. (warning_at): Likewise. (warning_n): Likewise. (pedwarn): Likewise. (permerror): Likewise. (error): Likewise. (error_n): Likewise. (error_at): Likewise. (sorry): Likewise. (fatal_error): Likewise. (internal_error): Likewise. (internal_error_no_backtrace): Likewise. (auto_diagnostic_group::auto_diagnostic_group): New ctor. (auto_diagnostic_group::~auto_diagnostic_group): New dtor. * diagnostic.h (struct diagnostic_context): Add fields "diagnostic_group_nesting_depth", "diagnostic_group_emission_count", "begin_group_cb", "end_group_cb". * gimple-ssa-isolate-paths.c (find_implicit_erroneous_behavior): Add auto_diagnostic_group instance(s). (find_explicit_erroneous_behavior): Likewise. * gimple-ssa-warn-alloca.c (pass_walloca::execute): Likewise. * gimple-ssa-warn-restrict.c (maybe_diag_offset_bounds): Likewise. * gimplify.c (warn_implicit_fallthrough_r): Likewise. (gimplify_va_arg_expr): Likewise. * hsa-gen.c (HSA_SORRY_ATV): Likewise. (HSA_SORRY_AT): Likewise. * ipa-devirt.c (compare_virtual_tables): Likewise. (warn_odr): Likewise. * multiple_target.c (expand_target_clones): Likewise. * opts-common.c (cmdline_handle_error): Likewise. * reginfo.c (globalize_reg): Likewise. * substring-locations.c (format_warning_n_va): Likewise. * tree-inline.c (expand_call_inline): Likewise. * tree-ssa-ccp.c (pass_post_ipa_warn::execute): Likewise. * tree-ssa-loop-niter.c (do_warn_aggressive_loop_optimizations): Likewise. * tree-ssa-uninit.c (warn_uninit): Likewise. * tree.c (warn_deprecated_use): Likewise. gcc/testsuite/ChangeLog: PR other/84889 * gcc.dg/plugin/diagnostic-group-test-1.c: New test. * gcc.dg/plugin/diagnostic_group_plugin.c: New test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests. From-SVN: r263675
2018-08-20 23:06:46 +02:00
auto_diagnostic_group d;
error_at (DECL_SOURCE_LOCATION (node->decl),
"clones for %<target_clones%> attribute cannot be created");
const char *reason = NULL;
if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
reason = G_("function %q+F can never be copied "
"because it has %<noclone%> attribute");
else
reason = copy_forbidden (DECL_STRUCT_FUNCTION (node->decl));
if (reason)
inform (DECL_SOURCE_LOCATION (node->decl), reason, node->decl);
return false;
}
char *attr_str = XNEWVEC (char, attr_len);
int attrnum = get_attr_str (arglist, attr_str);
char **attrs = XNEWVEC (char *, attrnum);
attrnum = separate_attrs (attr_str, attrs, attrnum);
if (attrnum == -1)
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"default target was not set");
XDELETEVEC (attrs);
XDELETEVEC (attr_str);
return false;
}
else if (attrnum == -2)
{
error_at (DECL_SOURCE_LOCATION (node->decl),
"an empty string cannot be in %<target_clones%> attribute");
XDELETEVEC (attrs);
XDELETEVEC (attr_str);
return false;
}
cgraph_function_version_info *decl1_v = NULL;
cgraph_function_version_info *decl2_v = NULL;
cgraph_function_version_info *before = NULL;
cgraph_function_version_info *after = NULL;
decl1_v = node->function_version ();
if (decl1_v == NULL)
decl1_v = node->insert_new_function_version ();
before = decl1_v;
DECL_FUNCTION_VERSIONED (node->decl) = 1;
for (i = 0; i < attrnum; i++)
{
char *attr = attrs[i];
char *suffix = XNEWVEC (char, strlen (attr) + 1);
create_new_asm_name (attr, suffix);
/* Create new target clone. */
cgraph_node *new_node = create_target_clone (node, definition, suffix);
new_node->local.local = false;
XDELETEVEC (suffix);
/* Set new attribute for the clone. */
tree attributes = make_attribute ("target", attr,
DECL_ATTRIBUTES (new_node->decl));
DECL_ATTRIBUTES (new_node->decl) = attributes;
location_t saved_loc = input_location;
input_location = DECL_SOURCE_LOCATION (node->decl);
if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
TREE_VALUE (attributes),
0))
return false;
input_location = saved_loc;
decl2_v = new_node->function_version ();
if (decl2_v != NULL)
continue;
decl2_v = new_node->insert_new_function_version ();
/* Chain decl2_v and decl1_v. All semantically identical versions
will be chained together. */
after = decl2_v;
while (before->next != NULL)
before = before->next;
while (after->prev != NULL)
after = after->prev;
before->next = after;
after->prev = before;
DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
}
XDELETEVEC (attrs);
XDELETEVEC (attr_str);
/* Setting new attribute to initial function. */
tree attributes = make_attribute ("target", "default",
DECL_ATTRIBUTES (node->decl));
DECL_ATTRIBUTES (node->decl) = attributes;
node->local.local = false;
location_t saved_loc = input_location;
input_location = DECL_SOURCE_LOCATION (node->decl);
bool ret
= targetm.target_option.valid_attribute_p (node->decl, NULL,
TREE_VALUE (attributes), 0);
input_location = saved_loc;
return ret;
}
/* When NODE is a target clone, consider all callees and redirect
to a clone with equal target attributes. That prevents multiple
multi-versioning dispatches and a call-chain can be optimized. */
static void
redirect_to_specific_clone (cgraph_node *node)
{
cgraph_function_version_info *fv = node->function_version ();
if (fv == NULL)
return;
tree attr_target = lookup_attribute ("target", DECL_ATTRIBUTES (node->decl));
if (attr_target == NULL_TREE)
return;
/* We need to remember NEXT_CALLER as it could be modified in the loop. */
for (cgraph_edge *e = node->callees; e ; e = e->next_callee)
{
cgraph_function_version_info *fv2 = e->callee->function_version ();
if (!fv2)
continue;
tree attr_target2 = lookup_attribute ("target",
DECL_ATTRIBUTES (e->callee->decl));
/* Function is not calling proper target clone. */
if (!attribute_list_equal (attr_target, attr_target2))
{
while (fv2->prev != NULL)
fv2 = fv2->prev;
/* Try to find a clone with equal target attribute. */
for (; fv2 != NULL; fv2 = fv2->next)
{
cgraph_node *callee = fv2->this_node;
attr_target2 = lookup_attribute ("target",
DECL_ATTRIBUTES (callee->decl));
if (attribute_list_equal (attr_target, attr_target2))
{
e->redirect_callee (callee);
e->redirect_call_stmt_to_callee ();
break;
}
}
}
}
}
static unsigned int
ipa_target_clone (void)
{
struct cgraph_node *node;
auto_vec<cgraph_node *> to_dispatch;
FOR_EACH_FUNCTION (node)
if (expand_target_clones (node, node->definition))
to_dispatch.safe_push (node);
for (unsigned i = 0; i < to_dispatch.length (); i++)
create_dispatcher_calls (to_dispatch[i]);
FOR_EACH_FUNCTION (node)
redirect_to_specific_clone (node);
return 0;
}
namespace {
const pass_data pass_data_target_clone =
{
SIMPLE_IPA_PASS, /* type */
"targetclone", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_NONE, /* tv_id */
( PROP_ssa | PROP_cfg ), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_update_ssa /* todo_flags_finish */
};
class pass_target_clone : public simple_ipa_opt_pass
{
public:
pass_target_clone (gcc::context *ctxt)
: simple_ipa_opt_pass (pass_data_target_clone, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *);
virtual unsigned int execute (function *) { return ipa_target_clone (); }
};
bool
pass_target_clone::gate (function *)
{
return true;
}
} // anon namespace
simple_ipa_opt_pass *
make_pass_target_clone (gcc::context *ctxt)
{
return new pass_target_clone (ctxt);
}