Mercurial > hg > CbC > CbC_gcc
comparison gcc/gcse.c @ 63:b7f97abdc517 gcc-4.6-20100522
update gcc from gcc-4.5.0 to gcc-4.6
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 24 May 2010 12:47:05 +0900 |
parents | 77e2b8dfacca |
children | f6334be47118 |
comparison
equal
deleted
inserted
replaced
56:3c8a44c06a95 | 63:b7f97abdc517 |
---|---|
1 /* Global common subexpression elimination/Partial redundancy elimination | 1 /* Global common subexpression elimination/Partial redundancy elimination |
2 and global constant/copy propagation for GNU compiler. | 2 and global constant/copy propagation for GNU compiler. |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, | 3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
4 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | 4 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
5 | 5 |
6 This file is part of GCC. | 6 This file is part of GCC. |
7 | 7 |
8 GCC is free software; you can redistribute it and/or modify it under | 8 GCC is free software; you can redistribute it and/or modify it under |
9 the terms of the GNU General Public License as published by the Free | 9 the terms of the GNU General Public License as published by the Free |
149 #include "tree.h" | 149 #include "tree.h" |
150 #include "tm_p.h" | 150 #include "tm_p.h" |
151 #include "regs.h" | 151 #include "regs.h" |
152 #include "hard-reg-set.h" | 152 #include "hard-reg-set.h" |
153 #include "flags.h" | 153 #include "flags.h" |
154 #include "real.h" | |
155 #include "insn-config.h" | 154 #include "insn-config.h" |
156 #include "recog.h" | 155 #include "recog.h" |
157 #include "basic-block.h" | 156 #include "basic-block.h" |
158 #include "output.h" | 157 #include "output.h" |
159 #include "function.h" | 158 #include "function.h" |
169 #include "hashtab.h" | 168 #include "hashtab.h" |
170 #include "df.h" | 169 #include "df.h" |
171 #include "dbgcnt.h" | 170 #include "dbgcnt.h" |
172 #include "target.h" | 171 #include "target.h" |
173 | 172 |
174 /* Propagate flow information through back edges and thus enable PRE's | |
175 moving loop invariant calculations out of loops. | |
176 | |
177 Originally this tended to create worse overall code, but several | |
178 improvements during the development of PRE seem to have made following | |
179 back edges generally a win. | |
180 | |
181 Note much of the loop invariant code motion done here would normally | |
182 be done by loop.c, which has more heuristics for when to move invariants | |
183 out of loops. At some point we might need to move some of those | |
184 heuristics into gcse.c. */ | |
185 | |
186 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations | 173 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations |
187 are a superset of those done by GCSE. | 174 are a superset of those done by classic GCSE. |
188 | 175 |
189 We perform the following steps: | 176 We perform the following steps: |
190 | 177 |
191 1) Compute table of places where registers are set. | 178 1) Compute table of places where registers are set. |
192 | 179 |
196 for size, or code hoisting if we are. | 183 for size, or code hoisting if we are. |
197 | 184 |
198 4) Perform another pass of copy/constant propagation. Try to bypass | 185 4) Perform another pass of copy/constant propagation. Try to bypass |
199 conditional jumps if the condition can be computed from a value of | 186 conditional jumps if the condition can be computed from a value of |
200 an incoming edge. | 187 an incoming edge. |
201 | |
202 5) Perform store motion. | |
203 | 188 |
204 Two passes of copy/constant propagation are done because the first one | 189 Two passes of copy/constant propagation are done because the first one |
205 enables more GCSE and the second one helps to clean up the copies that | 190 enables more GCSE and the second one helps to clean up the copies that |
206 GCSE creates. This is needed more for PRE than for Classic because Classic | 191 GCSE creates. This is needed more for PRE than for Classic because Classic |
207 GCSE will try to use an existing register containing the common | 192 GCSE will try to use an existing register containing the common |
210 | 195 |
211 Expressions we are interested in GCSE-ing are of the form | 196 Expressions we are interested in GCSE-ing are of the form |
212 (set (pseudo-reg) (expression)). | 197 (set (pseudo-reg) (expression)). |
213 Function want_to_gcse_p says what these are. | 198 Function want_to_gcse_p says what these are. |
214 | 199 |
215 In addition, expressions in REG_EQUAL notes are candidates for GXSE-ing. | 200 In addition, expressions in REG_EQUAL notes are candidates for GCSE-ing. |
216 This allows PRE to hoist expressions that are expressed in multiple insns, | 201 This allows PRE to hoist expressions that are expressed in multiple insns, |
217 such as comprex address calculations (e.g. for PIC code, or loads with a | 202 such as complex address calculations (e.g. for PIC code, or loads with a |
218 high part and as lowe part). | 203 high part and a low part). |
219 | 204 |
220 PRE handles moving invariant expressions out of loops (by treating them as | 205 PRE handles moving invariant expressions out of loops (by treating them as |
221 partially redundant). | 206 partially redundant). |
222 | |
223 Eventually it would be nice to replace cse.c/gcse.c with SSA (static single | |
224 assignment) based GVN (global value numbering). L. T. Simpson's paper | |
225 (Rice University) on value numbering is a useful reference for this. | |
226 | 207 |
227 ********************** | 208 ********************** |
228 | 209 |
229 We used to support multiple passes but there are diminishing returns in | 210 We used to support multiple passes but there are diminishing returns in |
230 doing so. The first pass usually makes 90% of the changes that are doable. | 211 doing so. The first pass usually makes 90% of the changes that are doable. |
269 | 250 |
270 Various papers have argued that PRE DFA is expensive (O(n^2)) and others | 251 Various papers have argued that PRE DFA is expensive (O(n^2)) and others |
271 argue it is not. The number of iterations for the algorithm to converge | 252 argue it is not. The number of iterations for the algorithm to converge |
272 is typically 2-4 so I don't view it as that expensive (relatively speaking). | 253 is typically 2-4 so I don't view it as that expensive (relatively speaking). |
273 | 254 |
274 PRE GCSE depends heavily on the second CSE pass to clean up the copies | 255 PRE GCSE depends heavily on the second CPROP pass to clean up the copies |
275 we create. To make an expression reach the place where it's redundant, | 256 we create. To make an expression reach the place where it's redundant, |
276 the result of the expression is copied to a new register, and the redundant | 257 the result of the expression is copied to a new register, and the redundant |
277 expression is deleted by replacing it with this new register. Classic GCSE | 258 expression is deleted by replacing it with this new register. Classic GCSE |
278 doesn't have this problem as much as it computes the reaching defs of | 259 doesn't have this problem as much as it computes the reaching defs of |
279 each register in each block and thus can try to use an existing | 260 each register in each block and thus can try to use an existing |
641 | 622 |
642 static void | 623 static void |
643 alloc_gcse_mem (void) | 624 alloc_gcse_mem (void) |
644 { | 625 { |
645 /* Allocate vars to track sets of regs. */ | 626 /* Allocate vars to track sets of regs. */ |
646 reg_set_bitmap = BITMAP_ALLOC (NULL); | 627 reg_set_bitmap = ALLOC_REG_SET (NULL); |
647 | 628 |
648 /* Allocate array to keep a list of insns which modify memory in each | 629 /* Allocate array to keep a list of insns which modify memory in each |
649 basic block. */ | 630 basic block. */ |
650 modify_mem_list = GCNEWVEC (rtx, last_basic_block); | 631 modify_mem_list = GCNEWVEC (rtx, last_basic_block); |
651 canon_modify_mem_list = GCNEWVEC (rtx, last_basic_block); | 632 canon_modify_mem_list = GCNEWVEC (rtx, last_basic_block); |
728 /* The occurrences recorded in antic_occr are exactly those that | 709 /* The occurrences recorded in antic_occr are exactly those that |
729 we want to set to nonzero in ANTLOC. */ | 710 we want to set to nonzero in ANTLOC. */ |
730 if (antloc) | 711 if (antloc) |
731 for (occr = expr->antic_occr; occr != NULL; occr = occr->next) | 712 for (occr = expr->antic_occr; occr != NULL; occr = occr->next) |
732 { | 713 { |
733 SET_BIT (antloc[BLOCK_NUM (occr->insn)], indx); | 714 SET_BIT (antloc[BLOCK_FOR_INSN (occr->insn)->index], indx); |
734 | 715 |
735 /* While we're scanning the table, this is a good place to | 716 /* While we're scanning the table, this is a good place to |
736 initialize this. */ | 717 initialize this. */ |
737 occr->deleted_p = 0; | 718 occr->deleted_p = 0; |
738 } | 719 } |
740 /* The occurrences recorded in avail_occr are exactly those that | 721 /* The occurrences recorded in avail_occr are exactly those that |
741 we want to set to nonzero in COMP. */ | 722 we want to set to nonzero in COMP. */ |
742 if (comp) | 723 if (comp) |
743 for (occr = expr->avail_occr; occr != NULL; occr = occr->next) | 724 for (occr = expr->avail_occr; occr != NULL; occr = occr->next) |
744 { | 725 { |
745 SET_BIT (comp[BLOCK_NUM (occr->insn)], indx); | 726 SET_BIT (comp[BLOCK_FOR_INSN (occr->insn)->index], indx); |
746 | 727 |
747 /* While we're scanning the table, this is a good place to | 728 /* While we're scanning the table, this is a good place to |
748 initialize this. */ | 729 initialize this. */ |
749 occr->copied_p = 0; | 730 occr->copied_p = 0; |
750 } | 731 } |
1160 /* Now record the occurrence(s). */ | 1141 /* Now record the occurrence(s). */ |
1161 if (antic_p) | 1142 if (antic_p) |
1162 { | 1143 { |
1163 antic_occr = cur_expr->antic_occr; | 1144 antic_occr = cur_expr->antic_occr; |
1164 | 1145 |
1165 if (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn)) | 1146 if (antic_occr |
1147 && BLOCK_FOR_INSN (antic_occr->insn) != BLOCK_FOR_INSN (insn)) | |
1166 antic_occr = NULL; | 1148 antic_occr = NULL; |
1167 | 1149 |
1168 if (antic_occr) | 1150 if (antic_occr) |
1169 /* Found another instance of the expression in the same basic block. | 1151 /* Found another instance of the expression in the same basic block. |
1170 Prefer the currently recorded one. We want the first one in the | 1152 Prefer the currently recorded one. We want the first one in the |
1184 | 1166 |
1185 if (avail_p) | 1167 if (avail_p) |
1186 { | 1168 { |
1187 avail_occr = cur_expr->avail_occr; | 1169 avail_occr = cur_expr->avail_occr; |
1188 | 1170 |
1189 if (avail_occr && BLOCK_NUM (avail_occr->insn) == BLOCK_NUM (insn)) | 1171 if (avail_occr |
1172 && BLOCK_FOR_INSN (avail_occr->insn) == BLOCK_FOR_INSN (insn)) | |
1190 { | 1173 { |
1191 /* Found another instance of the expression in the same basic block. | 1174 /* Found another instance of the expression in the same basic block. |
1192 Prefer this occurrence to the currently recorded one. We want | 1175 Prefer this occurrence to the currently recorded one. We want |
1193 the last one in the block and the block is scanned from start | 1176 the last one in the block and the block is scanned from start |
1194 to end. */ | 1177 to end. */ |
1257 } | 1240 } |
1258 | 1241 |
1259 /* Now record the occurrence. */ | 1242 /* Now record the occurrence. */ |
1260 cur_occr = cur_expr->avail_occr; | 1243 cur_occr = cur_expr->avail_occr; |
1261 | 1244 |
1262 if (cur_occr && BLOCK_NUM (cur_occr->insn) == BLOCK_NUM (insn)) | 1245 if (cur_occr |
1246 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn)) | |
1263 { | 1247 { |
1264 /* Found another instance of the expression in the same basic block. | 1248 /* Found another instance of the expression in the same basic block. |
1265 Prefer this occurrence to the currently recorded one. We want | 1249 Prefer this occurrence to the currently recorded one. We want |
1266 the last one in the block and the block is scanned from start | 1250 the last one in the block and the block is scanned from start |
1267 to end. */ | 1251 to end. */ |
1590 return; | 1574 return; |
1591 | 1575 |
1592 dest_addr = get_addr (XEXP (dest, 0)); | 1576 dest_addr = get_addr (XEXP (dest, 0)); |
1593 dest_addr = canon_rtx (dest_addr); | 1577 dest_addr = canon_rtx (dest_addr); |
1594 insn = (rtx) v_insn; | 1578 insn = (rtx) v_insn; |
1595 bb = BLOCK_NUM (insn); | 1579 bb = BLOCK_FOR_INSN (insn)->index; |
1596 | 1580 |
1597 canon_modify_mem_list[bb] = | 1581 canon_modify_mem_list[bb] = |
1598 alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]); | 1582 alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]); |
1599 canon_modify_mem_list[bb] = | 1583 canon_modify_mem_list[bb] = |
1600 alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]); | 1584 alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]); |
1605 a CALL_INSN). We merely need to record which insns modify memory. */ | 1589 a CALL_INSN). We merely need to record which insns modify memory. */ |
1606 | 1590 |
1607 static void | 1591 static void |
1608 record_last_mem_set_info (rtx insn) | 1592 record_last_mem_set_info (rtx insn) |
1609 { | 1593 { |
1610 int bb = BLOCK_NUM (insn); | 1594 int bb = BLOCK_FOR_INSN (insn)->index; |
1611 | 1595 |
1612 /* load_killed_in_block_p will handle the case of calls clobbering | 1596 /* load_killed_in_block_p will handle the case of calls clobbering |
1613 everything. */ | 1597 everything. */ |
1614 modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]); | 1598 modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]); |
1615 bitmap_set_bit (modify_mem_list_set, bb); | 1599 bitmap_set_bit (modify_mem_list_set, bb); |
2333 | 2317 |
2334 /* Find a set that is available at the start of the block | 2318 /* Find a set that is available at the start of the block |
2335 which contains INSN. */ | 2319 which contains INSN. */ |
2336 while (set) | 2320 while (set) |
2337 { | 2321 { |
2338 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index)) | 2322 if (TEST_BIT (cprop_avin[BLOCK_FOR_INSN (insn)->index], |
2323 set->bitmap_index)) | |
2339 break; | 2324 break; |
2340 set = next_set (regno, set); | 2325 set = next_set (regno, set); |
2341 } | 2326 } |
2342 | 2327 |
2343 /* If no available set was found we've reached the end of the | 2328 /* If no available set was found we've reached the end of the |
2736 basic_block bb; | 2721 basic_block bb; |
2737 rtx insn; | 2722 rtx insn; |
2738 struct reg_use *reg_used; | 2723 struct reg_use *reg_used; |
2739 bool changed = false; | 2724 bool changed = false; |
2740 | 2725 |
2741 cselib_init (false); | 2726 cselib_init (0); |
2742 FOR_EACH_BB (bb) | 2727 FOR_EACH_BB (bb) |
2743 { | 2728 { |
2744 FOR_BB_INSNS (bb, insn) | 2729 FOR_BB_INSNS (bb, insn) |
2745 { | 2730 { |
2746 if (INSN_P (insn)) | 2731 if (INSN_P (insn)) |
3491 of exception handling. */ | 3476 of exception handling. */ |
3492 else if (CALL_P (insn) | 3477 else if (CALL_P (insn) |
3493 && (!single_succ_p (bb) | 3478 && (!single_succ_p (bb) |
3494 || single_succ_edge (bb)->flags & EDGE_ABNORMAL)) | 3479 || single_succ_edge (bb)->flags & EDGE_ABNORMAL)) |
3495 { | 3480 { |
3496 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers, | 3481 /* Keeping in mind targets with small register classes and parameters |
3497 we search backward and place the instructions before the first | 3482 in registers, we search backward and place the instructions before |
3498 parameter is loaded. Do this for everyone for consistency and a | 3483 the first parameter is loaded. Do this for everyone for consistency |
3499 presumption that we'll get better code elsewhere as well. | 3484 and a presumption that we'll get better code elsewhere as well. |
3500 | 3485 |
3501 It should always be the case that we can put these instructions | 3486 It should always be the case that we can put these instructions |
3502 anywhere in the basic block with performing PRE optimizations. | 3487 anywhere in the basic block with performing PRE optimizations. |
3503 Check this. */ | 3488 Check this. */ |
3504 | 3489 |
3726 gcse_create_count++; | 3711 gcse_create_count++; |
3727 | 3712 |
3728 if (dump_file) | 3713 if (dump_file) |
3729 fprintf (dump_file, | 3714 fprintf (dump_file, |
3730 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n", | 3715 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n", |
3731 BLOCK_NUM (insn), INSN_UID (new_insn), indx, | 3716 BLOCK_FOR_INSN (insn)->index, INSN_UID (new_insn), indx, |
3732 INSN_UID (insn), regno); | 3717 INSN_UID (insn), regno); |
3733 } | 3718 } |
3734 | 3719 |
3735 /* Copy available expressions that reach the redundant expression | 3720 /* Copy available expressions that reach the redundant expression |
3736 to `reaching_reg'. */ | 3721 to `reaching_reg'. */ |
5072 | 5057 |
5073 static unsigned int | 5058 static unsigned int |
5074 execute_rtl_cprop (void) | 5059 execute_rtl_cprop (void) |
5075 { | 5060 { |
5076 delete_unreachable_blocks (); | 5061 delete_unreachable_blocks (); |
5077 df_note_add_problem (); | |
5078 df_set_flags (DF_LR_RUN_DCE); | 5062 df_set_flags (DF_LR_RUN_DCE); |
5079 df_analyze (); | 5063 df_analyze (); |
5080 flag_rerun_cse_after_global_opts |= one_cprop_pass (); | 5064 flag_rerun_cse_after_global_opts |= one_cprop_pass (); |
5081 return 0; | 5065 return 0; |
5082 } | 5066 } |
5092 | 5076 |
5093 static unsigned int | 5077 static unsigned int |
5094 execute_rtl_pre (void) | 5078 execute_rtl_pre (void) |
5095 { | 5079 { |
5096 delete_unreachable_blocks (); | 5080 delete_unreachable_blocks (); |
5097 df_note_add_problem (); | |
5098 df_analyze (); | 5081 df_analyze (); |
5099 flag_rerun_cse_after_global_opts |= one_pre_gcse_pass (); | 5082 flag_rerun_cse_after_global_opts |= one_pre_gcse_pass (); |
5100 return 0; | 5083 return 0; |
5101 } | 5084 } |
5102 | 5085 |
5114 | 5097 |
5115 static unsigned int | 5098 static unsigned int |
5116 execute_rtl_hoist (void) | 5099 execute_rtl_hoist (void) |
5117 { | 5100 { |
5118 delete_unreachable_blocks (); | 5101 delete_unreachable_blocks (); |
5119 df_note_add_problem (); | |
5120 df_analyze (); | 5102 df_analyze (); |
5121 flag_rerun_cse_after_global_opts |= one_code_hoisting_pass (); | 5103 flag_rerun_cse_after_global_opts |= one_code_hoisting_pass (); |
5122 return 0; | 5104 return 0; |
5123 } | 5105 } |
5124 | 5106 |