OSDN Git Service

Remove duplicate ".endfunc".
[pf3gnuchains/gcc-fork.git] / gcc / ipa-split.c
1 /* Function splitting pass
2    Copyright (C) 2010, 2011
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka  <jh@suse.cz>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* The purpose of this pass is to split function bodies to improve
23    inlining.  I.e. for function of the form:
24
25    func (...)
26      {
27        if (cheap_test)
28          something_small
29        else
30          something_big
31      }
32
33    Produce:
34
35    func.part (...)
36      {
37         something_big
38      }
39
40    func (...)
41      {
42        if (cheap_test)
43          something_small
44        else
45          func.part (...);
46      }
47
48    When func becomes inlinable and when cheap_test is often true, inlining func,
49    but not fund.part leads to performance improvement similar as inlining
50    original func while the code size growth is smaller.
51
52    The pass is organized in three stages:
53    1) Collect local info about basic block into BB_INFO structure and
54       compute function body estimated size and time.
55    2) Via DFS walk find all possible basic blocks where we can split
56       and chose best one.
57    3) If split point is found, split at the specified BB by creating a clone
58       and updating function to call it.  
59
60    The decisions what functions to split are in execute_split_functions
61    and consider_split.  
62
63    There are several possible future improvements for this pass including:
64
65    1) Splitting to break up large functions
66    2) Splitting to reduce stack frame usage
67    3) Allow split part of function to use values computed in the header part.
68       The values needs to be passed to split function, perhaps via same
69       interface as for nested functions or as argument.
70    4) Support for simple rematerialization.  I.e. when split part use
71       value computed in header from function parameter in very cheap way, we
72       can just recompute it.
73    5) Support splitting of nested functions.
74    6) Support non-SSA arguments.  
75    7) There is nothing preventing us from producing multiple parts of single function
76       when needed or splitting also the parts.  */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tree.h"
82 #include "target.h"
83 #include "cgraph.h"
84 #include "ipa-prop.h"
85 #include "tree-flow.h"
86 #include "tree-pass.h"
87 #include "flags.h"
88 #include "timevar.h"
89 #include "diagnostic.h"
90 #include "tree-dump.h"
91 #include "tree-inline.h"
92 #include "fibheap.h"
93 #include "params.h"
94 #include "gimple-pretty-print.h"
95
96 /* Per basic block info.  */
97
98 typedef struct
99 {
100   unsigned int size;
101   unsigned int time;
102 } bb_info;
103 DEF_VEC_O(bb_info);
104 DEF_VEC_ALLOC_O(bb_info,heap);
105
106 static VEC(bb_info, heap) *bb_info_vec;
107
108 /* Description of split point.  */
109
110 struct split_point
111 {
112   /* Size of the partitions.  */
113   unsigned int header_time, header_size, split_time, split_size;
114
115   /* SSA names that need to be passed into spit function.  */
116   bitmap ssa_names_to_pass;
117
118   /* Basic block where we split (that will become entry point of new function.  */
119   basic_block entry_bb;
120
121   /* Basic blocks we are splitting away.  */
122   bitmap split_bbs;
123
124   /* True when return value is computed on split part and thus it needs
125      to be returned.  */
126   bool split_part_set_retval;
127 };
128
129 /* Best split point found.  */
130
131 struct split_point best_split_point;
132
133 /* Set of basic blocks that are not allowed to dominate a split point.  */
134
135 static bitmap forbidden_dominators;
136
137 static tree find_retval (basic_block return_bb);
138
139 /* Callback for walk_stmt_load_store_addr_ops.  If T is non-SSA automatic
140    variable, check it if it is present in bitmap passed via DATA.  */
141
142 static bool
143 test_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
144 {
145   t = get_base_address (t);
146
147   if (!t || is_gimple_reg (t))
148     return false;
149
150   if (TREE_CODE (t) == PARM_DECL
151       || (TREE_CODE (t) == VAR_DECL
152           && auto_var_in_fn_p (t, current_function_decl))
153       || TREE_CODE (t) == RESULT_DECL
154       || TREE_CODE (t) == LABEL_DECL)
155     return bitmap_bit_p ((bitmap)data, DECL_UID (t));
156
157   /* For DECL_BY_REFERENCE, the return value is actually a pointer.  We want
158      to pretend that the value pointed to is actual result decl.  */
159   if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
160       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
161       && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
162       && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
163     return
164       bitmap_bit_p ((bitmap)data,
165                     DECL_UID (DECL_RESULT (current_function_decl)));
166
167   return false;
168 }
169
170 /* Dump split point CURRENT.  */
171
172 static void
173 dump_split_point (FILE * file, struct split_point *current)
174 {
175   fprintf (file,
176            "Split point at BB %i header time:%i header size: %i"
177            " split time: %i split size: %i\n  bbs: ",
178            current->entry_bb->index, current->header_time,
179            current->header_size, current->split_time, current->split_size);
180   dump_bitmap (file, current->split_bbs);
181   fprintf (file, "  SSA names to pass: ");
182   dump_bitmap (file, current->ssa_names_to_pass);
183 }
184
185 /* Look for all BBs in header that might lead to the split part and verify
186    that they are not defining any non-SSA var used by the split part.
187    Parameters are the same as for consider_split.  */
188
189 static bool
190 verify_non_ssa_vars (struct split_point *current, bitmap non_ssa_vars,
191                      basic_block return_bb)
192 {
193   bitmap seen = BITMAP_ALLOC (NULL);
194   VEC (basic_block,heap) *worklist = NULL;
195   edge e;
196   edge_iterator ei;
197   bool ok = true;
198
199   FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
200     if (e->src != ENTRY_BLOCK_PTR
201         && !bitmap_bit_p (current->split_bbs, e->src->index))
202       {
203         VEC_safe_push (basic_block, heap, worklist, e->src);
204         bitmap_set_bit (seen, e->src->index);
205       }
206
207   while (!VEC_empty (basic_block, worklist))
208     {
209       gimple_stmt_iterator bsi;
210       basic_block bb = VEC_pop (basic_block, worklist);
211
212       FOR_EACH_EDGE (e, ei, bb->preds)
213         if (e->src != ENTRY_BLOCK_PTR
214             && bitmap_set_bit (seen, e->src->index))
215           {
216             gcc_checking_assert (!bitmap_bit_p (current->split_bbs,
217                                                 e->src->index));
218             VEC_safe_push (basic_block, heap, worklist, e->src);
219           }
220       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
221         {
222           gimple stmt = gsi_stmt (bsi);
223           if (is_gimple_debug (stmt))
224             continue;
225           if (walk_stmt_load_store_addr_ops
226               (stmt, non_ssa_vars, test_nonssa_use, test_nonssa_use,
227                test_nonssa_use))
228             {
229               ok = false;
230               goto done;
231             }
232           if (gimple_code (stmt) == GIMPLE_LABEL
233               && test_nonssa_use (stmt, gimple_label_label (stmt),
234                                   non_ssa_vars))
235           {
236             ok = false;
237             goto done;
238           }
239         }
240       for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
241         {
242           if (walk_stmt_load_store_addr_ops
243               (gsi_stmt (bsi), non_ssa_vars, test_nonssa_use, test_nonssa_use,
244                test_nonssa_use))
245             {
246               ok = false;
247               goto done;
248             }
249         }
250       FOR_EACH_EDGE (e, ei, bb->succs)
251         {
252           if (e->dest != return_bb)
253             continue;
254           for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi);
255                gsi_next (&bsi))
256             {
257               gimple stmt = gsi_stmt (bsi);
258               tree op = gimple_phi_arg_def (stmt, e->dest_idx);
259
260               if (!is_gimple_reg (gimple_phi_result (stmt)))
261                 continue;
262               if (TREE_CODE (op) != SSA_NAME
263                   && test_nonssa_use (stmt, op, non_ssa_vars))
264                 {
265                   ok = false;
266                   goto done;
267                 }
268             }
269         }
270     }
271 done:
272   BITMAP_FREE (seen);
273   VEC_free (basic_block, heap, worklist);
274   return ok;
275 }
276
277 /* If STMT is a call, check the callee against a list of forbidden
278    predicate functions.  If a match is found, look for uses of the
279    call result in condition statements that compare against zero.
280    For each such use, find the block targeted by the condition
281    statement for the nonzero result, and set the bit for this block
282    in the forbidden dominators bitmap.  The purpose of this is to avoid
283    selecting a split point where we are likely to lose the chance
284    to optimize away an unused function call.  */
285
286 static void
287 check_forbidden_calls (gimple stmt)
288 {
289   imm_use_iterator use_iter;
290   use_operand_p use_p;
291   tree lhs;
292
293   /* At the moment, __builtin_constant_p is the only forbidden
294      predicate function call (see PR49642).  */
295   if (!gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))
296     return;
297
298   lhs = gimple_call_lhs (stmt);
299
300   if (!lhs || TREE_CODE (lhs) != SSA_NAME)
301     return;
302
303   FOR_EACH_IMM_USE_FAST (use_p, use_iter, lhs)
304     {
305       tree op1;
306       basic_block use_bb, forbidden_bb;
307       enum tree_code code;
308       edge true_edge, false_edge;
309       gimple use_stmt = USE_STMT (use_p);
310
311       if (gimple_code (use_stmt) != GIMPLE_COND)
312         continue;
313
314       /* Assuming canonical form for GIMPLE_COND here, with constant
315          in second position.  */
316       op1 = gimple_cond_rhs (use_stmt);
317       code = gimple_cond_code (use_stmt);
318       use_bb = gimple_bb (use_stmt);
319
320       extract_true_false_edges_from_block (use_bb, &true_edge, &false_edge);
321
322       /* We're only interested in comparisons that distinguish
323          unambiguously from zero.  */
324       if (!integer_zerop (op1) || code == LE_EXPR || code == GE_EXPR)
325         continue;
326
327       if (code == EQ_EXPR)
328         forbidden_bb = false_edge->dest;
329       else
330         forbidden_bb = true_edge->dest;
331
332       bitmap_set_bit (forbidden_dominators, forbidden_bb->index);
333     }
334 }
335
336 /* If BB is dominated by any block in the forbidden dominators set,
337    return TRUE; else FALSE.  */
338
339 static bool
340 dominated_by_forbidden (basic_block bb)
341 {
342   unsigned dom_bb;
343   bitmap_iterator bi;
344
345   EXECUTE_IF_SET_IN_BITMAP (forbidden_dominators, 1, dom_bb, bi)
346     {
347       if (dominated_by_p (CDI_DOMINATORS, bb, BASIC_BLOCK (dom_bb)))
348         return true;
349     }
350
351   return false;
352 }
353
354 /* We found an split_point CURRENT.  NON_SSA_VARS is bitmap of all non ssa
355    variables used and RETURN_BB is return basic block.
356    See if we can split function here.  */
357
358 static void
359 consider_split (struct split_point *current, bitmap non_ssa_vars,
360                 basic_block return_bb)
361 {
362   tree parm;
363   unsigned int num_args = 0;
364   unsigned int call_overhead;
365   edge e;
366   edge_iterator ei;
367   gimple_stmt_iterator bsi;
368   unsigned int i;
369   int incoming_freq = 0;
370   tree retval;
371
372   if (dump_file && (dump_flags & TDF_DETAILS))
373     dump_split_point (dump_file, current);
374
375   FOR_EACH_EDGE (e, ei, current->entry_bb->preds)
376     if (!bitmap_bit_p (current->split_bbs, e->src->index))
377       incoming_freq += EDGE_FREQUENCY (e);
378
379   /* Do not split when we would end up calling function anyway.  */
380   if (incoming_freq
381       >= (ENTRY_BLOCK_PTR->frequency
382           * PARAM_VALUE (PARAM_PARTIAL_INLINING_ENTRY_PROBABILITY) / 100))
383     {
384       if (dump_file && (dump_flags & TDF_DETAILS))
385         fprintf (dump_file,
386                  "  Refused: incoming frequency is too large.\n");
387       return;
388     }
389
390   if (!current->header_size)
391     {
392       if (dump_file && (dump_flags & TDF_DETAILS))
393         fprintf (dump_file, "  Refused: header empty\n");
394       return;
395     }
396
397   /* Verify that PHI args on entry are either virtual or all their operands
398      incoming from header are the same.  */
399   for (bsi = gsi_start_phis (current->entry_bb); !gsi_end_p (bsi); gsi_next (&bsi))
400     {
401       gimple stmt = gsi_stmt (bsi);
402       tree val = NULL;
403
404       if (!is_gimple_reg (gimple_phi_result (stmt)))
405         continue;
406       for (i = 0; i < gimple_phi_num_args (stmt); i++)
407         {
408           edge e = gimple_phi_arg_edge (stmt, i);
409           if (!bitmap_bit_p (current->split_bbs, e->src->index))
410             {
411               tree edge_val = gimple_phi_arg_def (stmt, i);
412               if (val && edge_val != val)
413                 {
414                   if (dump_file && (dump_flags & TDF_DETAILS))
415                     fprintf (dump_file,
416                              "  Refused: entry BB has PHI with multiple variants\n");
417                   return;
418                 }
419               val = edge_val;
420             }
421         }
422     }
423
424
425   /* See what argument we will pass to the split function and compute
426      call overhead.  */
427   call_overhead = eni_size_weights.call_cost;
428   for (parm = DECL_ARGUMENTS (current_function_decl); parm;
429        parm = DECL_CHAIN (parm))
430     {
431       if (!is_gimple_reg (parm))
432         {
433           if (bitmap_bit_p (non_ssa_vars, DECL_UID (parm)))
434             {
435               if (dump_file && (dump_flags & TDF_DETAILS))
436                 fprintf (dump_file,
437                          "  Refused: need to pass non-ssa param values\n");
438               return;
439             }
440         }
441       else if (gimple_default_def (cfun, parm)
442                && bitmap_bit_p (current->ssa_names_to_pass,
443                                 SSA_NAME_VERSION (gimple_default_def
444                                                   (cfun, parm))))
445         {
446           if (!VOID_TYPE_P (TREE_TYPE (parm)))
447             call_overhead += estimate_move_cost (TREE_TYPE (parm));
448           num_args++;
449         }
450     }
451   if (!VOID_TYPE_P (TREE_TYPE (current_function_decl)))
452     call_overhead += estimate_move_cost (TREE_TYPE (current_function_decl));
453
454   if (current->split_size <= call_overhead)
455     {
456       if (dump_file && (dump_flags & TDF_DETAILS))
457         fprintf (dump_file,
458                  "  Refused: split size is smaller than call overhead\n");
459       return;
460     }
461   if (current->header_size + call_overhead
462       >= (unsigned int)(DECL_DECLARED_INLINE_P (current_function_decl)
463                         ? MAX_INLINE_INSNS_SINGLE
464                         : MAX_INLINE_INSNS_AUTO))
465     {
466       if (dump_file && (dump_flags & TDF_DETAILS))
467         fprintf (dump_file,
468                  "  Refused: header size is too large for inline candidate\n");
469       return;
470     }
471
472   /* FIXME: we currently can pass only SSA function parameters to the split
473      arguments.  Once parm_adjustment infrastructure is supported by cloning,
474      we can pass more than that.  */
475   if (num_args != bitmap_count_bits (current->ssa_names_to_pass))
476     {
477       
478       if (dump_file && (dump_flags & TDF_DETAILS))
479         fprintf (dump_file,
480                  "  Refused: need to pass non-param values\n");
481       return;
482     }
483
484   /* When there are non-ssa vars used in the split region, see if they
485      are used in the header region.  If so, reject the split.
486      FIXME: we can use nested function support to access both.  */
487   if (!bitmap_empty_p (non_ssa_vars)
488       && !verify_non_ssa_vars (current, non_ssa_vars, return_bb))
489     {
490       if (dump_file && (dump_flags & TDF_DETAILS))
491         fprintf (dump_file,
492                  "  Refused: split part has non-ssa uses\n");
493       return;
494     }
495
496   /* If the split point is dominated by a forbidden block, reject
497      the split.  */
498   if (!bitmap_empty_p (forbidden_dominators)
499       && dominated_by_forbidden (current->entry_bb))
500     {
501       if (dump_file && (dump_flags & TDF_DETAILS))
502         fprintf (dump_file,
503                  "  Refused: split point dominated by forbidden block\n");
504       return;
505     }
506
507   /* See if retval used by return bb is computed by header or split part.
508      When it is computed by split part, we need to produce return statement
509      in the split part and add code to header to pass it around.
510
511      This is bit tricky to test:
512        1) When there is no return_bb or no return value, we always pass
513           value around.
514        2) Invariants are always computed by caller.
515        3) For SSA we need to look if defining statement is in header or split part
516        4) For non-SSA we need to look where the var is computed. */
517   retval = find_retval (return_bb);
518   if (!retval)
519     current->split_part_set_retval = true;
520   else if (is_gimple_min_invariant (retval))
521     current->split_part_set_retval = false;
522   /* Special case is value returned by reference we record as if it was non-ssa
523      set to result_decl.  */
524   else if (TREE_CODE (retval) == SSA_NAME
525            && TREE_CODE (SSA_NAME_VAR (retval)) == RESULT_DECL
526            && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
527     current->split_part_set_retval
528        = bitmap_bit_p (non_ssa_vars, DECL_UID (SSA_NAME_VAR (retval)));
529   else if (TREE_CODE (retval) == SSA_NAME)
530     current->split_part_set_retval
531       = (!SSA_NAME_IS_DEFAULT_DEF (retval)
532          && (bitmap_bit_p (current->split_bbs,
533                           gimple_bb (SSA_NAME_DEF_STMT (retval))->index)
534              || gimple_bb (SSA_NAME_DEF_STMT (retval)) == return_bb));
535   else if (TREE_CODE (retval) == PARM_DECL)
536     current->split_part_set_retval = false;
537   else if (TREE_CODE (retval) == VAR_DECL
538            || TREE_CODE (retval) == RESULT_DECL)
539     current->split_part_set_retval
540       = bitmap_bit_p (non_ssa_vars, DECL_UID (retval));
541   else
542     current->split_part_set_retval = true;
543
544   /* split_function fixes up at most one PHI non-virtual PHI node in return_bb,
545      for the return value.  If there are other PHIs, give up.  */
546   if (return_bb != EXIT_BLOCK_PTR)
547     {
548       gimple_stmt_iterator psi;
549
550       for (psi = gsi_start_phis (return_bb); !gsi_end_p (psi); gsi_next (&psi))
551         if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi)))
552             && !(retval
553                  && current->split_part_set_retval
554                  && TREE_CODE (retval) == SSA_NAME
555                  && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl))
556                  && SSA_NAME_DEF_STMT (retval) == gsi_stmt (psi)))
557           {
558             if (dump_file && (dump_flags & TDF_DETAILS))
559               fprintf (dump_file,
560                        "  Refused: return bb has extra PHIs\n");
561             return;
562           }
563     }
564
565   if (dump_file && (dump_flags & TDF_DETAILS))
566     fprintf (dump_file, "  Accepted!\n");
567
568   /* At the moment chose split point with lowest frequency and that leaves
569      out smallest size of header.
570      In future we might re-consider this heuristics.  */
571   if (!best_split_point.split_bbs
572       || best_split_point.entry_bb->frequency > current->entry_bb->frequency
573       || (best_split_point.entry_bb->frequency == current->entry_bb->frequency
574           && best_split_point.split_size < current->split_size))
575         
576     {
577       if (dump_file && (dump_flags & TDF_DETAILS))
578         fprintf (dump_file, "  New best split point!\n");
579       if (best_split_point.ssa_names_to_pass)
580         {
581           BITMAP_FREE (best_split_point.ssa_names_to_pass);
582           BITMAP_FREE (best_split_point.split_bbs);
583         }
584       best_split_point = *current;
585       best_split_point.ssa_names_to_pass = BITMAP_ALLOC (NULL);
586       bitmap_copy (best_split_point.ssa_names_to_pass,
587                    current->ssa_names_to_pass);
588       best_split_point.split_bbs = BITMAP_ALLOC (NULL);
589       bitmap_copy (best_split_point.split_bbs, current->split_bbs);
590     }
591 }
592
593 /* Return basic block containing RETURN statement.  We allow basic blocks
594    of the form:
595    <retval> = tmp_var;
596    return <retval>
597    but return_bb can not be more complex than this.
598    If nothing is found, return EXIT_BLOCK_PTR.
599
600    When there are multiple RETURN statement, chose one with return value,
601    since that one is more likely shared by multiple code paths.
602
603    Return BB is special, because for function splitting it is the only
604    basic block that is duplicated in between header and split part of the
605    function.
606
607    TODO: We might support multiple return blocks.  */
608
609 static basic_block
610 find_return_bb (void)
611 {
612   edge e;
613   basic_block return_bb = EXIT_BLOCK_PTR;
614   gimple_stmt_iterator bsi;
615   bool found_return = false;
616   tree retval = NULL_TREE;
617
618   if (!single_pred_p (EXIT_BLOCK_PTR))
619     return return_bb;
620
621   e = single_pred_edge (EXIT_BLOCK_PTR);
622   for (bsi = gsi_last_bb (e->src); !gsi_end_p (bsi); gsi_prev (&bsi))
623     {
624       gimple stmt = gsi_stmt (bsi);
625       if (gimple_code (stmt) == GIMPLE_LABEL || is_gimple_debug (stmt))
626         ;
627       else if (gimple_code (stmt) == GIMPLE_ASSIGN
628                && found_return
629                && gimple_assign_single_p (stmt)
630                && (auto_var_in_fn_p (gimple_assign_rhs1 (stmt),
631                                      current_function_decl)
632                    || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
633                && retval == gimple_assign_lhs (stmt))
634         ;
635       else if (gimple_code (stmt) == GIMPLE_RETURN)
636         {
637           found_return = true;
638           retval = gimple_return_retval (stmt);
639         }
640       else
641         break;
642     }
643   if (gsi_end_p (bsi) && found_return)
644     return_bb = e->src;
645
646   return return_bb;
647 }
648
649 /* Given return basic block RETURN_BB, see where return value is really
650    stored.  */
651 static tree
652 find_retval (basic_block return_bb)
653 {
654   gimple_stmt_iterator bsi;
655   for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
656     if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
657       return gimple_return_retval (gsi_stmt (bsi));
658     else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN)
659       return gimple_assign_rhs1 (gsi_stmt (bsi));
660   return NULL;
661 }
662
663 /* Callback for walk_stmt_load_store_addr_ops.  If T is non-SSA automatic
664    variable, mark it as used in bitmap passed via DATA.
665    Return true when access to T prevents splitting the function.  */
666
667 static bool
668 mark_nonssa_use (gimple stmt ATTRIBUTE_UNUSED, tree t, void *data)
669 {
670   t = get_base_address (t);
671
672   if (!t || is_gimple_reg (t))
673     return false;
674
675   /* At present we can't pass non-SSA arguments to split function.
676      FIXME: this can be relaxed by passing references to arguments.  */
677   if (TREE_CODE (t) == PARM_DECL)
678     {
679       if (dump_file && (dump_flags & TDF_DETAILS))
680         fprintf (dump_file,
681                  "Cannot split: use of non-ssa function parameter.\n");
682       return true;
683     }
684
685   if ((TREE_CODE (t) == VAR_DECL
686        && auto_var_in_fn_p (t, current_function_decl))
687       || TREE_CODE (t) == RESULT_DECL
688       || TREE_CODE (t) == LABEL_DECL)
689     bitmap_set_bit ((bitmap)data, DECL_UID (t));
690
691   /* For DECL_BY_REFERENCE, the return value is actually a pointer.  We want
692      to pretend that the value pointed to is actual result decl.  */
693   if ((TREE_CODE (t) == MEM_REF || INDIRECT_REF_P (t))
694       && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
695       && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (t, 0))) == RESULT_DECL
696       && DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
697     return
698       bitmap_bit_p ((bitmap)data,
699                     DECL_UID (DECL_RESULT (current_function_decl)));
700
701   return false;
702 }
703
704 /* Compute local properties of basic block BB we collect when looking for
705    split points.  We look for ssa defs and store them in SET_SSA_NAMES,
706    for ssa uses and store them in USED_SSA_NAMES and for any non-SSA automatic
707    vars stored in NON_SSA_VARS.
708
709    When BB has edge to RETURN_BB, collect uses in RETURN_BB too.  
710
711    Return false when BB contains something that prevents it from being put into
712    split function.  */
713
714 static bool
715 visit_bb (basic_block bb, basic_block return_bb,
716           bitmap set_ssa_names, bitmap used_ssa_names,
717           bitmap non_ssa_vars)
718 {
719   gimple_stmt_iterator bsi;
720   edge e;
721   edge_iterator ei;
722   bool can_split = true;
723
724   for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
725     {
726       gimple stmt = gsi_stmt (bsi);
727       tree op;
728       ssa_op_iter iter;
729       tree decl;
730
731       if (is_gimple_debug (stmt))
732         continue;
733
734       /* FIXME: We can split regions containing EH.  We can not however
735          split RESX, EH_DISPATCH and EH_POINTER referring to same region
736          into different partitions.  This would require tracking of
737          EH regions and checking in consider_split_point if they 
738          are not used elsewhere.  */
739       if (gimple_code (stmt) == GIMPLE_RESX)
740         {
741           if (dump_file && (dump_flags & TDF_DETAILS))
742             fprintf (dump_file, "Cannot split: resx.\n");
743           can_split = false;
744         }
745       if (gimple_code (stmt) == GIMPLE_EH_DISPATCH)
746         {
747           if (dump_file && (dump_flags & TDF_DETAILS))
748             fprintf (dump_file, "Cannot split: eh dispatch.\n");
749           can_split = false;
750         }
751
752       /* Check builtins that prevent splitting.  */
753       if (gimple_code (stmt) == GIMPLE_CALL
754           && (decl = gimple_call_fndecl (stmt)) != NULL_TREE
755           && DECL_BUILT_IN (decl)
756           && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
757         switch (DECL_FUNCTION_CODE (decl))
758           {
759           /* FIXME: once we will allow passing non-parm values to split part,
760              we need to be sure to handle correct builtin_stack_save and
761              builtin_stack_restore.  At the moment we are safe; there is no
762              way to store builtin_stack_save result in non-SSA variable
763              since all calls to those are compiler generated.  */
764           case BUILT_IN_APPLY:
765           case BUILT_IN_APPLY_ARGS:
766           case BUILT_IN_VA_START:
767             if (dump_file && (dump_flags & TDF_DETAILS))
768               fprintf (dump_file,
769                        "Cannot split: builtin_apply and va_start.\n");
770             can_split = false;
771             break;
772           case BUILT_IN_EH_POINTER:
773             if (dump_file && (dump_flags & TDF_DETAILS))
774               fprintf (dump_file, "Cannot split: builtin_eh_pointer.\n");
775             can_split = false;
776             break;
777           default:
778             break;
779           }
780
781       FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
782         bitmap_set_bit (set_ssa_names, SSA_NAME_VERSION (op));
783       FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
784         bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
785       can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
786                                                    mark_nonssa_use,
787                                                    mark_nonssa_use,
788                                                    mark_nonssa_use);
789     }
790   for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
791     {
792       gimple stmt = gsi_stmt (bsi);
793       unsigned int i;
794
795       if (is_gimple_debug (stmt))
796         continue;
797       if (!is_gimple_reg (gimple_phi_result (stmt)))
798         continue;
799       bitmap_set_bit (set_ssa_names,
800                       SSA_NAME_VERSION (gimple_phi_result (stmt)));
801       for (i = 0; i < gimple_phi_num_args (stmt); i++)
802         {
803           tree op = gimple_phi_arg_def (stmt, i);
804           if (TREE_CODE (op) == SSA_NAME)
805             bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
806         }
807       can_split &= !walk_stmt_load_store_addr_ops (stmt, non_ssa_vars,
808                                                    mark_nonssa_use,
809                                                    mark_nonssa_use,
810                                                    mark_nonssa_use);
811     }
812   /* Record also uses coming from PHI operand in return BB.  */
813   FOR_EACH_EDGE (e, ei, bb->succs)
814     if (e->dest == return_bb)
815       {
816         for (bsi = gsi_start_phis (return_bb); !gsi_end_p (bsi); gsi_next (&bsi))
817           {
818             gimple stmt = gsi_stmt (bsi);
819             tree op = gimple_phi_arg_def (stmt, e->dest_idx);
820
821             if (is_gimple_debug (stmt))
822               continue;
823             if (!is_gimple_reg (gimple_phi_result (stmt)))
824               continue;
825             if (TREE_CODE (op) == SSA_NAME)
826               bitmap_set_bit (used_ssa_names, SSA_NAME_VERSION (op));
827             else
828               can_split &= !mark_nonssa_use (stmt, op, non_ssa_vars);
829           }
830       }
831   return can_split;
832 }
833
834 /* Stack entry for recursive DFS walk in find_split_point.  */
835
836 typedef struct
837 {
838   /* Basic block we are examining.  */
839   basic_block bb;
840
841   /* SSA names set and used by the BB and all BBs reachable
842      from it via DFS walk.  */
843   bitmap set_ssa_names, used_ssa_names;
844   bitmap non_ssa_vars;
845
846   /* All BBS visited from this BB via DFS walk.  */
847   bitmap bbs_visited;
848
849   /* Last examined edge in DFS walk.  Since we walk unoriented graph,
850      the value is up to sum of incoming and outgoing edges of BB.  */
851   unsigned int edge_num;
852
853   /* Stack entry index of earliest BB reachable from current BB
854      or any BB visited later in DFS walk.  */
855   int earliest;
856
857   /* Overall time and size of all BBs reached from this BB in DFS walk.  */
858   int overall_time, overall_size;
859
860   /* When false we can not split on this BB.  */
861   bool can_split;
862 } stack_entry;
863 DEF_VEC_O(stack_entry);
864 DEF_VEC_ALLOC_O(stack_entry,heap);
865
866
867 /* Find all articulations and call consider_split on them.
868    OVERALL_TIME and OVERALL_SIZE is time and size of the function.
869
870    We perform basic algorithm for finding an articulation in a graph
871    created from CFG by considering it to be an unoriented graph.
872
873    The articulation is discovered via DFS walk. We collect earliest
874    basic block on stack that is reachable via backward edge.  Articulation
875    is any basic block such that there is no backward edge bypassing it.
876    To reduce stack usage we maintain heap allocated stack in STACK vector.
877    AUX pointer of BB is set to index it appears in the stack or -1 once
878    it is visited and popped off the stack.
879
880    The algorithm finds articulation after visiting the whole component
881    reachable by it.  This makes it convenient to collect information about
882    the component used by consider_split.  */
883
884 static void
885 find_split_points (int overall_time, int overall_size)
886 {
887   stack_entry first;
888   VEC(stack_entry, heap) *stack = NULL;
889   basic_block bb;
890   basic_block return_bb = find_return_bb ();
891   struct split_point current;
892
893   current.header_time = overall_time;
894   current.header_size = overall_size;
895   current.split_time = 0;
896   current.split_size = 0;
897   current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
898
899   first.bb = ENTRY_BLOCK_PTR;
900   first.edge_num = 0;
901   first.overall_time = 0;
902   first.overall_size = 0;
903   first.earliest = INT_MAX;
904   first.set_ssa_names = 0;
905   first.used_ssa_names = 0;
906   first.bbs_visited = 0;
907   VEC_safe_push (stack_entry, heap, stack, &first);
908   ENTRY_BLOCK_PTR->aux = (void *)(intptr_t)-1;
909
910   while (!VEC_empty (stack_entry, stack))
911     {
912       stack_entry *entry = VEC_last (stack_entry, stack);
913
914       /* We are walking an acyclic graph, so edge_num counts
915          succ and pred edges together.  However when considering
916          articulation, we want to have processed everything reachable
917          from articulation but nothing that reaches into it.  */
918       if (entry->edge_num == EDGE_COUNT (entry->bb->succs)
919           && entry->bb != ENTRY_BLOCK_PTR)
920         {
921           int pos = VEC_length (stack_entry, stack);
922           entry->can_split &= visit_bb (entry->bb, return_bb,
923                                         entry->set_ssa_names,
924                                         entry->used_ssa_names,
925                                         entry->non_ssa_vars);
926           if (pos <= entry->earliest && !entry->can_split
927               && dump_file && (dump_flags & TDF_DETAILS))
928             fprintf (dump_file,
929                      "found articulation at bb %i but can not split\n",
930                      entry->bb->index);
931           if (pos <= entry->earliest && entry->can_split)
932              {
933                if (dump_file && (dump_flags & TDF_DETAILS))
934                  fprintf (dump_file, "found articulation at bb %i\n",
935                           entry->bb->index);
936                current.entry_bb = entry->bb;
937                current.ssa_names_to_pass = BITMAP_ALLOC (NULL);
938                bitmap_and_compl (current.ssa_names_to_pass,
939                                  entry->used_ssa_names, entry->set_ssa_names);
940                current.header_time = overall_time - entry->overall_time;
941                current.header_size = overall_size - entry->overall_size;
942                current.split_time = entry->overall_time;
943                current.split_size = entry->overall_size;
944                current.split_bbs = entry->bbs_visited;
945                consider_split (&current, entry->non_ssa_vars, return_bb);
946                BITMAP_FREE (current.ssa_names_to_pass);
947              }
948         }
949       /* Do actual DFS walk.  */
950       if (entry->edge_num
951           < (EDGE_COUNT (entry->bb->succs)
952              + EDGE_COUNT (entry->bb->preds)))
953         {
954           edge e;
955           basic_block dest;
956           if (entry->edge_num < EDGE_COUNT (entry->bb->succs))
957             {
958               e = EDGE_SUCC (entry->bb, entry->edge_num);
959               dest = e->dest;
960             }
961           else
962             {
963               e = EDGE_PRED (entry->bb, entry->edge_num
964                              - EDGE_COUNT (entry->bb->succs));
965               dest = e->src;
966             }
967
968           entry->edge_num++;
969
970           /* New BB to visit, push it to the stack.  */
971           if (dest != return_bb && dest != EXIT_BLOCK_PTR
972               && !dest->aux)
973             {
974               stack_entry new_entry;
975
976               new_entry.bb = dest;
977               new_entry.edge_num = 0;
978               new_entry.overall_time
979                  = VEC_index (bb_info, bb_info_vec, dest->index)->time;
980               new_entry.overall_size
981                  = VEC_index (bb_info, bb_info_vec, dest->index)->size;
982               new_entry.earliest = INT_MAX;
983               new_entry.set_ssa_names = BITMAP_ALLOC (NULL);
984               new_entry.used_ssa_names = BITMAP_ALLOC (NULL);
985               new_entry.bbs_visited = BITMAP_ALLOC (NULL);
986               new_entry.non_ssa_vars = BITMAP_ALLOC (NULL);
987               new_entry.can_split = true;
988               bitmap_set_bit (new_entry.bbs_visited, dest->index);
989               VEC_safe_push (stack_entry, heap, stack, &new_entry);
990               dest->aux = (void *)(intptr_t)VEC_length (stack_entry, stack);
991             }
992           /* Back edge found, record the earliest point.  */
993           else if ((intptr_t)dest->aux > 0
994                    && (intptr_t)dest->aux < entry->earliest)
995             entry->earliest = (intptr_t)dest->aux;
996         }
997       /* We are done with examining the edges.  Pop off the value from stack
998          and merge stuff we accumulate during the walk.  */
999       else if (entry->bb != ENTRY_BLOCK_PTR)
1000         {
1001           stack_entry *prev = VEC_index (stack_entry, stack,
1002                                          VEC_length (stack_entry, stack) - 2);
1003
1004           entry->bb->aux = (void *)(intptr_t)-1;
1005           prev->can_split &= entry->can_split;
1006           if (prev->set_ssa_names)
1007             {
1008               bitmap_ior_into (prev->set_ssa_names, entry->set_ssa_names);
1009               bitmap_ior_into (prev->used_ssa_names, entry->used_ssa_names);
1010               bitmap_ior_into (prev->bbs_visited, entry->bbs_visited);
1011               bitmap_ior_into (prev->non_ssa_vars, entry->non_ssa_vars);
1012             }
1013           if (prev->earliest > entry->earliest)
1014             prev->earliest = entry->earliest;
1015           prev->overall_time += entry->overall_time;
1016           prev->overall_size += entry->overall_size;
1017           BITMAP_FREE (entry->set_ssa_names);
1018           BITMAP_FREE (entry->used_ssa_names);
1019           BITMAP_FREE (entry->bbs_visited);
1020           BITMAP_FREE (entry->non_ssa_vars);
1021           VEC_pop (stack_entry, stack);
1022         }
1023       else
1024         VEC_pop (stack_entry, stack);
1025     }
1026   ENTRY_BLOCK_PTR->aux = NULL;
1027   FOR_EACH_BB (bb)
1028     bb->aux = NULL;
1029   VEC_free (stack_entry, heap, stack);
1030   BITMAP_FREE (current.ssa_names_to_pass);
1031 }
1032
1033 /* Split function at SPLIT_POINT.  */
1034
1035 static void
1036 split_function (struct split_point *split_point)
1037 {
1038   VEC (tree, heap) *args_to_pass = NULL;
1039   bitmap args_to_skip = BITMAP_ALLOC (NULL);
1040   tree parm;
1041   int num = 0;
1042   struct cgraph_node *node, *cur_node = cgraph_node (current_function_decl);
1043   basic_block return_bb = find_return_bb ();
1044   basic_block call_bb;
1045   gimple_stmt_iterator gsi;
1046   gimple call;
1047   edge e;
1048   edge_iterator ei;
1049   tree retval = NULL, real_retval = NULL;
1050   bool split_part_return_p = false;
1051   gimple last_stmt = NULL;
1052   unsigned int i;
1053   tree arg;
1054
1055   if (dump_file)
1056     {
1057       fprintf (dump_file, "\n\nSplitting function at:\n");
1058       dump_split_point (dump_file, split_point);
1059     }
1060
1061   if (cur_node->local.can_change_signature
1062       && !TYPE_ATTRIBUTES (TREE_TYPE (cur_node->decl)))
1063     args_to_skip = BITMAP_ALLOC (NULL);
1064   else
1065     args_to_skip = NULL;
1066
1067   /* Collect the parameters of new function and args_to_skip bitmap.  */
1068   for (parm = DECL_ARGUMENTS (current_function_decl);
1069        parm; parm = DECL_CHAIN (parm), num++)
1070     if (args_to_skip
1071         && (!is_gimple_reg (parm)
1072             || !gimple_default_def (cfun, parm)
1073             || !bitmap_bit_p (split_point->ssa_names_to_pass,
1074                               SSA_NAME_VERSION (gimple_default_def (cfun,
1075                                                                     parm)))))
1076       bitmap_set_bit (args_to_skip, num);
1077     else
1078       {
1079         /* This parm might not have been used up to now, but is going to be
1080            used, hence register it.  */
1081         add_referenced_var (parm);
1082         if (is_gimple_reg (parm))
1083           {
1084             arg = gimple_default_def (cfun, parm);
1085             if (!arg)
1086               {
1087                 arg = make_ssa_name (parm, gimple_build_nop ());
1088                 set_default_def (parm, arg);
1089               }
1090           }
1091         else
1092           arg = parm;
1093
1094         if (!useless_type_conversion_p (DECL_ARG_TYPE (parm), TREE_TYPE (arg)))
1095           arg = fold_convert (DECL_ARG_TYPE (parm), arg);
1096         VEC_safe_push (tree, heap, args_to_pass, arg);
1097       }
1098
1099   /* See if the split function will return.  */
1100   FOR_EACH_EDGE (e, ei, return_bb->preds)
1101     if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1102       break;
1103   if (e)
1104     split_part_return_p = true;
1105
1106   /* Add return block to what will become the split function.
1107      We do not return; no return block is needed.  */
1108   if (!split_part_return_p)
1109     ;
1110   /* We have no return block, so nothing is needed.  */
1111   else if (return_bb == EXIT_BLOCK_PTR)
1112     ;
1113   /* When we do not want to return value, we need to construct
1114      new return block with empty return statement.
1115      FIXME: Once we are able to change return type, we should change function
1116      to return void instead of just outputting function with undefined return
1117      value.  For structures this affects quality of codegen.  */
1118   else if (!split_point->split_part_set_retval
1119            && find_retval (return_bb))
1120     {
1121       bool redirected = true;
1122       basic_block new_return_bb = create_basic_block (NULL, 0, return_bb);
1123       gimple_stmt_iterator gsi = gsi_start_bb (new_return_bb);
1124       gsi_insert_after (&gsi, gimple_build_return (NULL), GSI_NEW_STMT);
1125       while (redirected)
1126         {
1127           redirected = false;
1128           FOR_EACH_EDGE (e, ei, return_bb->preds)
1129             if (bitmap_bit_p (split_point->split_bbs, e->src->index))
1130               {
1131                 new_return_bb->count += e->count;
1132                 new_return_bb->frequency += EDGE_FREQUENCY (e);
1133                 redirect_edge_and_branch (e, new_return_bb);
1134                 redirected = true;
1135                 break;
1136               }
1137         }
1138       e = make_edge (new_return_bb, EXIT_BLOCK_PTR, 0);
1139       e->probability = REG_BR_PROB_BASE;
1140       e->count = new_return_bb->count;
1141       bitmap_set_bit (split_point->split_bbs, new_return_bb->index);
1142     }
1143   /* When we pass around the value, use existing return block.  */
1144   else
1145     bitmap_set_bit (split_point->split_bbs, return_bb->index);
1146
1147   /* If RETURN_BB has virtual operand PHIs, they must be removed and the
1148      virtual operand marked for renaming as we change the CFG in a way that
1149      tree-inline is not able to compensate for. 
1150
1151      Note this can happen whether or not we have a return value.  If we have
1152      a return value, then RETURN_BB may have PHIs for real operands too.  */
1153   if (return_bb != EXIT_BLOCK_PTR)
1154     {
1155       for (gsi = gsi_start_phis (return_bb); !gsi_end_p (gsi);)
1156         {
1157           gimple stmt = gsi_stmt (gsi);
1158           if (is_gimple_reg (gimple_phi_result (stmt)))
1159             {
1160               gsi_next (&gsi);
1161               continue;
1162             }
1163           mark_virtual_phi_result_for_renaming (stmt);
1164           remove_phi_node (&gsi, true);
1165         }
1166     }
1167
1168   /* Now create the actual clone.  */
1169   rebuild_cgraph_edges ();
1170   node = cgraph_function_versioning (cur_node, NULL, NULL, args_to_skip,
1171                                      split_point->split_bbs,
1172                                      split_point->entry_bb, "part");
1173   /* For usual cloning it is enough to clear builtin only when signature
1174      changes.  For partial inlining we however can not expect the part
1175      of builtin implementation to have same semantic as the whole.  */
1176   if (DECL_BUILT_IN (node->decl))
1177     {
1178       DECL_BUILT_IN_CLASS (node->decl) = NOT_BUILT_IN;
1179       DECL_FUNCTION_CODE (node->decl) = (enum built_in_function) 0;
1180     }
1181   cgraph_node_remove_callees (cur_node);
1182   if (!split_part_return_p)
1183     TREE_THIS_VOLATILE (node->decl) = 1;
1184   if (dump_file)
1185     dump_function_to_file (node->decl, dump_file, dump_flags);
1186
1187   /* Create the basic block we place call into.  It is the entry basic block
1188      split after last label.  */
1189   call_bb = split_point->entry_bb;
1190   for (gsi = gsi_start_bb (call_bb); !gsi_end_p (gsi);)
1191     if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
1192       {
1193         last_stmt = gsi_stmt (gsi);
1194         gsi_next (&gsi);
1195       }
1196     else
1197       break;
1198   e = split_block (split_point->entry_bb, last_stmt);
1199   remove_edge (e);
1200
1201   /* Produce the call statement.  */
1202   gsi = gsi_last_bb (call_bb);
1203   FOR_EACH_VEC_ELT (tree, args_to_pass, i, arg)
1204     if (!is_gimple_val (arg))
1205       {
1206         arg = force_gimple_operand_gsi (&gsi, arg, true, NULL_TREE,
1207                                         false, GSI_CONTINUE_LINKING);
1208         VEC_replace (tree, args_to_pass, i, arg);
1209       }
1210   call = gimple_build_call_vec (node->decl, args_to_pass);
1211   gimple_set_block (call, DECL_INITIAL (current_function_decl));
1212
1213   /* We avoid address being taken on any variable used by split part,
1214      so return slot optimization is always possible.  Moreover this is
1215      required to make DECL_BY_REFERENCE work.  */
1216   if (aggregate_value_p (DECL_RESULT (current_function_decl),
1217                          TREE_TYPE (current_function_decl)))
1218     gimple_call_set_return_slot_opt (call, true);
1219
1220   /* Update return value.  This is bit tricky.  When we do not return,
1221      do nothing.  When we return we might need to update return_bb
1222      or produce a new return statement.  */
1223   if (!split_part_return_p)
1224     gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1225   else
1226     {
1227       e = make_edge (call_bb, return_bb,
1228                      return_bb == EXIT_BLOCK_PTR ? 0 : EDGE_FALLTHRU);
1229       e->count = call_bb->count;
1230       e->probability = REG_BR_PROB_BASE;
1231
1232       /* If there is return basic block, see what value we need to store
1233          return value into and put call just before it.  */
1234       if (return_bb != EXIT_BLOCK_PTR)
1235         {
1236           real_retval = retval = find_retval (return_bb);
1237
1238           if (real_retval && split_point->split_part_set_retval)
1239             {
1240               gimple_stmt_iterator psi;
1241
1242               /* See if we need new SSA_NAME for the result.
1243                  When DECL_BY_REFERENCE is true, retval is actually pointer to
1244                  return value and it is constant in whole function.  */
1245               if (TREE_CODE (retval) == SSA_NAME
1246                   && !DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1247                 {
1248                   retval = make_ssa_name (SSA_NAME_VAR (retval), call);
1249
1250                   /* See if there is PHI defining return value.  */
1251                   for (psi = gsi_start_phis (return_bb);
1252                        !gsi_end_p (psi); gsi_next (&psi))
1253                     if (is_gimple_reg (gimple_phi_result (gsi_stmt (psi))))
1254                       break;
1255
1256                   /* When there is PHI, just update its value.  */
1257                   if (TREE_CODE (retval) == SSA_NAME
1258                       && !gsi_end_p (psi))
1259                     add_phi_arg (gsi_stmt (psi), retval, e, UNKNOWN_LOCATION);
1260                   /* Otherwise update the return BB itself.
1261                      find_return_bb allows at most one assignment to return value,
1262                      so update first statement.  */
1263                   else
1264                     {
1265                       gimple_stmt_iterator bsi;
1266                       for (bsi = gsi_start_bb (return_bb); !gsi_end_p (bsi);
1267                            gsi_next (&bsi))
1268                         if (gimple_code (gsi_stmt (bsi)) == GIMPLE_RETURN)
1269                           {
1270                             gimple_return_set_retval (gsi_stmt (bsi), retval);
1271                             break;
1272                           }
1273                         else if (gimple_code (gsi_stmt (bsi)) == GIMPLE_ASSIGN)
1274                           {
1275                             gimple_assign_set_rhs1 (gsi_stmt (bsi), retval);
1276                             break;
1277                           }
1278                       update_stmt (gsi_stmt (bsi));
1279                     }
1280                 }
1281               if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1282                 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1283               else
1284                 gimple_call_set_lhs (call, retval);
1285             }
1286           gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1287         }
1288       /* We don't use return block (there is either no return in function or
1289          multiple of them).  So create new basic block with return statement.
1290          */
1291       else
1292         {
1293           gimple ret;
1294           if (split_point->split_part_set_retval
1295               && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
1296             {
1297               retval = DECL_RESULT (current_function_decl);
1298
1299               /* We use temporary register to hold value when aggregate_value_p
1300                  is false.  Similarly for DECL_BY_REFERENCE we must avoid extra
1301                  copy.  */
1302               if (!aggregate_value_p (retval, TREE_TYPE (current_function_decl))
1303                   && !DECL_BY_REFERENCE (retval))
1304                 retval = create_tmp_reg (TREE_TYPE (retval), NULL);
1305               if (is_gimple_reg (retval))
1306                 {
1307                   /* When returning by reference, there is only one SSA name
1308                      assigned to RESULT_DECL (that is pointer to return value).
1309                      Look it up or create new one if it is missing.  */
1310                   if (DECL_BY_REFERENCE (retval))
1311                     {
1312                       tree retval_name;
1313                       if ((retval_name = gimple_default_def (cfun, retval))
1314                           != NULL)
1315                         retval = retval_name;
1316                       else
1317                         {
1318                           retval_name = make_ssa_name (retval,
1319                                                        gimple_build_nop ());
1320                           set_default_def (retval, retval_name);
1321                           retval = retval_name;
1322                         }
1323                     }
1324                   /* Otherwise produce new SSA name for return value.  */
1325                   else
1326                     retval = make_ssa_name (retval, call);
1327                 }
1328               if (DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)))
1329                 gimple_call_set_lhs (call, build_simple_mem_ref (retval));
1330               else
1331                 gimple_call_set_lhs (call, retval);
1332             }
1333           gsi_insert_after (&gsi, call, GSI_NEW_STMT);
1334           ret = gimple_build_return (retval);
1335           gsi_insert_after (&gsi, ret, GSI_NEW_STMT);
1336         }
1337     }
1338   free_dominance_info (CDI_DOMINATORS);
1339   free_dominance_info (CDI_POST_DOMINATORS);
1340   compute_inline_parameters (node);
1341 }
1342
1343 /* Execute function splitting pass.  */
1344
1345 static unsigned int
1346 execute_split_functions (void)
1347 {
1348   gimple_stmt_iterator bsi;
1349   basic_block bb;
1350   int overall_time = 0, overall_size = 0;
1351   int todo = 0;
1352   struct cgraph_node *node = cgraph_node (current_function_decl);
1353
1354   if (flags_from_decl_or_type (current_function_decl) & ECF_NORETURN)
1355     {
1356       if (dump_file)
1357         fprintf (dump_file, "Not splitting: noreturn function.\n");
1358       return 0;
1359     }
1360   if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
1361     {
1362       if (dump_file)
1363         fprintf (dump_file, "Not splitting: main function.\n");
1364       return 0;
1365     }
1366   /* This can be relaxed; function might become inlinable after splitting
1367      away the uninlinable part.  */
1368   if (!node->local.inlinable)
1369     {
1370       if (dump_file)
1371         fprintf (dump_file, "Not splitting: not inlinable.\n");
1372       return 0;
1373     }
1374   if (node->local.disregard_inline_limits)
1375     {
1376       if (dump_file)
1377         fprintf (dump_file, "Not splitting: disregarding inline limits.\n");
1378       return 0;
1379     }
1380   /* This can be relaxed; most of versioning tests actually prevents
1381      a duplication.  */
1382   if (!tree_versionable_function_p (current_function_decl))
1383     {
1384       if (dump_file)
1385         fprintf (dump_file, "Not splitting: not versionable.\n");
1386       return 0;
1387     }
1388   /* FIXME: we could support this.  */
1389   if (DECL_STRUCT_FUNCTION (current_function_decl)->static_chain_decl)
1390     {
1391       if (dump_file)
1392         fprintf (dump_file, "Not splitting: nested function.\n");
1393       return 0;
1394     }
1395
1396   /* See if it makes sense to try to split.
1397      It makes sense to split if we inline, that is if we have direct calls to
1398      handle or direct calls are possibly going to appear as result of indirect
1399      inlining or LTO.  Also handle -fprofile-generate as LTO to allow non-LTO
1400      training for LTO -fprofile-use build.
1401
1402      Note that we are not completely conservative about disqualifying functions
1403      called once.  It is possible that the caller is called more then once and
1404      then inlining would still benefit.  */
1405   if ((!node->callers || !node->callers->next_caller)
1406       && !node->address_taken
1407       && (!flag_lto || !node->local.externally_visible))
1408     {
1409       if (dump_file)
1410         fprintf (dump_file, "Not splitting: not called directly "
1411                  "or called once.\n");
1412       return 0;
1413     }
1414
1415   /* FIXME: We can actually split if splitting reduces call overhead.  */
1416   if (!flag_inline_small_functions
1417       && !DECL_DECLARED_INLINE_P (current_function_decl))
1418     {
1419       if (dump_file)
1420         fprintf (dump_file, "Not splitting: not autoinlining and function"
1421                  " is not inline.\n");
1422       return 0;
1423     }
1424
1425   /* Initialize bitmap to track forbidden calls.  */
1426   forbidden_dominators = BITMAP_ALLOC (NULL);
1427   calculate_dominance_info (CDI_DOMINATORS);
1428
1429   /* Compute local info about basic blocks and determine function size/time.  */
1430   VEC_safe_grow_cleared (bb_info, heap, bb_info_vec, last_basic_block + 1);
1431   memset (&best_split_point, 0, sizeof (best_split_point));
1432   FOR_EACH_BB (bb)
1433     {
1434       int time = 0;
1435       int size = 0;
1436       int freq = compute_call_stmt_bb_frequency (current_function_decl, bb);
1437
1438       if (dump_file && (dump_flags & TDF_DETAILS))
1439         fprintf (dump_file, "Basic block %i\n", bb->index);
1440
1441       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1442         {
1443           int this_time, this_size;
1444           gimple stmt = gsi_stmt (bsi);
1445
1446           this_size = estimate_num_insns (stmt, &eni_size_weights);
1447           this_time = estimate_num_insns (stmt, &eni_time_weights) * freq;
1448           size += this_size;
1449           time += this_time;
1450           check_forbidden_calls (stmt);
1451
1452           if (dump_file && (dump_flags & TDF_DETAILS))
1453             {
1454               fprintf (dump_file, "  freq:%6i size:%3i time:%3i ",
1455                        freq, this_size, this_time);
1456               print_gimple_stmt (dump_file, stmt, 0, 0);
1457             }
1458         }
1459       overall_time += time;
1460       overall_size += size;
1461       VEC_index (bb_info, bb_info_vec, bb->index)->time = time;
1462       VEC_index (bb_info, bb_info_vec, bb->index)->size = size;
1463     }
1464   find_split_points (overall_time, overall_size);
1465   if (best_split_point.split_bbs)
1466     {
1467       split_function (&best_split_point);
1468       BITMAP_FREE (best_split_point.ssa_names_to_pass);
1469       BITMAP_FREE (best_split_point.split_bbs);
1470       todo = TODO_update_ssa | TODO_cleanup_cfg;
1471     }
1472   BITMAP_FREE (forbidden_dominators);
1473   VEC_free (bb_info, heap, bb_info_vec);
1474   bb_info_vec = NULL;
1475   return todo;
1476 }
1477
1478 /* Gate function splitting pass.  When doing profile feedback, we want
1479    to execute the pass after profiling is read.  So disable one in 
1480    early optimization.  */
1481
1482 static bool
1483 gate_split_functions (void)
1484 {
1485   return (flag_partial_inlining
1486           && !profile_arc_flag && !flag_branch_probabilities);
1487 }
1488
1489 struct gimple_opt_pass pass_split_functions =
1490 {
1491  {
1492   GIMPLE_PASS,
1493   "fnsplit",                            /* name */
1494   gate_split_functions,                 /* gate */
1495   execute_split_functions,              /* execute */
1496   NULL,                                 /* sub */
1497   NULL,                                 /* next */
1498   0,                                    /* static_pass_number */
1499   TV_IPA_FNSPLIT,                       /* tv_id */
1500   PROP_cfg,                             /* properties_required */
1501   0,                                    /* properties_provided */
1502   0,                                    /* properties_destroyed */
1503   0,                                    /* todo_flags_start */
1504   TODO_dump_func                        /* todo_flags_finish */
1505  }
1506 };
1507
1508 /* Gate feedback driven function splitting pass.
1509    We don't need to split when profiling at all, we are producing
1510    lousy code anyway.  */
1511
1512 static bool
1513 gate_feedback_split_functions (void)
1514 {
1515   return (flag_partial_inlining
1516           && flag_branch_probabilities);
1517 }
1518
1519 /* Execute function splitting pass.  */
1520
1521 static unsigned int
1522 execute_feedback_split_functions (void)
1523 {
1524   unsigned int retval = execute_split_functions ();
1525   if (retval)
1526     retval |= TODO_rebuild_cgraph_edges;
1527   return retval;
1528 }
1529
1530 struct gimple_opt_pass pass_feedback_split_functions =
1531 {
1532  {
1533   GIMPLE_PASS,
1534   "feedback_fnsplit",                   /* name */
1535   gate_feedback_split_functions,        /* gate */
1536   execute_feedback_split_functions,     /* execute */
1537   NULL,                                 /* sub */
1538   NULL,                                 /* next */
1539   0,                                    /* static_pass_number */
1540   TV_IPA_FNSPLIT,                       /* tv_id */
1541   PROP_cfg,                             /* properties_required */
1542   0,                                    /* properties_provided */
1543   0,                                    /* properties_destroyed */
1544   0,                                    /* todo_flags_start */
1545   TODO_dump_func                        /* todo_flags_finish */
1546  }
1547 };