PR c++/68795: fix uninitialized close_paren_loc in cp_parser_postfix_expression
[gcc.git] / gcc / multiple_target.c
1 /* Pass for parsing functions with multiple target attributes.
2
3 Contributed by Evgeny Stupachenko <evstupac@gmail.com>
4
5 Copyright (C) 2015-2016 Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
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
38 /* If the call in NODE has multiple target attribute with multiple fields,
39 replace it with dispatcher call and create dispatcher (once). */
40
41 static void
42 create_dispatcher_calls (struct cgraph_node *node)
43 {
44 cgraph_edge *e;
45 cgraph_edge *e_next;
46
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)
49 {
50 tree resolver_decl;
51 tree idecl;
52 tree decl;
53 gimple *call = e->call_stmt;
54 struct cgraph_node *inode;
55
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;
63
64 if (!targetm.has_ifunc_p ())
65 {
66 error_at (gimple_location (call),
67 "the call requires ifunc, which is not"
68 " supported by this target");
69 break;
70 }
71 e_next = e->next_caller;
72 idecl = targetm.get_function_versions_dispatcher (decl);
73 if (!idecl)
74 {
75 error_at (gimple_location (call),
76 "default target_clones attribute was not set");
77 break;
78 }
79 inode = cgraph_node::get (idecl);
80 gcc_assert (inode);
81 resolver_decl = targetm.generate_version_dispatcher_body (inode);
82
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));
88
89 e->redirect_callee (inode);
90 /* Since REDIRECT_CALLEE modifies NEXT_CALLER field we move to
91 previously set NEXT_CALLER. */
92 e = NULL;
93 }
94 }
95
96 /* Return length of attribute names string,
97 if arglist chain > 1, -1 otherwise. */
98
99 static int
100 get_attr_len (tree arglist)
101 {
102 tree arg;
103 int str_len_sum = 0;
104 int argnum = 1;
105
106 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
107 {
108 unsigned int i;
109 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
110 int len = strlen (str);
111
112 str_len_sum += len + 1;
113 if (arg != arglist)
114 argnum++;
115 for (i = 0; i < strlen (str); i++)
116 if (str[i] == ',')
117 argnum++;
118 }
119 if (argnum == 1)
120 return -1;
121 return str_len_sum;
122 }
123
124 /* Create string with attributes separated by comma.
125 Return number of attributes. */
126
127 static int
128 get_attr_str (tree arglist, char *attr_str)
129 {
130 tree arg;
131 size_t str_len_sum = 0;
132 int argnum = 0;
133
134 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
135 {
136 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
137
138 size_t len = strlen (str);
139 memcpy (attr_str + str_len_sum, str, len);
140 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
141 str_len_sum += len + 1;
142 argnum++;
143 }
144 return argnum;
145 }
146
147 /* Return number of attributes separated by comma and put them into ARGS.
148 If there is no DEFAULT attribute return -1. */
149
150 static int
151 separate_attrs (char *attr_str, char **attrs)
152 {
153 int i = 0;
154 bool has_default = false;
155 char *attr = strtok (attr_str, ",");
156
157 while (attr != NULL)
158 {
159 if (strcmp (attr, "default") == 0)
160 {
161 has_default = true;
162 attr = strtok (NULL, ",");
163 continue;
164 }
165 attrs[i] = attr;
166 attr = strtok (NULL, ",");
167 i++;
168 }
169 if (!has_default)
170 return -1;
171 return i;
172 }
173
174 /* Return true if symbol is valid in assembler name. */
175
176 static bool
177 is_valid_asm_symbol (char c)
178 {
179 if ('a' <= c && c <= 'z')
180 return true;
181 if ('A' <= c && c <= 'Z')
182 return true;
183 if ('0' <= c && c <= '9')
184 return true;
185 if (c == '_')
186 return true;
187 return false;
188 }
189
190 /* Replace all not valid assembler symbols with '_'. */
191
192 static void
193 create_new_asm_name (char *old_asm_name, char *new_asm_name)
194 {
195 int i;
196 int old_name_len = strlen (old_asm_name);
197
198 /* Replace all not valid assembler symbols with '_'. */
199 for (i = 0; i < old_name_len; i++)
200 if (!is_valid_asm_symbol (old_asm_name[i]))
201 new_asm_name[i] = '_';
202 else
203 new_asm_name[i] = old_asm_name[i];
204 new_asm_name[old_name_len] = '\0';
205 }
206
207 /* Creates target clone of NODE. */
208
209 static cgraph_node *
210 create_target_clone (cgraph_node *node, bool definition, char *name)
211 {
212 cgraph_node *new_node;
213
214 if (definition)
215 {
216 new_node = node->create_version_clone_with_body (vNULL, NULL,
217 NULL, false,
218 NULL, NULL,
219 name);
220 new_node->force_output = true;
221 }
222 else
223 {
224 tree new_decl = copy_node (node->decl);
225 new_node = cgraph_node::get_create (new_decl);
226 /* Generate a new name for the new version. */
227 symtab->change_decl_assembler_name (new_node->decl,
228 clone_function_name (node->decl,
229 name));
230 }
231 return new_node;
232 }
233
234 /* If the function in NODE has multiple target attributes
235 create the appropriate clone for each valid target attribute. */
236
237 static bool
238 expand_target_clones (struct cgraph_node *node, bool defenition)
239 {
240 int i;
241 /* Parsing target attributes separated by comma. */
242 tree attr_target = lookup_attribute ("target_clones",
243 DECL_ATTRIBUTES (node->decl));
244 /* No targets specified. */
245 if (!attr_target)
246 return false;
247
248 tree arglist = TREE_VALUE (attr_target);
249 int attr_len = get_attr_len (arglist);
250
251 /* No need to clone for 1 target attribute. */
252 if (attr_len == -1)
253 {
254 warning_at (DECL_SOURCE_LOCATION (node->decl),
255 0,
256 "single target_clones attribute is ignored");
257 return false;
258 }
259
260 char *attr_str = XNEWVEC (char, attr_len);
261 int attrnum = get_attr_str (arglist, attr_str);
262 char **attrs = XNEWVEC (char *, attrnum);
263
264 attrnum = separate_attrs (attr_str, attrs);
265 if (attrnum == -1)
266 {
267 error_at (DECL_SOURCE_LOCATION (node->decl),
268 "default target was not set");
269 return false;
270 }
271
272 cgraph_function_version_info *decl1_v = NULL;
273 cgraph_function_version_info *decl2_v = NULL;
274 cgraph_function_version_info *before = NULL;
275 cgraph_function_version_info *after = NULL;
276 decl1_v = node->function_version ();
277 if (decl1_v == NULL)
278 decl1_v = node->insert_new_function_version ();
279 before = decl1_v;
280 DECL_FUNCTION_VERSIONED (node->decl) = 1;
281
282 for (i = 0; i < attrnum; i++)
283 {
284 char *attr = attrs[i];
285 char *suffix = XNEWVEC (char, strlen (attr) + 1);
286
287 create_new_asm_name (attr, suffix);
288 /* Create new target clone. */
289 cgraph_node *new_node = create_target_clone (node, defenition, suffix);
290 XDELETEVEC (suffix);
291
292 /* Set new attribute for the clone. */
293 tree attributes = make_attribute ("target", attr,
294 DECL_ATTRIBUTES (new_node->decl));
295 DECL_ATTRIBUTES (new_node->decl) = attributes;
296 if (!targetm.target_option.valid_attribute_p (new_node->decl, NULL,
297 TREE_VALUE (attributes), 0))
298 {
299 warning_at (DECL_SOURCE_LOCATION (node->decl), 0,
300 "attribute(target_clones(\"%s\")) is not "
301 "valid for current target", attr);
302 continue;
303 }
304
305 decl2_v = new_node->function_version ();
306 if (decl2_v != NULL)
307 continue;
308 decl2_v = new_node->insert_new_function_version ();
309
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;
317
318 before->next = after;
319 after->prev = before;
320 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
321 }
322
323 /* Setting new attribute to initial function. */
324 tree attributes = make_attribute ("target", "default",
325 DECL_ATTRIBUTES (node->decl));
326 DECL_ATTRIBUTES (node->decl) = attributes;
327 if (!targetm.target_option.valid_attribute_p (node->decl, NULL,
328 TREE_VALUE (attributes), 0))
329 {
330 error_at (DECL_SOURCE_LOCATION (node->decl),
331 "attribute(target_clones(\"default\")) is not "
332 "valid for current target");
333 return false;
334 }
335
336 XDELETEVEC (attrs);
337 XDELETEVEC (attr_str);
338 return true;
339 }
340
341 static bool target_clone_pass;
342
343 static unsigned int
344 ipa_target_clone (void)
345 {
346 struct cgraph_node *node;
347
348 target_clone_pass = false;
349 FOR_EACH_FUNCTION (node)
350 if (node->definition)
351 target_clone_pass |= expand_target_clones (node, true);
352 return 0;
353 }
354
355 namespace {
356
357 const pass_data pass_data_target_clone =
358 {
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 0 /* todo_flags_finish */
368 };
369
370 class pass_target_clone : public simple_ipa_opt_pass
371 {
372 public:
373 pass_target_clone (gcc::context *ctxt)
374 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
375 {}
376
377 /* opt_pass methods: */
378 virtual bool gate (function *);
379 virtual unsigned int execute (function *) { return ipa_target_clone (); }
380 };
381
382 bool
383 pass_target_clone::gate (function *)
384 {
385 return true;
386 }
387
388 } // anon namespace
389
390 simple_ipa_opt_pass *
391 make_pass_target_clone (gcc::context *ctxt)
392 {
393 return new pass_target_clone (ctxt);
394 }
395
396 static unsigned int
397 ipa_dispatcher_calls (void)
398 {
399 struct cgraph_node *node;
400
401 FOR_EACH_FUNCTION (node)
402 if (!node->definition)
403 target_clone_pass |= expand_target_clones (node, false);
404 if (target_clone_pass)
405 FOR_EACH_FUNCTION (node)
406 create_dispatcher_calls (node);
407 return 0;
408 }
409
410 namespace {
411
412 const pass_data pass_data_dispatcher_calls =
413 {
414 SIMPLE_IPA_PASS, /* type */
415 "dispachercalls", /* name */
416 OPTGROUP_NONE, /* optinfo_flags */
417 TV_NONE, /* tv_id */
418 ( PROP_ssa | PROP_cfg ), /* properties_required */
419 0, /* properties_provided */
420 0, /* properties_destroyed */
421 0, /* todo_flags_start */
422 0 /* todo_flags_finish */
423 };
424
425 class pass_dispatcher_calls : public simple_ipa_opt_pass
426 {
427 public:
428 pass_dispatcher_calls (gcc::context *ctxt)
429 : simple_ipa_opt_pass (pass_data_dispatcher_calls, ctxt)
430 {}
431
432 /* opt_pass methods: */
433 virtual bool gate (function *);
434 virtual unsigned int execute (function *) { return ipa_dispatcher_calls (); }
435 };
436
437 bool
438 pass_dispatcher_calls::gate (function *)
439 {
440 return true;
441 }
442
443 } // anon namespace
444
445 simple_ipa_opt_pass *
446 make_pass_dispatcher_calls (gcc::context *ctxt)
447 {
448 return new pass_dispatcher_calls (ctxt);
449 }