]>
Commit | Line | Data |
---|---|---|
518dc859 | 1 | /* Interprocedural analyses. |
23a5b65a | 2 | Copyright (C) 2005-2014 Free Software Foundation, Inc. |
518dc859 RL |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
518dc859 RL |
9 | version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
518dc859 RL |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tree.h" | |
2fb9a547 AM |
24 | #include "basic-block.h" |
25 | #include "tree-ssa-alias.h" | |
26 | #include "internal-fn.h" | |
27 | #include "gimple-fold.h" | |
28 | #include "tree-eh.h" | |
29 | #include "gimple-expr.h" | |
30 | #include "is-a.h" | |
18f429e2 | 31 | #include "gimple.h" |
d8a2d370 DN |
32 | #include "expr.h" |
33 | #include "stor-layout.h" | |
34 | #include "print-tree.h" | |
45b0be94 | 35 | #include "gimplify.h" |
5be5c238 | 36 | #include "gimple-iterator.h" |
18f429e2 | 37 | #include "gimplify-me.h" |
5be5c238 | 38 | #include "gimple-walk.h" |
518dc859 | 39 | #include "langhooks.h" |
518dc859 | 40 | #include "target.h" |
518dc859 | 41 | #include "ipa-prop.h" |
442b4905 AM |
42 | #include "bitmap.h" |
43 | #include "gimple-ssa.h" | |
44 | #include "tree-cfg.h" | |
45 | #include "tree-phinodes.h" | |
46 | #include "ssa-iterators.h" | |
47 | #include "tree-into-ssa.h" | |
48 | #include "tree-dfa.h" | |
518dc859 | 49 | #include "tree-pass.h" |
771578a0 | 50 | #include "tree-inline.h" |
0f378cb5 | 51 | #include "ipa-inline.h" |
518dc859 | 52 | #include "flags.h" |
3e293154 | 53 | #include "diagnostic.h" |
cf835838 | 54 | #include "gimple-pretty-print.h" |
fb3f88cc | 55 | #include "lto-streamer.h" |
f0efc7aa DN |
56 | #include "data-streamer.h" |
57 | #include "tree-streamer.h" | |
dfea20f1 | 58 | #include "params.h" |
450ad0cd | 59 | #include "ipa-utils.h" |
4df65a85 RB |
60 | #include "stringpool.h" |
61 | #include "tree-ssanames.h" | |
771578a0 | 62 | |
062c604f MJ |
63 | /* Intermediate information about a parameter that is only useful during the |
64 | run of ipa_analyze_node and is not kept afterwards. */ | |
65 | ||
66 | struct param_analysis_info | |
67 | { | |
8b7773a4 MJ |
68 | bool parm_modified, ref_modified, pt_modified; |
69 | bitmap parm_visited_statements, pt_visited_statements; | |
062c604f MJ |
70 | }; |
71 | ||
771578a0 | 72 | /* Vector where the parameter infos are actually stored. */ |
84562394 | 73 | vec<ipa_node_params> ipa_node_params_vector; |
2c9561b5 | 74 | /* Vector of known aggregate values in cloned nodes. */ |
9771b263 | 75 | vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements; |
771578a0 | 76 | /* Vector where the parameter infos are actually stored. */ |
84562394 | 77 | vec<ipa_edge_args, va_gc> *ipa_edge_args_vector; |
771578a0 MJ |
78 | |
79 | /* Holders of ipa cgraph hooks: */ | |
e2c9111c JH |
80 | static struct cgraph_edge_hook_list *edge_removal_hook_holder; |
81 | static struct cgraph_node_hook_list *node_removal_hook_holder; | |
82 | static struct cgraph_2edge_hook_list *edge_duplication_hook_holder; | |
83 | static struct cgraph_2node_hook_list *node_duplication_hook_holder; | |
40982661 | 84 | static struct cgraph_node_hook_list *function_insertion_hook_holder; |
518dc859 | 85 | |
4502fe8d MJ |
86 | /* Description of a reference to an IPA constant. */ |
87 | struct ipa_cst_ref_desc | |
88 | { | |
89 | /* Edge that corresponds to the statement which took the reference. */ | |
90 | struct cgraph_edge *cs; | |
91 | /* Linked list of duplicates created when call graph edges are cloned. */ | |
92 | struct ipa_cst_ref_desc *next_duplicate; | |
93 | /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value | |
94 | if out of control. */ | |
95 | int refcount; | |
96 | }; | |
97 | ||
98 | /* Allocation pool for reference descriptions. */ | |
99 | ||
100 | static alloc_pool ipa_refdesc_pool; | |
101 | ||
5fe8e757 MJ |
102 | /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated |
103 | with NODE should prevent us from analyzing it for the purposes of IPA-CP. */ | |
104 | ||
105 | static bool | |
106 | ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node) | |
107 | { | |
67348ccc | 108 | tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl); |
5fe8e757 MJ |
109 | struct cl_optimization *os; |
110 | ||
111 | if (!fs_opts) | |
112 | return false; | |
113 | os = TREE_OPTIMIZATION (fs_opts); | |
114 | return !os->x_optimize || !os->x_flag_ipa_cp; | |
115 | } | |
116 | ||
be95e2b9 MJ |
117 | /* Return index of the formal whose tree is PTREE in function which corresponds |
118 | to INFO. */ | |
119 | ||
d044dd17 | 120 | static int |
84562394 | 121 | ipa_get_param_decl_index_1 (vec<ipa_param_descriptor> descriptors, tree ptree) |
518dc859 RL |
122 | { |
123 | int i, count; | |
124 | ||
9771b263 | 125 | count = descriptors.length (); |
518dc859 | 126 | for (i = 0; i < count; i++) |
9771b263 | 127 | if (descriptors[i].decl == ptree) |
518dc859 RL |
128 | return i; |
129 | ||
130 | return -1; | |
131 | } | |
132 | ||
d044dd17 MJ |
133 | /* Return index of the formal whose tree is PTREE in function which corresponds |
134 | to INFO. */ | |
135 | ||
136 | int | |
137 | ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree) | |
138 | { | |
139 | return ipa_get_param_decl_index_1 (info->descriptors, ptree); | |
140 | } | |
141 | ||
142 | /* Populate the param_decl field in parameter DESCRIPTORS that correspond to | |
143 | NODE. */ | |
be95e2b9 | 144 | |
f8e2a1ed MJ |
145 | static void |
146 | ipa_populate_param_decls (struct cgraph_node *node, | |
84562394 | 147 | vec<ipa_param_descriptor> &descriptors) |
518dc859 RL |
148 | { |
149 | tree fndecl; | |
150 | tree fnargs; | |
151 | tree parm; | |
152 | int param_num; | |
3e293154 | 153 | |
67348ccc | 154 | fndecl = node->decl; |
0e8853ee | 155 | gcc_assert (gimple_has_body_p (fndecl)); |
518dc859 RL |
156 | fnargs = DECL_ARGUMENTS (fndecl); |
157 | param_num = 0; | |
910ad8de | 158 | for (parm = fnargs; parm; parm = DECL_CHAIN (parm)) |
518dc859 | 159 | { |
9771b263 | 160 | descriptors[param_num].decl = parm; |
0e8853ee | 161 | descriptors[param_num].move_cost = estimate_move_cost (TREE_TYPE (parm)); |
518dc859 RL |
162 | param_num++; |
163 | } | |
164 | } | |
165 | ||
3f84bf08 MJ |
166 | /* Return how many formal parameters FNDECL has. */ |
167 | ||
168 | static inline int | |
310bc633 | 169 | count_formal_params (tree fndecl) |
3f84bf08 MJ |
170 | { |
171 | tree parm; | |
172 | int count = 0; | |
0e8853ee | 173 | gcc_assert (gimple_has_body_p (fndecl)); |
3f84bf08 | 174 | |
910ad8de | 175 | for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm)) |
3f84bf08 MJ |
176 | count++; |
177 | ||
178 | return count; | |
179 | } | |
180 | ||
0e8853ee JH |
181 | /* Return the declaration of Ith formal parameter of the function corresponding |
182 | to INFO. Note there is no setter function as this array is built just once | |
183 | using ipa_initialize_node_params. */ | |
184 | ||
185 | void | |
186 | ipa_dump_param (FILE *file, struct ipa_node_params *info, int i) | |
187 | { | |
188 | fprintf (file, "param #%i", i); | |
189 | if (info->descriptors[i].decl) | |
190 | { | |
191 | fprintf (file, " "); | |
192 | print_generic_expr (file, info->descriptors[i].decl, 0); | |
193 | } | |
194 | } | |
195 | ||
196 | /* Initialize the ipa_node_params structure associated with NODE | |
197 | to hold PARAM_COUNT parameters. */ | |
198 | ||
199 | void | |
200 | ipa_alloc_node_params (struct cgraph_node *node, int param_count) | |
201 | { | |
202 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
203 | ||
204 | if (!info->descriptors.exists () && param_count) | |
205 | info->descriptors.safe_grow_cleared (param_count); | |
206 | } | |
207 | ||
f8e2a1ed MJ |
208 | /* Initialize the ipa_node_params structure associated with NODE by counting |
209 | the function parameters, creating the descriptors and populating their | |
210 | param_decls. */ | |
be95e2b9 | 211 | |
f8e2a1ed MJ |
212 | void |
213 | ipa_initialize_node_params (struct cgraph_node *node) | |
214 | { | |
215 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
216 | ||
9771b263 | 217 | if (!info->descriptors.exists ()) |
f8e2a1ed | 218 | { |
67348ccc | 219 | ipa_alloc_node_params (node, count_formal_params (node->decl)); |
0e8853ee | 220 | ipa_populate_param_decls (node, info->descriptors); |
f8e2a1ed | 221 | } |
518dc859 RL |
222 | } |
223 | ||
749aa96d MJ |
224 | /* Print the jump functions associated with call graph edge CS to file F. */ |
225 | ||
226 | static void | |
227 | ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs) | |
228 | { | |
229 | int i, count; | |
230 | ||
231 | count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs)); | |
232 | for (i = 0; i < count; i++) | |
233 | { | |
234 | struct ipa_jump_func *jump_func; | |
235 | enum jump_func_type type; | |
236 | ||
237 | jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i); | |
238 | type = jump_func->type; | |
239 | ||
240 | fprintf (f, " param %d: ", i); | |
241 | if (type == IPA_JF_UNKNOWN) | |
242 | fprintf (f, "UNKNOWN\n"); | |
243 | else if (type == IPA_JF_KNOWN_TYPE) | |
244 | { | |
c7573249 MJ |
245 | fprintf (f, "KNOWN TYPE: base "); |
246 | print_generic_expr (f, jump_func->value.known_type.base_type, 0); | |
247 | fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ", | |
248 | jump_func->value.known_type.offset); | |
249 | print_generic_expr (f, jump_func->value.known_type.component_type, 0); | |
250 | fprintf (f, "\n"); | |
749aa96d MJ |
251 | } |
252 | else if (type == IPA_JF_CONST) | |
253 | { | |
4502fe8d | 254 | tree val = jump_func->value.constant.value; |
749aa96d MJ |
255 | fprintf (f, "CONST: "); |
256 | print_generic_expr (f, val, 0); | |
257 | if (TREE_CODE (val) == ADDR_EXPR | |
258 | && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL) | |
259 | { | |
260 | fprintf (f, " -> "); | |
261 | print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)), | |
262 | 0); | |
263 | } | |
264 | fprintf (f, "\n"); | |
265 | } | |
749aa96d MJ |
266 | else if (type == IPA_JF_PASS_THROUGH) |
267 | { | |
268 | fprintf (f, "PASS THROUGH: "); | |
8b7773a4 | 269 | fprintf (f, "%d, op %s", |
749aa96d | 270 | jump_func->value.pass_through.formal_id, |
5806f481 | 271 | get_tree_code_name(jump_func->value.pass_through.operation)); |
749aa96d | 272 | if (jump_func->value.pass_through.operation != NOP_EXPR) |
8b7773a4 MJ |
273 | { |
274 | fprintf (f, " "); | |
275 | print_generic_expr (f, | |
276 | jump_func->value.pass_through.operand, 0); | |
277 | } | |
278 | if (jump_func->value.pass_through.agg_preserved) | |
279 | fprintf (f, ", agg_preserved"); | |
b8f6e610 MJ |
280 | if (jump_func->value.pass_through.type_preserved) |
281 | fprintf (f, ", type_preserved"); | |
3ea6239f | 282 | fprintf (f, "\n"); |
749aa96d MJ |
283 | } |
284 | else if (type == IPA_JF_ANCESTOR) | |
285 | { | |
286 | fprintf (f, "ANCESTOR: "); | |
287 | fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ", | |
288 | jump_func->value.ancestor.formal_id, | |
289 | jump_func->value.ancestor.offset); | |
290 | print_generic_expr (f, jump_func->value.ancestor.type, 0); | |
8b7773a4 MJ |
291 | if (jump_func->value.ancestor.agg_preserved) |
292 | fprintf (f, ", agg_preserved"); | |
b8f6e610 MJ |
293 | if (jump_func->value.ancestor.type_preserved) |
294 | fprintf (f, ", type_preserved"); | |
3ea6239f | 295 | fprintf (f, "\n"); |
749aa96d | 296 | } |
8b7773a4 MJ |
297 | |
298 | if (jump_func->agg.items) | |
299 | { | |
300 | struct ipa_agg_jf_item *item; | |
301 | int j; | |
302 | ||
303 | fprintf (f, " Aggregate passed by %s:\n", | |
304 | jump_func->agg.by_ref ? "reference" : "value"); | |
9771b263 | 305 | FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item) |
8b7773a4 MJ |
306 | { |
307 | fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ", | |
308 | item->offset); | |
309 | if (TYPE_P (item->value)) | |
310 | fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits", | |
ae7e9ddd | 311 | tree_to_uhwi (TYPE_SIZE (item->value))); |
8b7773a4 MJ |
312 | else |
313 | { | |
314 | fprintf (f, "cst: "); | |
315 | print_generic_expr (f, item->value, 0); | |
316 | } | |
317 | fprintf (f, "\n"); | |
318 | } | |
319 | } | |
749aa96d MJ |
320 | } |
321 | } | |
322 | ||
323 | ||
be95e2b9 MJ |
324 | /* Print the jump functions of all arguments on all call graph edges going from |
325 | NODE to file F. */ | |
326 | ||
518dc859 | 327 | void |
3e293154 | 328 | ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node) |
518dc859 | 329 | { |
3e293154 | 330 | struct cgraph_edge *cs; |
518dc859 | 331 | |
fec39fa6 | 332 | fprintf (f, " Jump functions of caller %s/%i:\n", node->name (), |
67348ccc | 333 | node->order); |
3e293154 MJ |
334 | for (cs = node->callees; cs; cs = cs->next_callee) |
335 | { | |
336 | if (!ipa_edge_args_info_available_for_edge_p (cs)) | |
337 | continue; | |
338 | ||
749aa96d | 339 | fprintf (f, " callsite %s/%i -> %s/%i : \n", |
fec39fa6 TS |
340 | xstrdup (node->name ()), node->order, |
341 | xstrdup (cs->callee->name ()), | |
67348ccc | 342 | cs->callee->order); |
749aa96d MJ |
343 | ipa_print_node_jump_functions_for_edge (f, cs); |
344 | } | |
518dc859 | 345 | |
9de04252 | 346 | for (cs = node->indirect_calls; cs; cs = cs->next_callee) |
749aa96d | 347 | { |
9de04252 | 348 | struct cgraph_indirect_call_info *ii; |
749aa96d MJ |
349 | if (!ipa_edge_args_info_available_for_edge_p (cs)) |
350 | continue; | |
3e293154 | 351 | |
9de04252 MJ |
352 | ii = cs->indirect_info; |
353 | if (ii->agg_contents) | |
c13bc3d9 | 354 | fprintf (f, " indirect %s callsite, calling param %i, " |
9de04252 | 355 | "offset " HOST_WIDE_INT_PRINT_DEC ", %s", |
c13bc3d9 | 356 | ii->member_ptr ? "member ptr" : "aggregate", |
9de04252 MJ |
357 | ii->param_index, ii->offset, |
358 | ii->by_ref ? "by reference" : "by_value"); | |
359 | else | |
85942f45 JH |
360 | fprintf (f, " indirect %s callsite, calling param %i, " |
361 | "offset " HOST_WIDE_INT_PRINT_DEC, | |
362 | ii->polymorphic ? "polymorphic" : "simple", ii->param_index, | |
363 | ii->offset); | |
9de04252 | 364 | |
749aa96d MJ |
365 | if (cs->call_stmt) |
366 | { | |
9de04252 | 367 | fprintf (f, ", for stmt "); |
749aa96d | 368 | print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM); |
3e293154 | 369 | } |
749aa96d | 370 | else |
9de04252 | 371 | fprintf (f, "\n"); |
749aa96d | 372 | ipa_print_node_jump_functions_for_edge (f, cs); |
3e293154 MJ |
373 | } |
374 | } | |
375 | ||
376 | /* Print ipa_jump_func data structures of all nodes in the call graph to F. */ | |
be95e2b9 | 377 | |
3e293154 MJ |
378 | void |
379 | ipa_print_all_jump_functions (FILE *f) | |
380 | { | |
381 | struct cgraph_node *node; | |
382 | ||
ca30a539 | 383 | fprintf (f, "\nJump functions:\n"); |
65c70e6b | 384 | FOR_EACH_FUNCTION (node) |
3e293154 MJ |
385 | { |
386 | ipa_print_node_jump_functions (f, node); | |
387 | } | |
388 | } | |
389 | ||
7b872d9e MJ |
390 | /* Set JFUNC to be a known type jump function. */ |
391 | ||
392 | static void | |
393 | ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset, | |
394 | tree base_type, tree component_type) | |
395 | { | |
06d65050 JH |
396 | gcc_assert (TREE_CODE (component_type) == RECORD_TYPE |
397 | && TYPE_BINFO (component_type)); | |
0a2550e7 JH |
398 | if (!flag_devirtualize) |
399 | return; | |
400 | gcc_assert (BINFO_VTABLE (TYPE_BINFO (component_type))); | |
7b872d9e MJ |
401 | jfunc->type = IPA_JF_KNOWN_TYPE; |
402 | jfunc->value.known_type.offset = offset, | |
403 | jfunc->value.known_type.base_type = base_type; | |
404 | jfunc->value.known_type.component_type = component_type; | |
68377e53 | 405 | gcc_assert (component_type); |
7b872d9e MJ |
406 | } |
407 | ||
b8f6e610 MJ |
408 | /* Set JFUNC to be a copy of another jmp (to be used by jump function |
409 | combination code). The two functions will share their rdesc. */ | |
410 | ||
411 | static void | |
412 | ipa_set_jf_cst_copy (struct ipa_jump_func *dst, | |
413 | struct ipa_jump_func *src) | |
414 | ||
415 | { | |
416 | gcc_checking_assert (src->type == IPA_JF_CONST); | |
417 | dst->type = IPA_JF_CONST; | |
418 | dst->value.constant = src->value.constant; | |
419 | } | |
420 | ||
7b872d9e MJ |
421 | /* Set JFUNC to be a constant jmp function. */ |
422 | ||
423 | static void | |
4502fe8d MJ |
424 | ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant, |
425 | struct cgraph_edge *cs) | |
7b872d9e | 426 | { |
5368224f DC |
427 | constant = unshare_expr (constant); |
428 | if (constant && EXPR_P (constant)) | |
429 | SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION); | |
7b872d9e | 430 | jfunc->type = IPA_JF_CONST; |
4502fe8d MJ |
431 | jfunc->value.constant.value = unshare_expr_without_location (constant); |
432 | ||
433 | if (TREE_CODE (constant) == ADDR_EXPR | |
434 | && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL) | |
435 | { | |
436 | struct ipa_cst_ref_desc *rdesc; | |
437 | if (!ipa_refdesc_pool) | |
438 | ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions", | |
439 | sizeof (struct ipa_cst_ref_desc), 32); | |
440 | ||
441 | rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool); | |
442 | rdesc->cs = cs; | |
443 | rdesc->next_duplicate = NULL; | |
444 | rdesc->refcount = 1; | |
445 | jfunc->value.constant.rdesc = rdesc; | |
446 | } | |
447 | else | |
448 | jfunc->value.constant.rdesc = NULL; | |
7b872d9e MJ |
449 | } |
450 | ||
451 | /* Set JFUNC to be a simple pass-through jump function. */ | |
452 | static void | |
8b7773a4 | 453 | ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id, |
b8f6e610 | 454 | bool agg_preserved, bool type_preserved) |
7b872d9e MJ |
455 | { |
456 | jfunc->type = IPA_JF_PASS_THROUGH; | |
457 | jfunc->value.pass_through.operand = NULL_TREE; | |
458 | jfunc->value.pass_through.formal_id = formal_id; | |
459 | jfunc->value.pass_through.operation = NOP_EXPR; | |
8b7773a4 | 460 | jfunc->value.pass_through.agg_preserved = agg_preserved; |
b8f6e610 | 461 | jfunc->value.pass_through.type_preserved = type_preserved; |
7b872d9e MJ |
462 | } |
463 | ||
464 | /* Set JFUNC to be an arithmetic pass through jump function. */ | |
465 | ||
466 | static void | |
467 | ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id, | |
468 | tree operand, enum tree_code operation) | |
469 | { | |
470 | jfunc->type = IPA_JF_PASS_THROUGH; | |
d1f98542 | 471 | jfunc->value.pass_through.operand = unshare_expr_without_location (operand); |
7b872d9e MJ |
472 | jfunc->value.pass_through.formal_id = formal_id; |
473 | jfunc->value.pass_through.operation = operation; | |
8b7773a4 | 474 | jfunc->value.pass_through.agg_preserved = false; |
b8f6e610 | 475 | jfunc->value.pass_through.type_preserved = false; |
7b872d9e MJ |
476 | } |
477 | ||
478 | /* Set JFUNC to be an ancestor jump function. */ | |
479 | ||
480 | static void | |
481 | ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset, | |
b8f6e610 MJ |
482 | tree type, int formal_id, bool agg_preserved, |
483 | bool type_preserved) | |
7b872d9e | 484 | { |
0a2550e7 JH |
485 | if (!flag_devirtualize) |
486 | type_preserved = false; | |
487 | gcc_assert (!type_preserved | |
488 | || (TREE_CODE (type) == RECORD_TYPE | |
489 | && TYPE_BINFO (type) | |
490 | && BINFO_VTABLE (TYPE_BINFO (type)))); | |
7b872d9e MJ |
491 | jfunc->type = IPA_JF_ANCESTOR; |
492 | jfunc->value.ancestor.formal_id = formal_id; | |
493 | jfunc->value.ancestor.offset = offset; | |
0a2550e7 | 494 | jfunc->value.ancestor.type = type_preserved ? type : NULL; |
8b7773a4 | 495 | jfunc->value.ancestor.agg_preserved = agg_preserved; |
b8f6e610 | 496 | jfunc->value.ancestor.type_preserved = type_preserved; |
7b872d9e MJ |
497 | } |
498 | ||
e248d83f MJ |
499 | /* Extract the acual BINFO being described by JFUNC which must be a known type |
500 | jump function. */ | |
501 | ||
502 | tree | |
503 | ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc) | |
504 | { | |
505 | tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type); | |
506 | if (!base_binfo) | |
507 | return NULL_TREE; | |
508 | return get_binfo_at_offset (base_binfo, | |
509 | jfunc->value.known_type.offset, | |
510 | jfunc->value.known_type.component_type); | |
511 | } | |
512 | ||
f65cf2b7 MJ |
513 | /* Structure to be passed in between detect_type_change and |
514 | check_stmt_for_type_change. */ | |
515 | ||
516 | struct type_change_info | |
517 | { | |
290ebcb7 MJ |
518 | /* Offset into the object where there is the virtual method pointer we are |
519 | looking for. */ | |
520 | HOST_WIDE_INT offset; | |
521 | /* The declaration or SSA_NAME pointer of the base that we are checking for | |
522 | type change. */ | |
523 | tree object; | |
524 | /* If we actually can tell the type that the object has changed to, it is | |
525 | stored in this field. Otherwise it remains NULL_TREE. */ | |
526 | tree known_current_type; | |
f65cf2b7 MJ |
527 | /* Set to true if dynamic type change has been detected. */ |
528 | bool type_maybe_changed; | |
290ebcb7 MJ |
529 | /* Set to true if multiple types have been encountered. known_current_type |
530 | must be disregarded in that case. */ | |
531 | bool multiple_types_encountered; | |
f65cf2b7 MJ |
532 | }; |
533 | ||
534 | /* Return true if STMT can modify a virtual method table pointer. | |
535 | ||
536 | This function makes special assumptions about both constructors and | |
537 | destructors which are all the functions that are allowed to alter the VMT | |
538 | pointers. It assumes that destructors begin with assignment into all VMT | |
539 | pointers and that constructors essentially look in the following way: | |
540 | ||
541 | 1) The very first thing they do is that they call constructors of ancestor | |
542 | sub-objects that have them. | |
543 | ||
544 | 2) Then VMT pointers of this and all its ancestors is set to new values | |
545 | corresponding to the type corresponding to the constructor. | |
546 | ||
547 | 3) Only afterwards, other stuff such as constructor of member sub-objects | |
548 | and the code written by the user is run. Only this may include calling | |
549 | virtual functions, directly or indirectly. | |
550 | ||
551 | There is no way to call a constructor of an ancestor sub-object in any | |
552 | other way. | |
553 | ||
554 | This means that we do not have to care whether constructors get the correct | |
555 | type information because they will always change it (in fact, if we define | |
556 | the type to be given by the VMT pointer, it is undefined). | |
557 | ||
558 | The most important fact to derive from the above is that if, for some | |
559 | statement in the section 3, we try to detect whether the dynamic type has | |
560 | changed, we can safely ignore all calls as we examine the function body | |
561 | backwards until we reach statements in section 2 because these calls cannot | |
562 | be ancestor constructors or destructors (if the input is not bogus) and so | |
563 | do not change the dynamic type (this holds true only for automatically | |
564 | allocated objects but at the moment we devirtualize only these). We then | |
565 | must detect that statements in section 2 change the dynamic type and can try | |
566 | to derive the new type. That is enough and we can stop, we will never see | |
567 | the calls into constructors of sub-objects in this code. Therefore we can | |
568 | safely ignore all call statements that we traverse. | |
569 | */ | |
570 | ||
571 | static bool | |
572 | stmt_may_be_vtbl_ptr_store (gimple stmt) | |
573 | { | |
574 | if (is_gimple_call (stmt)) | |
575 | return false; | |
993df21e | 576 | /* TODO: Skip clobbers, doing so triggers problem in PR60306. */ |
f65cf2b7 MJ |
577 | else if (is_gimple_assign (stmt)) |
578 | { | |
579 | tree lhs = gimple_assign_lhs (stmt); | |
580 | ||
0004f992 MJ |
581 | if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs))) |
582 | { | |
583 | if (flag_strict_aliasing | |
584 | && !POINTER_TYPE_P (TREE_TYPE (lhs))) | |
585 | return false; | |
586 | ||
587 | if (TREE_CODE (lhs) == COMPONENT_REF | |
588 | && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))) | |
f65cf2b7 | 589 | return false; |
0004f992 MJ |
590 | /* In the future we might want to use get_base_ref_and_offset to find |
591 | if there is a field corresponding to the offset and if so, proceed | |
592 | almost like if it was a component ref. */ | |
593 | } | |
f65cf2b7 MJ |
594 | } |
595 | return true; | |
596 | } | |
597 | ||
290ebcb7 MJ |
598 | /* If STMT can be proved to be an assignment to the virtual method table |
599 | pointer of ANALYZED_OBJ and the type associated with the new table | |
600 | identified, return the type. Otherwise return NULL_TREE. */ | |
601 | ||
602 | static tree | |
603 | extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci) | |
604 | { | |
605 | HOST_WIDE_INT offset, size, max_size; | |
390675c8 | 606 | tree lhs, rhs, base, binfo; |
290ebcb7 MJ |
607 | |
608 | if (!gimple_assign_single_p (stmt)) | |
609 | return NULL_TREE; | |
610 | ||
611 | lhs = gimple_assign_lhs (stmt); | |
612 | rhs = gimple_assign_rhs1 (stmt); | |
613 | if (TREE_CODE (lhs) != COMPONENT_REF | |
390675c8 | 614 | || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))) |
290ebcb7 MJ |
615 | return NULL_TREE; |
616 | ||
617 | base = get_ref_base_and_extent (lhs, &offset, &size, &max_size); | |
618 | if (offset != tci->offset | |
619 | || size != POINTER_SIZE | |
620 | || max_size != POINTER_SIZE) | |
621 | return NULL_TREE; | |
622 | if (TREE_CODE (base) == MEM_REF) | |
623 | { | |
624 | if (TREE_CODE (tci->object) != MEM_REF | |
625 | || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0) | |
626 | || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1), | |
627 | TREE_OPERAND (base, 1))) | |
628 | return NULL_TREE; | |
629 | } | |
630 | else if (tci->object != base) | |
631 | return NULL_TREE; | |
632 | ||
390675c8 JH |
633 | binfo = vtable_pointer_value_to_binfo (rhs); |
634 | ||
635 | /* FIXME: vtable_pointer_value_to_binfo may return BINFO of a | |
636 | base of outer type. In this case we would need to either | |
637 | work on binfos or translate it back to outer type and offset. | |
638 | KNOWN_TYPE jump functions are not ready for that, yet. */ | |
639 | if (!binfo || TYPE_BINFO (BINFO_TYPE (binfo)) != binfo) | |
640 | return NULL; | |
641 | ||
642 | return BINFO_TYPE (binfo); | |
290ebcb7 MJ |
643 | } |
644 | ||
61502ca8 | 645 | /* Callback of walk_aliased_vdefs and a helper function for |
f65cf2b7 MJ |
646 | detect_type_change to check whether a particular statement may modify |
647 | the virtual table pointer, and if possible also determine the new type of | |
648 | the (sub-)object. It stores its result into DATA, which points to a | |
649 | type_change_info structure. */ | |
650 | ||
651 | static bool | |
652 | check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data) | |
653 | { | |
654 | gimple stmt = SSA_NAME_DEF_STMT (vdef); | |
655 | struct type_change_info *tci = (struct type_change_info *) data; | |
656 | ||
657 | if (stmt_may_be_vtbl_ptr_store (stmt)) | |
658 | { | |
290ebcb7 MJ |
659 | tree type; |
660 | type = extr_type_from_vtbl_ptr_store (stmt, tci); | |
661 | if (tci->type_maybe_changed | |
662 | && type != tci->known_current_type) | |
663 | tci->multiple_types_encountered = true; | |
664 | tci->known_current_type = type; | |
f65cf2b7 MJ |
665 | tci->type_maybe_changed = true; |
666 | return true; | |
667 | } | |
668 | else | |
669 | return false; | |
670 | } | |
671 | ||
290ebcb7 MJ |
672 | |
673 | ||
06d65050 JH |
674 | /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before |
675 | callsite CALL) by looking for assignments to its virtual table pointer. If | |
676 | it is, return true and fill in the jump function JFUNC with relevant type | |
677 | information or set it to unknown. ARG is the object itself (not a pointer | |
678 | to it, unless dereferenced). BASE is the base of the memory access as | |
679 | returned by get_ref_base_and_extent, as is the offset. */ | |
f65cf2b7 MJ |
680 | |
681 | static bool | |
06d65050 JH |
682 | detect_type_change (tree arg, tree base, tree comp_type, gimple call, |
683 | struct ipa_jump_func *jfunc, HOST_WIDE_INT offset) | |
f65cf2b7 MJ |
684 | { |
685 | struct type_change_info tci; | |
686 | ao_ref ao; | |
687 | ||
688 | gcc_checking_assert (DECL_P (arg) | |
689 | || TREE_CODE (arg) == MEM_REF | |
690 | || handled_component_p (arg)); | |
691 | /* Const calls cannot call virtual methods through VMT and so type changes do | |
692 | not matter. */ | |
06d65050 JH |
693 | if (!flag_devirtualize || !gimple_vuse (call) |
694 | /* Be sure expected_type is polymorphic. */ | |
695 | || !comp_type | |
696 | || TREE_CODE (comp_type) != RECORD_TYPE | |
697 | || !TYPE_BINFO (comp_type) | |
698 | || !BINFO_VTABLE (TYPE_BINFO (comp_type))) | |
0a2550e7 | 699 | return true; |
f65cf2b7 | 700 | |
4bf2a588 JH |
701 | /* C++ methods are not allowed to change THIS pointer unless they |
702 | are constructors or destructors. */ | |
703 | if (TREE_CODE (base) == MEM_REF | |
704 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME | |
705 | && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)) | |
706 | && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0))) == PARM_DECL | |
707 | && TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE | |
708 | && !DECL_CXX_CONSTRUCTOR_P (current_function_decl) | |
709 | && !DECL_CXX_DESTRUCTOR_P (current_function_decl) | |
710 | && (SSA_NAME_VAR (TREE_OPERAND (base, 0)) | |
711 | == DECL_ARGUMENTS (current_function_decl))) | |
712 | return false; | |
713 | ||
dd887943 | 714 | ao_ref_init (&ao, arg); |
f65cf2b7 MJ |
715 | ao.base = base; |
716 | ao.offset = offset; | |
717 | ao.size = POINTER_SIZE; | |
718 | ao.max_size = ao.size; | |
f65cf2b7 | 719 | |
290ebcb7 MJ |
720 | tci.offset = offset; |
721 | tci.object = get_base_address (arg); | |
722 | tci.known_current_type = NULL_TREE; | |
723 | tci.type_maybe_changed = false; | |
724 | tci.multiple_types_encountered = false; | |
725 | ||
f65cf2b7 MJ |
726 | walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change, |
727 | &tci, NULL); | |
728 | if (!tci.type_maybe_changed) | |
729 | return false; | |
730 | ||
290ebcb7 MJ |
731 | if (!tci.known_current_type |
732 | || tci.multiple_types_encountered | |
733 | || offset != 0) | |
734 | jfunc->type = IPA_JF_UNKNOWN; | |
735 | else | |
7b872d9e | 736 | ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type); |
290ebcb7 | 737 | |
f65cf2b7 MJ |
738 | return true; |
739 | } | |
740 | ||
741 | /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer | |
742 | SSA name (its dereference will become the base and the offset is assumed to | |
743 | be zero). */ | |
744 | ||
745 | static bool | |
06d65050 JH |
746 | detect_type_change_ssa (tree arg, tree comp_type, |
747 | gimple call, struct ipa_jump_func *jfunc) | |
f65cf2b7 MJ |
748 | { |
749 | gcc_checking_assert (TREE_CODE (arg) == SSA_NAME); | |
05842ff5 | 750 | if (!flag_devirtualize |
06d65050 | 751 | || !POINTER_TYPE_P (TREE_TYPE (arg))) |
f65cf2b7 MJ |
752 | return false; |
753 | ||
754 | arg = build2 (MEM_REF, ptr_type_node, arg, | |
290ebcb7 | 755 | build_int_cst (ptr_type_node, 0)); |
f65cf2b7 | 756 | |
06d65050 | 757 | return detect_type_change (arg, arg, comp_type, call, jfunc, 0); |
f65cf2b7 MJ |
758 | } |
759 | ||
fdb0e1b4 MJ |
760 | /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the |
761 | boolean variable pointed to by DATA. */ | |
762 | ||
763 | static bool | |
764 | mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED, | |
765 | void *data) | |
766 | { | |
767 | bool *b = (bool *) data; | |
768 | *b = true; | |
769 | return true; | |
770 | } | |
771 | ||
688010ba | 772 | /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve |
8b7773a4 MJ |
773 | a value known not to be modified in this function before reaching the |
774 | statement STMT. PARM_AINFO is a pointer to a structure containing temporary | |
775 | information about the parameter. */ | |
fdb0e1b4 MJ |
776 | |
777 | static bool | |
8b7773a4 MJ |
778 | parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo, |
779 | gimple stmt, tree parm_load) | |
fdb0e1b4 MJ |
780 | { |
781 | bool modified = false; | |
8b7773a4 | 782 | bitmap *visited_stmts; |
fdb0e1b4 MJ |
783 | ao_ref refd; |
784 | ||
8b7773a4 MJ |
785 | if (parm_ainfo && parm_ainfo->parm_modified) |
786 | return false; | |
fdb0e1b4 MJ |
787 | |
788 | gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE); | |
8b7773a4 MJ |
789 | ao_ref_init (&refd, parm_load); |
790 | /* We can cache visited statements only when parm_ainfo is available and when | |
791 | we are looking at a naked load of the whole parameter. */ | |
792 | if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL) | |
793 | visited_stmts = NULL; | |
794 | else | |
795 | visited_stmts = &parm_ainfo->parm_visited_statements; | |
796 | walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified, | |
797 | visited_stmts); | |
798 | if (parm_ainfo && modified) | |
799 | parm_ainfo->parm_modified = true; | |
800 | return !modified; | |
fdb0e1b4 MJ |
801 | } |
802 | ||
803 | /* If STMT is an assignment that loads a value from an parameter declaration, | |
804 | return the index of the parameter in ipa_node_params which has not been | |
805 | modified. Otherwise return -1. */ | |
806 | ||
807 | static int | |
84562394 | 808 | load_from_unmodified_param (vec<ipa_param_descriptor> descriptors, |
fdb0e1b4 MJ |
809 | struct param_analysis_info *parms_ainfo, |
810 | gimple stmt) | |
811 | { | |
812 | int index; | |
813 | tree op1; | |
814 | ||
815 | if (!gimple_assign_single_p (stmt)) | |
816 | return -1; | |
817 | ||
818 | op1 = gimple_assign_rhs1 (stmt); | |
819 | if (TREE_CODE (op1) != PARM_DECL) | |
820 | return -1; | |
821 | ||
d044dd17 | 822 | index = ipa_get_param_decl_index_1 (descriptors, op1); |
fdb0e1b4 | 823 | if (index < 0 |
8b7773a4 MJ |
824 | || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index] |
825 | : NULL, stmt, op1)) | |
fdb0e1b4 MJ |
826 | return -1; |
827 | ||
828 | return index; | |
829 | } | |
f65cf2b7 | 830 | |
8b7773a4 MJ |
831 | /* Return true if memory reference REF loads data that are known to be |
832 | unmodified in this function before reaching statement STMT. PARM_AINFO, if | |
833 | non-NULL, is a pointer to a structure containing temporary information about | |
834 | PARM. */ | |
835 | ||
836 | static bool | |
837 | parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo, | |
838 | gimple stmt, tree ref) | |
839 | { | |
840 | bool modified = false; | |
841 | ao_ref refd; | |
842 | ||
843 | gcc_checking_assert (gimple_vuse (stmt)); | |
844 | if (parm_ainfo && parm_ainfo->ref_modified) | |
845 | return false; | |
846 | ||
847 | ao_ref_init (&refd, ref); | |
848 | walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified, | |
849 | NULL); | |
850 | if (parm_ainfo && modified) | |
851 | parm_ainfo->ref_modified = true; | |
852 | return !modified; | |
853 | } | |
854 | ||
855 | /* Return true if the data pointed to by PARM is known to be unmodified in this | |
856 | function before reaching call statement CALL into which it is passed. | |
857 | PARM_AINFO is a pointer to a structure containing temporary information | |
858 | about PARM. */ | |
859 | ||
860 | static bool | |
861 | parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo, | |
862 | gimple call, tree parm) | |
863 | { | |
864 | bool modified = false; | |
865 | ao_ref refd; | |
866 | ||
867 | /* It's unnecessary to calculate anything about memory contnets for a const | |
868 | function because it is not goin to use it. But do not cache the result | |
869 | either. Also, no such calculations for non-pointers. */ | |
870 | if (!gimple_vuse (call) | |
871 | || !POINTER_TYPE_P (TREE_TYPE (parm))) | |
872 | return false; | |
873 | ||
874 | if (parm_ainfo->pt_modified) | |
875 | return false; | |
876 | ||
877 | ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE); | |
878 | walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified, | |
879 | parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL); | |
880 | if (modified) | |
881 | parm_ainfo->pt_modified = true; | |
882 | return !modified; | |
883 | } | |
884 | ||
885 | /* Return true if we can prove that OP is a memory reference loading unmodified | |
886 | data from an aggregate passed as a parameter and if the aggregate is passed | |
887 | by reference, that the alias type of the load corresponds to the type of the | |
888 | formal parameter (so that we can rely on this type for TBAA in callers). | |
889 | INFO and PARMS_AINFO describe parameters of the current function (but the | |
890 | latter can be NULL), STMT is the load statement. If function returns true, | |
891 | *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset | |
892 | within the aggregate and whether it is a load from a value passed by | |
893 | reference respectively. */ | |
894 | ||
895 | static bool | |
84562394 | 896 | ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor> descriptors, |
8b7773a4 MJ |
897 | struct param_analysis_info *parms_ainfo, gimple stmt, |
898 | tree op, int *index_p, HOST_WIDE_INT *offset_p, | |
3ff2ca23 | 899 | HOST_WIDE_INT *size_p, bool *by_ref_p) |
8b7773a4 MJ |
900 | { |
901 | int index; | |
902 | HOST_WIDE_INT size, max_size; | |
903 | tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size); | |
904 | ||
905 | if (max_size == -1 || max_size != size || *offset_p < 0) | |
906 | return false; | |
907 | ||
908 | if (DECL_P (base)) | |
909 | { | |
d044dd17 | 910 | int index = ipa_get_param_decl_index_1 (descriptors, base); |
8b7773a4 MJ |
911 | if (index >= 0 |
912 | && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index] | |
913 | : NULL, stmt, op)) | |
914 | { | |
915 | *index_p = index; | |
916 | *by_ref_p = false; | |
3ff2ca23 JJ |
917 | if (size_p) |
918 | *size_p = size; | |
8b7773a4 MJ |
919 | return true; |
920 | } | |
921 | return false; | |
922 | } | |
923 | ||
924 | if (TREE_CODE (base) != MEM_REF | |
925 | || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME | |
926 | || !integer_zerop (TREE_OPERAND (base, 1))) | |
927 | return false; | |
928 | ||
929 | if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))) | |
930 | { | |
931 | tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0)); | |
d044dd17 | 932 | index = ipa_get_param_decl_index_1 (descriptors, parm); |
8b7773a4 MJ |
933 | } |
934 | else | |
935 | { | |
936 | /* This branch catches situations where a pointer parameter is not a | |
937 | gimple register, for example: | |
938 | ||
939 | void hip7(S*) (struct S * p) | |
940 | { | |
941 | void (*<T2e4>) (struct S *) D.1867; | |
942 | struct S * p.1; | |
943 | ||
944 | <bb 2>: | |
945 | p.1_1 = p; | |
946 | D.1867_2 = p.1_1->f; | |
947 | D.1867_2 (); | |
948 | gdp = &p; | |
949 | */ | |
950 | ||
951 | gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0)); | |
d044dd17 | 952 | index = load_from_unmodified_param (descriptors, parms_ainfo, def); |
8b7773a4 MJ |
953 | } |
954 | ||
955 | if (index >= 0 | |
956 | && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL, | |
957 | stmt, op)) | |
958 | { | |
959 | *index_p = index; | |
960 | *by_ref_p = true; | |
3ff2ca23 JJ |
961 | if (size_p) |
962 | *size_p = size; | |
8b7773a4 MJ |
963 | return true; |
964 | } | |
965 | return false; | |
966 | } | |
967 | ||
968 | /* Just like the previous function, just without the param_analysis_info | |
969 | pointer, for users outside of this file. */ | |
970 | ||
971 | bool | |
972 | ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt, | |
973 | tree op, int *index_p, HOST_WIDE_INT *offset_p, | |
974 | bool *by_ref_p) | |
975 | { | |
d044dd17 | 976 | return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p, |
3ff2ca23 | 977 | offset_p, NULL, by_ref_p); |
8b7773a4 MJ |
978 | } |
979 | ||
b258210c | 980 | /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result |
fdb0e1b4 MJ |
981 | of an assignment statement STMT, try to determine whether we are actually |
982 | handling any of the following cases and construct an appropriate jump | |
983 | function into JFUNC if so: | |
984 | ||
985 | 1) The passed value is loaded from a formal parameter which is not a gimple | |
986 | register (most probably because it is addressable, the value has to be | |
987 | scalar) and we can guarantee the value has not changed. This case can | |
988 | therefore be described by a simple pass-through jump function. For example: | |
989 | ||
990 | foo (int a) | |
991 | { | |
992 | int a.0; | |
993 | ||
994 | a.0_2 = a; | |
995 | bar (a.0_2); | |
996 | ||
997 | 2) The passed value can be described by a simple arithmetic pass-through | |
998 | jump function. E.g. | |
999 | ||
1000 | foo (int a) | |
1001 | { | |
1002 | int D.2064; | |
1003 | ||
1004 | D.2064_4 = a.1(D) + 4; | |
1005 | bar (D.2064_4); | |
1006 | ||
1007 | This case can also occur in combination of the previous one, e.g.: | |
1008 | ||
1009 | foo (int a, int z) | |
1010 | { | |
1011 | int a.0; | |
1012 | int D.2064; | |
1013 | ||
1014 | a.0_3 = a; | |
1015 | D.2064_4 = a.0_3 + 4; | |
1016 | foo (D.2064_4); | |
1017 | ||
1018 | 3) The passed value is an address of an object within another one (which | |
1019 | also passed by reference). Such situations are described by an ancestor | |
1020 | jump function and describe situations such as: | |
1021 | ||
1022 | B::foo() (struct B * const this) | |
1023 | { | |
1024 | struct A * D.1845; | |
1025 | ||
1026 | D.1845_2 = &this_1(D)->D.1748; | |
1027 | A::bar (D.1845_2); | |
1028 | ||
1029 | INFO is the structure describing individual parameters access different | |
1030 | stages of IPA optimizations. PARMS_AINFO contains the information that is | |
1031 | only needed for intraprocedural analysis. */ | |
685b0d13 MJ |
1032 | |
1033 | static void | |
b258210c | 1034 | compute_complex_assign_jump_func (struct ipa_node_params *info, |
fdb0e1b4 | 1035 | struct param_analysis_info *parms_ainfo, |
b258210c | 1036 | struct ipa_jump_func *jfunc, |
06d65050 JH |
1037 | gimple call, gimple stmt, tree name, |
1038 | tree param_type) | |
685b0d13 MJ |
1039 | { |
1040 | HOST_WIDE_INT offset, size, max_size; | |
fdb0e1b4 | 1041 | tree op1, tc_ssa, base, ssa; |
685b0d13 | 1042 | int index; |
685b0d13 | 1043 | |
685b0d13 | 1044 | op1 = gimple_assign_rhs1 (stmt); |
685b0d13 | 1045 | |
fdb0e1b4 | 1046 | if (TREE_CODE (op1) == SSA_NAME) |
685b0d13 | 1047 | { |
fdb0e1b4 MJ |
1048 | if (SSA_NAME_IS_DEFAULT_DEF (op1)) |
1049 | index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1)); | |
1050 | else | |
d044dd17 | 1051 | index = load_from_unmodified_param (info->descriptors, parms_ainfo, |
fdb0e1b4 MJ |
1052 | SSA_NAME_DEF_STMT (op1)); |
1053 | tc_ssa = op1; | |
1054 | } | |
1055 | else | |
1056 | { | |
d044dd17 | 1057 | index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt); |
fdb0e1b4 MJ |
1058 | tc_ssa = gimple_assign_lhs (stmt); |
1059 | } | |
1060 | ||
1061 | if (index >= 0) | |
1062 | { | |
1063 | tree op2 = gimple_assign_rhs2 (stmt); | |
685b0d13 | 1064 | |
b258210c | 1065 | if (op2) |
685b0d13 | 1066 | { |
b258210c MJ |
1067 | if (!is_gimple_ip_invariant (op2) |
1068 | || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison | |
1069 | && !useless_type_conversion_p (TREE_TYPE (name), | |
1070 | TREE_TYPE (op1)))) | |
1071 | return; | |
1072 | ||
7b872d9e MJ |
1073 | ipa_set_jf_arith_pass_through (jfunc, index, op2, |
1074 | gimple_assign_rhs_code (stmt)); | |
685b0d13 | 1075 | } |
b8f6e610 | 1076 | else if (gimple_assign_single_p (stmt)) |
8b7773a4 MJ |
1077 | { |
1078 | bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index], | |
1079 | call, tc_ssa); | |
06d65050 JH |
1080 | bool type_p = false; |
1081 | ||
1082 | if (param_type && POINTER_TYPE_P (param_type)) | |
1083 | type_p = !detect_type_change_ssa (tc_ssa, TREE_TYPE (param_type), | |
1084 | call, jfunc); | |
b8f6e610 MJ |
1085 | if (type_p || jfunc->type == IPA_JF_UNKNOWN) |
1086 | ipa_set_jf_simple_pass_through (jfunc, index, agg_p, type_p); | |
8b7773a4 | 1087 | } |
685b0d13 MJ |
1088 | return; |
1089 | } | |
1090 | ||
1091 | if (TREE_CODE (op1) != ADDR_EXPR) | |
1092 | return; | |
1093 | op1 = TREE_OPERAND (op1, 0); | |
f65cf2b7 | 1094 | if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE) |
b258210c | 1095 | return; |
32aa622c MJ |
1096 | base = get_ref_base_and_extent (op1, &offset, &size, &max_size); |
1097 | if (TREE_CODE (base) != MEM_REF | |
1a15bfdc RG |
1098 | /* If this is a varying address, punt. */ |
1099 | || max_size == -1 | |
1100 | || max_size != size) | |
685b0d13 | 1101 | return; |
807e902e | 1102 | offset += mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT; |
f65cf2b7 MJ |
1103 | ssa = TREE_OPERAND (base, 0); |
1104 | if (TREE_CODE (ssa) != SSA_NAME | |
1105 | || !SSA_NAME_IS_DEFAULT_DEF (ssa) | |
280fedf0 | 1106 | || offset < 0) |
685b0d13 MJ |
1107 | return; |
1108 | ||
b8f6e610 | 1109 | /* Dynamic types are changed in constructors and destructors. */ |
f65cf2b7 | 1110 | index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa)); |
06d65050 | 1111 | if (index >= 0 && param_type && POINTER_TYPE_P (param_type)) |
b8f6e610 | 1112 | { |
06d65050 JH |
1113 | bool type_p = !detect_type_change (op1, base, TREE_TYPE (param_type), |
1114 | call, jfunc, offset); | |
b8f6e610 | 1115 | if (type_p || jfunc->type == IPA_JF_UNKNOWN) |
0a2550e7 JH |
1116 | ipa_set_ancestor_jf (jfunc, offset, |
1117 | type_p ? TREE_TYPE (param_type) : NULL, index, | |
b8f6e610 MJ |
1118 | parm_ref_data_pass_through_p (&parms_ainfo[index], |
1119 | call, ssa), type_p); | |
1120 | } | |
685b0d13 MJ |
1121 | } |
1122 | ||
40591473 MJ |
1123 | /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if |
1124 | it looks like: | |
1125 | ||
1126 | iftmp.1_3 = &obj_2(D)->D.1762; | |
1127 | ||
1128 | The base of the MEM_REF must be a default definition SSA NAME of a | |
1129 | parameter. Return NULL_TREE if it looks otherwise. If case of success, the | |
1130 | whole MEM_REF expression is returned and the offset calculated from any | |
1131 | handled components and the MEM_REF itself is stored into *OFFSET. The whole | |
1132 | RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */ | |
1133 | ||
1134 | static tree | |
1135 | get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset) | |
1136 | { | |
1137 | HOST_WIDE_INT size, max_size; | |
1138 | tree expr, parm, obj; | |
1139 | ||
1140 | if (!gimple_assign_single_p (assign)) | |
1141 | return NULL_TREE; | |
1142 | expr = gimple_assign_rhs1 (assign); | |
1143 | ||
1144 | if (TREE_CODE (expr) != ADDR_EXPR) | |
1145 | return NULL_TREE; | |
1146 | expr = TREE_OPERAND (expr, 0); | |
1147 | obj = expr; | |
1148 | expr = get_ref_base_and_extent (expr, offset, &size, &max_size); | |
1149 | ||
1150 | if (TREE_CODE (expr) != MEM_REF | |
1151 | /* If this is a varying address, punt. */ | |
1152 | || max_size == -1 | |
1153 | || max_size != size | |
1154 | || *offset < 0) | |
1155 | return NULL_TREE; | |
1156 | parm = TREE_OPERAND (expr, 0); | |
1157 | if (TREE_CODE (parm) != SSA_NAME | |
1158 | || !SSA_NAME_IS_DEFAULT_DEF (parm) | |
1159 | || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL) | |
1160 | return NULL_TREE; | |
1161 | ||
807e902e | 1162 | *offset += mem_ref_offset (expr).to_short_addr () * BITS_PER_UNIT; |
40591473 MJ |
1163 | *obj_p = obj; |
1164 | return expr; | |
1165 | } | |
1166 | ||
685b0d13 | 1167 | |
b258210c MJ |
1168 | /* Given that an actual argument is an SSA_NAME that is a result of a phi |
1169 | statement PHI, try to find out whether NAME is in fact a | |
1170 | multiple-inheritance typecast from a descendant into an ancestor of a formal | |
1171 | parameter and thus can be described by an ancestor jump function and if so, | |
1172 | write the appropriate function into JFUNC. | |
1173 | ||
1174 | Essentially we want to match the following pattern: | |
1175 | ||
1176 | if (obj_2(D) != 0B) | |
1177 | goto <bb 3>; | |
1178 | else | |
1179 | goto <bb 4>; | |
1180 | ||
1181 | <bb 3>: | |
1182 | iftmp.1_3 = &obj_2(D)->D.1762; | |
1183 | ||
1184 | <bb 4>: | |
1185 | # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)> | |
1186 | D.1879_6 = middleman_1 (iftmp.1_1, i_5(D)); | |
1187 | return D.1879_6; */ | |
1188 | ||
1189 | static void | |
1190 | compute_complex_ancestor_jump_func (struct ipa_node_params *info, | |
8b7773a4 | 1191 | struct param_analysis_info *parms_ainfo, |
b258210c | 1192 | struct ipa_jump_func *jfunc, |
06d65050 | 1193 | gimple call, gimple phi, tree param_type) |
b258210c | 1194 | { |
40591473 | 1195 | HOST_WIDE_INT offset; |
b258210c MJ |
1196 | gimple assign, cond; |
1197 | basic_block phi_bb, assign_bb, cond_bb; | |
f65cf2b7 | 1198 | tree tmp, parm, expr, obj; |
b258210c MJ |
1199 | int index, i; |
1200 | ||
54e348cb | 1201 | if (gimple_phi_num_args (phi) != 2) |
b258210c MJ |
1202 | return; |
1203 | ||
54e348cb MJ |
1204 | if (integer_zerop (PHI_ARG_DEF (phi, 1))) |
1205 | tmp = PHI_ARG_DEF (phi, 0); | |
1206 | else if (integer_zerop (PHI_ARG_DEF (phi, 0))) | |
1207 | tmp = PHI_ARG_DEF (phi, 1); | |
1208 | else | |
1209 | return; | |
b258210c MJ |
1210 | if (TREE_CODE (tmp) != SSA_NAME |
1211 | || SSA_NAME_IS_DEFAULT_DEF (tmp) | |
1212 | || !POINTER_TYPE_P (TREE_TYPE (tmp)) | |
1213 | || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE) | |
1214 | return; | |
1215 | ||
1216 | assign = SSA_NAME_DEF_STMT (tmp); | |
1217 | assign_bb = gimple_bb (assign); | |
40591473 | 1218 | if (!single_pred_p (assign_bb)) |
b258210c | 1219 | return; |
40591473 MJ |
1220 | expr = get_ancestor_addr_info (assign, &obj, &offset); |
1221 | if (!expr) | |
b258210c MJ |
1222 | return; |
1223 | parm = TREE_OPERAND (expr, 0); | |
b258210c | 1224 | index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm)); |
20afe640 EB |
1225 | if (index < 0) |
1226 | return; | |
b258210c MJ |
1227 | |
1228 | cond_bb = single_pred (assign_bb); | |
1229 | cond = last_stmt (cond_bb); | |
69610617 SB |
1230 | if (!cond |
1231 | || gimple_code (cond) != GIMPLE_COND | |
b258210c MJ |
1232 | || gimple_cond_code (cond) != NE_EXPR |
1233 | || gimple_cond_lhs (cond) != parm | |
1234 | || !integer_zerop (gimple_cond_rhs (cond))) | |
1235 | return; | |
1236 | ||
b258210c MJ |
1237 | phi_bb = gimple_bb (phi); |
1238 | for (i = 0; i < 2; i++) | |
1239 | { | |
1240 | basic_block pred = EDGE_PRED (phi_bb, i)->src; | |
1241 | if (pred != assign_bb && pred != cond_bb) | |
1242 | return; | |
1243 | } | |
1244 | ||
06d65050 JH |
1245 | bool type_p = false; |
1246 | if (param_type && POINTER_TYPE_P (param_type)) | |
1247 | type_p = !detect_type_change (obj, expr, TREE_TYPE (param_type), | |
1248 | call, jfunc, offset); | |
b8f6e610 | 1249 | if (type_p || jfunc->type == IPA_JF_UNKNOWN) |
0a2550e7 | 1250 | ipa_set_ancestor_jf (jfunc, offset, type_p ? TREE_TYPE (param_type) : NULL, index, |
8b7773a4 | 1251 | parm_ref_data_pass_through_p (&parms_ainfo[index], |
b8f6e610 | 1252 | call, parm), type_p); |
b258210c MJ |
1253 | } |
1254 | ||
61502ca8 | 1255 | /* Given OP which is passed as an actual argument to a called function, |
b258210c | 1256 | determine if it is possible to construct a KNOWN_TYPE jump function for it |
06d65050 JH |
1257 | and if so, create one and store it to JFUNC. |
1258 | EXPECTED_TYPE represents a type the argument should be in */ | |
b258210c MJ |
1259 | |
1260 | static void | |
f65cf2b7 | 1261 | compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc, |
06d65050 | 1262 | gimple call, tree expected_type) |
b258210c | 1263 | { |
32aa622c | 1264 | HOST_WIDE_INT offset, size, max_size; |
c7573249 | 1265 | tree base; |
b258210c | 1266 | |
05842ff5 MJ |
1267 | if (!flag_devirtualize |
1268 | || TREE_CODE (op) != ADDR_EXPR | |
06d65050 JH |
1269 | || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE |
1270 | /* Be sure expected_type is polymorphic. */ | |
1271 | || !expected_type | |
1272 | || TREE_CODE (expected_type) != RECORD_TYPE | |
1273 | || !TYPE_BINFO (expected_type) | |
1274 | || !BINFO_VTABLE (TYPE_BINFO (expected_type))) | |
b258210c MJ |
1275 | return; |
1276 | ||
1277 | op = TREE_OPERAND (op, 0); | |
32aa622c MJ |
1278 | base = get_ref_base_and_extent (op, &offset, &size, &max_size); |
1279 | if (!DECL_P (base) | |
1280 | || max_size == -1 | |
1281 | || max_size != size | |
1282 | || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE | |
1283 | || is_global_var (base)) | |
1284 | return; | |
1285 | ||
06d65050 | 1286 | if (detect_type_change (op, base, expected_type, call, jfunc, offset)) |
f65cf2b7 MJ |
1287 | return; |
1288 | ||
06d65050 JH |
1289 | ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base), |
1290 | expected_type); | |
b258210c MJ |
1291 | } |
1292 | ||
be95e2b9 MJ |
1293 | /* Inspect the given TYPE and return true iff it has the same structure (the |
1294 | same number of fields of the same types) as a C++ member pointer. If | |
1295 | METHOD_PTR and DELTA are non-NULL, store the trees representing the | |
1296 | corresponding fields there. */ | |
1297 | ||
3e293154 MJ |
1298 | static bool |
1299 | type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta) | |
1300 | { | |
1301 | tree fld; | |
1302 | ||
1303 | if (TREE_CODE (type) != RECORD_TYPE) | |
1304 | return false; | |
1305 | ||
1306 | fld = TYPE_FIELDS (type); | |
1307 | if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld)) | |
8b7773a4 | 1308 | || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE |
cc269bb6 | 1309 | || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld))) |
3e293154 MJ |
1310 | return false; |
1311 | ||
1312 | if (method_ptr) | |
1313 | *method_ptr = fld; | |
1314 | ||
910ad8de | 1315 | fld = DECL_CHAIN (fld); |
8b7773a4 | 1316 | if (!fld || INTEGRAL_TYPE_P (fld) |
cc269bb6 | 1317 | || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld))) |
3e293154 MJ |
1318 | return false; |
1319 | if (delta) | |
1320 | *delta = fld; | |
1321 | ||
910ad8de | 1322 | if (DECL_CHAIN (fld)) |
3e293154 MJ |
1323 | return false; |
1324 | ||
1325 | return true; | |
1326 | } | |
1327 | ||
61502ca8 | 1328 | /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement, |
8b7773a4 MJ |
1329 | return the rhs of its defining statement. Otherwise return RHS as it |
1330 | is. */ | |
7ec49257 MJ |
1331 | |
1332 | static inline tree | |
1333 | get_ssa_def_if_simple_copy (tree rhs) | |
1334 | { | |
1335 | while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs)) | |
1336 | { | |
1337 | gimple def_stmt = SSA_NAME_DEF_STMT (rhs); | |
1338 | ||
1339 | if (gimple_assign_single_p (def_stmt)) | |
1340 | rhs = gimple_assign_rhs1 (def_stmt); | |
9961eb45 MJ |
1341 | else |
1342 | break; | |
7ec49257 MJ |
1343 | } |
1344 | return rhs; | |
1345 | } | |
1346 | ||
8b7773a4 MJ |
1347 | /* Simple linked list, describing known contents of an aggregate beforere |
1348 | call. */ | |
1349 | ||
1350 | struct ipa_known_agg_contents_list | |
1351 | { | |
1352 | /* Offset and size of the described part of the aggregate. */ | |
1353 | HOST_WIDE_INT offset, size; | |
1354 | /* Known constant value or NULL if the contents is known to be unknown. */ | |
1355 | tree constant; | |
1356 | /* Pointer to the next structure in the list. */ | |
1357 | struct ipa_known_agg_contents_list *next; | |
1358 | }; | |
3e293154 | 1359 | |
8b7773a4 MJ |
1360 | /* Traverse statements from CALL backwards, scanning whether an aggregate given |
1361 | in ARG is filled in with constant values. ARG can either be an aggregate | |
85942f45 JH |
1362 | expression or a pointer to an aggregate. ARG_TYPE is the type of the aggregate. |
1363 | JFUNC is the jump function into which the constants are subsequently stored. */ | |
be95e2b9 | 1364 | |
3e293154 | 1365 | static void |
85942f45 | 1366 | determine_known_aggregate_parts (gimple call, tree arg, tree arg_type, |
8b7773a4 | 1367 | struct ipa_jump_func *jfunc) |
3e293154 | 1368 | { |
8b7773a4 MJ |
1369 | struct ipa_known_agg_contents_list *list = NULL; |
1370 | int item_count = 0, const_count = 0; | |
1371 | HOST_WIDE_INT arg_offset, arg_size; | |
726a989a | 1372 | gimple_stmt_iterator gsi; |
8b7773a4 MJ |
1373 | tree arg_base; |
1374 | bool check_ref, by_ref; | |
1375 | ao_ref r; | |
3e293154 | 1376 | |
8b7773a4 MJ |
1377 | /* The function operates in three stages. First, we prepare check_ref, r, |
1378 | arg_base and arg_offset based on what is actually passed as an actual | |
1379 | argument. */ | |
3e293154 | 1380 | |
85942f45 | 1381 | if (POINTER_TYPE_P (arg_type)) |
8b7773a4 MJ |
1382 | { |
1383 | by_ref = true; | |
1384 | if (TREE_CODE (arg) == SSA_NAME) | |
1385 | { | |
1386 | tree type_size; | |
85942f45 | 1387 | if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (arg_type)))) |
8b7773a4 MJ |
1388 | return; |
1389 | check_ref = true; | |
1390 | arg_base = arg; | |
1391 | arg_offset = 0; | |
85942f45 | 1392 | type_size = TYPE_SIZE (TREE_TYPE (arg_type)); |
ae7e9ddd | 1393 | arg_size = tree_to_uhwi (type_size); |
8b7773a4 MJ |
1394 | ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE); |
1395 | } | |
1396 | else if (TREE_CODE (arg) == ADDR_EXPR) | |
1397 | { | |
1398 | HOST_WIDE_INT arg_max_size; | |
1399 | ||
1400 | arg = TREE_OPERAND (arg, 0); | |
1401 | arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size, | |
1402 | &arg_max_size); | |
1403 | if (arg_max_size == -1 | |
1404 | || arg_max_size != arg_size | |
1405 | || arg_offset < 0) | |
1406 | return; | |
1407 | if (DECL_P (arg_base)) | |
1408 | { | |
1409 | tree size; | |
1410 | check_ref = false; | |
1411 | size = build_int_cst (integer_type_node, arg_size); | |
1412 | ao_ref_init_from_ptr_and_size (&r, arg_base, size); | |
1413 | } | |
1414 | else | |
1415 | return; | |
1416 | } | |
1417 | else | |
1418 | return; | |
1419 | } | |
1420 | else | |
1421 | { | |
1422 | HOST_WIDE_INT arg_max_size; | |
1423 | ||
1424 | gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg))); | |
1425 | ||
1426 | by_ref = false; | |
1427 | check_ref = false; | |
1428 | arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size, | |
1429 | &arg_max_size); | |
1430 | if (arg_max_size == -1 | |
1431 | || arg_max_size != arg_size | |
1432 | || arg_offset < 0) | |
1433 | return; | |
1434 | ||
1435 | ao_ref_init (&r, arg); | |
1436 | } | |
1437 | ||
1438 | /* Second stage walks back the BB, looks at individual statements and as long | |
1439 | as it is confident of how the statements affect contents of the | |
1440 | aggregates, it builds a sorted linked list of ipa_agg_jf_list structures | |
1441 | describing it. */ | |
1442 | gsi = gsi_for_stmt (call); | |
726a989a RB |
1443 | gsi_prev (&gsi); |
1444 | for (; !gsi_end_p (gsi); gsi_prev (&gsi)) | |
3e293154 | 1445 | { |
8b7773a4 | 1446 | struct ipa_known_agg_contents_list *n, **p; |
726a989a | 1447 | gimple stmt = gsi_stmt (gsi); |
8b7773a4 MJ |
1448 | HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size; |
1449 | tree lhs, rhs, lhs_base; | |
1450 | bool partial_overlap; | |
3e293154 | 1451 | |
8b7773a4 | 1452 | if (!stmt_may_clobber_ref_p_1 (stmt, &r)) |
8aa29647 | 1453 | continue; |
8b75fc9b | 1454 | if (!gimple_assign_single_p (stmt)) |
8b7773a4 | 1455 | break; |
3e293154 | 1456 | |
726a989a RB |
1457 | lhs = gimple_assign_lhs (stmt); |
1458 | rhs = gimple_assign_rhs1 (stmt); | |
0c6b087c | 1459 | if (!is_gimple_reg_type (TREE_TYPE (rhs)) |
7d2fb524 MJ |
1460 | || TREE_CODE (lhs) == BIT_FIELD_REF |
1461 | || contains_bitfld_component_ref_p (lhs)) | |
8b7773a4 | 1462 | break; |
3e293154 | 1463 | |
8b7773a4 MJ |
1464 | lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size, |
1465 | &lhs_max_size); | |
1466 | if (lhs_max_size == -1 | |
1467 | || lhs_max_size != lhs_size | |
1468 | || (lhs_offset < arg_offset | |
1469 | && lhs_offset + lhs_size > arg_offset) | |
1470 | || (lhs_offset < arg_offset + arg_size | |
1471 | && lhs_offset + lhs_size > arg_offset + arg_size)) | |
1472 | break; | |
3e293154 | 1473 | |
8b7773a4 | 1474 | if (check_ref) |
518dc859 | 1475 | { |
8b7773a4 MJ |
1476 | if (TREE_CODE (lhs_base) != MEM_REF |
1477 | || TREE_OPERAND (lhs_base, 0) != arg_base | |
1478 | || !integer_zerop (TREE_OPERAND (lhs_base, 1))) | |
1479 | break; | |
3e293154 | 1480 | } |
8b7773a4 | 1481 | else if (lhs_base != arg_base) |
774b8a55 MJ |
1482 | { |
1483 | if (DECL_P (lhs_base)) | |
1484 | continue; | |
1485 | else | |
1486 | break; | |
1487 | } | |
3e293154 | 1488 | |
8b7773a4 MJ |
1489 | if (lhs_offset + lhs_size < arg_offset |
1490 | || lhs_offset >= (arg_offset + arg_size)) | |
1491 | continue; | |
1492 | ||
1493 | partial_overlap = false; | |
1494 | p = &list; | |
1495 | while (*p && (*p)->offset < lhs_offset) | |
3e293154 | 1496 | { |
8b7773a4 | 1497 | if ((*p)->offset + (*p)->size > lhs_offset) |
3e293154 | 1498 | { |
8b7773a4 MJ |
1499 | partial_overlap = true; |
1500 | break; | |
3e293154 | 1501 | } |
8b7773a4 MJ |
1502 | p = &(*p)->next; |
1503 | } | |
1504 | if (partial_overlap) | |
1505 | break; | |
1506 | if (*p && (*p)->offset < lhs_offset + lhs_size) | |
1507 | { | |
1508 | if ((*p)->offset == lhs_offset && (*p)->size == lhs_size) | |
1509 | /* We already know this value is subsequently overwritten with | |
1510 | something else. */ | |
1511 | continue; | |
3e293154 | 1512 | else |
8b7773a4 MJ |
1513 | /* Otherwise this is a partial overlap which we cannot |
1514 | represent. */ | |
1515 | break; | |
3e293154 | 1516 | } |
3e293154 | 1517 | |
8b7773a4 MJ |
1518 | rhs = get_ssa_def_if_simple_copy (rhs); |
1519 | n = XALLOCA (struct ipa_known_agg_contents_list); | |
1520 | n->size = lhs_size; | |
1521 | n->offset = lhs_offset; | |
1522 | if (is_gimple_ip_invariant (rhs)) | |
1523 | { | |
1524 | n->constant = rhs; | |
1525 | const_count++; | |
1526 | } | |
1527 | else | |
1528 | n->constant = NULL_TREE; | |
1529 | n->next = *p; | |
1530 | *p = n; | |
3e293154 | 1531 | |
8b7773a4 | 1532 | item_count++; |
dfea20f1 MJ |
1533 | if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS) |
1534 | || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)) | |
8b7773a4 MJ |
1535 | break; |
1536 | } | |
be95e2b9 | 1537 | |
8b7773a4 MJ |
1538 | /* Third stage just goes over the list and creates an appropriate vector of |
1539 | ipa_agg_jf_item structures out of it, of sourse only if there are | |
1540 | any known constants to begin with. */ | |
3e293154 | 1541 | |
8b7773a4 | 1542 | if (const_count) |
3e293154 | 1543 | { |
8b7773a4 | 1544 | jfunc->agg.by_ref = by_ref; |
9771b263 | 1545 | vec_alloc (jfunc->agg.items, const_count); |
8b7773a4 MJ |
1546 | while (list) |
1547 | { | |
1548 | if (list->constant) | |
1549 | { | |
f32682ca DN |
1550 | struct ipa_agg_jf_item item; |
1551 | item.offset = list->offset - arg_offset; | |
7d2fb524 | 1552 | gcc_assert ((item.offset % BITS_PER_UNIT) == 0); |
d1f98542 | 1553 | item.value = unshare_expr_without_location (list->constant); |
9771b263 | 1554 | jfunc->agg.items->quick_push (item); |
8b7773a4 MJ |
1555 | } |
1556 | list = list->next; | |
1557 | } | |
3e293154 MJ |
1558 | } |
1559 | } | |
1560 | ||
06d65050 JH |
1561 | static tree |
1562 | ipa_get_callee_param_type (struct cgraph_edge *e, int i) | |
1563 | { | |
1564 | int n; | |
1565 | tree type = (e->callee | |
67348ccc | 1566 | ? TREE_TYPE (e->callee->decl) |
06d65050 JH |
1567 | : gimple_call_fntype (e->call_stmt)); |
1568 | tree t = TYPE_ARG_TYPES (type); | |
1569 | ||
1570 | for (n = 0; n < i; n++) | |
1571 | { | |
1572 | if (!t) | |
1573 | break; | |
1574 | t = TREE_CHAIN (t); | |
1575 | } | |
1576 | if (t) | |
1577 | return TREE_VALUE (t); | |
1578 | if (!e->callee) | |
1579 | return NULL; | |
67348ccc | 1580 | t = DECL_ARGUMENTS (e->callee->decl); |
06d65050 JH |
1581 | for (n = 0; n < i; n++) |
1582 | { | |
1583 | if (!t) | |
1584 | return NULL; | |
1585 | t = TREE_CHAIN (t); | |
1586 | } | |
1587 | if (t) | |
1588 | return TREE_TYPE (t); | |
1589 | return NULL; | |
1590 | } | |
1591 | ||
3e293154 MJ |
1592 | /* Compute jump function for all arguments of callsite CS and insert the |
1593 | information in the jump_functions array in the ipa_edge_args corresponding | |
1594 | to this callsite. */ | |
be95e2b9 | 1595 | |
749aa96d | 1596 | static void |
c419671c | 1597 | ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo, |
062c604f | 1598 | struct cgraph_edge *cs) |
3e293154 MJ |
1599 | { |
1600 | struct ipa_node_params *info = IPA_NODE_REF (cs->caller); | |
606d9a09 MJ |
1601 | struct ipa_edge_args *args = IPA_EDGE_REF (cs); |
1602 | gimple call = cs->call_stmt; | |
8b7773a4 | 1603 | int n, arg_num = gimple_call_num_args (call); |
3e293154 | 1604 | |
606d9a09 | 1605 | if (arg_num == 0 || args->jump_functions) |
3e293154 | 1606 | return; |
9771b263 | 1607 | vec_safe_grow_cleared (args->jump_functions, arg_num); |
3e293154 | 1608 | |
96e24d49 JJ |
1609 | if (gimple_call_internal_p (call)) |
1610 | return; | |
5fe8e757 MJ |
1611 | if (ipa_func_spec_opts_forbid_analysis_p (cs->caller)) |
1612 | return; | |
1613 | ||
8b7773a4 MJ |
1614 | for (n = 0; n < arg_num; n++) |
1615 | { | |
1616 | struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n); | |
1617 | tree arg = gimple_call_arg (call, n); | |
06d65050 | 1618 | tree param_type = ipa_get_callee_param_type (cs, n); |
3e293154 | 1619 | |
8b7773a4 | 1620 | if (is_gimple_ip_invariant (arg)) |
4502fe8d | 1621 | ipa_set_jf_constant (jfunc, arg, cs); |
8b7773a4 MJ |
1622 | else if (!is_gimple_reg_type (TREE_TYPE (arg)) |
1623 | && TREE_CODE (arg) == PARM_DECL) | |
1624 | { | |
1625 | int index = ipa_get_param_decl_index (info, arg); | |
1626 | ||
1627 | gcc_assert (index >=0); | |
1628 | /* Aggregate passed by value, check for pass-through, otherwise we | |
1629 | will attempt to fill in aggregate contents later in this | |
1630 | for cycle. */ | |
1631 | if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg)) | |
1632 | { | |
b8f6e610 | 1633 | ipa_set_jf_simple_pass_through (jfunc, index, false, false); |
8b7773a4 MJ |
1634 | continue; |
1635 | } | |
1636 | } | |
1637 | else if (TREE_CODE (arg) == SSA_NAME) | |
1638 | { | |
1639 | if (SSA_NAME_IS_DEFAULT_DEF (arg)) | |
1640 | { | |
1641 | int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg)); | |
b8f6e610 | 1642 | if (index >= 0) |
8b7773a4 | 1643 | { |
b8f6e610 | 1644 | bool agg_p, type_p; |
8b7773a4 MJ |
1645 | agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index], |
1646 | call, arg); | |
06d65050 JH |
1647 | if (param_type && POINTER_TYPE_P (param_type)) |
1648 | type_p = !detect_type_change_ssa (arg, TREE_TYPE (param_type), | |
1649 | call, jfunc); | |
1650 | else | |
1651 | type_p = false; | |
b8f6e610 | 1652 | if (type_p || jfunc->type == IPA_JF_UNKNOWN) |
06d65050 JH |
1653 | ipa_set_jf_simple_pass_through (jfunc, index, agg_p, |
1654 | type_p); | |
8b7773a4 MJ |
1655 | } |
1656 | } | |
1657 | else | |
1658 | { | |
1659 | gimple stmt = SSA_NAME_DEF_STMT (arg); | |
1660 | if (is_gimple_assign (stmt)) | |
1661 | compute_complex_assign_jump_func (info, parms_ainfo, jfunc, | |
06d65050 | 1662 | call, stmt, arg, param_type); |
8b7773a4 MJ |
1663 | else if (gimple_code (stmt) == GIMPLE_PHI) |
1664 | compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc, | |
06d65050 | 1665 | call, stmt, param_type); |
8b7773a4 MJ |
1666 | } |
1667 | } | |
1668 | else | |
06d65050 JH |
1669 | compute_known_type_jump_func (arg, jfunc, call, |
1670 | param_type | |
1671 | && POINTER_TYPE_P (param_type) | |
1672 | ? TREE_TYPE (param_type) | |
1673 | : NULL); | |
3e293154 | 1674 | |
85942f45 JH |
1675 | /* If ARG is pointer, we can not use its type to determine the type of aggregate |
1676 | passed (because type conversions are ignored in gimple). Usually we can | |
1677 | safely get type from function declaration, but in case of K&R prototypes or | |
1678 | variadic functions we can try our luck with type of the pointer passed. | |
1679 | TODO: Since we look for actual initialization of the memory object, we may better | |
1680 | work out the type based on the memory stores we find. */ | |
1681 | if (!param_type) | |
1682 | param_type = TREE_TYPE (arg); | |
1683 | ||
8b7773a4 MJ |
1684 | if ((jfunc->type != IPA_JF_PASS_THROUGH |
1685 | || !ipa_get_jf_pass_through_agg_preserved (jfunc)) | |
1686 | && (jfunc->type != IPA_JF_ANCESTOR | |
1687 | || !ipa_get_jf_ancestor_agg_preserved (jfunc)) | |
1688 | && (AGGREGATE_TYPE_P (TREE_TYPE (arg)) | |
85942f45 JH |
1689 | || POINTER_TYPE_P (param_type))) |
1690 | determine_known_aggregate_parts (call, arg, param_type, jfunc); | |
8b7773a4 | 1691 | } |
3e293154 MJ |
1692 | } |
1693 | ||
749aa96d MJ |
1694 | /* Compute jump functions for all edges - both direct and indirect - outgoing |
1695 | from NODE. Also count the actual arguments in the process. */ | |
1696 | ||
062c604f MJ |
1697 | static void |
1698 | ipa_compute_jump_functions (struct cgraph_node *node, | |
c419671c | 1699 | struct param_analysis_info *parms_ainfo) |
749aa96d MJ |
1700 | { |
1701 | struct cgraph_edge *cs; | |
1702 | ||
1703 | for (cs = node->callees; cs; cs = cs->next_callee) | |
1704 | { | |
d7da5cc8 MJ |
1705 | struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee, |
1706 | NULL); | |
749aa96d MJ |
1707 | /* We do not need to bother analyzing calls to unknown |
1708 | functions unless they may become known during lto/whopr. */ | |
67348ccc | 1709 | if (!callee->definition && !flag_lto) |
749aa96d | 1710 | continue; |
c419671c | 1711 | ipa_compute_jump_functions_for_edge (parms_ainfo, cs); |
749aa96d MJ |
1712 | } |
1713 | ||
1714 | for (cs = node->indirect_calls; cs; cs = cs->next_callee) | |
c419671c | 1715 | ipa_compute_jump_functions_for_edge (parms_ainfo, cs); |
749aa96d MJ |
1716 | } |
1717 | ||
8b7773a4 MJ |
1718 | /* If STMT looks like a statement loading a value from a member pointer formal |
1719 | parameter, return that parameter and store the offset of the field to | |
1720 | *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still | |
1721 | might be clobbered). If USE_DELTA, then we look for a use of the delta | |
1722 | field rather than the pfn. */ | |
be95e2b9 | 1723 | |
3e293154 | 1724 | static tree |
8b7773a4 MJ |
1725 | ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta, |
1726 | HOST_WIDE_INT *offset_p) | |
3e293154 | 1727 | { |
8b7773a4 MJ |
1728 | tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field; |
1729 | ||
1730 | if (!gimple_assign_single_p (stmt)) | |
1731 | return NULL_TREE; | |
3e293154 | 1732 | |
8b7773a4 | 1733 | rhs = gimple_assign_rhs1 (stmt); |
ae788515 EB |
1734 | if (TREE_CODE (rhs) == COMPONENT_REF) |
1735 | { | |
1736 | ref_field = TREE_OPERAND (rhs, 1); | |
1737 | rhs = TREE_OPERAND (rhs, 0); | |
1738 | } | |
1739 | else | |
1740 | ref_field = NULL_TREE; | |
d242d063 | 1741 | if (TREE_CODE (rhs) != MEM_REF) |
3e293154 | 1742 | return NULL_TREE; |
3e293154 | 1743 | rec = TREE_OPERAND (rhs, 0); |
d242d063 MJ |
1744 | if (TREE_CODE (rec) != ADDR_EXPR) |
1745 | return NULL_TREE; | |
1746 | rec = TREE_OPERAND (rec, 0); | |
3e293154 | 1747 | if (TREE_CODE (rec) != PARM_DECL |
6f7b8b70 | 1748 | || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field)) |
3e293154 | 1749 | return NULL_TREE; |
d242d063 | 1750 | ref_offset = TREE_OPERAND (rhs, 1); |
ae788515 | 1751 | |
8b7773a4 MJ |
1752 | if (use_delta) |
1753 | fld = delta_field; | |
1754 | else | |
1755 | fld = ptr_field; | |
1756 | if (offset_p) | |
1757 | *offset_p = int_bit_position (fld); | |
1758 | ||
ae788515 EB |
1759 | if (ref_field) |
1760 | { | |
1761 | if (integer_nonzerop (ref_offset)) | |
1762 | return NULL_TREE; | |
ae788515 EB |
1763 | return ref_field == fld ? rec : NULL_TREE; |
1764 | } | |
3e293154 | 1765 | else |
8b7773a4 MJ |
1766 | return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec |
1767 | : NULL_TREE; | |
3e293154 MJ |
1768 | } |
1769 | ||
1770 | /* Returns true iff T is an SSA_NAME defined by a statement. */ | |
be95e2b9 | 1771 | |
3e293154 MJ |
1772 | static bool |
1773 | ipa_is_ssa_with_stmt_def (tree t) | |
1774 | { | |
1775 | if (TREE_CODE (t) == SSA_NAME | |
1776 | && !SSA_NAME_IS_DEFAULT_DEF (t)) | |
1777 | return true; | |
1778 | else | |
1779 | return false; | |
1780 | } | |
1781 | ||
40591473 MJ |
1782 | /* Find the indirect call graph edge corresponding to STMT and mark it as a |
1783 | call to a parameter number PARAM_INDEX. NODE is the caller. Return the | |
1784 | indirect call graph edge. */ | |
be95e2b9 | 1785 | |
40591473 MJ |
1786 | static struct cgraph_edge * |
1787 | ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt) | |
3e293154 | 1788 | { |
e33c6cd6 | 1789 | struct cgraph_edge *cs; |
3e293154 | 1790 | |
5f902d76 | 1791 | cs = cgraph_edge (node, stmt); |
b258210c | 1792 | cs->indirect_info->param_index = param_index; |
8b7773a4 | 1793 | cs->indirect_info->agg_contents = 0; |
c13bc3d9 | 1794 | cs->indirect_info->member_ptr = 0; |
40591473 | 1795 | return cs; |
3e293154 MJ |
1796 | } |
1797 | ||
e33c6cd6 | 1798 | /* Analyze the CALL and examine uses of formal parameters of the caller NODE |
c419671c | 1799 | (described by INFO). PARMS_AINFO is a pointer to a vector containing |
062c604f MJ |
1800 | intermediate information about each formal parameter. Currently it checks |
1801 | whether the call calls a pointer that is a formal parameter and if so, the | |
1802 | parameter is marked with the called flag and an indirect call graph edge | |
1803 | describing the call is created. This is very simple for ordinary pointers | |
1804 | represented in SSA but not-so-nice when it comes to member pointers. The | |
1805 | ugly part of this function does nothing more than trying to match the | |
1806 | pattern of such a call. An example of such a pattern is the gimple dump | |
1807 | below, the call is on the last line: | |
3e293154 | 1808 | |
ae788515 EB |
1809 | <bb 2>: |
1810 | f$__delta_5 = f.__delta; | |
1811 | f$__pfn_24 = f.__pfn; | |
1812 | ||
1813 | or | |
3e293154 | 1814 | <bb 2>: |
d242d063 MJ |
1815 | f$__delta_5 = MEM[(struct *)&f]; |
1816 | f$__pfn_24 = MEM[(struct *)&f + 4B]; | |
8aa29647 | 1817 | |
ae788515 | 1818 | and a few lines below: |
8aa29647 MJ |
1819 | |
1820 | <bb 5> | |
3e293154 MJ |
1821 | D.2496_3 = (int) f$__pfn_24; |
1822 | D.2497_4 = D.2496_3 & 1; | |
1823 | if (D.2497_4 != 0) | |
1824 | goto <bb 3>; | |
1825 | else | |
1826 | goto <bb 4>; | |
1827 | ||
8aa29647 | 1828 | <bb 6>: |
3e293154 MJ |
1829 | D.2500_7 = (unsigned int) f$__delta_5; |
1830 | D.2501_8 = &S + D.2500_7; | |
1831 | D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8; | |
1832 | D.2503_10 = *D.2502_9; | |
1833 | D.2504_12 = f$__pfn_24 + -1; | |
1834 | D.2505_13 = (unsigned int) D.2504_12; | |
1835 | D.2506_14 = D.2503_10 + D.2505_13; | |
1836 | D.2507_15 = *D.2506_14; | |
1837 | iftmp.11_16 = (String:: *) D.2507_15; | |
1838 | ||
8aa29647 | 1839 | <bb 7>: |
3e293154 MJ |
1840 | # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)> |
1841 | D.2500_19 = (unsigned int) f$__delta_5; | |
1842 | D.2508_20 = &S + D.2500_19; | |
1843 | D.2493_21 = iftmp.11_1 (D.2508_20, 4); | |
1844 | ||
1845 | Such patterns are results of simple calls to a member pointer: | |
1846 | ||
1847 | int doprinting (int (MyString::* f)(int) const) | |
1848 | { | |
1849 | MyString S ("somestring"); | |
1850 | ||
1851 | return (S.*f)(4); | |
1852 | } | |
8b7773a4 MJ |
1853 | |
1854 | Moreover, the function also looks for called pointers loaded from aggregates | |
1855 | passed by value or reference. */ | |
3e293154 MJ |
1856 | |
1857 | static void | |
b258210c MJ |
1858 | ipa_analyze_indirect_call_uses (struct cgraph_node *node, |
1859 | struct ipa_node_params *info, | |
c419671c | 1860 | struct param_analysis_info *parms_ainfo, |
b258210c | 1861 | gimple call, tree target) |
3e293154 | 1862 | { |
726a989a | 1863 | gimple def; |
3e293154 | 1864 | tree n1, n2; |
726a989a RB |
1865 | gimple d1, d2; |
1866 | tree rec, rec2, cond; | |
1867 | gimple branch; | |
3e293154 | 1868 | int index; |
3e293154 | 1869 | basic_block bb, virt_bb, join; |
8b7773a4 MJ |
1870 | HOST_WIDE_INT offset; |
1871 | bool by_ref; | |
3e293154 | 1872 | |
3e293154 MJ |
1873 | if (SSA_NAME_IS_DEFAULT_DEF (target)) |
1874 | { | |
b258210c | 1875 | tree var = SSA_NAME_VAR (target); |
3e293154 MJ |
1876 | index = ipa_get_param_decl_index (info, var); |
1877 | if (index >= 0) | |
40591473 | 1878 | ipa_note_param_call (node, index, call); |
3e293154 MJ |
1879 | return; |
1880 | } | |
1881 | ||
8b7773a4 MJ |
1882 | def = SSA_NAME_DEF_STMT (target); |
1883 | if (gimple_assign_single_p (def) | |
d044dd17 | 1884 | && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def, |
8b7773a4 | 1885 | gimple_assign_rhs1 (def), &index, &offset, |
3ff2ca23 | 1886 | NULL, &by_ref)) |
8b7773a4 MJ |
1887 | { |
1888 | struct cgraph_edge *cs = ipa_note_param_call (node, index, call); | |
68377e53 JH |
1889 | if (cs->indirect_info->offset != offset) |
1890 | cs->indirect_info->outer_type = NULL; | |
8b7773a4 MJ |
1891 | cs->indirect_info->offset = offset; |
1892 | cs->indirect_info->agg_contents = 1; | |
1893 | cs->indirect_info->by_ref = by_ref; | |
1894 | return; | |
1895 | } | |
1896 | ||
3e293154 MJ |
1897 | /* Now we need to try to match the complex pattern of calling a member |
1898 | pointer. */ | |
8b7773a4 MJ |
1899 | if (gimple_code (def) != GIMPLE_PHI |
1900 | || gimple_phi_num_args (def) != 2 | |
1901 | || !POINTER_TYPE_P (TREE_TYPE (target)) | |
3e293154 MJ |
1902 | || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE) |
1903 | return; | |
1904 | ||
3e293154 MJ |
1905 | /* First, we need to check whether one of these is a load from a member |
1906 | pointer that is a parameter to this function. */ | |
1907 | n1 = PHI_ARG_DEF (def, 0); | |
1908 | n2 = PHI_ARG_DEF (def, 1); | |
1fc8feb5 | 1909 | if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2)) |
3e293154 MJ |
1910 | return; |
1911 | d1 = SSA_NAME_DEF_STMT (n1); | |
1912 | d2 = SSA_NAME_DEF_STMT (n2); | |
1913 | ||
8aa29647 | 1914 | join = gimple_bb (def); |
8b7773a4 | 1915 | if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset))) |
3e293154 | 1916 | { |
8b7773a4 | 1917 | if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL)) |
3e293154 MJ |
1918 | return; |
1919 | ||
8aa29647 | 1920 | bb = EDGE_PRED (join, 0)->src; |
726a989a | 1921 | virt_bb = gimple_bb (d2); |
3e293154 | 1922 | } |
8b7773a4 | 1923 | else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset))) |
3e293154 | 1924 | { |
8aa29647 | 1925 | bb = EDGE_PRED (join, 1)->src; |
726a989a | 1926 | virt_bb = gimple_bb (d1); |
3e293154 MJ |
1927 | } |
1928 | else | |
1929 | return; | |
1930 | ||
1931 | /* Second, we need to check that the basic blocks are laid out in the way | |
1932 | corresponding to the pattern. */ | |
1933 | ||
3e293154 MJ |
1934 | if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb) |
1935 | || single_pred (virt_bb) != bb | |
1936 | || single_succ (virt_bb) != join) | |
1937 | return; | |
1938 | ||
1939 | /* Third, let's see that the branching is done depending on the least | |
1940 | significant bit of the pfn. */ | |
1941 | ||
1942 | branch = last_stmt (bb); | |
8aa29647 | 1943 | if (!branch || gimple_code (branch) != GIMPLE_COND) |
3e293154 MJ |
1944 | return; |
1945 | ||
12430896 RG |
1946 | if ((gimple_cond_code (branch) != NE_EXPR |
1947 | && gimple_cond_code (branch) != EQ_EXPR) | |
726a989a | 1948 | || !integer_zerop (gimple_cond_rhs (branch))) |
3e293154 | 1949 | return; |
3e293154 | 1950 | |
726a989a | 1951 | cond = gimple_cond_lhs (branch); |
3e293154 MJ |
1952 | if (!ipa_is_ssa_with_stmt_def (cond)) |
1953 | return; | |
1954 | ||
726a989a | 1955 | def = SSA_NAME_DEF_STMT (cond); |
8b75fc9b | 1956 | if (!is_gimple_assign (def) |
726a989a RB |
1957 | || gimple_assign_rhs_code (def) != BIT_AND_EXPR |
1958 | || !integer_onep (gimple_assign_rhs2 (def))) | |
3e293154 | 1959 | return; |
726a989a RB |
1960 | |
1961 | cond = gimple_assign_rhs1 (def); | |
3e293154 MJ |
1962 | if (!ipa_is_ssa_with_stmt_def (cond)) |
1963 | return; | |
1964 | ||
726a989a | 1965 | def = SSA_NAME_DEF_STMT (cond); |
3e293154 | 1966 | |
8b75fc9b MJ |
1967 | if (is_gimple_assign (def) |
1968 | && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def))) | |
3e293154 | 1969 | { |
726a989a | 1970 | cond = gimple_assign_rhs1 (def); |
3e293154 MJ |
1971 | if (!ipa_is_ssa_with_stmt_def (cond)) |
1972 | return; | |
726a989a | 1973 | def = SSA_NAME_DEF_STMT (cond); |
3e293154 MJ |
1974 | } |
1975 | ||
6f7b8b70 RE |
1976 | rec2 = ipa_get_stmt_member_ptr_load_param (def, |
1977 | (TARGET_PTRMEMFUNC_VBIT_LOCATION | |
8b7773a4 MJ |
1978 | == ptrmemfunc_vbit_in_delta), |
1979 | NULL); | |
3e293154 MJ |
1980 | if (rec != rec2) |
1981 | return; | |
1982 | ||
1983 | index = ipa_get_param_decl_index (info, rec); | |
8b7773a4 MJ |
1984 | if (index >= 0 |
1985 | && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec)) | |
1986 | { | |
1987 | struct cgraph_edge *cs = ipa_note_param_call (node, index, call); | |
68377e53 JH |
1988 | if (cs->indirect_info->offset != offset) |
1989 | cs->indirect_info->outer_type = NULL; | |
8b7773a4 MJ |
1990 | cs->indirect_info->offset = offset; |
1991 | cs->indirect_info->agg_contents = 1; | |
c13bc3d9 | 1992 | cs->indirect_info->member_ptr = 1; |
8b7773a4 | 1993 | } |
3e293154 MJ |
1994 | |
1995 | return; | |
1996 | } | |
1997 | ||
b258210c MJ |
1998 | /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the |
1999 | object referenced in the expression is a formal parameter of the caller | |
2000 | (described by INFO), create a call note for the statement. */ | |
2001 | ||
2002 | static void | |
2003 | ipa_analyze_virtual_call_uses (struct cgraph_node *node, | |
2004 | struct ipa_node_params *info, gimple call, | |
2005 | tree target) | |
2006 | { | |
40591473 MJ |
2007 | struct cgraph_edge *cs; |
2008 | struct cgraph_indirect_call_info *ii; | |
f65cf2b7 | 2009 | struct ipa_jump_func jfunc; |
b258210c | 2010 | tree obj = OBJ_TYPE_REF_OBJECT (target); |
b258210c | 2011 | int index; |
40591473 | 2012 | HOST_WIDE_INT anc_offset; |
b258210c | 2013 | |
05842ff5 MJ |
2014 | if (!flag_devirtualize) |
2015 | return; | |
2016 | ||
40591473 | 2017 | if (TREE_CODE (obj) != SSA_NAME) |
b258210c MJ |
2018 | return; |
2019 | ||
40591473 MJ |
2020 | if (SSA_NAME_IS_DEFAULT_DEF (obj)) |
2021 | { | |
2022 | if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL) | |
2023 | return; | |
b258210c | 2024 | |
40591473 MJ |
2025 | anc_offset = 0; |
2026 | index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj)); | |
2027 | gcc_assert (index >= 0); | |
06d65050 JH |
2028 | if (detect_type_change_ssa (obj, obj_type_ref_class (target), |
2029 | call, &jfunc)) | |
40591473 MJ |
2030 | return; |
2031 | } | |
2032 | else | |
2033 | { | |
2034 | gimple stmt = SSA_NAME_DEF_STMT (obj); | |
2035 | tree expr; | |
2036 | ||
2037 | expr = get_ancestor_addr_info (stmt, &obj, &anc_offset); | |
2038 | if (!expr) | |
2039 | return; | |
2040 | index = ipa_get_param_decl_index (info, | |
2041 | SSA_NAME_VAR (TREE_OPERAND (expr, 0))); | |
2042 | gcc_assert (index >= 0); | |
06d65050 JH |
2043 | if (detect_type_change (obj, expr, obj_type_ref_class (target), |
2044 | call, &jfunc, anc_offset)) | |
40591473 MJ |
2045 | return; |
2046 | } | |
2047 | ||
2048 | cs = ipa_note_param_call (node, index, call); | |
2049 | ii = cs->indirect_info; | |
8b7773a4 | 2050 | ii->offset = anc_offset; |
ae7e9ddd | 2051 | ii->otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target)); |
c49bdb2e | 2052 | ii->otr_type = obj_type_ref_class (target); |
40591473 | 2053 | ii->polymorphic = 1; |
b258210c MJ |
2054 | } |
2055 | ||
2056 | /* Analyze a call statement CALL whether and how it utilizes formal parameters | |
c419671c | 2057 | of the caller (described by INFO). PARMS_AINFO is a pointer to a vector |
062c604f | 2058 | containing intermediate information about each formal parameter. */ |
b258210c MJ |
2059 | |
2060 | static void | |
2061 | ipa_analyze_call_uses (struct cgraph_node *node, | |
062c604f | 2062 | struct ipa_node_params *info, |
c419671c | 2063 | struct param_analysis_info *parms_ainfo, gimple call) |
b258210c MJ |
2064 | { |
2065 | tree target = gimple_call_fn (call); | |
b786d31f JH |
2066 | struct cgraph_edge *cs; |
2067 | ||
2068 | if (!target | |
2069 | || (TREE_CODE (target) != SSA_NAME | |
2070 | && !virtual_method_call_p (target))) | |
2071 | return; | |
b258210c | 2072 | |
b786d31f JH |
2073 | /* If we previously turned the call into a direct call, there is |
2074 | no need to analyze. */ | |
2075 | cs = cgraph_edge (node, call); | |
2076 | if (cs && !cs->indirect_unknown_callee) | |
25583c4f | 2077 | return; |
b258210c | 2078 | if (TREE_CODE (target) == SSA_NAME) |
c419671c | 2079 | ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target); |
1d5755ef | 2080 | else if (virtual_method_call_p (target)) |
b258210c MJ |
2081 | ipa_analyze_virtual_call_uses (node, info, call, target); |
2082 | } | |
2083 | ||
2084 | ||
e33c6cd6 MJ |
2085 | /* Analyze the call statement STMT with respect to formal parameters (described |
2086 | in INFO) of caller given by NODE. Currently it only checks whether formal | |
c419671c | 2087 | parameters are called. PARMS_AINFO is a pointer to a vector containing |
062c604f | 2088 | intermediate information about each formal parameter. */ |
be95e2b9 | 2089 | |
3e293154 | 2090 | static void |
e33c6cd6 | 2091 | ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info, |
c419671c | 2092 | struct param_analysis_info *parms_ainfo, gimple stmt) |
3e293154 | 2093 | { |
726a989a | 2094 | if (is_gimple_call (stmt)) |
c419671c | 2095 | ipa_analyze_call_uses (node, info, parms_ainfo, stmt); |
062c604f MJ |
2096 | } |
2097 | ||
2098 | /* Callback of walk_stmt_load_store_addr_ops for the visit_load. | |
2099 | If OP is a parameter declaration, mark it as used in the info structure | |
2100 | passed in DATA. */ | |
2101 | ||
2102 | static bool | |
9f1363cd | 2103 | visit_ref_for_mod_analysis (gimple, tree op, tree, void *data) |
062c604f MJ |
2104 | { |
2105 | struct ipa_node_params *info = (struct ipa_node_params *) data; | |
2106 | ||
2107 | op = get_base_address (op); | |
2108 | if (op | |
2109 | && TREE_CODE (op) == PARM_DECL) | |
2110 | { | |
2111 | int index = ipa_get_param_decl_index (info, op); | |
2112 | gcc_assert (index >= 0); | |
310bc633 | 2113 | ipa_set_param_used (info, index, true); |
062c604f MJ |
2114 | } |
2115 | ||
2116 | return false; | |
3e293154 MJ |
2117 | } |
2118 | ||
2119 | /* Scan the function body of NODE and inspect the uses of formal parameters. | |
2120 | Store the findings in various structures of the associated ipa_node_params | |
c419671c | 2121 | structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a |
062c604f | 2122 | vector containing intermediate information about each formal parameter. */ |
be95e2b9 | 2123 | |
062c604f MJ |
2124 | static void |
2125 | ipa_analyze_params_uses (struct cgraph_node *node, | |
c419671c | 2126 | struct param_analysis_info *parms_ainfo) |
3e293154 | 2127 | { |
67348ccc | 2128 | tree decl = node->decl; |
3e293154 MJ |
2129 | basic_block bb; |
2130 | struct function *func; | |
726a989a | 2131 | gimple_stmt_iterator gsi; |
3e293154 | 2132 | struct ipa_node_params *info = IPA_NODE_REF (node); |
062c604f | 2133 | int i; |
3e293154 | 2134 | |
726a989a | 2135 | if (ipa_get_param_count (info) == 0 || info->uses_analysis_done) |
3e293154 | 2136 | return; |
3e293154 | 2137 | |
5fe8e757 MJ |
2138 | info->uses_analysis_done = 1; |
2139 | if (ipa_func_spec_opts_forbid_analysis_p (node)) | |
2140 | { | |
2141 | for (i = 0; i < ipa_get_param_count (info); i++) | |
2142 | { | |
2143 | ipa_set_param_used (info, i, true); | |
2144 | ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE); | |
2145 | } | |
2146 | return; | |
2147 | } | |
2148 | ||
062c604f MJ |
2149 | for (i = 0; i < ipa_get_param_count (info); i++) |
2150 | { | |
2151 | tree parm = ipa_get_param (info, i); | |
4502fe8d MJ |
2152 | int controlled_uses = 0; |
2153 | ||
062c604f MJ |
2154 | /* For SSA regs see if parameter is used. For non-SSA we compute |
2155 | the flag during modification analysis. */ | |
4502fe8d MJ |
2156 | if (is_gimple_reg (parm)) |
2157 | { | |
67348ccc | 2158 | tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl), |
4502fe8d MJ |
2159 | parm); |
2160 | if (ddef && !has_zero_uses (ddef)) | |
2161 | { | |
2162 | imm_use_iterator imm_iter; | |
2163 | use_operand_p use_p; | |
2164 | ||
2165 | ipa_set_param_used (info, i, true); | |
2166 | FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef) | |
2167 | if (!is_gimple_call (USE_STMT (use_p))) | |
2168 | { | |
c6de6665 JJ |
2169 | if (!is_gimple_debug (USE_STMT (use_p))) |
2170 | { | |
2171 | controlled_uses = IPA_UNDESCRIBED_USE; | |
2172 | break; | |
2173 | } | |
4502fe8d MJ |
2174 | } |
2175 | else | |
2176 | controlled_uses++; | |
2177 | } | |
2178 | else | |
2179 | controlled_uses = 0; | |
2180 | } | |
2181 | else | |
2182 | controlled_uses = IPA_UNDESCRIBED_USE; | |
2183 | ipa_set_controlled_uses (info, i, controlled_uses); | |
062c604f MJ |
2184 | } |
2185 | ||
3e293154 MJ |
2186 | func = DECL_STRUCT_FUNCTION (decl); |
2187 | FOR_EACH_BB_FN (bb, func) | |
2188 | { | |
726a989a | 2189 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
3e293154 | 2190 | { |
726a989a | 2191 | gimple stmt = gsi_stmt (gsi); |
062c604f MJ |
2192 | |
2193 | if (is_gimple_debug (stmt)) | |
2194 | continue; | |
2195 | ||
c419671c | 2196 | ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt); |
062c604f MJ |
2197 | walk_stmt_load_store_addr_ops (stmt, info, |
2198 | visit_ref_for_mod_analysis, | |
2199 | visit_ref_for_mod_analysis, | |
2200 | visit_ref_for_mod_analysis); | |
518dc859 | 2201 | } |
355a7673 | 2202 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
062c604f MJ |
2203 | walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info, |
2204 | visit_ref_for_mod_analysis, | |
2205 | visit_ref_for_mod_analysis, | |
2206 | visit_ref_for_mod_analysis); | |
518dc859 | 2207 | } |
3e293154 MJ |
2208 | } |
2209 | ||
2c9561b5 MJ |
2210 | /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */ |
2211 | ||
2212 | static void | |
2213 | free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count) | |
2214 | { | |
2215 | int i; | |
2216 | ||
2217 | for (i = 0; i < param_count; i++) | |
2218 | { | |
2219 | if (parms_ainfo[i].parm_visited_statements) | |
2220 | BITMAP_FREE (parms_ainfo[i].parm_visited_statements); | |
2221 | if (parms_ainfo[i].pt_visited_statements) | |
2222 | BITMAP_FREE (parms_ainfo[i].pt_visited_statements); | |
2223 | } | |
2224 | } | |
2225 | ||
dd5a833e MS |
2226 | /* Initialize the array describing properties of of formal parameters |
2227 | of NODE, analyze their uses and compute jump functions associated | |
2228 | with actual arguments of calls from within NODE. */ | |
062c604f MJ |
2229 | |
2230 | void | |
2231 | ipa_analyze_node (struct cgraph_node *node) | |
2232 | { | |
57dbdc5a | 2233 | struct ipa_node_params *info; |
c419671c | 2234 | struct param_analysis_info *parms_ainfo; |
2c9561b5 | 2235 | int param_count; |
062c604f | 2236 | |
57dbdc5a MJ |
2237 | ipa_check_create_node_params (); |
2238 | ipa_check_create_edge_args (); | |
2239 | info = IPA_NODE_REF (node); | |
67348ccc | 2240 | push_cfun (DECL_STRUCT_FUNCTION (node->decl)); |
062c604f MJ |
2241 | ipa_initialize_node_params (node); |
2242 | ||
2243 | param_count = ipa_get_param_count (info); | |
c419671c MJ |
2244 | parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count); |
2245 | memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count); | |
062c604f | 2246 | |
c419671c MJ |
2247 | ipa_analyze_params_uses (node, parms_ainfo); |
2248 | ipa_compute_jump_functions (node, parms_ainfo); | |
062c604f | 2249 | |
2c9561b5 | 2250 | free_parms_ainfo (parms_ainfo, param_count); |
f65cf2b7 | 2251 | pop_cfun (); |
062c604f MJ |
2252 | } |
2253 | ||
e248d83f MJ |
2254 | /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF |
2255 | attempt a type-based devirtualization. If successful, return the | |
2256 | target function declaration, otherwise return NULL. */ | |
2257 | ||
2258 | tree | |
2259 | ipa_intraprocedural_devirtualization (gimple call) | |
2260 | { | |
2261 | tree binfo, token, fndecl; | |
2262 | struct ipa_jump_func jfunc; | |
2263 | tree otr = gimple_call_fn (call); | |
2264 | ||
2265 | jfunc.type = IPA_JF_UNKNOWN; | |
2266 | compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc, | |
06d65050 | 2267 | call, obj_type_ref_class (otr)); |
e248d83f MJ |
2268 | if (jfunc.type != IPA_JF_KNOWN_TYPE) |
2269 | return NULL_TREE; | |
2270 | binfo = ipa_binfo_from_known_type_jfunc (&jfunc); | |
2271 | if (!binfo) | |
2272 | return NULL_TREE; | |
2273 | token = OBJ_TYPE_REF_TOKEN (otr); | |
ae7e9ddd | 2274 | fndecl = gimple_get_virt_method_for_binfo (tree_to_uhwi (token), |
e248d83f | 2275 | binfo); |
450ad0cd JH |
2276 | #ifdef ENABLE_CHECKING |
2277 | if (fndecl) | |
2278 | gcc_assert (possible_polymorphic_call_target_p | |
2279 | (otr, cgraph_get_node (fndecl))); | |
2280 | #endif | |
e248d83f MJ |
2281 | return fndecl; |
2282 | } | |
062c604f | 2283 | |
61502ca8 | 2284 | /* Update the jump function DST when the call graph edge corresponding to SRC is |
b258210c MJ |
2285 | is being inlined, knowing that DST is of type ancestor and src of known |
2286 | type. */ | |
2287 | ||
2288 | static void | |
2289 | combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src, | |
2290 | struct ipa_jump_func *dst) | |
2291 | { | |
c7573249 MJ |
2292 | HOST_WIDE_INT combined_offset; |
2293 | tree combined_type; | |
b258210c | 2294 | |
b8f6e610 MJ |
2295 | if (!ipa_get_jf_ancestor_type_preserved (dst)) |
2296 | { | |
2297 | dst->type = IPA_JF_UNKNOWN; | |
2298 | return; | |
2299 | } | |
2300 | ||
7b872d9e MJ |
2301 | combined_offset = ipa_get_jf_known_type_offset (src) |
2302 | + ipa_get_jf_ancestor_offset (dst); | |
2303 | combined_type = ipa_get_jf_ancestor_type (dst); | |
c7573249 | 2304 | |
7b872d9e MJ |
2305 | ipa_set_jf_known_type (dst, combined_offset, |
2306 | ipa_get_jf_known_type_base_type (src), | |
2307 | combined_type); | |
b258210c MJ |
2308 | } |
2309 | ||
be95e2b9 | 2310 | /* Update the jump functions associated with call graph edge E when the call |
3e293154 | 2311 | graph edge CS is being inlined, assuming that E->caller is already (possibly |
b258210c | 2312 | indirectly) inlined into CS->callee and that E has not been inlined. */ |
be95e2b9 | 2313 | |
3e293154 MJ |
2314 | static void |
2315 | update_jump_functions_after_inlining (struct cgraph_edge *cs, | |
2316 | struct cgraph_edge *e) | |
2317 | { | |
2318 | struct ipa_edge_args *top = IPA_EDGE_REF (cs); | |
2319 | struct ipa_edge_args *args = IPA_EDGE_REF (e); | |
2320 | int count = ipa_get_cs_argument_count (args); | |
2321 | int i; | |
2322 | ||
2323 | for (i = 0; i < count; i++) | |
2324 | { | |
b258210c | 2325 | struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i); |
3e293154 | 2326 | |
685b0d13 MJ |
2327 | if (dst->type == IPA_JF_ANCESTOR) |
2328 | { | |
b258210c | 2329 | struct ipa_jump_func *src; |
8b7773a4 | 2330 | int dst_fid = dst->value.ancestor.formal_id; |
685b0d13 | 2331 | |
b258210c MJ |
2332 | /* Variable number of arguments can cause havoc if we try to access |
2333 | one that does not exist in the inlined edge. So make sure we | |
2334 | don't. */ | |
8b7773a4 | 2335 | if (dst_fid >= ipa_get_cs_argument_count (top)) |
b258210c MJ |
2336 | { |
2337 | dst->type = IPA_JF_UNKNOWN; | |
2338 | continue; | |
2339 | } | |
2340 | ||
8b7773a4 MJ |
2341 | src = ipa_get_ith_jump_func (top, dst_fid); |
2342 | ||
2343 | if (src->agg.items | |
2344 | && (dst->value.ancestor.agg_preserved || !src->agg.by_ref)) | |
2345 | { | |
2346 | struct ipa_agg_jf_item *item; | |
2347 | int j; | |
2348 | ||
2349 | /* Currently we do not produce clobber aggregate jump functions, | |
2350 | replace with merging when we do. */ | |
2351 | gcc_assert (!dst->agg.items); | |
2352 | ||
9771b263 | 2353 | dst->agg.items = vec_safe_copy (src->agg.items); |
8b7773a4 | 2354 | dst->agg.by_ref = src->agg.by_ref; |
9771b263 | 2355 | FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item) |
8b7773a4 MJ |
2356 | item->offset -= dst->value.ancestor.offset; |
2357 | } | |
2358 | ||
b258210c MJ |
2359 | if (src->type == IPA_JF_KNOWN_TYPE) |
2360 | combine_known_type_and_ancestor_jfs (src, dst); | |
b258210c MJ |
2361 | else if (src->type == IPA_JF_PASS_THROUGH |
2362 | && src->value.pass_through.operation == NOP_EXPR) | |
8b7773a4 MJ |
2363 | { |
2364 | dst->value.ancestor.formal_id = src->value.pass_through.formal_id; | |
2365 | dst->value.ancestor.agg_preserved &= | |
2366 | src->value.pass_through.agg_preserved; | |
b8f6e610 MJ |
2367 | dst->value.ancestor.type_preserved &= |
2368 | src->value.pass_through.type_preserved; | |
8b7773a4 | 2369 | } |
b258210c MJ |
2370 | else if (src->type == IPA_JF_ANCESTOR) |
2371 | { | |
2372 | dst->value.ancestor.formal_id = src->value.ancestor.formal_id; | |
2373 | dst->value.ancestor.offset += src->value.ancestor.offset; | |
8b7773a4 MJ |
2374 | dst->value.ancestor.agg_preserved &= |
2375 | src->value.ancestor.agg_preserved; | |
b8f6e610 MJ |
2376 | dst->value.ancestor.type_preserved &= |
2377 | src->value.ancestor.type_preserved; | |
b258210c MJ |
2378 | } |
2379 | else | |
2380 | dst->type = IPA_JF_UNKNOWN; | |
2381 | } | |
2382 | else if (dst->type == IPA_JF_PASS_THROUGH) | |
3e293154 | 2383 | { |
b258210c MJ |
2384 | struct ipa_jump_func *src; |
2385 | /* We must check range due to calls with variable number of arguments | |
2386 | and we cannot combine jump functions with operations. */ | |
2387 | if (dst->value.pass_through.operation == NOP_EXPR | |
2388 | && (dst->value.pass_through.formal_id | |
2389 | < ipa_get_cs_argument_count (top))) | |
2390 | { | |
8b7773a4 MJ |
2391 | int dst_fid = dst->value.pass_through.formal_id; |
2392 | src = ipa_get_ith_jump_func (top, dst_fid); | |
b8f6e610 | 2393 | bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst); |
8b7773a4 | 2394 | |
b8f6e610 MJ |
2395 | switch (src->type) |
2396 | { | |
2397 | case IPA_JF_UNKNOWN: | |
2398 | dst->type = IPA_JF_UNKNOWN; | |
2399 | break; | |
2400 | case IPA_JF_KNOWN_TYPE: | |
2ace77c2 JH |
2401 | if (ipa_get_jf_pass_through_type_preserved (dst)) |
2402 | ipa_set_jf_known_type (dst, | |
2403 | ipa_get_jf_known_type_offset (src), | |
2404 | ipa_get_jf_known_type_base_type (src), | |
0a2550e7 | 2405 | ipa_get_jf_known_type_component_type (src)); |
2ace77c2 JH |
2406 | else |
2407 | dst->type = IPA_JF_UNKNOWN; | |
b8f6e610 MJ |
2408 | break; |
2409 | case IPA_JF_CONST: | |
2410 | ipa_set_jf_cst_copy (dst, src); | |
2411 | break; | |
2412 | ||
2413 | case IPA_JF_PASS_THROUGH: | |
2414 | { | |
2415 | int formal_id = ipa_get_jf_pass_through_formal_id (src); | |
2416 | enum tree_code operation; | |
2417 | operation = ipa_get_jf_pass_through_operation (src); | |
2418 | ||
2419 | if (operation == NOP_EXPR) | |
2420 | { | |
2421 | bool agg_p, type_p; | |
2422 | agg_p = dst_agg_p | |
2423 | && ipa_get_jf_pass_through_agg_preserved (src); | |
2424 | type_p = ipa_get_jf_pass_through_type_preserved (src) | |
2425 | && ipa_get_jf_pass_through_type_preserved (dst); | |
2426 | ipa_set_jf_simple_pass_through (dst, formal_id, | |
2427 | agg_p, type_p); | |
2428 | } | |
2429 | else | |
2430 | { | |
2431 | tree operand = ipa_get_jf_pass_through_operand (src); | |
2432 | ipa_set_jf_arith_pass_through (dst, formal_id, operand, | |
2433 | operation); | |
2434 | } | |
2435 | break; | |
2436 | } | |
2437 | case IPA_JF_ANCESTOR: | |
2438 | { | |
2439 | bool agg_p, type_p; | |
2440 | agg_p = dst_agg_p | |
2441 | && ipa_get_jf_ancestor_agg_preserved (src); | |
2442 | type_p = ipa_get_jf_ancestor_type_preserved (src) | |
2443 | && ipa_get_jf_pass_through_type_preserved (dst); | |
2444 | ipa_set_ancestor_jf (dst, | |
2445 | ipa_get_jf_ancestor_offset (src), | |
2446 | ipa_get_jf_ancestor_type (src), | |
2447 | ipa_get_jf_ancestor_formal_id (src), | |
2448 | agg_p, type_p); | |
2449 | break; | |
2450 | } | |
2451 | default: | |
2452 | gcc_unreachable (); | |
2453 | } | |
8b7773a4 MJ |
2454 | |
2455 | if (src->agg.items | |
b8f6e610 | 2456 | && (dst_agg_p || !src->agg.by_ref)) |
8b7773a4 MJ |
2457 | { |
2458 | /* Currently we do not produce clobber aggregate jump | |
2459 | functions, replace with merging when we do. */ | |
2460 | gcc_assert (!dst->agg.items); | |
2461 | ||
2462 | dst->agg.by_ref = src->agg.by_ref; | |
9771b263 | 2463 | dst->agg.items = vec_safe_copy (src->agg.items); |
8b7773a4 | 2464 | } |
b258210c MJ |
2465 | } |
2466 | else | |
2467 | dst->type = IPA_JF_UNKNOWN; | |
3e293154 | 2468 | } |
b258210c MJ |
2469 | } |
2470 | } | |
2471 | ||
2472 | /* If TARGET is an addr_expr of a function declaration, make it the destination | |
81fa35bd | 2473 | of an indirect edge IE and return the edge. Otherwise, return NULL. */ |
b258210c | 2474 | |
3949c4a7 | 2475 | struct cgraph_edge * |
81fa35bd | 2476 | ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target) |
b258210c MJ |
2477 | { |
2478 | struct cgraph_node *callee; | |
0f378cb5 | 2479 | struct inline_edge_summary *es = inline_edge_summary (ie); |
48b1474e | 2480 | bool unreachable = false; |
b258210c | 2481 | |
ceeffab0 MJ |
2482 | if (TREE_CODE (target) == ADDR_EXPR) |
2483 | target = TREE_OPERAND (target, 0); | |
b258210c | 2484 | if (TREE_CODE (target) != FUNCTION_DECL) |
a0a7b611 JH |
2485 | { |
2486 | target = canonicalize_constructor_val (target, NULL); | |
2487 | if (!target || TREE_CODE (target) != FUNCTION_DECL) | |
2488 | { | |
c13bc3d9 MJ |
2489 | if (ie->indirect_info->member_ptr) |
2490 | /* Member pointer call that goes through a VMT lookup. */ | |
2491 | return NULL; | |
2492 | ||
a0a7b611 JH |
2493 | if (dump_file) |
2494 | fprintf (dump_file, "ipa-prop: Discovered direct call to non-function" | |
48b1474e | 2495 | " in %s/%i, making it unreachable.\n", |
fec39fa6 | 2496 | ie->caller->name (), ie->caller->order); |
48b1474e MJ |
2497 | target = builtin_decl_implicit (BUILT_IN_UNREACHABLE); |
2498 | callee = cgraph_get_create_node (target); | |
2499 | unreachable = true; | |
a0a7b611 | 2500 | } |
48b1474e MJ |
2501 | else |
2502 | callee = cgraph_get_node (target); | |
a0a7b611 | 2503 | } |
48b1474e MJ |
2504 | else |
2505 | callee = cgraph_get_node (target); | |
a0a7b611 JH |
2506 | |
2507 | /* Because may-edges are not explicitely represented and vtable may be external, | |
2508 | we may create the first reference to the object in the unit. */ | |
2509 | if (!callee || callee->global.inlined_to) | |
2510 | { | |
a0a7b611 JH |
2511 | |
2512 | /* We are better to ensure we can refer to it. | |
2513 | In the case of static functions we are out of luck, since we already | |
2514 | removed its body. In the case of public functions we may or may | |
2515 | not introduce the reference. */ | |
2516 | if (!canonicalize_constructor_val (target, NULL) | |
2517 | || !TREE_PUBLIC (target)) | |
2518 | { | |
2519 | if (dump_file) | |
2520 | fprintf (dump_file, "ipa-prop: Discovered call to a known target " | |
2521 | "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n", | |
fec39fa6 | 2522 | xstrdup (ie->caller->name ()), |
67348ccc | 2523 | ie->caller->order, |
fec39fa6 | 2524 | xstrdup (ie->callee->name ()), |
67348ccc | 2525 | ie->callee->order); |
a0a7b611 JH |
2526 | return NULL; |
2527 | } | |
6f99e449 | 2528 | callee = cgraph_get_create_node (target); |
a0a7b611 | 2529 | } |
1dbee8c9 | 2530 | ipa_check_create_node_params (); |
ceeffab0 | 2531 | |
81fa35bd MJ |
2532 | /* We can not make edges to inline clones. It is bug that someone removed |
2533 | the cgraph node too early. */ | |
17afc0fe JH |
2534 | gcc_assert (!callee->global.inlined_to); |
2535 | ||
48b1474e | 2536 | if (dump_file && !unreachable) |
b258210c MJ |
2537 | { |
2538 | fprintf (dump_file, "ipa-prop: Discovered %s call to a known target " | |
ceeffab0 | 2539 | "(%s/%i -> %s/%i), for stmt ", |
b258210c | 2540 | ie->indirect_info->polymorphic ? "a virtual" : "an indirect", |
fec39fa6 | 2541 | xstrdup (ie->caller->name ()), |
67348ccc | 2542 | ie->caller->order, |
fec39fa6 | 2543 | xstrdup (callee->name ()), |
67348ccc | 2544 | callee->order); |
b258210c MJ |
2545 | if (ie->call_stmt) |
2546 | print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM); | |
2547 | else | |
2548 | fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid); | |
042ae7d2 JH |
2549 | } |
2550 | ie = cgraph_make_edge_direct (ie, callee); | |
2551 | es = inline_edge_summary (ie); | |
2552 | es->call_stmt_size -= (eni_size_weights.indirect_call_cost | |
2553 | - eni_size_weights.call_cost); | |
2554 | es->call_stmt_time -= (eni_time_weights.indirect_call_cost | |
2555 | - eni_time_weights.call_cost); | |
749aa96d | 2556 | |
b258210c | 2557 | return ie; |
3e293154 MJ |
2558 | } |
2559 | ||
8b7773a4 MJ |
2560 | /* Retrieve value from aggregate jump function AGG for the given OFFSET or |
2561 | return NULL if there is not any. BY_REF specifies whether the value has to | |
2562 | be passed by reference or by value. */ | |
2563 | ||
2564 | tree | |
2565 | ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg, | |
2566 | HOST_WIDE_INT offset, bool by_ref) | |
2567 | { | |
2568 | struct ipa_agg_jf_item *item; | |
2569 | int i; | |
2570 | ||
2571 | if (by_ref != agg->by_ref) | |
2572 | return NULL; | |
2573 | ||
9771b263 | 2574 | FOR_EACH_VEC_SAFE_ELT (agg->items, i, item) |
2c9561b5 MJ |
2575 | if (item->offset == offset) |
2576 | { | |
2577 | /* Currently we do not have clobber values, return NULL for them once | |
2578 | we do. */ | |
2579 | gcc_checking_assert (is_gimple_ip_invariant (item->value)); | |
2580 | return item->value; | |
2581 | } | |
8b7773a4 MJ |
2582 | return NULL; |
2583 | } | |
2584 | ||
4502fe8d | 2585 | /* Remove a reference to SYMBOL from the list of references of a node given by |
568cda29 MJ |
2586 | reference description RDESC. Return true if the reference has been |
2587 | successfully found and removed. */ | |
4502fe8d | 2588 | |
568cda29 | 2589 | static bool |
5e20cdc9 | 2590 | remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc) |
4502fe8d MJ |
2591 | { |
2592 | struct ipa_ref *to_del; | |
2593 | struct cgraph_edge *origin; | |
2594 | ||
2595 | origin = rdesc->cs; | |
a854f856 MJ |
2596 | if (!origin) |
2597 | return false; | |
67348ccc | 2598 | to_del = ipa_find_reference (origin->caller, symbol, |
042ae7d2 | 2599 | origin->call_stmt, origin->lto_stmt_uid); |
568cda29 MJ |
2600 | if (!to_del) |
2601 | return false; | |
2602 | ||
4502fe8d MJ |
2603 | ipa_remove_reference (to_del); |
2604 | if (dump_file) | |
2605 | fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n", | |
fec39fa6 TS |
2606 | xstrdup (origin->caller->name ()), |
2607 | origin->caller->order, xstrdup (symbol->name ())); | |
568cda29 | 2608 | return true; |
4502fe8d MJ |
2609 | } |
2610 | ||
2611 | /* If JFUNC has a reference description with refcount different from | |
2612 | IPA_UNDESCRIBED_USE, return the reference description, otherwise return | |
2613 | NULL. JFUNC must be a constant jump function. */ | |
2614 | ||
2615 | static struct ipa_cst_ref_desc * | |
2616 | jfunc_rdesc_usable (struct ipa_jump_func *jfunc) | |
2617 | { | |
2618 | struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc); | |
2619 | if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE) | |
2620 | return rdesc; | |
2621 | else | |
2622 | return NULL; | |
2623 | } | |
2624 | ||
568cda29 MJ |
2625 | /* If the value of constant jump function JFUNC is an address of a function |
2626 | declaration, return the associated call graph node. Otherwise return | |
2627 | NULL. */ | |
2628 | ||
2629 | static cgraph_node * | |
2630 | cgraph_node_for_jfunc (struct ipa_jump_func *jfunc) | |
2631 | { | |
2632 | gcc_checking_assert (jfunc->type == IPA_JF_CONST); | |
2633 | tree cst = ipa_get_jf_constant (jfunc); | |
2634 | if (TREE_CODE (cst) != ADDR_EXPR | |
2635 | || TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL) | |
2636 | return NULL; | |
2637 | ||
2638 | return cgraph_get_node (TREE_OPERAND (cst, 0)); | |
2639 | } | |
2640 | ||
2641 | ||
2642 | /* If JFUNC is a constant jump function with a usable rdesc, decrement its | |
2643 | refcount and if it hits zero, remove reference to SYMBOL from the caller of | |
2644 | the edge specified in the rdesc. Return false if either the symbol or the | |
2645 | reference could not be found, otherwise return true. */ | |
2646 | ||
2647 | static bool | |
2648 | try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc) | |
2649 | { | |
2650 | struct ipa_cst_ref_desc *rdesc; | |
2651 | if (jfunc->type == IPA_JF_CONST | |
2652 | && (rdesc = jfunc_rdesc_usable (jfunc)) | |
2653 | && --rdesc->refcount == 0) | |
2654 | { | |
5e20cdc9 | 2655 | symtab_node *symbol = cgraph_node_for_jfunc (jfunc); |
568cda29 MJ |
2656 | if (!symbol) |
2657 | return false; | |
2658 | ||
2659 | return remove_described_reference (symbol, rdesc); | |
2660 | } | |
2661 | return true; | |
2662 | } | |
2663 | ||
b258210c MJ |
2664 | /* Try to find a destination for indirect edge IE that corresponds to a simple |
2665 | call or a call of a member function pointer and where the destination is a | |
2666 | pointer formal parameter described by jump function JFUNC. If it can be | |
d250540a MJ |
2667 | determined, return the newly direct edge, otherwise return NULL. |
2668 | NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */ | |
be95e2b9 | 2669 | |
b258210c MJ |
2670 | static struct cgraph_edge * |
2671 | try_make_edge_direct_simple_call (struct cgraph_edge *ie, | |
d250540a MJ |
2672 | struct ipa_jump_func *jfunc, |
2673 | struct ipa_node_params *new_root_info) | |
b258210c | 2674 | { |
4502fe8d | 2675 | struct cgraph_edge *cs; |
b258210c | 2676 | tree target; |
042ae7d2 | 2677 | bool agg_contents = ie->indirect_info->agg_contents; |
b258210c | 2678 | |
8b7773a4 | 2679 | if (ie->indirect_info->agg_contents) |
d250540a MJ |
2680 | target = ipa_find_agg_cst_for_param (&jfunc->agg, |
2681 | ie->indirect_info->offset, | |
2682 | ie->indirect_info->by_ref); | |
b258210c | 2683 | else |
d250540a MJ |
2684 | target = ipa_value_from_jfunc (new_root_info, jfunc); |
2685 | if (!target) | |
2686 | return NULL; | |
4502fe8d MJ |
2687 | cs = ipa_make_edge_direct_to_target (ie, target); |
2688 | ||
a12cd2db | 2689 | if (cs && !agg_contents) |
568cda29 MJ |
2690 | { |
2691 | bool ok; | |
2692 | gcc_checking_assert (cs->callee | |
ae6d0907 MJ |
2693 | && (cs != ie |
2694 | || jfunc->type != IPA_JF_CONST | |
568cda29 MJ |
2695 | || !cgraph_node_for_jfunc (jfunc) |
2696 | || cs->callee == cgraph_node_for_jfunc (jfunc))); | |
2697 | ok = try_decrement_rdesc_refcount (jfunc); | |
2698 | gcc_checking_assert (ok); | |
2699 | } | |
4502fe8d MJ |
2700 | |
2701 | return cs; | |
b258210c MJ |
2702 | } |
2703 | ||
d250540a MJ |
2704 | /* Try to find a destination for indirect edge IE that corresponds to a virtual |
2705 | call based on a formal parameter which is described by jump function JFUNC | |
2706 | and if it can be determined, make it direct and return the direct edge. | |
2707 | Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices | |
2708 | are relative to. */ | |
b258210c MJ |
2709 | |
2710 | static struct cgraph_edge * | |
2711 | try_make_edge_direct_virtual_call (struct cgraph_edge *ie, | |
d250540a MJ |
2712 | struct ipa_jump_func *jfunc, |
2713 | struct ipa_node_params *new_root_info) | |
3e293154 | 2714 | { |
9de2f554 | 2715 | tree binfo, target; |
85942f45 JH |
2716 | |
2717 | if (!flag_devirtualize) | |
2718 | return NULL; | |
b258210c | 2719 | |
9de2f554 | 2720 | /* First try to do lookup via known virtual table pointer value. */ |
85942f45 JH |
2721 | if (!ie->indirect_info->by_ref) |
2722 | { | |
9de2f554 JH |
2723 | tree vtable; |
2724 | unsigned HOST_WIDE_INT offset; | |
85942f45 JH |
2725 | tree t = ipa_find_agg_cst_for_param (&jfunc->agg, |
2726 | ie->indirect_info->offset, | |
2727 | true); | |
9de2f554 JH |
2728 | if (t && vtable_pointer_value_to_vtable (t, &vtable, &offset)) |
2729 | { | |
2730 | target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token, | |
2731 | vtable, offset); | |
2732 | if (target) | |
2733 | { | |
2734 | if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE | |
2735 | && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE) | |
2736 | || !possible_polymorphic_call_target_p | |
2737 | (ie, cgraph_get_node (target))) | |
2738 | { | |
2739 | if (dump_file) | |
2740 | fprintf (dump_file, | |
2741 | "Type inconsident devirtualization: %s/%i->%s\n", | |
2742 | ie->caller->name (), ie->caller->order, | |
2743 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target))); | |
2744 | target = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
2745 | cgraph_get_create_node (target); | |
2746 | } | |
2747 | return ipa_make_edge_direct_to_target (ie, target); | |
2748 | } | |
2749 | } | |
85942f45 JH |
2750 | } |
2751 | ||
9de2f554 | 2752 | binfo = ipa_value_from_jfunc (new_root_info, jfunc); |
d250540a | 2753 | |
da942ca0 | 2754 | if (!binfo) |
b258210c | 2755 | return NULL; |
3e293154 | 2756 | |
da942ca0 JH |
2757 | if (TREE_CODE (binfo) != TREE_BINFO) |
2758 | { | |
5bccb77a JH |
2759 | ipa_polymorphic_call_context context; |
2760 | vec <cgraph_node *>targets; | |
2761 | bool final; | |
2762 | ||
2763 | if (!get_polymorphic_call_info_from_invariant | |
2764 | (&context, binfo, ie->indirect_info->otr_type, | |
2765 | ie->indirect_info->offset)) | |
2766 | return NULL; | |
2767 | targets = possible_polymorphic_call_targets | |
2768 | (ie->indirect_info->otr_type, | |
2769 | ie->indirect_info->otr_token, | |
2770 | context, &final); | |
2771 | if (!final || targets.length () > 1) | |
85942f45 | 2772 | return NULL; |
5bccb77a JH |
2773 | if (targets.length () == 1) |
2774 | target = targets[0]->decl; | |
2775 | else | |
2776 | { | |
2777 | target = builtin_decl_implicit (BUILT_IN_UNREACHABLE); | |
2778 | cgraph_get_create_node (target); | |
2779 | } | |
da942ca0 | 2780 | } |
b258210c | 2781 | else |
5bccb77a JH |
2782 | { |
2783 | binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset, | |
2784 | ie->indirect_info->otr_type); | |
2785 | if (binfo) | |
2786 | target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token, | |
2787 | binfo); | |
2788 | else | |
2789 | return NULL; | |
2790 | } | |
b258210c MJ |
2791 | |
2792 | if (target) | |
450ad0cd JH |
2793 | { |
2794 | #ifdef ENABLE_CHECKING | |
2795 | gcc_assert (possible_polymorphic_call_target_p | |
2796 | (ie, cgraph_get_node (target))); | |
2797 | #endif | |
2798 | return ipa_make_edge_direct_to_target (ie, target); | |
2799 | } | |
b258210c MJ |
2800 | else |
2801 | return NULL; | |
3e293154 MJ |
2802 | } |
2803 | ||
2804 | /* Update the param called notes associated with NODE when CS is being inlined, | |
2805 | assuming NODE is (potentially indirectly) inlined into CS->callee. | |
2806 | Moreover, if the callee is discovered to be constant, create a new cgraph | |
e56f5f3e | 2807 | edge for it. Newly discovered indirect edges will be added to *NEW_EDGES, |
f8e2a1ed | 2808 | unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */ |
be95e2b9 | 2809 | |
f8e2a1ed | 2810 | static bool |
e33c6cd6 MJ |
2811 | update_indirect_edges_after_inlining (struct cgraph_edge *cs, |
2812 | struct cgraph_node *node, | |
9771b263 | 2813 | vec<cgraph_edge_p> *new_edges) |
3e293154 | 2814 | { |
9e97ff61 | 2815 | struct ipa_edge_args *top; |
b258210c | 2816 | struct cgraph_edge *ie, *next_ie, *new_direct_edge; |
d250540a | 2817 | struct ipa_node_params *new_root_info; |
f8e2a1ed | 2818 | bool res = false; |
3e293154 | 2819 | |
e33c6cd6 | 2820 | ipa_check_create_edge_args (); |
9e97ff61 | 2821 | top = IPA_EDGE_REF (cs); |
d250540a MJ |
2822 | new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to |
2823 | ? cs->caller->global.inlined_to | |
2824 | : cs->caller); | |
e33c6cd6 MJ |
2825 | |
2826 | for (ie = node->indirect_calls; ie; ie = next_ie) | |
3e293154 | 2827 | { |
e33c6cd6 | 2828 | struct cgraph_indirect_call_info *ici = ie->indirect_info; |
3e293154 | 2829 | struct ipa_jump_func *jfunc; |
8b7773a4 | 2830 | int param_index; |
3e293154 | 2831 | |
e33c6cd6 | 2832 | next_ie = ie->next_callee; |
3e293154 | 2833 | |
5f902d76 JH |
2834 | if (ici->param_index == -1) |
2835 | continue; | |
e33c6cd6 | 2836 | |
3e293154 | 2837 | /* We must check range due to calls with variable number of arguments: */ |
e33c6cd6 | 2838 | if (ici->param_index >= ipa_get_cs_argument_count (top)) |
3e293154 | 2839 | { |
5ee53a06 | 2840 | ici->param_index = -1; |
3e293154 MJ |
2841 | continue; |
2842 | } | |
2843 | ||
8b7773a4 MJ |
2844 | param_index = ici->param_index; |
2845 | jfunc = ipa_get_ith_jump_func (top, param_index); | |
5ee53a06 JH |
2846 | |
2847 | if (!flag_indirect_inlining) | |
36b72910 JH |
2848 | new_direct_edge = NULL; |
2849 | else if (ici->polymorphic) | |
d250540a MJ |
2850 | new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc, |
2851 | new_root_info); | |
b258210c | 2852 | else |
d250540a MJ |
2853 | new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc, |
2854 | new_root_info); | |
042ae7d2 JH |
2855 | /* If speculation was removed, then we need to do nothing. */ |
2856 | if (new_direct_edge && new_direct_edge != ie) | |
2857 | { | |
2858 | new_direct_edge->indirect_inlining_edge = 1; | |
2859 | top = IPA_EDGE_REF (cs); | |
2860 | res = true; | |
2861 | } | |
2862 | else if (new_direct_edge) | |
685b0d13 | 2863 | { |
b258210c | 2864 | new_direct_edge->indirect_inlining_edge = 1; |
89faf322 RG |
2865 | if (new_direct_edge->call_stmt) |
2866 | new_direct_edge->call_stmt_cannot_inline_p | |
4de09b85 DC |
2867 | = !gimple_check_call_matching_types ( |
2868 | new_direct_edge->call_stmt, | |
67348ccc | 2869 | new_direct_edge->callee->decl, false); |
b258210c MJ |
2870 | if (new_edges) |
2871 | { | |
9771b263 | 2872 | new_edges->safe_push (new_direct_edge); |
b258210c MJ |
2873 | res = true; |
2874 | } | |
042ae7d2 | 2875 | top = IPA_EDGE_REF (cs); |
685b0d13 | 2876 | } |
36b72910 JH |
2877 | else if (jfunc->type == IPA_JF_PASS_THROUGH |
2878 | && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR) | |
2879 | { | |
8a2256dd MJ |
2880 | if ((ici->agg_contents |
2881 | && !ipa_get_jf_pass_through_agg_preserved (jfunc)) | |
2882 | || (ici->polymorphic | |
2883 | && !ipa_get_jf_pass_through_type_preserved (jfunc))) | |
36b72910 JH |
2884 | ici->param_index = -1; |
2885 | else | |
2886 | ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc); | |
2887 | } | |
2888 | else if (jfunc->type == IPA_JF_ANCESTOR) | |
2889 | { | |
8a2256dd MJ |
2890 | if ((ici->agg_contents |
2891 | && !ipa_get_jf_ancestor_agg_preserved (jfunc)) | |
2892 | || (ici->polymorphic | |
2893 | && !ipa_get_jf_ancestor_type_preserved (jfunc))) | |
36b72910 JH |
2894 | ici->param_index = -1; |
2895 | else | |
2896 | { | |
2897 | ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc); | |
68377e53 JH |
2898 | if (ipa_get_jf_ancestor_offset (jfunc)) |
2899 | ici->outer_type = NULL; | |
36b72910 JH |
2900 | ici->offset += ipa_get_jf_ancestor_offset (jfunc); |
2901 | } | |
2902 | } | |
2903 | else | |
2904 | /* Either we can find a destination for this edge now or never. */ | |
2905 | ici->param_index = -1; | |
3e293154 | 2906 | } |
e33c6cd6 | 2907 | |
f8e2a1ed | 2908 | return res; |
3e293154 MJ |
2909 | } |
2910 | ||
2911 | /* Recursively traverse subtree of NODE (including node) made of inlined | |
2912 | cgraph_edges when CS has been inlined and invoke | |
e33c6cd6 | 2913 | update_indirect_edges_after_inlining on all nodes and |
3e293154 MJ |
2914 | update_jump_functions_after_inlining on all non-inlined edges that lead out |
2915 | of this subtree. Newly discovered indirect edges will be added to | |
f8e2a1ed MJ |
2916 | *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were |
2917 | created. */ | |
be95e2b9 | 2918 | |
f8e2a1ed | 2919 | static bool |
3e293154 MJ |
2920 | propagate_info_to_inlined_callees (struct cgraph_edge *cs, |
2921 | struct cgraph_node *node, | |
9771b263 | 2922 | vec<cgraph_edge_p> *new_edges) |
3e293154 MJ |
2923 | { |
2924 | struct cgraph_edge *e; | |
f8e2a1ed | 2925 | bool res; |
3e293154 | 2926 | |
e33c6cd6 | 2927 | res = update_indirect_edges_after_inlining (cs, node, new_edges); |
3e293154 MJ |
2928 | |
2929 | for (e = node->callees; e; e = e->next_callee) | |
2930 | if (!e->inline_failed) | |
f8e2a1ed | 2931 | res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges); |
3e293154 MJ |
2932 | else |
2933 | update_jump_functions_after_inlining (cs, e); | |
5ee53a06 JH |
2934 | for (e = node->indirect_calls; e; e = e->next_callee) |
2935 | update_jump_functions_after_inlining (cs, e); | |
f8e2a1ed MJ |
2936 | |
2937 | return res; | |
3e293154 MJ |
2938 | } |
2939 | ||
4502fe8d MJ |
2940 | /* Combine two controlled uses counts as done during inlining. */ |
2941 | ||
2942 | static int | |
2943 | combine_controlled_uses_counters (int c, int d) | |
2944 | { | |
2945 | if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE) | |
2946 | return IPA_UNDESCRIBED_USE; | |
2947 | else | |
2948 | return c + d - 1; | |
2949 | } | |
2950 | ||
2951 | /* Propagate number of controlled users from CS->caleee to the new root of the | |
2952 | tree of inlined nodes. */ | |
2953 | ||
2954 | static void | |
2955 | propagate_controlled_uses (struct cgraph_edge *cs) | |
2956 | { | |
2957 | struct ipa_edge_args *args = IPA_EDGE_REF (cs); | |
2958 | struct cgraph_node *new_root = cs->caller->global.inlined_to | |
2959 | ? cs->caller->global.inlined_to : cs->caller; | |
2960 | struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root); | |
2961 | struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee); | |
2962 | int count, i; | |
2963 | ||
2964 | count = MIN (ipa_get_cs_argument_count (args), | |
2965 | ipa_get_param_count (old_root_info)); | |
2966 | for (i = 0; i < count; i++) | |
2967 | { | |
2968 | struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i); | |
2969 | struct ipa_cst_ref_desc *rdesc; | |
2970 | ||
2971 | if (jf->type == IPA_JF_PASS_THROUGH) | |
2972 | { | |
2973 | int src_idx, c, d; | |
2974 | src_idx = ipa_get_jf_pass_through_formal_id (jf); | |
2975 | c = ipa_get_controlled_uses (new_root_info, src_idx); | |
2976 | d = ipa_get_controlled_uses (old_root_info, i); | |
2977 | ||
2978 | gcc_checking_assert (ipa_get_jf_pass_through_operation (jf) | |
2979 | == NOP_EXPR || c == IPA_UNDESCRIBED_USE); | |
2980 | c = combine_controlled_uses_counters (c, d); | |
2981 | ipa_set_controlled_uses (new_root_info, src_idx, c); | |
2982 | if (c == 0 && new_root_info->ipcp_orig_node) | |
2983 | { | |
2984 | struct cgraph_node *n; | |
2985 | struct ipa_ref *ref; | |
2986 | tree t = new_root_info->known_vals[src_idx]; | |
2987 | ||
2988 | if (t && TREE_CODE (t) == ADDR_EXPR | |
2989 | && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL | |
2990 | && (n = cgraph_get_node (TREE_OPERAND (t, 0))) | |
67348ccc DM |
2991 | && (ref = ipa_find_reference (new_root, |
2992 | n, NULL, 0))) | |
4502fe8d MJ |
2993 | { |
2994 | if (dump_file) | |
2995 | fprintf (dump_file, "ipa-prop: Removing cloning-created " | |
2996 | "reference from %s/%i to %s/%i.\n", | |
fec39fa6 | 2997 | xstrdup (new_root->name ()), |
67348ccc | 2998 | new_root->order, |
fec39fa6 | 2999 | xstrdup (n->name ()), n->order); |
4502fe8d MJ |
3000 | ipa_remove_reference (ref); |
3001 | } | |
3002 | } | |
3003 | } | |
3004 | else if (jf->type == IPA_JF_CONST | |
3005 | && (rdesc = jfunc_rdesc_usable (jf))) | |
3006 | { | |
3007 | int d = ipa_get_controlled_uses (old_root_info, i); | |
3008 | int c = rdesc->refcount; | |
3009 | rdesc->refcount = combine_controlled_uses_counters (c, d); | |
3010 | if (rdesc->refcount == 0) | |
3011 | { | |
3012 | tree cst = ipa_get_jf_constant (jf); | |
3013 | struct cgraph_node *n; | |
3014 | gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR | |
3015 | && TREE_CODE (TREE_OPERAND (cst, 0)) | |
3016 | == FUNCTION_DECL); | |
3017 | n = cgraph_get_node (TREE_OPERAND (cst, 0)); | |
3018 | if (n) | |
3019 | { | |
3020 | struct cgraph_node *clone; | |
568cda29 | 3021 | bool ok; |
67348ccc | 3022 | ok = remove_described_reference (n, rdesc); |
568cda29 | 3023 | gcc_checking_assert (ok); |
4502fe8d MJ |
3024 | |
3025 | clone = cs->caller; | |
3026 | while (clone->global.inlined_to | |
3027 | && clone != rdesc->cs->caller | |
3028 | && IPA_NODE_REF (clone)->ipcp_orig_node) | |
3029 | { | |
3030 | struct ipa_ref *ref; | |
67348ccc DM |
3031 | ref = ipa_find_reference (clone, |
3032 | n, NULL, 0); | |
4502fe8d MJ |
3033 | if (ref) |
3034 | { | |
3035 | if (dump_file) | |
3036 | fprintf (dump_file, "ipa-prop: Removing " | |
3037 | "cloning-created reference " | |
3038 | "from %s/%i to %s/%i.\n", | |
fec39fa6 | 3039 | xstrdup (clone->name ()), |
67348ccc | 3040 | clone->order, |
fec39fa6 | 3041 | xstrdup (n->name ()), |
67348ccc | 3042 | n->order); |
4502fe8d MJ |
3043 | ipa_remove_reference (ref); |
3044 | } | |
3045 | clone = clone->callers->caller; | |
3046 | } | |
3047 | } | |
3048 | } | |
3049 | } | |
3050 | } | |
3051 | ||
3052 | for (i = ipa_get_param_count (old_root_info); | |
3053 | i < ipa_get_cs_argument_count (args); | |
3054 | i++) | |
3055 | { | |
3056 | struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i); | |
3057 | ||
3058 | if (jf->type == IPA_JF_CONST) | |
3059 | { | |
3060 | struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf); | |
3061 | if (rdesc) | |
3062 | rdesc->refcount = IPA_UNDESCRIBED_USE; | |
3063 | } | |
3064 | else if (jf->type == IPA_JF_PASS_THROUGH) | |
3065 | ipa_set_controlled_uses (new_root_info, | |
3066 | jf->value.pass_through.formal_id, | |
3067 | IPA_UNDESCRIBED_USE); | |
3068 | } | |
3069 | } | |
3070 | ||
3e293154 MJ |
3071 | /* Update jump functions and call note functions on inlining the call site CS. |
3072 | CS is expected to lead to a node already cloned by | |
3073 | cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to | |
f8e2a1ed MJ |
3074 | *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were + |
3075 | created. */ | |
be95e2b9 | 3076 | |
f8e2a1ed | 3077 | bool |
3e293154 | 3078 | ipa_propagate_indirect_call_infos (struct cgraph_edge *cs, |
9771b263 | 3079 | vec<cgraph_edge_p> *new_edges) |
3e293154 | 3080 | { |
5ee53a06 | 3081 | bool changed; |
f8e2a1ed MJ |
3082 | /* Do nothing if the preparation phase has not been carried out yet |
3083 | (i.e. during early inlining). */ | |
9771b263 | 3084 | if (!ipa_node_params_vector.exists ()) |
f8e2a1ed MJ |
3085 | return false; |
3086 | gcc_assert (ipa_edge_args_vector); | |
3087 | ||
4502fe8d | 3088 | propagate_controlled_uses (cs); |
5ee53a06 JH |
3089 | changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges); |
3090 | ||
5ee53a06 | 3091 | return changed; |
518dc859 RL |
3092 | } |
3093 | ||
771578a0 MJ |
3094 | /* Frees all dynamically allocated structures that the argument info points |
3095 | to. */ | |
be95e2b9 | 3096 | |
518dc859 | 3097 | void |
771578a0 | 3098 | ipa_free_edge_args_substructures (struct ipa_edge_args *args) |
518dc859 | 3099 | { |
9771b263 | 3100 | vec_free (args->jump_functions); |
771578a0 | 3101 | memset (args, 0, sizeof (*args)); |
518dc859 RL |
3102 | } |
3103 | ||
771578a0 | 3104 | /* Free all ipa_edge structures. */ |
be95e2b9 | 3105 | |
518dc859 | 3106 | void |
771578a0 | 3107 | ipa_free_all_edge_args (void) |
518dc859 | 3108 | { |
771578a0 MJ |
3109 | int i; |
3110 | struct ipa_edge_args *args; | |
518dc859 | 3111 | |
9771b263 DN |
3112 | if (!ipa_edge_args_vector) |
3113 | return; | |
3114 | ||
3115 | FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args) | |
771578a0 MJ |
3116 | ipa_free_edge_args_substructures (args); |
3117 | ||
9771b263 | 3118 | vec_free (ipa_edge_args_vector); |
518dc859 RL |
3119 | } |
3120 | ||
771578a0 MJ |
3121 | /* Frees all dynamically allocated structures that the param info points |
3122 | to. */ | |
be95e2b9 | 3123 | |
518dc859 | 3124 | void |
771578a0 | 3125 | ipa_free_node_params_substructures (struct ipa_node_params *info) |
518dc859 | 3126 | { |
9771b263 | 3127 | info->descriptors.release (); |
310bc633 MJ |
3128 | free (info->lattices); |
3129 | /* Lattice values and their sources are deallocated with their alocation | |
3130 | pool. */ | |
9771b263 | 3131 | info->known_vals.release (); |
771578a0 | 3132 | memset (info, 0, sizeof (*info)); |
518dc859 RL |
3133 | } |
3134 | ||
771578a0 | 3135 | /* Free all ipa_node_params structures. */ |
be95e2b9 | 3136 | |
518dc859 | 3137 | void |
771578a0 | 3138 | ipa_free_all_node_params (void) |
518dc859 | 3139 | { |
771578a0 MJ |
3140 | int i; |
3141 | struct ipa_node_params *info; | |
518dc859 | 3142 | |
9771b263 | 3143 | FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info) |
771578a0 MJ |
3144 | ipa_free_node_params_substructures (info); |
3145 | ||
9771b263 | 3146 | ipa_node_params_vector.release (); |
771578a0 MJ |
3147 | } |
3148 | ||
2c9561b5 MJ |
3149 | /* Set the aggregate replacements of NODE to be AGGVALS. */ |
3150 | ||
3151 | void | |
3152 | ipa_set_node_agg_value_chain (struct cgraph_node *node, | |
3153 | struct ipa_agg_replacement_value *aggvals) | |
3154 | { | |
9771b263 DN |
3155 | if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid) |
3156 | vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1); | |
2c9561b5 | 3157 | |
9771b263 | 3158 | (*ipa_node_agg_replacements)[node->uid] = aggvals; |
2c9561b5 MJ |
3159 | } |
3160 | ||
771578a0 | 3161 | /* Hook that is called by cgraph.c when an edge is removed. */ |
be95e2b9 | 3162 | |
771578a0 | 3163 | static void |
5c0466b5 | 3164 | ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED) |
771578a0 | 3165 | { |
568cda29 MJ |
3166 | struct ipa_edge_args *args; |
3167 | ||
3168 | /* During IPA-CP updating we can be called on not-yet analyzed clones. */ | |
9771b263 | 3169 | if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid) |
c6f7cfc1 | 3170 | return; |
568cda29 MJ |
3171 | |
3172 | args = IPA_EDGE_REF (cs); | |
3173 | if (args->jump_functions) | |
3174 | { | |
3175 | struct ipa_jump_func *jf; | |
3176 | int i; | |
3177 | FOR_EACH_VEC_ELT (*args->jump_functions, i, jf) | |
a854f856 MJ |
3178 | { |
3179 | struct ipa_cst_ref_desc *rdesc; | |
3180 | try_decrement_rdesc_refcount (jf); | |
3181 | if (jf->type == IPA_JF_CONST | |
3182 | && (rdesc = ipa_get_jf_constant_rdesc (jf)) | |
3183 | && rdesc->cs == cs) | |
3184 | rdesc->cs = NULL; | |
3185 | } | |
568cda29 MJ |
3186 | } |
3187 | ||
771578a0 | 3188 | ipa_free_edge_args_substructures (IPA_EDGE_REF (cs)); |
518dc859 RL |
3189 | } |
3190 | ||
771578a0 | 3191 | /* Hook that is called by cgraph.c when a node is removed. */ |
be95e2b9 | 3192 | |
771578a0 | 3193 | static void |
5c0466b5 | 3194 | ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) |
771578a0 | 3195 | { |
dd6d1ad7 | 3196 | /* During IPA-CP updating we can be called on not-yet analyze clones. */ |
9771b263 | 3197 | if (ipa_node_params_vector.length () > (unsigned)node->uid) |
2c9561b5 | 3198 | ipa_free_node_params_substructures (IPA_NODE_REF (node)); |
9771b263 DN |
3199 | if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid) |
3200 | (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL; | |
771578a0 MJ |
3201 | } |
3202 | ||
8b7773a4 | 3203 | /* Hook that is called by cgraph.c when an edge is duplicated. */ |
be95e2b9 | 3204 | |
771578a0 MJ |
3205 | static void |
3206 | ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst, | |
f8e2a1ed | 3207 | __attribute__((unused)) void *data) |
771578a0 MJ |
3208 | { |
3209 | struct ipa_edge_args *old_args, *new_args; | |
8b7773a4 | 3210 | unsigned int i; |
771578a0 MJ |
3211 | |
3212 | ipa_check_create_edge_args (); | |
3213 | ||
3214 | old_args = IPA_EDGE_REF (src); | |
3215 | new_args = IPA_EDGE_REF (dst); | |
3216 | ||
9771b263 | 3217 | new_args->jump_functions = vec_safe_copy (old_args->jump_functions); |
8b7773a4 | 3218 | |
9771b263 | 3219 | for (i = 0; i < vec_safe_length (old_args->jump_functions); i++) |
4502fe8d MJ |
3220 | { |
3221 | struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i); | |
3222 | struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i); | |
3223 | ||
3224 | dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items); | |
3225 | ||
3226 | if (src_jf->type == IPA_JF_CONST) | |
3227 | { | |
3228 | struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf); | |
3229 | ||
3230 | if (!src_rdesc) | |
3231 | dst_jf->value.constant.rdesc = NULL; | |
568cda29 MJ |
3232 | else if (src->caller == dst->caller) |
3233 | { | |
3234 | struct ipa_ref *ref; | |
5e20cdc9 | 3235 | symtab_node *n = cgraph_node_for_jfunc (src_jf); |
568cda29 | 3236 | gcc_checking_assert (n); |
67348ccc | 3237 | ref = ipa_find_reference (src->caller, n, |
568cda29 MJ |
3238 | src->call_stmt, src->lto_stmt_uid); |
3239 | gcc_checking_assert (ref); | |
67348ccc | 3240 | ipa_clone_ref (ref, dst->caller, ref->stmt); |
568cda29 MJ |
3241 | |
3242 | gcc_checking_assert (ipa_refdesc_pool); | |
3243 | struct ipa_cst_ref_desc *dst_rdesc | |
3244 | = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool); | |
3245 | dst_rdesc->cs = dst; | |
3246 | dst_rdesc->refcount = src_rdesc->refcount; | |
3247 | dst_rdesc->next_duplicate = NULL; | |
3248 | dst_jf->value.constant.rdesc = dst_rdesc; | |
3249 | } | |
4502fe8d MJ |
3250 | else if (src_rdesc->cs == src) |
3251 | { | |
3252 | struct ipa_cst_ref_desc *dst_rdesc; | |
3253 | gcc_checking_assert (ipa_refdesc_pool); | |
3254 | dst_rdesc | |
3255 | = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool); | |
3256 | dst_rdesc->cs = dst; | |
4502fe8d | 3257 | dst_rdesc->refcount = src_rdesc->refcount; |
2fd0985c MJ |
3258 | dst_rdesc->next_duplicate = src_rdesc->next_duplicate; |
3259 | src_rdesc->next_duplicate = dst_rdesc; | |
4502fe8d MJ |
3260 | dst_jf->value.constant.rdesc = dst_rdesc; |
3261 | } | |
3262 | else | |
3263 | { | |
3264 | struct ipa_cst_ref_desc *dst_rdesc; | |
3265 | /* This can happen during inlining, when a JFUNC can refer to a | |
3266 | reference taken in a function up in the tree of inline clones. | |
3267 | We need to find the duplicate that refers to our tree of | |
3268 | inline clones. */ | |
3269 | ||
3270 | gcc_assert (dst->caller->global.inlined_to); | |
3271 | for (dst_rdesc = src_rdesc->next_duplicate; | |
3272 | dst_rdesc; | |
3273 | dst_rdesc = dst_rdesc->next_duplicate) | |
2fd0985c MJ |
3274 | { |
3275 | struct cgraph_node *top; | |
3276 | top = dst_rdesc->cs->caller->global.inlined_to | |
3277 | ? dst_rdesc->cs->caller->global.inlined_to | |
3278 | : dst_rdesc->cs->caller; | |
3279 | if (dst->caller->global.inlined_to == top) | |
3280 | break; | |
3281 | } | |
44a60244 | 3282 | gcc_assert (dst_rdesc); |
4502fe8d MJ |
3283 | dst_jf->value.constant.rdesc = dst_rdesc; |
3284 | } | |
3285 | } | |
3286 | } | |
771578a0 MJ |
3287 | } |
3288 | ||
3289 | /* Hook that is called by cgraph.c when a node is duplicated. */ | |
be95e2b9 | 3290 | |
771578a0 MJ |
3291 | static void |
3292 | ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst, | |
10a5dd5d | 3293 | ATTRIBUTE_UNUSED void *data) |
771578a0 MJ |
3294 | { |
3295 | struct ipa_node_params *old_info, *new_info; | |
2c9561b5 | 3296 | struct ipa_agg_replacement_value *old_av, *new_av; |
771578a0 MJ |
3297 | |
3298 | ipa_check_create_node_params (); | |
3299 | old_info = IPA_NODE_REF (src); | |
3300 | new_info = IPA_NODE_REF (dst); | |
771578a0 | 3301 | |
9771b263 | 3302 | new_info->descriptors = old_info->descriptors.copy (); |
310bc633 | 3303 | new_info->lattices = NULL; |
771578a0 | 3304 | new_info->ipcp_orig_node = old_info->ipcp_orig_node; |
3949c4a7 | 3305 | |
3949c4a7 MJ |
3306 | new_info->uses_analysis_done = old_info->uses_analysis_done; |
3307 | new_info->node_enqueued = old_info->node_enqueued; | |
2c9561b5 MJ |
3308 | |
3309 | old_av = ipa_get_agg_replacements_for_node (src); | |
3310 | if (!old_av) | |
3311 | return; | |
3312 | ||
3313 | new_av = NULL; | |
3314 | while (old_av) | |
3315 | { | |
3316 | struct ipa_agg_replacement_value *v; | |
3317 | ||
766090c2 | 3318 | v = ggc_alloc<ipa_agg_replacement_value> (); |
2c9561b5 MJ |
3319 | memcpy (v, old_av, sizeof (*v)); |
3320 | v->next = new_av; | |
3321 | new_av = v; | |
3322 | old_av = old_av->next; | |
3323 | } | |
3324 | ipa_set_node_agg_value_chain (dst, new_av); | |
771578a0 MJ |
3325 | } |
3326 | ||
40982661 JH |
3327 | |
3328 | /* Analyze newly added function into callgraph. */ | |
3329 | ||
3330 | static void | |
3331 | ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
3332 | { | |
0136f8f0 AH |
3333 | if (cgraph_function_with_gimple_body_p (node)) |
3334 | ipa_analyze_node (node); | |
40982661 JH |
3335 | } |
3336 | ||
771578a0 | 3337 | /* Register our cgraph hooks if they are not already there. */ |
be95e2b9 | 3338 | |
518dc859 | 3339 | void |
771578a0 | 3340 | ipa_register_cgraph_hooks (void) |
518dc859 | 3341 | { |
771578a0 MJ |
3342 | if (!edge_removal_hook_holder) |
3343 | edge_removal_hook_holder = | |
3344 | cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL); | |
3345 | if (!node_removal_hook_holder) | |
3346 | node_removal_hook_holder = | |
3347 | cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL); | |
3348 | if (!edge_duplication_hook_holder) | |
3349 | edge_duplication_hook_holder = | |
3350 | cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL); | |
3351 | if (!node_duplication_hook_holder) | |
3352 | node_duplication_hook_holder = | |
3353 | cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL); | |
40982661 JH |
3354 | function_insertion_hook_holder = |
3355 | cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL); | |
771578a0 | 3356 | } |
518dc859 | 3357 | |
771578a0 | 3358 | /* Unregister our cgraph hooks if they are not already there. */ |
be95e2b9 | 3359 | |
771578a0 MJ |
3360 | static void |
3361 | ipa_unregister_cgraph_hooks (void) | |
3362 | { | |
3363 | cgraph_remove_edge_removal_hook (edge_removal_hook_holder); | |
3364 | edge_removal_hook_holder = NULL; | |
3365 | cgraph_remove_node_removal_hook (node_removal_hook_holder); | |
3366 | node_removal_hook_holder = NULL; | |
3367 | cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder); | |
3368 | edge_duplication_hook_holder = NULL; | |
3369 | cgraph_remove_node_duplication_hook (node_duplication_hook_holder); | |
3370 | node_duplication_hook_holder = NULL; | |
40982661 JH |
3371 | cgraph_remove_function_insertion_hook (function_insertion_hook_holder); |
3372 | function_insertion_hook_holder = NULL; | |
771578a0 MJ |
3373 | } |
3374 | ||
3375 | /* Free all ipa_node_params and all ipa_edge_args structures if they are no | |
3376 | longer needed after ipa-cp. */ | |
be95e2b9 | 3377 | |
771578a0 | 3378 | void |
e33c6cd6 | 3379 | ipa_free_all_structures_after_ipa_cp (void) |
3e293154 | 3380 | { |
5ee53a06 | 3381 | if (!optimize) |
3e293154 MJ |
3382 | { |
3383 | ipa_free_all_edge_args (); | |
3384 | ipa_free_all_node_params (); | |
310bc633 MJ |
3385 | free_alloc_pool (ipcp_sources_pool); |
3386 | free_alloc_pool (ipcp_values_pool); | |
2c9561b5 | 3387 | free_alloc_pool (ipcp_agg_lattice_pool); |
3e293154 | 3388 | ipa_unregister_cgraph_hooks (); |
4502fe8d MJ |
3389 | if (ipa_refdesc_pool) |
3390 | free_alloc_pool (ipa_refdesc_pool); | |
3e293154 MJ |
3391 | } |
3392 | } | |
3393 | ||
3394 | /* Free all ipa_node_params and all ipa_edge_args structures if they are no | |
3395 | longer needed after indirect inlining. */ | |
be95e2b9 | 3396 | |
3e293154 | 3397 | void |
e33c6cd6 | 3398 | ipa_free_all_structures_after_iinln (void) |
771578a0 MJ |
3399 | { |
3400 | ipa_free_all_edge_args (); | |
3401 | ipa_free_all_node_params (); | |
3402 | ipa_unregister_cgraph_hooks (); | |
310bc633 MJ |
3403 | if (ipcp_sources_pool) |
3404 | free_alloc_pool (ipcp_sources_pool); | |
3405 | if (ipcp_values_pool) | |
3406 | free_alloc_pool (ipcp_values_pool); | |
2c9561b5 MJ |
3407 | if (ipcp_agg_lattice_pool) |
3408 | free_alloc_pool (ipcp_agg_lattice_pool); | |
4502fe8d MJ |
3409 | if (ipa_refdesc_pool) |
3410 | free_alloc_pool (ipa_refdesc_pool); | |
518dc859 RL |
3411 | } |
3412 | ||
dcd416e3 | 3413 | /* Print ipa_tree_map data structures of all functions in the |
518dc859 | 3414 | callgraph to F. */ |
be95e2b9 | 3415 | |
518dc859 | 3416 | void |
2c9561b5 | 3417 | ipa_print_node_params (FILE *f, struct cgraph_node *node) |
518dc859 RL |
3418 | { |
3419 | int i, count; | |
3e293154 | 3420 | struct ipa_node_params *info; |
518dc859 | 3421 | |
67348ccc | 3422 | if (!node->definition) |
3e293154 MJ |
3423 | return; |
3424 | info = IPA_NODE_REF (node); | |
9de04252 | 3425 | fprintf (f, " function %s/%i parameter descriptors:\n", |
fec39fa6 | 3426 | node->name (), node->order); |
3e293154 MJ |
3427 | count = ipa_get_param_count (info); |
3428 | for (i = 0; i < count; i++) | |
518dc859 | 3429 | { |
4502fe8d MJ |
3430 | int c; |
3431 | ||
a4e33812 | 3432 | fprintf (f, " "); |
e067bd43 | 3433 | ipa_dump_param (f, info, i); |
339f49ec JH |
3434 | if (ipa_is_param_used (info, i)) |
3435 | fprintf (f, " used"); | |
4502fe8d MJ |
3436 | c = ipa_get_controlled_uses (info, i); |
3437 | if (c == IPA_UNDESCRIBED_USE) | |
3438 | fprintf (f, " undescribed_use"); | |
3439 | else | |
3440 | fprintf (f, " controlled_uses=%i", c); | |
3e293154 | 3441 | fprintf (f, "\n"); |
518dc859 RL |
3442 | } |
3443 | } | |
dcd416e3 | 3444 | |
ca30a539 | 3445 | /* Print ipa_tree_map data structures of all functions in the |
3e293154 | 3446 | callgraph to F. */ |
be95e2b9 | 3447 | |
3e293154 | 3448 | void |
ca30a539 | 3449 | ipa_print_all_params (FILE * f) |
3e293154 MJ |
3450 | { |
3451 | struct cgraph_node *node; | |
3452 | ||
ca30a539 | 3453 | fprintf (f, "\nFunction parameters:\n"); |
65c70e6b | 3454 | FOR_EACH_FUNCTION (node) |
ca30a539 | 3455 | ipa_print_node_params (f, node); |
3e293154 | 3456 | } |
3f84bf08 MJ |
3457 | |
3458 | /* Return a heap allocated vector containing formal parameters of FNDECL. */ | |
3459 | ||
9771b263 | 3460 | vec<tree> |
3f84bf08 MJ |
3461 | ipa_get_vector_of_formal_parms (tree fndecl) |
3462 | { | |
9771b263 | 3463 | vec<tree> args; |
3f84bf08 MJ |
3464 | int count; |
3465 | tree parm; | |
3466 | ||
0e8853ee | 3467 | gcc_assert (!flag_wpa); |
310bc633 | 3468 | count = count_formal_params (fndecl); |
9771b263 | 3469 | args.create (count); |
910ad8de | 3470 | for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm)) |
9771b263 | 3471 | args.quick_push (parm); |
3f84bf08 MJ |
3472 | |
3473 | return args; | |
3474 | } | |
3475 | ||
3476 | /* Return a heap allocated vector containing types of formal parameters of | |
3477 | function type FNTYPE. */ | |
3478 | ||
31519c38 AH |
3479 | vec<tree> |
3480 | ipa_get_vector_of_formal_parm_types (tree fntype) | |
3f84bf08 | 3481 | { |
9771b263 | 3482 | vec<tree> types; |
3f84bf08 MJ |
3483 | int count = 0; |
3484 | tree t; | |
3485 | ||
3486 | for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t)) | |
3487 | count++; | |
3488 | ||
9771b263 | 3489 | types.create (count); |
3f84bf08 | 3490 | for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t)) |
9771b263 | 3491 | types.quick_push (TREE_VALUE (t)); |
3f84bf08 MJ |
3492 | |
3493 | return types; | |
3494 | } | |
3495 | ||
3496 | /* Modify the function declaration FNDECL and its type according to the plan in | |
3497 | ADJUSTMENTS. It also sets base fields of individual adjustments structures | |
3498 | to reflect the actual parameters being modified which are determined by the | |
3499 | base_index field. */ | |
3500 | ||
3501 | void | |
31519c38 | 3502 | ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments) |
3f84bf08 | 3503 | { |
31519c38 AH |
3504 | vec<tree> oparms = ipa_get_vector_of_formal_parms (fndecl); |
3505 | tree orig_type = TREE_TYPE (fndecl); | |
3506 | tree old_arg_types = TYPE_ARG_TYPES (orig_type); | |
3f84bf08 MJ |
3507 | |
3508 | /* The following test is an ugly hack, some functions simply don't have any | |
3509 | arguments in their type. This is probably a bug but well... */ | |
31519c38 AH |
3510 | bool care_for_types = (old_arg_types != NULL_TREE); |
3511 | bool last_parm_void; | |
3512 | vec<tree> otypes; | |
3f84bf08 MJ |
3513 | if (care_for_types) |
3514 | { | |
3515 | last_parm_void = (TREE_VALUE (tree_last (old_arg_types)) | |
3516 | == void_type_node); | |
31519c38 | 3517 | otypes = ipa_get_vector_of_formal_parm_types (orig_type); |
3f84bf08 | 3518 | if (last_parm_void) |
9771b263 | 3519 | gcc_assert (oparms.length () + 1 == otypes.length ()); |
3f84bf08 | 3520 | else |
9771b263 | 3521 | gcc_assert (oparms.length () == otypes.length ()); |
3f84bf08 MJ |
3522 | } |
3523 | else | |
3524 | { | |
3525 | last_parm_void = false; | |
9771b263 | 3526 | otypes.create (0); |
3f84bf08 MJ |
3527 | } |
3528 | ||
31519c38 AH |
3529 | int len = adjustments.length (); |
3530 | tree *link = &DECL_ARGUMENTS (fndecl); | |
3531 | tree new_arg_types = NULL; | |
3532 | for (int i = 0; i < len; i++) | |
3f84bf08 MJ |
3533 | { |
3534 | struct ipa_parm_adjustment *adj; | |
3535 | gcc_assert (link); | |
3536 | ||
9771b263 | 3537 | adj = &adjustments[i]; |
31519c38 AH |
3538 | tree parm; |
3539 | if (adj->op == IPA_PARM_OP_NEW) | |
3540 | parm = NULL; | |
3541 | else | |
3542 | parm = oparms[adj->base_index]; | |
3f84bf08 MJ |
3543 | adj->base = parm; |
3544 | ||
31519c38 | 3545 | if (adj->op == IPA_PARM_OP_COPY) |
3f84bf08 MJ |
3546 | { |
3547 | if (care_for_types) | |
9771b263 | 3548 | new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index], |
3f84bf08 MJ |
3549 | new_arg_types); |
3550 | *link = parm; | |
910ad8de | 3551 | link = &DECL_CHAIN (parm); |
3f84bf08 | 3552 | } |
31519c38 | 3553 | else if (adj->op != IPA_PARM_OP_REMOVE) |
3f84bf08 MJ |
3554 | { |
3555 | tree new_parm; | |
3556 | tree ptype; | |
3557 | ||
3558 | if (adj->by_ref) | |
3559 | ptype = build_pointer_type (adj->type); | |
3560 | else | |
e69dbe37 MJ |
3561 | { |
3562 | ptype = adj->type; | |
3563 | if (is_gimple_reg_type (ptype)) | |
3564 | { | |
3565 | unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ptype)); | |
3566 | if (TYPE_ALIGN (ptype) < malign) | |
3567 | ptype = build_aligned_type (ptype, malign); | |
3568 | } | |
3569 | } | |
3f84bf08 MJ |
3570 | |
3571 | if (care_for_types) | |
3572 | new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types); | |
3573 | ||
3574 | new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE, | |
3575 | ptype); | |
31519c38 AH |
3576 | const char *prefix = adj->arg_prefix ? adj->arg_prefix : "SYNTH"; |
3577 | DECL_NAME (new_parm) = create_tmp_var_name (prefix); | |
3f84bf08 MJ |
3578 | DECL_ARTIFICIAL (new_parm) = 1; |
3579 | DECL_ARG_TYPE (new_parm) = ptype; | |
3580 | DECL_CONTEXT (new_parm) = fndecl; | |
3581 | TREE_USED (new_parm) = 1; | |
3582 | DECL_IGNORED_P (new_parm) = 1; | |
3583 | layout_decl (new_parm, 0); | |
3584 | ||
31519c38 AH |
3585 | if (adj->op == IPA_PARM_OP_NEW) |
3586 | adj->base = NULL; | |
3587 | else | |
3588 | adj->base = parm; | |
3589 | adj->new_decl = new_parm; | |
3f84bf08 MJ |
3590 | |
3591 | *link = new_parm; | |
910ad8de | 3592 | link = &DECL_CHAIN (new_parm); |
3f84bf08 MJ |
3593 | } |
3594 | } | |
3595 | ||
3596 | *link = NULL_TREE; | |
3597 | ||
31519c38 | 3598 | tree new_reversed = NULL; |
3f84bf08 MJ |
3599 | if (care_for_types) |
3600 | { | |
3601 | new_reversed = nreverse (new_arg_types); | |
3602 | if (last_parm_void) | |
3603 | { | |
3604 | if (new_reversed) | |
3605 | TREE_CHAIN (new_arg_types) = void_list_node; | |
3606 | else | |
3607 | new_reversed = void_list_node; | |
3608 | } | |
3609 | } | |
3610 | ||
3611 | /* Use copy_node to preserve as much as possible from original type | |
3612 | (debug info, attribute lists etc.) | |
3613 | Exception is METHOD_TYPEs must have THIS argument. | |
3614 | When we are asked to remove it, we need to build new FUNCTION_TYPE | |
3615 | instead. */ | |
31519c38 | 3616 | tree new_type = NULL; |
3f84bf08 | 3617 | if (TREE_CODE (orig_type) != METHOD_TYPE |
31519c38 | 3618 | || (adjustments[0].op == IPA_PARM_OP_COPY |
9771b263 | 3619 | && adjustments[0].base_index == 0)) |
3f84bf08 | 3620 | { |
4eb3f32c | 3621 | new_type = build_distinct_type_copy (orig_type); |
3f84bf08 MJ |
3622 | TYPE_ARG_TYPES (new_type) = new_reversed; |
3623 | } | |
3624 | else | |
3625 | { | |
3626 | new_type | |
3627 | = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type), | |
3628 | new_reversed)); | |
3629 | TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type); | |
3630 | DECL_VINDEX (fndecl) = NULL_TREE; | |
3631 | } | |
3632 | ||
d402c33d JH |
3633 | /* When signature changes, we need to clear builtin info. */ |
3634 | if (DECL_BUILT_IN (fndecl)) | |
3635 | { | |
3636 | DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN; | |
3637 | DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0; | |
3638 | } | |
3639 | ||
3f84bf08 MJ |
3640 | /* This is a new type, not a copy of an old type. Need to reassociate |
3641 | variants. We can handle everything except the main variant lazily. */ | |
31519c38 | 3642 | tree t = TYPE_MAIN_VARIANT (orig_type); |
3f84bf08 MJ |
3643 | if (orig_type != t) |
3644 | { | |
3645 | TYPE_MAIN_VARIANT (new_type) = t; | |
3646 | TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t); | |
3647 | TYPE_NEXT_VARIANT (t) = new_type; | |
3648 | } | |
3649 | else | |
3650 | { | |
3651 | TYPE_MAIN_VARIANT (new_type) = new_type; | |
3652 | TYPE_NEXT_VARIANT (new_type) = NULL; | |
3653 | } | |
3654 | ||
3655 | TREE_TYPE (fndecl) = new_type; | |
9b389a5e | 3656 | DECL_VIRTUAL_P (fndecl) = 0; |
70d6d5c1 | 3657 | DECL_LANG_SPECIFIC (fndecl) = NULL; |
9771b263 DN |
3658 | otypes.release (); |
3659 | oparms.release (); | |
3f84bf08 MJ |
3660 | } |
3661 | ||
3662 | /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS. | |
3663 | If this is a directly recursive call, CS must be NULL. Otherwise it must | |
3664 | contain the corresponding call graph edge. */ | |
3665 | ||
3666 | void | |
3667 | ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt, | |
3668 | ipa_parm_adjustment_vec adjustments) | |
3669 | { | |
82338059 | 3670 | struct cgraph_node *current_node = cgraph_get_node (current_function_decl); |
9771b263 DN |
3671 | vec<tree> vargs; |
3672 | vec<tree, va_gc> **debug_args = NULL; | |
3f84bf08 | 3673 | gimple new_stmt; |
82338059 | 3674 | gimple_stmt_iterator gsi, prev_gsi; |
3f84bf08 MJ |
3675 | tree callee_decl; |
3676 | int i, len; | |
3677 | ||
9771b263 DN |
3678 | len = adjustments.length (); |
3679 | vargs.create (len); | |
67348ccc DM |
3680 | callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl; |
3681 | ipa_remove_stmt_references (current_node, stmt); | |
3f84bf08 MJ |
3682 | |
3683 | gsi = gsi_for_stmt (stmt); | |
82338059 MJ |
3684 | prev_gsi = gsi; |
3685 | gsi_prev (&prev_gsi); | |
3f84bf08 MJ |
3686 | for (i = 0; i < len; i++) |
3687 | { | |
3688 | struct ipa_parm_adjustment *adj; | |
3689 | ||
9771b263 | 3690 | adj = &adjustments[i]; |
3f84bf08 | 3691 | |
31519c38 | 3692 | if (adj->op == IPA_PARM_OP_COPY) |
3f84bf08 MJ |
3693 | { |
3694 | tree arg = gimple_call_arg (stmt, adj->base_index); | |
3695 | ||
9771b263 | 3696 | vargs.quick_push (arg); |
3f84bf08 | 3697 | } |
31519c38 | 3698 | else if (adj->op != IPA_PARM_OP_REMOVE) |
3f84bf08 | 3699 | { |
fffe1e40 MJ |
3700 | tree expr, base, off; |
3701 | location_t loc; | |
f43245d1 | 3702 | unsigned int deref_align = 0; |
c1ed6a01 | 3703 | bool deref_base = false; |
fffe1e40 MJ |
3704 | |
3705 | /* We create a new parameter out of the value of the old one, we can | |
3706 | do the following kind of transformations: | |
3707 | ||
3708 | - A scalar passed by reference is converted to a scalar passed by | |
3709 | value. (adj->by_ref is false and the type of the original | |
3710 | actual argument is a pointer to a scalar). | |
3711 | ||
3712 | - A part of an aggregate is passed instead of the whole aggregate. | |
3713 | The part can be passed either by value or by reference, this is | |
3714 | determined by value of adj->by_ref. Moreover, the code below | |
3715 | handles both situations when the original aggregate is passed by | |
3716 | value (its type is not a pointer) and when it is passed by | |
3717 | reference (it is a pointer to an aggregate). | |
3718 | ||
3719 | When the new argument is passed by reference (adj->by_ref is true) | |
3720 | it must be a part of an aggregate and therefore we form it by | |
3721 | simply taking the address of a reference inside the original | |
3722 | aggregate. */ | |
3723 | ||
3724 | gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0); | |
3725 | base = gimple_call_arg (stmt, adj->base_index); | |
3a50da34 DC |
3726 | loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base) |
3727 | : EXPR_LOCATION (base); | |
fffe1e40 | 3728 | |
82d49829 MJ |
3729 | if (TREE_CODE (base) != ADDR_EXPR |
3730 | && POINTER_TYPE_P (TREE_TYPE (base))) | |
3731 | off = build_int_cst (adj->alias_ptr_type, | |
fffe1e40 | 3732 | adj->offset / BITS_PER_UNIT); |
3f84bf08 | 3733 | else |
3f84bf08 | 3734 | { |
fffe1e40 MJ |
3735 | HOST_WIDE_INT base_offset; |
3736 | tree prev_base; | |
c1ed6a01 | 3737 | bool addrof; |
fffe1e40 MJ |
3738 | |
3739 | if (TREE_CODE (base) == ADDR_EXPR) | |
c1ed6a01 MJ |
3740 | { |
3741 | base = TREE_OPERAND (base, 0); | |
3742 | addrof = true; | |
3743 | } | |
3744 | else | |
3745 | addrof = false; | |
fffe1e40 MJ |
3746 | prev_base = base; |
3747 | base = get_addr_base_and_unit_offset (base, &base_offset); | |
3748 | /* Aggregate arguments can have non-invariant addresses. */ | |
3749 | if (!base) | |
3750 | { | |
3751 | base = build_fold_addr_expr (prev_base); | |
82d49829 | 3752 | off = build_int_cst (adj->alias_ptr_type, |
fffe1e40 MJ |
3753 | adj->offset / BITS_PER_UNIT); |
3754 | } | |
3755 | else if (TREE_CODE (base) == MEM_REF) | |
3756 | { | |
c1ed6a01 MJ |
3757 | if (!addrof) |
3758 | { | |
3759 | deref_base = true; | |
3760 | deref_align = TYPE_ALIGN (TREE_TYPE (base)); | |
3761 | } | |
82d49829 | 3762 | off = build_int_cst (adj->alias_ptr_type, |
fffe1e40 MJ |
3763 | base_offset |
3764 | + adj->offset / BITS_PER_UNIT); | |
3765 | off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1), | |
d35936ab | 3766 | off); |
fffe1e40 MJ |
3767 | base = TREE_OPERAND (base, 0); |
3768 | } | |
3769 | else | |
3770 | { | |
82d49829 | 3771 | off = build_int_cst (adj->alias_ptr_type, |
fffe1e40 MJ |
3772 | base_offset |
3773 | + adj->offset / BITS_PER_UNIT); | |
3774 | base = build_fold_addr_expr (base); | |
3775 | } | |
3f84bf08 | 3776 | } |
fffe1e40 | 3777 | |
3a5a825a RG |
3778 | if (!adj->by_ref) |
3779 | { | |
3780 | tree type = adj->type; | |
3781 | unsigned int align; | |
3782 | unsigned HOST_WIDE_INT misalign; | |
644ffefd | 3783 | |
c1ed6a01 MJ |
3784 | if (deref_base) |
3785 | { | |
3786 | align = deref_align; | |
3787 | misalign = 0; | |
3788 | } | |
3789 | else | |
3790 | { | |
3791 | get_pointer_alignment_1 (base, &align, &misalign); | |
3792 | if (TYPE_ALIGN (type) > align) | |
3793 | align = TYPE_ALIGN (type); | |
3794 | } | |
807e902e | 3795 | misalign += (offset_int::from (off, SIGNED).to_short_addr () |
3a5a825a RG |
3796 | * BITS_PER_UNIT); |
3797 | misalign = misalign & (align - 1); | |
3798 | if (misalign != 0) | |
3799 | align = (misalign & -misalign); | |
3800 | if (align < TYPE_ALIGN (type)) | |
3801 | type = build_aligned_type (type, align); | |
4df65a85 RB |
3802 | base = force_gimple_operand_gsi (&gsi, base, |
3803 | true, NULL, true, GSI_SAME_STMT); | |
3a5a825a | 3804 | expr = fold_build2_loc (loc, MEM_REF, type, base, off); |
4df65a85 RB |
3805 | /* If expr is not a valid gimple call argument emit |
3806 | a load into a temporary. */ | |
3807 | if (is_gimple_reg_type (TREE_TYPE (expr))) | |
3808 | { | |
3809 | gimple tem = gimple_build_assign (NULL_TREE, expr); | |
3810 | if (gimple_in_ssa_p (cfun)) | |
3811 | { | |
3812 | gimple_set_vuse (tem, gimple_vuse (stmt)); | |
3813 | expr = make_ssa_name (TREE_TYPE (expr), tem); | |
3814 | } | |
3815 | else | |
3816 | expr = create_tmp_reg (TREE_TYPE (expr), NULL); | |
3817 | gimple_assign_set_lhs (tem, expr); | |
3818 | gsi_insert_before (&gsi, tem, GSI_SAME_STMT); | |
3819 | } | |
3a5a825a RG |
3820 | } |
3821 | else | |
3822 | { | |
3823 | expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off); | |
3824 | expr = build_fold_addr_expr (expr); | |
4df65a85 RB |
3825 | expr = force_gimple_operand_gsi (&gsi, expr, |
3826 | true, NULL, true, GSI_SAME_STMT); | |
3a5a825a | 3827 | } |
9771b263 | 3828 | vargs.quick_push (expr); |
3f84bf08 | 3829 | } |
31519c38 | 3830 | if (adj->op != IPA_PARM_OP_COPY && MAY_HAVE_DEBUG_STMTS) |
ddb555ed JJ |
3831 | { |
3832 | unsigned int ix; | |
3833 | tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg; | |
3834 | gimple def_temp; | |
3835 | ||
3836 | arg = gimple_call_arg (stmt, adj->base_index); | |
3837 | if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg))) | |
3838 | { | |
3839 | if (!fold_convertible_p (TREE_TYPE (origin), arg)) | |
3840 | continue; | |
3841 | arg = fold_convert_loc (gimple_location (stmt), | |
3842 | TREE_TYPE (origin), arg); | |
3843 | } | |
3844 | if (debug_args == NULL) | |
3845 | debug_args = decl_debug_args_insert (callee_decl); | |
9771b263 | 3846 | for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2) |
ddb555ed JJ |
3847 | if (ddecl == origin) |
3848 | { | |
9771b263 | 3849 | ddecl = (**debug_args)[ix + 1]; |
ddb555ed JJ |
3850 | break; |
3851 | } | |
3852 | if (ddecl == NULL) | |
3853 | { | |
3854 | ddecl = make_node (DEBUG_EXPR_DECL); | |
3855 | DECL_ARTIFICIAL (ddecl) = 1; | |
3856 | TREE_TYPE (ddecl) = TREE_TYPE (origin); | |
3857 | DECL_MODE (ddecl) = DECL_MODE (origin); | |
3858 | ||
9771b263 DN |
3859 | vec_safe_push (*debug_args, origin); |
3860 | vec_safe_push (*debug_args, ddecl); | |
ddb555ed | 3861 | } |
9771b263 | 3862 | def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt); |
ddb555ed JJ |
3863 | gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT); |
3864 | } | |
3f84bf08 MJ |
3865 | } |
3866 | ||
3867 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3868 | { | |
3869 | fprintf (dump_file, "replacing stmt:"); | |
3870 | print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0); | |
3871 | } | |
3872 | ||
3f84bf08 | 3873 | new_stmt = gimple_build_call_vec (callee_decl, vargs); |
9771b263 | 3874 | vargs.release (); |
3f84bf08 MJ |
3875 | if (gimple_call_lhs (stmt)) |
3876 | gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt)); | |
3877 | ||
3878 | gimple_set_block (new_stmt, gimple_block (stmt)); | |
3879 | if (gimple_has_location (stmt)) | |
3880 | gimple_set_location (new_stmt, gimple_location (stmt)); | |
3f84bf08 | 3881 | gimple_call_set_chain (new_stmt, gimple_call_chain (stmt)); |
a7a296ab | 3882 | gimple_call_copy_flags (new_stmt, stmt); |
4df65a85 RB |
3883 | if (gimple_in_ssa_p (cfun)) |
3884 | { | |
3885 | gimple_set_vuse (new_stmt, gimple_vuse (stmt)); | |
3886 | if (gimple_vdef (stmt)) | |
3887 | { | |
3888 | gimple_set_vdef (new_stmt, gimple_vdef (stmt)); | |
3889 | SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt; | |
3890 | } | |
3891 | } | |
3f84bf08 MJ |
3892 | |
3893 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3894 | { | |
3895 | fprintf (dump_file, "with stmt:"); | |
3896 | print_gimple_stmt (dump_file, new_stmt, 0, 0); | |
3897 | fprintf (dump_file, "\n"); | |
3898 | } | |
3899 | gsi_replace (&gsi, new_stmt, true); | |
3900 | if (cs) | |
3901 | cgraph_set_call_stmt (cs, new_stmt); | |
82338059 MJ |
3902 | do |
3903 | { | |
3904 | ipa_record_stmt_references (current_node, gsi_stmt (gsi)); | |
3905 | gsi_prev (&gsi); | |
3906 | } | |
3d354792 | 3907 | while (gsi_stmt (gsi) != gsi_stmt (prev_gsi)); |
3f84bf08 MJ |
3908 | } |
3909 | ||
31519c38 AH |
3910 | /* If the expression *EXPR should be replaced by a reduction of a parameter, do |
3911 | so. ADJUSTMENTS is a pointer to a vector of adjustments. CONVERT | |
3912 | specifies whether the function should care about type incompatibility the | |
3913 | current and new expressions. If it is false, the function will leave | |
3914 | incompatibility issues to the caller. Return true iff the expression | |
3915 | was modified. */ | |
3916 | ||
3917 | bool | |
3918 | ipa_modify_expr (tree *expr, bool convert, | |
3919 | ipa_parm_adjustment_vec adjustments) | |
3920 | { | |
3921 | struct ipa_parm_adjustment *cand | |
3922 | = ipa_get_adjustment_candidate (&expr, &convert, adjustments, false); | |
3923 | if (!cand) | |
3924 | return false; | |
3925 | ||
3926 | tree src; | |
3927 | if (cand->by_ref) | |
3928 | src = build_simple_mem_ref (cand->new_decl); | |
3929 | else | |
3930 | src = cand->new_decl; | |
3931 | ||
3932 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3933 | { | |
3934 | fprintf (dump_file, "About to replace expr "); | |
3935 | print_generic_expr (dump_file, *expr, 0); | |
3936 | fprintf (dump_file, " with "); | |
3937 | print_generic_expr (dump_file, src, 0); | |
3938 | fprintf (dump_file, "\n"); | |
3939 | } | |
3940 | ||
3941 | if (convert && !useless_type_conversion_p (TREE_TYPE (*expr), cand->type)) | |
3942 | { | |
3943 | tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*expr), src); | |
3944 | *expr = vce; | |
3945 | } | |
3946 | else | |
3947 | *expr = src; | |
3948 | return true; | |
3949 | } | |
3950 | ||
3951 | /* If T is an SSA_NAME, return NULL if it is not a default def or | |
3952 | return its base variable if it is. If IGNORE_DEFAULT_DEF is true, | |
3953 | the base variable is always returned, regardless if it is a default | |
3954 | def. Return T if it is not an SSA_NAME. */ | |
3955 | ||
3956 | static tree | |
3957 | get_ssa_base_param (tree t, bool ignore_default_def) | |
3958 | { | |
3959 | if (TREE_CODE (t) == SSA_NAME) | |
3960 | { | |
3961 | if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t)) | |
3962 | return SSA_NAME_VAR (t); | |
3963 | else | |
3964 | return NULL_TREE; | |
3965 | } | |
3966 | return t; | |
3967 | } | |
3968 | ||
3969 | /* Given an expression, return an adjustment entry specifying the | |
3970 | transformation to be done on EXPR. If no suitable adjustment entry | |
3971 | was found, returns NULL. | |
3972 | ||
3973 | If IGNORE_DEFAULT_DEF is set, consider SSA_NAMEs which are not a | |
3974 | default def, otherwise bail on them. | |
3975 | ||
3976 | If CONVERT is non-NULL, this function will set *CONVERT if the | |
3977 | expression provided is a component reference. ADJUSTMENTS is the | |
3978 | adjustments vector. */ | |
3979 | ||
3980 | ipa_parm_adjustment * | |
3981 | ipa_get_adjustment_candidate (tree **expr, bool *convert, | |
3982 | ipa_parm_adjustment_vec adjustments, | |
3983 | bool ignore_default_def) | |
3984 | { | |
3985 | if (TREE_CODE (**expr) == BIT_FIELD_REF | |
3986 | || TREE_CODE (**expr) == IMAGPART_EXPR | |
3987 | || TREE_CODE (**expr) == REALPART_EXPR) | |
3988 | { | |
3989 | *expr = &TREE_OPERAND (**expr, 0); | |
3990 | if (convert) | |
3991 | *convert = true; | |
3992 | } | |
3993 | ||
3994 | HOST_WIDE_INT offset, size, max_size; | |
3995 | tree base = get_ref_base_and_extent (**expr, &offset, &size, &max_size); | |
3996 | if (!base || size == -1 || max_size == -1) | |
3997 | return NULL; | |
3998 | ||
3999 | if (TREE_CODE (base) == MEM_REF) | |
4000 | { | |
807e902e | 4001 | offset += mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT; |
31519c38 AH |
4002 | base = TREE_OPERAND (base, 0); |
4003 | } | |
4004 | ||
4005 | base = get_ssa_base_param (base, ignore_default_def); | |
4006 | if (!base || TREE_CODE (base) != PARM_DECL) | |
4007 | return NULL; | |
4008 | ||
4009 | struct ipa_parm_adjustment *cand = NULL; | |
4010 | unsigned int len = adjustments.length (); | |
4011 | for (unsigned i = 0; i < len; i++) | |
4012 | { | |
4013 | struct ipa_parm_adjustment *adj = &adjustments[i]; | |
4014 | ||
4015 | if (adj->base == base | |
4016 | && (adj->offset == offset || adj->op == IPA_PARM_OP_REMOVE)) | |
4017 | { | |
4018 | cand = adj; | |
4019 | break; | |
4020 | } | |
4021 | } | |
4022 | ||
4023 | if (!cand || cand->op == IPA_PARM_OP_COPY || cand->op == IPA_PARM_OP_REMOVE) | |
4024 | return NULL; | |
4025 | return cand; | |
4026 | } | |
4027 | ||
3f84bf08 MJ |
4028 | /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */ |
4029 | ||
4030 | static bool | |
4031 | index_in_adjustments_multiple_times_p (int base_index, | |
4032 | ipa_parm_adjustment_vec adjustments) | |
4033 | { | |
9771b263 | 4034 | int i, len = adjustments.length (); |
3f84bf08 MJ |
4035 | bool one = false; |
4036 | ||
4037 | for (i = 0; i < len; i++) | |
4038 | { | |
4039 | struct ipa_parm_adjustment *adj; | |
9771b263 | 4040 | adj = &adjustments[i]; |
3f84bf08 MJ |
4041 | |
4042 | if (adj->base_index == base_index) | |
4043 | { | |
4044 | if (one) | |
4045 | return true; | |
4046 | else | |
4047 | one = true; | |
4048 | } | |
4049 | } | |
4050 | return false; | |
4051 | } | |
4052 | ||
4053 | ||
4054 | /* Return adjustments that should have the same effect on function parameters | |
4055 | and call arguments as if they were first changed according to adjustments in | |
4056 | INNER and then by adjustments in OUTER. */ | |
4057 | ||
4058 | ipa_parm_adjustment_vec | |
4059 | ipa_combine_adjustments (ipa_parm_adjustment_vec inner, | |
4060 | ipa_parm_adjustment_vec outer) | |
4061 | { | |
9771b263 DN |
4062 | int i, outlen = outer.length (); |
4063 | int inlen = inner.length (); | |
3f84bf08 MJ |
4064 | int removals = 0; |
4065 | ipa_parm_adjustment_vec adjustments, tmp; | |
4066 | ||
9771b263 | 4067 | tmp.create (inlen); |
3f84bf08 MJ |
4068 | for (i = 0; i < inlen; i++) |
4069 | { | |
4070 | struct ipa_parm_adjustment *n; | |
9771b263 | 4071 | n = &inner[i]; |
3f84bf08 | 4072 | |
31519c38 | 4073 | if (n->op == IPA_PARM_OP_REMOVE) |
3f84bf08 MJ |
4074 | removals++; |
4075 | else | |
31519c38 AH |
4076 | { |
4077 | /* FIXME: Handling of new arguments are not implemented yet. */ | |
4078 | gcc_assert (n->op != IPA_PARM_OP_NEW); | |
4079 | tmp.quick_push (*n); | |
4080 | } | |
3f84bf08 MJ |
4081 | } |
4082 | ||
9771b263 | 4083 | adjustments.create (outlen + removals); |
3f84bf08 MJ |
4084 | for (i = 0; i < outlen; i++) |
4085 | { | |
f32682ca | 4086 | struct ipa_parm_adjustment r; |
9771b263 DN |
4087 | struct ipa_parm_adjustment *out = &outer[i]; |
4088 | struct ipa_parm_adjustment *in = &tmp[out->base_index]; | |
3f84bf08 | 4089 | |
f32682ca | 4090 | memset (&r, 0, sizeof (r)); |
31519c38 AH |
4091 | gcc_assert (in->op != IPA_PARM_OP_REMOVE); |
4092 | if (out->op == IPA_PARM_OP_REMOVE) | |
3f84bf08 MJ |
4093 | { |
4094 | if (!index_in_adjustments_multiple_times_p (in->base_index, tmp)) | |
4095 | { | |
31519c38 | 4096 | r.op = IPA_PARM_OP_REMOVE; |
9771b263 | 4097 | adjustments.quick_push (r); |
3f84bf08 MJ |
4098 | } |
4099 | continue; | |
4100 | } | |
31519c38 AH |
4101 | else |
4102 | { | |
4103 | /* FIXME: Handling of new arguments are not implemented yet. */ | |
4104 | gcc_assert (out->op != IPA_PARM_OP_NEW); | |
4105 | } | |
3f84bf08 | 4106 | |
f32682ca DN |
4107 | r.base_index = in->base_index; |
4108 | r.type = out->type; | |
3f84bf08 MJ |
4109 | |
4110 | /* FIXME: Create nonlocal value too. */ | |
4111 | ||
31519c38 AH |
4112 | if (in->op == IPA_PARM_OP_COPY && out->op == IPA_PARM_OP_COPY) |
4113 | r.op = IPA_PARM_OP_COPY; | |
4114 | else if (in->op == IPA_PARM_OP_COPY) | |
f32682ca | 4115 | r.offset = out->offset; |
31519c38 | 4116 | else if (out->op == IPA_PARM_OP_COPY) |
f32682ca | 4117 | r.offset = in->offset; |
3f84bf08 | 4118 | else |
f32682ca | 4119 | r.offset = in->offset + out->offset; |
9771b263 | 4120 | adjustments.quick_push (r); |
3f84bf08 MJ |
4121 | } |
4122 | ||
4123 | for (i = 0; i < inlen; i++) | |
4124 | { | |
9771b263 | 4125 | struct ipa_parm_adjustment *n = &inner[i]; |
3f84bf08 | 4126 | |
31519c38 | 4127 | if (n->op == IPA_PARM_OP_REMOVE) |
9771b263 | 4128 | adjustments.quick_push (*n); |
3f84bf08 MJ |
4129 | } |
4130 | ||
9771b263 | 4131 | tmp.release (); |
3f84bf08 MJ |
4132 | return adjustments; |
4133 | } | |
4134 | ||
4135 | /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human | |
4136 | friendly way, assuming they are meant to be applied to FNDECL. */ | |
4137 | ||
4138 | void | |
4139 | ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments, | |
4140 | tree fndecl) | |
4141 | { | |
9771b263 | 4142 | int i, len = adjustments.length (); |
3f84bf08 | 4143 | bool first = true; |
9771b263 | 4144 | vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl); |
3f84bf08 MJ |
4145 | |
4146 | fprintf (file, "IPA param adjustments: "); | |
4147 | for (i = 0; i < len; i++) | |
4148 | { | |
4149 | struct ipa_parm_adjustment *adj; | |
9771b263 | 4150 | adj = &adjustments[i]; |
3f84bf08 MJ |
4151 | |
4152 | if (!first) | |
4153 | fprintf (file, " "); | |
4154 | else | |
4155 | first = false; | |
4156 | ||
4157 | fprintf (file, "%i. base_index: %i - ", i, adj->base_index); | |
9771b263 | 4158 | print_generic_expr (file, parms[adj->base_index], 0); |
3f84bf08 MJ |
4159 | if (adj->base) |
4160 | { | |
4161 | fprintf (file, ", base: "); | |
4162 | print_generic_expr (file, adj->base, 0); | |
4163 | } | |
31519c38 | 4164 | if (adj->new_decl) |
3f84bf08 | 4165 | { |
31519c38 AH |
4166 | fprintf (file, ", new_decl: "); |
4167 | print_generic_expr (file, adj->new_decl, 0); | |
3f84bf08 MJ |
4168 | } |
4169 | if (adj->new_ssa_base) | |
4170 | { | |
4171 | fprintf (file, ", new_ssa_base: "); | |
4172 | print_generic_expr (file, adj->new_ssa_base, 0); | |
4173 | } | |
4174 | ||
31519c38 | 4175 | if (adj->op == IPA_PARM_OP_COPY) |
3f84bf08 | 4176 | fprintf (file, ", copy_param"); |
31519c38 | 4177 | else if (adj->op == IPA_PARM_OP_REMOVE) |
3f84bf08 MJ |
4178 | fprintf (file, ", remove_param"); |
4179 | else | |
4180 | fprintf (file, ", offset %li", (long) adj->offset); | |
4181 | if (adj->by_ref) | |
4182 | fprintf (file, ", by_ref"); | |
4183 | print_node_brief (file, ", type: ", adj->type, 0); | |
4184 | fprintf (file, "\n"); | |
4185 | } | |
9771b263 | 4186 | parms.release (); |
3f84bf08 MJ |
4187 | } |
4188 | ||
2c9561b5 MJ |
4189 | /* Dump the AV linked list. */ |
4190 | ||
4191 | void | |
4192 | ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av) | |
4193 | { | |
4194 | bool comma = false; | |
4195 | fprintf (f, " Aggregate replacements:"); | |
4196 | for (; av; av = av->next) | |
4197 | { | |
4198 | fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "", | |
4199 | av->index, av->offset); | |
4200 | print_generic_expr (f, av->value, 0); | |
4201 | comma = true; | |
4202 | } | |
4203 | fprintf (f, "\n"); | |
4204 | } | |
4205 | ||
fb3f88cc JH |
4206 | /* Stream out jump function JUMP_FUNC to OB. */ |
4207 | ||
4208 | static void | |
4209 | ipa_write_jump_function (struct output_block *ob, | |
4210 | struct ipa_jump_func *jump_func) | |
4211 | { | |
8b7773a4 MJ |
4212 | struct ipa_agg_jf_item *item; |
4213 | struct bitpack_d bp; | |
4214 | int i, count; | |
fb3f88cc | 4215 | |
8b7773a4 | 4216 | streamer_write_uhwi (ob, jump_func->type); |
fb3f88cc JH |
4217 | switch (jump_func->type) |
4218 | { | |
4219 | case IPA_JF_UNKNOWN: | |
4220 | break; | |
b258210c | 4221 | case IPA_JF_KNOWN_TYPE: |
c7573249 MJ |
4222 | streamer_write_uhwi (ob, jump_func->value.known_type.offset); |
4223 | stream_write_tree (ob, jump_func->value.known_type.base_type, true); | |
4224 | stream_write_tree (ob, jump_func->value.known_type.component_type, true); | |
b258210c | 4225 | break; |
fb3f88cc | 4226 | case IPA_JF_CONST: |
5368224f | 4227 | gcc_assert ( |
4502fe8d MJ |
4228 | EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION); |
4229 | stream_write_tree (ob, jump_func->value.constant.value, true); | |
fb3f88cc JH |
4230 | break; |
4231 | case IPA_JF_PASS_THROUGH: | |
412288f1 | 4232 | streamer_write_uhwi (ob, jump_func->value.pass_through.operation); |
4a53743e MJ |
4233 | if (jump_func->value.pass_through.operation == NOP_EXPR) |
4234 | { | |
4235 | streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id); | |
4236 | bp = bitpack_create (ob->main_stream); | |
4237 | bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1); | |
b8f6e610 | 4238 | bp_pack_value (&bp, jump_func->value.pass_through.type_preserved, 1); |
4a53743e MJ |
4239 | streamer_write_bitpack (&bp); |
4240 | } | |
4241 | else | |
4242 | { | |
4243 | stream_write_tree (ob, jump_func->value.pass_through.operand, true); | |
4244 | streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id); | |
4245 | } | |
fb3f88cc JH |
4246 | break; |
4247 | case IPA_JF_ANCESTOR: | |
412288f1 | 4248 | streamer_write_uhwi (ob, jump_func->value.ancestor.offset); |
b9393656 | 4249 | stream_write_tree (ob, jump_func->value.ancestor.type, true); |
412288f1 | 4250 | streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id); |
8b7773a4 MJ |
4251 | bp = bitpack_create (ob->main_stream); |
4252 | bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1); | |
b8f6e610 | 4253 | bp_pack_value (&bp, jump_func->value.ancestor.type_preserved, 1); |
8b7773a4 | 4254 | streamer_write_bitpack (&bp); |
fb3f88cc | 4255 | break; |
8b7773a4 MJ |
4256 | } |
4257 | ||
9771b263 | 4258 | count = vec_safe_length (jump_func->agg.items); |
8b7773a4 MJ |
4259 | streamer_write_uhwi (ob, count); |
4260 | if (count) | |
4261 | { | |
4262 | bp = bitpack_create (ob->main_stream); | |
4263 | bp_pack_value (&bp, jump_func->agg.by_ref, 1); | |
4264 | streamer_write_bitpack (&bp); | |
4265 | } | |
4266 | ||
9771b263 | 4267 | FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item) |
8b7773a4 MJ |
4268 | { |
4269 | streamer_write_uhwi (ob, item->offset); | |
4270 | stream_write_tree (ob, item->value, true); | |
fb3f88cc JH |
4271 | } |
4272 | } | |
4273 | ||
4274 | /* Read in jump function JUMP_FUNC from IB. */ | |
4275 | ||
4276 | static void | |
4277 | ipa_read_jump_function (struct lto_input_block *ib, | |
4278 | struct ipa_jump_func *jump_func, | |
4502fe8d | 4279 | struct cgraph_edge *cs, |
fb3f88cc JH |
4280 | struct data_in *data_in) |
4281 | { | |
4a53743e MJ |
4282 | enum jump_func_type jftype; |
4283 | enum tree_code operation; | |
8b7773a4 | 4284 | int i, count; |
fb3f88cc | 4285 | |
4a53743e MJ |
4286 | jftype = (enum jump_func_type) streamer_read_uhwi (ib); |
4287 | switch (jftype) | |
fb3f88cc JH |
4288 | { |
4289 | case IPA_JF_UNKNOWN: | |
4a53743e | 4290 | jump_func->type = IPA_JF_UNKNOWN; |
fb3f88cc | 4291 | break; |
b258210c | 4292 | case IPA_JF_KNOWN_TYPE: |
4a53743e MJ |
4293 | { |
4294 | HOST_WIDE_INT offset = streamer_read_uhwi (ib); | |
4295 | tree base_type = stream_read_tree (ib, data_in); | |
4296 | tree component_type = stream_read_tree (ib, data_in); | |
4297 | ||
4298 | ipa_set_jf_known_type (jump_func, offset, base_type, component_type); | |
4299 | break; | |
4300 | } | |
fb3f88cc | 4301 | case IPA_JF_CONST: |
4502fe8d | 4302 | ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs); |
fb3f88cc JH |
4303 | break; |
4304 | case IPA_JF_PASS_THROUGH: | |
4a53743e MJ |
4305 | operation = (enum tree_code) streamer_read_uhwi (ib); |
4306 | if (operation == NOP_EXPR) | |
4307 | { | |
4308 | int formal_id = streamer_read_uhwi (ib); | |
4309 | struct bitpack_d bp = streamer_read_bitpack (ib); | |
4310 | bool agg_preserved = bp_unpack_value (&bp, 1); | |
b8f6e610 MJ |
4311 | bool type_preserved = bp_unpack_value (&bp, 1); |
4312 | ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved, | |
4313 | type_preserved); | |
4a53743e MJ |
4314 | } |
4315 | else | |
4316 | { | |
4317 | tree operand = stream_read_tree (ib, data_in); | |
4318 | int formal_id = streamer_read_uhwi (ib); | |
4319 | ipa_set_jf_arith_pass_through (jump_func, formal_id, operand, | |
4320 | operation); | |
4321 | } | |
fb3f88cc JH |
4322 | break; |
4323 | case IPA_JF_ANCESTOR: | |
4a53743e MJ |
4324 | { |
4325 | HOST_WIDE_INT offset = streamer_read_uhwi (ib); | |
4326 | tree type = stream_read_tree (ib, data_in); | |
4327 | int formal_id = streamer_read_uhwi (ib); | |
4328 | struct bitpack_d bp = streamer_read_bitpack (ib); | |
4329 | bool agg_preserved = bp_unpack_value (&bp, 1); | |
b8f6e610 | 4330 | bool type_preserved = bp_unpack_value (&bp, 1); |
4a53743e | 4331 | |
b8f6e610 MJ |
4332 | ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved, |
4333 | type_preserved); | |
4a53743e MJ |
4334 | break; |
4335 | } | |
8b7773a4 MJ |
4336 | } |
4337 | ||
4338 | count = streamer_read_uhwi (ib); | |
9771b263 | 4339 | vec_alloc (jump_func->agg.items, count); |
8b7773a4 MJ |
4340 | if (count) |
4341 | { | |
4a53743e | 4342 | struct bitpack_d bp = streamer_read_bitpack (ib); |
8b7773a4 MJ |
4343 | jump_func->agg.by_ref = bp_unpack_value (&bp, 1); |
4344 | } | |
4345 | for (i = 0; i < count; i++) | |
4346 | { | |
f32682ca DN |
4347 | struct ipa_agg_jf_item item; |
4348 | item.offset = streamer_read_uhwi (ib); | |
4349 | item.value = stream_read_tree (ib, data_in); | |
9771b263 | 4350 | jump_func->agg.items->quick_push (item); |
fb3f88cc JH |
4351 | } |
4352 | } | |
4353 | ||
e33c6cd6 MJ |
4354 | /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are |
4355 | relevant to indirect inlining to OB. */ | |
661e7330 MJ |
4356 | |
4357 | static void | |
e33c6cd6 MJ |
4358 | ipa_write_indirect_edge_info (struct output_block *ob, |
4359 | struct cgraph_edge *cs) | |
661e7330 | 4360 | { |
e33c6cd6 | 4361 | struct cgraph_indirect_call_info *ii = cs->indirect_info; |
2465dcc2 | 4362 | struct bitpack_d bp; |
e33c6cd6 | 4363 | |
412288f1 | 4364 | streamer_write_hwi (ob, ii->param_index); |
8b7773a4 | 4365 | streamer_write_hwi (ob, ii->offset); |
2465dcc2 RG |
4366 | bp = bitpack_create (ob->main_stream); |
4367 | bp_pack_value (&bp, ii->polymorphic, 1); | |
8b7773a4 | 4368 | bp_pack_value (&bp, ii->agg_contents, 1); |
c13bc3d9 | 4369 | bp_pack_value (&bp, ii->member_ptr, 1); |
8b7773a4 | 4370 | bp_pack_value (&bp, ii->by_ref, 1); |
68377e53 JH |
4371 | bp_pack_value (&bp, ii->maybe_in_construction, 1); |
4372 | bp_pack_value (&bp, ii->maybe_derived_type, 1); | |
412288f1 | 4373 | streamer_write_bitpack (&bp); |
b258210c MJ |
4374 | |
4375 | if (ii->polymorphic) | |
4376 | { | |
412288f1 | 4377 | streamer_write_hwi (ob, ii->otr_token); |
b9393656 | 4378 | stream_write_tree (ob, ii->otr_type, true); |
68377e53 | 4379 | stream_write_tree (ob, ii->outer_type, true); |
b258210c | 4380 | } |
661e7330 MJ |
4381 | } |
4382 | ||
e33c6cd6 MJ |
4383 | /* Read in parts of cgraph_indirect_call_info corresponding to CS that are |
4384 | relevant to indirect inlining from IB. */ | |
661e7330 MJ |
4385 | |
4386 | static void | |
e33c6cd6 MJ |
4387 | ipa_read_indirect_edge_info (struct lto_input_block *ib, |
4388 | struct data_in *data_in ATTRIBUTE_UNUSED, | |
4389 | struct cgraph_edge *cs) | |
661e7330 | 4390 | { |
e33c6cd6 | 4391 | struct cgraph_indirect_call_info *ii = cs->indirect_info; |
2465dcc2 | 4392 | struct bitpack_d bp; |
661e7330 | 4393 | |
412288f1 | 4394 | ii->param_index = (int) streamer_read_hwi (ib); |
8b7773a4 | 4395 | ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib); |
412288f1 | 4396 | bp = streamer_read_bitpack (ib); |
2465dcc2 | 4397 | ii->polymorphic = bp_unpack_value (&bp, 1); |
8b7773a4 | 4398 | ii->agg_contents = bp_unpack_value (&bp, 1); |
c13bc3d9 | 4399 | ii->member_ptr = bp_unpack_value (&bp, 1); |
8b7773a4 | 4400 | ii->by_ref = bp_unpack_value (&bp, 1); |
68377e53 JH |
4401 | ii->maybe_in_construction = bp_unpack_value (&bp, 1); |
4402 | ii->maybe_derived_type = bp_unpack_value (&bp, 1); | |
b258210c MJ |
4403 | if (ii->polymorphic) |
4404 | { | |
412288f1 | 4405 | ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib); |
b9393656 | 4406 | ii->otr_type = stream_read_tree (ib, data_in); |
68377e53 | 4407 | ii->outer_type = stream_read_tree (ib, data_in); |
b258210c | 4408 | } |
661e7330 MJ |
4409 | } |
4410 | ||
fb3f88cc JH |
4411 | /* Stream out NODE info to OB. */ |
4412 | ||
4413 | static void | |
4414 | ipa_write_node_info (struct output_block *ob, struct cgraph_node *node) | |
4415 | { | |
4416 | int node_ref; | |
7380e6ef | 4417 | lto_symtab_encoder_t encoder; |
fb3f88cc JH |
4418 | struct ipa_node_params *info = IPA_NODE_REF (node); |
4419 | int j; | |
4420 | struct cgraph_edge *e; | |
2465dcc2 | 4421 | struct bitpack_d bp; |
fb3f88cc | 4422 | |
7380e6ef | 4423 | encoder = ob->decl_state->symtab_node_encoder; |
67348ccc | 4424 | node_ref = lto_symtab_encoder_encode (encoder, node); |
412288f1 | 4425 | streamer_write_uhwi (ob, node_ref); |
fb3f88cc | 4426 | |
0e8853ee JH |
4427 | streamer_write_uhwi (ob, ipa_get_param_count (info)); |
4428 | for (j = 0; j < ipa_get_param_count (info); j++) | |
4429 | streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j)); | |
2465dcc2 | 4430 | bp = bitpack_create (ob->main_stream); |
062c604f | 4431 | gcc_assert (info->uses_analysis_done |
661e7330 | 4432 | || ipa_get_param_count (info) == 0); |
fb3f88cc JH |
4433 | gcc_assert (!info->node_enqueued); |
4434 | gcc_assert (!info->ipcp_orig_node); | |
4435 | for (j = 0; j < ipa_get_param_count (info); j++) | |
310bc633 | 4436 | bp_pack_value (&bp, ipa_is_param_used (info, j), 1); |
412288f1 | 4437 | streamer_write_bitpack (&bp); |
4502fe8d MJ |
4438 | for (j = 0; j < ipa_get_param_count (info); j++) |
4439 | streamer_write_hwi (ob, ipa_get_controlled_uses (info, j)); | |
fb3f88cc JH |
4440 | for (e = node->callees; e; e = e->next_callee) |
4441 | { | |
4442 | struct ipa_edge_args *args = IPA_EDGE_REF (e); | |
4443 | ||
412288f1 | 4444 | streamer_write_uhwi (ob, ipa_get_cs_argument_count (args)); |
fb3f88cc JH |
4445 | for (j = 0; j < ipa_get_cs_argument_count (args); j++) |
4446 | ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j)); | |
4447 | } | |
e33c6cd6 | 4448 | for (e = node->indirect_calls; e; e = e->next_callee) |
c8246dbe JH |
4449 | { |
4450 | struct ipa_edge_args *args = IPA_EDGE_REF (e); | |
4451 | ||
412288f1 | 4452 | streamer_write_uhwi (ob, ipa_get_cs_argument_count (args)); |
c8246dbe JH |
4453 | for (j = 0; j < ipa_get_cs_argument_count (args); j++) |
4454 | ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j)); | |
4455 | ipa_write_indirect_edge_info (ob, e); | |
4456 | } | |
fb3f88cc JH |
4457 | } |
4458 | ||
61502ca8 | 4459 | /* Stream in NODE info from IB. */ |
fb3f88cc JH |
4460 | |
4461 | static void | |
4462 | ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node, | |
4463 | struct data_in *data_in) | |
4464 | { | |
4465 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
4466 | int k; | |
4467 | struct cgraph_edge *e; | |
2465dcc2 | 4468 | struct bitpack_d bp; |
fb3f88cc | 4469 | |
0e8853ee | 4470 | ipa_alloc_node_params (node, streamer_read_uhwi (ib)); |
fb3f88cc | 4471 | |
0e8853ee JH |
4472 | for (k = 0; k < ipa_get_param_count (info); k++) |
4473 | info->descriptors[k].move_cost = streamer_read_uhwi (ib); | |
4474 | ||
412288f1 | 4475 | bp = streamer_read_bitpack (ib); |
fb3f88cc | 4476 | if (ipa_get_param_count (info) != 0) |
062c604f | 4477 | info->uses_analysis_done = true; |
fb3f88cc JH |
4478 | info->node_enqueued = false; |
4479 | for (k = 0; k < ipa_get_param_count (info); k++) | |
310bc633 | 4480 | ipa_set_param_used (info, k, bp_unpack_value (&bp, 1)); |
1b14621a MJ |
4481 | for (k = 0; k < ipa_get_param_count (info); k++) |
4482 | ipa_set_controlled_uses (info, k, streamer_read_hwi (ib)); | |
fb3f88cc JH |
4483 | for (e = node->callees; e; e = e->next_callee) |
4484 | { | |
4485 | struct ipa_edge_args *args = IPA_EDGE_REF (e); | |
412288f1 | 4486 | int count = streamer_read_uhwi (ib); |
fb3f88cc | 4487 | |
fb3f88cc JH |
4488 | if (!count) |
4489 | continue; | |
9771b263 | 4490 | vec_safe_grow_cleared (args->jump_functions, count); |
fb3f88cc | 4491 | |
fb3f88cc | 4492 | for (k = 0; k < ipa_get_cs_argument_count (args); k++) |
4502fe8d MJ |
4493 | ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e, |
4494 | data_in); | |
fb3f88cc | 4495 | } |
e33c6cd6 | 4496 | for (e = node->indirect_calls; e; e = e->next_callee) |
c8246dbe JH |
4497 | { |
4498 | struct ipa_edge_args *args = IPA_EDGE_REF (e); | |
412288f1 | 4499 | int count = streamer_read_uhwi (ib); |
c8246dbe | 4500 | |
c8246dbe JH |
4501 | if (count) |
4502 | { | |
9771b263 | 4503 | vec_safe_grow_cleared (args->jump_functions, count); |
c8246dbe | 4504 | for (k = 0; k < ipa_get_cs_argument_count (args); k++) |
4502fe8d | 4505 | ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e, |
606d9a09 | 4506 | data_in); |
c8246dbe JH |
4507 | } |
4508 | ipa_read_indirect_edge_info (ib, data_in, e); | |
4509 | } | |
fb3f88cc JH |
4510 | } |
4511 | ||
4512 | /* Write jump functions for nodes in SET. */ | |
4513 | ||
4514 | void | |
f27c1867 | 4515 | ipa_prop_write_jump_functions (void) |
fb3f88cc JH |
4516 | { |
4517 | struct cgraph_node *node; | |
93536c97 | 4518 | struct output_block *ob; |
fb3f88cc | 4519 | unsigned int count = 0; |
f27c1867 JH |
4520 | lto_symtab_encoder_iterator lsei; |
4521 | lto_symtab_encoder_t encoder; | |
4522 | ||
fb3f88cc | 4523 | |
9771b263 | 4524 | if (!ipa_node_params_vector.exists ()) |
93536c97 | 4525 | return; |
fb3f88cc | 4526 | |
93536c97 | 4527 | ob = create_output_block (LTO_section_jump_functions); |
f27c1867 | 4528 | encoder = ob->decl_state->symtab_node_encoder; |
93536c97 | 4529 | ob->cgraph_node = NULL; |
f27c1867 JH |
4530 | for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
4531 | lsei_next_function_in_partition (&lsei)) | |
fb3f88cc | 4532 | { |
f27c1867 | 4533 | node = lsei_cgraph_node (lsei); |
c47d0034 JH |
4534 | if (cgraph_function_with_gimple_body_p (node) |
4535 | && IPA_NODE_REF (node) != NULL) | |
fb3f88cc JH |
4536 | count++; |
4537 | } | |
4538 | ||
412288f1 | 4539 | streamer_write_uhwi (ob, count); |
fb3f88cc JH |
4540 | |
4541 | /* Process all of the functions. */ | |
f27c1867 JH |
4542 | for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
4543 | lsei_next_function_in_partition (&lsei)) | |
fb3f88cc | 4544 | { |
f27c1867 | 4545 | node = lsei_cgraph_node (lsei); |
c47d0034 JH |
4546 | if (cgraph_function_with_gimple_body_p (node) |
4547 | && IPA_NODE_REF (node) != NULL) | |
fb3f88cc JH |
4548 | ipa_write_node_info (ob, node); |
4549 | } | |
412288f1 | 4550 | streamer_write_char_stream (ob->main_stream, 0); |
fb3f88cc JH |
4551 | produce_asm (ob, NULL); |
4552 | destroy_output_block (ob); | |
4553 | } | |
4554 | ||
4555 | /* Read section in file FILE_DATA of length LEN with data DATA. */ | |
4556 | ||
4557 | static void | |
4558 | ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data, | |
4559 | size_t len) | |
4560 | { | |
4561 | const struct lto_function_header *header = | |
4562 | (const struct lto_function_header *) data; | |
4ad9a9de EB |
4563 | const int cfg_offset = sizeof (struct lto_function_header); |
4564 | const int main_offset = cfg_offset + header->cfg_size; | |
4565 | const int string_offset = main_offset + header->main_size; | |
fb3f88cc JH |
4566 | struct data_in *data_in; |
4567 | struct lto_input_block ib_main; | |
4568 | unsigned int i; | |
4569 | unsigned int count; | |
4570 | ||
4571 | LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0, | |
4572 | header->main_size); | |
4573 | ||
4574 | data_in = | |
4575 | lto_data_in_create (file_data, (const char *) data + string_offset, | |
6e1aa848 | 4576 | header->string_size, vNULL); |
412288f1 | 4577 | count = streamer_read_uhwi (&ib_main); |
fb3f88cc JH |
4578 | |
4579 | for (i = 0; i < count; i++) | |
4580 | { | |
4581 | unsigned int index; | |
4582 | struct cgraph_node *node; | |
7380e6ef | 4583 | lto_symtab_encoder_t encoder; |
fb3f88cc | 4584 | |
412288f1 | 4585 | index = streamer_read_uhwi (&ib_main); |
7380e6ef JH |
4586 | encoder = file_data->symtab_node_encoder; |
4587 | node = cgraph (lto_symtab_encoder_deref (encoder, index)); | |
67348ccc | 4588 | gcc_assert (node->definition); |
fb3f88cc JH |
4589 | ipa_read_node_info (&ib_main, node, data_in); |
4590 | } | |
4591 | lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data, | |
4592 | len); | |
4593 | lto_data_in_delete (data_in); | |
4594 | } | |
4595 | ||
4596 | /* Read ipcp jump functions. */ | |
4597 | ||
4598 | void | |
4599 | ipa_prop_read_jump_functions (void) | |
4600 | { | |
4601 | struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); | |
4602 | struct lto_file_decl_data *file_data; | |
4603 | unsigned int j = 0; | |
4604 | ||
4605 | ipa_check_create_node_params (); | |
4606 | ipa_check_create_edge_args (); | |
4607 | ipa_register_cgraph_hooks (); | |
4608 | ||
4609 | while ((file_data = file_data_vec[j++])) | |
4610 | { | |
4611 | size_t len; | |
4612 | const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len); | |
4613 | ||
4614 | if (data) | |
4615 | ipa_prop_read_section (file_data, data, len); | |
4616 | } | |
4617 | } | |
4618 | ||
b8698a0f | 4619 | /* After merging units, we can get mismatch in argument counts. |
61502ca8 | 4620 | Also decl merging might've rendered parameter lists obsolete. |
fb3f88cc JH |
4621 | Also compute called_with_variable_arg info. */ |
4622 | ||
4623 | void | |
4624 | ipa_update_after_lto_read (void) | |
4625 | { | |
05d3aa37 MJ |
4626 | ipa_check_create_node_params (); |
4627 | ipa_check_create_edge_args (); | |
fb3f88cc | 4628 | } |
2c9561b5 MJ |
4629 | |
4630 | void | |
4631 | write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node) | |
4632 | { | |
4633 | int node_ref; | |
4634 | unsigned int count = 0; | |
4635 | lto_symtab_encoder_t encoder; | |
4636 | struct ipa_agg_replacement_value *aggvals, *av; | |
4637 | ||
4638 | aggvals = ipa_get_agg_replacements_for_node (node); | |
4639 | encoder = ob->decl_state->symtab_node_encoder; | |
67348ccc | 4640 | node_ref = lto_symtab_encoder_encode (encoder, node); |
2c9561b5 MJ |
4641 | streamer_write_uhwi (ob, node_ref); |
4642 | ||
4643 | for (av = aggvals; av; av = av->next) | |
4644 | count++; | |
4645 | streamer_write_uhwi (ob, count); | |
4646 | ||
4647 | for (av = aggvals; av; av = av->next) | |
4648 | { | |
7b920a9a MJ |
4649 | struct bitpack_d bp; |
4650 | ||
2c9561b5 MJ |
4651 | streamer_write_uhwi (ob, av->offset); |
4652 | streamer_write_uhwi (ob, av->index); | |
4653 | stream_write_tree (ob, av->value, true); | |
7b920a9a MJ |
4654 | |
4655 | bp = bitpack_create (ob->main_stream); | |
4656 | bp_pack_value (&bp, av->by_ref, 1); | |
4657 | streamer_write_bitpack (&bp); | |
2c9561b5 MJ |
4658 | } |
4659 | } | |
4660 | ||
4661 | /* Stream in the aggregate value replacement chain for NODE from IB. */ | |
4662 | ||
4663 | static void | |
4664 | read_agg_replacement_chain (struct lto_input_block *ib, | |
4665 | struct cgraph_node *node, | |
4666 | struct data_in *data_in) | |
4667 | { | |
4668 | struct ipa_agg_replacement_value *aggvals = NULL; | |
4669 | unsigned int count, i; | |
4670 | ||
4671 | count = streamer_read_uhwi (ib); | |
4672 | for (i = 0; i <count; i++) | |
4673 | { | |
4674 | struct ipa_agg_replacement_value *av; | |
7b920a9a | 4675 | struct bitpack_d bp; |
2c9561b5 | 4676 | |
766090c2 | 4677 | av = ggc_alloc<ipa_agg_replacement_value> (); |
2c9561b5 MJ |
4678 | av->offset = streamer_read_uhwi (ib); |
4679 | av->index = streamer_read_uhwi (ib); | |
4680 | av->value = stream_read_tree (ib, data_in); | |
7b920a9a MJ |
4681 | bp = streamer_read_bitpack (ib); |
4682 | av->by_ref = bp_unpack_value (&bp, 1); | |
2c9561b5 MJ |
4683 | av->next = aggvals; |
4684 | aggvals = av; | |
4685 | } | |
4686 | ipa_set_node_agg_value_chain (node, aggvals); | |
4687 | } | |
4688 | ||
4689 | /* Write all aggregate replacement for nodes in set. */ | |
4690 | ||
4691 | void | |
4692 | ipa_prop_write_all_agg_replacement (void) | |
4693 | { | |
4694 | struct cgraph_node *node; | |
4695 | struct output_block *ob; | |
4696 | unsigned int count = 0; | |
4697 | lto_symtab_encoder_iterator lsei; | |
4698 | lto_symtab_encoder_t encoder; | |
4699 | ||
4700 | if (!ipa_node_agg_replacements) | |
4701 | return; | |
4702 | ||
4703 | ob = create_output_block (LTO_section_ipcp_transform); | |
4704 | encoder = ob->decl_state->symtab_node_encoder; | |
4705 | ob->cgraph_node = NULL; | |
4706 | for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); | |
4707 | lsei_next_function_in_partition (&lsei)) | |
4708 | { | |
4709 | node = lsei_cgraph_node (lsei); | |
4710 | if (cgraph_function_with_gimple_body_p (node) | |
4711 | && ipa_get_agg_replacements_for_node (node) != NULL) | |
4712 | count++; | |
4713 | } | |
4714 | ||
4715 | streamer_write_uhwi (ob, count); | |
4716 | ||
4717 | for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); | |
4718 | lsei_next_function_in_partition (&lsei)) | |
4719 | { | |
4720 | node = lsei_cgraph_node (lsei); | |
4721 | if (cgraph_function_with_gimple_body_p (node) | |
4722 | && ipa_get_agg_replacements_for_node (node) != NULL) | |
4723 | write_agg_replacement_chain (ob, node); | |
4724 | } | |
4725 | streamer_write_char_stream (ob->main_stream, 0); | |
4726 | produce_asm (ob, NULL); | |
4727 | destroy_output_block (ob); | |
4728 | } | |
4729 | ||
4730 | /* Read replacements section in file FILE_DATA of length LEN with data | |
4731 | DATA. */ | |
4732 | ||
4733 | static void | |
4734 | read_replacements_section (struct lto_file_decl_data *file_data, | |
4735 | const char *data, | |
4736 | size_t len) | |
4737 | { | |
4738 | const struct lto_function_header *header = | |
4739 | (const struct lto_function_header *) data; | |
4740 | const int cfg_offset = sizeof (struct lto_function_header); | |
4741 | const int main_offset = cfg_offset + header->cfg_size; | |
4742 | const int string_offset = main_offset + header->main_size; | |
4743 | struct data_in *data_in; | |
4744 | struct lto_input_block ib_main; | |
4745 | unsigned int i; | |
4746 | unsigned int count; | |
4747 | ||
4748 | LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0, | |
4749 | header->main_size); | |
4750 | ||
4751 | data_in = lto_data_in_create (file_data, (const char *) data + string_offset, | |
6e1aa848 | 4752 | header->string_size, vNULL); |
2c9561b5 MJ |
4753 | count = streamer_read_uhwi (&ib_main); |
4754 | ||
4755 | for (i = 0; i < count; i++) | |
4756 | { | |
4757 | unsigned int index; | |
4758 | struct cgraph_node *node; | |
4759 | lto_symtab_encoder_t encoder; | |
4760 | ||
4761 | index = streamer_read_uhwi (&ib_main); | |
4762 | encoder = file_data->symtab_node_encoder; | |
4763 | node = cgraph (lto_symtab_encoder_deref (encoder, index)); | |
67348ccc | 4764 | gcc_assert (node->definition); |
2c9561b5 MJ |
4765 | read_agg_replacement_chain (&ib_main, node, data_in); |
4766 | } | |
4767 | lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data, | |
4768 | len); | |
4769 | lto_data_in_delete (data_in); | |
4770 | } | |
4771 | ||
4772 | /* Read IPA-CP aggregate replacements. */ | |
4773 | ||
4774 | void | |
4775 | ipa_prop_read_all_agg_replacement (void) | |
4776 | { | |
4777 | struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); | |
4778 | struct lto_file_decl_data *file_data; | |
4779 | unsigned int j = 0; | |
4780 | ||
4781 | while ((file_data = file_data_vec[j++])) | |
4782 | { | |
4783 | size_t len; | |
4784 | const char *data = lto_get_section_data (file_data, | |
4785 | LTO_section_ipcp_transform, | |
4786 | NULL, &len); | |
4787 | if (data) | |
4788 | read_replacements_section (file_data, data, len); | |
4789 | } | |
4790 | } | |
4791 | ||
4792 | /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in | |
4793 | NODE. */ | |
4794 | ||
4795 | static void | |
4796 | adjust_agg_replacement_values (struct cgraph_node *node, | |
4797 | struct ipa_agg_replacement_value *aggval) | |
4798 | { | |
4799 | struct ipa_agg_replacement_value *v; | |
4800 | int i, c = 0, d = 0, *adj; | |
4801 | ||
4802 | if (!node->clone.combined_args_to_skip) | |
4803 | return; | |
4804 | ||
4805 | for (v = aggval; v; v = v->next) | |
4806 | { | |
4807 | gcc_assert (v->index >= 0); | |
4808 | if (c < v->index) | |
4809 | c = v->index; | |
4810 | } | |
4811 | c++; | |
4812 | ||
4813 | adj = XALLOCAVEC (int, c); | |
4814 | for (i = 0; i < c; i++) | |
4815 | if (bitmap_bit_p (node->clone.combined_args_to_skip, i)) | |
4816 | { | |
4817 | adj[i] = -1; | |
4818 | d++; | |
4819 | } | |
4820 | else | |
4821 | adj[i] = i - d; | |
4822 | ||
4823 | for (v = aggval; v; v = v->next) | |
4824 | v->index = adj[v->index]; | |
4825 | } | |
4826 | ||
4827 | ||
4828 | /* Function body transformation phase. */ | |
4829 | ||
4830 | unsigned int | |
4831 | ipcp_transform_function (struct cgraph_node *node) | |
4832 | { | |
84562394 | 4833 | vec<ipa_param_descriptor> descriptors = vNULL; |
2c9561b5 MJ |
4834 | struct param_analysis_info *parms_ainfo; |
4835 | struct ipa_agg_replacement_value *aggval; | |
4836 | gimple_stmt_iterator gsi; | |
4837 | basic_block bb; | |
4838 | int param_count; | |
4839 | bool cfg_changed = false, something_changed = false; | |
4840 | ||
4841 | gcc_checking_assert (cfun); | |
4842 | gcc_checking_assert (current_function_decl); | |
4843 | ||
4844 | if (dump_file) | |
4845 | fprintf (dump_file, "Modification phase of node %s/%i\n", | |
fec39fa6 | 4846 | node->name (), node->order); |
2c9561b5 MJ |
4847 | |
4848 | aggval = ipa_get_agg_replacements_for_node (node); | |
4849 | if (!aggval) | |
4850 | return 0; | |
67348ccc | 4851 | param_count = count_formal_params (node->decl); |
2c9561b5 MJ |
4852 | if (param_count == 0) |
4853 | return 0; | |
4854 | adjust_agg_replacement_values (node, aggval); | |
4855 | if (dump_file) | |
4856 | ipa_dump_agg_replacement_values (dump_file, aggval); | |
4857 | parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count); | |
4858 | memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count); | |
9771b263 | 4859 | descriptors.safe_grow_cleared (param_count); |
2c9561b5 MJ |
4860 | ipa_populate_param_decls (node, descriptors); |
4861 | ||
11cd3bed | 4862 | FOR_EACH_BB_FN (bb, cfun) |
2c9561b5 MJ |
4863 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
4864 | { | |
4865 | struct ipa_agg_replacement_value *v; | |
4866 | gimple stmt = gsi_stmt (gsi); | |
4867 | tree rhs, val, t; | |
3ff2ca23 | 4868 | HOST_WIDE_INT offset, size; |
2c9561b5 MJ |
4869 | int index; |
4870 | bool by_ref, vce; | |
4871 | ||
4872 | if (!gimple_assign_load_p (stmt)) | |
4873 | continue; | |
4874 | rhs = gimple_assign_rhs1 (stmt); | |
4875 | if (!is_gimple_reg_type (TREE_TYPE (rhs))) | |
4876 | continue; | |
4877 | ||
4878 | vce = false; | |
4879 | t = rhs; | |
4880 | while (handled_component_p (t)) | |
4881 | { | |
4882 | /* V_C_E can do things like convert an array of integers to one | |
4883 | bigger integer and similar things we do not handle below. */ | |
4884 | if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR) | |
4885 | { | |
4886 | vce = true; | |
4887 | break; | |
4888 | } | |
4889 | t = TREE_OPERAND (t, 0); | |
4890 | } | |
4891 | if (vce) | |
4892 | continue; | |
4893 | ||
4894 | if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt, | |
3ff2ca23 | 4895 | rhs, &index, &offset, &size, &by_ref)) |
2c9561b5 MJ |
4896 | continue; |
4897 | for (v = aggval; v; v = v->next) | |
4898 | if (v->index == index | |
4899 | && v->offset == offset) | |
4900 | break; | |
3ff2ca23 JJ |
4901 | if (!v |
4902 | || v->by_ref != by_ref | |
9439e9a1 | 4903 | || tree_to_shwi (TYPE_SIZE (TREE_TYPE (v->value))) != size) |
2c9561b5 MJ |
4904 | continue; |
4905 | ||
4906 | gcc_checking_assert (is_gimple_ip_invariant (v->value)); | |
4907 | if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value))) | |
4908 | { | |
4909 | if (fold_convertible_p (TREE_TYPE (rhs), v->value)) | |
4910 | val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value); | |
4911 | else if (TYPE_SIZE (TREE_TYPE (rhs)) | |
4912 | == TYPE_SIZE (TREE_TYPE (v->value))) | |
4913 | val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value); | |
4914 | else | |
4915 | { | |
4916 | if (dump_file) | |
4917 | { | |
4918 | fprintf (dump_file, " const "); | |
4919 | print_generic_expr (dump_file, v->value, 0); | |
4920 | fprintf (dump_file, " can't be converted to type of "); | |
4921 | print_generic_expr (dump_file, rhs, 0); | |
4922 | fprintf (dump_file, "\n"); | |
4923 | } | |
4924 | continue; | |
4925 | } | |
4926 | } | |
4927 | else | |
4928 | val = v->value; | |
4929 | ||
4930 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4931 | { | |
4932 | fprintf (dump_file, "Modifying stmt:\n "); | |
4933 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
4934 | } | |
4935 | gimple_assign_set_rhs_from_tree (&gsi, val); | |
4936 | update_stmt (stmt); | |
4937 | ||
4938 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4939 | { | |
4940 | fprintf (dump_file, "into:\n "); | |
4941 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
4942 | fprintf (dump_file, "\n"); | |
4943 | } | |
4944 | ||
4945 | something_changed = true; | |
4946 | if (maybe_clean_eh_stmt (stmt) | |
4947 | && gimple_purge_dead_eh_edges (gimple_bb (stmt))) | |
4948 | cfg_changed = true; | |
4949 | } | |
4950 | ||
9771b263 | 4951 | (*ipa_node_agg_replacements)[node->uid] = NULL; |
2c9561b5 | 4952 | free_parms_ainfo (parms_ainfo, param_count); |
9771b263 | 4953 | descriptors.release (); |
2c9561b5 MJ |
4954 | |
4955 | if (!something_changed) | |
4956 | return 0; | |
4957 | else if (cfg_changed) | |
4958 | return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg; | |
4959 | else | |
4960 | return TODO_update_ssa_only_virtuals; | |
4961 | } |