Mercurial > hg > CbC > CbC_gcc
comparison gcc/cfgbuild.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 |
---|---|
17 | 17 |
18 You should have received a copy of the GNU General Public License | 18 You should have received a copy of the GNU General Public License |
19 along with GCC; see the file COPYING3. If not see | 19 along with GCC; see the file COPYING3. If not see |
20 <http://www.gnu.org/licenses/>. */ | 20 <http://www.gnu.org/licenses/>. */ |
21 | 21 |
22 /* find_basic_blocks divides the current function's rtl into basic | |
23 blocks and constructs the CFG. The blocks are recorded in the | |
24 basic_block_info array; the CFG exists in the edge structures | |
25 referenced by the blocks. | |
26 | |
27 find_basic_blocks also finds any unreachable loops and deletes them. | |
28 | |
29 Available functionality: | |
30 - CFG construction | |
31 find_basic_blocks */ | |
32 | 22 |
33 #include "config.h" | 23 #include "config.h" |
34 #include "system.h" | 24 #include "system.h" |
35 #include "coretypes.h" | 25 #include "coretypes.h" |
36 #include "tm.h" | 26 #include "tm.h" |
41 #include "regs.h" | 31 #include "regs.h" |
42 #include "flags.h" | 32 #include "flags.h" |
43 #include "output.h" | 33 #include "output.h" |
44 #include "function.h" | 34 #include "function.h" |
45 #include "except.h" | 35 #include "except.h" |
36 #include "expr.h" | |
46 #include "toplev.h" | 37 #include "toplev.h" |
47 #include "timevar.h" | 38 #include "timevar.h" |
48 | 39 |
49 static int count_basic_blocks (const_rtx); | |
50 static void find_basic_blocks_1 (rtx); | |
51 static void make_edges (basic_block, basic_block, int); | 40 static void make_edges (basic_block, basic_block, int); |
52 static void make_label_edge (sbitmap, basic_block, rtx, int); | 41 static void make_label_edge (sbitmap, basic_block, rtx, int); |
53 static void find_bb_boundaries (basic_block); | 42 static void find_bb_boundaries (basic_block); |
54 static void compute_outgoing_frequencies (basic_block); | 43 static void compute_outgoing_frequencies (basic_block); |
55 | 44 |
72 return (GET_CODE (PATTERN (insn)) != ADDR_VEC | 61 return (GET_CODE (PATTERN (insn)) != ADDR_VEC |
73 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC); | 62 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC); |
74 | 63 |
75 case CALL_INSN: | 64 case CALL_INSN: |
76 case INSN: | 65 case INSN: |
66 case DEBUG_INSN: | |
77 return true; | 67 return true; |
78 | 68 |
79 case BARRIER: | 69 case BARRIER: |
80 case NOTE: | 70 case NOTE: |
81 return false; | 71 return false; |
89 the basic block. */ | 79 the basic block. */ |
90 | 80 |
91 bool | 81 bool |
92 control_flow_insn_p (const_rtx insn) | 82 control_flow_insn_p (const_rtx insn) |
93 { | 83 { |
94 rtx note; | |
95 | |
96 switch (GET_CODE (insn)) | 84 switch (GET_CODE (insn)) |
97 { | 85 { |
98 case NOTE: | 86 case NOTE: |
99 case CODE_LABEL: | 87 case CODE_LABEL: |
88 case DEBUG_INSN: | |
100 return false; | 89 return false; |
101 | 90 |
102 case JUMP_INSN: | 91 case JUMP_INSN: |
103 /* Jump insn always causes control transfer except for tablejumps. */ | 92 /* Jump insn always causes control transfer except for tablejumps. */ |
104 return (GET_CODE (PATTERN (insn)) != ADDR_VEC | 93 return (GET_CODE (PATTERN (insn)) != ADDR_VEC |
109 (but only if they happen unconditionally). */ | 98 (but only if they happen unconditionally). */ |
110 if ((SIBLING_CALL_P (insn) | 99 if ((SIBLING_CALL_P (insn) |
111 || find_reg_note (insn, REG_NORETURN, 0)) | 100 || find_reg_note (insn, REG_NORETURN, 0)) |
112 && GET_CODE (PATTERN (insn)) != COND_EXEC) | 101 && GET_CODE (PATTERN (insn)) != COND_EXEC) |
113 return true; | 102 return true; |
103 | |
114 /* Call insn may return to the nonlocal goto handler. */ | 104 /* Call insn may return to the nonlocal goto handler. */ |
115 return ((nonlocal_goto_handler_labels | 105 if (can_nonlocal_goto (insn)) |
116 && (0 == (note = find_reg_note (insn, REG_EH_REGION, | 106 return true; |
117 NULL_RTX)) | 107 break; |
118 || INTVAL (XEXP (note, 0)) >= 0)) | |
119 /* Or may trap. */ | |
120 || can_throw_internal (insn)); | |
121 | 108 |
122 case INSN: | 109 case INSN: |
123 /* Treat trap instructions like noreturn calls (same provision). */ | 110 /* Treat trap instructions like noreturn calls (same provision). */ |
124 if (GET_CODE (PATTERN (insn)) == TRAP_IF | 111 if (GET_CODE (PATTERN (insn)) == TRAP_IF |
125 && XEXP (PATTERN (insn), 0) == const1_rtx) | 112 && XEXP (PATTERN (insn), 0) == const1_rtx) |
126 return true; | 113 return true; |
127 | 114 if (!flag_non_call_exceptions) |
128 return (flag_non_call_exceptions && can_throw_internal (insn)); | 115 return false; |
116 break; | |
129 | 117 |
130 case BARRIER: | 118 case BARRIER: |
131 /* It is nonsense to reach barrier when looking for the | 119 /* It is nonsense to reach barrier when looking for the |
132 end of basic block, but before dead code is eliminated | 120 end of basic block, but before dead code is eliminated |
133 this may happen. */ | 121 this may happen. */ |
134 return false; | 122 return false; |
135 | 123 |
136 default: | 124 default: |
137 gcc_unreachable (); | 125 gcc_unreachable (); |
138 } | 126 } |
139 } | 127 |
140 | 128 return can_throw_internal (insn); |
141 /* Count the basic blocks of the function. */ | 129 } |
142 | 130 |
143 static int | |
144 count_basic_blocks (const_rtx f) | |
145 { | |
146 int count = NUM_FIXED_BLOCKS; | |
147 bool saw_insn = false; | |
148 const_rtx insn; | |
149 | |
150 for (insn = f; insn; insn = NEXT_INSN (insn)) | |
151 { | |
152 /* Code labels and barriers causes current basic block to be | |
153 terminated at previous real insn. */ | |
154 if ((LABEL_P (insn) || BARRIER_P (insn)) | |
155 && saw_insn) | |
156 count++, saw_insn = false; | |
157 | |
158 /* Start basic block if needed. */ | |
159 if (!saw_insn && inside_basic_block_p (insn)) | |
160 saw_insn = true; | |
161 | |
162 /* Control flow insn causes current basic block to be terminated. */ | |
163 if (saw_insn && control_flow_insn_p (insn)) | |
164 count++, saw_insn = false; | |
165 } | |
166 | |
167 if (saw_insn) | |
168 count++; | |
169 | |
170 /* The rest of the compiler works a bit smoother when we don't have to | |
171 check for the edge case of do-nothing functions with no basic blocks. */ | |
172 if (count == NUM_FIXED_BLOCKS) | |
173 { | |
174 emit_use (const0_rtx); | |
175 count = NUM_FIXED_BLOCKS + 1; | |
176 } | |
177 | |
178 return count; | |
179 } | |
180 | 131 |
181 /* Create an edge between two basic blocks. FLAGS are auxiliary information | 132 /* Create an edge between two basic blocks. FLAGS are auxiliary information |
182 about the edge that is accumulated between calls. */ | 133 about the edge that is accumulated between calls. */ |
183 | 134 |
184 /* Create an edge from a basic block to a label. */ | 135 /* Create an edge from a basic block to a label. */ |
202 /* Create the edges generated by INSN in REGION. */ | 153 /* Create the edges generated by INSN in REGION. */ |
203 | 154 |
204 void | 155 void |
205 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn) | 156 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn) |
206 { | 157 { |
207 int is_call = CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0; | 158 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn); |
208 rtx handlers, i; | 159 |
209 | 160 if (lp) |
210 handlers = reachable_handlers (insn); | 161 { |
211 | 162 rtx label = lp->landing_pad; |
212 for (i = handlers; i; i = XEXP (i, 1)) | 163 |
213 make_label_edge (edge_cache, src, XEXP (i, 0), | 164 /* During initial rtl generation, use the post_landing_pad. */ |
214 EDGE_ABNORMAL | EDGE_EH | is_call); | 165 if (label == NULL) |
215 | 166 { |
216 free_INSN_LIST_list (&handlers); | 167 gcc_assert (lp->post_landing_pad); |
168 label = label_rtx (lp->post_landing_pad); | |
169 } | |
170 | |
171 make_label_edge (edge_cache, src, label, | |
172 EDGE_ABNORMAL | EDGE_EH | |
173 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0)); | |
174 } | |
217 } | 175 } |
218 | 176 |
219 /* States of basic block as seen by find_many_sub_basic_blocks. */ | 177 /* States of basic block as seen by find_many_sub_basic_blocks. */ |
220 enum state { | 178 enum state { |
221 /* Basic blocks created via split_block belong to this state. | 179 /* Basic blocks created via split_block belong to this state. |
300 /* A branch. */ | 258 /* A branch. */ |
301 if (code == JUMP_INSN) | 259 if (code == JUMP_INSN) |
302 { | 260 { |
303 rtx tmp; | 261 rtx tmp; |
304 | 262 |
305 /* Recognize exception handling placeholders. */ | |
306 if (GET_CODE (PATTERN (insn)) == RESX) | |
307 rtl_make_eh_edge (edge_cache, bb, insn); | |
308 | |
309 /* Recognize a non-local goto as a branch outside the | 263 /* Recognize a non-local goto as a branch outside the |
310 current function. */ | 264 current function. */ |
311 else if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX)) | 265 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX)) |
312 ; | 266 ; |
313 | 267 |
314 /* Recognize a tablejump and do the right thing. */ | 268 /* Recognize a tablejump and do the right thing. */ |
315 else if (tablejump_p (insn, NULL, &tmp)) | 269 else if (tablejump_p (insn, NULL, &tmp)) |
316 { | 270 { |
347 | 301 |
348 /* Returns create an exit out. */ | 302 /* Returns create an exit out. */ |
349 else if (returnjump_p (insn)) | 303 else if (returnjump_p (insn)) |
350 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0); | 304 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0); |
351 | 305 |
306 /* Recognize asm goto and do the right thing. */ | |
307 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL) | |
308 { | |
309 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp); | |
310 for (i = 0; i < n; ++i) | |
311 make_label_edge (edge_cache, bb, | |
312 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0); | |
313 } | |
314 | |
352 /* Otherwise, we have a plain conditional or unconditional jump. */ | 315 /* Otherwise, we have a plain conditional or unconditional jump. */ |
353 else | 316 else |
354 { | 317 { |
355 gcc_assert (JUMP_LABEL (insn)); | 318 gcc_assert (JUMP_LABEL (insn)); |
356 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0); | 319 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0); |
380 to tell that certain calls will not do a nonlocal goto. | 343 to tell that certain calls will not do a nonlocal goto. |
381 For example, if the nested functions that do the nonlocal | 344 For example, if the nested functions that do the nonlocal |
382 gotos do not have their addresses taken, then only calls to | 345 gotos do not have their addresses taken, then only calls to |
383 those functions or to other nested functions that use them | 346 those functions or to other nested functions that use them |
384 could possibly do nonlocal gotos. */ | 347 could possibly do nonlocal gotos. */ |
385 | 348 if (can_nonlocal_goto (insn)) |
386 /* We do know that a REG_EH_REGION note with a value less | |
387 than 0 is guaranteed not to perform a non-local goto. */ | |
388 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX); | |
389 | |
390 if (!note || INTVAL (XEXP (note, 0)) >= 0) | |
391 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1)) | 349 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1)) |
392 make_label_edge (edge_cache, bb, XEXP (x, 0), | 350 make_label_edge (edge_cache, bb, XEXP (x, 0), |
393 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL); | 351 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL); |
394 } | 352 } |
395 } | 353 } |
414 } | 372 } |
415 } | 373 } |
416 | 374 |
417 if (edge_cache) | 375 if (edge_cache) |
418 sbitmap_vector_free (edge_cache); | 376 sbitmap_vector_free (edge_cache); |
419 } | |
420 | |
421 /* Find all basic blocks of the function whose first insn is F. | |
422 | |
423 Collect and return a list of labels whose addresses are taken. This | |
424 will be used in make_edges for use with computed gotos. */ | |
425 | |
426 static void | |
427 find_basic_blocks_1 (rtx f) | |
428 { | |
429 rtx insn, next; | |
430 rtx bb_note = NULL_RTX; | |
431 rtx head = NULL_RTX; | |
432 rtx end = NULL_RTX; | |
433 basic_block prev = ENTRY_BLOCK_PTR; | |
434 | |
435 /* We process the instructions in a slightly different way than we did | |
436 previously. This is so that we see a NOTE_BASIC_BLOCK after we have | |
437 closed out the previous block, so that it gets attached at the proper | |
438 place. Since this form should be equivalent to the previous, | |
439 count_basic_blocks continues to use the old form as a check. */ | |
440 | |
441 for (insn = f; insn; insn = next) | |
442 { | |
443 enum rtx_code code = GET_CODE (insn); | |
444 | |
445 next = NEXT_INSN (insn); | |
446 | |
447 if ((LABEL_P (insn) || BARRIER_P (insn)) | |
448 && head) | |
449 { | |
450 prev = create_basic_block_structure (head, end, bb_note, prev); | |
451 head = end = NULL_RTX; | |
452 bb_note = NULL_RTX; | |
453 } | |
454 | |
455 if (inside_basic_block_p (insn)) | |
456 { | |
457 if (head == NULL_RTX) | |
458 head = insn; | |
459 end = insn; | |
460 } | |
461 | |
462 if (head && control_flow_insn_p (insn)) | |
463 { | |
464 prev = create_basic_block_structure (head, end, bb_note, prev); | |
465 head = end = NULL_RTX; | |
466 bb_note = NULL_RTX; | |
467 } | |
468 | |
469 switch (code) | |
470 { | |
471 case NOTE: | |
472 /* Look for basic block notes with which to keep the | |
473 basic_block_info pointers stable. Unthread the note now; | |
474 we'll put it back at the right place in create_basic_block. | |
475 Or not at all if we've already found a note in this block. */ | |
476 if (NOTE_INSN_BASIC_BLOCK_P (insn)) | |
477 { | |
478 if (bb_note == NULL_RTX) | |
479 bb_note = insn; | |
480 else | |
481 next = delete_insn (insn); | |
482 } | |
483 break; | |
484 | |
485 case CODE_LABEL: | |
486 case JUMP_INSN: | |
487 case CALL_INSN: | |
488 case INSN: | |
489 case BARRIER: | |
490 break; | |
491 | |
492 default: | |
493 gcc_unreachable (); | |
494 } | |
495 } | |
496 | |
497 if (head != NULL_RTX) | |
498 create_basic_block_structure (head, end, bb_note, prev); | |
499 else if (bb_note) | |
500 delete_insn (bb_note); | |
501 | |
502 gcc_assert (last_basic_block == n_basic_blocks); | |
503 | |
504 clear_aux_for_blocks (); | |
505 } | |
506 | |
507 | |
508 /* Find basic blocks of the current function. | |
509 F is the first insn of the function. */ | |
510 | |
511 void | |
512 find_basic_blocks (rtx f) | |
513 { | |
514 basic_block bb; | |
515 | |
516 timevar_push (TV_CFG); | |
517 | |
518 /* Flush out existing data. */ | |
519 if (basic_block_info != NULL) | |
520 { | |
521 clear_edges (); | |
522 | |
523 /* Clear bb->aux on all extant basic blocks. We'll use this as a | |
524 tag for reuse during create_basic_block, just in case some pass | |
525 copies around basic block notes improperly. */ | |
526 FOR_EACH_BB (bb) | |
527 bb->aux = NULL; | |
528 | |
529 basic_block_info = NULL; | |
530 } | |
531 | |
532 n_basic_blocks = count_basic_blocks (f); | |
533 last_basic_block = NUM_FIXED_BLOCKS; | |
534 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR; | |
535 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR; | |
536 | |
537 | |
538 /* Size the basic block table. The actual structures will be allocated | |
539 by find_basic_blocks_1, since we want to keep the structure pointers | |
540 stable across calls to find_basic_blocks. */ | |
541 /* ??? This whole issue would be much simpler if we called find_basic_blocks | |
542 exactly once, and thereafter we don't have a single long chain of | |
543 instructions at all until close to the end of compilation when we | |
544 actually lay them out. */ | |
545 | |
546 basic_block_info = VEC_alloc (basic_block, gc, n_basic_blocks); | |
547 VEC_safe_grow_cleared (basic_block, gc, basic_block_info, n_basic_blocks); | |
548 SET_BASIC_BLOCK (ENTRY_BLOCK, ENTRY_BLOCK_PTR); | |
549 SET_BASIC_BLOCK (EXIT_BLOCK, EXIT_BLOCK_PTR); | |
550 | |
551 find_basic_blocks_1 (f); | |
552 | |
553 profile_status = PROFILE_ABSENT; | |
554 | |
555 /* Tell make_edges to examine every block for out-going edges. */ | |
556 FOR_EACH_BB (bb) | |
557 SET_STATE (bb, BLOCK_NEW); | |
558 | |
559 /* Discover the edges of our cfg. */ | |
560 make_edges (ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, 0); | |
561 | |
562 /* Do very simple cleanup now, for the benefit of code that runs between | |
563 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */ | |
564 tidy_fallthru_edges (); | |
565 | |
566 #ifdef ENABLE_CHECKING | |
567 verify_flow_info (); | |
568 #endif | |
569 timevar_pop (TV_CFG); | |
570 } | 377 } |
571 | 378 |
572 static void | 379 static void |
573 mark_tablejump_edge (rtx label) | 380 mark_tablejump_edge (rtx label) |
574 { | 381 { |
644 /* Scan insn chain and try to find new basic block boundaries. */ | 451 /* Scan insn chain and try to find new basic block boundaries. */ |
645 while (1) | 452 while (1) |
646 { | 453 { |
647 enum rtx_code code = GET_CODE (insn); | 454 enum rtx_code code = GET_CODE (insn); |
648 | 455 |
649 /* On code label, split current basic block. */ | 456 /* In case we've previously seen an insn that effects a control |
650 if (code == CODE_LABEL) | 457 flow transfer, split the block. */ |
458 if ((flow_transfer_insn || code == CODE_LABEL) | |
459 && inside_basic_block_p (insn)) | |
651 { | 460 { |
652 fallthru = split_block (bb, PREV_INSN (insn)); | 461 fallthru = split_block (bb, PREV_INSN (insn)); |
653 if (flow_transfer_insn) | 462 if (flow_transfer_insn) |
654 { | 463 { |
655 BB_END (bb) = flow_transfer_insn; | 464 BB_END (bb) = flow_transfer_insn; |
663 } | 472 } |
664 | 473 |
665 bb = fallthru->dest; | 474 bb = fallthru->dest; |
666 remove_edge (fallthru); | 475 remove_edge (fallthru); |
667 flow_transfer_insn = NULL_RTX; | 476 flow_transfer_insn = NULL_RTX; |
668 if (LABEL_ALT_ENTRY_P (insn)) | 477 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn)) |
669 make_edge (ENTRY_BLOCK_PTR, bb, 0); | 478 make_edge (ENTRY_BLOCK_PTR, bb, 0); |
670 } | 479 } |
671 | 480 else if (code == BARRIER) |
672 /* In case we've previously seen an insn that effects a control | 481 { |
673 flow transfer, split the block. */ | 482 /* __builtin_unreachable () may cause a barrier to be emitted in |
674 if (flow_transfer_insn && inside_basic_block_p (insn)) | 483 the middle of a BB. We need to split it in the same manner as |
675 { | 484 if the barrier were preceded by a control_flow_insn_p insn. */ |
676 fallthru = split_block (bb, PREV_INSN (insn)); | 485 if (!flow_transfer_insn) |
677 BB_END (bb) = flow_transfer_insn; | 486 flow_transfer_insn = prev_nonnote_insn_bb (insn); |
678 | |
679 /* Clean up the bb field for the insns between the blocks. */ | |
680 for (x = NEXT_INSN (flow_transfer_insn); | |
681 x != BB_HEAD (fallthru->dest); | |
682 x = NEXT_INSN (x)) | |
683 if (!BARRIER_P (x)) | |
684 set_block_for_insn (x, NULL); | |
685 | |
686 bb = fallthru->dest; | |
687 remove_edge (fallthru); | |
688 flow_transfer_insn = NULL_RTX; | |
689 } | 487 } |
690 | 488 |
691 if (control_flow_insn_p (insn)) | 489 if (control_flow_insn_p (insn)) |
692 flow_transfer_insn = insn; | 490 flow_transfer_insn = insn; |
693 if (insn == end) | 491 if (insn == end) |