OSDN Git Service

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