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