OSDN Git Service

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