OSDN Git Service

PR debug/39267
[pf3gnuchains/gcc-fork.git] / gcc / tree-inline.c
1 /* Tree inlining.
2    Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Alexandre Oliva <aoliva@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License 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 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "rtl.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "params.h"
33 #include "input.h"
34 #include "insn-config.h"
35 #include "varray.h"
36 #include "hashtab.h"
37 #include "langhooks.h"
38 #include "basic-block.h"
39 #include "tree-iterator.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "tree-mudflap.h"
43 #include "tree-flow.h"
44 #include "function.h"
45 #include "ggc.h"
46 #include "tree-flow.h"
47 #include "diagnostic.h"
48 #include "except.h"
49 #include "debug.h"
50 #include "pointer-set.h"
51 #include "ipa-prop.h"
52 #include "value-prof.h"
53 #include "tree-pass.h"
54 #include "target.h"
55 #include "integrate.h"
56
57 /* I'm not real happy about this, but we need to handle gimple and
58    non-gimple trees.  */
59 #include "gimple.h"
60
61 /* Inlining, Cloning, Versioning, Parallelization
62
63    Inlining: a function body is duplicated, but the PARM_DECLs are
64    remapped into VAR_DECLs, and non-void RETURN_EXPRs become
65    MODIFY_EXPRs that store to a dedicated returned-value variable.
66    The duplicated eh_region info of the copy will later be appended
67    to the info for the caller; the eh_region info in copied throwing
68    statements and RESX_EXPRs is adjusted accordingly.
69
70    Cloning: (only in C++) We have one body for a con/de/structor, and
71    multiple function decls, each with a unique parameter list.
72    Duplicate the body, using the given splay tree; some parameters
73    will become constants (like 0 or 1).
74
75    Versioning: a function body is duplicated and the result is a new
76    function rather than into blocks of an existing function as with
77    inlining.  Some parameters will become constants.
78
79    Parallelization: a region of a function is duplicated resulting in
80    a new function.  Variables may be replaced with complex expressions
81    to enable shared variable semantics.
82
83    All of these will simultaneously lookup any callgraph edges.  If
84    we're going to inline the duplicated function body, and the given
85    function has some cloned callgraph nodes (one for each place this
86    function will be inlined) those callgraph edges will be duplicated.
87    If we're cloning the body, those callgraph edges will be
88    updated to point into the new body.  (Note that the original
89    callgraph node and edge list will not be altered.)
90
91    See the CALL_EXPR handling case in copy_tree_body_r ().  */
92
93 /* To Do:
94
95    o In order to make inlining-on-trees work, we pessimized
96      function-local static constants.  In particular, they are now
97      always output, even when not addressed.  Fix this by treating
98      function-local static constants just like global static
99      constants; the back-end already knows not to output them if they
100      are not needed.
101
102    o Provide heuristics to clamp inlining of recursive template
103      calls?  */
104
105
106 /* Weights that estimate_num_insns uses for heuristics in inlining.  */
107
108 eni_weights eni_inlining_weights;
109
110 /* Weights that estimate_num_insns uses to estimate the size of the
111    produced code.  */
112
113 eni_weights eni_size_weights;
114
115 /* Weights that estimate_num_insns uses to estimate the time necessary
116    to execute the produced code.  */
117
118 eni_weights eni_time_weights;
119
120 /* Prototypes.  */
121
122 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
123 static bool inlinable_function_p (tree);
124 static void remap_block (tree *, copy_body_data *);
125 static void copy_bind_expr (tree *, int *, copy_body_data *);
126 static tree mark_local_for_remap_r (tree *, int *, void *);
127 static void unsave_expr_1 (tree);
128 static tree unsave_r (tree *, int *, void *);
129 static void declare_inline_vars (tree, tree);
130 static void remap_save_expr (tree *, void *, int *);
131 static void prepend_lexical_block (tree current_block, tree new_block);
132 static tree copy_decl_to_var (tree, copy_body_data *);
133 static tree copy_result_decl_to_var (tree, copy_body_data *);
134 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
135 static gimple remap_gimple_stmt (gimple, copy_body_data *);
136
137 /* Insert a tree->tree mapping for ID.  Despite the name suggests
138    that the trees should be variables, it is used for more than that.  */
139
140 void
141 insert_decl_map (copy_body_data *id, tree key, tree value)
142 {
143   *pointer_map_insert (id->decl_map, key) = value;
144
145   /* Always insert an identity map as well.  If we see this same new
146      node again, we won't want to duplicate it a second time.  */
147   if (key != value)
148     *pointer_map_insert (id->decl_map, value) = value;
149 }
150
151 /* Construct new SSA name for old NAME. ID is the inline context.  */
152
153 static tree
154 remap_ssa_name (tree name, copy_body_data *id)
155 {
156   tree new_tree;
157   tree *n;
158
159   gcc_assert (TREE_CODE (name) == SSA_NAME);
160
161   n = (tree *) pointer_map_contains (id->decl_map, name);
162   if (n)
163     return unshare_expr (*n);
164
165   /* Do not set DEF_STMT yet as statement is not copied yet. We do that
166      in copy_bb.  */
167   new_tree = remap_decl (SSA_NAME_VAR (name), id);
168
169   /* We might've substituted constant or another SSA_NAME for
170      the variable. 
171
172      Replace the SSA name representing RESULT_DECL by variable during
173      inlining:  this saves us from need to introduce PHI node in a case
174      return value is just partly initialized.  */
175   if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
176       && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
177           || !id->transform_return_to_modify))
178     {
179       new_tree = make_ssa_name (new_tree, NULL);
180       insert_decl_map (id, name, new_tree);
181       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
182         = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
183       TREE_TYPE (new_tree) = TREE_TYPE (SSA_NAME_VAR (new_tree));
184       if (gimple_nop_p (SSA_NAME_DEF_STMT (name)))
185         {
186           /* By inlining function having uninitialized variable, we might
187              extend the lifetime (variable might get reused).  This cause
188              ICE in the case we end up extending lifetime of SSA name across
189              abnormal edge, but also increase register pressure.
190
191              We simply initialize all uninitialized vars by 0 except
192              for case we are inlining to very first BB.  We can avoid
193              this for all BBs that are not inside strongly connected
194              regions of the CFG, but this is expensive to test.  */
195           if (id->entry_bb
196               && is_gimple_reg (SSA_NAME_VAR (name))
197               && TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL
198               && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
199                   || EDGE_COUNT (id->entry_bb->preds) != 1))
200             {
201               gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
202               gimple init_stmt;
203               
204               init_stmt = gimple_build_assign (new_tree,
205                                                fold_convert (TREE_TYPE (new_tree),
206                                                             integer_zero_node));
207               gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
208               SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
209             }
210           else
211             {
212               SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
213               if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name))
214                   == name)
215                 set_default_def (SSA_NAME_VAR (new_tree), new_tree);
216             }
217         }
218     }
219   else
220     insert_decl_map (id, name, new_tree);
221   return new_tree;
222 }
223
224 /* Remap DECL during the copying of the BLOCK tree for the function.  */
225
226 tree
227 remap_decl (tree decl, copy_body_data *id)
228 {
229   tree *n;
230   tree fn;
231
232   /* We only remap local variables in the current function.  */
233   fn = id->src_fn;
234
235   /* See if we have remapped this declaration.  */
236
237   n = (tree *) pointer_map_contains (id->decl_map, decl);
238
239   /* If we didn't already have an equivalent for this declaration,
240      create one now.  */
241   if (!n)
242     {
243       /* Make a copy of the variable or label.  */
244       tree t = id->copy_decl (decl, id);
245      
246       /* Remember it, so that if we encounter this local entity again
247          we can reuse this copy.  Do this early because remap_type may
248          need this decl for TYPE_STUB_DECL.  */
249       insert_decl_map (id, decl, t);
250
251       if (!DECL_P (t))
252         return t;
253
254       /* Remap types, if necessary.  */
255       TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
256       if (TREE_CODE (t) == TYPE_DECL)
257         DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
258
259       /* Remap sizes as necessary.  */
260       walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
261       walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
262
263       /* If fields, do likewise for offset and qualifier.  */
264       if (TREE_CODE (t) == FIELD_DECL)
265         {
266           walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
267           if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
268             walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
269         }
270
271       if (cfun && gimple_in_ssa_p (cfun)
272           && (TREE_CODE (t) == VAR_DECL
273               || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
274         {
275           tree def = gimple_default_def (id->src_cfun, decl);
276           get_var_ann (t);
277           if (TREE_CODE (decl) != PARM_DECL && def)
278             {
279               tree map = remap_ssa_name (def, id);
280               /* Watch out RESULT_DECLs whose SSA names map directly
281                  to them.  */
282               if (TREE_CODE (map) == SSA_NAME
283                   && gimple_nop_p (SSA_NAME_DEF_STMT (map)))
284                 set_default_def (t, map);
285             }
286           add_referenced_var (t);
287         }
288       return t;
289     }
290
291   return unshare_expr (*n);
292 }
293
294 static tree
295 remap_type_1 (tree type, copy_body_data *id)
296 {
297   tree new_tree, t;
298
299   /* We do need a copy.  build and register it now.  If this is a pointer or
300      reference type, remap the designated type and make a new pointer or
301      reference type.  */
302   if (TREE_CODE (type) == POINTER_TYPE)
303     {
304       new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
305                                          TYPE_MODE (type),
306                                          TYPE_REF_CAN_ALIAS_ALL (type));
307       insert_decl_map (id, type, new_tree);
308       return new_tree;
309     }
310   else if (TREE_CODE (type) == REFERENCE_TYPE)
311     {
312       new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
313                                             TYPE_MODE (type),
314                                             TYPE_REF_CAN_ALIAS_ALL (type));
315       insert_decl_map (id, type, new_tree);
316       return new_tree;
317     }
318   else
319     new_tree = copy_node (type);
320
321   insert_decl_map (id, type, new_tree);
322
323   /* This is a new type, not a copy of an old type.  Need to reassociate
324      variants.  We can handle everything except the main variant lazily.  */
325   t = TYPE_MAIN_VARIANT (type);
326   if (type != t)
327     {
328       t = remap_type (t, id);
329       TYPE_MAIN_VARIANT (new_tree) = t;
330       TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
331       TYPE_NEXT_VARIANT (t) = new_tree;
332     }
333   else
334     {
335       TYPE_MAIN_VARIANT (new_tree) = new_tree;
336       TYPE_NEXT_VARIANT (new_tree) = NULL;
337     }
338
339   if (TYPE_STUB_DECL (type))
340     TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
341
342   /* Lazily create pointer and reference types.  */
343   TYPE_POINTER_TO (new_tree) = NULL;
344   TYPE_REFERENCE_TO (new_tree) = NULL;
345
346   switch (TREE_CODE (new_tree))
347     {
348     case INTEGER_TYPE:
349     case REAL_TYPE:
350     case FIXED_POINT_TYPE:
351     case ENUMERAL_TYPE:
352     case BOOLEAN_TYPE:
353       t = TYPE_MIN_VALUE (new_tree);
354       if (t && TREE_CODE (t) != INTEGER_CST)
355         walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
356
357       t = TYPE_MAX_VALUE (new_tree);
358       if (t && TREE_CODE (t) != INTEGER_CST)
359         walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
360       return new_tree;
361
362     case FUNCTION_TYPE:
363       TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
364       walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
365       return new_tree;
366
367     case ARRAY_TYPE:
368       TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
369       TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
370       break;
371
372     case RECORD_TYPE:
373     case UNION_TYPE:
374     case QUAL_UNION_TYPE:
375       {
376         tree f, nf = NULL;
377
378         for (f = TYPE_FIELDS (new_tree); f ; f = TREE_CHAIN (f))
379           {
380             t = remap_decl (f, id);
381             DECL_CONTEXT (t) = new_tree;
382             TREE_CHAIN (t) = nf;
383             nf = t;
384           }
385         TYPE_FIELDS (new_tree) = nreverse (nf);
386       }
387       break;
388
389     case OFFSET_TYPE:
390     default:
391       /* Shouldn't have been thought variable sized.  */
392       gcc_unreachable ();
393     }
394
395   walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
396   walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
397
398   return new_tree;
399 }
400
401 tree
402 remap_type (tree type, copy_body_data *id)
403 {
404   tree *node;
405   tree tmp;
406
407   if (type == NULL)
408     return type;
409
410   /* See if we have remapped this type.  */
411   node = (tree *) pointer_map_contains (id->decl_map, type);
412   if (node)
413     return *node;
414
415   /* The type only needs remapping if it's variably modified.  */
416   if (! variably_modified_type_p (type, id->src_fn))
417     {
418       insert_decl_map (id, type, type);
419       return type;
420     }
421
422   id->remapping_type_depth++;
423   tmp = remap_type_1 (type, id);
424   id->remapping_type_depth--;
425
426   return tmp;
427 }
428
429 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs.  */
430   
431 static bool
432 can_be_nonlocal (tree decl, copy_body_data *id)
433 {
434   /* We can not duplicate function decls.  */
435   if (TREE_CODE (decl) == FUNCTION_DECL)
436     return true;
437
438   /* Local static vars must be non-local or we get multiple declaration
439      problems.  */
440   if (TREE_CODE (decl) == VAR_DECL
441       && !auto_var_in_fn_p (decl, id->src_fn))
442     return true;
443
444   /* At the moment dwarf2out can handle only these types of nodes.  We
445      can support more later.  */
446   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
447     return false;
448
449   /* We must use global type.  */
450   if (TREE_TYPE (decl) != remap_type (TREE_TYPE (decl), id))
451     return false;
452
453   /* Wihtout SSA we can't tell if variable is used.  */
454   if (!gimple_in_ssa_p (cfun))
455     return false;
456
457   /* Live variables must be copied so we can attach DECL_RTL.  */
458   if (var_ann (decl))
459     return false;
460
461   return true;
462 }
463
464 static tree
465 remap_decls (tree decls, VEC(tree,gc) **nonlocalized_list, copy_body_data *id)
466 {
467   tree old_var;
468   tree new_decls = NULL_TREE;
469
470   /* Remap its variables.  */
471   for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
472     {
473       tree new_var;
474       tree origin_var = DECL_ORIGIN (old_var);
475
476       if (can_be_nonlocal (old_var, id))
477         {
478           if (TREE_CODE (old_var) == VAR_DECL
479               && (var_ann (old_var) || !gimple_in_ssa_p (cfun)))
480             cfun->local_decls = tree_cons (NULL_TREE, old_var,
481                                                    cfun->local_decls);
482           if (debug_info_level > DINFO_LEVEL_TERSE
483               && !DECL_IGNORED_P (old_var)
484               && nonlocalized_list)
485             VEC_safe_push (tree, gc, *nonlocalized_list, origin_var);
486           continue;
487         }
488
489       /* Remap the variable.  */
490       new_var = remap_decl (old_var, id);
491
492       /* If we didn't remap this variable, we can't mess with its
493          TREE_CHAIN.  If we remapped this variable to the return slot, it's
494          already declared somewhere else, so don't declare it here.  */
495       
496       if (new_var == id->retvar)
497         ;
498       else if (!new_var)
499         {
500           if (debug_info_level > DINFO_LEVEL_TERSE
501               && !DECL_IGNORED_P (old_var)
502               && nonlocalized_list)
503             VEC_safe_push (tree, gc, *nonlocalized_list, origin_var);
504         }
505       else
506         {
507           gcc_assert (DECL_P (new_var));
508           TREE_CHAIN (new_var) = new_decls;
509           new_decls = new_var;
510         }
511     }
512
513   return nreverse (new_decls);
514 }
515
516 /* Copy the BLOCK to contain remapped versions of the variables
517    therein.  And hook the new block into the block-tree.  */
518
519 static void
520 remap_block (tree *block, copy_body_data *id)
521 {
522   tree old_block;
523   tree new_block;
524   tree fn;
525
526   /* Make the new block.  */
527   old_block = *block;
528   new_block = make_node (BLOCK);
529   TREE_USED (new_block) = TREE_USED (old_block);
530   BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
531   BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
532   BLOCK_NONLOCALIZED_VARS (new_block)
533     = VEC_copy (tree, gc, BLOCK_NONLOCALIZED_VARS (old_block));
534   *block = new_block;
535
536   /* Remap its variables.  */
537   BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
538                                         &BLOCK_NONLOCALIZED_VARS (new_block),
539                                         id);
540
541   fn = id->dst_fn;
542
543   if (id->transform_lang_insert_block)
544     id->transform_lang_insert_block (new_block);
545
546   /* Remember the remapped block.  */
547   insert_decl_map (id, old_block, new_block);
548 }
549
550 /* Copy the whole block tree and root it in id->block.  */
551 static tree
552 remap_blocks (tree block, copy_body_data *id)
553 {
554   tree t;
555   tree new_tree = block;
556
557   if (!block)
558     return NULL;
559
560   remap_block (&new_tree, id);
561   gcc_assert (new_tree != block);
562   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
563     prepend_lexical_block (new_tree, remap_blocks (t, id));
564   /* Blocks are in arbitrary order, but make things slightly prettier and do
565      not swap order when producing a copy.  */
566   BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
567   return new_tree;
568 }
569
570 static void
571 copy_statement_list (tree *tp)
572 {
573   tree_stmt_iterator oi, ni;
574   tree new_tree;
575
576   new_tree = alloc_stmt_list ();
577   ni = tsi_start (new_tree);
578   oi = tsi_start (*tp);
579   *tp = new_tree;
580
581   for (; !tsi_end_p (oi); tsi_next (&oi))
582     tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
583 }
584
585 static void
586 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
587 {
588   tree block = BIND_EXPR_BLOCK (*tp);
589   /* Copy (and replace) the statement.  */
590   copy_tree_r (tp, walk_subtrees, NULL);
591   if (block)
592     {
593       remap_block (&block, id);
594       BIND_EXPR_BLOCK (*tp) = block;
595     }
596
597   if (BIND_EXPR_VARS (*tp))
598     /* This will remap a lot of the same decls again, but this should be
599        harmless.  */
600     BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
601 }
602
603
604 /* Create a new gimple_seq by remapping all the statements in BODY
605    using the inlining information in ID.  */
606
607 gimple_seq
608 remap_gimple_seq (gimple_seq body, copy_body_data *id)
609 {
610   gimple_stmt_iterator si;
611   gimple_seq new_body = NULL;
612
613   for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
614     {
615       gimple new_stmt = remap_gimple_stmt (gsi_stmt (si), id);
616       gimple_seq_add_stmt (&new_body, new_stmt);
617     }
618
619   return new_body;
620 }
621
622
623 /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
624    block using the mapping information in ID.  */
625
626 static gimple
627 copy_gimple_bind (gimple stmt, copy_body_data *id)
628 {
629   gimple new_bind;
630   tree new_block, new_vars;
631   gimple_seq body, new_body;
632
633   /* Copy the statement.  Note that we purposely don't use copy_stmt
634      here because we need to remap statements as we copy.  */
635   body = gimple_bind_body (stmt);
636   new_body = remap_gimple_seq (body, id);
637
638   new_block = gimple_bind_block (stmt);
639   if (new_block)
640     remap_block (&new_block, id);
641
642   /* This will remap a lot of the same decls again, but this should be
643      harmless.  */
644   new_vars = gimple_bind_vars (stmt);
645   if (new_vars)
646     new_vars = remap_decls (new_vars, NULL, id);
647
648   new_bind = gimple_build_bind (new_vars, new_body, new_block);
649
650   return new_bind;
651 }
652
653
654 /* Remap the GIMPLE operand pointed to by *TP.  DATA is really a
655    'struct walk_stmt_info *'.  DATA->INFO is a 'copy_body_data *'.
656    WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
657    recursing into the children nodes of *TP.  */
658
659 static tree
660 remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
661 {
662   struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
663   copy_body_data *id = (copy_body_data *) wi_p->info;
664   tree fn = id->src_fn;
665
666   if (TREE_CODE (*tp) == SSA_NAME)
667     {
668       *tp = remap_ssa_name (*tp, id);
669       *walk_subtrees = 0;
670       return NULL;
671     }
672   else if (auto_var_in_fn_p (*tp, fn))
673     {
674       /* Local variables and labels need to be replaced by equivalent
675          variables.  We don't want to copy static variables; there's
676          only one of those, no matter how many times we inline the
677          containing function.  Similarly for globals from an outer
678          function.  */
679       tree new_decl;
680
681       /* Remap the declaration.  */
682       new_decl = remap_decl (*tp, id);
683       gcc_assert (new_decl);
684       /* Replace this variable with the copy.  */
685       STRIP_TYPE_NOPS (new_decl);
686       *tp = new_decl;
687       *walk_subtrees = 0;
688     }
689   else if (TREE_CODE (*tp) == STATEMENT_LIST)
690     gcc_unreachable ();
691   else if (TREE_CODE (*tp) == SAVE_EXPR)
692     gcc_unreachable ();
693   else if (TREE_CODE (*tp) == LABEL_DECL
694            && (!DECL_CONTEXT (*tp)
695                || decl_function_context (*tp) == id->src_fn))
696     /* These may need to be remapped for EH handling.  */
697     *tp = remap_decl (*tp, id);
698   else if (TYPE_P (*tp))
699     /* Types may need remapping as well.  */
700     *tp = remap_type (*tp, id);
701   else if (CONSTANT_CLASS_P (*tp))
702     {
703       /* If this is a constant, we have to copy the node iff the type
704          will be remapped.  copy_tree_r will not copy a constant.  */
705       tree new_type = remap_type (TREE_TYPE (*tp), id);
706
707       if (new_type == TREE_TYPE (*tp))
708         *walk_subtrees = 0;
709
710       else if (TREE_CODE (*tp) == INTEGER_CST)
711         *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
712                                   TREE_INT_CST_HIGH (*tp));
713       else
714         {
715           *tp = copy_node (*tp);
716           TREE_TYPE (*tp) = new_type;
717         }
718     }
719   else
720     {
721       /* Otherwise, just copy the node.  Note that copy_tree_r already
722          knows not to copy VAR_DECLs, etc., so this is safe.  */
723       if (TREE_CODE (*tp) == INDIRECT_REF)
724         {
725           /* Get rid of *& from inline substitutions that can happen when a
726              pointer argument is an ADDR_EXPR.  */
727           tree decl = TREE_OPERAND (*tp, 0);
728           tree *n;
729
730           n = (tree *) pointer_map_contains (id->decl_map, decl);
731           if (n)
732             {
733               tree type, new_tree, old;
734
735               /* If we happen to get an ADDR_EXPR in n->value, strip
736                  it manually here as we'll eventually get ADDR_EXPRs
737                  which lie about their types pointed to.  In this case
738                  build_fold_indirect_ref wouldn't strip the
739                  INDIRECT_REF, but we absolutely rely on that.  As
740                  fold_indirect_ref does other useful transformations,
741                  try that first, though.  */
742               type = TREE_TYPE (TREE_TYPE (*n));
743               new_tree = unshare_expr (*n);
744               old = *tp;
745               *tp = gimple_fold_indirect_ref (new_tree);
746               if (!*tp)
747                 {
748                   if (TREE_CODE (new_tree) == ADDR_EXPR)
749                     {
750                       *tp = fold_indirect_ref_1 (type, new_tree);
751                       /* ???  We should either assert here or build
752                          a VIEW_CONVERT_EXPR instead of blindly leaking
753                          incompatible types to our IL.  */
754                       if (! *tp)
755                         *tp = TREE_OPERAND (new_tree, 0);
756                     }
757                   else
758                     {
759                       *tp = build1 (INDIRECT_REF, type, new_tree);
760                       TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
761                       TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
762                     }
763                 }
764               *walk_subtrees = 0;
765               return NULL;
766             }
767         }
768
769       /* Here is the "usual case".  Copy this tree node, and then
770          tweak some special cases.  */
771       copy_tree_r (tp, walk_subtrees, NULL);
772
773       /* Global variables we haven't seen yet need to go into referenced
774          vars.  If not referenced from types only.  */
775       if (gimple_in_ssa_p (cfun)
776           && TREE_CODE (*tp) == VAR_DECL
777           && id->remapping_type_depth == 0)
778         add_referenced_var (*tp);
779
780       /* We should never have TREE_BLOCK set on non-statements.  */
781       if (EXPR_P (*tp))
782         gcc_assert (!TREE_BLOCK (*tp));
783
784       if (TREE_CODE (*tp) != OMP_CLAUSE)
785         TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
786
787       if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
788         {
789           /* The copied TARGET_EXPR has never been expanded, even if the
790              original node was expanded already.  */
791           TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
792           TREE_OPERAND (*tp, 3) = NULL_TREE;
793         }
794       else if (TREE_CODE (*tp) == ADDR_EXPR)
795         {
796           /* Variable substitution need not be simple.  In particular,
797              the INDIRECT_REF substitution above.  Make sure that
798              TREE_CONSTANT and friends are up-to-date.  But make sure
799              to not improperly set TREE_BLOCK on some sub-expressions.  */
800           int invariant = is_gimple_min_invariant (*tp);
801           tree block = id->block;
802           id->block = NULL_TREE;
803           walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
804           id->block = block;
805
806           /* Handle the case where we substituted an INDIRECT_REF
807              into the operand of the ADDR_EXPR.  */
808           if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
809             *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
810           else
811             recompute_tree_invariant_for_addr_expr (*tp);
812
813           /* If this used to be invariant, but is not any longer,
814              then regimplification is probably needed.  */
815           if (invariant && !is_gimple_min_invariant (*tp))
816             id->regimplify = true;
817
818           *walk_subtrees = 0;
819         }
820     }
821
822   /* Keep iterating.  */
823   return NULL_TREE;
824 }
825
826
827 /* Called from copy_body_id via walk_tree.  DATA is really a
828    `copy_body_data *'.  */
829
830 tree
831 copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
832 {
833   copy_body_data *id = (copy_body_data *) data;
834   tree fn = id->src_fn;
835   tree new_block;
836
837   /* Begin by recognizing trees that we'll completely rewrite for the
838      inlining context.  Our output for these trees is completely
839      different from out input (e.g. RETURN_EXPR is deleted, and morphs
840      into an edge).  Further down, we'll handle trees that get
841      duplicated and/or tweaked.  */
842
843   /* When requested, RETURN_EXPRs should be transformed to just the
844      contained MODIFY_EXPR.  The branch semantics of the return will
845      be handled elsewhere by manipulating the CFG rather than a statement.  */
846   if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
847     {
848       tree assignment = TREE_OPERAND (*tp, 0);
849
850       /* If we're returning something, just turn that into an
851          assignment into the equivalent of the original RESULT_DECL.
852          If the "assignment" is just the result decl, the result
853          decl has already been set (e.g. a recent "foo (&result_decl,
854          ...)"); just toss the entire RETURN_EXPR.  */
855       if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
856         {
857           /* Replace the RETURN_EXPR with (a copy of) the
858              MODIFY_EXPR hanging underneath.  */
859           *tp = copy_node (assignment);
860         }
861       else /* Else the RETURN_EXPR returns no value.  */
862         {
863           *tp = NULL;
864           return (tree) (void *)1;
865         }
866     }
867   else if (TREE_CODE (*tp) == SSA_NAME)
868     {
869       *tp = remap_ssa_name (*tp, id);
870       *walk_subtrees = 0;
871       return NULL;
872     }
873
874   /* Local variables and labels need to be replaced by equivalent
875      variables.  We don't want to copy static variables; there's only
876      one of those, no matter how many times we inline the containing
877      function.  Similarly for globals from an outer function.  */
878   else if (auto_var_in_fn_p (*tp, fn))
879     {
880       tree new_decl;
881
882       /* Remap the declaration.  */
883       new_decl = remap_decl (*tp, id);
884       gcc_assert (new_decl);
885       /* Replace this variable with the copy.  */
886       STRIP_TYPE_NOPS (new_decl);
887       *tp = new_decl;
888       *walk_subtrees = 0;
889     }
890   else if (TREE_CODE (*tp) == STATEMENT_LIST)
891     copy_statement_list (tp);
892   else if (TREE_CODE (*tp) == SAVE_EXPR)
893     remap_save_expr (tp, id->decl_map, walk_subtrees);
894   else if (TREE_CODE (*tp) == LABEL_DECL
895            && (! DECL_CONTEXT (*tp)
896                || decl_function_context (*tp) == id->src_fn))
897     /* These may need to be remapped for EH handling.  */
898     *tp = remap_decl (*tp, id);
899   else if (TREE_CODE (*tp) == BIND_EXPR)
900     copy_bind_expr (tp, walk_subtrees, id);
901   /* Types may need remapping as well.  */
902   else if (TYPE_P (*tp))
903     *tp = remap_type (*tp, id);
904
905   /* If this is a constant, we have to copy the node iff the type will be
906      remapped.  copy_tree_r will not copy a constant.  */
907   else if (CONSTANT_CLASS_P (*tp))
908     {
909       tree new_type = remap_type (TREE_TYPE (*tp), id);
910
911       if (new_type == TREE_TYPE (*tp))
912         *walk_subtrees = 0;
913
914       else if (TREE_CODE (*tp) == INTEGER_CST)
915         *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
916                                   TREE_INT_CST_HIGH (*tp));
917       else
918         {
919           *tp = copy_node (*tp);
920           TREE_TYPE (*tp) = new_type;
921         }
922     }
923
924   /* Otherwise, just copy the node.  Note that copy_tree_r already
925      knows not to copy VAR_DECLs, etc., so this is safe.  */
926   else
927     {
928       /* Here we handle trees that are not completely rewritten.
929          First we detect some inlining-induced bogosities for
930          discarding.  */
931       if (TREE_CODE (*tp) == MODIFY_EXPR
932           && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
933           && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
934         {
935           /* Some assignments VAR = VAR; don't generate any rtl code
936              and thus don't count as variable modification.  Avoid
937              keeping bogosities like 0 = 0.  */
938           tree decl = TREE_OPERAND (*tp, 0), value;
939           tree *n;
940
941           n = (tree *) pointer_map_contains (id->decl_map, decl);
942           if (n)
943             {
944               value = *n;
945               STRIP_TYPE_NOPS (value);
946               if (TREE_CONSTANT (value) || TREE_READONLY (value))
947                 {
948                   *tp = build_empty_stmt ();
949                   return copy_tree_body_r (tp, walk_subtrees, data);
950                 }
951             }
952         }
953       else if (TREE_CODE (*tp) == INDIRECT_REF)
954         {
955           /* Get rid of *& from inline substitutions that can happen when a
956              pointer argument is an ADDR_EXPR.  */
957           tree decl = TREE_OPERAND (*tp, 0);
958           tree *n;
959
960           n = (tree *) pointer_map_contains (id->decl_map, decl);
961           if (n)
962             {
963               tree new_tree;
964               tree old;
965               /* If we happen to get an ADDR_EXPR in n->value, strip
966                  it manually here as we'll eventually get ADDR_EXPRs
967                  which lie about their types pointed to.  In this case
968                  build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
969                  but we absolutely rely on that.  As fold_indirect_ref
970                  does other useful transformations, try that first, though.  */
971               tree type = TREE_TYPE (TREE_TYPE (*n));
972               new_tree = unshare_expr (*n);
973               old = *tp;
974               *tp = gimple_fold_indirect_ref (new_tree);
975               if (! *tp)
976                 {
977                   if (TREE_CODE (new_tree) == ADDR_EXPR)
978                     {
979                       *tp = fold_indirect_ref_1 (type, new_tree);
980                       /* ???  We should either assert here or build
981                          a VIEW_CONVERT_EXPR instead of blindly leaking
982                          incompatible types to our IL.  */
983                       if (! *tp)
984                         *tp = TREE_OPERAND (new_tree, 0);
985                     }
986                   else
987                     {
988                       *tp = build1 (INDIRECT_REF, type, new_tree);
989                       TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
990                       TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
991                     }
992                 }
993               *walk_subtrees = 0;
994               return NULL;
995             }
996         }
997
998       /* Here is the "usual case".  Copy this tree node, and then
999          tweak some special cases.  */
1000       copy_tree_r (tp, walk_subtrees, NULL);
1001
1002       /* Global variables we haven't seen yet needs to go into referenced
1003          vars.  If not referenced from types only.  */
1004       if (gimple_in_ssa_p (cfun)
1005           && TREE_CODE (*tp) == VAR_DECL
1006           && id->remapping_type_depth == 0)
1007         add_referenced_var (*tp);
1008        
1009       /* If EXPR has block defined, map it to newly constructed block.
1010          When inlining we want EXPRs without block appear in the block
1011          of function call.  */
1012       if (EXPR_P (*tp))
1013         {
1014           new_block = id->block;
1015           if (TREE_BLOCK (*tp))
1016             {
1017               tree *n;
1018               n = (tree *) pointer_map_contains (id->decl_map,
1019                                                  TREE_BLOCK (*tp));
1020               gcc_assert (n);
1021               new_block = *n;
1022             }
1023           TREE_BLOCK (*tp) = new_block;
1024         }
1025
1026       if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
1027         TREE_OPERAND (*tp, 0) =
1028           build_int_cst (NULL_TREE,
1029                          id->eh_region_offset
1030                          + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
1031
1032       if (TREE_CODE (*tp) != OMP_CLAUSE)
1033         TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1034
1035       /* The copied TARGET_EXPR has never been expanded, even if the
1036          original node was expanded already.  */
1037       if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
1038         {
1039           TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
1040           TREE_OPERAND (*tp, 3) = NULL_TREE;
1041         }
1042
1043       /* Variable substitution need not be simple.  In particular, the
1044          INDIRECT_REF substitution above.  Make sure that TREE_CONSTANT
1045          and friends are up-to-date.  */
1046       else if (TREE_CODE (*tp) == ADDR_EXPR)
1047         {
1048           int invariant = is_gimple_min_invariant (*tp);
1049           walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
1050
1051           /* Handle the case where we substituted an INDIRECT_REF
1052              into the operand of the ADDR_EXPR.  */
1053           if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
1054             *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
1055           else
1056             recompute_tree_invariant_for_addr_expr (*tp);
1057
1058           /* If this used to be invariant, but is not any longer,
1059              then regimplification is probably needed.  */
1060           if (invariant && !is_gimple_min_invariant (*tp))
1061             id->regimplify = true;
1062
1063           *walk_subtrees = 0;
1064         }
1065     }
1066
1067   /* Keep iterating.  */
1068   return NULL_TREE;
1069 }
1070
1071
1072 /* Helper for copy_bb.  Remap statement STMT using the inlining
1073    information in ID.  Return the new statement copy.  */
1074
1075 static gimple
1076 remap_gimple_stmt (gimple stmt, copy_body_data *id)
1077 {
1078   gimple copy = NULL;
1079   struct walk_stmt_info wi;
1080   tree new_block;
1081   bool skip_first = false;
1082
1083   /* Begin by recognizing trees that we'll completely rewrite for the
1084      inlining context.  Our output for these trees is completely
1085      different from out input (e.g. RETURN_EXPR is deleted, and morphs
1086      into an edge).  Further down, we'll handle trees that get
1087      duplicated and/or tweaked.  */
1088
1089   /* When requested, GIMPLE_RETURNs should be transformed to just the
1090      contained GIMPLE_ASSIGN.  The branch semantics of the return will
1091      be handled elsewhere by manipulating the CFG rather than the
1092      statement.  */
1093   if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
1094     {
1095       tree retval = gimple_return_retval (stmt);
1096
1097       /* If we're returning something, just turn that into an
1098          assignment into the equivalent of the original RESULT_DECL.
1099          If RETVAL is just the result decl, the result decl has
1100          already been set (e.g. a recent "foo (&result_decl, ...)");
1101          just toss the entire GIMPLE_RETURN.  */
1102       if (retval && TREE_CODE (retval) != RESULT_DECL)
1103         {
1104           copy = gimple_build_assign (id->retvar, retval);
1105           /* id->retvar is already substituted.  Skip it on later remapping.  */
1106           skip_first = true;
1107         }
1108       else
1109         return gimple_build_nop ();
1110     }
1111   else if (gimple_has_substatements (stmt))
1112     {
1113       gimple_seq s1, s2;
1114
1115       /* When cloning bodies from the C++ front end, we will be handed bodies
1116          in High GIMPLE form.  Handle here all the High GIMPLE statements that
1117          have embedded statements.  */
1118       switch (gimple_code (stmt))
1119         {
1120         case GIMPLE_BIND:
1121           copy = copy_gimple_bind (stmt, id);
1122           break;
1123
1124         case GIMPLE_CATCH:
1125           s1 = remap_gimple_seq (gimple_catch_handler (stmt), id);
1126           copy = gimple_build_catch (gimple_catch_types (stmt), s1);
1127           break;
1128
1129         case GIMPLE_EH_FILTER:
1130           s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
1131           copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
1132           break;
1133
1134         case GIMPLE_TRY:
1135           s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1136           s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1137           copy = gimple_build_try (s1, s2, gimple_try_kind (stmt)); 
1138           break;
1139
1140         case GIMPLE_WITH_CLEANUP_EXPR:
1141           s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1142           copy = gimple_build_wce (s1);
1143           break;
1144
1145         case GIMPLE_OMP_PARALLEL:
1146           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1147           copy = gimple_build_omp_parallel
1148                    (s1,
1149                     gimple_omp_parallel_clauses (stmt),
1150                     gimple_omp_parallel_child_fn (stmt),
1151                     gimple_omp_parallel_data_arg (stmt));
1152           break;
1153
1154         case GIMPLE_OMP_TASK:
1155           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1156           copy = gimple_build_omp_task
1157                    (s1,
1158                     gimple_omp_task_clauses (stmt),
1159                     gimple_omp_task_child_fn (stmt),
1160                     gimple_omp_task_data_arg (stmt),
1161                     gimple_omp_task_copy_fn (stmt),
1162                     gimple_omp_task_arg_size (stmt),
1163                     gimple_omp_task_arg_align (stmt));
1164           break;
1165
1166         case GIMPLE_OMP_FOR:
1167           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1168           s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
1169           copy = gimple_build_omp_for (s1, gimple_omp_for_clauses (stmt),
1170                                        gimple_omp_for_collapse (stmt), s2);
1171           {
1172             size_t i;
1173             for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1174               {
1175                 gimple_omp_for_set_index (copy, i,
1176                                           gimple_omp_for_index (stmt, i));
1177                 gimple_omp_for_set_initial (copy, i,
1178                                             gimple_omp_for_initial (stmt, i));
1179                 gimple_omp_for_set_final (copy, i,
1180                                           gimple_omp_for_final (stmt, i));
1181                 gimple_omp_for_set_incr (copy, i,
1182                                          gimple_omp_for_incr (stmt, i));
1183                 gimple_omp_for_set_cond (copy, i,
1184                                          gimple_omp_for_cond (stmt, i));
1185               }
1186           }
1187           break;
1188
1189         case GIMPLE_OMP_MASTER:
1190           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1191           copy = gimple_build_omp_master (s1);
1192           break;
1193
1194         case GIMPLE_OMP_ORDERED:
1195           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1196           copy = gimple_build_omp_ordered (s1);
1197           break;
1198
1199         case GIMPLE_OMP_SECTION:
1200           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1201           copy = gimple_build_omp_section (s1);
1202           break;
1203
1204         case GIMPLE_OMP_SECTIONS:
1205           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1206           copy = gimple_build_omp_sections
1207                    (s1, gimple_omp_sections_clauses (stmt));
1208           break;
1209
1210         case GIMPLE_OMP_SINGLE:
1211           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1212           copy = gimple_build_omp_single
1213                    (s1, gimple_omp_single_clauses (stmt));
1214           break;
1215
1216         case GIMPLE_OMP_CRITICAL:
1217           s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
1218           copy
1219             = gimple_build_omp_critical (s1, gimple_omp_critical_name (stmt));
1220           break;
1221
1222         default:
1223           gcc_unreachable ();
1224         }
1225     }
1226   else
1227     {
1228       if (gimple_assign_copy_p (stmt)
1229           && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
1230           && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
1231         {
1232           /* Here we handle statements that are not completely rewritten.
1233              First we detect some inlining-induced bogosities for
1234              discarding.  */
1235
1236           /* Some assignments VAR = VAR; don't generate any rtl code
1237              and thus don't count as variable modification.  Avoid
1238              keeping bogosities like 0 = 0.  */
1239           tree decl = gimple_assign_lhs (stmt), value;
1240           tree *n;
1241
1242           n = (tree *) pointer_map_contains (id->decl_map, decl);
1243           if (n)
1244             {
1245               value = *n;
1246               STRIP_TYPE_NOPS (value);
1247               if (TREE_CONSTANT (value) || TREE_READONLY (value))
1248                 return gimple_build_nop ();
1249             }
1250         }
1251
1252       /* Create a new deep copy of the statement.  */
1253       copy = gimple_copy (stmt);
1254     }
1255
1256   /* If STMT has a block defined, map it to the newly constructed
1257      block.  When inlining we want statements without a block to
1258      appear in the block of the function call.  */
1259   new_block = id->block;
1260   if (gimple_block (copy))
1261     {
1262       tree *n;
1263       n = (tree *) pointer_map_contains (id->decl_map, gimple_block (copy));
1264       gcc_assert (n);
1265       new_block = *n;
1266     }
1267
1268   gimple_set_block (copy, new_block);
1269
1270   /* Remap all the operands in COPY.  */
1271   memset (&wi, 0, sizeof (wi));
1272   wi.info = id;
1273   if (skip_first)
1274     walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1275   else
1276     walk_gimple_op (copy, remap_gimple_op_r, &wi); 
1277
1278   /* We have to handle EH region remapping of GIMPLE_RESX specially because
1279      the region number is not an operand.  */
1280   if (gimple_code (stmt) == GIMPLE_RESX && id->eh_region_offset)
1281     {
1282       gimple_resx_set_region (copy, gimple_resx_region (stmt) + id->eh_region_offset);
1283     }
1284   return copy;
1285 }
1286
1287
1288 /* Copy basic block, scale profile accordingly.  Edges will be taken care of
1289    later  */
1290
1291 static basic_block
1292 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
1293          gcov_type count_scale)
1294 {
1295   gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1296   basic_block copy_basic_block;
1297   tree decl;
1298
1299   /* create_basic_block() will append every new block to
1300      basic_block_info automatically.  */
1301   copy_basic_block = create_basic_block (NULL, (void *) 0,
1302                                          (basic_block) bb->prev_bb->aux);
1303   copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1304
1305   /* We are going to rebuild frequencies from scratch.  These values
1306      have just small importance to drive canonicalize_loop_headers.  */
1307   copy_basic_block->frequency = ((gcov_type)bb->frequency
1308                                  * frequency_scale / REG_BR_PROB_BASE);
1309
1310   if (copy_basic_block->frequency > BB_FREQ_MAX)
1311     copy_basic_block->frequency = BB_FREQ_MAX;
1312
1313   copy_gsi = gsi_start_bb (copy_basic_block);
1314
1315   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1316     {
1317       gimple stmt = gsi_stmt (gsi);
1318       gimple orig_stmt = stmt;
1319
1320       id->regimplify = false;
1321       stmt = remap_gimple_stmt (stmt, id);
1322       if (gimple_nop_p (stmt))
1323         continue;
1324
1325       gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
1326       seq_gsi = copy_gsi;
1327
1328       /* With return slot optimization we can end up with
1329          non-gimple (foo *)&this->m, fix that here.  */
1330       if (is_gimple_assign (stmt)
1331           && gimple_assign_rhs_code (stmt) == NOP_EXPR
1332           && !is_gimple_val (gimple_assign_rhs1 (stmt)))
1333         {
1334           tree new_rhs;
1335           new_rhs = force_gimple_operand_gsi (&seq_gsi,
1336                                               gimple_assign_rhs1 (stmt),
1337                                               true, NULL, true, GSI_SAME_STMT);
1338           gimple_assign_set_rhs1 (stmt, new_rhs);
1339           id->regimplify = false;
1340         }
1341
1342       gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
1343
1344       if (id->regimplify)
1345         gimple_regimplify_operands (stmt, &seq_gsi);
1346
1347       /* If copy_basic_block has been empty at the start of this iteration,
1348          call gsi_start_bb again to get at the newly added statements.  */
1349       if (gsi_end_p (copy_gsi))
1350         copy_gsi = gsi_start_bb (copy_basic_block);
1351       else
1352         gsi_next (&copy_gsi);
1353
1354       /* Process the new statement.  The call to gimple_regimplify_operands
1355          possibly turned the statement into multiple statements, we
1356          need to process all of them.  */
1357       do
1358         {
1359           stmt = gsi_stmt (copy_gsi);
1360           if (is_gimple_call (stmt)
1361               && gimple_call_va_arg_pack_p (stmt)
1362               && id->gimple_call)
1363             {
1364               /* __builtin_va_arg_pack () should be replaced by
1365                  all arguments corresponding to ... in the caller.  */
1366               tree p;
1367               gimple new_call;
1368               VEC(tree, heap) *argarray;
1369               size_t nargs = gimple_call_num_args (id->gimple_call);
1370               size_t n;
1371
1372               for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1373                 nargs--;
1374
1375               /* Create the new array of arguments.  */
1376               n = nargs + gimple_call_num_args (stmt);
1377               argarray = VEC_alloc (tree, heap, n);
1378               VEC_safe_grow (tree, heap, argarray, n);
1379
1380               /* Copy all the arguments before '...'  */
1381               memcpy (VEC_address (tree, argarray),
1382                       gimple_call_arg_ptr (stmt, 0),
1383                       gimple_call_num_args (stmt) * sizeof (tree));
1384
1385               /* Append the arguments passed in '...'  */
1386               memcpy (VEC_address(tree, argarray) + gimple_call_num_args (stmt),
1387                       gimple_call_arg_ptr (id->gimple_call, 0)
1388                         + (gimple_call_num_args (id->gimple_call) - nargs),
1389                       nargs * sizeof (tree));
1390
1391               new_call = gimple_build_call_vec (gimple_call_fn (stmt),
1392                                                 argarray);
1393
1394               VEC_free (tree, heap, argarray);
1395
1396               /* Copy all GIMPLE_CALL flags, location and block, except
1397                  GF_CALL_VA_ARG_PACK.  */
1398               gimple_call_copy_flags (new_call, stmt);
1399               gimple_call_set_va_arg_pack (new_call, false);
1400               gimple_set_location (new_call, gimple_location (stmt));
1401               gimple_set_block (new_call, gimple_block (stmt));
1402               gimple_call_set_lhs (new_call, gimple_call_lhs (stmt));
1403
1404               gsi_replace (&copy_gsi, new_call, false);
1405               gimple_set_bb (stmt, NULL);
1406               stmt = new_call;
1407             }
1408           else if (is_gimple_call (stmt)
1409                    && id->gimple_call
1410                    && (decl = gimple_call_fndecl (stmt))
1411                    && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1412                    && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
1413             {
1414               /* __builtin_va_arg_pack_len () should be replaced by
1415                  the number of anonymous arguments.  */
1416               size_t nargs = gimple_call_num_args (id->gimple_call);
1417               tree count, p;
1418               gimple new_stmt;
1419
1420               for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
1421                 nargs--;
1422
1423               count = build_int_cst (integer_type_node, nargs);
1424               new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
1425               gsi_replace (&copy_gsi, new_stmt, false);
1426               stmt = new_stmt;
1427             }
1428
1429           /* Statements produced by inlining can be unfolded, especially
1430              when we constant propagated some operands.  We can't fold
1431              them right now for two reasons:
1432              1) folding require SSA_NAME_DEF_STMTs to be correct
1433              2) we can't change function calls to builtins.
1434              So we just mark statement for later folding.  We mark
1435              all new statements, instead just statements that has changed
1436              by some nontrivial substitution so even statements made
1437              foldable indirectly are updated.  If this turns out to be
1438              expensive, copy_body can be told to watch for nontrivial
1439              changes.  */
1440           if (id->statements_to_fold)
1441             pointer_set_insert (id->statements_to_fold, stmt);
1442
1443           /* We're duplicating a CALL_EXPR.  Find any corresponding
1444              callgraph edges and update or duplicate them.  */
1445           if (is_gimple_call (stmt))
1446             {
1447               struct cgraph_node *node;
1448               struct cgraph_edge *edge;
1449               int flags;
1450
1451               switch (id->transform_call_graph_edges)
1452                 {
1453               case CB_CGE_DUPLICATE:
1454                 edge = cgraph_edge (id->src_node, orig_stmt);
1455                 if (edge)
1456                   cgraph_clone_edge (edge, id->dst_node, stmt,
1457                                            REG_BR_PROB_BASE, 1,
1458                                            edge->frequency, true);
1459                 break;
1460
1461               case CB_CGE_MOVE_CLONES:
1462                 for (node = id->dst_node->next_clone;
1463                     node;
1464                     node = node->next_clone)
1465                   {
1466                     edge = cgraph_edge (node, orig_stmt);
1467                           if (edge)
1468                             cgraph_set_call_stmt (edge, stmt);
1469                   }
1470                 /* FALLTHRU */
1471
1472               case CB_CGE_MOVE:
1473                 edge = cgraph_edge (id->dst_node, orig_stmt);
1474                 if (edge)
1475                   cgraph_set_call_stmt (edge, stmt);
1476                 break;
1477
1478               default:
1479                 gcc_unreachable ();
1480                 }
1481
1482               flags = gimple_call_flags (stmt);
1483
1484               if (flags & ECF_MAY_BE_ALLOCA)
1485                 cfun->calls_alloca = true;
1486               if (flags & ECF_RETURNS_TWICE)
1487                 cfun->calls_setjmp = true;
1488             }
1489
1490           /* If you think we can abort here, you are wrong.
1491              There is no region 0 in gimple.  */
1492           gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) != 0);
1493
1494           if (stmt_could_throw_p (stmt)
1495               /* When we are cloning for inlining, we are supposed to
1496                  construct a clone that calls precisely the same functions
1497                  as original.  However IPA optimizers might've proved
1498                  earlier some function calls as non-trapping that might
1499                  render some basic blocks dead that might become
1500                  unreachable.
1501
1502                  We can't update SSA with unreachable blocks in CFG and thus
1503                  we prevent the scenario by preserving even the "dead" eh
1504                  edges until the point they are later removed by
1505                  fixup_cfg pass.  */
1506               || (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
1507                   && lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) > 0))
1508             {
1509               int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
1510
1511               /* Add an entry for the copied tree in the EH hashtable.
1512                  When cloning or versioning, use the hashtable in
1513                  cfun, and just copy the EH number.  When inlining, use the
1514                  hashtable in the caller, and adjust the region number.  */
1515               if (region > 0)
1516                 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
1517
1518               /* If this tree doesn't have a region associated with it,
1519                  and there is a "current region,"
1520                  then associate this tree with the current region
1521                  and add edges associated with this region.  */
1522               if (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) <= 0
1523                   && id->eh_region > 0
1524                   && stmt_could_throw_p (stmt))
1525                 add_stmt_to_eh_region (stmt, id->eh_region);
1526             }
1527
1528           if (gimple_in_ssa_p (cfun))
1529             {
1530               ssa_op_iter i;
1531               tree def;
1532
1533               find_new_referenced_vars (gsi_stmt (copy_gsi));
1534               FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
1535                 if (TREE_CODE (def) == SSA_NAME)
1536                   SSA_NAME_DEF_STMT (def) = stmt;
1537             }
1538
1539           gsi_next (&copy_gsi);
1540         }
1541       while (!gsi_end_p (copy_gsi));
1542
1543       copy_gsi = gsi_last_bb (copy_basic_block);
1544     }
1545
1546   return copy_basic_block;
1547 }
1548
1549 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
1550    form is quite easy, since dominator relationship for old basic blocks does
1551    not change.
1552
1553    There is however exception where inlining might change dominator relation
1554    across EH edges from basic block within inlined functions destinating
1555    to landing pads in function we inline into.
1556
1557    The function fills in PHI_RESULTs of such PHI nodes if they refer
1558    to gimple regs.  Otherwise, the function mark PHI_RESULT of such
1559    PHI nodes for renaming.  For non-gimple regs, renaming is safe: the
1560    EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
1561    set, and this means that there will be no overlapping live ranges
1562    for the underlying symbol.
1563
1564    This might change in future if we allow redirecting of EH edges and
1565    we might want to change way build CFG pre-inlining to include
1566    all the possible edges then.  */
1567 static void
1568 update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
1569                                   bool can_throw, bool nonlocal_goto)
1570 {
1571   edge e;
1572   edge_iterator ei;
1573
1574   FOR_EACH_EDGE (e, ei, bb->succs)
1575     if (!e->dest->aux
1576         || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1577       {
1578         gimple phi;
1579         gimple_stmt_iterator si;
1580
1581         gcc_assert (e->flags & EDGE_ABNORMAL);
1582
1583         if (!nonlocal_goto)
1584           gcc_assert (e->flags & EDGE_EH);
1585
1586         if (!can_throw)
1587           gcc_assert (!(e->flags & EDGE_EH));
1588
1589         for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
1590           {
1591             edge re;
1592
1593             phi = gsi_stmt (si);
1594
1595             /* There shouldn't be any PHI nodes in the ENTRY_BLOCK.  */
1596             gcc_assert (!e->dest->aux);
1597
1598             gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1599
1600             if (!is_gimple_reg (PHI_RESULT (phi)))
1601               {
1602                 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1603                 continue;
1604               }
1605
1606             re = find_edge (ret_bb, e->dest);
1607             gcc_assert (re);
1608             gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
1609                         == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
1610
1611             SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1612                      USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
1613           }
1614       }
1615 }
1616
1617
1618 /* Copy edges from BB into its copy constructed earlier, scale profile
1619    accordingly.  Edges will be taken care of later.  Assume aux
1620    pointers to point to the copies of each BB.  */
1621
1622 static void
1623 copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb)
1624 {
1625   basic_block new_bb = (basic_block) bb->aux;
1626   edge_iterator ei;
1627   edge old_edge;
1628   gimple_stmt_iterator si;
1629   int flags;
1630
1631   /* Use the indices from the original blocks to create edges for the
1632      new ones.  */
1633   FOR_EACH_EDGE (old_edge, ei, bb->succs)
1634     if (!(old_edge->flags & EDGE_EH))
1635       {
1636         edge new_edge;
1637
1638         flags = old_edge->flags;
1639
1640         /* Return edges do get a FALLTHRU flag when the get inlined.  */
1641         if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
1642             && old_edge->dest->aux != EXIT_BLOCK_PTR)
1643           flags |= EDGE_FALLTHRU;
1644         new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
1645         new_edge->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
1646         new_edge->probability = old_edge->probability;
1647       }
1648
1649   if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
1650     return;
1651
1652   for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
1653     {
1654       gimple copy_stmt;
1655       bool can_throw, nonlocal_goto;
1656
1657       copy_stmt = gsi_stmt (si);
1658       update_stmt (copy_stmt);
1659       if (gimple_in_ssa_p (cfun))
1660         mark_symbols_for_renaming (copy_stmt);
1661
1662       /* Do this before the possible split_block.  */
1663       gsi_next (&si);
1664
1665       /* If this tree could throw an exception, there are two
1666          cases where we need to add abnormal edge(s): the
1667          tree wasn't in a region and there is a "current
1668          region" in the caller; or the original tree had
1669          EH edges.  In both cases split the block after the tree,
1670          and add abnormal edge(s) as needed; we need both
1671          those from the callee and the caller.
1672          We check whether the copy can throw, because the const
1673          propagation can change an INDIRECT_REF which throws
1674          into a COMPONENT_REF which doesn't.  If the copy
1675          can throw, the original could also throw.  */
1676       can_throw = stmt_can_throw_internal (copy_stmt);
1677       nonlocal_goto = stmt_can_make_abnormal_goto (copy_stmt);
1678
1679       if (can_throw || nonlocal_goto)
1680         {
1681           if (!gsi_end_p (si))
1682             /* Note that bb's predecessor edges aren't necessarily
1683                right at this point; split_block doesn't care.  */
1684             {
1685               edge e = split_block (new_bb, copy_stmt);
1686
1687               new_bb = e->dest;
1688               new_bb->aux = e->src->aux;
1689               si = gsi_start_bb (new_bb);
1690             }
1691         }
1692
1693       if (can_throw)
1694         make_eh_edges (copy_stmt);
1695
1696       if (nonlocal_goto)
1697         make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1698
1699       if ((can_throw || nonlocal_goto)
1700           && gimple_in_ssa_p (cfun))
1701         update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
1702                                           can_throw, nonlocal_goto);
1703     }
1704 }
1705
1706 /* Copy the PHIs.  All blocks and edges are copied, some blocks
1707    was possibly split and new outgoing EH edges inserted.
1708    BB points to the block of original function and AUX pointers links
1709    the original and newly copied blocks.  */
1710
1711 static void
1712 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1713 {
1714   basic_block const new_bb = (basic_block) bb->aux;
1715   edge_iterator ei;
1716   gimple phi;
1717   gimple_stmt_iterator si;
1718
1719   for (si = gsi_start (phi_nodes (bb)); !gsi_end_p (si); gsi_next (&si))
1720     {
1721       tree res, new_res;
1722       gimple new_phi;
1723       edge new_edge;
1724
1725       phi = gsi_stmt (si);
1726       res = PHI_RESULT (phi);
1727       new_res = res;
1728       if (is_gimple_reg (res))
1729         {
1730           walk_tree (&new_res, copy_tree_body_r, id, NULL);
1731           SSA_NAME_DEF_STMT (new_res)
1732             = new_phi = create_phi_node (new_res, new_bb);
1733           FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1734             {
1735               edge const old_edge
1736                 = find_edge ((basic_block) new_edge->src->aux, bb);
1737               tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1738               tree new_arg = arg;
1739               tree block = id->block;
1740               id->block = NULL_TREE;
1741               walk_tree (&new_arg, copy_tree_body_r, id, NULL);
1742               id->block = block;
1743               gcc_assert (new_arg);
1744               /* With return slot optimization we can end up with
1745                  non-gimple (foo *)&this->m, fix that here.  */
1746               if (TREE_CODE (new_arg) != SSA_NAME
1747                   && TREE_CODE (new_arg) != FUNCTION_DECL
1748                   && !is_gimple_val (new_arg))
1749                 {
1750                   gimple_seq stmts = NULL;
1751                   new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1752                   gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1753                 }
1754               add_phi_arg (new_phi, new_arg, new_edge);
1755             }
1756         }
1757     }
1758 }
1759
1760
1761 /* Wrapper for remap_decl so it can be used as a callback.  */
1762
1763 static tree
1764 remap_decl_1 (tree decl, void *data)
1765 {
1766   return remap_decl (decl, (copy_body_data *) data);
1767 }
1768
1769 /* Build struct function and associated datastructures for the new clone
1770    NEW_FNDECL to be build.  CALLEE_FNDECL is the original */
1771
1772 static void
1773 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1774                  int frequency)
1775 {
1776   struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1777   gcov_type count_scale, frequency_scale;
1778
1779   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1780     count_scale = (REG_BR_PROB_BASE * count
1781                    / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1782   else
1783     count_scale = 1;
1784
1785   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1786     frequency_scale = (REG_BR_PROB_BASE * frequency
1787                        /
1788                        ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1789   else
1790     frequency_scale = count_scale;
1791
1792   /* Register specific tree functions.  */
1793   gimple_register_cfg_hooks ();
1794
1795   /* Get clean struct function.  */
1796   push_struct_function (new_fndecl);
1797
1798   /* We will rebuild these, so just sanity check that they are empty.  */
1799   gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
1800   gcc_assert (cfun->local_decls == NULL);
1801   gcc_assert (cfun->cfg == NULL);
1802   gcc_assert (cfun->decl == new_fndecl);
1803
1804   /* Copy items we preserve during clonning.  */
1805   cfun->static_chain_decl = src_cfun->static_chain_decl;
1806   cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
1807   cfun->function_end_locus = src_cfun->function_end_locus;
1808   cfun->curr_properties = src_cfun->curr_properties;
1809   cfun->last_verified = src_cfun->last_verified;
1810   if (src_cfun->ipa_transforms_to_apply)
1811     cfun->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
1812                                               src_cfun->ipa_transforms_to_apply);
1813   cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
1814   cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
1815   cfun->function_frequency = src_cfun->function_frequency;
1816   cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
1817   cfun->stdarg = src_cfun->stdarg;
1818   cfun->dont_save_pending_sizes_p = src_cfun->dont_save_pending_sizes_p;
1819   cfun->after_inlining = src_cfun->after_inlining;
1820   cfun->returns_struct = src_cfun->returns_struct;
1821   cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
1822   cfun->after_tree_profile = src_cfun->after_tree_profile;
1823
1824   init_empty_tree_cfg ();
1825
1826   ENTRY_BLOCK_PTR->count =
1827     (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1828      REG_BR_PROB_BASE);
1829   ENTRY_BLOCK_PTR->frequency =
1830     (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1831      frequency_scale / REG_BR_PROB_BASE);
1832   EXIT_BLOCK_PTR->count =
1833     (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1834      REG_BR_PROB_BASE);
1835   EXIT_BLOCK_PTR->frequency =
1836     (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1837      frequency_scale / REG_BR_PROB_BASE);
1838   if (src_cfun->eh)
1839     init_eh_for_function ();
1840
1841   if (src_cfun->gimple_df)
1842     {
1843       init_tree_ssa (cfun);
1844       cfun->gimple_df->in_ssa_p = true;
1845       init_ssa_operands ();
1846     }
1847   pop_cfun ();
1848 }
1849
1850 /* Make a copy of the body of FN so that it can be inserted inline in
1851    another function.  Walks FN via CFG, returns new fndecl.  */
1852
1853 static tree
1854 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1855                basic_block entry_block_map, basic_block exit_block_map)
1856 {
1857   tree callee_fndecl = id->src_fn;
1858   /* Original cfun for the callee, doesn't change.  */
1859   struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1860   struct function *cfun_to_copy;
1861   basic_block bb;
1862   tree new_fndecl = NULL;
1863   gcov_type count_scale, frequency_scale;
1864   int last;
1865
1866   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1867     count_scale = (REG_BR_PROB_BASE * count
1868                    / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1869   else
1870     count_scale = 1;
1871
1872   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1873     frequency_scale = (REG_BR_PROB_BASE * frequency
1874                        /
1875                        ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1876   else
1877     frequency_scale = count_scale;
1878
1879   /* Register specific tree functions.  */
1880   gimple_register_cfg_hooks ();
1881
1882   /* Must have a CFG here at this point.  */
1883   gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1884               (DECL_STRUCT_FUNCTION (callee_fndecl)));
1885
1886   cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1887
1888   ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1889   EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1890   entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1891   exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1892
1893   /* Duplicate any exception-handling regions.  */
1894   if (cfun->eh)
1895     {
1896       id->eh_region_offset
1897         = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1898                                 0, id->eh_region);
1899     }
1900
1901   /* Use aux pointers to map the original blocks to copy.  */
1902   FOR_EACH_BB_FN (bb, cfun_to_copy)
1903     {
1904       basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
1905       bb->aux = new_bb;
1906       new_bb->aux = bb;
1907     }
1908
1909   last = last_basic_block;
1910
1911   /* Now that we've duplicated the blocks, duplicate their edges.  */
1912   FOR_ALL_BB_FN (bb, cfun_to_copy)
1913     copy_edges_for_bb (bb, count_scale, exit_block_map);
1914
1915   if (gimple_in_ssa_p (cfun))
1916     FOR_ALL_BB_FN (bb, cfun_to_copy)
1917       copy_phis_for_bb (bb, id);
1918
1919   FOR_ALL_BB_FN (bb, cfun_to_copy)
1920     {
1921       ((basic_block)bb->aux)->aux = NULL;
1922       bb->aux = NULL;
1923     }
1924
1925   /* Zero out AUX fields of newly created block during EH edge
1926      insertion. */
1927   for (; last < last_basic_block; last++)
1928     BASIC_BLOCK (last)->aux = NULL;
1929   entry_block_map->aux = NULL;
1930   exit_block_map->aux = NULL;
1931
1932   return new_fndecl;
1933 }
1934
1935 static tree
1936 copy_body (copy_body_data *id, gcov_type count, int frequency,
1937            basic_block entry_block_map, basic_block exit_block_map)
1938 {
1939   tree fndecl = id->src_fn;
1940   tree body;
1941
1942   /* If this body has a CFG, walk CFG and copy.  */
1943   gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1944   body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1945
1946   return body;
1947 }
1948
1949 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1950    defined in function FN, or of a data member thereof.  */
1951
1952 static bool
1953 self_inlining_addr_expr (tree value, tree fn)
1954 {
1955   tree var;
1956
1957   if (TREE_CODE (value) != ADDR_EXPR)
1958     return false;
1959
1960   var = get_base_address (TREE_OPERAND (value, 0));
1961
1962   return var && auto_var_in_fn_p (var, fn);
1963 }
1964
1965 static void
1966 insert_init_stmt (basic_block bb, gimple init_stmt)
1967 {
1968   /* If VAR represents a zero-sized variable, it's possible that the
1969      assignment statement may result in no gimple statements.  */
1970   if (init_stmt)
1971     {
1972       gimple_stmt_iterator si = gsi_last_bb (bb);
1973
1974       /* We can end up with init statements that store to a non-register
1975          from a rhs with a conversion.  Handle that here by forcing the
1976          rhs into a temporary.  gimple_regimplify_operands is not
1977          prepared to do this for us.  */
1978       if (!is_gimple_reg (gimple_assign_lhs (init_stmt))
1979           && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
1980           && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
1981         {
1982           tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
1983                              gimple_expr_type (init_stmt),
1984                              gimple_assign_rhs1 (init_stmt));
1985           rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
1986                                           GSI_NEW_STMT);
1987           gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
1988           gimple_assign_set_rhs1 (init_stmt, rhs);
1989         }
1990       gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
1991       gimple_regimplify_operands (init_stmt, &si);
1992       mark_symbols_for_renaming (init_stmt);
1993     }
1994 }
1995
1996 /* Initialize parameter P with VALUE.  If needed, produce init statement
1997    at the end of BB.  When BB is NULL, we return init statement to be
1998    output later.  */
1999 static gimple
2000 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
2001                      basic_block bb, tree *vars)
2002 {
2003   gimple init_stmt = NULL;
2004   tree var;
2005   tree rhs = value;
2006   tree def = (gimple_in_ssa_p (cfun)
2007               ? gimple_default_def (id->src_cfun, p) : NULL);
2008
2009   if (value
2010       && value != error_mark_node
2011       && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
2012     {
2013       if (fold_convertible_p (TREE_TYPE (p), value))
2014         rhs = fold_build1 (NOP_EXPR, TREE_TYPE (p), value);
2015       else
2016         /* ???  For valid (GIMPLE) programs we should not end up here.
2017            Still if something has gone wrong and we end up with truly
2018            mismatched types here, fall back to using a VIEW_CONVERT_EXPR
2019            to not leak invalid GIMPLE to the following passes.  */
2020         rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2021     }
2022
2023   /* If the parameter is never assigned to, has no SSA_NAMEs created,
2024      we may not need to create a new variable here at all.  Instead, we may
2025      be able to just use the argument value.  */
2026   if (TREE_READONLY (p)
2027       && !TREE_ADDRESSABLE (p)
2028       && value && !TREE_SIDE_EFFECTS (value)
2029       && !def)
2030     {
2031       /* We may produce non-gimple trees by adding NOPs or introduce
2032          invalid sharing when operand is not really constant.
2033          It is not big deal to prohibit constant propagation here as
2034          we will constant propagate in DOM1 pass anyway.  */
2035       if (is_gimple_min_invariant (value)
2036           && useless_type_conversion_p (TREE_TYPE (p),
2037                                                  TREE_TYPE (value))
2038           /* We have to be very careful about ADDR_EXPR.  Make sure
2039              the base variable isn't a local variable of the inlined
2040              function, e.g., when doing recursive inlining, direct or
2041              mutually-recursive or whatever, which is why we don't
2042              just test whether fn == current_function_decl.  */
2043           && ! self_inlining_addr_expr (value, fn))
2044         {
2045           insert_decl_map (id, p, value);
2046           return NULL;
2047         }
2048     }
2049
2050   /* Make an equivalent VAR_DECL.  Note that we must NOT remap the type
2051      here since the type of this decl must be visible to the calling
2052      function.  */
2053   var = copy_decl_to_var (p, id);
2054   if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2055     {
2056       get_var_ann (var);
2057       add_referenced_var (var);
2058     }
2059
2060   /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2061      that way, when the PARM_DECL is encountered, it will be
2062      automatically replaced by the VAR_DECL.  */
2063   insert_decl_map (id, p, var);
2064
2065   /* Declare this new variable.  */
2066   TREE_CHAIN (var) = *vars;
2067   *vars = var;
2068
2069   /* Make gimplifier happy about this variable.  */
2070   DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2071
2072   /* Even if P was TREE_READONLY, the new VAR should not be.
2073      In the original code, we would have constructed a
2074      temporary, and then the function body would have never
2075      changed the value of P.  However, now, we will be
2076      constructing VAR directly.  The constructor body may
2077      change its value multiple times as it is being
2078      constructed.  Therefore, it must not be TREE_READONLY;
2079      the back-end assumes that TREE_READONLY variable is
2080      assigned to only once.  */
2081   if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
2082     TREE_READONLY (var) = 0;
2083
2084   /* If there is no setup required and we are in SSA, take the easy route
2085      replacing all SSA names representing the function parameter by the
2086      SSA name passed to function.
2087
2088      We need to construct map for the variable anyway as it might be used
2089      in different SSA names when parameter is set in function.
2090
2091      FIXME: This usually kills the last connection in between inlined
2092      function parameter and the actual value in debug info.  Can we do
2093      better here?  If we just inserted the statement, copy propagation
2094      would kill it anyway as it always did in older versions of GCC.
2095
2096      We might want to introduce a notion that single SSA_NAME might
2097      represent multiple variables for purposes of debugging. */
2098   if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2099       && optimize
2100       && (TREE_CODE (rhs) == SSA_NAME
2101           || is_gimple_min_invariant (rhs))
2102       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2103     {
2104       insert_decl_map (id, def, rhs);
2105       return NULL;
2106     }
2107
2108   /* If the value of argument is never used, don't care about initializing
2109      it.  */
2110   if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2111     {
2112       gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2113       return NULL;
2114     }
2115
2116   /* Initialize this VAR_DECL from the equivalent argument.  Convert
2117      the argument to the proper type in case it was promoted.  */
2118   if (value)
2119     {
2120       if (rhs == error_mark_node)
2121         {
2122           insert_decl_map (id, p, var);
2123           return NULL;
2124         }
2125
2126       STRIP_USELESS_TYPE_CONVERSION (rhs);
2127
2128       /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2129          keep our trees in gimple form.  */
2130       if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
2131         {
2132           def = remap_ssa_name (def, id);
2133           init_stmt = gimple_build_assign (def, rhs);
2134           SSA_NAME_IS_DEFAULT_DEF (def) = 0;
2135           set_default_def (var, NULL);
2136         }
2137       else
2138         init_stmt = gimple_build_assign (var, rhs);
2139
2140       if (bb && init_stmt)
2141         insert_init_stmt (bb, init_stmt);
2142     }
2143   return init_stmt;
2144 }
2145
2146 /* Generate code to initialize the parameters of the function at the
2147    top of the stack in ID from the GIMPLE_CALL STMT.  */
2148
2149 static void
2150 initialize_inlined_parameters (copy_body_data *id, gimple stmt,
2151                                tree fn, basic_block bb)
2152 {
2153   tree parms;
2154   size_t i;
2155   tree p;
2156   tree vars = NULL_TREE;
2157   tree static_chain = gimple_call_chain (stmt);
2158
2159   /* Figure out what the parameters are.  */
2160   parms = DECL_ARGUMENTS (fn);
2161
2162   /* Loop through the parameter declarations, replacing each with an
2163      equivalent VAR_DECL, appropriately initialized.  */
2164   for (p = parms, i = 0; p; p = TREE_CHAIN (p), i++)
2165     {
2166       tree val;
2167       val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
2168       setup_one_parameter (id, p, val, fn, bb, &vars);
2169     }
2170
2171   /* Initialize the static chain.  */
2172   p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
2173   gcc_assert (fn != current_function_decl);
2174   if (p)
2175     {
2176       /* No static chain?  Seems like a bug in tree-nested.c.  */
2177       gcc_assert (static_chain);
2178
2179       setup_one_parameter (id, p, static_chain, fn, bb, &vars);
2180     }
2181
2182   declare_inline_vars (id->block, vars);
2183 }
2184
2185
2186 /* Declare a return variable to replace the RESULT_DECL for the
2187    function we are calling.  An appropriate DECL_STMT is returned.
2188    The USE_STMT is filled to contain a use of the declaration to
2189    indicate the return value of the function.
2190
2191    RETURN_SLOT, if non-null is place where to store the result.  It
2192    is set only for CALL_EXPR_RETURN_SLOT_OPT.  MODIFY_DEST, if non-null,
2193    was the LHS of the MODIFY_EXPR to which this call is the RHS.
2194
2195    The return value is a (possibly null) value that is the result of the
2196    function as seen by the callee.  *USE_P is a (possibly null) value that
2197    holds the result as seen by the caller.  */
2198
2199 static tree
2200 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
2201                          tree *use_p)
2202 {
2203   tree callee = id->src_fn;
2204   tree caller = id->dst_fn;
2205   tree result = DECL_RESULT (callee);
2206   tree callee_type = TREE_TYPE (result);
2207   tree caller_type = TREE_TYPE (TREE_TYPE (callee));
2208   tree var, use;
2209
2210   /* We don't need to do anything for functions that don't return
2211      anything.  */
2212   if (!result || VOID_TYPE_P (callee_type))
2213     {
2214       *use_p = NULL_TREE;
2215       return NULL_TREE;
2216     }
2217
2218   /* If there was a return slot, then the return value is the
2219      dereferenced address of that object.  */
2220   if (return_slot)
2221     {
2222       /* The front end shouldn't have used both return_slot and
2223          a modify expression.  */
2224       gcc_assert (!modify_dest);
2225       if (DECL_BY_REFERENCE (result))
2226         {
2227           tree return_slot_addr = build_fold_addr_expr (return_slot);
2228           STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2229
2230           /* We are going to construct *&return_slot and we can't do that
2231              for variables believed to be not addressable. 
2232
2233              FIXME: This check possibly can match, because values returned
2234              via return slot optimization are not believed to have address
2235              taken by alias analysis.  */
2236           gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2237           if (gimple_in_ssa_p (cfun))
2238             {
2239               HOST_WIDE_INT bitsize;
2240               HOST_WIDE_INT bitpos;
2241               tree offset;
2242               enum machine_mode mode;
2243               int unsignedp;
2244               int volatilep;
2245               tree base;
2246               base = get_inner_reference (return_slot, &bitsize, &bitpos,
2247                                           &offset,
2248                                           &mode, &unsignedp, &volatilep,
2249                                           false);
2250               if (TREE_CODE (base) == INDIRECT_REF)
2251                 base = TREE_OPERAND (base, 0);
2252               if (TREE_CODE (base) == SSA_NAME)
2253                 base = SSA_NAME_VAR (base);
2254               mark_sym_for_renaming (base);
2255             }
2256           var = return_slot_addr;
2257         }
2258       else
2259         {
2260           var = return_slot;
2261           gcc_assert (TREE_CODE (var) != SSA_NAME);
2262           TREE_ADDRESSABLE (var) |= TREE_ADDRESSABLE (result);
2263         }
2264       if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2265            || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2266           && !DECL_GIMPLE_REG_P (result)
2267           && DECL_P (var))
2268         DECL_GIMPLE_REG_P (var) = 0;
2269       use = NULL;
2270       goto done;
2271     }
2272
2273   /* All types requiring non-trivial constructors should have been handled.  */
2274   gcc_assert (!TREE_ADDRESSABLE (callee_type));
2275
2276   /* Attempt to avoid creating a new temporary variable.  */
2277   if (modify_dest
2278       && TREE_CODE (modify_dest) != SSA_NAME)
2279     {
2280       bool use_it = false;
2281
2282       /* We can't use MODIFY_DEST if there's type promotion involved.  */
2283       if (!useless_type_conversion_p (callee_type, caller_type))
2284         use_it = false;
2285
2286       /* ??? If we're assigning to a variable sized type, then we must
2287          reuse the destination variable, because we've no good way to
2288          create variable sized temporaries at this point.  */
2289       else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
2290         use_it = true;
2291
2292       /* If the callee cannot possibly modify MODIFY_DEST, then we can
2293          reuse it as the result of the call directly.  Don't do this if
2294          it would promote MODIFY_DEST to addressable.  */
2295       else if (TREE_ADDRESSABLE (result))
2296         use_it = false;
2297       else
2298         {
2299           tree base_m = get_base_address (modify_dest);
2300
2301           /* If the base isn't a decl, then it's a pointer, and we don't
2302              know where that's going to go.  */
2303           if (!DECL_P (base_m))
2304             use_it = false;
2305           else if (is_global_var (base_m))
2306             use_it = false;
2307           else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
2308                     || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
2309                    && !DECL_GIMPLE_REG_P (result)
2310                    && DECL_GIMPLE_REG_P (base_m))
2311             use_it = false;
2312           else if (!TREE_ADDRESSABLE (base_m))
2313             use_it = true;
2314         }
2315
2316       if (use_it)
2317         {
2318           var = modify_dest;
2319           use = NULL;
2320           goto done;
2321         }
2322     }
2323
2324   gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
2325
2326   var = copy_result_decl_to_var (result, id);
2327   if (gimple_in_ssa_p (cfun))
2328     {
2329       get_var_ann (var);
2330       add_referenced_var (var);
2331     }
2332
2333   DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2334   DECL_STRUCT_FUNCTION (caller)->local_decls
2335     = tree_cons (NULL_TREE, var,
2336                  DECL_STRUCT_FUNCTION (caller)->local_decls);
2337
2338   /* Do not have the rest of GCC warn about this variable as it should
2339      not be visible to the user.  */
2340   TREE_NO_WARNING (var) = 1;
2341
2342   declare_inline_vars (id->block, var);
2343
2344   /* Build the use expr.  If the return type of the function was
2345      promoted, convert it back to the expected type.  */
2346   use = var;
2347   if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2348     use = fold_convert (caller_type, var);
2349     
2350   STRIP_USELESS_TYPE_CONVERSION (use);
2351
2352   if (DECL_BY_REFERENCE (result))
2353     var = build_fold_addr_expr (var);
2354
2355  done:
2356   /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2357      way, when the RESULT_DECL is encountered, it will be
2358      automatically replaced by the VAR_DECL.  */
2359   insert_decl_map (id, result, var);
2360
2361   /* Remember this so we can ignore it in remap_decls.  */
2362   id->retvar = var;
2363
2364   *use_p = use;
2365   return var;
2366 }
2367
2368 /* Returns nonzero if a function can be inlined as a tree.  */
2369
2370 bool
2371 tree_inlinable_function_p (tree fn)
2372 {
2373   return inlinable_function_p (fn);
2374 }
2375
2376 static const char *inline_forbidden_reason;
2377
2378 /* A callback for walk_gimple_seq to handle tree operands.  Returns
2379    NULL_TREE if a function can be inlined, otherwise sets the reason
2380    why not and returns a tree representing the offending operand. */
2381
2382 static tree
2383 inline_forbidden_p_op (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
2384                          void *fnp ATTRIBUTE_UNUSED)
2385 {
2386   tree node = *nodep;
2387   tree t;
2388
2389   if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE)
2390     {
2391       /* We cannot inline a function of the form
2392
2393            void F (int i) { struct S { int ar[i]; } s; }
2394
2395          Attempting to do so produces a catch-22.
2396          If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
2397          UNION_TYPE nodes, then it goes into infinite recursion on a
2398          structure containing a pointer to its own type.  If it doesn't,
2399          then the type node for S doesn't get adjusted properly when
2400          F is inlined. 
2401
2402          ??? This is likely no longer true, but it's too late in the 4.0
2403          cycle to try to find out.  This should be checked for 4.1.  */
2404       for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
2405         if (variably_modified_type_p (TREE_TYPE (t), NULL))
2406           {
2407             inline_forbidden_reason
2408               = G_("function %q+F can never be inlined "
2409                    "because it uses variable sized variables");
2410             return node;
2411           }
2412     }
2413
2414   return NULL_TREE;
2415 }
2416
2417
2418 /* A callback for walk_gimple_seq to handle statements.  Returns
2419    non-NULL iff a function can not be inlined.  Also sets the reason
2420    why. */
2421
2422 static tree
2423 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2424                          struct walk_stmt_info *wip)
2425 {
2426   tree fn = (tree) wip->info;
2427   tree t;
2428   gimple stmt = gsi_stmt (*gsi);
2429
2430   switch (gimple_code (stmt))
2431     {
2432     case GIMPLE_CALL:
2433       /* Refuse to inline alloca call unless user explicitly forced so as
2434          this may change program's memory overhead drastically when the
2435          function using alloca is called in loop.  In GCC present in
2436          SPEC2000 inlining into schedule_block cause it to require 2GB of
2437          RAM instead of 256MB.  */
2438       if (gimple_alloca_call_p (stmt)
2439           && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
2440         {
2441           inline_forbidden_reason
2442             = G_("function %q+F can never be inlined because it uses "
2443                  "alloca (override using the always_inline attribute)");
2444           *handled_ops_p = true;
2445           return fn;
2446         }
2447
2448       t = gimple_call_fndecl (stmt);
2449       if (t == NULL_TREE)
2450         break;
2451
2452       /* We cannot inline functions that call setjmp.  */
2453       if (setjmp_call_p (t))
2454         {
2455           inline_forbidden_reason
2456             = G_("function %q+F can never be inlined because it uses setjmp");
2457           *handled_ops_p = true;
2458           return t;
2459         }
2460
2461       if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
2462         switch (DECL_FUNCTION_CODE (t))
2463           {
2464             /* We cannot inline functions that take a variable number of
2465                arguments.  */
2466           case BUILT_IN_VA_START:
2467           case BUILT_IN_NEXT_ARG:
2468           case BUILT_IN_VA_END:
2469             inline_forbidden_reason
2470               = G_("function %q+F can never be inlined because it "
2471                    "uses variable argument lists");
2472             *handled_ops_p = true;
2473             return t;
2474
2475           case BUILT_IN_LONGJMP:
2476             /* We can't inline functions that call __builtin_longjmp at
2477                all.  The non-local goto machinery really requires the
2478                destination be in a different function.  If we allow the
2479                function calling __builtin_longjmp to be inlined into the
2480                function calling __builtin_setjmp, Things will Go Awry.  */
2481             inline_forbidden_reason
2482               = G_("function %q+F can never be inlined because "
2483                    "it uses setjmp-longjmp exception handling");
2484             *handled_ops_p = true;
2485             return t;
2486
2487           case BUILT_IN_NONLOCAL_GOTO:
2488             /* Similarly.  */
2489             inline_forbidden_reason
2490               = G_("function %q+F can never be inlined because "
2491                    "it uses non-local goto");
2492             *handled_ops_p = true;
2493             return t;
2494
2495           case BUILT_IN_RETURN:
2496           case BUILT_IN_APPLY_ARGS:
2497             /* If a __builtin_apply_args caller would be inlined,
2498                it would be saving arguments of the function it has
2499                been inlined into.  Similarly __builtin_return would
2500                return from the function the inline has been inlined into.  */
2501             inline_forbidden_reason
2502               = G_("function %q+F can never be inlined because "
2503                    "it uses __builtin_return or __builtin_apply_args");
2504             *handled_ops_p = true;
2505             return t;
2506
2507           default:
2508             break;
2509           }
2510       break;
2511
2512     case GIMPLE_GOTO:
2513       t = gimple_goto_dest (stmt);
2514
2515       /* We will not inline a function which uses computed goto.  The
2516          addresses of its local labels, which may be tucked into
2517          global storage, are of course not constant across
2518          instantiations, which causes unexpected behavior.  */
2519       if (TREE_CODE (t) != LABEL_DECL)
2520         {
2521           inline_forbidden_reason
2522             = G_("function %q+F can never be inlined "
2523                  "because it contains a computed goto");
2524           *handled_ops_p = true;
2525           return t;
2526         }
2527       break;
2528
2529     case GIMPLE_LABEL:
2530       t = gimple_label_label (stmt);
2531       if (DECL_NONLOCAL (t))
2532         {
2533           /* We cannot inline a function that receives a non-local goto
2534              because we cannot remap the destination label used in the
2535              function that is performing the non-local goto.  */
2536           inline_forbidden_reason
2537             = G_("function %q+F can never be inlined "
2538                  "because it receives a non-local goto");
2539           *handled_ops_p = true;
2540           return t;
2541         }
2542       break;
2543
2544     default:
2545       break;
2546     }
2547
2548   *handled_ops_p = false;
2549   return NULL_TREE;
2550 }
2551
2552
2553 static tree
2554 inline_forbidden_p_2 (tree *nodep, int *walk_subtrees,
2555                       void *fnp)
2556 {
2557   tree node = *nodep;
2558   tree fn = (tree) fnp;
2559
2560   if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2561     {
2562       inline_forbidden_reason
2563         = G_("function %q+F can never be inlined "
2564              "because it saves address of local label in a static variable");
2565       return node;
2566     }
2567
2568   if (TYPE_P (node))
2569     *walk_subtrees = 0;
2570
2571   return NULL_TREE;
2572 }
2573
2574 /* Return true if FNDECL is a function that cannot be inlined into
2575    another one.  */
2576
2577 static bool
2578 inline_forbidden_p (tree fndecl)
2579 {
2580   location_t saved_loc = input_location;
2581   struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2582   tree step;
2583   struct walk_stmt_info wi;
2584   struct pointer_set_t *visited_nodes;
2585   basic_block bb;
2586   bool forbidden_p = false;
2587
2588   visited_nodes = pointer_set_create ();
2589   memset (&wi, 0, sizeof (wi));
2590   wi.info = (void *) fndecl;
2591   wi.pset = visited_nodes;
2592
2593   FOR_EACH_BB_FN (bb, fun)
2594     {
2595       gimple ret;
2596       gimple_seq seq = bb_seq (bb);
2597       ret = walk_gimple_seq (seq, inline_forbidden_p_stmt,
2598                              inline_forbidden_p_op, &wi);
2599       forbidden_p = (ret != NULL);
2600       if (forbidden_p)
2601         goto egress;
2602     }
2603
2604   for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2605     {
2606       tree decl = TREE_VALUE (step);
2607       if (TREE_CODE (decl) == VAR_DECL
2608           && TREE_STATIC (decl)
2609           && !DECL_EXTERNAL (decl)
2610           && DECL_INITIAL (decl))
2611         {
2612           tree ret;
2613           ret = walk_tree_without_duplicates (&DECL_INITIAL (decl),
2614                                               inline_forbidden_p_2, fndecl);
2615           forbidden_p = (ret != NULL);
2616           if (forbidden_p)
2617             goto egress;
2618         }
2619     }
2620
2621 egress:
2622   pointer_set_destroy (visited_nodes);
2623   input_location = saved_loc;
2624   return forbidden_p;
2625 }
2626
2627 /* Returns nonzero if FN is a function that does not have any
2628    fundamental inline blocking properties.  */
2629
2630 static bool
2631 inlinable_function_p (tree fn)
2632 {
2633   bool inlinable = true;
2634   bool do_warning;
2635   tree always_inline;
2636
2637   /* If we've already decided this function shouldn't be inlined,
2638      there's no need to check again.  */
2639   if (DECL_UNINLINABLE (fn))
2640     return false;
2641
2642   /* We only warn for functions declared `inline' by the user.  */
2643   do_warning = (warn_inline
2644                 && DECL_DECLARED_INLINE_P (fn)
2645                 && !DECL_NO_INLINE_WARNING_P (fn)
2646                 && !DECL_IN_SYSTEM_HEADER (fn));
2647
2648   always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
2649
2650   if (flag_no_inline
2651       && always_inline == NULL)
2652     {
2653       if (do_warning)
2654         warning (OPT_Winline, "function %q+F can never be inlined because it "
2655                  "is suppressed using -fno-inline", fn);
2656       inlinable = false;
2657     }
2658
2659   /* Don't auto-inline anything that might not be bound within
2660      this unit of translation.  */
2661   else if (!DECL_DECLARED_INLINE_P (fn)
2662            && DECL_REPLACEABLE_P (fn))
2663     inlinable = false;
2664
2665   else if (!function_attribute_inlinable_p (fn))
2666     {
2667       if (do_warning)
2668         warning (OPT_Winline, "function %q+F can never be inlined because it "
2669                  "uses attributes conflicting with inlining", fn);
2670       inlinable = false;
2671     }
2672
2673   else if (inline_forbidden_p (fn))
2674     {
2675       /* See if we should warn about uninlinable functions.  Previously,
2676          some of these warnings would be issued while trying to expand
2677          the function inline, but that would cause multiple warnings
2678          about functions that would for example call alloca.  But since
2679          this a property of the function, just one warning is enough.
2680          As a bonus we can now give more details about the reason why a
2681          function is not inlinable.  */
2682       if (always_inline)
2683         sorry (inline_forbidden_reason, fn);
2684       else if (do_warning)
2685         warning (OPT_Winline, inline_forbidden_reason, fn);
2686
2687       inlinable = false;
2688     }
2689
2690   /* Squirrel away the result so that we don't have to check again.  */
2691   DECL_UNINLINABLE (fn) = !inlinable;
2692
2693   return inlinable;
2694 }
2695
2696 /* Estimate the cost of a memory move.  Use machine dependent
2697    word size and take possible memcpy call into account.  */
2698
2699 int
2700 estimate_move_cost (tree type)
2701 {
2702   HOST_WIDE_INT size;
2703
2704   size = int_size_in_bytes (type);
2705
2706   if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
2707     /* Cost of a memcpy call, 3 arguments and the call.  */
2708     return 4;
2709   else
2710     return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
2711 }
2712
2713 /* Returns cost of operation CODE, according to WEIGHTS  */
2714
2715 static int
2716 estimate_operator_cost (enum tree_code code, eni_weights *weights)
2717 {
2718   switch (code)
2719     {
2720     /* These are "free" conversions, or their presumed cost
2721        is folded into other operations.  */
2722     case RANGE_EXPR:
2723     CASE_CONVERT:
2724     case COMPLEX_EXPR:
2725     case PAREN_EXPR:
2726       return 0;
2727
2728     /* Assign cost of 1 to usual operations.
2729        ??? We may consider mapping RTL costs to this.  */
2730     case COND_EXPR:
2731     case VEC_COND_EXPR:
2732
2733     case PLUS_EXPR:
2734     case POINTER_PLUS_EXPR:
2735     case MINUS_EXPR:
2736     case MULT_EXPR:
2737
2738     case FIXED_CONVERT_EXPR:
2739     case FIX_TRUNC_EXPR:
2740
2741     case NEGATE_EXPR:
2742     case FLOAT_EXPR:
2743     case MIN_EXPR:
2744     case MAX_EXPR:
2745     case ABS_EXPR:
2746
2747     case LSHIFT_EXPR:
2748     case RSHIFT_EXPR:
2749     case LROTATE_EXPR:
2750     case RROTATE_EXPR:
2751     case VEC_LSHIFT_EXPR:
2752     case VEC_RSHIFT_EXPR:
2753
2754     case BIT_IOR_EXPR:
2755     case BIT_XOR_EXPR:
2756     case BIT_AND_EXPR:
2757     case BIT_NOT_EXPR:
2758
2759     case TRUTH_ANDIF_EXPR:
2760     case TRUTH_ORIF_EXPR:
2761     case TRUTH_AND_EXPR:
2762     case TRUTH_OR_EXPR:
2763     case TRUTH_XOR_EXPR:
2764     case TRUTH_NOT_EXPR:
2765
2766     case LT_EXPR:
2767     case LE_EXPR:
2768     case GT_EXPR:
2769     case GE_EXPR:
2770     case EQ_EXPR:
2771     case NE_EXPR:
2772     case ORDERED_EXPR:
2773     case UNORDERED_EXPR:
2774
2775     case UNLT_EXPR:
2776     case UNLE_EXPR:
2777     case UNGT_EXPR:
2778     case UNGE_EXPR:
2779     case UNEQ_EXPR:
2780     case LTGT_EXPR:
2781
2782     case CONJ_EXPR:
2783
2784     case PREDECREMENT_EXPR:
2785     case PREINCREMENT_EXPR:
2786     case POSTDECREMENT_EXPR:
2787     case POSTINCREMENT_EXPR:
2788
2789     case REALIGN_LOAD_EXPR:
2790
2791     case REDUC_MAX_EXPR:
2792     case REDUC_MIN_EXPR:
2793     case REDUC_PLUS_EXPR:
2794     case WIDEN_SUM_EXPR:
2795     case WIDEN_MULT_EXPR:
2796     case DOT_PROD_EXPR:
2797
2798     case VEC_WIDEN_MULT_HI_EXPR:
2799     case VEC_WIDEN_MULT_LO_EXPR:
2800     case VEC_UNPACK_HI_EXPR:
2801     case VEC_UNPACK_LO_EXPR:
2802     case VEC_UNPACK_FLOAT_HI_EXPR:
2803     case VEC_UNPACK_FLOAT_LO_EXPR:
2804     case VEC_PACK_TRUNC_EXPR:
2805     case VEC_PACK_SAT_EXPR:
2806     case VEC_PACK_FIX_TRUNC_EXPR:
2807     case VEC_EXTRACT_EVEN_EXPR:
2808     case VEC_EXTRACT_ODD_EXPR:
2809     case VEC_INTERLEAVE_HIGH_EXPR:
2810     case VEC_INTERLEAVE_LOW_EXPR:
2811
2812       return 1;
2813
2814     /* Few special cases of expensive operations.  This is useful
2815        to avoid inlining on functions having too many of these.  */
2816     case TRUNC_DIV_EXPR:
2817     case CEIL_DIV_EXPR:
2818     case FLOOR_DIV_EXPR:
2819     case ROUND_DIV_EXPR:
2820     case EXACT_DIV_EXPR:
2821     case TRUNC_MOD_EXPR:
2822     case CEIL_MOD_EXPR:
2823     case FLOOR_MOD_EXPR:
2824     case ROUND_MOD_EXPR:
2825     case RDIV_EXPR:
2826       return weights->div_mod_cost;
2827
2828     default:
2829       /* We expect a copy assignment with no operator.  */
2830       gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
2831       return 0;
2832     }
2833 }
2834
2835
2836 /* Estimate number of instructions that will be created by expanding
2837    the statements in the statement sequence STMTS.
2838    WEIGHTS contains weights attributed to various constructs.  */
2839
2840 static
2841 int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
2842 {
2843   int cost;
2844   gimple_stmt_iterator gsi;
2845
2846   cost = 0;
2847   for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2848     cost += estimate_num_insns (gsi_stmt (gsi), weights);
2849
2850   return cost;
2851 }
2852
2853
2854 /* Estimate number of instructions that will be created by expanding STMT.
2855    WEIGHTS contains weights attributed to various constructs.  */
2856
2857 int
2858 estimate_num_insns (gimple stmt, eni_weights *weights)
2859 {
2860   unsigned cost, i;
2861   enum gimple_code code = gimple_code (stmt);
2862   tree lhs;
2863
2864   switch (code)
2865     {
2866     case GIMPLE_ASSIGN:
2867       /* Try to estimate the cost of assignments.  We have three cases to
2868          deal with:
2869          1) Simple assignments to registers;
2870          2) Stores to things that must live in memory.  This includes
2871             "normal" stores to scalars, but also assignments of large
2872             structures, or constructors of big arrays;
2873
2874          Let us look at the first two cases, assuming we have "a = b + C":
2875          <GIMPLE_ASSIGN <var_decl "a">
2876                 <plus_expr <var_decl "b"> <constant C>>
2877          If "a" is a GIMPLE register, the assignment to it is free on almost
2878          any target, because "a" usually ends up in a real register.  Hence
2879          the only cost of this expression comes from the PLUS_EXPR, and we
2880          can ignore the GIMPLE_ASSIGN.
2881          If "a" is not a GIMPLE register, the assignment to "a" will most
2882          likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
2883          of moving something into "a", which we compute using the function
2884          estimate_move_cost.  */
2885       lhs = gimple_assign_lhs (stmt);
2886       if (is_gimple_reg (lhs))
2887         cost = 0;
2888       else
2889         cost = estimate_move_cost (TREE_TYPE (lhs));
2890
2891       cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights);
2892       break;
2893
2894     case GIMPLE_COND:
2895       cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights);
2896       break;
2897
2898     case GIMPLE_SWITCH:
2899       /* Take into account cost of the switch + guess 2 conditional jumps for
2900          each case label.  
2901
2902          TODO: once the switch expansion logic is sufficiently separated, we can
2903          do better job on estimating cost of the switch.  */
2904       cost = gimple_switch_num_labels (stmt) * 2;
2905       break;
2906
2907     case GIMPLE_CALL:
2908       {
2909         tree decl = gimple_call_fndecl (stmt);
2910         tree addr = gimple_call_fn (stmt);
2911         tree funtype = TREE_TYPE (addr);
2912
2913         if (POINTER_TYPE_P (funtype))
2914           funtype = TREE_TYPE (funtype);
2915
2916         if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
2917           cost = weights->target_builtin_call_cost;
2918         else
2919           cost = weights->call_cost;
2920         
2921         if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2922           switch (DECL_FUNCTION_CODE (decl))
2923             {
2924             case BUILT_IN_CONSTANT_P:
2925               return 0;
2926             case BUILT_IN_EXPECT:
2927               cost = 0;
2928               break;
2929
2930             /* Prefetch instruction is not expensive.  */
2931             case BUILT_IN_PREFETCH:
2932               cost = weights->target_builtin_call_cost;
2933               break;
2934
2935             default:
2936               break;
2937             }
2938
2939         if (decl)
2940           funtype = TREE_TYPE (decl);
2941
2942         /* Our cost must be kept in sync with
2943            cgraph_estimate_size_after_inlining that does use function
2944            declaration to figure out the arguments.  */
2945         if (decl && DECL_ARGUMENTS (decl))
2946           {
2947             tree arg;
2948             for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2949               cost += estimate_move_cost (TREE_TYPE (arg));
2950           }
2951         else if (funtype && prototype_p (funtype))
2952           {
2953             tree t;
2954             for (t = TYPE_ARG_TYPES (funtype); t; t = TREE_CHAIN (t))
2955               cost += estimate_move_cost (TREE_VALUE (t));
2956           }
2957         else
2958           {
2959             for (i = 0; i < gimple_call_num_args (stmt); i++)
2960               {
2961                 tree arg = gimple_call_arg (stmt, i);
2962                 cost += estimate_move_cost (TREE_TYPE (arg));
2963               }
2964           }
2965
2966         break;
2967       }
2968
2969     case GIMPLE_GOTO:
2970     case GIMPLE_LABEL:
2971     case GIMPLE_NOP:
2972     case GIMPLE_PHI:
2973     case GIMPLE_RETURN:
2974     case GIMPLE_CHANGE_DYNAMIC_TYPE:
2975     case GIMPLE_PREDICT:
2976       return 0;
2977
2978     case GIMPLE_ASM:
2979     case GIMPLE_RESX:
2980       return 1;
2981
2982     case GIMPLE_BIND:
2983       return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
2984
2985     case GIMPLE_EH_FILTER:
2986       return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
2987
2988     case GIMPLE_CATCH:
2989       return estimate_num_insns_seq (gimple_catch_handler (stmt), weights);
2990
2991     case GIMPLE_TRY:
2992       return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
2993               + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
2994
2995     /* OpenMP directives are generally very expensive.  */
2996
2997     case GIMPLE_OMP_RETURN:
2998     case GIMPLE_OMP_SECTIONS_SWITCH:
2999     case GIMPLE_OMP_ATOMIC_STORE:
3000     case GIMPLE_OMP_CONTINUE:
3001       /* ...except these, which are cheap.  */
3002       return 0;
3003
3004     case GIMPLE_OMP_ATOMIC_LOAD:
3005       return weights->omp_cost;
3006
3007     case GIMPLE_OMP_FOR:
3008       return (weights->omp_cost
3009               + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
3010               + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
3011
3012     case GIMPLE_OMP_PARALLEL:
3013     case GIMPLE_OMP_TASK:
3014     case GIMPLE_OMP_CRITICAL:
3015     case GIMPLE_OMP_MASTER:
3016     case GIMPLE_OMP_ORDERED:
3017     case GIMPLE_OMP_SECTION:
3018     case GIMPLE_OMP_SECTIONS:
3019     case GIMPLE_OMP_SINGLE:
3020       return (weights->omp_cost
3021               + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
3022
3023     default:
3024       gcc_unreachable ();
3025     }
3026
3027   return cost;
3028 }
3029
3030 /* Estimate number of instructions that will be created by expanding
3031    function FNDECL.  WEIGHTS contains weights attributed to various
3032    constructs.  */
3033
3034 int
3035 estimate_num_insns_fn (tree fndecl, eni_weights *weights)
3036 {
3037   struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
3038   gimple_stmt_iterator bsi;
3039   basic_block bb;
3040   int n = 0;
3041
3042   gcc_assert (my_function && my_function->cfg);
3043   FOR_EACH_BB_FN (bb, my_function)
3044     {
3045       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
3046         n += estimate_num_insns (gsi_stmt (bsi), weights);
3047     }
3048
3049   return n;
3050 }
3051
3052
3053 /* Initializes weights used by estimate_num_insns.  */
3054
3055 void
3056 init_inline_once (void)
3057 {
3058   eni_inlining_weights.call_cost = PARAM_VALUE (PARAM_INLINE_CALL_COST);
3059   eni_inlining_weights.target_builtin_call_cost = 1;
3060   eni_inlining_weights.div_mod_cost = 10;
3061   eni_inlining_weights.omp_cost = 40;
3062
3063   eni_size_weights.call_cost = 1;
3064   eni_size_weights.target_builtin_call_cost = 1;
3065   eni_size_weights.div_mod_cost = 1;
3066   eni_size_weights.omp_cost = 40;
3067
3068   /* Estimating time for call is difficult, since we have no idea what the
3069      called function does.  In the current uses of eni_time_weights,
3070      underestimating the cost does less harm than overestimating it, so
3071      we choose a rather small value here.  */
3072   eni_time_weights.call_cost = 10;
3073   eni_time_weights.target_builtin_call_cost = 10;
3074   eni_time_weights.div_mod_cost = 10;
3075   eni_time_weights.omp_cost = 40;
3076 }
3077
3078 /* Estimate the number of instructions in a gimple_seq. */
3079
3080 int
3081 count_insns_seq (gimple_seq seq, eni_weights *weights)
3082 {
3083   gimple_stmt_iterator gsi;
3084   int n = 0;
3085   for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
3086     n += estimate_num_insns (gsi_stmt (gsi), weights);
3087
3088   return n;
3089 }
3090
3091
3092 /* Install new lexical TREE_BLOCK underneath 'current_block'.  */
3093
3094 static void
3095 prepend_lexical_block (tree current_block, tree new_block)
3096 {
3097   BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
3098   BLOCK_SUBBLOCKS (current_block) = new_block;
3099   BLOCK_SUPERCONTEXT (new_block) = current_block;
3100 }
3101
3102 /* Fetch callee declaration from the call graph edge going from NODE and
3103    associated with STMR call statement.  Return NULL_TREE if not found.  */
3104 static tree
3105 get_indirect_callee_fndecl (struct cgraph_node *node, gimple stmt)
3106 {
3107   struct cgraph_edge *cs;
3108
3109   cs = cgraph_edge (node, stmt);
3110   if (cs)
3111     return cs->callee->decl;
3112
3113   return NULL_TREE;
3114 }
3115
3116 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion.  */
3117
3118 static bool
3119 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3120 {
3121   tree retvar, use_retvar;
3122   tree fn;
3123   struct pointer_map_t *st;
3124   tree return_slot;
3125   tree modify_dest;
3126   location_t saved_location;
3127   struct cgraph_edge *cg_edge;
3128   const char *reason;
3129   basic_block return_block;
3130   edge e;
3131   gimple_stmt_iterator gsi, stmt_gsi;
3132   bool successfully_inlined = FALSE;
3133   bool purge_dead_abnormal_edges;
3134   tree t_step;
3135   tree var;
3136
3137   /* Set input_location here so we get the right instantiation context
3138      if we call instantiate_decl from inlinable_function_p.  */
3139   saved_location = input_location;
3140   if (gimple_has_location (stmt))
3141     input_location = gimple_location (stmt);
3142
3143   /* From here on, we're only interested in CALL_EXPRs.  */
3144   if (gimple_code (stmt) != GIMPLE_CALL)
3145     goto egress;
3146
3147   /* First, see if we can figure out what function is being called.
3148      If we cannot, then there is no hope of inlining the function.  */
3149   fn = gimple_call_fndecl (stmt);
3150   if (!fn)
3151     {
3152       fn = get_indirect_callee_fndecl (id->dst_node, stmt);
3153       if (!fn)
3154         goto egress;
3155     }
3156
3157   /* Turn forward declarations into real ones.  */
3158   fn = cgraph_node (fn)->decl;
3159
3160   /* If FN is a declaration of a function in a nested scope that was
3161      globally declared inline, we don't set its DECL_INITIAL.
3162      However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
3163      C++ front-end uses it for cdtors to refer to their internal
3164      declarations, that are not real functions.  Fortunately those
3165      don't have trees to be saved, so we can tell by checking their
3166      gimple_body.  */
3167   if (!DECL_INITIAL (fn)
3168       && DECL_ABSTRACT_ORIGIN (fn)
3169       && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
3170     fn = DECL_ABSTRACT_ORIGIN (fn);
3171
3172   /* Objective C and fortran still calls tree_rest_of_compilation directly.
3173      Kill this check once this is fixed.  */
3174   if (!id->dst_node->analyzed)
3175     goto egress;
3176
3177   cg_edge = cgraph_edge (id->dst_node, stmt);
3178
3179   /* Constant propagation on argument done during previous inlining
3180      may create new direct call.  Produce an edge for it.  */
3181   if (!cg_edge)
3182     {
3183       struct cgraph_node *dest = cgraph_node (fn);
3184
3185       /* We have missing edge in the callgraph.  This can happen in one case
3186          where previous inlining turned indirect call into direct call by
3187          constant propagating arguments.  In all other cases we hit a bug
3188          (incorrect node sharing is most common reason for missing edges.  */
3189       gcc_assert (dest->needed);
3190       cgraph_create_edge (id->dst_node, dest, stmt,
3191                           bb->count, CGRAPH_FREQ_BASE,
3192                           bb->loop_depth)->inline_failed
3193         = N_("originally indirect function call not considered for inlining");
3194       if (dump_file)
3195         {
3196            fprintf (dump_file, "Created new direct edge to %s",
3197                     cgraph_node_name (dest));
3198         }
3199       goto egress;
3200     }
3201
3202   /* Don't try to inline functions that are not well-suited to
3203      inlining.  */
3204   if (!cgraph_inline_p (cg_edge, &reason))
3205     {
3206       /* If this call was originally indirect, we do not want to emit any
3207          inlining related warnings or sorry messages because there are no
3208          guarantees regarding those.  */
3209       if (cg_edge->indirect_call)
3210         goto egress;
3211
3212       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3213           /* Avoid warnings during early inline pass. */
3214           && cgraph_global_info_ready)
3215         {
3216           sorry ("inlining failed in call to %q+F: %s", fn, reason);
3217           sorry ("called from here");
3218         }
3219       else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3220                && !DECL_IN_SYSTEM_HEADER (fn)
3221                && strlen (reason)
3222                && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3223                /* Avoid warnings during early inline pass. */
3224                && cgraph_global_info_ready)
3225         {
3226           warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3227                    fn, reason);
3228           warning (OPT_Winline, "called from here");
3229         }
3230       goto egress;
3231     }
3232   fn = cg_edge->callee->decl;
3233
3234 #ifdef ENABLE_CHECKING
3235   if (cg_edge->callee->decl != id->dst_node->decl)
3236     verify_cgraph_node (cg_edge->callee);
3237 #endif
3238
3239   /* We will be inlining this callee.  */
3240   id->eh_region = lookup_stmt_eh_region (stmt);
3241
3242   /* Split the block holding the GIMPLE_CALL.  */
3243   e = split_block (bb, stmt);
3244   bb = e->src;
3245   return_block = e->dest;
3246   remove_edge (e);
3247
3248   /* split_block splits after the statement; work around this by
3249      moving the call into the second block manually.  Not pretty,
3250      but seems easier than doing the CFG manipulation by hand
3251      when the GIMPLE_CALL is in the last statement of BB.  */
3252   stmt_gsi = gsi_last_bb (bb);
3253   gsi_remove (&stmt_gsi, false);
3254
3255   /* If the GIMPLE_CALL was in the last statement of BB, it may have
3256      been the source of abnormal edges.  In this case, schedule
3257      the removal of dead abnormal edges.  */
3258   gsi = gsi_start_bb (return_block);
3259   if (gsi_end_p (gsi))
3260     {
3261       gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
3262       purge_dead_abnormal_edges = true;
3263     }
3264   else
3265     {
3266       gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
3267       purge_dead_abnormal_edges = false;
3268     }
3269
3270   stmt_gsi = gsi_start_bb (return_block);
3271
3272   /* Build a block containing code to initialize the arguments, the
3273      actual inline expansion of the body, and a label for the return
3274      statements within the function to jump to.  The type of the
3275      statement expression is the return type of the function call.  */
3276   id->block = make_node (BLOCK);
3277   BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
3278   BLOCK_SOURCE_LOCATION (id->block) = input_location;
3279   prepend_lexical_block (gimple_block (stmt), id->block);
3280
3281   /* Local declarations will be replaced by their equivalents in this
3282      map.  */
3283   st = id->decl_map;
3284   id->decl_map = pointer_map_create ();
3285
3286   /* Record the function we are about to inline.  */
3287   id->src_fn = fn;
3288   id->src_node = cg_edge->callee;
3289   id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3290   id->gimple_call = stmt;
3291
3292   gcc_assert (!id->src_cfun->after_inlining);
3293
3294   id->entry_bb = bb;
3295   if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
3296     {
3297       gimple_stmt_iterator si = gsi_last_bb (bb);
3298       gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
3299                                                    NOT_TAKEN),
3300                         GSI_NEW_STMT);
3301     }
3302   initialize_inlined_parameters (id, stmt, fn, bb);
3303
3304   if (DECL_INITIAL (fn))
3305     prepend_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
3306
3307   /* Return statements in the function body will be replaced by jumps
3308      to the RET_LABEL.  */
3309   gcc_assert (DECL_INITIAL (fn));
3310   gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
3311
3312   /* Find the LHS to which the result of this call is assigned.  */
3313   return_slot = NULL;
3314   if (gimple_call_lhs (stmt))
3315     {
3316       modify_dest = gimple_call_lhs (stmt);
3317
3318       /* The function which we are inlining might not return a value,
3319          in which case we should issue a warning that the function
3320          does not return a value.  In that case the optimizers will
3321          see that the variable to which the value is assigned was not
3322          initialized.  We do not want to issue a warning about that
3323          uninitialized variable.  */
3324       if (DECL_P (modify_dest))
3325         TREE_NO_WARNING (modify_dest) = 1;
3326
3327       if (gimple_call_return_slot_opt_p (stmt))
3328         {
3329           return_slot = modify_dest;
3330           modify_dest = NULL;
3331         }
3332     }
3333   else
3334     modify_dest = NULL;
3335
3336   /* If we are inlining a call to the C++ operator new, we don't want
3337      to use type based alias analysis on the return value.  Otherwise
3338      we may get confused if the compiler sees that the inlined new
3339      function returns a pointer which was just deleted.  See bug
3340      33407.  */
3341   if (DECL_IS_OPERATOR_NEW (fn))
3342     {
3343       return_slot = NULL;
3344       modify_dest = NULL;
3345     }
3346
3347   /* Declare the return variable for the function.  */
3348   retvar = declare_return_variable (id, return_slot, modify_dest, &use_retvar);
3349
3350   if (DECL_IS_OPERATOR_NEW (fn))
3351     {
3352       gcc_assert (TREE_CODE (retvar) == VAR_DECL
3353                   && POINTER_TYPE_P (TREE_TYPE (retvar)));
3354       DECL_NO_TBAA_P (retvar) = 1;
3355     }
3356
3357   /* Add local vars in this inlined callee to caller.  */
3358   t_step = id->src_cfun->local_decls;
3359   for (; t_step; t_step = TREE_CHAIN (t_step))
3360     {
3361       var = TREE_VALUE (t_step);
3362       if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3363         {
3364           if (var_ann (var) && referenced_var_check_and_insert (var))
3365             cfun->local_decls = tree_cons (NULL_TREE, var,
3366                                            cfun->local_decls);
3367         }
3368       else if (!can_be_nonlocal (var, id))
3369         cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3370                                        cfun->local_decls);
3371     }
3372
3373   /* This is it.  Duplicate the callee body.  Assume callee is
3374      pre-gimplified.  Note that we must not alter the caller
3375      function in any way before this point, as this CALL_EXPR may be
3376      a self-referential call; if we're calling ourselves, we need to
3377      duplicate our body before altering anything.  */
3378   copy_body (id, bb->count, bb->frequency, bb, return_block);
3379
3380   /* Clean up.  */
3381   pointer_map_destroy (id->decl_map);
3382   id->decl_map = st;
3383
3384   /* If the inlined function returns a result that we care about,
3385      substitute the GIMPLE_CALL with an assignment of the return
3386      variable to the LHS of the call.  That is, if STMT was
3387      'a = foo (...)', substitute the call with 'a = USE_RETVAR'.  */
3388   if (use_retvar && gimple_call_lhs (stmt))
3389     {
3390       gimple old_stmt = stmt;
3391       stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3392       gsi_replace (&stmt_gsi, stmt, false);
3393       if (gimple_in_ssa_p (cfun))
3394         {
3395           update_stmt (stmt);
3396           mark_symbols_for_renaming (stmt);
3397         }
3398       maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3399     }
3400   else
3401     {
3402       /* Handle the case of inlining a function with no return
3403          statement, which causes the return value to become undefined.  */
3404       if (gimple_call_lhs (stmt)
3405           && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
3406         {
3407           tree name = gimple_call_lhs (stmt);
3408           tree var = SSA_NAME_VAR (name);
3409           tree def = gimple_default_def (cfun, var);
3410
3411           if (def)
3412             {
3413               /* If the variable is used undefined, make this name
3414                  undefined via a move.  */
3415               stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3416               gsi_replace (&stmt_gsi, stmt, true);
3417               update_stmt (stmt);
3418             }
3419           else
3420             {
3421               /* Otherwise make this variable undefined.  */
3422               gsi_remove (&stmt_gsi, true);
3423               set_default_def (var, name);
3424               SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
3425             }
3426         }
3427       else
3428         gsi_remove (&stmt_gsi, true);
3429     }
3430
3431   if (purge_dead_abnormal_edges)
3432     gimple_purge_dead_abnormal_call_edges (return_block);
3433
3434   /* If the value of the new expression is ignored, that's OK.  We
3435      don't warn about this for CALL_EXPRs, so we shouldn't warn about
3436      the equivalent inlined version either.  */
3437   if (is_gimple_assign (stmt))
3438     {
3439       gcc_assert (gimple_assign_single_p (stmt)
3440                   || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
3441       TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
3442     }
3443
3444   /* Output the inlining info for this abstract function, since it has been
3445      inlined.  If we don't do this now, we can lose the information about the
3446      variables in the function when the blocks get blown away as soon as we
3447      remove the cgraph node.  */
3448   (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
3449
3450   /* Update callgraph if needed.  */
3451   cgraph_remove_node (cg_edge->callee);
3452
3453   id->block = NULL_TREE;
3454   successfully_inlined = TRUE;
3455
3456  egress:
3457   input_location = saved_location;
3458   return successfully_inlined;
3459 }
3460
3461 /* Expand call statements reachable from STMT_P.
3462    We can only have CALL_EXPRs as the "toplevel" tree code or nested
3463    in a MODIFY_EXPR.  See tree-gimple.c:get_call_expr_in().  We can
3464    unfortunately not use that function here because we need a pointer
3465    to the CALL_EXPR, not the tree itself.  */
3466
3467 static bool
3468 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
3469 {
3470   gimple_stmt_iterator gsi;
3471
3472   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3473     {
3474       gimple stmt = gsi_stmt (gsi);
3475
3476       if (is_gimple_call (stmt)
3477           && expand_call_inline (bb, stmt, id))
3478         return true;
3479     }
3480
3481   return false;
3482 }
3483
3484
3485 /* Walk all basic blocks created after FIRST and try to fold every statement
3486    in the STATEMENTS pointer set.  */
3487
3488 static void
3489 fold_marked_statements (int first, struct pointer_set_t *statements)
3490 {
3491   for (; first < n_basic_blocks; first++)
3492     if (BASIC_BLOCK (first))
3493       {
3494         gimple_stmt_iterator gsi;
3495
3496         for (gsi = gsi_start_bb (BASIC_BLOCK (first));
3497              !gsi_end_p (gsi);
3498              gsi_next (&gsi))
3499           if (pointer_set_contains (statements, gsi_stmt (gsi)))
3500             {
3501               gimple old_stmt = gsi_stmt (gsi);
3502
3503               if (fold_stmt (&gsi))
3504                 {
3505                   /* Re-read the statement from GSI as fold_stmt() may
3506                      have changed it.  */
3507                   gimple new_stmt = gsi_stmt (gsi);
3508                   update_stmt (new_stmt);
3509
3510                   if (is_gimple_call (old_stmt))
3511                     cgraph_update_edges_for_call_stmt (old_stmt, new_stmt);
3512
3513                   if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3514                     gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3515                 }
3516             }
3517       }
3518 }
3519
3520 /* Return true if BB has at least one abnormal outgoing edge.  */
3521
3522 static inline bool
3523 has_abnormal_outgoing_edge_p (basic_block bb)
3524 {
3525   edge e;
3526   edge_iterator ei;
3527
3528   FOR_EACH_EDGE (e, ei, bb->succs)
3529     if (e->flags & EDGE_ABNORMAL)
3530       return true;
3531
3532   return false;
3533 }
3534
3535 /* Expand calls to inline functions in the body of FN.  */
3536
3537 unsigned int
3538 optimize_inline_calls (tree fn)
3539 {
3540   copy_body_data id;
3541   tree prev_fn;
3542   basic_block bb;
3543   int last = n_basic_blocks;
3544   struct gimplify_ctx gctx;
3545
3546   /* There is no point in performing inlining if errors have already
3547      occurred -- and we might crash if we try to inline invalid
3548      code.  */
3549   if (errorcount || sorrycount)
3550     return 0;
3551
3552   /* Clear out ID.  */
3553   memset (&id, 0, sizeof (id));
3554
3555   id.src_node = id.dst_node = cgraph_node (fn);
3556   id.dst_fn = fn;
3557   /* Or any functions that aren't finished yet.  */
3558   prev_fn = NULL_TREE;
3559   if (current_function_decl)
3560     {
3561       id.dst_fn = current_function_decl;
3562       prev_fn = current_function_decl;
3563     }
3564
3565   id.copy_decl = copy_decl_maybe_to_var;
3566   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3567   id.transform_new_cfg = false;
3568   id.transform_return_to_modify = true;
3569   id.transform_lang_insert_block = NULL;
3570   id.statements_to_fold = pointer_set_create ();
3571
3572   push_gimplify_context (&gctx);
3573
3574   /* We make no attempts to keep dominance info up-to-date.  */
3575   free_dominance_info (CDI_DOMINATORS);
3576   free_dominance_info (CDI_POST_DOMINATORS);
3577
3578   /* Register specific gimple functions.  */
3579   gimple_register_cfg_hooks ();
3580
3581   /* Reach the trees by walking over the CFG, and note the
3582      enclosing basic-blocks in the call edges.  */
3583   /* We walk the blocks going forward, because inlined function bodies
3584      will split id->current_basic_block, and the new blocks will
3585      follow it; we'll trudge through them, processing their CALL_EXPRs
3586      along the way.  */
3587   FOR_EACH_BB (bb)
3588     gimple_expand_calls_inline (bb, &id);
3589
3590   pop_gimplify_context (NULL);
3591
3592 #ifdef ENABLE_CHECKING
3593     {
3594       struct cgraph_edge *e;
3595
3596       verify_cgraph_node (id.dst_node);
3597
3598       /* Double check that we inlined everything we are supposed to inline.  */
3599       for (e = id.dst_node->callees; e; e = e->next_callee)
3600         gcc_assert (e->inline_failed);
3601     }
3602 #endif
3603   
3604   /* Fold the statements before compacting/renumbering the basic blocks.  */
3605   fold_marked_statements (last, id.statements_to_fold);
3606   pointer_set_destroy (id.statements_to_fold);
3607   
3608   /* Renumber the (code) basic_blocks consecutively.  */
3609   compact_blocks ();
3610   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
3611   number_blocks (fn);
3612
3613   /* We are not going to maintain the cgraph edges up to date.
3614      Kill it so it won't confuse us.  */
3615   cgraph_node_remove_callees (id.dst_node);
3616
3617   fold_cond_expr_cond ();
3618
3619   /* It would be nice to check SSA/CFG/statement consistency here, but it is
3620      not possible yet - the IPA passes might make various functions to not
3621      throw and they don't care to proactively update local EH info.  This is
3622      done later in fixup_cfg pass that also execute the verification.  */
3623   return (TODO_update_ssa
3624           | TODO_cleanup_cfg
3625           | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
3626           | (profile_status != PROFILE_ABSENT ? TODO_rebuild_frequencies : 0));
3627 }
3628
3629 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
3630
3631 tree
3632 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3633 {
3634   enum tree_code code = TREE_CODE (*tp);
3635   enum tree_code_class cl = TREE_CODE_CLASS (code);
3636
3637   /* We make copies of most nodes.  */
3638   if (IS_EXPR_CODE_CLASS (cl)
3639       || code == TREE_LIST
3640       || code == TREE_VEC
3641       || code == TYPE_DECL
3642       || code == OMP_CLAUSE)
3643     {
3644       /* Because the chain gets clobbered when we make a copy, we save it
3645          here.  */
3646       tree chain = NULL_TREE, new_tree;
3647
3648       chain = TREE_CHAIN (*tp);
3649
3650       /* Copy the node.  */
3651       new_tree = copy_node (*tp);
3652
3653       /* Propagate mudflap marked-ness.  */
3654       if (flag_mudflap && mf_marked_p (*tp))
3655         mf_mark (new_tree);
3656
3657       *tp = new_tree;
3658
3659       /* Now, restore the chain, if appropriate.  That will cause
3660          walk_tree to walk into the chain as well.  */
3661       if (code == PARM_DECL
3662           || code == TREE_LIST
3663           || code == OMP_CLAUSE)
3664         TREE_CHAIN (*tp) = chain;
3665
3666       /* For now, we don't update BLOCKs when we make copies.  So, we
3667          have to nullify all BIND_EXPRs.  */
3668       if (TREE_CODE (*tp) == BIND_EXPR)
3669         BIND_EXPR_BLOCK (*tp) = NULL_TREE;
3670     }
3671   else if (code == CONSTRUCTOR)
3672     {
3673       /* CONSTRUCTOR nodes need special handling because
3674          we need to duplicate the vector of elements.  */
3675       tree new_tree;
3676
3677       new_tree = copy_node (*tp);
3678
3679       /* Propagate mudflap marked-ness.  */
3680       if (flag_mudflap && mf_marked_p (*tp))
3681         mf_mark (new_tree);
3682
3683       CONSTRUCTOR_ELTS (new_tree) = VEC_copy (constructor_elt, gc,
3684                                          CONSTRUCTOR_ELTS (*tp));
3685       *tp = new_tree;
3686     }
3687   else if (TREE_CODE_CLASS (code) == tcc_type)
3688     *walk_subtrees = 0;
3689   else if (TREE_CODE_CLASS (code) == tcc_declaration)
3690     *walk_subtrees = 0;
3691   else if (TREE_CODE_CLASS (code) == tcc_constant)
3692     *walk_subtrees = 0;
3693   else
3694     gcc_assert (code != STATEMENT_LIST);
3695   return NULL_TREE;
3696 }
3697
3698 /* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
3699    information indicating to what new SAVE_EXPR this one should be mapped,
3700    use that one.  Otherwise, create a new node and enter it in ST.  FN is
3701    the function into which the copy will be placed.  */
3702
3703 static void
3704 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
3705 {
3706   struct pointer_map_t *st = (struct pointer_map_t *) st_;
3707   tree *n;
3708   tree t;
3709
3710   /* See if we already encountered this SAVE_EXPR.  */
3711   n = (tree *) pointer_map_contains (st, *tp);
3712
3713   /* If we didn't already remap this SAVE_EXPR, do so now.  */
3714   if (!n)
3715     {
3716       t = copy_node (*tp);
3717
3718       /* Remember this SAVE_EXPR.  */
3719       *pointer_map_insert (st, *tp) = t;
3720       /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
3721       *pointer_map_insert (st, t) = t;
3722     }
3723   else
3724     {
3725       /* We've already walked into this SAVE_EXPR; don't do it again.  */
3726       *walk_subtrees = 0;
3727       t = *n;
3728     }
3729
3730   /* Replace this SAVE_EXPR with the copy.  */
3731   *tp = t;
3732 }
3733
3734 /* Called via walk_tree.  If *TP points to a DECL_STMT for a local label,
3735    copies the declaration and enters it in the splay_tree in DATA (which is
3736    really an `copy_body_data *').  */
3737
3738 static tree
3739 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
3740                         void *data)
3741 {
3742   copy_body_data *id = (copy_body_data *) data;
3743
3744   /* Don't walk into types.  */
3745   if (TYPE_P (*tp))
3746     *walk_subtrees = 0;
3747
3748   else if (TREE_CODE (*tp) == LABEL_EXPR)
3749     {
3750       tree decl = TREE_OPERAND (*tp, 0);
3751
3752       /* Copy the decl and remember the copy.  */
3753       insert_decl_map (id, decl, id->copy_decl (decl, id));
3754     }
3755
3756   return NULL_TREE;
3757 }
3758
3759 /* Perform any modifications to EXPR required when it is unsaved.  Does
3760    not recurse into EXPR's subtrees.  */
3761
3762 static void
3763 unsave_expr_1 (tree expr)
3764 {
3765   switch (TREE_CODE (expr))
3766     {
3767     case TARGET_EXPR:
3768       /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3769          It's OK for this to happen if it was part of a subtree that
3770          isn't immediately expanded, such as operand 2 of another
3771          TARGET_EXPR.  */
3772       if (TREE_OPERAND (expr, 1))
3773         break;
3774
3775       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3776       TREE_OPERAND (expr, 3) = NULL_TREE;
3777       break;
3778
3779     default:
3780       break;
3781     }
3782 }
3783
3784 /* Called via walk_tree when an expression is unsaved.  Using the
3785    splay_tree pointed to by ST (which is really a `splay_tree'),
3786    remaps all local declarations to appropriate replacements.  */
3787
3788 static tree
3789 unsave_r (tree *tp, int *walk_subtrees, void *data)
3790 {
3791   copy_body_data *id = (copy_body_data *) data;
3792   struct pointer_map_t *st = id->decl_map;
3793   tree *n;
3794
3795   /* Only a local declaration (variable or label).  */
3796   if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
3797       || TREE_CODE (*tp) == LABEL_DECL)
3798     {
3799       /* Lookup the declaration.  */
3800       n = (tree *) pointer_map_contains (st, *tp);
3801
3802       /* If it's there, remap it.  */
3803       if (n)
3804         *tp = *n;
3805     }
3806
3807   else if (TREE_CODE (*tp) == STATEMENT_LIST)
3808     gcc_unreachable ();
3809   else if (TREE_CODE (*tp) == BIND_EXPR)
3810     copy_bind_expr (tp, walk_subtrees, id);
3811   else if (TREE_CODE (*tp) == SAVE_EXPR)
3812     remap_save_expr (tp, st, walk_subtrees);
3813   else
3814     {
3815       copy_tree_r (tp, walk_subtrees, NULL);
3816
3817       /* Do whatever unsaving is required.  */
3818       unsave_expr_1 (*tp);
3819     }
3820
3821   /* Keep iterating.  */
3822   return NULL_TREE;
3823 }
3824
3825 /* Copies everything in EXPR and replaces variables, labels
3826    and SAVE_EXPRs local to EXPR.  */
3827
3828 tree
3829 unsave_expr_now (tree expr)
3830 {
3831   copy_body_data id;
3832
3833   /* There's nothing to do for NULL_TREE.  */
3834   if (expr == 0)
3835     return expr;
3836
3837   /* Set up ID.  */
3838   memset (&id, 0, sizeof (id));
3839   id.src_fn = current_function_decl;
3840   id.dst_fn = current_function_decl;
3841   id.decl_map = pointer_map_create ();
3842
3843   id.copy_decl = copy_decl_no_change;
3844   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3845   id.transform_new_cfg = false;
3846   id.transform_return_to_modify = false;
3847   id.transform_lang_insert_block = NULL;
3848
3849   /* Walk the tree once to find local labels.  */
3850   walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
3851
3852   /* Walk the tree again, copying, remapping, and unsaving.  */
3853   walk_tree (&expr, unsave_r, &id, NULL);
3854
3855   /* Clean up.  */
3856   pointer_map_destroy (id.decl_map);
3857
3858   return expr;
3859 }
3860
3861 /* Called via walk_gimple_seq.  If *GSIP points to a GIMPLE_LABEL for a local
3862    label, copies the declaration and enters it in the splay_tree in DATA (which
3863    is really a 'copy_body_data *'.  */
3864
3865 static tree
3866 mark_local_labels_stmt (gimple_stmt_iterator *gsip,
3867                         bool *handled_ops_p ATTRIBUTE_UNUSED,
3868                         struct walk_stmt_info *wi)
3869 {
3870   copy_body_data *id = (copy_body_data *) wi->info;
3871   gimple stmt = gsi_stmt (*gsip);
3872
3873   if (gimple_code (stmt) == GIMPLE_LABEL)
3874     {
3875       tree decl = gimple_label_label (stmt);
3876
3877       /* Copy the decl and remember the copy.  */
3878       insert_decl_map (id, decl, id->copy_decl (decl, id));
3879     }
3880
3881   return NULL_TREE;
3882 }
3883
3884
3885 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3886    Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3887    remaps all local declarations to appropriate replacements in gimple
3888    operands. */
3889
3890 static tree
3891 replace_locals_op (tree *tp, int *walk_subtrees, void *data)
3892 {
3893   struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
3894   copy_body_data *id = (copy_body_data *) wi->info;
3895   struct pointer_map_t *st = id->decl_map;
3896   tree *n;
3897   tree expr = *tp;
3898
3899   /* Only a local declaration (variable or label).  */
3900   if ((TREE_CODE (expr) == VAR_DECL
3901        && !TREE_STATIC (expr))
3902       || TREE_CODE (expr) == LABEL_DECL)
3903     {
3904       /* Lookup the declaration.  */
3905       n = (tree *) pointer_map_contains (st, expr);
3906
3907       /* If it's there, remap it.  */
3908       if (n)
3909         *tp = *n;
3910       *walk_subtrees = 0;
3911     }
3912   else if (TREE_CODE (expr) == STATEMENT_LIST
3913            || TREE_CODE (expr) == BIND_EXPR
3914            || TREE_CODE (expr) == SAVE_EXPR)
3915     gcc_unreachable ();
3916   else if (TREE_CODE (expr) == TARGET_EXPR)
3917     {
3918       /* Don't mess with a TARGET_EXPR that hasn't been expanded.
3919          It's OK for this to happen if it was part of a subtree that
3920          isn't immediately expanded, such as operand 2 of another
3921          TARGET_EXPR.  */
3922       if (!TREE_OPERAND (expr, 1))
3923         {
3924           TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
3925           TREE_OPERAND (expr, 3) = NULL_TREE;
3926         }
3927     }
3928
3929   /* Keep iterating.  */
3930   return NULL_TREE;
3931 }
3932
3933
3934 /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
3935    Using the splay_tree pointed to by ST (which is really a `splay_tree'),
3936    remaps all local declarations to appropriate replacements in gimple
3937    statements. */
3938
3939 static tree
3940 replace_locals_stmt (gimple_stmt_iterator *gsip,
3941                      bool *handled_ops_p ATTRIBUTE_UNUSED,
3942                      struct walk_stmt_info *wi)
3943 {
3944   copy_body_data *id = (copy_body_data *) wi->info;
3945   gimple stmt = gsi_stmt (*gsip);
3946
3947   if (gimple_code (stmt) == GIMPLE_BIND)
3948     {
3949       tree block = gimple_bind_block (stmt);
3950
3951       if (block)
3952         {
3953           remap_block (&block, id);
3954           gimple_bind_set_block (stmt, block);
3955         }
3956
3957       /* This will remap a lot of the same decls again, but this should be
3958          harmless.  */
3959       if (gimple_bind_vars (stmt))
3960         gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt), NULL, id));
3961     }
3962
3963   /* Keep iterating.  */
3964   return NULL_TREE;
3965 }
3966
3967
3968 /* Copies everything in SEQ and replaces variables and labels local to
3969    current_function_decl.  */
3970
3971 gimple_seq
3972 copy_gimple_seq_and_replace_locals (gimple_seq seq)
3973 {
3974   copy_body_data id;
3975   struct walk_stmt_info wi;
3976   struct pointer_set_t *visited;
3977   gimple_seq copy;
3978
3979   /* There's nothing to do for NULL_TREE.  */
3980   if (seq == NULL)
3981     return seq;
3982
3983   /* Set up ID.  */
3984   memset (&id, 0, sizeof (id));
3985   id.src_fn = current_function_decl;
3986   id.dst_fn = current_function_decl;
3987   id.decl_map = pointer_map_create ();
3988
3989   id.copy_decl = copy_decl_no_change;
3990   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3991   id.transform_new_cfg = false;
3992   id.transform_return_to_modify = false;
3993   id.transform_lang_insert_block = NULL;
3994
3995   /* Walk the tree once to find local labels.  */
3996   memset (&wi, 0, sizeof (wi));
3997   visited = pointer_set_create ();
3998   wi.info = &id;
3999   wi.pset = visited;
4000   walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
4001   pointer_set_destroy (visited);
4002
4003   copy = gimple_seq_copy (seq);
4004
4005   /* Walk the copy, remapping decls.  */
4006   memset (&wi, 0, sizeof (wi));
4007   wi.info = &id;
4008   walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4009
4010   /* Clean up.  */
4011   pointer_map_destroy (id.decl_map);
4012
4013   return copy;
4014 }
4015
4016
4017 /* Allow someone to determine if SEARCH is a child of TOP from gdb.  */
4018
4019 static tree
4020 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
4021 {
4022   if (*tp == data)
4023     return (tree) data;
4024   else
4025     return NULL;
4026 }
4027
4028 bool
4029 debug_find_tree (tree top, tree search)
4030 {
4031   return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
4032 }
4033
4034
4035 /* Declare the variables created by the inliner.  Add all the variables in
4036    VARS to BIND_EXPR.  */
4037
4038 static void
4039 declare_inline_vars (tree block, tree vars)
4040 {
4041   tree t;
4042   for (t = vars; t; t = TREE_CHAIN (t))
4043     {
4044       DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
4045       gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
4046       cfun->local_decls = tree_cons (NULL_TREE, t, cfun->local_decls);
4047     }
4048
4049   if (block)
4050     BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
4051 }
4052
4053 /* Copy NODE (which must be a DECL).  The DECL originally was in the FROM_FN,
4054    but now it will be in the TO_FN.  PARM_TO_VAR means enable PARM_DECL to
4055    VAR_DECL translation.  */
4056
4057 static tree
4058 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
4059 {
4060   /* Don't generate debug information for the copy if we wouldn't have
4061      generated it for the copy either.  */
4062   DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4063   DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4064
4065   /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4066      declaration inspired this copy.  */ 
4067   DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4068
4069   /* The new variable/label has no RTL, yet.  */
4070   if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4071       && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4072     SET_DECL_RTL (copy, NULL_RTX);
4073   
4074   /* These args would always appear unused, if not for this.  */
4075   TREE_USED (copy) = 1;
4076
4077   /* Set the context for the new declaration.  */
4078   if (!DECL_CONTEXT (decl))
4079     /* Globals stay global.  */
4080     ;
4081   else if (DECL_CONTEXT (decl) != id->src_fn)
4082     /* Things that weren't in the scope of the function we're inlining
4083        from aren't in the scope we're inlining to, either.  */
4084     ;
4085   else if (TREE_STATIC (decl))
4086     /* Function-scoped static variables should stay in the original
4087        function.  */
4088     ;
4089   else
4090     /* Ordinary automatic local variables are now in the scope of the
4091        new function.  */
4092     DECL_CONTEXT (copy) = id->dst_fn;
4093
4094   return copy;
4095 }
4096
4097 static tree
4098 copy_decl_to_var (tree decl, copy_body_data *id)
4099 {
4100   tree copy, type;
4101
4102   gcc_assert (TREE_CODE (decl) == PARM_DECL
4103               || TREE_CODE (decl) == RESULT_DECL);
4104
4105   type = TREE_TYPE (decl);
4106
4107   copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4108   TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4109   TREE_READONLY (copy) = TREE_READONLY (decl);
4110   TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4111   DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4112   DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4113
4114   return copy_decl_for_dup_finish (id, decl, copy);
4115 }
4116
4117 /* Like copy_decl_to_var, but create a return slot object instead of a
4118    pointer variable for return by invisible reference.  */
4119
4120 static tree
4121 copy_result_decl_to_var (tree decl, copy_body_data *id)
4122 {
4123   tree copy, type;
4124
4125   gcc_assert (TREE_CODE (decl) == PARM_DECL
4126               || TREE_CODE (decl) == RESULT_DECL);
4127
4128   type = TREE_TYPE (decl);
4129   if (DECL_BY_REFERENCE (decl))
4130     type = TREE_TYPE (type);
4131
4132   copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
4133   TREE_READONLY (copy) = TREE_READONLY (decl);
4134   TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4135   if (!DECL_BY_REFERENCE (decl))
4136     {
4137       TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4138       DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4139       DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4140     }
4141
4142   return copy_decl_for_dup_finish (id, decl, copy);
4143 }
4144
4145 tree
4146 copy_decl_no_change (tree decl, copy_body_data *id)
4147 {
4148   tree copy;
4149
4150   copy = copy_node (decl);
4151
4152   /* The COPY is not abstract; it will be generated in DST_FN.  */
4153   DECL_ABSTRACT (copy) = 0;
4154   lang_hooks.dup_lang_specific_decl (copy);
4155
4156   /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
4157      been taken; it's for internal bookkeeping in expand_goto_internal.  */
4158   if (TREE_CODE (copy) == LABEL_DECL)
4159     {
4160       TREE_ADDRESSABLE (copy) = 0;
4161       LABEL_DECL_UID (copy) = -1;
4162     }
4163
4164   return copy_decl_for_dup_finish (id, decl, copy);
4165 }
4166
4167 static tree
4168 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
4169 {
4170   if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
4171     return copy_decl_to_var (decl, id);
4172   else
4173     return copy_decl_no_change (decl, id);
4174 }
4175
4176 /* Return a copy of the function's argument tree.  */
4177 static tree
4178 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
4179                                bitmap args_to_skip, tree *vars)
4180 {
4181   tree arg, *parg;
4182   tree new_parm = NULL;
4183   int i = 0;
4184
4185   parg = &new_parm;
4186
4187   for (arg = orig_parm; arg; arg = TREE_CHAIN (arg), i++)
4188     if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
4189       {
4190         tree new_tree = remap_decl (arg, id);
4191         lang_hooks.dup_lang_specific_decl (new_tree);
4192         *parg = new_tree;
4193         parg = &TREE_CHAIN (new_tree);
4194       }
4195     else if (!pointer_map_contains (id->decl_map, arg))
4196       {
4197         /* Make an equivalent VAR_DECL.  If the argument was used
4198            as temporary variable later in function, the uses will be
4199            replaced by local variable.  */
4200         tree var = copy_decl_to_var (arg, id);
4201         get_var_ann (var);
4202         add_referenced_var (var);
4203         insert_decl_map (id, arg, var);
4204         /* Declare this new variable.  */
4205         TREE_CHAIN (var) = *vars;
4206         *vars = var;
4207       }
4208   return new_parm;
4209 }
4210
4211 /* Return a copy of the function's static chain.  */
4212 static tree
4213 copy_static_chain (tree static_chain, copy_body_data * id)
4214 {
4215   tree *chain_copy, *pvar;
4216
4217   chain_copy = &static_chain;
4218   for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
4219     {
4220       tree new_tree = remap_decl (*pvar, id);
4221       lang_hooks.dup_lang_specific_decl (new_tree);
4222       TREE_CHAIN (new_tree) = TREE_CHAIN (*pvar);
4223       *pvar = new_tree;
4224     }
4225   return static_chain;
4226 }
4227
4228 /* Return true if the function is allowed to be versioned.
4229    This is a guard for the versioning functionality.  */
4230 bool
4231 tree_versionable_function_p (tree fndecl)
4232 {
4233   if (fndecl == NULL_TREE)
4234     return false;
4235   /* ??? There are cases where a function is
4236      uninlinable but can be versioned.  */
4237   if (!tree_inlinable_function_p (fndecl))
4238     return false;
4239   
4240   return true;
4241 }
4242
4243 /* Create a copy of a function's tree.
4244    OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4245    of the original function and the new copied function
4246    respectively.  In case we want to replace a DECL 
4247    tree with another tree while duplicating the function's 
4248    body, TREE_MAP represents the mapping between these 
4249    trees. If UPDATE_CLONES is set, the call_stmt fields
4250    of edges of clones of the function will be updated.  */
4251 void
4252 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
4253                           bool update_clones, bitmap args_to_skip)
4254 {
4255   struct cgraph_node *old_version_node;
4256   struct cgraph_node *new_version_node;
4257   copy_body_data id;
4258   tree p;
4259   unsigned i;
4260   struct ipa_replace_map *replace_info;
4261   basic_block old_entry_block;
4262   VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4263
4264   tree t_step;
4265   tree old_current_function_decl = current_function_decl;
4266   tree vars = NULL_TREE;
4267
4268   gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
4269               && TREE_CODE (new_decl) == FUNCTION_DECL);
4270   DECL_POSSIBLY_INLINED (old_decl) = 1;
4271
4272   old_version_node = cgraph_node (old_decl);
4273   new_version_node = cgraph_node (new_decl);
4274
4275   /* Output the inlining info for this abstract function, since it has been
4276      inlined.  If we don't do this now, we can lose the information about the
4277      variables in the function when the blocks get blown away as soon as we
4278      remove the cgraph node.  */
4279   (*debug_hooks->outlining_inline_function) (old_decl);
4280
4281   DECL_ARTIFICIAL (new_decl) = 1;
4282   DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4283
4284   /* Prepare the data structures for the tree copy.  */
4285   memset (&id, 0, sizeof (id));
4286
4287   /* Generate a new name for the new version. */
4288   if (!update_clones)
4289     {
4290       DECL_NAME (new_decl) =  create_tmp_var_name (NULL);
4291       SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
4292       SET_DECL_RTL (new_decl, NULL_RTX);
4293       id.statements_to_fold = pointer_set_create ();
4294     }
4295   
4296   id.decl_map = pointer_map_create ();
4297   id.src_fn = old_decl;
4298   id.dst_fn = new_decl;
4299   id.src_node = old_version_node;
4300   id.dst_node = new_version_node;
4301   id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4302   
4303   id.copy_decl = copy_decl_no_change;
4304   id.transform_call_graph_edges
4305     = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4306   id.transform_new_cfg = true;
4307   id.transform_return_to_modify = false;
4308   id.transform_lang_insert_block = NULL;
4309
4310   current_function_decl = new_decl;
4311   old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4312     (DECL_STRUCT_FUNCTION (old_decl));
4313   initialize_cfun (new_decl, old_decl,
4314                    old_entry_block->count,
4315                    old_entry_block->frequency);
4316   push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4317   
4318   /* Copy the function's static chain.  */
4319   p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4320   if (p)
4321     DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4322       copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4323                          &id);
4324   
4325   /* If there's a tree_map, prepare for substitution.  */
4326   if (tree_map)
4327     for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
4328       {
4329         gimple init;
4330         replace_info
4331           = (struct ipa_replace_map *) VARRAY_GENERIC_PTR (tree_map, i);
4332         if (replace_info->replace_p)
4333           {
4334             tree op = replace_info->new_tree;
4335
4336             STRIP_NOPS (op);
4337
4338             if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4339               op = TREE_OPERAND (op, 0);
4340             
4341             if (TREE_CODE (op) == ADDR_EXPR)
4342               {
4343                 op = TREE_OPERAND (op, 0);
4344                 while (handled_component_p (op))
4345                   op = TREE_OPERAND (op, 0);
4346                 if (TREE_CODE (op) == VAR_DECL)
4347                   add_referenced_var (op);
4348               }
4349             gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
4350             init = setup_one_parameter (&id, replace_info->old_tree,
4351                                         replace_info->new_tree, id.src_fn,
4352                                         NULL,
4353                                         &vars);
4354             if (init)
4355               VEC_safe_push (gimple, heap, init_stmts, init);
4356           }
4357       }
4358   /* Copy the function's arguments.  */
4359   if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4360     DECL_ARGUMENTS (new_decl) =
4361       copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4362                                      args_to_skip, &vars);
4363   
4364   DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4365   
4366   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
4367   number_blocks (id.dst_fn);
4368   
4369   declare_inline_vars (DECL_INITIAL (new_decl), vars);
4370   if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4371     /* Add local vars.  */
4372     for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4373          t_step; t_step = TREE_CHAIN (t_step))
4374       {
4375         tree var = TREE_VALUE (t_step);
4376         if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
4377           cfun->local_decls = tree_cons (NULL_TREE, var, cfun->local_decls);
4378         else if (!can_be_nonlocal (var, &id))
4379           cfun->local_decls =
4380             tree_cons (NULL_TREE, remap_decl (var, &id),
4381                        cfun->local_decls);
4382       }
4383   
4384   /* Copy the Function's body.  */
4385   copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4386   
4387   if (DECL_RESULT (old_decl) != NULL_TREE)
4388     {
4389       tree *res_decl = &DECL_RESULT (old_decl);
4390       DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4391       lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4392     }
4393   
4394   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
4395   number_blocks (new_decl);
4396
4397   if (VEC_length (gimple, init_stmts))
4398     {
4399       basic_block bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4400       while (VEC_length (gimple, init_stmts))
4401         insert_init_stmt (bb, VEC_pop (gimple, init_stmts));
4402     }
4403
4404   /* Clean up.  */
4405   pointer_map_destroy (id.decl_map);
4406   if (!update_clones)
4407     {
4408       fold_marked_statements (0, id.statements_to_fold);
4409       pointer_set_destroy (id.statements_to_fold);
4410       fold_cond_expr_cond ();
4411     }
4412   if (gimple_in_ssa_p (cfun))
4413     {
4414       free_dominance_info (CDI_DOMINATORS);
4415       free_dominance_info (CDI_POST_DOMINATORS);
4416       if (!update_clones)
4417         delete_unreachable_blocks ();
4418       update_ssa (TODO_update_ssa);
4419       if (!update_clones)
4420         {
4421           fold_cond_expr_cond ();
4422           if (need_ssa_update_p ())
4423             update_ssa (TODO_update_ssa);
4424         }
4425     }
4426   free_dominance_info (CDI_DOMINATORS);
4427   free_dominance_info (CDI_POST_DOMINATORS);
4428   VEC_free (gimple, heap, init_stmts);
4429   pop_cfun ();
4430   current_function_decl = old_current_function_decl;
4431   gcc_assert (!current_function_decl
4432               || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4433   return;
4434 }
4435
4436 /* Duplicate a type, fields and all.  */
4437
4438 tree
4439 build_duplicate_type (tree type)
4440 {
4441   struct copy_body_data id;
4442
4443   memset (&id, 0, sizeof (id));
4444   id.src_fn = current_function_decl;
4445   id.dst_fn = current_function_decl;
4446   id.src_cfun = cfun;
4447   id.decl_map = pointer_map_create ();
4448   id.copy_decl = copy_decl_no_change;
4449
4450   type = remap_type_1 (type, &id);
4451
4452   pointer_map_destroy (id.decl_map);
4453
4454   TYPE_CANONICAL (type) = type;
4455
4456   return type;
4457 }
4458
4459 /* Return whether it is safe to inline a function because it used different
4460    target specific options or different optimization options.  */
4461 bool
4462 tree_can_inline_p (tree caller, tree callee)
4463 {
4464 #if 0
4465   /* This causes a regression in SPEC in that it prevents a cold function from
4466      inlining a hot function.  Perhaps this should only apply to functions
4467      that the user declares hot/cold/optimize explicitly.  */
4468
4469   /* Don't inline a function with a higher optimization level than the
4470      caller, or with different space constraints (hot/cold functions).  */
4471   tree caller_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (caller);
4472   tree callee_tree = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (callee);
4473
4474   if (caller_tree != callee_tree)
4475     {
4476       struct cl_optimization *caller_opt
4477         = TREE_OPTIMIZATION ((caller_tree)
4478                              ? caller_tree
4479                              : optimization_default_node);
4480
4481       struct cl_optimization *callee_opt
4482         = TREE_OPTIMIZATION ((callee_tree)
4483                              ? callee_tree
4484                              : optimization_default_node);
4485
4486       if ((caller_opt->optimize > callee_opt->optimize)
4487           || (caller_opt->optimize_size != callee_opt->optimize_size))
4488         return false;
4489     }
4490 #endif
4491
4492   /* Allow the backend to decide if inlining is ok.  */
4493   return targetm.target_option.can_inline_p (caller, callee);
4494 }