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