OSDN Git Service

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