OSDN Git Service

3da2cc2b4d70701f82ac9b2ce4499fbe4f05dce3
[pf3gnuchains/gcc-fork.git] / gcc / tree-inline.c
1 /* Tree inlining.
2    Copyright 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3    Contributed by Alexandre Oliva <aoliva@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
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
55 /* I'm not real happy about this, but we need to handle gimple and
56    non-gimple trees.  */
57 #include "tree-gimple.h"
58
59 /* Inlining, Cloning, Versioning, Parallelization
60
61    Inlining: a function body is duplicated, but the PARM_DECLs are
62    remapped into VAR_DECLs, and non-void RETURN_EXPRs become
63    GIMPLE_MODIFY_STMTs that store to a dedicated returned-value variable.
64    The duplicated eh_region info of the copy will later be appended
65    to the info for the caller; the eh_region info in copied throwing
66    statements and RESX_EXPRs is adjusted accordingly.
67
68    Cloning: (only in C++) We have one body for a con/de/structor, and
69    multiple function decls, each with a unique parameter list.
70    Duplicate the body, using the given splay tree; some parameters
71    will become constants (like 0 or 1).
72
73    Versioning: a function body is duplicated and the result is a new
74    function rather than into blocks of an existing function as with
75    inlining.  Some parameters will become constants.
76
77    Parallelization: a region of a function is duplicated resulting in
78    a new function.  Variables may be replaced with complex expressions
79    to enable shared variable semantics.
80
81    All of these will simultaneously lookup any callgraph edges.  If
82    we're going to inline the duplicated function body, and the given
83    function has some cloned callgraph nodes (one for each place this
84    function will be inlined) those callgraph edges will be duplicated.
85    If we're cloning the body, those callgraph edges will be
86    updated to point into the new body.  (Note that the original
87    callgraph node and edge list will not be altered.)
88
89    See the CALL_EXPR handling case in copy_body_r ().  */
90
91 /* 0 if we should not perform inlining.
92    1 if we should expand functions calls inline at the tree level.
93    2 if we should consider *all* functions to be inline
94    candidates.  */
95
96 int flag_inline_trees = 0;
97
98 /* To Do:
99
100    o In order to make inlining-on-trees work, we pessimized
101      function-local static constants.  In particular, they are now
102      always output, even when not addressed.  Fix this by treating
103      function-local static constants just like global static
104      constants; the back-end already knows not to output them if they
105      are not needed.
106
107    o Provide heuristics to clamp inlining of recursive template
108      calls?  */
109
110 /* Prototypes.  */
111
112 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
113 static tree copy_generic_body (copy_body_data *);
114 static bool inlinable_function_p (tree);
115 static void remap_block (tree *, copy_body_data *);
116 static tree remap_decls (tree, copy_body_data *);
117 static void copy_bind_expr (tree *, int *, copy_body_data *);
118 static tree mark_local_for_remap_r (tree *, int *, void *);
119 static void unsave_expr_1 (tree);
120 static tree unsave_r (tree *, int *, void *);
121 static void declare_inline_vars (tree, tree);
122 static void remap_save_expr (tree *, void *, int *);
123 static void add_lexical_block (tree current_block, tree new_block);
124 static tree copy_decl_to_var (tree, copy_body_data *);
125 static tree copy_result_decl_to_var (tree, copy_body_data *);
126 static tree copy_decl_no_change (tree, copy_body_data *);
127 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
128
129 /* Insert a tree->tree mapping for ID.  Despite the name suggests
130    that the trees should be variables, it is used for more than that.  */
131
132 void
133 insert_decl_map (copy_body_data *id, tree key, tree value)
134 {
135   splay_tree_insert (id->decl_map, (splay_tree_key) key,
136                      (splay_tree_value) value);
137
138   /* Always insert an identity map as well.  If we see this same new
139      node again, we won't want to duplicate it a second time.  */
140   if (key != value)
141     splay_tree_insert (id->decl_map, (splay_tree_key) value,
142                        (splay_tree_value) value);
143 }
144
145 /* Construct new SSA name for old NAME. ID is the inline context.  */
146
147 static tree
148 remap_ssa_name (tree name, copy_body_data *id)
149 {
150   tree new;
151   splay_tree_node n;
152
153   gcc_assert (TREE_CODE (name) == SSA_NAME);
154
155   n = splay_tree_lookup (id->decl_map, (splay_tree_key) name);
156   if (n)
157     return (tree) n->value;
158
159   /* Do not set DEF_STMT yet as statement is not copied yet. We do that
160      in copy_bb.  */
161   new = remap_decl (SSA_NAME_VAR (name), id);
162   /* We might've substituted constant or another SSA_NAME for
163      the variable. 
164
165      Replace the SSA name representing RESULT_DECL by variable during
166      inlining:  this saves us from need to introduce PHI node in a case
167      return value is just partly initialized.  */
168   if ((TREE_CODE (new) == VAR_DECL || TREE_CODE (new) == PARM_DECL)
169       && (TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
170           || !id->transform_return_to_modify))
171     {
172       new = make_ssa_name (new, NULL);
173       insert_decl_map (id, name, new);
174       if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (name)))
175         {
176           SSA_NAME_DEF_STMT (new) = build_empty_stmt ();
177           if (gimple_default_def (id->src_cfun, SSA_NAME_VAR (name)) == name)
178             set_default_def (SSA_NAME_VAR (new), new);
179         }
180       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new)
181         = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
182       TREE_TYPE (new) = TREE_TYPE (SSA_NAME_VAR (new));
183     }
184   else
185     insert_decl_map (id, name, new);
186   return new;
187 }
188
189 /* Remap DECL during the copying of the BLOCK tree for the function.  */
190
191 tree
192 remap_decl (tree decl, copy_body_data *id)
193 {
194   splay_tree_node n;
195   tree fn;
196
197   /* We only remap local variables in the current function.  */
198   fn = id->src_fn;
199
200   /* See if we have remapped this declaration.  */
201
202   n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
203
204   /* If we didn't already have an equivalent for this declaration,
205      create one now.  */
206   if (!n)
207     {
208       /* Make a copy of the variable or label.  */
209       tree t = id->copy_decl (decl, id);
210      
211       /* Remember it, so that if we encounter this local entity again
212          we can reuse this copy.  Do this early because remap_type may
213          need this decl for TYPE_STUB_DECL.  */
214       insert_decl_map (id, decl, t);
215
216       if (!DECL_P (t))
217         return t;
218
219       /* Remap types, if necessary.  */
220       TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
221       if (TREE_CODE (t) == TYPE_DECL)
222         DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
223
224       /* Remap sizes as necessary.  */
225       walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
226       walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
227
228       /* If fields, do likewise for offset and qualifier.  */
229       if (TREE_CODE (t) == FIELD_DECL)
230         {
231           walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
232           if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
233             walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
234         }
235
236       if (cfun && gimple_in_ssa_p (cfun)
237           && (TREE_CODE (t) == VAR_DECL
238               || TREE_CODE (t) == RESULT_DECL || TREE_CODE (t) == PARM_DECL))
239         {
240           tree def = gimple_default_def (id->src_cfun, decl);
241           get_var_ann (t);
242           if (TREE_CODE (decl) != PARM_DECL && def)
243             {
244               tree map = remap_ssa_name (def, id);
245               /* Watch out RESULT_DECLs whose SSA names map directly
246                  to them.  */
247               if (TREE_CODE (map) == SSA_NAME)
248                 set_default_def (t, map);
249             }
250           add_referenced_var (t);
251         }
252       return t;
253     }
254
255   return unshare_expr ((tree) n->value);
256 }
257
258 static tree
259 remap_type_1 (tree type, copy_body_data *id)
260 {
261   splay_tree_node node;
262   tree new, t;
263
264   if (type == NULL)
265     return type;
266
267   /* See if we have remapped this type.  */
268   node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
269   if (node)
270     return (tree) node->value;
271
272   /* The type only needs remapping if it's variably modified.  */
273   if (! variably_modified_type_p (type, id->src_fn))
274     {
275       insert_decl_map (id, type, type);
276       return type;
277     }
278
279   /* We do need a copy.  build and register it now.  If this is a pointer or
280      reference type, remap the designated type and make a new pointer or
281      reference type.  */
282   if (TREE_CODE (type) == POINTER_TYPE)
283     {
284       new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
285                                          TYPE_MODE (type),
286                                          TYPE_REF_CAN_ALIAS_ALL (type));
287       insert_decl_map (id, type, new);
288       return new;
289     }
290   else if (TREE_CODE (type) == REFERENCE_TYPE)
291     {
292       new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
293                                             TYPE_MODE (type),
294                                             TYPE_REF_CAN_ALIAS_ALL (type));
295       insert_decl_map (id, type, new);
296       return new;
297     }
298   else
299     new = copy_node (type);
300
301   insert_decl_map (id, type, new);
302
303   /* This is a new type, not a copy of an old type.  Need to reassociate
304      variants.  We can handle everything except the main variant lazily.  */
305   t = TYPE_MAIN_VARIANT (type);
306   if (type != t)
307     {
308       t = remap_type (t, id);
309       TYPE_MAIN_VARIANT (new) = t;
310       TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
311       TYPE_NEXT_VARIANT (t) = new;
312     }
313   else
314     {
315       TYPE_MAIN_VARIANT (new) = new;
316       TYPE_NEXT_VARIANT (new) = NULL;
317     }
318
319   if (TYPE_STUB_DECL (type))
320     TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
321
322   /* Lazily create pointer and reference types.  */
323   TYPE_POINTER_TO (new) = NULL;
324   TYPE_REFERENCE_TO (new) = NULL;
325
326   switch (TREE_CODE (new))
327     {
328     case INTEGER_TYPE:
329     case REAL_TYPE:
330     case ENUMERAL_TYPE:
331     case BOOLEAN_TYPE:
332       t = TYPE_MIN_VALUE (new);
333       if (t && TREE_CODE (t) != INTEGER_CST)
334         walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
335
336       t = TYPE_MAX_VALUE (new);
337       if (t && TREE_CODE (t) != INTEGER_CST)
338         walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
339       return new;
340
341     case FUNCTION_TYPE:
342       TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
343       walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
344       return new;
345
346     case ARRAY_TYPE:
347       TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
348       TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
349       break;
350
351     case RECORD_TYPE:
352     case UNION_TYPE:
353     case QUAL_UNION_TYPE:
354       {
355         tree f, nf = NULL;
356
357         for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
358           {
359             t = remap_decl (f, id);
360             DECL_CONTEXT (t) = new;
361             TREE_CHAIN (t) = nf;
362             nf = t;
363           }
364         TYPE_FIELDS (new) = nreverse (nf);
365       }
366       break;
367
368     case OFFSET_TYPE:
369     default:
370       /* Shouldn't have been thought variable sized.  */
371       gcc_unreachable ();
372     }
373
374   walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
375   walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
376
377   return new;
378 }
379
380 tree
381 remap_type (tree type, copy_body_data *id)
382 {
383   splay_tree_node node;
384
385   if (type == NULL)
386     return type;
387
388   /* See if we have remapped this type.  */
389   node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
390   if (node)
391     return (tree) node->value;
392
393   /* The type only needs remapping if it's variably modified.  */
394   if (! variably_modified_type_p (type, id->src_fn))
395     {
396       insert_decl_map (id, type, type);
397       return type;
398     }
399
400   return remap_type_1 (type, id);
401 }
402
403 static tree
404 remap_decls (tree decls, copy_body_data *id)
405 {
406   tree old_var;
407   tree new_decls = NULL_TREE;
408
409   /* Remap its variables.  */
410   for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
411     {
412       tree new_var;
413
414       /* We can not chain the local static declarations into the unexpanded_var_list
415          as we can't duplicate them or break one decl rule.  Go ahead and link
416          them into unexpanded_var_list.  */
417       if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
418           && !DECL_EXTERNAL (old_var))
419         {
420           cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
421                                                  cfun->unexpanded_var_list);
422           continue;
423         }
424
425       /* Remap the variable.  */
426       new_var = remap_decl (old_var, id);
427
428       /* If we didn't remap this variable, so we can't mess with its
429          TREE_CHAIN.  If we remapped this variable to the return slot, it's
430          already declared somewhere else, so don't declare it here.  */
431       if (!new_var || new_var == id->retvar)
432         ;
433       else
434         {
435           gcc_assert (DECL_P (new_var));
436           TREE_CHAIN (new_var) = new_decls;
437           new_decls = new_var;
438         }
439     }
440
441   return nreverse (new_decls);
442 }
443
444 /* Copy the BLOCK to contain remapped versions of the variables
445    therein.  And hook the new block into the block-tree.  */
446
447 static void
448 remap_block (tree *block, copy_body_data *id)
449 {
450   tree old_block;
451   tree new_block;
452   tree fn;
453
454   /* Make the new block.  */
455   old_block = *block;
456   new_block = make_node (BLOCK);
457   TREE_USED (new_block) = TREE_USED (old_block);
458   BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
459   BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
460   *block = new_block;
461
462   /* Remap its variables.  */
463   BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
464
465   fn = id->dst_fn;
466
467   if (id->transform_lang_insert_block)
468     lang_hooks.decls.insert_block (new_block);
469
470   /* Remember the remapped block.  */
471   insert_decl_map (id, old_block, new_block);
472 }
473
474 /* Copy the whole block tree and root it in id->block.  */
475 static tree
476 remap_blocks (tree block, copy_body_data *id)
477 {
478   tree t;
479   tree new = block;
480
481   if (!block)
482     return NULL;
483
484   remap_block (&new, id);
485   gcc_assert (new != block);
486   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
487     add_lexical_block (new, remap_blocks (t, id));
488   return new;
489 }
490
491 static void
492 copy_statement_list (tree *tp)
493 {
494   tree_stmt_iterator oi, ni;
495   tree new;
496
497   new = alloc_stmt_list ();
498   ni = tsi_start (new);
499   oi = tsi_start (*tp);
500   *tp = new;
501
502   for (; !tsi_end_p (oi); tsi_next (&oi))
503     tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
504 }
505
506 static void
507 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
508 {
509   tree block = BIND_EXPR_BLOCK (*tp);
510   /* Copy (and replace) the statement.  */
511   copy_tree_r (tp, walk_subtrees, NULL);
512   if (block)
513     {
514       remap_block (&block, id);
515       BIND_EXPR_BLOCK (*tp) = block;
516     }
517
518   if (BIND_EXPR_VARS (*tp))
519     /* This will remap a lot of the same decls again, but this should be
520        harmless.  */
521     BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
522 }
523
524 /* Called from copy_body_id via walk_tree.  DATA is really an
525    `copy_body_data *'.  */
526
527 tree
528 copy_body_r (tree *tp, int *walk_subtrees, void *data)
529 {
530   copy_body_data *id = (copy_body_data *) data;
531   tree fn = id->src_fn;
532   tree new_block;
533
534   /* Begin by recognizing trees that we'll completely rewrite for the
535      inlining context.  Our output for these trees is completely
536      different from out input (e.g. RETURN_EXPR is deleted, and morphs
537      into an edge).  Further down, we'll handle trees that get
538      duplicated and/or tweaked.  */
539
540   /* When requested, RETURN_EXPRs should be transformed to just the
541      contained GIMPLE_MODIFY_STMT.  The branch semantics of the return will
542      be handled elsewhere by manipulating the CFG rather than a statement.  */
543   if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
544     {
545       tree assignment = TREE_OPERAND (*tp, 0);
546
547       /* If we're returning something, just turn that into an
548          assignment into the equivalent of the original RESULT_DECL.
549          If the "assignment" is just the result decl, the result
550          decl has already been set (e.g. a recent "foo (&result_decl,
551          ...)"); just toss the entire RETURN_EXPR.  */
552       if (assignment && TREE_CODE (assignment) == GIMPLE_MODIFY_STMT)
553         {
554           /* Replace the RETURN_EXPR with (a copy of) the
555              GIMPLE_MODIFY_STMT hanging underneath.  */
556           *tp = copy_node (assignment);
557         }
558       else /* Else the RETURN_EXPR returns no value.  */
559         {
560           *tp = NULL;
561           return (tree) (void *)1;
562         }
563     }
564   else if (TREE_CODE (*tp) == SSA_NAME)
565     {
566       *tp = remap_ssa_name (*tp, id);
567       *walk_subtrees = 0;
568       return NULL;
569     }
570
571   /* Local variables and labels need to be replaced by equivalent
572      variables.  We don't want to copy static variables; there's only
573      one of those, no matter how many times we inline the containing
574      function.  Similarly for globals from an outer function.  */
575   else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
576     {
577       tree new_decl;
578
579       /* Remap the declaration.  */
580       new_decl = remap_decl (*tp, id);
581       gcc_assert (new_decl);
582       /* Replace this variable with the copy.  */
583       STRIP_TYPE_NOPS (new_decl);
584       *tp = new_decl;
585       *walk_subtrees = 0;
586     }
587   else if (TREE_CODE (*tp) == STATEMENT_LIST)
588     copy_statement_list (tp);
589   else if (TREE_CODE (*tp) == SAVE_EXPR)
590     remap_save_expr (tp, id->decl_map, walk_subtrees);
591   else if (TREE_CODE (*tp) == LABEL_DECL
592            && (! DECL_CONTEXT (*tp)
593                || decl_function_context (*tp) == id->src_fn))
594     /* These may need to be remapped for EH handling.  */
595     *tp = remap_decl (*tp, id);
596   else if (TREE_CODE (*tp) == BIND_EXPR)
597     copy_bind_expr (tp, walk_subtrees, id);
598   /* Types may need remapping as well.  */
599   else if (TYPE_P (*tp))
600     *tp = remap_type (*tp, id);
601
602   /* If this is a constant, we have to copy the node iff the type will be
603      remapped.  copy_tree_r will not copy a constant.  */
604   else if (CONSTANT_CLASS_P (*tp))
605     {
606       tree new_type = remap_type (TREE_TYPE (*tp), id);
607
608       if (new_type == TREE_TYPE (*tp))
609         *walk_subtrees = 0;
610
611       else if (TREE_CODE (*tp) == INTEGER_CST)
612         *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
613                                   TREE_INT_CST_HIGH (*tp));
614       else
615         {
616           *tp = copy_node (*tp);
617           TREE_TYPE (*tp) = new_type;
618         }
619     }
620
621   /* Otherwise, just copy the node.  Note that copy_tree_r already
622      knows not to copy VAR_DECLs, etc., so this is safe.  */
623   else
624     {
625       /* Here we handle trees that are not completely rewritten.
626          First we detect some inlining-induced bogosities for
627          discarding.  */
628       if (TREE_CODE (*tp) == GIMPLE_MODIFY_STMT
629           && GIMPLE_STMT_OPERAND (*tp, 0) == GIMPLE_STMT_OPERAND (*tp, 1)
630           && (lang_hooks.tree_inlining.auto_var_in_fn_p
631               (GIMPLE_STMT_OPERAND (*tp, 0), fn)))
632         {
633           /* Some assignments VAR = VAR; don't generate any rtl code
634              and thus don't count as variable modification.  Avoid
635              keeping bogosities like 0 = 0.  */
636           tree decl = GIMPLE_STMT_OPERAND (*tp, 0), value;
637           splay_tree_node n;
638
639           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
640           if (n)
641             {
642               value = (tree) n->value;
643               STRIP_TYPE_NOPS (value);
644               if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
645                 {
646                   *tp = build_empty_stmt ();
647                   return copy_body_r (tp, walk_subtrees, data);
648                 }
649             }
650         }
651       else if (TREE_CODE (*tp) == INDIRECT_REF)
652         {
653           /* Get rid of *& from inline substitutions that can happen when a
654              pointer argument is an ADDR_EXPR.  */
655           tree decl = TREE_OPERAND (*tp, 0);
656           splay_tree_node n;
657
658           n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
659           if (n)
660             {
661               tree new;
662               tree old;
663               /* If we happen to get an ADDR_EXPR in n->value, strip
664                  it manually here as we'll eventually get ADDR_EXPRs
665                  which lie about their types pointed to.  In this case
666                  build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
667                  but we absolutely rely on that.  As fold_indirect_ref
668                  does other useful transformations, try that first, though.  */
669               tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
670               new = unshare_expr ((tree)n->value);
671               old = *tp;
672               *tp = fold_indirect_ref_1 (type, new);
673               if (! *tp)
674                 {
675                   if (TREE_CODE (new) == ADDR_EXPR)
676                     *tp = TREE_OPERAND (new, 0);
677                   else
678                     {
679                       *tp = build1 (INDIRECT_REF, type, new);
680                       TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
681                     }
682                 }
683               *walk_subtrees = 0;
684               return NULL;
685             }
686         }
687
688       /* Here is the "usual case".  Copy this tree node, and then
689          tweak some special cases.  */
690       copy_tree_r (tp, walk_subtrees, NULL);
691
692       /* Global variables we didn't seen yet needs to go into referenced
693          vars.  */
694       if (gimple_in_ssa_p (cfun) && TREE_CODE (*tp) == VAR_DECL)
695         add_referenced_var (*tp);
696        
697       /* If EXPR has block defined, map it to newly constructed block.
698          When inlining we want EXPRs without block appear in the block
699          of function call.  */
700       if (EXPR_P (*tp) || GIMPLE_STMT_P (*tp))
701         {
702           new_block = id->block;
703           if (TREE_BLOCK (*tp))
704             {
705               splay_tree_node n;
706               n = splay_tree_lookup (id->decl_map,
707                                      (splay_tree_key) TREE_BLOCK (*tp));
708               gcc_assert (n);
709               new_block = (tree) n->value;
710             }
711           TREE_BLOCK (*tp) = new_block;
712         }
713
714       if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
715         TREE_OPERAND (*tp, 0) =
716           build_int_cst
717             (NULL_TREE,
718              id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
719
720       if (!GIMPLE_TUPLE_P (*tp))
721         TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
722
723       /* The copied TARGET_EXPR has never been expanded, even if the
724          original node was expanded already.  */
725       if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
726         {
727           TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
728           TREE_OPERAND (*tp, 3) = NULL_TREE;
729         }
730
731       /* Variable substitution need not be simple.  In particular, the
732          INDIRECT_REF substitution above.  Make sure that TREE_CONSTANT
733          and friends are up-to-date.  */
734       else if (TREE_CODE (*tp) == ADDR_EXPR)
735         {
736           walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
737           /* Handle the case where we substituted an INDIRECT_REF
738              into the operand of the ADDR_EXPR.  */
739           if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
740             *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
741           else
742             recompute_tree_invariant_for_addr_expr (*tp);
743           *walk_subtrees = 0;
744         }
745     }
746
747   /* Keep iterating.  */
748   return NULL_TREE;
749 }
750
751 /* Copy basic block, scale profile accordingly.  Edges will be taken care of
752    later  */
753
754 static basic_block
755 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
756 {
757   block_stmt_iterator bsi, copy_bsi;
758   basic_block copy_basic_block;
759
760   /* create_basic_block() will append every new block to
761      basic_block_info automatically.  */
762   copy_basic_block = create_basic_block (NULL, (void *) 0,
763                                          (basic_block) bb->prev_bb->aux);
764   copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
765   copy_basic_block->frequency = (bb->frequency
766                                      * frequency_scale / REG_BR_PROB_BASE);
767   copy_bsi = bsi_start (copy_basic_block);
768
769   for (bsi = bsi_start (bb);
770        !bsi_end_p (bsi); bsi_next (&bsi))
771     {
772       tree stmt = bsi_stmt (bsi);
773       tree orig_stmt = stmt;
774
775       walk_tree (&stmt, copy_body_r, id, NULL);
776
777       /* RETURN_EXPR might be removed,
778          this is signalled by making stmt pointer NULL.  */
779       if (stmt)
780         {
781           tree call, decl;
782
783           gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun, orig_stmt);
784
785           /* With return slot optimization we can end up with
786              non-gimple (foo *)&this->m, fix that here.  */
787           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
788               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == NOP_EXPR
789               && !is_gimple_val (TREE_OPERAND (GIMPLE_STMT_OPERAND (stmt, 1), 0)))
790             gimplify_stmt (&stmt);
791
792           bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
793
794           /* Process new statement.  gimplify_stmt possibly turned statement
795              into multiple statements, we need to process all of them.  */
796           while (!bsi_end_p (copy_bsi))
797             {
798               stmt = bsi_stmt (copy_bsi);
799               call = get_call_expr_in (stmt);
800
801               /* Statements produced by inlining can be unfolded, especially
802                  when we constant propagated some operands.  We can't fold
803                  them right now for two reasons:
804                  1) folding require SSA_NAME_DEF_STMTs to be correct
805                  2) we can't change function calls to builtins.
806                  So we just mark statement for later folding.  We mark
807                  all new statements, instead just statements that has changed
808                  by some nontrivial substitution so even statements made
809                  foldable indirectly are updated.  If this turns out to be
810                  expensive, copy_body can be told to watch for nontrivial
811                  changes.  */
812               if (id->statements_to_fold)
813                 pointer_set_insert (id->statements_to_fold, stmt);
814               /* We're duplicating a CALL_EXPR.  Find any corresponding
815                  callgraph edges and update or duplicate them.  */
816               if (call && (decl = get_callee_fndecl (call)))
817                 {
818                   struct cgraph_node *node;
819                   struct cgraph_edge *edge;
820                  
821                   switch (id->transform_call_graph_edges)
822                     {
823                     case CB_CGE_DUPLICATE:
824                       edge = cgraph_edge (id->src_node, orig_stmt);
825                       if (edge)
826                         cgraph_clone_edge (edge, id->dst_node, stmt,
827                                            REG_BR_PROB_BASE, 1, true);
828                       break;
829
830                     case CB_CGE_MOVE_CLONES:
831                       for (node = id->dst_node->next_clone;
832                            node;
833                            node = node->next_clone)
834                         {
835                           edge = cgraph_edge (node, orig_stmt);
836                           gcc_assert (edge);
837                           cgraph_set_call_stmt (edge, stmt);
838                         }
839                       /* FALLTHRU */
840
841                     case CB_CGE_MOVE:
842                       edge = cgraph_edge (id->dst_node, orig_stmt);
843                       if (edge)
844                         cgraph_set_call_stmt (edge, stmt);
845                       break;
846
847                     default:
848                       gcc_unreachable ();
849                     }
850                 }
851               /* If you think we can abort here, you are wrong.
852                  There is no region 0 in tree land.  */
853               gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
854                           != 0);
855
856               if (tree_could_throw_p (stmt))
857                 {
858                   int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
859                   /* Add an entry for the copied tree in the EH hashtable.
860                      When cloning or versioning, use the hashtable in
861                      cfun, and just copy the EH number.  When inlining, use the
862                      hashtable in the caller, and adjust the region number.  */
863                   if (region > 0)
864                     add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
865
866                   /* If this tree doesn't have a region associated with it,
867                      and there is a "current region,"
868                      then associate this tree with the current region
869                      and add edges associated with this region.  */
870                   if ((lookup_stmt_eh_region_fn (id->src_cfun,
871                                                  orig_stmt) <= 0
872                        && id->eh_region > 0)
873                       && tree_could_throw_p (stmt))
874                     add_stmt_to_eh_region (stmt, id->eh_region);
875                 }
876               if (gimple_in_ssa_p (cfun))
877                 {
878                    ssa_op_iter i;
879                    tree def;
880
881                    find_new_referenced_vars (bsi_stmt_ptr (copy_bsi));
882                    FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
883                     if (TREE_CODE (def) == SSA_NAME)
884                       SSA_NAME_DEF_STMT (def) = stmt;
885                 }
886               bsi_next (&copy_bsi);
887             }
888           copy_bsi = bsi_last (copy_basic_block);
889         }
890     }
891   return copy_basic_block;
892 }
893
894 /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
895    form is quite easy, since dominator relationship for old basic blocks does
896    not change.
897
898    There is however exception where inlining might change dominator relation
899    across EH edges from basic block within inlined functions destinating
900    to landing pads in function we inline into.
901
902    The function mark PHI_RESULT of such PHI nodes for renaming; it is
903    safe the EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI
904    must be set.  This means, that there will be no overlapping live ranges
905    for the underlying symbol.
906
907    This might change in future if we allow redirecting of EH edges and
908    we might want to change way build CFG pre-inlining to include
909    all the possible edges then.  */
910 static void
911 update_ssa_across_eh_edges (basic_block bb)
912 {
913   edge e;
914   edge_iterator ei;
915
916   FOR_EACH_EDGE (e, ei, bb->succs)
917     if (!e->dest->aux
918         || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
919       {
920         tree phi;
921
922         gcc_assert (e->flags & EDGE_EH);
923         for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
924           {
925             gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI
926                         (PHI_RESULT (phi)));
927             mark_sym_for_renaming
928               (SSA_NAME_VAR (PHI_RESULT (phi)));
929           }
930       }
931 }
932
933 /* Copy edges from BB into its copy constructed earlier, scale profile
934    accordingly.  Edges will be taken care of later.  Assume aux
935    pointers to point to the copies of each BB.  */
936 static void
937 copy_edges_for_bb (basic_block bb, int count_scale)
938 {
939   basic_block new_bb = (basic_block) bb->aux;
940   edge_iterator ei;
941   edge old_edge;
942   block_stmt_iterator bsi;
943   int flags;
944
945   /* Use the indices from the original blocks to create edges for the
946      new ones.  */
947   FOR_EACH_EDGE (old_edge, ei, bb->succs)
948     if (!(old_edge->flags & EDGE_EH))
949       {
950         edge new;
951
952         flags = old_edge->flags;
953
954         /* Return edges do get a FALLTHRU flag when the get inlined.  */
955         if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
956             && old_edge->dest->aux != EXIT_BLOCK_PTR)
957           flags |= EDGE_FALLTHRU;
958         new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
959         new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
960         new->probability = old_edge->probability;
961       }
962
963   if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
964     return;
965
966   for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
967     {
968       tree copy_stmt;
969
970       copy_stmt = bsi_stmt (bsi);
971       update_stmt (copy_stmt);
972       if (gimple_in_ssa_p (cfun))
973         mark_symbols_for_renaming (copy_stmt);
974       /* Do this before the possible split_block.  */
975       bsi_next (&bsi);
976
977       /* If this tree could throw an exception, there are two
978          cases where we need to add abnormal edge(s): the
979          tree wasn't in a region and there is a "current
980          region" in the caller; or the original tree had
981          EH edges.  In both cases split the block after the tree,
982          and add abnormal edge(s) as needed; we need both
983          those from the callee and the caller.
984          We check whether the copy can throw, because the const
985          propagation can change an INDIRECT_REF which throws
986          into a COMPONENT_REF which doesn't.  If the copy
987          can throw, the original could also throw.  */
988
989       if (tree_can_throw_internal (copy_stmt))
990         {
991           if (!bsi_end_p (bsi))
992             /* Note that bb's predecessor edges aren't necessarily
993                right at this point; split_block doesn't care.  */
994             {
995               edge e = split_block (new_bb, copy_stmt);
996
997               new_bb = e->dest;
998               new_bb->aux = e->src->aux;
999               bsi = bsi_start (new_bb);
1000             }
1001
1002            make_eh_edges (copy_stmt);
1003
1004            if (gimple_in_ssa_p (cfun))
1005              update_ssa_across_eh_edges (bb_for_stmt (copy_stmt));
1006         }
1007     }
1008 }
1009
1010 /* Copy the PHIs.  All blocks and edges are copied, some blocks
1011    was possibly split and new outgoing EH edges inserted.
1012    BB points to the block of original function and AUX pointers links
1013    the original and newly copied blocks.  */
1014
1015 static void
1016 copy_phis_for_bb (basic_block bb, copy_body_data *id)
1017 {
1018   basic_block new_bb = bb->aux;
1019   edge_iterator ei;
1020   tree phi;
1021
1022   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1023     {
1024       tree res = PHI_RESULT (phi);
1025       tree new_res = res;
1026       tree new_phi;
1027       edge new_edge;
1028
1029       if (is_gimple_reg (res))
1030         {
1031           walk_tree (&new_res, copy_body_r, id, NULL);
1032           SSA_NAME_DEF_STMT (new_res)
1033             = new_phi = create_phi_node (new_res, new_bb);
1034           FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
1035             {
1036               edge old_edge = find_edge (new_edge->src->aux, bb);
1037               tree arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
1038               tree new_arg = arg;
1039
1040               walk_tree (&new_arg, copy_body_r, id, NULL);
1041               gcc_assert (new_arg);
1042               add_phi_arg (new_phi, new_arg, new_edge);
1043             }
1044         }
1045     }
1046 }
1047
1048 /* Wrapper for remap_decl so it can be used as a callback.  */
1049 static tree
1050 remap_decl_1 (tree decl, void *data)
1051 {
1052   return remap_decl (decl, (copy_body_data *) data);
1053 }
1054
1055 /* Build struct function and associated datastructures for the new clone
1056    NEW_FNDECL to be build.  CALLEE_FNDECL is the original */
1057
1058 static void
1059 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count,
1060                  int frequency)
1061 {
1062   struct function *new_cfun
1063      = (struct function *) ggc_alloc_cleared (sizeof (struct function));
1064   struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1065   int count_scale, frequency_scale;
1066
1067   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1068     count_scale = (REG_BR_PROB_BASE * count
1069                    / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1070   else
1071     count_scale = 1;
1072
1073   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1074     frequency_scale = (REG_BR_PROB_BASE * frequency
1075                        /
1076                        ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1077   else
1078     frequency_scale = count_scale;
1079
1080   /* Register specific tree functions.  */
1081   tree_register_cfg_hooks ();
1082   *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
1083   new_cfun->funcdef_no = get_next_funcdef_no ();
1084   VALUE_HISTOGRAMS (new_cfun) = NULL;
1085   new_cfun->unexpanded_var_list = NULL;
1086   new_cfun->cfg = NULL;
1087   new_cfun->decl = new_fndecl /*= copy_node (callee_fndecl)*/;
1088   new_cfun->ib_boundaries_block = NULL;
1089   DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
1090   push_cfun (new_cfun);
1091   init_empty_tree_cfg ();
1092
1093   ENTRY_BLOCK_PTR->count =
1094     (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1095      REG_BR_PROB_BASE);
1096   ENTRY_BLOCK_PTR->frequency =
1097     (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1098      frequency_scale / REG_BR_PROB_BASE);
1099   EXIT_BLOCK_PTR->count =
1100     (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1101      REG_BR_PROB_BASE);
1102   EXIT_BLOCK_PTR->frequency =
1103     (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
1104      frequency_scale / REG_BR_PROB_BASE);
1105   if (src_cfun->eh)
1106     init_eh_for_function ();
1107
1108   if (src_cfun->gimple_df)
1109     {
1110       init_tree_ssa ();
1111       cfun->gimple_df->in_ssa_p = true;
1112       init_ssa_operands ();
1113     }
1114   pop_cfun ();
1115 }
1116
1117 /* Make a copy of the body of FN so that it can be inserted inline in
1118    another function.  Walks FN via CFG, returns new fndecl.  */
1119
1120 static tree
1121 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
1122                basic_block entry_block_map, basic_block exit_block_map)
1123 {
1124   tree callee_fndecl = id->src_fn;
1125   /* Original cfun for the callee, doesn't change.  */
1126   struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1127   struct function *cfun_to_copy;
1128   basic_block bb;
1129   tree new_fndecl = NULL;
1130   int count_scale, frequency_scale;
1131   int last;
1132
1133   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1134     count_scale = (REG_BR_PROB_BASE * count
1135                    / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1136   else
1137     count_scale = 1;
1138
1139   if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1140     frequency_scale = (REG_BR_PROB_BASE * frequency
1141                        /
1142                        ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1143   else
1144     frequency_scale = count_scale;
1145
1146   /* Register specific tree functions.  */
1147   tree_register_cfg_hooks ();
1148
1149   /* Must have a CFG here at this point.  */
1150   gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
1151               (DECL_STRUCT_FUNCTION (callee_fndecl)));
1152
1153   cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1154
1155
1156   ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
1157   EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
1158   entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1159   exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1160
1161   /* Duplicate any exception-handling regions.  */
1162   if (cfun->eh)
1163     {
1164       id->eh_region_offset
1165         = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1166                                 0, id->eh_region);
1167     }
1168   /* Use aux pointers to map the original blocks to copy.  */
1169   FOR_EACH_BB_FN (bb, cfun_to_copy)
1170     {
1171       basic_block new = copy_bb (id, bb, frequency_scale, count_scale);
1172       bb->aux = new;
1173       new->aux = bb;
1174     }
1175
1176   last = n_basic_blocks;
1177   /* Now that we've duplicated the blocks, duplicate their edges.  */
1178   FOR_ALL_BB_FN (bb, cfun_to_copy)
1179     copy_edges_for_bb (bb, count_scale);
1180   if (gimple_in_ssa_p (cfun))
1181     FOR_ALL_BB_FN (bb, cfun_to_copy)
1182       copy_phis_for_bb (bb, id);
1183   FOR_ALL_BB_FN (bb, cfun_to_copy)
1184     {
1185       ((basic_block)bb->aux)->aux = NULL;
1186       bb->aux = NULL;
1187     }
1188   /* Zero out AUX fields of newly created block during EH edge
1189      insertion. */
1190   for (; last < n_basic_blocks; last++)
1191     BASIC_BLOCK (last)->aux = NULL;
1192   entry_block_map->aux = NULL;
1193   exit_block_map->aux = NULL;
1194
1195   return new_fndecl;
1196 }
1197
1198 /* Make a copy of the body of FN so that it can be inserted inline in
1199    another function.  */
1200
1201 static tree
1202 copy_generic_body (copy_body_data *id)
1203 {
1204   tree body;
1205   tree fndecl = id->src_fn;
1206
1207   body = DECL_SAVED_TREE (fndecl);
1208   walk_tree (&body, copy_body_r, id, NULL);
1209
1210   return body;
1211 }
1212
1213 static tree
1214 copy_body (copy_body_data *id, gcov_type count, int frequency,
1215            basic_block entry_block_map, basic_block exit_block_map)
1216 {
1217   tree fndecl = id->src_fn;
1218   tree body;
1219
1220   /* If this body has a CFG, walk CFG and copy.  */
1221   gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1222   body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
1223
1224   return body;
1225 }
1226
1227 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1228    defined in function FN, or of a data member thereof.  */
1229
1230 static bool
1231 self_inlining_addr_expr (tree value, tree fn)
1232 {
1233   tree var;
1234
1235   if (TREE_CODE (value) != ADDR_EXPR)
1236     return false;
1237
1238   var = get_base_address (TREE_OPERAND (value, 0));
1239
1240   return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1241 }
1242
1243 static void
1244 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1245                      basic_block bb, tree *vars)
1246 {
1247   tree init_stmt;
1248   tree var;
1249   tree var_sub;
1250   tree rhs = value ? fold_convert (TREE_TYPE (p), value) : NULL;
1251   tree def = (gimple_in_ssa_p (cfun)
1252               ? gimple_default_def (id->src_cfun, p) : NULL);
1253
1254   /* If the parameter is never assigned to, has no SSA_NAMEs created,
1255      we may not need to create a new variable here at all.  Instead, we may
1256      be able to just use the argument value.  */
1257   if (TREE_READONLY (p)
1258       && !TREE_ADDRESSABLE (p)
1259       && value && !TREE_SIDE_EFFECTS (value)
1260       && !def)
1261     {
1262       /* We may produce non-gimple trees by adding NOPs or introduce
1263          invalid sharing when operand is not really constant.
1264          It is not big deal to prohibit constant propagation here as
1265          we will constant propagate in DOM1 pass anyway.  */
1266       if (is_gimple_min_invariant (value)
1267           && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1268           /* We have to be very careful about ADDR_EXPR.  Make sure
1269              the base variable isn't a local variable of the inlined
1270              function, e.g., when doing recursive inlining, direct or
1271              mutually-recursive or whatever, which is why we don't
1272              just test whether fn == current_function_decl.  */
1273           && ! self_inlining_addr_expr (value, fn))
1274         {
1275           insert_decl_map (id, p, value);
1276           return;
1277         }
1278     }
1279
1280   /* Make an equivalent VAR_DECL.  Note that we must NOT remap the type
1281      here since the type of this decl must be visible to the calling
1282      function.  */
1283   var = copy_decl_to_var (p, id);
1284   if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
1285     {
1286       get_var_ann (var);
1287       add_referenced_var (var);
1288     }
1289
1290   /* See if the frontend wants to pass this by invisible reference.  If
1291      so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1292      replace uses of the PARM_DECL with dereferences.  */
1293   if (TREE_TYPE (var) != TREE_TYPE (p)
1294       && POINTER_TYPE_P (TREE_TYPE (var))
1295       && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1296     {
1297       insert_decl_map (id, var, var);
1298       var_sub = build_fold_indirect_ref (var);
1299     }
1300   else
1301     var_sub = var;
1302
1303   /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1304      that way, when the PARM_DECL is encountered, it will be
1305      automatically replaced by the VAR_DECL.  */
1306   insert_decl_map (id, p, var_sub);
1307
1308   /* Declare this new variable.  */
1309   TREE_CHAIN (var) = *vars;
1310   *vars = var;
1311
1312   /* Make gimplifier happy about this variable.  */
1313   DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1314
1315   /* Even if P was TREE_READONLY, the new VAR should not be.
1316      In the original code, we would have constructed a
1317      temporary, and then the function body would have never
1318      changed the value of P.  However, now, we will be
1319      constructing VAR directly.  The constructor body may
1320      change its value multiple times as it is being
1321      constructed.  Therefore, it must not be TREE_READONLY;
1322      the back-end assumes that TREE_READONLY variable is
1323      assigned to only once.  */
1324   if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1325     TREE_READONLY (var) = 0;
1326
1327   /* If there is no setup required and we are in SSA, take the easy route
1328      replacing all SSA names representing the function parameter by the
1329      SSA name passed to function.
1330
1331      We need to construct map for the variable anyway as it might be used
1332      in different SSA names when parameter is set in function.
1333
1334      FIXME: This usually kills the last connection in between inlined
1335      function parameter and the actual value in debug info.  Can we do
1336      better here?  If we just inserted the statement, copy propagation
1337      would kill it anyway as it always did in older versions of GCC.
1338
1339      We might want to introduce a notion that single SSA_NAME might
1340      represent multiple variables for purposes of debugging. */
1341   if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
1342       && (TREE_CODE (rhs) == SSA_NAME
1343           || is_gimple_min_invariant (rhs))
1344       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
1345     {
1346       insert_decl_map (id, def, rhs);
1347       return;
1348     }
1349
1350   /* Initialize this VAR_DECL from the equivalent argument.  Convert
1351      the argument to the proper type in case it was promoted.  */
1352   if (value)
1353     {
1354       block_stmt_iterator bsi = bsi_last (bb);
1355
1356       if (rhs == error_mark_node)
1357         {
1358           insert_decl_map (id, p, var_sub);
1359           return;
1360         }
1361
1362       STRIP_USELESS_TYPE_CONVERSION (rhs);
1363
1364       /* We want to use GIMPLE_MODIFY_STMT, not INIT_EXPR here so that we
1365          keep our trees in gimple form.  */
1366       if (def && gimple_in_ssa_p (cfun) && is_gimple_reg (p))
1367         {
1368           def = remap_ssa_name (def, id);
1369           init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), def, rhs);
1370           SSA_NAME_DEF_STMT (def) = init_stmt;
1371           SSA_NAME_IS_DEFAULT_DEF (def) = 0;
1372           set_default_def (var, NULL);
1373         }
1374       else
1375         init_stmt = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (var), var, rhs);
1376
1377       /* If we did not create a gimple value and we did not create a gimple
1378          cast of a gimple value, then we will need to gimplify INIT_STMTS
1379          at the end.  Note that is_gimple_cast only checks the outer
1380          tree code, not its operand.  Thus the explicit check that its
1381          operand is a gimple value.  */
1382       if ((!is_gimple_val (rhs)
1383           && (!is_gimple_cast (rhs)
1384               || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1385           || !is_gimple_reg (var))
1386         {
1387           tree_stmt_iterator i;
1388
1389           push_gimplify_context ();
1390           gimplify_stmt (&init_stmt);
1391           if (gimple_in_ssa_p (cfun)
1392               && init_stmt && TREE_CODE (init_stmt) == STATEMENT_LIST)
1393             {
1394               /* The replacement can expose previously unreferenced
1395                  variables.  */
1396               for (i = tsi_start (init_stmt); !tsi_end_p (i); tsi_next (&i))
1397                 find_new_referenced_vars (tsi_stmt_ptr (i));
1398              }
1399           pop_gimplify_context (NULL);
1400         }
1401
1402       /* If VAR represents a zero-sized variable, it's possible that the
1403          assignment statment may result in no gimple statements.  */
1404       if (init_stmt)
1405         bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1406       if (gimple_in_ssa_p (cfun))
1407         for (;!bsi_end_p (bsi); bsi_next (&bsi))
1408           mark_symbols_for_renaming (bsi_stmt (bsi));
1409     }
1410 }
1411
1412 /* Generate code to initialize the parameters of the function at the
1413    top of the stack in ID from the ARGS (presented as a TREE_LIST).  */
1414
1415 static void
1416 initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
1417                                tree fn, basic_block bb)
1418 {
1419   tree parms;
1420   tree a;
1421   tree p;
1422   tree vars = NULL_TREE;
1423   int argnum = 0;
1424
1425   /* Figure out what the parameters are.  */
1426   parms = DECL_ARGUMENTS (fn);
1427
1428   /* Loop through the parameter declarations, replacing each with an
1429      equivalent VAR_DECL, appropriately initialized.  */
1430   for (p = parms, a = args; p;
1431        a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
1432     {
1433       tree value;
1434
1435       ++argnum;
1436
1437       /* Find the initializer.  */
1438       value = lang_hooks.tree_inlining.convert_parm_for_inlining
1439               (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
1440
1441       setup_one_parameter (id, p, value, fn, bb, &vars);
1442     }
1443
1444   /* Initialize the static chain.  */
1445   p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1446   gcc_assert (fn != current_function_decl);
1447   if (p)
1448     {
1449       /* No static chain?  Seems like a bug in tree-nested.c.  */
1450       gcc_assert (static_chain);
1451
1452       setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1453     }
1454
1455   declare_inline_vars (id->block, vars);
1456 }
1457
1458 /* Declare a return variable to replace the RESULT_DECL for the
1459    function we are calling.  An appropriate DECL_STMT is returned.
1460    The USE_STMT is filled to contain a use of the declaration to
1461    indicate the return value of the function.
1462
1463    RETURN_SLOT, if non-null is place where to store the result.  It
1464    is set only for CALL_EXPR_RETURN_SLOT_OPT.  MODIFY_DEST, if non-null,
1465    was the LHS of the GIMPLE_MODIFY_STMT to which this call is the RHS.
1466
1467    The return value is a (possibly null) value that is the result of the
1468    function as seen by the callee.  *USE_P is a (possibly null) value that
1469    holds the result as seen by the caller.  */
1470
1471 static tree
1472 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
1473                          tree *use_p)
1474 {
1475   tree callee = id->src_fn;
1476   tree caller = id->dst_fn;
1477   tree result = DECL_RESULT (callee);
1478   tree callee_type = TREE_TYPE (result);
1479   tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1480   tree var, use;
1481
1482   /* We don't need to do anything for functions that don't return
1483      anything.  */
1484   if (!result || VOID_TYPE_P (callee_type))
1485     {
1486       *use_p = NULL_TREE;
1487       return NULL_TREE;
1488     }
1489
1490   /* If there was a return slot, then the return value is the
1491      dereferenced address of that object.  */
1492   if (return_slot)
1493     {
1494       /* The front end shouldn't have used both return_slot and
1495          a modify expression.  */
1496       gcc_assert (!modify_dest);
1497       if (DECL_BY_REFERENCE (result))
1498         {
1499           tree return_slot_addr = build_fold_addr_expr (return_slot);
1500           STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
1501
1502           /* We are going to construct *&return_slot and we can't do that
1503              for variables believed to be not addressable. 
1504
1505              FIXME: This check possibly can match, because values returned
1506              via return slot optimization are not believed to have address
1507              taken by alias analysis.  */
1508           gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
1509           if (gimple_in_ssa_p (cfun))
1510             {
1511               HOST_WIDE_INT bitsize;
1512               HOST_WIDE_INT bitpos;
1513               tree offset;
1514               enum machine_mode mode;
1515               int unsignedp;
1516               int volatilep;
1517               tree base;
1518               base = get_inner_reference (return_slot, &bitsize, &bitpos,
1519                                           &offset,
1520                                           &mode, &unsignedp, &volatilep,
1521                                           false);
1522               if (TREE_CODE (base) == INDIRECT_REF)
1523                 base = TREE_OPERAND (base, 0);
1524               if (TREE_CODE (base) == SSA_NAME)
1525                 base = SSA_NAME_VAR (base);
1526               mark_sym_for_renaming (base);
1527             }
1528           var = return_slot_addr;
1529         }
1530       else
1531         {
1532           var = return_slot;
1533           gcc_assert (TREE_CODE (var) != SSA_NAME);
1534         }
1535       if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1536            || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1537           && !DECL_GIMPLE_REG_P (result)
1538           && DECL_P (var))
1539         DECL_GIMPLE_REG_P (var) = 0;
1540       use = NULL;
1541       goto done;
1542     }
1543
1544   /* All types requiring non-trivial constructors should have been handled.  */
1545   gcc_assert (!TREE_ADDRESSABLE (callee_type));
1546
1547   /* Attempt to avoid creating a new temporary variable.  */
1548   if (modify_dest
1549       && TREE_CODE (modify_dest) != SSA_NAME)
1550     {
1551       bool use_it = false;
1552
1553       /* We can't use MODIFY_DEST if there's type promotion involved.  */
1554       if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1555         use_it = false;
1556
1557       /* ??? If we're assigning to a variable sized type, then we must
1558          reuse the destination variable, because we've no good way to
1559          create variable sized temporaries at this point.  */
1560       else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1561         use_it = true;
1562
1563       /* If the callee cannot possibly modify MODIFY_DEST, then we can
1564          reuse it as the result of the call directly.  Don't do this if
1565          it would promote MODIFY_DEST to addressable.  */
1566       else if (TREE_ADDRESSABLE (result))
1567         use_it = false;
1568       else
1569         {
1570           tree base_m = get_base_address (modify_dest);
1571
1572           /* If the base isn't a decl, then it's a pointer, and we don't
1573              know where that's going to go.  */
1574           if (!DECL_P (base_m))
1575             use_it = false;
1576           else if (is_global_var (base_m))
1577             use_it = false;
1578           else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1579                     || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
1580                    && !DECL_GIMPLE_REG_P (result)
1581                    && DECL_GIMPLE_REG_P (base_m))
1582             use_it = false;
1583           else if (!TREE_ADDRESSABLE (base_m))
1584             use_it = true;
1585         }
1586
1587       if (use_it)
1588         {
1589           var = modify_dest;
1590           use = NULL;
1591           goto done;
1592         }
1593     }
1594
1595   gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
1596
1597   var = copy_result_decl_to_var (result, id);
1598   if (gimple_in_ssa_p (cfun))
1599     {
1600       get_var_ann (var);
1601       add_referenced_var (var);
1602     }
1603
1604   DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1605   DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1606     = tree_cons (NULL_TREE, var,
1607                  DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
1608
1609   /* Do not have the rest of GCC warn about this variable as it should
1610      not be visible to the user.  */
1611   TREE_NO_WARNING (var) = 1;
1612
1613   declare_inline_vars (id->block, var);
1614
1615   /* Build the use expr.  If the return type of the function was
1616      promoted, convert it back to the expected type.  */
1617   use = var;
1618   if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1619     use = fold_convert (caller_type, var);
1620     
1621   STRIP_USELESS_TYPE_CONVERSION (use);
1622
1623   if (DECL_BY_REFERENCE (result))
1624     var = build_fold_addr_expr (var);
1625
1626  done:
1627   /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
1628      way, when the RESULT_DECL is encountered, it will be
1629      automatically replaced by the VAR_DECL.  */
1630   insert_decl_map (id, result, var);
1631
1632   /* Remember this so we can ignore it in remap_decls.  */
1633   id->retvar = var;
1634
1635   *use_p = use;
1636   return var;
1637 }
1638
1639 /* Returns nonzero if a function can be inlined as a tree.  */
1640
1641 bool
1642 tree_inlinable_function_p (tree fn)
1643 {
1644   return inlinable_function_p (fn);
1645 }
1646
1647 static const char *inline_forbidden_reason;
1648
1649 static tree
1650 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1651                       void *fnp)
1652 {
1653   tree node = *nodep;
1654   tree fn = (tree) fnp;
1655   tree t;
1656
1657   switch (TREE_CODE (node))
1658     {
1659     case CALL_EXPR:
1660       /* Refuse to inline alloca call unless user explicitly forced so as
1661          this may change program's memory overhead drastically when the
1662          function using alloca is called in loop.  In GCC present in
1663          SPEC2000 inlining into schedule_block cause it to require 2GB of
1664          RAM instead of 256MB.  */
1665       if (alloca_call_p (node)
1666           && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1667         {
1668           inline_forbidden_reason
1669             = G_("function %q+F can never be inlined because it uses "
1670                  "alloca (override using the always_inline attribute)");
1671           return node;
1672         }
1673       t = get_callee_fndecl (node);
1674       if (! t)
1675         break;
1676
1677       /* We cannot inline functions that call setjmp.  */
1678       if (setjmp_call_p (t))
1679         {
1680           inline_forbidden_reason
1681             = G_("function %q+F can never be inlined because it uses setjmp");
1682           return node;
1683         }
1684
1685       if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1686         switch (DECL_FUNCTION_CODE (t))
1687           {
1688             /* We cannot inline functions that take a variable number of
1689                arguments.  */
1690           case BUILT_IN_VA_START:
1691           case BUILT_IN_STDARG_START:
1692           case BUILT_IN_NEXT_ARG:
1693           case BUILT_IN_VA_END:
1694             inline_forbidden_reason
1695               = G_("function %q+F can never be inlined because it "
1696                    "uses variable argument lists");
1697             return node;
1698
1699           case BUILT_IN_LONGJMP:
1700             /* We can't inline functions that call __builtin_longjmp at
1701                all.  The non-local goto machinery really requires the
1702                destination be in a different function.  If we allow the
1703                function calling __builtin_longjmp to be inlined into the
1704                function calling __builtin_setjmp, Things will Go Awry.  */
1705             inline_forbidden_reason
1706               = G_("function %q+F can never be inlined because "
1707                    "it uses setjmp-longjmp exception handling");
1708             return node;
1709
1710           case BUILT_IN_NONLOCAL_GOTO:
1711             /* Similarly.  */
1712             inline_forbidden_reason
1713               = G_("function %q+F can never be inlined because "
1714                    "it uses non-local goto");
1715             return node;
1716
1717           case BUILT_IN_RETURN:
1718           case BUILT_IN_APPLY_ARGS:
1719             /* If a __builtin_apply_args caller would be inlined,
1720                it would be saving arguments of the function it has
1721                been inlined into.  Similarly __builtin_return would
1722                return from the function the inline has been inlined into.  */
1723             inline_forbidden_reason
1724               = G_("function %q+F can never be inlined because "
1725                    "it uses __builtin_return or __builtin_apply_args");
1726             return node;
1727
1728           default:
1729             break;
1730           }
1731       break;
1732
1733     case GOTO_EXPR:
1734       t = TREE_OPERAND (node, 0);
1735
1736       /* We will not inline a function which uses computed goto.  The
1737          addresses of its local labels, which may be tucked into
1738          global storage, are of course not constant across
1739          instantiations, which causes unexpected behavior.  */
1740       if (TREE_CODE (t) != LABEL_DECL)
1741         {
1742           inline_forbidden_reason
1743             = G_("function %q+F can never be inlined "
1744                  "because it contains a computed goto");
1745           return node;
1746         }
1747       break;
1748
1749     case LABEL_EXPR:
1750       t = TREE_OPERAND (node, 0);
1751       if (DECL_NONLOCAL (t))
1752         {
1753           /* We cannot inline a function that receives a non-local goto
1754              because we cannot remap the destination label used in the
1755              function that is performing the non-local goto.  */
1756           inline_forbidden_reason
1757             = G_("function %q+F can never be inlined "
1758                  "because it receives a non-local goto");
1759           return node;
1760         }
1761       break;
1762
1763     case RECORD_TYPE:
1764     case UNION_TYPE:
1765       /* We cannot inline a function of the form
1766
1767            void F (int i) { struct S { int ar[i]; } s; }
1768
1769          Attempting to do so produces a catch-22.
1770          If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1771          UNION_TYPE nodes, then it goes into infinite recursion on a
1772          structure containing a pointer to its own type.  If it doesn't,
1773          then the type node for S doesn't get adjusted properly when
1774          F is inlined. 
1775
1776          ??? This is likely no longer true, but it's too late in the 4.0
1777          cycle to try to find out.  This should be checked for 4.1.  */
1778       for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1779         if (variably_modified_type_p (TREE_TYPE (t), NULL))
1780           {
1781             inline_forbidden_reason
1782               = G_("function %q+F can never be inlined "
1783                    "because it uses variable sized variables");
1784             return node;
1785           }
1786
1787     default:
1788       break;
1789     }
1790
1791   return NULL_TREE;
1792 }
1793
1794 /* Return subexpression representing possible alloca call, if any.  */
1795 static tree
1796 inline_forbidden_p (tree fndecl)
1797 {
1798   location_t saved_loc = input_location;
1799   block_stmt_iterator bsi;
1800   basic_block bb;
1801   tree ret = NULL_TREE;
1802
1803   FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1804     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1805       {
1806         ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1807                                     inline_forbidden_p_1, fndecl);
1808         if (ret)
1809           goto egress;
1810       }
1811
1812 egress:
1813   input_location = saved_loc;
1814   return ret;
1815 }
1816
1817 /* Returns nonzero if FN is a function that does not have any
1818    fundamental inline blocking properties.  */
1819
1820 static bool
1821 inlinable_function_p (tree fn)
1822 {
1823   bool inlinable = true;
1824
1825   /* If we've already decided this function shouldn't be inlined,
1826      there's no need to check again.  */
1827   if (DECL_UNINLINABLE (fn))
1828     return false;
1829
1830   /* See if there is any language-specific reason it cannot be
1831      inlined.  (It is important that this hook be called early because
1832      in C++ it may result in template instantiation.)
1833      If the function is not inlinable for language-specific reasons,
1834      it is left up to the langhook to explain why.  */
1835   inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
1836
1837   /* If we don't have the function body available, we can't inline it.
1838      However, this should not be recorded since we also get here for
1839      forward declared inline functions.  Therefore, return at once.  */
1840   if (!DECL_SAVED_TREE (fn))
1841     return false;
1842
1843   /* If we're not inlining at all, then we cannot inline this function.  */
1844   else if (!flag_inline_trees)
1845     inlinable = false;
1846
1847   /* Only try to inline functions if DECL_INLINE is set.  This should be
1848      true for all functions declared `inline', and for all other functions
1849      as well with -finline-functions.
1850
1851      Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1852      it's the front-end that must set DECL_INLINE in this case, because
1853      dwarf2out loses if a function that does not have DECL_INLINE set is
1854      inlined anyway.  That is why we have both DECL_INLINE and
1855      DECL_DECLARED_INLINE_P.  */
1856   /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1857             here should be redundant.  */
1858   else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1859     inlinable = false;
1860
1861   else if (inline_forbidden_p (fn))
1862     {
1863       /* See if we should warn about uninlinable functions.  Previously,
1864          some of these warnings would be issued while trying to expand
1865          the function inline, but that would cause multiple warnings
1866          about functions that would for example call alloca.  But since
1867          this a property of the function, just one warning is enough.
1868          As a bonus we can now give more details about the reason why a
1869          function is not inlinable.
1870          We only warn for functions declared `inline' by the user.  */
1871       bool do_warning = (warn_inline
1872                          && DECL_INLINE (fn)
1873                          && DECL_DECLARED_INLINE_P (fn)
1874                          && !DECL_IN_SYSTEM_HEADER (fn));
1875
1876       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1877         sorry (inline_forbidden_reason, fn);
1878       else if (do_warning)
1879         warning (OPT_Winline, inline_forbidden_reason, fn);
1880
1881       inlinable = false;
1882     }
1883
1884   /* Squirrel away the result so that we don't have to check again.  */
1885   DECL_UNINLINABLE (fn) = !inlinable;
1886
1887   return inlinable;
1888 }
1889
1890 /* Estimate the cost of a memory move.  Use machine dependent
1891    word size and take possible memcpy call into account.  */
1892
1893 int
1894 estimate_move_cost (tree type)
1895 {
1896   HOST_WIDE_INT size;
1897
1898   size = int_size_in_bytes (type);
1899
1900   if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1901     /* Cost of a memcpy call, 3 arguments and the call.  */
1902     return 4;
1903   else
1904     return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1905 }
1906
1907 /* Used by estimate_num_insns.  Estimate number of instructions seen
1908    by given statement.  */
1909
1910 static tree
1911 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
1912 {
1913   int *count = (int *) data;
1914   tree x = *tp;
1915
1916   if (IS_TYPE_OR_DECL_P (x))
1917     {
1918       *walk_subtrees = 0;
1919       return NULL;
1920     }
1921   /* Assume that constants and references counts nothing.  These should
1922      be majorized by amount of operations among them we count later
1923      and are common target of CSE and similar optimizations.  */
1924   else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1925     return NULL;
1926
1927   switch (TREE_CODE (x))
1928     {
1929     /* Containers have no cost.  */
1930     case TREE_LIST:
1931     case TREE_VEC:
1932     case BLOCK:
1933     case COMPONENT_REF:
1934     case BIT_FIELD_REF:
1935     case INDIRECT_REF:
1936     case ALIGN_INDIRECT_REF:
1937     case MISALIGNED_INDIRECT_REF:
1938     case ARRAY_REF:
1939     case ARRAY_RANGE_REF:
1940     case OBJ_TYPE_REF:
1941     case EXC_PTR_EXPR: /* ??? */
1942     case FILTER_EXPR: /* ??? */
1943     case COMPOUND_EXPR:
1944     case BIND_EXPR:
1945     case WITH_CLEANUP_EXPR:
1946     case NOP_EXPR:
1947     case VIEW_CONVERT_EXPR:
1948     case SAVE_EXPR:
1949     case ADDR_EXPR:
1950     case COMPLEX_EXPR:
1951     case RANGE_EXPR:
1952     case CASE_LABEL_EXPR:
1953     case SSA_NAME:
1954     case CATCH_EXPR:
1955     case EH_FILTER_EXPR:
1956     case STATEMENT_LIST:
1957     case ERROR_MARK:
1958     case NON_LVALUE_EXPR:
1959     case FDESC_EXPR:
1960     case VA_ARG_EXPR:
1961     case TRY_CATCH_EXPR:
1962     case TRY_FINALLY_EXPR:
1963     case LABEL_EXPR:
1964     case GOTO_EXPR:
1965     case RETURN_EXPR:
1966     case EXIT_EXPR:
1967     case LOOP_EXPR:
1968     case PHI_NODE:
1969     case WITH_SIZE_EXPR:
1970     case OMP_CLAUSE:
1971     case OMP_RETURN:
1972     case OMP_CONTINUE:
1973       break;
1974
1975     /* We don't account constants for now.  Assume that the cost is amortized
1976        by operations that do use them.  We may re-consider this decision once
1977        we are able to optimize the tree before estimating its size and break
1978        out static initializers.  */
1979     case IDENTIFIER_NODE:
1980     case INTEGER_CST:
1981     case REAL_CST:
1982     case COMPLEX_CST:
1983     case VECTOR_CST:
1984     case STRING_CST:
1985       *walk_subtrees = 0;
1986       return NULL;
1987
1988     /* Try to estimate the cost of assignments.  We have three cases to
1989        deal with:
1990         1) Simple assignments to registers;
1991         2) Stores to things that must live in memory.  This includes
1992            "normal" stores to scalars, but also assignments of large
1993            structures, or constructors of big arrays;
1994         3) TARGET_EXPRs.
1995
1996        Let us look at the first two cases, assuming we have "a = b + C":
1997        <GIMPLE_MODIFY_STMT <var_decl "a">
1998                            <plus_expr <var_decl "b"> <constant C>>
1999        If "a" is a GIMPLE register, the assignment to it is free on almost
2000        any target, because "a" usually ends up in a real register.  Hence
2001        the only cost of this expression comes from the PLUS_EXPR, and we
2002        can ignore the GIMPLE_MODIFY_STMT.
2003        If "a" is not a GIMPLE register, the assignment to "a" will most
2004        likely be a real store, so the cost of the GIMPLE_MODIFY_STMT is the cost
2005        of moving something into "a", which we compute using the function
2006        estimate_move_cost.
2007
2008        The third case deals with TARGET_EXPRs, for which the semantics are
2009        that a temporary is assigned, unless the TARGET_EXPR itself is being
2010        assigned to something else.  In the latter case we do not need the
2011        temporary.  E.g. in:
2012                 <GIMPLE_MODIFY_STMT <var_decl "a"> <target_expr>>, the
2013        GIMPLE_MODIFY_STMT is free.  */
2014     case INIT_EXPR:
2015     case GIMPLE_MODIFY_STMT:
2016       /* Is the right and side a TARGET_EXPR?  */
2017       if (TREE_CODE (GENERIC_TREE_OPERAND (x, 1)) == TARGET_EXPR)
2018         break;
2019       /* ... fall through ...  */
2020
2021     case TARGET_EXPR:
2022       x = GENERIC_TREE_OPERAND (x, 0);
2023       /* Is this an assignments to a register?  */
2024       if (is_gimple_reg (x))
2025         break;
2026       /* Otherwise it's a store, so fall through to compute the move cost.  */
2027
2028     case CONSTRUCTOR:
2029       *count += estimate_move_cost (TREE_TYPE (x));
2030       break;
2031
2032     /* Assign cost of 1 to usual operations.
2033        ??? We may consider mapping RTL costs to this.  */
2034     case COND_EXPR:
2035     case VEC_COND_EXPR:
2036
2037     case PLUS_EXPR:
2038     case MINUS_EXPR:
2039     case MULT_EXPR:
2040
2041     case FIX_TRUNC_EXPR:
2042
2043     case NEGATE_EXPR:
2044     case FLOAT_EXPR:
2045     case MIN_EXPR:
2046     case MAX_EXPR:
2047     case ABS_EXPR:
2048
2049     case LSHIFT_EXPR:
2050     case RSHIFT_EXPR:
2051     case LROTATE_EXPR:
2052     case RROTATE_EXPR:
2053     case VEC_LSHIFT_EXPR:
2054     case VEC_RSHIFT_EXPR:
2055
2056     case BIT_IOR_EXPR:
2057     case BIT_XOR_EXPR:
2058     case BIT_AND_EXPR:
2059     case BIT_NOT_EXPR:
2060
2061     case TRUTH_ANDIF_EXPR:
2062     case TRUTH_ORIF_EXPR:
2063     case TRUTH_AND_EXPR:
2064     case TRUTH_OR_EXPR:
2065     case TRUTH_XOR_EXPR:
2066     case TRUTH_NOT_EXPR:
2067
2068     case LT_EXPR:
2069     case LE_EXPR:
2070     case GT_EXPR:
2071     case GE_EXPR:
2072     case EQ_EXPR:
2073     case NE_EXPR:
2074     case ORDERED_EXPR:
2075     case UNORDERED_EXPR:
2076
2077     case UNLT_EXPR:
2078     case UNLE_EXPR:
2079     case UNGT_EXPR:
2080     case UNGE_EXPR:
2081     case UNEQ_EXPR:
2082     case LTGT_EXPR:
2083
2084     case CONVERT_EXPR:
2085
2086     case CONJ_EXPR:
2087
2088     case PREDECREMENT_EXPR:
2089     case PREINCREMENT_EXPR:
2090     case POSTDECREMENT_EXPR:
2091     case POSTINCREMENT_EXPR:
2092
2093     case SWITCH_EXPR:
2094
2095     case ASM_EXPR:
2096
2097     case REALIGN_LOAD_EXPR:
2098
2099     case REDUC_MAX_EXPR:
2100     case REDUC_MIN_EXPR:
2101     case REDUC_PLUS_EXPR:
2102     case WIDEN_SUM_EXPR:
2103     case DOT_PROD_EXPR: 
2104     case VEC_WIDEN_MULT_HI_EXPR:
2105     case VEC_WIDEN_MULT_LO_EXPR:
2106     case VEC_UNPACK_HI_EXPR:
2107     case VEC_UNPACK_LO_EXPR:
2108     case VEC_PACK_MOD_EXPR:
2109     case VEC_PACK_SAT_EXPR:
2110
2111     case WIDEN_MULT_EXPR:
2112
2113     case VEC_EXTRACT_EVEN_EXPR:
2114     case VEC_EXTRACT_ODD_EXPR:
2115     case VEC_INTERLEAVE_HIGH_EXPR:
2116     case VEC_INTERLEAVE_LOW_EXPR:
2117
2118     case RESX_EXPR:
2119       *count += 1;
2120       break;
2121
2122     /* Few special cases of expensive operations.  This is useful
2123        to avoid inlining on functions having too many of these.  */
2124     case TRUNC_DIV_EXPR:
2125     case CEIL_DIV_EXPR:
2126     case FLOOR_DIV_EXPR:
2127     case ROUND_DIV_EXPR:
2128     case EXACT_DIV_EXPR:
2129     case TRUNC_MOD_EXPR:
2130     case CEIL_MOD_EXPR:
2131     case FLOOR_MOD_EXPR:
2132     case ROUND_MOD_EXPR:
2133     case RDIV_EXPR:
2134       *count += 10;
2135       break;
2136     case CALL_EXPR:
2137       {
2138         tree decl = get_callee_fndecl (x);
2139         tree arg;
2140
2141         if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2142           switch (DECL_FUNCTION_CODE (decl))
2143             {
2144             case BUILT_IN_CONSTANT_P:
2145               *walk_subtrees = 0;
2146               return NULL_TREE;
2147             case BUILT_IN_EXPECT:
2148               return NULL_TREE;
2149             default:
2150               break;
2151             }
2152
2153         /* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
2154            that does use function declaration to figure out the arguments.  */
2155         if (!decl)
2156           {
2157             for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
2158               *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
2159           }
2160         else
2161           {
2162             for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2163               *count += estimate_move_cost (TREE_TYPE (arg));
2164           }
2165
2166         *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
2167         break;
2168       }
2169
2170     case OMP_PARALLEL:
2171     case OMP_FOR:
2172     case OMP_SECTIONS:
2173     case OMP_SINGLE:
2174     case OMP_SECTION:
2175     case OMP_MASTER:
2176     case OMP_ORDERED:
2177     case OMP_CRITICAL:
2178     case OMP_ATOMIC:
2179       /* OpenMP directives are generally very expensive.  */
2180       *count += 40;
2181       break;
2182
2183     default:
2184       gcc_unreachable ();
2185     }
2186   return NULL;
2187 }
2188
2189 /* Estimate number of instructions that will be created by expanding EXPR.  */
2190
2191 int
2192 estimate_num_insns (tree expr)
2193 {
2194   int num = 0;
2195   struct pointer_set_t *visited_nodes;
2196   basic_block bb;
2197   block_stmt_iterator bsi;
2198   struct function *my_function;
2199
2200   /* If we're given an entire function, walk the CFG.  */
2201   if (TREE_CODE (expr) == FUNCTION_DECL)
2202     {
2203       my_function = DECL_STRUCT_FUNCTION (expr);
2204       gcc_assert (my_function && my_function->cfg);
2205       visited_nodes = pointer_set_create ();
2206       FOR_EACH_BB_FN (bb, my_function)
2207         {
2208           for (bsi = bsi_start (bb);
2209                !bsi_end_p (bsi);
2210                bsi_next (&bsi))
2211             {
2212               walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
2213                          &num, visited_nodes);
2214             }
2215         }
2216       pointer_set_destroy (visited_nodes);
2217     }
2218   else
2219     walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
2220
2221   return num;
2222 }
2223
2224 typedef struct function *function_p;
2225
2226 DEF_VEC_P(function_p);
2227 DEF_VEC_ALLOC_P(function_p,heap);
2228
2229 /* Initialized with NOGC, making this poisonous to the garbage collector.  */
2230 static VEC(function_p,heap) *cfun_stack;
2231
2232 void
2233 push_cfun (struct function *new_cfun)
2234 {
2235   VEC_safe_push (function_p, heap, cfun_stack, cfun);
2236   cfun = new_cfun;
2237 }
2238
2239 void
2240 pop_cfun (void)
2241 {
2242   cfun = VEC_pop (function_p, cfun_stack);
2243 }
2244
2245 /* Install new lexical TREE_BLOCK underneath 'current_block'.  */
2246 static void
2247 add_lexical_block (tree current_block, tree new_block)
2248 {
2249   tree *blk_p;
2250
2251   /* Walk to the last sub-block.  */
2252   for (blk_p = &BLOCK_SUBBLOCKS (current_block);
2253        *blk_p;
2254        blk_p = &TREE_CHAIN (*blk_p))
2255     ;
2256   *blk_p = new_block;
2257   BLOCK_SUPERCONTEXT (new_block) = current_block;
2258 }
2259
2260 /* If *TP is a CALL_EXPR, replace it with its inline expansion.  */
2261
2262 static bool
2263 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
2264 {
2265   copy_body_data *id;
2266   tree t;
2267   tree use_retvar;
2268   tree fn;
2269   splay_tree st;
2270   tree args;
2271   tree return_slot;
2272   tree modify_dest;
2273   location_t saved_location;
2274   struct cgraph_edge *cg_edge;
2275   const char *reason;
2276   basic_block return_block;
2277   edge e;
2278   block_stmt_iterator bsi, stmt_bsi;
2279   bool successfully_inlined = FALSE;
2280   bool purge_dead_abnormal_edges;
2281   tree t_step;
2282   tree var;
2283
2284   /* See what we've got.  */
2285   id = (copy_body_data *) data;
2286   t = *tp;
2287
2288   /* Set input_location here so we get the right instantiation context
2289      if we call instantiate_decl from inlinable_function_p.  */
2290   saved_location = input_location;
2291   if (EXPR_HAS_LOCATION (t))
2292     input_location = EXPR_LOCATION (t);
2293
2294   /* From here on, we're only interested in CALL_EXPRs.  */
2295   if (TREE_CODE (t) != CALL_EXPR)
2296     goto egress;
2297
2298   /* First, see if we can figure out what function is being called.
2299      If we cannot, then there is no hope of inlining the function.  */
2300   fn = get_callee_fndecl (t);
2301   if (!fn)
2302     goto egress;
2303
2304   /* Turn forward declarations into real ones.  */
2305   fn = cgraph_node (fn)->decl;
2306
2307   /* If fn is a declaration of a function in a nested scope that was
2308      globally declared inline, we don't set its DECL_INITIAL.
2309      However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
2310      C++ front-end uses it for cdtors to refer to their internal
2311      declarations, that are not real functions.  Fortunately those
2312      don't have trees to be saved, so we can tell by checking their
2313      DECL_SAVED_TREE.  */
2314   if (! DECL_INITIAL (fn)
2315       && DECL_ABSTRACT_ORIGIN (fn)
2316       && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
2317     fn = DECL_ABSTRACT_ORIGIN (fn);
2318
2319   /* Objective C and fortran still calls tree_rest_of_compilation directly.
2320      Kill this check once this is fixed.  */
2321   if (!id->dst_node->analyzed)
2322     goto egress;
2323
2324   cg_edge = cgraph_edge (id->dst_node, stmt);
2325
2326   /* Constant propagation on argument done during previous inlining
2327      may create new direct call.  Produce an edge for it.  */
2328   if (!cg_edge)
2329     {
2330       struct cgraph_node *dest = cgraph_node (fn);
2331
2332       /* We have missing edge in the callgraph.  This can happen in one case
2333          where previous inlining turned indirect call into direct call by
2334          constant propagating arguments.  In all other cases we hit a bug
2335          (incorrect node sharing is most common reason for missing edges.  */
2336       gcc_assert (dest->needed || !flag_unit_at_a_time);
2337       cgraph_create_edge (id->dst_node, dest, stmt,
2338                           bb->count, bb->loop_depth)->inline_failed
2339         = N_("originally indirect function call not considered for inlining");
2340       goto egress;
2341     }
2342
2343   /* Don't try to inline functions that are not well-suited to
2344      inlining.  */
2345   if (!cgraph_inline_p (cg_edge, &reason))
2346     {
2347       if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
2348           /* Avoid warnings during early inline pass. */
2349           && (!flag_unit_at_a_time || cgraph_global_info_ready))
2350         {
2351           sorry ("inlining failed in call to %q+F: %s", fn, reason);
2352           sorry ("called from here");
2353         }
2354       else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
2355                && !DECL_IN_SYSTEM_HEADER (fn)
2356                && strlen (reason)
2357                && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
2358                /* Avoid warnings during early inline pass. */
2359                && (!flag_unit_at_a_time || cgraph_global_info_ready))
2360         {
2361           warning (OPT_Winline, "inlining failed in call to %q+F: %s",
2362                    fn, reason);
2363           warning (OPT_Winline, "called from here");
2364         }
2365       goto egress;
2366     }
2367   fn = cg_edge->callee->decl;
2368
2369 #ifdef ENABLE_CHECKING
2370   if (cg_edge->callee->decl != id->dst_node->decl)
2371     verify_cgraph_node (cg_edge->callee);
2372 #endif
2373
2374   /* We will be inlining this callee.  */
2375   id->eh_region = lookup_stmt_eh_region (stmt);
2376
2377   /* Split the block holding the CALL_EXPR.  */
2378   e = split_block (bb, stmt);
2379   bb = e->src;
2380   return_block = e->dest;
2381   remove_edge (e);
2382
2383   /* split_block splits after the statement; work around this by
2384      moving the call into the second block manually.  Not pretty,
2385      but seems easier than doing the CFG manipulation by hand
2386      when the CALL_EXPR is in the last statement of BB.  */
2387   stmt_bsi = bsi_last (bb);
2388   bsi_remove (&stmt_bsi, false);
2389
2390   /* If the CALL_EXPR was in the last statement of BB, it may have
2391      been the source of abnormal edges.  In this case, schedule
2392      the removal of dead abnormal edges.  */
2393   bsi = bsi_start (return_block);
2394   if (bsi_end_p (bsi))
2395     {
2396       bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2397       purge_dead_abnormal_edges = true;
2398     }
2399   else
2400     {
2401       bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2402       purge_dead_abnormal_edges = false;
2403     }
2404
2405   stmt_bsi = bsi_start (return_block);
2406
2407   /* Build a block containing code to initialize the arguments, the
2408      actual inline expansion of the body, and a label for the return
2409      statements within the function to jump to.  The type of the
2410      statement expression is the return type of the function call.  */
2411   id->block = make_node (BLOCK);
2412   BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2413   BLOCK_SOURCE_LOCATION (id->block) = input_location;
2414   add_lexical_block (TREE_BLOCK (stmt), id->block);
2415
2416   /* Local declarations will be replaced by their equivalents in this
2417      map.  */
2418   st = id->decl_map;
2419   id->decl_map = splay_tree_new (splay_tree_compare_pointers,
2420                                  NULL, NULL);
2421
2422   /* Initialize the parameters.  */
2423   args = TREE_OPERAND (t, 1);
2424
2425   /* Record the function we are about to inline.  */
2426   id->src_fn = fn;
2427   id->src_node = cg_edge->callee;
2428   id->src_cfun = DECL_STRUCT_FUNCTION (fn);
2429
2430   initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2431
2432   if (DECL_INITIAL (fn))
2433     add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
2434
2435   /* Return statements in the function body will be replaced by jumps
2436      to the RET_LABEL.  */
2437
2438   gcc_assert (DECL_INITIAL (fn));
2439   gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
2440
2441   /* Find the lhs to which the result of this call is assigned.  */
2442   return_slot = NULL;
2443   if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
2444     {
2445       modify_dest = GIMPLE_STMT_OPERAND (stmt, 0);
2446
2447       /* The function which we are inlining might not return a value,
2448          in which case we should issue a warning that the function
2449          does not return a value.  In that case the optimizers will
2450          see that the variable to which the value is assigned was not
2451          initialized.  We do not want to issue a warning about that
2452          uninitialized variable.  */
2453       if (DECL_P (modify_dest))
2454         TREE_NO_WARNING (modify_dest) = 1;
2455       if (CALL_EXPR_RETURN_SLOT_OPT (t))
2456         {
2457           return_slot = modify_dest;
2458           modify_dest = NULL;
2459         }
2460     }
2461   else
2462     modify_dest = NULL;
2463
2464   /* Declare the return variable for the function.  */
2465   declare_return_variable (id, return_slot,
2466                            modify_dest, &use_retvar);
2467
2468   /* This is it.  Duplicate the callee body.  Assume callee is
2469      pre-gimplified.  Note that we must not alter the caller
2470      function in any way before this point, as this CALL_EXPR may be
2471      a self-referential call; if we're calling ourselves, we need to
2472      duplicate our body before altering anything.  */
2473   copy_body (id, bb->count, bb->frequency, bb, return_block);
2474
2475   /* Add local vars in this inlined callee to caller.  */
2476   t_step = id->src_cfun->unexpanded_var_list;
2477   for (; t_step; t_step = TREE_CHAIN (t_step))
2478     {
2479       var = TREE_VALUE (t_step);
2480       if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2481         cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2482                                                cfun->unexpanded_var_list);
2483       else
2484         cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2485                                                cfun->unexpanded_var_list);
2486     }
2487
2488   /* Clean up.  */
2489   splay_tree_delete (id->decl_map);
2490   id->decl_map = st;
2491
2492   /* If the inlined function returns a result that we care about,
2493      clobber the CALL_EXPR with a reference to the return variable.  */
2494   if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2495     {
2496       *tp = use_retvar;
2497       if (gimple_in_ssa_p (cfun))
2498         {
2499           update_stmt (stmt);
2500           mark_symbols_for_renaming (stmt);
2501         }
2502       maybe_clean_or_replace_eh_stmt (stmt, stmt);
2503     }
2504   else
2505     /* We're modifying a TSI owned by gimple_expand_calls_inline();
2506        tsi_delink() will leave the iterator in a sane state.  */
2507     {
2508       /* Handle case of inlining function that miss return statement so 
2509          return value becomes undefined.  */
2510       if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
2511           && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME)
2512         {
2513           tree name = TREE_OPERAND (stmt, 0);
2514           tree var = SSA_NAME_VAR (TREE_OPERAND (stmt, 0));
2515           tree def = gimple_default_def (cfun, var);
2516
2517           /* If the variable is used undefined, make this name undefined via
2518              move.  */
2519           if (def)
2520             {
2521               TREE_OPERAND (stmt, 1) = def;
2522               update_stmt (stmt);
2523             }
2524           /* Otherwise make this variable undefined.  */
2525           else
2526             {
2527               bsi_remove (&stmt_bsi, true);
2528               set_default_def (var, name);
2529               SSA_NAME_DEF_STMT (name) = build_empty_stmt ();
2530             }
2531         }
2532       else
2533         bsi_remove (&stmt_bsi, true);
2534     }
2535
2536   if (purge_dead_abnormal_edges)
2537     tree_purge_dead_abnormal_call_edges (return_block);
2538
2539   /* If the value of the new expression is ignored, that's OK.  We
2540      don't warn about this for CALL_EXPRs, so we shouldn't warn about
2541      the equivalent inlined version either.  */
2542   TREE_USED (*tp) = 1;
2543
2544   /* Output the inlining info for this abstract function, since it has been
2545      inlined.  If we don't do this now, we can lose the information about the
2546      variables in the function when the blocks get blown away as soon as we
2547      remove the cgraph node.  */
2548   (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2549
2550   /* Update callgraph if needed.  */
2551   cgraph_remove_node (cg_edge->callee);
2552
2553   id->block = NULL_TREE;
2554   successfully_inlined = TRUE;
2555
2556  egress:
2557   input_location = saved_location;
2558   return successfully_inlined;
2559 }
2560
2561 /* Expand call statements reachable from STMT_P.
2562    We can only have CALL_EXPRs as the "toplevel" tree code or nested
2563    in a GIMPLE_MODIFY_STMT.  See tree-gimple.c:get_call_expr_in().  We can
2564    unfortunately not use that function here because we need a pointer
2565    to the CALL_EXPR, not the tree itself.  */
2566
2567 static bool
2568 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
2569 {
2570   block_stmt_iterator bsi;
2571
2572   /* Register specific tree functions.  */
2573   tree_register_cfg_hooks ();
2574   for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2575     {
2576       tree *expr_p = bsi_stmt_ptr (bsi);
2577       tree stmt = *expr_p;
2578
2579       if (TREE_CODE (*expr_p) == GIMPLE_MODIFY_STMT)
2580         expr_p = &GIMPLE_STMT_OPERAND (*expr_p, 1);
2581       if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2582         expr_p = &TREE_OPERAND (*expr_p, 0);
2583       if (TREE_CODE (*expr_p) == CALL_EXPR)
2584         if (expand_call_inline (bb, stmt, expr_p, id))
2585           return true;
2586     }
2587   return false;
2588 }
2589
2590 /* Walk all basic blocks created after FIRST and try to fold every statement
2591    in the STATEMENTS pointer set.  */
2592 static void
2593 fold_marked_statements (int first, struct pointer_set_t *statements)
2594 {
2595   for (;first < n_basic_blocks;first++)
2596     if (BASIC_BLOCK (first))
2597       {
2598         block_stmt_iterator bsi;
2599         for (bsi = bsi_start (BASIC_BLOCK (first));
2600              !bsi_end_p (bsi); bsi_next (&bsi))
2601           if (pointer_set_contains (statements, bsi_stmt (bsi)))
2602             {
2603               tree old_stmt = bsi_stmt (bsi);
2604               if (fold_stmt (bsi_stmt_ptr (bsi)))
2605                 {
2606                   update_stmt (bsi_stmt (bsi));
2607                   if (maybe_clean_or_replace_eh_stmt (old_stmt, bsi_stmt (bsi)))
2608                      tree_purge_dead_eh_edges (BASIC_BLOCK (first));
2609                 }
2610             }
2611       }
2612 }
2613
2614 /* Expand calls to inline functions in the body of FN.  */
2615
2616 void
2617 optimize_inline_calls (tree fn)
2618 {
2619   copy_body_data id;
2620   tree prev_fn;
2621   basic_block bb;
2622   int last = n_basic_blocks;
2623   /* There is no point in performing inlining if errors have already
2624      occurred -- and we might crash if we try to inline invalid
2625      code.  */
2626   if (errorcount || sorrycount)
2627     return;
2628
2629   /* Clear out ID.  */
2630   memset (&id, 0, sizeof (id));
2631
2632   id.src_node = id.dst_node = cgraph_node (fn);
2633   id.dst_fn = fn;
2634   /* Or any functions that aren't finished yet.  */
2635   prev_fn = NULL_TREE;
2636   if (current_function_decl)
2637     {
2638       id.dst_fn = current_function_decl;
2639       prev_fn = current_function_decl;
2640     }
2641
2642   id.copy_decl = copy_decl_maybe_to_var;
2643   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2644   id.transform_new_cfg = false;
2645   id.transform_return_to_modify = true;
2646   id.transform_lang_insert_block = false;
2647   id.statements_to_fold = pointer_set_create ();
2648
2649   push_gimplify_context ();
2650
2651   /* Reach the trees by walking over the CFG, and note the
2652      enclosing basic-blocks in the call edges.  */
2653   /* We walk the blocks going forward, because inlined function bodies
2654      will split id->current_basic_block, and the new blocks will
2655      follow it; we'll trudge through them, processing their CALL_EXPRs
2656      along the way.  */
2657   FOR_EACH_BB (bb)
2658     gimple_expand_calls_inline (bb, &id);
2659
2660   pop_gimplify_context (NULL);
2661   /* Renumber the (code) basic_blocks consecutively.  */
2662   compact_blocks ();
2663   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
2664   number_blocks (fn);
2665
2666 #ifdef ENABLE_CHECKING
2667     {
2668       struct cgraph_edge *e;
2669
2670       verify_cgraph_node (id.dst_node);
2671
2672       /* Double check that we inlined everything we are supposed to inline.  */
2673       for (e = id.dst_node->callees; e; e = e->next_callee)
2674         gcc_assert (e->inline_failed);
2675     }
2676 #endif
2677   /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2678      as inlining loops might increase the maximum.  */
2679   if (ENTRY_BLOCK_PTR->count)
2680     counts_to_freqs ();
2681
2682   fold_marked_statements (last, id.statements_to_fold);
2683   pointer_set_destroy (id.statements_to_fold);
2684   if (gimple_in_ssa_p (cfun))
2685     {
2686       /* We make no attempts to keep dominance info up-to-date.  */
2687       free_dominance_info (CDI_DOMINATORS);
2688       free_dominance_info (CDI_POST_DOMINATORS);
2689       delete_unreachable_blocks ();
2690       update_ssa (TODO_update_ssa);
2691       fold_cond_expr_cond ();
2692       if (need_ssa_update_p ())
2693         update_ssa (TODO_update_ssa);
2694     }
2695   else
2696     fold_cond_expr_cond ();
2697   /* It would be nice to check SSA/CFG/statement consistency here, but it is
2698      not possible yet - the IPA passes might make various functions to not
2699      throw and they don't care to proactively update local EH info.  This is
2700      done later in fixup_cfg pass that also execute the verification.  */
2701 }
2702
2703 /* FN is a function that has a complete body, and CLONE is a function whose
2704    body is to be set to a copy of FN, mapping argument declarations according
2705    to the ARG_MAP splay_tree.  */
2706
2707 void
2708 clone_body (tree clone, tree fn, void *arg_map)
2709 {
2710   copy_body_data id;
2711
2712   /* Clone the body, as if we were making an inline call.  But, remap the
2713      parameters in the callee to the parameters of caller.  */
2714   memset (&id, 0, sizeof (id));
2715   id.src_fn = fn;
2716   id.dst_fn = clone;
2717   id.src_cfun = DECL_STRUCT_FUNCTION (fn);
2718   id.decl_map = (splay_tree)arg_map;
2719
2720   id.copy_decl = copy_decl_no_change;
2721   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2722   id.transform_new_cfg = true;
2723   id.transform_return_to_modify = false;
2724   id.transform_lang_insert_block = true;
2725
2726   /* We're not inside any EH region.  */
2727   id.eh_region = -1;
2728
2729   /* Actually copy the body.  */
2730   append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
2731 }
2732
2733 /* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
2734
2735 tree
2736 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
2737 {
2738   enum tree_code code = TREE_CODE (*tp);
2739   enum tree_code_class cl = TREE_CODE_CLASS (code);
2740
2741   /* We make copies of most nodes.  */
2742   if (IS_EXPR_CODE_CLASS (cl)
2743       || IS_GIMPLE_STMT_CODE_CLASS (cl)
2744       || code == TREE_LIST
2745       || code == TREE_VEC
2746       || code == TYPE_DECL
2747       || code == OMP_CLAUSE)
2748     {
2749       /* Because the chain gets clobbered when we make a copy, we save it
2750          here.  */
2751       tree chain = NULL_TREE, new;
2752
2753       if (!GIMPLE_TUPLE_P (*tp))
2754         chain = TREE_CHAIN (*tp);
2755
2756       /* Copy the node.  */
2757       new = copy_node (*tp);
2758
2759       /* Propagate mudflap marked-ness.  */
2760       if (flag_mudflap && mf_marked_p (*tp))
2761         mf_mark (new);
2762
2763       *tp = new;
2764
2765       /* Now, restore the chain, if appropriate.  That will cause
2766          walk_tree to walk into the chain as well.  */
2767       if (code == PARM_DECL
2768           || code == TREE_LIST
2769           || code == OMP_CLAUSE)
2770         TREE_CHAIN (*tp) = chain;
2771
2772       /* For now, we don't update BLOCKs when we make copies.  So, we
2773          have to nullify all BIND_EXPRs.  */
2774       if (TREE_CODE (*tp) == BIND_EXPR)
2775         BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2776     }
2777   else if (code == CONSTRUCTOR)
2778     {
2779       /* CONSTRUCTOR nodes need special handling because
2780          we need to duplicate the vector of elements.  */
2781       tree new;
2782
2783       new = copy_node (*tp);
2784
2785       /* Propagate mudflap marked-ness.  */
2786       if (flag_mudflap && mf_marked_p (*tp))
2787         mf_mark (new);
2788
2789       CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2790                                          CONSTRUCTOR_ELTS (*tp));
2791       *tp = new;
2792     }
2793   else if (TREE_CODE_CLASS (code) == tcc_type)
2794     *walk_subtrees = 0;
2795   else if (TREE_CODE_CLASS (code) == tcc_declaration)
2796     *walk_subtrees = 0;
2797   else if (TREE_CODE_CLASS (code) == tcc_constant)
2798     *walk_subtrees = 0;
2799   else
2800     gcc_assert (code != STATEMENT_LIST);
2801   return NULL_TREE;
2802 }
2803
2804 /* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
2805    information indicating to what new SAVE_EXPR this one should be mapped,
2806    use that one.  Otherwise, create a new node and enter it in ST.  FN is
2807    the function into which the copy will be placed.  */
2808
2809 static void
2810 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2811 {
2812   splay_tree st = (splay_tree) st_;
2813   splay_tree_node n;
2814   tree t;
2815
2816   /* See if we already encountered this SAVE_EXPR.  */
2817   n = splay_tree_lookup (st, (splay_tree_key) *tp);
2818
2819   /* If we didn't already remap this SAVE_EXPR, do so now.  */
2820   if (!n)
2821     {
2822       t = copy_node (*tp);
2823
2824       /* Remember this SAVE_EXPR.  */
2825       splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2826       /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
2827       splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
2828     }
2829   else
2830     {
2831       /* We've already walked into this SAVE_EXPR; don't do it again.  */
2832       *walk_subtrees = 0;
2833       t = (tree) n->value;
2834     }
2835
2836   /* Replace this SAVE_EXPR with the copy.  */
2837   *tp = t;
2838 }
2839
2840 /* Called via walk_tree.  If *TP points to a DECL_STMT for a local label,
2841    copies the declaration and enters it in the splay_tree in DATA (which is
2842    really an `copy_body_data *').  */
2843
2844 static tree
2845 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2846                         void *data)
2847 {
2848   copy_body_data *id = (copy_body_data *) data;
2849
2850   /* Don't walk into types.  */
2851   if (TYPE_P (*tp))
2852     *walk_subtrees = 0;
2853
2854   else if (TREE_CODE (*tp) == LABEL_EXPR)
2855     {
2856       tree decl = TREE_OPERAND (*tp, 0);
2857
2858       /* Copy the decl and remember the copy.  */
2859       insert_decl_map (id, decl, id->copy_decl (decl, id));
2860     }
2861
2862   return NULL_TREE;
2863 }
2864
2865 /* Perform any modifications to EXPR required when it is unsaved.  Does
2866    not recurse into EXPR's subtrees.  */
2867
2868 static void
2869 unsave_expr_1 (tree expr)
2870 {
2871   switch (TREE_CODE (expr))
2872     {
2873     case TARGET_EXPR:
2874       /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2875          It's OK for this to happen if it was part of a subtree that
2876          isn't immediately expanded, such as operand 2 of another
2877          TARGET_EXPR.  */
2878       if (TREE_OPERAND (expr, 1))
2879         break;
2880
2881       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2882       TREE_OPERAND (expr, 3) = NULL_TREE;
2883       break;
2884
2885     default:
2886       break;
2887     }
2888 }
2889
2890 /* Called via walk_tree when an expression is unsaved.  Using the
2891    splay_tree pointed to by ST (which is really a `splay_tree'),
2892    remaps all local declarations to appropriate replacements.  */
2893
2894 static tree
2895 unsave_r (tree *tp, int *walk_subtrees, void *data)
2896 {
2897   copy_body_data *id = (copy_body_data *) data;
2898   splay_tree st = id->decl_map;
2899   splay_tree_node n;
2900
2901   /* Only a local declaration (variable or label).  */
2902   if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2903       || TREE_CODE (*tp) == LABEL_DECL)
2904     {
2905       /* Lookup the declaration.  */
2906       n = splay_tree_lookup (st, (splay_tree_key) *tp);
2907
2908       /* If it's there, remap it.  */
2909       if (n)
2910         *tp = (tree) n->value;
2911     }
2912
2913   else if (TREE_CODE (*tp) == STATEMENT_LIST)
2914     copy_statement_list (tp);
2915   else if (TREE_CODE (*tp) == BIND_EXPR)
2916     copy_bind_expr (tp, walk_subtrees, id);
2917   else if (TREE_CODE (*tp) == SAVE_EXPR)
2918     remap_save_expr (tp, st, walk_subtrees);
2919   else
2920     {
2921       copy_tree_r (tp, walk_subtrees, NULL);
2922
2923       /* Do whatever unsaving is required.  */
2924       unsave_expr_1 (*tp);
2925     }
2926
2927   /* Keep iterating.  */
2928   return NULL_TREE;
2929 }
2930
2931 /* Copies everything in EXPR and replaces variables, labels
2932    and SAVE_EXPRs local to EXPR.  */
2933
2934 tree
2935 unsave_expr_now (tree expr)
2936 {
2937   copy_body_data id;
2938
2939   /* There's nothing to do for NULL_TREE.  */
2940   if (expr == 0)
2941     return expr;
2942
2943   /* Set up ID.  */
2944   memset (&id, 0, sizeof (id));
2945   id.src_fn = current_function_decl;
2946   id.dst_fn = current_function_decl;
2947   id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2948
2949   id.copy_decl = copy_decl_no_change;
2950   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2951   id.transform_new_cfg = false;
2952   id.transform_return_to_modify = false;
2953   id.transform_lang_insert_block = false;
2954
2955   /* Walk the tree once to find local labels.  */
2956   walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2957
2958   /* Walk the tree again, copying, remapping, and unsaving.  */
2959   walk_tree (&expr, unsave_r, &id, NULL);
2960
2961   /* Clean up.  */
2962   splay_tree_delete (id.decl_map);
2963
2964   return expr;
2965 }
2966
2967 /* Allow someone to determine if SEARCH is a child of TOP from gdb.  */
2968
2969 static tree
2970 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2971 {
2972   if (*tp == data)
2973     return (tree) data;
2974   else
2975     return NULL;
2976 }
2977
2978 bool
2979 debug_find_tree (tree top, tree search)
2980 {
2981   return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2982 }
2983
2984
2985 /* Declare the variables created by the inliner.  Add all the variables in
2986    VARS to BIND_EXPR.  */
2987
2988 static void
2989 declare_inline_vars (tree block, tree vars)
2990 {
2991   tree t;
2992   for (t = vars; t; t = TREE_CHAIN (t))
2993     {
2994       DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2995       gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
2996       cfun->unexpanded_var_list =
2997         tree_cons (NULL_TREE, t,
2998                    cfun->unexpanded_var_list);
2999     }
3000
3001   if (block)
3002     BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
3003 }
3004
3005
3006 /* Copy NODE (which must be a DECL).  The DECL originally was in the FROM_FN,
3007    but now it will be in the TO_FN.  PARM_TO_VAR means enable PARM_DECL to
3008    VAR_DECL translation.  */
3009
3010 static tree
3011 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
3012 {
3013   /* Don't generate debug information for the copy if we wouldn't have
3014      generated it for the copy either.  */
3015   DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
3016   DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
3017
3018   /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
3019      declaration inspired this copy.  */ 
3020   DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
3021
3022   /* The new variable/label has no RTL, yet.  */
3023   if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
3024       && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
3025     SET_DECL_RTL (copy, NULL_RTX);
3026   
3027   /* These args would always appear unused, if not for this.  */
3028   TREE_USED (copy) = 1;
3029
3030   /* Set the context for the new declaration.  */
3031   if (!DECL_CONTEXT (decl))
3032     /* Globals stay global.  */
3033     ;
3034   else if (DECL_CONTEXT (decl) != id->src_fn)
3035     /* Things that weren't in the scope of the function we're inlining
3036        from aren't in the scope we're inlining to, either.  */
3037     ;
3038   else if (TREE_STATIC (decl))
3039     /* Function-scoped static variables should stay in the original
3040        function.  */
3041     ;
3042   else
3043     /* Ordinary automatic local variables are now in the scope of the
3044        new function.  */
3045     DECL_CONTEXT (copy) = id->dst_fn;
3046
3047   return copy;
3048 }
3049
3050 static tree
3051 copy_decl_to_var (tree decl, copy_body_data *id)
3052 {
3053   tree copy, type;
3054
3055   gcc_assert (TREE_CODE (decl) == PARM_DECL
3056               || TREE_CODE (decl) == RESULT_DECL);
3057
3058   type = TREE_TYPE (decl);
3059
3060   copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
3061   TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
3062   TREE_READONLY (copy) = TREE_READONLY (decl);
3063   TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
3064   DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
3065
3066   return copy_decl_for_dup_finish (id, decl, copy);
3067 }
3068
3069 /* Like copy_decl_to_var, but create a return slot object instead of a
3070    pointer variable for return by invisible reference.  */
3071
3072 static tree
3073 copy_result_decl_to_var (tree decl, copy_body_data *id)
3074 {
3075   tree copy, type;
3076
3077   gcc_assert (TREE_CODE (decl) == PARM_DECL
3078               || TREE_CODE (decl) == RESULT_DECL);
3079
3080   type = TREE_TYPE (decl);
3081   if (DECL_BY_REFERENCE (decl))
3082     type = TREE_TYPE (type);
3083
3084   copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
3085   TREE_READONLY (copy) = TREE_READONLY (decl);
3086   TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
3087   if (!DECL_BY_REFERENCE (decl))
3088     {
3089       TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
3090       DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
3091     }
3092
3093   return copy_decl_for_dup_finish (id, decl, copy);
3094 }
3095
3096
3097 static tree
3098 copy_decl_no_change (tree decl, copy_body_data *id)
3099 {
3100   tree copy;
3101
3102   copy = copy_node (decl);
3103
3104   /* The COPY is not abstract; it will be generated in DST_FN.  */
3105   DECL_ABSTRACT (copy) = 0;
3106   lang_hooks.dup_lang_specific_decl (copy);
3107
3108   /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
3109      been taken; it's for internal bookkeeping in expand_goto_internal.  */
3110   if (TREE_CODE (copy) == LABEL_DECL)
3111     {
3112       TREE_ADDRESSABLE (copy) = 0;
3113       LABEL_DECL_UID (copy) = -1;
3114     }
3115
3116   return copy_decl_for_dup_finish (id, decl, copy);
3117 }
3118
3119 static tree
3120 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
3121 {
3122   if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
3123     return copy_decl_to_var (decl, id);
3124   else
3125     return copy_decl_no_change (decl, id);
3126 }
3127
3128 /* Return a copy of the function's argument tree.  */
3129 static tree
3130 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
3131 {
3132   tree *arg_copy, *parg;
3133
3134   arg_copy = &orig_parm;
3135   for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
3136     {
3137       tree new = remap_decl (*parg, id);
3138       lang_hooks.dup_lang_specific_decl (new);
3139       TREE_CHAIN (new) = TREE_CHAIN (*parg);
3140       *parg = new;
3141     }
3142   return orig_parm;
3143 }
3144
3145 /* Return a copy of the function's static chain.  */
3146 static tree
3147 copy_static_chain (tree static_chain, copy_body_data * id)
3148 {
3149   tree *chain_copy, *pvar;
3150
3151   chain_copy = &static_chain;
3152   for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
3153     {
3154       tree new = remap_decl (*pvar, id);
3155       lang_hooks.dup_lang_specific_decl (new);
3156       TREE_CHAIN (new) = TREE_CHAIN (*pvar);
3157       *pvar = new;
3158     }
3159   return static_chain;
3160 }
3161
3162 /* Return true if the function is allowed to be versioned.
3163    This is a guard for the versioning functionality.  */
3164 bool
3165 tree_versionable_function_p (tree fndecl)
3166 {
3167   if (fndecl == NULL_TREE)
3168     return false;
3169   /* ??? There are cases where a function is
3170      uninlinable but can be versioned.  */
3171   if (!tree_inlinable_function_p (fndecl))
3172     return false;
3173   
3174   return true;
3175 }
3176
3177 /* Create a copy of a function's tree.
3178    OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
3179    of the original function and the new copied function
3180    respectively.  In case we want to replace a DECL 
3181    tree with another tree while duplicating the function's 
3182    body, TREE_MAP represents the mapping between these 
3183    trees. If UPDATE_CLONES is set, the call_stmt fields
3184    of edges of clones of the function will be updated.  */
3185 void
3186 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
3187                           bool update_clones)
3188 {
3189   struct cgraph_node *old_version_node;
3190   struct cgraph_node *new_version_node;
3191   copy_body_data id;
3192   tree p;
3193   unsigned i;
3194   struct ipa_replace_map *replace_info;
3195   basic_block old_entry_block;
3196   tree t_step;
3197
3198   gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
3199               && TREE_CODE (new_decl) == FUNCTION_DECL);
3200   DECL_POSSIBLY_INLINED (old_decl) = 1;
3201
3202   old_version_node = cgraph_node (old_decl);
3203   new_version_node = cgraph_node (new_decl);
3204
3205   allocate_struct_function (new_decl);
3206   /* Cfun points to the new allocated function struct at this point.  */
3207   cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
3208
3209   DECL_ARTIFICIAL (new_decl) = 1;
3210   DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
3211
3212   /* Prepare the data structures for the tree copy.  */
3213   memset (&id, 0, sizeof (id));
3214
3215   /* Generate a new name for the new version. */
3216   if (!update_clones)
3217     {
3218       DECL_NAME (new_decl) =  create_tmp_var_name (NULL);
3219       SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
3220       SET_DECL_RTL (new_decl, NULL_RTX);
3221       id.statements_to_fold = pointer_set_create ();
3222     }
3223   
3224   id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3225   id.src_fn = old_decl;
3226   id.dst_fn = new_decl;
3227   id.src_node = old_version_node;
3228   id.dst_node = new_version_node;
3229   id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
3230   
3231   id.copy_decl = copy_decl_no_change;
3232   id.transform_call_graph_edges
3233     = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
3234   id.transform_new_cfg = true;
3235   id.transform_return_to_modify = false;
3236   id.transform_lang_insert_block = false;
3237
3238   current_function_decl = new_decl;
3239   old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
3240     (DECL_STRUCT_FUNCTION (old_decl));
3241   initialize_cfun (new_decl, old_decl,
3242                    old_entry_block->count,
3243                    old_entry_block->frequency);
3244   push_cfun (DECL_STRUCT_FUNCTION (new_decl));
3245   
3246   /* Copy the function's static chain.  */
3247   p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
3248   if (p)
3249     DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
3250       copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
3251                          &id);
3252   /* Copy the function's arguments.  */
3253   if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
3254     DECL_ARGUMENTS (new_decl) =
3255       copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
3256   
3257   /* If there's a tree_map, prepare for substitution.  */
3258   if (tree_map)
3259     for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
3260       {
3261         replace_info = VARRAY_GENERIC_PTR (tree_map, i);
3262         if (replace_info->replace_p)
3263           insert_decl_map (&id, replace_info->old_tree,
3264                            replace_info->new_tree);
3265       }
3266   
3267   DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
3268   
3269   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
3270   number_blocks (id.dst_fn);
3271   
3272   if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
3273     /* Add local vars.  */
3274     for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
3275          t_step; t_step = TREE_CHAIN (t_step))
3276       {
3277         tree var = TREE_VALUE (t_step);
3278         if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
3279           cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
3280                                                  cfun->unexpanded_var_list);
3281         else
3282           cfun->unexpanded_var_list =
3283             tree_cons (NULL_TREE, remap_decl (var, &id),
3284                        cfun->unexpanded_var_list);
3285       }
3286   
3287   /* Copy the Function's body.  */
3288   copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
3289   
3290   if (DECL_RESULT (old_decl) != NULL_TREE)
3291     {
3292       tree *res_decl = &DECL_RESULT (old_decl);
3293       DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
3294       lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
3295     }
3296   
3297   /* Renumber the lexical scoping (non-code) blocks consecutively.  */
3298   number_blocks (new_decl);
3299
3300   /* Clean up.  */
3301   splay_tree_delete (id.decl_map);
3302   if (!update_clones)
3303     {
3304       fold_marked_statements (0, id.statements_to_fold);
3305       pointer_set_destroy (id.statements_to_fold);
3306       fold_cond_expr_cond ();
3307     }
3308   if (gimple_in_ssa_p (cfun))
3309     {
3310       free_dominance_info (CDI_DOMINATORS);
3311       free_dominance_info (CDI_POST_DOMINATORS);
3312       if (!update_clones)
3313         delete_unreachable_blocks ();
3314       update_ssa (TODO_update_ssa);
3315       if (!update_clones)
3316         {
3317           fold_cond_expr_cond ();
3318           if (need_ssa_update_p ())
3319             update_ssa (TODO_update_ssa);
3320         }
3321     }
3322   free_dominance_info (CDI_DOMINATORS);
3323   free_dominance_info (CDI_POST_DOMINATORS);
3324   pop_cfun ();
3325   current_function_decl = NULL;
3326   return;
3327 }
3328
3329 /* Duplicate a type, fields and all.  */
3330
3331 tree
3332 build_duplicate_type (tree type)
3333 {
3334   struct copy_body_data id;
3335
3336   memset (&id, 0, sizeof (id));
3337   id.src_fn = current_function_decl;
3338   id.dst_fn = current_function_decl;
3339   id.src_cfun = cfun;
3340   id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
3341
3342   type = remap_type_1 (type, &id);
3343
3344   splay_tree_delete (id.decl_map);
3345
3346   return type;
3347 }