OSDN Git Service

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