OSDN Git Service

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