comparison gcc/tree-inline.c @ 55:77e2b8dfacca gcc-4.4.5

update it from 4.4.3 to 4.5.0
author ryoma <e075725@ie.u-ryukyu.ac.jp>
date Fri, 12 Feb 2010 23:39:51 +0900
parents 58ad6c70ea60
children b7f97abdc517
comparison
equal deleted inserted replaced
52:c156f1bd5cd9 55:77e2b8dfacca
30 #include "expr.h" 30 #include "expr.h"
31 #include "flags.h" 31 #include "flags.h"
32 #include "params.h" 32 #include "params.h"
33 #include "input.h" 33 #include "input.h"
34 #include "insn-config.h" 34 #include "insn-config.h"
35 #include "varray.h"
36 #include "hashtab.h" 35 #include "hashtab.h"
37 #include "langhooks.h" 36 #include "langhooks.h"
38 #include "basic-block.h" 37 #include "basic-block.h"
39 #include "tree-iterator.h" 38 #include "tree-iterator.h"
40 #include "cgraph.h" 39 #include "cgraph.h"
63 Inlining: a function body is duplicated, but the PARM_DECLs are 62 Inlining: a function body is duplicated, but the PARM_DECLs are
64 remapped into VAR_DECLs, and non-void RETURN_EXPRs become 63 remapped into VAR_DECLs, and non-void RETURN_EXPRs become
65 MODIFY_EXPRs that store to a dedicated returned-value variable. 64 MODIFY_EXPRs that store to a dedicated returned-value variable.
66 The duplicated eh_region info of the copy will later be appended 65 The duplicated eh_region info of the copy will later be appended
67 to the info for the caller; the eh_region info in copied throwing 66 to the info for the caller; the eh_region info in copied throwing
68 statements and RESX_EXPRs is adjusted accordingly. 67 statements and RESX statements are adjusted accordingly.
69 68
70 Cloning: (only in C++) We have one body for a con/de/structor, and 69 Cloning: (only in C++) We have one body for a con/de/structor, and
71 multiple function decls, each with a unique parameter list. 70 multiple function decls, each with a unique parameter list.
72 Duplicate the body, using the given splay tree; some parameters 71 Duplicate the body, using the given splay tree; some parameters
73 will become constants (like 0 or 1). 72 will become constants (like 0 or 1).
117 116
118 eni_weights eni_time_weights; 117 eni_weights eni_time_weights;
119 118
120 /* Prototypes. */ 119 /* Prototypes. */
121 120
122 static tree declare_return_variable (copy_body_data *, tree, tree, tree *); 121 static tree declare_return_variable (copy_body_data *, tree, tree);
123 static bool inlinable_function_p (tree);
124 static void remap_block (tree *, copy_body_data *); 122 static void remap_block (tree *, copy_body_data *);
125 static void copy_bind_expr (tree *, int *, copy_body_data *); 123 static void copy_bind_expr (tree *, int *, copy_body_data *);
126 static tree mark_local_for_remap_r (tree *, int *, void *); 124 static tree mark_local_for_remap_r (tree *, int *, void *);
127 static void unsave_expr_1 (tree); 125 static void unsave_expr_1 (tree);
128 static tree unsave_r (tree *, int *, void *); 126 static tree unsave_r (tree *, int *, void *);
131 static void prepend_lexical_block (tree current_block, tree new_block); 129 static void prepend_lexical_block (tree current_block, tree new_block);
132 static tree copy_decl_to_var (tree, copy_body_data *); 130 static tree copy_decl_to_var (tree, copy_body_data *);
133 static tree copy_result_decl_to_var (tree, copy_body_data *); 131 static tree copy_result_decl_to_var (tree, copy_body_data *);
134 static tree copy_decl_maybe_to_var (tree, copy_body_data *); 132 static tree copy_decl_maybe_to_var (tree, copy_body_data *);
135 static gimple remap_gimple_stmt (gimple, copy_body_data *); 133 static gimple remap_gimple_stmt (gimple, copy_body_data *);
134 static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
136 135
137 /* Insert a tree->tree mapping for ID. Despite the name suggests 136 /* Insert a tree->tree mapping for ID. Despite the name suggests
138 that the trees should be variables, it is used for more than that. */ 137 that the trees should be variables, it is used for more than that. */
139 138
140 void 139 void
146 node again, we won't want to duplicate it a second time. */ 145 node again, we won't want to duplicate it a second time. */
147 if (key != value) 146 if (key != value)
148 *pointer_map_insert (id->decl_map, value) = value; 147 *pointer_map_insert (id->decl_map, value) = value;
149 } 148 }
150 149
150 /* Insert a tree->tree mapping for ID. This is only used for
151 variables. */
152
153 static void
154 insert_debug_decl_map (copy_body_data *id, tree key, tree value)
155 {
156 if (!gimple_in_ssa_p (id->src_cfun))
157 return;
158
159 if (!MAY_HAVE_DEBUG_STMTS)
160 return;
161
162 if (!target_for_debug_bind (key))
163 return;
164
165 gcc_assert (TREE_CODE (key) == PARM_DECL);
166 gcc_assert (TREE_CODE (value) == VAR_DECL);
167
168 if (!id->debug_map)
169 id->debug_map = pointer_map_create ();
170
171 *pointer_map_insert (id->debug_map, key) = value;
172 }
173
151 /* Construct new SSA name for old NAME. ID is the inline context. */ 174 /* Construct new SSA name for old NAME. ID is the inline context. */
152 175
153 static tree 176 static tree
154 remap_ssa_name (tree name, copy_body_data *id) 177 remap_ssa_name (tree name, copy_body_data *id)
155 { 178 {
165 /* Do not set DEF_STMT yet as statement is not copied yet. We do that 188 /* Do not set DEF_STMT yet as statement is not copied yet. We do that
166 in copy_bb. */ 189 in copy_bb. */
167 new_tree = remap_decl (SSA_NAME_VAR (name), id); 190 new_tree = remap_decl (SSA_NAME_VAR (name), id);
168 191
169 /* We might've substituted constant or another SSA_NAME for 192 /* We might've substituted constant or another SSA_NAME for
170 the variable. 193 the variable.
171 194
172 Replace the SSA name representing RESULT_DECL by variable during 195 Replace the SSA name representing RESULT_DECL by variable during
173 inlining: this saves us from need to introduce PHI node in a case 196 inlining: this saves us from need to introduce PHI node in a case
174 return value is just partly initialized. */ 197 return value is just partly initialized. */
175 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL) 198 if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
198 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest 221 && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
199 || EDGE_COUNT (id->entry_bb->preds) != 1)) 222 || EDGE_COUNT (id->entry_bb->preds) != 1))
200 { 223 {
201 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb); 224 gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
202 gimple init_stmt; 225 gimple init_stmt;
203 226
204 init_stmt = gimple_build_assign (new_tree, 227 init_stmt = gimple_build_assign (new_tree,
205 fold_convert (TREE_TYPE (new_tree), 228 fold_convert (TREE_TYPE (new_tree),
206 integer_zero_node)); 229 integer_zero_node));
207 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT); 230 gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
208 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0; 231 SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
219 else 242 else
220 insert_decl_map (id, name, new_tree); 243 insert_decl_map (id, name, new_tree);
221 return new_tree; 244 return new_tree;
222 } 245 }
223 246
247 /* If nonzero, we're remapping the contents of inlined debug
248 statements. If negative, an error has occurred, such as a
249 reference to a variable that isn't available in the inlined
250 context. */
251 int processing_debug_stmt = 0;
252
224 /* Remap DECL during the copying of the BLOCK tree for the function. */ 253 /* Remap DECL during the copying of the BLOCK tree for the function. */
225 254
226 tree 255 tree
227 remap_decl (tree decl, copy_body_data *id) 256 remap_decl (tree decl, copy_body_data *id)
228 { 257 {
229 tree *n; 258 tree *n;
230 tree fn;
231 259
232 /* We only remap local variables in the current function. */ 260 /* We only remap local variables in the current function. */
233 fn = id->src_fn;
234 261
235 /* See if we have remapped this declaration. */ 262 /* See if we have remapped this declaration. */
236 263
237 n = (tree *) pointer_map_contains (id->decl_map, decl); 264 n = (tree *) pointer_map_contains (id->decl_map, decl);
265
266 if (!n && processing_debug_stmt)
267 {
268 processing_debug_stmt = -1;
269 return decl;
270 }
238 271
239 /* If we didn't already have an equivalent for this declaration, 272 /* If we didn't already have an equivalent for this declaration,
240 create one now. */ 273 create one now. */
241 if (!n) 274 if (!n)
242 { 275 {
243 /* Make a copy of the variable or label. */ 276 /* Make a copy of the variable or label. */
244 tree t = id->copy_decl (decl, id); 277 tree t = id->copy_decl (decl, id);
245 278
246 /* Remember it, so that if we encounter this local entity again 279 /* Remember it, so that if we encounter this local entity again
247 we can reuse this copy. Do this early because remap_type may 280 we can reuse this copy. Do this early because remap_type may
248 need this decl for TYPE_STUB_DECL. */ 281 need this decl for TYPE_STUB_DECL. */
249 insert_decl_map (id, decl, t); 282 insert_decl_map (id, decl, t);
250 283
286 add_referenced_var (t); 319 add_referenced_var (t);
287 } 320 }
288 return t; 321 return t;
289 } 322 }
290 323
291 return unshare_expr (*n); 324 if (id->do_not_unshare)
325 return *n;
326 else
327 return unshare_expr (*n);
292 } 328 }
293 329
294 static tree 330 static tree
295 remap_type_1 (tree type, copy_body_data *id) 331 remap_type_1 (tree type, copy_body_data *id)
296 { 332 {
302 if (TREE_CODE (type) == POINTER_TYPE) 338 if (TREE_CODE (type) == POINTER_TYPE)
303 { 339 {
304 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id), 340 new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
305 TYPE_MODE (type), 341 TYPE_MODE (type),
306 TYPE_REF_CAN_ALIAS_ALL (type)); 342 TYPE_REF_CAN_ALIAS_ALL (type));
343 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
344 new_tree = build_type_attribute_qual_variant (new_tree,
345 TYPE_ATTRIBUTES (type),
346 TYPE_QUALS (type));
307 insert_decl_map (id, type, new_tree); 347 insert_decl_map (id, type, new_tree);
308 return new_tree; 348 return new_tree;
309 } 349 }
310 else if (TREE_CODE (type) == REFERENCE_TYPE) 350 else if (TREE_CODE (type) == REFERENCE_TYPE)
311 { 351 {
312 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id), 352 new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
313 TYPE_MODE (type), 353 TYPE_MODE (type),
314 TYPE_REF_CAN_ALIAS_ALL (type)); 354 TYPE_REF_CAN_ALIAS_ALL (type));
355 if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
356 new_tree = build_type_attribute_qual_variant (new_tree,
357 TYPE_ATTRIBUTES (type),
358 TYPE_QUALS (type));
315 insert_decl_map (id, type, new_tree); 359 insert_decl_map (id, type, new_tree);
316 return new_tree; 360 return new_tree;
317 } 361 }
318 else 362 else
319 new_tree = copy_node (type); 363 new_tree = copy_node (type);
445 return NULL; 489 return NULL;
446 } 490 }
447 491
448 /* The type only needs remapping if it's variably modified. */ 492 /* The type only needs remapping if it's variably modified. */
449 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */ 493 /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
450 494
451 static bool 495 static bool
452 can_be_nonlocal (tree decl, copy_body_data *id) 496 can_be_nonlocal (tree decl, copy_body_data *id)
453 { 497 {
454 /* We can not duplicate function decls. */ 498 /* We can not duplicate function decls. */
455 if (TREE_CODE (decl) == FUNCTION_DECL) 499 if (TREE_CODE (decl) == FUNCTION_DECL)
496 tree origin_var = DECL_ORIGIN (old_var); 540 tree origin_var = DECL_ORIGIN (old_var);
497 541
498 if (can_be_nonlocal (old_var, id)) 542 if (can_be_nonlocal (old_var, id))
499 { 543 {
500 if (TREE_CODE (old_var) == VAR_DECL 544 if (TREE_CODE (old_var) == VAR_DECL
545 && ! DECL_EXTERNAL (old_var)
501 && (var_ann (old_var) || !gimple_in_ssa_p (cfun))) 546 && (var_ann (old_var) || !gimple_in_ssa_p (cfun)))
502 cfun->local_decls = tree_cons (NULL_TREE, old_var, 547 cfun->local_decls = tree_cons (NULL_TREE, old_var,
503 cfun->local_decls); 548 cfun->local_decls);
504 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE) 549 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
505 && !DECL_IGNORED_P (old_var) 550 && !DECL_IGNORED_P (old_var)
512 new_var = remap_decl (old_var, id); 557 new_var = remap_decl (old_var, id);
513 558
514 /* If we didn't remap this variable, we can't mess with its 559 /* If we didn't remap this variable, we can't mess with its
515 TREE_CHAIN. If we remapped this variable to the return slot, it's 560 TREE_CHAIN. If we remapped this variable to the return slot, it's
516 already declared somewhere else, so don't declare it here. */ 561 already declared somewhere else, so don't declare it here. */
517 562
518 if (new_var == id->retvar) 563 if (new_var == id->retvar)
519 ; 564 ;
520 else if (!new_var) 565 else if (!new_var)
521 { 566 {
522 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE) 567 if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
541 static void 586 static void
542 remap_block (tree *block, copy_body_data *id) 587 remap_block (tree *block, copy_body_data *id)
543 { 588 {
544 tree old_block; 589 tree old_block;
545 tree new_block; 590 tree new_block;
546 tree fn;
547 591
548 /* Make the new block. */ 592 /* Make the new block. */
549 old_block = *block; 593 old_block = *block;
550 new_block = make_node (BLOCK); 594 new_block = make_node (BLOCK);
551 TREE_USED (new_block) = TREE_USED (old_block); 595 TREE_USED (new_block) = TREE_USED (old_block);
558 /* Remap its variables. */ 602 /* Remap its variables. */
559 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block), 603 BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
560 &BLOCK_NONLOCALIZED_VARS (new_block), 604 &BLOCK_NONLOCALIZED_VARS (new_block),
561 id); 605 id);
562 606
563 fn = id->dst_fn;
564
565 if (id->transform_lang_insert_block) 607 if (id->transform_lang_insert_block)
566 id->transform_lang_insert_block (new_block); 608 id->transform_lang_insert_block (new_block);
567 609
568 /* Remember the remapped block. */ 610 /* Remember the remapped block. */
569 insert_decl_map (id, old_block, new_block); 611 insert_decl_map (id, old_block, new_block);
596 tree new_tree; 638 tree new_tree;
597 639
598 new_tree = alloc_stmt_list (); 640 new_tree = alloc_stmt_list ();
599 ni = tsi_start (new_tree); 641 ni = tsi_start (new_tree);
600 oi = tsi_start (*tp); 642 oi = tsi_start (*tp);
643 TREE_TYPE (new_tree) = TREE_TYPE (*tp);
601 *tp = new_tree; 644 *tp = new_tree;
602 645
603 for (; !tsi_end_p (oi); tsi_next (&oi)) 646 for (; !tsi_end_p (oi); tsi_next (&oi))
604 tsi_link_after (&ni, tsi_stmt (oi), TSI_NEW_STMT); 647 {
648 tree stmt = tsi_stmt (oi);
649 if (TREE_CODE (stmt) == STATEMENT_LIST)
650 copy_statement_list (&stmt);
651 tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
652 }
605 } 653 }
606 654
607 static void 655 static void
608 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id) 656 copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
609 { 657 {
703 /* Remap the declaration. */ 751 /* Remap the declaration. */
704 new_decl = remap_decl (*tp, id); 752 new_decl = remap_decl (*tp, id);
705 gcc_assert (new_decl); 753 gcc_assert (new_decl);
706 /* Replace this variable with the copy. */ 754 /* Replace this variable with the copy. */
707 STRIP_TYPE_NOPS (new_decl); 755 STRIP_TYPE_NOPS (new_decl);
756 /* ??? The C++ frontend uses void * pointer zero to initialize
757 any other type. This confuses the middle-end type verification.
758 As cloned bodies do not go through gimplification again the fixup
759 there doesn't trigger. */
760 if (TREE_CODE (new_decl) == INTEGER_CST
761 && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
762 new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
708 *tp = new_decl; 763 *tp = new_decl;
709 *walk_subtrees = 0; 764 *walk_subtrees = 0;
710 } 765 }
711 else if (TREE_CODE (*tp) == STATEMENT_LIST) 766 else if (TREE_CODE (*tp) == STATEMENT_LIST)
712 gcc_unreachable (); 767 gcc_unreachable ();
767 *tp = gimple_fold_indirect_ref (new_tree); 822 *tp = gimple_fold_indirect_ref (new_tree);
768 if (!*tp) 823 if (!*tp)
769 { 824 {
770 if (TREE_CODE (new_tree) == ADDR_EXPR) 825 if (TREE_CODE (new_tree) == ADDR_EXPR)
771 { 826 {
772 *tp = fold_indirect_ref_1 (type, new_tree); 827 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
828 type, new_tree);
773 /* ??? We should either assert here or build 829 /* ??? We should either assert here or build
774 a VIEW_CONVERT_EXPR instead of blindly leaking 830 a VIEW_CONVERT_EXPR instead of blindly leaking
775 incompatible types to our IL. */ 831 incompatible types to our IL. */
776 if (! *tp) 832 if (! *tp)
777 *tp = TREE_OPERAND (new_tree, 0); 833 *tp = TREE_OPERAND (new_tree, 0);
794 850
795 /* Global variables we haven't seen yet need to go into referenced 851 /* Global variables we haven't seen yet need to go into referenced
796 vars. If not referenced from types only. */ 852 vars. If not referenced from types only. */
797 if (gimple_in_ssa_p (cfun) 853 if (gimple_in_ssa_p (cfun)
798 && TREE_CODE (*tp) == VAR_DECL 854 && TREE_CODE (*tp) == VAR_DECL
799 && id->remapping_type_depth == 0) 855 && id->remapping_type_depth == 0
856 && !processing_debug_stmt)
800 add_referenced_var (*tp); 857 add_referenced_var (*tp);
801 858
802 /* We should never have TREE_BLOCK set on non-statements. */ 859 /* We should never have TREE_BLOCK set on non-statements. */
803 if (EXPR_P (*tp)) 860 if (EXPR_P (*tp))
804 gcc_assert (!TREE_BLOCK (*tp)); 861 gcc_assert (!TREE_BLOCK (*tp));
909 *tp = new_decl; 966 *tp = new_decl;
910 *walk_subtrees = 0; 967 *walk_subtrees = 0;
911 } 968 }
912 else if (TREE_CODE (*tp) == STATEMENT_LIST) 969 else if (TREE_CODE (*tp) == STATEMENT_LIST)
913 copy_statement_list (tp); 970 copy_statement_list (tp);
914 else if (TREE_CODE (*tp) == SAVE_EXPR) 971 else if (TREE_CODE (*tp) == SAVE_EXPR
972 || TREE_CODE (*tp) == TARGET_EXPR)
915 remap_save_expr (tp, id->decl_map, walk_subtrees); 973 remap_save_expr (tp, id->decl_map, walk_subtrees);
916 else if (TREE_CODE (*tp) == LABEL_DECL 974 else if (TREE_CODE (*tp) == LABEL_DECL
917 && (! DECL_CONTEXT (*tp) 975 && (! DECL_CONTEXT (*tp)
918 || decl_function_context (*tp) == id->src_fn)) 976 || decl_function_context (*tp) == id->src_fn))
919 /* These may need to be remapped for EH handling. */ 977 /* These may need to be remapped for EH handling. */
965 { 1023 {
966 value = *n; 1024 value = *n;
967 STRIP_TYPE_NOPS (value); 1025 STRIP_TYPE_NOPS (value);
968 if (TREE_CONSTANT (value) || TREE_READONLY (value)) 1026 if (TREE_CONSTANT (value) || TREE_READONLY (value))
969 { 1027 {
970 *tp = build_empty_stmt (); 1028 *tp = build_empty_stmt (EXPR_LOCATION (*tp));
971 return copy_tree_body_r (tp, walk_subtrees, data); 1029 return copy_tree_body_r (tp, walk_subtrees, data);
972 } 1030 }
973 } 1031 }
974 } 1032 }
975 else if (TREE_CODE (*tp) == INDIRECT_REF) 1033 else if (TREE_CODE (*tp) == INDIRECT_REF)
989 which lie about their types pointed to. In this case 1047 which lie about their types pointed to. In this case
990 build_fold_indirect_ref wouldn't strip the INDIRECT_REF, 1048 build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
991 but we absolutely rely on that. As fold_indirect_ref 1049 but we absolutely rely on that. As fold_indirect_ref
992 does other useful transformations, try that first, though. */ 1050 does other useful transformations, try that first, though. */
993 tree type = TREE_TYPE (TREE_TYPE (*n)); 1051 tree type = TREE_TYPE (TREE_TYPE (*n));
994 new_tree = unshare_expr (*n); 1052 if (id->do_not_unshare)
1053 new_tree = *n;
1054 else
1055 new_tree = unshare_expr (*n);
995 old = *tp; 1056 old = *tp;
996 *tp = gimple_fold_indirect_ref (new_tree); 1057 *tp = gimple_fold_indirect_ref (new_tree);
997 if (! *tp) 1058 if (! *tp)
998 { 1059 {
999 if (TREE_CODE (new_tree) == ADDR_EXPR) 1060 if (TREE_CODE (new_tree) == ADDR_EXPR)
1000 { 1061 {
1001 *tp = fold_indirect_ref_1 (type, new_tree); 1062 *tp = fold_indirect_ref_1 (EXPR_LOCATION (new_tree),
1063 type, new_tree);
1002 /* ??? We should either assert here or build 1064 /* ??? We should either assert here or build
1003 a VIEW_CONVERT_EXPR instead of blindly leaking 1065 a VIEW_CONVERT_EXPR instead of blindly leaking
1004 incompatible types to our IL. */ 1066 incompatible types to our IL. */
1005 if (! *tp) 1067 if (! *tp)
1006 *tp = TREE_OPERAND (new_tree, 0); 1068 *tp = TREE_OPERAND (new_tree, 0);
1020 /* Here is the "usual case". Copy this tree node, and then 1082 /* Here is the "usual case". Copy this tree node, and then
1021 tweak some special cases. */ 1083 tweak some special cases. */
1022 copy_tree_r (tp, walk_subtrees, NULL); 1084 copy_tree_r (tp, walk_subtrees, NULL);
1023 1085
1024 /* Global variables we haven't seen yet needs to go into referenced 1086 /* Global variables we haven't seen yet needs to go into referenced
1025 vars. If not referenced from types only. */ 1087 vars. If not referenced from types or debug stmts only. */
1026 if (gimple_in_ssa_p (cfun) 1088 if (gimple_in_ssa_p (cfun)
1027 && TREE_CODE (*tp) == VAR_DECL 1089 && TREE_CODE (*tp) == VAR_DECL
1028 && id->remapping_type_depth == 0) 1090 && id->remapping_type_depth == 0
1091 && !processing_debug_stmt)
1029 add_referenced_var (*tp); 1092 add_referenced_var (*tp);
1030 1093
1031 /* If EXPR has block defined, map it to newly constructed block. 1094 /* If EXPR has block defined, map it to newly constructed block.
1032 When inlining we want EXPRs without block appear in the block 1095 When inlining we want EXPRs without block appear in the block
1033 of function call. */ 1096 of function call if we are not remapping a type. */
1034 if (EXPR_P (*tp)) 1097 if (EXPR_P (*tp))
1035 { 1098 {
1036 new_block = id->block; 1099 new_block = id->remapping_type_depth == 0 ? id->block : NULL;
1037 if (TREE_BLOCK (*tp)) 1100 if (TREE_BLOCK (*tp))
1038 { 1101 {
1039 tree *n; 1102 tree *n;
1040 n = (tree *) pointer_map_contains (id->decl_map, 1103 n = (tree *) pointer_map_contains (id->decl_map,
1041 TREE_BLOCK (*tp)); 1104 TREE_BLOCK (*tp));
1043 new_block = *n; 1106 new_block = *n;
1044 } 1107 }
1045 TREE_BLOCK (*tp) = new_block; 1108 TREE_BLOCK (*tp) = new_block;
1046 } 1109 }
1047 1110
1048 if (TREE_CODE (*tp) == RESX_EXPR && id->eh_region_offset)
1049 TREE_OPERAND (*tp, 0) =
1050 build_int_cst (NULL_TREE,
1051 id->eh_region_offset
1052 + TREE_INT_CST_LOW (TREE_OPERAND (*tp, 0)));
1053
1054 if (TREE_CODE (*tp) != OMP_CLAUSE) 1111 if (TREE_CODE (*tp) != OMP_CLAUSE)
1055 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id); 1112 TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
1056 1113
1057 /* The copied TARGET_EXPR has never been expanded, even if the 1114 /* The copied TARGET_EXPR has never been expanded, even if the
1058 original node was expanded already. */ 1115 original node was expanded already. */
1088 1145
1089 /* Keep iterating. */ 1146 /* Keep iterating. */
1090 return NULL_TREE; 1147 return NULL_TREE;
1091 } 1148 }
1092 1149
1150 /* Helper for remap_gimple_stmt. Given an EH region number for the
1151 source function, map that to the duplicate EH region number in
1152 the destination function. */
1153
1154 static int
1155 remap_eh_region_nr (int old_nr, copy_body_data *id)
1156 {
1157 eh_region old_r, new_r;
1158 void **slot;
1159
1160 old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
1161 slot = pointer_map_contains (id->eh_map, old_r);
1162 new_r = (eh_region) *slot;
1163
1164 return new_r->index;
1165 }
1166
1167 /* Similar, but operate on INTEGER_CSTs. */
1168
1169 static tree
1170 remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
1171 {
1172 int old_nr, new_nr;
1173
1174 old_nr = tree_low_cst (old_t_nr, 0);
1175 new_nr = remap_eh_region_nr (old_nr, id);
1176
1177 return build_int_cst (NULL, new_nr);
1178 }
1093 1179
1094 /* Helper for copy_bb. Remap statement STMT using the inlining 1180 /* Helper for copy_bb. Remap statement STMT using the inlining
1095 information in ID. Return the new statement copy. */ 1181 information in ID. Return the new statement copy. */
1096 1182
1097 static gimple 1183 static gimple
1154 break; 1240 break;
1155 1241
1156 case GIMPLE_TRY: 1242 case GIMPLE_TRY:
1157 s1 = remap_gimple_seq (gimple_try_eval (stmt), id); 1243 s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
1158 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id); 1244 s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
1159 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt)); 1245 copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
1160 break; 1246 break;
1161 1247
1162 case GIMPLE_WITH_CLEANUP_EXPR: 1248 case GIMPLE_WITH_CLEANUP_EXPR:
1163 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id); 1249 s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
1164 copy = gimple_build_wce (s1); 1250 copy = gimple_build_wce (s1);
1269 if (TREE_CONSTANT (value) || TREE_READONLY (value)) 1355 if (TREE_CONSTANT (value) || TREE_READONLY (value))
1270 return gimple_build_nop (); 1356 return gimple_build_nop ();
1271 } 1357 }
1272 } 1358 }
1273 1359
1360 if (gimple_debug_bind_p (stmt))
1361 {
1362 copy = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
1363 gimple_debug_bind_get_value (stmt),
1364 stmt);
1365 VEC_safe_push (gimple, heap, id->debug_stmts, copy);
1366 return copy;
1367 }
1368
1274 /* Create a new deep copy of the statement. */ 1369 /* Create a new deep copy of the statement. */
1275 copy = gimple_copy (stmt); 1370 copy = gimple_copy (stmt);
1371
1372 /* Remap the region numbers for __builtin_eh_{pointer,filter},
1373 RESX and EH_DISPATCH. */
1374 if (id->eh_map)
1375 switch (gimple_code (copy))
1376 {
1377 case GIMPLE_CALL:
1378 {
1379 tree r, fndecl = gimple_call_fndecl (copy);
1380 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1381 switch (DECL_FUNCTION_CODE (fndecl))
1382 {
1383 case BUILT_IN_EH_COPY_VALUES:
1384 r = gimple_call_arg (copy, 1);
1385 r = remap_eh_region_tree_nr (r, id);
1386 gimple_call_set_arg (copy, 1, r);
1387 /* FALLTHRU */
1388
1389 case BUILT_IN_EH_POINTER:
1390 case BUILT_IN_EH_FILTER:
1391 r = gimple_call_arg (copy, 0);
1392 r = remap_eh_region_tree_nr (r, id);
1393 gimple_call_set_arg (copy, 0, r);
1394 break;
1395
1396 default:
1397 break;
1398 }
1399 }
1400 break;
1401
1402 case GIMPLE_RESX:
1403 {
1404 int r = gimple_resx_region (copy);
1405 r = remap_eh_region_nr (r, id);
1406 gimple_resx_set_region (copy, r);
1407 }
1408 break;
1409
1410 case GIMPLE_EH_DISPATCH:
1411 {
1412 int r = gimple_eh_dispatch_region (copy);
1413 r = remap_eh_region_nr (r, id);
1414 gimple_eh_dispatch_set_region (copy, r);
1415 }
1416 break;
1417
1418 default:
1419 break;
1420 }
1276 } 1421 }
1277 1422
1278 /* If STMT has a block defined, map it to the newly constructed 1423 /* If STMT has a block defined, map it to the newly constructed
1279 block. When inlining we want statements without a block to 1424 block. When inlining we want statements without a block to
1280 appear in the block of the function call. */ 1425 appear in the block of the function call. */
1286 gcc_assert (n); 1431 gcc_assert (n);
1287 new_block = *n; 1432 new_block = *n;
1288 } 1433 }
1289 1434
1290 gimple_set_block (copy, new_block); 1435 gimple_set_block (copy, new_block);
1436
1437 if (gimple_debug_bind_p (copy))
1438 return copy;
1291 1439
1292 /* Remap all the operands in COPY. */ 1440 /* Remap all the operands in COPY. */
1293 memset (&wi, 0, sizeof (wi)); 1441 memset (&wi, 0, sizeof (wi));
1294 wi.info = id; 1442 wi.info = id;
1295 if (skip_first) 1443 if (skip_first)
1296 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL); 1444 walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
1297 else 1445 else
1298 walk_gimple_op (copy, remap_gimple_op_r, &wi); 1446 walk_gimple_op (copy, remap_gimple_op_r, &wi);
1299 1447
1300 /* We have to handle EH region remapping of GIMPLE_RESX specially because 1448 /* Clear the copied virtual operands. We are not remapping them here
1301 the region number is not an operand. */ 1449 but are going to recreate them from scratch. */
1302 if (gimple_code (stmt) == GIMPLE_RESX && id->eh_region_offset) 1450 if (gimple_has_mem_ops (copy))
1303 { 1451 {
1304 gimple_resx_set_region (copy, gimple_resx_region (stmt) + id->eh_region_offset); 1452 gimple_set_vdef (copy, NULL_TREE);
1305 } 1453 gimple_set_vuse (copy, NULL_TREE);
1454 }
1455
1306 return copy; 1456 return copy;
1307 } 1457 }
1308 1458
1309 1459
1310 /* Copy basic block, scale profile accordingly. Edges will be taken care of 1460 /* Copy basic block, scale profile accordingly. Edges will be taken care of
1315 gcov_type count_scale) 1465 gcov_type count_scale)
1316 { 1466 {
1317 gimple_stmt_iterator gsi, copy_gsi, seq_gsi; 1467 gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
1318 basic_block copy_basic_block; 1468 basic_block copy_basic_block;
1319 tree decl; 1469 tree decl;
1470 gcov_type freq;
1320 1471
1321 /* create_basic_block() will append every new block to 1472 /* create_basic_block() will append every new block to
1322 basic_block_info automatically. */ 1473 basic_block_info automatically. */
1323 copy_basic_block = create_basic_block (NULL, (void *) 0, 1474 copy_basic_block = create_basic_block (NULL, (void *) 0,
1324 (basic_block) bb->prev_bb->aux); 1475 (basic_block) bb->prev_bb->aux);
1325 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE; 1476 copy_basic_block->count = bb->count * count_scale / REG_BR_PROB_BASE;
1326 1477
1327 /* We are going to rebuild frequencies from scratch. These values 1478 /* We are going to rebuild frequencies from scratch. These values
1328 have just small importance to drive canonicalize_loop_headers. */ 1479 have just small importance to drive canonicalize_loop_headers. */
1329 copy_basic_block->frequency = ((gcov_type)bb->frequency 1480 freq = ((gcov_type)bb->frequency * frequency_scale / REG_BR_PROB_BASE);
1330 * frequency_scale / REG_BR_PROB_BASE); 1481
1331 1482 /* We recompute frequencies after inlining, so this is quite safe. */
1332 if (copy_basic_block->frequency > BB_FREQ_MAX) 1483 if (freq > BB_FREQ_MAX)
1333 copy_basic_block->frequency = BB_FREQ_MAX; 1484 freq = BB_FREQ_MAX;
1485 copy_basic_block->frequency = freq;
1334 1486
1335 copy_gsi = gsi_start_bb (copy_basic_block); 1487 copy_gsi = gsi_start_bb (copy_basic_block);
1336 1488
1337 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 1489 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1338 { 1490 {
1376 /* Process the new statement. The call to gimple_regimplify_operands 1528 /* Process the new statement. The call to gimple_regimplify_operands
1377 possibly turned the statement into multiple statements, we 1529 possibly turned the statement into multiple statements, we
1378 need to process all of them. */ 1530 need to process all of them. */
1379 do 1531 do
1380 { 1532 {
1533 tree fn;
1534
1381 stmt = gsi_stmt (copy_gsi); 1535 stmt = gsi_stmt (copy_gsi);
1382 if (is_gimple_call (stmt) 1536 if (is_gimple_call (stmt)
1383 && gimple_call_va_arg_pack_p (stmt) 1537 && gimple_call_va_arg_pack_p (stmt)
1384 && id->gimple_call) 1538 && id->gimple_call)
1385 { 1539 {
1464 1618
1465 /* We're duplicating a CALL_EXPR. Find any corresponding 1619 /* We're duplicating a CALL_EXPR. Find any corresponding
1466 callgraph edges and update or duplicate them. */ 1620 callgraph edges and update or duplicate them. */
1467 if (is_gimple_call (stmt)) 1621 if (is_gimple_call (stmt))
1468 { 1622 {
1469 struct cgraph_node *node;
1470 struct cgraph_edge *edge; 1623 struct cgraph_edge *edge;
1471 int flags; 1624 int flags;
1472 1625
1473 switch (id->transform_call_graph_edges) 1626 switch (id->transform_call_graph_edges)
1474 { 1627 {
1475 case CB_CGE_DUPLICATE: 1628 case CB_CGE_DUPLICATE:
1476 edge = cgraph_edge (id->src_node, orig_stmt); 1629 edge = cgraph_edge (id->src_node, orig_stmt);
1477 if (edge) 1630 if (edge)
1478 cgraph_clone_edge (edge, id->dst_node, stmt, 1631 {
1479 REG_BR_PROB_BASE, 1, 1632 int edge_freq = edge->frequency;
1480 edge->frequency, true); 1633 edge = cgraph_clone_edge (edge, id->dst_node, stmt,
1481 break; 1634 gimple_uid (stmt),
1482 1635 REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
1483 case CB_CGE_MOVE_CLONES: 1636 edge->frequency, true);
1484 for (node = id->dst_node->next_clone; 1637 /* We could also just rescale the frequency, but
1485 node; 1638 doing so would introduce roundoff errors and make
1486 node = node->next_clone) 1639 verifier unhappy. */
1487 { 1640 edge->frequency
1488 edge = cgraph_edge (node, orig_stmt); 1641 = compute_call_stmt_bb_frequency (id->dst_node->decl,
1489 if (edge) 1642 copy_basic_block);
1490 cgraph_set_call_stmt (edge, stmt); 1643 if (dump_file
1491 } 1644 && profile_status_for_function (cfun) != PROFILE_ABSENT
1492 /* FALLTHRU */ 1645 && (edge_freq > edge->frequency + 10
1493 1646 || edge_freq < edge->frequency - 10))
1494 case CB_CGE_MOVE: 1647 {
1495 edge = cgraph_edge (id->dst_node, orig_stmt); 1648 fprintf (dump_file, "Edge frequency estimated by "
1496 if (edge) 1649 "cgraph %i diverge from inliner's estimate %i\n",
1497 cgraph_set_call_stmt (edge, stmt); 1650 edge_freq,
1498 break; 1651 edge->frequency);
1499 1652 fprintf (dump_file,
1500 default: 1653 "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
1501 gcc_unreachable (); 1654 bb->index,
1655 bb->frequency,
1656 copy_basic_block->frequency);
1657 }
1658 }
1659 break;
1660
1661 case CB_CGE_MOVE_CLONES:
1662 cgraph_set_call_stmt_including_clones (id->dst_node,
1663 orig_stmt, stmt);
1664 edge = cgraph_edge (id->dst_node, stmt);
1665 break;
1666
1667 case CB_CGE_MOVE:
1668 edge = cgraph_edge (id->dst_node, orig_stmt);
1669 if (edge)
1670 cgraph_set_call_stmt (edge, stmt);
1671 break;
1672
1673 default:
1674 gcc_unreachable ();
1502 } 1675 }
1503 1676
1677 /* Constant propagation on argument done during inlining
1678 may create new direct call. Produce an edge for it. */
1679 if ((!edge
1680 || (edge->indirect_call
1681 && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
1682 && is_gimple_call (stmt)
1683 && (fn = gimple_call_fndecl (stmt)) != NULL)
1684 {
1685 struct cgraph_node *dest = cgraph_node (fn);
1686
1687 /* We have missing edge in the callgraph. This can happen
1688 when previous inlining turned an indirect call into a
1689 direct call by constant propagating arguments or we are
1690 producing dead clone (for further clonning). In all
1691 other cases we hit a bug (incorrect node sharing is the
1692 most common reason for missing edges). */
1693 gcc_assert (dest->needed || !dest->analyzed
1694 || !id->src_node->analyzed);
1695 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
1696 cgraph_create_edge_including_clones
1697 (id->dst_node, dest, orig_stmt, stmt, bb->count,
1698 compute_call_stmt_bb_frequency (id->dst_node->decl,
1699 copy_basic_block),
1700 bb->loop_depth, CIF_ORIGINALLY_INDIRECT_CALL);
1701 else
1702 cgraph_create_edge (id->dst_node, dest, stmt,
1703 bb->count,
1704 compute_call_stmt_bb_frequency
1705 (id->dst_node->decl, copy_basic_block),
1706 bb->loop_depth)->inline_failed
1707 = CIF_ORIGINALLY_INDIRECT_CALL;
1708 if (dump_file)
1709 {
1710 fprintf (dump_file, "Created new direct edge to %s",
1711 cgraph_node_name (dest));
1712 }
1713 }
1714
1504 flags = gimple_call_flags (stmt); 1715 flags = gimple_call_flags (stmt);
1505
1506 if (flags & ECF_MAY_BE_ALLOCA) 1716 if (flags & ECF_MAY_BE_ALLOCA)
1507 cfun->calls_alloca = true; 1717 cfun->calls_alloca = true;
1508 if (flags & ECF_RETURNS_TWICE) 1718 if (flags & ECF_RETURNS_TWICE)
1509 cfun->calls_setjmp = true; 1719 cfun->calls_setjmp = true;
1510 } 1720 }
1511 1721
1512 /* If you think we can abort here, you are wrong. 1722 maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
1513 There is no region 0 in gimple. */ 1723 id->eh_map, id->eh_lp_nr);
1514 gcc_assert (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) != 0); 1724
1515 1725 if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
1516 if (stmt_could_throw_p (stmt)
1517 /* When we are cloning for inlining, we are supposed to
1518 construct a clone that calls precisely the same functions
1519 as original. However IPA optimizers might've proved
1520 earlier some function calls as non-trapping that might
1521 render some basic blocks dead that might become
1522 unreachable.
1523
1524 We can't update SSA with unreachable blocks in CFG and thus
1525 we prevent the scenario by preserving even the "dead" eh
1526 edges until the point they are later removed by
1527 fixup_cfg pass. */
1528 || (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
1529 && lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) > 0))
1530 {
1531 int region = lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt);
1532
1533 /* Add an entry for the copied tree in the EH hashtable.
1534 When cloning or versioning, use the hashtable in
1535 cfun, and just copy the EH number. When inlining, use the
1536 hashtable in the caller, and adjust the region number. */
1537 if (region > 0)
1538 add_stmt_to_eh_region (stmt, region + id->eh_region_offset);
1539
1540 /* If this tree doesn't have a region associated with it,
1541 and there is a "current region,"
1542 then associate this tree with the current region
1543 and add edges associated with this region. */
1544 if (lookup_stmt_eh_region_fn (id->src_cfun, orig_stmt) <= 0
1545 && id->eh_region > 0
1546 && stmt_could_throw_p (stmt))
1547 add_stmt_to_eh_region (stmt, id->eh_region);
1548 }
1549
1550 if (gimple_in_ssa_p (cfun))
1551 { 1726 {
1552 ssa_op_iter i; 1727 ssa_op_iter i;
1553 tree def; 1728 tree def;
1554 1729
1555 find_new_referenced_vars (gsi_stmt (copy_gsi)); 1730 find_new_referenced_vars (gsi_stmt (copy_gsi));
1598 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK) 1773 || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
1599 { 1774 {
1600 gimple phi; 1775 gimple phi;
1601 gimple_stmt_iterator si; 1776 gimple_stmt_iterator si;
1602 1777
1603 gcc_assert (e->flags & EDGE_ABNORMAL);
1604
1605 if (!nonlocal_goto) 1778 if (!nonlocal_goto)
1606 gcc_assert (e->flags & EDGE_EH); 1779 gcc_assert (e->flags & EDGE_EH);
1607 1780
1608 if (!can_throw) 1781 if (!can_throw)
1609 gcc_assert (!(e->flags & EDGE_EH)); 1782 gcc_assert (!(e->flags & EDGE_EH));
1615 phi = gsi_stmt (si); 1788 phi = gsi_stmt (si);
1616 1789
1617 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */ 1790 /* There shouldn't be any PHI nodes in the ENTRY_BLOCK. */
1618 gcc_assert (!e->dest->aux); 1791 gcc_assert (!e->dest->aux);
1619 1792
1620 gcc_assert (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi))); 1793 gcc_assert ((e->flags & EDGE_EH)
1794 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
1621 1795
1622 if (!is_gimple_reg (PHI_RESULT (phi))) 1796 if (!is_gimple_reg (PHI_RESULT (phi)))
1623 { 1797 {
1624 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi))); 1798 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (phi)));
1625 continue; 1799 continue;
1675 { 1849 {
1676 gimple copy_stmt; 1850 gimple copy_stmt;
1677 bool can_throw, nonlocal_goto; 1851 bool can_throw, nonlocal_goto;
1678 1852
1679 copy_stmt = gsi_stmt (si); 1853 copy_stmt = gsi_stmt (si);
1680 update_stmt (copy_stmt); 1854 if (!is_gimple_debug (copy_stmt))
1681 if (gimple_in_ssa_p (cfun)) 1855 {
1682 mark_symbols_for_renaming (copy_stmt); 1856 update_stmt (copy_stmt);
1857 if (gimple_in_ssa_p (cfun))
1858 mark_symbols_for_renaming (copy_stmt);
1859 }
1683 1860
1684 /* Do this before the possible split_block. */ 1861 /* Do this before the possible split_block. */
1685 gsi_next (&si); 1862 gsi_next (&si);
1686 1863
1687 /* If this tree could throw an exception, there are two 1864 /* If this tree could throw an exception, there are two
1710 new_bb->aux = e->src->aux; 1887 new_bb->aux = e->src->aux;
1711 si = gsi_start_bb (new_bb); 1888 si = gsi_start_bb (new_bb);
1712 } 1889 }
1713 } 1890 }
1714 1891
1715 if (can_throw) 1892 if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
1893 make_eh_dispatch_edges (copy_stmt);
1894 else if (can_throw)
1716 make_eh_edges (copy_stmt); 1895 make_eh_edges (copy_stmt);
1717 1896
1718 if (nonlocal_goto) 1897 if (nonlocal_goto)
1719 make_abnormal_goto_edges (gimple_bb (copy_stmt), true); 1898 make_abnormal_goto_edges (gimple_bb (copy_stmt), true);
1720 1899
1771 { 1950 {
1772 gimple_seq stmts = NULL; 1951 gimple_seq stmts = NULL;
1773 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL); 1952 new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
1774 gsi_insert_seq_on_edge_immediate (new_edge, stmts); 1953 gsi_insert_seq_on_edge_immediate (new_edge, stmts);
1775 } 1954 }
1776 add_phi_arg (new_phi, new_arg, new_edge); 1955 add_phi_arg (new_phi, new_arg, new_edge,
1956 gimple_phi_arg_location_from_edge (phi, old_edge));
1777 } 1957 }
1778 } 1958 }
1779 } 1959 }
1780 } 1960 }
1781 1961
1790 1970
1791 /* Build struct function and associated datastructures for the new clone 1971 /* Build struct function and associated datastructures for the new clone
1792 NEW_FNDECL to be build. CALLEE_FNDECL is the original */ 1972 NEW_FNDECL to be build. CALLEE_FNDECL is the original */
1793 1973
1794 static void 1974 static void
1795 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count, 1975 initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
1796 int frequency)
1797 { 1976 {
1798 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl); 1977 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1799 gcov_type count_scale, frequency_scale; 1978 gcov_type count_scale;
1800 1979
1801 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count) 1980 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1802 count_scale = (REG_BR_PROB_BASE * count 1981 count_scale = (REG_BR_PROB_BASE * count
1803 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count); 1982 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1804 else 1983 else
1805 count_scale = 1; 1984 count_scale = REG_BR_PROB_BASE;
1806
1807 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1808 frequency_scale = (REG_BR_PROB_BASE * frequency
1809 /
1810 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1811 else
1812 frequency_scale = count_scale;
1813 1985
1814 /* Register specific tree functions. */ 1986 /* Register specific tree functions. */
1815 gimple_register_cfg_hooks (); 1987 gimple_register_cfg_hooks ();
1816 1988
1817 /* Get clean struct function. */ 1989 /* Get clean struct function. */
1827 cfun->static_chain_decl = src_cfun->static_chain_decl; 1999 cfun->static_chain_decl = src_cfun->static_chain_decl;
1828 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area; 2000 cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
1829 cfun->function_end_locus = src_cfun->function_end_locus; 2001 cfun->function_end_locus = src_cfun->function_end_locus;
1830 cfun->curr_properties = src_cfun->curr_properties; 2002 cfun->curr_properties = src_cfun->curr_properties;
1831 cfun->last_verified = src_cfun->last_verified; 2003 cfun->last_verified = src_cfun->last_verified;
1832 if (src_cfun->ipa_transforms_to_apply)
1833 cfun->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
1834 src_cfun->ipa_transforms_to_apply);
1835 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size; 2004 cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
1836 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size; 2005 cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
1837 cfun->function_frequency = src_cfun->function_frequency; 2006 cfun->function_frequency = src_cfun->function_frequency;
1838 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label; 2007 cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
1839 cfun->stdarg = src_cfun->stdarg; 2008 cfun->stdarg = src_cfun->stdarg;
1843 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct; 2012 cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
1844 cfun->after_tree_profile = src_cfun->after_tree_profile; 2013 cfun->after_tree_profile = src_cfun->after_tree_profile;
1845 2014
1846 init_empty_tree_cfg (); 2015 init_empty_tree_cfg ();
1847 2016
2017 profile_status_for_function (cfun) = profile_status_for_function (src_cfun);
1848 ENTRY_BLOCK_PTR->count = 2018 ENTRY_BLOCK_PTR->count =
1849 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale / 2019 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1850 REG_BR_PROB_BASE); 2020 REG_BR_PROB_BASE);
1851 ENTRY_BLOCK_PTR->frequency = 2021 ENTRY_BLOCK_PTR->frequency
1852 (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency * 2022 = ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
1853 frequency_scale / REG_BR_PROB_BASE);
1854 EXIT_BLOCK_PTR->count = 2023 EXIT_BLOCK_PTR->count =
1855 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale / 2024 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count * count_scale /
1856 REG_BR_PROB_BASE); 2025 REG_BR_PROB_BASE);
1857 EXIT_BLOCK_PTR->frequency = 2026 EXIT_BLOCK_PTR->frequency =
1858 (EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency * 2027 EXIT_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency;
1859 frequency_scale / REG_BR_PROB_BASE);
1860 if (src_cfun->eh) 2028 if (src_cfun->eh)
1861 init_eh_for_function (); 2029 init_eh_for_function ();
1862 2030
1863 if (src_cfun->gimple_df) 2031 if (src_cfun->gimple_df)
1864 { 2032 {
1871 2039
1872 /* Make a copy of the body of FN so that it can be inserted inline in 2040 /* Make a copy of the body of FN so that it can be inserted inline in
1873 another function. Walks FN via CFG, returns new fndecl. */ 2041 another function. Walks FN via CFG, returns new fndecl. */
1874 2042
1875 static tree 2043 static tree
1876 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency, 2044 copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
1877 basic_block entry_block_map, basic_block exit_block_map) 2045 basic_block entry_block_map, basic_block exit_block_map)
1878 { 2046 {
1879 tree callee_fndecl = id->src_fn; 2047 tree callee_fndecl = id->src_fn;
1880 /* Original cfun for the callee, doesn't change. */ 2048 /* Original cfun for the callee, doesn't change. */
1881 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl); 2049 struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
1882 struct function *cfun_to_copy; 2050 struct function *cfun_to_copy;
1883 basic_block bb; 2051 basic_block bb;
1884 tree new_fndecl = NULL; 2052 tree new_fndecl = NULL;
1885 gcov_type count_scale, frequency_scale; 2053 gcov_type count_scale;
1886 int last; 2054 int last;
1887 2055
1888 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count) 2056 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count)
1889 count_scale = (REG_BR_PROB_BASE * count 2057 count_scale = (REG_BR_PROB_BASE * count
1890 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count); 2058 / ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->count);
1891 else 2059 else
1892 count_scale = 1; 2060 count_scale = REG_BR_PROB_BASE;
1893
1894 if (ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency)
1895 frequency_scale = (REG_BR_PROB_BASE * frequency
1896 /
1897 ENTRY_BLOCK_PTR_FOR_FUNCTION (src_cfun)->frequency);
1898 else
1899 frequency_scale = count_scale;
1900 2061
1901 /* Register specific tree functions. */ 2062 /* Register specific tree functions. */
1902 gimple_register_cfg_hooks (); 2063 gimple_register_cfg_hooks ();
1903 2064
1904 /* Must have a CFG here at this point. */ 2065 /* Must have a CFG here at this point. */
1912 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy); 2073 entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1913 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy); 2074 exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FUNCTION (cfun_to_copy);
1914 2075
1915 /* Duplicate any exception-handling regions. */ 2076 /* Duplicate any exception-handling regions. */
1916 if (cfun->eh) 2077 if (cfun->eh)
1917 { 2078 id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
1918 id->eh_region_offset 2079 remap_decl_1, id);
1919 = duplicate_eh_regions (cfun_to_copy, remap_decl_1, id,
1920 0, id->eh_region);
1921 }
1922 2080
1923 /* Use aux pointers to map the original blocks to copy. */ 2081 /* Use aux pointers to map the original blocks to copy. */
1924 FOR_EACH_BB_FN (bb, cfun_to_copy) 2082 FOR_EACH_BB_FN (bb, cfun_to_copy)
1925 { 2083 {
1926 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale); 2084 basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
1949 for (; last < last_basic_block; last++) 2107 for (; last < last_basic_block; last++)
1950 BASIC_BLOCK (last)->aux = NULL; 2108 BASIC_BLOCK (last)->aux = NULL;
1951 entry_block_map->aux = NULL; 2109 entry_block_map->aux = NULL;
1952 exit_block_map->aux = NULL; 2110 exit_block_map->aux = NULL;
1953 2111
2112 if (id->eh_map)
2113 {
2114 pointer_map_destroy (id->eh_map);
2115 id->eh_map = NULL;
2116 }
2117
1954 return new_fndecl; 2118 return new_fndecl;
1955 } 2119 }
1956 2120
2121 /* Copy the debug STMT using ID. We deal with these statements in a
2122 special way: if any variable in their VALUE expression wasn't
2123 remapped yet, we won't remap it, because that would get decl uids
2124 out of sync, causing codegen differences between -g and -g0. If
2125 this arises, we drop the VALUE expression altogether. */
2126
2127 static void
2128 copy_debug_stmt (gimple stmt, copy_body_data *id)
2129 {
2130 tree t, *n;
2131 struct walk_stmt_info wi;
2132
2133 t = id->block;
2134 if (gimple_block (stmt))
2135 {
2136 tree *n;
2137 n = (tree *) pointer_map_contains (id->decl_map, gimple_block (stmt));
2138 if (n)
2139 t = *n;
2140 }
2141 gimple_set_block (stmt, t);
2142
2143 /* Remap all the operands in COPY. */
2144 memset (&wi, 0, sizeof (wi));
2145 wi.info = id;
2146
2147 processing_debug_stmt = 1;
2148
2149 t = gimple_debug_bind_get_var (stmt);
2150
2151 if (TREE_CODE (t) == PARM_DECL && id->debug_map
2152 && (n = (tree *) pointer_map_contains (id->debug_map, t)))
2153 {
2154 gcc_assert (TREE_CODE (*n) == VAR_DECL);
2155 t = *n;
2156 }
2157 else
2158 walk_tree (&t, remap_gimple_op_r, &wi, NULL);
2159
2160 gimple_debug_bind_set_var (stmt, t);
2161
2162 if (gimple_debug_bind_has_value_p (stmt))
2163 walk_tree (gimple_debug_bind_get_value_ptr (stmt),
2164 remap_gimple_op_r, &wi, NULL);
2165
2166 /* Punt if any decl couldn't be remapped. */
2167 if (processing_debug_stmt < 0)
2168 gimple_debug_bind_reset_value (stmt);
2169
2170 processing_debug_stmt = 0;
2171
2172 update_stmt (stmt);
2173 if (gimple_in_ssa_p (cfun))
2174 mark_symbols_for_renaming (stmt);
2175 }
2176
2177 /* Process deferred debug stmts. In order to give values better odds
2178 of being successfully remapped, we delay the processing of debug
2179 stmts until all other stmts that might require remapping are
2180 processed. */
2181
2182 static void
2183 copy_debug_stmts (copy_body_data *id)
2184 {
2185 size_t i;
2186 gimple stmt;
2187
2188 if (!id->debug_stmts)
2189 return;
2190
2191 for (i = 0; VEC_iterate (gimple, id->debug_stmts, i, stmt); i++)
2192 copy_debug_stmt (stmt, id);
2193
2194 VEC_free (gimple, heap, id->debug_stmts);
2195 }
2196
2197 /* Make a copy of the body of SRC_FN so that it can be inserted inline in
2198 another function. */
2199
1957 static tree 2200 static tree
1958 copy_body (copy_body_data *id, gcov_type count, int frequency, 2201 copy_tree_body (copy_body_data *id)
2202 {
2203 tree fndecl = id->src_fn;
2204 tree body = DECL_SAVED_TREE (fndecl);
2205
2206 walk_tree (&body, copy_tree_body_r, id, NULL);
2207
2208 return body;
2209 }
2210
2211 /* Make a copy of the body of FN so that it can be inserted inline in
2212 another function. */
2213
2214 static tree
2215 copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
1959 basic_block entry_block_map, basic_block exit_block_map) 2216 basic_block entry_block_map, basic_block exit_block_map)
1960 { 2217 {
1961 tree fndecl = id->src_fn; 2218 tree fndecl = id->src_fn;
1962 tree body; 2219 tree body;
1963 2220
1964 /* If this body has a CFG, walk CFG and copy. */ 2221 /* If this body has a CFG, walk CFG and copy. */
1965 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl))); 2222 gcc_assert (ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (fndecl)));
1966 body = copy_cfg_body (id, count, frequency, entry_block_map, exit_block_map); 2223 body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map);
2224 copy_debug_stmts (id);
1967 2225
1968 return body; 2226 return body;
1969 } 2227 }
1970 2228
1971 /* Return true if VALUE is an ADDR_EXPR of an automatic variable 2229 /* Return true if VALUE is an ADDR_EXPR of an automatic variable
1982 var = get_base_address (TREE_OPERAND (value, 0)); 2240 var = get_base_address (TREE_OPERAND (value, 0));
1983 2241
1984 return var && auto_var_in_fn_p (var, fn); 2242 return var && auto_var_in_fn_p (var, fn);
1985 } 2243 }
1986 2244
2245 /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
2246 lexical block and line number information from base_stmt, if given,
2247 or from the last stmt of the block otherwise. */
2248
2249 static gimple
2250 insert_init_debug_bind (copy_body_data *id,
2251 basic_block bb, tree var, tree value,
2252 gimple base_stmt)
2253 {
2254 gimple note;
2255 gimple_stmt_iterator gsi;
2256 tree tracked_var;
2257
2258 if (!gimple_in_ssa_p (id->src_cfun))
2259 return NULL;
2260
2261 if (!MAY_HAVE_DEBUG_STMTS)
2262 return NULL;
2263
2264 tracked_var = target_for_debug_bind (var);
2265 if (!tracked_var)
2266 return NULL;
2267
2268 if (bb)
2269 {
2270 gsi = gsi_last_bb (bb);
2271 if (!base_stmt && !gsi_end_p (gsi))
2272 base_stmt = gsi_stmt (gsi);
2273 }
2274
2275 note = gimple_build_debug_bind (tracked_var, value, base_stmt);
2276
2277 if (bb)
2278 {
2279 if (!gsi_end_p (gsi))
2280 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
2281 else
2282 gsi_insert_before (&gsi, note, GSI_SAME_STMT);
2283 }
2284
2285 return note;
2286 }
2287
1987 static void 2288 static void
1988 insert_init_stmt (basic_block bb, gimple init_stmt) 2289 insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
1989 { 2290 {
1990 /* If VAR represents a zero-sized variable, it's possible that the 2291 /* If VAR represents a zero-sized variable, it's possible that the
1991 assignment statement may result in no gimple statements. */ 2292 assignment statement may result in no gimple statements. */
1992 if (init_stmt) 2293 if (init_stmt)
1993 { 2294 {
1995 2296
1996 /* We can end up with init statements that store to a non-register 2297 /* We can end up with init statements that store to a non-register
1997 from a rhs with a conversion. Handle that here by forcing the 2298 from a rhs with a conversion. Handle that here by forcing the
1998 rhs into a temporary. gimple_regimplify_operands is not 2299 rhs into a temporary. gimple_regimplify_operands is not
1999 prepared to do this for us. */ 2300 prepared to do this for us. */
2000 if (!is_gimple_reg (gimple_assign_lhs (init_stmt)) 2301 if (!is_gimple_debug (init_stmt)
2302 && !is_gimple_reg (gimple_assign_lhs (init_stmt))
2001 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt))) 2303 && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
2002 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS) 2304 && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
2003 { 2305 {
2004 tree rhs = build1 (gimple_assign_rhs_code (init_stmt), 2306 tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
2005 gimple_expr_type (init_stmt), 2307 gimple_expr_type (init_stmt),
2010 gimple_assign_set_rhs1 (init_stmt, rhs); 2312 gimple_assign_set_rhs1 (init_stmt, rhs);
2011 } 2313 }
2012 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT); 2314 gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
2013 gimple_regimplify_operands (init_stmt, &si); 2315 gimple_regimplify_operands (init_stmt, &si);
2014 mark_symbols_for_renaming (init_stmt); 2316 mark_symbols_for_renaming (init_stmt);
2317
2318 if (!is_gimple_debug (init_stmt) && MAY_HAVE_DEBUG_STMTS)
2319 {
2320 tree var, def = gimple_assign_lhs (init_stmt);
2321
2322 if (TREE_CODE (def) == SSA_NAME)
2323 var = SSA_NAME_VAR (def);
2324 else
2325 var = def;
2326
2327 insert_init_debug_bind (id, bb, var, def, init_stmt);
2328 }
2015 } 2329 }
2016 } 2330 }
2017 2331
2018 /* Initialize parameter P with VALUE. If needed, produce init statement 2332 /* Initialize parameter P with VALUE. If needed, produce init statement
2019 at the end of BB. When BB is NULL, we return init statement to be 2333 at the end of BB. When BB is NULL, we return init statement to be
2040 mismatched types here, fall back to using a VIEW_CONVERT_EXPR 2354 mismatched types here, fall back to using a VIEW_CONVERT_EXPR
2041 to not leak invalid GIMPLE to the following passes. */ 2355 to not leak invalid GIMPLE to the following passes. */
2042 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value); 2356 rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
2043 } 2357 }
2044 2358
2359 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2360 here since the type of this decl must be visible to the calling
2361 function. */
2362 var = copy_decl_to_var (p, id);
2363
2364 /* We're actually using the newly-created var. */
2365 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2366 {
2367 get_var_ann (var);
2368 add_referenced_var (var);
2369 }
2370
2371 /* Declare this new variable. */
2372 TREE_CHAIN (var) = *vars;
2373 *vars = var;
2374
2375 /* Make gimplifier happy about this variable. */
2376 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2377
2045 /* If the parameter is never assigned to, has no SSA_NAMEs created, 2378 /* If the parameter is never assigned to, has no SSA_NAMEs created,
2046 we may not need to create a new variable here at all. Instead, we may 2379 we would not need to create a new variable here at all, if it
2047 be able to just use the argument value. */ 2380 weren't for debug info. Still, we can just use the argument
2381 value. */
2048 if (TREE_READONLY (p) 2382 if (TREE_READONLY (p)
2049 && !TREE_ADDRESSABLE (p) 2383 && !TREE_ADDRESSABLE (p)
2050 && value && !TREE_SIDE_EFFECTS (value) 2384 && value && !TREE_SIDE_EFFECTS (value)
2051 && !def) 2385 && !def)
2052 { 2386 {
2063 mutually-recursive or whatever, which is why we don't 2397 mutually-recursive or whatever, which is why we don't
2064 just test whether fn == current_function_decl. */ 2398 just test whether fn == current_function_decl. */
2065 && ! self_inlining_addr_expr (value, fn)) 2399 && ! self_inlining_addr_expr (value, fn))
2066 { 2400 {
2067 insert_decl_map (id, p, value); 2401 insert_decl_map (id, p, value);
2068 return NULL; 2402 insert_debug_decl_map (id, p, var);
2403 return insert_init_debug_bind (id, bb, var, value, NULL);
2069 } 2404 }
2070 }
2071
2072 /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
2073 here since the type of this decl must be visible to the calling
2074 function. */
2075 var = copy_decl_to_var (p, id);
2076 if (gimple_in_ssa_p (cfun) && TREE_CODE (var) == VAR_DECL)
2077 {
2078 get_var_ann (var);
2079 add_referenced_var (var);
2080 } 2405 }
2081 2406
2082 /* Register the VAR_DECL as the equivalent for the PARM_DECL; 2407 /* Register the VAR_DECL as the equivalent for the PARM_DECL;
2083 that way, when the PARM_DECL is encountered, it will be 2408 that way, when the PARM_DECL is encountered, it will be
2084 automatically replaced by the VAR_DECL. */ 2409 automatically replaced by the VAR_DECL. */
2085 insert_decl_map (id, p, var); 2410 insert_decl_map (id, p, var);
2086
2087 /* Declare this new variable. */
2088 TREE_CHAIN (var) = *vars;
2089 *vars = var;
2090
2091 /* Make gimplifier happy about this variable. */
2092 DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
2093 2411
2094 /* Even if P was TREE_READONLY, the new VAR should not be. 2412 /* Even if P was TREE_READONLY, the new VAR should not be.
2095 In the original code, we would have constructed a 2413 In the original code, we would have constructed a
2096 temporary, and then the function body would have never 2414 temporary, and then the function body would have never
2097 changed the value of P. However, now, we will be 2415 changed the value of P. However, now, we will be
2110 We need to construct map for the variable anyway as it might be used 2428 We need to construct map for the variable anyway as it might be used
2111 in different SSA names when parameter is set in function. 2429 in different SSA names when parameter is set in function.
2112 2430
2113 Do replacement at -O0 for const arguments replaced by constant. 2431 Do replacement at -O0 for const arguments replaced by constant.
2114 This is important for builtin_constant_p and other construct requiring 2432 This is important for builtin_constant_p and other construct requiring
2115 constant argument to be visible in inlined function body. 2433 constant argument to be visible in inlined function body. */
2116
2117 FIXME: This usually kills the last connection in between inlined
2118 function parameter and the actual value in debug info. Can we do
2119 better here? If we just inserted the statement, copy propagation
2120 would kill it anyway as it always did in older versions of GCC.
2121
2122 We might want to introduce a notion that single SSA_NAME might
2123 represent multiple variables for purposes of debugging. */
2124 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p) 2434 if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
2125 && (optimize 2435 && (optimize
2126 || (TREE_READONLY (p) 2436 || (TREE_READONLY (p)
2127 && is_gimple_min_invariant (rhs))) 2437 && is_gimple_min_invariant (rhs)))
2128 && (TREE_CODE (rhs) == SSA_NAME 2438 && (TREE_CODE (rhs) == SSA_NAME
2129 || is_gimple_min_invariant (rhs)) 2439 || is_gimple_min_invariant (rhs))
2130 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)) 2440 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
2131 { 2441 {
2132 insert_decl_map (id, def, rhs); 2442 insert_decl_map (id, def, rhs);
2133 return NULL; 2443 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2134 } 2444 }
2135 2445
2136 /* If the value of argument is never used, don't care about initializing 2446 /* If the value of argument is never used, don't care about initializing
2137 it. */ 2447 it. */
2138 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p)) 2448 if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
2139 { 2449 {
2140 gcc_assert (!value || !TREE_SIDE_EFFECTS (value)); 2450 gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
2141 return NULL; 2451 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2142 } 2452 }
2143 2453
2144 /* Initialize this VAR_DECL from the equivalent argument. Convert 2454 /* Initialize this VAR_DECL from the equivalent argument. Convert
2145 the argument to the proper type in case it was promoted. */ 2455 the argument to the proper type in case it was promoted. */
2146 if (value) 2456 if (value)
2147 { 2457 {
2148 if (rhs == error_mark_node) 2458 if (rhs == error_mark_node)
2149 { 2459 {
2150 insert_decl_map (id, p, var); 2460 insert_decl_map (id, p, var);
2151 return NULL; 2461 return insert_init_debug_bind (id, bb, var, rhs, NULL);
2152 } 2462 }
2153 2463
2154 STRIP_USELESS_TYPE_CONVERSION (rhs); 2464 STRIP_USELESS_TYPE_CONVERSION (rhs);
2155 2465
2156 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we 2466 /* We want to use MODIFY_EXPR, not INIT_EXPR here so that we
2164 } 2474 }
2165 else 2475 else
2166 init_stmt = gimple_build_assign (var, rhs); 2476 init_stmt = gimple_build_assign (var, rhs);
2167 2477
2168 if (bb && init_stmt) 2478 if (bb && init_stmt)
2169 insert_init_stmt (bb, init_stmt); 2479 insert_init_stmt (id, bb, init_stmt);
2170 } 2480 }
2171 return init_stmt; 2481 return init_stmt;
2172 } 2482 }
2173 2483
2174 /* Generate code to initialize the parameters of the function at the 2484 /* Generate code to initialize the parameters of the function at the
2218 2528
2219 RETURN_SLOT, if non-null is place where to store the result. It 2529 RETURN_SLOT, if non-null is place where to store the result. It
2220 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null, 2530 is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
2221 was the LHS of the MODIFY_EXPR to which this call is the RHS. 2531 was the LHS of the MODIFY_EXPR to which this call is the RHS.
2222 2532
2223 The return value is a (possibly null) value that is the result of the 2533 The return value is a (possibly null) value that holds the result
2224 function as seen by the callee. *USE_P is a (possibly null) value that 2534 as seen by the caller. */
2225 holds the result as seen by the caller. */
2226 2535
2227 static tree 2536 static tree
2228 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest, 2537 declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest)
2229 tree *use_p)
2230 { 2538 {
2231 tree callee = id->src_fn; 2539 tree callee = id->src_fn;
2232 tree caller = id->dst_fn; 2540 tree caller = id->dst_fn;
2233 tree result = DECL_RESULT (callee); 2541 tree result = DECL_RESULT (callee);
2234 tree callee_type = TREE_TYPE (result); 2542 tree callee_type = TREE_TYPE (result);
2236 tree var, use; 2544 tree var, use;
2237 2545
2238 /* We don't need to do anything for functions that don't return 2546 /* We don't need to do anything for functions that don't return
2239 anything. */ 2547 anything. */
2240 if (!result || VOID_TYPE_P (callee_type)) 2548 if (!result || VOID_TYPE_P (callee_type))
2241 { 2549 return NULL_TREE;
2242 *use_p = NULL_TREE;
2243 return NULL_TREE;
2244 }
2245 2550
2246 /* If there was a return slot, then the return value is the 2551 /* If there was a return slot, then the return value is the
2247 dereferenced address of that object. */ 2552 dereferenced address of that object. */
2248 if (return_slot) 2553 if (return_slot)
2249 { 2554 {
2254 { 2559 {
2255 tree return_slot_addr = build_fold_addr_expr (return_slot); 2560 tree return_slot_addr = build_fold_addr_expr (return_slot);
2256 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr); 2561 STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
2257 2562
2258 /* We are going to construct *&return_slot and we can't do that 2563 /* We are going to construct *&return_slot and we can't do that
2259 for variables believed to be not addressable. 2564 for variables believed to be not addressable.
2260 2565
2261 FIXME: This check possibly can match, because values returned 2566 FIXME: This check possibly can match, because values returned
2262 via return slot optimization are not believed to have address 2567 via return slot optimization are not believed to have address
2263 taken by alias analysis. */ 2568 taken by alias analysis. */
2264 gcc_assert (TREE_CODE (return_slot) != SSA_NAME); 2569 gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
2372 /* Build the use expr. If the return type of the function was 2677 /* Build the use expr. If the return type of the function was
2373 promoted, convert it back to the expected type. */ 2678 promoted, convert it back to the expected type. */
2374 use = var; 2679 use = var;
2375 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var))) 2680 if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
2376 use = fold_convert (caller_type, var); 2681 use = fold_convert (caller_type, var);
2377 2682
2378 STRIP_USELESS_TYPE_CONVERSION (use); 2683 STRIP_USELESS_TYPE_CONVERSION (use);
2379 2684
2380 if (DECL_BY_REFERENCE (result)) 2685 if (DECL_BY_REFERENCE (result))
2381 var = build_fold_addr_expr (var); 2686 {
2687 TREE_ADDRESSABLE (var) = 1;
2688 var = build_fold_addr_expr (var);
2689 }
2382 2690
2383 done: 2691 done:
2384 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that 2692 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
2385 way, when the RESULT_DECL is encountered, it will be 2693 way, when the RESULT_DECL is encountered, it will be
2386 automatically replaced by the VAR_DECL. */ 2694 automatically replaced by the VAR_DECL. */
2387 insert_decl_map (id, result, var); 2695 insert_decl_map (id, result, var);
2388 2696
2389 /* Remember this so we can ignore it in remap_decls. */ 2697 /* Remember this so we can ignore it in remap_decls. */
2390 id->retvar = var; 2698 id->retvar = var;
2391 2699
2392 *use_p = use; 2700 return use;
2393 return var; 2701 }
2394 } 2702
2395 2703 /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
2396 /* Returns nonzero if a function can be inlined as a tree. */ 2704 to a local label. */
2397
2398 bool
2399 tree_inlinable_function_p (tree fn)
2400 {
2401 return inlinable_function_p (fn);
2402 }
2403
2404 static const char *inline_forbidden_reason;
2405
2406 /* A callback for walk_gimple_seq to handle tree operands. Returns
2407 NULL_TREE if a function can be inlined, otherwise sets the reason
2408 why not and returns a tree representing the offending operand. */
2409 2705
2410 static tree 2706 static tree
2411 inline_forbidden_p_op (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED, 2707 has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
2412 void *fnp ATTRIBUTE_UNUSED)
2413 { 2708 {
2414 tree node = *nodep; 2709 tree node = *nodep;
2415 tree t; 2710 tree fn = (tree) fnp;
2711
2712 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2713 return node;
2714
2715 if (TYPE_P (node))
2716 *walk_subtrees = 0;
2717
2718 return NULL_TREE;
2719 }
2720
2721 /* Callback through walk_tree. Determine if we've got an aggregate
2722 type that we can't support; return non-null if so. */
2723
2724 static tree
2725 cannot_copy_type_1 (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
2726 void *data ATTRIBUTE_UNUSED)
2727 {
2728 tree t, node = *nodep;
2416 2729
2417 if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE) 2730 if (TREE_CODE (node) == RECORD_TYPE || TREE_CODE (node) == UNION_TYPE)
2418 { 2731 {
2419 /* We cannot inline a function of the form 2732 /* We cannot inline a function of the form
2420 2733
2423 Attempting to do so produces a catch-22. 2736 Attempting to do so produces a catch-22.
2424 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/ 2737 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
2425 UNION_TYPE nodes, then it goes into infinite recursion on a 2738 UNION_TYPE nodes, then it goes into infinite recursion on a
2426 structure containing a pointer to its own type. If it doesn't, 2739 structure containing a pointer to its own type. If it doesn't,
2427 then the type node for S doesn't get adjusted properly when 2740 then the type node for S doesn't get adjusted properly when
2428 F is inlined. 2741 F is inlined.
2429 2742
2430 ??? This is likely no longer true, but it's too late in the 4.0 2743 ??? This is likely no longer true, but it's too late in the 4.0
2431 cycle to try to find out. This should be checked for 4.1. */ 2744 cycle to try to find out. This should be checked for 4.1. */
2432 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t)) 2745 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
2433 if (variably_modified_type_p (TREE_TYPE (t), NULL)) 2746 if (variably_modified_type_p (TREE_TYPE (t), NULL))
2434 { 2747 return node;
2435 inline_forbidden_reason
2436 = G_("function %q+F can never be inlined "
2437 "because it uses variable sized variables");
2438 return node;
2439 }
2440 } 2748 }
2441 2749
2442 return NULL_TREE; 2750 return NULL_TREE;
2443 } 2751 }
2444 2752
2445 2753
2446 /* A callback for walk_gimple_seq to handle statements. Returns 2754 /* Determine if the function can be copied. If so return NULL. If
2447 non-NULL iff a function can not be inlined. Also sets the reason 2755 not return a string describng the reason for failure. */
2448 why. */ 2756
2757 static const char *
2758 copy_forbidden (struct function *fun, tree fndecl)
2759 {
2760 const char *reason = fun->cannot_be_copied_reason;
2761 tree step;
2762
2763 /* Only examine the function once. */
2764 if (fun->cannot_be_copied_set)
2765 return reason;
2766
2767 /* We cannot copy a function that receives a non-local goto
2768 because we cannot remap the destination label used in the
2769 function that is performing the non-local goto. */
2770 /* ??? Actually, this should be possible, if we work at it.
2771 No doubt there's just a handful of places that simply
2772 assume it doesn't happen and don't substitute properly. */
2773 if (fun->has_nonlocal_label)
2774 {
2775 reason = G_("function %q+F can never be copied "
2776 "because it receives a non-local goto");
2777 goto fail;
2778 }
2779
2780 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2781 {
2782 tree decl = TREE_VALUE (step);
2783
2784 if (TREE_CODE (decl) == VAR_DECL
2785 && TREE_STATIC (decl)
2786 && !DECL_EXTERNAL (decl)
2787 && DECL_INITIAL (decl)
2788 && walk_tree_without_duplicates (&DECL_INITIAL (decl),
2789 has_label_address_in_static_1,
2790 fndecl))
2791 {
2792 reason = G_("function %q+F can never be copied because it saves "
2793 "address of local label in a static variable");
2794 goto fail;
2795 }
2796
2797 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)
2798 && variably_modified_type_p (TREE_TYPE (decl), NULL)
2799 && walk_tree_without_duplicates (&TREE_TYPE (decl),
2800 cannot_copy_type_1, NULL))
2801 {
2802 reason = G_("function %q+F can never be copied "
2803 "because it uses variable sized variables");
2804 goto fail;
2805 }
2806 }
2807
2808 fail:
2809 fun->cannot_be_copied_reason = reason;
2810 fun->cannot_be_copied_set = true;
2811 return reason;
2812 }
2813
2814
2815 static const char *inline_forbidden_reason;
2816
2817 /* A callback for walk_gimple_seq to handle statements. Returns non-null
2818 iff a function can not be inlined. Also sets the reason why. */
2449 2819
2450 static tree 2820 static tree
2451 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p, 2821 inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
2452 struct walk_stmt_info *wip) 2822 struct walk_stmt_info *wip)
2453 { 2823 {
2552 *handled_ops_p = true; 2922 *handled_ops_p = true;
2553 return t; 2923 return t;
2554 } 2924 }
2555 break; 2925 break;
2556 2926
2557 case GIMPLE_LABEL:
2558 t = gimple_label_label (stmt);
2559 if (DECL_NONLOCAL (t))
2560 {
2561 /* We cannot inline a function that receives a non-local goto
2562 because we cannot remap the destination label used in the
2563 function that is performing the non-local goto. */
2564 inline_forbidden_reason
2565 = G_("function %q+F can never be inlined "
2566 "because it receives a non-local goto");
2567 *handled_ops_p = true;
2568 return t;
2569 }
2570 break;
2571
2572 default: 2927 default:
2573 break; 2928 break;
2574 } 2929 }
2575 2930
2576 *handled_ops_p = false; 2931 *handled_ops_p = false;
2577 return NULL_TREE; 2932 return NULL_TREE;
2578 } 2933 }
2579 2934
2580
2581 static tree
2582 inline_forbidden_p_2 (tree *nodep, int *walk_subtrees,
2583 void *fnp)
2584 {
2585 tree node = *nodep;
2586 tree fn = (tree) fnp;
2587
2588 if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
2589 {
2590 inline_forbidden_reason
2591 = G_("function %q+F can never be inlined "
2592 "because it saves address of local label in a static variable");
2593 return node;
2594 }
2595
2596 if (TYPE_P (node))
2597 *walk_subtrees = 0;
2598
2599 return NULL_TREE;
2600 }
2601
2602 /* Return true if FNDECL is a function that cannot be inlined into 2935 /* Return true if FNDECL is a function that cannot be inlined into
2603 another one. */ 2936 another one. */
2604 2937
2605 static bool 2938 static bool
2606 inline_forbidden_p (tree fndecl) 2939 inline_forbidden_p (tree fndecl)
2607 { 2940 {
2608 location_t saved_loc = input_location;
2609 struct function *fun = DECL_STRUCT_FUNCTION (fndecl); 2941 struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
2610 tree step;
2611 struct walk_stmt_info wi; 2942 struct walk_stmt_info wi;
2612 struct pointer_set_t *visited_nodes; 2943 struct pointer_set_t *visited_nodes;
2613 basic_block bb; 2944 basic_block bb;
2614 bool forbidden_p = false; 2945 bool forbidden_p = false;
2615 2946
2947 /* First check for shared reasons not to copy the code. */
2948 inline_forbidden_reason = copy_forbidden (fun, fndecl);
2949 if (inline_forbidden_reason != NULL)
2950 return true;
2951
2952 /* Next, walk the statements of the function looking for
2953 constraucts we can't handle, or are non-optimal for inlining. */
2616 visited_nodes = pointer_set_create (); 2954 visited_nodes = pointer_set_create ();
2617 memset (&wi, 0, sizeof (wi)); 2955 memset (&wi, 0, sizeof (wi));
2618 wi.info = (void *) fndecl; 2956 wi.info = (void *) fndecl;
2619 wi.pset = visited_nodes; 2957 wi.pset = visited_nodes;
2620 2958
2621 FOR_EACH_BB_FN (bb, fun) 2959 FOR_EACH_BB_FN (bb, fun)
2622 { 2960 {
2623 gimple ret; 2961 gimple ret;
2624 gimple_seq seq = bb_seq (bb); 2962 gimple_seq seq = bb_seq (bb);
2625 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, 2963 ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
2626 inline_forbidden_p_op, &wi);
2627 forbidden_p = (ret != NULL); 2964 forbidden_p = (ret != NULL);
2628 if (forbidden_p) 2965 if (forbidden_p)
2629 goto egress; 2966 break;
2630 } 2967 }
2631 2968
2632 for (step = fun->local_decls; step; step = TREE_CHAIN (step))
2633 {
2634 tree decl = TREE_VALUE (step);
2635 if (TREE_CODE (decl) == VAR_DECL
2636 && TREE_STATIC (decl)
2637 && !DECL_EXTERNAL (decl)
2638 && DECL_INITIAL (decl))
2639 {
2640 tree ret;
2641 ret = walk_tree_without_duplicates (&DECL_INITIAL (decl),
2642 inline_forbidden_p_2, fndecl);
2643 forbidden_p = (ret != NULL);
2644 if (forbidden_p)
2645 goto egress;
2646 }
2647 }
2648
2649 egress:
2650 pointer_set_destroy (visited_nodes); 2969 pointer_set_destroy (visited_nodes);
2651 input_location = saved_loc;
2652 return forbidden_p; 2970 return forbidden_p;
2653 } 2971 }
2654 2972
2655 /* Returns nonzero if FN is a function that does not have any 2973 /* Returns nonzero if FN is a function that does not have any
2656 fundamental inline blocking properties. */ 2974 fundamental inline blocking properties. */
2657 2975
2658 static bool 2976 bool
2659 inlinable_function_p (tree fn) 2977 tree_inlinable_function_p (tree fn)
2660 { 2978 {
2661 bool inlinable = true; 2979 bool inlinable = true;
2662 bool do_warning; 2980 bool do_warning;
2663 tree always_inline; 2981 tree always_inline;
2664 2982
2727 int 3045 int
2728 estimate_move_cost (tree type) 3046 estimate_move_cost (tree type)
2729 { 3047 {
2730 HOST_WIDE_INT size; 3048 HOST_WIDE_INT size;
2731 3049
3050 gcc_assert (!VOID_TYPE_P (type));
3051
2732 size = int_size_in_bytes (type); 3052 size = int_size_in_bytes (type);
2733 3053
2734 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size)) 3054 if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (!optimize_size))
2735 /* Cost of a memcpy call, 3 arguments and the call. */ 3055 /* Cost of a memcpy call, 3 arguments and the call. */
2736 return 4; 3056 return 4;
2739 } 3059 }
2740 3060
2741 /* Returns cost of operation CODE, according to WEIGHTS */ 3061 /* Returns cost of operation CODE, according to WEIGHTS */
2742 3062
2743 static int 3063 static int
2744 estimate_operator_cost (enum tree_code code, eni_weights *weights) 3064 estimate_operator_cost (enum tree_code code, eni_weights *weights,
3065 tree op1 ATTRIBUTE_UNUSED, tree op2)
2745 { 3066 {
2746 switch (code) 3067 switch (code)
2747 { 3068 {
2748 /* These are "free" conversions, or their presumed cost 3069 /* These are "free" conversions, or their presumed cost
2749 is folded into other operations. */ 3070 is folded into other operations. */
2761 case PLUS_EXPR: 3082 case PLUS_EXPR:
2762 case POINTER_PLUS_EXPR: 3083 case POINTER_PLUS_EXPR:
2763 case MINUS_EXPR: 3084 case MINUS_EXPR:
2764 case MULT_EXPR: 3085 case MULT_EXPR:
2765 3086
3087 case ADDR_SPACE_CONVERT_EXPR:
2766 case FIXED_CONVERT_EXPR: 3088 case FIXED_CONVERT_EXPR:
2767 case FIX_TRUNC_EXPR: 3089 case FIX_TRUNC_EXPR:
2768 3090
2769 case NEGATE_EXPR: 3091 case NEGATE_EXPR:
2770 case FLOAT_EXPR: 3092 case FLOAT_EXPR:
2849 case TRUNC_MOD_EXPR: 3171 case TRUNC_MOD_EXPR:
2850 case CEIL_MOD_EXPR: 3172 case CEIL_MOD_EXPR:
2851 case FLOOR_MOD_EXPR: 3173 case FLOOR_MOD_EXPR:
2852 case ROUND_MOD_EXPR: 3174 case ROUND_MOD_EXPR:
2853 case RDIV_EXPR: 3175 case RDIV_EXPR:
2854 return weights->div_mod_cost; 3176 if (TREE_CODE (op2) != INTEGER_CST)
3177 return weights->div_mod_cost;
3178 return 1;
2855 3179
2856 default: 3180 default:
2857 /* We expect a copy assignment with no operator. */ 3181 /* We expect a copy assignment with no operator. */
2858 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS); 3182 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
2859 return 0; 3183 return 0;
2886 estimate_num_insns (gimple stmt, eni_weights *weights) 3210 estimate_num_insns (gimple stmt, eni_weights *weights)
2887 { 3211 {
2888 unsigned cost, i; 3212 unsigned cost, i;
2889 enum gimple_code code = gimple_code (stmt); 3213 enum gimple_code code = gimple_code (stmt);
2890 tree lhs; 3214 tree lhs;
3215 tree rhs;
2891 3216
2892 switch (code) 3217 switch (code)
2893 { 3218 {
2894 case GIMPLE_ASSIGN: 3219 case GIMPLE_ASSIGN:
2895 /* Try to estimate the cost of assignments. We have three cases to 3220 /* Try to estimate the cost of assignments. We have three cases to
2909 If "a" is not a GIMPLE register, the assignment to "a" will most 3234 If "a" is not a GIMPLE register, the assignment to "a" will most
2910 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost 3235 likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
2911 of moving something into "a", which we compute using the function 3236 of moving something into "a", which we compute using the function
2912 estimate_move_cost. */ 3237 estimate_move_cost. */
2913 lhs = gimple_assign_lhs (stmt); 3238 lhs = gimple_assign_lhs (stmt);
3239 rhs = gimple_assign_rhs1 (stmt);
3240
2914 if (is_gimple_reg (lhs)) 3241 if (is_gimple_reg (lhs))
2915 cost = 0; 3242 cost = 0;
2916 else 3243 else
2917 cost = estimate_move_cost (TREE_TYPE (lhs)); 3244 cost = estimate_move_cost (TREE_TYPE (lhs));
2918 3245
2919 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights); 3246 if (!is_gimple_reg (rhs) && !is_gimple_min_invariant (rhs))
3247 cost += estimate_move_cost (TREE_TYPE (rhs));
3248
3249 cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
3250 gimple_assign_rhs1 (stmt),
3251 get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
3252 == GIMPLE_BINARY_RHS
3253 ? gimple_assign_rhs2 (stmt) : NULL);
2920 break; 3254 break;
2921 3255
2922 case GIMPLE_COND: 3256 case GIMPLE_COND:
2923 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights); 3257 cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
3258 gimple_op (stmt, 0),
3259 gimple_op (stmt, 1));
2924 break; 3260 break;
2925 3261
2926 case GIMPLE_SWITCH: 3262 case GIMPLE_SWITCH:
2927 /* Take into account cost of the switch + guess 2 conditional jumps for 3263 /* Take into account cost of the switch + guess 2 conditional jumps for
2928 each case label. 3264 each case label.
2929 3265
2930 TODO: once the switch expansion logic is sufficiently separated, we can 3266 TODO: once the switch expansion logic is sufficiently separated, we can
2931 do better job on estimating cost of the switch. */ 3267 do better job on estimating cost of the switch. */
2932 cost = gimple_switch_num_labels (stmt) * 2; 3268 if (weights->time_based)
3269 cost = floor_log2 (gimple_switch_num_labels (stmt)) * 2;
3270 else
3271 cost = gimple_switch_num_labels (stmt) * 2;
2933 break; 3272 break;
2934 3273
2935 case GIMPLE_CALL: 3274 case GIMPLE_CALL:
2936 { 3275 {
2937 tree decl = gimple_call_fndecl (stmt); 3276 tree decl = gimple_call_fndecl (stmt);
2943 3282
2944 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD) 3283 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
2945 cost = weights->target_builtin_call_cost; 3284 cost = weights->target_builtin_call_cost;
2946 else 3285 else
2947 cost = weights->call_cost; 3286 cost = weights->call_cost;
2948 3287
2949 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 3288 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2950 switch (DECL_FUNCTION_CODE (decl)) 3289 switch (DECL_FUNCTION_CODE (decl))
2951 { 3290 {
2952 case BUILT_IN_CONSTANT_P: 3291 case BUILT_IN_CONSTANT_P:
2953 return 0; 3292 return 0;
2954 case BUILT_IN_EXPECT: 3293 case BUILT_IN_EXPECT:
2955 cost = 0; 3294 return 0;
2956 break;
2957 3295
2958 /* Prefetch instruction is not expensive. */ 3296 /* Prefetch instruction is not expensive. */
2959 case BUILT_IN_PREFETCH: 3297 case BUILT_IN_PREFETCH:
2960 cost = weights->target_builtin_call_cost; 3298 cost = weights->target_builtin_call_cost;
2961 break; 3299 break;
2965 } 3303 }
2966 3304
2967 if (decl) 3305 if (decl)
2968 funtype = TREE_TYPE (decl); 3306 funtype = TREE_TYPE (decl);
2969 3307
3308 if (!VOID_TYPE_P (TREE_TYPE (funtype)))
3309 cost += estimate_move_cost (TREE_TYPE (funtype));
2970 /* Our cost must be kept in sync with 3310 /* Our cost must be kept in sync with
2971 cgraph_estimate_size_after_inlining that does use function 3311 cgraph_estimate_size_after_inlining that does use function
2972 declaration to figure out the arguments. */ 3312 declaration to figure out the arguments. */
2973 if (decl && DECL_ARGUMENTS (decl)) 3313 if (decl && DECL_ARGUMENTS (decl))
2974 { 3314 {
2975 tree arg; 3315 tree arg;
2976 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg)) 3316 for (arg = DECL_ARGUMENTS (decl); arg; arg = TREE_CHAIN (arg))
2977 cost += estimate_move_cost (TREE_TYPE (arg)); 3317 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3318 cost += estimate_move_cost (TREE_TYPE (arg));
2978 } 3319 }
2979 else if (funtype && prototype_p (funtype)) 3320 else if (funtype && prototype_p (funtype))
2980 { 3321 {
2981 tree t; 3322 tree t;
2982 for (t = TYPE_ARG_TYPES (funtype); t; t = TREE_CHAIN (t)) 3323 for (t = TYPE_ARG_TYPES (funtype); t && t != void_list_node;
2983 cost += estimate_move_cost (TREE_VALUE (t)); 3324 t = TREE_CHAIN (t))
3325 if (!VOID_TYPE_P (TREE_VALUE (t)))
3326 cost += estimate_move_cost (TREE_VALUE (t));
2984 } 3327 }
2985 else 3328 else
2986 { 3329 {
2987 for (i = 0; i < gimple_call_num_args (stmt); i++) 3330 for (i = 0; i < gimple_call_num_args (stmt); i++)
2988 { 3331 {
2989 tree arg = gimple_call_arg (stmt, i); 3332 tree arg = gimple_call_arg (stmt, i);
2990 cost += estimate_move_cost (TREE_TYPE (arg)); 3333 if (!VOID_TYPE_P (TREE_TYPE (arg)))
3334 cost += estimate_move_cost (TREE_TYPE (arg));
2991 } 3335 }
2992 } 3336 }
2993 3337
2994 break; 3338 break;
2995 } 3339 }
2997 case GIMPLE_GOTO: 3341 case GIMPLE_GOTO:
2998 case GIMPLE_LABEL: 3342 case GIMPLE_LABEL:
2999 case GIMPLE_NOP: 3343 case GIMPLE_NOP:
3000 case GIMPLE_PHI: 3344 case GIMPLE_PHI:
3001 case GIMPLE_RETURN: 3345 case GIMPLE_RETURN:
3002 case GIMPLE_CHANGE_DYNAMIC_TYPE:
3003 case GIMPLE_PREDICT: 3346 case GIMPLE_PREDICT:
3347 case GIMPLE_DEBUG:
3004 return 0; 3348 return 0;
3005 3349
3006 case GIMPLE_ASM: 3350 case GIMPLE_ASM:
3351 return asm_str_count (gimple_asm_string (stmt));
3352
3007 case GIMPLE_RESX: 3353 case GIMPLE_RESX:
3008 return 1; 3354 /* This is either going to be an external function call with one
3355 argument, or two register copy statements plus a goto. */
3356 return 2;
3357
3358 case GIMPLE_EH_DISPATCH:
3359 /* ??? This is going to turn into a switch statement. Ideally
3360 we'd have a look at the eh region and estimate the number of
3361 edges involved. */
3362 return 10;
3009 3363
3010 case GIMPLE_BIND: 3364 case GIMPLE_BIND:
3011 return estimate_num_insns_seq (gimple_bind_body (stmt), weights); 3365 return estimate_num_insns_seq (gimple_bind_body (stmt), weights);
3012 3366
3013 case GIMPLE_EH_FILTER: 3367 case GIMPLE_EH_FILTER:
3081 /* Initializes weights used by estimate_num_insns. */ 3435 /* Initializes weights used by estimate_num_insns. */
3082 3436
3083 void 3437 void
3084 init_inline_once (void) 3438 init_inline_once (void)
3085 { 3439 {
3086 eni_inlining_weights.call_cost = PARAM_VALUE (PARAM_INLINE_CALL_COST);
3087 eni_inlining_weights.target_builtin_call_cost = 1;
3088 eni_inlining_weights.div_mod_cost = 10;
3089 eni_inlining_weights.omp_cost = 40;
3090
3091 eni_size_weights.call_cost = 1; 3440 eni_size_weights.call_cost = 1;
3092 eni_size_weights.target_builtin_call_cost = 1; 3441 eni_size_weights.target_builtin_call_cost = 1;
3093 eni_size_weights.div_mod_cost = 1; 3442 eni_size_weights.div_mod_cost = 1;
3094 eni_size_weights.omp_cost = 40; 3443 eni_size_weights.omp_cost = 40;
3444 eni_size_weights.time_based = false;
3095 3445
3096 /* Estimating time for call is difficult, since we have no idea what the 3446 /* Estimating time for call is difficult, since we have no idea what the
3097 called function does. In the current uses of eni_time_weights, 3447 called function does. In the current uses of eni_time_weights,
3098 underestimating the cost does less harm than overestimating it, so 3448 underestimating the cost does less harm than overestimating it, so
3099 we choose a rather small value here. */ 3449 we choose a rather small value here. */
3100 eni_time_weights.call_cost = 10; 3450 eni_time_weights.call_cost = 10;
3101 eni_time_weights.target_builtin_call_cost = 10; 3451 eni_time_weights.target_builtin_call_cost = 10;
3102 eni_time_weights.div_mod_cost = 10; 3452 eni_time_weights.div_mod_cost = 10;
3103 eni_time_weights.omp_cost = 40; 3453 eni_time_weights.omp_cost = 40;
3454 eni_time_weights.time_based = true;
3104 } 3455 }
3105 3456
3106 /* Estimate the number of instructions in a gimple_seq. */ 3457 /* Estimate the number of instructions in a gimple_seq. */
3107 3458
3108 int 3459 int
3144 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */ 3495 /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
3145 3496
3146 static bool 3497 static bool
3147 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id) 3498 expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
3148 { 3499 {
3149 tree retvar, use_retvar; 3500 tree use_retvar;
3150 tree fn; 3501 tree fn;
3151 struct pointer_map_t *st; 3502 struct pointer_map_t *st, *dst;
3152 tree return_slot; 3503 tree return_slot;
3153 tree modify_dest; 3504 tree modify_dest;
3154 location_t saved_location; 3505 location_t saved_location;
3155 struct cgraph_edge *cg_edge; 3506 struct cgraph_edge *cg_edge;
3156 const char *reason; 3507 cgraph_inline_failed_t reason;
3157 basic_block return_block; 3508 basic_block return_block;
3158 edge e; 3509 edge e;
3159 gimple_stmt_iterator gsi, stmt_gsi; 3510 gimple_stmt_iterator gsi, stmt_gsi;
3160 bool successfully_inlined = FALSE; 3511 bool successfully_inlined = FALSE;
3161 bool purge_dead_abnormal_edges; 3512 bool purge_dead_abnormal_edges;
3202 if (!id->dst_node->analyzed) 3553 if (!id->dst_node->analyzed)
3203 goto egress; 3554 goto egress;
3204 3555
3205 cg_edge = cgraph_edge (id->dst_node, stmt); 3556 cg_edge = cgraph_edge (id->dst_node, stmt);
3206 3557
3207 /* Constant propagation on argument done during previous inlining 3558 /* Don't inline functions with different EH personalities. */
3208 may create new direct call. Produce an edge for it. */ 3559 if (DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3209 if (!cg_edge) 3560 && DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl)
3210 { 3561 && (DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3211 struct cgraph_node *dest = cgraph_node (fn); 3562 != DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl)))
3212 3563 goto egress;
3213 /* We have missing edge in the callgraph. This can happen in one case
3214 where previous inlining turned indirect call into direct call by
3215 constant propagating arguments. In all other cases we hit a bug
3216 (incorrect node sharing is most common reason for missing edges. */
3217 gcc_assert (dest->needed);
3218 cgraph_create_edge (id->dst_node, dest, stmt,
3219 bb->count, CGRAPH_FREQ_BASE,
3220 bb->loop_depth)->inline_failed
3221 = N_("originally indirect function call not considered for inlining");
3222 if (dump_file)
3223 {
3224 fprintf (dump_file, "Created new direct edge to %s",
3225 cgraph_node_name (dest));
3226 }
3227 goto egress;
3228 }
3229 3564
3230 /* Don't try to inline functions that are not well-suited to 3565 /* Don't try to inline functions that are not well-suited to
3231 inlining. */ 3566 inlining. */
3232 if (!cgraph_inline_p (cg_edge, &reason)) 3567 if (!cgraph_inline_p (cg_edge, &reason))
3233 { 3568 {
3239 3574
3240 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) 3575 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
3241 /* Avoid warnings during early inline pass. */ 3576 /* Avoid warnings during early inline pass. */
3242 && cgraph_global_info_ready) 3577 && cgraph_global_info_ready)
3243 { 3578 {
3244 sorry ("inlining failed in call to %q+F: %s", fn, reason); 3579 sorry ("inlining failed in call to %q+F: %s", fn,
3580 cgraph_inline_failed_string (reason));
3245 sorry ("called from here"); 3581 sorry ("called from here");
3246 } 3582 }
3247 else if (warn_inline && DECL_DECLARED_INLINE_P (fn) 3583 else if (warn_inline && DECL_DECLARED_INLINE_P (fn)
3248 && !DECL_IN_SYSTEM_HEADER (fn) 3584 && !DECL_IN_SYSTEM_HEADER (fn)
3249 && strlen (reason) 3585 && reason != CIF_UNSPECIFIED
3250 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn)) 3586 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
3251 /* Avoid warnings during early inline pass. */ 3587 /* Avoid warnings during early inline pass. */
3252 && cgraph_global_info_ready) 3588 && cgraph_global_info_ready)
3253 { 3589 {
3254 warning (OPT_Winline, "inlining failed in call to %q+F: %s", 3590 warning (OPT_Winline, "inlining failed in call to %q+F: %s",
3255 fn, reason); 3591 fn, cgraph_inline_failed_string (reason));
3256 warning (OPT_Winline, "called from here"); 3592 warning (OPT_Winline, "called from here");
3257 } 3593 }
3258 goto egress; 3594 goto egress;
3259 } 3595 }
3260 fn = cg_edge->callee->decl; 3596 fn = cg_edge->callee->decl;
3263 if (cg_edge->callee->decl != id->dst_node->decl) 3599 if (cg_edge->callee->decl != id->dst_node->decl)
3264 verify_cgraph_node (cg_edge->callee); 3600 verify_cgraph_node (cg_edge->callee);
3265 #endif 3601 #endif
3266 3602
3267 /* We will be inlining this callee. */ 3603 /* We will be inlining this callee. */
3268 id->eh_region = lookup_stmt_eh_region (stmt); 3604 id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
3605
3606 /* Update the callers EH personality. */
3607 if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
3608 DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
3609 = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
3269 3610
3270 /* Split the block holding the GIMPLE_CALL. */ 3611 /* Split the block holding the GIMPLE_CALL. */
3271 e = split_block (bb, stmt); 3612 e = split_block (bb, stmt);
3272 bb = e->src; 3613 bb = e->src;
3273 return_block = e->dest; 3614 return_block = e->dest;
3308 3649
3309 /* Local declarations will be replaced by their equivalents in this 3650 /* Local declarations will be replaced by their equivalents in this
3310 map. */ 3651 map. */
3311 st = id->decl_map; 3652 st = id->decl_map;
3312 id->decl_map = pointer_map_create (); 3653 id->decl_map = pointer_map_create ();
3654 dst = id->debug_map;
3655 id->debug_map = NULL;
3313 3656
3314 /* Record the function we are about to inline. */ 3657 /* Record the function we are about to inline. */
3315 id->src_fn = fn; 3658 id->src_fn = fn;
3316 id->src_node = cg_edge->callee; 3659 id->src_node = cg_edge->callee;
3317 id->src_cfun = DECL_STRUCT_FUNCTION (fn); 3660 id->src_cfun = DECL_STRUCT_FUNCTION (fn);
3371 return_slot = NULL; 3714 return_slot = NULL;
3372 modify_dest = NULL; 3715 modify_dest = NULL;
3373 } 3716 }
3374 3717
3375 /* Declare the return variable for the function. */ 3718 /* Declare the return variable for the function. */
3376 retvar = declare_return_variable (id, return_slot, modify_dest, &use_retvar); 3719 use_retvar = declare_return_variable (id, return_slot, modify_dest);
3377
3378 if (DECL_IS_OPERATOR_NEW (fn))
3379 {
3380 gcc_assert (TREE_CODE (retvar) == VAR_DECL
3381 && POINTER_TYPE_P (TREE_TYPE (retvar)));
3382 DECL_NO_TBAA_P (retvar) = 1;
3383 }
3384 3720
3385 /* Add local vars in this inlined callee to caller. */ 3721 /* Add local vars in this inlined callee to caller. */
3386 t_step = id->src_cfun->local_decls; 3722 t_step = id->src_cfun->local_decls;
3387 for (; t_step; t_step = TREE_CHAIN (t_step)) 3723 for (; t_step; t_step = TREE_CHAIN (t_step))
3388 { 3724 {
3396 else if (!can_be_nonlocal (var, id)) 3732 else if (!can_be_nonlocal (var, id))
3397 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id), 3733 cfun->local_decls = tree_cons (NULL_TREE, remap_decl (var, id),
3398 cfun->local_decls); 3734 cfun->local_decls);
3399 } 3735 }
3400 3736
3737 if (dump_file && (dump_flags & TDF_DETAILS))
3738 {
3739 fprintf (dump_file, "Inlining ");
3740 print_generic_expr (dump_file, id->src_fn, 0);
3741 fprintf (dump_file, " to ");
3742 print_generic_expr (dump_file, id->dst_fn, 0);
3743 fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
3744 }
3745
3401 /* This is it. Duplicate the callee body. Assume callee is 3746 /* This is it. Duplicate the callee body. Assume callee is
3402 pre-gimplified. Note that we must not alter the caller 3747 pre-gimplified. Note that we must not alter the caller
3403 function in any way before this point, as this CALL_EXPR may be 3748 function in any way before this point, as this CALL_EXPR may be
3404 a self-referential call; if we're calling ourselves, we need to 3749 a self-referential call; if we're calling ourselves, we need to
3405 duplicate our body before altering anything. */ 3750 duplicate our body before altering anything. */
3406 copy_body (id, bb->count, bb->frequency, bb, return_block); 3751 copy_body (id, bb->count,
3752 cg_edge->frequency * REG_BR_PROB_BASE / CGRAPH_FREQ_BASE,
3753 bb, return_block);
3754
3755 /* Reset the escaped and callused solutions. */
3756 if (cfun->gimple_df)
3757 {
3758 pt_solution_reset (&cfun->gimple_df->escaped);
3759 pt_solution_reset (&cfun->gimple_df->callused);
3760 }
3407 3761
3408 /* Clean up. */ 3762 /* Clean up. */
3763 if (id->debug_map)
3764 {
3765 pointer_map_destroy (id->debug_map);
3766 id->debug_map = dst;
3767 }
3409 pointer_map_destroy (id->decl_map); 3768 pointer_map_destroy (id->decl_map);
3410 id->decl_map = st; 3769 id->decl_map = st;
3770
3771 /* Unlink the calls virtual operands before replacing it. */
3772 unlink_stmt_vdef (stmt);
3411 3773
3412 /* If the inlined function returns a result that we care about, 3774 /* If the inlined function returns a result that we care about,
3413 substitute the GIMPLE_CALL with an assignment of the return 3775 substitute the GIMPLE_CALL with an assignment of the return
3414 variable to the LHS of the call. That is, if STMT was 3776 variable to the LHS of the call. That is, if STMT was
3415 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */ 3777 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
3417 { 3779 {
3418 gimple old_stmt = stmt; 3780 gimple old_stmt = stmt;
3419 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar); 3781 stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
3420 gsi_replace (&stmt_gsi, stmt, false); 3782 gsi_replace (&stmt_gsi, stmt, false);
3421 if (gimple_in_ssa_p (cfun)) 3783 if (gimple_in_ssa_p (cfun))
3422 { 3784 mark_symbols_for_renaming (stmt);
3423 update_stmt (stmt);
3424 mark_symbols_for_renaming (stmt);
3425 }
3426 maybe_clean_or_replace_eh_stmt (old_stmt, stmt); 3785 maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
3427 } 3786 }
3428 else 3787 else
3429 { 3788 {
3430 /* Handle the case of inlining a function with no return 3789 /* Handle the case of inlining a function with no return
3440 { 3799 {
3441 /* If the variable is used undefined, make this name 3800 /* If the variable is used undefined, make this name
3442 undefined via a move. */ 3801 undefined via a move. */
3443 stmt = gimple_build_assign (gimple_call_lhs (stmt), def); 3802 stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
3444 gsi_replace (&stmt_gsi, stmt, true); 3803 gsi_replace (&stmt_gsi, stmt, true);
3445 update_stmt (stmt);
3446 } 3804 }
3447 else 3805 else
3448 { 3806 {
3449 /* Otherwise make this variable undefined. */ 3807 /* Otherwise make this variable undefined. */
3450 gsi_remove (&stmt_gsi, true); 3808 gsi_remove (&stmt_gsi, true);
3525 !gsi_end_p (gsi); 3883 !gsi_end_p (gsi);
3526 gsi_next (&gsi)) 3884 gsi_next (&gsi))
3527 if (pointer_set_contains (statements, gsi_stmt (gsi))) 3885 if (pointer_set_contains (statements, gsi_stmt (gsi)))
3528 { 3886 {
3529 gimple old_stmt = gsi_stmt (gsi); 3887 gimple old_stmt = gsi_stmt (gsi);
3530 3888 tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
3531 if (fold_stmt (&gsi)) 3889
3890 if (old_decl && DECL_BUILT_IN (old_decl))
3891 {
3892 /* Folding builtins can create multiple instructions,
3893 we need to look at all of them. */
3894 gimple_stmt_iterator i2 = gsi;
3895 gsi_prev (&i2);
3896 if (fold_stmt (&gsi))
3897 {
3898 gimple new_stmt;
3899 if (gsi_end_p (i2))
3900 i2 = gsi_start_bb (BASIC_BLOCK (first));
3901 else
3902 gsi_next (&i2);
3903 while (1)
3904 {
3905 new_stmt = gsi_stmt (i2);
3906 update_stmt (new_stmt);
3907 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
3908 new_stmt);
3909
3910 if (new_stmt == gsi_stmt (gsi))
3911 {
3912 /* It is okay to check only for the very last
3913 of these statements. If it is a throwing
3914 statement nothing will change. If it isn't
3915 this can remove EH edges. If that weren't
3916 correct then because some intermediate stmts
3917 throw, but not the last one. That would mean
3918 we'd have to split the block, which we can't
3919 here and we'd loose anyway. And as builtins
3920 probably never throw, this all
3921 is mood anyway. */
3922 if (maybe_clean_or_replace_eh_stmt (old_stmt,
3923 new_stmt))
3924 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3925 break;
3926 }
3927 gsi_next (&i2);
3928 }
3929 }
3930 }
3931 else if (fold_stmt (&gsi))
3532 { 3932 {
3533 /* Re-read the statement from GSI as fold_stmt() may 3933 /* Re-read the statement from GSI as fold_stmt() may
3534 have changed it. */ 3934 have changed it. */
3535 gimple new_stmt = gsi_stmt (gsi); 3935 gimple new_stmt = gsi_stmt (gsi);
3536 update_stmt (new_stmt); 3936 update_stmt (new_stmt);
3537 3937
3538 if (is_gimple_call (old_stmt)) 3938 if (is_gimple_call (old_stmt)
3539 cgraph_update_edges_for_call_stmt (old_stmt, new_stmt); 3939 || is_gimple_call (new_stmt))
3940 cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
3941 new_stmt);
3540 3942
3541 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt)) 3943 if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
3542 gimple_purge_dead_eh_edges (BASIC_BLOCK (first)); 3944 gimple_purge_dead_eh_edges (BASIC_BLOCK (first));
3543 } 3945 }
3544 } 3946 }
3564 3966
3565 unsigned int 3967 unsigned int
3566 optimize_inline_calls (tree fn) 3968 optimize_inline_calls (tree fn)
3567 { 3969 {
3568 copy_body_data id; 3970 copy_body_data id;
3569 tree prev_fn;
3570 basic_block bb; 3971 basic_block bb;
3571 int last = n_basic_blocks; 3972 int last = n_basic_blocks;
3572 struct gimplify_ctx gctx; 3973 struct gimplify_ctx gctx;
3573 3974
3574 /* There is no point in performing inlining if errors have already 3975 /* There is no point in performing inlining if errors have already
3581 memset (&id, 0, sizeof (id)); 3982 memset (&id, 0, sizeof (id));
3582 3983
3583 id.src_node = id.dst_node = cgraph_node (fn); 3984 id.src_node = id.dst_node = cgraph_node (fn);
3584 id.dst_fn = fn; 3985 id.dst_fn = fn;
3585 /* Or any functions that aren't finished yet. */ 3986 /* Or any functions that aren't finished yet. */
3586 prev_fn = NULL_TREE;
3587 if (current_function_decl) 3987 if (current_function_decl)
3588 { 3988 id.dst_fn = current_function_decl;
3589 id.dst_fn = current_function_decl;
3590 prev_fn = current_function_decl;
3591 }
3592 3989
3593 id.copy_decl = copy_decl_maybe_to_var; 3990 id.copy_decl = copy_decl_maybe_to_var;
3594 id.transform_call_graph_edges = CB_CGE_DUPLICATE; 3991 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3595 id.transform_new_cfg = false; 3992 id.transform_new_cfg = false;
3596 id.transform_return_to_modify = true; 3993 id.transform_return_to_modify = true;
3626 /* Double check that we inlined everything we are supposed to inline. */ 4023 /* Double check that we inlined everything we are supposed to inline. */
3627 for (e = id.dst_node->callees; e; e = e->next_callee) 4024 for (e = id.dst_node->callees; e; e = e->next_callee)
3628 gcc_assert (e->inline_failed); 4025 gcc_assert (e->inline_failed);
3629 } 4026 }
3630 #endif 4027 #endif
3631 4028
3632 /* Fold the statements before compacting/renumbering the basic blocks. */ 4029 /* Fold the statements before compacting/renumbering the basic blocks. */
3633 fold_marked_statements (last, id.statements_to_fold); 4030 fold_marked_statements (last, id.statements_to_fold);
3634 pointer_set_destroy (id.statements_to_fold); 4031 pointer_set_destroy (id.statements_to_fold);
3635 4032
4033 gcc_assert (!id.debug_stmts);
4034
3636 /* Renumber the (code) basic_blocks consecutively. */ 4035 /* Renumber the (code) basic_blocks consecutively. */
3637 compact_blocks (); 4036 compact_blocks ();
3638 /* Renumber the lexical scoping (non-code) blocks consecutively. */ 4037 /* Renumber the lexical scoping (non-code) blocks consecutively. */
3639 number_blocks (fn); 4038 number_blocks (fn);
3640 4039
3641 /* We are not going to maintain the cgraph edges up to date.
3642 Kill it so it won't confuse us. */
3643 cgraph_node_remove_callees (id.dst_node);
3644
3645 fold_cond_expr_cond (); 4040 fold_cond_expr_cond ();
4041 delete_unreachable_blocks_update_callgraph (&id);
4042 #ifdef ENABLE_CHECKING
4043 verify_cgraph_node (id.dst_node);
4044 #endif
3646 4045
3647 /* It would be nice to check SSA/CFG/statement consistency here, but it is 4046 /* It would be nice to check SSA/CFG/statement consistency here, but it is
3648 not possible yet - the IPA passes might make various functions to not 4047 not possible yet - the IPA passes might make various functions to not
3649 throw and they don't care to proactively update local EH info. This is 4048 throw and they don't care to proactively update local EH info. This is
3650 done later in fixup_cfg pass that also execute the verification. */ 4049 done later in fixup_cfg pass that also execute the verification. */
3834 4233
3835 else if (TREE_CODE (*tp) == STATEMENT_LIST) 4234 else if (TREE_CODE (*tp) == STATEMENT_LIST)
3836 gcc_unreachable (); 4235 gcc_unreachable ();
3837 else if (TREE_CODE (*tp) == BIND_EXPR) 4236 else if (TREE_CODE (*tp) == BIND_EXPR)
3838 copy_bind_expr (tp, walk_subtrees, id); 4237 copy_bind_expr (tp, walk_subtrees, id);
3839 else if (TREE_CODE (*tp) == SAVE_EXPR) 4238 else if (TREE_CODE (*tp) == SAVE_EXPR
4239 || TREE_CODE (*tp) == TARGET_EXPR)
3840 remap_save_expr (tp, st, walk_subtrees); 4240 remap_save_expr (tp, st, walk_subtrees);
3841 else 4241 else
3842 { 4242 {
3843 copy_tree_r (tp, walk_subtrees, NULL); 4243 copy_tree_r (tp, walk_subtrees, NULL);
3844 4244
3865 /* Set up ID. */ 4265 /* Set up ID. */
3866 memset (&id, 0, sizeof (id)); 4266 memset (&id, 0, sizeof (id));
3867 id.src_fn = current_function_decl; 4267 id.src_fn = current_function_decl;
3868 id.dst_fn = current_function_decl; 4268 id.dst_fn = current_function_decl;
3869 id.decl_map = pointer_map_create (); 4269 id.decl_map = pointer_map_create ();
4270 id.debug_map = NULL;
3870 4271
3871 id.copy_decl = copy_decl_no_change; 4272 id.copy_decl = copy_decl_no_change;
3872 id.transform_call_graph_edges = CB_CGE_DUPLICATE; 4273 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
3873 id.transform_new_cfg = false; 4274 id.transform_new_cfg = false;
3874 id.transform_return_to_modify = false; 4275 id.transform_return_to_modify = false;
3880 /* Walk the tree again, copying, remapping, and unsaving. */ 4281 /* Walk the tree again, copying, remapping, and unsaving. */
3881 walk_tree (&expr, unsave_r, &id, NULL); 4282 walk_tree (&expr, unsave_r, &id, NULL);
3882 4283
3883 /* Clean up. */ 4284 /* Clean up. */
3884 pointer_map_destroy (id.decl_map); 4285 pointer_map_destroy (id.decl_map);
4286 if (id.debug_map)
4287 pointer_map_destroy (id.debug_map);
3885 4288
3886 return expr; 4289 return expr;
3887 } 4290 }
3888 4291
3889 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local 4292 /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
4011 /* Set up ID. */ 4414 /* Set up ID. */
4012 memset (&id, 0, sizeof (id)); 4415 memset (&id, 0, sizeof (id));
4013 id.src_fn = current_function_decl; 4416 id.src_fn = current_function_decl;
4014 id.dst_fn = current_function_decl; 4417 id.dst_fn = current_function_decl;
4015 id.decl_map = pointer_map_create (); 4418 id.decl_map = pointer_map_create ();
4419 id.debug_map = NULL;
4016 4420
4017 id.copy_decl = copy_decl_no_change; 4421 id.copy_decl = copy_decl_no_change;
4018 id.transform_call_graph_edges = CB_CGE_DUPLICATE; 4422 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
4019 id.transform_new_cfg = false; 4423 id.transform_new_cfg = false;
4020 id.transform_return_to_modify = false; 4424 id.transform_return_to_modify = false;
4035 wi.info = &id; 4439 wi.info = &id;
4036 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi); 4440 walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
4037 4441
4038 /* Clean up. */ 4442 /* Clean up. */
4039 pointer_map_destroy (id.decl_map); 4443 pointer_map_destroy (id.decl_map);
4444 if (id.debug_map)
4445 pointer_map_destroy (id.debug_map);
4040 4446
4041 return copy; 4447 return copy;
4042 } 4448 }
4043 4449
4044 4450
4089 generated it for the copy either. */ 4495 generated it for the copy either. */
4090 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl); 4496 DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
4091 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl); 4497 DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
4092 4498
4093 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what 4499 /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
4094 declaration inspired this copy. */ 4500 declaration inspired this copy. */
4095 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl); 4501 DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
4096 4502
4097 /* The new variable/label has no RTL, yet. */ 4503 /* The new variable/label has no RTL, yet. */
4098 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL) 4504 if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
4099 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy)) 4505 && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
4100 SET_DECL_RTL (copy, NULL_RTX); 4506 SET_DECL_RTL (copy, NULL_RTX);
4101 4507
4102 /* These args would always appear unused, if not for this. */ 4508 /* These args would always appear unused, if not for this. */
4103 TREE_USED (copy) = 1; 4509 TREE_USED (copy) = 1;
4104 4510
4105 /* Set the context for the new declaration. */ 4511 /* Set the context for the new declaration. */
4106 if (!DECL_CONTEXT (decl)) 4512 if (!DECL_CONTEXT (decl))
4130 gcc_assert (TREE_CODE (decl) == PARM_DECL 4536 gcc_assert (TREE_CODE (decl) == PARM_DECL
4131 || TREE_CODE (decl) == RESULT_DECL); 4537 || TREE_CODE (decl) == RESULT_DECL);
4132 4538
4133 type = TREE_TYPE (decl); 4539 type = TREE_TYPE (decl);
4134 4540
4135 copy = build_decl (VAR_DECL, DECL_NAME (decl), type); 4541 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4542 VAR_DECL, DECL_NAME (decl), type);
4136 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl); 4543 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4137 TREE_READONLY (copy) = TREE_READONLY (decl); 4544 TREE_READONLY (copy) = TREE_READONLY (decl);
4138 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl); 4545 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4139 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl); 4546 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4140 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4141 4547
4142 return copy_decl_for_dup_finish (id, decl, copy); 4548 return copy_decl_for_dup_finish (id, decl, copy);
4143 } 4549 }
4144 4550
4145 /* Like copy_decl_to_var, but create a return slot object instead of a 4551 /* Like copy_decl_to_var, but create a return slot object instead of a
4155 4561
4156 type = TREE_TYPE (decl); 4562 type = TREE_TYPE (decl);
4157 if (DECL_BY_REFERENCE (decl)) 4563 if (DECL_BY_REFERENCE (decl))
4158 type = TREE_TYPE (type); 4564 type = TREE_TYPE (type);
4159 4565
4160 copy = build_decl (VAR_DECL, DECL_NAME (decl), type); 4566 copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
4567 VAR_DECL, DECL_NAME (decl), type);
4161 TREE_READONLY (copy) = TREE_READONLY (decl); 4568 TREE_READONLY (copy) = TREE_READONLY (decl);
4162 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl); 4569 TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
4163 if (!DECL_BY_REFERENCE (decl)) 4570 if (!DECL_BY_REFERENCE (decl))
4164 { 4571 {
4165 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl); 4572 TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
4166 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl); 4573 DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
4167 DECL_NO_TBAA_P (copy) = DECL_NO_TBAA_P (decl);
4168 } 4574 }
4169 4575
4170 return copy_decl_for_dup_finish (id, decl, copy); 4576 return copy_decl_for_dup_finish (id, decl, copy);
4171 } 4577 }
4172 4578
4253 return static_chain; 4659 return static_chain;
4254 } 4660 }
4255 4661
4256 /* Return true if the function is allowed to be versioned. 4662 /* Return true if the function is allowed to be versioned.
4257 This is a guard for the versioning functionality. */ 4663 This is a guard for the versioning functionality. */
4664
4258 bool 4665 bool
4259 tree_versionable_function_p (tree fndecl) 4666 tree_versionable_function_p (tree fndecl)
4260 { 4667 {
4261 if (fndecl == NULL_TREE) 4668 return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
4262 return false; 4669 && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
4263 /* ??? There are cases where a function is 4670 }
4264 uninlinable but can be versioned. */ 4671
4265 if (!tree_inlinable_function_p (fndecl)) 4672 /* Delete all unreachable basic blocks and update callgraph.
4266 return false; 4673 Doing so is somewhat nontrivial because we need to update all clones and
4267 4674 remove inline function that become unreachable. */
4268 return true; 4675
4676 static bool
4677 delete_unreachable_blocks_update_callgraph (copy_body_data *id)
4678 {
4679 bool changed = false;
4680 basic_block b, next_bb;
4681
4682 find_unreachable_blocks ();
4683
4684 /* Delete all unreachable basic blocks. */
4685
4686 for (b = ENTRY_BLOCK_PTR->next_bb; b != EXIT_BLOCK_PTR; b = next_bb)
4687 {
4688 next_bb = b->next_bb;
4689
4690 if (!(b->flags & BB_REACHABLE))
4691 {
4692 gimple_stmt_iterator bsi;
4693
4694 for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
4695 if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL)
4696 {
4697 struct cgraph_edge *e;
4698 struct cgraph_node *node;
4699
4700 if ((e = cgraph_edge (id->dst_node, gsi_stmt (bsi))) != NULL)
4701 {
4702 if (!e->inline_failed)
4703 cgraph_remove_node_and_inline_clones (e->callee);
4704 else
4705 cgraph_remove_edge (e);
4706 }
4707 if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
4708 && id->dst_node->clones)
4709 for (node = id->dst_node->clones; node != id->dst_node;)
4710 {
4711 if ((e = cgraph_edge (node, gsi_stmt (bsi))) != NULL)
4712 {
4713 if (!e->inline_failed)
4714 cgraph_remove_node_and_inline_clones (e->callee);
4715 else
4716 cgraph_remove_edge (e);
4717 }
4718
4719 if (node->clones)
4720 node = node->clones;
4721 else if (node->next_sibling_clone)
4722 node = node->next_sibling_clone;
4723 else
4724 {
4725 while (node != id->dst_node && !node->next_sibling_clone)
4726 node = node->clone_of;
4727 if (node != id->dst_node)
4728 node = node->next_sibling_clone;
4729 }
4730 }
4731 }
4732 delete_basic_block (b);
4733 changed = true;
4734 }
4735 }
4736
4737 if (changed)
4738 tidy_fallthru_edges ();
4739 return changed;
4740 }
4741
4742 /* Update clone info after duplication. */
4743
4744 static void
4745 update_clone_info (copy_body_data * id)
4746 {
4747 struct cgraph_node *node;
4748 if (!id->dst_node->clones)
4749 return;
4750 for (node = id->dst_node->clones; node != id->dst_node;)
4751 {
4752 /* First update replace maps to match the new body. */
4753 if (node->clone.tree_map)
4754 {
4755 unsigned int i;
4756 for (i = 0; i < VEC_length (ipa_replace_map_p, node->clone.tree_map); i++)
4757 {
4758 struct ipa_replace_map *replace_info;
4759 replace_info = VEC_index (ipa_replace_map_p, node->clone.tree_map, i);
4760 walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
4761 walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
4762 }
4763 }
4764 if (node->clones)
4765 node = node->clones;
4766 else if (node->next_sibling_clone)
4767 node = node->next_sibling_clone;
4768 else
4769 {
4770 while (node != id->dst_node && !node->next_sibling_clone)
4771 node = node->clone_of;
4772 if (node != id->dst_node)
4773 node = node->next_sibling_clone;
4774 }
4775 }
4269 } 4776 }
4270 4777
4271 /* Create a copy of a function's tree. 4778 /* Create a copy of a function's tree.
4272 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes 4779 OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
4273 of the original function and the new copied function 4780 of the original function and the new copied function
4274 respectively. In case we want to replace a DECL 4781 respectively. In case we want to replace a DECL
4275 tree with another tree while duplicating the function's 4782 tree with another tree while duplicating the function's
4276 body, TREE_MAP represents the mapping between these 4783 body, TREE_MAP represents the mapping between these
4277 trees. If UPDATE_CLONES is set, the call_stmt fields 4784 trees. If UPDATE_CLONES is set, the call_stmt fields
4278 of edges of clones of the function will be updated. */ 4785 of edges of clones of the function will be updated. */
4279 void 4786 void
4280 tree_function_versioning (tree old_decl, tree new_decl, varray_type tree_map, 4787 tree_function_versioning (tree old_decl, tree new_decl,
4788 VEC(ipa_replace_map_p,gc)* tree_map,
4281 bool update_clones, bitmap args_to_skip) 4789 bool update_clones, bitmap args_to_skip)
4282 { 4790 {
4283 struct cgraph_node *old_version_node; 4791 struct cgraph_node *old_version_node;
4284 struct cgraph_node *new_version_node; 4792 struct cgraph_node *new_version_node;
4285 copy_body_data id; 4793 copy_body_data id;
4286 tree p; 4794 tree p;
4287 unsigned i; 4795 unsigned i;
4288 struct ipa_replace_map *replace_info; 4796 struct ipa_replace_map *replace_info;
4289 basic_block old_entry_block; 4797 basic_block old_entry_block, bb;
4290 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10); 4798 VEC (gimple, heap) *init_stmts = VEC_alloc (gimple, heap, 10);
4291 4799
4292 tree t_step; 4800 tree t_step;
4293 tree old_current_function_decl = current_function_decl; 4801 tree old_current_function_decl = current_function_decl;
4294 tree vars = NULL_TREE; 4802 tree vars = NULL_TREE;
4306 remove the cgraph node. */ 4814 remove the cgraph node. */
4307 (*debug_hooks->outlining_inline_function) (old_decl); 4815 (*debug_hooks->outlining_inline_function) (old_decl);
4308 4816
4309 DECL_ARTIFICIAL (new_decl) = 1; 4817 DECL_ARTIFICIAL (new_decl) = 1;
4310 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl); 4818 DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
4819 DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
4311 4820
4312 /* Prepare the data structures for the tree copy. */ 4821 /* Prepare the data structures for the tree copy. */
4313 memset (&id, 0, sizeof (id)); 4822 memset (&id, 0, sizeof (id));
4314 4823
4315 /* Generate a new name for the new version. */ 4824 /* Generate a new name for the new version. */
4316 if (!update_clones) 4825 id.statements_to_fold = pointer_set_create ();
4317 { 4826
4318 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
4319 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
4320 SET_DECL_RTL (new_decl, NULL_RTX);
4321 id.statements_to_fold = pointer_set_create ();
4322 }
4323
4324 id.decl_map = pointer_map_create (); 4827 id.decl_map = pointer_map_create ();
4828 id.debug_map = NULL;
4325 id.src_fn = old_decl; 4829 id.src_fn = old_decl;
4326 id.dst_fn = new_decl; 4830 id.dst_fn = new_decl;
4327 id.src_node = old_version_node; 4831 id.src_node = old_version_node;
4328 id.dst_node = new_version_node; 4832 id.dst_node = new_version_node;
4329 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl); 4833 id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
4330 4834 if (id.src_node->ipa_transforms_to_apply)
4835 {
4836 VEC(ipa_opt_pass,heap) * old_transforms_to_apply = id.dst_node->ipa_transforms_to_apply;
4837 unsigned int i;
4838
4839 id.dst_node->ipa_transforms_to_apply = VEC_copy (ipa_opt_pass, heap,
4840 id.src_node->ipa_transforms_to_apply);
4841 for (i = 0; i < VEC_length (ipa_opt_pass, old_transforms_to_apply); i++)
4842 VEC_safe_push (ipa_opt_pass, heap, id.dst_node->ipa_transforms_to_apply,
4843 VEC_index (ipa_opt_pass,
4844 old_transforms_to_apply,
4845 i));
4846 }
4847
4331 id.copy_decl = copy_decl_no_change; 4848 id.copy_decl = copy_decl_no_change;
4332 id.transform_call_graph_edges 4849 id.transform_call_graph_edges
4333 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE; 4850 = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
4334 id.transform_new_cfg = true; 4851 id.transform_new_cfg = true;
4335 id.transform_return_to_modify = false; 4852 id.transform_return_to_modify = false;
4337 4854
4338 current_function_decl = new_decl; 4855 current_function_decl = new_decl;
4339 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION 4856 old_entry_block = ENTRY_BLOCK_PTR_FOR_FUNCTION
4340 (DECL_STRUCT_FUNCTION (old_decl)); 4857 (DECL_STRUCT_FUNCTION (old_decl));
4341 initialize_cfun (new_decl, old_decl, 4858 initialize_cfun (new_decl, old_decl,
4342 old_entry_block->count, 4859 old_entry_block->count);
4343 old_entry_block->frequency);
4344 push_cfun (DECL_STRUCT_FUNCTION (new_decl)); 4860 push_cfun (DECL_STRUCT_FUNCTION (new_decl));
4345 4861
4346 /* Copy the function's static chain. */ 4862 /* Copy the function's static chain. */
4347 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl; 4863 p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
4348 if (p) 4864 if (p)
4349 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl = 4865 DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
4350 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl, 4866 copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
4351 &id); 4867 &id);
4352 4868
4353 /* If there's a tree_map, prepare for substitution. */ 4869 /* If there's a tree_map, prepare for substitution. */
4354 if (tree_map) 4870 if (tree_map)
4355 for (i = 0; i < VARRAY_ACTIVE_SIZE (tree_map); i++) 4871 for (i = 0; i < VEC_length (ipa_replace_map_p, tree_map); i++)
4356 { 4872 {
4357 gimple init; 4873 gimple init;
4358 replace_info 4874 replace_info = VEC_index (ipa_replace_map_p, tree_map, i);
4359 = (struct ipa_replace_map *) VARRAY_GENERIC_PTR (tree_map, i);
4360 if (replace_info->replace_p) 4875 if (replace_info->replace_p)
4361 { 4876 {
4362 tree op = replace_info->new_tree; 4877 tree op = replace_info->new_tree;
4363 4878
4364 STRIP_NOPS (op); 4879 STRIP_NOPS (op);
4365 4880
4366 if (TREE_CODE (op) == VIEW_CONVERT_EXPR) 4881 if (TREE_CODE (op) == VIEW_CONVERT_EXPR)
4367 op = TREE_OPERAND (op, 0); 4882 op = TREE_OPERAND (op, 0);
4368 4883
4369 if (TREE_CODE (op) == ADDR_EXPR) 4884 if (TREE_CODE (op) == ADDR_EXPR)
4370 { 4885 {
4371 op = TREE_OPERAND (op, 0); 4886 op = TREE_OPERAND (op, 0);
4372 while (handled_component_p (op)) 4887 while (handled_component_p (op))
4373 op = TREE_OPERAND (op, 0); 4888 op = TREE_OPERAND (op, 0);
4386 /* Copy the function's arguments. */ 4901 /* Copy the function's arguments. */
4387 if (DECL_ARGUMENTS (old_decl) != NULL_TREE) 4902 if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
4388 DECL_ARGUMENTS (new_decl) = 4903 DECL_ARGUMENTS (new_decl) =
4389 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id, 4904 copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
4390 args_to_skip, &vars); 4905 args_to_skip, &vars);
4391 4906
4392 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id); 4907 DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
4393 4908
4394 /* Renumber the lexical scoping (non-code) blocks consecutively. */ 4909 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4395 number_blocks (id.dst_fn); 4910 number_blocks (id.dst_fn);
4396 4911
4397 declare_inline_vars (DECL_INITIAL (new_decl), vars); 4912 declare_inline_vars (DECL_INITIAL (new_decl), vars);
4913
4398 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE) 4914 if (DECL_STRUCT_FUNCTION (old_decl)->local_decls != NULL_TREE)
4399 /* Add local vars. */ 4915 /* Add local vars. */
4400 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls; 4916 for (t_step = DECL_STRUCT_FUNCTION (old_decl)->local_decls;
4401 t_step; t_step = TREE_CHAIN (t_step)) 4917 t_step; t_step = TREE_CHAIN (t_step))
4402 { 4918 {
4406 else if (!can_be_nonlocal (var, &id)) 4922 else if (!can_be_nonlocal (var, &id))
4407 cfun->local_decls = 4923 cfun->local_decls =
4408 tree_cons (NULL_TREE, remap_decl (var, &id), 4924 tree_cons (NULL_TREE, remap_decl (var, &id),
4409 cfun->local_decls); 4925 cfun->local_decls);
4410 } 4926 }
4411 4927
4412 /* Copy the Function's body. */ 4928 /* Copy the Function's body. */
4413 copy_body (&id, old_entry_block->count, old_entry_block->frequency, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR); 4929 copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
4414 4930 ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR);
4931
4415 if (DECL_RESULT (old_decl) != NULL_TREE) 4932 if (DECL_RESULT (old_decl) != NULL_TREE)
4416 { 4933 {
4417 tree *res_decl = &DECL_RESULT (old_decl); 4934 tree *res_decl = &DECL_RESULT (old_decl);
4418 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id); 4935 DECL_RESULT (new_decl) = remap_decl (*res_decl, &id);
4419 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl)); 4936 lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
4420 } 4937 }
4421 4938
4422 /* Renumber the lexical scoping (non-code) blocks consecutively. */ 4939 /* Renumber the lexical scoping (non-code) blocks consecutively. */
4423 number_blocks (new_decl); 4940 number_blocks (new_decl);
4424 4941
4425 if (VEC_length (gimple, init_stmts)) 4942 /* We want to create the BB unconditionally, so that the addition of
4426 { 4943 debug stmts doesn't affect BB count, which may in the end cause
4427 basic_block bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR)); 4944 codegen differences. */
4428 while (VEC_length (gimple, init_stmts)) 4945 bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
4429 insert_init_stmt (bb, VEC_pop (gimple, init_stmts)); 4946 while (VEC_length (gimple, init_stmts))
4947 insert_init_stmt (&id, bb, VEC_pop (gimple, init_stmts));
4948 update_clone_info (&id);
4949
4950 /* Remap the nonlocal_goto_save_area, if any. */
4951 if (cfun->nonlocal_goto_save_area)
4952 {
4953 struct walk_stmt_info wi;
4954
4955 memset (&wi, 0, sizeof (wi));
4956 wi.info = &id;
4957 walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
4430 } 4958 }
4431 4959
4432 /* Clean up. */ 4960 /* Clean up. */
4433 pointer_map_destroy (id.decl_map); 4961 pointer_map_destroy (id.decl_map);
4434 if (!update_clones) 4962 if (id.debug_map)
4435 { 4963 pointer_map_destroy (id.debug_map);
4436 fold_marked_statements (0, id.statements_to_fold);
4437 pointer_set_destroy (id.statements_to_fold);
4438 fold_cond_expr_cond ();
4439 }
4440 if (gimple_in_ssa_p (cfun))
4441 {
4442 free_dominance_info (CDI_DOMINATORS);
4443 free_dominance_info (CDI_POST_DOMINATORS);
4444 if (!update_clones)
4445 delete_unreachable_blocks ();
4446 update_ssa (TODO_update_ssa);
4447 if (!update_clones)
4448 {
4449 fold_cond_expr_cond ();
4450 if (need_ssa_update_p ())
4451 update_ssa (TODO_update_ssa);
4452 }
4453 }
4454 free_dominance_info (CDI_DOMINATORS); 4964 free_dominance_info (CDI_DOMINATORS);
4455 free_dominance_info (CDI_POST_DOMINATORS); 4965 free_dominance_info (CDI_POST_DOMINATORS);
4966
4967 fold_marked_statements (0, id.statements_to_fold);
4968 pointer_set_destroy (id.statements_to_fold);
4969 fold_cond_expr_cond ();
4970 delete_unreachable_blocks_update_callgraph (&id);
4971 update_ssa (TODO_update_ssa);
4972 free_dominance_info (CDI_DOMINATORS);
4973 free_dominance_info (CDI_POST_DOMINATORS);
4974
4975 gcc_assert (!id.debug_stmts);
4456 VEC_free (gimple, heap, init_stmts); 4976 VEC_free (gimple, heap, init_stmts);
4457 pop_cfun (); 4977 pop_cfun ();
4458 current_function_decl = old_current_function_decl; 4978 current_function_decl = old_current_function_decl;
4459 gcc_assert (!current_function_decl 4979 gcc_assert (!current_function_decl
4460 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun); 4980 || DECL_STRUCT_FUNCTION (current_function_decl) == cfun);
4461 return; 4981 return;
4462 } 4982 }
4463 4983
4984 /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
4985 the callee and return the inlined body on success. */
4986
4987 tree
4988 maybe_inline_call_in_expr (tree exp)
4989 {
4990 tree fn = get_callee_fndecl (exp);
4991
4992 /* We can only try to inline "const" functions. */
4993 if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
4994 {
4995 struct pointer_map_t *decl_map = pointer_map_create ();
4996 call_expr_arg_iterator iter;
4997 copy_body_data id;
4998 tree param, arg, t;
4999
5000 /* Remap the parameters. */
5001 for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
5002 param;
5003 param = TREE_CHAIN (param), arg = next_call_expr_arg (&iter))
5004 *pointer_map_insert (decl_map, param) = arg;
5005
5006 memset (&id, 0, sizeof (id));
5007 id.src_fn = fn;
5008 id.dst_fn = current_function_decl;
5009 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
5010 id.decl_map = decl_map;
5011
5012 id.copy_decl = copy_decl_no_change;
5013 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5014 id.transform_new_cfg = false;
5015 id.transform_return_to_modify = true;
5016 id.transform_lang_insert_block = false;
5017
5018 /* Make sure not to unshare trees behind the front-end's back
5019 since front-end specific mechanisms may rely on sharing. */
5020 id.regimplify = false;
5021 id.do_not_unshare = true;
5022
5023 /* We're not inside any EH region. */
5024 id.eh_lp_nr = 0;
5025
5026 t = copy_tree_body (&id);
5027 pointer_map_destroy (decl_map);
5028
5029 /* We can only return something suitable for use in a GENERIC
5030 expression tree. */
5031 if (TREE_CODE (t) == MODIFY_EXPR)
5032 return TREE_OPERAND (t, 1);
5033 }
5034
5035 return NULL_TREE;
5036 }
5037
4464 /* Duplicate a type, fields and all. */ 5038 /* Duplicate a type, fields and all. */
4465 5039
4466 tree 5040 tree
4467 build_duplicate_type (tree type) 5041 build_duplicate_type (tree type)
4468 { 5042 {
4471 memset (&id, 0, sizeof (id)); 5045 memset (&id, 0, sizeof (id));
4472 id.src_fn = current_function_decl; 5046 id.src_fn = current_function_decl;
4473 id.dst_fn = current_function_decl; 5047 id.dst_fn = current_function_decl;
4474 id.src_cfun = cfun; 5048 id.src_cfun = cfun;
4475 id.decl_map = pointer_map_create (); 5049 id.decl_map = pointer_map_create ();
5050 id.debug_map = NULL;
4476 id.copy_decl = copy_decl_no_change; 5051 id.copy_decl = copy_decl_no_change;
4477 5052
4478 type = remap_type_1 (type, &id); 5053 type = remap_type_1 (type, &id);
4479 5054
4480 pointer_map_destroy (id.decl_map); 5055 pointer_map_destroy (id.decl_map);
5056 if (id.debug_map)
5057 pointer_map_destroy (id.debug_map);
4481 5058
4482 TYPE_CANONICAL (type) = type; 5059 TYPE_CANONICAL (type) = type;
4483 5060
4484 return type; 5061 return type;
4485 } 5062 }
4486 5063
4487 /* Return whether it is safe to inline a function because it used different 5064 /* Return whether it is safe to inline a function because it used different
4488 target specific options or different optimization options. */ 5065 target specific options or call site actual types mismatch parameter types.
5066 E is the call edge to be checked. */
4489 bool 5067 bool
4490 tree_can_inline_p (tree caller, tree callee) 5068 tree_can_inline_p (struct cgraph_edge *e)
4491 { 5069 {
4492 #if 0 5070 #if 0
4493 /* This causes a regression in SPEC in that it prevents a cold function from 5071 /* This causes a regression in SPEC in that it prevents a cold function from
4494 inlining a hot function. Perhaps this should only apply to functions 5072 inlining a hot function. Perhaps this should only apply to functions
4495 that the user declares hot/cold/optimize explicitly. */ 5073 that the user declares hot/cold/optimize explicitly. */
4514 if ((caller_opt->optimize > callee_opt->optimize) 5092 if ((caller_opt->optimize > callee_opt->optimize)
4515 || (caller_opt->optimize_size != callee_opt->optimize_size)) 5093 || (caller_opt->optimize_size != callee_opt->optimize_size))
4516 return false; 5094 return false;
4517 } 5095 }
4518 #endif 5096 #endif
5097 tree caller, callee;
5098
5099 caller = e->caller->decl;
5100 callee = e->callee->decl;
5101
5102 /* We cannot inline a function that uses a different EH personality
5103 than the caller. */
5104 if (DECL_FUNCTION_PERSONALITY (caller)
5105 && DECL_FUNCTION_PERSONALITY (callee)
5106 && (DECL_FUNCTION_PERSONALITY (caller)
5107 != DECL_FUNCTION_PERSONALITY (callee)))
5108 {
5109 e->inline_failed = CIF_UNSPECIFIED;
5110 gimple_call_set_cannot_inline (e->call_stmt, true);
5111 return false;
5112 }
4519 5113
4520 /* Allow the backend to decide if inlining is ok. */ 5114 /* Allow the backend to decide if inlining is ok. */
4521 return targetm.target_option.can_inline_p (caller, callee); 5115 if (!targetm.target_option.can_inline_p (caller, callee))
4522 } 5116 {
5117 e->inline_failed = CIF_TARGET_OPTION_MISMATCH;
5118 gimple_call_set_cannot_inline (e->call_stmt, true);
5119 e->call_stmt_cannot_inline_p = true;
5120 return false;
5121 }
5122
5123 if (e->call_stmt
5124 && !gimple_check_call_args (e->call_stmt))
5125 {
5126 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
5127 gimple_call_set_cannot_inline (e->call_stmt, true);
5128 e->call_stmt_cannot_inline_p = true;
5129 return false;
5130 }
5131
5132 return true;
5133 }