OSDN Git Service

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