OSDN Git Service

a376f45c7b3c7e5f1a252bfb2c3c41597b46fefd
[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 /* Initialize worklist to contain all functions.  */
49
50 struct ipa_func_list *
51 ipa_init_func_list (void)
52 {
53   struct cgraph_node *node;
54   struct ipa_func_list * wl;
55
56   wl = NULL;
57   for (node = cgraph_nodes; node; node = node->next)
58     if (node->analyzed)
59       {
60         /* Unreachable nodes should have been eliminated before ipcp and
61            inlining.  */
62         gcc_assert (node->needed || node->reachable);
63         ipa_push_func_to_list (&wl, node);
64       }
65
66   return wl;
67 }
68
69 /* Add cgraph node MT to the worklist. Set worklist element WL
70    to point to MT.  */
71
72 void
73 ipa_push_func_to_list (struct ipa_func_list **wl, struct cgraph_node *mt)
74 {
75   struct ipa_func_list *temp;
76
77   temp = XCNEW (struct ipa_func_list);
78   temp->node = mt;
79   temp->next = *wl;
80   *wl = temp;
81 }
82
83 /* Remove a function from the worklist. WL points to the first
84    element in the list, which is removed.  */
85
86 struct cgraph_node *
87 ipa_pop_func_from_list (struct ipa_func_list ** wl)
88 {
89   struct ipa_func_list *first;
90   struct cgraph_node *return_func;
91
92   first = *wl;
93   *wl = (*wl)->next;
94   return_func = first->node;
95   free (first);
96   return return_func;
97 }
98
99 /* Return index of the formal whose tree is PTREE in function which corresponds
100    to INFO.  */
101
102 static int
103 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
104 {
105   int i, count;
106
107   count = ipa_get_param_count (info);
108   for (i = 0; i < count; i++)
109     if (ipa_get_param(info, i) == ptree)
110       return i;
111
112   return -1;
113 }
114
115 /* Populate the param_decl field in parameter descriptors of INFO that
116    corresponds to NODE.  */
117
118 static void
119 ipa_populate_param_decls (struct cgraph_node *node,
120                           struct ipa_node_params *info)
121 {
122   tree fndecl;
123   tree fnargs;
124   tree parm;
125   int param_num;
126
127   fndecl = node->decl;
128   fnargs = DECL_ARGUMENTS (fndecl);
129   param_num = 0;
130   for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
131     {
132       info->params[param_num].decl = parm;
133       param_num++;
134     }
135 }
136
137 /* Count number of formal parameters in NOTE. Store the result to the
138    appropriate field of INFO.  */
139
140 static void
141 ipa_count_formal_params (struct cgraph_node *node,
142                          struct ipa_node_params *info)
143 {
144   tree fndecl;
145   tree fnargs;
146   tree parm;
147   int param_num;
148
149   fndecl = node->decl;
150   fnargs = DECL_ARGUMENTS (fndecl);
151   param_num = 0;
152   for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
153     param_num++;
154   ipa_set_param_count (info, param_num);
155 }
156
157 /* Initialize the ipa_node_params structure associated with NODE by counting
158    the function parameters, creating the descriptors and populating their
159    param_decls.  */
160
161 void
162 ipa_initialize_node_params (struct cgraph_node *node)
163 {
164   struct ipa_node_params *info = IPA_NODE_REF (node);
165
166   if (!info->params)
167     {
168       ipa_count_formal_params (node, info);
169       info->params = XCNEWVEC (struct ipa_param_descriptor,
170                                     ipa_get_param_count (info));
171       ipa_populate_param_decls (node, info);
172     }
173 }
174
175 /* Callback of walk_stmt_load_store_addr_ops for the visit_store and visit_addr
176    parameters.  If OP is a parameter declaration, mark it as modified in the
177    info structure passed in DATA.  */
178
179 static bool
180 visit_store_addr_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
181                                    tree op, void *data)
182 {
183   struct ipa_node_params *info = (struct ipa_node_params *) data;
184
185   if (TREE_CODE (op) == PARM_DECL)
186     {
187       int index = ipa_get_param_decl_index (info, op);
188       gcc_assert (index >= 0);
189       info->params[index].modified = true;
190     }
191
192   return false;
193 }
194
195 /* Compute which formal parameters of function associated with NODE are locally
196    modified or their address is taken.  Note that this does not apply on
197    parameters with SSA names but those can and should be analyzed
198    differently.  */
199
200 void
201 ipa_detect_param_modifications (struct cgraph_node *node)
202 {
203   tree decl = node->decl;
204   basic_block bb;
205   struct function *func;
206   gimple_stmt_iterator gsi;
207   struct ipa_node_params *info = IPA_NODE_REF (node);
208
209   if (ipa_get_param_count (info) == 0 || info->modification_analysis_done)
210     return;
211
212   func = DECL_STRUCT_FUNCTION (decl);
213   FOR_EACH_BB_FN (bb, func)
214     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
215       walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info, NULL,
216                                      visit_store_addr_for_mod_analysis,
217                                      visit_store_addr_for_mod_analysis);
218
219   info->modification_analysis_done = 1;
220 }
221
222 /* Count number of arguments callsite CS has and store it in
223    ipa_edge_args structure corresponding to this callsite.  */
224
225 void
226 ipa_count_arguments (struct cgraph_edge *cs)
227 {
228   gimple stmt;
229   int arg_num;
230
231   stmt = cs->call_stmt;
232   gcc_assert (is_gimple_call (stmt));
233   arg_num = gimple_call_num_args (stmt);
234   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
235       <= (unsigned) cgraph_edge_max_uid)
236     VEC_safe_grow_cleared (ipa_edge_args_t, heap,
237                            ipa_edge_args_vector, cgraph_edge_max_uid + 1);
238   ipa_set_cs_argument_count (IPA_EDGE_REF (cs), arg_num);
239 }
240
241 /* Print the jump functions of all arguments on all call graph edges going from
242    NODE to file F.  */
243
244 void
245 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
246 {
247   int i, count;
248   struct cgraph_edge *cs;
249   struct ipa_jump_func *jump_func;
250   enum jump_func_type type;
251
252   fprintf (f, "  Jump functions of caller  %s:\n", cgraph_node_name (node));
253   for (cs = node->callees; cs; cs = cs->next_callee)
254     {
255       if (!ipa_edge_args_info_available_for_edge_p (cs))
256         continue;
257
258       fprintf (f, "    callsite  %s ", cgraph_node_name (node));
259       fprintf (f, "-> %s :: \n", cgraph_node_name (cs->callee));
260
261       count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
262       for (i = 0; i < count; i++)
263         {
264           jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
265           type = jump_func->type;
266
267           fprintf (f, "       param %d: ", i);
268           if (type == IPA_JF_UNKNOWN)
269             fprintf (f, "UNKNOWN\n");
270           else if (type == IPA_JF_CONST)
271             {
272               tree val = jump_func->value.constant;
273               fprintf (f, "CONST: ");
274               print_generic_expr (f, val, 0);
275               fprintf (f, "\n");
276             }
277           else if (type == IPA_JF_CONST_MEMBER_PTR)
278             {
279               fprintf (f, "CONST MEMBER PTR: ");
280               print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
281               fprintf (f, ", ");
282               print_generic_expr (f, jump_func->value.member_cst.delta, 0);
283               fprintf (f, "\n");
284             }
285           else if (type == IPA_JF_PASS_THROUGH)
286             {
287               fprintf (f, "PASS THROUGH: ");
288               fprintf (f, "%d\n", jump_func->value.formal_id);
289             }
290         }
291     }
292 }
293
294 /* Print ipa_jump_func data structures of all nodes in the call graph to F.  */
295
296 void
297 ipa_print_all_jump_functions (FILE *f)
298 {
299   struct cgraph_node *node;
300
301   fprintf (f, "\nJump functions:\n");
302   for (node = cgraph_nodes; node; node = node->next)
303     {
304       ipa_print_node_jump_functions (f, node);
305     }
306 }
307
308 /* Determine the jump functions of scalar arguments.  Scalar means SSA names
309    and constants of a number of selected types.  INFO is the ipa_node_params
310    structure associated with the caller, FUNCTIONS is a pointer to an array of
311    jump function structures associated with CALL which is the call statement
312    being examined.*/
313
314 static void
315 compute_scalar_jump_functions (struct ipa_node_params *info,
316                                struct ipa_jump_func *functions,
317                                gimple call)
318 {
319   tree arg;
320   unsigned num = 0;
321
322   for (num = 0; num < gimple_call_num_args (call); num++)
323     {
324       arg = gimple_call_arg (call, num);
325
326       if (is_gimple_ip_invariant (arg))
327         {
328           functions[num].type = IPA_JF_CONST;
329           functions[num].value.constant = arg;
330         }
331       else if ((TREE_CODE (arg) == SSA_NAME) && SSA_NAME_IS_DEFAULT_DEF (arg))
332         {
333           int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
334
335           if (index >= 0)
336             {
337               functions[num].type = IPA_JF_PASS_THROUGH;
338               functions[num].value.formal_id = index;
339             }
340         }
341     }
342 }
343
344 /* Inspect the given TYPE and return true iff it has the same structure (the
345    same number of fields of the same types) as a C++ member pointer.  If
346    METHOD_PTR and DELTA are non-NULL, store the trees representing the
347    corresponding fields there.  */
348
349 static bool
350 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
351 {
352   tree fld;
353
354   if (TREE_CODE (type) != RECORD_TYPE)
355     return false;
356
357   fld = TYPE_FIELDS (type);
358   if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
359       || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
360     return false;
361
362   if (method_ptr)
363     *method_ptr = fld;
364
365   fld = TREE_CHAIN (fld);
366   if (!fld || INTEGRAL_TYPE_P (fld))
367     return false;
368   if (delta)
369     *delta = fld;
370
371   if (TREE_CHAIN (fld))
372     return false;
373
374   return true;
375 }
376
377 /* Go through arguments of the CALL and for every one that looks like a member
378    pointer, check whether it can be safely declared pass-through and if so,
379    mark that to the corresponding item of jump FUNCTIONS.  Return true iff
380    there are non-pass-through member pointers within the arguments.  INFO
381    describes formal parameters of the caller.  */
382
383 static bool
384 compute_pass_through_member_ptrs (struct ipa_node_params *info,
385                                   struct ipa_jump_func *functions,
386                                   gimple call)
387 {
388   bool undecided_members = false;
389   unsigned num;
390   tree arg;
391
392   for (num = 0; num < gimple_call_num_args (call); num++)
393     {
394       arg = gimple_call_arg (call, num);
395
396       if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
397         {
398           if (TREE_CODE (arg) == PARM_DECL)
399             {
400               int index = ipa_get_param_decl_index (info, arg);
401
402               gcc_assert (index >=0);
403               if (!ipa_is_param_modified (info, index))
404                 {
405                   functions[num].type = IPA_JF_PASS_THROUGH;
406                   functions[num].value.formal_id = index;
407                 }
408               else
409                 undecided_members = true;
410             }
411           else
412             undecided_members = true;
413         }
414     }
415
416   return undecided_members;
417 }
418
419 /* Simple function filling in a member pointer constant jump function (with PFN
420    and DELTA as the constant value) into JFUNC.  */
421
422 static void
423 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
424                                    tree pfn, tree delta)
425 {
426   jfunc->type = IPA_JF_CONST_MEMBER_PTR;
427   jfunc->value.member_cst.pfn = pfn;
428   jfunc->value.member_cst.delta = delta;
429 }
430
431 /* If RHS is an SSA_NAMe and it is defined by a simple copy assign statement,
432    return the rhs of its defining statement.  */
433
434 static inline tree
435 get_ssa_def_if_simple_copy (tree rhs)
436 {
437   while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
438     {
439       gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
440
441       if (gimple_assign_single_p (def_stmt))
442         rhs = gimple_assign_rhs1 (def_stmt);
443       else
444         break;
445     }
446   return rhs;
447 }
448
449 /* Traverse statements from CALL backwards, scanning whether the argument ARG
450    which is a member pointer is filled in with constant values.  If it is, fill
451    the jump function JFUNC in appropriately.  METHOD_FIELD and DELTA_FIELD are
452    fields of the record type of the member pointer.  To give an example, we
453    look for a pattern looking like the following:
454
455      D.2515.__pfn ={v} printStuff;
456      D.2515.__delta ={v} 0;
457      i_1 = doprinting (D.2515);  */
458
459 static void
460 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
461                           tree delta_field, struct ipa_jump_func *jfunc)
462 {
463   gimple_stmt_iterator gsi;
464   tree method = NULL_TREE;
465   tree delta = NULL_TREE;
466
467   gsi = gsi_for_stmt (call);
468
469   gsi_prev (&gsi);
470   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
471     {
472       gimple stmt = gsi_stmt (gsi);
473       tree lhs, rhs, fld;
474
475       if (!gimple_assign_single_p (stmt))
476         return;
477
478       lhs = gimple_assign_lhs (stmt);
479       rhs = gimple_assign_rhs1 (stmt);
480
481       if (TREE_CODE (lhs) != COMPONENT_REF
482           || TREE_OPERAND (lhs, 0) != arg)
483         continue;
484
485       fld = TREE_OPERAND (lhs, 1);
486       if (!method && fld == method_field)
487         {
488           rhs = get_ssa_def_if_simple_copy (rhs);
489           if (TREE_CODE (rhs) == ADDR_EXPR
490               && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
491               && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
492             {
493               method = TREE_OPERAND (rhs, 0);
494               if (delta)
495                 {
496                   fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
497                   return;
498                 }
499             }
500           else
501             return;
502         }
503
504       if (!delta && fld == delta_field)
505         {
506           rhs = get_ssa_def_if_simple_copy (rhs);
507           if (TREE_CODE (rhs) == INTEGER_CST)
508             {
509               delta = rhs;
510               if (method)
511                 {
512                   fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
513                   return;
514                 }
515             }
516           else
517             return;
518         }
519     }
520
521   return;
522 }
523
524 /* Go through the arguments of the CALL and for every member pointer within
525    tries determine whether it is a constant.  If it is, create a corresponding
526    constant jump function in FUNCTIONS which is an array of jump functions
527    associated with the call.  */
528
529 static void
530 compute_cst_member_ptr_arguments (struct ipa_jump_func *functions,
531                                   gimple call)
532 {
533   unsigned num;
534   tree arg, method_field, delta_field;
535
536   for (num = 0; num < gimple_call_num_args (call); num++)
537     {
538       arg = gimple_call_arg (call, num);
539
540       if (functions[num].type == IPA_JF_UNKNOWN
541           && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
542                                      &delta_field))
543         determine_cst_member_ptr (call, arg, method_field, delta_field,
544                                   &functions[num]);
545     }
546 }
547
548 /* Compute jump function for all arguments of callsite CS and insert the
549    information in the jump_functions array in the ipa_edge_args corresponding
550    to this callsite.  */
551
552 void
553 ipa_compute_jump_functions (struct cgraph_edge *cs)
554 {
555   struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
556   struct ipa_edge_args *arguments = IPA_EDGE_REF (cs);
557   gimple call;
558
559   if (ipa_get_cs_argument_count (arguments) == 0 || arguments->jump_functions)
560     return;
561   arguments->jump_functions = XCNEWVEC (struct ipa_jump_func,
562                                         ipa_get_cs_argument_count (arguments));
563
564   call = cs->call_stmt;
565   gcc_assert (is_gimple_call (call));
566
567   /* We will deal with constants and SSA scalars first:  */
568   compute_scalar_jump_functions (info, arguments->jump_functions, call);
569
570   /* Let's check whether there are any potential member pointers and if so,
571      whether we can determine their functions as pass_through.  */
572   if (!compute_pass_through_member_ptrs (info, arguments->jump_functions, call))
573     return;
574
575   /* Finally, let's check whether we actually pass a new constant member
576      pointer here...  */
577   compute_cst_member_ptr_arguments (arguments->jump_functions, call);
578 }
579
580 /* If RHS looks like a rhs of a statement loading pfn from a member pointer
581    formal parameter, return the parameter, otherwise return NULL.  */
582
583 static tree
584 ipa_get_member_ptr_load_param (tree rhs)
585 {
586   tree rec, fld;
587   tree ptr_field;
588
589   if (TREE_CODE (rhs) != COMPONENT_REF)
590     return NULL_TREE;
591
592   rec = TREE_OPERAND (rhs, 0);
593   if (TREE_CODE (rec) != PARM_DECL
594       || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, NULL))
595     return NULL_TREE;
596
597   fld = TREE_OPERAND (rhs, 1);
598   if (fld == ptr_field)
599     return rec;
600   else
601     return NULL_TREE;
602 }
603
604 /* If STMT looks like a statement loading a value from a member pointer formal
605    parameter, this function returns that parameter.  */
606
607 static tree
608 ipa_get_stmt_member_ptr_load_param (gimple stmt)
609 {
610   tree rhs;
611
612   if (!gimple_assign_single_p (stmt))
613     return NULL_TREE;
614
615   rhs = gimple_assign_rhs1 (stmt);
616   return ipa_get_member_ptr_load_param (rhs);
617 }
618
619 /* Returns true iff T is an SSA_NAME defined by a statement.  */
620
621 static bool
622 ipa_is_ssa_with_stmt_def (tree t)
623 {
624   if (TREE_CODE (t) == SSA_NAME
625       && !SSA_NAME_IS_DEFAULT_DEF (t))
626     return true;
627   else
628     return false;
629 }
630
631 /* Creates a new note describing a call to a parameter number FORMAL_ID and
632    attaches it to the linked list of INFO.  It also sets the called flag of the
633    parameter.  STMT is the corresponding call statement.  */
634
635 static void
636 ipa_note_param_call (struct ipa_node_params *info, int formal_id,
637                      gimple stmt)
638 {
639   struct ipa_param_call_note *note;
640   basic_block bb = gimple_bb (stmt);
641
642   info->params[formal_id].called = 1;
643
644   note = XCNEW (struct ipa_param_call_note);
645   note->formal_id = formal_id;
646   note->stmt = stmt;
647   note->count = bb->count;
648   note->frequency = compute_call_stmt_bb_frequency (current_function_decl, bb);
649
650   note->next = info->param_calls;
651   info->param_calls = note;
652
653   return;
654 }
655
656 /* Analyze the CALL and examine uses of formal parameters of the caller
657    (described by INFO).  Currently it checks whether the call calls a pointer
658    that is a formal parameter and if so, the parameter is marked with the
659    called flag and a note describing the call is created.  This is very simple
660    for ordinary pointers represented in SSA but not-so-nice when it comes to
661    member pointers.  The ugly part of this function does nothing more than
662    tries to match the pattern of such a call.  An example of such a pattern is
663    the gimple dump below, the call is on the last line:
664
665      <bb 2>:
666        f$__delta_5 = f.__delta;
667        f$__pfn_24 = f.__pfn;
668        D.2496_3 = (int) f$__pfn_24;
669        D.2497_4 = D.2496_3 & 1;
670        if (D.2497_4 != 0)
671          goto <bb 3>;
672        else
673          goto <bb 4>;
674
675      <bb 3>:
676        D.2500_7 = (unsigned int) f$__delta_5;
677        D.2501_8 = &S + D.2500_7;
678        D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
679        D.2503_10 = *D.2502_9;
680        D.2504_12 = f$__pfn_24 + -1;
681        D.2505_13 = (unsigned int) D.2504_12;
682        D.2506_14 = D.2503_10 + D.2505_13;
683        D.2507_15 = *D.2506_14;
684        iftmp.11_16 = (String:: *) D.2507_15;
685
686      <bb 4>:
687        # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
688        D.2500_19 = (unsigned int) f$__delta_5;
689        D.2508_20 = &S + D.2500_19;
690        D.2493_21 = iftmp.11_1 (D.2508_20, 4);
691
692    Such patterns are results of simple calls to a member pointer:
693
694      int doprinting (int (MyString::* f)(int) const)
695      {
696        MyString S ("somestring");
697
698        return (S.*f)(4);
699      }
700 */
701
702 static void
703 ipa_analyze_call_uses (struct ipa_node_params *info, gimple call)
704 {
705   tree target = gimple_call_fn (call);
706   gimple def;
707   tree var;
708   tree n1, n2;
709   gimple d1, d2;
710   tree rec, rec2, cond;
711   gimple branch;
712   int index;
713   basic_block bb, virt_bb, join;
714
715   if (TREE_CODE (target) != SSA_NAME)
716     return;
717
718   var = SSA_NAME_VAR (target);
719   if (SSA_NAME_IS_DEFAULT_DEF (target))
720     {
721       /* assuming TREE_CODE (var) == PARM_DECL */
722       index = ipa_get_param_decl_index (info, var);
723       if (index >= 0)
724         ipa_note_param_call (info, index, call);
725       return;
726     }
727
728   /* Now we need to try to match the complex pattern of calling a member
729      pointer. */
730
731   if (!POINTER_TYPE_P (TREE_TYPE (target))
732       || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
733     return;
734
735   def = SSA_NAME_DEF_STMT (target);
736   if (gimple_code (def) != GIMPLE_PHI)
737     return;
738
739   if (gimple_phi_num_args (def) != 2)
740     return;
741
742   /* First, we need to check whether one of these is a load from a member
743      pointer that is a parameter to this function. */
744   n1 = PHI_ARG_DEF (def, 0);
745   n2 = PHI_ARG_DEF (def, 1);
746   if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
747     return;
748   d1 = SSA_NAME_DEF_STMT (n1);
749   d2 = SSA_NAME_DEF_STMT (n2);
750
751   if ((rec = ipa_get_stmt_member_ptr_load_param (d1)))
752     {
753       if (ipa_get_stmt_member_ptr_load_param (d2))
754         return;
755
756       bb = gimple_bb (d1);
757       virt_bb = gimple_bb (d2);
758     }
759   else if ((rec = ipa_get_stmt_member_ptr_load_param (d2)))
760     {
761       bb = gimple_bb (d2);
762       virt_bb = gimple_bb (d1);
763     }
764   else
765     return;
766
767   /* Second, we need to check that the basic blocks are laid out in the way
768      corresponding to the pattern. */
769
770   join = gimple_bb (def);
771   if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
772       || single_pred (virt_bb) != bb
773       || single_succ (virt_bb) != join)
774     return;
775
776   /* Third, let's see that the branching is done depending on the least
777      significant bit of the pfn. */
778
779   branch = last_stmt (bb);
780   if (gimple_code (branch) != GIMPLE_COND)
781     return;
782
783   if (gimple_cond_code (branch) != NE_EXPR
784       || !integer_zerop (gimple_cond_rhs (branch)))
785     return;
786
787   cond = gimple_cond_lhs (branch);
788   if (!ipa_is_ssa_with_stmt_def (cond))
789     return;
790
791   def = SSA_NAME_DEF_STMT (cond);
792   if (!is_gimple_assign (def)
793       || gimple_assign_rhs_code (def) != BIT_AND_EXPR
794       || !integer_onep (gimple_assign_rhs2 (def)))
795     return;
796
797   cond = gimple_assign_rhs1 (def);
798   if (!ipa_is_ssa_with_stmt_def (cond))
799     return;
800
801   def = SSA_NAME_DEF_STMT (cond);
802
803   if (is_gimple_assign (def)
804       && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
805     {
806       cond = gimple_assign_rhs1 (def);
807       if (!ipa_is_ssa_with_stmt_def (cond))
808         return;
809       def = SSA_NAME_DEF_STMT (cond);
810     }
811
812   rec2 = ipa_get_stmt_member_ptr_load_param (def);
813   if (rec != rec2)
814     return;
815
816   index = ipa_get_param_decl_index (info, rec);
817   if (index >= 0 && !ipa_is_param_modified (info, index))
818     ipa_note_param_call (info, index, call);
819
820   return;
821 }
822
823 /* Analyze the statement STMT with respect to formal parameters (described in
824    INFO) and their uses.  Currently it only checks whether formal parameters
825    are called.  */
826
827 static void
828 ipa_analyze_stmt_uses (struct ipa_node_params *info, gimple stmt)
829 {
830   if (is_gimple_call (stmt))
831     ipa_analyze_call_uses (info, stmt);
832 }
833
834 /* Scan the function body of NODE and inspect the uses of formal parameters.
835    Store the findings in various structures of the associated ipa_node_params
836    structure, such as parameter flags, notes etc.  */
837
838 void
839 ipa_analyze_params_uses (struct cgraph_node *node)
840 {
841   tree decl = node->decl;
842   basic_block bb;
843   struct function *func;
844   gimple_stmt_iterator gsi;
845   struct ipa_node_params *info = IPA_NODE_REF (node);
846
847   if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
848     return;
849
850   func = DECL_STRUCT_FUNCTION (decl);
851   FOR_EACH_BB_FN (bb, func)
852     {
853       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
854         {
855           gimple stmt = gsi_stmt (gsi);
856           ipa_analyze_stmt_uses (info, stmt);
857         }
858     }
859
860   info->uses_analysis_done = 1;
861 }
862
863 /* Update the jump functions associated with call graph edge E when the call
864    graph edge CS is being inlined, assuming that E->caller is already (possibly
865    indirectly) inlined into CS->callee and that E has not been inlined.  */
866
867 static void
868 update_jump_functions_after_inlining (struct cgraph_edge *cs,
869                                       struct cgraph_edge *e)
870 {
871   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
872   struct ipa_edge_args *args = IPA_EDGE_REF (e);
873   int count = ipa_get_cs_argument_count (args);
874   int i;
875
876   for (i = 0; i < count; i++)
877     {
878       struct ipa_jump_func *src, *dst = ipa_get_ith_jump_func (args, i);
879
880       if (dst->type != IPA_JF_PASS_THROUGH)
881         continue;
882
883       /* We must check range due to calls with variable number of arguments:  */
884       if (dst->value.formal_id >= (unsigned) ipa_get_cs_argument_count (top))
885         {
886           dst->type = IPA_JF_UNKNOWN;
887           continue;
888         }
889
890       src = ipa_get_ith_jump_func (top, dst->value.formal_id);
891       *dst = *src;
892     }
893 }
894
895 /* Print out a debug message to file F that we have discovered that an indirect
896    call described by NT is in fact a call of a known constant function described
897    by JFUNC.  NODE is the node where the call is.  */
898
899 static void
900 print_edge_addition_message (FILE *f, struct ipa_param_call_note *nt,
901                              struct ipa_jump_func *jfunc,
902                              struct cgraph_node *node)
903 {
904   fprintf (f, "ipa-prop: Discovered an indirect call to a known target (");
905   if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
906     {
907       print_node_brief (f, "", jfunc->value.member_cst.pfn, 0);
908       print_node_brief (f, ", ", jfunc->value.member_cst.delta, 0);
909     }
910   else
911     print_node_brief(f, "", jfunc->value.constant, 0);
912
913   fprintf (f, ") in %s: ", cgraph_node_name (node));
914   print_gimple_stmt (f, nt->stmt, 2, TDF_SLIM);
915 }
916
917 /* Update the param called notes associated with NODE when CS is being inlined,
918    assuming NODE is (potentially indirectly) inlined into CS->callee.
919    Moreover, if the callee is discovered to be constant, create a new cgraph
920    edge for it.  Newly discovered indirect edges will be added to *NEW_EDGES,
921    unless NEW_EDGES is NULL.  Return true iff a new edge(s) were created.  */
922
923 static bool
924 update_call_notes_after_inlining (struct cgraph_edge *cs,
925                                   struct cgraph_node *node,
926                                   VEC (cgraph_edge_p, heap) **new_edges)
927 {
928   struct ipa_node_params *info = IPA_NODE_REF (node);
929   struct ipa_edge_args *top = IPA_EDGE_REF (cs);
930   struct ipa_param_call_note *nt;
931   bool res = false;
932
933   for (nt = info->param_calls; nt; nt = nt->next)
934     {
935       struct ipa_jump_func *jfunc;
936
937       if (nt->processed)
938         continue;
939
940       /* We must check range due to calls with variable number of arguments:  */
941       if (nt->formal_id >= (unsigned) ipa_get_cs_argument_count (top))
942         {
943           nt->processed = true;
944           continue;
945         }
946
947       jfunc = ipa_get_ith_jump_func (top, nt->formal_id);
948       if (jfunc->type == IPA_JF_PASS_THROUGH)
949         nt->formal_id = jfunc->value.formal_id;
950       else if (jfunc->type == IPA_JF_CONST
951                || jfunc->type == IPA_JF_CONST_MEMBER_PTR)
952         {
953           struct cgraph_node *callee;
954           struct cgraph_edge *new_indirect_edge;
955           tree decl;
956
957           nt->processed = true;
958           if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
959             decl = jfunc->value.member_cst.pfn;
960           else
961             decl = jfunc->value.constant;
962
963           if (TREE_CODE (decl) != ADDR_EXPR)
964             continue;
965           decl = TREE_OPERAND (decl, 0);
966
967           if (TREE_CODE (decl) != FUNCTION_DECL)
968             continue;
969           callee = cgraph_node (decl);
970           if (!callee || !callee->local.inlinable)
971             continue;
972
973           res = true;
974           if (dump_file)
975             print_edge_addition_message (dump_file, nt, jfunc, node);
976
977           new_indirect_edge = cgraph_create_edge (node, callee, nt->stmt,
978                                                   nt->count, nt->frequency,
979                                                   nt->loop_nest);
980           new_indirect_edge->indirect_call = 1;
981           ipa_check_create_edge_args ();
982           if (new_edges)
983             VEC_safe_push (cgraph_edge_p, heap, *new_edges, new_indirect_edge);
984           top = IPA_EDGE_REF (cs);
985         }
986     }
987   return res;
988 }
989
990 /* Recursively traverse subtree of NODE (including node) made of inlined
991    cgraph_edges when CS has been inlined and invoke
992    update_call_notes_after_inlining on all nodes and
993    update_jump_functions_after_inlining on all non-inlined edges that lead out
994    of this subtree.  Newly discovered indirect edges will be added to
995    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were
996    created.  */
997
998 static bool
999 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1000                                    struct cgraph_node *node,
1001                                    VEC (cgraph_edge_p, heap) **new_edges)
1002 {
1003   struct cgraph_edge *e;
1004   bool res;
1005
1006   res = update_call_notes_after_inlining (cs, node, new_edges);
1007
1008   for (e = node->callees; e; e = e->next_callee)
1009     if (!e->inline_failed)
1010       res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1011     else
1012       update_jump_functions_after_inlining (cs, e);
1013
1014   return res;
1015 }
1016
1017 /* Update jump functions and call note functions on inlining the call site CS.
1018    CS is expected to lead to a node already cloned by
1019    cgraph_clone_inline_nodes.  Newly discovered indirect edges will be added to
1020    *NEW_EDGES, unless NEW_EDGES is NULL.  Return true iff a new edge(s) were +
1021    created.  */
1022
1023 bool
1024 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1025                                    VEC (cgraph_edge_p, heap) **new_edges)
1026 {
1027   /* Do nothing if the preparation phase has not been carried out yet
1028      (i.e. during early inlining).  */
1029   if (!ipa_node_params_vector)
1030     return false;
1031   gcc_assert (ipa_edge_args_vector);
1032
1033   return propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1034 }
1035
1036 /* Frees all dynamically allocated structures that the argument info points
1037    to.  */
1038
1039 void
1040 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1041 {
1042   if (args->jump_functions)
1043     free (args->jump_functions);
1044
1045   memset (args, 0, sizeof (*args));
1046 }
1047
1048 /* Free all ipa_edge structures.  */
1049
1050 void
1051 ipa_free_all_edge_args (void)
1052 {
1053   int i;
1054   struct ipa_edge_args *args;
1055
1056   for (i = 0;
1057        VEC_iterate (ipa_edge_args_t, ipa_edge_args_vector, i, args);
1058        i++)
1059     ipa_free_edge_args_substructures (args);
1060
1061   VEC_free (ipa_edge_args_t, heap, ipa_edge_args_vector);
1062   ipa_edge_args_vector = NULL;
1063 }
1064
1065 /* Frees all dynamically allocated structures that the param info points
1066    to.  */
1067
1068 void
1069 ipa_free_node_params_substructures (struct ipa_node_params *info)
1070 {
1071   if (info->params)
1072     free (info->params);
1073
1074   while (info->param_calls)
1075     {
1076       struct ipa_param_call_note *note = info->param_calls;
1077       info->param_calls = note->next;
1078       free (note);
1079     }
1080
1081   memset (info, 0, sizeof (*info));
1082 }
1083
1084 /* Free all ipa_node_params structures.  */
1085
1086 void
1087 ipa_free_all_node_params (void)
1088 {
1089   int i;
1090   struct ipa_node_params *info;
1091
1092   for (i = 0;
1093        VEC_iterate (ipa_node_params_t, ipa_node_params_vector, i, info);
1094        i++)
1095     ipa_free_node_params_substructures (info);
1096
1097   VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1098   ipa_node_params_vector = NULL;
1099 }
1100
1101 /* Hook that is called by cgraph.c when an edge is removed.  */
1102
1103 static void
1104 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1105 {
1106   /* During IPA-CP updating we can be called on not-yet analyze clones.  */
1107   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1108       <= (unsigned)cs->uid)
1109     return;
1110   ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1111 }
1112
1113 /* Hook that is called by cgraph.c when a node is removed.  */
1114
1115 static void
1116 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1117 {
1118   ipa_free_node_params_substructures (IPA_NODE_REF (node));
1119 }
1120
1121 /* Helper function to duplicate an array of size N that is at SRC and store a
1122    pointer to it to DST.  Nothing is done if SRC is NULL.  */
1123
1124 static void *
1125 duplicate_array (void *src, size_t n)
1126 {
1127   void *p;
1128
1129   if (!src)
1130     return NULL;
1131
1132   p = xcalloc (1, n);
1133   memcpy (p, src, n);
1134   return p;
1135 }
1136
1137 /* Hook that is called by cgraph.c when a node is duplicated.  */
1138
1139 static void
1140 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1141                            __attribute__((unused)) void *data)
1142 {
1143   struct ipa_edge_args *old_args, *new_args;
1144   int arg_count;
1145
1146   ipa_check_create_edge_args ();
1147
1148   old_args = IPA_EDGE_REF (src);
1149   new_args = IPA_EDGE_REF (dst);
1150
1151   arg_count = ipa_get_cs_argument_count (old_args);
1152   ipa_set_cs_argument_count (new_args, arg_count);
1153   new_args->jump_functions = (struct ipa_jump_func *)
1154     duplicate_array (old_args->jump_functions,
1155                      sizeof (struct ipa_jump_func) * arg_count);
1156 }
1157
1158 /* Hook that is called by cgraph.c when a node is duplicated.  */
1159
1160 static void
1161 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1162                            __attribute__((unused)) void *data)
1163 {
1164   struct ipa_node_params *old_info, *new_info;
1165   struct ipa_param_call_note *note;
1166   int param_count;
1167
1168   ipa_check_create_node_params ();
1169   old_info = IPA_NODE_REF (src);
1170   new_info = IPA_NODE_REF (dst);
1171   param_count = ipa_get_param_count (old_info);
1172
1173   ipa_set_param_count (new_info, param_count);
1174   new_info->params = (struct ipa_param_descriptor *)
1175     duplicate_array (old_info->params,
1176                      sizeof (struct ipa_param_descriptor) * param_count);
1177   new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1178   new_info->count_scale = old_info->count_scale;
1179
1180   for (note = old_info->param_calls; note; note = note->next)
1181     {
1182       struct ipa_param_call_note *nn;
1183
1184       nn = (struct ipa_param_call_note *)
1185         xcalloc (1, sizeof (struct ipa_param_call_note));
1186       memcpy (nn, note, sizeof (struct ipa_param_call_note));
1187       nn->next = new_info->param_calls;
1188       new_info->param_calls = nn;
1189     }
1190 }
1191
1192 /* Register our cgraph hooks if they are not already there.  */
1193
1194 void
1195 ipa_register_cgraph_hooks (void)
1196 {
1197   if (!edge_removal_hook_holder)
1198     edge_removal_hook_holder =
1199       cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1200   if (!node_removal_hook_holder)
1201     node_removal_hook_holder =
1202       cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1203   if (!edge_duplication_hook_holder)
1204     edge_duplication_hook_holder =
1205       cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1206   if (!node_duplication_hook_holder)
1207     node_duplication_hook_holder =
1208       cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1209 }
1210
1211 /* Unregister our cgraph hooks if they are not already there.  */
1212
1213 static void
1214 ipa_unregister_cgraph_hooks (void)
1215 {
1216   cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1217   edge_removal_hook_holder = NULL;
1218   cgraph_remove_node_removal_hook (node_removal_hook_holder);
1219   node_removal_hook_holder = NULL;
1220   cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1221   edge_duplication_hook_holder = NULL;
1222   cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1223   node_duplication_hook_holder = NULL;
1224 }
1225
1226 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1227    longer needed after ipa-cp.  */
1228
1229 void
1230 free_all_ipa_structures_after_ipa_cp (void)
1231 {
1232   if (!flag_indirect_inlining)
1233     {
1234       ipa_free_all_edge_args ();
1235       ipa_free_all_node_params ();
1236       ipa_unregister_cgraph_hooks ();
1237     }
1238 }
1239
1240 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1241    longer needed after indirect inlining.  */
1242
1243 void
1244 free_all_ipa_structures_after_iinln (void)
1245 {
1246   ipa_free_all_edge_args ();
1247   ipa_free_all_node_params ();
1248   ipa_unregister_cgraph_hooks ();
1249 }
1250
1251 /* Print ipa_tree_map data structures of all functions in the
1252    callgraph to F.  */
1253
1254 void
1255 ipa_print_node_params (FILE * f, struct cgraph_node *node)
1256 {
1257   int i, count;
1258   tree temp;
1259   struct ipa_node_params *info;
1260
1261   if (!node->analyzed)
1262     return;
1263   info = IPA_NODE_REF (node);
1264   fprintf (f, "  function  %s Trees :: \n", cgraph_node_name (node));
1265   count = ipa_get_param_count (info);
1266   for (i = 0; i < count; i++)
1267     {
1268       temp = ipa_get_param (info, i);
1269       if (TREE_CODE (temp) == PARM_DECL)
1270         fprintf (f, "    param %d : %s", i,
1271                  (*lang_hooks.decl_printable_name) (temp, 2));
1272       if (ipa_is_param_modified (info, i))
1273         fprintf (f, " modified");
1274       if (ipa_is_param_called (info, i))
1275         fprintf (f, " called");
1276       fprintf (f, "\n");
1277     }
1278 }
1279
1280 /* Print ipa_tree_map data structures of all functions in the
1281    callgraph to F.  */
1282
1283 void
1284 ipa_print_all_params (FILE * f)
1285 {
1286   struct cgraph_node *node;
1287
1288   fprintf (f, "\nFunction parameters:\n");
1289   for (node = cgraph_nodes; node; node = node->next)
1290     ipa_print_node_params (f, node);
1291 }