Emit SIMD moves as mov
[official-gcc.git] / gcc / multiple_target.c
blobbdb5b3bf22815b5caa04b87e11a53a8f23d804b8
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 return;
70 auto_vec<cgraph_edge *> edges_to_redirect;
71 auto_vec<ipa_ref *> references_to_redirect;
73 for (unsigned i = 0; node->iterate_referring (i, ref); i++)
74 references_to_redirect.safe_push (ref);
76 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
77 for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
78 edges_to_redirect.safe_push (e);
80 if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
82 if (!targetm.has_ifunc_p ())
84 error_at (DECL_SOURCE_LOCATION (node->decl),
85 "the call requires ifunc, which is not"
86 " supported by this target");
87 return;
89 else if (!targetm.get_function_versions_dispatcher)
91 error_at (DECL_SOURCE_LOCATION (node->decl),
92 "target does not support function version dispatcher");
93 return;
96 tree idecl = targetm.get_function_versions_dispatcher (node->decl);
97 if (!idecl)
99 error_at (DECL_SOURCE_LOCATION (node->decl),
100 "default target_clones attribute was not set");
101 return;
104 cgraph_node *inode = cgraph_node::get (idecl);
105 gcc_assert (inode);
106 tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
108 /* Update aliases. */
109 inode->alias = true;
110 inode->alias_target = resolver_decl;
111 if (!inode->analyzed)
112 inode->resolve_alias (cgraph_node::get (resolver_decl));
114 /* Redirect edges. */
115 unsigned i;
116 cgraph_edge *e;
117 FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
119 e->redirect_callee (inode);
120 e->redirect_call_stmt_to_callee ();
123 /* Redirect references. */
124 FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
126 if (ref->use == IPA_REF_ADDR)
128 struct walk_stmt_info wi;
129 memset (&wi, 0, sizeof (wi));
130 wi.info = (void *)node->function_version ();
132 if (dyn_cast<varpool_node *> (ref->referring))
134 hash_set<tree> visited_nodes;
135 walk_tree (&DECL_INITIAL (ref->referring->decl),
136 replace_function_decl, &wi, &visited_nodes);
138 else
140 gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
141 if (ref->referring->decl != resolver_decl)
142 walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
145 else
146 gcc_unreachable ();
150 symtab->change_decl_assembler_name (node->decl,
151 clone_function_name (node->decl,
152 "default"));
155 /* Return length of attribute names string,
156 if arglist chain > 1, -1 otherwise. */
158 static int
159 get_attr_len (tree arglist)
161 tree arg;
162 int str_len_sum = 0;
163 int argnum = 0;
165 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
167 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
168 size_t len = strlen (str);
169 str_len_sum += len + 1;
170 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
171 argnum++;
172 argnum++;
174 if (argnum <= 1)
175 return -1;
176 return str_len_sum;
179 /* Create string with attributes separated by comma.
180 Return number of attributes. */
182 static int
183 get_attr_str (tree arglist, char *attr_str)
185 tree arg;
186 size_t str_len_sum = 0;
187 int argnum = 0;
189 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
191 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
192 size_t len = strlen (str);
193 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
194 argnum++;
195 memcpy (attr_str + str_len_sum, str, len);
196 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
197 str_len_sum += len + 1;
198 argnum++;
200 return argnum;
203 /* Return number of attributes separated by comma and put them into ARGS.
204 If there is no DEFAULT attribute return -1. */
206 static int
207 separate_attrs (char *attr_str, char **attrs)
209 int i = 0;
210 bool has_default = false;
212 for (char *attr = strtok (attr_str, ",");
213 attr != NULL; attr = strtok (NULL, ","))
215 if (strcmp (attr, "default") == 0)
217 has_default = true;
218 continue;
220 attrs[i++] = attr;
222 if (!has_default)
223 return -1;
224 return i;
227 /* Return true if symbol is valid in assembler name. */
229 static bool
230 is_valid_asm_symbol (char c)
232 if ('a' <= c && c <= 'z')
233 return true;
234 if ('A' <= c && c <= 'Z')
235 return true;
236 if ('0' <= c && c <= '9')
237 return true;
238 if (c == '_')
239 return true;
240 return false;
243 /* Replace all not valid assembler symbols with '_'. */
245 static void
246 create_new_asm_name (char *old_asm_name, char *new_asm_name)
248 int i;
249 int old_name_len = strlen (old_asm_name);
251 /* Replace all not valid assembler symbols with '_'. */
252 for (i = 0; i < old_name_len; i++)
253 if (!is_valid_asm_symbol (old_asm_name[i]))
254 new_asm_name[i] = '_';
255 else
256 new_asm_name[i] = old_asm_name[i];
257 new_asm_name[old_name_len] = '\0';
260 /* Creates target clone of NODE. */
262 static cgraph_node *
263 create_target_clone (cgraph_node *node, bool definition, char *name)
265 cgraph_node *new_node;
267 if (definition)
269 new_node = node->create_version_clone_with_body (vNULL, NULL,
270 NULL, false,
271 NULL, NULL,
272 name);
273 new_node->force_output = true;
275 else
277 tree new_decl = copy_node (node->decl);
278 new_node = cgraph_node::get_create (new_decl);
279 /* Generate a new name for the new version. */
280 symtab->change_decl_assembler_name (new_node->decl,
281 clone_function_name (node->decl,
282 name));
284 return new_node;
287 /* If the function in NODE has multiple target attributes
288 create the appropriate clone for each valid target attribute. */
290 static bool
291 expand_target_clones (struct cgraph_node *node, bool definition)
293 int i;
294 /* Parsing target attributes separated by comma. */
295 tree attr_target = lookup_attribute ("target_clones",
296 DECL_ATTRIBUTES (node->decl));
297 /* No targets specified. */
298 if (!attr_target)
299 return false;
301 tree arglist = TREE_VALUE (attr_target);
302 int attr_len = get_attr_len (arglist);
304 /* No need to clone for 1 target attribute. */
305 if (attr_len == -1)
307 warning_at (DECL_SOURCE_LOCATION (node->decl),
309 "single target_clones attribute is ignored");
310 return false;
313 char *attr_str = XNEWVEC (char, attr_len);
314 int attrnum = get_attr_str (arglist, attr_str);
315 char **attrs = XNEWVEC (char *, attrnum);
317 attrnum = separate_attrs (attr_str, attrs);
318 if (attrnum == -1)
320 error_at (DECL_SOURCE_LOCATION (node->decl),
321 "default target was not set");
322 XDELETEVEC (attrs);
323 XDELETEVEC (attr_str);
324 return false;
327 cgraph_function_version_info *decl1_v = NULL;
328 cgraph_function_version_info *decl2_v = NULL;
329 cgraph_function_version_info *before = NULL;
330 cgraph_function_version_info *after = NULL;
331 decl1_v = node->function_version ();
332 if (decl1_v == NULL)
333 decl1_v = node->insert_new_function_version ();
334 before = decl1_v;
335 DECL_FUNCTION_VERSIONED (node->decl) = 1;
337 for (i = 0; i < attrnum; i++)
339 char *attr = attrs[i];
340 char *suffix = XNEWVEC (char, strlen (attr) + 1);
342 create_new_asm_name (attr, suffix);
343 /* Create new target clone. */
344 cgraph_node *new_node = create_target_clone (node, definition, suffix);
345 new_node->local.local = false;
346 XDELETEVEC (suffix);
348 /* Set new attribute for the clone. */
349 tree attributes = make_attribute ("target", attr,
350 DECL_ATTRIBUTES (new_node->decl));
351 DECL_ATTRIBUTES (new_node->decl) = attributes;
352 location_t saved_loc = input_location;
353 input_location = DECL_SOURCE_LOCATION (node->decl);
354 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
355 TREE_VALUE (attributes),
357 return false;
359 input_location = saved_loc;
360 decl2_v = new_node->function_version ();
361 if (decl2_v != NULL)
362 continue;
363 decl2_v = new_node->insert_new_function_version ();
365 /* Chain decl2_v and decl1_v. All semantically identical versions
366 will be chained together. */
367 after = decl2_v;
368 while (before->next != NULL)
369 before = before->next;
370 while (after->prev != NULL)
371 after = after->prev;
373 before->next = after;
374 after->prev = before;
375 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
378 XDELETEVEC (attrs);
379 XDELETEVEC (attr_str);
381 /* Setting new attribute to initial function. */
382 tree attributes = make_attribute ("target", "default",
383 DECL_ATTRIBUTES (node->decl));
384 DECL_ATTRIBUTES (node->decl) = attributes;
385 node->local.local = false;
386 location_t saved_loc = input_location;
387 input_location = DECL_SOURCE_LOCATION (node->decl);
388 bool ret
389 = targetm.target_option.valid_attribute_p (node->decl, NULL,
390 TREE_VALUE (attributes), 0);
391 input_location = saved_loc;
392 return ret;
395 static unsigned int
396 ipa_target_clone (void)
398 struct cgraph_node *node;
400 bool target_clone_pass = false;
401 FOR_EACH_FUNCTION (node)
402 target_clone_pass |= expand_target_clones (node, node->definition);
404 if (target_clone_pass)
405 FOR_EACH_FUNCTION (node)
406 create_dispatcher_calls (node);
408 return 0;
411 namespace {
413 const pass_data pass_data_target_clone =
415 SIMPLE_IPA_PASS, /* type */
416 "targetclone", /* name */
417 OPTGROUP_NONE, /* optinfo_flags */
418 TV_NONE, /* tv_id */
419 ( PROP_ssa | PROP_cfg ), /* properties_required */
420 0, /* properties_provided */
421 0, /* properties_destroyed */
422 0, /* todo_flags_start */
423 TODO_update_ssa /* todo_flags_finish */
426 class pass_target_clone : public simple_ipa_opt_pass
428 public:
429 pass_target_clone (gcc::context *ctxt)
430 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
433 /* opt_pass methods: */
434 virtual bool gate (function *);
435 virtual unsigned int execute (function *) { return ipa_target_clone (); }
438 bool
439 pass_target_clone::gate (function *)
441 return true;
444 } // anon namespace
446 simple_ipa_opt_pass *
447 make_pass_target_clone (gcc::context *ctxt)
449 return new pass_target_clone (ctxt);