00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "config.h"
00026 #include "system.h"
00027 #include "coretypes.h"
00028 #include "tm.h"
00029 #include "toplev.h"
00030 #include "tree.h"
00031 #include "tree-inline.h"
00032 #include "rtl.h"
00033 #include "expr.h"
00034 #include "flags.h"
00035 #include "params.h"
00036 #include "input.h"
00037 #include "insn-config.h"
00038 #include "varray.h"
00039 #include "hashtab.h"
00040 #include "langhooks.h"
00041 #include "basic-block.h"
00042 #include "tree-iterator.h"
00043 #include "cgraph.h"
00044 #include "intl.h"
00045 #include "tree-mudflap.h"
00046 #include "tree-flow.h"
00047 #include "function.h"
00048 #include "ggc.h"
00049 #include "tree-flow.h"
00050 #include "diagnostic.h"
00051 #include "except.h"
00052 #include "debug.h"
00053 #include "pointer-set.h"
00054 #include "ipa-prop.h"
00055
00056
00057
00058 #include "tree-gimple.h"
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 int flag_inline_trees = 0;
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 static tree declare_return_variable (copy_body_data *, tree, tree, tree *);
00114 static tree copy_generic_body (copy_body_data *);
00115 static bool inlinable_function_p (tree);
00116 static void remap_block (tree *, copy_body_data *);
00117 static tree remap_decls (tree, copy_body_data *);
00118 static void copy_bind_expr (tree *, int *, copy_body_data *);
00119 static tree mark_local_for_remap_r (tree *, int *, void *);
00120 static void unsave_expr_1 (tree);
00121 static tree unsave_r (tree *, int *, void *);
00122 static void declare_inline_vars (tree, tree);
00123 static void remap_save_expr (tree *, void *, int *);
00124 static void add_lexical_block (tree current_block, tree new_block);
00125 static tree copy_decl_to_var (tree, copy_body_data *);
00126 static tree copy_result_decl_to_var (tree, copy_body_data *);
00127 static tree copy_decl_no_change (tree, copy_body_data *);
00128 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
00129
00130
00131
00132
00133 void
00134 insert_decl_map (copy_body_data *id, tree key, tree value)
00135 {
00136 splay_tree_insert (id->decl_map, (splay_tree_key) key,
00137 (splay_tree_value) value);
00138
00139
00140
00141 if (key != value)
00142 splay_tree_insert (id->decl_map, (splay_tree_key) value,
00143 (splay_tree_value) value);
00144 }
00145
00146
00147
00148 tree
00149 remap_decl (tree decl, copy_body_data *id)
00150 {
00151 splay_tree_node n;
00152 tree fn;
00153
00154
00155 fn = id->src_fn;
00156
00157
00158
00159 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
00160
00161
00162
00163 if (!n)
00164 {
00165
00166 tree t = id->copy_decl (decl, id);
00167
00168
00169
00170
00171 insert_decl_map (id, decl, t);
00172
00173 if (!DECL_P (t))
00174 return t;
00175
00176
00177 TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
00178 if (TREE_CODE (t) == TYPE_DECL)
00179 DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
00180
00181
00182 walk_tree (&DECL_SIZE (t), copy_body_r, id, NULL);
00183 walk_tree (&DECL_SIZE_UNIT (t), copy_body_r, id, NULL);
00184
00185
00186 if (TREE_CODE (t) == FIELD_DECL)
00187 {
00188 walk_tree (&DECL_FIELD_OFFSET (t), copy_body_r, id, NULL);
00189 if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
00190 walk_tree (&DECL_QUALIFIER (t), copy_body_r, id, NULL);
00191 }
00192
00193 return t;
00194 }
00195
00196 return unshare_expr ((tree) n->value);
00197 }
00198
00199 static tree
00200 remap_type_1 (tree type, copy_body_data *id)
00201 {
00202 splay_tree_node node;
00203 tree new, t;
00204
00205 if (type == NULL)
00206 return type;
00207
00208
00209 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
00210 if (node)
00211 return (tree) node->value;
00212
00213
00214 if (! variably_modified_type_p (type, id->src_fn))
00215 {
00216 insert_decl_map (id, type, type);
00217 return type;
00218 }
00219
00220
00221
00222
00223 if (TREE_CODE (type) == POINTER_TYPE)
00224 {
00225 new = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
00226 TYPE_MODE (type),
00227 TYPE_REF_CAN_ALIAS_ALL (type));
00228 insert_decl_map (id, type, new);
00229 return new;
00230 }
00231 else if (TREE_CODE (type) == REFERENCE_TYPE)
00232 {
00233 new = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
00234 TYPE_MODE (type),
00235 TYPE_REF_CAN_ALIAS_ALL (type));
00236 insert_decl_map (id, type, new);
00237 return new;
00238 }
00239 else
00240 new = copy_node (type);
00241
00242 insert_decl_map (id, type, new);
00243
00244
00245
00246 t = TYPE_MAIN_VARIANT (type);
00247 if (type != t)
00248 {
00249 t = remap_type (t, id);
00250 TYPE_MAIN_VARIANT (new) = t;
00251 TYPE_NEXT_VARIANT (new) = TYPE_MAIN_VARIANT (t);
00252 TYPE_NEXT_VARIANT (t) = new;
00253 }
00254 else
00255 {
00256 TYPE_MAIN_VARIANT (new) = new;
00257 TYPE_NEXT_VARIANT (new) = NULL;
00258 }
00259
00260 if (TYPE_STUB_DECL (type))
00261 TYPE_STUB_DECL (new) = remap_decl (TYPE_STUB_DECL (type), id);
00262
00263
00264 TYPE_POINTER_TO (new) = NULL;
00265 TYPE_REFERENCE_TO (new) = NULL;
00266
00267 switch (TREE_CODE (new))
00268 {
00269 case INTEGER_TYPE:
00270 case REAL_TYPE:
00271 case ENUMERAL_TYPE:
00272 case BOOLEAN_TYPE:
00273 t = TYPE_MIN_VALUE (new);
00274 if (t && TREE_CODE (t) != INTEGER_CST)
00275 walk_tree (&TYPE_MIN_VALUE (new), copy_body_r, id, NULL);
00276
00277 t = TYPE_MAX_VALUE (new);
00278 if (t && TREE_CODE (t) != INTEGER_CST)
00279 walk_tree (&TYPE_MAX_VALUE (new), copy_body_r, id, NULL);
00280 return new;
00281
00282 case FUNCTION_TYPE:
00283 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
00284 walk_tree (&TYPE_ARG_TYPES (new), copy_body_r, id, NULL);
00285 return new;
00286
00287 case ARRAY_TYPE:
00288 TREE_TYPE (new) = remap_type (TREE_TYPE (new), id);
00289 TYPE_DOMAIN (new) = remap_type (TYPE_DOMAIN (new), id);
00290 break;
00291
00292 case RECORD_TYPE:
00293 case UNION_TYPE:
00294 case QUAL_UNION_TYPE:
00295 {
00296 tree f, nf = NULL;
00297
00298 for (f = TYPE_FIELDS (new); f ; f = TREE_CHAIN (f))
00299 {
00300 t = remap_decl (f, id);
00301 DECL_CONTEXT (t) = new;
00302 TREE_CHAIN (t) = nf;
00303 nf = t;
00304 }
00305 TYPE_FIELDS (new) = nreverse (nf);
00306 }
00307 break;
00308
00309 case OFFSET_TYPE:
00310 default:
00311
00312 gcc_unreachable ();
00313 }
00314
00315 walk_tree (&TYPE_SIZE (new), copy_body_r, id, NULL);
00316 walk_tree (&TYPE_SIZE_UNIT (new), copy_body_r, id, NULL);
00317
00318 return new;
00319 }
00320
00321 tree
00322 remap_type (tree type, copy_body_data *id)
00323 {
00324 splay_tree_node node;
00325
00326 if (type == NULL)
00327 return type;
00328
00329
00330 node = splay_tree_lookup (id->decl_map, (splay_tree_key) type);
00331 if (node)
00332 return (tree) node->value;
00333
00334
00335 if (! variably_modified_type_p (type, id->src_fn))
00336 {
00337 insert_decl_map (id, type, type);
00338 return type;
00339 }
00340
00341 return remap_type_1 (type, id);
00342 }
00343
00344 static tree
00345 remap_decls (tree decls, copy_body_data *id)
00346 {
00347 tree old_var;
00348 tree new_decls = NULL_TREE;
00349
00350
00351 for (old_var = decls; old_var; old_var = TREE_CHAIN (old_var))
00352 {
00353 tree new_var;
00354
00355
00356
00357
00358 if (!lang_hooks.tree_inlining.auto_var_in_fn_p (old_var, id->src_fn)
00359 && !DECL_EXTERNAL (old_var))
00360 {
00361 cfun->unexpanded_var_list = tree_cons (NULL_TREE, old_var,
00362 cfun->unexpanded_var_list);
00363 continue;
00364 }
00365
00366
00367 new_var = remap_decl (old_var, id);
00368
00369
00370
00371
00372 if (!new_var || new_var == id->retvar)
00373 ;
00374 else
00375 {
00376 gcc_assert (DECL_P (new_var));
00377 TREE_CHAIN (new_var) = new_decls;
00378 new_decls = new_var;
00379 }
00380 }
00381
00382 return nreverse (new_decls);
00383 }
00384
00385
00386
00387
00388 static void
00389 remap_block (tree *block, copy_body_data *id)
00390 {
00391 tree old_block;
00392 tree new_block;
00393 tree fn;
00394
00395
00396 old_block = *block;
00397 new_block = make_node (BLOCK);
00398 TREE_USED (new_block) = TREE_USED (old_block);
00399 BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
00400 BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
00401 *block = new_block;
00402
00403
00404 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), id);
00405
00406 fn = id->dst_fn;
00407
00408 if (id->transform_lang_insert_block)
00409 lang_hooks.decls.insert_block (new_block);
00410
00411
00412 insert_decl_map (id, old_block, new_block);
00413 }
00414
00415
00416 static tree
00417 remap_blocks (tree block, copy_body_data *id)
00418 {
00419 tree t;
00420 tree new = block;
00421
00422 if (!block)
00423 return NULL;
00424
00425 remap_block (&new, id);
00426 gcc_assert (new != block);
00427 for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
00428 add_lexical_block (new, remap_blocks (t, id));
00429 return new;
00430 }
00431
00432 static void
00433 copy_statement_list (tree *tp)
00434 {
00435 tree_stmt_iterator oi, ni;
00436 tree new;
00437
00438 new = alloc_stmt_list ();
00439
00440 #ifdef KEY
00441
00442
00443
00444 if (!strcmp ("GNU C++", lang_hooks.name) &&
00445 flag_spin_file)
00446 TREE_TYPE(new) = TREE_TYPE(*tp);
00447 #endif
00448
00449 ni = tsi_start (new);
00450 oi = tsi_start (*tp);
00451 *tp = new;
00452
00453 for (; !tsi_end_p (oi); tsi_next (&oi))
00454 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT);
00455 }
00456
00457 static void
00458 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
00459 {
00460 tree block = BIND_EXPR_BLOCK (*tp);
00461
00462 copy_tree_r (tp, walk_subtrees, NULL);
00463 if (block)
00464 {
00465 remap_block (&block, id);
00466 BIND_EXPR_BLOCK (*tp) = block;
00467 }
00468
00469 if (BIND_EXPR_VARS (*tp))
00470
00471
00472 BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), id);
00473 }
00474
00475
00476
00477
00478 tree
00479 copy_body_r (tree *tp, int *walk_subtrees, void *data)
00480 {
00481 copy_body_data *id = (copy_body_data *) data;
00482 tree fn = id->src_fn;
00483 tree new_block;
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494 if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
00495 {
00496 tree assignment = TREE_OPERAND (*tp, 0);
00497
00498
00499
00500
00501
00502
00503 if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
00504 {
00505
00506
00507 *tp = copy_node (assignment);
00508 }
00509 else
00510 {
00511 *tp = NULL;
00512 return (tree) (void *)1;
00513 }
00514 }
00515
00516
00517
00518
00519
00520 else if (lang_hooks.tree_inlining.auto_var_in_fn_p (*tp, fn))
00521 {
00522 tree new_decl;
00523
00524
00525 new_decl = remap_decl (*tp, id);
00526 gcc_assert (new_decl);
00527
00528 STRIP_TYPE_NOPS (new_decl);
00529 *tp = new_decl;
00530 *walk_subtrees = 0;
00531 }
00532 else if (TREE_CODE (*tp) == STATEMENT_LIST)
00533 copy_statement_list (tp);
00534 else if (TREE_CODE (*tp) == SAVE_EXPR)
00535 remap_save_expr (tp, id->decl_map, walk_subtrees);
00536 else if (TREE_CODE (*tp) == LABEL_DECL
00537 && (! DECL_CONTEXT (*tp)
00538 || decl_function_context (*tp) == id->src_fn))
00539
00540 *tp = remap_decl (*tp, id);
00541 else if (TREE_CODE (*tp) == BIND_EXPR)
00542 copy_bind_expr (tp, walk_subtrees, id);
00543
00544 else if (TYPE_P (*tp))
00545 *tp = remap_type (*tp, id);
00546
00547
00548
00549 else if (CONSTANT_CLASS_P (*tp))
00550 {
00551 tree new_type = remap_type (TREE_TYPE (*tp), id);
00552
00553 if (new_type == TREE_TYPE (*tp))
00554 *walk_subtrees = 0;
00555
00556 else if (TREE_CODE (*tp) == INTEGER_CST)
00557 *tp = build_int_cst_wide (new_type, TREE_INT_CST_LOW (*tp),
00558 TREE_INT_CST_HIGH (*tp));
00559 else
00560 {
00561 *tp = copy_node (*tp);
00562 TREE_TYPE (*tp) = new_type;
00563 }
00564 }
00565
00566
00567
00568 else
00569 {
00570
00571
00572
00573 if (TREE_CODE (*tp) == MODIFY_EXPR
00574 && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
00575 && (lang_hooks.tree_inlining.auto_var_in_fn_p
00576 (TREE_OPERAND (*tp, 0), fn)))
00577 {
00578
00579
00580
00581 tree decl = TREE_OPERAND (*tp, 0), value;
00582 splay_tree_node n;
00583
00584 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
00585 if (n)
00586 {
00587 value = (tree) n->value;
00588 STRIP_TYPE_NOPS (value);
00589 if (TREE_CONSTANT (value) || TREE_READONLY_DECL_P (value))
00590 {
00591 *tp = build_empty_stmt ();
00592 return copy_body_r (tp, walk_subtrees, data);
00593 }
00594 }
00595 }
00596 else if (TREE_CODE (*tp) == INDIRECT_REF)
00597 {
00598
00599
00600 tree decl = TREE_OPERAND (*tp, 0);
00601 splay_tree_node n;
00602
00603 n = splay_tree_lookup (id->decl_map, (splay_tree_key) decl);
00604 if (n)
00605 {
00606 tree new;
00607 tree old;
00608
00609
00610
00611
00612
00613
00614 tree type = TREE_TYPE (TREE_TYPE ((tree)n->value));
00615 new = unshare_expr ((tree)n->value);
00616 old = *tp;
00617 *tp = fold_indirect_ref_1 (type, new);
00618 if (! *tp)
00619 {
00620 if (TREE_CODE (new) == ADDR_EXPR)
00621 *tp = TREE_OPERAND (new, 0);
00622 else
00623 {
00624 *tp = build1 (INDIRECT_REF, type, new);
00625 TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
00626 }
00627 }
00628 *walk_subtrees = 0;
00629 return NULL;
00630 }
00631 }
00632
00633
00634
00635 copy_tree_r (tp, walk_subtrees, NULL);
00636
00637
00638
00639
00640 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (*tp))))
00641 {
00642 new_block = id->block;
00643 if (TREE_BLOCK (*tp))
00644 {
00645 splay_tree_node n;
00646 n = splay_tree_lookup (id->decl_map,
00647 (splay_tree_key) TREE_BLOCK (*tp));
00648 gcc_assert (n);
00649 new_block = (tree) n->value;
00650 }
00651 TREE_BLOCK (*tp) = new_block;
00652 }
00653
00654 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
00655 TREE_OPERAND (*tp, 0) =
00656 build_int_cst
00657 (NULL_TREE,
00658 id->eh_region_offset + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
00659
00660 if (TREE_CODE (*tp) != OMP_CLAUSE
00661 #ifdef KEY
00662
00663
00664 && (!flag_spin_file || (TREE_TYPE(*tp) && TYPE_P(TREE_TYPE(*tp))))
00665 #endif
00666 )
00667 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
00668
00669
00670
00671 if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
00672 {
00673 TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
00674 TREE_OPERAND (*tp, 3) = NULL_TREE;
00675 }
00676
00677
00678
00679
00680 else if (TREE_CODE (*tp) == ADDR_EXPR)
00681 {
00682 walk_tree (&TREE_OPERAND (*tp, 0), copy_body_r, id, NULL);
00683
00684
00685 if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
00686 *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
00687 else
00688 recompute_tree_invariant_for_addr_expr (*tp);
00689 *walk_subtrees = 0;
00690 }
00691 }
00692
00693
00694 return NULL_TREE;
00695 }
00696
00697
00698
00699
00700 static basic_block
00701 copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scale)
00702 {
00703 block_stmt_iterator bsi, copy_bsi;
00704 basic_block copy_basic_block;
00705
00706
00707
00708 copy_basic_block = create_basic_block (NULL, (void *) 0,
00709 (basic_block) bb->prev_bb->aux);
00710 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
00711 copy_basic_block->frequency = (bb->frequency
00712 * frequency_scale / REG_BR_PROB_BASE);
00713 copy_bsi = bsi_start (copy_basic_block);
00714
00715 for (bsi = bsi_start (bb);
00716 !bsi_end_p (bsi); bsi_next (&bsi))
00717 {
00718 tree stmt = bsi_stmt (bsi);
00719 tree orig_stmt = stmt;
00720
00721 walk_tree (&stmt, copy_body_r, id, NULL);
00722
00723
00724
00725 if (stmt)
00726 {
00727 tree call, decl;
00728
00729
00730
00731 if (TREE_CODE (stmt) == MODIFY_EXPR
00732 && TREE_CODE (TREE_OPERAND (stmt, 1)) == NOP_EXPR
00733 && !is_gimple_val (TREE_OPERAND (TREE_OPERAND (stmt, 1), 0)))
00734 gimplify_stmt (&stmt);
00735
00736 bsi_insert_after (©_bsi, stmt, BSI_NEW_STMT);
00737 call = get_call_expr_in (stmt);
00738
00739
00740 if (call && (decl = get_callee_fndecl (call)))
00741 {
00742 struct cgraph_node *node;
00743 struct cgraph_edge *edge;
00744
00745 switch (id->transform_call_graph_edges)
00746 {
00747 case CB_CGE_DUPLICATE:
00748 edge = cgraph_edge (id->src_node, orig_stmt);
00749 if (edge)
00750 cgraph_clone_edge (edge, id->dst_node, stmt,
00751 REG_BR_PROB_BASE, 1, true);
00752 break;
00753
00754 case CB_CGE_MOVE_CLONES:
00755 for (node = id->dst_node->next_clone;
00756 node;
00757 node = node->next_clone)
00758 {
00759 edge = cgraph_edge (node, orig_stmt);
00760 gcc_assert (edge);
00761 cgraph_set_call_stmt (edge, stmt);
00762 }
00763
00764
00765 case CB_CGE_MOVE:
00766 edge = cgraph_edge (id->dst_node, orig_stmt);
00767 if (edge)
00768 cgraph_set_call_stmt (edge, stmt);
00769 break;
00770
00771 default:
00772 gcc_unreachable ();
00773 }
00774 }
00775
00776
00777 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt)
00778 != 0);
00779
00780 if (tree_could_throw_p (stmt))
00781 {
00782 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
00783
00784
00785
00786
00787 if (region > 0)
00788 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
00789
00790
00791
00792
00793
00794 if ((lookup_stmt_eh_region_fn (id->src_cfun,
00795 orig_stmt) <= 0
00796 && id->eh_region > 0)
00797 && tree_could_throw_p (stmt))
00798 add_stmt_to_eh_region (stmt, id->eh_region);
00799 }
00800 }
00801 }
00802 return copy_basic_block;
00803 }
00804
00805
00806
00807
00808 static void
00809 copy_edges_for_bb (basic_block bb, int count_scale)
00810 {
00811 basic_block new_bb = (basic_block) bb->aux;
00812 edge_iterator ei;
00813 edge old_edge;
00814 block_stmt_iterator bsi;
00815 int flags;
00816
00817
00818
00819 FOR_EACH_EDGE (old_edge, ei, bb->succs)
00820 if (!(old_edge->flags & EDGE_EH))
00821 {
00822 edge new;
00823
00824 flags = old_edge->flags;
00825
00826
00827 if (old_edge->dest->index == EXIT_BLOCK && !old_edge->flags
00828 && old_edge->dest->aux != EXIT_BLOCK_PTR)
00829 flags |= EDGE_FALLTHRU;
00830 new = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
00831 new->count = old_edge->count * count_scale / REG_BR_PROB_BASE;
00832 new->probability = old_edge->probability;
00833 }
00834
00835 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
00836 return;
00837
00838 for (bsi = bsi_start (new_bb); !bsi_end_p (bsi);)
00839 {
00840 tree copy_stmt;
00841
00842 copy_stmt = bsi_stmt (bsi);
00843 update_stmt (copy_stmt);
00844
00845 bsi_next (&bsi);
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 if (tree_can_throw_internal (copy_stmt))
00860 {
00861 if (!bsi_end_p (bsi))
00862
00863
00864 {
00865 edge e = split_block (new_bb, copy_stmt);
00866 new_bb = e->dest;
00867 bsi = bsi_start (new_bb);
00868 }
00869
00870 make_eh_edges (copy_stmt);
00871 }
00872 }
00873 }
00874
00875
00876 static tree
00877 remap_decl_1 (tree decl, void *data)
00878 {
00879 return remap_decl (decl, (copy_body_data *) data);
00880 }
00881
00882
00883
00884
00885 static tree
00886 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency,
00887 basic_block entry_block_map, basic_block exit_block_map)
00888 {
00889 tree callee_fndecl = id->src_fn;
00890
00891 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
00892
00893 struct function *new_cfun;
00894
00895
00896 struct function *cfun_to_copy =
00897 (struct function *) ggc_alloc_cleared (sizeof (struct function));
00898 basic_block bb;
00899 tree new_fndecl = NULL;
00900 int count_scale, frequency_scale;
00901
00902 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
00903 count_scale = (REG_BR_PROB_BASE * count
00904 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
00905 else
00906 count_scale = 1;
00907
00908 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
00909 frequency_scale = (REG_BR_PROB_BASE * frequency
00910 /
00911 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
00912 else
00913 frequency_scale = count_scale;
00914
00915
00916 tree_register_cfg_hooks ();
00917
00918
00919 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION
00920 (DECL_STRUCT_FUNCTION (callee_fndecl)));
00921
00922 *cfun_to_copy = *DECL_STRUCT_FUNCTION (callee_fndecl);
00923
00924 id->src_cfun = cfun_to_copy;
00925
00926
00927
00928 if (id->transform_new_cfg)
00929 {
00930 new_cfun =
00931 (struct function *) ggc_alloc_cleared (sizeof (struct function));
00932 *new_cfun = *DECL_STRUCT_FUNCTION (callee_fndecl);
00933 new_cfun->cfg = NULL;
00934 new_cfun->decl = new_fndecl = copy_node (callee_fndecl);
00935 new_cfun->ib_boundaries_block = NULL;
00936 DECL_STRUCT_FUNCTION (new_fndecl) = new_cfun;
00937 push_cfun (new_cfun);
00938 init_empty_tree_cfg ();
00939
00940 ENTRY_BLOCK_PTR->count =
00941 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
00942 REG_BR_PROB_BASE);
00943 ENTRY_BLOCK_PTR->frequency =
00944 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
00945 frequency_scale / REG_BR_PROB_BASE);
00946 EXIT_BLOCK_PTR->count =
00947 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
00948 REG_BR_PROB_BASE);
00949 EXIT_BLOCK_PTR->frequency =
00950 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency *
00951 frequency_scale / REG_BR_PROB_BASE);
00952
00953 entry_block_map = ENTRY_BLOCK_PTR;
00954 exit_block_map = EXIT_BLOCK_PTR;
00955 }
00956
00957 ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = entry_block_map;
00958 EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy)->aux = exit_block_map;
00959
00960
00961 if (cfun->eh)
00962 {
00963 if (id->transform_new_cfg)
00964 init_eh_for_function ();
00965 id->eh_region_offset
00966 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
00967 0, id->eh_region);
00968 }
00969
00970 FOR_EACH_BB_FN (bb, cfun_to_copy)
00971 bb->aux = copy_bb (id, bb, frequency_scale, count_scale);
00972
00973 FOR_ALL_BB_FN (bb, cfun_to_copy)
00974 copy_edges_for_bb (bb, count_scale);
00975 FOR_ALL_BB_FN (bb, cfun_to_copy)
00976 bb->aux = NULL;
00977
00978 if (id->transform_new_cfg)
00979 pop_cfun ();
00980
00981 return new_fndecl;
00982 }
00983
00984
00985
00986
00987 static tree
00988 copy_generic_body (copy_body_data *id)
00989 {
00990 tree body;
00991 tree fndecl = id->src_fn;
00992
00993 body = DECL_SAVED_TREE (fndecl);
00994 walk_tree (&body, copy_body_r, id, NULL);
00995
00996 return body;
00997 }
00998
00999 static tree
01000 copy_body (copy_body_data *id, gcov_type count, int frequency,
01001 basic_block entry_block_map, basic_block exit_block_map)
01002 {
01003 tree fndecl = id->src_fn;
01004 tree body;
01005
01006
01007 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
01008 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map);
01009
01010 return body;
01011 }
01012
01013
01014
01015
01016 static bool
01017 self_inlining_addr_expr (tree value, tree fn)
01018 {
01019 tree var;
01020
01021 if (TREE_CODE (value) != ADDR_EXPR)
01022 return false;
01023
01024 var = get_base_address (TREE_OPERAND (value, 0));
01025
01026 return var && lang_hooks.tree_inlining.auto_var_in_fn_p (var, fn);
01027 }
01028
01029 static void
01030 setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
01031 basic_block bb, tree *vars)
01032 {
01033 tree init_stmt;
01034 tree var;
01035 tree var_sub;
01036
01037
01038
01039
01040 if (TREE_READONLY (p)
01041 && !TREE_ADDRESSABLE (p)
01042 && value && !TREE_SIDE_EFFECTS (value))
01043 {
01044
01045
01046
01047
01048 if (is_gimple_min_invariant (value)
01049 && lang_hooks.types_compatible_p (TREE_TYPE (value), TREE_TYPE (p))
01050
01051
01052
01053
01054
01055 && ! self_inlining_addr_expr (value, fn))
01056 {
01057 insert_decl_map (id, p, value);
01058 return;
01059 }
01060 }
01061
01062
01063
01064
01065 var = copy_decl_to_var (p, id);
01066
01067
01068
01069
01070 if (TREE_TYPE (var) != TREE_TYPE (p)
01071 && POINTER_TYPE_P (TREE_TYPE (var))
01072 && TREE_TYPE (TREE_TYPE (var)) == TREE_TYPE (p))
01073 {
01074 insert_decl_map (id, var, var);
01075 var_sub = build_fold_indirect_ref (var);
01076 }
01077 else
01078 var_sub = var;
01079
01080
01081
01082
01083 insert_decl_map (id, p, var_sub);
01084
01085
01086 TREE_CHAIN (var) = *vars;
01087 *vars = var;
01088
01089
01090 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
01102 TREE_READONLY (var) = 0;
01103
01104
01105
01106 if (value)
01107 {
01108 tree rhs = fold_convert (TREE_TYPE (var), value);
01109 block_stmt_iterator bsi = bsi_last (bb);
01110
01111 if (rhs == error_mark_node)
01112 return;
01113
01114 STRIP_USELESS_TYPE_CONVERSION (rhs);
01115
01116
01117
01118 init_stmt = build2 (MODIFY_EXPR, TREE_TYPE (var), var, rhs);
01119
01120
01121
01122
01123
01124
01125 if (!is_gimple_val (rhs)
01126 && (!is_gimple_cast (rhs)
01127 || !is_gimple_val (TREE_OPERAND (rhs, 0))))
01128 gimplify_stmt (&init_stmt);
01129
01130
01131
01132 if (init_stmt)
01133 bsi_insert_after (&bsi, init_stmt, BSI_NEW_STMT);
01134 }
01135 }
01136
01137
01138
01139
01140 static void
01141 initialize_inlined_parameters (copy_body_data *id, tree args, tree static_chain,
01142 tree fn, basic_block bb)
01143 {
01144 tree parms;
01145 tree a;
01146 tree p;
01147 tree vars = NULL_TREE;
01148 int argnum = 0;
01149
01150
01151 parms = DECL_ARGUMENTS (fn);
01152
01153
01154
01155 for (p = parms, a = args; p;
01156 a = a ? TREE_CHAIN (a) : a, p = TREE_CHAIN (p))
01157 {
01158 tree value;
01159
01160 ++argnum;
01161
01162
01163 value = lang_hooks.tree_inlining.convert_parm_for_inlining
01164 (p, a ? TREE_VALUE (a) : NULL_TREE, fn, argnum);
01165
01166 setup_one_parameter (id, p, value, fn, bb, &vars);
01167 }
01168
01169
01170 p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
01171 gcc_assert (fn != current_function_decl);
01172 if (p)
01173 {
01174
01175 gcc_assert (static_chain);
01176
01177 setup_one_parameter (id, p, static_chain, fn, bb, &vars);
01178 }
01179
01180 declare_inline_vars (id->block, vars);
01181 }
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196 static tree
01197 declare_return_variable (copy_body_data *id, tree return_slot_addr,
01198 tree modify_dest, tree *use_p)
01199 {
01200 tree callee = id->src_fn;
01201 tree caller = id->dst_fn;
01202 tree result = DECL_RESULT (callee);
01203 tree callee_type = TREE_TYPE (result);
01204 tree caller_type = TREE_TYPE (TREE_TYPE (callee));
01205 tree var, use;
01206
01207
01208
01209 if (!result || VOID_TYPE_P (callee_type))
01210 {
01211 *use_p = NULL_TREE;
01212 return NULL_TREE;
01213 }
01214
01215
01216
01217 if (return_slot_addr)
01218 {
01219
01220
01221 gcc_assert (!modify_dest);
01222 if (DECL_BY_REFERENCE (result))
01223 var = return_slot_addr;
01224 else
01225 var = build_fold_indirect_ref (return_slot_addr);
01226 if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
01227 && !DECL_COMPLEX_GIMPLE_REG_P (result)
01228 && DECL_P (var))
01229 DECL_COMPLEX_GIMPLE_REG_P (var) = 0;
01230 use = NULL;
01231 goto done;
01232 }
01233
01234
01235 gcc_assert (!TREE_ADDRESSABLE (callee_type));
01236
01237
01238 if (modify_dest)
01239 {
01240 bool use_it = false;
01241
01242
01243 if (!lang_hooks.types_compatible_p (caller_type, callee_type))
01244 use_it = false;
01245
01246
01247
01248
01249 else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
01250 use_it = true;
01251
01252
01253
01254
01255 else if (TREE_ADDRESSABLE (result))
01256 use_it = false;
01257 else
01258 {
01259 tree base_m = get_base_address (modify_dest);
01260
01261
01262
01263 if (!DECL_P (base_m))
01264 use_it = false;
01265 else if (is_global_var (base_m))
01266 use_it = false;
01267 else if (TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
01268 && !DECL_COMPLEX_GIMPLE_REG_P (result)
01269 && DECL_COMPLEX_GIMPLE_REG_P (base_m))
01270 use_it = false;
01271 else if (!TREE_ADDRESSABLE (base_m))
01272 use_it = true;
01273 }
01274
01275 if (use_it)
01276 {
01277 var = modify_dest;
01278 use = NULL;
01279 goto done;
01280 }
01281 }
01282
01283 gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
01284
01285 var = copy_result_decl_to_var (result, id);
01286
01287 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
01288 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list
01289 = tree_cons (NULL_TREE, var,
01290 DECL_STRUCT_FUNCTION (caller)->unexpanded_var_list);
01291
01292
01293
01294 TREE_NO_WARNING (var) = 1;
01295
01296 declare_inline_vars (id->block, var);
01297
01298
01299
01300 use = var;
01301 if (!lang_hooks.types_compatible_p (TREE_TYPE (var), caller_type))
01302 use = fold_convert (caller_type, var);
01303
01304 STRIP_USELESS_TYPE_CONVERSION (use);
01305
01306 if (DECL_BY_REFERENCE (result))
01307 var = build_fold_addr_expr (var);
01308
01309 done:
01310
01311
01312
01313 insert_decl_map (id, result, var);
01314
01315
01316 id->retvar = var;
01317
01318 *use_p = use;
01319 return var;
01320 }
01321
01322
01323
01324 bool
01325 tree_inlinable_function_p (tree fn)
01326 {
01327 return inlinable_function_p (fn);
01328 }
01329
01330 static const char *inline_forbidden_reason;
01331
01332 static tree
01333 inline_forbidden_p_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
01334 void *fnp)
01335 {
01336 tree node = *nodep;
01337 tree fn = (tree) fnp;
01338 tree t;
01339
01340 switch (TREE_CODE (node))
01341 {
01342 case CALL_EXPR:
01343
01344
01345
01346
01347
01348 if (alloca_call_p (node)
01349 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
01350 {
01351 inline_forbidden_reason
01352 = G_("function %q+F can never be inlined because it uses "
01353 "alloca (override using the always_inline attribute)");
01354 return node;
01355 }
01356 t = get_callee_fndecl (node);
01357 if (! t)
01358 break;
01359
01360
01361 if (setjmp_call_p (t))
01362 {
01363 inline_forbidden_reason
01364 = G_("function %q+F can never be inlined because it uses setjmp");
01365 return node;
01366 }
01367
01368 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
01369 switch (DECL_FUNCTION_CODE (t))
01370 {
01371
01372
01373 case BUILT_IN_VA_START:
01374 case BUILT_IN_STDARG_START:
01375 case BUILT_IN_NEXT_ARG:
01376 case BUILT_IN_VA_END:
01377 inline_forbidden_reason
01378 = G_("function %q+F can never be inlined because it "
01379 "uses variable argument lists");
01380 return node;
01381
01382 case BUILT_IN_LONGJMP:
01383
01384
01385
01386
01387
01388 inline_forbidden_reason
01389 = G_("function %q+F can never be inlined because "
01390 "it uses setjmp-longjmp exception handling");
01391 return node;
01392
01393 case BUILT_IN_NONLOCAL_GOTO:
01394
01395 inline_forbidden_reason
01396 = G_("function %q+F can never be inlined because "
01397 "it uses non-local goto");
01398 return node;
01399
01400 case BUILT_IN_RETURN:
01401 case BUILT_IN_APPLY_ARGS:
01402
01403
01404
01405
01406 inline_forbidden_reason
01407 = G_("function %q+F can never be inlined because "
01408 "it uses __builtin_return or __builtin_apply_args");
01409 return node;
01410
01411 default:
01412 break;
01413 }
01414 break;
01415
01416 case GOTO_EXPR:
01417 t = TREE_OPERAND (node, 0);
01418
01419
01420
01421
01422
01423 if (TREE_CODE (t) != LABEL_DECL)
01424 {
01425 inline_forbidden_reason
01426 = G_("function %q+F can never be inlined "
01427 "because it contains a computed goto");
01428 return node;
01429 }
01430 break;
01431
01432 case LABEL_EXPR:
01433 t = TREE_OPERAND (node, 0);
01434 if (DECL_NONLOCAL (t))
01435 {
01436
01437
01438
01439 inline_forbidden_reason
01440 = G_("function %q+F can never be inlined "
01441 "because it receives a non-local goto");
01442 return node;
01443 }
01444 break;
01445
01446 case RECORD_TYPE:
01447 case UNION_TYPE:
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
01462 if (variably_modified_type_p (TREE_TYPE (t), NULL))
01463 {
01464 inline_forbidden_reason
01465 = G_("function %q+F can never be inlined "
01466 "because it uses variable sized variables");
01467 return node;
01468 }
01469
01470 default:
01471 break;
01472 }
01473
01474 return NULL_TREE;
01475 }
01476
01477
01478 static tree
01479 inline_forbidden_p (tree fndecl)
01480 {
01481 location_t saved_loc = input_location;
01482 block_stmt_iterator bsi;
01483 basic_block bb;
01484 tree ret = NULL_TREE;
01485
01486 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (fndecl))
01487 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
01488 {
01489 ret = walk_tree_without_duplicates (bsi_stmt_ptr (bsi),
01490 inline_forbidden_p_1, fndecl);
01491 if (ret)
01492 goto egress;
01493 }
01494
01495 egress:
01496 input_location = saved_loc;
01497 return ret;
01498 }
01499
01500
01501
01502
01503 static bool
01504 inlinable_function_p (tree fn)
01505 {
01506 bool inlinable = true;
01507
01508
01509
01510 if (DECL_UNINLINABLE (fn))
01511 return false;
01512
01513
01514
01515
01516
01517
01518 inlinable = !lang_hooks.tree_inlining.cannot_inline_tree_fn (&fn);
01519
01520
01521
01522
01523 if (!DECL_SAVED_TREE (fn))
01524 return false;
01525
01526
01527 else if (!flag_inline_trees)
01528 inlinable = false;
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541 else if (!DECL_INLINE (fn) && !flag_unit_at_a_time)
01542 inlinable = false;
01543
01544 else if (inline_forbidden_p (fn))
01545 {
01546
01547
01548
01549
01550
01551
01552
01553
01554 bool do_warning = (warn_inline
01555 && DECL_INLINE (fn)
01556 && DECL_DECLARED_INLINE_P (fn)
01557 && !DECL_IN_SYSTEM_HEADER (fn));
01558
01559 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
01560 sorry (inline_forbidden_reason, fn);
01561 else if (do_warning)
01562 warning (OPT_Winline, inline_forbidden_reason, fn);
01563
01564 inlinable = false;
01565 }
01566
01567
01568 DECL_UNINLINABLE (fn) = !inlinable;
01569
01570 return inlinable;
01571 }
01572
01573
01574
01575
01576 int
01577 estimate_move_cost (tree type)
01578 {
01579 HOST_WIDE_INT size;
01580
01581 size = int_size_in_bytes (type);
01582
01583 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
01584
01585 return 4;
01586 else
01587 return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
01588 }
01589
01590
01591
01592
01593 static tree
01594 estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
01595 {
01596 int *count = (int *) data;
01597 tree x = *tp;
01598
01599 if (IS_TYPE_OR_DECL_P (x))
01600 {
01601 *walk_subtrees = 0;
01602 return NULL;
01603 }
01604
01605
01606
01607 else if (CONSTANT_CLASS_P (x) || REFERENCE_CLASS_P (x))
01608 return NULL;
01609
01610 switch (TREE_CODE (x))
01611 {
01612
01613 case TREE_LIST:
01614 case TREE_VEC:
01615 case BLOCK:
01616 case COMPONENT_REF:
01617 case BIT_FIELD_REF:
01618 case INDIRECT_REF:
01619 case ALIGN_INDIRECT_REF:
01620 case MISALIGNED_INDIRECT_REF:
01621 case ARRAY_REF:
01622 case ARRAY_RANGE_REF:
01623 case OBJ_TYPE_REF:
01624 case EXC_PTR_EXPR:
01625 case FILTER_EXPR:
01626 case COMPOUND_EXPR:
01627 case BIND_EXPR:
01628 case WITH_CLEANUP_EXPR:
01629 case NOP_EXPR:
01630 case VIEW_CONVERT_EXPR:
01631 case SAVE_EXPR:
01632 case ADDR_EXPR:
01633 case COMPLEX_EXPR:
01634 case RANGE_EXPR:
01635 case CASE_LABEL_EXPR:
01636 case SSA_NAME:
01637 case CATCH_EXPR:
01638 case EH_FILTER_EXPR:
01639 case STATEMENT_LIST:
01640 case ERROR_MARK:
01641 case NON_LVALUE_EXPR:
01642 case FDESC_EXPR:
01643 case VA_ARG_EXPR:
01644 case TRY_CATCH_EXPR:
01645 case TRY_FINALLY_EXPR:
01646 case LABEL_EXPR:
01647 case GOTO_EXPR:
01648 case RETURN_EXPR:
01649 case EXIT_EXPR:
01650 case LOOP_EXPR:
01651 case PHI_NODE:
01652 case WITH_SIZE_EXPR:
01653 case OMP_CLAUSE:
01654 case OMP_RETURN:
01655 case OMP_CONTINUE:
01656 break;
01657
01658
01659
01660
01661
01662 case IDENTIFIER_NODE:
01663 case INTEGER_CST:
01664 case REAL_CST:
01665 case COMPLEX_CST:
01666 case VECTOR_CST:
01667 case STRING_CST:
01668 *walk_subtrees = 0;
01669 return NULL;
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695 case INIT_EXPR:
01696 case MODIFY_EXPR:
01697
01698 if (TREE_CODE (TREE_OPERAND (x, 1)) == TARGET_EXPR)
01699 break;
01700
01701
01702 case TARGET_EXPR:
01703 x = TREE_OPERAND (x, 0);
01704
01705 if (is_gimple_reg (x))
01706 break;
01707
01708
01709 case CONSTRUCTOR:
01710 *count += estimate_move_cost (TREE_TYPE (x));
01711 break;
01712
01713
01714
01715 case COND_EXPR:
01716 case VEC_COND_EXPR:
01717
01718 case PLUS_EXPR:
01719 case MINUS_EXPR:
01720 case MULT_EXPR:
01721
01722 case FIX_TRUNC_EXPR:
01723 case FIX_CEIL_EXPR:
01724 case FIX_FLOOR_EXPR:
01725 case FIX_ROUND_EXPR:
01726
01727 case NEGATE_EXPR:
01728 case FLOAT_EXPR:
01729 case MIN_EXPR:
01730 case MAX_EXPR:
01731 case ABS_EXPR:
01732
01733 case LSHIFT_EXPR:
01734 case RSHIFT_EXPR:
01735 case LROTATE_EXPR:
01736 case RROTATE_EXPR:
01737 case VEC_LSHIFT_EXPR:
01738 case VEC_RSHIFT_EXPR:
01739
01740 case BIT_IOR_EXPR:
01741 case BIT_XOR_EXPR:
01742 case BIT_AND_EXPR:
01743 case BIT_NOT_EXPR:
01744
01745 case TRUTH_ANDIF_EXPR:
01746 case TRUTH_ORIF_EXPR:
01747 case TRUTH_AND_EXPR:
01748 case TRUTH_OR_EXPR:
01749 case TRUTH_XOR_EXPR:
01750 case TRUTH_NOT_EXPR:
01751
01752 case LT_EXPR:
01753 case LE_EXPR:
01754 case GT_EXPR:
01755 case GE_EXPR:
01756 case EQ_EXPR:
01757 case NE_EXPR:
01758 case ORDERED_EXPR:
01759 case UNORDERED_EXPR:
01760
01761 case UNLT_EXPR:
01762 case UNLE_EXPR:
01763 case UNGT_EXPR:
01764 case UNGE_EXPR:
01765 case UNEQ_EXPR:
01766 case LTGT_EXPR:
01767
01768 case CONVERT_EXPR:
01769
01770 case CONJ_EXPR:
01771
01772 case PREDECREMENT_EXPR:
01773 case PREINCREMENT_EXPR:
01774 case POSTDECREMENT_EXPR:
01775 case POSTINCREMENT_EXPR:
01776
01777 case SWITCH_EXPR:
01778
01779 case ASM_EXPR:
01780
01781 case REALIGN_LOAD_EXPR:
01782
01783 case REDUC_MAX_EXPR:
01784 case REDUC_MIN_EXPR:
01785 case REDUC_PLUS_EXPR:
01786 case WIDEN_SUM_EXPR:
01787 case DOT_PROD_EXPR:
01788
01789 case WIDEN_MULT_EXPR:
01790
01791 case RESX_EXPR:
01792 *count += 1;
01793 break;
01794
01795
01796
01797 case TRUNC_DIV_EXPR:
01798 case CEIL_DIV_EXPR:
01799 case FLOOR_DIV_EXPR:
01800 case ROUND_DIV_EXPR:
01801 case EXACT_DIV_EXPR:
01802 case TRUNC_MOD_EXPR:
01803 case CEIL_MOD_EXPR:
01804 case FLOOR_MOD_EXPR:
01805 case ROUND_MOD_EXPR:
01806 case RDIV_EXPR:
01807 *count += 10;
01808 break;
01809 case CALL_EXPR:
01810 {
01811 tree decl = get_callee_fndecl (x);
01812 tree arg;
01813
01814 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
01815 switch (DECL_FUNCTION_CODE (decl))
01816 {
01817 case BUILT_IN_CONSTANT_P:
01818 *walk_subtrees = 0;
01819 return NULL_TREE;
01820 case BUILT_IN_EXPECT:
01821 return NULL_TREE;
01822 default:
01823 break;
01824 }
01825
01826
01827
01828 if (!decl)
01829 {
01830 for (arg = TREE_OPERAND (x, 1); arg; arg = TREE_CHAIN (arg))
01831 *count += estimate_move_cost (TREE_TYPE (TREE_VALUE (arg)));
01832 }
01833 else
01834 {
01835 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
01836 *count += estimate_move_cost (TREE_TYPE (arg));
01837 }
01838
01839 *count += PARAM_VALUE (PARAM_INLINE_CALL_COST);
01840 break;
01841 }
01842
01843 case OMP_PARALLEL:
01844 case OMP_FOR:
01845 case OMP_SECTIONS:
01846 case OMP_SINGLE:
01847 case OMP_SECTION:
01848 case OMP_MASTER:
01849 case OMP_ORDERED:
01850 case OMP_CRITICAL:
01851 case OMP_ATOMIC:
01852
01853 *count += 40;
01854 break;
01855
01856 default:
01857 #ifdef KEY
01858
01859
01860
01861 if (flag_spin_file) {
01862 *count += 1;
01863 break;
01864 }
01865 #endif
01866 gcc_unreachable ();
01867 }
01868 return NULL;
01869 }
01870
01871
01872
01873 int
01874 estimate_num_insns (tree expr)
01875 {
01876 int num = 0;
01877 struct pointer_set_t *visited_nodes;
01878 basic_block bb;
01879 block_stmt_iterator bsi;
01880 struct function *my_function;
01881
01882
01883 if (TREE_CODE (expr) == FUNCTION_DECL)
01884 {
01885 my_function = DECL_STRUCT_FUNCTION (expr);
01886 gcc_assert (my_function && my_function->cfg);
01887 visited_nodes = pointer_set_create ();
01888 FOR_EACH_BB_FN (bb, my_function)
01889 {
01890 for (bsi = bsi_start (bb);
01891 !bsi_end_p (bsi);
01892 bsi_next (&bsi))
01893 {
01894 walk_tree (bsi_stmt_ptr (bsi), estimate_num_insns_1,
01895 &num, visited_nodes);
01896 }
01897 }
01898 pointer_set_destroy (visited_nodes);
01899 }
01900 else
01901 walk_tree_without_duplicates (&expr, estimate_num_insns_1, &num);
01902
01903 return num;
01904 }
01905
01906 typedef struct function *function_p;
01907
01908 DEF_VEC_P(function_p);
01909 DEF_VEC_ALLOC_P(function_p,heap);
01910
01911
01912 static VEC(function_p,heap) *cfun_stack;
01913
01914 void
01915 push_cfun (struct function *new_cfun)
01916 {
01917 VEC_safe_push (function_p, heap, cfun_stack, cfun);
01918 cfun = new_cfun;
01919 }
01920
01921 void
01922 pop_cfun (void)
01923 {
01924 cfun = VEC_pop (function_p, cfun_stack);
01925 }
01926
01927
01928 static void
01929 add_lexical_block (tree current_block, tree new_block)
01930 {
01931 tree *blk_p;
01932
01933
01934 for (blk_p = &BLOCK_SUBBLOCKS (current_block);
01935 *blk_p;
01936 blk_p = &TREE_CHAIN (*blk_p))
01937 ;
01938 *blk_p = new_block;
01939 BLOCK_SUPERCONTEXT (new_block) = current_block;
01940 }
01941
01942
01943
01944 static bool
01945 expand_call_inline (basic_block bb, tree stmt, tree *tp, void *data)
01946 {
01947 copy_body_data *id;
01948 tree t;
01949 tree use_retvar;
01950 tree fn;
01951 splay_tree st;
01952 tree args;
01953 tree return_slot_addr;
01954 tree modify_dest;
01955 location_t saved_location;
01956 struct cgraph_edge *cg_edge;
01957 const char *reason;
01958 basic_block return_block;
01959 edge e;
01960 block_stmt_iterator bsi, stmt_bsi;
01961 bool successfully_inlined = FALSE;
01962 bool purge_dead_abnormal_edges;
01963 tree t_step;
01964 tree var;
01965
01966
01967 id = (copy_body_data *) data;
01968 t = *tp;
01969
01970
01971
01972 saved_location = input_location;
01973 if (EXPR_HAS_LOCATION (t))
01974 input_location = EXPR_LOCATION (t);
01975
01976
01977 if (TREE_CODE (t) != CALL_EXPR)
01978 goto egress;
01979
01980
01981
01982 fn = get_callee_fndecl (t);
01983 if (!fn)
01984 goto egress;
01985
01986
01987 fn = cgraph_node (fn)->decl;
01988
01989
01990
01991
01992
01993
01994
01995
01996 if (! DECL_INITIAL (fn)
01997 && DECL_ABSTRACT_ORIGIN (fn)
01998 && DECL_SAVED_TREE (DECL_ABSTRACT_ORIGIN (fn)))
01999 fn = DECL_ABSTRACT_ORIGIN (fn);
02000
02001
02002
02003 if (!id->dst_node->analyzed)
02004 goto egress;
02005
02006 cg_edge = cgraph_edge (id->dst_node, stmt);
02007
02008
02009
02010 if (!cg_edge)
02011 {
02012 struct cgraph_node *dest = cgraph_node (fn);
02013
02014
02015
02016
02017
02018 gcc_assert (dest->needed || !flag_unit_at_a_time);
02019 cgraph_create_edge (id->dst_node, dest, stmt,
02020 bb->count, bb->loop_depth)->inline_failed
02021 = N_("originally indirect function call not considered for inlining");
02022 goto egress;
02023 }
02024
02025
02026
02027 if (!cgraph_inline_p (cg_edge, &reason))
02028 {
02029 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
02030
02031 && (!flag_unit_at_a_time || cgraph_global_info_ready))
02032 {
02033 sorry ("inlining failed in call to %q+F: %s", fn, reason);
02034 sorry ("called from here");
02035 }
02036 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
02037 && !DECL_IN_SYSTEM_HEADER (fn)
02038 && strlen (reason)
02039 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
02040
02041 && (!flag_unit_at_a_time || cgraph_global_info_ready))
02042 {
02043 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
02044 fn, reason);
02045 warning (OPT_Winline, "called from here");
02046 }
02047 goto egress;
02048 }
02049 fn = cg_edge->callee->decl;
02050
02051 #ifdef ENABLE_CHECKING
02052 if (cg_edge->callee->decl != id->dst_node->decl)
02053 verify_cgraph_node (cg_edge->callee);
02054 #endif
02055
02056
02057 id->eh_region = lookup_stmt_eh_region (stmt);
02058
02059
02060 e = split_block (bb, stmt);
02061 bb = e->src;
02062 return_block = e->dest;
02063 remove_edge (e);
02064
02065
02066
02067
02068
02069 stmt_bsi = bsi_last (bb);
02070 bsi_remove (&stmt_bsi, false);
02071
02072
02073
02074
02075 bsi = bsi_start (return_block);
02076 if (bsi_end_p (bsi))
02077 {
02078 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
02079 purge_dead_abnormal_edges = true;
02080 }
02081 else
02082 {
02083 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
02084 purge_dead_abnormal_edges = false;
02085 }
02086
02087 stmt_bsi = bsi_start (return_block);
02088
02089
02090
02091
02092
02093 id->block = make_node (BLOCK);
02094 BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
02095 BLOCK_SOURCE_LOCATION (id->block) = input_location;
02096 add_lexical_block (TREE_BLOCK (stmt), id->block);
02097
02098
02099
02100 st = id->decl_map;
02101 id->decl_map = splay_tree_new (splay_tree_compare_pointers,
02102 NULL, NULL);
02103
02104
02105 args = TREE_OPERAND (t, 1);
02106
02107
02108 id->src_fn = fn;
02109 id->src_node = cg_edge->callee;
02110
02111 initialize_inlined_parameters (id, args, TREE_OPERAND (t, 2), fn, bb);
02112
02113 if (DECL_INITIAL (fn))
02114 add_lexical_block (id->block, remap_blocks (DECL_INITIAL (fn), id));
02115
02116
02117
02118
02119 gcc_assert (DECL_INITIAL (fn));
02120 gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
02121
02122
02123 return_slot_addr = NULL;
02124 if (TREE_CODE (stmt) == MODIFY_EXPR)
02125 {
02126 modify_dest = TREE_OPERAND (stmt, 0);
02127
02128
02129
02130
02131
02132
02133
02134 if (DECL_P (modify_dest))
02135 TREE_NO_WARNING (modify_dest) = 1;
02136 if (CALL_EXPR_RETURN_SLOT_OPT (t))
02137 {
02138 return_slot_addr = build_fold_addr_expr (modify_dest);
02139 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
02140 modify_dest = NULL;
02141 }
02142 }
02143 else
02144 modify_dest = NULL;
02145
02146
02147 declare_return_variable (id, return_slot_addr,
02148 modify_dest, &use_retvar);
02149
02150
02151
02152
02153
02154
02155 copy_body (id, bb->count, bb->frequency, bb, return_block);
02156
02157
02158 t_step = id->src_cfun->unexpanded_var_list;
02159 for (; t_step; t_step = TREE_CHAIN (t_step))
02160 {
02161 var = TREE_VALUE (t_step);
02162 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
02163 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
02164 cfun->unexpanded_var_list);
02165 else
02166 cfun->unexpanded_var_list = tree_cons (NULL_TREE, remap_decl (var, id),
02167 cfun->unexpanded_var_list);
02168 }
02169
02170
02171 splay_tree_delete (id->decl_map);
02172 id->decl_map = st;
02173
02174
02175
02176 if (use_retvar && (TREE_CODE (bsi_stmt (stmt_bsi)) != CALL_EXPR))
02177 {
02178 *tp = use_retvar;
02179 maybe_clean_or_replace_eh_stmt (stmt, stmt);
02180 }
02181 else
02182
02183
02184 bsi_remove (&stmt_bsi, true);
02185
02186 if (purge_dead_abnormal_edges)
02187 tree_purge_dead_abnormal_call_edges (return_block);
02188
02189
02190
02191
02192 TREE_USED (*tp) = 1;
02193
02194
02195
02196
02197
02198 (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
02199
02200
02201 cgraph_remove_node (cg_edge->callee);
02202
02203 id->block = NULL_TREE;
02204 successfully_inlined = TRUE;
02205
02206 egress:
02207 input_location = saved_location;
02208 return successfully_inlined;
02209 }
02210
02211
02212
02213
02214
02215
02216
02217 static bool
02218 gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
02219 {
02220 block_stmt_iterator bsi;
02221
02222
02223 tree_register_cfg_hooks ();
02224 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
02225 {
02226 tree *expr_p = bsi_stmt_ptr (bsi);
02227 tree stmt = *expr_p;
02228
02229 if (TREE_CODE (*expr_p) == MODIFY_EXPR)
02230 expr_p = &TREE_OPERAND (*expr_p, 1);
02231 if (TREE_CODE (*expr_p) == WITH_SIZE_EXPR)
02232 expr_p = &TREE_OPERAND (*expr_p, 0);
02233 if (TREE_CODE (*expr_p) == CALL_EXPR)
02234 if (expand_call_inline (bb, stmt, expr_p, id))
02235 return true;
02236 }
02237 return false;
02238 }
02239
02240
02241
02242 void
02243 optimize_inline_calls (tree fn)
02244 {
02245 copy_body_data id;
02246 tree prev_fn;
02247 basic_block bb;
02248
02249
02250
02251 if (errorcount || sorrycount)
02252 return;
02253
02254
02255 memset (&id, 0, sizeof (id));
02256
02257 id.src_node = id.dst_node = cgraph_node (fn);
02258 id.dst_fn = fn;
02259
02260 prev_fn = NULL_TREE;
02261 if (current_function_decl)
02262 {
02263 id.dst_fn = current_function_decl;
02264 prev_fn = current_function_decl;
02265 }
02266
02267 id.copy_decl = copy_decl_maybe_to_var;
02268 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
02269 id.transform_new_cfg = false;
02270 id.transform_return_to_modify = true;
02271 id.transform_lang_insert_block = false;
02272
02273 push_gimplify_context ();
02274
02275
02276
02277
02278
02279
02280
02281 FOR_EACH_BB (bb)
02282 gimple_expand_calls_inline (bb, &id);
02283
02284 pop_gimplify_context (NULL);
02285
02286 compact_blocks ();
02287
02288 number_blocks (fn);
02289
02290 #ifdef ENABLE_CHECKING
02291 {
02292 struct cgraph_edge *e;
02293
02294 verify_cgraph_node (id.dst_node);
02295
02296
02297 for (e = id.dst_node->callees; e; e = e->next_callee)
02298 gcc_assert (e->inline_failed);
02299 }
02300 #endif
02301
02302
02303 if (ENTRY_BLOCK_PTR->count)
02304 counts_to_freqs ();
02305 fold_cond_expr_cond ();
02306 }
02307
02308
02309
02310
02311
02312 void
02313 clone_body (tree clone, tree fn, void *arg_map)
02314 {
02315 copy_body_data id;
02316
02317
02318
02319 memset (&id, 0, sizeof (id));
02320 id.src_fn = fn;
02321 id.dst_fn = clone;
02322 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
02323 id.decl_map = (splay_tree)arg_map;
02324
02325 id.copy_decl = copy_decl_no_change;
02326 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
02327 id.transform_new_cfg = true;
02328 id.transform_return_to_modify = false;
02329 id.transform_lang_insert_block = true;
02330
02331
02332 id.eh_region = -1;
02333
02334
02335 append_to_statement_list_force (copy_generic_body (&id), &DECL_SAVED_TREE (clone));
02336 }
02337
02338
02339
02340 tree
02341 copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
02342 {
02343 enum tree_code code = TREE_CODE (*tp);
02344
02345
02346 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
02347 || code == TREE_LIST
02348 || code == TREE_VEC
02349 || code == TYPE_DECL
02350 || code == OMP_CLAUSE)
02351 {
02352
02353
02354 tree chain = TREE_CHAIN (*tp);
02355 tree new;
02356
02357
02358 new = copy_node (*tp);
02359
02360
02361 if (flag_mudflap && mf_marked_p (*tp))
02362 mf_mark (new);
02363
02364 *tp = new;
02365
02366
02367
02368 if (code == PARM_DECL
02369 || code == TREE_LIST
02370 || code == OMP_CLAUSE)
02371 TREE_CHAIN (*tp) = chain;
02372
02373
02374
02375 if (TREE_CODE (*tp) == BIND_EXPR)
02376 BIND_EXPR_BLOCK (*tp) = NULL_TREE;
02377 }
02378 else if (code == CONSTRUCTOR)
02379 {
02380
02381
02382 tree new;
02383
02384 new = copy_node (*tp);
02385
02386
02387 if (flag_mudflap && mf_marked_p (*tp))
02388 mf_mark (new);
02389
02390 CONSTRUCTOR_ELTS (new) = VEC_copy (constructor_elt, gc,
02391 CONSTRUCTOR_ELTS (*tp));
02392 *tp = new;
02393 }
02394 else if (TREE_CODE_CLASS (code) == tcc_type)
02395 *walk_subtrees = 0;
02396 else if (TREE_CODE_CLASS (code) == tcc_declaration)
02397 *walk_subtrees = 0;
02398 else if (TREE_CODE_CLASS (code) == tcc_constant)
02399 *walk_subtrees = 0;
02400 else
02401 gcc_assert (code != STATEMENT_LIST);
02402 return NULL_TREE;
02403 }
02404
02405
02406
02407
02408
02409
02410 static void
02411 remap_save_expr (tree *tp, void *st_, int *walk_subtrees)
02412 {
02413 splay_tree st = (splay_tree) st_;
02414 splay_tree_node n;
02415 tree t;
02416
02417
02418 n = splay_tree_lookup (st, (splay_tree_key) *tp);
02419
02420
02421 if (!n)
02422 {
02423 t = copy_node (*tp);
02424
02425
02426 splay_tree_insert (st, (splay_tree_key) *tp, (splay_tree_value) t);
02427
02428 splay_tree_insert (st, (splay_tree_key) t, (splay_tree_value) t);
02429 }
02430 else
02431 {
02432
02433 *walk_subtrees = 0;
02434 t = (tree) n->value;
02435 }
02436
02437
02438 *tp = t;
02439 }
02440
02441
02442
02443
02444
02445 static tree
02446 mark_local_for_remap_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
02447 void *data)
02448 {
02449 copy_body_data *id = (copy_body_data *) data;
02450
02451
02452 if (TYPE_P (*tp))
02453 *walk_subtrees = 0;
02454
02455 else if (TREE_CODE (*tp) == LABEL_EXPR)
02456 {
02457 tree decl = TREE_OPERAND (*tp, 0);
02458
02459
02460 insert_decl_map (id, decl, id->copy_decl (decl, id));
02461 }
02462
02463 return NULL_TREE;
02464 }
02465
02466
02467
02468
02469 static void
02470 unsave_expr_1 (tree expr)
02471 {
02472 switch (TREE_CODE (expr))
02473 {
02474 case TARGET_EXPR:
02475
02476
02477
02478
02479 if (TREE_OPERAND (expr, 1))
02480 break;
02481
02482 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
02483 TREE_OPERAND (expr, 3) = NULL_TREE;
02484 break;
02485
02486 default:
02487 break;
02488 }
02489 }
02490
02491
02492
02493
02494
02495 static tree
02496 unsave_r (tree *tp, int *walk_subtrees, void *data)
02497 {
02498 copy_body_data *id = (copy_body_data *) data;
02499 splay_tree st = id->decl_map;
02500 splay_tree_node n;
02501
02502
02503 if ((TREE_CODE (*tp) == VAR_DECL && !TREE_STATIC (*tp))
02504 || TREE_CODE (*tp) == LABEL_DECL)
02505 {
02506
02507 n = splay_tree_lookup (st, (splay_tree_key) *tp);
02508
02509
02510 if (n)
02511 *tp = (tree) n->value;
02512 }
02513
02514 else if (TREE_CODE (*tp) == STATEMENT_LIST)
02515 copy_statement_list (tp);
02516 else if (TREE_CODE (*tp) == BIND_EXPR)
02517 copy_bind_expr (tp, walk_subtrees, id);
02518 else if (TREE_CODE (*tp) == SAVE_EXPR)
02519 remap_save_expr (tp, st, walk_subtrees);
02520 else
02521 {
02522 copy_tree_r (tp, walk_subtrees, NULL);
02523
02524
02525 unsave_expr_1 (*tp);
02526 }
02527
02528
02529 return NULL_TREE;
02530 }
02531
02532
02533
02534
02535 tree
02536 unsave_expr_now (tree expr)
02537 {
02538 copy_body_data id;
02539
02540
02541 if (expr == 0)
02542 return expr;
02543
02544
02545 memset (&id, 0, sizeof (id));
02546 id.src_fn = current_function_decl;
02547 id.dst_fn = current_function_decl;
02548 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
02549
02550 id.copy_decl = copy_decl_no_change;
02551 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
02552 id.transform_new_cfg = false;
02553 id.transform_return_to_modify = false;
02554 id.transform_lang_insert_block = false;
02555
02556
02557 walk_tree_without_duplicates (&expr, mark_local_for_remap_r, &id);
02558
02559
02560 walk_tree (&expr, unsave_r, &id, NULL);
02561
02562
02563 splay_tree_delete (id.decl_map);
02564
02565 return expr;
02566 }
02567
02568
02569
02570 static tree
02571 debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
02572 {
02573 if (*tp == data)
02574 return (tree) data;
02575 else
02576 return NULL;
02577 }
02578
02579 bool
02580 debug_find_tree (tree top, tree search)
02581 {
02582 return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
02583 }
02584
02585
02586
02587
02588
02589 static void
02590 declare_inline_vars (tree block, tree vars)
02591 {
02592 tree t;
02593 for (t = vars; t; t = TREE_CHAIN (t))
02594 {
02595 DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
02596 gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
02597 cfun->unexpanded_var_list =
02598 tree_cons (NULL_TREE, t,
02599 cfun->unexpanded_var_list);
02600 }
02601
02602 if (block)
02603 BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
02604 }
02605
02606
02607
02608
02609
02610
02611 static tree
02612 copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
02613 {
02614
02615
02616 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
02617 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
02618
02619
02620
02621 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
02622
02623
02624 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
02625 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
02626 SET_DECL_RTL (copy, NULL_RTX);
02627
02628
02629 TREE_USED (copy) = 1;
02630
02631
02632 if (!DECL_CONTEXT (decl))
02633
02634 ;
02635 else if (DECL_CONTEXT (decl) != id->src_fn)
02636
02637
02638 ;
02639 else if (TREE_STATIC (decl))
02640
02641
02642 ;
02643 else
02644
02645
02646 DECL_CONTEXT (copy) = id->dst_fn;
02647
02648 return copy;
02649 }
02650
02651 static tree
02652 copy_decl_to_var (tree decl, copy_body_data *id)
02653 {
02654 tree copy, type;
02655
02656 gcc_assert (TREE_CODE (decl) == PARM_DECL
02657 || TREE_CODE (decl) == RESULT_DECL);
02658
02659 type = TREE_TYPE (decl);
02660
02661 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
02662 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
02663 TREE_READONLY (copy) = TREE_READONLY (decl);
02664 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
02665 DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
02666
02667 return copy_decl_for_dup_finish (id, decl, copy);
02668 }
02669
02670
02671
02672
02673 static tree
02674 copy_result_decl_to_var (tree decl, copy_body_data *id)
02675 {
02676 tree copy, type;
02677
02678 gcc_assert (TREE_CODE (decl) == PARM_DECL
02679 || TREE_CODE (decl) == RESULT_DECL);
02680
02681 type = TREE_TYPE (decl);
02682 if (DECL_BY_REFERENCE (decl))
02683 type = TREE_TYPE (type);
02684
02685 copy = build_decl (VAR_DECL, DECL_NAME (decl), type);
02686 TREE_READONLY (copy) = TREE_READONLY (decl);
02687 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
02688 if (!DECL_BY_REFERENCE (decl))
02689 {
02690 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
02691 DECL_COMPLEX_GIMPLE_REG_P (copy) = DECL_COMPLEX_GIMPLE_REG_P (decl);
02692 }
02693
02694 return copy_decl_for_dup_finish (id, decl, copy);
02695 }
02696
02697
02698 static tree
02699 copy_decl_no_change (tree decl, copy_body_data *id)
02700 {
02701 tree copy;
02702
02703 copy = copy_node (decl);
02704
02705
02706 DECL_ABSTRACT (copy) = 0;
02707 lang_hooks.dup_lang_specific_decl (copy);
02708
02709
02710
02711 if (TREE_CODE (copy) == LABEL_DECL)
02712 {
02713 TREE_ADDRESSABLE (copy) = 0;
02714 LABEL_DECL_UID (copy) = -1;
02715 }
02716
02717 return copy_decl_for_dup_finish (id, decl, copy);
02718 }
02719
02720 static tree
02721 copy_decl_maybe_to_var (tree decl, copy_body_data *id)
02722 {
02723 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
02724 return copy_decl_to_var (decl, id);
02725 else
02726 return copy_decl_no_change (decl, id);
02727 }
02728
02729
02730 static tree
02731 copy_arguments_for_versioning (tree orig_parm, copy_body_data * id)
02732 {
02733 tree *arg_copy, *parg;
02734
02735 arg_copy = &orig_parm;
02736 for (parg = arg_copy; *parg; parg = &TREE_CHAIN (*parg))
02737 {
02738 tree new = remap_decl (*parg, id);
02739 lang_hooks.dup_lang_specific_decl (new);
02740 TREE_CHAIN (new) = TREE_CHAIN (*parg);
02741 *parg = new;
02742 }
02743 return orig_parm;
02744 }
02745
02746
02747 static tree
02748 copy_static_chain (tree static_chain, copy_body_data * id)
02749 {
02750 tree *chain_copy, *pvar;
02751
02752 chain_copy = &static_chain;
02753 for (pvar = chain_copy; *pvar; pvar = &TREE_CHAIN (*pvar))
02754 {
02755 tree new = remap_decl (*pvar, id);
02756 lang_hooks.dup_lang_specific_decl (new);
02757 TREE_CHAIN (new) = TREE_CHAIN (*pvar);
02758 *pvar = new;
02759 }
02760 return static_chain;
02761 }
02762
02763
02764
02765 bool
02766 tree_versionable_function_p (tree fndecl)
02767 {
02768 if (fndecl == NULL_TREE)
02769 return false;
02770
02771
02772 if (!tree_inlinable_function_p (fndecl))
02773 return false;
02774
02775 return true;
02776 }
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786 void
02787 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map,
02788 bool update_clones)
02789 {
02790 struct cgraph_node *old_version_node;
02791 struct cgraph_node *new_version_node;
02792 copy_body_data id;
02793 tree p, new_fndecl;
02794 unsigned i;
02795 struct ipa_replace_map *replace_info;
02796 basic_block old_entry_block;
02797 tree t_step;
02798
02799 gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
02800 && TREE_CODE (new_decl) == FUNCTION_DECL);
02801 DECL_POSSIBLY_INLINED (old_decl) = 1;
02802
02803 old_version_node = cgraph_node (old_decl);
02804 new_version_node = cgraph_node (new_decl);
02805
02806 allocate_struct_function (new_decl);
02807
02808 cfun->function_end_locus = DECL_SOURCE_LOCATION (new_decl);
02809
02810 DECL_ARTIFICIAL (new_decl) = 1;
02811 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
02812
02813
02814 if (!update_clones)
02815 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
02816
02817 if (DECL_RTL (old_decl) != NULL)
02818 {
02819 SET_DECL_RTL (new_decl, copy_rtx (DECL_RTL (old_decl)));
02820 XEXP (DECL_RTL (new_decl), 0) =
02821 gen_rtx_SYMBOL_REF (GET_MODE (XEXP (DECL_RTL (old_decl), 0)),
02822 IDENTIFIER_POINTER (DECL_NAME (new_decl)));
02823 }
02824
02825
02826 memset (&id, 0, sizeof (id));
02827
02828 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
02829 id.src_fn = old_decl;
02830 id.dst_fn = new_decl;
02831 id.src_node = old_version_node;
02832 id.dst_node = new_version_node;
02833 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
02834
02835 id.copy_decl = copy_decl_no_change;
02836 id.transform_call_graph_edges
02837 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
02838 id.transform_new_cfg = true;
02839 id.transform_return_to_modify = false;
02840 id.transform_lang_insert_block = false;
02841
02842 current_function_decl = new_decl;
02843
02844
02845 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
02846 if (p)
02847 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
02848 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
02849 &id);
02850
02851 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
02852 DECL_ARGUMENTS (new_decl) =
02853 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id);
02854
02855
02856 if (tree_map)
02857 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++)
02858 {
02859 replace_info = VARRAY_GENERIC_PTR (tree_map, i);
02860 if (replace_info->replace_p)
02861 insert_decl_map (&id, replace_info->old_tree,
02862 replace_info->new_tree);
02863 }
02864
02865 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
02866
02867
02868 number_blocks (id.dst_fn);
02869
02870 if (DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list != NULL_TREE)
02871
02872 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->unexpanded_var_list;
02873 t_step; t_step = TREE_CHAIN (t_step))
02874 {
02875 tree var = TREE_VALUE (t_step);
02876 if (TREE_STATIC (var) && !TREE_ASM_WRITTEN (var))
02877 cfun->unexpanded_var_list = tree_cons (NULL_TREE, var,
02878 cfun->unexpanded_var_list);
02879 else
02880 cfun->unexpanded_var_list =
02881 tree_cons (NULL_TREE, remap_decl (var, &id),
02882 cfun->unexpanded_var_list);
02883 }
02884
02885
02886 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
02887 (DECL_STRUCT_FUNCTION (old_decl));
02888 new_fndecl = copy_body (&id,
02889 old_entry_block->count,
02890 old_entry_block->frequency, NULL, NULL);
02891
02892 DECL_SAVED_TREE (new_decl) = DECL_SAVED_TREE (new_fndecl);
02893
02894 DECL_STRUCT_FUNCTION (new_decl)->cfg =
02895 DECL_STRUCT_FUNCTION (new_fndecl)->cfg;
02896 DECL_STRUCT_FUNCTION (new_decl)->eh = DECL_STRUCT_FUNCTION (new_fndecl)->eh;
02897 DECL_STRUCT_FUNCTION (new_decl)->ib_boundaries_block =
02898 DECL_STRUCT_FUNCTION (new_fndecl)->ib_boundaries_block;
02899 DECL_STRUCT_FUNCTION (new_decl)->last_label_uid =
02900 DECL_STRUCT_FUNCTION (new_fndecl)->last_label_uid;
02901
02902 if (DECL_RESULT (old_decl) != NULL_TREE)
02903 {
02904 tree *res_decl = &DECL_RESULT (old_decl);
02905 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
02906 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
02907 }
02908
02909 current_function_decl = NULL;
02910
02911 number_blocks (new_decl);
02912
02913
02914 splay_tree_delete (id.decl_map);
02915 fold_cond_expr_cond ();
02916 return;
02917 }
02918
02919
02920
02921 tree
02922 build_duplicate_type (tree type)
02923 {
02924 struct copy_body_data id;
02925
02926 memset (&id, 0, sizeof (id));
02927 id.src_fn = current_function_decl;
02928 id.dst_fn = current_function_decl;
02929 id.src_cfun = cfun;
02930 id.decl_map = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
02931
02932 type = remap_type_1 (type, &id);
02933
02934 splay_tree_delete (id.decl_map);
02935
02936 return type;
02937 }