OSDN Git Service

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