OSDN Git Service

2011-10-31 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-prop.c
1 /* Interprocedural analyses.
2    Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
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 "gimple.h"
34 #include "flags.h"
35 #include "timevar.h"
36 #include "flags.h"
37 #include "diagnostic.h"
38 #include "tree-pretty-print.h"
39 #include "gimple-pretty-print.h"
40 #include "lto-streamer.h"
41 #include "data-streamer.h"
42 #include "tree-streamer.h"
43
44
45 /* Intermediate information about a parameter that is only useful during the
46    run of ipa_analyze_node and is not kept afterwards.  */
47
48 struct param_analysis_info
49 {
50   bool modified;
51   bitmap visited_statements;
52 };
53
54 /* Vector where the parameter infos are actually stored. */
55 VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
56 /* Vector where the parameter infos are actually stored. */
57 VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
58
59 /* Holders of ipa cgraph hooks: */
60 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
61 static struct cgraph_node_hook_list *node_removal_hook_holder;
62 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
63 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
64 static struct cgraph_node_hook_list *function_insertion_hook_holder;
65
66 /* Return index of the formal whose tree is PTREE in function which corresponds
67    to INFO.  */
68
69 int
70 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
71 {
72   int i, count;
73
74   count = ipa_get_param_count (info);
75   for (i = 0; i < count; i++)
76     if (ipa_get_param (info, i) == ptree)
77       return i;
78
79   return -1;
80 }
81
82 /* Populate the param_decl field in parameter descriptors of INFO that
83    corresponds to NODE.  */
84
85 static void
86 ipa_populate_param_decls (struct cgraph_node *node,
87                           struct ipa_node_params *info)
88 {
89   tree fndecl;
90   tree fnargs;
91   tree parm;
92   int param_num;
93
94   fndecl = node->decl;
95   fnargs = DECL_ARGUMENTS (fndecl);
96   param_num = 0;
97   for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
98     {
99       VEC_index (ipa_param_descriptor_t,
100                  info->descriptors, param_num)->decl = parm;
101       param_num++;
102     }
103 }
104
105 /* Return how many formal parameters FNDECL has.  */
106
107 static inline int
108 count_formal_params (tree fndecl)
109 {
110   tree parm;
111   int count = 0;
112
113   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
114     count++;
115
116   return count;
117 }
118
119 /* Initialize the ipa_node_params structure associated with NODE by counting
120    the function parameters, creating the descriptors and populating their
121    param_decls.  */
122
123 void
124 ipa_initialize_node_params (struct cgraph_node *node)
125 {
126   struct ipa_node_params *info = IPA_NODE_REF (node);
127
128   if (!info->descriptors)
129     {
130       int param_count;
131
132       param_count = count_formal_params (node->decl);
133       if (param_count)
134         {
135           VEC_safe_grow_cleared (ipa_param_descriptor_t, heap,
136                                  info->descriptors, param_count);
137           ipa_populate_param_decls (node, info);
138         }
139     }
140 }
141
142 /* Print the jump functions associated with call graph edge CS to file F.  */
143
144 static void
145 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
146 {
147   int i, count;
148
149   count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
150   for (i = 0; i < count; i++)
151     {
152       struct ipa_jump_func *jump_func;
153       enum jump_func_type type;
154
155       jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
156       type = jump_func->type;
157
158       fprintf (f, "       param %d: ", i);
159       if (type == IPA_JF_UNKNOWN)
160         fprintf (f, "UNKNOWN\n");
161       else if (type == IPA_JF_KNOWN_TYPE)
162         {
163           fprintf (f, "KNOWN TYPE: base  ");
164           print_generic_expr (f, jump_func->value.known_type.base_type, 0);
165           fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
166                    jump_func->value.known_type.offset);
167           print_generic_expr (f, jump_func->value.known_type.component_type, 0);
168           fprintf (f, "\n");
169         }
170       else if (type == IPA_JF_CONST)
171         {
172           tree val = jump_func->value.constant;
173           fprintf (f, "CONST: ");
174           print_generic_expr (f, val, 0);
175           if (TREE_CODE (val) == ADDR_EXPR
176               && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
177             {
178               fprintf (f, " -> ");
179               print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
180                                   0);
181             }
182           fprintf (f, "\n");
183         }
184       else if (type == IPA_JF_CONST_MEMBER_PTR)
185         {
186           fprintf (f, "CONST MEMBER PTR: ");
187           print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
188           fprintf (f, ", ");
189           print_generic_expr (f, jump_func->value.member_cst.delta, 0);
190           fprintf (f, "\n");
191         }
192       else if (type == IPA_JF_PASS_THROUGH)
193         {
194           fprintf (f, "PASS THROUGH: ");
195           fprintf (f, "%d, op %s ",
196                    jump_func->value.pass_through.formal_id,
197                    tree_code_name[(int)
198                                   jump_func->value.pass_through.operation]);
199           if (jump_func->value.pass_through.operation != NOP_EXPR)
200             print_generic_expr (f,
201                                 jump_func->value.pass_through.operand, 0);
202           fprintf (f, "\n");
203         }
204       else if (type == IPA_JF_ANCESTOR)
205         {
206           fprintf (f, "ANCESTOR: ");
207           fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
208                    jump_func->value.ancestor.formal_id,
209                    jump_func->value.ancestor.offset);
210           print_generic_expr (f, jump_func->value.ancestor.type, 0);
211           fprintf (f, "\n");
212         }
213     }
214 }
215
216
217 /* Print the jump functions of all arguments on all call graph edges going from
218    NODE to file F.  */
219
220 void
221 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
222 {
223   struct cgraph_edge *cs;
224   int i;
225
226   fprintf (f, "  Jump functions of caller  %s:\n", cgraph_node_name (node));
227   for (cs = node->callees; cs; cs = cs->next_callee)
228     {
229       if (!ipa_edge_args_info_available_for_edge_p (cs))
230         continue;
231
232       fprintf (f, "    callsite  %s/%i -> %s/%i : \n",
233                cgraph_node_name (node), node->uid,
234                cgraph_node_name (cs->callee), cs->callee->uid);
235       ipa_print_node_jump_functions_for_edge (f, cs);
236     }
237
238   for (cs = node->indirect_calls, i = 0; cs; cs = cs->next_callee, i++)
239     {
240       if (!ipa_edge_args_info_available_for_edge_p (cs))
241         continue;
242
243       if (cs->call_stmt)
244         {
245           fprintf (f, "    indirect callsite %d for stmt ", i);
246           print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
247         }
248       else
249         fprintf (f, "    indirect callsite %d :\n", i);
250       ipa_print_node_jump_functions_for_edge (f, cs);
251
252     }
253 }
254
255 /* Print ipa_jump_func data structures of all nodes in the call graph to F.  */
256
257 void
258 ipa_print_all_jump_functions (FILE *f)
259 {
260   struct cgraph_node *node;
261
262   fprintf (f, "\nJump functions:\n");
263   for (node = cgraph_nodes; node; node = node->next)
264     {
265       ipa_print_node_jump_functions (f, node);
266     }
267 }
268
269 /* Structure to be passed in between detect_type_change and
270    check_stmt_for_type_change.  */
271
272 struct type_change_info
273 {
274   /* Set to true if dynamic type change has been detected.  */
275   bool type_maybe_changed;
276 };
277
278 /* Return true if STMT can modify a virtual method table pointer.
279
280    This function makes special assumptions about both constructors and
281    destructors which are all the functions that are allowed to alter the VMT
282    pointers.  It assumes that destructors begin with assignment into all VMT
283    pointers and that constructors essentially look in the following way:
284
285    1) The very first thing they do is that they call constructors of ancestor
286    sub-objects that have them.
287
288    2) Then VMT pointers of this and all its ancestors is set to new values
289    corresponding to the type corresponding to the constructor.
290
291    3) Only afterwards, other stuff such as constructor of member sub-objects
292    and the code written by the user is run.  Only this may include calling
293    virtual functions, directly or indirectly.
294
295    There is no way to call a constructor of an ancestor sub-object in any
296    other way.
297
298    This means that we do not have to care whether constructors get the correct
299    type information because they will always change it (in fact, if we define
300    the type to be given by the VMT pointer, it is undefined).
301
302    The most important fact to derive from the above is that if, for some
303    statement in the section 3, we try to detect whether the dynamic type has
304    changed, we can safely ignore all calls as we examine the function body
305    backwards until we reach statements in section 2 because these calls cannot
306    be ancestor constructors or destructors (if the input is not bogus) and so
307    do not change the dynamic type (this holds true only for automatically
308    allocated objects but at the moment we devirtualize only these).  We then
309    must detect that statements in section 2 change the dynamic type and can try
310    to derive the new type.  That is enough and we can stop, we will never see
311    the calls into constructors of sub-objects in this code.  Therefore we can
312    safely ignore all call statements that we traverse.
313   */
314
315 static bool
316 stmt_may_be_vtbl_ptr_store (gimple stmt)
317 {
318   if (is_gimple_call (stmt))
319     return false;
320   else if (is_gimple_assign (stmt))
321     {
322       tree lhs = gimple_assign_lhs (stmt);
323
324       if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
325         {
326           if (flag_strict_aliasing
327               && !POINTER_TYPE_P (TREE_TYPE (lhs)))
328             return false;
329
330           if (TREE_CODE (lhs) == COMPONENT_REF
331               && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
332             return false;
333           /* In the future we might want to use get_base_ref_and_offset to find
334              if there is a field corresponding to the offset and if so, proceed
335              almost like if it was a component ref.  */
336         }
337     }
338   return true;
339 }
340
341 /* Callback of walk_aliased_vdefs and a helper function for
342    detect_type_change to check whether a particular statement may modify
343    the virtual table pointer, and if possible also determine the new type of
344    the (sub-)object.  It stores its result into DATA, which points to a
345    type_change_info structure.  */
346
347 static bool
348 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
349 {
350   gimple stmt = SSA_NAME_DEF_STMT (vdef);
351   struct type_change_info *tci = (struct type_change_info *) data;
352
353   if (stmt_may_be_vtbl_ptr_store (stmt))
354     {
355       tci->type_maybe_changed = true;
356       return true;
357     }
358   else
359     return false;
360 }
361
362 /* Detect whether the dynamic type of ARG has changed (before callsite CALL) by
363    looking for assignments to its virtual table pointer.  If it is, return true
364    and fill in the jump function JFUNC with relevant type information or set it
365    to unknown.  ARG is the object itself (not a pointer to it, unless
366    dereferenced).  BASE is the base of the memory access as returned by
367    get_ref_base_and_extent, as is the offset.  */
368
369 static bool
370 detect_type_change (tree arg, tree base, gimple call,
371                     struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
372 {
373   struct type_change_info tci;
374   ao_ref ao;
375
376   gcc_checking_assert (DECL_P (arg)
377                        || TREE_CODE (arg) == MEM_REF
378                        || handled_component_p (arg));
379   /* Const calls cannot call virtual methods through VMT and so type changes do
380      not matter.  */
381   if (!flag_devirtualize || !gimple_vuse (call))
382     return false;
383
384   tci.type_maybe_changed = false;
385
386   ao.ref = arg;
387   ao.base = base;
388   ao.offset = offset;
389   ao.size = POINTER_SIZE;
390   ao.max_size = ao.size;
391   ao.ref_alias_set = -1;
392   ao.base_alias_set = -1;
393
394   walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
395                       &tci, NULL);
396   if (!tci.type_maybe_changed)
397     return false;
398
399   jfunc->type = IPA_JF_UNKNOWN;
400   return true;
401 }
402
403 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
404    SSA name (its dereference will become the base and the offset is assumed to
405    be zero).  */
406
407 static bool
408 detect_type_change_ssa (tree arg, gimple call, struct ipa_jump_func *jfunc)
409 {
410   gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
411   if (!flag_devirtualize
412       || !POINTER_TYPE_P (TREE_TYPE (arg))
413       || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != RECORD_TYPE)
414     return false;
415
416   arg = build2 (MEM_REF, ptr_type_node, arg,
417                 build_int_cst (ptr_type_node, 0));
418
419   return detect_type_change (arg, arg, call, jfunc, 0);
420 }
421
422 /* Callback of walk_aliased_vdefs.  Flags that it has been invoked to the
423    boolean variable pointed to by DATA.  */
424
425 static bool
426 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
427                      void *data)
428 {
429   bool *b = (bool *) data;
430   *b = true;
431   return true;
432 }
433
434 /* Return true if the formal parameter PARM might have been modified in this
435    function before reaching the statement STMT.  PARM_AINFO is a pointer to a
436    structure containing temporary information about PARM.  */
437
438 static bool
439 is_parm_modified_before_stmt (struct param_analysis_info *parm_ainfo,
440                               gimple stmt, tree parm)
441 {
442   bool modified = false;
443   ao_ref refd;
444
445   if (parm_ainfo->modified)
446     return true;
447
448   gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
449   ao_ref_init (&refd, parm);
450   walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
451                       &modified, &parm_ainfo->visited_statements);
452   if (modified)
453     {
454       parm_ainfo->modified = true;
455       return true;
456     }
457   return false;
458 }
459
460 /* If STMT is an assignment that loads a value from an parameter declaration,
461    return the index of the parameter in ipa_node_params which has not been
462    modified.  Otherwise return -1.  */
463
464 static int
465 load_from_unmodified_param (struct ipa_node_params *info,
466                             struct param_analysis_info *parms_ainfo,
467                             gimple stmt)
468 {
469   int index;
470   tree op1;
471
472   if (!gimple_assign_single_p (stmt))
473     return -1;
474
475   op1 = gimple_assign_rhs1 (stmt);
476   if (TREE_CODE (op1) != PARM_DECL)
477     return -1;
478
479   index = ipa_get_param_decl_index (info, op1);
480   if (index < 0
481       || is_parm_modified_before_stmt (&parms_ainfo[index], stmt, op1))
482     return -1;
483
484   return index;
485 }
486
487 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
488    of an assignment statement STMT, try to determine whether we are actually
489    handling any of the following cases and construct an appropriate jump
490    function into JFUNC if so:
491
492    1) The passed value is loaded from a formal parameter which is not a gimple
493    register (most probably because it is addressable, the value has to be
494    scalar) and we can guarantee the value has not changed.  This case can
495    therefore be described by a simple pass-through jump function.  For example:
496
497       foo (int a)
498       {
499         int a.0;
500
501         a.0_2 = a;
502         bar (a.0_2);
503
504    2) The passed value can be described by a simple arithmetic pass-through
505    jump function. E.g.
506
507       foo (int a)
508       {
509         int D.2064;
510
511         D.2064_4 = a.1(D) + 4;
512         bar (D.2064_4);
513
514    This case can also occur in combination of the previous one, e.g.:
515
516       foo (int a, int z)
517       {
518         int a.0;
519         int D.2064;
520
521         a.0_3 = a;
522         D.2064_4 = a.0_3 + 4;
523         foo (D.2064_4);
524
525    3) The passed value is an address of an object within another one (which
526    also passed by reference).  Such situations are described by an ancestor
527    jump function and describe situations such as:
528
529      B::foo() (struct B * const this)
530      {
531        struct A * D.1845;
532
533        D.1845_2 = &this_1(D)->D.1748;
534        A::bar (D.1845_2);
535
536    INFO is the structure describing individual parameters access different
537    stages of IPA optimizations.  PARMS_AINFO contains the information that is
538    only needed for intraprocedural analysis.  */
539
540 static void
541 compute_complex_assign_jump_func (struct ipa_node_params *info,
542                                   struct param_analysis_info *parms_ainfo,
543                                   struct ipa_jump_func *jfunc,
544                                   gimple call, gimple stmt, tree name)
545 {
546   HOST_WIDE_INT offset, size, max_size;
547   tree op1, tc_ssa, base, ssa;
548   int index;
549
550   op1 = gimple_assign_rhs1 (stmt);
551
552   if (TREE_CODE (op1) == SSA_NAME)
553     {
554       if (SSA_NAME_IS_DEFAULT_DEF (op1))
555         index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
556       else
557         index = load_from_unmodified_param (info, parms_ainfo,
558                                             SSA_NAME_DEF_STMT (op1));
559       tc_ssa = op1;
560     }
561   else
562     {
563       index = load_from_unmodified_param (info, parms_ainfo, stmt);
564       tc_ssa = gimple_assign_lhs (stmt);
565     }
566
567   if (index >= 0)
568     {
569       tree op2 = gimple_assign_rhs2 (stmt);
570
571       if (op2)
572         {
573           if (!is_gimple_ip_invariant (op2)
574               || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
575                   && !useless_type_conversion_p (TREE_TYPE (name),
576                                                  TREE_TYPE (op1))))
577             return;
578
579           jfunc->type = IPA_JF_PASS_THROUGH;
580           jfunc->value.pass_through.formal_id = index;
581           jfunc->value.pass_through.operation = gimple_assign_rhs_code (stmt);
582           jfunc->value.pass_through.operand = op2;
583         }
584       else if (gimple_assign_single_p (stmt)
585                && !detect_type_change_ssa (tc_ssa, call, jfunc))
586         {
587           jfunc->type = IPA_JF_PASS_THROUGH;
588           jfunc->value.pass_through.formal_id = index;
589           jfunc->value.pass_through.operation = NOP_EXPR;
590         }
591       return;
592     }
593
594   if (TREE_CODE (op1) != ADDR_EXPR)
595     return;
596   op1 = TREE_OPERAND (op1, 0);
597   if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
598     return;
599   base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
600   if (TREE_CODE (base) != MEM_REF
601       /* If this is a varying address, punt.  */
602       || max_size == -1
603       || max_size != size)
604     return;
605   offset += mem_ref_offset (base).low * BITS_PER_UNIT;
606   ssa = TREE_OPERAND (base, 0);
607   if (TREE_CODE (ssa) != SSA_NAME
608       || !SSA_NAME_IS_DEFAULT_DEF (ssa)
609       || offset < 0)
610     return;
611
612   /* Dynamic types are changed only in constructors and destructors and  */
613   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
614   if (index >= 0
615       && !detect_type_change (op1, base, call, jfunc, offset))
616     {
617       jfunc->type = IPA_JF_ANCESTOR;
618       jfunc->value.ancestor.formal_id = index;
619       jfunc->value.ancestor.offset = offset;
620       jfunc->value.ancestor.type = TREE_TYPE (op1);
621     }
622 }
623
624 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
625    it looks like:
626
627    iftmp.1_3 = &obj_2(D)->D.1762;
628
629    The base of the MEM_REF must be a default definition SSA NAME of a
630    parameter.  Return NULL_TREE if it looks otherwise.  If case of success, the
631    whole MEM_REF expression is returned and the offset calculated from any
632    handled components and the MEM_REF itself is stored into *OFFSET.  The whole
633    RHS stripped off the ADDR_EXPR is stored into *OBJ_P.  */
634
635 static tree
636 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
637 {
638   HOST_WIDE_INT size, max_size;
639   tree expr, parm, obj;
640
641   if (!gimple_assign_single_p (assign))
642     return NULL_TREE;
643   expr = gimple_assign_rhs1 (assign);
644
645   if (TREE_CODE (expr) != ADDR_EXPR)
646     return NULL_TREE;
647   expr = TREE_OPERAND (expr, 0);
648   obj = expr;
649   expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
650
651   if (TREE_CODE (expr) != MEM_REF
652       /* If this is a varying address, punt.  */
653       || max_size == -1
654       || max_size != size
655       || *offset < 0)
656     return NULL_TREE;
657   parm = TREE_OPERAND (expr, 0);
658   if (TREE_CODE (parm) != SSA_NAME
659       || !SSA_NAME_IS_DEFAULT_DEF (parm)
660       || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
661     return NULL_TREE;
662
663   *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
664   *obj_p = obj;
665   return expr;
666 }
667
668
669 /* Given that an actual argument is an SSA_NAME that is a result of a phi
670    statement PHI, try to find out whether NAME is in fact a
671    multiple-inheritance typecast from a descendant into an ancestor of a formal
672    parameter and thus can be described by an ancestor jump function and if so,
673    write the appropriate function into JFUNC.
674
675    Essentially we want to match the following pattern:
676
677      if (obj_2(D) != 0B)
678        goto <bb 3>;
679      else
680        goto <bb 4>;
681
682    <bb 3>:
683      iftmp.1_3 = &obj_2(D)->D.1762;
684
685    <bb 4>:
686      # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
687      D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
688      return D.1879_6;  */
689
690 static void
691 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
692                                     struct ipa_jump_func *jfunc,
693                                     gimple call, gimple phi)
694 {
695   HOST_WIDE_INT offset;
696   gimple assign, cond;
697   basic_block phi_bb, assign_bb, cond_bb;
698   tree tmp, parm, expr, obj;
699   int index, i;
700
701   if (gimple_phi_num_args (phi) != 2)
702     return;
703
704   if (integer_zerop (PHI_ARG_DEF (phi, 1)))
705     tmp = PHI_ARG_DEF (phi, 0);
706   else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
707     tmp = PHI_ARG_DEF (phi, 1);
708   else
709     return;
710   if (TREE_CODE (tmp) != SSA_NAME
711       || SSA_NAME_IS_DEFAULT_DEF (tmp)
712       || !POINTER_TYPE_P (TREE_TYPE (tmp))
713       || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
714     return;
715
716   assign = SSA_NAME_DEF_STMT (tmp);
717   assign_bb = gimple_bb (assign);
718   if (!single_pred_p (assign_bb))
719     return;
720   expr = get_ancestor_addr_info (assign, &obj, &offset);
721   if (!expr)
722     return;
723   parm = TREE_OPERAND (expr, 0);
724   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
725   gcc_assert (index >= 0);
726
727   cond_bb = single_pred (assign_bb);
728   cond = last_stmt (cond_bb);
729   if (!cond
730       || gimple_code (cond) != GIMPLE_COND
731       || gimple_cond_code (cond) != NE_EXPR
732       || gimple_cond_lhs (cond) != parm
733       || !integer_zerop (gimple_cond_rhs (cond)))
734     return;
735
736   phi_bb = gimple_bb (phi);
737   for (i = 0; i < 2; i++)
738     {
739       basic_block pred = EDGE_PRED (phi_bb, i)->src;
740       if (pred != assign_bb && pred != cond_bb)
741         return;
742     }
743
744   if (!detect_type_change (obj, expr, call, jfunc, offset))
745     {
746       jfunc->type = IPA_JF_ANCESTOR;
747       jfunc->value.ancestor.formal_id = index;
748       jfunc->value.ancestor.offset = offset;
749       jfunc->value.ancestor.type = TREE_TYPE (obj);
750     }
751 }
752
753 /* Given OP which is passed as an actual argument to a called function,
754    determine if it is possible to construct a KNOWN_TYPE jump function for it
755    and if so, create one and store it to JFUNC.  */
756
757 static void
758 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
759                               gimple call)
760 {
761   HOST_WIDE_INT offset, size, max_size;
762   tree base;
763
764   if (!flag_devirtualize
765       || TREE_CODE (op) != ADDR_EXPR
766       || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE)
767     return;
768
769   op = TREE_OPERAND (op, 0);
770   base = get_ref_base_and_extent (op, &offset, &size, &max_size);
771   if (!DECL_P (base)
772       || max_size == -1
773       || max_size != size
774       || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
775       || is_global_var (base))
776     return;
777
778   if (detect_type_change (op, base, call, jfunc, offset)
779       || !TYPE_BINFO (TREE_TYPE (base)))
780     return;
781
782   jfunc->type = IPA_JF_KNOWN_TYPE;
783   jfunc->value.known_type.base_type = TREE_TYPE (base);
784   jfunc->value.known_type.offset = offset;
785   jfunc->value.known_type.component_type = TREE_TYPE (op);
786 }
787
788
789 /* Determine the jump functions of scalar arguments.  Scalar means SSA names
790    and constants of a number of selected types.  INFO is the ipa_node_params
791    structure associated with the caller, PARMS_AINFO describes state of
792    analysis with respect to individual formal parameters.  ARGS is the
793    ipa_edge_args structure describing the callsite CALL which is the call
794    statement being examined.*/
795
796 static void
797 compute_scalar_jump_functions (struct ipa_node_params *info,
798                                struct param_analysis_info *parms_ainfo,
799                                struct ipa_edge_args *args,
800                                gimple call)
801 {
802   tree arg;
803   unsigned num = 0;
804
805   for (num = 0; num < gimple_call_num_args (call); num++)
806     {
807       struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, num);
808       arg = gimple_call_arg (call, num);
809
810       if (is_gimple_ip_invariant (arg))
811         {
812           jfunc->type = IPA_JF_CONST;
813           jfunc->value.constant = arg;
814         }
815       else if (TREE_CODE (arg) == SSA_NAME)
816         {
817           if (SSA_NAME_IS_DEFAULT_DEF (arg))
818             {
819               int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
820
821               if (index >= 0
822                   && !detect_type_change_ssa (arg, call, jfunc))
823                 {
824                   jfunc->type = IPA_JF_PASS_THROUGH;
825                   jfunc->value.pass_through.formal_id = index;
826                   jfunc->value.pass_through.operation = NOP_EXPR;
827                 }
828             }
829           else
830             {
831               gimple stmt = SSA_NAME_DEF_STMT (arg);
832               if (is_gimple_assign (stmt))
833                 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
834                                                   call, stmt, arg);
835               else if (gimple_code (stmt) == GIMPLE_PHI)
836                 compute_complex_ancestor_jump_func (info, jfunc, call, stmt);
837             }
838         }
839       else
840         compute_known_type_jump_func (arg, jfunc, call);
841     }
842 }
843
844 /* Inspect the given TYPE and return true iff it has the same structure (the
845    same number of fields of the same types) as a C++ member pointer.  If
846    METHOD_PTR and DELTA are non-NULL, store the trees representing the
847    corresponding fields there.  */
848
849 static bool
850 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
851 {
852   tree fld;
853
854   if (TREE_CODE (type) != RECORD_TYPE)
855     return false;
856
857   fld = TYPE_FIELDS (type);
858   if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
859       || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
860     return false;
861
862   if (method_ptr)
863     *method_ptr = fld;
864
865   fld = DECL_CHAIN (fld);
866   if (!fld || INTEGRAL_TYPE_P (fld))
867     return false;
868   if (delta)
869     *delta = fld;
870
871   if (DECL_CHAIN (fld))
872     return false;
873
874   return true;
875 }
876
877 /* Go through arguments of the CALL and for every one that looks like a member
878    pointer, check whether it can be safely declared pass-through and if so,
879    mark that to the corresponding item of jump FUNCTIONS.  Return true iff
880    there are non-pass-through member pointers within the arguments.  INFO
881    describes formal parameters of the caller.  PARMS_INFO is a pointer to a
882    vector containing intermediate information about each formal parameter.  */
883
884 static bool
885 compute_pass_through_member_ptrs (struct ipa_node_params *info,
886                                   struct param_analysis_info *parms_ainfo,
887                                   struct ipa_edge_args *args,
888                                   gimple call)
889 {
890   bool undecided_members = false;
891   unsigned num;
892   tree arg;
893
894   for (num = 0; num < gimple_call_num_args (call); num++)
895     {
896       arg = gimple_call_arg (call, num);
897
898       if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
899         {
900           if (TREE_CODE (arg) == PARM_DECL)
901             {
902               int index = ipa_get_param_decl_index (info, arg);
903
904               gcc_assert (index >=0);
905               if (!is_parm_modified_before_stmt (&parms_ainfo[index], call,
906                                                  arg))
907                 {
908                   struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args,
909                                                                        num);
910                   jfunc->type = IPA_JF_PASS_THROUGH;
911                   jfunc->value.pass_through.formal_id = index;
912                   jfunc->value.pass_through.operation = NOP_EXPR;
913                 }
914               else
915                 undecided_members = true;
916             }
917           else
918             undecided_members = true;
919         }
920     }
921
922   return undecided_members;
923 }
924
925 /* Simple function filling in a member pointer constant jump function (with PFN
926    and DELTA as the constant value) into JFUNC.  */
927
928 static void
929 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
930                                    tree pfn, tree delta)
931 {
932   jfunc->type = IPA_JF_CONST_MEMBER_PTR;
933   jfunc->value.member_cst.pfn = pfn;
934   jfunc->value.member_cst.delta = delta;
935 }
936
937 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
938    return the rhs of its defining statement.  */
939
940 static inline tree
941 get_ssa_def_if_simple_copy (tree rhs)
942 {
943   while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
944     {
945       gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
946
947       if (gimple_assign_single_p (def_stmt))
948         rhs = gimple_assign_rhs1 (def_stmt);
949       else
950         break;
951     }
952   return rhs;
953 }
954
955 /* Traverse statements from CALL backwards, scanning whether the argument ARG
956    which is a member pointer is filled in with constant values.  If it is, fill
957    the jump function JFUNC in appropriately.  METHOD_FIELD and DELTA_FIELD are
958    fields of the record type of the member pointer.  To give an example, we
959    look for a pattern looking like the following:
960
961      D.2515.__pfn ={v} printStuff;
962      D.2515.__delta ={v} 0;
963      i_1 = doprinting (D.2515);  */
964
965 static void
966 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
967                           tree delta_field, struct ipa_jump_func *jfunc)
968 {
969   gimple_stmt_iterator gsi;
970   tree method = NULL_TREE;
971   tree delta = NULL_TREE;
972
973   gsi = gsi_for_stmt (call);
974
975   gsi_prev (&gsi);
976   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
977     {
978       gimple stmt = gsi_stmt (gsi);
979       tree lhs, rhs, fld;
980
981       if (!stmt_may_clobber_ref_p (stmt, arg))
982         continue;
983       if (!gimple_assign_single_p (stmt))
984         return;
985
986       lhs = gimple_assign_lhs (stmt);
987       rhs = gimple_assign_rhs1 (stmt);
988
989       if (TREE_CODE (lhs) != COMPONENT_REF
990           || TREE_OPERAND (lhs, 0) != arg)
991         return;
992
993       fld = TREE_OPERAND (lhs, 1);
994       if (!method && fld == method_field)
995         {
996           rhs = get_ssa_def_if_simple_copy (rhs);
997           if (TREE_CODE (rhs) == ADDR_EXPR
998               && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
999               && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
1000             {
1001               method = TREE_OPERAND (rhs, 0);
1002               if (delta)
1003                 {
1004                   fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
1005                   return;
1006                 }
1007             }
1008           else
1009             return;
1010         }
1011
1012       if (!delta && fld == delta_field)
1013         {
1014           rhs = get_ssa_def_if_simple_copy (rhs);
1015           if (TREE_CODE (rhs) == INTEGER_CST)
1016             {
1017               delta = rhs;
1018               if (method)
1019                 {
1020                   fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
1021                   return;
1022                 }
1023             }
1024           else
1025             return;
1026         }
1027     }
1028
1029   return;
1030 }
1031
1032 /* Go through the arguments of the CALL and for every member pointer within
1033    tries determine whether it is a constant.  If it is, create a corresponding
1034    constant jump function in FUNCTIONS which is an array of jump functions
1035    associated with the call.  */
1036
1037 static void
1038 compute_cst_member_ptr_arguments (struct ipa_edge_args *args,
1039                                   gimple call)
1040 {
1041   unsigned num;
1042   tree arg, method_field, delta_field;
1043
1044   for (num = 0; num < gimple_call_num_args (call); num++)
1045     {
1046       struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, num);
1047       arg = gimple_call_arg (call, num);
1048
1049       if (jfunc->type == IPA_JF_UNKNOWN
1050           && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
1051                                      &delta_field))
1052         determine_cst_member_ptr (call, arg, method_field, delta_field, jfunc);
1053     }
1054 }
1055
1056 /* Compute jump function for all arguments of callsite CS and insert the
1057    information in the jump_functions array in the ipa_edge_args corresponding
1058    to this callsite.  */
1059
1060 static void
1061 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1062                                      struct cgraph_edge *cs)
1063 {
1064   struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1065   struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1066   gimple call = cs->call_stmt;
1067   int arg_num = gimple_call_num_args (call);
1068
1069   if (arg_num == 0 || args->jump_functions)
1070     return;
1071   VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions, arg_num);
1072
1073   /* We will deal with constants and SSA scalars first:  */
1074   compute_scalar_jump_functions (info, parms_ainfo, args, call);
1075
1076   /* Let's check whether there are any potential member pointers and if so,
1077      whether we can determine their functions as pass_through.  */
1078   if (!compute_pass_through_member_ptrs (info, parms_ainfo, args, call))
1079     return;
1080
1081   /* Finally, let's check whether we actually pass a new constant member
1082      pointer here...  */
1083   compute_cst_member_ptr_arguments (args, call);
1084 }
1085
1086 /* Compute jump functions for all edges - both direct and indirect - outgoing
1087    from NODE.  Also count the actual arguments in the process.  */
1088
1089 static void
1090 ipa_compute_jump_functions (struct cgraph_node *node,
1091                             struct param_analysis_info *parms_ainfo)
1092 {
1093   struct cgraph_edge *cs;
1094
1095   for (cs = node->callees; cs; cs = cs->next_callee)
1096     {
1097       struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1098                                                                   NULL);
1099       /* We do not need to bother analyzing calls to unknown
1100          functions unless they may become known during lto/whopr.  */
1101       if (!callee->analyzed && !flag_lto)
1102         continue;
1103       ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1104     }
1105
1106   for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1107     ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1108 }
1109
1110 /* If RHS looks like a rhs of a statement loading pfn from a member
1111    pointer formal parameter, return the parameter, otherwise return
1112    NULL.  If USE_DELTA, then we look for a use of the delta field
1113    rather than the pfn.  */
1114
1115 static tree
1116 ipa_get_member_ptr_load_param (tree rhs, bool use_delta)
1117 {
1118   tree rec, ref_field, ref_offset, fld, fld_offset, ptr_field, delta_field;
1119
1120   if (TREE_CODE (rhs) == COMPONENT_REF)
1121     {
1122       ref_field = TREE_OPERAND (rhs, 1);
1123       rhs = TREE_OPERAND (rhs, 0);
1124     }
1125   else
1126     ref_field = NULL_TREE;
1127   if (TREE_CODE (rhs) != MEM_REF)
1128     return NULL_TREE;
1129   rec = TREE_OPERAND (rhs, 0);
1130   if (TREE_CODE (rec) != ADDR_EXPR)
1131     return NULL_TREE;
1132   rec = TREE_OPERAND (rec, 0);
1133   if (TREE_CODE (rec) != PARM_DECL
1134       || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1135     return NULL_TREE;
1136
1137   ref_offset = TREE_OPERAND (rhs, 1);
1138
1139   if (ref_field)
1140     {
1141       if (integer_nonzerop (ref_offset))
1142         return NULL_TREE;
1143
1144       if (use_delta)
1145         fld = delta_field;
1146       else
1147         fld = ptr_field;
1148
1149       return ref_field == fld ? rec : NULL_TREE;
1150     }
1151
1152   if (use_delta)
1153     fld_offset = byte_position (delta_field);
1154   else
1155     fld_offset = byte_position (ptr_field);
1156
1157   return tree_int_cst_equal (ref_offset, fld_offset) ? rec : NULL_TREE;
1158 }
1159
1160 /* If STMT looks like a statement loading a value from a member pointer formal
1161    parameter, this function returns that parameter.  */
1162
1163 static tree
1164 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta)
1165 {
1166   tree rhs;
1167
1168   if (!gimple_assign_single_p (stmt))
1169     return NULL_TREE;
1170
1171   rhs = gimple_assign_rhs1 (stmt);
1172   return ipa_get_member_ptr_load_param (rhs, use_delta);
1173 }
1174
1175 /* Returns true iff T is an SSA_NAME defined by a statement.  */
1176
1177 static bool
1178 ipa_is_ssa_with_stmt_def (tree t)
1179 {
1180   if (TREE_CODE (t) == SSA_NAME
1181       && !SSA_NAME_IS_DEFAULT_DEF (t))
1182     return true;
1183   else
1184     return false;
1185 }
1186
1187 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1188    call to a parameter number PARAM_INDEX.  NODE is the caller.  Return the
1189    indirect call graph edge.  */
1190
1191 static struct cgraph_edge *
1192 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1193 {
1194   struct cgraph_edge *cs;
1195
1196   cs = cgraph_edge (node, stmt);
1197   cs->indirect_info->param_index = param_index;
1198   cs->indirect_info->anc_offset = 0;
1199   cs->indirect_info->polymorphic = 0;
1200   return cs;
1201 }
1202
1203 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1204    (described by INFO).  PARMS_AINFO is a pointer to a vector containing
1205    intermediate information about each formal parameter.  Currently it checks
1206    whether the call calls a pointer that is a formal parameter and if so, the
1207    parameter is marked with the called flag and an indirect call graph edge
1208    describing the call is created.  This is very simple for ordinary pointers
1209    represented in SSA but not-so-nice when it comes to member pointers.  The
1210    ugly part of this function does nothing more than trying to match the
1211    pattern of such a call.  An example of such a pattern is the gimple dump
1212    below, the call is on the last line:
1213
1214      <bb 2>:
1215        f$__delta_5 = f.__delta;
1216        f$__pfn_24 = f.__pfn;
1217
1218    or
1219      <bb 2>:
1220        f$__delta_5 = MEM[(struct  *)&f];
1221        f$__pfn_24 = MEM[(struct  *)&f + 4B];
1222
1223    and a few lines below:
1224
1225      <bb 5>
1226        D.2496_3 = (int) f$__pfn_24;
1227        D.2497_4 = D.2496_3 & 1;
1228        if (D.2497_4 != 0)
1229          goto <bb 3>;
1230        else
1231          goto <bb 4>;
1232
1233      <bb 6>:
1234        D.2500_7 = (unsigned int) f$__delta_5;
1235        D.2501_8 = &S + D.2500_7;
1236        D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1237        D.2503_10 = *D.2502_9;
1238        D.2504_12 = f$__pfn_24 + -1;
1239        D.2505_13 = (unsigned int) D.2504_12;
1240        D.2506_14 = D.2503_10 + D.2505_13;
1241        D.2507_15 = *D.2506_14;
1242        iftmp.11_16 = (String:: *) D.2507_15;
1243
1244      <bb 7>:
1245        # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1246        D.2500_19 = (unsigned int) f$__delta_5;
1247        D.2508_20 = &S + D.2500_19;
1248        D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1249
1250    Such patterns are results of simple calls to a member pointer:
1251
1252      int doprinting (int (MyString::* f)(int) const)
1253      {
1254        MyString S ("somestring");
1255
1256        return (S.*f)(4);
1257      }
1258 */
1259
1260 static void
1261 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1262                                 struct ipa_node_params *info,
1263                                 struct param_analysis_info *parms_ainfo,
1264                                 gimple call, tree target)
1265 {
1266   gimple def;
1267   tree n1, n2;
1268   gimple d1, d2;
1269   tree rec, rec2, cond;
1270   gimple branch;
1271   int index;
1272   basic_block bb, virt_bb, join;
1273
1274   if (SSA_NAME_IS_DEFAULT_DEF (target))
1275     {
1276       tree var = SSA_NAME_VAR (target);
1277       index = ipa_get_param_decl_index (info, var);
1278       if (index >= 0)
1279         ipa_note_param_call (node, index, call);
1280       return;
1281     }
1282
1283   /* Now we need to try to match the complex pattern of calling a member
1284      pointer. */
1285
1286   if (!POINTER_TYPE_P (TREE_TYPE (target))
1287       || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1288     return;
1289
1290   def = SSA_NAME_DEF_STMT (target);
1291   if (gimple_code (def) != GIMPLE_PHI)
1292     return;
1293
1294   if (gimple_phi_num_args (def) != 2)
1295     return;
1296
1297   /* First, we need to check whether one of these is a load from a member
1298      pointer that is a parameter to this function. */
1299   n1 = PHI_ARG_DEF (def, 0);
1300   n2 = PHI_ARG_DEF (def, 1);
1301   if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1302     return;
1303   d1 = SSA_NAME_DEF_STMT (n1);
1304   d2 = SSA_NAME_DEF_STMT (n2);
1305
1306   join = gimple_bb (def);
1307   if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false)))
1308     {
1309       if (ipa_get_stmt_member_ptr_load_param (d2, false))
1310         return;
1311
1312       bb = EDGE_PRED (join, 0)->src;
1313       virt_bb = gimple_bb (d2);
1314     }
1315   else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false)))
1316     {
1317       bb = EDGE_PRED (join, 1)->src;
1318       virt_bb = gimple_bb (d1);
1319     }
1320   else
1321     return;
1322
1323   /* Second, we need to check that the basic blocks are laid out in the way
1324      corresponding to the pattern. */
1325
1326   if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1327       || single_pred (virt_bb) != bb
1328       || single_succ (virt_bb) != join)
1329     return;
1330
1331   /* Third, let's see that the branching is done depending on the least
1332      significant bit of the pfn. */
1333
1334   branch = last_stmt (bb);
1335   if (!branch || gimple_code (branch) != GIMPLE_COND)
1336     return;
1337
1338   if ((gimple_cond_code (branch) != NE_EXPR
1339        && gimple_cond_code (branch) != EQ_EXPR)
1340       || !integer_zerop (gimple_cond_rhs (branch)))
1341     return;
1342
1343   cond = gimple_cond_lhs (branch);
1344   if (!ipa_is_ssa_with_stmt_def (cond))
1345     return;
1346
1347   def = SSA_NAME_DEF_STMT (cond);
1348   if (!is_gimple_assign (def)
1349       || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1350       || !integer_onep (gimple_assign_rhs2 (def)))
1351     return;
1352
1353   cond = gimple_assign_rhs1 (def);
1354   if (!ipa_is_ssa_with_stmt_def (cond))
1355     return;
1356
1357   def = SSA_NAME_DEF_STMT (cond);
1358
1359   if (is_gimple_assign (def)
1360       && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1361     {
1362       cond = gimple_assign_rhs1 (def);
1363       if (!ipa_is_ssa_with_stmt_def (cond))
1364         return;
1365       def = SSA_NAME_DEF_STMT (cond);
1366     }
1367
1368   rec2 = ipa_get_stmt_member_ptr_load_param (def,
1369                                              (TARGET_PTRMEMFUNC_VBIT_LOCATION
1370                                               == ptrmemfunc_vbit_in_delta));
1371
1372   if (rec != rec2)
1373     return;
1374
1375   index = ipa_get_param_decl_index (info, rec);
1376   if (index >= 0 && !is_parm_modified_before_stmt (&parms_ainfo[index],
1377                                                    call, rec))
1378     ipa_note_param_call (node, index, call);
1379
1380   return;
1381 }
1382
1383 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1384    object referenced in the expression is a formal parameter of the caller
1385    (described by INFO), create a call note for the statement. */
1386
1387 static void
1388 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1389                                struct ipa_node_params *info, gimple call,
1390                                tree target)
1391 {
1392   struct cgraph_edge *cs;
1393   struct cgraph_indirect_call_info *ii;
1394   struct ipa_jump_func jfunc;
1395   tree obj = OBJ_TYPE_REF_OBJECT (target);
1396   int index;
1397   HOST_WIDE_INT anc_offset;
1398
1399   if (!flag_devirtualize)
1400     return;
1401
1402   if (TREE_CODE (obj) != SSA_NAME)
1403     return;
1404
1405   if (SSA_NAME_IS_DEFAULT_DEF (obj))
1406     {
1407       if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1408         return;
1409
1410       anc_offset = 0;
1411       index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1412       gcc_assert (index >= 0);
1413       if (detect_type_change_ssa (obj, call, &jfunc))
1414         return;
1415     }
1416   else
1417     {
1418       gimple stmt = SSA_NAME_DEF_STMT (obj);
1419       tree expr;
1420
1421       expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1422       if (!expr)
1423         return;
1424       index = ipa_get_param_decl_index (info,
1425                                         SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1426       gcc_assert (index >= 0);
1427       if (detect_type_change (obj, expr, call, &jfunc, anc_offset))
1428         return;
1429     }
1430
1431   cs = ipa_note_param_call (node, index, call);
1432   ii = cs->indirect_info;
1433   ii->anc_offset = anc_offset;
1434   ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1435   ii->otr_type = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (target)));
1436   ii->polymorphic = 1;
1437 }
1438
1439 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1440    of the caller (described by INFO).  PARMS_AINFO is a pointer to a vector
1441    containing intermediate information about each formal parameter.  */
1442
1443 static void
1444 ipa_analyze_call_uses (struct cgraph_node *node,
1445                        struct ipa_node_params *info,
1446                        struct param_analysis_info *parms_ainfo, gimple call)
1447 {
1448   tree target = gimple_call_fn (call);
1449
1450   if (!target)
1451     return;
1452   if (TREE_CODE (target) == SSA_NAME)
1453     ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
1454   else if (TREE_CODE (target) == OBJ_TYPE_REF)
1455     ipa_analyze_virtual_call_uses (node, info, call, target);
1456 }
1457
1458
1459 /* Analyze the call statement STMT with respect to formal parameters (described
1460    in INFO) of caller given by NODE.  Currently it only checks whether formal
1461    parameters are called.  PARMS_AINFO is a pointer to a vector containing
1462    intermediate information about each formal parameter.  */
1463
1464 static void
1465 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
1466                        struct param_analysis_info *parms_ainfo, gimple stmt)
1467 {
1468   if (is_gimple_call (stmt))
1469     ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
1470 }
1471
1472 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
1473    If OP is a parameter declaration, mark it as used in the info structure
1474    passed in DATA.  */
1475
1476 static bool
1477 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
1478                              tree op, void *data)
1479 {
1480   struct ipa_node_params *info = (struct ipa_node_params *) data;
1481
1482   op = get_base_address (op);
1483   if (op
1484       && TREE_CODE (op) == PARM_DECL)
1485     {
1486       int index = ipa_get_param_decl_index (info, op);
1487       gcc_assert (index >= 0);
1488       ipa_set_param_used (info, index, true);
1489     }
1490
1491   return false;
1492 }
1493
1494 /* Scan the function body of NODE and inspect the uses of formal parameters.
1495    Store the findings in various structures of the associated ipa_node_params
1496    structure, such as parameter flags, notes etc.  PARMS_AINFO is a pointer to a
1497    vector containing intermediate information about each formal parameter.   */
1498
1499 static void
1500 ipa_analyze_params_uses (struct cgraph_node *node,
1501                          struct param_analysis_info *parms_ainfo)
1502 {
1503   tree decl = node->decl;
1504   basic_block bb;
1505   struct function *func;
1506   gimple_stmt_iterator gsi;
1507   struct ipa_node_params *info = IPA_NODE_REF (node);
1508   int i;
1509
1510   if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
1511     return;
1512
1513   for (i = 0; i < ipa_get_param_count (info); i++)
1514     {
1515       tree parm = ipa_get_param (info, i);
1516       /* For SSA regs see if parameter is used.  For non-SSA we compute
1517          the flag during modification analysis.  */
1518       if (is_gimple_reg (parm)
1519           && gimple_default_def (DECL_STRUCT_FUNCTION (node->decl), parm))
1520         ipa_set_param_used (info, i, true);
1521     }
1522
1523   func = DECL_STRUCT_FUNCTION (decl);
1524   FOR_EACH_BB_FN (bb, func)
1525     {
1526       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1527         {
1528           gimple stmt = gsi_stmt (gsi);
1529
1530           if (is_gimple_debug (stmt))
1531             continue;
1532
1533           ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
1534           walk_stmt_load_store_addr_ops (stmt, info,
1535                                          visit_ref_for_mod_analysis,
1536                                          visit_ref_for_mod_analysis,
1537                                          visit_ref_for_mod_analysis);
1538         }
1539       for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
1540         walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
1541                                        visit_ref_for_mod_analysis,
1542                                        visit_ref_for_mod_analysis,
1543                                        visit_ref_for_mod_analysis);
1544     }
1545
1546   info->uses_analysis_done = 1;
1547 }
1548
1549 /* Initialize the array describing properties of of formal parameters
1550    of NODE, analyze their uses and compute jump functions associated
1551    with actual arguments of calls from within NODE.  */
1552
1553 void
1554 ipa_analyze_node (struct cgraph_node *node)
1555 {
1556   struct ipa_node_params *info;
1557   struct param_analysis_info *parms_ainfo;
1558   int i, param_count;
1559
1560   ipa_check_create_node_params ();
1561   ipa_check_create_edge_args ();
1562   info = IPA_NODE_REF (node);
1563   push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1564   current_function_decl = node->decl;
1565   ipa_initialize_node_params (node);
1566
1567   param_count = ipa_get_param_count (info);
1568   parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
1569   memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
1570
1571   ipa_analyze_params_uses (node, parms_ainfo);
1572   ipa_compute_jump_functions (node, parms_ainfo);
1573
1574   for (i = 0; i < param_count; i++)
1575     if (parms_ainfo[i].visited_statements)
1576       BITMAP_FREE (parms_ainfo[i].visited_statements);
1577
1578   current_function_decl = NULL;
1579   pop_cfun ();
1580 }
1581
1582
1583 /* Update the jump function DST when the call graph edge corresponding to SRC is
1584    is being inlined, knowing that DST is of type ancestor and src of known
1585    type.  */
1586
1587 static void
1588 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
1589                                      struct ipa_jump_func *dst)
1590 {
1591   HOST_WIDE_INT combined_offset;
1592   tree combined_type;
1593
1594   combined_offset = src->value.known_type.offset + dst->value.ancestor.offset;
1595   combined_type = dst->value.ancestor.type;
1596
1597   dst->type = IPA_JF_KNOWN_TYPE;
1598   dst->value.known_type.base_type = src->value.known_type.base_type;
1599   dst->value.known_type.offset = combined_offset;
1600   dst->value.known_type.component_type = combined_type;
1601 }
1602
1603 /* Update the jump functions associated with call graph edge E when the call
1604    graph edge CS is being inlined, assuming that E->caller is already (possibly
1605    indirectly) inlined into CS->callee and that E has not been inlined.  */
1606
1607 static void
1608 update_jump_functions_after_inlining (struct cgraph_edge *cs,
1609                                       struct cgraph_edge *e)
1610 {
1611   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1612   struct ipa_edge_args *args = IPA_EDGE_REF (e);
1613   int count = ipa_get_cs_argument_count (args);
1614   int i;
1615
1616   for (i = 0; i < count; i++)
1617     {
1618       struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
1619
1620       if (dst->type == IPA_JF_ANCESTOR)
1621         {
1622           struct ipa_jump_func *src;
1623
1624           /* Variable number of arguments can cause havoc if we try to access
1625              one that does not exist in the inlined edge.  So make sure we
1626              don't.  */
1627           if (dst->value.ancestor.formal_id >= ipa_get_cs_argument_count (top))
1628             {
1629               dst->type = IPA_JF_UNKNOWN;
1630               continue;
1631             }
1632
1633           src = ipa_get_ith_jump_func (top, dst->value.ancestor.formal_id);
1634           if (src->type == IPA_JF_KNOWN_TYPE)
1635             combine_known_type_and_ancestor_jfs (src, dst);
1636           else if (src->type == IPA_JF_PASS_THROUGH
1637                    && src->value.pass_through.operation == NOP_EXPR)
1638             dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
1639           else if (src->type == IPA_JF_ANCESTOR)
1640             {
1641               dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
1642               dst->value.ancestor.offset += src->value.ancestor.offset;
1643             }
1644           else
1645             dst->type = IPA_JF_UNKNOWN;
1646         }
1647       else if (dst->type == IPA_JF_PASS_THROUGH)
1648         {
1649           struct ipa_jump_func *src;
1650           /* We must check range due to calls with variable number of arguments
1651              and we cannot combine jump functions with operations.  */
1652           if (dst->value.pass_through.operation == NOP_EXPR
1653               && (dst->value.pass_through.formal_id
1654                   < ipa_get_cs_argument_count (top)))
1655             {
1656               src = ipa_get_ith_jump_func (top,
1657                                            dst->value.pass_through.formal_id);
1658               *dst = *src;
1659             }
1660           else
1661             dst->type = IPA_JF_UNKNOWN;
1662         }
1663     }
1664 }
1665
1666 /* If TARGET is an addr_expr of a function declaration, make it the destination
1667    of an indirect edge IE and return the edge.  Otherwise, return NULL.  */
1668
1669 struct cgraph_edge *
1670 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
1671 {
1672   struct cgraph_node *callee;
1673
1674   if (TREE_CODE (target) == ADDR_EXPR)
1675     target = TREE_OPERAND (target, 0);
1676   if (TREE_CODE (target) != FUNCTION_DECL)
1677     return NULL;
1678   callee = cgraph_get_node (target);
1679   if (!callee)
1680     return NULL;
1681   ipa_check_create_node_params ();
1682
1683   /* We can not make edges to inline clones.  It is bug that someone removed
1684      the cgraph node too early.  */
1685   gcc_assert (!callee->global.inlined_to);
1686
1687   cgraph_make_edge_direct (ie, callee);
1688   if (dump_file)
1689     {
1690       fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
1691                "(%s/%i -> %s/%i), for stmt ",
1692                ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
1693                cgraph_node_name (ie->caller), ie->caller->uid,
1694                cgraph_node_name (ie->callee), ie->callee->uid);
1695       if (ie->call_stmt)
1696         print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
1697       else
1698         fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
1699     }
1700   callee = cgraph_function_or_thunk_node (callee, NULL);
1701
1702   return ie;
1703 }
1704
1705 /* Try to find a destination for indirect edge IE that corresponds to a simple
1706    call or a call of a member function pointer and where the destination is a
1707    pointer formal parameter described by jump function JFUNC.  If it can be
1708    determined, return the newly direct edge, otherwise return NULL.  */
1709
1710 static struct cgraph_edge *
1711 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
1712                                   struct ipa_jump_func *jfunc)
1713 {
1714   tree target;
1715
1716   if (jfunc->type == IPA_JF_CONST)
1717     target = jfunc->value.constant;
1718   else if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1719     target = jfunc->value.member_cst.pfn;
1720   else
1721     return NULL;
1722
1723   return ipa_make_edge_direct_to_target (ie, target);
1724 }
1725
1726 /* Try to find a destination for indirect edge IE that corresponds to a
1727    virtual call based on a formal parameter which is described by jump
1728    function JFUNC and if it can be determined, make it direct and return the
1729    direct edge.  Otherwise, return NULL.  */
1730
1731 static struct cgraph_edge *
1732 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
1733                                    struct ipa_jump_func *jfunc)
1734 {
1735   tree binfo, target;
1736
1737   if (jfunc->type != IPA_JF_KNOWN_TYPE)
1738     return NULL;
1739
1740   binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
1741   gcc_checking_assert (binfo);
1742   binfo = get_binfo_at_offset (binfo, jfunc->value.known_type.offset
1743                                + ie->indirect_info->anc_offset,
1744                                ie->indirect_info->otr_type);
1745   if (binfo)
1746     target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
1747                                                binfo);
1748   else
1749     return NULL;
1750
1751   if (target)
1752     return ipa_make_edge_direct_to_target (ie, target);
1753   else
1754     return NULL;
1755 }
1756
1757 /* Update the param called notes associated with NODE when CS is being inlined,
1758    assuming NODE is (potentially indirectly) inlined into CS->callee.
1759    Moreover, if the callee is discovered to be constant, create a new cgraph
1760    edge for it.  Newly discovered indirect edges will be added to *NEW_EDGES,
1761    unless NEW_EDGES is NULL.  Return true iff a new edge(s) were created.  */
1762
1763 static bool
1764 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
1765                                       struct cgraph_node *node,
1766                                       VEC (cgraph_edge_p, heap) **new_edges)
1767 {
1768   struct ipa_edge_args *top;
1769   struct cgraph_edge *ie, *next_ie, *new_direct_edge;
1770   bool res = false;
1771
1772   ipa_check_create_edge_args ();
1773   top = IPA_EDGE_REF (cs);
1774
1775   for (ie = node->indirect_calls; ie; ie = next_ie)
1776     {
1777       struct cgraph_indirect_call_info *ici = ie->indirect_info;
1778       struct ipa_jump_func *jfunc;
1779
1780       next_ie = ie->next_callee;
1781
1782       if (ici->param_index == -1)
1783         continue;
1784
1785       /* We must check range due to calls with variable number of arguments:  */
1786       if (ici->param_index >= ipa_get_cs_argument_count (top))
1787         {
1788           ici->param_index = -1;
1789           continue;
1790         }
1791
1792       jfunc = ipa_get_ith_jump_func (top, ici->param_index);
1793       if (jfunc->type == IPA_JF_PASS_THROUGH
1794           && jfunc->value.pass_through.operation == NOP_EXPR)
1795         ici->param_index = jfunc->value.pass_through.formal_id;
1796       else if (jfunc->type == IPA_JF_ANCESTOR)
1797         {
1798           ici->param_index = jfunc->value.ancestor.formal_id;
1799           ici->anc_offset += jfunc->value.ancestor.offset;
1800         }
1801       else
1802         /* Either we can find a destination for this edge now or never. */
1803         ici->param_index = -1;
1804
1805       if (!flag_indirect_inlining)
1806         continue;
1807
1808       if (ici->polymorphic)
1809         new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc);
1810       else
1811         new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc);
1812
1813       if (new_direct_edge)
1814         {
1815           new_direct_edge->indirect_inlining_edge = 1;
1816           if (new_edges)
1817             {
1818               VEC_safe_push (cgraph_edge_p, heap, *new_edges,
1819                              new_direct_edge);
1820               top = IPA_EDGE_REF (cs);
1821               res = true;
1822             }
1823         }
1824     }
1825
1826   return res;
1827 }
1828
1829 /* Recursively traverse subtree of NODE (including node) made of inlined
1830    cgraph_edges when CS has been inlined and invoke
1831    update_indirect_edges_after_inlining on all nodes and
1832    update_jump_functions_after_inlining on all non-inlined edges that lead out
1833    of this subtree.  Newly discovered indirect edges will be added to
1834    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were
1835    created.  */
1836
1837 static bool
1838 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1839                                    struct cgraph_node *node,
1840                                    VEC (cgraph_edge_p, heap) **new_edges)
1841 {
1842   struct cgraph_edge *e;
1843   bool res;
1844
1845   res = update_indirect_edges_after_inlining (cs, node, new_edges);
1846
1847   for (e = node->callees; e; e = e->next_callee)
1848     if (!e->inline_failed)
1849       res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1850     else
1851       update_jump_functions_after_inlining (cs, e);
1852   for (e = node->indirect_calls; e; e = e->next_callee)
1853     update_jump_functions_after_inlining (cs, e);
1854
1855   return res;
1856 }
1857
1858 /* Update jump functions and call note functions on inlining the call site CS.
1859    CS is expected to lead to a node already cloned by
1860    cgraph_clone_inline_nodes.  Newly discovered indirect edges will be added to
1861    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were +
1862    created.  */
1863
1864 bool
1865 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1866                                    VEC (cgraph_edge_p, heap) **new_edges)
1867 {
1868   bool changed;
1869   /* Do nothing if the preparation phase has not been carried out yet
1870      (i.e. during early inlining).  */
1871   if (!ipa_node_params_vector)
1872     return false;
1873   gcc_assert (ipa_edge_args_vector);
1874
1875   changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1876
1877   /* We do not keep jump functions of inlined edges up to date. Better to free
1878      them so we do not access them accidentally.  */
1879   ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1880   return changed;
1881 }
1882
1883 /* Frees all dynamically allocated structures that the argument info points
1884    to.  */
1885
1886 void
1887 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1888 {
1889   if (args->jump_functions)
1890     ggc_free (args->jump_functions);
1891
1892   memset (args, 0, sizeof (*args));
1893 }
1894
1895 /* Free all ipa_edge structures.  */
1896
1897 void
1898 ipa_free_all_edge_args (void)
1899 {
1900   int i;
1901   struct ipa_edge_args *args;
1902
1903   FOR_EACH_VEC_ELT (ipa_edge_args_t, ipa_edge_args_vector, i, args)
1904     ipa_free_edge_args_substructures (args);
1905
1906   VEC_free (ipa_edge_args_t, gc, ipa_edge_args_vector);
1907   ipa_edge_args_vector = NULL;
1908 }
1909
1910 /* Frees all dynamically allocated structures that the param info points
1911    to.  */
1912
1913 void
1914 ipa_free_node_params_substructures (struct ipa_node_params *info)
1915 {
1916   VEC_free (ipa_param_descriptor_t, heap, info->descriptors);
1917   free (info->lattices);
1918   /* Lattice values and their sources are deallocated with their alocation
1919      pool.  */
1920   VEC_free (tree, heap, info->known_vals);
1921   memset (info, 0, sizeof (*info));
1922 }
1923
1924 /* Free all ipa_node_params structures.  */
1925
1926 void
1927 ipa_free_all_node_params (void)
1928 {
1929   int i;
1930   struct ipa_node_params *info;
1931
1932   FOR_EACH_VEC_ELT (ipa_node_params_t, ipa_node_params_vector, i, info)
1933     ipa_free_node_params_substructures (info);
1934
1935   VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1936   ipa_node_params_vector = NULL;
1937 }
1938
1939 /* Hook that is called by cgraph.c when an edge is removed.  */
1940
1941 static void
1942 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1943 {
1944   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1945   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1946       <= (unsigned)cs->uid)
1947     return;
1948   ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1949 }
1950
1951 /* Hook that is called by cgraph.c when a node is removed.  */
1952
1953 static void
1954 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1955 {
1956   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1957   if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
1958       <= (unsigned)node->uid)
1959     return;
1960   ipa_free_node_params_substructures (IPA_NODE_REF (node));
1961 }
1962
1963 /* Hook that is called by cgraph.c when a node is duplicated.  */
1964
1965 static void
1966 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1967                            __attribute__((unused)) void *data)
1968 {
1969   struct ipa_edge_args *old_args, *new_args;
1970
1971   ipa_check_create_edge_args ();
1972
1973   old_args = IPA_EDGE_REF (src);
1974   new_args = IPA_EDGE_REF (dst);
1975
1976   new_args->jump_functions = VEC_copy (ipa_jump_func_t, gc,
1977                                        old_args->jump_functions);
1978 }
1979
1980 /* Hook that is called by cgraph.c when a node is duplicated.  */
1981
1982 static void
1983 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1984                            ATTRIBUTE_UNUSED void *data)
1985 {
1986   struct ipa_node_params *old_info, *new_info;
1987
1988   ipa_check_create_node_params ();
1989   old_info = IPA_NODE_REF (src);
1990   new_info = IPA_NODE_REF (dst);
1991
1992   new_info->descriptors = VEC_copy (ipa_param_descriptor_t, heap,
1993                                     old_info->descriptors);
1994   new_info->lattices = NULL;
1995   new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1996
1997   new_info->uses_analysis_done = old_info->uses_analysis_done;
1998   new_info->node_enqueued = old_info->node_enqueued;
1999 }
2000
2001
2002 /* Analyze newly added function into callgraph.  */
2003
2004 static void
2005 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2006 {
2007   ipa_analyze_node (node);
2008 }
2009
2010 /* Register our cgraph hooks if they are not already there.  */
2011
2012 void
2013 ipa_register_cgraph_hooks (void)
2014 {
2015   if (!edge_removal_hook_holder)
2016     edge_removal_hook_holder =
2017       cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
2018   if (!node_removal_hook_holder)
2019     node_removal_hook_holder =
2020       cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
2021   if (!edge_duplication_hook_holder)
2022     edge_duplication_hook_holder =
2023       cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
2024   if (!node_duplication_hook_holder)
2025     node_duplication_hook_holder =
2026       cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
2027   function_insertion_hook_holder =
2028       cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
2029 }
2030
2031 /* Unregister our cgraph hooks if they are not already there.  */
2032
2033 static void
2034 ipa_unregister_cgraph_hooks (void)
2035 {
2036   cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2037   edge_removal_hook_holder = NULL;
2038   cgraph_remove_node_removal_hook (node_removal_hook_holder);
2039   node_removal_hook_holder = NULL;
2040   cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2041   edge_duplication_hook_holder = NULL;
2042   cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2043   node_duplication_hook_holder = NULL;
2044   cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2045   function_insertion_hook_holder = NULL;
2046 }
2047
2048 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2049    longer needed after ipa-cp.  */
2050
2051 void
2052 ipa_free_all_structures_after_ipa_cp (void)
2053 {
2054   if (!optimize)
2055     {
2056       ipa_free_all_edge_args ();
2057       ipa_free_all_node_params ();
2058       free_alloc_pool (ipcp_sources_pool);
2059       free_alloc_pool (ipcp_values_pool);
2060       ipa_unregister_cgraph_hooks ();
2061     }
2062 }
2063
2064 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2065    longer needed after indirect inlining.  */
2066
2067 void
2068 ipa_free_all_structures_after_iinln (void)
2069 {
2070   ipa_free_all_edge_args ();
2071   ipa_free_all_node_params ();
2072   ipa_unregister_cgraph_hooks ();
2073   if (ipcp_sources_pool)
2074     free_alloc_pool (ipcp_sources_pool);
2075   if (ipcp_values_pool)
2076     free_alloc_pool (ipcp_values_pool);
2077 }
2078
2079 /* Print ipa_tree_map data structures of all functions in the
2080    callgraph to F.  */
2081
2082 void
2083 ipa_print_node_params (FILE * f, struct cgraph_node *node)
2084 {
2085   int i, count;
2086   tree temp;
2087   struct ipa_node_params *info;
2088
2089   if (!node->analyzed)
2090     return;
2091   info = IPA_NODE_REF (node);
2092   fprintf (f, "  function  %s parameter descriptors:\n",
2093            cgraph_node_name (node));
2094   count = ipa_get_param_count (info);
2095   for (i = 0; i < count; i++)
2096     {
2097       temp = ipa_get_param (info, i);
2098       if (TREE_CODE (temp) == PARM_DECL)
2099         fprintf (f, "    param %d : %s", i,
2100                  (DECL_NAME (temp)
2101                   ? (*lang_hooks.decl_printable_name) (temp, 2)
2102                   : "(unnamed)"));
2103       if (ipa_is_param_used (info, i))
2104         fprintf (f, " used");
2105       fprintf (f, "\n");
2106     }
2107 }
2108
2109 /* Print ipa_tree_map data structures of all functions in the
2110    callgraph to F.  */
2111
2112 void
2113 ipa_print_all_params (FILE * f)
2114 {
2115   struct cgraph_node *node;
2116
2117   fprintf (f, "\nFunction parameters:\n");
2118   for (node = cgraph_nodes; node; node = node->next)
2119     ipa_print_node_params (f, node);
2120 }
2121
2122 /* Return a heap allocated vector containing formal parameters of FNDECL.  */
2123
2124 VEC(tree, heap) *
2125 ipa_get_vector_of_formal_parms (tree fndecl)
2126 {
2127   VEC(tree, heap) *args;
2128   int count;
2129   tree parm;
2130
2131   count = count_formal_params (fndecl);
2132   args = VEC_alloc (tree, heap, count);
2133   for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
2134     VEC_quick_push (tree, args, parm);
2135
2136   return args;
2137 }
2138
2139 /* Return a heap allocated vector containing types of formal parameters of
2140    function type FNTYPE.  */
2141
2142 static inline VEC(tree, heap) *
2143 get_vector_of_formal_parm_types (tree fntype)
2144 {
2145   VEC(tree, heap) *types;
2146   int count = 0;
2147   tree t;
2148
2149   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2150     count++;
2151
2152   types = VEC_alloc (tree, heap, count);
2153   for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2154     VEC_quick_push (tree, types, TREE_VALUE (t));
2155
2156   return types;
2157 }
2158
2159 /* Modify the function declaration FNDECL and its type according to the plan in
2160    ADJUSTMENTS.  It also sets base fields of individual adjustments structures
2161    to reflect the actual parameters being modified which are determined by the
2162    base_index field.  */
2163
2164 void
2165 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
2166                               const char *synth_parm_prefix)
2167 {
2168   VEC(tree, heap) *oparms, *otypes;
2169   tree orig_type, new_type = NULL;
2170   tree old_arg_types, t, new_arg_types = NULL;
2171   tree parm, *link = &DECL_ARGUMENTS (fndecl);
2172   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2173   tree new_reversed = NULL;
2174   bool care_for_types, last_parm_void;
2175
2176   if (!synth_parm_prefix)
2177     synth_parm_prefix = "SYNTH";
2178
2179   oparms = ipa_get_vector_of_formal_parms (fndecl);
2180   orig_type = TREE_TYPE (fndecl);
2181   old_arg_types = TYPE_ARG_TYPES (orig_type);
2182
2183   /* The following test is an ugly hack, some functions simply don't have any
2184      arguments in their type.  This is probably a bug but well... */
2185   care_for_types = (old_arg_types != NULL_TREE);
2186   if (care_for_types)
2187     {
2188       last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
2189                         == void_type_node);
2190       otypes = get_vector_of_formal_parm_types (orig_type);
2191       if (last_parm_void)
2192         gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
2193       else
2194         gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
2195     }
2196   else
2197     {
2198       last_parm_void = false;
2199       otypes = NULL;
2200     }
2201
2202   for (i = 0; i < len; i++)
2203     {
2204       struct ipa_parm_adjustment *adj;
2205       gcc_assert (link);
2206
2207       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2208       parm = VEC_index (tree, oparms, adj->base_index);
2209       adj->base = parm;
2210
2211       if (adj->copy_param)
2212         {
2213           if (care_for_types)
2214             new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
2215                                                              adj->base_index),
2216                                        new_arg_types);
2217           *link = parm;
2218           link = &DECL_CHAIN (parm);
2219         }
2220       else if (!adj->remove_param)
2221         {
2222           tree new_parm;
2223           tree ptype;
2224
2225           if (adj->by_ref)
2226             ptype = build_pointer_type (adj->type);
2227           else
2228             ptype = adj->type;
2229
2230           if (care_for_types)
2231             new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
2232
2233           new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
2234                                  ptype);
2235           DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
2236
2237           DECL_ARTIFICIAL (new_parm) = 1;
2238           DECL_ARG_TYPE (new_parm) = ptype;
2239           DECL_CONTEXT (new_parm) = fndecl;
2240           TREE_USED (new_parm) = 1;
2241           DECL_IGNORED_P (new_parm) = 1;
2242           layout_decl (new_parm, 0);
2243
2244           add_referenced_var (new_parm);
2245           mark_sym_for_renaming (new_parm);
2246           adj->base = parm;
2247           adj->reduction = new_parm;
2248
2249           *link = new_parm;
2250
2251           link = &DECL_CHAIN (new_parm);
2252         }
2253     }
2254
2255   *link = NULL_TREE;
2256
2257   if (care_for_types)
2258     {
2259       new_reversed = nreverse (new_arg_types);
2260       if (last_parm_void)
2261         {
2262           if (new_reversed)
2263             TREE_CHAIN (new_arg_types) = void_list_node;
2264           else
2265             new_reversed = void_list_node;
2266         }
2267     }
2268
2269   /* Use copy_node to preserve as much as possible from original type
2270      (debug info, attribute lists etc.)
2271      Exception is METHOD_TYPEs must have THIS argument.
2272      When we are asked to remove it, we need to build new FUNCTION_TYPE
2273      instead.  */
2274   if (TREE_CODE (orig_type) != METHOD_TYPE
2275        || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
2276          && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
2277     {
2278       new_type = build_distinct_type_copy (orig_type);
2279       TYPE_ARG_TYPES (new_type) = new_reversed;
2280     }
2281   else
2282     {
2283       new_type
2284         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
2285                                                          new_reversed));
2286       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
2287       DECL_VINDEX (fndecl) = NULL_TREE;
2288     }
2289
2290   /* When signature changes, we need to clear builtin info.  */
2291   if (DECL_BUILT_IN (fndecl))
2292     {
2293       DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
2294       DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
2295     }
2296
2297   /* This is a new type, not a copy of an old type.  Need to reassociate
2298      variants.  We can handle everything except the main variant lazily.  */
2299   t = TYPE_MAIN_VARIANT (orig_type);
2300   if (orig_type != t)
2301     {
2302       TYPE_MAIN_VARIANT (new_type) = t;
2303       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
2304       TYPE_NEXT_VARIANT (t) = new_type;
2305     }
2306   else
2307     {
2308       TYPE_MAIN_VARIANT (new_type) = new_type;
2309       TYPE_NEXT_VARIANT (new_type) = NULL;
2310     }
2311
2312   TREE_TYPE (fndecl) = new_type;
2313   DECL_VIRTUAL_P (fndecl) = 0;
2314   if (otypes)
2315     VEC_free (tree, heap, otypes);
2316   VEC_free (tree, heap, oparms);
2317 }
2318
2319 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
2320    If this is a directly recursive call, CS must be NULL.  Otherwise it must
2321    contain the corresponding call graph edge.  */
2322
2323 void
2324 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
2325                            ipa_parm_adjustment_vec adjustments)
2326 {
2327   VEC(tree, heap) *vargs;
2328   VEC(tree, gc) **debug_args = NULL;
2329   gimple new_stmt;
2330   gimple_stmt_iterator gsi;
2331   tree callee_decl;
2332   int i, len;
2333
2334   len = VEC_length (ipa_parm_adjustment_t, adjustments);
2335   vargs = VEC_alloc (tree, heap, len);
2336   callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
2337
2338   gsi = gsi_for_stmt (stmt);
2339   for (i = 0; i < len; i++)
2340     {
2341       struct ipa_parm_adjustment *adj;
2342
2343       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2344
2345       if (adj->copy_param)
2346         {
2347           tree arg = gimple_call_arg (stmt, adj->base_index);
2348
2349           VEC_quick_push (tree, vargs, arg);
2350         }
2351       else if (!adj->remove_param)
2352         {
2353           tree expr, base, off;
2354           location_t loc;
2355
2356           /* We create a new parameter out of the value of the old one, we can
2357              do the following kind of transformations:
2358
2359              - A scalar passed by reference is converted to a scalar passed by
2360                value.  (adj->by_ref is false and the type of the original
2361                actual argument is a pointer to a scalar).
2362
2363              - A part of an aggregate is passed instead of the whole aggregate.
2364                The part can be passed either by value or by reference, this is
2365                determined by value of adj->by_ref.  Moreover, the code below
2366                handles both situations when the original aggregate is passed by
2367                value (its type is not a pointer) and when it is passed by
2368                reference (it is a pointer to an aggregate).
2369
2370              When the new argument is passed by reference (adj->by_ref is true)
2371              it must be a part of an aggregate and therefore we form it by
2372              simply taking the address of a reference inside the original
2373              aggregate.  */
2374
2375           gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
2376           base = gimple_call_arg (stmt, adj->base_index);
2377           loc = EXPR_LOCATION (base);
2378
2379           if (TREE_CODE (base) != ADDR_EXPR
2380               && POINTER_TYPE_P (TREE_TYPE (base)))
2381             off = build_int_cst (adj->alias_ptr_type,
2382                                  adj->offset / BITS_PER_UNIT);
2383           else
2384             {
2385               HOST_WIDE_INT base_offset;
2386               tree prev_base;
2387
2388               if (TREE_CODE (base) == ADDR_EXPR)
2389                 base = TREE_OPERAND (base, 0);
2390               prev_base = base;
2391               base = get_addr_base_and_unit_offset (base, &base_offset);
2392               /* Aggregate arguments can have non-invariant addresses.  */
2393               if (!base)
2394                 {
2395                   base = build_fold_addr_expr (prev_base);
2396                   off = build_int_cst (adj->alias_ptr_type,
2397                                        adj->offset / BITS_PER_UNIT);
2398                 }
2399               else if (TREE_CODE (base) == MEM_REF)
2400                 {
2401                   off = build_int_cst (adj->alias_ptr_type,
2402                                        base_offset
2403                                        + adj->offset / BITS_PER_UNIT);
2404                   off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
2405                                          off);
2406                   base = TREE_OPERAND (base, 0);
2407                 }
2408               else
2409                 {
2410                   off = build_int_cst (adj->alias_ptr_type,
2411                                        base_offset
2412                                        + adj->offset / BITS_PER_UNIT);
2413                   base = build_fold_addr_expr (base);
2414                 }
2415             }
2416
2417           expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
2418           if (adj->by_ref)
2419             expr = build_fold_addr_expr (expr);
2420
2421           expr = force_gimple_operand_gsi (&gsi, expr,
2422                                            adj->by_ref
2423                                            || is_gimple_reg_type (adj->type),
2424                                            NULL, true, GSI_SAME_STMT);
2425           VEC_quick_push (tree, vargs, expr);
2426         }
2427       if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
2428         {
2429           unsigned int ix;
2430           tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
2431           gimple def_temp;
2432
2433           arg = gimple_call_arg (stmt, adj->base_index);
2434           if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
2435             {
2436               if (!fold_convertible_p (TREE_TYPE (origin), arg))
2437                 continue;
2438               arg = fold_convert_loc (gimple_location (stmt),
2439                                       TREE_TYPE (origin), arg);
2440             }
2441           if (debug_args == NULL)
2442             debug_args = decl_debug_args_insert (callee_decl);
2443           for (ix = 0; VEC_iterate (tree, *debug_args, ix, ddecl); ix += 2)
2444             if (ddecl == origin)
2445               {
2446                 ddecl = VEC_index (tree, *debug_args, ix + 1);
2447                 break;
2448               }
2449           if (ddecl == NULL)
2450             {
2451               ddecl = make_node (DEBUG_EXPR_DECL);
2452               DECL_ARTIFICIAL (ddecl) = 1;
2453               TREE_TYPE (ddecl) = TREE_TYPE (origin);
2454               DECL_MODE (ddecl) = DECL_MODE (origin);
2455
2456               VEC_safe_push (tree, gc, *debug_args, origin);
2457               VEC_safe_push (tree, gc, *debug_args, ddecl);
2458             }
2459           def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
2460                                               stmt);
2461           gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
2462         }
2463     }
2464
2465   if (dump_file && (dump_flags & TDF_DETAILS))
2466     {
2467       fprintf (dump_file, "replacing stmt:");
2468       print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
2469     }
2470
2471   new_stmt = gimple_build_call_vec (callee_decl, vargs);
2472   VEC_free (tree, heap, vargs);
2473   if (gimple_call_lhs (stmt))
2474     gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2475
2476   gimple_set_block (new_stmt, gimple_block (stmt));
2477   if (gimple_has_location (stmt))
2478     gimple_set_location (new_stmt, gimple_location (stmt));
2479   gimple_call_copy_flags (new_stmt, stmt);
2480   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2481
2482   if (dump_file && (dump_flags & TDF_DETAILS))
2483     {
2484       fprintf (dump_file, "with stmt:");
2485       print_gimple_stmt (dump_file, new_stmt, 0, 0);
2486       fprintf (dump_file, "\n");
2487     }
2488   gsi_replace (&gsi, new_stmt, true);
2489   if (cs)
2490     cgraph_set_call_stmt (cs, new_stmt);
2491   update_ssa (TODO_update_ssa);
2492   free_dominance_info (CDI_DOMINATORS);
2493 }
2494
2495 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once.  */
2496
2497 static bool
2498 index_in_adjustments_multiple_times_p (int base_index,
2499                                        ipa_parm_adjustment_vec adjustments)
2500 {
2501   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2502   bool one = false;
2503
2504   for (i = 0; i < len; i++)
2505     {
2506       struct ipa_parm_adjustment *adj;
2507       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2508
2509       if (adj->base_index == base_index)
2510         {
2511           if (one)
2512             return true;
2513           else
2514             one = true;
2515         }
2516     }
2517   return false;
2518 }
2519
2520
2521 /* Return adjustments that should have the same effect on function parameters
2522    and call arguments as if they were first changed according to adjustments in
2523    INNER and then by adjustments in OUTER.  */
2524
2525 ipa_parm_adjustment_vec
2526 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
2527                          ipa_parm_adjustment_vec outer)
2528 {
2529   int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
2530   int inlen = VEC_length (ipa_parm_adjustment_t, inner);
2531   int removals = 0;
2532   ipa_parm_adjustment_vec adjustments, tmp;
2533
2534   tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
2535   for (i = 0; i < inlen; i++)
2536     {
2537       struct ipa_parm_adjustment *n;
2538       n = VEC_index (ipa_parm_adjustment_t, inner, i);
2539
2540       if (n->remove_param)
2541         removals++;
2542       else
2543         VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
2544     }
2545
2546   adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
2547   for (i = 0; i < outlen; i++)
2548     {
2549       struct ipa_parm_adjustment *r;
2550       struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
2551                                                    outer, i);
2552       struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
2553                                                   out->base_index);
2554
2555       gcc_assert (!in->remove_param);
2556       if (out->remove_param)
2557         {
2558           if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
2559             {
2560               r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
2561               memset (r, 0, sizeof (*r));
2562               r->remove_param = true;
2563             }
2564           continue;
2565         }
2566
2567       r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
2568       memset (r, 0, sizeof (*r));
2569       r->base_index = in->base_index;
2570       r->type = out->type;
2571
2572       /* FIXME:  Create nonlocal value too.  */
2573
2574       if (in->copy_param && out->copy_param)
2575         r->copy_param = true;
2576       else if (in->copy_param)
2577         r->offset = out->offset;
2578       else if (out->copy_param)
2579         r->offset = in->offset;
2580       else
2581         r->offset = in->offset + out->offset;
2582     }
2583
2584   for (i = 0; i < inlen; i++)
2585     {
2586       struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
2587                                                  inner, i);
2588
2589       if (n->remove_param)
2590         VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
2591     }
2592
2593   VEC_free (ipa_parm_adjustment_t, heap, tmp);
2594   return adjustments;
2595 }
2596
2597 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
2598    friendly way, assuming they are meant to be applied to FNDECL.  */
2599
2600 void
2601 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
2602                             tree fndecl)
2603 {
2604   int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2605   bool first = true;
2606   VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
2607
2608   fprintf (file, "IPA param adjustments: ");
2609   for (i = 0; i < len; i++)
2610     {
2611       struct ipa_parm_adjustment *adj;
2612       adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2613
2614       if (!first)
2615         fprintf (file, "                 ");
2616       else
2617         first = false;
2618
2619       fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
2620       print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
2621       if (adj->base)
2622         {
2623           fprintf (file, ", base: ");
2624           print_generic_expr (file, adj->base, 0);
2625         }
2626       if (adj->reduction)
2627         {
2628           fprintf (file, ", reduction: ");
2629           print_generic_expr (file, adj->reduction, 0);
2630         }
2631       if (adj->new_ssa_base)
2632         {
2633           fprintf (file, ", new_ssa_base: ");
2634           print_generic_expr (file, adj->new_ssa_base, 0);
2635         }
2636
2637       if (adj->copy_param)
2638         fprintf (file, ", copy_param");
2639       else if (adj->remove_param)
2640         fprintf (file, ", remove_param");
2641       else
2642         fprintf (file, ", offset %li", (long) adj->offset);
2643       if (adj->by_ref)
2644         fprintf (file, ", by_ref");
2645       print_node_brief (file, ", type: ", adj->type, 0);
2646       fprintf (file, "\n");
2647     }
2648   VEC_free (tree, heap, parms);
2649 }
2650
2651 /* Stream out jump function JUMP_FUNC to OB.  */
2652
2653 static void
2654 ipa_write_jump_function (struct output_block *ob,
2655                          struct ipa_jump_func *jump_func)
2656 {
2657   streamer_write_uhwi (ob, jump_func->type);
2658
2659   switch (jump_func->type)
2660     {
2661     case IPA_JF_UNKNOWN:
2662       break;
2663     case IPA_JF_KNOWN_TYPE:
2664       streamer_write_uhwi (ob, jump_func->value.known_type.offset);
2665       stream_write_tree (ob, jump_func->value.known_type.base_type, true);
2666       stream_write_tree (ob, jump_func->value.known_type.component_type, true);
2667       break;
2668     case IPA_JF_CONST:
2669       stream_write_tree (ob, jump_func->value.constant, true);
2670       break;
2671     case IPA_JF_PASS_THROUGH:
2672       stream_write_tree (ob, jump_func->value.pass_through.operand, true);
2673       streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
2674       streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
2675       break;
2676     case IPA_JF_ANCESTOR:
2677       streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
2678       stream_write_tree (ob, jump_func->value.ancestor.type, true);
2679       streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
2680       break;
2681     case IPA_JF_CONST_MEMBER_PTR:
2682       stream_write_tree (ob, jump_func->value.member_cst.pfn, true);
2683       stream_write_tree (ob, jump_func->value.member_cst.delta, false);
2684       break;
2685     }
2686 }
2687
2688 /* Read in jump function JUMP_FUNC from IB.  */
2689
2690 static void
2691 ipa_read_jump_function (struct lto_input_block *ib,
2692                         struct ipa_jump_func *jump_func,
2693                         struct data_in *data_in)
2694 {
2695   jump_func->type = (enum jump_func_type) streamer_read_uhwi (ib);
2696
2697   switch (jump_func->type)
2698     {
2699     case IPA_JF_UNKNOWN:
2700       break;
2701     case IPA_JF_KNOWN_TYPE:
2702       jump_func->value.known_type.offset = streamer_read_uhwi (ib);
2703       jump_func->value.known_type.base_type = stream_read_tree (ib, data_in);
2704       jump_func->value.known_type.component_type = stream_read_tree (ib,
2705                                                                      data_in);
2706       break;
2707     case IPA_JF_CONST:
2708       jump_func->value.constant = stream_read_tree (ib, data_in);
2709       break;
2710     case IPA_JF_PASS_THROUGH:
2711       jump_func->value.pass_through.operand = stream_read_tree (ib, data_in);
2712       jump_func->value.pass_through.formal_id = streamer_read_uhwi (ib);
2713       jump_func->value.pass_through.operation
2714         = (enum tree_code) streamer_read_uhwi (ib);
2715       break;
2716     case IPA_JF_ANCESTOR:
2717       jump_func->value.ancestor.offset = streamer_read_uhwi (ib);
2718       jump_func->value.ancestor.type = stream_read_tree (ib, data_in);
2719       jump_func->value.ancestor.formal_id = streamer_read_uhwi (ib);
2720       break;
2721     case IPA_JF_CONST_MEMBER_PTR:
2722       jump_func->value.member_cst.pfn = stream_read_tree (ib, data_in);
2723       jump_func->value.member_cst.delta = stream_read_tree (ib, data_in);
2724       break;
2725     }
2726 }
2727
2728 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
2729    relevant to indirect inlining to OB.  */
2730
2731 static void
2732 ipa_write_indirect_edge_info (struct output_block *ob,
2733                               struct cgraph_edge *cs)
2734 {
2735   struct cgraph_indirect_call_info *ii = cs->indirect_info;
2736   struct bitpack_d bp;
2737
2738   streamer_write_hwi (ob, ii->param_index);
2739   streamer_write_hwi (ob, ii->anc_offset);
2740   bp = bitpack_create (ob->main_stream);
2741   bp_pack_value (&bp, ii->polymorphic, 1);
2742   streamer_write_bitpack (&bp);
2743
2744   if (ii->polymorphic)
2745     {
2746       streamer_write_hwi (ob, ii->otr_token);
2747       stream_write_tree (ob, ii->otr_type, true);
2748     }
2749 }
2750
2751 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
2752    relevant to indirect inlining from IB.  */
2753
2754 static void
2755 ipa_read_indirect_edge_info (struct lto_input_block *ib,
2756                              struct data_in *data_in ATTRIBUTE_UNUSED,
2757                              struct cgraph_edge *cs)
2758 {
2759   struct cgraph_indirect_call_info *ii = cs->indirect_info;
2760   struct bitpack_d bp;
2761
2762   ii->param_index = (int) streamer_read_hwi (ib);
2763   ii->anc_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
2764   bp = streamer_read_bitpack (ib);
2765   ii->polymorphic = bp_unpack_value (&bp, 1);
2766   if (ii->polymorphic)
2767     {
2768       ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
2769       ii->otr_type = stream_read_tree (ib, data_in);
2770     }
2771 }
2772
2773 /* Stream out NODE info to OB.  */
2774
2775 static void
2776 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
2777 {
2778   int node_ref;
2779   lto_cgraph_encoder_t encoder;
2780   struct ipa_node_params *info = IPA_NODE_REF (node);
2781   int j;
2782   struct cgraph_edge *e;
2783   struct bitpack_d bp;
2784
2785   encoder = ob->decl_state->cgraph_node_encoder;
2786   node_ref = lto_cgraph_encoder_encode (encoder, node);
2787   streamer_write_uhwi (ob, node_ref);
2788
2789   bp = bitpack_create (ob->main_stream);
2790   gcc_assert (info->uses_analysis_done
2791               || ipa_get_param_count (info) == 0);
2792   gcc_assert (!info->node_enqueued);
2793   gcc_assert (!info->ipcp_orig_node);
2794   for (j = 0; j < ipa_get_param_count (info); j++)
2795     bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
2796   streamer_write_bitpack (&bp);
2797   for (e = node->callees; e; e = e->next_callee)
2798     {
2799       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2800
2801       streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
2802       for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2803         ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2804     }
2805   for (e = node->indirect_calls; e; e = e->next_callee)
2806     {
2807       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2808
2809       streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
2810       for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2811         ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2812       ipa_write_indirect_edge_info (ob, e);
2813     }
2814 }
2815
2816 /* Stream in NODE info from IB.  */
2817
2818 static void
2819 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2820                     struct data_in *data_in)
2821 {
2822   struct ipa_node_params *info = IPA_NODE_REF (node);
2823   int k;
2824   struct cgraph_edge *e;
2825   struct bitpack_d bp;
2826
2827   ipa_initialize_node_params (node);
2828
2829   bp = streamer_read_bitpack (ib);
2830   if (ipa_get_param_count (info) != 0)
2831     info->uses_analysis_done = true;
2832   info->node_enqueued = false;
2833   for (k = 0; k < ipa_get_param_count (info); k++)
2834     ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
2835   for (e = node->callees; e; e = e->next_callee)
2836     {
2837       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2838       int count = streamer_read_uhwi (ib);
2839
2840       if (!count)
2841         continue;
2842       VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions, count);
2843
2844       for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2845         ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2846     }
2847   for (e = node->indirect_calls; e; e = e->next_callee)
2848     {
2849       struct ipa_edge_args *args = IPA_EDGE_REF (e);
2850       int count = streamer_read_uhwi (ib);
2851
2852       if (count)
2853         {
2854           VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions,
2855                                  count);
2856           for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2857             ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k),
2858                                     data_in);
2859         }
2860       ipa_read_indirect_edge_info (ib, data_in, e);
2861     }
2862 }
2863
2864 /* Write jump functions for nodes in SET.  */
2865
2866 void
2867 ipa_prop_write_jump_functions (cgraph_node_set set)
2868 {
2869   struct cgraph_node *node;
2870   struct output_block *ob;
2871   unsigned int count = 0;
2872   cgraph_node_set_iterator csi;
2873
2874   if (!ipa_node_params_vector)
2875     return;
2876
2877   ob = create_output_block (LTO_section_jump_functions);
2878   ob->cgraph_node = NULL;
2879   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2880     {
2881       node = csi_node (csi);
2882       if (cgraph_function_with_gimple_body_p (node)
2883           && IPA_NODE_REF (node) != NULL)
2884         count++;
2885     }
2886
2887   streamer_write_uhwi (ob, count);
2888
2889   /* Process all of the functions.  */
2890   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2891     {
2892       node = csi_node (csi);
2893       if (cgraph_function_with_gimple_body_p (node)
2894           && IPA_NODE_REF (node) != NULL)
2895         ipa_write_node_info (ob, node);
2896     }
2897   streamer_write_char_stream (ob->main_stream, 0);
2898   produce_asm (ob, NULL);
2899   destroy_output_block (ob);
2900 }
2901
2902 /* Read section in file FILE_DATA of length LEN with data DATA.  */
2903
2904 static void
2905 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2906                        size_t len)
2907 {
2908   const struct lto_function_header *header =
2909     (const struct lto_function_header *) data;
2910   const int32_t cfg_offset = sizeof (struct lto_function_header);
2911   const int32_t main_offset = cfg_offset + header->cfg_size;
2912   const int32_t string_offset = main_offset + header->main_size;
2913   struct data_in *data_in;
2914   struct lto_input_block ib_main;
2915   unsigned int i;
2916   unsigned int count;
2917
2918   LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2919                         header->main_size);
2920
2921   data_in =
2922     lto_data_in_create (file_data, (const char *) data + string_offset,
2923                         header->string_size, NULL);
2924   count = streamer_read_uhwi (&ib_main);
2925
2926   for (i = 0; i < count; i++)
2927     {
2928       unsigned int index;
2929       struct cgraph_node *node;
2930       lto_cgraph_encoder_t encoder;
2931
2932       index = streamer_read_uhwi (&ib_main);
2933       encoder = file_data->cgraph_node_encoder;
2934       node = lto_cgraph_encoder_deref (encoder, index);
2935       gcc_assert (node->analyzed);
2936       ipa_read_node_info (&ib_main, node, data_in);
2937     }
2938   lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2939                          len);
2940   lto_data_in_delete (data_in);
2941 }
2942
2943 /* Read ipcp jump functions.  */
2944
2945 void
2946 ipa_prop_read_jump_functions (void)
2947 {
2948   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2949   struct lto_file_decl_data *file_data;
2950   unsigned int j = 0;
2951
2952   ipa_check_create_node_params ();
2953   ipa_check_create_edge_args ();
2954   ipa_register_cgraph_hooks ();
2955
2956   while ((file_data = file_data_vec[j++]))
2957     {
2958       size_t len;
2959       const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2960
2961       if (data)
2962         ipa_prop_read_section (file_data, data, len);
2963     }
2964 }
2965
2966 /* After merging units, we can get mismatch in argument counts.
2967    Also decl merging might've rendered parameter lists obsolete.
2968    Also compute called_with_variable_arg info.  */
2969
2970 void
2971 ipa_update_after_lto_read (void)
2972 {
2973   struct cgraph_node *node;
2974
2975   ipa_check_create_node_params ();
2976   ipa_check_create_edge_args ();
2977
2978   for (node = cgraph_nodes; node; node = node->next)
2979     if (node->analyzed)
2980       ipa_initialize_node_params (node);
2981 }