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