PR target/76731
[official-gcc.git] / gcc / multiple_target.c
blob5be3980db201b0453ce317a993e48be286c5b2ff
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"
38 /* If the call in NODE has multiple target attribute with multiple fields,
39 replace it with dispatcher call and create dispatcher (once). */
41 static void
42 create_dispatcher_calls (struct cgraph_node *node)
44 cgraph_edge *e;
45 cgraph_edge *e_next = NULL;
47 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
48 for (e = node->callers; e ;e = (e == NULL) ? e_next : e->next_caller)
50 tree resolver_decl;
51 tree idecl;
52 tree decl;
53 gimple *call = e->call_stmt;
54 struct cgraph_node *inode;
56 /* Checking if call of function is call of versioned function.
57 Versioned function are not inlined, so there is no need to
58 check for inline. */
59 if (!call
60 || !(decl = gimple_call_fndecl (call))
61 || !DECL_FUNCTION_VERSIONED (decl))
62 continue;
64 if (!targetm.has_ifunc_p ())
66 error_at (gimple_location (call),
67 "the call requires ifunc, which is not"
68 " supported by this target");
69 break;
71 e_next = e->next_caller;
72 idecl = targetm.get_function_versions_dispatcher (decl);
73 if (!idecl)
75 error_at (gimple_location (call),
76 "default target_clones attribute was not set");
77 break;
79 inode = cgraph_node::get (idecl);
80 gcc_assert (inode);
81 resolver_decl = targetm.generate_version_dispatcher_body (inode);
83 /* Update aliases. */
84 inode->alias = true;
85 inode->alias_target = resolver_decl;
86 if (!inode->analyzed)
87 inode->resolve_alias (cgraph_node::get (resolver_decl));
89 e->redirect_callee (inode);
90 /* Since REDIRECT_CALLEE modifies NEXT_CALLER field we move to
91 previously set NEXT_CALLER. */
92 e = NULL;
96 /* Return length of attribute names string,
97 if arglist chain > 1, -1 otherwise. */
99 static int
100 get_attr_len (tree arglist)
102 tree arg;
103 int str_len_sum = 0;
104 int argnum = 0;
106 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
108 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
109 size_t len = strlen (str);
110 str_len_sum += len + 1;
111 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
112 argnum++;
113 argnum++;
115 if (argnum <= 1)
116 return -1;
117 return str_len_sum;
120 /* Create string with attributes separated by comma.
121 Return number of attributes. */
123 static int
124 get_attr_str (tree arglist, char *attr_str)
126 tree arg;
127 size_t str_len_sum = 0;
128 int argnum = 0;
130 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
132 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
133 size_t len = strlen (str);
134 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
135 argnum++;
136 memcpy (attr_str + str_len_sum, str, len);
137 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
138 str_len_sum += len + 1;
139 argnum++;
141 return argnum;
144 /* Return number of attributes separated by comma and put them into ARGS.
145 If there is no DEFAULT attribute return -1. */
147 static int
148 separate_attrs (char *attr_str, char **attrs)
150 int i = 0;
151 bool has_default = false;
153 for (char *attr = strtok (attr_str, ",");
154 attr != NULL; attr = strtok (NULL, ","))
156 if (strcmp (attr, "default") == 0)
158 has_default = true;
159 continue;
161 attrs[i++] = attr;
163 if (!has_default)
164 return -1;
165 return i;
168 /* Return true if symbol is valid in assembler name. */
170 static bool
171 is_valid_asm_symbol (char c)
173 if ('a' <= c && c <= 'z')
174 return true;
175 if ('A' <= c && c <= 'Z')
176 return true;
177 if ('0' <= c && c <= '9')
178 return true;
179 if (c == '_')
180 return true;
181 return false;
184 /* Replace all not valid assembler symbols with '_'. */
186 static void
187 create_new_asm_name (char *old_asm_name, char *new_asm_name)
189 int i;
190 int old_name_len = strlen (old_asm_name);
192 /* Replace all not valid assembler symbols with '_'. */
193 for (i = 0; i < old_name_len; i++)
194 if (!is_valid_asm_symbol (old_asm_name[i]))
195 new_asm_name[i] = '_';
196 else
197 new_asm_name[i] = old_asm_name[i];
198 new_asm_name[old_name_len] = '\0';
201 /* Creates target clone of NODE. */
203 static cgraph_node *
204 create_target_clone (cgraph_node *node, bool definition, char *name)
206 cgraph_node *new_node;
208 if (definition)
210 new_node = node->create_version_clone_with_body (vNULL, NULL,
211 NULL, false,
212 NULL, NULL,
213 name);
214 new_node->force_output = true;
216 else
218 tree new_decl = copy_node (node->decl);
219 new_node = cgraph_node::get_create (new_decl);
220 /* Generate a new name for the new version. */
221 symtab->change_decl_assembler_name (new_node->decl,
222 clone_function_name (node->decl,
223 name));
225 return new_node;
228 /* If the function in NODE has multiple target attributes
229 create the appropriate clone for each valid target attribute. */
231 static bool
232 expand_target_clones (struct cgraph_node *node, bool definition)
234 int i;
235 /* Parsing target attributes separated by comma. */
236 tree attr_target = lookup_attribute ("target_clones",
237 DECL_ATTRIBUTES (node->decl));
238 /* No targets specified. */
239 if (!attr_target)
240 return false;
242 tree arglist = TREE_VALUE (attr_target);
243 int attr_len = get_attr_len (arglist);
245 /* No need to clone for 1 target attribute. */
246 if (attr_len == -1)
248 warning_at (DECL_SOURCE_LOCATION (node->decl),
250 "single target_clones attribute is ignored");
251 return false;
254 char *attr_str = XNEWVEC (char, attr_len);
255 int attrnum = get_attr_str (arglist, attr_str);
256 char **attrs = XNEWVEC (char *, attrnum);
258 attrnum = separate_attrs (attr_str, attrs);
259 if (attrnum == -1)
261 error_at (DECL_SOURCE_LOCATION (node->decl),
262 "default target was not set");
263 XDELETEVEC (attrs);
264 XDELETEVEC (attr_str);
265 return false;
268 cgraph_function_version_info *decl1_v = NULL;
269 cgraph_function_version_info *decl2_v = NULL;
270 cgraph_function_version_info *before = NULL;
271 cgraph_function_version_info *after = NULL;
272 decl1_v = node->function_version ();
273 if (decl1_v == NULL)
274 decl1_v = node->insert_new_function_version ();
275 before = decl1_v;
276 DECL_FUNCTION_VERSIONED (node->decl) = 1;
278 for (i = 0; i < attrnum; i++)
280 char *attr = attrs[i];
281 char *suffix = XNEWVEC (char, strlen (attr) + 1);
283 create_new_asm_name (attr, suffix);
284 /* Create new target clone. */
285 cgraph_node *new_node = create_target_clone (node, definition, suffix);
286 XDELETEVEC (suffix);
288 /* Set new attribute for the clone. */
289 tree attributes = make_attribute ("target", attr,
290 DECL_ATTRIBUTES (new_node->decl));
291 DECL_ATTRIBUTES (new_node->decl) = attributes;
292 location_t saved_loc = input_location;
293 input_location = DECL_SOURCE_LOCATION (node->decl);
294 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
295 TREE_VALUE (attributes),
298 input_location = saved_loc;
299 continue;
302 input_location = saved_loc;
303 decl2_v = new_node->function_version ();
304 if (decl2_v != NULL)
305 continue;
306 decl2_v = new_node->insert_new_function_version ();
308 /* Chain decl2_v and decl1_v. All semantically identical versions
309 will be chained together. */
310 after = decl2_v;
311 while (before->next != NULL)
312 before = before->next;
313 while (after->prev != NULL)
314 after = after->prev;
316 before->next = after;
317 after->prev = before;
318 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
321 XDELETEVEC (attrs);
322 XDELETEVEC (attr_str);
324 /* Setting new attribute to initial function. */
325 tree attributes = make_attribute ("target", "default",
326 DECL_ATTRIBUTES (node->decl));
327 DECL_ATTRIBUTES (node->decl) = attributes;
328 location_t saved_loc = input_location;
329 input_location = DECL_SOURCE_LOCATION (node->decl);
330 bool ret
331 = targetm.target_option.valid_attribute_p (node->decl, NULL,
332 TREE_VALUE (attributes), 0);
333 input_location = saved_loc;
334 return ret;
337 static bool target_clone_pass;
339 static unsigned int
340 ipa_target_clone (void)
342 struct cgraph_node *node;
344 target_clone_pass = false;
345 FOR_EACH_FUNCTION (node)
346 if (node->definition)
347 target_clone_pass |= expand_target_clones (node, true);
348 return 0;
351 namespace {
353 const pass_data pass_data_target_clone =
355 SIMPLE_IPA_PASS, /* type */
356 "targetclone", /* name */
357 OPTGROUP_NONE, /* optinfo_flags */
358 TV_NONE, /* tv_id */
359 ( PROP_ssa | PROP_cfg ), /* properties_required */
360 0, /* properties_provided */
361 0, /* properties_destroyed */
362 0, /* todo_flags_start */
363 0 /* todo_flags_finish */
366 class pass_target_clone : public simple_ipa_opt_pass
368 public:
369 pass_target_clone (gcc::context *ctxt)
370 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
373 /* opt_pass methods: */
374 virtual bool gate (function *);
375 virtual unsigned int execute (function *) { return ipa_target_clone (); }
378 bool
379 pass_target_clone::gate (function *)
381 return true;
384 } // anon namespace
386 simple_ipa_opt_pass *
387 make_pass_target_clone (gcc::context *ctxt)
389 return new pass_target_clone (ctxt);
392 static unsigned int
393 ipa_dispatcher_calls (void)
395 struct cgraph_node *node;
397 FOR_EACH_FUNCTION (node)
398 if (!node->definition)
399 target_clone_pass |= expand_target_clones (node, false);
400 if (target_clone_pass)
401 FOR_EACH_FUNCTION (node)
402 create_dispatcher_calls (node);
403 return 0;
406 namespace {
408 const pass_data pass_data_dispatcher_calls =
410 SIMPLE_IPA_PASS, /* type */
411 "dispatchercalls", /* name */
412 OPTGROUP_NONE, /* optinfo_flags */
413 TV_NONE, /* tv_id */
414 ( PROP_ssa | PROP_cfg ), /* properties_required */
415 0, /* properties_provided */
416 0, /* properties_destroyed */
417 0, /* todo_flags_start */
418 0 /* todo_flags_finish */
421 class pass_dispatcher_calls : public simple_ipa_opt_pass
423 public:
424 pass_dispatcher_calls (gcc::context *ctxt)
425 : simple_ipa_opt_pass (pass_data_dispatcher_calls, ctxt)
428 /* opt_pass methods: */
429 virtual bool gate (function *);
430 virtual unsigned int execute (function *) { return ipa_dispatcher_calls (); }
433 bool
434 pass_dispatcher_calls::gate (function *)
436 return true;
439 } // anon namespace
441 simple_ipa_opt_pass *
442 make_pass_dispatcher_calls (gcc::context *ctxt)
444 return new pass_dispatcher_calls (ctxt);