OSDN Git Service

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