gcc/ChangeLog:
[official-gcc.git] / gcc / multiple_target.c
blobb006a5ab6eca98c3380030eff403dc664178be0f
1 /* Pass for parsing functions with multiple target attributes.
3 Contributed by Evgeny Stupachenko <evstupac@gmail.com>
5 Copyright (C) 2015-2018 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "gimple.h"
30 #include "diagnostic-core.h"
31 #include "gimple-ssa.h"
32 #include "cgraph.h"
33 #include "tree-pass.h"
34 #include "target.h"
35 #include "attribs.h"
36 #include "pretty-print.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "tree-inline.h"
40 #include "intl.h"
42 /* Walker callback that replaces all FUNCTION_DECL of a function that's
43 going to be versioned. */
45 static tree
46 replace_function_decl (tree *op, int *walk_subtrees, void *data)
48 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
49 cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
51 if (TREE_CODE (*op) == FUNCTION_DECL
52 && info->this_node->decl == *op)
54 *op = info->dispatcher_resolver;
55 *walk_subtrees = 0;
58 return NULL;
61 /* If the call in NODE has multiple target attribute with multiple fields,
62 replace it with dispatcher call and create dispatcher (once). */
64 static void
65 create_dispatcher_calls (struct cgraph_node *node)
67 ipa_ref *ref;
69 if (!DECL_FUNCTION_VERSIONED (node->decl)
70 || !is_function_default_version (node->decl))
71 return;
73 if (!targetm.has_ifunc_p ())
75 error_at (DECL_SOURCE_LOCATION (node->decl),
76 "the call requires ifunc, which is not"
77 " supported by this target");
78 return;
80 else if (!targetm.get_function_versions_dispatcher)
82 error_at (DECL_SOURCE_LOCATION (node->decl),
83 "target does not support function version dispatcher");
84 return;
87 tree idecl = targetm.get_function_versions_dispatcher (node->decl);
88 if (!idecl)
90 error_at (DECL_SOURCE_LOCATION (node->decl),
91 "default target_clones attribute was not set");
92 return;
95 cgraph_node *inode = cgraph_node::get (idecl);
96 gcc_assert (inode);
97 tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
99 /* Update aliases. */
100 inode->alias = true;
101 inode->alias_target = resolver_decl;
102 if (!inode->analyzed)
103 inode->resolve_alias (cgraph_node::get (resolver_decl));
105 auto_vec<cgraph_edge *> edges_to_redirect;
106 auto_vec<ipa_ref *> references_to_redirect;
108 for (unsigned i = 0; node->iterate_referring (i, ref); i++)
109 references_to_redirect.safe_push (ref);
111 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
112 for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
113 edges_to_redirect.safe_push (e);
115 if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
117 /* Redirect edges. */
118 unsigned i;
119 cgraph_edge *e;
120 FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
122 e->redirect_callee (inode);
123 e->redirect_call_stmt_to_callee ();
126 /* Redirect references. */
127 FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
129 if (ref->use == IPA_REF_ADDR)
131 struct walk_stmt_info wi;
132 memset (&wi, 0, sizeof (wi));
133 wi.info = (void *)node->function_version ();
135 if (dyn_cast<varpool_node *> (ref->referring))
137 hash_set<tree> visited_nodes;
138 walk_tree (&DECL_INITIAL (ref->referring->decl),
139 replace_function_decl, &wi, &visited_nodes);
141 else
143 gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
144 if (ref->referring->decl != resolver_decl)
145 walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
148 symtab_node *source = ref->referring;
149 ref->remove_reference ();
150 source->create_reference (inode, IPA_REF_ADDR);
152 else if (ref->use == IPA_REF_ALIAS)
154 symtab_node *source = ref->referring;
155 ref->remove_reference ();
156 source->create_reference (inode, IPA_REF_ALIAS);
157 source->add_to_same_comdat_group (inode);
159 else
160 gcc_unreachable ();
164 TREE_PUBLIC (node->decl) = 0;
165 symtab->change_decl_assembler_name (node->decl,
166 clone_function_name (node->decl,
167 "default"));
170 /* Return length of attribute names string,
171 if arglist chain > 1, -1 otherwise. */
173 static int
174 get_attr_len (tree arglist)
176 tree arg;
177 int str_len_sum = 0;
178 int argnum = 0;
180 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
182 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
183 size_t len = strlen (str);
184 str_len_sum += len + 1;
185 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
186 argnum++;
187 argnum++;
189 if (argnum <= 1)
190 return -1;
191 return str_len_sum;
194 /* Create string with attributes separated by comma.
195 Return number of attributes. */
197 static int
198 get_attr_str (tree arglist, char *attr_str)
200 tree arg;
201 size_t str_len_sum = 0;
202 int argnum = 0;
204 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
206 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
207 size_t len = strlen (str);
208 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
209 argnum++;
210 memcpy (attr_str + str_len_sum, str, len);
211 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
212 str_len_sum += len + 1;
213 argnum++;
215 return argnum;
218 /* Return number of attributes separated by comma and put them into ARGS.
219 If there is no DEFAULT attribute return -1. */
221 static int
222 separate_attrs (char *attr_str, char **attrs)
224 int i = 0;
225 bool has_default = false;
227 for (char *attr = strtok (attr_str, ",");
228 attr != NULL; attr = strtok (NULL, ","))
230 if (strcmp (attr, "default") == 0)
232 has_default = true;
233 continue;
235 attrs[i++] = attr;
237 if (!has_default)
238 return -1;
239 return i;
242 /* Return true if symbol is valid in assembler name. */
244 static bool
245 is_valid_asm_symbol (char c)
247 if ('a' <= c && c <= 'z')
248 return true;
249 if ('A' <= c && c <= 'Z')
250 return true;
251 if ('0' <= c && c <= '9')
252 return true;
253 if (c == '_')
254 return true;
255 return false;
258 /* Replace all not valid assembler symbols with '_'. */
260 static void
261 create_new_asm_name (char *old_asm_name, char *new_asm_name)
263 int i;
264 int old_name_len = strlen (old_asm_name);
266 /* Replace all not valid assembler symbols with '_'. */
267 for (i = 0; i < old_name_len; i++)
268 if (!is_valid_asm_symbol (old_asm_name[i]))
269 new_asm_name[i] = '_';
270 else
271 new_asm_name[i] = old_asm_name[i];
272 new_asm_name[old_name_len] = '\0';
275 /* Creates target clone of NODE. */
277 static cgraph_node *
278 create_target_clone (cgraph_node *node, bool definition, char *name)
280 cgraph_node *new_node;
282 if (definition)
284 new_node = node->create_version_clone_with_body (vNULL, NULL,
285 NULL, false,
286 NULL, NULL,
287 name);
288 new_node->force_output = true;
290 else
292 tree new_decl = copy_node (node->decl);
293 new_node = cgraph_node::get_create (new_decl);
294 /* Generate a new name for the new version. */
295 symtab->change_decl_assembler_name (new_node->decl,
296 clone_function_name (node->decl,
297 name));
299 return new_node;
302 /* If the function in NODE has multiple target attributes
303 create the appropriate clone for each valid target attribute. */
305 static bool
306 expand_target_clones (struct cgraph_node *node, bool definition)
308 int i;
309 /* Parsing target attributes separated by comma. */
310 tree attr_target = lookup_attribute ("target_clones",
311 DECL_ATTRIBUTES (node->decl));
312 /* No targets specified. */
313 if (!attr_target)
314 return false;
316 tree arglist = TREE_VALUE (attr_target);
317 int attr_len = get_attr_len (arglist);
319 /* No need to clone for 1 target attribute. */
320 if (attr_len == -1)
322 warning_at (DECL_SOURCE_LOCATION (node->decl),
324 "single target_clones attribute is ignored");
325 return false;
328 if (node->definition
329 && !tree_versionable_function_p (node->decl))
331 error_at (DECL_SOURCE_LOCATION (node->decl),
332 "clones for %<target_clones%> attribute cannot be created");
333 const char *reason = NULL;
334 if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
335 reason = G_("function %q+F can never be copied "
336 "because it has %<noclone%> attribute");
337 else
338 reason = copy_forbidden (DECL_STRUCT_FUNCTION (node->decl));
339 if (reason)
340 inform (DECL_SOURCE_LOCATION (node->decl), reason, node->decl);
341 return false;
344 char *attr_str = XNEWVEC (char, attr_len);
345 int attrnum = get_attr_str (arglist, attr_str);
346 char **attrs = XNEWVEC (char *, attrnum);
348 attrnum = separate_attrs (attr_str, attrs);
349 if (attrnum == -1)
351 error_at (DECL_SOURCE_LOCATION (node->decl),
352 "default target was not set");
353 XDELETEVEC (attrs);
354 XDELETEVEC (attr_str);
355 return false;
358 cgraph_function_version_info *decl1_v = NULL;
359 cgraph_function_version_info *decl2_v = NULL;
360 cgraph_function_version_info *before = NULL;
361 cgraph_function_version_info *after = NULL;
362 decl1_v = node->function_version ();
363 if (decl1_v == NULL)
364 decl1_v = node->insert_new_function_version ();
365 before = decl1_v;
366 DECL_FUNCTION_VERSIONED (node->decl) = 1;
368 for (i = 0; i < attrnum; i++)
370 char *attr = attrs[i];
371 char *suffix = XNEWVEC (char, strlen (attr) + 1);
373 create_new_asm_name (attr, suffix);
374 /* Create new target clone. */
375 cgraph_node *new_node = create_target_clone (node, definition, suffix);
376 new_node->local.local = false;
377 XDELETEVEC (suffix);
379 /* Set new attribute for the clone. */
380 tree attributes = make_attribute ("target", attr,
381 DECL_ATTRIBUTES (new_node->decl));
382 DECL_ATTRIBUTES (new_node->decl) = attributes;
383 location_t saved_loc = input_location;
384 input_location = DECL_SOURCE_LOCATION (node->decl);
385 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
386 TREE_VALUE (attributes),
388 return false;
390 input_location = saved_loc;
391 decl2_v = new_node->function_version ();
392 if (decl2_v != NULL)
393 continue;
394 decl2_v = new_node->insert_new_function_version ();
396 /* Chain decl2_v and decl1_v. All semantically identical versions
397 will be chained together. */
398 after = decl2_v;
399 while (before->next != NULL)
400 before = before->next;
401 while (after->prev != NULL)
402 after = after->prev;
404 before->next = after;
405 after->prev = before;
406 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
409 XDELETEVEC (attrs);
410 XDELETEVEC (attr_str);
412 /* Setting new attribute to initial function. */
413 tree attributes = make_attribute ("target", "default",
414 DECL_ATTRIBUTES (node->decl));
415 DECL_ATTRIBUTES (node->decl) = attributes;
416 node->local.local = false;
417 location_t saved_loc = input_location;
418 input_location = DECL_SOURCE_LOCATION (node->decl);
419 bool ret
420 = targetm.target_option.valid_attribute_p (node->decl, NULL,
421 TREE_VALUE (attributes), 0);
422 input_location = saved_loc;
423 return ret;
426 static unsigned int
427 ipa_target_clone (void)
429 struct cgraph_node *node;
431 bool target_clone_pass = false;
432 FOR_EACH_FUNCTION (node)
433 target_clone_pass |= expand_target_clones (node, node->definition);
435 if (target_clone_pass)
436 FOR_EACH_FUNCTION (node)
437 create_dispatcher_calls (node);
439 return 0;
442 namespace {
444 const pass_data pass_data_target_clone =
446 SIMPLE_IPA_PASS, /* type */
447 "targetclone", /* name */
448 OPTGROUP_NONE, /* optinfo_flags */
449 TV_NONE, /* tv_id */
450 ( PROP_ssa | PROP_cfg ), /* properties_required */
451 0, /* properties_provided */
452 0, /* properties_destroyed */
453 0, /* todo_flags_start */
454 TODO_update_ssa /* todo_flags_finish */
457 class pass_target_clone : public simple_ipa_opt_pass
459 public:
460 pass_target_clone (gcc::context *ctxt)
461 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
464 /* opt_pass methods: */
465 virtual bool gate (function *);
466 virtual unsigned int execute (function *) { return ipa_target_clone (); }
469 bool
470 pass_target_clone::gate (function *)
472 return true;
475 } // anon namespace
477 simple_ipa_opt_pass *
478 make_pass_target_clone (gcc::context *ctxt)
480 return new pass_target_clone (ctxt);