OSDN Git Service

Daily bump.
[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   ipa_free_node_params_substructures (IPA_NODE_REF (node));
1264 }
1265
1266 /* Helper function to duplicate an array of size N that is at SRC and store a
1267    pointer to it to DST.  Nothing is done if SRC is NULL.  */
1268
1269 static void *
1270 duplicate_array (void *src, size_t n)
1271 {
1272   void *p;
1273
1274   if (!src)
1275     return NULL;
1276
1277   p = xmalloc (n);
1278   memcpy (p, src, n);
1279   return p;
1280 }
1281
1282 /* Like duplicate_array byt in GGC memory.  */
1283
1284 static void *
1285 duplicate_ggc_array (void *src, size_t n)
1286 {
1287   void *p;
1288
1289   if (!src)
1290     return NULL;
1291
1292   p = ggc_alloc (n);
1293   memcpy (p, src, n);
1294   return p;
1295 }
1296
1297 /* Hook that is called by cgraph.c when a node is duplicated.  */
1298
1299 static void
1300 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1301                            __attribute__((unused)) void *data)
1302 {
1303   struct ipa_edge_args *old_args, *new_args;
1304   int arg_count;
1305
1306   ipa_check_create_edge_args ();
1307
1308   old_args = IPA_EDGE_REF (src);
1309   new_args = IPA_EDGE_REF (dst);
1310
1311   arg_count = ipa_get_cs_argument_count (old_args);
1312   ipa_set_cs_argument_count (new_args, arg_count);
1313   new_args->jump_functions = (struct ipa_jump_func *)
1314     duplicate_ggc_array (old_args->jump_functions,
1315                          sizeof (struct ipa_jump_func) * arg_count);
1316 }
1317
1318 /* Hook that is called by cgraph.c when a node is duplicated.  */
1319
1320 static void
1321 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1322                            __attribute__((unused)) void *data)
1323 {
1324   struct ipa_node_params *old_info, *new_info;
1325   struct ipa_param_call_note *note;
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   for (note = old_info->param_calls; note; note = note->next)
1341     {
1342       struct ipa_param_call_note *nn;
1343
1344       nn = (struct ipa_param_call_note *)
1345         xcalloc (1, sizeof (struct ipa_param_call_note));
1346       memcpy (nn, note, sizeof (struct ipa_param_call_note));
1347       nn->next = new_info->param_calls;
1348       new_info->param_calls = nn;
1349     }
1350 }
1351
1352 /* Register our cgraph hooks if they are not already there.  */
1353
1354 void
1355 ipa_register_cgraph_hooks (void)
1356 {
1357   if (!edge_removal_hook_holder)
1358     edge_removal_hook_holder =
1359       cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1360   if (!node_removal_hook_holder)
1361     node_removal_hook_holder =
1362       cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1363   if (!edge_duplication_hook_holder)
1364     edge_duplication_hook_holder =
1365       cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1366   if (!node_duplication_hook_holder)
1367     node_duplication_hook_holder =
1368       cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1369 }
1370
1371 /* Unregister our cgraph hooks if they are not already there.  */
1372
1373 static void
1374 ipa_unregister_cgraph_hooks (void)
1375 {
1376   cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1377   edge_removal_hook_holder = NULL;
1378   cgraph_remove_node_removal_hook (node_removal_hook_holder);
1379   node_removal_hook_holder = NULL;
1380   cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1381   edge_duplication_hook_holder = NULL;
1382   cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1383   node_duplication_hook_holder = NULL;
1384 }
1385
1386 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1387    longer needed after ipa-cp.  */
1388
1389 void
1390 free_all_ipa_structures_after_ipa_cp (void)
1391 {
1392   if (!flag_indirect_inlining)
1393     {
1394       ipa_free_all_edge_args ();
1395       ipa_free_all_node_params ();
1396       ipa_unregister_cgraph_hooks ();
1397     }
1398 }
1399
1400 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1401    longer needed after indirect inlining.  */
1402
1403 void
1404 free_all_ipa_structures_after_iinln (void)
1405 {
1406   ipa_free_all_edge_args ();
1407   ipa_free_all_node_params ();
1408   ipa_unregister_cgraph_hooks ();
1409 }
1410
1411 /* Print ipa_tree_map data structures of all functions in the
1412    callgraph to F.  */
1413
1414 void
1415 ipa_print_node_params (FILE * f, struct cgraph_node *node)
1416 {
1417   int i, count;
1418   tree temp;
1419   struct ipa_node_params *info;
1420
1421   if (!node->analyzed)
1422     return;
1423   info = IPA_NODE_REF (node);
1424   fprintf (f, "  function  %s Trees :: \n", cgraph_node_name (node));
1425   count = ipa_get_param_count (info);
1426   for (i = 0; i < count; i++)
1427     {
1428       temp = ipa_get_param (info, i);
1429       if (TREE_CODE (temp) == PARM_DECL)
1430         fprintf (f, "    param %d : %s", i,
1431                  (DECL_NAME (temp)
1432                   ? (*lang_hooks.decl_printable_name) (temp, 2)
1433                   : "(unnamed)"));
1434       if (ipa_is_param_modified (info, i))
1435         fprintf (f, " modified");
1436       fprintf (f, "\n");
1437     }
1438 }
1439
1440 /* Print ipa_tree_map data structures of all functions in the
1441    callgraph to F.  */
1442
1443 void
1444 ipa_print_all_params (FILE * f)
1445 {
1446   struct cgraph_node *node;
1447
1448   fprintf (f, "\nFunction parameters:\n");
1449   for (node = cgraph_nodes; node; node = node->next)
1450     ipa_print_node_params (f, node);
1451 }
1452
1453 /* Return a heap allocated vector containing formal parameters of FNDECL.  */
1454
1455 VEC(tree, heap) *
1456 ipa_get_vector_of_formal_parms (tree fndecl)
1457 {
1458   VEC(tree, heap) *args;
1459   int count;
1460   tree parm;
1461
1462   count = count_formal_params_1 (fndecl);
1463   args = VEC_alloc (tree, heap, count);
1464   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
1465     VEC_quick_push (tree, args, parm);
1466
1467   return args;
1468 }
1469
1470 /* Return a heap allocated vector containing types of formal parameters of
1471    function type FNTYPE.  */
1472
1473 static inline VEC(tree, heap) *
1474 get_vector_of_formal_parm_types (tree fntype)
1475 {
1476   VEC(tree, heap) *types;
1477   int count = 0;
1478   tree t;
1479
1480   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1481     count++;
1482
1483   types = VEC_alloc (tree, heap, count);
1484   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1485     VEC_quick_push (tree, types, TREE_VALUE (t));
1486
1487   return types;
1488 }
1489
1490 /* Modify the function declaration FNDECL and its type according to the plan in
1491    ADJUSTMENTS.  It also sets base fields of individual adjustments structures
1492    to reflect the actual parameters being modified which are determined by the
1493    base_index field.  */
1494
1495 void
1496 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
1497                               const char *synth_parm_prefix)
1498 {
1499   VEC(tree, heap) *oparms, *otypes;
1500   tree orig_type, new_type = NULL;
1501   tree old_arg_types, t, new_arg_types = NULL;
1502   tree parm, *link = &DECL_ARGUMENTS (fndecl);
1503   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1504   tree new_reversed = NULL;
1505   bool care_for_types, last_parm_void;
1506
1507   if (!synth_parm_prefix)
1508     synth_parm_prefix = "SYNTH";
1509
1510   oparms = ipa_get_vector_of_formal_parms (fndecl);
1511   orig_type = TREE_TYPE (fndecl);
1512   old_arg_types = TYPE_ARG_TYPES (orig_type);
1513
1514   /* The following test is an ugly hack, some functions simply don't have any
1515      arguments in their type.  This is probably a bug but well... */
1516   care_for_types = (old_arg_types != NULL_TREE);
1517   if (care_for_types)
1518     {
1519       last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
1520                         == void_type_node);
1521       otypes = get_vector_of_formal_parm_types (orig_type);
1522       if (last_parm_void)
1523         gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
1524       else
1525         gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
1526     }
1527   else
1528     {
1529       last_parm_void = false;
1530       otypes = NULL;
1531     }
1532
1533   for (i = 0; i < len; i++)
1534     {
1535       struct ipa_parm_adjustment *adj;
1536       gcc_assert (link);
1537
1538       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1539       parm = VEC_index (tree, oparms, adj->base_index);
1540       adj->base = parm;
1541
1542       if (adj->copy_param)
1543         {
1544           if (care_for_types)
1545             new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
1546                                                              adj->base_index),
1547                                        new_arg_types);
1548           *link = parm;
1549           link = &TREE_CHAIN (parm);
1550         }
1551       else if (!adj->remove_param)
1552         {
1553           tree new_parm;
1554           tree ptype;
1555
1556           if (adj->by_ref)
1557             ptype = build_pointer_type (adj->type);
1558           else
1559             ptype = adj->type;
1560
1561           if (care_for_types)
1562             new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
1563
1564           new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1565                                  ptype);
1566           DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
1567
1568           DECL_ARTIFICIAL (new_parm) = 1;
1569           DECL_ARG_TYPE (new_parm) = ptype;
1570           DECL_CONTEXT (new_parm) = fndecl;
1571           TREE_USED (new_parm) = 1;
1572           DECL_IGNORED_P (new_parm) = 1;
1573           layout_decl (new_parm, 0);
1574
1575           add_referenced_var (new_parm);
1576           mark_sym_for_renaming (new_parm);
1577           adj->base = parm;
1578           adj->reduction = new_parm;
1579
1580           *link = new_parm;
1581
1582           link = &TREE_CHAIN (new_parm);
1583         }
1584     }
1585
1586   *link = NULL_TREE;
1587
1588   if (care_for_types)
1589     {
1590       new_reversed = nreverse (new_arg_types);
1591       if (last_parm_void)
1592         {
1593           if (new_reversed)
1594             TREE_CHAIN (new_arg_types) = void_list_node;
1595           else
1596             new_reversed = void_list_node;
1597         }
1598     }
1599
1600   /* Use copy_node to preserve as much as possible from original type
1601      (debug info, attribute lists etc.)
1602      Exception is METHOD_TYPEs must have THIS argument.
1603      When we are asked to remove it, we need to build new FUNCTION_TYPE
1604      instead.  */
1605   if (TREE_CODE (orig_type) != METHOD_TYPE
1606        || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
1607          && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
1608     {
1609       new_type = copy_node (orig_type);
1610       TYPE_ARG_TYPES (new_type) = new_reversed;
1611     }
1612   else
1613     {
1614       new_type
1615         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
1616                                                          new_reversed));
1617       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
1618       DECL_VINDEX (fndecl) = NULL_TREE;
1619     }
1620
1621   /* This is a new type, not a copy of an old type.  Need to reassociate
1622      variants.  We can handle everything except the main variant lazily.  */
1623   t = TYPE_MAIN_VARIANT (orig_type);
1624   if (orig_type != t)
1625     {
1626       TYPE_MAIN_VARIANT (new_type) = t;
1627       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
1628       TYPE_NEXT_VARIANT (t) = new_type;
1629     }
1630   else
1631     {
1632       TYPE_MAIN_VARIANT (new_type) = new_type;
1633       TYPE_NEXT_VARIANT (new_type) = NULL;
1634     }
1635
1636   TREE_TYPE (fndecl) = new_type;
1637   if (otypes)
1638     VEC_free (tree, heap, otypes);
1639   VEC_free (tree, heap, oparms);
1640 }
1641
1642 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1643    If this is a directly recursive call, CS must be NULL.  Otherwise it must
1644    contain the corresponding call graph edge.  */
1645
1646 void
1647 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
1648                            ipa_parm_adjustment_vec adjustments)
1649 {
1650   VEC(tree, heap) *vargs;
1651   gimple new_stmt;
1652   gimple_stmt_iterator gsi;
1653   tree callee_decl;
1654   int i, len;
1655
1656   len = VEC_length (ipa_parm_adjustment_t, adjustments);
1657   vargs = VEC_alloc (tree, heap, len);
1658
1659   gsi = gsi_for_stmt (stmt);
1660   for (i = 0; i < len; i++)
1661     {
1662       struct ipa_parm_adjustment *adj;
1663
1664       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1665
1666       if (adj->copy_param)
1667         {
1668           tree arg = gimple_call_arg (stmt, adj->base_index);
1669
1670           VEC_quick_push (tree, vargs, arg);
1671         }
1672       else if (!adj->remove_param)
1673         {
1674           tree expr, orig_expr;
1675           bool allow_ptr, repl_found;
1676
1677           orig_expr = expr = gimple_call_arg (stmt, adj->base_index);
1678           if (TREE_CODE (expr) == ADDR_EXPR)
1679             {
1680               allow_ptr = false;
1681               expr = TREE_OPERAND (expr, 0);
1682             }
1683           else
1684             allow_ptr = true;
1685
1686           repl_found = build_ref_for_offset (&expr, TREE_TYPE (expr),
1687                                              adj->offset, adj->type,
1688                                              allow_ptr);
1689           if (repl_found)
1690             {
1691               if (adj->by_ref)
1692                 expr = build_fold_addr_expr (expr);
1693             }
1694           else
1695             {
1696               tree ptrtype = build_pointer_type (adj->type);
1697               expr = orig_expr;
1698               if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1699                 expr = build_fold_addr_expr (expr);
1700               if (!useless_type_conversion_p (ptrtype, TREE_TYPE (expr)))
1701                 expr = fold_convert (ptrtype, expr);
1702               expr = fold_build2 (POINTER_PLUS_EXPR, ptrtype, expr,
1703                                   build_int_cst (size_type_node,
1704                                                  adj->offset / BITS_PER_UNIT));
1705               if (!adj->by_ref)
1706                 expr = fold_build1 (INDIRECT_REF, adj->type, expr);
1707             }
1708           expr = force_gimple_operand_gsi (&gsi, expr,
1709                                            adj->by_ref
1710                                            || is_gimple_reg_type (adj->type),
1711                                            NULL, true, GSI_SAME_STMT);
1712           VEC_quick_push (tree, vargs, expr);
1713         }
1714     }
1715
1716   if (dump_file && (dump_flags & TDF_DETAILS))
1717     {
1718       fprintf (dump_file, "replacing stmt:");
1719       print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1720     }
1721
1722   callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
1723   new_stmt = gimple_build_call_vec (callee_decl, vargs);
1724   VEC_free (tree, heap, vargs);
1725   if (gimple_call_lhs (stmt))
1726     gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
1727
1728   gimple_set_block (new_stmt, gimple_block (stmt));
1729   if (gimple_has_location (stmt))
1730     gimple_set_location (new_stmt, gimple_location (stmt));
1731   gimple_call_copy_flags (new_stmt, stmt);
1732   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1733
1734   if (dump_file && (dump_flags & TDF_DETAILS))
1735     {
1736       fprintf (dump_file, "with stmt:");
1737       print_gimple_stmt (dump_file, new_stmt, 0, 0);
1738       fprintf (dump_file, "\n");
1739     }
1740   gsi_replace (&gsi, new_stmt, true);
1741   if (cs)
1742     cgraph_set_call_stmt (cs, new_stmt);
1743   update_ssa (TODO_update_ssa);
1744   free_dominance_info (CDI_DOMINATORS);
1745 }
1746
1747 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once.  */
1748
1749 static bool
1750 index_in_adjustments_multiple_times_p (int base_index,
1751                                        ipa_parm_adjustment_vec adjustments)
1752 {
1753   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1754   bool one = false;
1755
1756   for (i = 0; i < len; i++)
1757     {
1758       struct ipa_parm_adjustment *adj;
1759       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1760
1761       if (adj->base_index == base_index)
1762         {
1763           if (one)
1764             return true;
1765           else
1766             one = true;
1767         }
1768     }
1769   return false;
1770 }
1771
1772
1773 /* Return adjustments that should have the same effect on function parameters
1774    and call arguments as if they were first changed according to adjustments in
1775    INNER and then by adjustments in OUTER.  */
1776
1777 ipa_parm_adjustment_vec
1778 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
1779                          ipa_parm_adjustment_vec outer)
1780 {
1781   int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
1782   int inlen = VEC_length (ipa_parm_adjustment_t, inner);
1783   int removals = 0;
1784   ipa_parm_adjustment_vec adjustments, tmp;
1785
1786   tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
1787   for (i = 0; i < inlen; i++)
1788     {
1789       struct ipa_parm_adjustment *n;
1790       n = VEC_index (ipa_parm_adjustment_t, inner, i);
1791
1792       if (n->remove_param)
1793         removals++;
1794       else
1795         VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
1796     }
1797
1798   adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
1799   for (i = 0; i < outlen; i++)
1800     {
1801       struct ipa_parm_adjustment *r;
1802       struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
1803                                                    outer, i);
1804       struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
1805                                                   out->base_index);
1806
1807       gcc_assert (!in->remove_param);
1808       if (out->remove_param)
1809         {
1810           if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
1811             {
1812               r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1813               memset (r, 0, sizeof (*r));
1814               r->remove_param = true;
1815             }
1816           continue;
1817         }
1818
1819       r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1820       memset (r, 0, sizeof (*r));
1821       r->base_index = in->base_index;
1822       r->type = out->type;
1823
1824       /* FIXME:  Create nonlocal value too.  */
1825
1826       if (in->copy_param && out->copy_param)
1827         r->copy_param = true;
1828       else if (in->copy_param)
1829         r->offset = out->offset;
1830       else if (out->copy_param)
1831         r->offset = in->offset;
1832       else
1833         r->offset = in->offset + out->offset;
1834     }
1835
1836   for (i = 0; i < inlen; i++)
1837     {
1838       struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
1839                                                  inner, i);
1840
1841       if (n->remove_param)
1842         VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
1843     }
1844
1845   VEC_free (ipa_parm_adjustment_t, heap, tmp);
1846   return adjustments;
1847 }
1848
1849 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1850    friendly way, assuming they are meant to be applied to FNDECL.  */
1851
1852 void
1853 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
1854                             tree fndecl)
1855 {
1856   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1857   bool first = true;
1858   VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
1859
1860   fprintf (file, "IPA param adjustments: ");
1861   for (i = 0; i < len; i++)
1862     {
1863       struct ipa_parm_adjustment *adj;
1864       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1865
1866       if (!first)
1867         fprintf (file, "                 ");
1868       else
1869         first = false;
1870
1871       fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
1872       print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
1873       if (adj->base)
1874         {
1875           fprintf (file, ", base: ");
1876           print_generic_expr (file, adj->base, 0);
1877         }
1878       if (adj->reduction)
1879         {
1880           fprintf (file, ", reduction: ");
1881           print_generic_expr (file, adj->reduction, 0);
1882         }
1883       if (adj->new_ssa_base)
1884         {
1885           fprintf (file, ", new_ssa_base: ");
1886           print_generic_expr (file, adj->new_ssa_base, 0);
1887         }
1888
1889       if (adj->copy_param)
1890         fprintf (file, ", copy_param");
1891       else if (adj->remove_param)
1892         fprintf (file, ", remove_param");
1893       else
1894         fprintf (file, ", offset %li", (long) adj->offset);
1895       if (adj->by_ref)
1896         fprintf (file, ", by_ref");
1897       print_node_brief (file, ", type: ", adj->type, 0);
1898       fprintf (file, "\n");
1899     }
1900   VEC_free (tree, heap, parms);
1901 }
1902
1903 /* Stream out jump function JUMP_FUNC to OB.  */
1904
1905 static void
1906 ipa_write_jump_function (struct output_block *ob,
1907                          struct ipa_jump_func *jump_func)
1908 {
1909   lto_output_uleb128_stream (ob->main_stream,
1910                              jump_func->type);
1911
1912   switch (jump_func->type)
1913     {
1914     case IPA_JF_UNKNOWN:
1915       break;
1916     case IPA_JF_CONST:
1917       lto_output_tree (ob, jump_func->value.constant, true);
1918       break;
1919     case IPA_JF_PASS_THROUGH:
1920       lto_output_tree (ob, jump_func->value.pass_through.operand, true);
1921       lto_output_uleb128_stream (ob->main_stream,
1922                                  jump_func->value.pass_through.formal_id);
1923       lto_output_uleb128_stream (ob->main_stream,
1924                                  jump_func->value.pass_through.operation);
1925       break;
1926     case IPA_JF_ANCESTOR:
1927       lto_output_uleb128_stream (ob->main_stream,
1928                                  jump_func->value.ancestor.offset);
1929       lto_output_tree (ob, jump_func->value.ancestor.type, true);
1930       lto_output_uleb128_stream (ob->main_stream,
1931                                  jump_func->value.ancestor.formal_id);
1932       break;
1933     case IPA_JF_CONST_MEMBER_PTR:
1934       lto_output_tree (ob, jump_func->value.member_cst.pfn, true);
1935       lto_output_tree (ob, jump_func->value.member_cst.delta, false);
1936       break;
1937     }
1938 }
1939
1940 /* Read in jump function JUMP_FUNC from IB.  */
1941
1942 static void
1943 ipa_read_jump_function (struct lto_input_block *ib,
1944                         struct ipa_jump_func *jump_func,
1945                         struct data_in *data_in)
1946 {
1947   jump_func->type = (enum jump_func_type) lto_input_uleb128 (ib);
1948
1949   switch (jump_func->type)
1950     {
1951     case IPA_JF_UNKNOWN:
1952       break;
1953     case IPA_JF_CONST:
1954       jump_func->value.constant = lto_input_tree (ib, data_in);
1955       break;
1956     case IPA_JF_PASS_THROUGH:
1957       jump_func->value.pass_through.operand = lto_input_tree (ib, data_in);
1958       jump_func->value.pass_through.formal_id = lto_input_uleb128 (ib);
1959       jump_func->value.pass_through.operation = (enum tree_code) lto_input_uleb128 (ib);
1960       break;
1961     case IPA_JF_ANCESTOR:
1962       jump_func->value.ancestor.offset = lto_input_uleb128 (ib);
1963       jump_func->value.ancestor.type = lto_input_tree (ib, data_in);
1964       jump_func->value.ancestor.formal_id = lto_input_uleb128 (ib);
1965       break;
1966     case IPA_JF_CONST_MEMBER_PTR:
1967       jump_func->value.member_cst.pfn = lto_input_tree (ib, data_in);
1968       jump_func->value.member_cst.delta = lto_input_tree (ib, data_in);
1969       break;
1970     }
1971 }
1972
1973 /* Stream out a parameter call note.  */
1974
1975 static void
1976 ipa_write_param_call_note (struct output_block *ob,
1977                            struct ipa_param_call_note *note)
1978 {
1979   gcc_assert (!note->processed);
1980   lto_output_uleb128_stream (ob->main_stream, gimple_uid (note->stmt));
1981   lto_output_sleb128_stream (ob->main_stream, note->formal_id);
1982   lto_output_sleb128_stream (ob->main_stream, note->count);
1983   lto_output_sleb128_stream (ob->main_stream, note->frequency);
1984   lto_output_sleb128_stream (ob->main_stream, note->loop_nest);
1985 }
1986
1987 /* Read in a parameter call note.  */
1988
1989 static void
1990 ipa_read_param_call_note (struct lto_input_block *ib,
1991                           struct ipa_node_params *info)
1992
1993 {
1994   struct ipa_param_call_note *note = XCNEW (struct ipa_param_call_note);
1995
1996   note->lto_stmt_uid = (unsigned int) lto_input_uleb128 (ib);
1997   note->formal_id = (int) lto_input_sleb128 (ib);
1998   note->count = (gcov_type) lto_input_sleb128 (ib);
1999   note->frequency = (int) lto_input_sleb128 (ib);
2000   note->loop_nest = (int) lto_input_sleb128 (ib);
2001
2002   note->next = info->param_calls;
2003   info->param_calls = note;
2004 }
2005
2006
2007 /* Stream out NODE info to OB.  */
2008
2009 static void
2010 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
2011 {
2012   int node_ref;
2013   lto_cgraph_encoder_t encoder;
2014   struct ipa_node_params *info = IPA_NODE_REF (node);
2015   int j;
2016   struct cgraph_edge *e;
2017   struct bitpack_d *bp;
2018   int note_count = 0;
2019   struct ipa_param_call_note *note;
2020
2021   encoder = ob->decl_state->cgraph_node_encoder;
2022   node_ref = lto_cgraph_encoder_encode (encoder, node);
2023   lto_output_uleb128_stream (ob->main_stream, node_ref);
2024
2025   bp = bitpack_create ();
2026   bp_pack_value (bp, info->called_with_var_arguments, 1);
2027   bp_pack_value (bp, info->uses_analysis_done, 1);
2028   gcc_assert (info->modification_analysis_done
2029               || ipa_get_param_count (info) == 0);
2030   gcc_assert (!info->node_enqueued);
2031   gcc_assert (!info->ipcp_orig_node);
2032   for (j = 0; j < ipa_get_param_count (info); j++)
2033     bp_pack_value (bp, info->params[j].modified, 1);
2034   lto_output_bitpack (ob->main_stream, bp);
2035   bitpack_delete (bp);
2036   for (e = node->callees; e; e = e->next_callee)
2037     {
2038       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2039
2040       lto_output_uleb128_stream (ob->main_stream,
2041                                  ipa_get_cs_argument_count (args));
2042       for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2043         ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2044     }
2045
2046   for (note = info->param_calls; note; note = note->next)
2047     note_count++;
2048   lto_output_uleb128_stream (ob->main_stream, note_count);
2049   for (note = info->param_calls; note; note = note->next)
2050     ipa_write_param_call_note (ob, note);
2051 }
2052
2053 /* Srtream in NODE info from IB.  */
2054
2055 static void
2056 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2057                     struct data_in *data_in)
2058 {
2059   struct ipa_node_params *info = IPA_NODE_REF (node);
2060   int k;
2061   struct cgraph_edge *e;
2062   struct bitpack_d *bp;
2063   int i, note_count;
2064
2065   ipa_initialize_node_params (node);
2066
2067   bp = lto_input_bitpack (ib);
2068   info->called_with_var_arguments = bp_unpack_value (bp, 1);
2069   info->uses_analysis_done = bp_unpack_value (bp, 1);
2070   if (ipa_get_param_count (info) != 0)
2071     {
2072       info->modification_analysis_done = true;
2073       info->uses_analysis_done = true;
2074     }
2075   info->node_enqueued = false;
2076   for (k = 0; k < ipa_get_param_count (info); k++)
2077     info->params[k].modified = bp_unpack_value (bp, 1);
2078   bitpack_delete (bp);
2079   for (e = node->callees; e; e = e->next_callee)
2080     {
2081       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2082       int count = lto_input_uleb128 (ib);
2083
2084       ipa_set_cs_argument_count (args, count);
2085       if (!count)
2086         continue;
2087
2088       args->jump_functions = GGC_CNEWVEC (struct ipa_jump_func,
2089                                           ipa_get_cs_argument_count (args));
2090       for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2091         ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2092     }
2093
2094   note_count = lto_input_uleb128 (ib);
2095   for (i = 0; i < note_count; i++)
2096     ipa_read_param_call_note (ib, info);
2097 }
2098
2099 /* Write jump functions for nodes in SET.  */
2100
2101 void
2102 ipa_prop_write_jump_functions (cgraph_node_set set)
2103 {
2104   struct cgraph_node *node;
2105   struct output_block *ob = create_output_block (LTO_section_jump_functions);
2106   unsigned int count = 0;
2107   cgraph_node_set_iterator csi;
2108
2109   ob->cgraph_node = NULL;
2110
2111   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2112     {
2113       node = csi_node (csi);
2114       if (node->analyzed && IPA_NODE_REF (node) != NULL)
2115         count++;
2116     }
2117
2118   lto_output_uleb128_stream (ob->main_stream, count);
2119
2120   /* Process all of the functions.  */
2121   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2122     {
2123       node = csi_node (csi);
2124       if (node->analyzed && IPA_NODE_REF (node) != NULL)
2125         ipa_write_node_info (ob, node);
2126     }
2127   lto_output_1_stream (ob->main_stream, 0);
2128   produce_asm (ob, NULL);
2129   destroy_output_block (ob);
2130 }
2131
2132 /* Read section in file FILE_DATA of length LEN with data DATA.  */
2133
2134 static void
2135 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2136                        size_t len)
2137 {
2138   const struct lto_function_header *header =
2139     (const struct lto_function_header *) data;
2140   const int32_t cfg_offset = sizeof (struct lto_function_header);
2141   const int32_t main_offset = cfg_offset + header->cfg_size;
2142   const int32_t string_offset = main_offset + header->main_size;
2143   struct data_in *data_in;
2144   struct lto_input_block ib_main;
2145   unsigned int i;
2146   unsigned int count;
2147
2148   LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2149                         header->main_size);
2150
2151   data_in =
2152     lto_data_in_create (file_data, (const char *) data + string_offset,
2153                         header->string_size, NULL);
2154   count = lto_input_uleb128 (&ib_main);
2155
2156   for (i = 0; i < count; i++)
2157     {
2158       unsigned int index;
2159       struct cgraph_node *node;
2160       lto_cgraph_encoder_t encoder;
2161
2162       index = lto_input_uleb128 (&ib_main);
2163       encoder = file_data->cgraph_node_encoder;
2164       node = lto_cgraph_encoder_deref (encoder, index);
2165       ipa_read_node_info (&ib_main, node, data_in);
2166     }
2167   lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2168                          len);
2169   lto_data_in_delete (data_in);
2170 }
2171
2172 /* Read ipcp jump functions.  */
2173
2174 void
2175 ipa_prop_read_jump_functions (void)
2176 {
2177   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2178   struct lto_file_decl_data *file_data;
2179   unsigned int j = 0;
2180
2181   ipa_check_create_node_params ();
2182   ipa_check_create_edge_args ();
2183   ipa_register_cgraph_hooks ();
2184
2185   while ((file_data = file_data_vec[j++]))
2186     {
2187       size_t len;
2188       const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2189
2190       if (data)
2191         ipa_prop_read_section (file_data, data, len);
2192     }
2193 }
2194
2195 /* After merging units, we can get mismatch in argument counts.
2196    Also decl merging might've rendered parameter lists obsolette.
2197    Also compute called_with_variable_arg info.  */
2198
2199 void
2200 ipa_update_after_lto_read (void)
2201 {
2202   struct cgraph_node *node;
2203   struct cgraph_edge *cs;
2204
2205   ipa_check_create_node_params ();
2206   ipa_check_create_edge_args ();
2207
2208   for (node = cgraph_nodes; node; node = node->next)
2209     if (node->analyzed)
2210       ipa_initialize_node_params (node);
2211
2212   for (node = cgraph_nodes; node; node = node->next)
2213     if (node->analyzed)
2214       for (cs = node->callees; cs; cs = cs->next_callee)
2215         {
2216           if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
2217               != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
2218             ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
2219         }
2220 }
2221
2222 /* Walk param call notes of NODE and set their call statements given the uid
2223    stored in each note and STMTS which is an array of statements indexed by the
2224    uid.  */
2225
2226 void
2227 lto_ipa_fixup_call_notes (struct cgraph_node *node, gimple *stmts)
2228 {
2229   struct ipa_node_params *info;
2230   struct ipa_param_call_note *note;
2231
2232   ipa_check_create_node_params ();
2233   info = IPA_NODE_REF (node);
2234   note = info->param_calls;
2235   /* If there are no notes or they have already been fixed up (the same fixup
2236      is called for both inlining and ipa-cp), there's nothing to do. */
2237   if (!note || note->stmt)
2238     return;
2239
2240   do
2241     {
2242       note->stmt = stmts[note->lto_stmt_uid];
2243       note = note->next;
2244     }
2245   while (note);
2246 }