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