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"
38 /* If the call in NODE has multiple target attribute with multiple fields,
39 replace it with dispatcher call and create dispatcher (once). */
42 create_dispatcher_calls (struct cgraph_node
*node
)
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
)
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
60 || !(decl
= gimple_call_fndecl (call
))
61 || !DECL_FUNCTION_VERSIONED (decl
))
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");
71 else if (!targetm
.get_function_versions_dispatcher
)
73 error_at (gimple_location (call
),
74 "target does not support function version dispatcher");
78 e_next
= e
->next_caller
;
79 idecl
= targetm
.get_function_versions_dispatcher (decl
);
82 error_at (gimple_location (call
),
83 "default target_clones attribute was not set");
86 inode
= cgraph_node::get (idecl
);
88 resolver_decl
= targetm
.generate_version_dispatcher_body (inode
);
92 inode
->alias_target
= resolver_decl
;
94 inode
->resolve_alias (cgraph_node::get (resolver_decl
));
96 e
->redirect_callee (inode
);
97 e
->redirect_call_stmt_to_callee ();
98 /* Since REDIRECT_CALLEE modifies NEXT_CALLER field we move to
99 previously set NEXT_CALLER. */
104 /* Return length of attribute names string,
105 if arglist chain > 1, -1 otherwise. */
108 get_attr_len (tree arglist
)
114 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
116 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
117 size_t len
= strlen (str
);
118 str_len_sum
+= len
+ 1;
119 for (const char *p
= strchr (str
, ','); p
; p
= strchr (p
+ 1, ','))
128 /* Create string with attributes separated by comma.
129 Return number of attributes. */
132 get_attr_str (tree arglist
, char *attr_str
)
135 size_t str_len_sum
= 0;
138 for (arg
= arglist
; arg
; arg
= TREE_CHAIN (arg
))
140 const char *str
= TREE_STRING_POINTER (TREE_VALUE (arg
));
141 size_t len
= strlen (str
);
142 for (const char *p
= strchr (str
, ','); p
; p
= strchr (p
+ 1, ','))
144 memcpy (attr_str
+ str_len_sum
, str
, len
);
145 attr_str
[str_len_sum
+ len
] = TREE_CHAIN (arg
) ? ',' : '\0';
146 str_len_sum
+= len
+ 1;
152 /* Return number of attributes separated by comma and put them into ARGS.
153 If there is no DEFAULT attribute return -1. */
156 separate_attrs (char *attr_str
, char **attrs
)
159 bool has_default
= false;
161 for (char *attr
= strtok (attr_str
, ",");
162 attr
!= NULL
; attr
= strtok (NULL
, ","))
164 if (strcmp (attr
, "default") == 0)
176 /* Return true if symbol is valid in assembler name. */
179 is_valid_asm_symbol (char c
)
181 if ('a' <= c
&& c
<= 'z')
183 if ('A' <= c
&& c
<= 'Z')
185 if ('0' <= c
&& c
<= '9')
192 /* Replace all not valid assembler symbols with '_'. */
195 create_new_asm_name (char *old_asm_name
, char *new_asm_name
)
198 int old_name_len
= strlen (old_asm_name
);
200 /* Replace all not valid assembler symbols with '_'. */
201 for (i
= 0; i
< old_name_len
; i
++)
202 if (!is_valid_asm_symbol (old_asm_name
[i
]))
203 new_asm_name
[i
] = '_';
205 new_asm_name
[i
] = old_asm_name
[i
];
206 new_asm_name
[old_name_len
] = '\0';
209 /* Creates target clone of NODE. */
212 create_target_clone (cgraph_node
*node
, bool definition
, char *name
)
214 cgraph_node
*new_node
;
218 new_node
= node
->create_version_clone_with_body (vNULL
, NULL
,
222 new_node
->force_output
= true;
226 tree new_decl
= copy_node (node
->decl
);
227 new_node
= cgraph_node::get_create (new_decl
);
228 /* Generate a new name for the new version. */
229 symtab
->change_decl_assembler_name (new_node
->decl
,
230 clone_function_name (node
->decl
,
236 /* If the function in NODE has multiple target attributes
237 create the appropriate clone for each valid target attribute. */
240 expand_target_clones (struct cgraph_node
*node
, bool definition
)
243 /* Parsing target attributes separated by comma. */
244 tree attr_target
= lookup_attribute ("target_clones",
245 DECL_ATTRIBUTES (node
->decl
));
246 /* No targets specified. */
250 tree arglist
= TREE_VALUE (attr_target
);
251 int attr_len
= get_attr_len (arglist
);
253 /* No need to clone for 1 target attribute. */
256 warning_at (DECL_SOURCE_LOCATION (node
->decl
),
258 "single target_clones attribute is ignored");
262 char *attr_str
= XNEWVEC (char, attr_len
);
263 int attrnum
= get_attr_str (arglist
, attr_str
);
264 char **attrs
= XNEWVEC (char *, attrnum
);
266 attrnum
= separate_attrs (attr_str
, attrs
);
269 error_at (DECL_SOURCE_LOCATION (node
->decl
),
270 "default target was not set");
272 XDELETEVEC (attr_str
);
276 cgraph_function_version_info
*decl1_v
= NULL
;
277 cgraph_function_version_info
*decl2_v
= NULL
;
278 cgraph_function_version_info
*before
= NULL
;
279 cgraph_function_version_info
*after
= NULL
;
280 decl1_v
= node
->function_version ();
282 decl1_v
= node
->insert_new_function_version ();
284 DECL_FUNCTION_VERSIONED (node
->decl
) = 1;
286 for (i
= 0; i
< attrnum
; i
++)
288 char *attr
= attrs
[i
];
289 char *suffix
= XNEWVEC (char, strlen (attr
) + 1);
291 create_new_asm_name (attr
, suffix
);
292 /* Create new target clone. */
293 cgraph_node
*new_node
= create_target_clone (node
, definition
, suffix
);
294 new_node
->local
.local
= false;
297 /* Set new attribute for the clone. */
298 tree attributes
= make_attribute ("target", attr
,
299 DECL_ATTRIBUTES (new_node
->decl
));
300 DECL_ATTRIBUTES (new_node
->decl
) = attributes
;
301 location_t saved_loc
= input_location
;
302 input_location
= DECL_SOURCE_LOCATION (node
->decl
);
303 if (!targetm
.target_option
.valid_attribute_p (new_node
->decl
, NULL
,
304 TREE_VALUE (attributes
),
308 input_location
= saved_loc
;
309 decl2_v
= new_node
->function_version ();
312 decl2_v
= new_node
->insert_new_function_version ();
314 /* Chain decl2_v and decl1_v. All semantically identical versions
315 will be chained together. */
317 while (before
->next
!= NULL
)
318 before
= before
->next
;
319 while (after
->prev
!= NULL
)
322 before
->next
= after
;
323 after
->prev
= before
;
324 DECL_FUNCTION_VERSIONED (new_node
->decl
) = 1;
328 XDELETEVEC (attr_str
);
330 /* Setting new attribute to initial function. */
331 tree attributes
= make_attribute ("target", "default",
332 DECL_ATTRIBUTES (node
->decl
));
333 DECL_ATTRIBUTES (node
->decl
) = attributes
;
334 node
->local
.local
= false;
335 location_t saved_loc
= input_location
;
336 input_location
= DECL_SOURCE_LOCATION (node
->decl
);
338 = targetm
.target_option
.valid_attribute_p (node
->decl
, NULL
,
339 TREE_VALUE (attributes
), 0);
340 input_location
= saved_loc
;
345 ipa_target_clone (void)
347 struct cgraph_node
*node
;
349 bool target_clone_pass
= false;
350 FOR_EACH_FUNCTION (node
)
351 target_clone_pass
|= expand_target_clones (node
, node
->definition
);
353 if (target_clone_pass
)
354 FOR_EACH_FUNCTION (node
)
355 create_dispatcher_calls (node
);
362 const pass_data pass_data_target_clone
=
364 SIMPLE_IPA_PASS
, /* type */
365 "targetclone", /* name */
366 OPTGROUP_NONE
, /* optinfo_flags */
368 ( PROP_ssa
| PROP_cfg
), /* properties_required */
369 0, /* properties_provided */
370 0, /* properties_destroyed */
371 0, /* todo_flags_start */
372 TODO_update_ssa
/* todo_flags_finish */
375 class pass_target_clone
: public simple_ipa_opt_pass
378 pass_target_clone (gcc::context
*ctxt
)
379 : simple_ipa_opt_pass (pass_data_target_clone
, ctxt
)
382 /* opt_pass methods: */
383 virtual bool gate (function
*);
384 virtual unsigned int execute (function
*) { return ipa_target_clone (); }
388 pass_target_clone::gate (function
*)
395 simple_ipa_opt_pass
*
396 make_pass_target_clone (gcc::context
*ctxt
)
398 return new pass_target_clone (ctxt
);