OSDN Git Service

* config/mips/dbxmdebug.h: Remove.
[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   basic_block bb = gimple_bb (stmt);
761   int freq;
762
763   freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
764   cs = cgraph_create_indirect_edge (node, stmt, bb->count, freq,
765                                     bb->loop_depth);
766   cs->indirect_info->param_index = formal_id;
767 }
768
769 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
770    (described by INFO).  Currently it checks whether the call calls a pointer
771    that is a formal parameter and if so, the parameter is marked with the
772    called flag and an indirect call graph edge describing the call is created.
773    This is very simple for ordinary pointers represented in SSA but not-so-nice
774    when it comes to member pointers.  The ugly part of this function does
775    nothing more than trying to match the pattern of such a call.  An example of
776    such a pattern is the gimple dump below, the call is on the last line:
777
778      <bb 2>:
779        f$__delta_5 = f.__delta;
780        f$__pfn_24 = f.__pfn;
781        D.2496_3 = (int) f$__pfn_24;
782        D.2497_4 = D.2496_3 & 1;
783        if (D.2497_4 != 0)
784          goto <bb 3>;
785        else
786          goto <bb 4>;
787
788      <bb 3>:
789        D.2500_7 = (unsigned int) f$__delta_5;
790        D.2501_8 = &S + D.2500_7;
791        D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
792        D.2503_10 = *D.2502_9;
793        D.2504_12 = f$__pfn_24 + -1;
794        D.2505_13 = (unsigned int) D.2504_12;
795        D.2506_14 = D.2503_10 + D.2505_13;
796        D.2507_15 = *D.2506_14;
797        iftmp.11_16 = (String:: *) D.2507_15;
798
799      <bb 4>:
800        # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
801        D.2500_19 = (unsigned int) f$__delta_5;
802        D.2508_20 = &S + D.2500_19;
803        D.2493_21 = iftmp.11_1 (D.2508_20, 4);
804
805    Such patterns are results of simple calls to a member pointer:
806
807      int doprinting (int (MyString::* f)(int) const)
808      {
809        MyString S ("somestring");
810
811        return (S.*f)(4);
812      }
813 */
814
815 static void
816 ipa_analyze_call_uses (struct cgraph_node *node, struct ipa_node_params *info,
817                        gimple call)
818 {
819   tree target = gimple_call_fn (call);
820   gimple def;
821   tree var;
822   tree n1, n2;
823   gimple d1, d2;
824   tree rec, rec2, cond;
825   gimple branch;
826   int index;
827   basic_block bb, virt_bb, join;
828
829   if (TREE_CODE (target) != SSA_NAME)
830     return;
831
832   var = SSA_NAME_VAR (target);
833   if (SSA_NAME_IS_DEFAULT_DEF (target))
834     {
835       /* assuming TREE_CODE (var) == PARM_DECL */
836       index = ipa_get_param_decl_index (info, var);
837       if (index >= 0)
838         ipa_note_param_call (node, index, call);
839       return;
840     }
841
842   /* Now we need to try to match the complex pattern of calling a member
843      pointer. */
844
845   if (!POINTER_TYPE_P (TREE_TYPE (target))
846       || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
847     return;
848
849   def = SSA_NAME_DEF_STMT (target);
850   if (gimple_code (def) != GIMPLE_PHI)
851     return;
852
853   if (gimple_phi_num_args (def) != 2)
854     return;
855
856   /* First, we need to check whether one of these is a load from a member
857      pointer that is a parameter to this function. */
858   n1 = PHI_ARG_DEF (def, 0);
859   n2 = PHI_ARG_DEF (def, 1);
860   if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
861     return;
862   d1 = SSA_NAME_DEF_STMT (n1);
863   d2 = SSA_NAME_DEF_STMT (n2);
864
865   if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false)))
866     {
867       if (ipa_get_stmt_member_ptr_load_param (d2, false))
868         return;
869
870       bb = gimple_bb (d1);
871       virt_bb = gimple_bb (d2);
872     }
873   else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false)))
874     {
875       bb = gimple_bb (d2);
876       virt_bb = gimple_bb (d1);
877     }
878   else
879     return;
880
881   /* Second, we need to check that the basic blocks are laid out in the way
882      corresponding to the pattern. */
883
884   join = gimple_bb (def);
885   if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
886       || single_pred (virt_bb) != bb
887       || single_succ (virt_bb) != join)
888     return;
889
890   /* Third, let's see that the branching is done depending on the least
891      significant bit of the pfn. */
892
893   branch = last_stmt (bb);
894   if (gimple_code (branch) != GIMPLE_COND)
895     return;
896
897   if (gimple_cond_code (branch) != NE_EXPR
898       || !integer_zerop (gimple_cond_rhs (branch)))
899     return;
900
901   cond = gimple_cond_lhs (branch);
902   if (!ipa_is_ssa_with_stmt_def (cond))
903     return;
904
905   def = SSA_NAME_DEF_STMT (cond);
906   if (!is_gimple_assign (def)
907       || gimple_assign_rhs_code (def) != BIT_AND_EXPR
908       || !integer_onep (gimple_assign_rhs2 (def)))
909     return;
910
911   cond = gimple_assign_rhs1 (def);
912   if (!ipa_is_ssa_with_stmt_def (cond))
913     return;
914
915   def = SSA_NAME_DEF_STMT (cond);
916
917   if (is_gimple_assign (def)
918       && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
919     {
920       cond = gimple_assign_rhs1 (def);
921       if (!ipa_is_ssa_with_stmt_def (cond))
922         return;
923       def = SSA_NAME_DEF_STMT (cond);
924     }
925
926   rec2 = ipa_get_stmt_member_ptr_load_param (def,
927                                              (TARGET_PTRMEMFUNC_VBIT_LOCATION
928                                               == ptrmemfunc_vbit_in_delta));
929
930   if (rec != rec2)
931     return;
932
933   index = ipa_get_param_decl_index (info, rec);
934   if (index >= 0 && !ipa_is_param_modified (info, index))
935     ipa_note_param_call (node, index, call);
936
937   return;
938 }
939
940 /* Analyze the call statement STMT with respect to formal parameters (described
941    in INFO) of caller given by NODE.  Currently it only checks whether formal
942    parameters are called.  */
943
944 static void
945 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
946                        gimple stmt)
947 {
948   if (is_gimple_call (stmt))
949     ipa_analyze_call_uses (node, info, stmt);
950 }
951
952 /* Scan the function body of NODE and inspect the uses of formal parameters.
953    Store the findings in various structures of the associated ipa_node_params
954    structure, such as parameter flags, notes etc.  */
955
956 void
957 ipa_analyze_params_uses (struct cgraph_node *node)
958 {
959   tree decl = node->decl;
960   basic_block bb;
961   struct function *func;
962   gimple_stmt_iterator gsi;
963   struct ipa_node_params *info = IPA_NODE_REF (node);
964
965   if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
966     return;
967
968   func = DECL_STRUCT_FUNCTION (decl);
969   FOR_EACH_BB_FN (bb, func)
970     {
971       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
972         {
973           gimple stmt = gsi_stmt (gsi);
974           ipa_analyze_stmt_uses (node, info, stmt);
975         }
976     }
977
978   info->uses_analysis_done = 1;
979 }
980
981 /* Update the jump functions associated with call graph edge E when the call
982    graph edge CS is being inlined, assuming that E->caller is already (possibly
983    indirectly) inlined into CS->callee and that E has not been inlined.
984
985    We keep pass through functions only if they do not contain any operation.
986    This is sufficient for inlining and greately simplifies things.  */
987
988 static void
989 update_jump_functions_after_inlining (struct cgraph_edge *cs,
990                                       struct cgraph_edge *e)
991 {
992   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
993   struct ipa_edge_args *args = IPA_EDGE_REF (e);
994   int count = ipa_get_cs_argument_count (args);
995   int i;
996
997   for (i = 0; i < count; i++)
998     {
999       struct ipa_jump_func *src, *dst = ipa_get_ith_jump_func (args, i);
1000
1001       if (dst->type == IPA_JF_ANCESTOR)
1002         {
1003           dst->type = IPA_JF_UNKNOWN;
1004           continue;
1005         }
1006
1007       if (dst->type != IPA_JF_PASS_THROUGH)
1008         continue;
1009
1010       /* We must check range due to calls with variable number of arguments and
1011          we cannot combine jump functions with operations.  */
1012       if (dst->value.pass_through.operation != NOP_EXPR
1013           || (dst->value.pass_through.formal_id
1014               >= ipa_get_cs_argument_count (top)))
1015         {
1016           dst->type = IPA_JF_UNKNOWN;
1017           continue;
1018         }
1019
1020       src = ipa_get_ith_jump_func (top, dst->value.pass_through.formal_id);
1021       *dst = *src;
1022     }
1023 }
1024
1025 /* Print out a debug message to file F that we have discovered that an indirect
1026    call described by NT is in fact a call of a known constant function described
1027    by JFUNC.  NODE is the node where the call is.  */
1028
1029 static void
1030 print_edge_addition_message (FILE *f, struct cgraph_edge *e,
1031                              struct ipa_jump_func *jfunc)
1032 {
1033   fprintf (f, "ipa-prop: Discovered an indirect call to a known target (");
1034   if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1035     {
1036       print_node_brief (f, "", jfunc->value.member_cst.pfn, 0);
1037       print_node_brief (f, ", ", jfunc->value.member_cst.delta, 0);
1038     }
1039   else
1040     print_node_brief(f, "", jfunc->value.constant, 0);
1041
1042   fprintf (f, ") in %s: ", cgraph_node_name (e->caller));
1043   print_gimple_stmt (f, e->call_stmt, 2, TDF_SLIM);
1044 }
1045
1046 /* Update the param called notes associated with NODE when CS is being inlined,
1047    assuming NODE is (potentially indirectly) inlined into CS->callee.
1048    Moreover, if the callee is discovered to be constant, create a new cgraph
1049    edge for it.  Newly discovered indirect edges will be added to *NEW_EDGES,
1050    unless NEW_EDGES is NULL.  Return true iff a new edge(s) were created.  */
1051
1052 static bool
1053 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
1054                                       struct cgraph_node *node,
1055                                       VEC (cgraph_edge_p, heap) **new_edges)
1056 {
1057   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1058   struct cgraph_edge *ie, *next_ie;
1059   bool res = false;
1060
1061   ipa_check_create_edge_args ();
1062
1063   for (ie = node->indirect_calls; ie; ie = next_ie)
1064     {
1065       struct cgraph_indirect_call_info *ici = ie->indirect_info;
1066       struct ipa_jump_func *jfunc;
1067
1068       next_ie = ie->next_callee;
1069       if (bitmap_bit_p (iinlining_processed_edges, ie->uid))
1070         continue;
1071
1072       /* If we ever use indirect edges for anything other than indirect
1073          inlining, we will need to skip those with negative param_indices. */
1074       gcc_assert (ici->param_index >= 0);
1075
1076       /* We must check range due to calls with variable number of arguments:  */
1077       if (ici->param_index >= ipa_get_cs_argument_count (top))
1078         {
1079           bitmap_set_bit (iinlining_processed_edges, ie->uid);
1080           continue;
1081         }
1082
1083       jfunc = ipa_get_ith_jump_func (top, ici->param_index);
1084       if (jfunc->type == IPA_JF_PASS_THROUGH
1085           && jfunc->value.pass_through.operation == NOP_EXPR)
1086         ici->param_index = jfunc->value.pass_through.formal_id;
1087       else if (jfunc->type == IPA_JF_CONST
1088                || jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1089         {
1090           struct cgraph_node *callee;
1091           tree decl;
1092
1093           bitmap_set_bit (iinlining_processed_edges, ie->uid);
1094           if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1095             decl = jfunc->value.member_cst.pfn;
1096           else
1097             decl = jfunc->value.constant;
1098
1099           if (TREE_CODE (decl) != ADDR_EXPR)
1100             continue;
1101           decl = TREE_OPERAND (decl, 0);
1102
1103           if (TREE_CODE (decl) != FUNCTION_DECL)
1104             continue;
1105           callee = cgraph_node (decl);
1106           if (!callee || !callee->local.inlinable)
1107             continue;
1108
1109           res = true;
1110           if (dump_file)
1111             print_edge_addition_message (dump_file, ie, jfunc);
1112
1113           cgraph_make_edge_direct (ie, callee);
1114           ie->indirect_inlining_edge = 1;
1115           if (new_edges)
1116             VEC_safe_push (cgraph_edge_p, heap, *new_edges, ie);
1117           top = IPA_EDGE_REF (cs);
1118         }
1119       else
1120         {
1121           /* Ancestor jump functions and pass theoughs with operations should
1122              not be used on parameters that then get called.  */
1123           gcc_assert (jfunc->type == IPA_JF_UNKNOWN);
1124           bitmap_set_bit (iinlining_processed_edges, ie->uid);
1125         }
1126     }
1127
1128   return res;
1129 }
1130
1131 /* Recursively traverse subtree of NODE (including node) made of inlined
1132    cgraph_edges when CS has been inlined and invoke
1133    update_indirect_edges_after_inlining on all nodes and
1134    update_jump_functions_after_inlining on all non-inlined edges that lead out
1135    of this subtree.  Newly discovered indirect edges will be added to
1136    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were
1137    created.  */
1138
1139 static bool
1140 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1141                                    struct cgraph_node *node,
1142                                    VEC (cgraph_edge_p, heap) **new_edges)
1143 {
1144   struct cgraph_edge *e;
1145   bool res;
1146
1147   res = update_indirect_edges_after_inlining (cs, node, new_edges);
1148
1149   for (e = node->callees; e; e = e->next_callee)
1150     if (!e->inline_failed)
1151       res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1152     else
1153       update_jump_functions_after_inlining (cs, e);
1154
1155   return res;
1156 }
1157
1158 /* Update jump functions and call note functions on inlining the call site CS.
1159    CS is expected to lead to a node already cloned by
1160    cgraph_clone_inline_nodes.  Newly discovered indirect edges will be added to
1161    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were +
1162    created.  */
1163
1164 bool
1165 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1166                                    VEC (cgraph_edge_p, heap) **new_edges)
1167 {
1168   /* FIXME lto: We do not stream out indirect call information.  */
1169   if (flag_wpa)
1170     return false;
1171
1172   /* Do nothing if the preparation phase has not been carried out yet
1173      (i.e. during early inlining).  */
1174   if (!ipa_node_params_vector)
1175     return false;
1176   gcc_assert (ipa_edge_args_vector);
1177
1178   return propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1179 }
1180
1181 /* Frees all dynamically allocated structures that the argument info points
1182    to.  */
1183
1184 void
1185 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1186 {
1187   if (args->jump_functions)
1188     ggc_free (args->jump_functions);
1189
1190   memset (args, 0, sizeof (*args));
1191 }
1192
1193 /* Free all ipa_edge structures.  */
1194
1195 void
1196 ipa_free_all_edge_args (void)
1197 {
1198   int i;
1199   struct ipa_edge_args *args;
1200
1201   for (i = 0;
1202        VEC_iterate (ipa_edge_args_t, ipa_edge_args_vector, i, args);
1203        i++)
1204     ipa_free_edge_args_substructures (args);
1205
1206   VEC_free (ipa_edge_args_t, gc, ipa_edge_args_vector);
1207   ipa_edge_args_vector = NULL;
1208 }
1209
1210 /* Frees all dynamically allocated structures that the param info points
1211    to.  */
1212
1213 void
1214 ipa_free_node_params_substructures (struct ipa_node_params *info)
1215 {
1216   if (info->params)
1217     free (info->params);
1218
1219   memset (info, 0, sizeof (*info));
1220 }
1221
1222 /* Free all ipa_node_params structures.  */
1223
1224 void
1225 ipa_free_all_node_params (void)
1226 {
1227   int i;
1228   struct ipa_node_params *info;
1229
1230   for (i = 0;
1231        VEC_iterate (ipa_node_params_t, ipa_node_params_vector, i, info);
1232        i++)
1233     ipa_free_node_params_substructures (info);
1234
1235   VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1236   ipa_node_params_vector = NULL;
1237 }
1238
1239 /* Hook that is called by cgraph.c when an edge is removed.  */
1240
1241 static void
1242 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1243 {
1244   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1245   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1246       <= (unsigned)cs->uid)
1247     return;
1248   ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1249 }
1250
1251 /* Hook that is called by cgraph.c when a node is removed.  */
1252
1253 static void
1254 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1255 {
1256   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1257   if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
1258       <= (unsigned)node->uid)
1259     return;
1260   ipa_free_node_params_substructures (IPA_NODE_REF (node));
1261 }
1262
1263 /* Helper function to duplicate an array of size N that is at SRC and store a
1264    pointer to it to DST.  Nothing is done if SRC is NULL.  */
1265
1266 static void *
1267 duplicate_array (void *src, size_t n)
1268 {
1269   void *p;
1270
1271   if (!src)
1272     return NULL;
1273
1274   p = xmalloc (n);
1275   memcpy (p, src, n);
1276   return p;
1277 }
1278
1279 /* Like duplicate_array byt in GGC memory.  */
1280
1281 static void *
1282 duplicate_ggc_array (void *src, size_t n)
1283 {
1284   void *p;
1285
1286   if (!src)
1287     return NULL;
1288
1289   p = ggc_alloc (n);
1290   memcpy (p, src, n);
1291   return p;
1292 }
1293
1294 /* Hook that is called by cgraph.c when a node is duplicated.  */
1295
1296 static void
1297 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1298                            __attribute__((unused)) void *data)
1299 {
1300   struct ipa_edge_args *old_args, *new_args;
1301   int arg_count;
1302
1303   ipa_check_create_edge_args ();
1304
1305   old_args = IPA_EDGE_REF (src);
1306   new_args = IPA_EDGE_REF (dst);
1307
1308   arg_count = ipa_get_cs_argument_count (old_args);
1309   ipa_set_cs_argument_count (new_args, arg_count);
1310   new_args->jump_functions = (struct ipa_jump_func *)
1311     duplicate_ggc_array (old_args->jump_functions,
1312                          sizeof (struct ipa_jump_func) * arg_count);
1313
1314   if (iinlining_processed_edges
1315       && bitmap_bit_p (iinlining_processed_edges, src->uid))
1316     bitmap_set_bit (iinlining_processed_edges, dst->uid);
1317 }
1318
1319 /* Hook that is called by cgraph.c when a node is duplicated.  */
1320
1321 static void
1322 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1323                            __attribute__((unused)) void *data)
1324 {
1325   struct ipa_node_params *old_info, *new_info;
1326   int param_count;
1327
1328   ipa_check_create_node_params ();
1329   old_info = IPA_NODE_REF (src);
1330   new_info = IPA_NODE_REF (dst);
1331   param_count = ipa_get_param_count (old_info);
1332
1333   ipa_set_param_count (new_info, param_count);
1334   new_info->params = (struct ipa_param_descriptor *)
1335     duplicate_array (old_info->params,
1336                      sizeof (struct ipa_param_descriptor) * param_count);
1337   new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1338   new_info->count_scale = old_info->count_scale;
1339 }
1340
1341 /* Register our cgraph hooks if they are not already there.  */
1342
1343 void
1344 ipa_register_cgraph_hooks (void)
1345 {
1346   if (!edge_removal_hook_holder)
1347     edge_removal_hook_holder =
1348       cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1349   if (!node_removal_hook_holder)
1350     node_removal_hook_holder =
1351       cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1352   if (!edge_duplication_hook_holder)
1353     edge_duplication_hook_holder =
1354       cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1355   if (!node_duplication_hook_holder)
1356     node_duplication_hook_holder =
1357       cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1358 }
1359
1360 /* Unregister our cgraph hooks if they are not already there.  */
1361
1362 static void
1363 ipa_unregister_cgraph_hooks (void)
1364 {
1365   cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1366   edge_removal_hook_holder = NULL;
1367   cgraph_remove_node_removal_hook (node_removal_hook_holder);
1368   node_removal_hook_holder = NULL;
1369   cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1370   edge_duplication_hook_holder = NULL;
1371   cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1372   node_duplication_hook_holder = NULL;
1373 }
1374
1375 /* Allocate all necessary data strucutures necessary for indirect inlining.  */
1376
1377 void
1378 ipa_create_all_structures_for_iinln (void)
1379 {
1380   iinlining_processed_edges = BITMAP_ALLOC (NULL);
1381 }
1382
1383 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1384    longer needed after ipa-cp.  */
1385
1386 void
1387 ipa_free_all_structures_after_ipa_cp (void)
1388 {
1389   if (!flag_indirect_inlining)
1390     {
1391       ipa_free_all_edge_args ();
1392       ipa_free_all_node_params ();
1393       ipa_unregister_cgraph_hooks ();
1394     }
1395 }
1396
1397 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1398    longer needed after indirect inlining.  */
1399
1400 void
1401 ipa_free_all_structures_after_iinln (void)
1402 {
1403   BITMAP_FREE (iinlining_processed_edges);
1404
1405   ipa_free_all_edge_args ();
1406   ipa_free_all_node_params ();
1407   ipa_unregister_cgraph_hooks ();
1408 }
1409
1410 /* Print ipa_tree_map data structures of all functions in the
1411    callgraph to F.  */
1412
1413 void
1414 ipa_print_node_params (FILE * f, struct cgraph_node *node)
1415 {
1416   int i, count;
1417   tree temp;
1418   struct ipa_node_params *info;
1419
1420   if (!node->analyzed)
1421     return;
1422   info = IPA_NODE_REF (node);
1423   fprintf (f, "  function  %s Trees :: \n", cgraph_node_name (node));
1424   count = ipa_get_param_count (info);
1425   for (i = 0; i < count; i++)
1426     {
1427       temp = ipa_get_param (info, i);
1428       if (TREE_CODE (temp) == PARM_DECL)
1429         fprintf (f, "    param %d : %s", i,
1430                  (DECL_NAME (temp)
1431                   ? (*lang_hooks.decl_printable_name) (temp, 2)
1432                   : "(unnamed)"));
1433       if (ipa_is_param_modified (info, i))
1434         fprintf (f, " modified");
1435       fprintf (f, "\n");
1436     }
1437 }
1438
1439 /* Print ipa_tree_map data structures of all functions in the
1440    callgraph to F.  */
1441
1442 void
1443 ipa_print_all_params (FILE * f)
1444 {
1445   struct cgraph_node *node;
1446
1447   fprintf (f, "\nFunction parameters:\n");
1448   for (node = cgraph_nodes; node; node = node->next)
1449     ipa_print_node_params (f, node);
1450 }
1451
1452 /* Return a heap allocated vector containing formal parameters of FNDECL.  */
1453
1454 VEC(tree, heap) *
1455 ipa_get_vector_of_formal_parms (tree fndecl)
1456 {
1457   VEC(tree, heap) *args;
1458   int count;
1459   tree parm;
1460
1461   count = count_formal_params_1 (fndecl);
1462   args = VEC_alloc (tree, heap, count);
1463   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
1464     VEC_quick_push (tree, args, parm);
1465
1466   return args;
1467 }
1468
1469 /* Return a heap allocated vector containing types of formal parameters of
1470    function type FNTYPE.  */
1471
1472 static inline VEC(tree, heap) *
1473 get_vector_of_formal_parm_types (tree fntype)
1474 {
1475   VEC(tree, heap) *types;
1476   int count = 0;
1477   tree t;
1478
1479   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1480     count++;
1481
1482   types = VEC_alloc (tree, heap, count);
1483   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1484     VEC_quick_push (tree, types, TREE_VALUE (t));
1485
1486   return types;
1487 }
1488
1489 /* Modify the function declaration FNDECL and its type according to the plan in
1490    ADJUSTMENTS.  It also sets base fields of individual adjustments structures
1491    to reflect the actual parameters being modified which are determined by the
1492    base_index field.  */
1493
1494 void
1495 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
1496                               const char *synth_parm_prefix)
1497 {
1498   VEC(tree, heap) *oparms, *otypes;
1499   tree orig_type, new_type = NULL;
1500   tree old_arg_types, t, new_arg_types = NULL;
1501   tree parm, *link = &DECL_ARGUMENTS (fndecl);
1502   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1503   tree new_reversed = NULL;
1504   bool care_for_types, last_parm_void;
1505
1506   if (!synth_parm_prefix)
1507     synth_parm_prefix = "SYNTH";
1508
1509   oparms = ipa_get_vector_of_formal_parms (fndecl);
1510   orig_type = TREE_TYPE (fndecl);
1511   old_arg_types = TYPE_ARG_TYPES (orig_type);
1512
1513   /* The following test is an ugly hack, some functions simply don't have any
1514      arguments in their type.  This is probably a bug but well... */
1515   care_for_types = (old_arg_types != NULL_TREE);
1516   if (care_for_types)
1517     {
1518       last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
1519                         == void_type_node);
1520       otypes = get_vector_of_formal_parm_types (orig_type);
1521       if (last_parm_void)
1522         gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
1523       else
1524         gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
1525     }
1526   else
1527     {
1528       last_parm_void = false;
1529       otypes = NULL;
1530     }
1531
1532   for (i = 0; i < len; i++)
1533     {
1534       struct ipa_parm_adjustment *adj;
1535       gcc_assert (link);
1536
1537       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1538       parm = VEC_index (tree, oparms, adj->base_index);
1539       adj->base = parm;
1540
1541       if (adj->copy_param)
1542         {
1543           if (care_for_types)
1544             new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
1545                                                              adj->base_index),
1546                                        new_arg_types);
1547           *link = parm;
1548           link = &TREE_CHAIN (parm);
1549         }
1550       else if (!adj->remove_param)
1551         {
1552           tree new_parm;
1553           tree ptype;
1554
1555           if (adj->by_ref)
1556             ptype = build_pointer_type (adj->type);
1557           else
1558             ptype = adj->type;
1559
1560           if (care_for_types)
1561             new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
1562
1563           new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1564                                  ptype);
1565           DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
1566
1567           DECL_ARTIFICIAL (new_parm) = 1;
1568           DECL_ARG_TYPE (new_parm) = ptype;
1569           DECL_CONTEXT (new_parm) = fndecl;
1570           TREE_USED (new_parm) = 1;
1571           DECL_IGNORED_P (new_parm) = 1;
1572           layout_decl (new_parm, 0);
1573
1574           add_referenced_var (new_parm);
1575           mark_sym_for_renaming (new_parm);
1576           adj->base = parm;
1577           adj->reduction = new_parm;
1578
1579           *link = new_parm;
1580
1581           link = &TREE_CHAIN (new_parm);
1582         }
1583     }
1584
1585   *link = NULL_TREE;
1586
1587   if (care_for_types)
1588     {
1589       new_reversed = nreverse (new_arg_types);
1590       if (last_parm_void)
1591         {
1592           if (new_reversed)
1593             TREE_CHAIN (new_arg_types) = void_list_node;
1594           else
1595             new_reversed = void_list_node;
1596         }
1597     }
1598
1599   /* Use copy_node to preserve as much as possible from original type
1600      (debug info, attribute lists etc.)
1601      Exception is METHOD_TYPEs must have THIS argument.
1602      When we are asked to remove it, we need to build new FUNCTION_TYPE
1603      instead.  */
1604   if (TREE_CODE (orig_type) != METHOD_TYPE
1605        || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
1606          && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
1607     {
1608       new_type = copy_node (orig_type);
1609       TYPE_ARG_TYPES (new_type) = new_reversed;
1610     }
1611   else
1612     {
1613       new_type
1614         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
1615                                                          new_reversed));
1616       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
1617       DECL_VINDEX (fndecl) = NULL_TREE;
1618     }
1619
1620   /* This is a new type, not a copy of an old type.  Need to reassociate
1621      variants.  We can handle everything except the main variant lazily.  */
1622   t = TYPE_MAIN_VARIANT (orig_type);
1623   if (orig_type != t)
1624     {
1625       TYPE_MAIN_VARIANT (new_type) = t;
1626       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
1627       TYPE_NEXT_VARIANT (t) = new_type;
1628     }
1629   else
1630     {
1631       TYPE_MAIN_VARIANT (new_type) = new_type;
1632       TYPE_NEXT_VARIANT (new_type) = NULL;
1633     }
1634
1635   TREE_TYPE (fndecl) = new_type;
1636   if (otypes)
1637     VEC_free (tree, heap, otypes);
1638   VEC_free (tree, heap, oparms);
1639 }
1640
1641 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1642    If this is a directly recursive call, CS must be NULL.  Otherwise it must
1643    contain the corresponding call graph edge.  */
1644
1645 void
1646 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
1647                            ipa_parm_adjustment_vec adjustments)
1648 {
1649   VEC(tree, heap) *vargs;
1650   gimple new_stmt;
1651   gimple_stmt_iterator gsi;
1652   tree callee_decl;
1653   int i, len;
1654
1655   len = VEC_length (ipa_parm_adjustment_t, adjustments);
1656   vargs = VEC_alloc (tree, heap, len);
1657
1658   gsi = gsi_for_stmt (stmt);
1659   for (i = 0; i < len; i++)
1660     {
1661       struct ipa_parm_adjustment *adj;
1662
1663       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1664
1665       if (adj->copy_param)
1666         {
1667           tree arg = gimple_call_arg (stmt, adj->base_index);
1668
1669           VEC_quick_push (tree, vargs, arg);
1670         }
1671       else if (!adj->remove_param)
1672         {
1673           tree expr, orig_expr;
1674           bool allow_ptr, repl_found;
1675
1676           orig_expr = expr = gimple_call_arg (stmt, adj->base_index);
1677           if (TREE_CODE (expr) == ADDR_EXPR)
1678             {
1679               allow_ptr = false;
1680               expr = TREE_OPERAND (expr, 0);
1681             }
1682           else
1683             allow_ptr = true;
1684
1685           repl_found = build_ref_for_offset (&expr, TREE_TYPE (expr),
1686                                              adj->offset, adj->type,
1687                                              allow_ptr);
1688           if (repl_found)
1689             {
1690               if (adj->by_ref)
1691                 expr = build_fold_addr_expr (expr);
1692             }
1693           else
1694             {
1695               tree ptrtype = build_pointer_type (adj->type);
1696               expr = orig_expr;
1697               if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1698                 expr = build_fold_addr_expr (expr);
1699               if (!useless_type_conversion_p (ptrtype, TREE_TYPE (expr)))
1700                 expr = fold_convert (ptrtype, expr);
1701               expr = fold_build2 (POINTER_PLUS_EXPR, ptrtype, expr,
1702                                   build_int_cst (sizetype,
1703                                                  adj->offset / BITS_PER_UNIT));
1704               if (!adj->by_ref)
1705                 expr = fold_build1 (INDIRECT_REF, adj->type, expr);
1706             }
1707           expr = force_gimple_operand_gsi (&gsi, expr,
1708                                            adj->by_ref
1709                                            || is_gimple_reg_type (adj->type),
1710                                            NULL, true, GSI_SAME_STMT);
1711           VEC_quick_push (tree, vargs, expr);
1712         }
1713     }
1714
1715   if (dump_file && (dump_flags & TDF_DETAILS))
1716     {
1717       fprintf (dump_file, "replacing stmt:");
1718       print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1719     }
1720
1721   callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
1722   new_stmt = gimple_build_call_vec (callee_decl, vargs);
1723   VEC_free (tree, heap, vargs);
1724   if (gimple_call_lhs (stmt))
1725     gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
1726
1727   gimple_set_block (new_stmt, gimple_block (stmt));
1728   if (gimple_has_location (stmt))
1729     gimple_set_location (new_stmt, gimple_location (stmt));
1730   gimple_call_copy_flags (new_stmt, stmt);
1731   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1732
1733   if (dump_file && (dump_flags & TDF_DETAILS))
1734     {
1735       fprintf (dump_file, "with stmt:");
1736       print_gimple_stmt (dump_file, new_stmt, 0, 0);
1737       fprintf (dump_file, "\n");
1738     }
1739   gsi_replace (&gsi, new_stmt, true);
1740   if (cs)
1741     cgraph_set_call_stmt (cs, new_stmt);
1742   update_ssa (TODO_update_ssa);
1743   free_dominance_info (CDI_DOMINATORS);
1744 }
1745
1746 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once.  */
1747
1748 static bool
1749 index_in_adjustments_multiple_times_p (int base_index,
1750                                        ipa_parm_adjustment_vec adjustments)
1751 {
1752   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1753   bool one = false;
1754
1755   for (i = 0; i < len; i++)
1756     {
1757       struct ipa_parm_adjustment *adj;
1758       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1759
1760       if (adj->base_index == base_index)
1761         {
1762           if (one)
1763             return true;
1764           else
1765             one = true;
1766         }
1767     }
1768   return false;
1769 }
1770
1771
1772 /* Return adjustments that should have the same effect on function parameters
1773    and call arguments as if they were first changed according to adjustments in
1774    INNER and then by adjustments in OUTER.  */
1775
1776 ipa_parm_adjustment_vec
1777 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
1778                          ipa_parm_adjustment_vec outer)
1779 {
1780   int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
1781   int inlen = VEC_length (ipa_parm_adjustment_t, inner);
1782   int removals = 0;
1783   ipa_parm_adjustment_vec adjustments, tmp;
1784
1785   tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
1786   for (i = 0; i < inlen; i++)
1787     {
1788       struct ipa_parm_adjustment *n;
1789       n = VEC_index (ipa_parm_adjustment_t, inner, i);
1790
1791       if (n->remove_param)
1792         removals++;
1793       else
1794         VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
1795     }
1796
1797   adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
1798   for (i = 0; i < outlen; i++)
1799     {
1800       struct ipa_parm_adjustment *r;
1801       struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
1802                                                    outer, i);
1803       struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
1804                                                   out->base_index);
1805
1806       gcc_assert (!in->remove_param);
1807       if (out->remove_param)
1808         {
1809           if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
1810             {
1811               r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1812               memset (r, 0, sizeof (*r));
1813               r->remove_param = true;
1814             }
1815           continue;
1816         }
1817
1818       r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1819       memset (r, 0, sizeof (*r));
1820       r->base_index = in->base_index;
1821       r->type = out->type;
1822
1823       /* FIXME:  Create nonlocal value too.  */
1824
1825       if (in->copy_param && out->copy_param)
1826         r->copy_param = true;
1827       else if (in->copy_param)
1828         r->offset = out->offset;
1829       else if (out->copy_param)
1830         r->offset = in->offset;
1831       else
1832         r->offset = in->offset + out->offset;
1833     }
1834
1835   for (i = 0; i < inlen; i++)
1836     {
1837       struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
1838                                                  inner, i);
1839
1840       if (n->remove_param)
1841         VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
1842     }
1843
1844   VEC_free (ipa_parm_adjustment_t, heap, tmp);
1845   return adjustments;
1846 }
1847
1848 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1849    friendly way, assuming they are meant to be applied to FNDECL.  */
1850
1851 void
1852 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
1853                             tree fndecl)
1854 {
1855   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1856   bool first = true;
1857   VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
1858
1859   fprintf (file, "IPA param adjustments: ");
1860   for (i = 0; i < len; i++)
1861     {
1862       struct ipa_parm_adjustment *adj;
1863       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1864
1865       if (!first)
1866         fprintf (file, "                 ");
1867       else
1868         first = false;
1869
1870       fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
1871       print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
1872       if (adj->base)
1873         {
1874           fprintf (file, ", base: ");
1875           print_generic_expr (file, adj->base, 0);
1876         }
1877       if (adj->reduction)
1878         {
1879           fprintf (file, ", reduction: ");
1880           print_generic_expr (file, adj->reduction, 0);
1881         }
1882       if (adj->new_ssa_base)
1883         {
1884           fprintf (file, ", new_ssa_base: ");
1885           print_generic_expr (file, adj->new_ssa_base, 0);
1886         }
1887
1888       if (adj->copy_param)
1889         fprintf (file, ", copy_param");
1890       else if (adj->remove_param)
1891         fprintf (file, ", remove_param");
1892       else
1893         fprintf (file, ", offset %li", (long) adj->offset);
1894       if (adj->by_ref)
1895         fprintf (file, ", by_ref");
1896       print_node_brief (file, ", type: ", adj->type, 0);
1897       fprintf (file, "\n");
1898     }
1899   VEC_free (tree, heap, parms);
1900 }
1901
1902 /* Stream out jump function JUMP_FUNC to OB.  */
1903
1904 static void
1905 ipa_write_jump_function (struct output_block *ob,
1906                          struct ipa_jump_func *jump_func)
1907 {
1908   lto_output_uleb128_stream (ob->main_stream,
1909                              jump_func->type);
1910
1911   switch (jump_func->type)
1912     {
1913     case IPA_JF_UNKNOWN:
1914       break;
1915     case IPA_JF_CONST:
1916       lto_output_tree (ob, jump_func->value.constant, true);
1917       break;
1918     case IPA_JF_PASS_THROUGH:
1919       lto_output_tree (ob, jump_func->value.pass_through.operand, true);
1920       lto_output_uleb128_stream (ob->main_stream,
1921                                  jump_func->value.pass_through.formal_id);
1922       lto_output_uleb128_stream (ob->main_stream,
1923                                  jump_func->value.pass_through.operation);
1924       break;
1925     case IPA_JF_ANCESTOR:
1926       lto_output_uleb128_stream (ob->main_stream,
1927                                  jump_func->value.ancestor.offset);
1928       lto_output_tree (ob, jump_func->value.ancestor.type, true);
1929       lto_output_uleb128_stream (ob->main_stream,
1930                                  jump_func->value.ancestor.formal_id);
1931       break;
1932     case IPA_JF_CONST_MEMBER_PTR:
1933       lto_output_tree (ob, jump_func->value.member_cst.pfn, true);
1934       lto_output_tree (ob, jump_func->value.member_cst.delta, false);
1935       break;
1936     }
1937 }
1938
1939 /* Read in jump function JUMP_FUNC from IB.  */
1940
1941 static void
1942 ipa_read_jump_function (struct lto_input_block *ib,
1943                         struct ipa_jump_func *jump_func,
1944                         struct data_in *data_in)
1945 {
1946   jump_func->type = (enum jump_func_type) lto_input_uleb128 (ib);
1947
1948   switch (jump_func->type)
1949     {
1950     case IPA_JF_UNKNOWN:
1951       break;
1952     case IPA_JF_CONST:
1953       jump_func->value.constant = lto_input_tree (ib, data_in);
1954       break;
1955     case IPA_JF_PASS_THROUGH:
1956       jump_func->value.pass_through.operand = lto_input_tree (ib, data_in);
1957       jump_func->value.pass_through.formal_id = lto_input_uleb128 (ib);
1958       jump_func->value.pass_through.operation = (enum tree_code) lto_input_uleb128 (ib);
1959       break;
1960     case IPA_JF_ANCESTOR:
1961       jump_func->value.ancestor.offset = lto_input_uleb128 (ib);
1962       jump_func->value.ancestor.type = lto_input_tree (ib, data_in);
1963       jump_func->value.ancestor.formal_id = lto_input_uleb128 (ib);
1964       break;
1965     case IPA_JF_CONST_MEMBER_PTR:
1966       jump_func->value.member_cst.pfn = lto_input_tree (ib, data_in);
1967       jump_func->value.member_cst.delta = lto_input_tree (ib, data_in);
1968       break;
1969     }
1970 }
1971
1972 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
1973    relevant to indirect inlining to OB.  */
1974
1975 static void
1976 ipa_write_indirect_edge_info (struct output_block *ob,
1977                               struct cgraph_edge *cs)
1978 {
1979   struct cgraph_indirect_call_info *ii = cs->indirect_info;
1980
1981   lto_output_sleb128_stream (ob->main_stream, ii->param_index);
1982 }
1983
1984 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
1985    relevant to indirect inlining from IB.  */
1986
1987 static void
1988 ipa_read_indirect_edge_info (struct lto_input_block *ib,
1989                              struct data_in *data_in ATTRIBUTE_UNUSED,
1990                              struct cgraph_edge *cs)
1991 {
1992   struct cgraph_indirect_call_info *ii = cs->indirect_info;
1993
1994   ii->param_index = (int) lto_input_sleb128 (ib);
1995 }
1996
1997 /* Stream out NODE info to OB.  */
1998
1999 static void
2000 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
2001 {
2002   int node_ref;
2003   lto_cgraph_encoder_t encoder;
2004   struct ipa_node_params *info = IPA_NODE_REF (node);
2005   int j;
2006   struct cgraph_edge *e;
2007   struct bitpack_d *bp;
2008
2009   encoder = ob->decl_state->cgraph_node_encoder;
2010   node_ref = lto_cgraph_encoder_encode (encoder, node);
2011   lto_output_uleb128_stream (ob->main_stream, node_ref);
2012
2013   bp = bitpack_create ();
2014   bp_pack_value (bp, info->called_with_var_arguments, 1);
2015   bp_pack_value (bp, info->uses_analysis_done, 1);
2016   gcc_assert (info->modification_analysis_done
2017               || ipa_get_param_count (info) == 0);
2018   gcc_assert (!info->node_enqueued);
2019   gcc_assert (!info->ipcp_orig_node);
2020   for (j = 0; j < ipa_get_param_count (info); j++)
2021     bp_pack_value (bp, info->params[j].modified, 1);
2022   lto_output_bitpack (ob->main_stream, bp);
2023   bitpack_delete (bp);
2024   for (e = node->callees; e; e = e->next_callee)
2025     {
2026       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2027
2028       lto_output_uleb128_stream (ob->main_stream,
2029                                  ipa_get_cs_argument_count (args));
2030       for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2031         ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2032     }
2033   for (e = node->indirect_calls; e; e = e->next_callee)
2034     ipa_write_indirect_edge_info (ob, e);
2035 }
2036
2037 /* Srtream in NODE info from IB.  */
2038
2039 static void
2040 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2041                     struct data_in *data_in)
2042 {
2043   struct ipa_node_params *info = IPA_NODE_REF (node);
2044   int k;
2045   struct cgraph_edge *e;
2046   struct bitpack_d *bp;
2047
2048   ipa_initialize_node_params (node);
2049
2050   bp = lto_input_bitpack (ib);
2051   info->called_with_var_arguments = bp_unpack_value (bp, 1);
2052   info->uses_analysis_done = bp_unpack_value (bp, 1);
2053   if (ipa_get_param_count (info) != 0)
2054     {
2055       info->modification_analysis_done = true;
2056       info->uses_analysis_done = true;
2057     }
2058   info->node_enqueued = false;
2059   for (k = 0; k < ipa_get_param_count (info); k++)
2060     info->params[k].modified = bp_unpack_value (bp, 1);
2061   bitpack_delete (bp);
2062   for (e = node->callees; e; e = e->next_callee)
2063     {
2064       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2065       int count = lto_input_uleb128 (ib);
2066
2067       ipa_set_cs_argument_count (args, count);
2068       if (!count)
2069         continue;
2070
2071       args->jump_functions = GGC_CNEWVEC (struct ipa_jump_func,
2072                                           ipa_get_cs_argument_count (args));
2073       for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2074         ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2075     }
2076   for (e = node->indirect_calls; e; e = e->next_callee)
2077     ipa_read_indirect_edge_info (ib, data_in, e);
2078 }
2079
2080 /* Write jump functions for nodes in SET.  */
2081
2082 void
2083 ipa_prop_write_jump_functions (cgraph_node_set set)
2084 {
2085   struct cgraph_node *node;
2086   struct output_block *ob = create_output_block (LTO_section_jump_functions);
2087   unsigned int count = 0;
2088   cgraph_node_set_iterator csi;
2089
2090   ob->cgraph_node = NULL;
2091
2092   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2093     {
2094       node = csi_node (csi);
2095       if (node->analyzed && IPA_NODE_REF (node) != NULL)
2096         count++;
2097     }
2098
2099   lto_output_uleb128_stream (ob->main_stream, count);
2100
2101   /* Process all of the functions.  */
2102   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2103     {
2104       node = csi_node (csi);
2105       if (node->analyzed && IPA_NODE_REF (node) != NULL)
2106         ipa_write_node_info (ob, node);
2107     }
2108   lto_output_1_stream (ob->main_stream, 0);
2109   produce_asm (ob, NULL);
2110   destroy_output_block (ob);
2111 }
2112
2113 /* Read section in file FILE_DATA of length LEN with data DATA.  */
2114
2115 static void
2116 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2117                        size_t len)
2118 {
2119   const struct lto_function_header *header =
2120     (const struct lto_function_header *) data;
2121   const int32_t cfg_offset = sizeof (struct lto_function_header);
2122   const int32_t main_offset = cfg_offset + header->cfg_size;
2123   const int32_t string_offset = main_offset + header->main_size;
2124   struct data_in *data_in;
2125   struct lto_input_block ib_main;
2126   unsigned int i;
2127   unsigned int count;
2128
2129   LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2130                         header->main_size);
2131
2132   data_in =
2133     lto_data_in_create (file_data, (const char *) data + string_offset,
2134                         header->string_size, NULL);
2135   count = lto_input_uleb128 (&ib_main);
2136
2137   for (i = 0; i < count; i++)
2138     {
2139       unsigned int index;
2140       struct cgraph_node *node;
2141       lto_cgraph_encoder_t encoder;
2142
2143       index = lto_input_uleb128 (&ib_main);
2144       encoder = file_data->cgraph_node_encoder;
2145       node = lto_cgraph_encoder_deref (encoder, index);
2146       ipa_read_node_info (&ib_main, node, data_in);
2147     }
2148   lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2149                          len);
2150   lto_data_in_delete (data_in);
2151 }
2152
2153 /* Read ipcp jump functions.  */
2154
2155 void
2156 ipa_prop_read_jump_functions (void)
2157 {
2158   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2159   struct lto_file_decl_data *file_data;
2160   unsigned int j = 0;
2161
2162   ipa_check_create_node_params ();
2163   ipa_check_create_edge_args ();
2164   ipa_register_cgraph_hooks ();
2165
2166   while ((file_data = file_data_vec[j++]))
2167     {
2168       size_t len;
2169       const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2170
2171       if (data)
2172         ipa_prop_read_section (file_data, data, len);
2173     }
2174 }
2175
2176 /* After merging units, we can get mismatch in argument counts.
2177    Also decl merging might've rendered parameter lists obsolette.
2178    Also compute called_with_variable_arg info.  */
2179
2180 void
2181 ipa_update_after_lto_read (void)
2182 {
2183   struct cgraph_node *node;
2184   struct cgraph_edge *cs;
2185
2186   ipa_check_create_node_params ();
2187   ipa_check_create_edge_args ();
2188
2189   for (node = cgraph_nodes; node; node = node->next)
2190     if (node->analyzed)
2191       ipa_initialize_node_params (node);
2192
2193   for (node = cgraph_nodes; node; node = node->next)
2194     if (node->analyzed)
2195       for (cs = node->callees; cs; cs = cs->next_callee)
2196         {
2197           if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
2198               != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
2199             ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
2200         }
2201 }