2017-02-20 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / multiple_target.c
blob7b735ae81ae910876229288cc80f3014bdb20cae
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
12 version.
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.
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/>. */
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"
38 /* If the call in NODE has multiple target attribute with multiple fields,
39 replace it with dispatcher call and create dispatcher (once). */
41 static void
42 create_dispatcher_calls (struct cgraph_node *node)
44 cgraph_edge *e;
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)
50 tree resolver_decl;
51 tree idecl;
52 tree decl;
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
58 check for inline. */
59 if (!call
60 || !(decl = gimple_call_fndecl (call))
61 || !DECL_FUNCTION_VERSIONED (decl))
62 continue;
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");
69 break;
71 e_next = e->next_caller;
72 idecl = targetm.get_function_versions_dispatcher (decl);
73 if (!idecl)
75 error_at (gimple_location (call),
76 "default target_clones attribute was not set");
77 break;
79 inode = cgraph_node::get (idecl);
80 gcc_assert (inode);
81 resolver_decl = targetm.generate_version_dispatcher_body (inode);
83 /* Update aliases. */
84 inode->alias = true;
85 inode->alias_target = resolver_decl;
86 if (!inode->analyzed)
87 inode->resolve_alias (cgraph_node::get (resolver_decl));
89 e->redirect_callee (inode);
90 e->redirect_call_stmt_to_callee ();
91 /* Since REDIRECT_CALLEE modifies NEXT_CALLER field we move to
92 previously set NEXT_CALLER. */
93 e = NULL;
97 /* Return length of attribute names string,
98 if arglist chain > 1, -1 otherwise. */
100 static int
101 get_attr_len (tree arglist)
103 tree arg;
104 int str_len_sum = 0;
105 int argnum = 0;
107 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
109 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
110 size_t len = strlen (str);
111 str_len_sum += len + 1;
112 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
113 argnum++;
114 argnum++;
116 if (argnum <= 1)
117 return -1;
118 return str_len_sum;
121 /* Create string with attributes separated by comma.
122 Return number of attributes. */
124 static int
125 get_attr_str (tree arglist, char *attr_str)
127 tree arg;
128 size_t str_len_sum = 0;
129 int argnum = 0;
131 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
133 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
134 size_t len = strlen (str);
135 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
136 argnum++;
137 memcpy (attr_str + str_len_sum, str, len);
138 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
139 str_len_sum += len + 1;
140 argnum++;
142 return argnum;
145 /* Return number of attributes separated by comma and put them into ARGS.
146 If there is no DEFAULT attribute return -1. */
148 static int
149 separate_attrs (char *attr_str, char **attrs)
151 int i = 0;
152 bool has_default = false;
154 for (char *attr = strtok (attr_str, ",");
155 attr != NULL; attr = strtok (NULL, ","))
157 if (strcmp (attr, "default") == 0)
159 has_default = true;
160 continue;
162 attrs[i++] = attr;
164 if (!has_default)
165 return -1;
166 return i;
169 /* Return true if symbol is valid in assembler name. */
171 static bool
172 is_valid_asm_symbol (char c)
174 if ('a' <= c && c <= 'z')
175 return true;
176 if ('A' <= c && c <= 'Z')
177 return true;
178 if ('0' <= c && c <= '9')
179 return true;
180 if (c == '_')
181 return true;
182 return false;
185 /* Replace all not valid assembler symbols with '_'. */
187 static void
188 create_new_asm_name (char *old_asm_name, char *new_asm_name)
190 int i;
191 int old_name_len = strlen (old_asm_name);
193 /* Replace all not valid assembler symbols with '_'. */
194 for (i = 0; i < old_name_len; i++)
195 if (!is_valid_asm_symbol (old_asm_name[i]))
196 new_asm_name[i] = '_';
197 else
198 new_asm_name[i] = old_asm_name[i];
199 new_asm_name[old_name_len] = '\0';
202 /* Creates target clone of NODE. */
204 static cgraph_node *
205 create_target_clone (cgraph_node *node, bool definition, char *name)
207 cgraph_node *new_node;
209 if (definition)
211 new_node = node->create_version_clone_with_body (vNULL, NULL,
212 NULL, false,
213 NULL, NULL,
214 name);
215 new_node->force_output = true;
217 else
219 tree new_decl = copy_node (node->decl);
220 new_node = cgraph_node::get_create (new_decl);
221 /* Generate a new name for the new version. */
222 symtab->change_decl_assembler_name (new_node->decl,
223 clone_function_name (node->decl,
224 name));
226 return new_node;
229 /* If the function in NODE has multiple target attributes
230 create the appropriate clone for each valid target attribute. */
232 static bool
233 expand_target_clones (struct cgraph_node *node, bool definition)
235 int i;
236 /* Parsing target attributes separated by comma. */
237 tree attr_target = lookup_attribute ("target_clones",
238 DECL_ATTRIBUTES (node->decl));
239 /* No targets specified. */
240 if (!attr_target)
241 return false;
243 tree arglist = TREE_VALUE (attr_target);
244 int attr_len = get_attr_len (arglist);
246 /* No need to clone for 1 target attribute. */
247 if (attr_len == -1)
249 warning_at (DECL_SOURCE_LOCATION (node->decl),
251 "single target_clones attribute is ignored");
252 return false;
255 char *attr_str = XNEWVEC (char, attr_len);
256 int attrnum = get_attr_str (arglist, attr_str);
257 char **attrs = XNEWVEC (char *, attrnum);
259 attrnum = separate_attrs (attr_str, attrs);
260 if (attrnum == -1)
262 error_at (DECL_SOURCE_LOCATION (node->decl),
263 "default target was not set");
264 XDELETEVEC (attrs);
265 XDELETEVEC (attr_str);
266 return false;
269 cgraph_function_version_info *decl1_v = NULL;
270 cgraph_function_version_info *decl2_v = NULL;
271 cgraph_function_version_info *before = NULL;
272 cgraph_function_version_info *after = NULL;
273 decl1_v = node->function_version ();
274 if (decl1_v == NULL)
275 decl1_v = node->insert_new_function_version ();
276 before = decl1_v;
277 DECL_FUNCTION_VERSIONED (node->decl) = 1;
279 for (i = 0; i < attrnum; i++)
281 char *attr = attrs[i];
282 char *suffix = XNEWVEC (char, strlen (attr) + 1);
284 create_new_asm_name (attr, suffix);
285 /* Create new target clone. */
286 cgraph_node *new_node = create_target_clone (node, definition, suffix);
287 new_node->local.local = false;
288 XDELETEVEC (suffix);
290 /* Set new attribute for the clone. */
291 tree attributes = make_attribute ("target", attr,
292 DECL_ATTRIBUTES (new_node->decl));
293 DECL_ATTRIBUTES (new_node->decl) = attributes;
294 location_t saved_loc = input_location;
295 input_location = DECL_SOURCE_LOCATION (node->decl);
296 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
297 TREE_VALUE (attributes),
300 input_location = saved_loc;
301 continue;
304 input_location = saved_loc;
305 decl2_v = new_node->function_version ();
306 if (decl2_v != NULL)
307 continue;
308 decl2_v = new_node->insert_new_function_version ();
310 /* Chain decl2_v and decl1_v. All semantically identical versions
311 will be chained together. */
312 after = decl2_v;
313 while (before->next != NULL)
314 before = before->next;
315 while (after->prev != NULL)
316 after = after->prev;
318 before->next = after;
319 after->prev = before;
320 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
323 XDELETEVEC (attrs);
324 XDELETEVEC (attr_str);
326 /* Setting new attribute to initial function. */
327 tree attributes = make_attribute ("target", "default",
328 DECL_ATTRIBUTES (node->decl));
329 DECL_ATTRIBUTES (node->decl) = attributes;
330 location_t saved_loc = input_location;
331 input_location = DECL_SOURCE_LOCATION (node->decl);
332 bool ret
333 = targetm.target_option.valid_attribute_p (node->decl, NULL,
334 TREE_VALUE (attributes), 0);
335 input_location = saved_loc;
336 return ret;
339 static unsigned int
340 ipa_target_clone (void)
342 struct cgraph_node *node;
344 bool target_clone_pass = false;
345 FOR_EACH_FUNCTION (node)
346 target_clone_pass |= expand_target_clones (node, node->definition);
348 if (target_clone_pass)
349 FOR_EACH_FUNCTION (node)
350 create_dispatcher_calls (node);
352 return 0;
355 namespace {
357 const pass_data pass_data_target_clone =
359 SIMPLE_IPA_PASS, /* type */
360 "targetclone", /* name */
361 OPTGROUP_NONE, /* optinfo_flags */
362 TV_NONE, /* tv_id */
363 ( PROP_ssa | PROP_cfg ), /* properties_required */
364 0, /* properties_provided */
365 0, /* properties_destroyed */
366 0, /* todo_flags_start */
367 TODO_update_ssa /* todo_flags_finish */
370 class pass_target_clone : public simple_ipa_opt_pass
372 public:
373 pass_target_clone (gcc::context *ctxt)
374 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
377 /* opt_pass methods: */
378 virtual bool gate (function *);
379 virtual unsigned int execute (function *) { return ipa_target_clone (); }
382 bool
383 pass_target_clone::gate (function *)
385 return true;
388 } // anon namespace
390 simple_ipa_opt_pass *
391 make_pass_target_clone (gcc::context *ctxt)
393 return new pass_target_clone (ctxt);