OSDN Git Service

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