1169689Skan/* Tree inlining.
2169689Skan   Copyright 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
390075Sobrien   Contributed by Alexandre Oliva <aoliva@redhat.com>
490075Sobrien
5132718SkanThis file is part of GCC.
690075Sobrien
7132718SkanGCC is free software; you can redistribute it and/or modify
890075Sobrienit under the terms of the GNU General Public License as published by
990075Sobrienthe Free Software Foundation; either version 2, or (at your option)
1090075Sobrienany later version.
1190075Sobrien
12132718SkanGCC is distributed in the hope that it will be useful,
1390075Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of
1490075SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1590075SobrienGNU General Public License for more details.
1690075Sobrien
1790075SobrienYou should have received a copy of the GNU General Public License
18132718Skanalong with GCC; see the file COPYING.  If not, write to
19169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
20169689SkanBoston, MA 02110-1301, USA.  */
2190075Sobrien
2290075Sobrien#include "config.h"
2390075Sobrien#include "system.h"
24132718Skan#include "coretypes.h"
25132718Skan#include "tm.h"
2690075Sobrien#include "toplev.h"
2790075Sobrien#include "tree.h"
2890075Sobrien#include "tree-inline.h"
2990075Sobrien#include "rtl.h"
3090075Sobrien#include "expr.h"
3190075Sobrien#include "flags.h"
3290075Sobrien#include "params.h"
3390075Sobrien#include "input.h"
3490075Sobrien#include "insn-config.h"
3590075Sobrien#include "varray.h"
3690075Sobrien#include "hashtab.h"
3790075Sobrien#include "langhooks.h"
38169689Skan#include "basic-block.h"
39169689Skan#include "tree-iterator.h"
40132718Skan#include "cgraph.h"
41132718Skan#include "intl.h"
42169689Skan#include "tree-mudflap.h"
43169689Skan#include "tree-flow.h"
44169689Skan#include "function.h"
45169689Skan#include "ggc.h"
46169689Skan#include "tree-flow.h"
47132718Skan#include "diagnostic.h"
48169689Skan#include "except.h"
49169689Skan#include "debug.h"
50169689Skan#include "pointer-set.h"
51169689Skan#include "ipa-prop.h"
5290075Sobrien
53169689Skan/* I'm not real happy about this, but we need to handle gimple and
54169689Skan   non-gimple trees.  */
55169689Skan#include "tree-gimple.h"
5690075Sobrien
57169689Skan/* Inlining, Cloning, Versioning, Parallelization
58169689Skan
59169689Skan   Inlining: a function body is duplicated, but the PARM_DECLs are
60169689Skan   remapped into VAR_DECLs, and non-void RETURN_EXPRs become
61169689Skan   MODIFY_EXPRs that store to a dedicated returned-value variable.
62169689Skan   The duplicated eh_region info of the copy will later be appended
63169689Skan   to the info for the caller; the eh_region info in copied throwing
64169689Skan   statements and RESX_EXPRs is adjusted accordingly.
65169689Skan
66169689Skan   Cloning: (only in C++) We have one body for a con/de/structor, and
67169689Skan   multiple function decls, each with a unique parameter list.
68169689Skan   Duplicate the body, using the given splay tree; some parameters
69169689Skan   will become constants (like 0 or 1).
70169689Skan
71169689Skan   Versioning: a function body is duplicated and the result is a new
72169689Skan   function rather than into blocks of an existing function as with
73169689Skan   inlining.  Some parameters will become constants.
74169689Skan
75169689Skan   Parallelization: a region of a function is duplicated resulting in
76169689Skan   a new function.  Variables may be replaced with complex expressions
77169689Skan   to enable shared variable semantics.
78169689Skan
79169689Skan   All of these will simultaneously lookup any callgraph edges.  If
80169689Skan   we're going to inline the duplicated function body, and the given
81169689Skan   function has some cloned callgraph nodes (one for each place this
82169689Skan   function will be inlined) those callgraph edges will be duplicated.
83169689Skan   If we're cloning the body, those callgraph edges will be
84169689Skan   updated to point into the new body.  (Note that the original
85169689Skan   callgraph node and edge list will not be altered.)
86169689Skan
87169689Skan   See the CALL_EXPR handling case in copy_body_r ().  */
88169689Skan
8990075Sobrien/* 0 if we should not perform inlining.
90117395Skan   1 if we should expand functions calls inline at the tree level.
91117395Skan   2 if we should consider *all* functions to be inline
9290075Sobrien   candidates.  */
9390075Sobrien
9490075Sobrienint flag_inline_trees = 0;
9590075Sobrien
9690075Sobrien/* To Do:
9790075Sobrien
9890075Sobrien   o In order to make inlining-on-trees work, we pessimized
9990075Sobrien     function-local static constants.  In particular, they are now
10090075Sobrien     always output, even when not addressed.  Fix this by treating
10190075Sobrien     function-local static constants just like global static
10290075Sobrien     constants; the back-end already knows not to output them if they
10390075Sobrien     are not needed.
10490075Sobrien
10590075Sobrien   o Provide heuristics to clamp inlining of recursive template
10690075Sobrien     calls?  */
10790075Sobrien
108169689Skan/* Prototypes.  */
10990075Sobrien
110169689Skanstatic tree declare_return_variable (copy_body_data *, tree, tree, tree *);
111169689Skanstatic tree copy_generic_body (copy_body_data *);
112169689Skanstatic bool inlinable_function_p (tree);
113169689Skanstatic void remap_block (tree *, copy_body_data *);
114169689Skanstatic tree remap_decls (tree, copy_body_data *);
115169689Skanstatic void copy_bind_expr (tree *, int *, copy_body_data *);
116169689Skanstatic tree mark_local_for_remap_r (tree *, int *, void *);
117169689Skanstatic void unsave_expr_1 (tree);
118169689Skanstatic tree unsave_r (tree *, int *, void *);
119169689Skanstatic void declare_inline_vars (tree, tree);
120169689Skanstatic void remap_save_expr (tree *, void *, int *);
121169689Skanstatic void add_lexical_block (tree current_block, tree new_block);
122169689Skanstatic tree copy_decl_to_var (tree, copy_body_data *);
123169689Skanstatic tree copy_result_decl_to_var (tree, copy_body_data *);
124169689Skanstatic tree copy_decl_no_change (tree, copy_body_data *);
125169689Skanstatic tree copy_decl_maybe_to_var (tree, copy_body_data *);
126169689Skan
127169689Skan/* Insert a tree->tree mapping for ID.  Despite the name suggests
128169689Skan   that the trees should be variables, it is used for more than that.  */
129169689Skan
130169689Skanvoid
131169689Skaninsert_decl_map (copy_body_data *id, tree key, tree value)
13290075Sobrien{
133169689Skan  splay_tree_insert (id->decl_map, (splay_tree_key) key,
134169689Skan		     (splay_tree_value) value);
13590075Sobrien
136169689Skan  /* Always insert an identity map as well.  If we see this same new
137169689Skan     node again, we won't want to duplicate it a second time.  */
138169689Skan  if (key != value)
139169689Skan    splay_tree_insert (id->decl_map, (splay_tree_key) value,
140169689Skan		       (splay_tree_value) value);
141169689Skan}
14290075Sobrien
14390075Sobrien/* Remap DECL during the copying of the BLOCK tree for the function.  */
14490075Sobrien
145169689Skantree
146169689Skanremap_decl (tree decl, copy_body_data *id)
14790075Sobrien{
14890075Sobrien  splay_tree_node n;
14990075Sobrien  tree fn;
15090075Sobrien
15190075Sobrien  /* We only remap local variables in the current function.  */
152169689Skan  fn = id->src_fn;
15390075Sobrien
15490075Sobrien  /* See if we have remapped this declaration.  */
155169689Skan
15690075Sobrien  n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
157132718Skan
15890075Sobrien  /* If we didn't already have an equivalent for this declaration,
15990075Sobrien     create one now.  */
16090075Sobrien  if (!n)
16190075Sobrien    {
16290075Sobrien      /* Make a copy of the variable or label.  */
163169689Skan      tree t = id->copy_decl (decl, id);
164169689Skan
165169689Skan      /* Remember it, so that if we encounter this local entity again
166169689Skan	 we can reuse this copy.  Do this early because remap_type may
167169689Skan	 need this decl for TYPE_STUB_DECL.  */
168169689Skan      insert_decl_map (id, decl, t);
16990075Sobrien
170169689Skan      if (!DECL_P (t))
171169689Skan	return t;
172169689Skan
173132718Skan      /* Remap types, if necessary.  */
174132718Skan      TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
175132718Skan      if (TREE_CODE (t) == TYPE_DECL)
176132718Skan        DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
17790075Sobrien
178132718Skan      /* Remap sizes as necessary.  */
179132718Skan      walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
180132718Skan      walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
181132718Skan
182169689Skan      /* If fields, do likewise for offset and qualifier.  */
183169689Skan      if (TREE_CODE (t) == FIELD_DECL)
18490075Sobrien	{
185169689Skan	  walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
186169689Skan	  if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
187169689Skan	    walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
18890075Sobrien	}
189117395Skan
190169689Skan      return t;
19190075Sobrien    }
19290075Sobrien
193169689Skan  return unshare_expr ((tree) n->value);
19490075Sobrien}
19590075Sobrien
196132718Skanstatic tree
197169689Skanremap_type_1 (tree type, copy_body_data *id)
198132718Skan{
199132718Skan  splay_tree_node node;
200132718Skan  tree new, t;
201132718Skan
202132718Skan  if (type == NULL)
203132718Skan    return type;
204132718Skan
205132718Skan  /* See if we have remapped this type.  */
206132718Skan  node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
207132718Skan  if (node)
208132718Skan    return (tree) node->value;
209132718Skan
210132718Skan  /* The type only needs remapping if it's variably modified.  */
211169689Skan  if (! variably_modified_type_p (type, id->src_fn))
212132718Skan    {
213169689Skan      insert_decl_map (id, type, type);
214132718Skan      return type;
215132718Skan    }
216132718Skan
217169689Skan  /* We do need a copy.  build and register it now.  If this is a pointer or
218169689Skan     reference type, remap the designated type and make a new pointer or
219169689Skan     reference type.  */
220169689Skan  if (TREE_CODE (type) == POINTER_TYPE)
221169689Skan    {
222169689Skan      new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
223169689Skan					 TYPE_MODE (type),
224169689Skan					 TYPE_REF_CAN_ALIAS_ALL (type));
225169689Skan      insert_decl_map (id, type, new);
226169689Skan      return new;
227169689Skan    }
228169689Skan  else if (TREE_CODE (type) == REFERENCE_TYPE)
229169689Skan    {
230169689Skan      new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
231169689Skan					    TYPE_MODE (type),
232169689Skan					    TYPE_REF_CAN_ALIAS_ALL (type));
233169689Skan      insert_decl_map (id, type, new);
234169689Skan      return new;
235169689Skan    }
236169689Skan  else
237169689Skan    new = copy_node (type);
238169689Skan
239169689Skan  insert_decl_map (id, type, new);
240169689Skan
241132718Skan  /* This is a new type, not a copy of an old type.  Need to reassociate
242132718Skan     variants.  We can handle everything except the main variant lazily.  */
243132718Skan  t = TYPE_MAIN_VARIANT (type);
244132718Skan  if (type != t)
245132718Skan    {
246132718Skan      t = remap_type (t, id);
247132718Skan      TYPE_MAIN_VARIANT (new) = t;
248132718Skan      TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
249132718Skan      TYPE_NEXT_VARIANT (t) = new;
250132718Skan    }
251132718Skan  else
252132718Skan    {
253132718Skan      TYPE_MAIN_VARIANT (new) = new;
254132718Skan      TYPE_NEXT_VARIANT (new) = NULL;
255132718Skan    }
256132718Skan
257169689Skan  if (TYPE_STUB_DECL (type))
258169689Skan    TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
259169689Skan
260132718Skan  /* Lazily create pointer and reference types.  */
261132718Skan  TYPE_POINTER_TO (new) = NULL;
262132718Skan  TYPE_REFERENCE_TO (new) = NULL;
263132718Skan
264132718Skan  switch (TREE_CODE (new))
265132718Skan    {
266132718Skan    case INTEGER_TYPE:
267132718Skan    case REAL_TYPE:
268132718Skan    case ENUMERAL_TYPE:
269132718Skan    case BOOLEAN_TYPE:
270132718Skan      t = TYPE_MIN_VALUE (new);
271132718Skan      if (t && TREE_CODE (t) != INTEGER_CST)
272132718Skan        walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
273169689Skan
274132718Skan      t = TYPE_MAX_VALUE (new);
275132718Skan      if (t && TREE_CODE (t) != INTEGER_CST)
276132718Skan        walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
277132718Skan      return new;
278132718Skan
279132718Skan    case FUNCTION_TYPE:
280132718Skan      TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
281132718Skan      walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
282132718Skan      return new;
283132718Skan
284132718Skan    case ARRAY_TYPE:
285132718Skan      TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
286132718Skan      TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
287132718Skan      break;
288132718Skan
289132718Skan    case RECORD_TYPE:
290132718Skan    case UNION_TYPE:
291132718Skan    case QUAL_UNION_TYPE:
292169689Skan      {
293169689Skan	tree f, nf = NULL;
294169689Skan
295169689Skan	for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
296169689Skan	  {
297169689Skan	    t = remap_decl (f, id);
298169689Skan	    DECL_CONTEXT (t) = new;
299169689Skan	    TREE_CHAIN (t) = nf;
300169689Skan	    nf = t;
301169689Skan	  }
302169689Skan	TYPE_FIELDS (new) = nreverse (nf);
303169689Skan      }
304132718Skan      break;
305132718Skan
306132718Skan    case OFFSET_TYPE:
307132718Skan    default:
308132718Skan      /* Shouldn't have been thought variable sized.  */
309169689Skan      gcc_unreachable ();
310132718Skan    }
311132718Skan
312132718Skan  walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
313132718Skan  walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
314132718Skan
315132718Skan  return new;
316132718Skan}
317132718Skan
318169689Skantree
319169689Skanremap_type (tree type, copy_body_data *id)
320169689Skan{
321169689Skan  splay_tree_node node;
32290075Sobrien
323169689Skan  if (type == NULL)
324169689Skan    return type;
325169689Skan
326169689Skan  /* See if we have remapped this type.  */
327169689Skan  node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
328169689Skan  if (node)
329169689Skan    return (tree) node->value;
330169689Skan
331169689Skan  /* The type only needs remapping if it's variably modified.  */
332169689Skan  if (! variably_modified_type_p (type, id->src_fn))
33390075Sobrien    {
334169689Skan      insert_decl_map (id, type, type);
335169689Skan      return type;
33690075Sobrien    }
33790075Sobrien
338169689Skan  return remap_type_1 (type, id);
339169689Skan}
340169689Skan
341169689Skanstatic tree
342169689Skanremap_decls (tree decls, copy_body_data *id)
343169689Skan{
344169689Skan  tree old_var;
345169689Skan  tree new_decls = NULL_TREE;
346169689Skan
347169689Skan  /* Remap its variables.  */
348169689Skan  for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
34990075Sobrien    {
350169689Skan      tree new_var;
35190075Sobrien
352169689Skan      /* We can not chain the local static declarations into the unexpanded_var_list
353169689Skan         as we can't duplicate them or break one decl rule.  Go ahead and link
354169689Skan         them into unexpanded_var_list.  */
355169689Skan      if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
356169689Skan	  && !DECL_EXTERNAL (old_var))
35790075Sobrien	{
358169689Skan	  cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
359169689Skan						 cfun->unexpanded_var_list);
360169689Skan	  continue;
361169689Skan	}
36290075Sobrien
363169689Skan      /* Remap the variable.  */
364169689Skan      new_var = remap_decl (old_var, id);
365169689Skan
366169689Skan      /* If we didn't remap this variable, so we can't mess with its
367169689Skan	 TREE_CHAIN.  If we remapped this variable to the return slot, it's
368169689Skan	 already declared somewhere else, so don't declare it here.  */
369169689Skan      if (!new_var || new_var == id->retvar)
370169689Skan	;
37190075Sobrien      else
37290075Sobrien	{
373169689Skan	  gcc_assert (DECL_P (new_var));
374169689Skan	  TREE_CHAIN (new_var) = new_decls;
375169689Skan	  new_decls = new_var;
37690075Sobrien	}
37790075Sobrien    }
37890075Sobrien
379169689Skan  return nreverse (new_decls);
380169689Skan}
381169689Skan
382169689Skan/* Copy the BLOCK to contain remapped versions of the variables
383169689Skan   therein.  And hook the new block into the block-tree.  */
384169689Skan
385169689Skanstatic void
386169689Skanremap_block (tree *block, copy_body_data *id)
387169689Skan{
388117395Skan  tree old_block;
389117395Skan  tree new_block;
390117395Skan  tree fn;
391117395Skan
392117395Skan  /* Make the new block.  */
393117395Skan  old_block = *block;
394117395Skan  new_block = make_node (BLOCK);
395117395Skan  TREE_USED (new_block) = TREE_USED (old_block);
396117395Skan  BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
397169689Skan  BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
398117395Skan  *block = new_block;
399117395Skan
400117395Skan  /* Remap its variables.  */
401169689Skan  BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
402117395Skan
403169689Skan  fn = id->dst_fn;
404117395Skan
405169689Skan  if (id->transform_lang_insert_block)
406169689Skan    lang_hooks.decls.insert_block (new_block);
407169689Skan
408117395Skan  /* Remember the remapped block.  */
409169689Skan  insert_decl_map (id, old_block, new_block);
41090075Sobrien}
41190075Sobrien
412169689Skan/* Copy the whole block tree and root it in id->block.  */
413169689Skanstatic tree
414169689Skanremap_blocks (tree block, copy_body_data *id)
415169689Skan{
416169689Skan  tree t;
417169689Skan  tree new = block;
41890075Sobrien
419169689Skan  if (!block)
420169689Skan    return NULL;
421169689Skan
422169689Skan  remap_block (&new, id);
423169689Skan  gcc_assert (new != block);
424169689Skan  for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
425169689Skan    add_lexical_block (new, remap_blocks (t, id));
426169689Skan  return new;
427169689Skan}
428169689Skan
42990075Sobrienstatic void
430169689Skancopy_statement_list (tree *tp)
43190075Sobrien{
432169689Skan  tree_stmt_iterator oi, ni;
433169689Skan  tree new;
43490075Sobrien
435169689Skan  new = alloc_stmt_list ();
436169689Skan  ni = tsi_start (new);
437169689Skan  oi = tsi_start (*tp);
438169689Skan  *tp = new;
439169689Skan
440169689Skan  for (; !tsi_end_p (oi); tsi_next (&oi))
441169689Skan    tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
442169689Skan}
443169689Skan
444169689Skanstatic void
445169689Skancopy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
446169689Skan{
447169689Skan  tree block = BIND_EXPR_BLOCK (*tp);
44890075Sobrien  /* Copy (and replace) the statement.  */
44990075Sobrien  copy_tree_r (tp, walk_subtrees, NULL);
450169689Skan  if (block)
451169689Skan    {
452169689Skan      remap_block (&block, id);
453169689Skan      BIND_EXPR_BLOCK (*tp) = block;
454169689Skan    }
45590075Sobrien
456169689Skan  if (BIND_EXPR_VARS (*tp))
457169689Skan    /* This will remap a lot of the same decls again, but this should be
458169689Skan       harmless.  */
459169689Skan    BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
46090075Sobrien}
46190075Sobrien
462169689Skan/* Called from copy_body_id via walk_tree.  DATA is really an
463169689Skan   `copy_body_data *'.  */
464169689Skan
465169689Skantree
466132718Skancopy_body_r (tree *tp, int *walk_subtrees, void *data)
46790075Sobrien{
468169689Skan  copy_body_data *id = (copy_body_data *) data;
469169689Skan  tree fn = id->src_fn;
470169689Skan  tree new_block;
47190075Sobrien
472169689Skan  /* Begin by recognizing trees that we'll completely rewrite for the
473169689Skan     inlining context.  Our output for these trees is completely
474169689Skan     different from out input (e.g. RETURN_EXPR is deleted, and morphs
475169689Skan     into an edge).  Further down, we'll handle trees that get
476169689Skan     duplicated and/or tweaked.  */
47790075Sobrien
478169689Skan  /* When requested, RETURN_EXPRs should be transformed to just the
479169689Skan     contained MODIFY_EXPR.  The branch semantics of the return will
480169689Skan     be handled elsewhere by manipulating the CFG rather than a statement.  */
481169689Skan  if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
48290075Sobrien    {
483169689Skan      tree assignment = TREE_OPERAND (*tp, 0);
48490075Sobrien
48590075Sobrien      /* If we're returning something, just turn that into an
486169689Skan	 assignment into the equivalent of the original RESULT_DECL.
487169689Skan	 If the "assignment" is just the result decl, the result
488169689Skan	 decl has already been set (e.g. a recent "foo (&result_decl,
489169689Skan	 ...)"); just toss the entire RETURN_EXPR.  */
490169689Skan      if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
49190075Sobrien	{
492169689Skan	  /* Replace the RETURN_EXPR with (a copy of) the
493169689Skan	     MODIFY_EXPR hanging underneath.  */
494169689Skan	  *tp = copy_node (assignment);
49590075Sobrien	}
496169689Skan      else /* Else the RETURN_EXPR returns no value.  */
497117395Skan	{
498169689Skan	  *tp = NULL;
499169689Skan	  return (tree) (void *)1;
500117395Skan	}
50190075Sobrien    }
502169689Skan
50390075Sobrien  /* Local variables and labels need to be replaced by equivalent
50490075Sobrien     variables.  We don't want to copy static variables; there's only
50590075Sobrien     one of those, no matter how many times we inline the containing
506169689Skan     function.  Similarly for globals from an outer function.  */
507169689Skan  else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
50890075Sobrien    {
50990075Sobrien      tree new_decl;
51090075Sobrien
51190075Sobrien      /* Remap the declaration.  */
51290075Sobrien      new_decl = remap_decl (*tp, id);
513169689Skan      gcc_assert (new_decl);
51490075Sobrien      /* Replace this variable with the copy.  */
51590075Sobrien      STRIP_TYPE_NOPS (new_decl);
51690075Sobrien      *tp = new_decl;
517169689Skan      *walk_subtrees = 0;
51890075Sobrien    }
519169689Skan  else if (TREE_CODE (*tp) == STATEMENT_LIST)
520169689Skan    copy_statement_list (tp);
52190075Sobrien  else if (TREE_CODE (*tp) == SAVE_EXPR)
522169689Skan    remap_save_expr (tp, id->decl_map, walk_subtrees);
523169689Skan  else if (TREE_CODE (*tp) == LABEL_DECL
524169689Skan	   && (! DECL_CONTEXT (*tp)
525169689Skan	       || decl_function_context (*tp) == id->src_fn))
526169689Skan    /* These may need to be remapped for EH handling.  */
527169689Skan    *tp = remap_decl (*tp, id);
528169689Skan  else if (TREE_CODE (*tp) == BIND_EXPR)
529169689Skan    copy_bind_expr (tp, walk_subtrees, id);
530132718Skan  /* Types may need remapping as well.  */
531132718Skan  else if (TYPE_P (*tp))
532132718Skan    *tp = remap_type (*tp, id);
533132718Skan
534169689Skan  /* If this is a constant, we have to copy the node iff the type will be
535169689Skan     remapped.  copy_tree_r will not copy a constant.  */
536169689Skan  else if (CONSTANT_CLASS_P (*tp))
537169689Skan    {
538169689Skan      tree new_type = remap_type (TREE_TYPE (*tp), id);
539169689Skan
540169689Skan      if (new_type == TREE_TYPE (*tp))
541169689Skan	*walk_subtrees = 0;
542169689Skan
543169689Skan      else if (TREE_CODE (*tp) == INTEGER_CST)
544169689Skan	*tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
545169689Skan				  TREE_INT_CST_HIGH (*tp));
546169689Skan      else
547169689Skan	{
548169689Skan	  *tp = copy_node (*tp);
549169689Skan	  TREE_TYPE (*tp) = new_type;
550169689Skan	}
551169689Skan    }
552169689Skan
55390075Sobrien  /* Otherwise, just copy the node.  Note that copy_tree_r already
55490075Sobrien     knows not to copy VAR_DECLs, etc., so this is safe.  */
55590075Sobrien  else
55690075Sobrien    {
557169689Skan      /* Here we handle trees that are not completely rewritten.
558169689Skan	 First we detect some inlining-induced bogosities for
559169689Skan	 discarding.  */
560132718Skan      if (TREE_CODE (*tp) == MODIFY_EXPR
561132718Skan	  && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
562169689Skan	  && (lang_hooks.tree_inlining.auto_var_in_fn_p
563132718Skan	      (TREE_OPERAND (*tp, 0), fn)))
56490075Sobrien	{
56590075Sobrien	  /* Some assignments VAR = VAR; don't generate any rtl code
56690075Sobrien	     and thus don't count as variable modification.  Avoid
56790075Sobrien	     keeping bogosities like 0 = 0.  */
56890075Sobrien	  tree decl = TREE_OPERAND (*tp, 0), value;
56990075Sobrien	  splay_tree_node n;
57090075Sobrien
57190075Sobrien	  n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
57290075Sobrien	  if (n)
57390075Sobrien	    {
57490075Sobrien	      value = (tree) n->value;
57590075Sobrien	      STRIP_TYPE_NOPS (value);
57690075Sobrien	      if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
577132718Skan		{
578169689Skan		  *tp = build_empty_stmt ();
579132718Skan		  return copy_body_r (tp, walk_subtrees, data);
580132718Skan		}
58190075Sobrien	    }
58290075Sobrien	}
583169689Skan      else if (TREE_CODE (*tp) == INDIRECT_REF)
584132718Skan	{
585169689Skan	  /* Get rid of *& from inline substitutions that can happen when a
586169689Skan	     pointer argument is an ADDR_EXPR.  */
587169689Skan	  tree decl = TREE_OPERAND (*tp, 0);
588132718Skan	  splay_tree_node n;
589132718Skan
590132718Skan	  n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
591132718Skan	  if (n)
592132718Skan	    {
593169689Skan	      tree new;
594169689Skan	      tree old;
595169689Skan	      /* If we happen to get an ADDR_EXPR in n->value, strip
596169689Skan	         it manually here as we'll eventually get ADDR_EXPRs
597169689Skan		 which lie about their types pointed to.  In this case
598169689Skan		 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
599169689Skan		 but we absolutely rely on that.  As fold_indirect_ref
600169689Skan	         does other useful transformations, try that first, though.  */
601169689Skan	      tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
602169689Skan	      new = unshare_expr ((tree)n->value);
603169689Skan	      old = *tp;
604169689Skan	      *tp = fold_indirect_ref_1 (type, new);
605169689Skan	      if (! *tp)
606169689Skan	        {
607169689Skan		  if (TREE_CODE (new) == ADDR_EXPR)
608169689Skan		    *tp = TREE_OPERAND (new, 0);
609169689Skan	          else
610169689Skan		    {
611169689Skan	              *tp = build1 (INDIRECT_REF, type, new);
612169689Skan		      TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
613169689Skan		    }
614132718Skan		}
615169689Skan	      *walk_subtrees = 0;
616169689Skan	      return NULL;
617132718Skan	    }
618132718Skan	}
619132718Skan
620169689Skan      /* Here is the "usual case".  Copy this tree node, and then
621169689Skan	 tweak some special cases.  */
622132718Skan      copy_tree_r (tp, walk_subtrees, NULL);
623169689Skan
624169689Skan      /* If EXPR has block defined, map it to newly constructed block.
625169689Skan         When inlining we want EXPRs without block appear in the block
626169689Skan	 of function call.  */
627169689Skan      if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
628169689Skan	{
629169689Skan	  new_block = id->block;
630169689Skan	  if (TREE_BLOCK (*tp))
631169689Skan	    {
632169689Skan	      splay_tree_node n;
633169689Skan	      n = splay_tree_lookup (id->decl_map,
634169689Skan				     (splay_tree_key) TREE_BLOCK (*tp));
635169689Skan	      gcc_assert (n);
636169689Skan	      new_block = (tree) n->value;
637169689Skan	    }
638169689Skan	  TREE_BLOCK (*tp) = new_block;
639169689Skan	}
640132718Skan
641169689Skan      if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
642169689Skan	TREE_OPERAND (*tp, 0) =
643169689Skan	  build_int_cst
644169689Skan	    (NULL_TREE,
645169689Skan	     id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
646132718Skan
647169689Skan      if (TREE_CODE (*tp) != OMP_CLAUSE)
648169689Skan	TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
649169689Skan
650132718Skan      /* The copied TARGET_EXPR has never been expanded, even if the
651132718Skan	 original node was expanded already.  */
652132718Skan      if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
653132718Skan	{
654132718Skan	  TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
655132718Skan	  TREE_OPERAND (*tp, 3) = NULL_TREE;
656132718Skan	}
657169689Skan
658169689Skan      /* Variable substitution need not be simple.  In particular, the
659169689Skan	 INDIRECT_REF substitution above.  Make sure that TREE_CONSTANT
660169689Skan	 and friends are up-to-date.  */
661169689Skan      else if (TREE_CODE (*tp) == ADDR_EXPR)
662169689Skan	{
663169689Skan	  walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
664169689Skan	  /* Handle the case where we substituted an INDIRECT_REF
665169689Skan	     into the operand of the ADDR_EXPR.  */
666169689Skan	  if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
667169689Skan	    *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
668169689Skan	  else
669169689Skan	    recompute_tree_invariant_for_addr_expr (*tp);
670169689Skan	  *walk_subtrees = 0;
671169689Skan	}
67290075Sobrien    }
67390075Sobrien
67490075Sobrien  /* Keep iterating.  */
67590075Sobrien  return NULL_TREE;
67690075Sobrien}
67790075Sobrien
678169689Skan/* Copy basic block, scale profile accordingly.  Edges will be taken care of
679169689Skan   later  */
680169689Skan
681169689Skanstatic basic_block
682169689Skancopy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
683169689Skan{
684169689Skan  block_stmt_iterator bsi, copy_bsi;
685169689Skan  basic_block copy_basic_block;
686169689Skan
687169689Skan  /* create_basic_block() will append every new block to
688169689Skan     basic_block_info automatically.  */
689169689Skan  copy_basic_block = create_basic_block (NULL, (void *) 0,
690169689Skan                                         (basic_block) bb->prev_bb->aux);
691169689Skan  copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
692169689Skan  copy_basic_block->frequency = (bb->frequency
693169689Skan				     * frequency_scale / REG_BR_PROB_BASE);
694169689Skan  copy_bsi = bsi_start (copy_basic_block);
695169689Skan
696169689Skan  for (bsi = bsi_start (bb);
697169689Skan       !bsi_end_p (bsi); bsi_next (&bsi))
698169689Skan    {
699169689Skan      tree stmt = bsi_stmt (bsi);
700169689Skan      tree orig_stmt = stmt;
701169689Skan
702169689Skan      walk_tree (&stmt, copy_body_r, id, NULL);
703169689Skan
704169689Skan      /* RETURN_EXPR might be removed,
705169689Skan         this is signalled by making stmt pointer NULL.  */
706169689Skan      if (stmt)
707169689Skan	{
708169689Skan	  tree call, decl;
709169689Skan
710169689Skan	  /* With return slot optimization we can end up with
711169689Skan	     non-gimple (foo *)&this->m, fix that here.  */
712169689Skan	  if (TREE_CODE (stmt) == MODIFY_EXPR
713169689Skan	      && TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
714169689Skan	      && !is_gimple_val (TREE_OPERAND (TREE_OPERAND (stmt, 1), 0)))
715169689Skan	    gimplify_stmt (&stmt);
716169689Skan
717169689Skan          bsi_insert_after (&copy_bsi, stmt, BSI_NEW_STMT);
718169689Skan	  call = get_call_expr_in (stmt);
719169689Skan	  /* We're duplicating a CALL_EXPR.  Find any corresponding
720169689Skan	     callgraph edges and update or duplicate them.  */
721169689Skan	  if (call && (decl = get_callee_fndecl (call)))
722169689Skan	    {
723169689Skan	      struct cgraph_node *node;
724169689Skan	      struct cgraph_edge *edge;
725169689Skan
726169689Skan	      switch (id->transform_call_graph_edges)
727169689Skan		{
728169689Skan		case CB_CGE_DUPLICATE:
729169689Skan		  edge = cgraph_edge (id->src_node, orig_stmt);
730169689Skan		  if (edge)
731169689Skan		    cgraph_clone_edge (edge, id->dst_node, stmt,
732169689Skan				       REG_BR_PROB_BASE, 1, true);
733169689Skan		  break;
734169689Skan
735169689Skan		case CB_CGE_MOVE_CLONES:
736169689Skan		  for (node = id->dst_node->next_clone;
737169689Skan		       node;
738169689Skan		       node = node->next_clone)
739169689Skan		    {
740169689Skan		      edge = cgraph_edge (node, orig_stmt);
741169689Skan		      gcc_assert (edge);
742169689Skan		      cgraph_set_call_stmt (edge, stmt);
743169689Skan		    }
744169689Skan		  /* FALLTHRU */
745169689Skan
746169689Skan		case CB_CGE_MOVE:
747169689Skan		  edge = cgraph_edge (id->dst_node, orig_stmt);
748169689Skan		  if (edge)
749169689Skan		    cgraph_set_call_stmt (edge, stmt);
750169689Skan		  break;
751169689Skan
752169689Skan		default:
753169689Skan		  gcc_unreachable ();
754169689Skan		}
755169689Skan	    }
756169689Skan	  /* If you think we can abort here, you are wrong.
757169689Skan	     There is no region 0 in tree land.  */
758169689Skan	  gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
759169689Skan		      != 0);
760169689Skan
761169689Skan	  if (tree_could_throw_p (stmt))
762169689Skan	    {
763169689Skan	      int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
764169689Skan	      /* Add an entry for the copied tree in the EH hashtable.
765169689Skan		 When cloning or versioning, use the hashtable in
766169689Skan		 cfun, and just copy the EH number.  When inlining, use the
767169689Skan		 hashtable in the caller, and adjust the region number.  */
768169689Skan	      if (region > 0)
769169689Skan		add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
770169689Skan
771169689Skan	      /* If this tree doesn't have a region associated with it,
772169689Skan		 and there is a "current region,"
773169689Skan		 then associate this tree with the current region
774169689Skan		 and add edges associated with this region.  */
775169689Skan	      if ((lookup_stmt_eh_region_fn (id->src_cfun,
776169689Skan					     orig_stmt) <= 0
777169689Skan		   && id->eh_region > 0)
778169689Skan		  && tree_could_throw_p (stmt))
779169689Skan		add_stmt_to_eh_region (stmt, id->eh_region);
780169689Skan	    }
781169689Skan	}
782169689Skan    }
783169689Skan  return copy_basic_block;
784169689Skan}
785169689Skan
786169689Skan/* Copy edges from BB into its copy constructed earlier, scale profile
787169689Skan   accordingly.  Edges will be taken care of later.  Assume aux
788169689Skan   pointers to point to the copies of each BB.  */
789169689Skanstatic void
790169689Skancopy_edges_for_bb (basic_block bb, int count_scale)
791169689Skan{
792169689Skan  basic_block new_bb = (basic_block) bb->aux;
793169689Skan  edge_iterator ei;
794169689Skan  edge old_edge;
795169689Skan  block_stmt_iterator bsi;
796169689Skan  int flags;
797169689Skan
798169689Skan  /* Use the indices from the original blocks to create edges for the
799169689Skan     new ones.  */
800169689Skan  FOR_EACH_EDGE (old_edge, ei, bb->succs)
801169689Skan    if (!(old_edge->flags & EDGE_EH))
802169689Skan      {
803169689Skan	edge new;
804169689Skan
805169689Skan	flags = old_edge->flags;
806169689Skan
807169689Skan	/* Return edges do get a FALLTHRU flag when the get inlined.  */
808169689Skan	if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
809169689Skan	    && old_edge->dest->aux != EXIT_BLOCK_PTR)
810169689Skan	  flags |= EDGE_FALLTHRU;
811169689Skan	new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
812169689Skan	new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
813169689Skan	new->probability = old_edge->probability;
814169689Skan      }
815169689Skan
816169689Skan  if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
817169689Skan    return;
818169689Skan
819169689Skan  for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
820169689Skan    {
821169689Skan      tree copy_stmt;
822169689Skan
823169689Skan      copy_stmt = bsi_stmt (bsi);
824169689Skan      update_stmt (copy_stmt);
825169689Skan      /* Do this before the possible split_block.  */
826169689Skan      bsi_next (&bsi);
827169689Skan
828169689Skan      /* If this tree could throw an exception, there are two
829169689Skan         cases where we need to add abnormal edge(s): the
830169689Skan         tree wasn't in a region and there is a "current
831169689Skan         region" in the caller; or the original tree had
832169689Skan         EH edges.  In both cases split the block after the tree,
833169689Skan         and add abnormal edge(s) as needed; we need both
834169689Skan         those from the callee and the caller.
835169689Skan         We check whether the copy can throw, because the const
836169689Skan         propagation can change an INDIRECT_REF which throws
837169689Skan         into a COMPONENT_REF which doesn't.  If the copy
838169689Skan         can throw, the original could also throw.  */
839169689Skan
840169689Skan      if (tree_can_throw_internal (copy_stmt))
841169689Skan	{
842169689Skan	  if (!bsi_end_p (bsi))
843169689Skan	    /* Note that bb's predecessor edges aren't necessarily
844169689Skan	       right at this point; split_block doesn't care.  */
845169689Skan	    {
846169689Skan	      edge e = split_block (new_bb, copy_stmt);
847169689Skan	      new_bb = e->dest;
848169689Skan	      bsi = bsi_start (new_bb);
849169689Skan	    }
850169689Skan
851169689Skan           make_eh_edges (copy_stmt);
852169689Skan	}
853169689Skan    }
854169689Skan}
855169689Skan
856169689Skan/* Wrapper for remap_decl so it can be used as a callback.  */
857169689Skanstatic tree
858169689Skanremap_decl_1 (tree decl, void *data)
859169689Skan{
860169689Skan  return remap_decl (decl, (copy_body_data *) data);
861169689Skan}
862169689Skan
86390075Sobrien/* Make a copy of the body of FN so that it can be inserted inline in
864169689Skan   another function.  Walks FN via CFG, returns new fndecl.  */
865169689Skan
866169689Skanstatic tree
867169689Skancopy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
868169689Skan	       basic_block entry_block_map, basic_block exit_block_map)
869169689Skan{
870169689Skan  tree callee_fndecl = id->src_fn;
871169689Skan  /* Original cfun for the callee, doesn't change.  */
872169689Skan  struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
873169689Skan  /* Copy, built by this function.  */
874169689Skan  struct function *new_cfun;
875169689Skan  /* Place to copy from; when a copy of the function was saved off earlier,
876169689Skan     use that instead of the main copy.  */
877169689Skan  struct function *cfun_to_copy =
878169689Skan    (struct function *) ggc_alloc_cleared (sizeof (struct function));
879169689Skan  basic_block bb;
880169689Skan  tree new_fndecl = NULL;
881169689Skan  int count_scale, frequency_scale;
882169689Skan
883169689Skan  if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
884169689Skan    count_scale = (REG_BR_PROB_BASE * count
885169689Skan		   / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
886169689Skan  else
887169689Skan    count_scale = 1;
888169689Skan
889169689Skan  if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
890169689Skan    frequency_scale = (REG_BR_PROB_BASE * frequency
891169689Skan		       /
892169689Skan		       ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
893169689Skan  else
894169689Skan    frequency_scale = count_scale;
895169689Skan
896169689Skan  /* Register specific tree functions.  */
897169689Skan  tree_register_cfg_hooks ();
898169689Skan
899169689Skan  /* Must have a CFG here at this point.  */
900169689Skan  gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
901169689Skan	      (DECL_STRUCT_FUNCTION (callee_fndecl)));
902169689Skan
903169689Skan  *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
904169689Skan
905169689Skan  id->src_cfun = cfun_to_copy;
906169689Skan
907169689Skan  /* If requested, create new basic_block_info and label_to_block_maps.
908169689Skan     Otherwise, insert our new blocks and labels into the existing cfg.  */
909169689Skan  if (id->transform_new_cfg)
910169689Skan    {
911169689Skan      new_cfun =
912169689Skan	(struct function *) ggc_alloc_cleared (sizeof (struct function));
913169689Skan      *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
914169689Skan      new_cfun->cfg = NULL;
915169689Skan      new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
916169689Skan      new_cfun->ib_boundaries_block = NULL;
917169689Skan      DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
918169689Skan      push_cfun (new_cfun);
919169689Skan      init_empty_tree_cfg ();
920169689Skan
921169689Skan      ENTRY_BLOCK_PTR->count =
922169689Skan	(ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
923169689Skan	 REG_BR_PROB_BASE);
924169689Skan      ENTRY_BLOCK_PTR->frequency =
925169689Skan	(ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
926169689Skan	 frequency_scale / REG_BR_PROB_BASE);
927169689Skan      EXIT_BLOCK_PTR->count =
928169689Skan	(EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
929169689Skan	 REG_BR_PROB_BASE);
930169689Skan      EXIT_BLOCK_PTR->frequency =
931169689Skan	(EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
932169689Skan	 frequency_scale / REG_BR_PROB_BASE);
933169689Skan
934169689Skan      entry_block_map = ENTRY_BLOCK_PTR;
935169689Skan      exit_block_map = EXIT_BLOCK_PTR;
936169689Skan    }
937169689Skan
938169689Skan  ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
939169689Skan  EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
940169689Skan
941169689Skan  /* Duplicate any exception-handling regions.  */
942169689Skan  if (cfun->eh)
943169689Skan    {
944169689Skan      if (id->transform_new_cfg)
945169689Skan        init_eh_for_function ();
946169689Skan      id->eh_region_offset
947169689Skan	= duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
948169689Skan				0, id->eh_region);
949169689Skan    }
950169689Skan  /* Use aux pointers to map the original blocks to copy.  */
951169689Skan  FOR_EACH_BB_FN (bb, cfun_to_copy)
952169689Skan    bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
953169689Skan  /* Now that we've duplicated the blocks, duplicate their edges.  */
954169689Skan  FOR_ALL_BB_FN (bb, cfun_to_copy)
955169689Skan    copy_edges_for_bb (bb, count_scale);
956169689Skan  FOR_ALL_BB_FN (bb, cfun_to_copy)
957169689Skan    bb->aux = NULL;
958169689Skan
959169689Skan  if (id->transform_new_cfg)
960169689Skan    pop_cfun ();
961169689Skan
962169689Skan  return new_fndecl;
963169689Skan}
964169689Skan
965169689Skan/* Make a copy of the body of FN so that it can be inserted inline in
96690075Sobrien   another function.  */
96790075Sobrien
96890075Sobrienstatic tree
969169689Skancopy_generic_body (copy_body_data *id)
97090075Sobrien{
97190075Sobrien  tree body;
972169689Skan  tree fndecl = id->src_fn;
97390075Sobrien
974169689Skan  body = DECL_SAVED_TREE (fndecl);
97590075Sobrien  walk_tree (&body, copy_body_r, id, NULL);
97690075Sobrien
97790075Sobrien  return body;
97890075Sobrien}
97990075Sobrien
980169689Skanstatic tree
981169689Skancopy_body (copy_body_data *id, gcov_type count, int frequency,
982169689Skan	   basic_block entry_block_map, basic_block exit_block_map)
983169689Skan{
984169689Skan  tree fndecl = id->src_fn;
985169689Skan  tree body;
986169689Skan
987169689Skan  /* If this body has a CFG, walk CFG and copy.  */
988169689Skan  gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
989169689Skan  body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
990169689Skan
991169689Skan  return body;
992169689Skan}
993169689Skan
994169689Skan/* Return true if VALUE is an ADDR_EXPR of an automatic variable
995169689Skan   defined in function FN, or of a data member thereof.  */
996169689Skan
997169689Skanstatic bool
998169689Skanself_inlining_addr_expr (tree value, tree fn)
999169689Skan{
1000169689Skan  tree var;
1001169689Skan
1002169689Skan  if (TREE_CODE (value) != ADDR_EXPR)
1003169689Skan    return false;
1004169689Skan
1005169689Skan  var = get_base_address (TREE_OPERAND (value, 0));
1006169689Skan
1007169689Skan  return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
1008169689Skan}
1009169689Skan
1010169689Skanstatic void
1011169689Skansetup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
1012169689Skan		     basic_block bb, tree *vars)
1013169689Skan{
1014169689Skan  tree init_stmt;
1015169689Skan  tree var;
1016169689Skan  tree var_sub;
1017169689Skan
1018169689Skan  /* If the parameter is never assigned to, we may not need to
1019169689Skan     create a new variable here at all.  Instead, we may be able
1020169689Skan     to just use the argument value.  */
1021169689Skan  if (TREE_READONLY (p)
1022169689Skan      && !TREE_ADDRESSABLE (p)
1023169689Skan      && value && !TREE_SIDE_EFFECTS (value))
1024169689Skan    {
1025169689Skan      /* We may produce non-gimple trees by adding NOPs or introduce
1026169689Skan	 invalid sharing when operand is not really constant.
1027169689Skan	 It is not big deal to prohibit constant propagation here as
1028169689Skan	 we will constant propagate in DOM1 pass anyway.  */
1029169689Skan      if (is_gimple_min_invariant (value)
1030169689Skan	  && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
1031169689Skan	  /* We have to be very careful about ADDR_EXPR.  Make sure
1032169689Skan	     the base variable isn't a local variable of the inlined
1033169689Skan	     function, e.g., when doing recursive inlining, direct or
1034169689Skan	     mutually-recursive or whatever, which is why we don't
1035169689Skan	     just test whether fn == current_function_decl.  */
1036169689Skan	  && ! self_inlining_addr_expr (value, fn))
1037169689Skan	{
1038169689Skan	  insert_decl_map (id, p, value);
1039169689Skan	  return;
1040169689Skan	}
1041169689Skan    }
1042169689Skan
1043169689Skan  /* Make an equivalent VAR_DECL.  Note that we must NOT remap the type
1044169689Skan     here since the type of this decl must be visible to the calling
1045169689Skan     function.  */
1046169689Skan  var = copy_decl_to_var (p, id);
1047169689Skan
1048169689Skan  /* See if the frontend wants to pass this by invisible reference.  If
1049169689Skan     so, our new VAR_DECL will have REFERENCE_TYPE, and we need to
1050169689Skan     replace uses of the PARM_DECL with dereferences.  */
1051169689Skan  if (TREE_TYPE (var) != TREE_TYPE (p)
1052169689Skan      && POINTER_TYPE_P (TREE_TYPE (var))
1053169689Skan      && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
1054169689Skan    {
1055169689Skan      insert_decl_map (id, var, var);
1056169689Skan      var_sub = build_fold_indirect_ref (var);
1057169689Skan    }
1058169689Skan  else
1059169689Skan    var_sub = var;
1060169689Skan
1061169689Skan  /* Register the VAR_DECL as the equivalent for the PARM_DECL;
1062169689Skan     that way, when the PARM_DECL is encountered, it will be
1063169689Skan     automatically replaced by the VAR_DECL.  */
1064169689Skan  insert_decl_map (id, p, var_sub);
1065169689Skan
1066169689Skan  /* Declare this new variable.  */
1067169689Skan  TREE_CHAIN (var) = *vars;
1068169689Skan  *vars = var;
1069169689Skan
1070169689Skan  /* Make gimplifier happy about this variable.  */
1071169689Skan  DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1072169689Skan
1073169689Skan  /* Even if P was TREE_READONLY, the new VAR should not be.
1074169689Skan     In the original code, we would have constructed a
1075169689Skan     temporary, and then the function body would have never
1076169689Skan     changed the value of P.  However, now, we will be
1077169689Skan     constructing VAR directly.  The constructor body may
1078169689Skan     change its value multiple times as it is being
1079169689Skan     constructed.  Therefore, it must not be TREE_READONLY;
1080169689Skan     the back-end assumes that TREE_READONLY variable is
1081169689Skan     assigned to only once.  */
1082169689Skan  if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
1083169689Skan    TREE_READONLY (var) = 0;
1084169689Skan
1085169689Skan  /* Initialize this VAR_DECL from the equivalent argument.  Convert
1086169689Skan     the argument to the proper type in case it was promoted.  */
1087169689Skan  if (value)
1088169689Skan    {
1089169689Skan      tree rhs = fold_convert (TREE_TYPE (var), value);
1090169689Skan      block_stmt_iterator bsi = bsi_last (bb);
1091169689Skan
1092169689Skan      if (rhs == error_mark_node)
1093169689Skan	return;
1094169689Skan
1095169689Skan      STRIP_USELESS_TYPE_CONVERSION (rhs);
1096169689Skan
1097169689Skan      /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
1098169689Skan	 keep our trees in gimple form.  */
1099169689Skan      init_stmt = build2 (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
1100169689Skan
1101169689Skan      /* If we did not create a gimple value and we did not create a gimple
1102169689Skan	 cast of a gimple value, then we will need to gimplify INIT_STMTS
1103169689Skan	 at the end.  Note that is_gimple_cast only checks the outer
1104169689Skan	 tree code, not its operand.  Thus the explicit check that its
1105169689Skan	 operand is a gimple value.  */
1106169689Skan      if (!is_gimple_val (rhs)
1107169689Skan	  && (!is_gimple_cast (rhs)
1108169689Skan	      || !is_gimple_val (TREE_OPERAND (rhs, 0))))
1109169689Skan	gimplify_stmt (&init_stmt);
1110169689Skan
1111169689Skan      /* If VAR represents a zero-sized variable, it's possible that the
1112169689Skan	 assignment statment may result in no gimple statements.  */
1113169689Skan      if (init_stmt)
1114169689Skan        bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
1115169689Skan    }
1116169689Skan}
1117169689Skan
111890075Sobrien/* Generate code to initialize the parameters of the function at the
111990075Sobrien   top of the stack in ID from the ARGS (presented as a TREE_LIST).  */
112090075Sobrien
1121169689Skanstatic void
1122169689Skaninitialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
1123169689Skan			       tree fn, basic_block bb)
112490075Sobrien{
112590075Sobrien  tree parms;
112690075Sobrien  tree a;
112790075Sobrien  tree p;
1128117395Skan  tree vars = NULL_TREE;
1129132718Skan  int argnum = 0;
113090075Sobrien
113190075Sobrien  /* Figure out what the parameters are.  */
1132169689Skan  parms = DECL_ARGUMENTS (fn);
113390075Sobrien
113490075Sobrien  /* Loop through the parameter declarations, replacing each with an
113590075Sobrien     equivalent VAR_DECL, appropriately initialized.  */
113690075Sobrien  for (p = parms, a = args; p;
113790075Sobrien       a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
113890075Sobrien    {
113990075Sobrien      tree value;
114090075Sobrien
1141132718Skan      ++argnum;
1142132718Skan
114390075Sobrien      /* Find the initializer.  */
1144169689Skan      value = lang_hooks.tree_inlining.convert_parm_for_inlining
1145132718Skan	      (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
114690075Sobrien
1147169689Skan      setup_one_parameter (id, p, value, fn, bb, &vars);
1148169689Skan    }
114990075Sobrien
1150169689Skan  /* Initialize the static chain.  */
1151169689Skan  p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
1152169689Skan  gcc_assert (fn != current_function_decl);
1153169689Skan  if (p)
1154169689Skan    {
1155169689Skan      /* No static chain?  Seems like a bug in tree-nested.c.  */
1156169689Skan      gcc_assert (static_chain);
115790075Sobrien
1158169689Skan      setup_one_parameter (id, p, static_chain, fn, bb, &vars);
1159169689Skan    }
1160117395Skan
1161169689Skan  declare_inline_vars (id->block, vars);
1162169689Skan}
116390075Sobrien
1164169689Skan/* Declare a return variable to replace the RESULT_DECL for the
1165169689Skan   function we are calling.  An appropriate DECL_STMT is returned.
1166169689Skan   The USE_STMT is filled to contain a use of the declaration to
1167169689Skan   indicate the return value of the function.
1168117395Skan
1169169689Skan   RETURN_SLOT_ADDR, if non-null, was a fake parameter that
1170169689Skan   took the address of the result.  MODIFY_DEST, if non-null, was the LHS of
1171169689Skan   the MODIFY_EXPR to which this call is the RHS.
1172169689Skan
1173169689Skan   The return value is a (possibly null) value that is the result of the
1174169689Skan   function as seen by the callee.  *USE_P is a (possibly null) value that
1175169689Skan   holds the result as seen by the caller.  */
1176169689Skan
1177169689Skanstatic tree
1178169689Skandeclare_return_variable (copy_body_data *id, tree return_slot_addr,
1179169689Skan			 tree modify_dest, tree *use_p)
1180169689Skan{
1181169689Skan  tree callee = id->src_fn;
1182169689Skan  tree caller = id->dst_fn;
1183169689Skan  tree result = DECL_RESULT (callee);
1184169689Skan  tree callee_type = TREE_TYPE (result);
1185169689Skan  tree caller_type = TREE_TYPE (TREE_TYPE (callee));
1186169689Skan  tree var, use;
1187169689Skan
1188169689Skan  /* We don't need to do anything for functions that don't return
1189169689Skan     anything.  */
1190169689Skan  if (!result || VOID_TYPE_P (callee_type))
1191169689Skan    {
1192169689Skan      *use_p = NULL_TREE;
1193169689Skan      return NULL_TREE;
1194169689Skan    }
1195169689Skan
1196169689Skan  /* If there was a return slot, then the return value is the
1197169689Skan     dereferenced address of that object.  */
1198169689Skan  if (return_slot_addr)
1199169689Skan    {
1200169689Skan      /* The front end shouldn't have used both return_slot_addr and
1201169689Skan	 a modify expression.  */
1202169689Skan      gcc_assert (!modify_dest);
1203169689Skan      if (DECL_BY_REFERENCE (result))
1204169689Skan	var = return_slot_addr;
1205117395Skan      else
1206169689Skan	var = build_fold_indirect_ref (return_slot_addr);
1207169689Skan      if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1208169689Skan	  && !DECL_COMPLEX_GIMPLE_REG_P (result)
1209169689Skan	  && DECL_P (var))
1210169689Skan	DECL_COMPLEX_GIMPLE_REG_P (var) = 0;
1211169689Skan      use = NULL;
1212169689Skan      goto done;
1213169689Skan    }
1214117395Skan
1215169689Skan  /* All types requiring non-trivial constructors should have been handled.  */
1216169689Skan  gcc_assert (!TREE_ADDRESSABLE (callee_type));
121790075Sobrien
1218169689Skan  /* Attempt to avoid creating a new temporary variable.  */
1219169689Skan  if (modify_dest)
1220169689Skan    {
1221169689Skan      bool use_it = false;
122290075Sobrien
1223169689Skan      /* We can't use MODIFY_DEST if there's type promotion involved.  */
1224169689Skan      if (!lang_hooks.types_compatible_p (caller_type, callee_type))
1225169689Skan	use_it = false;
1226169689Skan
1227169689Skan      /* ??? If we're assigning to a variable sized type, then we must
1228169689Skan	 reuse the destination variable, because we've no good way to
1229169689Skan	 create variable sized temporaries at this point.  */
1230169689Skan      else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
1231169689Skan	use_it = true;
1232169689Skan
1233169689Skan      /* If the callee cannot possibly modify MODIFY_DEST, then we can
1234169689Skan	 reuse it as the result of the call directly.  Don't do this if
1235169689Skan	 it would promote MODIFY_DEST to addressable.  */
1236169689Skan      else if (TREE_ADDRESSABLE (result))
1237169689Skan	use_it = false;
123890075Sobrien      else
123990075Sobrien	{
1240169689Skan	  tree base_m = get_base_address (modify_dest);
124190075Sobrien
1242169689Skan	  /* If the base isn't a decl, then it's a pointer, and we don't
1243169689Skan	     know where that's going to go.  */
1244169689Skan	  if (!DECL_P (base_m))
1245169689Skan	    use_it = false;
1246169689Skan	  else if (is_global_var (base_m))
1247169689Skan	    use_it = false;
1248169689Skan	  else if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
1249169689Skan		   && !DECL_COMPLEX_GIMPLE_REG_P (result)
1250169689Skan		   && DECL_COMPLEX_GIMPLE_REG_P (base_m))
1251169689Skan	    use_it = false;
1252169689Skan	  else if (!TREE_ADDRESSABLE (base_m))
1253169689Skan	    use_it = true;
125490075Sobrien	}
125596263Sobrien
1256169689Skan      if (use_it)
125796263Sobrien	{
1258169689Skan	  var = modify_dest;
1259169689Skan	  use = NULL;
1260169689Skan	  goto done;
126196263Sobrien	}
126290075Sobrien    }
126390075Sobrien
1264169689Skan  gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
126590075Sobrien
1266169689Skan  var = copy_result_decl_to_var (result, id);
126790075Sobrien
1268169689Skan  DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
1269169689Skan  DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
1270169689Skan    = tree_cons (NULL_TREE, var,
1271169689Skan		 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
127290075Sobrien
1273169689Skan  /* Do not have the rest of GCC warn about this variable as it should
1274169689Skan     not be visible to the user.  */
1275169689Skan  TREE_NO_WARNING (var) = 1;
127690075Sobrien
1277169689Skan  declare_inline_vars (id->block, var);
127890075Sobrien
1279169689Skan  /* Build the use expr.  If the return type of the function was
1280169689Skan     promoted, convert it back to the expected type.  */
1281169689Skan  use = var;
1282169689Skan  if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
1283169689Skan    use = fold_convert (caller_type, var);
1284169689Skan
1285169689Skan  STRIP_USELESS_TYPE_CONVERSION (use);
128690075Sobrien
1287169689Skan  if (DECL_BY_REFERENCE (result))
1288169689Skan    var = build_fold_addr_expr (var);
128990075Sobrien
1290169689Skan done:
129190075Sobrien  /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
129290075Sobrien     way, when the RESULT_DECL is encountered, it will be
129390075Sobrien     automatically replaced by the VAR_DECL.  */
1294169689Skan  insert_decl_map (id, result, var);
129590075Sobrien
1296169689Skan  /* Remember this so we can ignore it in remap_decls.  */
1297169689Skan  id->retvar = var;
129890075Sobrien
1299169689Skan  *use_p = use;
1300169689Skan  return var;
130190075Sobrien}
130290075Sobrien
1303117395Skan/* Returns nonzero if a function can be inlined as a tree.  */
130490075Sobrien
1305132718Skanbool
1306132718Skantree_inlinable_function_p (tree fn)
130790075Sobrien{
1308132718Skan  return inlinable_function_p (fn);
130990075Sobrien}
131090075Sobrien
1311132718Skanstatic const char *inline_forbidden_reason;
1312117395Skan
1313117395Skanstatic tree
1314132718Skaninline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
1315132718Skan		      void *fnp)
1316117395Skan{
1317132718Skan  tree node = *nodep;
1318132718Skan  tree fn = (tree) fnp;
1319132718Skan  tree t;
1320117395Skan
1321132718Skan  switch (TREE_CODE (node))
1322132718Skan    {
1323132718Skan    case CALL_EXPR:
1324132718Skan      /* Refuse to inline alloca call unless user explicitly forced so as
1325132718Skan	 this may change program's memory overhead drastically when the
1326132718Skan	 function using alloca is called in loop.  In GCC present in
1327132718Skan	 SPEC2000 inlining into schedule_block cause it to require 2GB of
1328132718Skan	 RAM instead of 256MB.  */
1329132718Skan      if (alloca_call_p (node)
1330132718Skan	  && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1331132718Skan	{
1332132718Skan	  inline_forbidden_reason
1333169689Skan	    = G_("function %q+F can never be inlined because it uses "
1334132718Skan		 "alloca (override using the always_inline attribute)");
1335132718Skan	  return node;
1336132718Skan	}
1337132718Skan      t = get_callee_fndecl (node);
1338132718Skan      if (! t)
1339132718Skan	break;
1340117395Skan
1341132718Skan      /* We cannot inline functions that call setjmp.  */
1342132718Skan      if (setjmp_call_p (t))
1343132718Skan	{
1344132718Skan	  inline_forbidden_reason
1345169689Skan	    = G_("function %q+F can never be inlined because it uses setjmp");
1346132718Skan	  return node;
1347132718Skan	}
1348132718Skan
1349169689Skan      if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
1350132718Skan	switch (DECL_FUNCTION_CODE (t))
1351132718Skan	  {
1352132718Skan	    /* We cannot inline functions that take a variable number of
1353132718Skan	       arguments.  */
1354132718Skan	  case BUILT_IN_VA_START:
1355132718Skan	  case BUILT_IN_STDARG_START:
1356132718Skan	  case BUILT_IN_NEXT_ARG:
1357132718Skan	  case BUILT_IN_VA_END:
1358169689Skan	    inline_forbidden_reason
1359169689Skan	      = G_("function %q+F can never be inlined because it "
1360169689Skan		   "uses variable argument lists");
1361169689Skan	    return node;
1362169689Skan
1363132718Skan	  case BUILT_IN_LONGJMP:
1364169689Skan	    /* We can't inline functions that call __builtin_longjmp at
1365169689Skan	       all.  The non-local goto machinery really requires the
1366169689Skan	       destination be in a different function.  If we allow the
1367169689Skan	       function calling __builtin_longjmp to be inlined into the
1368169689Skan	       function calling __builtin_setjmp, Things will Go Awry.  */
1369169689Skan	    inline_forbidden_reason
1370169689Skan	      = G_("function %q+F can never be inlined because "
1371169689Skan		   "it uses setjmp-longjmp exception handling");
1372169689Skan	    return node;
1373132718Skan
1374169689Skan	  case BUILT_IN_NONLOCAL_GOTO:
1375169689Skan	    /* Similarly.  */
1376169689Skan	    inline_forbidden_reason
1377169689Skan	      = G_("function %q+F can never be inlined because "
1378169689Skan		   "it uses non-local goto");
1379169689Skan	    return node;
1380169689Skan
1381169689Skan	  case BUILT_IN_RETURN:
1382169689Skan	  case BUILT_IN_APPLY_ARGS:
1383169689Skan	    /* If a __builtin_apply_args caller would be inlined,
1384169689Skan	       it would be saving arguments of the function it has
1385169689Skan	       been inlined into.  Similarly __builtin_return would
1386169689Skan	       return from the function the inline has been inlined into.  */
1387169689Skan	    inline_forbidden_reason
1388169689Skan	      = G_("function %q+F can never be inlined because "
1389169689Skan		   "it uses __builtin_return or __builtin_apply_args");
1390169689Skan	    return node;
1391169689Skan
1392132718Skan	  default:
1393132718Skan	    break;
1394132718Skan	  }
1395132718Skan      break;
1396132718Skan
1397132718Skan    case GOTO_EXPR:
1398132718Skan      t = TREE_OPERAND (node, 0);
1399132718Skan
1400132718Skan      /* We will not inline a function which uses computed goto.  The
1401132718Skan	 addresses of its local labels, which may be tucked into
1402132718Skan	 global storage, are of course not constant across
1403132718Skan	 instantiations, which causes unexpected behavior.  */
1404132718Skan      if (TREE_CODE (t) != LABEL_DECL)
1405132718Skan	{
1406132718Skan	  inline_forbidden_reason
1407169689Skan	    = G_("function %q+F can never be inlined "
1408132718Skan		 "because it contains a computed goto");
1409132718Skan	  return node;
1410132718Skan	}
1411169689Skan      break;
1412132718Skan
1413169689Skan    case LABEL_EXPR:
1414169689Skan      t = TREE_OPERAND (node, 0);
1415169689Skan      if (DECL_NONLOCAL (t))
1416132718Skan	{
1417169689Skan	  /* We cannot inline a function that receives a non-local goto
1418169689Skan	     because we cannot remap the destination label used in the
1419169689Skan	     function that is performing the non-local goto.  */
1420132718Skan	  inline_forbidden_reason
1421169689Skan	    = G_("function %q+F can never be inlined "
1422169689Skan		 "because it receives a non-local goto");
1423132718Skan	  return node;
1424132718Skan	}
1425132718Skan      break;
1426132718Skan
1427132718Skan    case RECORD_TYPE:
1428132718Skan    case UNION_TYPE:
1429132718Skan      /* We cannot inline a function of the form
1430132718Skan
1431132718Skan	   void F (int i) { struct S { int ar[i]; } s; }
1432132718Skan
1433132718Skan	 Attempting to do so produces a catch-22.
1434132718Skan	 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
1435132718Skan	 UNION_TYPE nodes, then it goes into infinite recursion on a
1436132718Skan	 structure containing a pointer to its own type.  If it doesn't,
1437132718Skan	 then the type node for S doesn't get adjusted properly when
1438169689Skan	 F is inlined.
1439169689Skan
1440169689Skan	 ??? This is likely no longer true, but it's too late in the 4.0
1441169689Skan	 cycle to try to find out.  This should be checked for 4.1.  */
1442132718Skan      for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
1443169689Skan	if (variably_modified_type_p (TREE_TYPE (t), NULL))
1444132718Skan	  {
1445132718Skan	    inline_forbidden_reason
1446169689Skan	      = G_("function %q+F can never be inlined "
1447132718Skan		   "because it uses variable sized variables");
1448132718Skan	    return node;
1449132718Skan	  }
1450169689Skan
1451132718Skan    default:
1452132718Skan      break;
1453132718Skan    }
1454132718Skan
1455132718Skan  return NULL_TREE;
1456117395Skan}
1457117395Skan
1458132718Skan/* Return subexpression representing possible alloca call, if any.  */
1459117395Skanstatic tree
1460132718Skaninline_forbidden_p (tree fndecl)
1461117395Skan{
1462132718Skan  location_t saved_loc = input_location;
1463169689Skan  block_stmt_iterator bsi;
1464169689Skan  basic_block bb;
1465169689Skan  tree ret = NULL_TREE;
1466169689Skan
1467169689Skan  FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
1468169689Skan    for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1469169689Skan      {
1470169689Skan	ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
1471169689Skan				    inline_forbidden_p_1, fndecl);
1472169689Skan	if (ret)
1473169689Skan	  goto egress;
1474169689Skan      }
1475169689Skan
1476169689Skanegress:
1477132718Skan  input_location = saved_loc;
1478132718Skan  return ret;
1479117395Skan}
1480117395Skan
1481132718Skan/* Returns nonzero if FN is a function that does not have any
1482132718Skan   fundamental inline blocking properties.  */
148390075Sobrien
1484132718Skanstatic bool
1485132718Skaninlinable_function_p (tree fn)
148690075Sobrien{
1487132718Skan  bool inlinable = true;
148890075Sobrien
148990075Sobrien  /* If we've already decided this function shouldn't be inlined,
149090075Sobrien     there's no need to check again.  */
149190075Sobrien  if (DECL_UNINLINABLE (fn))
1492132718Skan    return false;
149390075Sobrien
1494132718Skan  /* See if there is any language-specific reason it cannot be
1495132718Skan     inlined.  (It is important that this hook be called early because
1496132718Skan     in C++ it may result in template instantiation.)
1497132718Skan     If the function is not inlinable for language-specific reasons,
1498132718Skan     it is left up to the langhook to explain why.  */
1499169689Skan  inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
150090075Sobrien
1501132718Skan  /* If we don't have the function body available, we can't inline it.
1502132718Skan     However, this should not be recorded since we also get here for
1503132718Skan     forward declared inline functions.  Therefore, return at once.  */
1504132718Skan  if (!DECL_SAVED_TREE (fn))
1505132718Skan    return false;
1506132718Skan
1507132718Skan  /* If we're not inlining at all, then we cannot inline this function.  */
1508132718Skan  else if (!flag_inline_trees)
1509132718Skan    inlinable = false;
1510132718Skan
1511132718Skan  /* Only try to inline functions if DECL_INLINE is set.  This should be
1512132718Skan     true for all functions declared `inline', and for all other functions
1513132718Skan     as well with -finline-functions.
1514132718Skan
1515132718Skan     Don't think of disregarding DECL_INLINE when flag_inline_trees == 2;
1516132718Skan     it's the front-end that must set DECL_INLINE in this case, because
1517132718Skan     dwarf2out loses if a function that does not have DECL_INLINE set is
1518132718Skan     inlined anyway.  That is why we have both DECL_INLINE and
1519132718Skan     DECL_DECLARED_INLINE_P.  */
1520132718Skan  /* FIXME: When flag_inline_trees dies, the check for flag_unit_at_a_time
1521132718Skan	    here should be redundant.  */
1522132718Skan  else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
1523132718Skan    inlinable = false;
1524132718Skan
1525132718Skan  else if (inline_forbidden_p (fn))
1526117395Skan    {
1527132718Skan      /* See if we should warn about uninlinable functions.  Previously,
1528132718Skan	 some of these warnings would be issued while trying to expand
1529132718Skan	 the function inline, but that would cause multiple warnings
1530132718Skan	 about functions that would for example call alloca.  But since
1531132718Skan	 this a property of the function, just one warning is enough.
1532132718Skan	 As a bonus we can now give more details about the reason why a
1533132718Skan	 function is not inlinable.
1534132718Skan	 We only warn for functions declared `inline' by the user.  */
1535132718Skan      bool do_warning = (warn_inline
1536132718Skan			 && DECL_INLINE (fn)
1537132718Skan			 && DECL_DECLARED_INLINE_P (fn)
1538132718Skan			 && !DECL_IN_SYSTEM_HEADER (fn));
153990075Sobrien
1540169689Skan      if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
1541169689Skan	sorry (inline_forbidden_reason, fn);
1542132718Skan      else if (do_warning)
1543169689Skan	warning (OPT_Winline, inline_forbidden_reason, fn);
1544117395Skan
1545132718Skan      inlinable = false;
1546132718Skan    }
154790075Sobrien
1548132718Skan  /* Squirrel away the result so that we don't have to check again.  */
1549132718Skan  DECL_UNINLINABLE (fn) = !inlinable;
155090075Sobrien
155190075Sobrien  return inlinable;
155290075Sobrien}
155390075Sobrien
1554169689Skan/* Estimate the cost of a memory move.  Use machine dependent
1555169689Skan   word size and take possible memcpy call into account.  */
155690075Sobrien
1557169689Skanint
1558169689Skanestimate_move_cost (tree type)
1559169689Skan{
1560169689Skan  HOST_WIDE_INT size;
1561169689Skan
1562169689Skan  size = int_size_in_bytes (type);
1563169689Skan
1564169689Skan  if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1565169689Skan    /* Cost of a memcpy call, 3 arguments and the call.  */
1566169689Skan    return 4;
1567169689Skan  else
1568169689Skan    return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1569169689Skan}
1570169689Skan
1571169689Skan/* Used by estimate_num_insns.  Estimate number of instructions seen
1572169689Skan   by given statement.  */
1573169689Skan
157490075Sobrienstatic tree
1575169689Skanestimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
157690075Sobrien{
1577169689Skan  int *count = (int *) data;
1578169689Skan  tree x = *tp;
157990075Sobrien
1580169689Skan  if (IS_TYPE_OR_DECL_P (x))
1581169689Skan    {
1582169689Skan      *walk_subtrees = 0;
1583169689Skan      return NULL;
1584169689Skan    }
1585169689Skan  /* Assume that constants and references counts nothing.  These should
1586169689Skan     be majorized by amount of operations among them we count later
1587169689Skan     and are common target of CSE and similar optimizations.  */
1588169689Skan  else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
1589169689Skan    return NULL;
159090075Sobrien
1591169689Skan  switch (TREE_CODE (x))
159290075Sobrien    {
1593169689Skan    /* Containers have no cost.  */
1594169689Skan    case TREE_LIST:
1595169689Skan    case TREE_VEC:
1596169689Skan    case BLOCK:
1597169689Skan    case COMPONENT_REF:
1598169689Skan    case BIT_FIELD_REF:
1599169689Skan    case INDIRECT_REF:
1600169689Skan    case ALIGN_INDIRECT_REF:
1601169689Skan    case MISALIGNED_INDIRECT_REF:
1602169689Skan    case ARRAY_REF:
1603169689Skan    case ARRAY_RANGE_REF:
1604169689Skan    case OBJ_TYPE_REF:
1605169689Skan    case EXC_PTR_EXPR: /* ??? */
1606169689Skan    case FILTER_EXPR: /* ??? */
1607169689Skan    case COMPOUND_EXPR:
1608169689Skan    case BIND_EXPR:
1609169689Skan    case WITH_CLEANUP_EXPR:
1610169689Skan    case NOP_EXPR:
1611169689Skan    case VIEW_CONVERT_EXPR:
1612169689Skan    case SAVE_EXPR:
1613169689Skan    case ADDR_EXPR:
1614169689Skan    case COMPLEX_EXPR:
1615169689Skan    case RANGE_EXPR:
1616169689Skan    case CASE_LABEL_EXPR:
1617169689Skan    case SSA_NAME:
1618169689Skan    case CATCH_EXPR:
1619169689Skan    case EH_FILTER_EXPR:
1620169689Skan    case STATEMENT_LIST:
1621169689Skan    case ERROR_MARK:
1622169689Skan    case NON_LVALUE_EXPR:
1623169689Skan    case FDESC_EXPR:
1624169689Skan    case VA_ARG_EXPR:
1625169689Skan    case TRY_CATCH_EXPR:
1626169689Skan    case TRY_FINALLY_EXPR:
1627169689Skan    case LABEL_EXPR:
1628169689Skan    case GOTO_EXPR:
1629169689Skan    case RETURN_EXPR:
1630169689Skan    case EXIT_EXPR:
1631169689Skan    case LOOP_EXPR:
1632169689Skan    case PHI_NODE:
1633169689Skan    case WITH_SIZE_EXPR:
1634169689Skan    case OMP_CLAUSE:
1635169689Skan    case OMP_RETURN:
1636169689Skan    case OMP_CONTINUE:
1637169689Skan      break;
163890075Sobrien
1639169689Skan    /* We don't account constants for now.  Assume that the cost is amortized
1640169689Skan       by operations that do use them.  We may re-consider this decision once
1641169689Skan       we are able to optimize the tree before estimating its size and break
1642169689Skan       out static initializers.  */
1643169689Skan    case IDENTIFIER_NODE:
1644169689Skan    case INTEGER_CST:
1645169689Skan    case REAL_CST:
1646169689Skan    case COMPLEX_CST:
1647169689Skan    case VECTOR_CST:
1648169689Skan    case STRING_CST:
164990075Sobrien      *walk_subtrees = 0;
1650169689Skan      return NULL;
165190075Sobrien
1652169689Skan    /* Try to estimate the cost of assignments.  We have three cases to
1653169689Skan       deal with:
1654169689Skan	1) Simple assignments to registers;
1655169689Skan	2) Stores to things that must live in memory.  This includes
1656169689Skan	   "normal" stores to scalars, but also assignments of large
1657169689Skan	   structures, or constructors of big arrays;
1658169689Skan	3) TARGET_EXPRs.
165990075Sobrien
1660169689Skan       Let us look at the first two cases, assuming we have "a = b + C":
1661169689Skan       <modify_expr <var_decl "a"> <plus_expr <var_decl "b"> <constant C>>
1662169689Skan       If "a" is a GIMPLE register, the assignment to it is free on almost
1663169689Skan       any target, because "a" usually ends up in a real register.  Hence
1664169689Skan       the only cost of this expression comes from the PLUS_EXPR, and we
1665169689Skan       can ignore the MODIFY_EXPR.
1666169689Skan       If "a" is not a GIMPLE register, the assignment to "a" will most
1667169689Skan       likely be a real store, so the cost of the MODIFY_EXPR is the cost
1668169689Skan       of moving something into "a", which we compute using the function
1669169689Skan       estimate_move_cost.
1670169689Skan
1671169689Skan       The third case deals with TARGET_EXPRs, for which the semantics are
1672169689Skan       that a temporary is assigned, unless the TARGET_EXPR itself is being
1673169689Skan       assigned to something else.  In the latter case we do not need the
1674169689Skan       temporary.  E.g. in <modify_expr <var_decl "a"> <target_expr>>, the
1675169689Skan       MODIFY_EXPR is free.  */
1676169689Skan    case INIT_EXPR:
1677169689Skan    case MODIFY_EXPR:
1678169689Skan      /* Is the right and side a TARGET_EXPR?  */
1679169689Skan      if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
1680169689Skan	break;
1681169689Skan      /* ... fall through ...  */
1682169689Skan
1683169689Skan    case TARGET_EXPR:
1684169689Skan      x = TREE_OPERAND (x, 0);
1685169689Skan      /* Is this an assignments to a register?  */
1686169689Skan      if (is_gimple_reg (x))
1687169689Skan	break;
1688169689Skan      /* Otherwise it's a store, so fall through to compute the move cost.  */
1689169689Skan
1690169689Skan    case CONSTRUCTOR:
1691169689Skan      *count += estimate_move_cost (TREE_TYPE (x));
1692169689Skan      break;
1693169689Skan
1694169689Skan    /* Assign cost of 1 to usual operations.
1695169689Skan       ??? We may consider mapping RTL costs to this.  */
1696169689Skan    case COND_EXPR:
1697169689Skan    case VEC_COND_EXPR:
1698169689Skan
1699169689Skan    case PLUS_EXPR:
1700169689Skan    case MINUS_EXPR:
1701169689Skan    case MULT_EXPR:
1702169689Skan
1703169689Skan    case FIX_TRUNC_EXPR:
1704169689Skan    case FIX_CEIL_EXPR:
1705169689Skan    case FIX_FLOOR_EXPR:
1706169689Skan    case FIX_ROUND_EXPR:
1707169689Skan
1708169689Skan    case NEGATE_EXPR:
1709169689Skan    case FLOAT_EXPR:
1710169689Skan    case MIN_EXPR:
1711169689Skan    case MAX_EXPR:
1712169689Skan    case ABS_EXPR:
1713169689Skan
1714169689Skan    case LSHIFT_EXPR:
1715169689Skan    case RSHIFT_EXPR:
1716169689Skan    case LROTATE_EXPR:
1717169689Skan    case RROTATE_EXPR:
1718169689Skan    case VEC_LSHIFT_EXPR:
1719169689Skan    case VEC_RSHIFT_EXPR:
1720169689Skan
1721169689Skan    case BIT_IOR_EXPR:
1722169689Skan    case BIT_XOR_EXPR:
1723169689Skan    case BIT_AND_EXPR:
1724169689Skan    case BIT_NOT_EXPR:
1725169689Skan
1726169689Skan    case TRUTH_ANDIF_EXPR:
1727169689Skan    case TRUTH_ORIF_EXPR:
1728169689Skan    case TRUTH_AND_EXPR:
1729169689Skan    case TRUTH_OR_EXPR:
1730169689Skan    case TRUTH_XOR_EXPR:
1731169689Skan    case TRUTH_NOT_EXPR:
1732169689Skan
1733169689Skan    case LT_EXPR:
1734169689Skan    case LE_EXPR:
1735169689Skan    case GT_EXPR:
1736169689Skan    case GE_EXPR:
1737169689Skan    case EQ_EXPR:
1738169689Skan    case NE_EXPR:
1739169689Skan    case ORDERED_EXPR:
1740169689Skan    case UNORDERED_EXPR:
1741169689Skan
1742169689Skan    case UNLT_EXPR:
1743169689Skan    case UNLE_EXPR:
1744169689Skan    case UNGT_EXPR:
1745169689Skan    case UNGE_EXPR:
1746169689Skan    case UNEQ_EXPR:
1747169689Skan    case LTGT_EXPR:
1748169689Skan
1749169689Skan    case CONVERT_EXPR:
1750169689Skan
1751169689Skan    case CONJ_EXPR:
1752169689Skan
1753169689Skan    case PREDECREMENT_EXPR:
1754169689Skan    case PREINCREMENT_EXPR:
1755169689Skan    case POSTDECREMENT_EXPR:
1756169689Skan    case POSTINCREMENT_EXPR:
1757169689Skan
1758169689Skan    case SWITCH_EXPR:
1759169689Skan
1760169689Skan    case ASM_EXPR:
1761169689Skan
1762169689Skan    case REALIGN_LOAD_EXPR:
1763169689Skan
1764169689Skan    case REDUC_MAX_EXPR:
1765169689Skan    case REDUC_MIN_EXPR:
1766169689Skan    case REDUC_PLUS_EXPR:
1767169689Skan    case WIDEN_SUM_EXPR:
1768169689Skan    case DOT_PROD_EXPR:
1769169689Skan
1770169689Skan    case WIDEN_MULT_EXPR:
1771169689Skan
1772169689Skan    case RESX_EXPR:
1773169689Skan      *count += 1;
1774169689Skan      break;
1775169689Skan
1776169689Skan    /* Few special cases of expensive operations.  This is useful
1777169689Skan       to avoid inlining on functions having too many of these.  */
1778169689Skan    case TRUNC_DIV_EXPR:
1779169689Skan    case CEIL_DIV_EXPR:
1780169689Skan    case FLOOR_DIV_EXPR:
1781169689Skan    case ROUND_DIV_EXPR:
1782169689Skan    case EXACT_DIV_EXPR:
1783169689Skan    case TRUNC_MOD_EXPR:
1784169689Skan    case CEIL_MOD_EXPR:
1785169689Skan    case FLOOR_MOD_EXPR:
1786169689Skan    case ROUND_MOD_EXPR:
1787169689Skan    case RDIV_EXPR:
1788169689Skan      *count += 10;
1789169689Skan      break;
1790169689Skan    case CALL_EXPR:
1791169689Skan      {
1792169689Skan	tree decl = get_callee_fndecl (x);
1793169689Skan	tree arg;
1794169689Skan
1795169689Skan	if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
1796169689Skan	  switch (DECL_FUNCTION_CODE (decl))
1797169689Skan	    {
1798169689Skan	    case BUILT_IN_CONSTANT_P:
1799169689Skan	      *walk_subtrees = 0;
1800169689Skan	      return NULL_TREE;
1801169689Skan	    case BUILT_IN_EXPECT:
1802169689Skan	      return NULL_TREE;
1803169689Skan	    default:
1804169689Skan	      break;
1805169689Skan	    }
1806169689Skan
1807169689Skan	/* Our cost must be kept in sync with cgraph_estimate_size_after_inlining
1808169689Skan	   that does use function declaration to figure out the arguments.  */
1809169689Skan	if (!decl)
1810169689Skan	  {
1811169689Skan	    for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
1812169689Skan	      *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
1813169689Skan	  }
1814169689Skan	else
1815169689Skan	  {
1816169689Skan	    for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
1817169689Skan	      *count += estimate_move_cost (TREE_TYPE (arg));
1818169689Skan	  }
1819169689Skan
1820169689Skan	*count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
1821169689Skan	break;
1822169689Skan      }
1823169689Skan
1824169689Skan    case OMP_PARALLEL:
1825169689Skan    case OMP_FOR:
1826169689Skan    case OMP_SECTIONS:
1827169689Skan    case OMP_SINGLE:
1828169689Skan    case OMP_SECTION:
1829169689Skan    case OMP_MASTER:
1830169689Skan    case OMP_ORDERED:
1831169689Skan    case OMP_CRITICAL:
1832169689Skan    case OMP_ATOMIC:
1833169689Skan      /* OpenMP directives are generally very expensive.  */
1834169689Skan      *count += 40;
1835169689Skan      break;
1836169689Skan
1837169689Skan    default:
1838169689Skan      gcc_unreachable ();
183990075Sobrien    }
1840169689Skan  return NULL;
1841169689Skan}
1842169689Skan
1843169689Skan/* Estimate number of instructions that will be created by expanding EXPR.  */
1844169689Skan
1845169689Skanint
1846169689Skanestimate_num_insns (tree expr)
1847169689Skan{
1848169689Skan  int num = 0;
1849169689Skan  struct pointer_set_t *visited_nodes;
1850169689Skan  basic_block bb;
1851169689Skan  block_stmt_iterator bsi;
1852169689Skan  struct function *my_function;
1853169689Skan
1854169689Skan  /* If we're given an entire function, walk the CFG.  */
1855169689Skan  if (TREE_CODE (expr) == FUNCTION_DECL)
1856117395Skan    {
1857169689Skan      my_function = DECL_STRUCT_FUNCTION (expr);
1858169689Skan      gcc_assert (my_function && my_function->cfg);
1859169689Skan      visited_nodes = pointer_set_create ();
1860169689Skan      FOR_EACH_BB_FN (bb, my_function)
1861169689Skan	{
1862169689Skan	  for (bsi = bsi_start (bb);
1863169689Skan	       !bsi_end_p (bsi);
1864169689Skan	       bsi_next (&bsi))
1865169689Skan	    {
1866169689Skan	      walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
1867169689Skan			 &num, visited_nodes);
1868169689Skan	    }
1869169689Skan	}
1870169689Skan      pointer_set_destroy (visited_nodes);
1871117395Skan    }
1872169689Skan  else
1873169689Skan    walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
1874117395Skan
1875169689Skan  return num;
1876169689Skan}
187790075Sobrien
1878169689Skantypedef struct function *function_p;
1879169689Skan
1880169689SkanDEF_VEC_P(function_p);
1881169689SkanDEF_VEC_ALLOC_P(function_p,heap);
1882169689Skan
1883169689Skan/* Initialized with NOGC, making this poisonous to the garbage collector.  */
1884169689Skanstatic VEC(function_p,heap) *cfun_stack;
1885169689Skan
1886169689Skanvoid
1887169689Skanpush_cfun (struct function *new_cfun)
1888169689Skan{
1889169689Skan  VEC_safe_push (function_p, heap, cfun_stack, cfun);
1890169689Skan  cfun = new_cfun;
1891169689Skan}
1892169689Skan
1893169689Skanvoid
1894169689Skanpop_cfun (void)
1895169689Skan{
1896169689Skan  cfun = VEC_pop (function_p, cfun_stack);
1897169689Skan}
1898169689Skan
1899169689Skan/* Install new lexical TREE_BLOCK underneath 'current_block'.  */
1900169689Skanstatic void
1901169689Skanadd_lexical_block (tree current_block, tree new_block)
1902169689Skan{
1903169689Skan  tree *blk_p;
1904169689Skan
1905169689Skan  /* Walk to the last sub-block.  */
1906169689Skan  for (blk_p = &BLOCK_SUBBLOCKS (current_block);
1907169689Skan       *blk_p;
1908169689Skan       blk_p = &TREE_CHAIN (*blk_p))
1909169689Skan    ;
1910169689Skan  *blk_p = new_block;
1911169689Skan  BLOCK_SUPERCONTEXT (new_block) = current_block;
1912169689Skan}
1913169689Skan
1914169689Skan/* If *TP is a CALL_EXPR, replace it with its inline expansion.  */
1915169689Skan
1916169689Skanstatic bool
1917169689Skanexpand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
1918169689Skan{
1919169689Skan  copy_body_data *id;
1920169689Skan  tree t;
1921169689Skan  tree use_retvar;
1922169689Skan  tree fn;
1923169689Skan  splay_tree st;
1924169689Skan  tree args;
1925169689Skan  tree return_slot_addr;
1926169689Skan  tree modify_dest;
1927169689Skan  location_t saved_location;
1928169689Skan  struct cgraph_edge *cg_edge;
1929169689Skan  const char *reason;
1930169689Skan  basic_block return_block;
1931169689Skan  edge e;
1932169689Skan  block_stmt_iterator bsi, stmt_bsi;
1933169689Skan  bool successfully_inlined = FALSE;
1934169689Skan  bool purge_dead_abnormal_edges;
1935169689Skan  tree t_step;
1936169689Skan  tree var;
1937169689Skan
1938169689Skan  /* See what we've got.  */
1939169689Skan  id = (copy_body_data *) data;
1940169689Skan  t = *tp;
1941169689Skan
1942169689Skan  /* Set input_location here so we get the right instantiation context
1943169689Skan     if we call instantiate_decl from inlinable_function_p.  */
1944169689Skan  saved_location = input_location;
1945169689Skan  if (EXPR_HAS_LOCATION (t))
1946169689Skan    input_location = EXPR_LOCATION (t);
1947169689Skan
194890075Sobrien  /* From here on, we're only interested in CALL_EXPRs.  */
194990075Sobrien  if (TREE_CODE (t) != CALL_EXPR)
1950169689Skan    goto egress;
195190075Sobrien
195290075Sobrien  /* First, see if we can figure out what function is being called.
195390075Sobrien     If we cannot, then there is no hope of inlining the function.  */
195490075Sobrien  fn = get_callee_fndecl (t);
195590075Sobrien  if (!fn)
1956169689Skan    goto egress;
195790075Sobrien
1958132718Skan  /* Turn forward declarations into real ones.  */
1959132718Skan  fn = cgraph_node (fn)->decl;
1960132718Skan
196190075Sobrien  /* If fn is a declaration of a function in a nested scope that was
196290075Sobrien     globally declared inline, we don't set its DECL_INITIAL.
196390075Sobrien     However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
196490075Sobrien     C++ front-end uses it for cdtors to refer to their internal
196590075Sobrien     declarations, that are not real functions.  Fortunately those
196690075Sobrien     don't have trees to be saved, so we can tell by checking their
196790075Sobrien     DECL_SAVED_TREE.  */
196890075Sobrien  if (! DECL_INITIAL (fn)
196990075Sobrien      && DECL_ABSTRACT_ORIGIN (fn)
197090075Sobrien      && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
197190075Sobrien    fn = DECL_ABSTRACT_ORIGIN (fn);
197290075Sobrien
1973169689Skan  /* Objective C and fortran still calls tree_rest_of_compilation directly.
1974169689Skan     Kill this check once this is fixed.  */
1975169689Skan  if (!id->dst_node->analyzed)
1976169689Skan    goto egress;
1977169689Skan
1978169689Skan  cg_edge = cgraph_edge (id->dst_node, stmt);
1979169689Skan
1980169689Skan  /* Constant propagation on argument done during previous inlining
1981169689Skan     may create new direct call.  Produce an edge for it.  */
1982169689Skan  if (!cg_edge)
1983169689Skan    {
1984169689Skan      struct cgraph_node *dest = cgraph_node (fn);
1985169689Skan
1986169689Skan      /* We have missing edge in the callgraph.  This can happen in one case
1987169689Skan         where previous inlining turned indirect call into direct call by
1988169689Skan         constant propagating arguments.  In all other cases we hit a bug
1989169689Skan         (incorrect node sharing is most common reason for missing edges.  */
1990169689Skan      gcc_assert (dest->needed || !flag_unit_at_a_time);
1991169689Skan      cgraph_create_edge (id->dst_node, dest, stmt,
1992169689Skan			  bb->count, bb->loop_depth)->inline_failed
1993169689Skan	= N_("originally indirect function call not considered for inlining");
1994169689Skan      goto egress;
1995169689Skan    }
1996169689Skan
199790075Sobrien  /* Don't try to inline functions that are not well-suited to
199890075Sobrien     inlining.  */
1999169689Skan  if (!cgraph_inline_p (cg_edge, &reason))
2000117395Skan    {
2001169689Skan      if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
2002169689Skan	  /* Avoid warnings during early inline pass. */
2003169689Skan	  && (!flag_unit_at_a_time || cgraph_global_info_ready))
2004117395Skan	{
2005169689Skan	  sorry ("inlining failed in call to %q+F: %s", fn, reason);
2006132718Skan	  sorry ("called from here");
2007132718Skan	}
2008132718Skan      else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
2009132718Skan	       && !DECL_IN_SYSTEM_HEADER (fn)
2010146895Skan	       && strlen (reason)
2011169689Skan	       && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
2012169689Skan	       /* Avoid warnings during early inline pass. */
2013169689Skan	       && (!flag_unit_at_a_time || cgraph_global_info_ready))
2014132718Skan	{
2015169689Skan	  warning (OPT_Winline, "inlining failed in call to %q+F: %s",
2016169689Skan		   fn, reason);
2017169689Skan	  warning (OPT_Winline, "called from here");
2018117395Skan	}
2019169689Skan      goto egress;
2020117395Skan    }
2021169689Skan  fn = cg_edge->callee->decl;
202290075Sobrien
2023169689Skan#ifdef ENABLE_CHECKING
2024169689Skan  if (cg_edge->callee->decl != id->dst_node->decl)
2025169689Skan    verify_cgraph_node (cg_edge->callee);
2026169689Skan#endif
202790075Sobrien
2028169689Skan  /* We will be inlining this callee.  */
2029169689Skan  id->eh_region = lookup_stmt_eh_region (stmt);
203090075Sobrien
2031169689Skan  /* Split the block holding the CALL_EXPR.  */
2032169689Skan  e = split_block (bb, stmt);
2033169689Skan  bb = e->src;
2034169689Skan  return_block = e->dest;
2035169689Skan  remove_edge (e);
2036169689Skan
2037169689Skan  /* split_block splits after the statement; work around this by
2038169689Skan     moving the call into the second block manually.  Not pretty,
2039169689Skan     but seems easier than doing the CFG manipulation by hand
2040169689Skan     when the CALL_EXPR is in the last statement of BB.  */
2041169689Skan  stmt_bsi = bsi_last (bb);
2042169689Skan  bsi_remove (&stmt_bsi, false);
2043169689Skan
2044169689Skan  /* If the CALL_EXPR was in the last statement of BB, it may have
2045169689Skan     been the source of abnormal edges.  In this case, schedule
2046169689Skan     the removal of dead abnormal edges.  */
2047169689Skan  bsi = bsi_start (return_block);
2048169689Skan  if (bsi_end_p (bsi))
2049169689Skan    {
2050169689Skan      bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
2051169689Skan      purge_dead_abnormal_edges = true;
2052169689Skan    }
2053169689Skan  else
2054169689Skan    {
2055169689Skan      bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
2056169689Skan      purge_dead_abnormal_edges = false;
2057169689Skan    }
2058169689Skan
2059169689Skan  stmt_bsi = bsi_start (return_block);
2060169689Skan
2061117395Skan  /* Build a block containing code to initialize the arguments, the
2062117395Skan     actual inline expansion of the body, and a label for the return
2063117395Skan     statements within the function to jump to.  The type of the
2064117395Skan     statement expression is the return type of the function call.  */
2065169689Skan  id->block = make_node (BLOCK);
2066169689Skan  BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
2067169689Skan  BLOCK_SOURCE_LOCATION (id->block) = input_location;
2068169689Skan  add_lexical_block (TREE_BLOCK (stmt), id->block);
2069117395Skan
207090075Sobrien  /* Local declarations will be replaced by their equivalents in this
207190075Sobrien     map.  */
207290075Sobrien  st = id->decl_map;
207390075Sobrien  id->decl_map = splay_tree_new (splay_tree_compare_pointers,
207490075Sobrien				 NULL, NULL);
207590075Sobrien
207690075Sobrien  /* Initialize the parameters.  */
2077132718Skan  args = TREE_OPERAND (t, 1);
2078132718Skan
2079169689Skan  /* Record the function we are about to inline.  */
2080169689Skan  id->src_fn = fn;
2081169689Skan  id->src_node = cg_edge->callee;
208290075Sobrien
2083169689Skan  initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
2084117395Skan
2085169689Skan  if (DECL_INITIAL (fn))
2086169689Skan    add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
208790075Sobrien
208890075Sobrien  /* Return statements in the function body will be replaced by jumps
208990075Sobrien     to the RET_LABEL.  */
209090075Sobrien
2091169689Skan  gcc_assert (DECL_INITIAL (fn));
2092169689Skan  gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
209390075Sobrien
2094169689Skan  /* Find the lhs to which the result of this call is assigned.  */
2095169689Skan  return_slot_addr = NULL;
2096169689Skan  if (TREE_CODE (stmt) == MODIFY_EXPR)
2097169689Skan    {
2098169689Skan      modify_dest = TREE_OPERAND (stmt, 0);
209990075Sobrien
2100169689Skan      /* The function which we are inlining might not return a value,
2101169689Skan	 in which case we should issue a warning that the function
2102169689Skan	 does not return a value.  In that case the optimizers will
2103169689Skan	 see that the variable to which the value is assigned was not
2104169689Skan	 initialized.  We do not want to issue a warning about that
2105169689Skan	 uninitialized variable.  */
2106169689Skan      if (DECL_P (modify_dest))
2107169689Skan	TREE_NO_WARNING (modify_dest) = 1;
2108169689Skan      if (CALL_EXPR_RETURN_SLOT_OPT (t))
2109169689Skan	{
2110169689Skan	  return_slot_addr = build_fold_addr_expr (modify_dest);
2111169689Skan	  STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2112169689Skan	  modify_dest = NULL;
2113169689Skan	}
2114169689Skan    }
2115169689Skan  else
2116169689Skan    modify_dest = NULL;
211790075Sobrien
211890075Sobrien  /* Declare the return variable for the function.  */
2119169689Skan  declare_return_variable (id, return_slot_addr,
2120169689Skan			   modify_dest, &use_retvar);
212190075Sobrien
2122169689Skan  /* This is it.  Duplicate the callee body.  Assume callee is
2123169689Skan     pre-gimplified.  Note that we must not alter the caller
2124169689Skan     function in any way before this point, as this CALL_EXPR may be
2125169689Skan     a self-referential call; if we're calling ourselves, we need to
2126169689Skan     duplicate our body before altering anything.  */
2127169689Skan  copy_body (id, bb->count, bb->frequency, bb, return_block);
212890075Sobrien
2129169689Skan  /* Add local vars in this inlined callee to caller.  */
2130169689Skan  t_step = id->src_cfun->unexpanded_var_list;
2131169689Skan  for (; t_step; t_step = TREE_CHAIN (t_step))
2132117395Skan    {
2133169689Skan      var = TREE_VALUE (t_step);
2134169689Skan      if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2135169689Skan	cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2136169689Skan					       cfun->unexpanded_var_list);
2137169689Skan      else
2138169689Skan	cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
2139169689Skan					       cfun->unexpanded_var_list);
2140117395Skan    }
214190075Sobrien
214290075Sobrien  /* Clean up.  */
214390075Sobrien  splay_tree_delete (id->decl_map);
214490075Sobrien  id->decl_map = st;
214590075Sobrien
2146169689Skan  /* If the inlined function returns a result that we care about,
2147169689Skan     clobber the CALL_EXPR with a reference to the return variable.  */
2148169689Skan  if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
2149169689Skan    {
2150169689Skan      *tp = use_retvar;
2151169689Skan      maybe_clean_or_replace_eh_stmt (stmt, stmt);
2152169689Skan    }
2153169689Skan  else
2154169689Skan    /* We're modifying a TSI owned by gimple_expand_calls_inline();
2155169689Skan       tsi_delink() will leave the iterator in a sane state.  */
2156169689Skan    bsi_remove (&stmt_bsi, true);
215790075Sobrien
2158169689Skan  if (purge_dead_abnormal_edges)
2159169689Skan    tree_purge_dead_abnormal_call_edges (return_block);
216090075Sobrien
216190075Sobrien  /* If the value of the new expression is ignored, that's OK.  We
216290075Sobrien     don't warn about this for CALL_EXPRs, so we shouldn't warn about
216390075Sobrien     the equivalent inlined version either.  */
216490075Sobrien  TREE_USED (*tp) = 1;
216590075Sobrien
2166169689Skan  /* Output the inlining info for this abstract function, since it has been
2167169689Skan     inlined.  If we don't do this now, we can lose the information about the
2168169689Skan     variables in the function when the blocks get blown away as soon as we
2169169689Skan     remove the cgraph node.  */
2170169689Skan  (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
2171169689Skan
2172132718Skan  /* Update callgraph if needed.  */
2173169689Skan  cgraph_remove_node (cg_edge->callee);
217490075Sobrien
2175169689Skan  id->block = NULL_TREE;
2176169689Skan  successfully_inlined = TRUE;
217790075Sobrien
2178169689Skan egress:
2179169689Skan  input_location = saved_location;
2180169689Skan  return successfully_inlined;
2181169689Skan}
218290075Sobrien
2183169689Skan/* Expand call statements reachable from STMT_P.
2184169689Skan   We can only have CALL_EXPRs as the "toplevel" tree code or nested
2185169689Skan   in a MODIFY_EXPR.  See tree-gimple.c:get_call_expr_in().  We can
2186169689Skan   unfortunately not use that function here because we need a pointer
2187169689Skan   to the CALL_EXPR, not the tree itself.  */
218890075Sobrien
2189169689Skanstatic bool
2190169689Skangimple_expand_calls_inline (basic_block bb, copy_body_data *id)
2191169689Skan{
2192169689Skan  block_stmt_iterator bsi;
219390075Sobrien
2194169689Skan  /* Register specific tree functions.  */
2195169689Skan  tree_register_cfg_hooks ();
2196169689Skan  for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2197169689Skan    {
2198169689Skan      tree *expr_p = bsi_stmt_ptr (bsi);
2199169689Skan      tree stmt = *expr_p;
2200169689Skan
2201169689Skan      if (TREE_CODE (*expr_p) == MODIFY_EXPR)
2202169689Skan	expr_p = &TREE_OPERAND (*expr_p, 1);
2203169689Skan      if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
2204169689Skan	expr_p = &TREE_OPERAND (*expr_p, 0);
2205169689Skan      if (TREE_CODE (*expr_p) == CALL_EXPR)
2206169689Skan	if (expand_call_inline (bb, stmt, expr_p, id))
2207169689Skan	  return true;
2208169689Skan    }
2209169689Skan  return false;
221090075Sobrien}
221190075Sobrien
221290075Sobrien/* Expand calls to inline functions in the body of FN.  */
221390075Sobrien
221490075Sobrienvoid
2215132718Skanoptimize_inline_calls (tree fn)
221690075Sobrien{
2217169689Skan  copy_body_data id;
221890075Sobrien  tree prev_fn;
2219169689Skan  basic_block bb;
2220132718Skan  /* There is no point in performing inlining if errors have already
2221132718Skan     occurred -- and we might crash if we try to inline invalid
2222132718Skan     code.  */
2223132718Skan  if (errorcount || sorrycount)
2224132718Skan    return;
2225132718Skan
222690075Sobrien  /* Clear out ID.  */
222790075Sobrien  memset (&id, 0, sizeof (id));
222890075Sobrien
2229169689Skan  id.src_node = id.dst_node = cgraph_node (fn);
2230169689Skan  id.dst_fn = fn;
223190075Sobrien  /* Or any functions that aren't finished yet.  */
223290075Sobrien  prev_fn = NULL_TREE;
223390075Sobrien  if (current_function_decl)
223490075Sobrien    {
2235169689Skan      id.dst_fn = current_function_decl;
223690075Sobrien      prev_fn = current_function_decl;
223790075Sobrien    }
223890075Sobrien
2239169689Skan  id.copy_decl = copy_decl_maybe_to_var;
2240169689Skan  id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2241169689Skan  id.transform_new_cfg = false;
2242169689Skan  id.transform_return_to_modify = true;
2243169689Skan  id.transform_lang_insert_block = false;
2244117395Skan
2245169689Skan  push_gimplify_context ();
224690075Sobrien
2247169689Skan  /* Reach the trees by walking over the CFG, and note the
2248169689Skan     enclosing basic-blocks in the call edges.  */
2249169689Skan  /* We walk the blocks going forward, because inlined function bodies
2250169689Skan     will split id->current_basic_block, and the new blocks will
2251169689Skan     follow it; we'll trudge through them, processing their CALL_EXPRs
2252169689Skan     along the way.  */
2253169689Skan  FOR_EACH_BB (bb)
2254169689Skan    gimple_expand_calls_inline (bb, &id);
225590075Sobrien
2256169689Skan  pop_gimplify_context (NULL);
2257169689Skan  /* Renumber the (code) basic_blocks consecutively.  */
2258169689Skan  compact_blocks ();
2259169689Skan  /* Renumber the lexical scoping (non-code) blocks consecutively.  */
2260169689Skan  number_blocks (fn);
226190075Sobrien
2262169689Skan#ifdef ENABLE_CHECKING
226390075Sobrien    {
2264169689Skan      struct cgraph_edge *e;
2265117395Skan
2266169689Skan      verify_cgraph_node (id.dst_node);
2267169689Skan
2268169689Skan      /* Double check that we inlined everything we are supposed to inline.  */
2269169689Skan      for (e = id.dst_node->callees; e; e = e->next_callee)
2270169689Skan	gcc_assert (e->inline_failed);
227190075Sobrien    }
2272169689Skan#endif
2273169689Skan  /* We need to rescale frequencies again to peak at REG_BR_PROB_BASE
2274169689Skan     as inlining loops might increase the maximum.  */
2275169689Skan  if (ENTRY_BLOCK_PTR->count)
2276169689Skan    counts_to_freqs ();
2277169689Skan  fold_cond_expr_cond ();
227890075Sobrien}
227990075Sobrien
2280169689Skan/* FN is a function that has a complete body, and CLONE is a function whose
2281169689Skan   body is to be set to a copy of FN, mapping argument declarations according
2282169689Skan   to the ARG_MAP splay_tree.  */
228390075Sobrien
228490075Sobrienvoid
2285132718Skanclone_body (tree clone, tree fn, void *arg_map)
228690075Sobrien{
2287169689Skan  copy_body_data id;
228890075Sobrien
2289169689Skan  /* Clone the body, as if we were making an inline call.  But, remap the
2290169689Skan     parameters in the callee to the parameters of caller.  */
229190075Sobrien  memset (&id, 0, sizeof (id));
2292169689Skan  id.src_fn = fn;
2293169689Skan  id.dst_fn = clone;
2294169689Skan  id.src_cfun = DECL_STRUCT_FUNCTION (fn);
229590075Sobrien  id.decl_map = (splay_tree)arg_map;
229690075Sobrien
2297169689Skan  id.copy_decl = copy_decl_no_change;
2298169689Skan  id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2299169689Skan  id.transform_new_cfg = true;
2300169689Skan  id.transform_return_to_modify = false;
2301169689Skan  id.transform_lang_insert_block = true;
230290075Sobrien
2303169689Skan  /* We're not inside any EH region.  */
2304169689Skan  id.eh_region = -1;
2305169689Skan
230690075Sobrien  /* Actually copy the body.  */
2307169689Skan  append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
230890075Sobrien}
230990075Sobrien
2310169689Skan/* Passed to walk_tree.  Copies the node pointed to, if appropriate.  */
231190075Sobrien
2312117395Skantree
2313169689Skancopy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
231490075Sobrien{
2315169689Skan  enum tree_code code = TREE_CODE (*tp);
2316117395Skan
2317169689Skan  /* We make copies of most nodes.  */
2318169689Skan  if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
2319169689Skan      || code == TREE_LIST
2320169689Skan      || code == TREE_VEC
2321169689Skan      || code == TYPE_DECL
2322169689Skan      || code == OMP_CLAUSE)
2323169689Skan    {
2324169689Skan      /* Because the chain gets clobbered when we make a copy, we save it
2325169689Skan	 here.  */
2326169689Skan      tree chain = TREE_CHAIN (*tp);
2327169689Skan      tree new;
232890075Sobrien
2329169689Skan      /* Copy the node.  */
2330169689Skan      new = copy_node (*tp);
233190075Sobrien
2332169689Skan      /* Propagate mudflap marked-ness.  */
2333169689Skan      if (flag_mudflap && mf_marked_p (*tp))
2334169689Skan        mf_mark (new);
233590075Sobrien
2336169689Skan      *tp = new;
2337169689Skan
2338169689Skan      /* Now, restore the chain, if appropriate.  That will cause
2339169689Skan	 walk_tree to walk into the chain as well.  */
2340169689Skan      if (code == PARM_DECL
2341169689Skan	  || code == TREE_LIST
2342169689Skan	  || code == OMP_CLAUSE)
2343169689Skan	TREE_CHAIN (*tp) = chain;
2344169689Skan
2345169689Skan      /* For now, we don't update BLOCKs when we make copies.  So, we
2346169689Skan	 have to nullify all BIND_EXPRs.  */
2347169689Skan      if (TREE_CODE (*tp) == BIND_EXPR)
2348169689Skan	BIND_EXPR_BLOCK (*tp) = NULL_TREE;
2349169689Skan    }
2350169689Skan  else if (code == CONSTRUCTOR)
235190075Sobrien    {
2352169689Skan      /* CONSTRUCTOR nodes need special handling because
2353169689Skan         we need to duplicate the vector of elements.  */
2354169689Skan      tree new;
2355117395Skan
2356169689Skan      new = copy_node (*tp);
2357169689Skan
2358169689Skan      /* Propagate mudflap marked-ness.  */
2359169689Skan      if (flag_mudflap && mf_marked_p (*tp))
2360169689Skan        mf_mark (new);
2361169689Skan
2362169689Skan      CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
2363169689Skan					 CONSTRUCTOR_ELTS (*tp));
2364169689Skan      *tp = new;
236590075Sobrien    }
2366169689Skan  else if (TREE_CODE_CLASS (code) == tcc_type)
2367169689Skan    *walk_subtrees = 0;
2368169689Skan  else if (TREE_CODE_CLASS (code) == tcc_declaration)
2369169689Skan    *walk_subtrees = 0;
2370169689Skan  else if (TREE_CODE_CLASS (code) == tcc_constant)
2371169689Skan    *walk_subtrees = 0;
2372169689Skan  else
2373169689Skan    gcc_assert (code != STATEMENT_LIST);
2374169689Skan  return NULL_TREE;
2375169689Skan}
237690075Sobrien
2377169689Skan/* The SAVE_EXPR pointed to by TP is being copied.  If ST contains
2378169689Skan   information indicating to what new SAVE_EXPR this one should be mapped,
2379169689Skan   use that one.  Otherwise, create a new node and enter it in ST.  FN is
2380169689Skan   the function into which the copy will be placed.  */
238190075Sobrien
2382169689Skanstatic void
2383169689Skanremap_save_expr (tree *tp, void *st_, int *walk_subtrees)
2384169689Skan{
2385169689Skan  splay_tree st = (splay_tree) st_;
2386169689Skan  splay_tree_node n;
2387169689Skan  tree t;
238890075Sobrien
2389169689Skan  /* See if we already encountered this SAVE_EXPR.  */
2390169689Skan  n = splay_tree_lookup (st, (splay_tree_key) *tp);
239190075Sobrien
2392169689Skan  /* If we didn't already remap this SAVE_EXPR, do so now.  */
2393169689Skan  if (!n)
239490075Sobrien    {
2395169689Skan      t = copy_node (*tp);
2396169689Skan
2397169689Skan      /* Remember this SAVE_EXPR.  */
2398169689Skan      splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
2399169689Skan      /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
2400169689Skan      splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
240190075Sobrien    }
2402169689Skan  else
240390075Sobrien    {
2404169689Skan      /* We've already walked into this SAVE_EXPR; don't do it again.  */
2405169689Skan      *walk_subtrees = 0;
2406169689Skan      t = (tree) n->value;
2407169689Skan    }
240890075Sobrien
2409169689Skan  /* Replace this SAVE_EXPR with the copy.  */
2410169689Skan  *tp = t;
2411169689Skan}
241290075Sobrien
2413169689Skan/* Called via walk_tree.  If *TP points to a DECL_STMT for a local label,
2414169689Skan   copies the declaration and enters it in the splay_tree in DATA (which is
2415169689Skan   really an `copy_body_data *').  */
241690075Sobrien
2417169689Skanstatic tree
2418169689Skanmark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2419169689Skan			void *data)
2420169689Skan{
2421169689Skan  copy_body_data *id = (copy_body_data *) data;
242290075Sobrien
2423169689Skan  /* Don't walk into types.  */
2424169689Skan  if (TYPE_P (*tp))
2425169689Skan    *walk_subtrees = 0;
242690075Sobrien
2427169689Skan  else if (TREE_CODE (*tp) == LABEL_EXPR)
242890075Sobrien    {
2429169689Skan      tree decl = TREE_OPERAND (*tp, 0);
2430169689Skan
2431169689Skan      /* Copy the decl and remember the copy.  */
2432169689Skan      insert_decl_map (id, decl, id->copy_decl (decl, id));
243390075Sobrien    }
243490075Sobrien
2435169689Skan  return NULL_TREE;
2436169689Skan}
243790075Sobrien
2438169689Skan/* Perform any modifications to EXPR required when it is unsaved.  Does
2439169689Skan   not recurse into EXPR's subtrees.  */
2440169689Skan
2441169689Skanstatic void
2442169689Skanunsave_expr_1 (tree expr)
2443169689Skan{
2444169689Skan  switch (TREE_CODE (expr))
244590075Sobrien    {
2446169689Skan    case TARGET_EXPR:
2447169689Skan      /* Don't mess with a TARGET_EXPR that hasn't been expanded.
2448169689Skan         It's OK for this to happen if it was part of a subtree that
2449169689Skan         isn't immediately expanded, such as operand 2 of another
2450169689Skan         TARGET_EXPR.  */
2451169689Skan      if (TREE_OPERAND (expr, 1))
2452169689Skan	break;
245390075Sobrien
2454169689Skan      TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
2455169689Skan      TREE_OPERAND (expr, 3) = NULL_TREE;
245690075Sobrien      break;
245790075Sobrien
2458169689Skan    default:
245990075Sobrien      break;
2460169689Skan    }
2461169689Skan}
246290075Sobrien
2463169689Skan/* Called via walk_tree when an expression is unsaved.  Using the
2464169689Skan   splay_tree pointed to by ST (which is really a `splay_tree'),
2465169689Skan   remaps all local declarations to appropriate replacements.  */
246690075Sobrien
2467169689Skanstatic tree
2468169689Skanunsave_r (tree *tp, int *walk_subtrees, void *data)
2469169689Skan{
2470169689Skan  copy_body_data *id = (copy_body_data *) data;
2471169689Skan  splay_tree st = id->decl_map;
2472169689Skan  splay_tree_node n;
247390075Sobrien
2474169689Skan  /* Only a local declaration (variable or label).  */
2475169689Skan  if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
2476169689Skan      || TREE_CODE (*tp) == LABEL_DECL)
2477169689Skan    {
2478169689Skan      /* Lookup the declaration.  */
2479169689Skan      n = splay_tree_lookup (st, (splay_tree_key) *tp);
248090075Sobrien
2481169689Skan      /* If it's there, remap it.  */
2482169689Skan      if (n)
2483169689Skan	*tp = (tree) n->value;
2484169689Skan    }
248590075Sobrien
2486169689Skan  else if (TREE_CODE (*tp) == STATEMENT_LIST)
2487169689Skan    copy_statement_list (tp);
2488169689Skan  else if (TREE_CODE (*tp) == BIND_EXPR)
2489169689Skan    copy_bind_expr (tp, walk_subtrees, id);
2490169689Skan  else if (TREE_CODE (*tp) == SAVE_EXPR)
2491169689Skan    remap_save_expr (tp, st, walk_subtrees);
2492169689Skan  else
2493169689Skan    {
2494169689Skan      copy_tree_r (tp, walk_subtrees, NULL);
249590075Sobrien
2496169689Skan      /* Do whatever unsaving is required.  */
2497169689Skan      unsave_expr_1 (*tp);
2498169689Skan    }
249990075Sobrien
2500169689Skan  /* Keep iterating.  */
2501169689Skan  return NULL_TREE;
2502169689Skan}
250390075Sobrien
2504169689Skan/* Copies everything in EXPR and replaces variables, labels
2505169689Skan   and SAVE_EXPRs local to EXPR.  */
250690075Sobrien
2507169689Skantree
2508169689Skanunsave_expr_now (tree expr)
2509169689Skan{
2510169689Skan  copy_body_data id;
251190075Sobrien
2512169689Skan  /* There's nothing to do for NULL_TREE.  */
2513169689Skan  if (expr == 0)
2514169689Skan    return expr;
251590075Sobrien
2516169689Skan  /* Set up ID.  */
2517169689Skan  memset (&id, 0, sizeof (id));
2518169689Skan  id.src_fn = current_function_decl;
2519169689Skan  id.dst_fn = current_function_decl;
2520169689Skan  id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
252190075Sobrien
2522169689Skan  id.copy_decl = copy_decl_no_change;
2523169689Skan  id.transform_call_graph_edges = CB_CGE_DUPLICATE;
2524169689Skan  id.transform_new_cfg = false;
2525169689Skan  id.transform_return_to_modify = false;
2526169689Skan  id.transform_lang_insert_block = false;
252790075Sobrien
2528169689Skan  /* Walk the tree once to find local labels.  */
2529169689Skan  walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
2530117395Skan
2531169689Skan  /* Walk the tree again, copying, remapping, and unsaving.  */
2532169689Skan  walk_tree (&expr, unsave_r, &id, NULL);
2533117395Skan
2534169689Skan  /* Clean up.  */
2535169689Skan  splay_tree_delete (id.decl_map);
2536169689Skan
2537169689Skan  return expr;
2538169689Skan}
2539169689Skan
2540169689Skan/* Allow someone to determine if SEARCH is a child of TOP from gdb.  */
2541169689Skan
2542169689Skanstatic tree
2543169689Skandebug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
2544169689Skan{
2545169689Skan  if (*tp == data)
2546169689Skan    return (tree) data;
2547169689Skan  else
2548169689Skan    return NULL;
2549169689Skan}
2550169689Skan
2551169689Skanbool
2552169689Skandebug_find_tree (tree top, tree search)
2553169689Skan{
2554169689Skan  return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
2555169689Skan}
2556169689Skan
2557169689Skan
2558169689Skan/* Declare the variables created by the inliner.  Add all the variables in
2559169689Skan   VARS to BIND_EXPR.  */
2560169689Skan
2561169689Skanstatic void
2562169689Skandeclare_inline_vars (tree block, tree vars)
2563169689Skan{
2564169689Skan  tree t;
2565169689Skan  for (t = vars; t; t = TREE_CHAIN (t))
2566169689Skan    {
2567169689Skan      DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
2568169689Skan      gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
2569169689Skan      cfun->unexpanded_var_list =
2570169689Skan	tree_cons (NULL_TREE, t,
2571169689Skan		   cfun->unexpanded_var_list);
257290075Sobrien    }
257390075Sobrien
2574169689Skan  if (block)
2575169689Skan    BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
2576169689Skan}
257790075Sobrien
2578169689Skan
2579169689Skan/* Copy NODE (which must be a DECL).  The DECL originally was in the FROM_FN,
2580169689Skan   but now it will be in the TO_FN.  PARM_TO_VAR means enable PARM_DECL to
2581169689Skan   VAR_DECL translation.  */
2582169689Skan
2583169689Skanstatic tree
2584169689Skancopy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
2585169689Skan{
2586169689Skan  /* Don't generate debug information for the copy if we wouldn't have
2587169689Skan     generated it for the copy either.  */
2588169689Skan  DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
2589169689Skan  DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
2590169689Skan
2591169689Skan  /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
2592169689Skan     declaration inspired this copy.  */
2593169689Skan  DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
2594169689Skan
2595169689Skan  /* The new variable/label has no RTL, yet.  */
2596169689Skan  if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
2597169689Skan      && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
2598169689Skan    SET_DECL_RTL (copy, NULL_RTX);
2599169689Skan
2600169689Skan  /* These args would always appear unused, if not for this.  */
2601169689Skan  TREE_USED (copy) = 1;
2602169689Skan
2603169689Skan  /* Set the context for the new declaration.  */
2604169689Skan  if (!DECL_CONTEXT (decl))
2605169689Skan    /* Globals stay global.  */
2606169689Skan    ;
2607169689Skan  else if (DECL_CONTEXT (decl) != id->src_fn)
2608169689Skan    /* Things that weren't in the scope of the function we're inlining
2609169689Skan       from aren't in the scope we're inlining to, either.  */
2610169689Skan    ;
2611169689Skan  else if (TREE_STATIC (decl))
2612169689Skan    /* Function-scoped static variables should stay in the original
2613169689Skan       function.  */
2614169689Skan    ;
2615169689Skan  else
2616169689Skan    /* Ordinary automatic local variables are now in the scope of the
2617169689Skan       new function.  */
2618169689Skan    DECL_CONTEXT (copy) = id->dst_fn;
2619169689Skan
2620169689Skan  return copy;
262190075Sobrien}
262290075Sobrien
2623169689Skanstatic tree
2624169689Skancopy_decl_to_var (tree decl, copy_body_data *id)
2625169689Skan{
2626169689Skan  tree copy, type;
262790075Sobrien
2628169689Skan  gcc_assert (TREE_CODE (decl) == PARM_DECL
2629169689Skan	      || TREE_CODE (decl) == RESULT_DECL);
2630169689Skan
2631169689Skan  type = TREE_TYPE (decl);
2632169689Skan
2633169689Skan  copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2634169689Skan  TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2635169689Skan  TREE_READONLY (copy) = TREE_READONLY (decl);
2636169689Skan  TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2637169689Skan  DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
2638169689Skan
2639169689Skan  return copy_decl_for_dup_finish (id, decl, copy);
2640169689Skan}
2641169689Skan
2642169689Skan/* Like copy_decl_to_var, but create a return slot object instead of a
2643169689Skan   pointer variable for return by invisible reference.  */
2644169689Skan
2645169689Skanstatic tree
2646169689Skancopy_result_decl_to_var (tree decl, copy_body_data *id)
264790075Sobrien{
2648169689Skan  tree copy, type;
264990075Sobrien
2650169689Skan  gcc_assert (TREE_CODE (decl) == PARM_DECL
2651169689Skan	      || TREE_CODE (decl) == RESULT_DECL);
2652169689Skan
2653169689Skan  type = TREE_TYPE (decl);
2654169689Skan  if (DECL_BY_REFERENCE (decl))
2655169689Skan    type = TREE_TYPE (type);
2656169689Skan
2657169689Skan  copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
2658169689Skan  TREE_READONLY (copy) = TREE_READONLY (decl);
2659169689Skan  TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
2660169689Skan  if (!DECL_BY_REFERENCE (decl))
2661169689Skan    {
2662169689Skan      TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
2663169689Skan      DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
2664169689Skan    }
2665169689Skan
2666169689Skan  return copy_decl_for_dup_finish (id, decl, copy);
266790075Sobrien}
266890075Sobrien
266990075Sobrien
2670169689Skanstatic tree
2671169689Skancopy_decl_no_change (tree decl, copy_body_data *id)
267290075Sobrien{
2673169689Skan  tree copy;
267490075Sobrien
2675169689Skan  copy = copy_node (decl);
2676169689Skan
2677169689Skan  /* The COPY is not abstract; it will be generated in DST_FN.  */
2678169689Skan  DECL_ABSTRACT (copy) = 0;
2679169689Skan  lang_hooks.dup_lang_specific_decl (copy);
2680169689Skan
2681169689Skan  /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
2682169689Skan     been taken; it's for internal bookkeeping in expand_goto_internal.  */
2683169689Skan  if (TREE_CODE (copy) == LABEL_DECL)
268490075Sobrien    {
2685169689Skan      TREE_ADDRESSABLE (copy) = 0;
2686169689Skan      LABEL_DECL_UID (copy) = -1;
2687169689Skan    }
268890075Sobrien
2689169689Skan  return copy_decl_for_dup_finish (id, decl, copy);
2690169689Skan}
269190075Sobrien
2692169689Skanstatic tree
2693169689Skancopy_decl_maybe_to_var (tree decl, copy_body_data *id)
2694169689Skan{
2695169689Skan  if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
2696169689Skan    return copy_decl_to_var (decl, id);
2697169689Skan  else
2698169689Skan    return copy_decl_no_change (decl, id);
2699169689Skan}
270090075Sobrien
2701169689Skan/* Return a copy of the function's argument tree.  */
2702169689Skanstatic tree
2703169689Skancopy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
2704169689Skan{
2705169689Skan  tree *arg_copy, *parg;
2706169689Skan
2707169689Skan  arg_copy = &orig_parm;
2708169689Skan  for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
2709169689Skan    {
2710169689Skan      tree new = remap_decl (*parg, id);
2711169689Skan      lang_hooks.dup_lang_specific_decl (new);
2712169689Skan      TREE_CHAIN (new) = TREE_CHAIN (*parg);
2713169689Skan      *parg = new;
271490075Sobrien    }
2715169689Skan  return orig_parm;
2716169689Skan}
271790075Sobrien
2718169689Skan/* Return a copy of the function's static chain.  */
2719169689Skanstatic tree
2720169689Skancopy_static_chain (tree static_chain, copy_body_data * id)
2721169689Skan{
2722169689Skan  tree *chain_copy, *pvar;
2723169689Skan
2724169689Skan  chain_copy = &static_chain;
2725169689Skan  for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
2726169689Skan    {
2727169689Skan      tree new = remap_decl (*pvar, id);
2728169689Skan      lang_hooks.dup_lang_specific_decl (new);
2729169689Skan      TREE_CHAIN (new) = TREE_CHAIN (*pvar);
2730169689Skan      *pvar = new;
2731169689Skan    }
2732169689Skan  return static_chain;
273390075Sobrien}
273490075Sobrien
2735169689Skan/* Return true if the function is allowed to be versioned.
2736169689Skan   This is a guard for the versioning functionality.  */
2737169689Skanbool
2738169689Skantree_versionable_function_p (tree fndecl)
2739169689Skan{
2740169689Skan  if (fndecl == NULL_TREE)
2741169689Skan    return false;
2742169689Skan  /* ??? There are cases where a function is
2743169689Skan     uninlinable but can be versioned.  */
2744169689Skan  if (!tree_inlinable_function_p (fndecl))
2745169689Skan    return false;
2746169689Skan
2747169689Skan  return true;
2748169689Skan}
274990075Sobrien
2750169689Skan/* Create a copy of a function's tree.
2751169689Skan   OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
2752169689Skan   of the original function and the new copied function
2753169689Skan   respectively.  In case we want to replace a DECL
2754169689Skan   tree with another tree while duplicating the function's
2755169689Skan   body, TREE_MAP represents the mapping between these
2756169689Skan   trees. If UPDATE_CLONES is set, the call_stmt fields
2757169689Skan   of edges of clones of the function will be updated.  */
275890075Sobrienvoid
2759169689Skantree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
2760169689Skan			  bool update_clones)
276190075Sobrien{
2762169689Skan  struct cgraph_node *old_version_node;
2763169689Skan  struct cgraph_node *new_version_node;
2764169689Skan  copy_body_data id;
2765169689Skan  tree p, new_fndecl;
2766169689Skan  unsigned i;
2767169689Skan  struct ipa_replace_map *replace_info;
2768169689Skan  basic_block old_entry_block;
2769169689Skan  tree t_step;
277090075Sobrien
2771169689Skan  gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
2772169689Skan	      && TREE_CODE (new_decl) == FUNCTION_DECL);
2773169689Skan  DECL_POSSIBLY_INLINED (old_decl) = 1;
2774117395Skan
2775169689Skan  old_version_node = cgraph_node (old_decl);
2776169689Skan  new_version_node = cgraph_node (new_decl);
2777169689Skan
2778169689Skan  allocate_struct_function (new_decl);
2779169689Skan  /* Cfun points to the new allocated function struct at this point.  */
2780169689Skan  cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
2781169689Skan
2782169689Skan  DECL_ARTIFICIAL (new_decl) = 1;
2783169689Skan  DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
2784169689Skan
2785169689Skan  /* Generate a new name for the new version. */
2786169689Skan  if (!update_clones)
2787169689Skan    DECL_NAME (new_decl) = create_tmp_var_name (NULL);
2788169689Skan  /* Create a new SYMBOL_REF rtx for the new name. */
2789169689Skan  if (DECL_RTL (old_decl) != NULL)
279090075Sobrien    {
2791169689Skan      SET_DECL_RTL (new_decl, copy_rtx (DECL_RTL (old_decl)));
2792169689Skan      XEXP (DECL_RTL (new_decl), 0) =
2793169689Skan	gen_rtx_SYMBOL_REF (GET_MODE (XEXP (DECL_RTL (old_decl), 0)),
2794169689Skan			    IDENTIFIER_POINTER (DECL_NAME (new_decl)));
2795169689Skan    }
279690075Sobrien
2797169689Skan  /* Prepare the data structures for the tree copy.  */
2798169689Skan  memset (&id, 0, sizeof (id));
2799169689Skan
2800169689Skan  id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2801169689Skan  id.src_fn = old_decl;
2802169689Skan  id.dst_fn = new_decl;
2803169689Skan  id.src_node = old_version_node;
2804169689Skan  id.dst_node = new_version_node;
2805169689Skan  id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
2806169689Skan
2807169689Skan  id.copy_decl = copy_decl_no_change;
2808169689Skan  id.transform_call_graph_edges
2809169689Skan    = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
2810169689Skan  id.transform_new_cfg = true;
2811169689Skan  id.transform_return_to_modify = false;
2812169689Skan  id.transform_lang_insert_block = false;
2813169689Skan
2814169689Skan  current_function_decl = new_decl;
2815169689Skan
2816169689Skan  /* Copy the function's static chain.  */
2817169689Skan  p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
2818169689Skan  if (p)
2819169689Skan    DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
2820169689Skan      copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
2821169689Skan			 &id);
2822169689Skan  /* Copy the function's arguments.  */
2823169689Skan  if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
2824169689Skan    DECL_ARGUMENTS (new_decl) =
2825169689Skan      copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
2826169689Skan
2827169689Skan  /* If there's a tree_map, prepare for substitution.  */
2828169689Skan  if (tree_map)
2829169689Skan    for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
2830169689Skan      {
2831169689Skan	replace_info = VARRAY_GENERIC_PTR (tree_map, i);
2832169689Skan	if (replace_info->replace_p)
2833169689Skan	  insert_decl_map (&id, replace_info->old_tree,
2834169689Skan			   replace_info->new_tree);
2835169689Skan      }
2836169689Skan
2837169689Skan  DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
2838169689Skan
2839169689Skan  /* Renumber the lexical scoping (non-code) blocks consecutively.  */
2840169689Skan  number_blocks (id.dst_fn);
2841169689Skan
2842169689Skan  if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
2843169689Skan    /* Add local vars.  */
2844169689Skan    for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
2845169689Skan	 t_step; t_step = TREE_CHAIN (t_step))
2846169689Skan      {
2847169689Skan	tree var = TREE_VALUE (t_step);
2848169689Skan	if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
2849169689Skan	  cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
2850169689Skan						 cfun->unexpanded_var_list);
2851169689Skan	else
2852169689Skan	  cfun->unexpanded_var_list =
2853169689Skan	    tree_cons (NULL_TREE, remap_decl (var, &id),
2854169689Skan		       cfun->unexpanded_var_list);
2855169689Skan      }
2856169689Skan
2857169689Skan  /* Copy the Function's body.  */
2858169689Skan  old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
2859169689Skan    (DECL_STRUCT_FUNCTION (old_decl));
2860169689Skan  new_fndecl = copy_body (&id,
2861169689Skan			  old_entry_block->count,
2862169689Skan			  old_entry_block->frequency, NULL, NULL);
2863169689Skan
2864169689Skan  DECL_SAVED_TREE (new_decl) = DECL_SAVED_TREE (new_fndecl);
2865169689Skan
2866169689Skan  DECL_STRUCT_FUNCTION (new_decl)->cfg =
2867169689Skan    DECL_STRUCT_FUNCTION (new_fndecl)->cfg;
2868169689Skan  DECL_STRUCT_FUNCTION (new_decl)->eh = DECL_STRUCT_FUNCTION (new_fndecl)->eh;
2869169689Skan  DECL_STRUCT_FUNCTION (new_decl)->ib_boundaries_block =
2870169689Skan    DECL_STRUCT_FUNCTION (new_fndecl)->ib_boundaries_block;
2871169689Skan  DECL_STRUCT_FUNCTION (new_decl)->last_label_uid =
2872169689Skan    DECL_STRUCT_FUNCTION (new_fndecl)->last_label_uid;
2873169689Skan
2874169689Skan  if (DECL_RESULT (old_decl) != NULL_TREE)
2875169689Skan    {
2876169689Skan      tree *res_decl = &DECL_RESULT (old_decl);
2877169689Skan      DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
2878169689Skan      lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
287990075Sobrien    }
2880169689Skan
2881169689Skan  current_function_decl = NULL;
2882169689Skan  /* Renumber the lexical scoping (non-code) blocks consecutively.  */
2883169689Skan  number_blocks (new_decl);
288490075Sobrien
2885169689Skan  /* Clean up.  */
2886169689Skan  splay_tree_delete (id.decl_map);
2887169689Skan  fold_cond_expr_cond ();
2888169689Skan  return;
288990075Sobrien}
2890117395Skan
2891169689Skan/* Duplicate a type, fields and all.  */
2892117395Skan
2893169689Skantree
2894169689Skanbuild_duplicate_type (tree type)
2895117395Skan{
2896169689Skan  struct copy_body_data id;
2897169689Skan
2898169689Skan  memset (&id, 0, sizeof (id));
2899169689Skan  id.src_fn = current_function_decl;
2900169689Skan  id.dst_fn = current_function_decl;
2901169689Skan  id.src_cfun = cfun;
2902169689Skan  id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2903169689Skan
2904169689Skan  type = remap_type_1 (type, &id);
2905169689Skan
2906169689Skan  splay_tree_delete (id.decl_map);
2907169689Skan
2908169689Skan  return type;
2909117395Skan}
2910