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
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"
40 /* Walker callback that replaces all FUNCTION_DECL of a function that's
41 going to be versioned. */
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
;
59 /* If the call in NODE has multiple target attribute with multiple fields,
60 replace it with dispatcher call and create dispatcher (once). */
63 create_dispatcher_calls (struct cgraph_node
*node
)
67 if (!DECL_FUNCTION_VERSIONED (node
->decl
))
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");
89 else if (!targetm
.get_function_versions_dispatcher
)
91 error_at (DECL_SOURCE_LOCATION (node
->decl
),
92 "target does not support function version dispatcher");
96 tree idecl
= targetm
.get_function_versions_dispatcher (node
->decl
);
99 error_at (DECL_SOURCE_LOCATION (node
->decl
),
100 "default target_clones attribute was not set");
104 cgraph_node
*inode
= cgraph_node::get (idecl
);
106 tree resolver_decl
= targetm
.generate_version_dispatcher_body (inode
);
108 /* Update aliases. */
110 inode
->alias_target
= resolver_decl
;
111 if (!inode
->analyzed
)
112 inode
->resolve_alias (cgraph_node::get (resolver_decl
));
114 /* Redirect edges. */
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
);
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
);
150 symtab
->change_decl_assembler_name (node
->decl
,
151 clone_function_name (node
->decl
,
155 /* Return length of attribute names string,
156 if arglist chain > 1, -1 otherwise. */
159 get_attr_len (tree arglist
)
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, ','))
179 /* Create string with attributes separated by comma.
180 Return number of attributes. */
183 get_attr_str (tree arglist
, char *attr_str
)
186 size_t str_len_sum
= 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, ','))
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;
203 /* Return number of attributes separated by comma and put them into ARGS.
204 If there is no DEFAULT attribute return -1. */
207 separate_attrs (char *attr_str
, char **attrs
)
210 bool has_default
= false;
212 for (char *attr
= strtok (attr_str
, ",");
213 attr
!= NULL
; attr
= strtok (NULL
, ","))
215 if (strcmp (attr
, "default") == 0)
227 /* Return true if symbol is valid in assembler name. */
230 is_valid_asm_symbol (char c
)
232 if ('a' <= c
&& c
<= 'z')
234 if ('A' <= c
&& c
<= 'Z')
236 if ('0' <= c
&& c
<= '9')
243 /* Replace all not valid assembler symbols with '_'. */
246 create_new_asm_name (char *old_asm_name
, char *new_asm_name
)
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
] = '_';
256 new_asm_name
[i
] = old_asm_name
[i
];
257 new_asm_name
[old_name_len
] = '\0';
260 /* Creates target clone of NODE. */
263 create_target_clone (cgraph_node
*node
, bool definition
, char *name
)
265 cgraph_node
*new_node
;
269 new_node
= node
->create_version_clone_with_body (vNULL
, NULL
,
273 new_node
->force_output
= true;
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
,
287 /* If the function in NODE has multiple target attributes
288 create the appropriate clone for each valid target attribute. */
291 expand_target_clones (struct cgraph_node
*node
, bool definition
)
294 /* Parsing target attributes separated by comma. */
295 tree attr_target
= lookup_attribute ("target_clones",
296 DECL_ATTRIBUTES (node
->decl
));
297 /* No targets specified. */
301 tree arglist
= TREE_VALUE (attr_target
);
302 int attr_len
= get_attr_len (arglist
);
304 /* No need to clone for 1 target attribute. */
307 warning_at (DECL_SOURCE_LOCATION (node
->decl
),
309 "single target_clones attribute is ignored");
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
);
320 error_at (DECL_SOURCE_LOCATION (node
->decl
),
321 "default target was not set");
323 XDELETEVEC (attr_str
);
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 ();
333 decl1_v
= node
->insert_new_function_version ();
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;
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
),
359 input_location
= saved_loc
;
360 decl2_v
= new_node
->function_version ();
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. */
368 while (before
->next
!= NULL
)
369 before
= before
->next
;
370 while (after
->prev
!= NULL
)
373 before
->next
= after
;
374 after
->prev
= before
;
375 DECL_FUNCTION_VERSIONED (new_node
->decl
) = 1;
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
);
389 = targetm
.target_option
.valid_attribute_p (node
->decl
, NULL
,
390 TREE_VALUE (attributes
), 0);
391 input_location
= saved_loc
;
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
);
413 const pass_data pass_data_target_clone
=
415 SIMPLE_IPA_PASS
, /* type */
416 "targetclone", /* name */
417 OPTGROUP_NONE
, /* optinfo_flags */
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
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 (); }
439 pass_target_clone::gate (function
*)
446 simple_ipa_opt_pass
*
447 make_pass_target_clone (gcc::context
*ctxt
)
449 return new pass_target_clone (ctxt
);