1/* Pass for parsing functions with multiple target attributes.
2
3 Contributed by Evgeny Stupachenko <evstupac@gmail.com>
4
5 Copyright (C) 2015-2017 Free Software Foundation, Inc.
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License
20along 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"
39
40/* Walker callback that replaces all FUNCTION_DECL of a function that's
41 going to be versioned. */
42
43static tree
44replace_function_decl (tree *op, int *walk_subtrees, void *data)
45{
46 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
47 cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
48
49 if (TREE_CODE (*op) == FUNCTION_DECL
50 && info->this_node->decl == *op)
51 {
52 *op = info->dispatcher_resolver;
53 *walk_subtrees = 0;
54 }
55
56 return NULL;
57}
58
59/* If the call in NODE has multiple target attribute with multiple fields,
60 replace it with dispatcher call and create dispatcher (once). */
61
62static void
63create_dispatcher_calls (struct cgraph_node *node)
64{
65 ipa_ref *ref;
66
67 if (!DECL_FUNCTION_VERSIONED (node->decl)
68 || !is_function_default_version (node->decl))
69 return;
70
71 if (!targetm.has_ifunc_p ())
72 {
73 error_at (DECL_SOURCE_LOCATION (node->decl),
74 "the call requires ifunc, which is not"
75 " supported by this target");
76 return;
77 }
78 else if (!targetm.get_function_versions_dispatcher)
79 {
80 error_at (DECL_SOURCE_LOCATION (node->decl),
81 "target does not support function version dispatcher");
82 return;
83 }
84
85 tree idecl = targetm.get_function_versions_dispatcher (node->decl);
86 if (!idecl)
87 {
88 error_at (DECL_SOURCE_LOCATION (node->decl),
89 "default target_clones attribute was not set");
90 return;
91 }
92
93 cgraph_node *inode = cgraph_node::get (idecl);
94 gcc_assert (inode);
95 tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
96
97 /* Update aliases. */
98 inode->alias = true;
99 inode->alias_target = resolver_decl;
100 if (!inode->analyzed)
101 inode->resolve_alias (cgraph_node::get (resolver_decl));
102
103 auto_vec<cgraph_edge *> edges_to_redirect;
104 auto_vec<ipa_ref *> references_to_redirect;
105
106 for (unsigned i = 0; node->iterate_referring (i, ref); i++)
107 references_to_redirect.safe_push (ref);
108
109 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
110 for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
111 edges_to_redirect.safe_push (e);
112
113 if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
114 {
115 /* Redirect edges. */
116 unsigned i;
117 cgraph_edge *e;
118 FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
119 {
120 e->redirect_callee (inode);
121 e->redirect_call_stmt_to_callee ();
122 }
123
124 /* Redirect references. */
125 FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
126 {
127 if (ref->use == IPA_REF_ADDR)
128 {
129 struct walk_stmt_info wi;
130 memset (&wi, 0, sizeof (wi));
131 wi.info = (void *)node->function_version ();
132
133 if (dyn_cast<varpool_node *> (ref->referring))
134 {
135 hash_set<tree> visited_nodes;
136 walk_tree (&DECL_INITIAL (ref->referring->decl),
137 replace_function_decl, &wi, &visited_nodes);
138 }
139 else
140 {
141 gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
142 if (ref->referring->decl != resolver_decl)
143 walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
144 }
145 }
146 else
147 gcc_unreachable ();
148 }
149 }
150
151 TREE_PUBLIC (node->decl) = 0;
152 symtab->change_decl_assembler_name (node->decl,
153 clone_function_name (node->decl,
154 "default"));
155}
156
157/* Return length of attribute names string,
158 if arglist chain > 1, -1 otherwise. */
159
160static int
161get_attr_len (tree arglist)
162{
163 tree arg;
164 int str_len_sum = 0;
165 int argnum = 0;
166
167 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
168 {
169 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
170 size_t len = strlen (str);
171 str_len_sum += len + 1;
172 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
173 argnum++;
174 argnum++;
175 }
176 if (argnum <= 1)
177 return -1;
178 return str_len_sum;
179}
180
181/* Create string with attributes separated by comma.
182 Return number of attributes. */
183
184static int
185get_attr_str (tree arglist, char *attr_str)
186{
187 tree arg;
188 size_t str_len_sum = 0;
189 int argnum = 0;
190
191 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
192 {
193 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
194 size_t len = strlen (str);
195 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
196 argnum++;
197 memcpy (attr_str + str_len_sum, str, len);
198 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
199 str_len_sum += len + 1;
200 argnum++;
201 }
202 return argnum;
203}
204
205/* Return number of attributes separated by comma and put them into ARGS.
206 If there is no DEFAULT attribute return -1. */
207
208static int
209separate_attrs (char *attr_str, char **attrs)
210{
211 int i = 0;
212 bool has_default = false;
213
214 for (char *attr = strtok (attr_str, ",");
215 attr != NULL; attr = strtok (NULL, ","))
216 {
217 if (strcmp (attr, "default") == 0)
218 {
219 has_default = true;
220 continue;
221 }
222 attrs[i++] = attr;
223 }
224 if (!has_default)
225 return -1;
226 return i;
227}
228
229/* Return true if symbol is valid in assembler name. */
230
231static bool
232is_valid_asm_symbol (char c)
233{
234 if ('a' <= c && c <= 'z')
235 return true;
236 if ('A' <= c && c <= 'Z')
237 return true;
238 if ('0' <= c && c <= '9')
239 return true;
240 if (c == '_')
241 return true;
242 return false;
243}
244
245/* Replace all not valid assembler symbols with '_'. */
246
247static void
248create_new_asm_name (char *old_asm_name, char *new_asm_name)
249{
250 int i;
251 int old_name_len = strlen (old_asm_name);
252
253 /* Replace all not valid assembler symbols with '_'. */
254 for (i = 0; i < old_name_len; i++)
255 if (!is_valid_asm_symbol (old_asm_name[i]))
256 new_asm_name[i] = '_';
257 else
258 new_asm_name[i] = old_asm_name[i];
259 new_asm_name[old_name_len] = '\0';
260}
261
262/* Creates target clone of NODE. */
263
264static cgraph_node *
265create_target_clone (cgraph_node *node, bool definition, char *name)
266{
267 cgraph_node *new_node;
268
269 if (definition)
270 {
271 new_node = node->create_version_clone_with_body (vNULL, NULL,
272 NULL, false,
273 NULL, NULL,
274 name);
275 new_node->force_output = true;
276 }
277 else
278 {
279 tree new_decl = copy_node (node->decl);
280 new_node = cgraph_node::get_create (new_decl);
281 /* Generate a new name for the new version. */
282 symtab->change_decl_assembler_name (new_node->decl,
283 clone_function_name (node->decl,
284 name));
285 }
286 return new_node;
287}
288
289/* If the function in NODE has multiple target attributes
290 create the appropriate clone for each valid target attribute. */
291
292static bool
293expand_target_clones (struct cgraph_node *node, bool definition)
294{
295 int i;
296 /* Parsing target attributes separated by comma. */
297 tree attr_target = lookup_attribute ("target_clones",
298 DECL_ATTRIBUTES (node->decl));
299 /* No targets specified. */
300 if (!attr_target)
301 return false;
302
303 tree arglist = TREE_VALUE (attr_target);
304 int attr_len = get_attr_len (arglist);
305
306 /* No need to clone for 1 target attribute. */
307 if (attr_len == -1)
308 {
309 warning_at (DECL_SOURCE_LOCATION (node->decl),
310 0,
311 "single target_clones attribute is ignored");
312 return false;
313 }
314
315 char *attr_str = XNEWVEC (char, attr_len);
316 int attrnum = get_attr_str (arglist, attr_str);
317 char **attrs = XNEWVEC (char *, attrnum);
318
319 attrnum = separate_attrs (attr_str, attrs);
320 if (attrnum == -1)
321 {
322 error_at (DECL_SOURCE_LOCATION (node->decl),
323 "default target was not set");
324 XDELETEVEC (attrs);
325 XDELETEVEC (attr_str);
326 return false;
327 }
328
329 cgraph_function_version_info *decl1_v = NULL;
330 cgraph_function_version_info *decl2_v = NULL;
331 cgraph_function_version_info *before = NULL;
332 cgraph_function_version_info *after = NULL;
333 decl1_v = node->function_version ();
334 if (decl1_v == NULL)
335 decl1_v = node->insert_new_function_version ();
336 before = decl1_v;
337 DECL_FUNCTION_VERSIONED (node->decl) = 1;
338
339 for (i = 0; i < attrnum; i++)
340 {
341 char *attr = attrs[i];
342 char *suffix = XNEWVEC (char, strlen (attr) + 1);
343
344 create_new_asm_name (attr, suffix);
345 /* Create new target clone. */
346 cgraph_node *new_node = create_target_clone (node, definition, suffix);
347 new_node->local.local = false;
348 XDELETEVEC (suffix);
349
350 /* Set new attribute for the clone. */
351 tree attributes = make_attribute ("target", attr,
352 DECL_ATTRIBUTES (new_node->decl));
353 DECL_ATTRIBUTES (new_node->decl) = attributes;
354 location_t saved_loc = input_location;
355 input_location = DECL_SOURCE_LOCATION (node->decl);
356 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
357 TREE_VALUE (attributes),
358 0))
359 return false;
360
361 input_location = saved_loc;
362 decl2_v = new_node->function_version ();
363 if (decl2_v != NULL)
364 continue;
365 decl2_v = new_node->insert_new_function_version ();
366
367 /* Chain decl2_v and decl1_v. All semantically identical versions
368 will be chained together. */
369 after = decl2_v;
370 while (before->next != NULL)
371 before = before->next;
372 while (after->prev != NULL)
373 after = after->prev;
374
375 before->next = after;
376 after->prev = before;
377 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
378 }
379
380 XDELETEVEC (attrs);
381 XDELETEVEC (attr_str);
382
383 /* Setting new attribute to initial function. */
384 tree attributes = make_attribute ("target", "default",
385 DECL_ATTRIBUTES (node->decl));
386 DECL_ATTRIBUTES (node->decl) = attributes;
387 node->local.local = false;
388 location_t saved_loc = input_location;
389 input_location = DECL_SOURCE_LOCATION (node->decl);
390 bool ret
391 = targetm.target_option.valid_attribute_p (node->decl, NULL,
392 TREE_VALUE (attributes), 0);
393 input_location = saved_loc;
394 return ret;
395}
396
397static unsigned int
398ipa_target_clone (void)
399{
400 struct cgraph_node *node;
401
402 bool target_clone_pass = false;
403 FOR_EACH_FUNCTION (node)
404 target_clone_pass |= expand_target_clones (node, node->definition);
405
406 if (target_clone_pass)
407 FOR_EACH_FUNCTION (node)
408 create_dispatcher_calls (node);
409
410 return 0;
411}
412
413namespace {
414
415const pass_data pass_data_target_clone =
416{
417 SIMPLE_IPA_PASS, /* type */
418 "targetclone", /* name */
419 OPTGROUP_NONE, /* optinfo_flags */
420 TV_NONE, /* tv_id */
421 ( PROP_ssa | PROP_cfg ), /* properties_required */
422 0, /* properties_provided */
423 0, /* properties_destroyed */
424 0, /* todo_flags_start */
425 TODO_update_ssa /* todo_flags_finish */
426};
427
428class pass_target_clone : public simple_ipa_opt_pass
429{
430public:
431 pass_target_clone (gcc::context *ctxt)
432 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
433 {}
434
435 /* opt_pass methods: */
436 virtual bool gate (function *);
437 virtual unsigned int execute (function *) { return ipa_target_clone (); }
438};
439
440bool
441pass_target_clone::gate (function *)
442{
443 return true;
444}
445
446} // anon namespace
447
448simple_ipa_opt_pass *
449make_pass_target_clone (gcc::context *ctxt)
450{
451 return new pass_target_clone (ctxt);
452}
453