OSDN Git Service

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