annotate gcc/multiple_target.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Pass for parsing functions with multiple target attributes.
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 Contributed by Evgeny Stupachenko <evstupac@gmail.com>
kono
parents:
diff changeset
4
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
5 Copyright (C) 2015-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 This file is part of GCC.
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
10 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
11 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
12 version.
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
17 for more details.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
20 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
21 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 #include "config.h"
kono
parents:
diff changeset
24 #include "system.h"
kono
parents:
diff changeset
25 #include "coretypes.h"
kono
parents:
diff changeset
26 #include "backend.h"
kono
parents:
diff changeset
27 #include "tree.h"
kono
parents:
diff changeset
28 #include "stringpool.h"
kono
parents:
diff changeset
29 #include "gimple.h"
kono
parents:
diff changeset
30 #include "diagnostic-core.h"
kono
parents:
diff changeset
31 #include "gimple-ssa.h"
kono
parents:
diff changeset
32 #include "cgraph.h"
kono
parents:
diff changeset
33 #include "tree-pass.h"
kono
parents:
diff changeset
34 #include "target.h"
kono
parents:
diff changeset
35 #include "attribs.h"
kono
parents:
diff changeset
36 #include "pretty-print.h"
kono
parents:
diff changeset
37 #include "gimple-iterator.h"
kono
parents:
diff changeset
38 #include "gimple-walk.h"
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
39 #include "tree-inline.h"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
40 #include "intl.h"
111
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 /* Walker callback that replaces all FUNCTION_DECL of a function that's
kono
parents:
diff changeset
43 going to be versioned. */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 static tree
kono
parents:
diff changeset
46 replace_function_decl (tree *op, int *walk_subtrees, void *data)
kono
parents:
diff changeset
47 {
kono
parents:
diff changeset
48 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
kono
parents:
diff changeset
49 cgraph_function_version_info *info = (cgraph_function_version_info *)wi->info;
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 if (TREE_CODE (*op) == FUNCTION_DECL
kono
parents:
diff changeset
52 && info->this_node->decl == *op)
kono
parents:
diff changeset
53 {
kono
parents:
diff changeset
54 *op = info->dispatcher_resolver;
kono
parents:
diff changeset
55 *walk_subtrees = 0;
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 return NULL;
kono
parents:
diff changeset
59 }
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 /* If the call in NODE has multiple target attribute with multiple fields,
kono
parents:
diff changeset
62 replace it with dispatcher call and create dispatcher (once). */
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 static void
kono
parents:
diff changeset
65 create_dispatcher_calls (struct cgraph_node *node)
kono
parents:
diff changeset
66 {
kono
parents:
diff changeset
67 ipa_ref *ref;
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 if (!DECL_FUNCTION_VERSIONED (node->decl)
kono
parents:
diff changeset
70 || !is_function_default_version (node->decl))
kono
parents:
diff changeset
71 return;
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 if (!targetm.has_ifunc_p ())
kono
parents:
diff changeset
74 {
kono
parents:
diff changeset
75 error_at (DECL_SOURCE_LOCATION (node->decl),
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
76 "the call requires %<ifunc%>, which is not"
111
kono
parents:
diff changeset
77 " supported by this target");
kono
parents:
diff changeset
78 return;
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80 else if (!targetm.get_function_versions_dispatcher)
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 error_at (DECL_SOURCE_LOCATION (node->decl),
kono
parents:
diff changeset
83 "target does not support function version dispatcher");
kono
parents:
diff changeset
84 return;
kono
parents:
diff changeset
85 }
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 tree idecl = targetm.get_function_versions_dispatcher (node->decl);
kono
parents:
diff changeset
88 if (!idecl)
kono
parents:
diff changeset
89 {
kono
parents:
diff changeset
90 error_at (DECL_SOURCE_LOCATION (node->decl),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
91 "default %<target_clones%> attribute was not set");
111
kono
parents:
diff changeset
92 return;
kono
parents:
diff changeset
93 }
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 cgraph_node *inode = cgraph_node::get (idecl);
kono
parents:
diff changeset
96 gcc_assert (inode);
kono
parents:
diff changeset
97 tree resolver_decl = targetm.generate_version_dispatcher_body (inode);
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 /* Update aliases. */
kono
parents:
diff changeset
100 inode->alias = true;
kono
parents:
diff changeset
101 inode->alias_target = resolver_decl;
kono
parents:
diff changeset
102 if (!inode->analyzed)
kono
parents:
diff changeset
103 inode->resolve_alias (cgraph_node::get (resolver_decl));
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 auto_vec<cgraph_edge *> edges_to_redirect;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
106 /* We need to capture the references by value rather than just pointers to them
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
107 and remove them right away, as removing them later would invalidate what
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
108 some other reference pointers point to. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
109 auto_vec<ipa_ref> references_to_redirect;
111
kono
parents:
diff changeset
110
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
111 while (node->iterate_referring (0, ref))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
112 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
113 references_to_redirect.safe_push (*ref);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
114 ref->remove_reference ();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
115 }
111
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
kono
parents:
diff changeset
118 for (cgraph_edge *e = node->callers; e ; e = e->next_caller)
kono
parents:
diff changeset
119 edges_to_redirect.safe_push (e);
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 if (!edges_to_redirect.is_empty () || !references_to_redirect.is_empty ())
kono
parents:
diff changeset
122 {
kono
parents:
diff changeset
123 /* Redirect edges. */
kono
parents:
diff changeset
124 unsigned i;
kono
parents:
diff changeset
125 cgraph_edge *e;
kono
parents:
diff changeset
126 FOR_EACH_VEC_ELT (edges_to_redirect, i, e)
kono
parents:
diff changeset
127 {
kono
parents:
diff changeset
128 e->redirect_callee (inode);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
129 cgraph_edge::redirect_call_stmt_to_callee (e);
111
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 /* Redirect references. */
kono
parents:
diff changeset
133 FOR_EACH_VEC_ELT (references_to_redirect, i, ref)
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 if (ref->use == IPA_REF_ADDR)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 struct walk_stmt_info wi;
kono
parents:
diff changeset
138 memset (&wi, 0, sizeof (wi));
kono
parents:
diff changeset
139 wi.info = (void *)node->function_version ();
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 if (dyn_cast<varpool_node *> (ref->referring))
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 hash_set<tree> visited_nodes;
kono
parents:
diff changeset
144 walk_tree (&DECL_INITIAL (ref->referring->decl),
kono
parents:
diff changeset
145 replace_function_decl, &wi, &visited_nodes);
kono
parents:
diff changeset
146 }
kono
parents:
diff changeset
147 else
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 gimple_stmt_iterator it = gsi_for_stmt (ref->stmt);
kono
parents:
diff changeset
150 if (ref->referring->decl != resolver_decl)
kono
parents:
diff changeset
151 walk_gimple_stmt (&it, NULL, replace_function_decl, &wi);
kono
parents:
diff changeset
152 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
153
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
154 symtab_node *source = ref->referring;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
155 source->create_reference (inode, IPA_REF_ADDR);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
156 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
157 else if (ref->use == IPA_REF_ALIAS)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
158 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159 symtab_node *source = ref->referring;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
160 source->create_reference (inode, IPA_REF_ALIAS);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
161 if (inode->get_comdat_group ())
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
162 source->add_to_same_comdat_group (inode);
111
kono
parents:
diff changeset
163 }
kono
parents:
diff changeset
164 else
kono
parents:
diff changeset
165 gcc_unreachable ();
kono
parents:
diff changeset
166 }
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 symtab->change_decl_assembler_name (node->decl,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
170 clone_function_name_numbered (
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
171 node->decl, "default"));
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
172
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
173 /* FIXME: copy of cgraph_node::make_local that should be cleaned up
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
174 in next stage1. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
175 node->make_decl_local ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
176 node->set_section (NULL);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
177 node->set_comdat_group (NULL);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
178 node->externally_visible = false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 node->forced_by_abi = false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 node->set_section (NULL);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
182 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
183 && !flag_incremental_link);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
184 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
185
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
186 DECL_ARTIFICIAL (node->decl) = 1;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
187 node->force_output = true;
111
kono
parents:
diff changeset
188 }
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 /* Return length of attribute names string,
kono
parents:
diff changeset
191 if arglist chain > 1, -1 otherwise. */
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 static int
kono
parents:
diff changeset
194 get_attr_len (tree arglist)
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 tree arg;
kono
parents:
diff changeset
197 int str_len_sum = 0;
kono
parents:
diff changeset
198 int argnum = 0;
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
kono
parents:
diff changeset
201 {
kono
parents:
diff changeset
202 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
kono
parents:
diff changeset
203 size_t len = strlen (str);
kono
parents:
diff changeset
204 str_len_sum += len + 1;
kono
parents:
diff changeset
205 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
kono
parents:
diff changeset
206 argnum++;
kono
parents:
diff changeset
207 argnum++;
kono
parents:
diff changeset
208 }
kono
parents:
diff changeset
209 if (argnum <= 1)
kono
parents:
diff changeset
210 return -1;
kono
parents:
diff changeset
211 return str_len_sum;
kono
parents:
diff changeset
212 }
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 /* Create string with attributes separated by comma.
kono
parents:
diff changeset
215 Return number of attributes. */
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 static int
kono
parents:
diff changeset
218 get_attr_str (tree arglist, char *attr_str)
kono
parents:
diff changeset
219 {
kono
parents:
diff changeset
220 tree arg;
kono
parents:
diff changeset
221 size_t str_len_sum = 0;
kono
parents:
diff changeset
222 int argnum = 0;
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 for (arg = arglist; arg; arg = TREE_CHAIN (arg))
kono
parents:
diff changeset
225 {
kono
parents:
diff changeset
226 const char *str = TREE_STRING_POINTER (TREE_VALUE (arg));
kono
parents:
diff changeset
227 size_t len = strlen (str);
kono
parents:
diff changeset
228 for (const char *p = strchr (str, ','); p; p = strchr (p + 1, ','))
kono
parents:
diff changeset
229 argnum++;
kono
parents:
diff changeset
230 memcpy (attr_str + str_len_sum, str, len);
kono
parents:
diff changeset
231 attr_str[str_len_sum + len] = TREE_CHAIN (arg) ? ',' : '\0';
kono
parents:
diff changeset
232 str_len_sum += len + 1;
kono
parents:
diff changeset
233 argnum++;
kono
parents:
diff changeset
234 }
kono
parents:
diff changeset
235 return argnum;
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 /* Return number of attributes separated by comma and put them into ARGS.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
239 If there is no DEFAULT attribute return -1.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
240 If there is an empty string in attribute return -2.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
241 If there are multiple DEFAULT attributes return -3.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
242 */
111
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 static int
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
245 separate_attrs (char *attr_str, char **attrs, int attrnum)
111
kono
parents:
diff changeset
246 {
kono
parents:
diff changeset
247 int i = 0;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
248 int default_count = 0;
111
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 for (char *attr = strtok (attr_str, ",");
kono
parents:
diff changeset
251 attr != NULL; attr = strtok (NULL, ","))
kono
parents:
diff changeset
252 {
kono
parents:
diff changeset
253 if (strcmp (attr, "default") == 0)
kono
parents:
diff changeset
254 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
255 default_count++;
111
kono
parents:
diff changeset
256 continue;
kono
parents:
diff changeset
257 }
kono
parents:
diff changeset
258 attrs[i++] = attr;
kono
parents:
diff changeset
259 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
260 if (default_count == 0)
111
kono
parents:
diff changeset
261 return -1;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
262 else if (default_count > 1)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
263 return -3;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
264 else if (i + default_count < attrnum)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
265 return -2;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
266
111
kono
parents:
diff changeset
267 return i;
kono
parents:
diff changeset
268 }
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 /* Return true if symbol is valid in assembler name. */
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 static bool
kono
parents:
diff changeset
273 is_valid_asm_symbol (char c)
kono
parents:
diff changeset
274 {
kono
parents:
diff changeset
275 if ('a' <= c && c <= 'z')
kono
parents:
diff changeset
276 return true;
kono
parents:
diff changeset
277 if ('A' <= c && c <= 'Z')
kono
parents:
diff changeset
278 return true;
kono
parents:
diff changeset
279 if ('0' <= c && c <= '9')
kono
parents:
diff changeset
280 return true;
kono
parents:
diff changeset
281 if (c == '_')
kono
parents:
diff changeset
282 return true;
kono
parents:
diff changeset
283 return false;
kono
parents:
diff changeset
284 }
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 /* Replace all not valid assembler symbols with '_'. */
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 static void
kono
parents:
diff changeset
289 create_new_asm_name (char *old_asm_name, char *new_asm_name)
kono
parents:
diff changeset
290 {
kono
parents:
diff changeset
291 int i;
kono
parents:
diff changeset
292 int old_name_len = strlen (old_asm_name);
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 /* Replace all not valid assembler symbols with '_'. */
kono
parents:
diff changeset
295 for (i = 0; i < old_name_len; i++)
kono
parents:
diff changeset
296 if (!is_valid_asm_symbol (old_asm_name[i]))
kono
parents:
diff changeset
297 new_asm_name[i] = '_';
kono
parents:
diff changeset
298 else
kono
parents:
diff changeset
299 new_asm_name[i] = old_asm_name[i];
kono
parents:
diff changeset
300 new_asm_name[old_name_len] = '\0';
kono
parents:
diff changeset
301 }
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 /* Creates target clone of NODE. */
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 static cgraph_node *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
306 create_target_clone (cgraph_node *node, bool definition, char *name,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
307 tree attributes)
111
kono
parents:
diff changeset
308 {
kono
parents:
diff changeset
309 cgraph_node *new_node;
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 if (definition)
kono
parents:
diff changeset
312 {
kono
parents:
diff changeset
313 new_node = node->create_version_clone_with_body (vNULL, NULL,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
314 NULL, NULL,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
315 NULL, name, attributes);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
316 if (new_node == NULL)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
317 return NULL;
111
kono
parents:
diff changeset
318 new_node->force_output = true;
kono
parents:
diff changeset
319 }
kono
parents:
diff changeset
320 else
kono
parents:
diff changeset
321 {
kono
parents:
diff changeset
322 tree new_decl = copy_node (node->decl);
kono
parents:
diff changeset
323 new_node = cgraph_node::get_create (new_decl);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
324 DECL_ATTRIBUTES (new_decl) = attributes;
111
kono
parents:
diff changeset
325 /* Generate a new name for the new version. */
kono
parents:
diff changeset
326 symtab->change_decl_assembler_name (new_node->decl,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
327 clone_function_name_numbered (
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
328 node->decl, name));
111
kono
parents:
diff changeset
329 }
kono
parents:
diff changeset
330 return new_node;
kono
parents:
diff changeset
331 }
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 /* If the function in NODE has multiple target attributes
kono
parents:
diff changeset
334 create the appropriate clone for each valid target attribute. */
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 static bool
kono
parents:
diff changeset
337 expand_target_clones (struct cgraph_node *node, bool definition)
kono
parents:
diff changeset
338 {
kono
parents:
diff changeset
339 int i;
kono
parents:
diff changeset
340 /* Parsing target attributes separated by comma. */
kono
parents:
diff changeset
341 tree attr_target = lookup_attribute ("target_clones",
kono
parents:
diff changeset
342 DECL_ATTRIBUTES (node->decl));
kono
parents:
diff changeset
343 /* No targets specified. */
kono
parents:
diff changeset
344 if (!attr_target)
kono
parents:
diff changeset
345 return false;
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 tree arglist = TREE_VALUE (attr_target);
kono
parents:
diff changeset
348 int attr_len = get_attr_len (arglist);
kono
parents:
diff changeset
349
kono
parents:
diff changeset
350 /* No need to clone for 1 target attribute. */
kono
parents:
diff changeset
351 if (attr_len == -1)
kono
parents:
diff changeset
352 {
kono
parents:
diff changeset
353 warning_at (DECL_SOURCE_LOCATION (node->decl),
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
354 0, "single %<target_clones%> attribute is ignored");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
355 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
356 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
357
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
358 if (node->definition
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
359 && (node->alias || !tree_versionable_function_p (node->decl)))
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
360 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
361 auto_diagnostic_group d;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
362 error_at (DECL_SOURCE_LOCATION (node->decl),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
363 "clones for %<target_clones%> attribute cannot be created");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
364 const char *reason = NULL;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
365 if (lookup_attribute ("noclone", DECL_ATTRIBUTES (node->decl)))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
366 reason = G_("function %q+F can never be copied "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
367 "because it has %<noclone%> attribute");
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
368 else if (node->alias)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
369 reason
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
370 = "%<target_clones%> cannot be combined with %<alias%> attribute";
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
371 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
372 reason = copy_forbidden (DECL_STRUCT_FUNCTION (node->decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
373 if (reason)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
374 inform (DECL_SOURCE_LOCATION (node->decl), reason, node->decl);
111
kono
parents:
diff changeset
375 return false;
kono
parents:
diff changeset
376 }
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 char *attr_str = XNEWVEC (char, attr_len);
kono
parents:
diff changeset
379 int attrnum = get_attr_str (arglist, attr_str);
kono
parents:
diff changeset
380 char **attrs = XNEWVEC (char *, attrnum);
kono
parents:
diff changeset
381
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
382 attrnum = separate_attrs (attr_str, attrs, attrnum);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
383 switch (attrnum)
111
kono
parents:
diff changeset
384 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
385 case -1:
111
kono
parents:
diff changeset
386 error_at (DECL_SOURCE_LOCATION (node->decl),
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
387 "%<default%> target was not set");
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
388 break;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
389 case -2:
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
390 error_at (DECL_SOURCE_LOCATION (node->decl),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
391 "an empty string cannot be in %<target_clones%> attribute");
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
392 break;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
393 case -3:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
394 error_at (DECL_SOURCE_LOCATION (node->decl),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
395 "multiple %<default%> targets were set");
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
396 break;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
397 default:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
398 break;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
399 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
400
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
401 if (attrnum < 0)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
402 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
403 XDELETEVEC (attrs);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
404 XDELETEVEC (attr_str);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
405 return false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
406 }
111
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 cgraph_function_version_info *decl1_v = NULL;
kono
parents:
diff changeset
409 cgraph_function_version_info *decl2_v = NULL;
kono
parents:
diff changeset
410 cgraph_function_version_info *before = NULL;
kono
parents:
diff changeset
411 cgraph_function_version_info *after = NULL;
kono
parents:
diff changeset
412 decl1_v = node->function_version ();
kono
parents:
diff changeset
413 if (decl1_v == NULL)
kono
parents:
diff changeset
414 decl1_v = node->insert_new_function_version ();
kono
parents:
diff changeset
415 before = decl1_v;
kono
parents:
diff changeset
416 DECL_FUNCTION_VERSIONED (node->decl) = 1;
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 for (i = 0; i < attrnum; i++)
kono
parents:
diff changeset
419 {
kono
parents:
diff changeset
420 char *attr = attrs[i];
kono
parents:
diff changeset
421 char *suffix = XNEWVEC (char, strlen (attr) + 1);
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 create_new_asm_name (attr, suffix);
kono
parents:
diff changeset
424 /* Create new target clone. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
425 tree attributes = make_attribute ("target", attr,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
426 DECL_ATTRIBUTES (node->decl));
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
427
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
428 cgraph_node *new_node = create_target_clone (node, definition, suffix,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
429 attributes);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
430 if (new_node == NULL)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
431 return false;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
432 new_node->local = false;
111
kono
parents:
diff changeset
433 XDELETEVEC (suffix);
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 decl2_v = new_node->function_version ();
kono
parents:
diff changeset
436 if (decl2_v != NULL)
kono
parents:
diff changeset
437 continue;
kono
parents:
diff changeset
438 decl2_v = new_node->insert_new_function_version ();
kono
parents:
diff changeset
439
kono
parents:
diff changeset
440 /* Chain decl2_v and decl1_v. All semantically identical versions
kono
parents:
diff changeset
441 will be chained together. */
kono
parents:
diff changeset
442 after = decl2_v;
kono
parents:
diff changeset
443 while (before->next != NULL)
kono
parents:
diff changeset
444 before = before->next;
kono
parents:
diff changeset
445 while (after->prev != NULL)
kono
parents:
diff changeset
446 after = after->prev;
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 before->next = after;
kono
parents:
diff changeset
449 after->prev = before;
kono
parents:
diff changeset
450 DECL_FUNCTION_VERSIONED (new_node->decl) = 1;
kono
parents:
diff changeset
451 }
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 XDELETEVEC (attrs);
kono
parents:
diff changeset
454 XDELETEVEC (attr_str);
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 /* Setting new attribute to initial function. */
kono
parents:
diff changeset
457 tree attributes = make_attribute ("target", "default",
kono
parents:
diff changeset
458 DECL_ATTRIBUTES (node->decl));
kono
parents:
diff changeset
459 DECL_ATTRIBUTES (node->decl) = attributes;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
460 node->local = false;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
461 return true;
111
kono
parents:
diff changeset
462 }
kono
parents:
diff changeset
463
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
464 /* When NODE is a target clone, consider all callees and redirect
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
465 to a clone with equal target attributes. That prevents multiple
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
466 multi-versioning dispatches and a call-chain can be optimized. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
467
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
468 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
469 redirect_to_specific_clone (cgraph_node *node)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
470 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
471 cgraph_function_version_info *fv = node->function_version ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
472 if (fv == NULL)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
473 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
474
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
475 tree attr_target = lookup_attribute ("target", DECL_ATTRIBUTES (node->decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
476 if (attr_target == NULL_TREE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
477 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
478
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
479 /* We need to remember NEXT_CALLER as it could be modified in the loop. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
480 for (cgraph_edge *e = node->callees; e ; e = e->next_callee)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
481 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
482 cgraph_function_version_info *fv2 = e->callee->function_version ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
483 if (!fv2)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
484 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
485
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
486 tree attr_target2 = lookup_attribute ("target",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
487 DECL_ATTRIBUTES (e->callee->decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
488
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
489 /* Function is not calling proper target clone. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
490 if (!attribute_list_equal (attr_target, attr_target2))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
491 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
492 while (fv2->prev != NULL)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
493 fv2 = fv2->prev;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
494
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
495 /* Try to find a clone with equal target attribute. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
496 for (; fv2 != NULL; fv2 = fv2->next)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
497 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
498 cgraph_node *callee = fv2->this_node;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
499 attr_target2 = lookup_attribute ("target",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
500 DECL_ATTRIBUTES (callee->decl));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 if (attribute_list_equal (attr_target, attr_target2))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
502 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
503 e->redirect_callee (callee);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
504 cgraph_edge::redirect_call_stmt_to_callee (e);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
505 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
506 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
507 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
508 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
509 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
510 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
511
111
kono
parents:
diff changeset
512 static unsigned int
kono
parents:
diff changeset
513 ipa_target_clone (void)
kono
parents:
diff changeset
514 {
kono
parents:
diff changeset
515 struct cgraph_node *node;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
516 auto_vec<cgraph_node *> to_dispatch;
111
kono
parents:
diff changeset
517
kono
parents:
diff changeset
518 FOR_EACH_FUNCTION (node)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
519 if (expand_target_clones (node, node->definition))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
520 to_dispatch.safe_push (node);
111
kono
parents:
diff changeset
521
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
522 for (unsigned i = 0; i < to_dispatch.length (); i++)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
523 create_dispatcher_calls (to_dispatch[i]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
524
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
525 FOR_EACH_FUNCTION (node)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
526 redirect_to_specific_clone (node);
111
kono
parents:
diff changeset
527
kono
parents:
diff changeset
528 return 0;
kono
parents:
diff changeset
529 }
kono
parents:
diff changeset
530
kono
parents:
diff changeset
531 namespace {
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 const pass_data pass_data_target_clone =
kono
parents:
diff changeset
534 {
kono
parents:
diff changeset
535 SIMPLE_IPA_PASS, /* type */
kono
parents:
diff changeset
536 "targetclone", /* name */
kono
parents:
diff changeset
537 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
538 TV_NONE, /* tv_id */
kono
parents:
diff changeset
539 ( PROP_ssa | PROP_cfg ), /* properties_required */
kono
parents:
diff changeset
540 0, /* properties_provided */
kono
parents:
diff changeset
541 0, /* properties_destroyed */
kono
parents:
diff changeset
542 0, /* todo_flags_start */
kono
parents:
diff changeset
543 TODO_update_ssa /* todo_flags_finish */
kono
parents:
diff changeset
544 };
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 class pass_target_clone : public simple_ipa_opt_pass
kono
parents:
diff changeset
547 {
kono
parents:
diff changeset
548 public:
kono
parents:
diff changeset
549 pass_target_clone (gcc::context *ctxt)
kono
parents:
diff changeset
550 : simple_ipa_opt_pass (pass_data_target_clone, ctxt)
kono
parents:
diff changeset
551 {}
kono
parents:
diff changeset
552
kono
parents:
diff changeset
553 /* opt_pass methods: */
kono
parents:
diff changeset
554 virtual bool gate (function *);
kono
parents:
diff changeset
555 virtual unsigned int execute (function *) { return ipa_target_clone (); }
kono
parents:
diff changeset
556 };
kono
parents:
diff changeset
557
kono
parents:
diff changeset
558 bool
kono
parents:
diff changeset
559 pass_target_clone::gate (function *)
kono
parents:
diff changeset
560 {
kono
parents:
diff changeset
561 return true;
kono
parents:
diff changeset
562 }
kono
parents:
diff changeset
563
kono
parents:
diff changeset
564 } // anon namespace
kono
parents:
diff changeset
565
kono
parents:
diff changeset
566 simple_ipa_opt_pass *
kono
parents:
diff changeset
567 make_pass_target_clone (gcc::context *ctxt)
kono
parents:
diff changeset
568 {
kono
parents:
diff changeset
569 return new pass_target_clone (ctxt);
kono
parents:
diff changeset
570 }