OSDN Git Service

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