OSDN Git Service

* cgraphbuild.c (build_cgraph_edges, rebuild_cgraph_edges): Build
[pf3gnuchains/gcc-fork.git] / gcc / ipa-prop.c
1 /* Interprocedural analyses.
2    Copyright (C) 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "langhooks.h"
26 #include "ggc.h"
27 #include "target.h"
28 #include "cgraph.h"
29 #include "ipa-prop.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-inline.h"
33 #include "flags.h"
34 #include "timevar.h"
35 #include "flags.h"
36 #include "diagnostic.h"
37 #include "lto-streamer.h"
38
39 /* Vector where the parameter infos are actually stored. */
40 VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
41 /* Vector where the parameter infos are actually stored. */
42 VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
43
44 /* Bitmap with all UIDs of call graph edges that have been already processed
45    by indirect inlining.  */
46 static bitmap iinlining_processed_edges;
47
48 /* Holders of ipa cgraph hooks: */
49 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
50 static struct cgraph_node_hook_list *node_removal_hook_holder;
51 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
52 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
53
54 /* Add cgraph NODE described by INFO to the worklist WL regardless of whether
55    it is in one or not.  It should almost never be used directly, as opposed to
56    ipa_push_func_to_list.  */
57
58 void
59 ipa_push_func_to_list_1 (struct ipa_func_list **wl,
60                          struct cgraph_node *node,
61                          struct ipa_node_params *info)
62 {
63   struct ipa_func_list *temp;
64
65   info->node_enqueued = 1;
66   temp = XCNEW (struct ipa_func_list);
67   temp->node = node;
68   temp->next = *wl;
69   *wl = temp;
70 }
71
72 /* Initialize worklist to contain all functions.  */
73
74 struct ipa_func_list *
75 ipa_init_func_list (void)
76 {
77   struct cgraph_node *node;
78   struct ipa_func_list * wl;
79
80   wl = NULL;
81   for (node = cgraph_nodes; node; node = node->next)
82     if (node->analyzed)
83       {
84         struct ipa_node_params *info = IPA_NODE_REF (node);
85         /* Unreachable nodes should have been eliminated before ipcp and
86            inlining.  */
87         gcc_assert (node->needed || node->reachable);
88         ipa_push_func_to_list_1 (&wl, node, info);
89       }
90
91   return wl;
92 }
93
94 /* Remove a function from the worklist WL and return it.  */
95
96 struct cgraph_node *
97 ipa_pop_func_from_list (struct ipa_func_list **wl)
98 {
99   struct ipa_node_params *info;
100   struct ipa_func_list *first;
101   struct cgraph_node *node;
102
103   first = *wl;
104   *wl = (*wl)->next;
105   node = first->node;
106   free (first);
107
108   info = IPA_NODE_REF (node);
109   info->node_enqueued = 0;
110   return node;
111 }
112
113 /* Return index of the formal whose tree is PTREE in function which corresponds
114    to INFO.  */
115
116 static int
117 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
118 {
119   int i, count;
120
121   count = ipa_get_param_count (info);
122   for (i = 0; i < count; i++)
123     if (ipa_get_param(info, i) == ptree)
124       return i;
125
126   return -1;
127 }
128
129 /* Populate the param_decl field in parameter descriptors of INFO that
130    corresponds to NODE.  */
131
132 static void
133 ipa_populate_param_decls (struct cgraph_node *node,
134                           struct ipa_node_params *info)
135 {
136   tree fndecl;
137   tree fnargs;
138   tree parm;
139   int param_num;
140
141   fndecl = node->decl;
142   fnargs = DECL_ARGUMENTS (fndecl);
143   param_num = 0;
144   for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
145     {
146       info->params[param_num].decl = parm;
147       param_num++;
148     }
149 }
150
151 /* Return how many formal parameters FNDECL has.  */
152
153 static inline int
154 count_formal_params_1 (tree fndecl)
155 {
156   tree parm;
157   int count = 0;
158
159   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
160     count++;
161
162   return count;
163 }
164
165 /* Count number of formal parameters in NOTE. Store the result to the
166    appropriate field of INFO.  */
167
168 static void
169 ipa_count_formal_params (struct cgraph_node *node,
170                          struct ipa_node_params *info)
171 {
172   int param_num;
173
174   param_num = count_formal_params_1 (node->decl);
175   ipa_set_param_count (info, param_num);
176 }
177
178 /* Initialize the ipa_node_params structure associated with NODE by counting
179    the function parameters, creating the descriptors and populating their
180    param_decls.  */
181
182 void
183 ipa_initialize_node_params (struct cgraph_node *node)
184 {
185   struct ipa_node_params *info = IPA_NODE_REF (node);
186
187   if (!info->params)
188     {
189       ipa_count_formal_params (node, info);
190       info->params = XCNEWVEC (struct ipa_param_descriptor,
191                                     ipa_get_param_count (info));
192       ipa_populate_param_decls (node, info);
193     }
194 }
195
196 /* Callback of walk_stmt_load_store_addr_ops for the visit_store and visit_addr
197    parameters.  If OP is a parameter declaration, mark it as modified in the
198    info structure passed in DATA.  */
199
200 static bool
201 visit_store_addr_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
202                                    tree op, void *data)
203 {
204   struct ipa_node_params *info = (struct ipa_node_params *) data;
205
206   op = get_base_address (op);
207   if (op
208       && TREE_CODE (op) == PARM_DECL)
209     {
210       int index = ipa_get_param_decl_index (info, op);
211       gcc_assert (index >= 0);
212       info->params[index].modified = true;
213     }
214
215   return false;
216 }
217
218 /* Compute which formal parameters of function associated with NODE are locally
219    modified or their address is taken.  Note that this does not apply on
220    parameters with SSA names but those can and should be analyzed
221    differently.  */
222
223 void
224 ipa_detect_param_modifications (struct cgraph_node *node)
225 {
226   tree decl = node->decl;
227   basic_block bb;
228   struct function *func;
229   gimple_stmt_iterator gsi;
230   struct ipa_node_params *info = IPA_NODE_REF (node);
231
232   if (ipa_get_param_count (info) == 0 || info->modification_analysis_done)
233     return;
234
235   func = DECL_STRUCT_FUNCTION (decl);
236   FOR_EACH_BB_FN (bb, func)
237     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
238       walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info, NULL,
239                                      visit_store_addr_for_mod_analysis,
240                                      visit_store_addr_for_mod_analysis);
241
242   info->modification_analysis_done = 1;
243 }
244
245 /* Count number of arguments callsite CS has and store it in
246    ipa_edge_args structure corresponding to this callsite.  */
247
248 void
249 ipa_count_arguments (struct cgraph_edge *cs)
250 {
251   gimple stmt;
252   int arg_num;
253
254   stmt = cs->call_stmt;
255   gcc_assert (is_gimple_call (stmt));
256   arg_num = gimple_call_num_args (stmt);
257   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
258       <= (unsigned) cgraph_edge_max_uid)
259     VEC_safe_grow_cleared (ipa_edge_args_t, gc,
260                            ipa_edge_args_vector, cgraph_edge_max_uid + 1);
261   ipa_set_cs_argument_count (IPA_EDGE_REF (cs), arg_num);
262 }
263
264 /* Print the jump functions of all arguments on all call graph edges going from
265    NODE to file F.  */
266
267 void
268 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
269 {
270   int i, count;
271   struct cgraph_edge *cs;
272   struct ipa_jump_func *jump_func;
273   enum jump_func_type type;
274
275   fprintf (f, "  Jump functions of caller  %s:\n", cgraph_node_name (node));
276   for (cs = node->callees; cs; cs = cs->next_callee)
277     {
278       if (!ipa_edge_args_info_available_for_edge_p (cs))
279         continue;
280
281       fprintf (f, "    callsite  %s ", cgraph_node_name (node));
282       fprintf (f, "-> %s :: \n", cgraph_node_name (cs->callee));
283
284       count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
285       for (i = 0; i < count; i++)
286         {
287           jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
288           type = jump_func->type;
289
290           fprintf (f, "       param %d: ", i);
291           if (type == IPA_JF_UNKNOWN)
292             fprintf (f, "UNKNOWN\n");
293           else if (type == IPA_JF_CONST)
294             {
295               tree val = jump_func->value.constant;
296               fprintf (f, "CONST: ");
297               print_generic_expr (f, val, 0);
298               if (TREE_CODE (val) == ADDR_EXPR
299                   && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
300                 {
301                   fprintf (f, " -> ");
302                   print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
303                                                        0);
304                 }
305               fprintf (f, "\n");
306             }
307           else if (type == IPA_JF_CONST_MEMBER_PTR)
308             {
309               fprintf (f, "CONST MEMBER PTR: ");
310               print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
311               fprintf (f, ", ");
312               print_generic_expr (f, jump_func->value.member_cst.delta, 0);
313               fprintf (f, "\n");
314             }
315           else if (type == IPA_JF_PASS_THROUGH)
316             {
317               fprintf (f, "PASS THROUGH: ");
318               fprintf (f, "%d, op %s ",
319                        jump_func->value.pass_through.formal_id,
320                        tree_code_name[(int)
321                                       jump_func->value.pass_through.operation]);
322               if (jump_func->value.pass_through.operation != NOP_EXPR)
323                 print_generic_expr (dump_file,
324                                     jump_func->value.pass_through.operand, 0);
325               fprintf (dump_file, "\n");
326             }
327           else if (type == IPA_JF_ANCESTOR)
328             {
329               fprintf (f, "ANCESTOR: ");
330               fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC"\n",
331                        jump_func->value.ancestor.formal_id,
332                        jump_func->value.ancestor.offset);
333             }
334         }
335     }
336 }
337
338 /* Print ipa_jump_func data structures of all nodes in the call graph to F.  */
339
340 void
341 ipa_print_all_jump_functions (FILE *f)
342 {
343   struct cgraph_node *node;
344
345   fprintf (f, "\nJump functions:\n");
346   for (node = cgraph_nodes; node; node = node->next)
347     {
348       ipa_print_node_jump_functions (f, node);
349     }
350 }
351
352 /* Determine whether passing ssa name NAME constitutes a polynomial
353    pass-through function or getting an address of an acestor and if so, write
354    such a jump function to JFUNC.  INFO describes the caller.  */
355
356 static void
357 compute_complex_pass_through (struct ipa_node_params *info,
358                               struct ipa_jump_func *jfunc,
359                               tree name)
360 {
361   HOST_WIDE_INT offset, size, max_size;
362   tree op1, op2, type;
363   int index;
364   gimple stmt = SSA_NAME_DEF_STMT (name);
365
366   if (!is_gimple_assign (stmt))
367     return;
368   op1 = gimple_assign_rhs1 (stmt);
369   op2 = gimple_assign_rhs2 (stmt);
370
371   if (op2)
372     {
373       if (TREE_CODE (op1) != SSA_NAME
374           || !SSA_NAME_IS_DEFAULT_DEF (op1)
375           || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
376               && !useless_type_conversion_p (TREE_TYPE (name),
377                                              TREE_TYPE (op1)))
378           || !is_gimple_ip_invariant (op2))
379         return;
380
381       index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
382       if (index >= 0)
383         {
384           jfunc->type = IPA_JF_PASS_THROUGH;
385           jfunc->value.pass_through.formal_id = index;
386           jfunc->value.pass_through.operation = gimple_assign_rhs_code (stmt);
387           jfunc->value.pass_through.operand = op2;
388         }
389       return;
390     }
391
392   if (TREE_CODE (op1) != ADDR_EXPR)
393     return;
394   op1 = TREE_OPERAND (op1, 0);
395   type = TREE_TYPE (op1);
396
397   op1 = get_ref_base_and_extent (op1, &offset, &size, &max_size);
398   if (TREE_CODE (op1) != INDIRECT_REF
399       /* If this is a varying address, punt.  */
400       || max_size == -1
401       || max_size != size)
402     return;
403   op1 = TREE_OPERAND (op1, 0);
404   if (TREE_CODE (op1) != SSA_NAME
405       || !SSA_NAME_IS_DEFAULT_DEF (op1))
406     return;
407
408   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
409   if (index >= 0)
410     {
411       jfunc->type = IPA_JF_ANCESTOR;
412       jfunc->value.ancestor.formal_id = index;
413       jfunc->value.ancestor.offset = offset;
414       jfunc->value.ancestor.type = type;
415     }
416 }
417
418
419 /* Determine the jump functions of scalar arguments.  Scalar means SSA names
420    and constants of a number of selected types.  INFO is the ipa_node_params
421    structure associated with the caller, FUNCTIONS is a pointer to an array of
422    jump function structures associated with CALL which is the call statement
423    being examined.*/
424
425 static void
426 compute_scalar_jump_functions (struct ipa_node_params *info,
427                                struct ipa_jump_func *functions,
428                                gimple call)
429 {
430   tree arg;
431   unsigned num = 0;
432
433   for (num = 0; num < gimple_call_num_args (call); num++)
434     {
435       arg = gimple_call_arg (call, num);
436
437       if (is_gimple_ip_invariant (arg))
438         {
439           functions[num].type = IPA_JF_CONST;
440           functions[num].value.constant = arg;
441         }
442       else if (TREE_CODE (arg) == SSA_NAME)
443         {
444           if (SSA_NAME_IS_DEFAULT_DEF (arg))
445             {
446               int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
447
448               if (index >= 0)
449                 {
450                   functions[num].type = IPA_JF_PASS_THROUGH;
451                   functions[num].value.pass_through.formal_id = index;
452                   functions[num].value.pass_through.operation = NOP_EXPR;
453                 }
454             }
455           else
456             compute_complex_pass_through (info, &functions[num], arg);
457         }
458     }
459 }
460
461 /* Inspect the given TYPE and return true iff it has the same structure (the
462    same number of fields of the same types) as a C++ member pointer.  If
463    METHOD_PTR and DELTA are non-NULL, store the trees representing the
464    corresponding fields there.  */
465
466 static bool
467 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
468 {
469   tree fld;
470
471   if (TREE_CODE (type) != RECORD_TYPE)
472     return false;
473
474   fld = TYPE_FIELDS (type);
475   if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
476       || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
477     return false;
478
479   if (method_ptr)
480     *method_ptr = fld;
481
482   fld = TREE_CHAIN (fld);
483   if (!fld || INTEGRAL_TYPE_P (fld))
484     return false;
485   if (delta)
486     *delta = fld;
487
488   if (TREE_CHAIN (fld))
489     return false;
490
491   return true;
492 }
493
494 /* Go through arguments of the CALL and for every one that looks like a member
495    pointer, check whether it can be safely declared pass-through and if so,
496    mark that to the corresponding item of jump FUNCTIONS.  Return true iff
497    there are non-pass-through member pointers within the arguments.  INFO
498    describes formal parameters of the caller.  */
499
500 static bool
501 compute_pass_through_member_ptrs (struct ipa_node_params *info,
502                                   struct ipa_jump_func *functions,
503                                   gimple call)
504 {
505   bool undecided_members = false;
506   unsigned num;
507   tree arg;
508
509   for (num = 0; num < gimple_call_num_args (call); num++)
510     {
511       arg = gimple_call_arg (call, num);
512
513       if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
514         {
515           if (TREE_CODE (arg) == PARM_DECL)
516             {
517               int index = ipa_get_param_decl_index (info, arg);
518
519               gcc_assert (index >=0);
520               if (!ipa_is_param_modified (info, index))
521                 {
522                   functions[num].type = IPA_JF_PASS_THROUGH;
523                   functions[num].value.pass_through.formal_id = index;
524                   functions[num].value.pass_through.operation = NOP_EXPR;
525                 }
526               else
527                 undecided_members = true;
528             }
529           else
530             undecided_members = true;
531         }
532     }
533
534   return undecided_members;
535 }
536
537 /* Simple function filling in a member pointer constant jump function (with PFN
538    and DELTA as the constant value) into JFUNC.  */
539
540 static void
541 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
542                                    tree pfn, tree delta)
543 {
544   jfunc->type = IPA_JF_CONST_MEMBER_PTR;
545   jfunc->value.member_cst.pfn = pfn;
546   jfunc->value.member_cst.delta = delta;
547 }
548
549 /* If RHS is an SSA_NAMe and it is defined by a simple copy assign statement,
550    return the rhs of its defining statement.  */
551
552 static inline tree
553 get_ssa_def_if_simple_copy (tree rhs)
554 {
555   while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
556     {
557       gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
558
559       if (gimple_assign_single_p (def_stmt))
560         rhs = gimple_assign_rhs1 (def_stmt);
561       else
562         break;
563     }
564   return rhs;
565 }
566
567 /* Traverse statements from CALL backwards, scanning whether the argument ARG
568    which is a member pointer is filled in with constant values.  If it is, fill
569    the jump function JFUNC in appropriately.  METHOD_FIELD and DELTA_FIELD are
570    fields of the record type of the member pointer.  To give an example, we
571    look for a pattern looking like the following:
572
573      D.2515.__pfn ={v} printStuff;
574      D.2515.__delta ={v} 0;
575      i_1 = doprinting (D.2515);  */
576
577 static void
578 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
579                           tree delta_field, struct ipa_jump_func *jfunc)
580 {
581   gimple_stmt_iterator gsi;
582   tree method = NULL_TREE;
583   tree delta = NULL_TREE;
584
585   gsi = gsi_for_stmt (call);
586
587   gsi_prev (&gsi);
588   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
589     {
590       gimple stmt = gsi_stmt (gsi);
591       tree lhs, rhs, fld;
592
593       if (!gimple_assign_single_p (stmt))
594         return;
595
596       lhs = gimple_assign_lhs (stmt);
597       rhs = gimple_assign_rhs1 (stmt);
598
599       if (TREE_CODE (lhs) != COMPONENT_REF
600           || TREE_OPERAND (lhs, 0) != arg)
601         continue;
602
603       fld = TREE_OPERAND (lhs, 1);
604       if (!method && fld == method_field)
605         {
606           rhs = get_ssa_def_if_simple_copy (rhs);
607           if (TREE_CODE (rhs) == ADDR_EXPR
608               && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
609               && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
610             {
611               method = TREE_OPERAND (rhs, 0);
612               if (delta)
613                 {
614                   fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
615                   return;
616                 }
617             }
618           else
619             return;
620         }
621
622       if (!delta && fld == delta_field)
623         {
624           rhs = get_ssa_def_if_simple_copy (rhs);
625           if (TREE_CODE (rhs) == INTEGER_CST)
626             {
627               delta = rhs;
628               if (method)
629                 {
630                   fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
631                   return;
632                 }
633             }
634           else
635             return;
636         }
637     }
638
639   return;
640 }
641
642 /* Go through the arguments of the CALL and for every member pointer within
643    tries determine whether it is a constant.  If it is, create a corresponding
644    constant jump function in FUNCTIONS which is an array of jump functions
645    associated with the call.  */
646
647 static void
648 compute_cst_member_ptr_arguments (struct ipa_jump_func *functions,
649                                   gimple call)
650 {
651   unsigned num;
652   tree arg, method_field, delta_field;
653
654   for (num = 0; num < gimple_call_num_args (call); num++)
655     {
656       arg = gimple_call_arg (call, num);
657
658       if (functions[num].type == IPA_JF_UNKNOWN
659           && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
660                                      &delta_field))
661         determine_cst_member_ptr (call, arg, method_field, delta_field,
662                                   &functions[num]);
663     }
664 }
665
666 /* Compute jump function for all arguments of callsite CS and insert the
667    information in the jump_functions array in the ipa_edge_args corresponding
668    to this callsite.  */
669
670 void
671 ipa_compute_jump_functions (struct cgraph_edge *cs)
672 {
673   struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
674   struct ipa_edge_args *arguments = IPA_EDGE_REF (cs);
675   gimple call;
676
677   if (ipa_get_cs_argument_count (arguments) == 0 || arguments->jump_functions)
678     return;
679   arguments->jump_functions = GGC_CNEWVEC (struct ipa_jump_func,
680                                            ipa_get_cs_argument_count (arguments));
681
682   call = cs->call_stmt;
683   gcc_assert (is_gimple_call (call));
684
685   /* We will deal with constants and SSA scalars first:  */
686   compute_scalar_jump_functions (info, arguments->jump_functions, call);
687
688   /* Let's check whether there are any potential member pointers and if so,
689      whether we can determine their functions as pass_through.  */
690   if (!compute_pass_through_member_ptrs (info, arguments->jump_functions, call))
691     return;
692
693   /* Finally, let's check whether we actually pass a new constant member
694      pointer here...  */
695   compute_cst_member_ptr_arguments (arguments->jump_functions, call);
696 }
697
698 /* If RHS looks like a rhs of a statement loading pfn from a member
699    pointer formal parameter, return the parameter, otherwise return
700    NULL.  If USE_DELTA, then we look for a use of the delta field
701    rather than the pfn.  */
702
703 static tree
704 ipa_get_member_ptr_load_param (tree rhs, bool use_delta)
705 {
706   tree rec, fld;
707   tree ptr_field;
708   tree delta_field;
709
710   if (TREE_CODE (rhs) != COMPONENT_REF)
711     return NULL_TREE;
712
713   rec = TREE_OPERAND (rhs, 0);
714   if (TREE_CODE (rec) != PARM_DECL
715       || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
716     return NULL_TREE;
717
718   fld = TREE_OPERAND (rhs, 1);
719   if (use_delta ? (fld == delta_field) : (fld == ptr_field))
720     return rec;
721   else
722     return NULL_TREE;
723 }
724
725 /* If STMT looks like a statement loading a value from a member pointer formal
726    parameter, this function returns that parameter.  */
727
728 static tree
729 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta)
730 {
731   tree rhs;
732
733   if (!gimple_assign_single_p (stmt))
734     return NULL_TREE;
735
736   rhs = gimple_assign_rhs1 (stmt);
737   return ipa_get_member_ptr_load_param (rhs, use_delta);
738 }
739
740 /* Returns true iff T is an SSA_NAME defined by a statement.  */
741
742 static bool
743 ipa_is_ssa_with_stmt_def (tree t)
744 {
745   if (TREE_CODE (t) == SSA_NAME
746       && !SSA_NAME_IS_DEFAULT_DEF (t))
747     return true;
748   else
749     return false;
750 }
751
752 /* Create a new indirect call graph edge describing a call to a parameter
753    number FORMAL_ID and and set the called flag of the parameter.  NODE is the
754    caller.  STMT is the corresponding call statement.  */
755
756 static void
757 ipa_note_param_call (struct cgraph_node *node, int formal_id, gimple stmt)
758 {
759   struct cgraph_edge *cs;
760
761   cs = cgraph_edge (node, stmt);
762   cs->indirect_info->param_index = formal_id;
763 }
764
765 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
766    (described by INFO).  Currently it checks whether the call calls a pointer
767    that is a formal parameter and if so, the parameter is marked with the
768    called flag and an indirect call graph edge describing the call is created.
769    This is very simple for ordinary pointers represented in SSA but not-so-nice
770    when it comes to member pointers.  The ugly part of this function does
771    nothing more than trying to match the pattern of such a call.  An example of
772    such a pattern is the gimple dump below, the call is on the last line:
773
774      <bb 2>:
775        f$__delta_5 = f.__delta;
776        f$__pfn_24 = f.__pfn;
777        D.2496_3 = (int) f$__pfn_24;
778        D.2497_4 = D.2496_3 & 1;
779        if (D.2497_4 != 0)
780          goto <bb 3>;
781        else
782          goto <bb 4>;
783
784      <bb 3>:
785        D.2500_7 = (unsigned int) f$__delta_5;
786        D.2501_8 = &S + D.2500_7;
787        D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
788        D.2503_10 = *D.2502_9;
789        D.2504_12 = f$__pfn_24 + -1;
790        D.2505_13 = (unsigned int) D.2504_12;
791        D.2506_14 = D.2503_10 + D.2505_13;
792        D.2507_15 = *D.2506_14;
793        iftmp.11_16 = (String:: *) D.2507_15;
794
795      <bb 4>:
796        # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
797        D.2500_19 = (unsigned int) f$__delta_5;
798        D.2508_20 = &S + D.2500_19;
799        D.2493_21 = iftmp.11_1 (D.2508_20, 4);
800
801    Such patterns are results of simple calls to a member pointer:
802
803      int doprinting (int (MyString::* f)(int) const)
804      {
805        MyString S ("somestring");
806
807        return (S.*f)(4);
808      }
809 */
810
811 static void
812 ipa_analyze_call_uses (struct cgraph_node *node, struct ipa_node_params *info,
813                        gimple call)
814 {
815   tree target = gimple_call_fn (call);
816   gimple def;
817   tree var;
818   tree n1, n2;
819   gimple d1, d2;
820   tree rec, rec2, cond;
821   gimple branch;
822   int index;
823   basic_block bb, virt_bb, join;
824
825   if (TREE_CODE (target) != SSA_NAME)
826     return;
827
828   var = SSA_NAME_VAR (target);
829   if (SSA_NAME_IS_DEFAULT_DEF (target))
830     {
831       /* assuming TREE_CODE (var) == PARM_DECL */
832       index = ipa_get_param_decl_index (info, var);
833       if (index >= 0)
834         ipa_note_param_call (node, index, call);
835       return;
836     }
837
838   /* Now we need to try to match the complex pattern of calling a member
839      pointer. */
840
841   if (!POINTER_TYPE_P (TREE_TYPE (target))
842       || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
843     return;
844
845   def = SSA_NAME_DEF_STMT (target);
846   if (gimple_code (def) != GIMPLE_PHI)
847     return;
848
849   if (gimple_phi_num_args (def) != 2)
850     return;
851
852   /* First, we need to check whether one of these is a load from a member
853      pointer that is a parameter to this function. */
854   n1 = PHI_ARG_DEF (def, 0);
855   n2 = PHI_ARG_DEF (def, 1);
856   if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
857     return;
858   d1 = SSA_NAME_DEF_STMT (n1);
859   d2 = SSA_NAME_DEF_STMT (n2);
860
861   if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false)))
862     {
863       if (ipa_get_stmt_member_ptr_load_param (d2, false))
864         return;
865
866       bb = gimple_bb (d1);
867       virt_bb = gimple_bb (d2);
868     }
869   else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false)))
870     {
871       bb = gimple_bb (d2);
872       virt_bb = gimple_bb (d1);
873     }
874   else
875     return;
876
877   /* Second, we need to check that the basic blocks are laid out in the way
878      corresponding to the pattern. */
879
880   join = gimple_bb (def);
881   if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
882       || single_pred (virt_bb) != bb
883       || single_succ (virt_bb) != join)
884     return;
885
886   /* Third, let's see that the branching is done depending on the least
887      significant bit of the pfn. */
888
889   branch = last_stmt (bb);
890   if (gimple_code (branch) != GIMPLE_COND)
891     return;
892
893   if (gimple_cond_code (branch) != NE_EXPR
894       || !integer_zerop (gimple_cond_rhs (branch)))
895     return;
896
897   cond = gimple_cond_lhs (branch);
898   if (!ipa_is_ssa_with_stmt_def (cond))
899     return;
900
901   def = SSA_NAME_DEF_STMT (cond);
902   if (!is_gimple_assign (def)
903       || gimple_assign_rhs_code (def) != BIT_AND_EXPR
904       || !integer_onep (gimple_assign_rhs2 (def)))
905     return;
906
907   cond = gimple_assign_rhs1 (def);
908   if (!ipa_is_ssa_with_stmt_def (cond))
909     return;
910
911   def = SSA_NAME_DEF_STMT (cond);
912
913   if (is_gimple_assign (def)
914       && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
915     {
916       cond = gimple_assign_rhs1 (def);
917       if (!ipa_is_ssa_with_stmt_def (cond))
918         return;
919       def = SSA_NAME_DEF_STMT (cond);
920     }
921
922   rec2 = ipa_get_stmt_member_ptr_load_param (def,
923                                              (TARGET_PTRMEMFUNC_VBIT_LOCATION
924                                               == ptrmemfunc_vbit_in_delta));
925
926   if (rec != rec2)
927     return;
928
929   index = ipa_get_param_decl_index (info, rec);
930   if (index >= 0 && !ipa_is_param_modified (info, index))
931     ipa_note_param_call (node, index, call);
932
933   return;
934 }
935
936 /* Analyze the call statement STMT with respect to formal parameters (described
937    in INFO) of caller given by NODE.  Currently it only checks whether formal
938    parameters are called.  */
939
940 static void
941 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
942                        gimple stmt)
943 {
944   if (is_gimple_call (stmt))
945     ipa_analyze_call_uses (node, info, stmt);
946 }
947
948 /* Scan the function body of NODE and inspect the uses of formal parameters.
949    Store the findings in various structures of the associated ipa_node_params
950    structure, such as parameter flags, notes etc.  */
951
952 void
953 ipa_analyze_params_uses (struct cgraph_node *node)
954 {
955   tree decl = node->decl;
956   basic_block bb;
957   struct function *func;
958   gimple_stmt_iterator gsi;
959   struct ipa_node_params *info = IPA_NODE_REF (node);
960
961   if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
962     return;
963
964   func = DECL_STRUCT_FUNCTION (decl);
965   FOR_EACH_BB_FN (bb, func)
966     {
967       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
968         {
969           gimple stmt = gsi_stmt (gsi);
970           ipa_analyze_stmt_uses (node, info, stmt);
971         }
972     }
973
974   info->uses_analysis_done = 1;
975 }
976
977 /* Update the jump functions associated with call graph edge E when the call
978    graph edge CS is being inlined, assuming that E->caller is already (possibly
979    indirectly) inlined into CS->callee and that E has not been inlined.
980
981    We keep pass through functions only if they do not contain any operation.
982    This is sufficient for inlining and greately simplifies things.  */
983
984 static void
985 update_jump_functions_after_inlining (struct cgraph_edge *cs,
986                                       struct cgraph_edge *e)
987 {
988   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
989   struct ipa_edge_args *args = IPA_EDGE_REF (e);
990   int count = ipa_get_cs_argument_count (args);
991   int i;
992
993   for (i = 0; i < count; i++)
994     {
995       struct ipa_jump_func *src, *dst = ipa_get_ith_jump_func (args, i);
996
997       if (dst->type == IPA_JF_ANCESTOR)
998         {
999           dst->type = IPA_JF_UNKNOWN;
1000           continue;
1001         }
1002
1003       if (dst->type != IPA_JF_PASS_THROUGH)
1004         continue;
1005
1006       /* We must check range due to calls with variable number of arguments and
1007          we cannot combine jump functions with operations.  */
1008       if (dst->value.pass_through.operation != NOP_EXPR
1009           || (dst->value.pass_through.formal_id
1010               >= ipa_get_cs_argument_count (top)))
1011         {
1012           dst->type = IPA_JF_UNKNOWN;
1013           continue;
1014         }
1015
1016       src = ipa_get_ith_jump_func (top, dst->value.pass_through.formal_id);
1017       *dst = *src;
1018     }
1019 }
1020
1021 /* Print out a debug message to file F that we have discovered that an indirect
1022    call described by NT is in fact a call of a known constant function described
1023    by JFUNC.  NODE is the node where the call is.  */
1024
1025 static void
1026 print_edge_addition_message (FILE *f, struct cgraph_edge *e,
1027                              struct ipa_jump_func *jfunc)
1028 {
1029   fprintf (f, "ipa-prop: Discovered an indirect call to a known target (");
1030   if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1031     {
1032       print_node_brief (f, "", jfunc->value.member_cst.pfn, 0);
1033       print_node_brief (f, ", ", jfunc->value.member_cst.delta, 0);
1034     }
1035   else
1036     print_node_brief(f, "", jfunc->value.constant, 0);
1037
1038   fprintf (f, ") in %s: ", cgraph_node_name (e->caller));
1039   print_gimple_stmt (f, e->call_stmt, 2, TDF_SLIM);
1040 }
1041
1042 /* Update the param called notes associated with NODE when CS is being inlined,
1043    assuming NODE is (potentially indirectly) inlined into CS->callee.
1044    Moreover, if the callee is discovered to be constant, create a new cgraph
1045    edge for it.  Newly discovered indirect edges will be added to *NEW_EDGES,
1046    unless NEW_EDGES is NULL.  Return true iff a new edge(s) were created.  */
1047
1048 static bool
1049 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
1050                                       struct cgraph_node *node,
1051                                       VEC (cgraph_edge_p, heap) **new_edges)
1052 {
1053   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1054   struct cgraph_edge *ie, *next_ie;
1055   bool res = false;
1056
1057   ipa_check_create_edge_args ();
1058
1059   for (ie = node->indirect_calls; ie; ie = next_ie)
1060     {
1061       struct cgraph_indirect_call_info *ici = ie->indirect_info;
1062       struct ipa_jump_func *jfunc;
1063
1064       next_ie = ie->next_callee;
1065       if (bitmap_bit_p (iinlining_processed_edges, ie->uid))
1066         continue;
1067
1068       /* If we ever use indirect edges for anything other than indirect
1069          inlining, we will need to skip those with negative param_indices. */
1070       if (ici->param_index == -1)
1071         continue;
1072
1073       /* We must check range due to calls with variable number of arguments:  */
1074       if (ici->param_index >= ipa_get_cs_argument_count (top))
1075         {
1076           bitmap_set_bit (iinlining_processed_edges, ie->uid);
1077           continue;
1078         }
1079
1080       jfunc = ipa_get_ith_jump_func (top, ici->param_index);
1081       if (jfunc->type == IPA_JF_PASS_THROUGH
1082           && jfunc->value.pass_through.operation == NOP_EXPR)
1083         ici->param_index = jfunc->value.pass_through.formal_id;
1084       else if (jfunc->type == IPA_JF_CONST
1085                || jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1086         {
1087           struct cgraph_node *callee;
1088           tree decl;
1089
1090           bitmap_set_bit (iinlining_processed_edges, ie->uid);
1091           if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1092             decl = jfunc->value.member_cst.pfn;
1093           else
1094             decl = jfunc->value.constant;
1095
1096           if (TREE_CODE (decl) != ADDR_EXPR)
1097             continue;
1098           decl = TREE_OPERAND (decl, 0);
1099
1100           if (TREE_CODE (decl) != FUNCTION_DECL)
1101             continue;
1102           callee = cgraph_node (decl);
1103           if (!callee || !callee->local.inlinable)
1104             continue;
1105
1106           res = true;
1107           if (dump_file)
1108             print_edge_addition_message (dump_file, ie, jfunc);
1109
1110           cgraph_make_edge_direct (ie, callee);
1111           ie->indirect_inlining_edge = 1;
1112           if (new_edges)
1113             VEC_safe_push (cgraph_edge_p, heap, *new_edges, ie);
1114           top = IPA_EDGE_REF (cs);
1115         }
1116       else
1117         {
1118           /* Ancestor jump functions and pass theoughs with operations should
1119              not be used on parameters that then get called.  */
1120           gcc_assert (jfunc->type == IPA_JF_UNKNOWN);
1121           bitmap_set_bit (iinlining_processed_edges, ie->uid);
1122         }
1123     }
1124
1125   return res;
1126 }
1127
1128 /* Recursively traverse subtree of NODE (including node) made of inlined
1129    cgraph_edges when CS has been inlined and invoke
1130    update_indirect_edges_after_inlining on all nodes and
1131    update_jump_functions_after_inlining on all non-inlined edges that lead out
1132    of this subtree.  Newly discovered indirect edges will be added to
1133    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were
1134    created.  */
1135
1136 static bool
1137 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1138                                    struct cgraph_node *node,
1139                                    VEC (cgraph_edge_p, heap) **new_edges)
1140 {
1141   struct cgraph_edge *e;
1142   bool res;
1143
1144   res = update_indirect_edges_after_inlining (cs, node, new_edges);
1145
1146   for (e = node->callees; e; e = e->next_callee)
1147     if (!e->inline_failed)
1148       res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1149     else
1150       update_jump_functions_after_inlining (cs, e);
1151
1152   return res;
1153 }
1154
1155 /* Update jump functions and call note functions on inlining the call site CS.
1156    CS is expected to lead to a node already cloned by
1157    cgraph_clone_inline_nodes.  Newly discovered indirect edges will be added to
1158    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were +
1159    created.  */
1160
1161 bool
1162 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1163                                    VEC (cgraph_edge_p, heap) **new_edges)
1164 {
1165   /* FIXME lto: We do not stream out indirect call information.  */
1166   if (flag_wpa)
1167     return false;
1168
1169   /* Do nothing if the preparation phase has not been carried out yet
1170      (i.e. during early inlining).  */
1171   if (!ipa_node_params_vector)
1172     return false;
1173   gcc_assert (ipa_edge_args_vector);
1174
1175   return propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1176 }
1177
1178 /* Frees all dynamically allocated structures that the argument info points
1179    to.  */
1180
1181 void
1182 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1183 {
1184   if (args->jump_functions)
1185     ggc_free (args->jump_functions);
1186
1187   memset (args, 0, sizeof (*args));
1188 }
1189
1190 /* Free all ipa_edge structures.  */
1191
1192 void
1193 ipa_free_all_edge_args (void)
1194 {
1195   int i;
1196   struct ipa_edge_args *args;
1197
1198   for (i = 0;
1199        VEC_iterate (ipa_edge_args_t, ipa_edge_args_vector, i, args);
1200        i++)
1201     ipa_free_edge_args_substructures (args);
1202
1203   VEC_free (ipa_edge_args_t, gc, ipa_edge_args_vector);
1204   ipa_edge_args_vector = NULL;
1205 }
1206
1207 /* Frees all dynamically allocated structures that the param info points
1208    to.  */
1209
1210 void
1211 ipa_free_node_params_substructures (struct ipa_node_params *info)
1212 {
1213   if (info->params)
1214     free (info->params);
1215
1216   memset (info, 0, sizeof (*info));
1217 }
1218
1219 /* Free all ipa_node_params structures.  */
1220
1221 void
1222 ipa_free_all_node_params (void)
1223 {
1224   int i;
1225   struct ipa_node_params *info;
1226
1227   for (i = 0;
1228        VEC_iterate (ipa_node_params_t, ipa_node_params_vector, i, info);
1229        i++)
1230     ipa_free_node_params_substructures (info);
1231
1232   VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1233   ipa_node_params_vector = NULL;
1234 }
1235
1236 /* Hook that is called by cgraph.c when an edge is removed.  */
1237
1238 static void
1239 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1240 {
1241   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1242   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1243       <= (unsigned)cs->uid)
1244     return;
1245   ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1246 }
1247
1248 /* Hook that is called by cgraph.c when a node is removed.  */
1249
1250 static void
1251 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1252 {
1253   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1254   if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
1255       <= (unsigned)node->uid)
1256     return;
1257   ipa_free_node_params_substructures (IPA_NODE_REF (node));
1258 }
1259
1260 /* Helper function to duplicate an array of size N that is at SRC and store a
1261    pointer to it to DST.  Nothing is done if SRC is NULL.  */
1262
1263 static void *
1264 duplicate_array (void *src, size_t n)
1265 {
1266   void *p;
1267
1268   if (!src)
1269     return NULL;
1270
1271   p = xmalloc (n);
1272   memcpy (p, src, n);
1273   return p;
1274 }
1275
1276 /* Like duplicate_array byt in GGC memory.  */
1277
1278 static void *
1279 duplicate_ggc_array (void *src, size_t n)
1280 {
1281   void *p;
1282
1283   if (!src)
1284     return NULL;
1285
1286   p = ggc_alloc (n);
1287   memcpy (p, src, n);
1288   return p;
1289 }
1290
1291 /* Hook that is called by cgraph.c when a node is duplicated.  */
1292
1293 static void
1294 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1295                            __attribute__((unused)) void *data)
1296 {
1297   struct ipa_edge_args *old_args, *new_args;
1298   int arg_count;
1299
1300   ipa_check_create_edge_args ();
1301
1302   old_args = IPA_EDGE_REF (src);
1303   new_args = IPA_EDGE_REF (dst);
1304
1305   arg_count = ipa_get_cs_argument_count (old_args);
1306   ipa_set_cs_argument_count (new_args, arg_count);
1307   new_args->jump_functions = (struct ipa_jump_func *)
1308     duplicate_ggc_array (old_args->jump_functions,
1309                          sizeof (struct ipa_jump_func) * arg_count);
1310
1311   if (iinlining_processed_edges
1312       && bitmap_bit_p (iinlining_processed_edges, src->uid))
1313     bitmap_set_bit (iinlining_processed_edges, dst->uid);
1314 }
1315
1316 /* Hook that is called by cgraph.c when a node is duplicated.  */
1317
1318 static void
1319 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1320                            __attribute__((unused)) void *data)
1321 {
1322   struct ipa_node_params *old_info, *new_info;
1323   int param_count;
1324
1325   ipa_check_create_node_params ();
1326   old_info = IPA_NODE_REF (src);
1327   new_info = IPA_NODE_REF (dst);
1328   param_count = ipa_get_param_count (old_info);
1329
1330   ipa_set_param_count (new_info, param_count);
1331   new_info->params = (struct ipa_param_descriptor *)
1332     duplicate_array (old_info->params,
1333                      sizeof (struct ipa_param_descriptor) * param_count);
1334   new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1335   new_info->count_scale = old_info->count_scale;
1336 }
1337
1338 /* Register our cgraph hooks if they are not already there.  */
1339
1340 void
1341 ipa_register_cgraph_hooks (void)
1342 {
1343   if (!edge_removal_hook_holder)
1344     edge_removal_hook_holder =
1345       cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1346   if (!node_removal_hook_holder)
1347     node_removal_hook_holder =
1348       cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1349   if (!edge_duplication_hook_holder)
1350     edge_duplication_hook_holder =
1351       cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1352   if (!node_duplication_hook_holder)
1353     node_duplication_hook_holder =
1354       cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1355 }
1356
1357 /* Unregister our cgraph hooks if they are not already there.  */
1358
1359 static void
1360 ipa_unregister_cgraph_hooks (void)
1361 {
1362   cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1363   edge_removal_hook_holder = NULL;
1364   cgraph_remove_node_removal_hook (node_removal_hook_holder);
1365   node_removal_hook_holder = NULL;
1366   cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1367   edge_duplication_hook_holder = NULL;
1368   cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1369   node_duplication_hook_holder = NULL;
1370 }
1371
1372 /* Allocate all necessary data strucutures necessary for indirect inlining.  */
1373
1374 void
1375 ipa_create_all_structures_for_iinln (void)
1376 {
1377   iinlining_processed_edges = BITMAP_ALLOC (NULL);
1378 }
1379
1380 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1381    longer needed after ipa-cp.  */
1382
1383 void
1384 ipa_free_all_structures_after_ipa_cp (void)
1385 {
1386   if (!flag_indirect_inlining)
1387     {
1388       ipa_free_all_edge_args ();
1389       ipa_free_all_node_params ();
1390       ipa_unregister_cgraph_hooks ();
1391     }
1392 }
1393
1394 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1395    longer needed after indirect inlining.  */
1396
1397 void
1398 ipa_free_all_structures_after_iinln (void)
1399 {
1400   BITMAP_FREE (iinlining_processed_edges);
1401
1402   ipa_free_all_edge_args ();
1403   ipa_free_all_node_params ();
1404   ipa_unregister_cgraph_hooks ();
1405 }
1406
1407 /* Print ipa_tree_map data structures of all functions in the
1408    callgraph to F.  */
1409
1410 void
1411 ipa_print_node_params (FILE * f, struct cgraph_node *node)
1412 {
1413   int i, count;
1414   tree temp;
1415   struct ipa_node_params *info;
1416
1417   if (!node->analyzed)
1418     return;
1419   info = IPA_NODE_REF (node);
1420   fprintf (f, "  function  %s Trees :: \n", cgraph_node_name (node));
1421   count = ipa_get_param_count (info);
1422   for (i = 0; i < count; i++)
1423     {
1424       temp = ipa_get_param (info, i);
1425       if (TREE_CODE (temp) == PARM_DECL)
1426         fprintf (f, "    param %d : %s", i,
1427                  (DECL_NAME (temp)
1428                   ? (*lang_hooks.decl_printable_name) (temp, 2)
1429                   : "(unnamed)"));
1430       if (ipa_is_param_modified (info, i))
1431         fprintf (f, " modified");
1432       fprintf (f, "\n");
1433     }
1434 }
1435
1436 /* Print ipa_tree_map data structures of all functions in the
1437    callgraph to F.  */
1438
1439 void
1440 ipa_print_all_params (FILE * f)
1441 {
1442   struct cgraph_node *node;
1443
1444   fprintf (f, "\nFunction parameters:\n");
1445   for (node = cgraph_nodes; node; node = node->next)
1446     ipa_print_node_params (f, node);
1447 }
1448
1449 /* Return a heap allocated vector containing formal parameters of FNDECL.  */
1450
1451 VEC(tree, heap) *
1452 ipa_get_vector_of_formal_parms (tree fndecl)
1453 {
1454   VEC(tree, heap) *args;
1455   int count;
1456   tree parm;
1457
1458   count = count_formal_params_1 (fndecl);
1459   args = VEC_alloc (tree, heap, count);
1460   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
1461     VEC_quick_push (tree, args, parm);
1462
1463   return args;
1464 }
1465
1466 /* Return a heap allocated vector containing types of formal parameters of
1467    function type FNTYPE.  */
1468
1469 static inline VEC(tree, heap) *
1470 get_vector_of_formal_parm_types (tree fntype)
1471 {
1472   VEC(tree, heap) *types;
1473   int count = 0;
1474   tree t;
1475
1476   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1477     count++;
1478
1479   types = VEC_alloc (tree, heap, count);
1480   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1481     VEC_quick_push (tree, types, TREE_VALUE (t));
1482
1483   return types;
1484 }
1485
1486 /* Modify the function declaration FNDECL and its type according to the plan in
1487    ADJUSTMENTS.  It also sets base fields of individual adjustments structures
1488    to reflect the actual parameters being modified which are determined by the
1489    base_index field.  */
1490
1491 void
1492 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
1493                               const char *synth_parm_prefix)
1494 {
1495   VEC(tree, heap) *oparms, *otypes;
1496   tree orig_type, new_type = NULL;
1497   tree old_arg_types, t, new_arg_types = NULL;
1498   tree parm, *link = &DECL_ARGUMENTS (fndecl);
1499   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1500   tree new_reversed = NULL;
1501   bool care_for_types, last_parm_void;
1502
1503   if (!synth_parm_prefix)
1504     synth_parm_prefix = "SYNTH";
1505
1506   oparms = ipa_get_vector_of_formal_parms (fndecl);
1507   orig_type = TREE_TYPE (fndecl);
1508   old_arg_types = TYPE_ARG_TYPES (orig_type);
1509
1510   /* The following test is an ugly hack, some functions simply don't have any
1511      arguments in their type.  This is probably a bug but well... */
1512   care_for_types = (old_arg_types != NULL_TREE);
1513   if (care_for_types)
1514     {
1515       last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
1516                         == void_type_node);
1517       otypes = get_vector_of_formal_parm_types (orig_type);
1518       if (last_parm_void)
1519         gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
1520       else
1521         gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
1522     }
1523   else
1524     {
1525       last_parm_void = false;
1526       otypes = NULL;
1527     }
1528
1529   for (i = 0; i < len; i++)
1530     {
1531       struct ipa_parm_adjustment *adj;
1532       gcc_assert (link);
1533
1534       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1535       parm = VEC_index (tree, oparms, adj->base_index);
1536       adj->base = parm;
1537
1538       if (adj->copy_param)
1539         {
1540           if (care_for_types)
1541             new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
1542                                                              adj->base_index),
1543                                        new_arg_types);
1544           *link = parm;
1545           link = &TREE_CHAIN (parm);
1546         }
1547       else if (!adj->remove_param)
1548         {
1549           tree new_parm;
1550           tree ptype;
1551
1552           if (adj->by_ref)
1553             ptype = build_pointer_type (adj->type);
1554           else
1555             ptype = adj->type;
1556
1557           if (care_for_types)
1558             new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
1559
1560           new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1561                                  ptype);
1562           DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
1563
1564           DECL_ARTIFICIAL (new_parm) = 1;
1565           DECL_ARG_TYPE (new_parm) = ptype;
1566           DECL_CONTEXT (new_parm) = fndecl;
1567           TREE_USED (new_parm) = 1;
1568           DECL_IGNORED_P (new_parm) = 1;
1569           layout_decl (new_parm, 0);
1570
1571           add_referenced_var (new_parm);
1572           mark_sym_for_renaming (new_parm);
1573           adj->base = parm;
1574           adj->reduction = new_parm;
1575
1576           *link = new_parm;
1577
1578           link = &TREE_CHAIN (new_parm);
1579         }
1580     }
1581
1582   *link = NULL_TREE;
1583
1584   if (care_for_types)
1585     {
1586       new_reversed = nreverse (new_arg_types);
1587       if (last_parm_void)
1588         {
1589           if (new_reversed)
1590             TREE_CHAIN (new_arg_types) = void_list_node;
1591           else
1592             new_reversed = void_list_node;
1593         }
1594     }
1595
1596   /* Use copy_node to preserve as much as possible from original type
1597      (debug info, attribute lists etc.)
1598      Exception is METHOD_TYPEs must have THIS argument.
1599      When we are asked to remove it, we need to build new FUNCTION_TYPE
1600      instead.  */
1601   if (TREE_CODE (orig_type) != METHOD_TYPE
1602        || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
1603          && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
1604     {
1605       new_type = copy_node (orig_type);
1606       TYPE_ARG_TYPES (new_type) = new_reversed;
1607     }
1608   else
1609     {
1610       new_type
1611         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
1612                                                          new_reversed));
1613       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
1614       DECL_VINDEX (fndecl) = NULL_TREE;
1615     }
1616
1617   /* This is a new type, not a copy of an old type.  Need to reassociate
1618      variants.  We can handle everything except the main variant lazily.  */
1619   t = TYPE_MAIN_VARIANT (orig_type);
1620   if (orig_type != t)
1621     {
1622       TYPE_MAIN_VARIANT (new_type) = t;
1623       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
1624       TYPE_NEXT_VARIANT (t) = new_type;
1625     }
1626   else
1627     {
1628       TYPE_MAIN_VARIANT (new_type) = new_type;
1629       TYPE_NEXT_VARIANT (new_type) = NULL;
1630     }
1631
1632   TREE_TYPE (fndecl) = new_type;
1633   if (otypes)
1634     VEC_free (tree, heap, otypes);
1635   VEC_free (tree, heap, oparms);
1636 }
1637
1638 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1639    If this is a directly recursive call, CS must be NULL.  Otherwise it must
1640    contain the corresponding call graph edge.  */
1641
1642 void
1643 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
1644                            ipa_parm_adjustment_vec adjustments)
1645 {
1646   VEC(tree, heap) *vargs;
1647   gimple new_stmt;
1648   gimple_stmt_iterator gsi;
1649   tree callee_decl;
1650   int i, len;
1651
1652   len = VEC_length (ipa_parm_adjustment_t, adjustments);
1653   vargs = VEC_alloc (tree, heap, len);
1654
1655   gsi = gsi_for_stmt (stmt);
1656   for (i = 0; i < len; i++)
1657     {
1658       struct ipa_parm_adjustment *adj;
1659
1660       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1661
1662       if (adj->copy_param)
1663         {
1664           tree arg = gimple_call_arg (stmt, adj->base_index);
1665
1666           VEC_quick_push (tree, vargs, arg);
1667         }
1668       else if (!adj->remove_param)
1669         {
1670           tree expr, orig_expr;
1671           bool allow_ptr, repl_found;
1672
1673           orig_expr = expr = gimple_call_arg (stmt, adj->base_index);
1674           if (TREE_CODE (expr) == ADDR_EXPR)
1675             {
1676               allow_ptr = false;
1677               expr = TREE_OPERAND (expr, 0);
1678             }
1679           else
1680             allow_ptr = true;
1681
1682           repl_found = build_ref_for_offset (&expr, TREE_TYPE (expr),
1683                                              adj->offset, adj->type,
1684                                              allow_ptr);
1685           if (repl_found)
1686             {
1687               if (adj->by_ref)
1688                 expr = build_fold_addr_expr (expr);
1689             }
1690           else
1691             {
1692               tree ptrtype = build_pointer_type (adj->type);
1693               expr = orig_expr;
1694               if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1695                 expr = build_fold_addr_expr (expr);
1696               if (!useless_type_conversion_p (ptrtype, TREE_TYPE (expr)))
1697                 expr = fold_convert (ptrtype, expr);
1698               expr = fold_build2 (POINTER_PLUS_EXPR, ptrtype, expr,
1699                                   build_int_cst (sizetype,
1700                                                  adj->offset / BITS_PER_UNIT));
1701               if (!adj->by_ref)
1702                 expr = fold_build1 (INDIRECT_REF, adj->type, expr);
1703             }
1704           expr = force_gimple_operand_gsi (&gsi, expr,
1705                                            adj->by_ref
1706                                            || is_gimple_reg_type (adj->type),
1707                                            NULL, true, GSI_SAME_STMT);
1708           VEC_quick_push (tree, vargs, expr);
1709         }
1710     }
1711
1712   if (dump_file && (dump_flags & TDF_DETAILS))
1713     {
1714       fprintf (dump_file, "replacing stmt:");
1715       print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1716     }
1717
1718   callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
1719   new_stmt = gimple_build_call_vec (callee_decl, vargs);
1720   VEC_free (tree, heap, vargs);
1721   if (gimple_call_lhs (stmt))
1722     gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
1723
1724   gimple_set_block (new_stmt, gimple_block (stmt));
1725   if (gimple_has_location (stmt))
1726     gimple_set_location (new_stmt, gimple_location (stmt));
1727   gimple_call_copy_flags (new_stmt, stmt);
1728   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1729
1730   if (dump_file && (dump_flags & TDF_DETAILS))
1731     {
1732       fprintf (dump_file, "with stmt:");
1733       print_gimple_stmt (dump_file, new_stmt, 0, 0);
1734       fprintf (dump_file, "\n");
1735     }
1736   gsi_replace (&gsi, new_stmt, true);
1737   if (cs)
1738     cgraph_set_call_stmt (cs, new_stmt);
1739   update_ssa (TODO_update_ssa);
1740   free_dominance_info (CDI_DOMINATORS);
1741 }
1742
1743 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once.  */
1744
1745 static bool
1746 index_in_adjustments_multiple_times_p (int base_index,
1747                                        ipa_parm_adjustment_vec adjustments)
1748 {
1749   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1750   bool one = false;
1751
1752   for (i = 0; i < len; i++)
1753     {
1754       struct ipa_parm_adjustment *adj;
1755       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1756
1757       if (adj->base_index == base_index)
1758         {
1759           if (one)
1760             return true;
1761           else
1762             one = true;
1763         }
1764     }
1765   return false;
1766 }
1767
1768
1769 /* Return adjustments that should have the same effect on function parameters
1770    and call arguments as if they were first changed according to adjustments in
1771    INNER and then by adjustments in OUTER.  */
1772
1773 ipa_parm_adjustment_vec
1774 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
1775                          ipa_parm_adjustment_vec outer)
1776 {
1777   int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
1778   int inlen = VEC_length (ipa_parm_adjustment_t, inner);
1779   int removals = 0;
1780   ipa_parm_adjustment_vec adjustments, tmp;
1781
1782   tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
1783   for (i = 0; i < inlen; i++)
1784     {
1785       struct ipa_parm_adjustment *n;
1786       n = VEC_index (ipa_parm_adjustment_t, inner, i);
1787
1788       if (n->remove_param)
1789         removals++;
1790       else
1791         VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
1792     }
1793
1794   adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
1795   for (i = 0; i < outlen; i++)
1796     {
1797       struct ipa_parm_adjustment *r;
1798       struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
1799                                                    outer, i);
1800       struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
1801                                                   out->base_index);
1802
1803       gcc_assert (!in->remove_param);
1804       if (out->remove_param)
1805         {
1806           if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
1807             {
1808               r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1809               memset (r, 0, sizeof (*r));
1810               r->remove_param = true;
1811             }
1812           continue;
1813         }
1814
1815       r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1816       memset (r, 0, sizeof (*r));
1817       r->base_index = in->base_index;
1818       r->type = out->type;
1819
1820       /* FIXME:  Create nonlocal value too.  */
1821
1822       if (in->copy_param && out->copy_param)
1823         r->copy_param = true;
1824       else if (in->copy_param)
1825         r->offset = out->offset;
1826       else if (out->copy_param)
1827         r->offset = in->offset;
1828       else
1829         r->offset = in->offset + out->offset;
1830     }
1831
1832   for (i = 0; i < inlen; i++)
1833     {
1834       struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
1835                                                  inner, i);
1836
1837       if (n->remove_param)
1838         VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
1839     }
1840
1841   VEC_free (ipa_parm_adjustment_t, heap, tmp);
1842   return adjustments;
1843 }
1844
1845 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1846    friendly way, assuming they are meant to be applied to FNDECL.  */
1847
1848 void
1849 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
1850                             tree fndecl)
1851 {
1852   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1853   bool first = true;
1854   VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
1855
1856   fprintf (file, "IPA param adjustments: ");
1857   for (i = 0; i < len; i++)
1858     {
1859       struct ipa_parm_adjustment *adj;
1860       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1861
1862       if (!first)
1863         fprintf (file, "                 ");
1864       else
1865         first = false;
1866
1867       fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
1868       print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
1869       if (adj->base)
1870         {
1871           fprintf (file, ", base: ");
1872           print_generic_expr (file, adj->base, 0);
1873         }
1874       if (adj->reduction)
1875         {
1876           fprintf (file, ", reduction: ");
1877           print_generic_expr (file, adj->reduction, 0);
1878         }
1879       if (adj->new_ssa_base)
1880         {
1881           fprintf (file, ", new_ssa_base: ");
1882           print_generic_expr (file, adj->new_ssa_base, 0);
1883         }
1884
1885       if (adj->copy_param)
1886         fprintf (file, ", copy_param");
1887       else if (adj->remove_param)
1888         fprintf (file, ", remove_param");
1889       else
1890         fprintf (file, ", offset %li", (long) adj->offset);
1891       if (adj->by_ref)
1892         fprintf (file, ", by_ref");
1893       print_node_brief (file, ", type: ", adj->type, 0);
1894       fprintf (file, "\n");
1895     }
1896   VEC_free (tree, heap, parms);
1897 }
1898
1899 /* Stream out jump function JUMP_FUNC to OB.  */
1900
1901 static void
1902 ipa_write_jump_function (struct output_block *ob,
1903                          struct ipa_jump_func *jump_func)
1904 {
1905   lto_output_uleb128_stream (ob->main_stream,
1906                              jump_func->type);
1907
1908   switch (jump_func->type)
1909     {
1910     case IPA_JF_UNKNOWN:
1911       break;
1912     case IPA_JF_CONST:
1913       lto_output_tree (ob, jump_func->value.constant, true);
1914       break;
1915     case IPA_JF_PASS_THROUGH:
1916       lto_output_tree (ob, jump_func->value.pass_through.operand, true);
1917       lto_output_uleb128_stream (ob->main_stream,
1918                                  jump_func->value.pass_through.formal_id);
1919       lto_output_uleb128_stream (ob->main_stream,
1920                                  jump_func->value.pass_through.operation);
1921       break;
1922     case IPA_JF_ANCESTOR:
1923       lto_output_uleb128_stream (ob->main_stream,
1924                                  jump_func->value.ancestor.offset);
1925       lto_output_tree (ob, jump_func->value.ancestor.type, true);
1926       lto_output_uleb128_stream (ob->main_stream,
1927                                  jump_func->value.ancestor.formal_id);
1928       break;
1929     case IPA_JF_CONST_MEMBER_PTR:
1930       lto_output_tree (ob, jump_func->value.member_cst.pfn, true);
1931       lto_output_tree (ob, jump_func->value.member_cst.delta, false);
1932       break;
1933     }
1934 }
1935
1936 /* Read in jump function JUMP_FUNC from IB.  */
1937
1938 static void
1939 ipa_read_jump_function (struct lto_input_block *ib,
1940                         struct ipa_jump_func *jump_func,
1941                         struct data_in *data_in)
1942 {
1943   jump_func->type = (enum jump_func_type) lto_input_uleb128 (ib);
1944
1945   switch (jump_func->type)
1946     {
1947     case IPA_JF_UNKNOWN:
1948       break;
1949     case IPA_JF_CONST:
1950       jump_func->value.constant = lto_input_tree (ib, data_in);
1951       break;
1952     case IPA_JF_PASS_THROUGH:
1953       jump_func->value.pass_through.operand = lto_input_tree (ib, data_in);
1954       jump_func->value.pass_through.formal_id = lto_input_uleb128 (ib);
1955       jump_func->value.pass_through.operation = (enum tree_code) lto_input_uleb128 (ib);
1956       break;
1957     case IPA_JF_ANCESTOR:
1958       jump_func->value.ancestor.offset = lto_input_uleb128 (ib);
1959       jump_func->value.ancestor.type = lto_input_tree (ib, data_in);
1960       jump_func->value.ancestor.formal_id = lto_input_uleb128 (ib);
1961       break;
1962     case IPA_JF_CONST_MEMBER_PTR:
1963       jump_func->value.member_cst.pfn = lto_input_tree (ib, data_in);
1964       jump_func->value.member_cst.delta = lto_input_tree (ib, data_in);
1965       break;
1966     }
1967 }
1968
1969 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
1970    relevant to indirect inlining to OB.  */
1971
1972 static void
1973 ipa_write_indirect_edge_info (struct output_block *ob,
1974                               struct cgraph_edge *cs)
1975 {
1976   struct cgraph_indirect_call_info *ii = cs->indirect_info;
1977
1978   lto_output_sleb128_stream (ob->main_stream, ii->param_index);
1979 }
1980
1981 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
1982    relevant to indirect inlining from IB.  */
1983
1984 static void
1985 ipa_read_indirect_edge_info (struct lto_input_block *ib,
1986                              struct data_in *data_in ATTRIBUTE_UNUSED,
1987                              struct cgraph_edge *cs)
1988 {
1989   struct cgraph_indirect_call_info *ii = cs->indirect_info;
1990
1991   ii->param_index = (int) lto_input_sleb128 (ib);
1992 }
1993
1994 /* Stream out NODE info to OB.  */
1995
1996 static void
1997 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
1998 {
1999   int node_ref;
2000   lto_cgraph_encoder_t encoder;
2001   struct ipa_node_params *info = IPA_NODE_REF (node);
2002   int j;
2003   struct cgraph_edge *e;
2004   struct bitpack_d *bp;
2005
2006   encoder = ob->decl_state->cgraph_node_encoder;
2007   node_ref = lto_cgraph_encoder_encode (encoder, node);
2008   lto_output_uleb128_stream (ob->main_stream, node_ref);
2009
2010   bp = bitpack_create ();
2011   bp_pack_value (bp, info->called_with_var_arguments, 1);
2012   bp_pack_value (bp, info->uses_analysis_done, 1);
2013   gcc_assert (info->modification_analysis_done
2014               || ipa_get_param_count (info) == 0);
2015   gcc_assert (!info->node_enqueued);
2016   gcc_assert (!info->ipcp_orig_node);
2017   for (j = 0; j < ipa_get_param_count (info); j++)
2018     bp_pack_value (bp, info->params[j].modified, 1);
2019   lto_output_bitpack (ob->main_stream, bp);
2020   bitpack_delete (bp);
2021   for (e = node->callees; e; e = e->next_callee)
2022     {
2023       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2024
2025       lto_output_uleb128_stream (ob->main_stream,
2026                                  ipa_get_cs_argument_count (args));
2027       for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2028         ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2029     }
2030   for (e = node->indirect_calls; e; e = e->next_callee)
2031     ipa_write_indirect_edge_info (ob, e);
2032 }
2033
2034 /* Srtream in NODE info from IB.  */
2035
2036 static void
2037 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2038                     struct data_in *data_in)
2039 {
2040   struct ipa_node_params *info = IPA_NODE_REF (node);
2041   int k;
2042   struct cgraph_edge *e;
2043   struct bitpack_d *bp;
2044
2045   ipa_initialize_node_params (node);
2046
2047   bp = lto_input_bitpack (ib);
2048   info->called_with_var_arguments = bp_unpack_value (bp, 1);
2049   info->uses_analysis_done = bp_unpack_value (bp, 1);
2050   if (ipa_get_param_count (info) != 0)
2051     {
2052       info->modification_analysis_done = true;
2053       info->uses_analysis_done = true;
2054     }
2055   info->node_enqueued = false;
2056   for (k = 0; k < ipa_get_param_count (info); k++)
2057     info->params[k].modified = bp_unpack_value (bp, 1);
2058   bitpack_delete (bp);
2059   for (e = node->callees; e; e = e->next_callee)
2060     {
2061       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2062       int count = lto_input_uleb128 (ib);
2063
2064       ipa_set_cs_argument_count (args, count);
2065       if (!count)
2066         continue;
2067
2068       args->jump_functions = GGC_CNEWVEC (struct ipa_jump_func,
2069                                           ipa_get_cs_argument_count (args));
2070       for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2071         ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2072     }
2073   for (e = node->indirect_calls; e; e = e->next_callee)
2074     ipa_read_indirect_edge_info (ib, data_in, e);
2075 }
2076
2077 /* Write jump functions for nodes in SET.  */
2078
2079 void
2080 ipa_prop_write_jump_functions (cgraph_node_set set)
2081 {
2082   struct cgraph_node *node;
2083   struct output_block *ob = create_output_block (LTO_section_jump_functions);
2084   unsigned int count = 0;
2085   cgraph_node_set_iterator csi;
2086
2087   ob->cgraph_node = NULL;
2088
2089   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2090     {
2091       node = csi_node (csi);
2092       if (node->analyzed && IPA_NODE_REF (node) != NULL)
2093         count++;
2094     }
2095
2096   lto_output_uleb128_stream (ob->main_stream, count);
2097
2098   /* Process all of the functions.  */
2099   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2100     {
2101       node = csi_node (csi);
2102       if (node->analyzed && IPA_NODE_REF (node) != NULL)
2103         ipa_write_node_info (ob, node);
2104     }
2105   lto_output_1_stream (ob->main_stream, 0);
2106   produce_asm (ob, NULL);
2107   destroy_output_block (ob);
2108 }
2109
2110 /* Read section in file FILE_DATA of length LEN with data DATA.  */
2111
2112 static void
2113 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2114                        size_t len)
2115 {
2116   const struct lto_function_header *header =
2117     (const struct lto_function_header *) data;
2118   const int32_t cfg_offset = sizeof (struct lto_function_header);
2119   const int32_t main_offset = cfg_offset + header->cfg_size;
2120   const int32_t string_offset = main_offset + header->main_size;
2121   struct data_in *data_in;
2122   struct lto_input_block ib_main;
2123   unsigned int i;
2124   unsigned int count;
2125
2126   LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2127                         header->main_size);
2128
2129   data_in =
2130     lto_data_in_create (file_data, (const char *) data + string_offset,
2131                         header->string_size, NULL);
2132   count = lto_input_uleb128 (&ib_main);
2133
2134   for (i = 0; i < count; i++)
2135     {
2136       unsigned int index;
2137       struct cgraph_node *node;
2138       lto_cgraph_encoder_t encoder;
2139
2140       index = lto_input_uleb128 (&ib_main);
2141       encoder = file_data->cgraph_node_encoder;
2142       node = lto_cgraph_encoder_deref (encoder, index);
2143       ipa_read_node_info (&ib_main, node, data_in);
2144     }
2145   lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2146                          len);
2147   lto_data_in_delete (data_in);
2148 }
2149
2150 /* Read ipcp jump functions.  */
2151
2152 void
2153 ipa_prop_read_jump_functions (void)
2154 {
2155   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2156   struct lto_file_decl_data *file_data;
2157   unsigned int j = 0;
2158
2159   ipa_check_create_node_params ();
2160   ipa_check_create_edge_args ();
2161   ipa_register_cgraph_hooks ();
2162
2163   while ((file_data = file_data_vec[j++]))
2164     {
2165       size_t len;
2166       const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2167
2168       if (data)
2169         ipa_prop_read_section (file_data, data, len);
2170     }
2171 }
2172
2173 /* After merging units, we can get mismatch in argument counts.
2174    Also decl merging might've rendered parameter lists obsolette.
2175    Also compute called_with_variable_arg info.  */
2176
2177 void
2178 ipa_update_after_lto_read (void)
2179 {
2180   struct cgraph_node *node;
2181   struct cgraph_edge *cs;
2182
2183   ipa_check_create_node_params ();
2184   ipa_check_create_edge_args ();
2185
2186   for (node = cgraph_nodes; node; node = node->next)
2187     if (node->analyzed)
2188       ipa_initialize_node_params (node);
2189
2190   for (node = cgraph_nodes; node; node = node->next)
2191     if (node->analyzed)
2192       for (cs = node->callees; cs; cs = cs->next_callee)
2193         {
2194           if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
2195               != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
2196             ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
2197         }
2198 }