libgo: add misc/cgo files
[official-gcc.git] / gcc / multiple_target.c
blob38d6892af282a90609473cfc6bdb6319f68d54ee
1 /* Pass for parsing functions with multiple target attributes.
3 Contributed by Evgeny Stupachenko <evstupac@gmail.com>
5 Copyright (C) 2015-2017 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"
40 /* Walker callback that replaces all FUNCTION_DECL of a function that's
41 going to be versioned. */
43 static tree
44 replace_function_decl (tree *op, int *walk_subtrees, void *data)
46 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
47 cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
49 if (TREE_CODE (*op) == FUNCTION_DECL
50 && info->this_node->decl == *op)
52 *op = info->dispatcher_resolver;
53 *walk_subtrees = 0;
56 return NULL;
59 /* If the call in NODE has multiple target attribute with multiple fields,
60 replace it with dispatcher call and create dispatcher (once). */
62 static void
63 create_dispatcher_calls (struct cgraph_node *node)
65 ipa_ref *ref;
67 if (!DECL_FUNCTION_VERSIONED (node->decl)
68 || !is_function_default_version (node->decl))
69 return;
71 auto_vec<cgraph_edge *> edges_to_redirect;
72 auto_vec<ipa_ref *> references_to_redirect;
74 for (unsigned i = 0; node->iterate_referring (i, ref); i++)
75 references_to_redirect.safe_push (ref);
77 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
78 for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
79 edges_to_redirect.safe_push (e);
81 if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
83 if (!targetm.has_ifunc_p ())
85 error_at (DECL_SOURCE_LOCATION (node->decl),
86 "the call requires ifunc, which is not"
87 " supported by this target");
88 return;
90 else if (!targetm.get_function_versions_dispatcher)
92 error_at (DECL_SOURCE_LOCATION (node->decl),
93 "target does not support function version dispatcher");
94 return;
97 tree idecl = targetm.get_function_versions_dispatcher (node->decl);
98 if (!idecl)
100 error_at (DECL_SOURCE_LOCATION (node->decl),
101 "default target_clones attribute was not set");
102 return;
105 cgraph_node *inode = cgraph_node::get (idecl);
106 gcc_assert (inode);
107 tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
109 /* Update aliases. */
110 inode->alias = true;
111 inode->alias_target = resolver_decl;
112 if (!inode->analyzed)
113 inode->resolve_alias (cgraph_node::get (resolver_decl));
115 /* Redirect edges. */
116 unsigned i;
117 cgraph_edge *e;
118 FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
120 e->redirect_callee (inode);
121 e->redirect_call_stmt_to_callee ();
124 /* Redirect references. */
125 FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
127 if (ref->use == IPA_REF_ADDR)
129 struct walk_stmt_info wi;
130 memset (&wi, 0, sizeof (wi));
131 wi.info = (void *)node->function_version ();
133 if (dyn_cast<varpool_node *> (ref->referring))
135 hash_set<tree> visited_nodes;
136 walk_tree (&DECL_INITIAL (ref->referring->decl),
137 replace_function_decl, &wi, &visited_nodes);
139 else
141 gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
142 if (ref->referring->decl != resolver_decl)
143 walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
146 else
147 gcc_unreachable ();
151 symtab->change_decl_assembler_name (node->decl,
152 clone_function_name (node->decl,
153 "default"));
156 /* Return length of attribute names string,
157 if arglist chain > 1, -1 otherwise. */
159 static int
160 get_attr_len (tree arglist)
162 tree arg;
163 int str_len_sum = 0;
164 int argnum = 0;
166 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
168 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
169 size_t len = strlen (str);
170 str_len_sum += len + 1;
171 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
172 argnum++;
173 argnum++;
175 if (argnum <= 1)
176 return -1;
177 return str_len_sum;
180 /* Create string with attributes separated by comma.
181 Return number of attributes. */
183 static int
184 get_attr_str (tree arglist, char *attr_str)
186 tree arg;
187 size_t str_len_sum = 0;
188 int argnum = 0;
190 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
192 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
193 size_t len = strlen (str);
194 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
195 argnum++;
196 memcpy (attr_str + str_len_sum, str, len);
197 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
198 str_len_sum += len + 1;
199 argnum++;
201 return argnum;
204 /* Return number of attributes separated by comma and put them into ARGS.
205 If there is no DEFAULT attribute return -1. */
207 static int
208 separate_attrs (char *attr_str, char **attrs)
210 int i = 0;
211 bool has_default = false;
213 for (char *attr = strtok (attr_str, ",");
214 attr != NULL; attr = strtok (NULL, ","))
216 if (strcmp (attr, "default") == 0)
218 has_default = true;
219 continue;
221 attrs[i++] = attr;
223 if (!has_default)
224 return -1;
225 return i;
228 /* Return true if symbol is valid in assembler name. */
230 static bool
231 is_valid_asm_symbol (char c)
233 if ('a' <= c && c <= 'z')
234 return true;
235 if ('A' <= c && c <= 'Z')
236 return true;
237 if ('0' <= c && c <= '9')
238 return true;
239 if (c == '_')
240 return true;
241 return false;
244 /* Replace all not valid assembler symbols with '_'. */
246 static void
247 create_new_asm_name (char *old_asm_name, char *new_asm_name)
249 int i;
250 int old_name_len = strlen (old_asm_name);
252 /* Replace all not valid assembler symbols with '_'. */
253 for (i = 0; i < old_name_len; i++)
254 if (!is_valid_asm_symbol (old_asm_name[i]))
255 new_asm_name[i] = '_';
256 else
257 new_asm_name[i] = old_asm_name[i];
258 new_asm_name[old_name_len] = '\0';
261 /* Creates target clone of NODE. */
263 static cgraph_node *
264 create_target_clone (cgraph_node *node, bool definition, char *name)
266 cgraph_node *new_node;
268 if (definition)
270 new_node = node->create_version_clone_with_body (vNULL, NULL,
271 NULL, false,
272 NULL, NULL,
273 name);
274 new_node->force_output = true;
276 else
278 tree new_decl = copy_node (node->decl);
279 new_node = cgraph_node::get_create (new_decl);
280 /* Generate a new name for the new version. */
281 symtab->change_decl_assembler_name (new_node->decl,
282 clone_function_name (node->decl,
283 name));
285 return new_node;
288 /* If the function in NODE has multiple target attributes
289 create the appropriate clone for each valid target attribute. */
291 static bool
292 expand_target_clones (struct cgraph_node *node, bool definition)
294 int i;
295 /* Parsing target attributes separated by comma. */
296 tree attr_target = lookup_attribute ("target_clones",
297 DECL_ATTRIBUTES (node->decl));
298 /* No targets specified. */
299 if (!attr_target)
300 return false;
302 tree arglist = TREE_VALUE (attr_target);
303 int attr_len = get_attr_len (arglist);
305 /* No need to clone for 1 target attribute. */
306 if (attr_len == -1)
308 warning_at (DECL_SOURCE_LOCATION (node->decl),
310 "single target_clones attribute is ignored");
311 return false;
314 char *attr_str = XNEWVEC (char, attr_len);
315 int attrnum = get_attr_str (arglist, attr_str);
316 char **attrs = XNEWVEC (char *, attrnum);
318 attrnum = separate_attrs (attr_str, attrs);
319 if (attrnum == -1)
321 error_at (DECL_SOURCE_LOCATION (node->decl),
322 "default target was not set");
323 XDELETEVEC (attrs);
324 XDELETEVEC (attr_str);
325 return false;
328 cgraph_function_version_info *decl1_v = NULL;
329 cgraph_function_version_info *decl2_v = NULL;
330 cgraph_function_version_info *before = NULL;
331 cgraph_function_version_info *after = NULL;
332 decl1_v = node->function_version ();
333 if (decl1_v == NULL)
334 decl1_v = node->insert_new_function_version ();
335 before = decl1_v;
336 DECL_FUNCTION_VERSIONED (node->decl) = 1;
338 for (i = 0; i < attrnum; i++)
340 char *attr = attrs[i];
341 char *suffix = XNEWVEC (char, strlen (attr) + 1);
343 create_new_asm_name (attr, suffix);
344 /* Create new target clone. */
345 cgraph_node *new_node = create_target_clone (node, definition, suffix);
346 new_node->local.local = false;
347 XDELETEVEC (suffix);
349 /* Set new attribute for the clone. */
350 tree attributes = make_attribute ("target", attr,
351 DECL_ATTRIBUTES (new_node->decl));
352 DECL_ATTRIBUTES (new_node->decl) = attributes;
353 location_t saved_loc = input_location;
354 input_location = DECL_SOURCE_LOCATION (node->decl);
355 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
356 TREE_VALUE (attributes),
358 return false;
360 input_location = saved_loc;
361 decl2_v = new_node->function_version ();
362 if (decl2_v != NULL)
363 continue;
364 decl2_v = new_node->insert_new_function_version ();
366 /* Chain decl2_v and decl1_v. All semantically identical versions
367 will be chained together. */
368 after = decl2_v;
369 while (before->next != NULL)
370 before = before->next;
371 while (after->prev != NULL)
372 after = after->prev;
374 before->next = after;
375 after->prev = before;
376 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
379 XDELETEVEC (attrs);
380 XDELETEVEC (attr_str);
382 /* Setting new attribute to initial function. */
383 tree attributes = make_attribute ("target", "default",
384 DECL_ATTRIBUTES (node->decl));
385 DECL_ATTRIBUTES (node->decl) = attributes;
386 node->local.local = false;
387 location_t saved_loc = input_location;
388 input_location = DECL_SOURCE_LOCATION (node->decl);
389 bool ret
390 = targetm.target_option.valid_attribute_p (node->decl, NULL,
391 TREE_VALUE (attributes), 0);
392 input_location = saved_loc;
393 return ret;
396 static unsigned int
397 ipa_target_clone (void)
399 struct cgraph_node *node;
401 bool target_clone_pass = false;
402 FOR_EACH_FUNCTION (node)
403 target_clone_pass |= expand_target_clones (node, node->definition);
405 if (target_clone_pass)
406 FOR_EACH_FUNCTION (node)
407 create_dispatcher_calls (node);
409 return 0;
412 namespace {
414 const pass_data pass_data_target_clone =
416 SIMPLE_IPA_PASS, /* type */
417 "targetclone", /* name */
418 OPTGROUP_NONE, /* optinfo_flags */
419 TV_NONE, /* tv_id */
420 ( PROP_ssa | PROP_cfg ), /* properties_required */
421 0, /* properties_provided */
422 0, /* properties_destroyed */
423 0, /* todo_flags_start */
424 TODO_update_ssa /* todo_flags_finish */
427 class pass_target_clone : public simple_ipa_opt_pass
429 public:
430 pass_target_clone (gcc::context *ctxt)
431 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
434 /* opt_pass methods: */
435 virtual bool gate (function *);
436 virtual unsigned int execute (function *) { return ipa_target_clone (); }
439 bool
440 pass_target_clone::gate (function *)
442 return true;
445 } // anon namespace
447 simple_ipa_opt_pass *
448 make_pass_target_clone (gcc::context *ctxt)
450 return new pass_target_clone (ctxt);