Mercurial > hg > CbC > CbC_gcc
comparison gcc/tree-dfa.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 | a06113de4d67 |
children | b7f97abdc517 |
comparison
equal
deleted
inserted
replaced
52:c156f1bd5cd9 | 55:77e2b8dfacca |
---|---|
87 | 87 |
88 FOR_EACH_BB (bb) | 88 FOR_EACH_BB (bb) |
89 { | 89 { |
90 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) | 90 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) |
91 { | 91 { |
92 size_t i; | |
93 gimple stmt = gsi_stmt (si); | 92 gimple stmt = gsi_stmt (si); |
94 for (i = 0; i < gimple_num_ops (stmt); i++) | 93 if (is_gimple_debug (stmt)) |
95 walk_tree (gimple_op_ptr (stmt, i), find_vars_r, NULL, NULL); | 94 continue; |
95 find_referenced_vars_in (gsi_stmt (si)); | |
96 } | 96 } |
97 | 97 |
98 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si)) | 98 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si)) |
99 { | 99 find_referenced_vars_in (gsi_stmt (si)); |
100 gimple phi = gsi_stmt (si); | |
101 size_t i, len = gimple_phi_num_args (phi); | |
102 | |
103 walk_tree (gimple_phi_result_ptr (phi), find_vars_r, NULL, NULL); | |
104 | |
105 for (i = 0; i < len; i++) | |
106 { | |
107 tree arg = gimple_phi_arg_def (phi, i); | |
108 walk_tree (&arg, find_vars_r, NULL, NULL); | |
109 } | |
110 } | |
111 } | 100 } |
112 | 101 |
113 return 0; | 102 return 0; |
114 } | 103 } |
115 | 104 |
116 struct gimple_opt_pass pass_referenced_vars = | 105 struct gimple_opt_pass pass_referenced_vars = |
117 { | 106 { |
118 { | 107 { |
119 GIMPLE_PASS, | 108 GIMPLE_PASS, |
120 NULL, /* name */ | 109 "*referenced_vars", /* name */ |
121 NULL, /* gate */ | 110 NULL, /* gate */ |
122 find_referenced_vars, /* execute */ | 111 find_referenced_vars, /* execute */ |
123 NULL, /* sub */ | 112 NULL, /* sub */ |
124 NULL, /* next */ | 113 NULL, /* next */ |
125 0, /* static_pass_number */ | 114 0, /* static_pass_number */ |
142 create_var_ann (tree t) | 131 create_var_ann (tree t) |
143 { | 132 { |
144 var_ann_t ann; | 133 var_ann_t ann; |
145 | 134 |
146 gcc_assert (t); | 135 gcc_assert (t); |
147 gcc_assert (DECL_P (t)); | 136 gcc_assert (TREE_CODE (t) == VAR_DECL |
148 gcc_assert (!t->base.ann || t->base.ann->common.type == VAR_ANN); | 137 || TREE_CODE (t) == PARM_DECL |
138 || TREE_CODE (t) == RESULT_DECL); | |
149 | 139 |
150 ann = GGC_CNEW (struct var_ann_d); | 140 ann = GGC_CNEW (struct var_ann_d); |
151 ann->common.type = VAR_ANN; | 141 *DECL_VAR_ANN_PTR (t) = ann; |
152 t->base.ann = (tree_ann_t) ann; | |
153 | 142 |
154 return ann; | 143 return ann; |
155 } | 144 } |
156 | 145 |
157 /* Create a new annotation for a FUNCTION_DECL node T. */ | |
158 | |
159 function_ann_t | |
160 create_function_ann (tree t) | |
161 { | |
162 function_ann_t ann; | |
163 | |
164 gcc_assert (t); | |
165 gcc_assert (TREE_CODE (t) == FUNCTION_DECL); | |
166 gcc_assert (!t->base.ann || t->base.ann->common.type == FUNCTION_ANN); | |
167 | |
168 ann = (function_ann_t) ggc_alloc (sizeof (*ann)); | |
169 memset ((void *) ann, 0, sizeof (*ann)); | |
170 | |
171 ann->common.type = FUNCTION_ANN; | |
172 | |
173 t->base.ann = (tree_ann_t) ann; | |
174 | |
175 return ann; | |
176 } | |
177 | |
178 /* Renumber all of the gimple stmt uids. */ | 146 /* Renumber all of the gimple stmt uids. */ |
179 | 147 |
180 void | 148 void |
181 renumber_gimple_stmt_uids (void) | 149 renumber_gimple_stmt_uids (void) |
182 { | 150 { |
183 basic_block bb; | 151 basic_block bb; |
184 | 152 |
185 set_gimple_stmt_max_uid (cfun, 0); | 153 set_gimple_stmt_max_uid (cfun, 0); |
192 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); | 160 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); |
193 } | 161 } |
194 } | 162 } |
195 } | 163 } |
196 | 164 |
197 /* Create a new annotation for a tree T. */ | 165 /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks |
198 | 166 in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */ |
199 tree_ann_common_t | 167 |
200 create_tree_common_ann (tree t) | 168 void |
201 { | 169 renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks) |
202 tree_ann_common_t ann; | 170 { |
203 | 171 int i; |
204 gcc_assert (t); | 172 |
205 gcc_assert (!t->base.ann || t->base.ann->common.type == TREE_ANN_COMMON); | 173 set_gimple_stmt_max_uid (cfun, 0); |
206 | 174 for (i = 0; i < n_blocks; i++) |
207 ann = GGC_CNEW (struct tree_ann_common_d); | 175 { |
208 | 176 basic_block bb = blocks[i]; |
209 ann->type = TREE_ANN_COMMON; | 177 gimple_stmt_iterator bsi; |
210 ann->rn = -1; | 178 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi)) |
211 t->base.ann = (tree_ann_t) ann; | 179 { |
212 | 180 gimple stmt = gsi_stmt (bsi); |
213 return ann; | 181 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); |
182 } | |
183 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) | |
184 { | |
185 gimple stmt = gsi_stmt (bsi); | |
186 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun)); | |
187 } | |
188 } | |
214 } | 189 } |
215 | 190 |
216 /* Build a temporary. Make sure and register it to be renamed. */ | 191 /* Build a temporary. Make sure and register it to be renamed. */ |
217 | 192 |
218 tree | 193 tree |
244 void | 219 void |
245 dump_referenced_vars (FILE *file) | 220 dump_referenced_vars (FILE *file) |
246 { | 221 { |
247 tree var; | 222 tree var; |
248 referenced_var_iterator rvi; | 223 referenced_var_iterator rvi; |
249 | 224 |
250 fprintf (file, "\nReferenced variables in %s: %u\n\n", | 225 fprintf (file, "\nReferenced variables in %s: %u\n\n", |
251 get_name (current_function_decl), (unsigned) num_referenced_vars); | 226 get_name (current_function_decl), (unsigned) num_referenced_vars); |
252 | 227 |
253 FOR_EACH_REFERENCED_VAR (var, rvi) | 228 FOR_EACH_REFERENCED_VAR (var, rvi) |
254 { | 229 { |
255 fprintf (file, "Variable: "); | 230 fprintf (file, "Variable: "); |
256 dump_variable (file, var); | 231 dump_variable (file, var); |
257 fprintf (file, "\n"); | 232 } |
258 } | 233 |
234 fprintf (file, "\n"); | |
259 } | 235 } |
260 | 236 |
261 | 237 |
262 /* Dump the list of all the referenced variables to stderr. */ | 238 /* Dump the list of all the referenced variables to stderr. */ |
263 | 239 |
295 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var)); | 271 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var)); |
296 | 272 |
297 fprintf (file, ", "); | 273 fprintf (file, ", "); |
298 print_generic_expr (file, TREE_TYPE (var), dump_flags); | 274 print_generic_expr (file, TREE_TYPE (var), dump_flags); |
299 | 275 |
300 if (ann && ann->symbol_mem_tag) | |
301 { | |
302 fprintf (file, ", symbol memory tag: "); | |
303 print_generic_expr (file, ann->symbol_mem_tag, dump_flags); | |
304 } | |
305 | |
306 if (TREE_ADDRESSABLE (var)) | 276 if (TREE_ADDRESSABLE (var)) |
307 fprintf (file, ", is addressable"); | 277 fprintf (file, ", is addressable"); |
308 | 278 |
309 if (is_global_var (var)) | 279 if (is_global_var (var)) |
310 fprintf (file, ", is global"); | 280 fprintf (file, ", is global"); |
311 | 281 |
312 if (TREE_THIS_VOLATILE (var)) | 282 if (TREE_THIS_VOLATILE (var)) |
313 fprintf (file, ", is volatile"); | 283 fprintf (file, ", is volatile"); |
314 | 284 |
315 dump_mem_sym_stats_for_var (file, var); | |
316 | |
317 if (is_call_clobbered (var)) | 285 if (is_call_clobbered (var)) |
318 { | 286 fprintf (file, ", call clobbered"); |
319 const char *s = ""; | 287 else if (is_call_used (var)) |
320 var_ann_t va = var_ann (var); | 288 fprintf (file, ", call used"); |
321 unsigned int escape_mask = va->escape_mask; | 289 |
322 | 290 if (ann && ann->noalias_state == NO_ALIAS) |
323 fprintf (file, ", call clobbered"); | |
324 fprintf (file, " ("); | |
325 if (escape_mask & ESCAPE_STORED_IN_GLOBAL) | |
326 { fprintf (file, "%sstored in global", s); s = ", "; } | |
327 if (escape_mask & ESCAPE_TO_ASM) | |
328 { fprintf (file, "%sgoes through ASM", s); s = ", "; } | |
329 if (escape_mask & ESCAPE_TO_CALL) | |
330 { fprintf (file, "%spassed to call", s); s = ", "; } | |
331 if (escape_mask & ESCAPE_BAD_CAST) | |
332 { fprintf (file, "%sbad cast", s); s = ", "; } | |
333 if (escape_mask & ESCAPE_TO_RETURN) | |
334 { fprintf (file, "%sreturned from func", s); s = ", "; } | |
335 if (escape_mask & ESCAPE_TO_PURE_CONST) | |
336 { fprintf (file, "%spassed to pure/const", s); s = ", "; } | |
337 if (escape_mask & ESCAPE_IS_GLOBAL) | |
338 { fprintf (file, "%sis global var", s); s = ", "; } | |
339 if (escape_mask & ESCAPE_IS_PARM) | |
340 { fprintf (file, "%sis incoming pointer", s); s = ", "; } | |
341 if (escape_mask & ESCAPE_UNKNOWN) | |
342 { fprintf (file, "%sunknown escape", s); s = ", "; } | |
343 fprintf (file, ")"); | |
344 } | |
345 | |
346 if (ann->noalias_state == NO_ALIAS) | |
347 fprintf (file, ", NO_ALIAS (does not alias other NO_ALIAS symbols)"); | 291 fprintf (file, ", NO_ALIAS (does not alias other NO_ALIAS symbols)"); |
348 else if (ann->noalias_state == NO_ALIAS_GLOBAL) | 292 else if (ann && ann->noalias_state == NO_ALIAS_GLOBAL) |
349 fprintf (file, ", NO_ALIAS_GLOBAL (does not alias other NO_ALIAS symbols" | 293 fprintf (file, ", NO_ALIAS_GLOBAL (does not alias other NO_ALIAS symbols" |
350 " and global vars)"); | 294 " and global vars)"); |
351 else if (ann->noalias_state == NO_ALIAS_ANYTHING) | 295 else if (ann && ann->noalias_state == NO_ALIAS_ANYTHING) |
352 fprintf (file, ", NO_ALIAS_ANYTHING (does not alias any other symbols)"); | 296 fprintf (file, ", NO_ALIAS_ANYTHING (does not alias any other symbols)"); |
353 | 297 |
354 if (gimple_default_def (cfun, var)) | 298 if (cfun && gimple_default_def (cfun, var)) |
355 { | 299 { |
356 fprintf (file, ", default def: "); | 300 fprintf (file, ", default def: "); |
357 print_generic_expr (file, gimple_default_def (cfun, var), dump_flags); | 301 print_generic_expr (file, gimple_default_def (cfun, var), dump_flags); |
358 } | 302 } |
359 | 303 |
360 if (MTAG_P (var) && may_aliases (var)) | 304 if (DECL_INITIAL (var)) |
361 { | 305 { |
362 fprintf (file, ", may aliases: "); | 306 fprintf (file, ", initial: "); |
363 dump_may_aliases_for (file, var); | 307 print_generic_expr (file, DECL_INITIAL (var), dump_flags); |
364 } | |
365 | |
366 if (!is_gimple_reg (var)) | |
367 { | |
368 if (memory_partition (var)) | |
369 { | |
370 fprintf (file, ", belongs to partition: "); | |
371 print_generic_expr (file, memory_partition (var), dump_flags); | |
372 } | |
373 | |
374 if (TREE_CODE (var) == MEMORY_PARTITION_TAG) | |
375 { | |
376 fprintf (file, ", partition symbols: "); | |
377 dump_decl_set (file, MPT_SYMBOLS (var)); | |
378 } | |
379 } | 308 } |
380 | 309 |
381 fprintf (file, "\n"); | 310 fprintf (file, "\n"); |
382 } | 311 } |
383 | 312 |
514 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) | 443 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) |
515 { | 444 { |
516 gimple stmt = gsi_stmt (si); | 445 gimple stmt = gsi_stmt (si); |
517 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF); | 446 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF); |
518 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE); | 447 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE); |
519 dfa_stats_p->num_vdefs += NUM_SSA_OPERANDS (stmt, SSA_OP_VDEF); | 448 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0; |
520 dfa_stats_p->num_vuses += NUM_SSA_OPERANDS (stmt, SSA_OP_VUSE); | 449 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0; |
521 } | 450 } |
522 } | 451 } |
523 } | 452 } |
524 | 453 |
525 | 454 |
548 *walk_subtrees = 0; | 477 *walk_subtrees = 0; |
549 | 478 |
550 return NULL_TREE; | 479 return NULL_TREE; |
551 } | 480 } |
552 | 481 |
482 /* Find referenced variables in STMT. In contrast with | |
483 find_new_referenced_vars, this function will not mark newly found | |
484 variables for renaming. */ | |
485 | |
486 void | |
487 find_referenced_vars_in (gimple stmt) | |
488 { | |
489 size_t i; | |
490 | |
491 if (gimple_code (stmt) != GIMPLE_PHI) | |
492 { | |
493 for (i = 0; i < gimple_num_ops (stmt); i++) | |
494 walk_tree (gimple_op_ptr (stmt, i), find_vars_r, NULL, NULL); | |
495 } | |
496 else | |
497 { | |
498 walk_tree (gimple_phi_result_ptr (stmt), find_vars_r, NULL, NULL); | |
499 | |
500 for (i = 0; i < gimple_phi_num_args (stmt); i++) | |
501 { | |
502 tree arg = gimple_phi_arg_def (stmt, i); | |
503 walk_tree (&arg, find_vars_r, NULL, NULL); | |
504 } | |
505 } | |
506 } | |
507 | |
508 | |
553 /* Lookup UID in the referenced_vars hashtable and return the associated | 509 /* Lookup UID in the referenced_vars hashtable and return the associated |
554 variable. */ | 510 variable. */ |
555 | 511 |
556 tree | 512 tree |
557 referenced_var_lookup (unsigned int uid) | 513 referenced_var_lookup (unsigned int uid) |
558 { | 514 { |
559 tree h; | 515 tree h; |
560 struct tree_decl_minimal in; | 516 struct tree_decl_minimal in; |
561 in.uid = uid; | 517 in.uid = uid; |
562 h = (tree) htab_find_with_hash (gimple_referenced_vars (cfun), &in, uid); | 518 h = (tree) htab_find_with_hash (gimple_referenced_vars (cfun), &in, uid); |
563 gcc_assert (h || uid == 0); | 519 gcc_assert (h || uid == 0); |
564 return h; | 520 return h; |
565 } | 521 } |
566 | 522 |
567 /* Check if TO is in the referenced_vars hash table and insert it if not. | 523 /* Check if TO is in the referenced_vars hash table and insert it if not. |
568 Return true if it required insertion. */ | 524 Return true if it required insertion. */ |
569 | 525 |
570 bool | 526 bool |
571 referenced_var_check_and_insert (tree to) | 527 referenced_var_check_and_insert (tree to) |
572 { | 528 { |
573 tree h, *loc; | 529 tree h, *loc; |
574 struct tree_decl_minimal in; | 530 struct tree_decl_minimal in; |
575 unsigned int uid = DECL_UID (to); | 531 unsigned int uid = DECL_UID (to); |
576 | 532 |
577 in.uid = uid; | 533 in.uid = uid; |
591 } | 547 } |
592 | 548 |
593 /* Lookup VAR UID in the default_defs hashtable and return the associated | 549 /* Lookup VAR UID in the default_defs hashtable and return the associated |
594 variable. */ | 550 variable. */ |
595 | 551 |
596 tree | 552 tree |
597 gimple_default_def (struct function *fn, tree var) | 553 gimple_default_def (struct function *fn, tree var) |
598 { | 554 { |
599 struct tree_decl_minimal ind; | 555 struct tree_decl_minimal ind; |
600 struct tree_ssa_name in; | 556 struct tree_ssa_name in; |
601 gcc_assert (SSA_VAR_P (var)); | 557 gcc_assert (SSA_VAR_P (var)); |
606 | 562 |
607 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */ | 563 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */ |
608 | 564 |
609 void | 565 void |
610 set_default_def (tree var, tree def) | 566 set_default_def (tree var, tree def) |
611 { | 567 { |
612 struct tree_decl_minimal ind; | 568 struct tree_decl_minimal ind; |
613 struct tree_ssa_name in; | 569 struct tree_ssa_name in; |
614 void **loc; | 570 void **loc; |
615 | 571 |
616 gcc_assert (SSA_VAR_P (var)); | 572 gcc_assert (SSA_VAR_P (var)); |
640 /* Add VAR to the list of referenced variables if it isn't already there. */ | 596 /* Add VAR to the list of referenced variables if it isn't already there. */ |
641 | 597 |
642 bool | 598 bool |
643 add_referenced_var (tree var) | 599 add_referenced_var (tree var) |
644 { | 600 { |
645 var_ann_t v_ann; | 601 get_var_ann (var); |
646 | |
647 v_ann = get_var_ann (var); | |
648 gcc_assert (DECL_P (var)); | 602 gcc_assert (DECL_P (var)); |
649 | 603 |
650 /* Insert VAR into the referenced_vars has table if it isn't present. */ | 604 /* Insert VAR into the referenced_vars has table if it isn't present. */ |
651 if (referenced_var_check_and_insert (var)) | 605 if (referenced_var_check_and_insert (var)) |
652 { | 606 { |
653 /* This is the first time we found this variable, annotate it with | |
654 attributes that are intrinsic to the variable. */ | |
655 | |
656 /* Tag's don't have DECL_INITIAL. */ | |
657 if (MTAG_P (var)) | |
658 return true; | |
659 | |
660 /* Scan DECL_INITIAL for pointer variables as they may contain | 607 /* Scan DECL_INITIAL for pointer variables as they may contain |
661 address arithmetic referencing the address of other | 608 address arithmetic referencing the address of other |
662 variables. | 609 variables. As we are only interested in directly referenced |
663 Even non-constant initializers need to be walked, because | 610 globals or referenced locals restrict this to initializers |
664 IPA passes might prove that their are invariant later on. */ | 611 than can refer to local variables. */ |
665 if (DECL_INITIAL (var) | 612 if (DECL_INITIAL (var) |
666 /* Initializers of external variables are not useful to the | 613 && DECL_CONTEXT (var) == current_function_decl) |
667 optimizers. */ | |
668 && !DECL_EXTERNAL (var)) | |
669 walk_tree (&DECL_INITIAL (var), find_vars_r, NULL, 0); | 614 walk_tree (&DECL_INITIAL (var), find_vars_r, NULL, 0); |
670 | 615 |
671 return true; | 616 return true; |
672 } | 617 } |
673 | 618 |
682 var_ann_t v_ann; | 627 var_ann_t v_ann; |
683 struct tree_decl_minimal in; | 628 struct tree_decl_minimal in; |
684 void **loc; | 629 void **loc; |
685 unsigned int uid = DECL_UID (var); | 630 unsigned int uid = DECL_UID (var); |
686 | 631 |
687 clear_call_clobbered (var); | 632 /* Preserve var_anns of globals. */ |
688 bitmap_clear_bit (gimple_call_used_vars (cfun), uid); | 633 if (!is_global_var (var) |
689 if ((v_ann = var_ann (var))) | 634 && (v_ann = var_ann (var))) |
690 { | 635 { |
691 /* Preserve var_anns of globals, but clear their alias info. */ | 636 ggc_free (v_ann); |
692 if (MTAG_P (var) | 637 *DECL_VAR_ANN_PTR (var) = NULL; |
693 || (!TREE_STATIC (var) && !DECL_EXTERNAL (var))) | |
694 { | |
695 ggc_free (v_ann); | |
696 var->base.ann = NULL; | |
697 } | |
698 else | |
699 { | |
700 v_ann->mpt = NULL_TREE; | |
701 v_ann->symbol_mem_tag = NULL_TREE; | |
702 } | |
703 } | 638 } |
704 gcc_assert (DECL_P (var)); | 639 gcc_assert (DECL_P (var)); |
705 in.uid = uid; | 640 in.uid = uid; |
706 loc = htab_find_slot_with_hash (gimple_referenced_vars (cfun), &in, uid, | 641 loc = htab_find_slot_with_hash (gimple_referenced_vars (cfun), &in, uid, |
707 NO_INSERT); | 642 NO_INSERT); |
730 gcc_assert (!is_gimple_reg (var)); | 665 gcc_assert (!is_gimple_reg (var)); |
731 | 666 |
732 return var; | 667 return var; |
733 } | 668 } |
734 | 669 |
735 /* Mark all the naked symbols in STMT for SSA renaming. | 670 /* Mark all the naked symbols in STMT for SSA renaming. */ |
736 | |
737 NOTE: This function should only be used for brand new statements. | |
738 If the caller is modifying an existing statement, it should use the | |
739 combination push_stmt_changes/pop_stmt_changes. */ | |
740 | 671 |
741 void | 672 void |
742 mark_symbols_for_renaming (gimple stmt) | 673 mark_symbols_for_renaming (gimple stmt) |
743 { | 674 { |
744 tree op; | 675 tree op; |
800 HOST_WIDE_INT maxsize = -1; | 731 HOST_WIDE_INT maxsize = -1; |
801 tree size_tree = NULL_TREE; | 732 tree size_tree = NULL_TREE; |
802 HOST_WIDE_INT bit_offset = 0; | 733 HOST_WIDE_INT bit_offset = 0; |
803 bool seen_variable_array_ref = false; | 734 bool seen_variable_array_ref = false; |
804 | 735 |
805 gcc_assert (!SSA_VAR_P (exp)); | |
806 | |
807 /* First get the final access size from just the outermost expression. */ | 736 /* First get the final access size from just the outermost expression. */ |
808 if (TREE_CODE (exp) == COMPONENT_REF) | 737 if (TREE_CODE (exp) == COMPONENT_REF) |
809 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1)); | 738 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1)); |
810 else if (TREE_CODE (exp) == BIT_FIELD_REF) | 739 else if (TREE_CODE (exp) == BIT_FIELD_REF) |
811 size_tree = TREE_OPERAND (exp, 1); | 740 size_tree = TREE_OPERAND (exp, 1); |
812 else | 741 else if (!VOID_TYPE_P (TREE_TYPE (exp))) |
813 { | 742 { |
814 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); | 743 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); |
815 if (mode == BLKmode) | 744 if (mode == BLKmode) |
816 size_tree = TYPE_SIZE (TREE_TYPE (exp)); | 745 size_tree = TYPE_SIZE (TREE_TYPE (exp)); |
817 else | 746 else |
834 while (1) | 763 while (1) |
835 { | 764 { |
836 switch (TREE_CODE (exp)) | 765 switch (TREE_CODE (exp)) |
837 { | 766 { |
838 case BIT_FIELD_REF: | 767 case BIT_FIELD_REF: |
839 bit_offset += tree_low_cst (TREE_OPERAND (exp, 2), 0); | 768 bit_offset += TREE_INT_CST_LOW (TREE_OPERAND (exp, 2)); |
840 break; | 769 break; |
841 | 770 |
842 case COMPONENT_REF: | 771 case COMPONENT_REF: |
843 { | 772 { |
844 tree field = TREE_OPERAND (exp, 1); | 773 tree field = TREE_OPERAND (exp, 1); |
845 tree this_offset = component_ref_field_offset (exp); | 774 tree this_offset = component_ref_field_offset (exp); |
846 | 775 |
847 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST) | 776 if (this_offset |
777 && TREE_CODE (this_offset) == INTEGER_CST | |
778 && host_integerp (this_offset, 0)) | |
848 { | 779 { |
849 HOST_WIDE_INT hthis_offset = tree_low_cst (this_offset, 0); | 780 HOST_WIDE_INT hthis_offset = TREE_INT_CST_LOW (this_offset); |
850 | |
851 hthis_offset *= BITS_PER_UNIT; | 781 hthis_offset *= BITS_PER_UNIT; |
782 hthis_offset | |
783 += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field)); | |
852 bit_offset += hthis_offset; | 784 bit_offset += hthis_offset; |
853 bit_offset += tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 0); | 785 |
786 /* If we had seen a variable array ref already and we just | |
787 referenced the last field of a struct or a union member | |
788 then we have to adjust maxsize by the padding at the end | |
789 of our field. */ | |
790 if (seen_variable_array_ref | |
791 && maxsize != -1) | |
792 { | |
793 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0)); | |
794 tree next = TREE_CHAIN (field); | |
795 while (next && TREE_CODE (next) != FIELD_DECL) | |
796 next = TREE_CHAIN (next); | |
797 if (!next | |
798 || TREE_CODE (stype) != RECORD_TYPE) | |
799 { | |
800 tree fsize = DECL_SIZE_UNIT (field); | |
801 tree ssize = TYPE_SIZE_UNIT (stype); | |
802 if (host_integerp (fsize, 0) | |
803 && host_integerp (ssize, 0)) | |
804 maxsize += ((TREE_INT_CST_LOW (ssize) | |
805 - TREE_INT_CST_LOW (fsize)) | |
806 * BITS_PER_UNIT - hthis_offset); | |
807 else | |
808 maxsize = -1; | |
809 } | |
810 } | |
854 } | 811 } |
855 else | 812 else |
856 { | 813 { |
857 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0))); | 814 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0))); |
858 /* We need to adjust maxsize to the whole structure bitsize. | 815 /* We need to adjust maxsize to the whole structure bitsize. |
868 | 825 |
869 case ARRAY_REF: | 826 case ARRAY_REF: |
870 case ARRAY_RANGE_REF: | 827 case ARRAY_RANGE_REF: |
871 { | 828 { |
872 tree index = TREE_OPERAND (exp, 1); | 829 tree index = TREE_OPERAND (exp, 1); |
873 tree low_bound = array_ref_low_bound (exp); | 830 tree low_bound, unit_size; |
874 tree unit_size = array_ref_element_size (exp); | |
875 | 831 |
876 /* If the resulting bit-offset is constant, track it. */ | 832 /* If the resulting bit-offset is constant, track it. */ |
877 if (host_integerp (index, 0) | 833 if (TREE_CODE (index) == INTEGER_CST |
878 && host_integerp (low_bound, 0) | 834 && host_integerp (index, 0) |
879 && host_integerp (unit_size, 1)) | 835 && (low_bound = array_ref_low_bound (exp), |
836 host_integerp (low_bound, 0)) | |
837 && (unit_size = array_ref_element_size (exp), | |
838 host_integerp (unit_size, 1))) | |
880 { | 839 { |
881 HOST_WIDE_INT hindex = tree_low_cst (index, 0); | 840 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (index); |
882 | 841 |
883 hindex -= tree_low_cst (low_bound, 0); | 842 hindex -= TREE_INT_CST_LOW (low_bound); |
884 hindex *= tree_low_cst (unit_size, 1); | 843 hindex *= TREE_INT_CST_LOW (unit_size); |
885 hindex *= BITS_PER_UNIT; | 844 hindex *= BITS_PER_UNIT; |
886 bit_offset += hindex; | 845 bit_offset += hindex; |
887 | 846 |
888 /* An array ref with a constant index up in the structure | 847 /* An array ref with a constant index up in the structure |
889 hierarchy will constrain the size of any variable array ref | 848 hierarchy will constrain the size of any variable array ref |
914 case IMAGPART_EXPR: | 873 case IMAGPART_EXPR: |
915 bit_offset += bitsize; | 874 bit_offset += bitsize; |
916 break; | 875 break; |
917 | 876 |
918 case VIEW_CONVERT_EXPR: | 877 case VIEW_CONVERT_EXPR: |
919 /* ??? We probably should give up here and bail out. */ | |
920 break; | 878 break; |
921 | 879 |
922 default: | 880 default: |
923 goto done; | 881 goto done; |
924 } | 882 } |
929 | 887 |
930 /* We need to deal with variable arrays ending structures such as | 888 /* We need to deal with variable arrays ending structures such as |
931 struct { int length; int a[1]; } x; x.a[d] | 889 struct { int length; int a[1]; } x; x.a[d] |
932 struct { struct { int a; int b; } a[1]; } x; x.a[d].a | 890 struct { struct { int a; int b; } a[1]; } x; x.a[d].a |
933 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0] | 891 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0] |
892 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d] | |
934 where we do not know maxsize for variable index accesses to | 893 where we do not know maxsize for variable index accesses to |
935 the array. The simplest way to conservatively deal with this | 894 the array. The simplest way to conservatively deal with this |
936 is to punt in the case that offset + maxsize reaches the | 895 is to punt in the case that offset + maxsize reaches the |
937 base type boundary. */ | 896 base type boundary. This needs to include possible trailing padding |
938 if (seen_variable_array_ref | 897 that is there for alignment purposes. |
939 && maxsize != -1 | 898 |
940 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1) | 899 That is of course only true if the base object is not a decl. */ |
941 && bit_offset + maxsize | 900 |
942 == (signed)TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))) | 901 if (DECL_P (exp)) |
902 { | |
903 /* If maxsize is unknown adjust it according to the size of the | |
904 base decl. */ | |
905 if (maxsize == -1 | |
906 && host_integerp (DECL_SIZE (exp), 1)) | |
907 maxsize = TREE_INT_CST_LOW (DECL_SIZE (exp)) - bit_offset; | |
908 } | |
909 else if (seen_variable_array_ref | |
910 && maxsize != -1 | |
911 && (!host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1) | |
912 || (bit_offset + maxsize | |
913 == (signed) TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))))) | |
943 maxsize = -1; | 914 maxsize = -1; |
944 | 915 |
945 /* ??? Due to negative offsets in ARRAY_REF we can end up with | 916 /* ??? Due to negative offsets in ARRAY_REF we can end up with |
946 negative bit_offset here. We might want to store a zero offset | 917 negative bit_offset here. We might want to store a zero offset |
947 in this case. */ | 918 in this case. */ |
968 } | 939 } |
969 | 940 |
970 return false; | 941 return false; |
971 } | 942 } |
972 | 943 |
973 /* Return true, if the two memory references REF1 and REF2 may alias. */ | |
974 | |
975 bool | |
976 refs_may_alias_p (tree ref1, tree ref2) | |
977 { | |
978 tree base1, base2; | |
979 HOST_WIDE_INT offset1 = 0, offset2 = 0; | |
980 HOST_WIDE_INT size1 = -1, size2 = -1; | |
981 HOST_WIDE_INT max_size1 = -1, max_size2 = -1; | |
982 bool strict_aliasing_applies; | |
983 | |
984 gcc_assert ((SSA_VAR_P (ref1) | |
985 || handled_component_p (ref1) | |
986 || INDIRECT_REF_P (ref1) | |
987 || TREE_CODE (ref1) == TARGET_MEM_REF) | |
988 && (SSA_VAR_P (ref2) | |
989 || handled_component_p (ref2) | |
990 || INDIRECT_REF_P (ref2) | |
991 || TREE_CODE (ref2) == TARGET_MEM_REF)); | |
992 | |
993 /* Defer to TBAA if possible. */ | |
994 if (flag_strict_aliasing | |
995 && !alias_sets_conflict_p (get_alias_set (ref1), get_alias_set (ref2))) | |
996 return false; | |
997 | |
998 /* Decompose the references into their base objects and the access. */ | |
999 base1 = ref1; | |
1000 if (handled_component_p (ref1)) | |
1001 base1 = get_ref_base_and_extent (ref1, &offset1, &size1, &max_size1); | |
1002 base2 = ref2; | |
1003 if (handled_component_p (ref2)) | |
1004 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &max_size2); | |
1005 | |
1006 /* If both references are based on different variables, they cannot alias. | |
1007 If both references are based on the same variable, they cannot alias if | |
1008 the accesses do not overlap. */ | |
1009 if (SSA_VAR_P (base1) | |
1010 && SSA_VAR_P (base2)) | |
1011 { | |
1012 if (!operand_equal_p (base1, base2, 0)) | |
1013 return false; | |
1014 return ranges_overlap_p (offset1, max_size1, offset2, max_size2); | |
1015 } | |
1016 | |
1017 /* If one base is a ref-all pointer weird things are allowed. */ | |
1018 strict_aliasing_applies = (flag_strict_aliasing | |
1019 && (!INDIRECT_REF_P (base1) | |
1020 || get_alias_set (base1) != 0) | |
1021 && (!INDIRECT_REF_P (base2) | |
1022 || get_alias_set (base2) != 0)); | |
1023 | |
1024 /* If strict aliasing applies the only way to access a scalar variable | |
1025 is through a pointer dereference or through a union (gcc extension). */ | |
1026 if (strict_aliasing_applies | |
1027 && ((SSA_VAR_P (ref2) | |
1028 && !AGGREGATE_TYPE_P (TREE_TYPE (ref2)) | |
1029 && !INDIRECT_REF_P (ref1) | |
1030 && TREE_CODE (TREE_TYPE (base1)) != UNION_TYPE) | |
1031 || (SSA_VAR_P (ref1) | |
1032 && !AGGREGATE_TYPE_P (TREE_TYPE (ref1)) | |
1033 && !INDIRECT_REF_P (ref2) | |
1034 && TREE_CODE (TREE_TYPE (base2)) != UNION_TYPE))) | |
1035 return false; | |
1036 | |
1037 /* If both references are through the same type, or if strict aliasing | |
1038 doesn't apply they are through two same pointers, they do not alias | |
1039 if the accesses do not overlap. */ | |
1040 if ((strict_aliasing_applies | |
1041 && (TYPE_MAIN_VARIANT (TREE_TYPE (base1)) | |
1042 == TYPE_MAIN_VARIANT (TREE_TYPE (base2)))) | |
1043 || (TREE_CODE (base1) == INDIRECT_REF | |
1044 && TREE_CODE (base2) == INDIRECT_REF | |
1045 && operand_equal_p (TREE_OPERAND (base1, 0), | |
1046 TREE_OPERAND (base2, 0), 0))) | |
1047 return ranges_overlap_p (offset1, max_size1, offset2, max_size2); | |
1048 | |
1049 /* If both are component references through pointers try to find a | |
1050 common base and apply offset based disambiguation. This handles | |
1051 for example | |
1052 struct A { int i; int j; } *q; | |
1053 struct B { struct A a; int k; } *p; | |
1054 disambiguating q->i and p->a.j. */ | |
1055 if (strict_aliasing_applies | |
1056 && (TREE_CODE (base1) == INDIRECT_REF | |
1057 || TREE_CODE (base2) == INDIRECT_REF) | |
1058 && handled_component_p (ref1) | |
1059 && handled_component_p (ref2)) | |
1060 { | |
1061 tree *refp; | |
1062 /* Now search for the type of base1 in the access path of ref2. This | |
1063 would be a common base for doing offset based disambiguation on. */ | |
1064 refp = &ref2; | |
1065 while (handled_component_p (*refp) | |
1066 /* Note that the following is only conservative if there are | |
1067 never copies of types appearing as sub-structures. */ | |
1068 && (TYPE_MAIN_VARIANT (TREE_TYPE (*refp)) | |
1069 != TYPE_MAIN_VARIANT (TREE_TYPE (base1)))) | |
1070 refp = &TREE_OPERAND (*refp, 0); | |
1071 if (TYPE_MAIN_VARIANT (TREE_TYPE (*refp)) | |
1072 == TYPE_MAIN_VARIANT (TREE_TYPE (base1))) | |
1073 { | |
1074 HOST_WIDE_INT offadj, sztmp, msztmp; | |
1075 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp); | |
1076 offset2 -= offadj; | |
1077 return ranges_overlap_p (offset1, max_size1, offset2, max_size2); | |
1078 } | |
1079 /* The other way around. */ | |
1080 refp = &ref1; | |
1081 while (handled_component_p (*refp) | |
1082 && (TYPE_MAIN_VARIANT (TREE_TYPE (*refp)) | |
1083 != TYPE_MAIN_VARIANT (TREE_TYPE (base2)))) | |
1084 refp = &TREE_OPERAND (*refp, 0); | |
1085 if (TYPE_MAIN_VARIANT (TREE_TYPE (*refp)) | |
1086 == TYPE_MAIN_VARIANT (TREE_TYPE (base2))) | |
1087 { | |
1088 HOST_WIDE_INT offadj, sztmp, msztmp; | |
1089 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp); | |
1090 offset1 -= offadj; | |
1091 return ranges_overlap_p (offset1, max_size1, offset2, max_size2); | |
1092 } | |
1093 /* If we can be sure to catch all equivalent types in the search | |
1094 for the common base then we could return false here. In that | |
1095 case we would be able to disambiguate q->i and p->k. */ | |
1096 } | |
1097 | |
1098 return true; | |
1099 } | |
1100 | |
1101 /* Given a stmt STMT that references memory, return the single stmt | |
1102 that is reached by following the VUSE -> VDEF link. Returns | |
1103 NULL_TREE, if there is no single stmt that defines all VUSEs of | |
1104 STMT. | |
1105 Note that for a stmt with a single virtual operand this may return | |
1106 a PHI node as well. Note that if all VUSEs are default definitions | |
1107 this function will return an empty statement. */ | |
1108 | |
1109 gimple | |
1110 get_single_def_stmt (gimple stmt) | |
1111 { | |
1112 gimple def_stmt = NULL; | |
1113 tree use; | |
1114 ssa_op_iter iter; | |
1115 | |
1116 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_VIRTUAL_USES) | |
1117 { | |
1118 gimple tmp = SSA_NAME_DEF_STMT (use); | |
1119 | |
1120 /* ??? This is too simplistic for multiple virtual operands | |
1121 reaching different PHI nodes of the same basic blocks or for | |
1122 reaching all default definitions. */ | |
1123 if (def_stmt | |
1124 && def_stmt != tmp | |
1125 && !(gimple_nop_p (def_stmt) | |
1126 && gimple_nop_p (tmp))) | |
1127 return NULL; | |
1128 | |
1129 def_stmt = tmp; | |
1130 } | |
1131 | |
1132 return def_stmt; | |
1133 } | |
1134 | |
1135 /* Given a PHI node of virtual operands, tries to eliminate cyclic | |
1136 reached definitions if they do not alias REF and returns the | |
1137 defining statement of the single virtual operand that flows in | |
1138 from a non-backedge. Returns NULL_TREE if such statement within | |
1139 the above conditions cannot be found. */ | |
1140 | |
1141 gimple | |
1142 get_single_def_stmt_from_phi (tree ref, gimple phi) | |
1143 { | |
1144 tree def_arg = NULL_TREE; | |
1145 unsigned i; | |
1146 | |
1147 /* Find the single PHI argument that is not flowing in from a | |
1148 back edge and verify that the loop-carried definitions do | |
1149 not alias the reference we look for. */ | |
1150 for (i = 0; i < gimple_phi_num_args (phi); ++i) | |
1151 { | |
1152 tree arg = PHI_ARG_DEF (phi, i); | |
1153 gimple def_stmt; | |
1154 | |
1155 if (!(gimple_phi_arg_edge (phi, i)->flags & EDGE_DFS_BACK)) | |
1156 { | |
1157 /* Multiple non-back edges? Do not try to handle this. */ | |
1158 if (def_arg) | |
1159 return NULL; | |
1160 def_arg = arg; | |
1161 continue; | |
1162 } | |
1163 | |
1164 /* Follow the definitions back to the original PHI node. Bail | |
1165 out once a definition is found that may alias REF. */ | |
1166 def_stmt = SSA_NAME_DEF_STMT (arg); | |
1167 do | |
1168 { | |
1169 if (!is_gimple_assign (def_stmt) | |
1170 || refs_may_alias_p (ref, gimple_assign_lhs (def_stmt))) | |
1171 return NULL; | |
1172 /* ??? This will only work, reaching the PHI node again if | |
1173 there is a single virtual operand on def_stmt. */ | |
1174 def_stmt = get_single_def_stmt (def_stmt); | |
1175 if (!def_stmt) | |
1176 return NULL; | |
1177 } | |
1178 while (def_stmt != phi); | |
1179 } | |
1180 | |
1181 return SSA_NAME_DEF_STMT (def_arg); | |
1182 } | |
1183 | |
1184 /* Return the single reference statement defining all virtual uses | |
1185 on STMT or NULL_TREE, if there are multiple defining statements. | |
1186 Take into account only definitions that alias REF if following | |
1187 back-edges when looking through a loop PHI node. */ | |
1188 | |
1189 gimple | |
1190 get_single_def_stmt_with_phi (tree ref, gimple stmt) | |
1191 { | |
1192 switch (NUM_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES)) | |
1193 { | |
1194 case 0: | |
1195 gcc_unreachable (); | |
1196 | |
1197 case 1: | |
1198 { | |
1199 gimple def_stmt = SSA_NAME_DEF_STMT (SINGLE_SSA_TREE_OPERAND | |
1200 (stmt, SSA_OP_VIRTUAL_USES)); | |
1201 /* We can handle lookups over PHI nodes only for a single | |
1202 virtual operand. */ | |
1203 if (gimple_code (def_stmt) == GIMPLE_PHI) | |
1204 return get_single_def_stmt_from_phi (ref, def_stmt); | |
1205 return def_stmt; | |
1206 } | |
1207 | |
1208 default: | |
1209 return get_single_def_stmt (stmt); | |
1210 } | |
1211 } |