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
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
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/>. */
25 #include "coretypes.h"
28 #include "stringpool.h"
30 #include "diagnostic-core.h"
31 #include "gimple-ssa.h"
33 #include "tree-pass.h"
36 #include "pretty-print.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "tree-inline.h"
42 /* Walker callback that replaces all FUNCTION_DECL of a function that's
43 going to be versioned. */
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
;
61 /* If the call in NODE has multiple target attribute with multiple fields,
62 replace it with dispatcher call and create dispatcher (once). */
65 create_dispatcher_calls (struct cgraph_node
*node
)
69 if (!DECL_FUNCTION_VERSIONED (node
->decl
)
70 || !is_function_default_version (node
->decl
))
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");
80 else if (!targetm
.get_function_versions_dispatcher
)
82 error_at (DECL_SOURCE_LOCATION (node
->decl
),
83 "target does not support function version dispatcher");
87 tree idecl
= targetm
.get_function_versions_dispatcher (node
->decl
);
90 error_at (DECL_SOURCE_LOCATION (node
->decl
),
91 "default %<target_clones%> attribute was not set");
95 cgraph_node
*inode
= cgraph_node::get (idecl
);
97 tree resolver_decl
= targetm
.generate_version_dispatcher_body (inode
);
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. */
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
);
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
);
164 symtab
->change_decl_assembler_name (node
->decl
,
165 clone_function_name (node
->decl
,
168 /* FIXME: copy of cgraph_node::make_local that should be cleaned up
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
;
181 DECL_ARTIFICIAL (node
->decl
) = 1;
182 node
->force_output
= true;
185 /* Return length of attribute names string,
186 if arglist chain > 1, -1 otherwise. */
189 get_attr_len (tree arglist
)
195 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
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, ','))
209 /* Create string with attributes separated by comma.
210 Return number of attributes. */
213 get_attr_str (tree arglist
, char *attr_str
)
216 size_t str_len_sum
= 0;
219 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
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, ','))
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;
233 /* Return number of attributes separated by comma and put them into ARGS.
234 If there is no DEFAULT attribute return -1. If there is an empty
235 string in attribute return -2. */
238 separate_attrs (char *attr_str
, char **attrs
, int attrnum
)
241 int default_count
= 0;
243 for (char *attr
= strtok (attr_str
, ",");
244 attr
!= NULL
; attr
= strtok (NULL
, ","))
246 if (strcmp (attr
, "default") == 0)
253 if (default_count
== 0)
255 else if (i
+ default_count
< attrnum
)
261 /* Return true if symbol is valid in assembler name. */
264 is_valid_asm_symbol (char c
)
266 if ('a' <= c
&& c
<= 'z')
268 if ('A' <= c
&& c
<= 'Z')
270 if ('0' <= c
&& c
<= '9')
277 /* Replace all not valid assembler symbols with '_'. */
280 create_new_asm_name (char *old_asm_name
, char *new_asm_name
)
283 int old_name_len
= strlen (old_asm_name
);
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
] = '_';
290 new_asm_name
[i
] = old_asm_name
[i
];
291 new_asm_name
[old_name_len
] = '\0';
294 /* Creates target clone of NODE. */
297 create_target_clone (cgraph_node
*node
, bool definition
, char *name
)
299 cgraph_node
*new_node
;
303 new_node
= node
->create_version_clone_with_body (vNULL
, NULL
,
307 new_node
->force_output
= true;
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
,
321 /* If the function in NODE has multiple target attributes
322 create the appropriate clone for each valid target attribute. */
325 expand_target_clones (struct cgraph_node
*node
, bool definition
)
328 /* Parsing target attributes separated by comma. */
329 tree attr_target
= lookup_attribute ("target_clones",
330 DECL_ATTRIBUTES (node
->decl
));
331 /* No targets specified. */
335 tree arglist
= TREE_VALUE (attr_target
);
336 int attr_len
= get_attr_len (arglist
);
338 /* No need to clone for 1 target attribute. */
341 warning_at (DECL_SOURCE_LOCATION (node
->decl
),
343 "single %<target_clones%> attribute is ignored");
348 && !tree_versionable_function_p (node
->decl
))
350 error_at (DECL_SOURCE_LOCATION (node
->decl
),
351 "clones for %<target_clones%> attribute cannot be created");
352 const char *reason
= NULL
;
353 if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node
->decl
)))
354 reason
= G_("function %q+F can never be copied "
355 "because it has %<noclone%> attribute");
357 reason
= copy_forbidden (DECL_STRUCT_FUNCTION (node
->decl
));
359 inform (DECL_SOURCE_LOCATION (node
->decl
), reason
, node
->decl
);
363 char *attr_str
= XNEWVEC (char, attr_len
);
364 int attrnum
= get_attr_str (arglist
, attr_str
);
365 char **attrs
= XNEWVEC (char *, attrnum
);
367 attrnum
= separate_attrs (attr_str
, attrs
, attrnum
);
370 error_at (DECL_SOURCE_LOCATION (node
->decl
),
371 "default target was not set");
373 XDELETEVEC (attr_str
);
376 else if (attrnum
== -2)
378 error_at (DECL_SOURCE_LOCATION (node
->decl
),
379 "an empty string cannot be in %<target_clones%> attribute");
381 XDELETEVEC (attr_str
);
385 cgraph_function_version_info
*decl1_v
= NULL
;
386 cgraph_function_version_info
*decl2_v
= NULL
;
387 cgraph_function_version_info
*before
= NULL
;
388 cgraph_function_version_info
*after
= NULL
;
389 decl1_v
= node
->function_version ();
391 decl1_v
= node
->insert_new_function_version ();
393 DECL_FUNCTION_VERSIONED (node
->decl
) = 1;
395 for (i
= 0; i
< attrnum
; i
++)
397 char *attr
= attrs
[i
];
398 char *suffix
= XNEWVEC (char, strlen (attr
) + 1);
400 create_new_asm_name (attr
, suffix
);
401 /* Create new target clone. */
402 cgraph_node
*new_node
= create_target_clone (node
, definition
, suffix
);
403 new_node
->local
.local
= false;
406 /* Set new attribute for the clone. */
407 tree attributes
= make_attribute ("target", attr
,
408 DECL_ATTRIBUTES (new_node
->decl
));
409 DECL_ATTRIBUTES (new_node
->decl
) = attributes
;
410 location_t saved_loc
= input_location
;
411 input_location
= DECL_SOURCE_LOCATION (node
->decl
);
412 if (!targetm
.target_option
.valid_attribute_p (new_node
->decl
, NULL
,
413 TREE_VALUE (attributes
),
417 input_location
= saved_loc
;
418 decl2_v
= new_node
->function_version ();
421 decl2_v
= new_node
->insert_new_function_version ();
423 /* Chain decl2_v and decl1_v. All semantically identical versions
424 will be chained together. */
426 while (before
->next
!= NULL
)
427 before
= before
->next
;
428 while (after
->prev
!= NULL
)
431 before
->next
= after
;
432 after
->prev
= before
;
433 DECL_FUNCTION_VERSIONED (new_node
->decl
) = 1;
437 XDELETEVEC (attr_str
);
439 /* Setting new attribute to initial function. */
440 tree attributes
= make_attribute ("target", "default",
441 DECL_ATTRIBUTES (node
->decl
));
442 DECL_ATTRIBUTES (node
->decl
) = attributes
;
443 node
->local
.local
= false;
444 location_t saved_loc
= input_location
;
445 input_location
= DECL_SOURCE_LOCATION (node
->decl
);
447 = targetm
.target_option
.valid_attribute_p (node
->decl
, NULL
,
448 TREE_VALUE (attributes
), 0);
449 input_location
= saved_loc
;
454 ipa_target_clone (void)
456 struct cgraph_node
*node
;
457 auto_vec
<cgraph_node
*> to_dispatch
;
459 FOR_EACH_FUNCTION (node
)
460 if (expand_target_clones (node
, node
->definition
))
461 to_dispatch
.safe_push (node
);
463 for (unsigned i
= 0; i
< to_dispatch
.length (); i
++)
464 create_dispatcher_calls (to_dispatch
[i
]);
471 const pass_data pass_data_target_clone
=
473 SIMPLE_IPA_PASS
, /* type */
474 "targetclone", /* name */
475 OPTGROUP_NONE
, /* optinfo_flags */
477 ( PROP_ssa
| PROP_cfg
), /* properties_required */
478 0, /* properties_provided */
479 0, /* properties_destroyed */
480 0, /* todo_flags_start */
481 TODO_update_ssa
/* todo_flags_finish */
484 class pass_target_clone
: public simple_ipa_opt_pass
487 pass_target_clone (gcc::context
*ctxt
)
488 : simple_ipa_opt_pass (pass_data_target_clone
, ctxt
)
491 /* opt_pass methods: */
492 virtual bool gate (function
*);
493 virtual unsigned int execute (function
*) { return ipa_target_clone (); }
497 pass_target_clone::gate (function
*)
504 simple_ipa_opt_pass
*
505 make_pass_target_clone (gcc::context
*ctxt
)
507 return new pass_target_clone (ctxt
);