OSDN Git Service

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