OSDN Git Service

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