111
|
1 /* Pass for parsing functions with multiple target attributes.
|
|
2
|
|
3 Contributed by Evgeny Stupachenko <evstupac@gmail.com>
|
|
4
|
131
|
5 Copyright (C) 2015-2018 Free Software Foundation, Inc.
|
111
|
6
|
|
7 This file is part of GCC.
|
|
8
|
|
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.
|
|
13
|
|
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.
|
|
18
|
|
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/>. */
|
|
22
|
|
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"
|
131
|
39 #include "tree-inline.h"
|
|
40 #include "intl.h"
|
111
|
41
|
|
42 /* Walker callback that replaces all FUNCTION_DECL of a function that's
|
|
43 going to be versioned. */
|
|
44
|
|
45 static tree
|
|
46 replace_function_decl (tree *op, int *walk_subtrees, void *data)
|
|
47 {
|
|
48 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
|
|
49 cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
|
|
50
|
|
51 if (TREE_CODE (*op) == FUNCTION_DECL
|
|
52 && info->this_node->decl == *op)
|
|
53 {
|
|
54 *op = info->dispatcher_resolver;
|
|
55 *walk_subtrees = 0;
|
|
56 }
|
|
57
|
|
58 return NULL;
|
|
59 }
|
|
60
|
|
61 /* If the call in NODE has multiple target attribute with multiple fields,
|
|
62 replace it with dispatcher call and create dispatcher (once). */
|
|
63
|
|
64 static void
|
|
65 create_dispatcher_calls (struct cgraph_node *node)
|
|
66 {
|
|
67 ipa_ref *ref;
|
|
68
|
|
69 if (!DECL_FUNCTION_VERSIONED (node->decl)
|
|
70 || !is_function_default_version (node->decl))
|
|
71 return;
|
|
72
|
|
73 if (!targetm.has_ifunc_p ())
|
|
74 {
|
|
75 error_at (DECL_SOURCE_LOCATION (node->decl),
|
|
76 "the call requires ifunc, which is not"
|
|
77 " supported by this target");
|
|
78 return;
|
|
79 }
|
|
80 else if (!targetm.get_function_versions_dispatcher)
|
|
81 {
|
|
82 error_at (DECL_SOURCE_LOCATION (node->decl),
|
|
83 "target does not support function version dispatcher");
|
|
84 return;
|
|
85 }
|
|
86
|
|
87 tree idecl = targetm.get_function_versions_dispatcher (node->decl);
|
|
88 if (!idecl)
|
|
89 {
|
|
90 error_at (DECL_SOURCE_LOCATION (node->decl),
|
131
|
91 "default %<target_clones%> attribute was not set");
|
111
|
92 return;
|
|
93 }
|
|
94
|
|
95 cgraph_node *inode = cgraph_node::get (idecl);
|
|
96 gcc_assert (inode);
|
|
97 tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
|
|
98
|
|
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));
|
|
104
|
|
105 auto_vec<cgraph_edge *> edges_to_redirect;
|
|
106 auto_vec<ipa_ref *> references_to_redirect;
|
|
107
|
|
108 for (unsigned i = 0; node->iterate_referring (i, ref); i++)
|
|
109 references_to_redirect.safe_push (ref);
|
|
110
|
|
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);
|
|
114
|
|
115 if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
|
|
116 {
|
|
117 /* Redirect edges. */
|
|
118 unsigned i;
|
|
119 cgraph_edge *e;
|
|
120 FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
|
|
121 {
|
|
122 e->redirect_callee (inode);
|
|
123 e->redirect_call_stmt_to_callee ();
|
|
124 }
|
|
125
|
|
126 /* Redirect references. */
|
|
127 FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
|
|
128 {
|
|
129 if (ref->use == IPA_REF_ADDR)
|
|
130 {
|
|
131 struct walk_stmt_info wi;
|
|
132 memset (&wi, 0, sizeof (wi));
|
|
133 wi.info = (void *)node->function_version ();
|
|
134
|
|
135 if (dyn_cast<varpool_node *> (ref->referring))
|
|
136 {
|
|
137 hash_set<tree> visited_nodes;
|
|
138 walk_tree (&DECL_INITIAL (ref->referring->decl),
|
|
139 replace_function_decl, &wi, &visited_nodes);
|
|
140 }
|
|
141 else
|
|
142 {
|
|
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);
|
|
146 }
|
131
|
147
|
|
148 symtab_node *source = ref->referring;
|
|
149 ref->remove_reference ();
|
|
150 source->create_reference (inode, IPA_REF_ADDR);
|
|
151 }
|
|
152 else if (ref->use == IPA_REF_ALIAS)
|
|
153 {
|
|
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);
|
111
|
158 }
|
|
159 else
|
|
160 gcc_unreachable ();
|
|
161 }
|
|
162 }
|
|
163
|
|
164 symtab->change_decl_assembler_name (node->decl,
|
|
165 clone_function_name (node->decl,
|
|
166 "default"));
|
131
|
167
|
|
168 /* FIXME: copy of cgraph_node::make_local that should be cleaned up
|
|
169 in next stage1. */
|
|
170 node->make_decl_local ();
|
|
171 node->set_section (NULL);
|
|
172 node->set_comdat_group (NULL);
|
|
173 node->externally_visible = false;
|
|
174 node->forced_by_abi = false;
|
|
175 node->set_section (NULL);
|
|
176 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
|
|
177 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
|
|
178 && !flag_incremental_link);
|
|
179 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
|
|
180
|
|
181 DECL_ARTIFICIAL (node->decl) = 1;
|
|
182 node->force_output = true;
|
111
|
183 }
|
|
184
|
|
185 /* Return length of attribute names string,
|
|
186 if arglist chain > 1, -1 otherwise. */
|
|
187
|
|
188 static int
|
|
189 get_attr_len (tree arglist)
|
|
190 {
|
|
191 tree arg;
|
|
192 int str_len_sum = 0;
|
|
193 int argnum = 0;
|
|
194
|
|
195 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
|
|
196 {
|
|
197 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
|
|
198 size_t len = strlen (str);
|
|
199 str_len_sum += len + 1;
|
|
200 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
|
|
201 argnum++;
|
|
202 argnum++;
|
|
203 }
|
|
204 if (argnum <= 1)
|
|
205 return -1;
|
|
206 return str_len_sum;
|
|
207 }
|
|
208
|
|
209 /* Create string with attributes separated by comma.
|
|
210 Return number of attributes. */
|
|
211
|
|
212 static int
|
|
213 get_attr_str (tree arglist, char *attr_str)
|
|
214 {
|
|
215 tree arg;
|
|
216 size_t str_len_sum = 0;
|
|
217 int argnum = 0;
|
|
218
|
|
219 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
|
|
220 {
|
|
221 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
|
|
222 size_t len = strlen (str);
|
|
223 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
|
|
224 argnum++;
|
|
225 memcpy (attr_str + str_len_sum, str, len);
|
|
226 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
|
|
227 str_len_sum += len + 1;
|
|
228 argnum++;
|
|
229 }
|
|
230 return argnum;
|
|
231 }
|
|
232
|
|
233 /* Return number of attributes separated by comma and put them into ARGS.
|
131
|
234 If there is no DEFAULT attribute return -1. If there is an empty
|
|
235 string in attribute return -2. */
|
111
|
236
|
|
237 static int
|
131
|
238 separate_attrs (char *attr_str, char **attrs, int attrnum)
|
111
|
239 {
|
|
240 int i = 0;
|
131
|
241 int default_count = 0;
|
111
|
242
|
|
243 for (char *attr = strtok (attr_str, ",");
|
|
244 attr != NULL; attr = strtok (NULL, ","))
|
|
245 {
|
|
246 if (strcmp (attr, "default") == 0)
|
|
247 {
|
131
|
248 default_count++;
|
111
|
249 continue;
|
|
250 }
|
|
251 attrs[i++] = attr;
|
|
252 }
|
131
|
253 if (default_count == 0)
|
111
|
254 return -1;
|
131
|
255 else if (i + default_count < attrnum)
|
|
256 return -2;
|
|
257
|
111
|
258 return i;
|
|
259 }
|
|
260
|
|
261 /* Return true if symbol is valid in assembler name. */
|
|
262
|
|
263 static bool
|
|
264 is_valid_asm_symbol (char c)
|
|
265 {
|
|
266 if ('a' <= c && c <= 'z')
|
|
267 return true;
|
|
268 if ('A' <= c && c <= 'Z')
|
|
269 return true;
|
|
270 if ('0' <= c && c <= '9')
|
|
271 return true;
|
|
272 if (c == '_')
|
|
273 return true;
|
|
274 return false;
|
|
275 }
|
|
276
|
|
277 /* Replace all not valid assembler symbols with '_'. */
|
|
278
|
|
279 static void
|
|
280 create_new_asm_name (char *old_asm_name, char *new_asm_name)
|
|
281 {
|
|
282 int i;
|
|
283 int old_name_len = strlen (old_asm_name);
|
|
284
|
|
285 /* Replace all not valid assembler symbols with '_'. */
|
|
286 for (i = 0; i < old_name_len; i++)
|
|
287 if (!is_valid_asm_symbol (old_asm_name[i]))
|
|
288 new_asm_name[i] = '_';
|
|
289 else
|
|
290 new_asm_name[i] = old_asm_name[i];
|
|
291 new_asm_name[old_name_len] = '\0';
|
|
292 }
|
|
293
|
|
294 /* Creates target clone of NODE. */
|
|
295
|
|
296 static cgraph_node *
|
|
297 create_target_clone (cgraph_node *node, bool definition, char *name)
|
|
298 {
|
|
299 cgraph_node *new_node;
|
|
300
|
|
301 if (definition)
|
|
302 {
|
|
303 new_node = node->create_version_clone_with_body (vNULL, NULL,
|
|
304 NULL, false,
|
|
305 NULL, NULL,
|
|
306 name);
|
|
307 new_node->force_output = true;
|
|
308 }
|
|
309 else
|
|
310 {
|
|
311 tree new_decl = copy_node (node->decl);
|
|
312 new_node = cgraph_node::get_create (new_decl);
|
|
313 /* Generate a new name for the new version. */
|
|
314 symtab->change_decl_assembler_name (new_node->decl,
|
|
315 clone_function_name (node->decl,
|
|
316 name));
|
|
317 }
|
|
318 return new_node;
|
|
319 }
|
|
320
|
|
321 /* If the function in NODE has multiple target attributes
|
|
322 create the appropriate clone for each valid target attribute. */
|
|
323
|
|
324 static bool
|
|
325 expand_target_clones (struct cgraph_node *node, bool definition)
|
|
326 {
|
|
327 int i;
|
|
328 /* Parsing target attributes separated by comma. */
|
|
329 tree attr_target = lookup_attribute ("target_clones",
|
|
330 DECL_ATTRIBUTES (node->decl));
|
|
331 /* No targets specified. */
|
|
332 if (!attr_target)
|
|
333 return false;
|
|
334
|
|
335 tree arglist = TREE_VALUE (attr_target);
|
|
336 int attr_len = get_attr_len (arglist);
|
|
337
|
|
338 /* No need to clone for 1 target attribute. */
|
|
339 if (attr_len == -1)
|
|
340 {
|
|
341 warning_at (DECL_SOURCE_LOCATION (node->decl),
|
|
342 0,
|
131
|
343 "single %<target_clones%> attribute is ignored");
|
|
344 return false;
|
|
345 }
|
|
346
|
|
347 if (node->definition
|
|
348 && !tree_versionable_function_p (node->decl))
|
|
349 {
|
|
350 auto_diagnostic_group d;
|
|
351 error_at (DECL_SOURCE_LOCATION (node->decl),
|
|
352 "clones for %<target_clones%> attribute cannot be created");
|
|
353 const char *reason = NULL;
|
|
354 if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
|
|
355 reason = G_("function %q+F can never be copied "
|
|
356 "because it has %<noclone%> attribute");
|
|
357 else
|
|
358 reason = copy_forbidden (DECL_STRUCT_FUNCTION (node->decl));
|
|
359 if (reason)
|
|
360 inform (DECL_SOURCE_LOCATION (node->decl), reason, node->decl);
|
111
|
361 return false;
|
|
362 }
|
|
363
|
|
364 char *attr_str = XNEWVEC (char, attr_len);
|
|
365 int attrnum = get_attr_str (arglist, attr_str);
|
|
366 char **attrs = XNEWVEC (char *, attrnum);
|
|
367
|
131
|
368 attrnum = separate_attrs (attr_str, attrs, attrnum);
|
111
|
369 if (attrnum == -1)
|
|
370 {
|
|
371 error_at (DECL_SOURCE_LOCATION (node->decl),
|
|
372 "default target was not set");
|
|
373 XDELETEVEC (attrs);
|
|
374 XDELETEVEC (attr_str);
|
|
375 return false;
|
|
376 }
|
131
|
377 else if (attrnum == -2)
|
|
378 {
|
|
379 error_at (DECL_SOURCE_LOCATION (node->decl),
|
|
380 "an empty string cannot be in %<target_clones%> attribute");
|
|
381 XDELETEVEC (attrs);
|
|
382 XDELETEVEC (attr_str);
|
|
383 return false;
|
|
384 }
|
111
|
385
|
|
386 cgraph_function_version_info *decl1_v = NULL;
|
|
387 cgraph_function_version_info *decl2_v = NULL;
|
|
388 cgraph_function_version_info *before = NULL;
|
|
389 cgraph_function_version_info *after = NULL;
|
|
390 decl1_v = node->function_version ();
|
|
391 if (decl1_v == NULL)
|
|
392 decl1_v = node->insert_new_function_version ();
|
|
393 before = decl1_v;
|
|
394 DECL_FUNCTION_VERSIONED (node->decl) = 1;
|
|
395
|
|
396 for (i = 0; i < attrnum; i++)
|
|
397 {
|
|
398 char *attr = attrs[i];
|
|
399 char *suffix = XNEWVEC (char, strlen (attr) + 1);
|
|
400
|
|
401 create_new_asm_name (attr, suffix);
|
|
402 /* Create new target clone. */
|
|
403 cgraph_node *new_node = create_target_clone (node, definition, suffix);
|
|
404 new_node->local.local = false;
|
|
405 XDELETEVEC (suffix);
|
|
406
|
|
407 /* Set new attribute for the clone. */
|
|
408 tree attributes = make_attribute ("target", attr,
|
|
409 DECL_ATTRIBUTES (new_node->decl));
|
|
410 DECL_ATTRIBUTES (new_node->decl) = attributes;
|
|
411 location_t saved_loc = input_location;
|
|
412 input_location = DECL_SOURCE_LOCATION (node->decl);
|
|
413 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
|
|
414 TREE_VALUE (attributes),
|
|
415 0))
|
|
416 return false;
|
|
417
|
|
418 input_location = saved_loc;
|
|
419 decl2_v = new_node->function_version ();
|
|
420 if (decl2_v != NULL)
|
|
421 continue;
|
|
422 decl2_v = new_node->insert_new_function_version ();
|
|
423
|
|
424 /* Chain decl2_v and decl1_v. All semantically identical versions
|
|
425 will be chained together. */
|
|
426 after = decl2_v;
|
|
427 while (before->next != NULL)
|
|
428 before = before->next;
|
|
429 while (after->prev != NULL)
|
|
430 after = after->prev;
|
|
431
|
|
432 before->next = after;
|
|
433 after->prev = before;
|
|
434 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
|
|
435 }
|
|
436
|
|
437 XDELETEVEC (attrs);
|
|
438 XDELETEVEC (attr_str);
|
|
439
|
|
440 /* Setting new attribute to initial function. */
|
|
441 tree attributes = make_attribute ("target", "default",
|
|
442 DECL_ATTRIBUTES (node->decl));
|
|
443 DECL_ATTRIBUTES (node->decl) = attributes;
|
|
444 node->local.local = false;
|
|
445 location_t saved_loc = input_location;
|
|
446 input_location = DECL_SOURCE_LOCATION (node->decl);
|
|
447 bool ret
|
|
448 = targetm.target_option.valid_attribute_p (node->decl, NULL,
|
|
449 TREE_VALUE (attributes), 0);
|
|
450 input_location = saved_loc;
|
|
451 return ret;
|
|
452 }
|
|
453
|
131
|
454 /* When NODE is a target clone, consider all callees and redirect
|
|
455 to a clone with equal target attributes. That prevents multiple
|
|
456 multi-versioning dispatches and a call-chain can be optimized. */
|
|
457
|
|
458 static void
|
|
459 redirect_to_specific_clone (cgraph_node *node)
|
|
460 {
|
|
461 cgraph_function_version_info *fv = node->function_version ();
|
|
462 if (fv == NULL)
|
|
463 return;
|
|
464
|
|
465 tree attr_target = lookup_attribute ("target", DECL_ATTRIBUTES (node->decl));
|
|
466 if (attr_target == NULL_TREE)
|
|
467 return;
|
|
468
|
|
469 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
|
|
470 for (cgraph_edge *e = node->callees; e ; e = e->next_callee)
|
|
471 {
|
|
472 cgraph_function_version_info *fv2 = e->callee->function_version ();
|
|
473 if (!fv2)
|
|
474 continue;
|
|
475
|
|
476 tree attr_target2 = lookup_attribute ("target",
|
|
477 DECL_ATTRIBUTES (e->callee->decl));
|
|
478
|
|
479 /* Function is not calling proper target clone. */
|
|
480 if (!attribute_list_equal (attr_target, attr_target2))
|
|
481 {
|
|
482 while (fv2->prev != NULL)
|
|
483 fv2 = fv2->prev;
|
|
484
|
|
485 /* Try to find a clone with equal target attribute. */
|
|
486 for (; fv2 != NULL; fv2 = fv2->next)
|
|
487 {
|
|
488 cgraph_node *callee = fv2->this_node;
|
|
489 attr_target2 = lookup_attribute ("target",
|
|
490 DECL_ATTRIBUTES (callee->decl));
|
|
491 if (attribute_list_equal (attr_target, attr_target2))
|
|
492 {
|
|
493 e->redirect_callee (callee);
|
|
494 e->redirect_call_stmt_to_callee ();
|
|
495 break;
|
|
496 }
|
|
497 }
|
|
498 }
|
|
499 }
|
|
500 }
|
|
501
|
111
|
502 static unsigned int
|
|
503 ipa_target_clone (void)
|
|
504 {
|
|
505 struct cgraph_node *node;
|
131
|
506 auto_vec<cgraph_node *> to_dispatch;
|
111
|
507
|
|
508 FOR_EACH_FUNCTION (node)
|
131
|
509 if (expand_target_clones (node, node->definition))
|
|
510 to_dispatch.safe_push (node);
|
111
|
511
|
131
|
512 for (unsigned i = 0; i < to_dispatch.length (); i++)
|
|
513 create_dispatcher_calls (to_dispatch[i]);
|
|
514
|
|
515 FOR_EACH_FUNCTION (node)
|
|
516 redirect_to_specific_clone (node);
|
111
|
517
|
|
518 return 0;
|
|
519 }
|
|
520
|
|
521 namespace {
|
|
522
|
|
523 const pass_data pass_data_target_clone =
|
|
524 {
|
|
525 SIMPLE_IPA_PASS, /* type */
|
|
526 "targetclone", /* name */
|
|
527 OPTGROUP_NONE, /* optinfo_flags */
|
|
528 TV_NONE, /* tv_id */
|
|
529 ( PROP_ssa | PROP_cfg ), /* properties_required */
|
|
530 0, /* properties_provided */
|
|
531 0, /* properties_destroyed */
|
|
532 0, /* todo_flags_start */
|
|
533 TODO_update_ssa /* todo_flags_finish */
|
|
534 };
|
|
535
|
|
536 class pass_target_clone : public simple_ipa_opt_pass
|
|
537 {
|
|
538 public:
|
|
539 pass_target_clone (gcc::context *ctxt)
|
|
540 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
|
|
541 {}
|
|
542
|
|
543 /* opt_pass methods: */
|
|
544 virtual bool gate (function *);
|
|
545 virtual unsigned int execute (function *) { return ipa_target_clone (); }
|
|
546 };
|
|
547
|
|
548 bool
|
|
549 pass_target_clone::gate (function *)
|
|
550 {
|
|
551 return true;
|
|
552 }
|
|
553
|
|
554 } // anon namespace
|
|
555
|
|
556 simple_ipa_opt_pass *
|
|
557 make_pass_target_clone (gcc::context *ctxt)
|
|
558 {
|
|
559 return new pass_target_clone (ctxt);
|
|
560 }
|