]>
Commit | Line | Data |
---|---|---|
28dcb9ed | 1 | /* Data flow analysis for GNU compiler. |
f4ac4e51 | 2 | Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc. |
28dcb9ed | 3 | |
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
0355838f | 18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
28dcb9ed | 20 | |
21 | ||
22 | /* This file contains the data flow analysis pass of the compiler. | |
23 | It computes data flow information | |
24 | which tells combine_instructions which insns to consider combining | |
25 | and controls register allocation. | |
26 | ||
27 | Additional data flow information that is too bulky to record | |
28 | is generated during the analysis, and is used at that time to | |
29 | create autoincrement and autodecrement addressing. | |
30 | ||
31 | The first step is dividing the function into basic blocks. | |
32 | find_basic_blocks does this. Then life_analysis determines | |
33 | where each register is live and where it is dead. | |
34 | ||
35 | ** find_basic_blocks ** | |
36 | ||
37 | find_basic_blocks divides the current function's rtl | |
38 | into basic blocks. It records the beginnings and ends of the | |
39 | basic blocks in the vectors basic_block_head and basic_block_end, | |
40 | and the number of blocks in n_basic_blocks. | |
41 | ||
42 | find_basic_blocks also finds any unreachable loops | |
43 | and deletes them. | |
44 | ||
45 | ** life_analysis ** | |
46 | ||
47 | life_analysis is called immediately after find_basic_blocks. | |
48 | It uses the basic block information to determine where each | |
49 | hard or pseudo register is live. | |
50 | ||
51 | ** live-register info ** | |
52 | ||
53 | The information about where each register is live is in two parts: | |
54 | the REG_NOTES of insns, and the vector basic_block_live_at_start. | |
55 | ||
56 | basic_block_live_at_start has an element for each basic block, | |
57 | and the element is a bit-vector with a bit for each hard or pseudo | |
58 | register. The bit is 1 if the register is live at the beginning | |
59 | of the basic block. | |
60 | ||
61 | Two types of elements can be added to an insn's REG_NOTES. | |
62 | A REG_DEAD note is added to an insn's REG_NOTES for any register | |
63 | that meets both of two conditions: The value in the register is not | |
64 | needed in subsequent insns and the insn does not replace the value in | |
65 | the register (in the case of multi-word hard registers, the value in | |
66 | each register must be replaced by the insn to avoid a REG_DEAD note). | |
67 | ||
68 | In the vast majority of cases, an object in a REG_DEAD note will be | |
69 | used somewhere in the insn. The (rare) exception to this is if an | |
70 | insn uses a multi-word hard register and only some of the registers are | |
71 | needed in subsequent insns. In that case, REG_DEAD notes will be | |
72 | provided for those hard registers that are not subsequently needed. | |
73 | Partial REG_DEAD notes of this type do not occur when an insn sets | |
74 | only some of the hard registers used in such a multi-word operand; | |
75 | omitting REG_DEAD notes for objects stored in an insn is optional and | |
76 | the desire to do so does not justify the complexity of the partial | |
77 | REG_DEAD notes. | |
78 | ||
79 | REG_UNUSED notes are added for each register that is set by the insn | |
80 | but is unused subsequently (if every register set by the insn is unused | |
81 | and the insn does not reference memory or have some other side-effect, | |
82 | the insn is deleted instead). If only part of a multi-word hard | |
83 | register is used in a subsequent insn, REG_UNUSED notes are made for | |
84 | the parts that will not be used. | |
85 | ||
86 | To determine which registers are live after any insn, one can | |
87 | start from the beginning of the basic block and scan insns, noting | |
88 | which registers are set by each insn and which die there. | |
89 | ||
90 | ** Other actions of life_analysis ** | |
91 | ||
92 | life_analysis sets up the LOG_LINKS fields of insns because the | |
93 | information needed to do so is readily available. | |
94 | ||
95 | life_analysis deletes insns whose only effect is to store a value | |
96 | that is never used. | |
97 | ||
98 | life_analysis notices cases where a reference to a register as | |
99 | a memory address can be combined with a preceding or following | |
100 | incrementation or decrementation of the register. The separate | |
101 | instruction to increment or decrement is deleted and the address | |
102 | is changed to a POST_INC or similar rtx. | |
103 | ||
104 | Each time an incrementing or decrementing address is created, | |
105 | a REG_INC element is added to the insn's REG_NOTES list. | |
106 | ||
107 | life_analysis fills in certain vectors containing information about | |
108 | register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length, | |
22c8984a | 109 | reg_n_calls_crosses and reg_basic_block. |
110 | ||
111 | life_analysis sets current_function_sp_is_unchanging if the function | |
112 | doesn't modify the stack pointer. */ | |
28dcb9ed | 113 | \f |
28dcb9ed | 114 | #include "config.h" |
405711de | 115 | #include "system.h" |
28dcb9ed | 116 | #include "rtl.h" |
117 | #include "basic-block.h" | |
118 | #include "insn-config.h" | |
119 | #include "regs.h" | |
120 | #include "hard-reg-set.h" | |
121 | #include "flags.h" | |
122 | #include "output.h" | |
037a5228 | 123 | #include "except.h" |
ce1fd7fc | 124 | #include "toplev.h" |
ba1c8484 | 125 | #include "recog.h" |
28dcb9ed | 126 | |
127 | #include "obstack.h" | |
128 | #define obstack_chunk_alloc xmalloc | |
129 | #define obstack_chunk_free free | |
130 | ||
22548064 | 131 | #define XNMALLOC(TYPE, COUNT) ((TYPE *) xmalloc ((COUNT) * sizeof (TYPE))) |
132 | ||
fc1ef759 | 133 | /* The contents of the current function definition are allocated |
134 | in this obstack, and all are freed at the end of the function. | |
135 | For top-level functions, this is temporary_obstack. | |
136 | Separate obstacks are made for nested functions. */ | |
137 | ||
138 | extern struct obstack *function_obstack; | |
139 | ||
28dcb9ed | 140 | /* List of labels that must never be deleted. */ |
141 | extern rtx forced_labels; | |
142 | ||
143 | /* Get the basic block number of an insn. | |
144 | This info should not be expected to remain available | |
145 | after the end of life_analysis. */ | |
146 | ||
147 | /* This is the limit of the allocated space in the following two arrays. */ | |
148 | ||
149 | static int max_uid_for_flow; | |
150 | ||
151 | #define BLOCK_NUM(INSN) uid_block_number[INSN_UID (INSN)] | |
152 | ||
153 | /* This is where the BLOCK_NUM values are really stored. | |
154 | This is set up by find_basic_blocks and used there and in life_analysis, | |
155 | and then freed. */ | |
156 | ||
61e82936 | 157 | int *uid_block_number; |
28dcb9ed | 158 | |
159 | /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */ | |
160 | ||
161 | #define INSN_VOLATILE(INSN) uid_volatile[INSN_UID (INSN)] | |
162 | static char *uid_volatile; | |
163 | ||
1d2be6a7 | 164 | /* Nonzero if the second flow pass has completed. */ |
165 | int flow2_completed; | |
166 | ||
28dcb9ed | 167 | /* Number of basic blocks in the current function. */ |
168 | ||
169 | int n_basic_blocks; | |
170 | ||
171 | /* Maximum register number used in this function, plus one. */ | |
172 | ||
173 | int max_regno; | |
174 | ||
394685a4 | 175 | /* Indexed by n, giving various register information */ |
28dcb9ed | 176 | |
d6ff8d83 | 177 | varray_type reg_n_info; |
28dcb9ed | 178 | |
485205d1 | 179 | /* Size of the reg_n_info table. */ |
180 | ||
181 | unsigned int reg_n_max; | |
182 | ||
28dcb9ed | 183 | /* Element N is the next insn that uses (hard or pseudo) register number N |
184 | within the current basic block; or zero, if there is no such insn. | |
185 | This is valid only during the final backward scan in propagate_block. */ | |
186 | ||
187 | static rtx *reg_next_use; | |
188 | ||
189 | /* Size of a regset for the current function, | |
190 | in (1) bytes and (2) elements. */ | |
191 | ||
192 | int regset_bytes; | |
193 | int regset_size; | |
194 | ||
195 | /* Element N is first insn in basic block N. | |
196 | This info lasts until we finish compiling the function. */ | |
197 | ||
68676d00 | 198 | rtx *x_basic_block_head; |
28dcb9ed | 199 | |
200 | /* Element N is last insn in basic block N. | |
201 | This info lasts until we finish compiling the function. */ | |
202 | ||
68676d00 | 203 | rtx *x_basic_block_end; |
28dcb9ed | 204 | |
f24e9d92 | 205 | /* Element N indicates whether basic block N can be reached through a |
206 | computed jump. */ | |
207 | ||
208 | char *basic_block_computed_jump_target; | |
209 | ||
28dcb9ed | 210 | /* Element N is a regset describing the registers live |
211 | at the start of basic block N. | |
212 | This info lasts until we finish compiling the function. */ | |
213 | ||
214 | regset *basic_block_live_at_start; | |
215 | ||
216 | /* Regset of regs live when calls to `setjmp'-like functions happen. */ | |
217 | ||
218 | regset regs_live_at_setjmp; | |
219 | ||
220 | /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers | |
221 | that have to go in the same hard reg. | |
222 | The first two regs in the list are a pair, and the next two | |
223 | are another pair, etc. */ | |
224 | rtx regs_may_share; | |
225 | ||
22548064 | 226 | /* Pointer to head of predecessor/successor block list. */ |
227 | static int_list_block *flow_int_list_blocks; | |
228 | ||
229 | /* Element N is the list of successors of basic block N. */ | |
230 | static int_list_ptr *basic_block_succ; | |
28dcb9ed | 231 | |
22548064 | 232 | /* Element N is the list of predecessors of basic block N. */ |
233 | static int_list_ptr *basic_block_pred; | |
28dcb9ed | 234 | |
235 | /* Element N is depth within loops of the last insn in basic block number N. | |
236 | Freed after life_analysis. */ | |
237 | ||
238 | static short *basic_block_loop_depth; | |
239 | ||
28dcb9ed | 240 | /* Depth within loops of basic block being scanned for lifetime analysis, |
241 | plus one. This is the weight attached to references to registers. */ | |
242 | ||
243 | static int loop_depth; | |
244 | ||
245 | /* During propagate_block, this is non-zero if the value of CC0 is live. */ | |
246 | ||
247 | static int cc0_live; | |
248 | ||
d29fc2f4 | 249 | /* During propagate_block, this contains a list of all the MEMs we are |
250 | tracking for dead store elimination. */ | |
28dcb9ed | 251 | |
d29fc2f4 | 252 | static rtx mem_set_list; |
28dcb9ed | 253 | |
254 | /* Set of registers that may be eliminable. These are handled specially | |
255 | in updating regs_ever_live. */ | |
256 | ||
257 | static HARD_REG_SET elim_reg_set; | |
258 | ||
259 | /* Forward declarations */ | |
850cc2fa | 260 | static void find_basic_blocks_1 PROTO((rtx, rtx)); |
22548064 | 261 | static void add_edge PROTO((int, int)); |
262 | static void add_edge_to_label PROTO((int, rtx)); | |
cfc185a4 | 263 | static void make_edges PROTO((int)); |
22548064 | 264 | static void mark_label_ref PROTO((int, rtx)); |
265 | static void delete_unreachable_blocks PROTO((void)); | |
cfc185a4 | 266 | static int delete_block PROTO((int)); |
61e82936 | 267 | static void life_analysis_1 PROTO((rtx, int)); |
a123fe25 | 268 | static void propagate_block PROTO((regset, rtx, rtx, int, |
269 | regset, int)); | |
cfc185a4 | 270 | static int set_noop_p PROTO((rtx)); |
271 | static int noop_move_p PROTO((rtx)); | |
272 | static void record_volatile_insns PROTO((rtx)); | |
273 | static void mark_regs_live_at_end PROTO((regset)); | |
71a522b5 | 274 | static int insn_dead_p PROTO((rtx, regset, int, rtx)); |
a123fe25 | 275 | static int libcall_dead_p PROTO((rtx, regset, rtx, rtx)); |
276 | static void mark_set_regs PROTO((regset, regset, rtx, | |
277 | rtx, regset)); | |
278 | static void mark_set_1 PROTO((regset, regset, rtx, | |
279 | rtx, regset)); | |
99c14947 | 280 | #ifdef AUTO_INC_DEC |
a123fe25 | 281 | static void find_auto_inc PROTO((regset, rtx, rtx)); |
a123fe25 | 282 | static int try_pre_increment_1 PROTO((rtx)); |
283 | static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT)); | |
99c14947 | 284 | #endif |
285 | static void mark_used_regs PROTO((regset, regset, rtx, int, rtx)); | |
a123fe25 | 286 | void dump_flow_info PROTO((FILE *)); |
61e82936 | 287 | static void add_pred_succ PROTO ((int, int, int_list_ptr *, |
288 | int_list_ptr *, int *, int *)); | |
289 | static int_list_ptr alloc_int_list_node PROTO ((int_list_block **)); | |
290 | static int_list_ptr add_int_list_node PROTO ((int_list_block **, | |
291 | int_list **, int)); | |
90a70252 | 292 | static void init_regset_vector PROTO ((regset *, int, |
293 | struct obstack *)); | |
23f5e759 | 294 | static void count_reg_sets_1 PROTO ((rtx)); |
295 | static void count_reg_sets PROTO ((rtx)); | |
296 | static void count_reg_references PROTO ((rtx)); | |
22c8984a | 297 | static void notice_stack_pointer_modification PROTO ((rtx, rtx)); |
a447bb5e | 298 | static void invalidate_mems_from_autoinc PROTO ((rtx)); |
28dcb9ed | 299 | \f |
61e82936 | 300 | /* Find basic blocks of the current function. |
28dcb9ed | 301 | F is the first insn of the function and NREGS the number of register numbers |
f2017203 | 302 | in use. */ |
28dcb9ed | 303 | |
304 | void | |
053ec0a1 | 305 | find_basic_blocks (f, nregs, file) |
28dcb9ed | 306 | rtx f; |
307 | int nregs; | |
308 | FILE *file; | |
309 | { | |
310 | register rtx insn; | |
311 | register int i; | |
05b74099 | 312 | rtx nonlocal_label_list = nonlocal_label_rtx_list (); |
28dcb9ed | 313 | |
22548064 | 314 | /* Avoid leaking memory if this is called multiple times per compiled |
315 | function. */ | |
316 | free_bb_memory (); | |
317 | ||
28dcb9ed | 318 | /* Count the basic blocks. Also find maximum insn uid value used. */ |
319 | ||
320 | { | |
d2be0e00 | 321 | rtx prev_call = 0; |
28dcb9ed | 322 | register RTX_CODE prev_code = JUMP_INSN; |
323 | register RTX_CODE code; | |
a9ea090e | 324 | int eh_region = 0; |
b067035b | 325 | int call_had_abnormal_edge = 0; |
28dcb9ed | 326 | |
28dcb9ed | 327 | for (insn = f, i = 0; insn; insn = NEXT_INSN (insn)) |
328 | { | |
329 | code = GET_CODE (insn); | |
d2be0e00 | 330 | |
331 | /* A basic block starts at label, or after something that can jump. */ | |
332 | if (code == CODE_LABEL | |
333 | || (GET_RTX_CLASS (code) == 'i' | |
334 | && (prev_code == JUMP_INSN | |
335 | || (prev_code == CALL_INSN && call_had_abnormal_edge) | |
336 | || prev_code == BARRIER))) | |
7bdba5dd | 337 | { |
d2be0e00 | 338 | i++; |
339 | ||
340 | /* If the previous insn was a call that did not create an | |
341 | abnormal edge, we want to add a nop so that the CALL_INSN | |
68676d00 | 342 | itself is not at basic block end. This allows us to easily |
d2be0e00 | 343 | distinguish between normal calls and those which create |
344 | abnormal edges in the flow graph. */ | |
345 | ||
346 | if (i > 0 && !call_had_abnormal_edge && prev_call != 0) | |
7bdba5dd | 347 | { |
d2be0e00 | 348 | rtx nop = gen_rtx_USE (VOIDmode, const0_rtx); |
349 | emit_insn_after (nop, prev_call); | |
7bdba5dd | 350 | } |
351 | } | |
5a8ef220 | 352 | |
d2be0e00 | 353 | if (code == CALL_INSN) |
454c42da | 354 | { |
355 | rtx note = find_reg_note(insn, REG_EH_REGION, NULL_RTX); | |
356 | ||
357 | /* We change the code of the CALL_INSN, so that it won't start a | |
358 | new block. */ | |
359 | if (note && XINT (XEXP (note, 0), 0) == 0) | |
360 | code = INSN; | |
361 | else | |
362 | { | |
363 | prev_call = insn; | |
364 | call_had_abnormal_edge = (nonlocal_label_list != 0 | |
365 | || eh_region); | |
366 | } | |
367 | } | |
368 | ||
d2be0e00 | 369 | else if (code != NOTE && code != BARRIER) |
370 | prev_call = 0; | |
b067035b | 371 | |
f11fd6c8 | 372 | if (code != NOTE) |
28dcb9ed | 373 | prev_code = code; |
a9ea090e | 374 | else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) |
375 | ++eh_region; | |
376 | else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END) | |
377 | --eh_region; | |
28dcb9ed | 378 | } |
379 | } | |
380 | ||
61e82936 | 381 | n_basic_blocks = i; |
382 | ||
d2be0e00 | 383 | max_uid_for_flow = get_max_uid (); |
28dcb9ed | 384 | #ifdef AUTO_INC_DEC |
61e82936 | 385 | /* Leave space for insns life_analysis makes in some cases for auto-inc. |
386 | These cases are rare, so we don't need too much space. */ | |
28dcb9ed | 387 | max_uid_for_flow += max_uid_for_flow / 10; |
388 | #endif | |
389 | ||
390 | /* Allocate some tables that last till end of compiling this function | |
391 | and some needed only in find_basic_blocks and life_analysis. */ | |
392 | ||
68676d00 | 393 | x_basic_block_head = XNMALLOC (rtx, n_basic_blocks); |
394 | x_basic_block_end = XNMALLOC (rtx, n_basic_blocks); | |
22548064 | 395 | basic_block_succ = XNMALLOC (int_list_ptr, n_basic_blocks); |
396 | basic_block_pred = XNMALLOC (int_list_ptr, n_basic_blocks); | |
397 | bzero ((char *)basic_block_succ, n_basic_blocks * sizeof (int_list_ptr)); | |
398 | bzero ((char *)basic_block_pred, n_basic_blocks * sizeof (int_list_ptr)); | |
399 | ||
f24e9d92 | 400 | basic_block_computed_jump_target = (char *) oballoc (n_basic_blocks); |
22548064 | 401 | basic_block_loop_depth = XNMALLOC (short, n_basic_blocks); |
402 | uid_block_number = XNMALLOC (int, (max_uid_for_flow + 1)); | |
403 | uid_volatile = XNMALLOC (char, (max_uid_for_flow + 1)); | |
28dcb9ed | 404 | bzero (uid_volatile, max_uid_for_flow + 1); |
405 | ||
053ec0a1 | 406 | find_basic_blocks_1 (f, nonlocal_label_list); |
28dcb9ed | 407 | } |
61e82936 | 408 | |
cfc185a4 | 409 | /* For communication between find_basic_blocks_1 and its subroutines. */ |
410 | ||
411 | /* An array of CODE_LABELs, indexed by UID for the start of the active | |
412 | EH handler for each insn in F. */ | |
413 | static int *active_eh_region; | |
414 | static int *nested_eh_region; | |
415 | ||
416 | /* Element N nonzero if basic block N can actually be reached. */ | |
417 | ||
418 | static char *block_live_static; | |
419 | ||
420 | /* List of label_refs to all labels whose addresses are taken | |
421 | and used as data. */ | |
422 | static rtx label_value_list; | |
423 | ||
424 | /* a list of non-local labels in the function. */ | |
425 | static rtx nonlocal_label_list; | |
426 | ||
28dcb9ed | 427 | /* Find all basic blocks of the function whose first insn is F. |
428 | Store the correct data in the tables that describe the basic blocks, | |
429 | set up the chains of references for each CODE_LABEL, and | |
05b74099 | 430 | delete any entire basic blocks that cannot be reached. |
431 | ||
cfc185a4 | 432 | NONLOCAL_LABELS is a list of non-local labels in the function. |
61e82936 | 433 | Blocks that are otherwise unreachable may be reachable with a non-local |
f2017203 | 434 | goto. */ |
28dcb9ed | 435 | |
436 | static void | |
053ec0a1 | 437 | find_basic_blocks_1 (f, nonlocal_labels) |
cfc185a4 | 438 | rtx f, nonlocal_labels; |
28dcb9ed | 439 | { |
440 | register rtx insn; | |
441 | register int i; | |
442 | register char *block_live = (char *) alloca (n_basic_blocks); | |
443 | register char *block_marked = (char *) alloca (n_basic_blocks); | |
cfc185a4 | 444 | rtx note, eh_note; |
a123fe25 | 445 | enum rtx_code prev_code, code; |
22548064 | 446 | int depth; |
7bdba5dd | 447 | int call_had_abnormal_edge = 0; |
28dcb9ed | 448 | |
011a7f23 | 449 | active_eh_region = (int *) alloca ((max_uid_for_flow + 1) * sizeof (int)); |
450 | nested_eh_region = (int *) alloca ((max_label_num () + 1) * sizeof (int)); | |
cfc185a4 | 451 | nonlocal_label_list = nonlocal_labels; |
dd1cc380 | 452 | |
453 | label_value_list = 0; | |
28dcb9ed | 454 | block_live_static = block_live; |
455 | bzero (block_live, n_basic_blocks); | |
456 | bzero (block_marked, n_basic_blocks); | |
f24e9d92 | 457 | bzero (basic_block_computed_jump_target, n_basic_blocks); |
011a7f23 | 458 | bzero ((char *) active_eh_region, (max_uid_for_flow + 1) * sizeof (int)); |
459 | bzero ((char *) nested_eh_region, (max_label_num () + 1) * sizeof (int)); | |
f24e9d92 | 460 | current_function_has_computed_jump = 0; |
28dcb9ed | 461 | |
462 | /* Initialize with just block 0 reachable and no blocks marked. */ | |
463 | if (n_basic_blocks > 0) | |
464 | block_live[0] = 1; | |
465 | ||
a123fe25 | 466 | /* Initialize the ref chain of each label to 0. Record where all the |
467 | blocks start and end and their depth in loops. For each insn, record | |
468 | the block it is in. Also mark as reachable any blocks headed by labels | |
469 | that must not be deleted. */ | |
28dcb9ed | 470 | |
f11893d3 | 471 | for (eh_note = NULL_RTX, insn = f, i = -1, prev_code = JUMP_INSN, depth = 1; |
a123fe25 | 472 | insn; insn = NEXT_INSN (insn)) |
473 | { | |
474 | code = GET_CODE (insn); | |
475 | if (code == NOTE) | |
476 | { | |
477 | if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG) | |
478 | depth++; | |
479 | else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END) | |
480 | depth--; | |
481 | } | |
28dcb9ed | 482 | |
a123fe25 | 483 | /* A basic block starts at label, or after something that can jump. */ |
484 | else if (code == CODE_LABEL | |
485 | || (GET_RTX_CLASS (code) == 'i' | |
486 | && (prev_code == JUMP_INSN | |
7bdba5dd | 487 | || (prev_code == CALL_INSN && call_had_abnormal_edge) |
a123fe25 | 488 | || prev_code == BARRIER))) |
489 | { | |
68676d00 | 490 | BLOCK_HEAD (++i) = insn; |
491 | BLOCK_END (i) = insn; | |
a123fe25 | 492 | basic_block_loop_depth[i] = depth; |
493 | ||
494 | if (code == CODE_LABEL) | |
495 | { | |
7bdba5dd | 496 | LABEL_REFS (insn) = insn; |
497 | /* Any label that cannot be deleted | |
498 | is considered to start a reachable block. */ | |
499 | if (LABEL_PRESERVE_P (insn)) | |
500 | block_live[i] = 1; | |
501 | } | |
a123fe25 | 502 | } |
28dcb9ed | 503 | |
a123fe25 | 504 | else if (GET_RTX_CLASS (code) == 'i') |
505 | { | |
68676d00 | 506 | BLOCK_END (i) = insn; |
a123fe25 | 507 | basic_block_loop_depth[i] = depth; |
45fa04a9 | 508 | } |
a123fe25 | 509 | |
45fa04a9 | 510 | if (GET_RTX_CLASS (code) == 'i') |
511 | { | |
a123fe25 | 512 | /* Make a list of all labels referred to other than by jumps. */ |
513 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | |
931c14df | 514 | if (REG_NOTE_KIND (note) == REG_LABEL |
ec37ccb4 | 515 | && XEXP (note, 0) != eh_return_stub_label) |
941522d6 | 516 | label_value_list = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0), |
517 | label_value_list); | |
45fa04a9 | 518 | } |
28dcb9ed | 519 | |
011a7f23 | 520 | /* Keep a lifo list of the currently active exception notes. */ |
f11893d3 | 521 | if (GET_CODE (insn) == NOTE) |
522 | { | |
523 | if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) | |
524 | { | |
011a7f23 | 525 | if (eh_note) |
526 | nested_eh_region [NOTE_BLOCK_NUMBER (insn)] = | |
527 | NOTE_BLOCK_NUMBER (XEXP (eh_note, 0)); | |
528 | else | |
529 | nested_eh_region [NOTE_BLOCK_NUMBER (insn)] = 0; | |
530 | eh_note = gen_rtx_EXPR_LIST (VOIDmode, | |
531 | insn, eh_note); | |
f11893d3 | 532 | } |
533 | else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END) | |
534 | eh_note = XEXP (eh_note, 1); | |
535 | } | |
536 | /* If we encounter a CALL_INSN, note which exception handler it | |
537 | might pass control to. | |
538 | ||
539 | If doing asynchronous exceptions, record the active EH handler | |
540 | for every insn, since most insns can throw. */ | |
541 | else if (eh_note | |
542 | && (asynchronous_exceptions | |
454c42da | 543 | || (GET_CODE (insn) == CALL_INSN))) |
cfc185a4 | 544 | active_eh_region[INSN_UID (insn)] = |
011a7f23 | 545 | NOTE_BLOCK_NUMBER (XEXP (eh_note, 0)); |
a123fe25 | 546 | BLOCK_NUM (insn) = i; |
05b74099 | 547 | |
5d3a797d | 548 | /* We change the code of the CALL_INSN, so that it won't start a |
454c42da | 549 | new block if it doesn't throw. */ |
550 | if (code == CALL_INSN) | |
551 | { | |
552 | rtx rnote = find_reg_note(insn, REG_EH_REGION, NULL_RTX); | |
553 | if (rnote && XINT (XEXP (rnote, 0), 0) == 0) | |
554 | code = INSN; | |
555 | } | |
5d3a797d | 556 | |
7bdba5dd | 557 | /* Record whether this call created an edge. */ |
558 | if (code == CALL_INSN) | |
559 | call_had_abnormal_edge = (nonlocal_label_list != 0 || eh_note); | |
560 | ||
f11fd6c8 | 561 | if (code != NOTE) |
a123fe25 | 562 | prev_code = code; |
12144423 | 563 | |
a123fe25 | 564 | } |
565 | ||
22548064 | 566 | if (i + 1 != n_basic_blocks) |
a123fe25 | 567 | abort (); |
28dcb9ed | 568 | |
569 | /* Now find which basic blocks can actually be reached | |
570 | and put all jump insns' LABEL_REFS onto the ref-chains | |
571 | of their target labels. */ | |
572 | ||
573 | if (n_basic_blocks > 0) | |
574 | { | |
575 | int something_marked = 1; | |
576 | ||
28dcb9ed | 577 | /* Pass over all blocks, marking each block that is reachable |
578 | and has not yet been marked. | |
579 | Keep doing this until, in one pass, no blocks have been marked. | |
580 | Then blocks_live and blocks_marked are identical and correct. | |
581 | In addition, all jumps actually reachable have been marked. */ | |
582 | ||
583 | while (something_marked) | |
584 | { | |
585 | something_marked = 0; | |
586 | for (i = 0; i < n_basic_blocks; i++) | |
587 | if (block_live[i] && !block_marked[i]) | |
588 | { | |
22548064 | 589 | int_list_ptr p; |
590 | ||
28dcb9ed | 591 | block_marked[i] = 1; |
592 | something_marked = 1; | |
f11893d3 | 593 | |
cfc185a4 | 594 | make_edges (i); |
22548064 | 595 | |
596 | for (p = basic_block_succ[i]; p; p = p->next) | |
597 | block_live[INT_LIST_VAL (p)] = 1; | |
28dcb9ed | 598 | } |
599 | } | |
600 | ||
f11893d3 | 601 | /* This should never happen. If it does that means we've computed an |
602 | incorrect flow graph, which can lead to aborts/crashes later in the | |
603 | compiler or incorrect code generation. | |
1b5174f0 | 604 | |
f11893d3 | 605 | We used to try and continue here, but that's just asking for trouble |
606 | later during the compile or at runtime. It's easier to debug the | |
607 | problem here than later! */ | |
1b5174f0 | 608 | for (i = 1; i < n_basic_blocks; i++) |
22548064 | 609 | if (block_live[i] && basic_block_pred[i] == 0) |
f11893d3 | 610 | abort (); |
1b5174f0 | 611 | |
8ebf7433 | 612 | if (! reload_completed) |
22548064 | 613 | delete_unreachable_blocks (); |
28dcb9ed | 614 | } |
615 | } | |
61e82936 | 616 | |
617 | /* Record INSN's block number as BB. */ | |
618 | ||
619 | void | |
620 | set_block_num (insn, bb) | |
621 | rtx insn; | |
622 | int bb; | |
623 | { | |
624 | if (INSN_UID (insn) >= max_uid_for_flow) | |
625 | { | |
626 | /* Add one-eighth the size so we don't keep calling xrealloc. */ | |
627 | max_uid_for_flow = INSN_UID (insn) + (INSN_UID (insn) + 7) / 8; | |
628 | uid_block_number = (int *) | |
629 | xrealloc (uid_block_number, (max_uid_for_flow + 1) * sizeof (int)); | |
630 | } | |
631 | BLOCK_NUM (insn) = bb; | |
632 | } | |
28dcb9ed | 633 | \f |
dd1cc380 | 634 | /* Subroutines of find_basic_blocks. */ |
635 | ||
22548064 | 636 | void |
637 | free_bb_memory () | |
638 | { | |
639 | free_int_list (&flow_int_list_blocks); | |
640 | } | |
641 | ||
642 | /* Make an edge in the cfg from block PRED to block SUCC. */ | |
643 | static void | |
644 | add_edge (pred, succ) | |
645 | int pred, succ; | |
646 | { | |
647 | add_int_list_node (&flow_int_list_blocks, basic_block_pred + succ, pred); | |
648 | add_int_list_node (&flow_int_list_blocks, basic_block_succ + pred, succ); | |
649 | } | |
650 | ||
651 | /* Make an edge in the cfg from block PRED to the block starting with | |
652 | label LABEL. */ | |
653 | static void | |
654 | add_edge_to_label (pred, label) | |
655 | int pred; | |
656 | rtx label; | |
657 | { | |
658 | /* If the label was never emitted, this insn is junk, | |
659 | but avoid a crash trying to refer to BLOCK_NUM (label). | |
660 | This can happen as a result of a syntax error | |
661 | and a diagnostic has already been printed. */ | |
662 | if (INSN_UID (label) == 0) | |
663 | return; | |
664 | ||
665 | add_edge (pred, BLOCK_NUM (label)); | |
666 | } | |
667 | ||
668 | /* Check expression X for label references. If one is found, add an edge | |
669 | from basic block PRED to the block beginning with the label. */ | |
670 | ||
671 | static void | |
672 | mark_label_ref (pred, x) | |
673 | int pred; | |
674 | rtx x; | |
675 | { | |
676 | register RTX_CODE code; | |
677 | register int i; | |
678 | register char *fmt; | |
679 | ||
680 | code = GET_CODE (x); | |
681 | if (code == LABEL_REF) | |
682 | { | |
683 | add_edge_to_label (pred, XEXP (x, 0)); | |
684 | return; | |
685 | } | |
686 | ||
687 | fmt = GET_RTX_FORMAT (code); | |
688 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
689 | { | |
690 | if (fmt[i] == 'e') | |
691 | mark_label_ref (pred, XEXP (x, i)); | |
692 | if (fmt[i] == 'E') | |
693 | { | |
694 | register int j; | |
695 | for (j = 0; j < XVECLEN (x, i); j++) | |
696 | mark_label_ref (pred, XVECEXP (x, i, j)); | |
697 | } | |
698 | } | |
699 | } | |
700 | ||
cfc185a4 | 701 | /* For basic block I, make edges and mark live all blocks which are reachable |
702 | from it. */ | |
703 | static void | |
704 | make_edges (i) | |
705 | int i; | |
706 | { | |
707 | rtx insn, x; | |
d63ea2f2 | 708 | rtx pending_eh_region = NULL_RTX; |
cfc185a4 | 709 | |
22548064 | 710 | /* See if control drops into the next block. */ |
711 | if (i + 1 < n_basic_blocks) | |
712 | { | |
68676d00 | 713 | for (insn = PREV_INSN (BLOCK_HEAD (i + 1)); |
22548064 | 714 | insn && GET_CODE (insn) == NOTE; insn = PREV_INSN (insn)) |
715 | ; | |
716 | ||
717 | if (insn && GET_CODE (insn) != BARRIER) | |
718 | add_edge (i, i + 1); | |
719 | } | |
720 | ||
68676d00 | 721 | insn = BLOCK_END (i); |
cfc185a4 | 722 | if (GET_CODE (insn) == JUMP_INSN) |
22548064 | 723 | mark_label_ref (i, PATTERN (insn)); |
cfc185a4 | 724 | |
725 | /* If we have any forced labels, mark them as potentially reachable from | |
726 | this block. */ | |
727 | for (x = forced_labels; x; x = XEXP (x, 1)) | |
728 | if (! LABEL_REF_NONLOCAL_P (x)) | |
22548064 | 729 | add_edge_to_label (i, XEXP (x, 0)); |
cfc185a4 | 730 | |
731 | /* Now scan the insns for this block, we may need to make edges for some of | |
732 | them to various non-obvious locations (exception handlers, nonlocal | |
733 | labels, etc). */ | |
68676d00 | 734 | for (insn = BLOCK_HEAD (i); |
735 | insn != NEXT_INSN (BLOCK_END (i)); | |
cfc185a4 | 736 | insn = NEXT_INSN (insn)) |
737 | { | |
738 | if (GET_RTX_CLASS (GET_CODE (insn)) == 'i') | |
739 | { | |
740 | rtx note; | |
741 | /* References to labels in non-jumping insns have REG_LABEL notes | |
742 | attached to them. | |
743 | ||
744 | This can happen for computed gotos; we don't care about them | |
745 | here since the values are also on the label_value_list and will | |
746 | be marked live if we find a live computed goto. | |
747 | ||
748 | This can also happen when we take the address of a label to pass | |
749 | as an argument to __throw. Note throw only uses the value to | |
750 | determine what handler should be called -- ie the label is not | |
751 | used as a jump target, it just marks regions in the code. | |
752 | ||
753 | In theory we should be able to ignore the REG_LABEL notes, but | |
754 | we have to make sure that the label and associated insns aren't | |
755 | marked dead, so we make the block in question live and create an | |
756 | edge from this insn to the label. This is not strictly correct, | |
757 | but it is close enough for now. | |
758 | ||
759 | See below for code that handles the eh_stub label specially. */ | |
760 | for (note = REG_NOTES (insn); | |
761 | note; | |
762 | note = XEXP (note, 1)) | |
763 | { | |
764 | if (REG_NOTE_KIND (note) == REG_LABEL | |
765 | && XEXP (note, 0) != eh_return_stub_label) | |
22548064 | 766 | add_edge_to_label (i, XEXP (note, 0)); |
cfc185a4 | 767 | } |
768 | ||
769 | /* If this is a computed jump, then mark it as reaching everything | |
770 | on the label_value_list and forced_labels list. */ | |
771 | if (computed_jump_p (insn)) | |
772 | { | |
773 | current_function_has_computed_jump = 1; | |
774 | for (x = label_value_list; x; x = XEXP (x, 1)) | |
775 | { | |
776 | int b = BLOCK_NUM (XEXP (x, 0)); | |
777 | basic_block_computed_jump_target[b] = 1; | |
22548064 | 778 | add_edge (i, b); |
cfc185a4 | 779 | } |
780 | ||
781 | for (x = forced_labels; x; x = XEXP (x, 1)) | |
782 | { | |
783 | int b = BLOCK_NUM (XEXP (x, 0)); | |
784 | basic_block_computed_jump_target[b] = 1; | |
22548064 | 785 | add_edge (i, b); |
cfc185a4 | 786 | } |
787 | } | |
788 | ||
d63ea2f2 | 789 | /* If this is a call with an EH_RETHROW note, then we |
790 | know its a rethrow call, and we know exactly where | |
791 | this call can end up going. */ | |
792 | else if (GET_CODE (insn) == CALL_INSN | |
793 | && (note = find_reg_note (insn, REG_EH_RETHROW, NULL_RTX))) | |
794 | { | |
795 | int region = XINT (XEXP (note, 0), 0); | |
796 | /* if nested region is not 0, we know for sure it has been | |
797 | processed. If it is zero, we dont know whether its an | |
798 | outer region, or hasn't been seen yet, so defer it */ | |
799 | if (nested_eh_region[region] != 0) | |
800 | { | |
801 | /* start with the first region OUTSIDE the one specified | |
802 | in the rethrow parameter. (since a rethrow behaves | |
803 | as if a handler in the region didn't handle the | |
804 | exception, so the handlers for the next outer region | |
805 | are going to get a shot at it.*/ | |
806 | for ( region = nested_eh_region[region]; region; | |
807 | region = nested_eh_region[region]) | |
808 | { | |
809 | handler_info *ptr = get_first_handler (region); | |
810 | for ( ; ptr ; ptr = ptr->next) | |
811 | add_edge_to_label (i, ptr->handler_label); | |
812 | } | |
813 | } | |
814 | else | |
815 | { | |
816 | /* Push this region onto a list, and after we've done the | |
817 | whole procedure, we'll process everything on the list */ | |
818 | pending_eh_region = gen_rtx_EXPR_LIST (VOIDmode, insn, | |
819 | pending_eh_region); | |
820 | } | |
821 | } | |
822 | ||
cfc185a4 | 823 | /* If this is a CALL_INSN, then mark it as reaching the active EH |
824 | handler for this CALL_INSN. If we're handling asynchronous | |
825 | exceptions mark every insn as reaching the active EH handler. | |
826 | ||
827 | Also mark the CALL_INSN as reaching any nonlocal goto sites. */ | |
828 | else if (asynchronous_exceptions | |
829 | || (GET_CODE (insn) == CALL_INSN | |
830 | && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))) | |
831 | { | |
454c42da | 832 | int region = active_eh_region[INSN_UID (insn)]; |
833 | note = find_reg_note(insn, REG_EH_REGION, NULL_RTX); | |
834 | ||
835 | /* Override region if we see a REG_EH_REGION note. */ | |
836 | if (note) | |
837 | region = XINT (XEXP (note, 0), 0); | |
838 | ||
839 | if (region) | |
cfc185a4 | 840 | { |
cfc185a4 | 841 | handler_info *ptr; |
842 | region = active_eh_region[INSN_UID (insn)]; | |
22548064 | 843 | for ( ; region; region = nested_eh_region[region]) |
cfc185a4 | 844 | { |
845 | ptr = get_first_handler (region); | |
846 | for ( ; ptr ; ptr = ptr->next) | |
22548064 | 847 | add_edge_to_label (i, ptr->handler_label); |
cfc185a4 | 848 | } |
849 | } | |
850 | if (! asynchronous_exceptions) | |
851 | { | |
852 | for (x = nonlocal_label_list; x; x = XEXP (x, 1)) | |
22548064 | 853 | add_edge_to_label (i, XEXP (x, 0)); |
cfc185a4 | 854 | } |
855 | /* ??? This could be made smarter: in some cases it's possible | |
856 | to tell that certain calls will not do a nonlocal goto. | |
857 | ||
858 | For example, if the nested functions that do the nonlocal | |
859 | gotos do not have their addresses taken, then only calls to | |
860 | those functions or to other nested functions that use them | |
861 | could possibly do nonlocal gotos. */ | |
862 | } | |
863 | } | |
864 | } | |
d63ea2f2 | 865 | |
866 | while (pending_eh_region != NULL_RTX) | |
867 | { | |
868 | rtx insn = XEXP (pending_eh_region, 0); | |
869 | rtx note = find_reg_note (insn, REG_EH_RETHROW, NULL_RTX); | |
870 | int region = XINT (XEXP (note, 0), 0); | |
871 | /* start with the first region OUTSIDE the one specified | |
872 | in the rethrow parameter */ | |
873 | for ( region = nested_eh_region[region]; region; | |
874 | region = nested_eh_region[region]) | |
875 | { | |
876 | handler_info *ptr = get_first_handler (region); | |
877 | for ( ; ptr ; ptr = ptr->next) | |
878 | add_edge_to_label (BLOCK_NUM (insn), ptr->handler_label); | |
879 | } | |
880 | pending_eh_region = XEXP (pending_eh_region, 1); | |
881 | } | |
882 | ||
cfc185a4 | 883 | /* We know something about the structure of the function __throw in |
884 | libgcc2.c. It is the only function that ever contains eh_stub labels. | |
885 | It modifies its return address so that the last block returns to one of | |
886 | the eh_stub labels within it. So we have to make additional edges in | |
887 | the flow graph. */ | |
888 | if (i + 1 == n_basic_blocks && eh_return_stub_label != 0) | |
22548064 | 889 | add_edge_to_label (i, eh_return_stub_label); |
28dcb9ed | 890 | } |
dd1cc380 | 891 | |
cfc185a4 | 892 | /* Now delete the code for any basic blocks that can't be reached. |
893 | They can occur because jump_optimize does not recognize unreachable loops | |
22548064 | 894 | as unreachable. */ |
895 | static void | |
cfc185a4 | 896 | delete_unreachable_blocks () |
897 | { | |
898 | int deleted_handler = 0; | |
899 | int deleted = 0; | |
22548064 | 900 | int i, j; |
cfc185a4 | 901 | rtx insn; |
22548064 | 902 | int *block_num_map = XNMALLOC (int, n_basic_blocks); |
cfc185a4 | 903 | |
22548064 | 904 | for (i = n_basic_blocks - 1; i >= 0; i--) |
cfc185a4 | 905 | if (! block_live_static[i]) |
22548064 | 906 | deleted_handler |= delete_block (i); |
907 | ||
908 | for (i = 0; i < n_basic_blocks; i++) | |
909 | if (block_live_static[i]) | |
910 | block_num_map[i] = i - deleted; | |
911 | else | |
cfc185a4 | 912 | { |
913 | deleted++; | |
22548064 | 914 | block_num_map[i] = -1; |
cfc185a4 | 915 | } |
916 | ||
22548064 | 917 | /* Eliminate all traces of the deleted blocks by renumbering the remaining |
918 | ones. */ | |
919 | for (i = j = 0; i < n_basic_blocks; i++) | |
920 | { | |
921 | int_list_ptr p; | |
922 | ||
923 | if (block_num_map[i] == -1) | |
924 | continue; | |
925 | ||
926 | for (p = basic_block_pred[i]; p; p = p->next) | |
927 | INT_LIST_VAL (p) = block_num_map[INT_LIST_VAL (p)]; | |
928 | for (p = basic_block_succ[i]; p; p = p->next) | |
929 | INT_LIST_VAL (p) = block_num_map[INT_LIST_VAL (p)]; | |
930 | ||
931 | if (i != j) | |
932 | { | |
68676d00 | 933 | rtx tmp = BLOCK_HEAD (i); |
22548064 | 934 | for (;;) |
935 | { | |
936 | BLOCK_NUM (tmp) = j; | |
68676d00 | 937 | if (tmp == BLOCK_END (i)) |
22548064 | 938 | break; |
939 | tmp = NEXT_INSN (tmp); | |
940 | } | |
68676d00 | 941 | BLOCK_HEAD (j) = BLOCK_HEAD (i); |
942 | BLOCK_END (j) = BLOCK_END (i); | |
22548064 | 943 | basic_block_pred[j] = basic_block_pred[i]; |
944 | basic_block_succ[j] = basic_block_succ[i]; | |
945 | basic_block_loop_depth[j] = basic_block_loop_depth[i]; | |
946 | basic_block_computed_jump_target[j] | |
947 | = basic_block_computed_jump_target[i]; | |
948 | } | |
949 | j++; | |
950 | } | |
951 | n_basic_blocks -= deleted; | |
952 | free (block_num_map); | |
953 | ||
cfc185a4 | 954 | /* If we deleted an exception handler, we may have EH region |
955 | begin/end blocks to remove as well. */ | |
956 | if (deleted_handler) | |
957 | for (insn = get_insns (); insn; insn = NEXT_INSN (insn)) | |
958 | if (GET_CODE (insn) == NOTE) | |
959 | { | |
22548064 | 960 | if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG || |
961 | NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END) | |
cfc185a4 | 962 | { |
963 | int num = CODE_LABEL_NUMBER (insn); | |
d63ea2f2 | 964 | /* A NULL handler indicates a region is no longer needed, |
965 | unless its the target of a rethrow. */ | |
966 | if (get_first_handler (num) == NULL && !rethrow_used (num)) | |
cfc185a4 | 967 | { |
968 | NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED; | |
969 | NOTE_SOURCE_FILE (insn) = 0; | |
970 | } | |
971 | } | |
972 | } | |
cfc185a4 | 973 | } |
974 | ||
975 | /* Delete the insns in a (non-live) block. We physically delete every | |
68676d00 | 976 | non-note insn except the start and end (so BLOCK_HEAD/END needn't |
cfc185a4 | 977 | be updated), we turn the latter into NOTE_INSN_DELETED notes. |
978 | ||
979 | We use to "delete" the insns by turning them into notes, but we may be | |
980 | deleting lots of insns that subsequent passes would otherwise have to | |
981 | process. Secondly, lots of deleted blocks in a row can really slow down | |
982 | propagate_block since it will otherwise process insn-turned-notes multiple | |
983 | times when it looks for loop begin/end notes. | |
984 | ||
985 | Return nonzero if we deleted an exception handler. */ | |
986 | static int | |
987 | delete_block (i) | |
988 | int i; | |
989 | { | |
990 | int deleted_handler = 0; | |
991 | rtx insn; | |
22548064 | 992 | rtx kept_head = 0; |
993 | rtx kept_tail = 0; | |
994 | ||
995 | /* If the head of this block is a CODE_LABEL, then it might | |
996 | be the label for an exception handler which can't be | |
997 | reached. | |
cfc185a4 | 998 | |
22548064 | 999 | We need to remove the label from the exception_handler_label |
1000 | list and remove the associated NOTE_EH_REGION_BEG and | |
1001 | NOTE_EH_REGION_END notes. */ | |
68676d00 | 1002 | insn = BLOCK_HEAD (i); |
22548064 | 1003 | if (GET_CODE (insn) == CODE_LABEL) |
cfc185a4 | 1004 | { |
22548064 | 1005 | rtx x, *prev = &exception_handler_labels; |
1006 | ||
1007 | for (x = exception_handler_labels; x; x = XEXP (x, 1)) | |
cfc185a4 | 1008 | { |
22548064 | 1009 | if (XEXP (x, 0) == insn) |
1010 | { | |
1011 | /* Found a match, splice this label out of the | |
1012 | EH label list. */ | |
1013 | *prev = XEXP (x, 1); | |
1014 | XEXP (x, 1) = NULL_RTX; | |
1015 | XEXP (x, 0) = NULL_RTX; | |
1016 | ||
1017 | /* Remove the handler from all regions */ | |
1018 | remove_handler (insn); | |
1019 | deleted_handler = 1; | |
1020 | break; | |
1021 | } | |
1022 | prev = &XEXP (x, 1); | |
cfc185a4 | 1023 | } |
1024 | } | |
1025 | ||
22548064 | 1026 | /* Walk the insns of the block, building a chain of NOTEs that need to be |
1027 | kept. */ | |
68676d00 | 1028 | insn = BLOCK_HEAD (i); |
22548064 | 1029 | for (;;) |
cfc185a4 | 1030 | { |
cfc185a4 | 1031 | if (GET_CODE (insn) == BARRIER) |
1032 | abort (); | |
22548064 | 1033 | else if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED) |
cfc185a4 | 1034 | { |
22548064 | 1035 | if (kept_head == 0) |
1036 | kept_head = kept_tail = insn; | |
1037 | else | |
cfc185a4 | 1038 | { |
22548064 | 1039 | NEXT_INSN (kept_tail) = insn; |
1040 | PREV_INSN (insn) = kept_tail; | |
1041 | kept_tail = insn; | |
cfc185a4 | 1042 | } |
1043 | } | |
68676d00 | 1044 | if (insn == BLOCK_END (i)) |
22548064 | 1045 | break; |
1046 | insn = NEXT_INSN (insn); | |
cfc185a4 | 1047 | } |
22548064 | 1048 | insn = NEXT_INSN (insn); |
1049 | ||
cfc185a4 | 1050 | /* BARRIERs are between basic blocks, not part of one. |
1051 | Delete a BARRIER if the preceding jump is deleted. | |
1052 | We cannot alter a BARRIER into a NOTE | |
1053 | because it is too short; but we can really delete | |
1054 | it because it is not part of a basic block. */ | |
22548064 | 1055 | if (insn != 0 && GET_CODE (insn) == BARRIER) |
1056 | insn = NEXT_INSN (insn); | |
1057 | ||
1058 | /* Now unchain all of the block, and put the chain of kept notes in its | |
1059 | place. */ | |
1060 | if (kept_head == 0) | |
1061 | { | |
68676d00 | 1062 | NEXT_INSN (PREV_INSN (BLOCK_HEAD (i))) = insn; |
22548064 | 1063 | if (insn != 0) |
68676d00 | 1064 | PREV_INSN (insn) = PREV_INSN (BLOCK_HEAD (i)); |
137c7d42 | 1065 | else |
68676d00 | 1066 | set_last_insn (PREV_INSN (BLOCK_HEAD(i))); |
22548064 | 1067 | } |
1068 | else | |
1069 | { | |
68676d00 | 1070 | NEXT_INSN (PREV_INSN (BLOCK_HEAD (i))) = kept_head; |
22548064 | 1071 | if (insn != 0) |
1072 | PREV_INSN (insn) = kept_tail; | |
1073 | ||
68676d00 | 1074 | PREV_INSN (kept_head) = PREV_INSN (BLOCK_HEAD (i)); |
22548064 | 1075 | NEXT_INSN (kept_tail) = insn; |
5f4f0be0 | 1076 | |
1077 | /* This must happen after NEXT_INSN (kept_tail) has been reinitialized | |
1078 | since set_last_insn will abort if it detects a non-NULL NEXT_INSN | |
1079 | field in its argument. */ | |
1080 | if (insn == NULL_RTX) | |
1081 | set_last_insn (kept_tail); | |
22548064 | 1082 | } |
cfc185a4 | 1083 | |
1084 | /* Each time we delete some basic blocks, | |
1085 | see if there is a jump around them that is | |
1086 | being turned into a no-op. If so, delete it. */ | |
1087 | ||
1088 | if (block_live_static[i - 1]) | |
1089 | { | |
1090 | register int j; | |
1091 | for (j = i + 1; j < n_basic_blocks; j++) | |
1092 | if (block_live_static[j]) | |
1093 | { | |
1094 | rtx label; | |
68676d00 | 1095 | insn = BLOCK_END (i - 1); |
cfc185a4 | 1096 | if (GET_CODE (insn) == JUMP_INSN |
1097 | /* An unconditional jump is the only possibility | |
1098 | we must check for, since a conditional one | |
1099 | would make these blocks live. */ | |
1100 | && simplejump_p (insn) | |
1101 | && (label = XEXP (SET_SRC (PATTERN (insn)), 0), 1) | |
1102 | && INSN_UID (label) != 0 | |
1103 | && BLOCK_NUM (label) == j) | |
1104 | { | |
cfc185a4 | 1105 | PUT_CODE (insn, NOTE); |
1106 | NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED; | |
1107 | NOTE_SOURCE_FILE (insn) = 0; | |
1108 | if (GET_CODE (NEXT_INSN (insn)) != BARRIER) | |
1109 | abort (); | |
1110 | delete_insn (NEXT_INSN (insn)); | |
1111 | } | |
1112 | break; | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | return deleted_handler; | |
1117 | } | |
28dcb9ed | 1118 | \f |
61e82936 | 1119 | /* Perform data flow analysis. |
1120 | F is the first insn of the function and NREGS the number of register numbers | |
1121 | in use. */ | |
1122 | ||
1123 | void | |
1124 | life_analysis (f, nregs, file) | |
1125 | rtx f; | |
1126 | int nregs; | |
1127 | FILE *file; | |
1128 | { | |
61e82936 | 1129 | #ifdef ELIMINABLE_REGS |
3c5c852a | 1130 | register size_t i; |
61e82936 | 1131 | static struct {int from, to; } eliminables[] = ELIMINABLE_REGS; |
1132 | #endif | |
1133 | ||
1134 | /* Record which registers will be eliminated. We use this in | |
1135 | mark_used_regs. */ | |
1136 | ||
1137 | CLEAR_HARD_REG_SET (elim_reg_set); | |
1138 | ||
1139 | #ifdef ELIMINABLE_REGS | |
1140 | for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++) | |
1141 | SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from); | |
1142 | #else | |
1143 | SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM); | |
1144 | #endif | |
1145 | ||
d29fc2f4 | 1146 | /* We want alias analysis information for local dead store elimination. */ |
1147 | init_alias_analysis (); | |
61e82936 | 1148 | life_analysis_1 (f, nregs); |
d29fc2f4 | 1149 | end_alias_analysis (); |
1150 | ||
61e82936 | 1151 | if (file) |
1152 | dump_flow_info (file); | |
1153 | ||
1154 | free_basic_block_vars (1); | |
1155 | } | |
1156 | ||
1157 | /* Free the variables allocated by find_basic_blocks. | |
1158 | ||
68676d00 | 1159 | KEEP_HEAD_END_P is non-zero if BLOCK_HEAD and BLOCK_END |
61e82936 | 1160 | are not to be freed. */ |
1161 | ||
1162 | void | |
1163 | free_basic_block_vars (keep_head_end_p) | |
1164 | int keep_head_end_p; | |
1165 | { | |
61e82936 | 1166 | if (basic_block_loop_depth) |
1167 | { | |
1168 | free (basic_block_loop_depth); | |
1169 | basic_block_loop_depth = 0; | |
1170 | } | |
1171 | if (uid_block_number) | |
1172 | { | |
1173 | free (uid_block_number); | |
1174 | uid_block_number = 0; | |
1175 | } | |
1176 | if (uid_volatile) | |
1177 | { | |
1178 | free (uid_volatile); | |
1179 | uid_volatile = 0; | |
1180 | } | |
1181 | ||
68676d00 | 1182 | if (! keep_head_end_p && x_basic_block_head) |
61e82936 | 1183 | { |
68676d00 | 1184 | free (x_basic_block_head); |
1185 | x_basic_block_head = 0; | |
1186 | free (x_basic_block_end); | |
1187 | x_basic_block_end = 0; | |
61e82936 | 1188 | } |
1189 | } | |
1190 | ||
cfc185a4 | 1191 | /* Return nonzero if the destination of SET equals the source. */ |
1192 | static int | |
1193 | set_noop_p (set) | |
1194 | rtx set; | |
1195 | { | |
1196 | rtx src = SET_SRC (set); | |
1197 | rtx dst = SET_DEST (set); | |
1198 | if (GET_CODE (src) == REG && GET_CODE (dst) == REG | |
1199 | && REGNO (src) == REGNO (dst)) | |
1200 | return 1; | |
1201 | if (GET_CODE (src) != SUBREG || GET_CODE (dst) != SUBREG | |
1202 | || SUBREG_WORD (src) != SUBREG_WORD (dst)) | |
1203 | return 0; | |
1204 | src = SUBREG_REG (src); | |
1205 | dst = SUBREG_REG (dst); | |
1206 | if (GET_CODE (src) == REG && GET_CODE (dst) == REG | |
1207 | && REGNO (src) == REGNO (dst)) | |
1208 | return 1; | |
1209 | return 0; | |
1210 | } | |
1211 | ||
1212 | /* Return nonzero if an insn consists only of SETs, each of which only sets a | |
1213 | value to itself. */ | |
1214 | static int | |
1215 | noop_move_p (insn) | |
1216 | rtx insn; | |
1217 | { | |
1218 | rtx pat = PATTERN (insn); | |
1219 | ||
1220 | /* Insns carrying these notes are useful later on. */ | |
1221 | if (find_reg_note (insn, REG_EQUAL, NULL_RTX)) | |
1222 | return 0; | |
1223 | ||
1224 | if (GET_CODE (pat) == SET && set_noop_p (pat)) | |
1225 | return 1; | |
1226 | ||
1227 | if (GET_CODE (pat) == PARALLEL) | |
1228 | { | |
1229 | int i; | |
1230 | /* If nothing but SETs of registers to themselves, | |
1231 | this insn can also be deleted. */ | |
1232 | for (i = 0; i < XVECLEN (pat, 0); i++) | |
1233 | { | |
1234 | rtx tem = XVECEXP (pat, 0, i); | |
1235 | ||
1236 | if (GET_CODE (tem) == USE | |
1237 | || GET_CODE (tem) == CLOBBER) | |
1238 | continue; | |
1239 | ||
1240 | if (GET_CODE (tem) != SET || ! set_noop_p (tem)) | |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | return 1; | |
1245 | } | |
1246 | return 0; | |
1247 | } | |
1248 | ||
22c8984a | 1249 | static void |
1250 | notice_stack_pointer_modification (x, pat) | |
1251 | rtx x; | |
1252 | rtx pat ATTRIBUTE_UNUSED; | |
1253 | { | |
1254 | if (x == stack_pointer_rtx | |
1255 | /* The stack pointer is only modified indirectly as the result | |
1256 | of a push until later in flow. See the comments in rtl.texi | |
1257 | regarding Embedded Side-Effects on Addresses. */ | |
1258 | || (GET_CODE (x) == MEM | |
1259 | && (GET_CODE (XEXP (x, 0)) == PRE_DEC | |
1260 | || GET_CODE (XEXP (x, 0)) == PRE_INC | |
1261 | || GET_CODE (XEXP (x, 0)) == POST_DEC | |
1262 | || GET_CODE (XEXP (x, 0)) == POST_INC) | |
1263 | && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx)) | |
1264 | current_function_sp_is_unchanging = 0; | |
1265 | } | |
1266 | ||
cfc185a4 | 1267 | /* Record which insns refer to any volatile memory |
1268 | or for any reason can't be deleted just because they are dead stores. | |
22c8984a | 1269 | Also, delete any insns that copy a register to itself. |
1270 | And see if the stack pointer is modified. */ | |
cfc185a4 | 1271 | static void |
1272 | record_volatile_insns (f) | |
1273 | rtx f; | |
1274 | { | |
1275 | rtx insn; | |
1276 | for (insn = f; insn; insn = NEXT_INSN (insn)) | |
1277 | { | |
1278 | enum rtx_code code1 = GET_CODE (insn); | |
1279 | if (code1 == CALL_INSN) | |
1280 | INSN_VOLATILE (insn) = 1; | |
1281 | else if (code1 == INSN || code1 == JUMP_INSN) | |
1282 | { | |
1283 | if (GET_CODE (PATTERN (insn)) != USE | |
1284 | && volatile_refs_p (PATTERN (insn))) | |
1285 | INSN_VOLATILE (insn) = 1; | |
1286 | ||
1287 | /* A SET that makes space on the stack cannot be dead. | |
1288 | (Such SETs occur only for allocating variable-size data, | |
1289 | so they will always have a PLUS or MINUS according to the | |
1290 | direction of stack growth.) | |
1291 | Even if this function never uses this stack pointer value, | |
1292 | signal handlers do! */ | |
1293 | else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET | |
1294 | && SET_DEST (PATTERN (insn)) == stack_pointer_rtx | |
1295 | #ifdef STACK_GROWS_DOWNWARD | |
1296 | && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS | |
1297 | #else | |
1298 | && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS | |
1299 | #endif | |
1300 | && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx) | |
1301 | INSN_VOLATILE (insn) = 1; | |
1302 | ||
1303 | /* Delete (in effect) any obvious no-op moves. */ | |
1304 | else if (noop_move_p (insn)) | |
1305 | { | |
1306 | PUT_CODE (insn, NOTE); | |
1307 | NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED; | |
1308 | NOTE_SOURCE_FILE (insn) = 0; | |
1309 | } | |
1310 | } | |
22c8984a | 1311 | |
1312 | /* Check if insn modifies the stack pointer. */ | |
1313 | if ( current_function_sp_is_unchanging | |
1314 | && GET_RTX_CLASS (GET_CODE (insn)) == 'i') | |
1315 | note_stores (PATTERN (insn), notice_stack_pointer_modification); | |
cfc185a4 | 1316 | } |
1317 | } | |
1318 | ||
1319 | /* Mark those regs which are needed at the end of the function as live | |
1320 | at the end of the last basic block. */ | |
1321 | static void | |
1322 | mark_regs_live_at_end (set) | |
1323 | regset set; | |
1324 | { | |
1325 | int i; | |
1326 | ||
1327 | #ifdef EXIT_IGNORE_STACK | |
1328 | if (! EXIT_IGNORE_STACK | |
1329 | || (! FRAME_POINTER_REQUIRED | |
1330 | && ! current_function_calls_alloca | |
22c8984a | 1331 | && flag_omit_frame_pointer) |
1332 | || current_function_sp_is_unchanging) | |
cfc185a4 | 1333 | #endif |
1334 | /* If exiting needs the right stack value, | |
1335 | consider the stack pointer live at the end of the function. */ | |
1336 | SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM); | |
1337 | ||
1338 | /* Mark the frame pointer is needed at the end of the function. If | |
1339 | we end up eliminating it, it will be removed from the live list | |
1340 | of each basic block by reload. */ | |
1341 | ||
1342 | SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM); | |
1343 | #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM | |
1344 | /* If they are different, also mark the hard frame pointer as live */ | |
1345 | SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM); | |
1346 | #endif | |
1347 | ||
1348 | ||
1349 | /* Mark all global registers and all registers used by the epilogue | |
1350 | as being live at the end of the function since they may be | |
1351 | referenced by our caller. */ | |
1352 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) | |
1353 | if (global_regs[i] | |
1354 | #ifdef EPILOGUE_USES | |
1355 | || EPILOGUE_USES (i) | |
1356 | #endif | |
1357 | ) | |
1358 | SET_REGNO_REG_SET (set, i); | |
1359 | } | |
1360 | ||
28dcb9ed | 1361 | /* Determine which registers are live at the start of each |
1362 | basic block of the function whose first insn is F. | |
1363 | NREGS is the number of registers used in F. | |
1364 | We allocate the vector basic_block_live_at_start | |
1365 | and the regsets that it points to, and fill them with the data. | |
1366 | regset_size and regset_bytes are also set here. */ | |
1367 | ||
1368 | static void | |
61e82936 | 1369 | life_analysis_1 (f, nregs) |
28dcb9ed | 1370 | rtx f; |
1371 | int nregs; | |
1372 | { | |
28dcb9ed | 1373 | int first_pass; |
1374 | int changed; | |
1375 | /* For each basic block, a bitmask of regs | |
1376 | live on exit from the block. */ | |
1377 | regset *basic_block_live_at_end; | |
1378 | /* For each basic block, a bitmask of regs | |
1379 | live on entry to a successor-block of this block. | |
1380 | If this does not match basic_block_live_at_end, | |
1381 | that must be updated, and the block must be rescanned. */ | |
1382 | regset *basic_block_new_live_at_end; | |
1383 | /* For each basic block, a bitmask of regs | |
1384 | whose liveness at the end of the basic block | |
1385 | can make a difference in which regs are live on entry to the block. | |
1386 | These are the regs that are set within the basic block, | |
1387 | possibly excluding those that are used after they are set. */ | |
1388 | regset *basic_block_significant; | |
1389 | register int i; | |
3f4d644c | 1390 | char save_regs_ever_live[FIRST_PSEUDO_REGISTER]; |
28dcb9ed | 1391 | |
1392 | struct obstack flow_obstack; | |
1393 | ||
1394 | gcc_obstack_init (&flow_obstack); | |
1395 | ||
1396 | max_regno = nregs; | |
1397 | ||
3f4d644c | 1398 | /* The post-reload life analysis have (on a global basis) the same registers |
1399 | live as was computed by reload itself. | |
1400 | ||
1401 | Otherwise elimination offsets and such may be incorrect. | |
1402 | ||
1403 | Reload will make some registers as live even though they do not appear | |
1404 | in the rtl. */ | |
1405 | if (reload_completed) | |
1406 | bcopy (regs_ever_live, save_regs_ever_live, (sizeof (regs_ever_live))); | |
1407 | ||
28dcb9ed | 1408 | bzero (regs_ever_live, sizeof regs_ever_live); |
1409 | ||
1410 | /* Allocate and zero out many data structures | |
1411 | that will record the data from lifetime analysis. */ | |
1412 | ||
1413 | allocate_for_life_analysis (); | |
1414 | ||
1415 | reg_next_use = (rtx *) alloca (nregs * sizeof (rtx)); | |
748e6d74 | 1416 | bzero ((char *) reg_next_use, nregs * sizeof (rtx)); |
28dcb9ed | 1417 | |
1418 | /* Set up several regset-vectors used internally within this function. | |
1419 | Their meanings are documented above, with their declarations. */ | |
1420 | ||
748e6d74 | 1421 | basic_block_live_at_end |
1422 | = (regset *) alloca (n_basic_blocks * sizeof (regset)); | |
1423 | ||
28dcb9ed | 1424 | /* Don't use alloca since that leads to a crash rather than an error message |
1425 | if there isn't enough space. | |
1426 | Don't use oballoc since we may need to allocate other things during | |
1427 | this function on the temporary obstack. */ | |
7ee969d0 | 1428 | init_regset_vector (basic_block_live_at_end, n_basic_blocks, &flow_obstack); |
28dcb9ed | 1429 | |
748e6d74 | 1430 | basic_block_new_live_at_end |
1431 | = (regset *) alloca (n_basic_blocks * sizeof (regset)); | |
7ee969d0 | 1432 | init_regset_vector (basic_block_new_live_at_end, n_basic_blocks, |
fc1ef759 | 1433 | &flow_obstack); |
28dcb9ed | 1434 | |
748e6d74 | 1435 | basic_block_significant |
1436 | = (regset *) alloca (n_basic_blocks * sizeof (regset)); | |
7ee969d0 | 1437 | init_regset_vector (basic_block_significant, n_basic_blocks, &flow_obstack); |
28dcb9ed | 1438 | |
22c8984a | 1439 | /* Assume that the stack pointer is unchanging if alloca hasn't been used. |
1440 | This will be cleared by record_volatile_insns if it encounters an insn | |
1441 | which modifies the stack pointer. */ | |
1442 | current_function_sp_is_unchanging = !current_function_calls_alloca; | |
1443 | ||
cfc185a4 | 1444 | record_volatile_insns (f); |
4837da3a | 1445 | |
1446 | if (n_basic_blocks > 0) | |
1447 | { | |
cfc185a4 | 1448 | mark_regs_live_at_end (basic_block_live_at_end[n_basic_blocks - 1]); |
1449 | COPY_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1], | |
1450 | basic_block_live_at_end[n_basic_blocks - 1]); | |
1451 | } | |
28dcb9ed | 1452 | |
1453 | /* Propagate life info through the basic blocks | |
1454 | around the graph of basic blocks. | |
1455 | ||
1456 | This is a relaxation process: each time a new register | |
1457 | is live at the end of the basic block, we must scan the block | |
1458 | to determine which registers are, as a consequence, live at the beginning | |
1459 | of that block. These registers must then be marked live at the ends | |
1460 | of all the blocks that can transfer control to that block. | |
1461 | The process continues until it reaches a fixed point. */ | |
1462 | ||
1463 | first_pass = 1; | |
1464 | changed = 1; | |
1465 | while (changed) | |
1466 | { | |
1467 | changed = 0; | |
1468 | for (i = n_basic_blocks - 1; i >= 0; i--) | |
1469 | { | |
1470 | int consider = first_pass; | |
1471 | int must_rescan = first_pass; | |
1472 | register int j; | |
1473 | ||
1474 | if (!first_pass) | |
1475 | { | |
1476 | /* Set CONSIDER if this block needs thinking about at all | |
1477 | (that is, if the regs live now at the end of it | |
1478 | are not the same as were live at the end of it when | |
1479 | we last thought about it). | |
1480 | Set must_rescan if it needs to be thought about | |
1481 | instruction by instruction (that is, if any additional | |
1482 | reg that is live at the end now but was not live there before | |
1483 | is one of the significant regs of this basic block). */ | |
1484 | ||
d4d7427d | 1485 | EXECUTE_IF_AND_COMPL_IN_REG_SET |
1486 | (basic_block_new_live_at_end[i], | |
1487 | basic_block_live_at_end[i], 0, j, | |
1488 | { | |
1489 | consider = 1; | |
5286e409 | 1490 | if (REGNO_REG_SET_P (basic_block_significant[i], j)) |
d4d7427d | 1491 | { |
1492 | must_rescan = 1; | |
1493 | goto done; | |
1494 | } | |
1495 | }); | |
74666a14 | 1496 | done: |
28dcb9ed | 1497 | if (! consider) |
1498 | continue; | |
1499 | } | |
1500 | ||
1501 | /* The live_at_start of this block may be changing, | |
1502 | so another pass will be required after this one. */ | |
1503 | changed = 1; | |
1504 | ||
1505 | if (! must_rescan) | |
1506 | { | |
1507 | /* No complete rescan needed; | |
1508 | just record those variables newly known live at end | |
1509 | as live at start as well. */ | |
74666a14 | 1510 | IOR_AND_COMPL_REG_SET (basic_block_live_at_start[i], |
1511 | basic_block_new_live_at_end[i], | |
1512 | basic_block_live_at_end[i]); | |
1513 | ||
1514 | IOR_AND_COMPL_REG_SET (basic_block_live_at_end[i], | |
1515 | basic_block_new_live_at_end[i], | |
1516 | basic_block_live_at_end[i]); | |
28dcb9ed | 1517 | } |
1518 | else | |
1519 | { | |
1520 | /* Update the basic_block_live_at_start | |
1521 | by propagation backwards through the block. */ | |
74666a14 | 1522 | COPY_REG_SET (basic_block_live_at_end[i], |
1523 | basic_block_new_live_at_end[i]); | |
1524 | COPY_REG_SET (basic_block_live_at_start[i], | |
1525 | basic_block_live_at_end[i]); | |
28dcb9ed | 1526 | propagate_block (basic_block_live_at_start[i], |
68676d00 | 1527 | BLOCK_HEAD (i), BLOCK_END (i), 0, |
1bb04728 | 1528 | first_pass ? basic_block_significant[i] |
1529 | : (regset) 0, | |
28dcb9ed | 1530 | i); |
1531 | } | |
1532 | ||
1533 | { | |
22548064 | 1534 | int_list_ptr p; |
1b5174f0 | 1535 | |
28dcb9ed | 1536 | /* Update the basic_block_new_live_at_end's of |
22548064 | 1537 | all the blocks that reach this one. */ |
1538 | for (p = basic_block_pred[i]; p; p = p->next) | |
1539 | { | |
1540 | register int from_block = INT_LIST_VAL (p); | |
1541 | IOR_REG_SET (basic_block_new_live_at_end[from_block], | |
1542 | basic_block_live_at_start[i]); | |
1543 | } | |
28dcb9ed | 1544 | } |
1545 | #ifdef USE_C_ALLOCA | |
1546 | alloca (0); | |
1547 | #endif | |
1548 | } | |
1549 | first_pass = 0; | |
1550 | } | |
1551 | ||
1552 | /* The only pseudos that are live at the beginning of the function are | |
1553 | those that were not set anywhere in the function. local-alloc doesn't | |
1554 | know how to handle these correctly, so mark them as not local to any | |
1555 | one basic block. */ | |
1556 | ||
1557 | if (n_basic_blocks > 0) | |
74666a14 | 1558 | EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[0], |
1559 | FIRST_PSEUDO_REGISTER, i, | |
1560 | { | |
1561 | REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; | |
1562 | }); | |
28dcb9ed | 1563 | |
1564 | /* Now the life information is accurate. | |
1565 | Make one more pass over each basic block | |
1566 | to delete dead stores, create autoincrement addressing | |
1567 | and record how many times each register is used, is set, or dies. | |
1568 | ||
1569 | To save time, we operate directly in basic_block_live_at_end[i], | |
1570 | thus destroying it (in fact, converting it into a copy of | |
1571 | basic_block_live_at_start[i]). This is ok now because | |
1572 | basic_block_live_at_end[i] is no longer used past this point. */ | |
1573 | ||
28dcb9ed | 1574 | for (i = 0; i < n_basic_blocks; i++) |
1575 | { | |
1576 | propagate_block (basic_block_live_at_end[i], | |
68676d00 | 1577 | BLOCK_HEAD (i), BLOCK_END (i), 1, |
1bb04728 | 1578 | (regset) 0, i); |
28dcb9ed | 1579 | #ifdef USE_C_ALLOCA |
1580 | alloca (0); | |
1581 | #endif | |
1582 | } | |
1583 | ||
1584 | #if 0 | |
1585 | /* Something live during a setjmp should not be put in a register | |
1586 | on certain machines which restore regs from stack frames | |
1587 | rather than from the jmpbuf. | |
1588 | But we don't need to do this for the user's variables, since | |
1589 | ANSI says only volatile variables need this. */ | |
1590 | #ifdef LONGJMP_RESTORE_FROM_STACK | |
74666a14 | 1591 | EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp, |
1592 | FIRST_PSEUDO_REGISTER, i, | |
1593 | { | |
1594 | if (regno_reg_rtx[i] != 0 | |
1595 | && ! REG_USERVAR_P (regno_reg_rtx[i])) | |
1596 | { | |
1597 | REG_LIVE_LENGTH (i) = -1; | |
1598 | REG_BASIC_BLOCK (i) = -1; | |
1599 | } | |
1600 | }); | |
28dcb9ed | 1601 | #endif |
1602 | #endif | |
1603 | ||
1604 | /* We have a problem with any pseudoreg that | |
1605 | lives across the setjmp. ANSI says that if a | |
1606 | user variable does not change in value | |
1607 | between the setjmp and the longjmp, then the longjmp preserves it. | |
1608 | This includes longjmp from a place where the pseudo appears dead. | |
1609 | (In principle, the value still exists if it is in scope.) | |
1610 | If the pseudo goes in a hard reg, some other value may occupy | |
1611 | that hard reg where this pseudo is dead, thus clobbering the pseudo. | |
1612 | Conclusion: such a pseudo must not go in a hard reg. */ | |
74666a14 | 1613 | EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp, |
1614 | FIRST_PSEUDO_REGISTER, i, | |
1615 | { | |
1616 | if (regno_reg_rtx[i] != 0) | |
1617 | { | |
1618 | REG_LIVE_LENGTH (i) = -1; | |
1619 | REG_BASIC_BLOCK (i) = -1; | |
1620 | } | |
1621 | }); | |
28dcb9ed | 1622 | |
3f4d644c | 1623 | /* Restore regs_ever_live that was provided by reload. */ |
1624 | if (reload_completed) | |
1625 | bcopy (save_regs_ever_live, regs_ever_live, (sizeof (regs_ever_live))); | |
7ee969d0 | 1626 | |
1627 | free_regset_vector (basic_block_live_at_end, n_basic_blocks); | |
1628 | free_regset_vector (basic_block_new_live_at_end, n_basic_blocks); | |
1629 | free_regset_vector (basic_block_significant, n_basic_blocks); | |
1630 | basic_block_live_at_end = (regset *)0; | |
1631 | basic_block_new_live_at_end = (regset *)0; | |
1632 | basic_block_significant = (regset *)0; | |
1633 | ||
1bb04728 | 1634 | obstack_free (&flow_obstack, NULL_PTR); |
28dcb9ed | 1635 | } |
1636 | \f | |
1637 | /* Subroutines of life analysis. */ | |
1638 | ||
1639 | /* Allocate the permanent data structures that represent the results | |
1640 | of life analysis. Not static since used also for stupid life analysis. */ | |
1641 | ||
1642 | void | |
1643 | allocate_for_life_analysis () | |
1644 | { | |
1645 | register int i; | |
28dcb9ed | 1646 | |
7ee969d0 | 1647 | /* Recalculate the register space, in case it has grown. Old style |
1648 | vector oriented regsets would set regset_{size,bytes} here also. */ | |
1649 | allocate_reg_info (max_regno, FALSE, FALSE); | |
28dcb9ed | 1650 | |
394685a4 | 1651 | /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS |
1652 | information, explicitly reset it here. The allocation should have | |
1653 | already happened on the previous reg_scan pass. Make sure in case | |
1654 | some more registers were allocated. */ | |
28dcb9ed | 1655 | for (i = 0; i < max_regno; i++) |
394685a4 | 1656 | REG_N_SETS (i) = 0; |
28dcb9ed | 1657 | |
748e6d74 | 1658 | basic_block_live_at_start |
1659 | = (regset *) oballoc (n_basic_blocks * sizeof (regset)); | |
7ee969d0 | 1660 | init_regset_vector (basic_block_live_at_start, n_basic_blocks, |
fc1ef759 | 1661 | function_obstack); |
28dcb9ed | 1662 | |
fc1ef759 | 1663 | regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack); |
1664 | CLEAR_REG_SET (regs_live_at_setjmp); | |
28dcb9ed | 1665 | } |
1666 | ||
7ee969d0 | 1667 | /* Make each element of VECTOR point at a regset. The vector has |
1668 | NELTS elements, and space is allocated from the ALLOC_OBSTACK | |
1669 | obstack. */ | |
28dcb9ed | 1670 | |
90a70252 | 1671 | static void |
7ee969d0 | 1672 | init_regset_vector (vector, nelts, alloc_obstack) |
28dcb9ed | 1673 | regset *vector; |
28dcb9ed | 1674 | int nelts; |
fc1ef759 | 1675 | struct obstack *alloc_obstack; |
28dcb9ed | 1676 | { |
1677 | register int i; | |
28dcb9ed | 1678 | |
1679 | for (i = 0; i < nelts; i++) | |
1680 | { | |
fc1ef759 | 1681 | vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack); |
1682 | CLEAR_REG_SET (vector[i]); | |
28dcb9ed | 1683 | } |
1684 | } | |
a123fe25 | 1685 | |
7ee969d0 | 1686 | /* Release any additional space allocated for each element of VECTOR point |
1687 | other than the regset header itself. The vector has NELTS elements. */ | |
1688 | ||
1689 | void | |
1690 | free_regset_vector (vector, nelts) | |
1691 | regset *vector; | |
1692 | int nelts; | |
1693 | { | |
1694 | register int i; | |
1695 | ||
1696 | for (i = 0; i < nelts; i++) | |
1697 | FREE_REG_SET (vector[i]); | |
1698 | } | |
1699 | ||
28dcb9ed | 1700 | /* Compute the registers live at the beginning of a basic block |
1701 | from those live at the end. | |
1702 | ||
1703 | When called, OLD contains those live at the end. | |
1704 | On return, it contains those live at the beginning. | |
1705 | FIRST and LAST are the first and last insns of the basic block. | |
1706 | ||
1707 | FINAL is nonzero if we are doing the final pass which is not | |
1708 | for computing the life info (since that has already been done) | |
1709 | but for acting on it. On this pass, we delete dead stores, | |
1710 | set up the logical links and dead-variables lists of instructions, | |
1711 | and merge instructions for autoincrement and autodecrement addresses. | |
1712 | ||
1713 | SIGNIFICANT is nonzero only the first time for each basic block. | |
1714 | If it is nonzero, it points to a regset in which we store | |
1715 | a 1 for each register that is set within the block. | |
1716 | ||
1717 | BNUM is the number of the basic block. */ | |
1718 | ||
1719 | static void | |
1720 | propagate_block (old, first, last, final, significant, bnum) | |
1721 | register regset old; | |
1722 | rtx first; | |
1723 | rtx last; | |
1724 | int final; | |
1725 | regset significant; | |
1726 | int bnum; | |
1727 | { | |
1728 | register rtx insn; | |
1729 | rtx prev; | |
1730 | regset live; | |
1731 | regset dead; | |
1732 | ||
28dcb9ed | 1733 | /* The loop depth may change in the middle of a basic block. Since we |
1734 | scan from end to beginning, we start with the depth at the end of the | |
1735 | current basic block, and adjust as we pass ends and starts of loops. */ | |
1736 | loop_depth = basic_block_loop_depth[bnum]; | |
1737 | ||
fc1ef759 | 1738 | dead = ALLOCA_REG_SET (); |
1739 | live = ALLOCA_REG_SET (); | |
28dcb9ed | 1740 | |
1741 | cc0_live = 0; | |
d29fc2f4 | 1742 | mem_set_list = NULL_RTX; |
28dcb9ed | 1743 | |
1744 | /* Include any notes at the end of the block in the scan. | |
1745 | This is in case the block ends with a call to setjmp. */ | |
1746 | ||
1747 | while (NEXT_INSN (last) != 0 && GET_CODE (NEXT_INSN (last)) == NOTE) | |
1748 | { | |
1749 | /* Look for loop boundaries, we are going forward here. */ | |
1750 | last = NEXT_INSN (last); | |
1751 | if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_BEG) | |
1752 | loop_depth++; | |
1753 | else if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_END) | |
1754 | loop_depth--; | |
1755 | } | |
1756 | ||
1757 | if (final) | |
1758 | { | |
74666a14 | 1759 | register int i; |
28dcb9ed | 1760 | |
28dcb9ed | 1761 | /* Process the regs live at the end of the block. |
91444b9c | 1762 | Mark them as not local to any one basic block. */ |
74666a14 | 1763 | EXECUTE_IF_SET_IN_REG_SET (old, 0, i, |
1764 | { | |
1765 | REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL; | |
74666a14 | 1766 | }); |
28dcb9ed | 1767 | } |
1768 | ||
1769 | /* Scan the block an insn at a time from end to beginning. */ | |
1770 | ||
1771 | for (insn = last; ; insn = prev) | |
1772 | { | |
1773 | prev = PREV_INSN (insn); | |
1774 | ||
dd1cc380 | 1775 | if (GET_CODE (insn) == NOTE) |
28dcb9ed | 1776 | { |
dd1cc380 | 1777 | /* Look for loop boundaries, remembering that we are going |
1778 | backwards. */ | |
1779 | if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END) | |
1780 | loop_depth++; | |
1781 | else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG) | |
1782 | loop_depth--; | |
1783 | ||
1784 | /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. | |
1785 | Abort now rather than setting register status incorrectly. */ | |
1786 | if (loop_depth == 0) | |
1787 | abort (); | |
1788 | ||
1789 | /* If this is a call to `setjmp' et al, | |
1790 | warn if any non-volatile datum is live. */ | |
1791 | ||
1792 | if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP) | |
74666a14 | 1793 | IOR_REG_SET (regs_live_at_setjmp, old); |
28dcb9ed | 1794 | } |
1795 | ||
1796 | /* Update the life-status of regs for this insn. | |
1797 | First DEAD gets which regs are set in this insn | |
1798 | then LIVE gets which regs are used in this insn. | |
1799 | Then the regs live before the insn | |
1800 | are those live after, with DEAD regs turned off, | |
1801 | and then LIVE regs turned on. */ | |
1802 | ||
dd1cc380 | 1803 | else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i') |
28dcb9ed | 1804 | { |
1805 | register int i; | |
1bb04728 | 1806 | rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX); |
28dcb9ed | 1807 | int insn_is_dead |
71a522b5 | 1808 | = (insn_dead_p (PATTERN (insn), old, 0, REG_NOTES (insn)) |
28dcb9ed | 1809 | /* Don't delete something that refers to volatile storage! */ |
1810 | && ! INSN_VOLATILE (insn)); | |
1811 | int libcall_is_dead | |
1812 | = (insn_is_dead && note != 0 | |
1813 | && libcall_dead_p (PATTERN (insn), old, note, insn)); | |
1814 | ||
1815 | /* If an instruction consists of just dead store(s) on final pass, | |
1816 | "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED. | |
1817 | We could really delete it with delete_insn, but that | |
1818 | can cause trouble for first or last insn in a basic block. */ | |
5286e409 | 1819 | if (final && insn_is_dead) |
28dcb9ed | 1820 | { |
1821 | PUT_CODE (insn, NOTE); | |
1822 | NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED; | |
1823 | NOTE_SOURCE_FILE (insn) = 0; | |
1824 | ||
2b6a4706 | 1825 | /* CC0 is now known to be dead. Either this insn used it, |
1826 | in which case it doesn't anymore, or clobbered it, | |
1827 | so the next insn can't use it. */ | |
1828 | cc0_live = 0; | |
1829 | ||
28dcb9ed | 1830 | /* If this insn is copying the return value from a library call, |
1831 | delete the entire library call. */ | |
1832 | if (libcall_is_dead) | |
1833 | { | |
1834 | rtx first = XEXP (note, 0); | |
1835 | rtx p = insn; | |
1836 | while (INSN_DELETED_P (first)) | |
1837 | first = NEXT_INSN (first); | |
1838 | while (p != first) | |
1839 | { | |
1840 | p = PREV_INSN (p); | |
1841 | PUT_CODE (p, NOTE); | |
1842 | NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED; | |
1843 | NOTE_SOURCE_FILE (p) = 0; | |
1844 | } | |
1845 | } | |
1846 | goto flushed; | |
1847 | } | |
1848 | ||
74666a14 | 1849 | CLEAR_REG_SET (dead); |
1850 | CLEAR_REG_SET (live); | |
28dcb9ed | 1851 | |
1852 | /* See if this is an increment or decrement that can be | |
1853 | merged into a following memory address. */ | |
1854 | #ifdef AUTO_INC_DEC | |
1855 | { | |
ad87de1e | 1856 | register rtx x = single_set (insn); |
1857 | ||
28dcb9ed | 1858 | /* Does this instruction increment or decrement a register? */ |
3f4d644c | 1859 | if (!reload_completed |
1860 | && final && x != 0 | |
28dcb9ed | 1861 | && GET_CODE (SET_DEST (x)) == REG |
1862 | && (GET_CODE (SET_SRC (x)) == PLUS | |
1863 | || GET_CODE (SET_SRC (x)) == MINUS) | |
1864 | && XEXP (SET_SRC (x), 0) == SET_DEST (x) | |
1865 | && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT | |
1866 | /* Ok, look for a following memory ref we can combine with. | |
1867 | If one is found, change the memory ref to a PRE_INC | |
1868 | or PRE_DEC, cancel this insn, and return 1. | |
1869 | Return 0 if nothing has been done. */ | |
1870 | && try_pre_increment_1 (insn)) | |
1871 | goto flushed; | |
1872 | } | |
1873 | #endif /* AUTO_INC_DEC */ | |
1874 | ||
1875 | /* If this is not the final pass, and this insn is copying the | |
1876 | value of a library call and it's dead, don't scan the | |
1877 | insns that perform the library call, so that the call's | |
1878 | arguments are not marked live. */ | |
1879 | if (libcall_is_dead) | |
1880 | { | |
1881 | /* Mark the dest reg as `significant'. */ | |
1bb04728 | 1882 | mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant); |
28dcb9ed | 1883 | |
1884 | insn = XEXP (note, 0); | |
1885 | prev = PREV_INSN (insn); | |
1886 | } | |
1887 | else if (GET_CODE (PATTERN (insn)) == SET | |
1888 | && SET_DEST (PATTERN (insn)) == stack_pointer_rtx | |
1889 | && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS | |
1890 | && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx | |
1891 | && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT) | |
1892 | /* We have an insn to pop a constant amount off the stack. | |
1893 | (Such insns use PLUS regardless of the direction of the stack, | |
1894 | and any insn to adjust the stack by a constant is always a pop.) | |
1895 | These insns, if not dead stores, have no effect on life. */ | |
1896 | ; | |
1897 | else | |
1898 | { | |
91444b9c | 1899 | /* Any regs live at the time of a call instruction |
1900 | must not go in a register clobbered by calls. | |
1901 | Find all regs now live and record this for them. */ | |
1902 | ||
1903 | if (GET_CODE (insn) == CALL_INSN && final) | |
1904 | EXECUTE_IF_SET_IN_REG_SET (old, 0, i, | |
1905 | { | |
1906 | REG_N_CALLS_CROSSED (i)++; | |
1907 | }); | |
1908 | ||
28dcb9ed | 1909 | /* LIVE gets the regs used in INSN; |
1910 | DEAD gets those set by it. Dead insns don't make anything | |
1911 | live. */ | |
1912 | ||
1bb04728 | 1913 | mark_set_regs (old, dead, PATTERN (insn), |
1914 | final ? insn : NULL_RTX, significant); | |
28dcb9ed | 1915 | |
1916 | /* If an insn doesn't use CC0, it becomes dead since we | |
1917 | assume that every insn clobbers it. So show it dead here; | |
1918 | mark_used_regs will set it live if it is referenced. */ | |
1919 | cc0_live = 0; | |
1920 | ||
1921 | if (! insn_is_dead) | |
1922 | mark_used_regs (old, live, PATTERN (insn), final, insn); | |
1923 | ||
1924 | /* Sometimes we may have inserted something before INSN (such as | |
1925 | a move) when we make an auto-inc. So ensure we will scan | |
1926 | those insns. */ | |
1927 | #ifdef AUTO_INC_DEC | |
1928 | prev = PREV_INSN (insn); | |
1929 | #endif | |
1930 | ||
1931 | if (! insn_is_dead && GET_CODE (insn) == CALL_INSN) | |
1932 | { | |
1933 | register int i; | |
1934 | ||
f11fd6c8 | 1935 | rtx note; |
1936 | ||
1937 | for (note = CALL_INSN_FUNCTION_USAGE (insn); | |
1938 | note; | |
1939 | note = XEXP (note, 1)) | |
1940 | if (GET_CODE (XEXP (note, 0)) == USE) | |
1941 | mark_used_regs (old, live, SET_DEST (XEXP (note, 0)), | |
1942 | final, insn); | |
1943 | ||
28dcb9ed | 1944 | /* Each call clobbers all call-clobbered regs that are not |
5335b84a | 1945 | global or fixed. Note that the function-value reg is a |
28dcb9ed | 1946 | call-clobbered reg, and mark_set_regs has already had |
1947 | a chance to handle it. */ | |
1948 | ||
1949 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) | |
5335b84a | 1950 | if (call_used_regs[i] && ! global_regs[i] |
1951 | && ! fixed_regs[i]) | |
74666a14 | 1952 | SET_REGNO_REG_SET (dead, i); |
28dcb9ed | 1953 | |
1954 | /* The stack ptr is used (honorarily) by a CALL insn. */ | |
74666a14 | 1955 | SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM); |
28dcb9ed | 1956 | |
1957 | /* Calls may also reference any of the global registers, | |
1958 | so they are made live. */ | |
28dcb9ed | 1959 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) |
1960 | if (global_regs[i]) | |
83573809 | 1961 | mark_used_regs (old, live, |
941522d6 | 1962 | gen_rtx_REG (reg_raw_mode[i], i), |
83573809 | 1963 | final, insn); |
28dcb9ed | 1964 | |
1965 | /* Calls also clobber memory. */ | |
d29fc2f4 | 1966 | mem_set_list = NULL_RTX; |
28dcb9ed | 1967 | } |
1968 | ||
1969 | /* Update OLD for the registers used or set. */ | |
74666a14 | 1970 | AND_COMPL_REG_SET (old, dead); |
1971 | IOR_REG_SET (old, live); | |
28dcb9ed | 1972 | |
28dcb9ed | 1973 | } |
1974 | ||
91444b9c | 1975 | /* On final pass, update counts of how many insns each reg is live |
1976 | at. */ | |
28dcb9ed | 1977 | if (final) |
91444b9c | 1978 | EXECUTE_IF_SET_IN_REG_SET (old, 0, i, |
1979 | { REG_LIVE_LENGTH (i)++; }); | |
28dcb9ed | 1980 | } |
1981 | flushed: ; | |
1982 | if (insn == first) | |
1983 | break; | |
1984 | } | |
1985 | ||
7ee969d0 | 1986 | FREE_REG_SET (dead); |
1987 | FREE_REG_SET (live); | |
28dcb9ed | 1988 | } |
1989 | \f | |
1990 | /* Return 1 if X (the body of an insn, or part of it) is just dead stores | |
1991 | (SET expressions whose destinations are registers dead after the insn). | |
1992 | NEEDED is the regset that says which regs are alive after the insn. | |
1993 | ||
71a522b5 | 1994 | Unless CALL_OK is non-zero, an insn is needed if it contains a CALL. |
1995 | ||
1996 | If X is the entire body of an insn, NOTES contains the reg notes | |
1997 | pertaining to the insn. */ | |
28dcb9ed | 1998 | |
1999 | static int | |
71a522b5 | 2000 | insn_dead_p (x, needed, call_ok, notes) |
28dcb9ed | 2001 | rtx x; |
2002 | regset needed; | |
2003 | int call_ok; | |
71a522b5 | 2004 | rtx notes ATTRIBUTE_UNUSED; |
28dcb9ed | 2005 | { |
997d68fe | 2006 | enum rtx_code code = GET_CODE (x); |
2007 | ||
71a522b5 | 2008 | #ifdef AUTO_INC_DEC |
2009 | /* If flow is invoked after reload, we must take existing AUTO_INC | |
2010 | expresions into account. */ | |
2011 | if (reload_completed) | |
2012 | { | |
2013 | for ( ; notes; notes = XEXP (notes, 1)) | |
2014 | { | |
2015 | if (REG_NOTE_KIND (notes) == REG_INC) | |
2016 | { | |
2017 | int regno = REGNO (XEXP (notes, 0)); | |
2018 | ||
2019 | /* Don't delete insns to set global regs. */ | |
2020 | if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno]) | |
2021 | || REGNO_REG_SET_P (needed, regno)) | |
2022 | return 0; | |
2023 | } | |
2024 | } | |
2025 | } | |
2026 | #endif | |
2027 | ||
28dcb9ed | 2028 | /* If setting something that's a reg or part of one, |
2029 | see if that register's altered value will be live. */ | |
2030 | ||
2031 | if (code == SET) | |
2032 | { | |
997d68fe | 2033 | rtx r = SET_DEST (x); |
2034 | ||
28dcb9ed | 2035 | /* A SET that is a subroutine call cannot be dead. */ |
2036 | if (! call_ok && GET_CODE (SET_SRC (x)) == CALL) | |
2037 | return 0; | |
2038 | ||
2039 | #ifdef HAVE_cc0 | |
2040 | if (GET_CODE (r) == CC0) | |
2041 | return ! cc0_live; | |
2042 | #endif | |
2043 | ||
d29fc2f4 | 2044 | if (GET_CODE (r) == MEM && ! MEM_VOLATILE_P (r)) |
2045 | { | |
2046 | rtx temp; | |
2047 | /* Walk the set of memory locations we are currently tracking | |
2048 | and see if one is an identical match to this memory location. | |
2049 | If so, this memory write is dead (remember, we're walking | |
2050 | backwards from the end of the block to the start. */ | |
2051 | temp = mem_set_list; | |
2052 | while (temp) | |
2053 | { | |
2054 | if (rtx_equal_p (XEXP (temp, 0), r)) | |
2055 | return 1; | |
2056 | temp = XEXP (temp, 1); | |
2057 | } | |
2058 | } | |
28dcb9ed | 2059 | |
997d68fe | 2060 | while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART |
2061 | || GET_CODE (r) == ZERO_EXTRACT) | |
28dcb9ed | 2062 | r = SUBREG_REG (r); |
2063 | ||
2064 | if (GET_CODE (r) == REG) | |
2065 | { | |
997d68fe | 2066 | int regno = REGNO (r); |
28dcb9ed | 2067 | |
c8e3d518 | 2068 | /* Don't delete insns to set global regs. */ |
28dcb9ed | 2069 | if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno]) |
2070 | /* Make sure insns to set frame pointer aren't deleted. */ | |
2071 | || regno == FRAME_POINTER_REGNUM | |
4dd5e02b | 2072 | #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM |
2073 | || regno == HARD_FRAME_POINTER_REGNUM | |
2074 | #endif | |
05b74099 | 2075 | #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM |
2076 | /* Make sure insns to set arg pointer are never deleted | |
2077 | (if the arg pointer isn't fixed, there will be a USE for | |
a92771b8 | 2078 | it, so we can treat it normally). */ |
05b74099 | 2079 | || (regno == ARG_POINTER_REGNUM && fixed_regs[regno]) |
2080 | #endif | |
74666a14 | 2081 | || REGNO_REG_SET_P (needed, regno)) |
28dcb9ed | 2082 | return 0; |
2083 | ||
2084 | /* If this is a hard register, verify that subsequent words are | |
2085 | not needed. */ | |
2086 | if (regno < FIRST_PSEUDO_REGISTER) | |
2087 | { | |
2088 | int n = HARD_REGNO_NREGS (regno, GET_MODE (r)); | |
2089 | ||
2090 | while (--n > 0) | |
74666a14 | 2091 | if (REGNO_REG_SET_P (needed, regno+n)) |
28dcb9ed | 2092 | return 0; |
2093 | } | |
2094 | ||
2095 | return 1; | |
2096 | } | |
2097 | } | |
997d68fe | 2098 | |
28dcb9ed | 2099 | /* If performing several activities, |
2100 | insn is dead if each activity is individually dead. | |
2101 | Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE | |
2102 | that's inside a PARALLEL doesn't make the insn worth keeping. */ | |
2103 | else if (code == PARALLEL) | |
2104 | { | |
997d68fe | 2105 | int i = XVECLEN (x, 0); |
2106 | ||
28dcb9ed | 2107 | for (i--; i >= 0; i--) |
997d68fe | 2108 | if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER |
2109 | && GET_CODE (XVECEXP (x, 0, i)) != USE | |
71a522b5 | 2110 | && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok, NULL_RTX)) |
997d68fe | 2111 | return 0; |
2112 | ||
28dcb9ed | 2113 | return 1; |
2114 | } | |
997d68fe | 2115 | |
2116 | /* A CLOBBER of a pseudo-register that is dead serves no purpose. That | |
2117 | is not necessarily true for hard registers. */ | |
2118 | else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG | |
2119 | && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER | |
2120 | && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0)))) | |
2121 | return 1; | |
2122 | ||
2123 | /* We do not check other CLOBBER or USE here. An insn consisting of just | |
2124 | a CLOBBER or just a USE should not be deleted. */ | |
28dcb9ed | 2125 | return 0; |
2126 | } | |
2127 | ||
2128 | /* If X is the pattern of the last insn in a libcall, and assuming X is dead, | |
2129 | return 1 if the entire library call is dead. | |
2130 | This is true if X copies a register (hard or pseudo) | |
2131 | and if the hard return reg of the call insn is dead. | |
2132 | (The caller should have tested the destination of X already for death.) | |
2133 | ||
2134 | If this insn doesn't just copy a register, then we don't | |
2135 | have an ordinary libcall. In that case, cse could not have | |
2136 | managed to substitute the source for the dest later on, | |
2137 | so we can assume the libcall is dead. | |
2138 | ||
2139 | NEEDED is the bit vector of pseudoregs live before this insn. | |
2140 | NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */ | |
2141 | ||
2142 | static int | |
2143 | libcall_dead_p (x, needed, note, insn) | |
2144 | rtx x; | |
2145 | regset needed; | |
2146 | rtx note; | |
2147 | rtx insn; | |
2148 | { | |
2149 | register RTX_CODE code = GET_CODE (x); | |
2150 | ||
2151 | if (code == SET) | |
2152 | { | |
2153 | register rtx r = SET_SRC (x); | |
2154 | if (GET_CODE (r) == REG) | |
2155 | { | |
2156 | rtx call = XEXP (note, 0); | |
71a522b5 | 2157 | rtx call_pat; |
28dcb9ed | 2158 | register int i; |
2159 | ||
2160 | /* Find the call insn. */ | |
2161 | while (call != insn && GET_CODE (call) != CALL_INSN) | |
2162 | call = NEXT_INSN (call); | |
2163 | ||
2164 | /* If there is none, do nothing special, | |
2165 | since ordinary death handling can understand these insns. */ | |
2166 | if (call == insn) | |
2167 | return 0; | |
2168 | ||
2169 | /* See if the hard reg holding the value is dead. | |
2170 | If this is a PARALLEL, find the call within it. */ | |
71a522b5 | 2171 | call_pat = PATTERN (call); |
2172 | if (GET_CODE (call_pat) == PARALLEL) | |
28dcb9ed | 2173 | { |
71a522b5 | 2174 | for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--) |
2175 | if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET | |
2176 | && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL) | |
28dcb9ed | 2177 | break; |
2178 | ||
adc14fe9 | 2179 | /* This may be a library call that is returning a value |
2180 | via invisible pointer. Do nothing special, since | |
2181 | ordinary death handling can understand these insns. */ | |
28dcb9ed | 2182 | if (i < 0) |
adc14fe9 | 2183 | return 0; |
28dcb9ed | 2184 | |
71a522b5 | 2185 | call_pat = XVECEXP (call_pat, 0, i); |
28dcb9ed | 2186 | } |
2187 | ||
71a522b5 | 2188 | return insn_dead_p (call_pat, needed, 1, REG_NOTES (call)); |
28dcb9ed | 2189 | } |
2190 | } | |
2191 | return 1; | |
2192 | } | |
2193 | ||
159c21fd | 2194 | /* Return 1 if register REGNO was used before it was set, i.e. if it is |
2195 | live at function entry. Don't count global register variables, variables | |
2196 | in registers that can be used for function arg passing, or variables in | |
2197 | fixed hard registers. */ | |
28dcb9ed | 2198 | |
2199 | int | |
2200 | regno_uninitialized (regno) | |
2201 | int regno; | |
2202 | { | |
285bad0b | 2203 | if (n_basic_blocks == 0 |
d4c423d6 | 2204 | || (regno < FIRST_PSEUDO_REGISTER |
159c21fd | 2205 | && (global_regs[regno] |
2206 | || fixed_regs[regno] | |
2207 | || FUNCTION_ARG_REGNO_P (regno)))) | |
28dcb9ed | 2208 | return 0; |
2209 | ||
74666a14 | 2210 | return REGNO_REG_SET_P (basic_block_live_at_start[0], regno); |
28dcb9ed | 2211 | } |
2212 | ||
2213 | /* 1 if register REGNO was alive at a place where `setjmp' was called | |
2214 | and was set more than once or is an argument. | |
2215 | Such regs may be clobbered by `longjmp'. */ | |
2216 | ||
2217 | int | |
2218 | regno_clobbered_at_setjmp (regno) | |
2219 | int regno; | |
2220 | { | |
2221 | if (n_basic_blocks == 0) | |
2222 | return 0; | |
2223 | ||
394685a4 | 2224 | return ((REG_N_SETS (regno) > 1 |
74666a14 | 2225 | || REGNO_REG_SET_P (basic_block_live_at_start[0], regno)) |
2226 | && REGNO_REG_SET_P (regs_live_at_setjmp, regno)); | |
28dcb9ed | 2227 | } |
2228 | \f | |
a447bb5e | 2229 | /* INSN references memory, possibly using autoincrement addressing modes. |
2230 | Find any entries on the mem_set_list that need to be invalidated due | |
2231 | to an address change. */ | |
2232 | static void | |
2233 | invalidate_mems_from_autoinc (insn) | |
2234 | rtx insn; | |
2235 | { | |
2236 | rtx note = REG_NOTES (insn); | |
2237 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | |
2238 | { | |
2239 | if (REG_NOTE_KIND (note) == REG_INC) | |
2240 | { | |
2241 | rtx temp = mem_set_list; | |
2242 | rtx prev = NULL_RTX; | |
2243 | ||
2244 | while (temp) | |
2245 | { | |
2246 | if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0))) | |
2247 | { | |
2248 | /* Splice temp out of list. */ | |
2249 | if (prev) | |
2250 | XEXP (prev, 1) = XEXP (temp, 1); | |
2251 | else | |
2252 | mem_set_list = XEXP (temp, 1); | |
2253 | } | |
2254 | else | |
2255 | prev = temp; | |
2256 | temp = XEXP (temp, 1); | |
2257 | } | |
2258 | } | |
2259 | } | |
2260 | } | |
2261 | ||
28dcb9ed | 2262 | /* Process the registers that are set within X. |
2263 | Their bits are set to 1 in the regset DEAD, | |
2264 | because they are dead prior to this insn. | |
2265 | ||
2266 | If INSN is nonzero, it is the insn being processed | |
2267 | and the fact that it is nonzero implies this is the FINAL pass | |
2268 | in propagate_block. In this case, various info about register | |
2269 | usage is stored, LOG_LINKS fields of insns are set up. */ | |
2270 | ||
28dcb9ed | 2271 | static void |
2272 | mark_set_regs (needed, dead, x, insn, significant) | |
2273 | regset needed; | |
2274 | regset dead; | |
2275 | rtx x; | |
2276 | rtx insn; | |
2277 | regset significant; | |
2278 | { | |
2279 | register RTX_CODE code = GET_CODE (x); | |
2280 | ||
2281 | if (code == SET || code == CLOBBER) | |
2282 | mark_set_1 (needed, dead, x, insn, significant); | |
2283 | else if (code == PARALLEL) | |
2284 | { | |
2285 | register int i; | |
2286 | for (i = XVECLEN (x, 0) - 1; i >= 0; i--) | |
2287 | { | |
2288 | code = GET_CODE (XVECEXP (x, 0, i)); | |
2289 | if (code == SET || code == CLOBBER) | |
2290 | mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant); | |
2291 | } | |
2292 | } | |
2293 | } | |
2294 | ||
2295 | /* Process a single SET rtx, X. */ | |
2296 | ||
2297 | static void | |
2298 | mark_set_1 (needed, dead, x, insn, significant) | |
2299 | regset needed; | |
2300 | regset dead; | |
2301 | rtx x; | |
2302 | rtx insn; | |
2303 | regset significant; | |
2304 | { | |
2305 | register int regno; | |
2306 | register rtx reg = SET_DEST (x); | |
2307 | ||
53309b20 | 2308 | /* Some targets place small structures in registers for |
2309 | return values of functions. We have to detect this | |
2310 | case specially here to get correct flow information. */ | |
2311 | if (GET_CODE (reg) == PARALLEL | |
2312 | && GET_MODE (reg) == BLKmode) | |
2313 | { | |
2314 | register int i; | |
2315 | ||
2316 | for (i = XVECLEN (reg, 0) - 1; i >= 0; i--) | |
2317 | mark_set_1 (needed, dead, XVECEXP (reg, 0, i), insn, significant); | |
2318 | return; | |
2319 | } | |
2320 | ||
28dcb9ed | 2321 | /* Modifying just one hardware register of a multi-reg value |
2322 | or just a byte field of a register | |
2323 | does not mean the value from before this insn is now dead. | |
2324 | But it does mean liveness of that register at the end of the block | |
2325 | is significant. | |
2326 | ||
2327 | Within mark_set_1, however, we treat it as if the register is | |
2328 | indeed modified. mark_used_regs will, however, also treat this | |
2329 | register as being used. Thus, we treat these insns as setting a | |
2330 | new value for the register as a function of its old value. This | |
2331 | cases LOG_LINKS to be made appropriately and this will help combine. */ | |
2332 | ||
2333 | while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT | |
2334 | || GET_CODE (reg) == SIGN_EXTRACT | |
2335 | || GET_CODE (reg) == STRICT_LOW_PART) | |
2336 | reg = XEXP (reg, 0); | |
2337 | ||
d29fc2f4 | 2338 | /* If this set is a MEM, then it kills any aliased writes. |
2339 | If this set is a REG, then it kills any MEMs which use the reg. */ | |
28dcb9ed | 2340 | if (GET_CODE (reg) == MEM |
d29fc2f4 | 2341 | || GET_CODE (reg) == REG) |
2342 | { | |
2343 | rtx temp = mem_set_list; | |
2344 | rtx prev = NULL_RTX; | |
2345 | ||
2346 | while (temp) | |
2347 | { | |
2348 | if ((GET_CODE (reg) == MEM | |
2349 | && output_dependence (XEXP (temp, 0), reg)) | |
2350 | || (GET_CODE (reg) == REG | |
2351 | && reg_overlap_mentioned_p (reg, XEXP (temp, 0)))) | |
2352 | { | |
2353 | /* Splice this entry out of the list. */ | |
2354 | if (prev) | |
2355 | XEXP (prev, 1) = XEXP (temp, 1); | |
2356 | else | |
2357 | mem_set_list = XEXP (temp, 1); | |
2358 | } | |
2359 | else | |
2360 | prev = temp; | |
2361 | temp = XEXP (temp, 1); | |
2362 | } | |
2363 | } | |
a447bb5e | 2364 | |
2365 | /* If the memory reference had embedded side effects (autoincrement | |
2366 | address modes. Then we may need to kill some entries on the | |
2367 | memory set list. */ | |
2368 | if (insn && GET_CODE (reg) == MEM) | |
2369 | invalidate_mems_from_autoinc (insn); | |
2370 | ||
28dcb9ed | 2371 | if (GET_CODE (reg) == MEM && ! side_effects_p (reg) |
2372 | /* There are no REG_INC notes for SP, so we can't assume we'll see | |
2373 | everything that invalidates it. To be safe, don't eliminate any | |
2374 | stores though SP; none of them should be redundant anyway. */ | |
2375 | && ! reg_mentioned_p (stack_pointer_rtx, reg)) | |
d29fc2f4 | 2376 | mem_set_list = gen_rtx_EXPR_LIST (VOIDmode, reg, mem_set_list); |
28dcb9ed | 2377 | |
2378 | if (GET_CODE (reg) == REG | |
2379 | && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM) | |
4dd5e02b | 2380 | #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM |
2381 | && regno != HARD_FRAME_POINTER_REGNUM | |
2382 | #endif | |
05b74099 | 2383 | #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM |
2384 | && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno]) | |
2385 | #endif | |
28dcb9ed | 2386 | && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])) |
2387 | /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */ | |
2388 | { | |
74666a14 | 2389 | int some_needed = REGNO_REG_SET_P (needed, regno); |
2390 | int some_not_needed = ! some_needed; | |
28dcb9ed | 2391 | |
2392 | /* Mark it as a significant register for this basic block. */ | |
2393 | if (significant) | |
74666a14 | 2394 | SET_REGNO_REG_SET (significant, regno); |
28dcb9ed | 2395 | |
3398e91d | 2396 | /* Mark it as dead before this insn. */ |
74666a14 | 2397 | SET_REGNO_REG_SET (dead, regno); |
28dcb9ed | 2398 | |
2399 | /* A hard reg in a wide mode may really be multiple registers. | |
2400 | If so, mark all of them just like the first. */ | |
2401 | if (regno < FIRST_PSEUDO_REGISTER) | |
2402 | { | |
2403 | int n; | |
2404 | ||
2405 | /* Nothing below is needed for the stack pointer; get out asap. | |
2406 | Eg, log links aren't needed, since combine won't use them. */ | |
2407 | if (regno == STACK_POINTER_REGNUM) | |
2408 | return; | |
2409 | ||
2410 | n = HARD_REGNO_NREGS (regno, GET_MODE (reg)); | |
2411 | while (--n > 0) | |
2412 | { | |
74666a14 | 2413 | int regno_n = regno + n; |
2414 | int needed_regno = REGNO_REG_SET_P (needed, regno_n); | |
28dcb9ed | 2415 | if (significant) |
74666a14 | 2416 | SET_REGNO_REG_SET (significant, regno_n); |
d0f99e96 | 2417 | |
74666a14 | 2418 | SET_REGNO_REG_SET (dead, regno_n); |
2419 | some_needed |= needed_regno; | |
2420 | some_not_needed |= ! needed_regno; | |
28dcb9ed | 2421 | } |
2422 | } | |
2423 | /* Additional data to record if this is the final pass. */ | |
2424 | if (insn) | |
2425 | { | |
2426 | register rtx y = reg_next_use[regno]; | |
2427 | register int blocknum = BLOCK_NUM (insn); | |
2428 | ||
2429 | /* If this is a hard reg, record this function uses the reg. */ | |
2430 | ||
2431 | if (regno < FIRST_PSEUDO_REGISTER) | |
2432 | { | |
2433 | register int i; | |
2434 | int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg)); | |
2435 | ||
2436 | for (i = regno; i < endregno; i++) | |
2437 | { | |
96450661 | 2438 | /* The next use is no longer "next", since a store |
2439 | intervenes. */ | |
2440 | reg_next_use[i] = 0; | |
2441 | ||
28dcb9ed | 2442 | regs_ever_live[i] = 1; |
394685a4 | 2443 | REG_N_SETS (i)++; |
28dcb9ed | 2444 | } |
2445 | } | |
2446 | else | |
2447 | { | |
96450661 | 2448 | /* The next use is no longer "next", since a store |
2449 | intervenes. */ | |
2450 | reg_next_use[regno] = 0; | |
2451 | ||
28dcb9ed | 2452 | /* Keep track of which basic blocks each reg appears in. */ |
2453 | ||
394685a4 | 2454 | if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN) |
2455 | REG_BASIC_BLOCK (regno) = blocknum; | |
2456 | else if (REG_BASIC_BLOCK (regno) != blocknum) | |
2457 | REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL; | |
28dcb9ed | 2458 | |
2459 | /* Count (weighted) references, stores, etc. This counts a | |
2460 | register twice if it is modified, but that is correct. */ | |
394685a4 | 2461 | REG_N_SETS (regno)++; |
28dcb9ed | 2462 | |
394685a4 | 2463 | REG_N_REFS (regno) += loop_depth; |
28dcb9ed | 2464 | |
2465 | /* The insns where a reg is live are normally counted | |
2466 | elsewhere, but we want the count to include the insn | |
2467 | where the reg is set, and the normal counting mechanism | |
2468 | would not count it. */ | |
394685a4 | 2469 | REG_LIVE_LENGTH (regno)++; |
28dcb9ed | 2470 | } |
2471 | ||
d0f99e96 | 2472 | if (! some_not_needed) |
28dcb9ed | 2473 | { |
2474 | /* Make a logical link from the next following insn | |
2475 | that uses this register, back to this insn. | |
2476 | The following insns have already been processed. | |
2477 | ||
2478 | We don't build a LOG_LINK for hard registers containing | |
2479 | in ASM_OPERANDs. If these registers get replaced, | |
2480 | we might wind up changing the semantics of the insn, | |
2481 | even if reload can make what appear to be valid assignments | |
2482 | later. */ | |
2483 | if (y && (BLOCK_NUM (y) == blocknum) | |
2484 | && (regno >= FIRST_PSEUDO_REGISTER | |
2485 | || asm_noperands (PATTERN (y)) < 0)) | |
2486 | LOG_LINKS (y) | |
941522d6 | 2487 | = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y)); |
28dcb9ed | 2488 | } |
2489 | else if (! some_needed) | |
2490 | { | |
2491 | /* Note that dead stores have already been deleted when possible | |
2492 | If we get here, we have found a dead store that cannot | |
2493 | be eliminated (because the same insn does something useful). | |
2494 | Indicate this by marking the reg being set as dying here. */ | |
2495 | REG_NOTES (insn) | |
941522d6 | 2496 | = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn)); |
394685a4 | 2497 | REG_N_DEATHS (REGNO (reg))++; |
28dcb9ed | 2498 | } |
2499 | else | |
2500 | { | |
2501 | /* This is a case where we have a multi-word hard register | |
2502 | and some, but not all, of the words of the register are | |
2503 | needed in subsequent insns. Write REG_UNUSED notes | |
2504 | for those parts that were not needed. This case should | |
2505 | be rare. */ | |
2506 | ||
2507 | int i; | |
2508 | ||
2509 | for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1; | |
2510 | i >= 0; i--) | |
74666a14 | 2511 | if (!REGNO_REG_SET_P (needed, regno + i)) |
28dcb9ed | 2512 | REG_NOTES (insn) |
941522d6 | 2513 | = gen_rtx_EXPR_LIST (REG_UNUSED, |
2514 | gen_rtx_REG (reg_raw_mode[regno + i], | |
2515 | regno + i), | |
2516 | REG_NOTES (insn)); | |
28dcb9ed | 2517 | } |
2518 | } | |
2519 | } | |
6c23dbad | 2520 | else if (GET_CODE (reg) == REG) |
2521 | reg_next_use[regno] = 0; | |
28dcb9ed | 2522 | |
2523 | /* If this is the last pass and this is a SCRATCH, show it will be dying | |
2524 | here and count it. */ | |
2525 | else if (GET_CODE (reg) == SCRATCH && insn != 0) | |
2526 | { | |
2527 | REG_NOTES (insn) | |
941522d6 | 2528 | = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn)); |
28dcb9ed | 2529 | } |
2530 | } | |
2531 | \f | |
2532 | #ifdef AUTO_INC_DEC | |
2533 | ||
2534 | /* X is a MEM found in INSN. See if we can convert it into an auto-increment | |
2535 | reference. */ | |
2536 | ||
2537 | static void | |
2538 | find_auto_inc (needed, x, insn) | |
2539 | regset needed; | |
2540 | rtx x; | |
2541 | rtx insn; | |
2542 | { | |
2543 | rtx addr = XEXP (x, 0); | |
a123fe25 | 2544 | HOST_WIDE_INT offset = 0; |
76d20f22 | 2545 | rtx set; |
28dcb9ed | 2546 | |
2547 | /* Here we detect use of an index register which might be good for | |
2548 | postincrement, postdecrement, preincrement, or predecrement. */ | |
2549 | ||
2550 | if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT) | |
2551 | offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0); | |
2552 | ||
2553 | if (GET_CODE (addr) == REG) | |
2554 | { | |
2555 | register rtx y; | |
2556 | register int size = GET_MODE_SIZE (GET_MODE (x)); | |
2557 | rtx use; | |
2558 | rtx incr; | |
2559 | int regno = REGNO (addr); | |
2560 | ||
2561 | /* Is the next use an increment that might make auto-increment? */ | |
76d20f22 | 2562 | if ((incr = reg_next_use[regno]) != 0 |
2563 | && (set = single_set (incr)) != 0 | |
2564 | && GET_CODE (set) == SET | |
28dcb9ed | 2565 | && BLOCK_NUM (incr) == BLOCK_NUM (insn) |
2566 | /* Can't add side effects to jumps; if reg is spilled and | |
2567 | reloaded, there's no way to store back the altered value. */ | |
2568 | && GET_CODE (insn) != JUMP_INSN | |
76d20f22 | 2569 | && (y = SET_SRC (set), GET_CODE (y) == PLUS) |
28dcb9ed | 2570 | && XEXP (y, 0) == addr |
2571 | && GET_CODE (XEXP (y, 1)) == CONST_INT | |
e4e498cf | 2572 | && ((HAVE_POST_INCREMENT |
2573 | && (INTVAL (XEXP (y, 1)) == size && offset == 0)) | |
2574 | || (HAVE_POST_DECREMENT | |
2575 | && (INTVAL (XEXP (y, 1)) == - size && offset == 0)) | |
2576 | || (HAVE_PRE_INCREMENT | |
2577 | && (INTVAL (XEXP (y, 1)) == size && offset == size)) | |
2578 | || (HAVE_PRE_DECREMENT | |
2579 | && (INTVAL (XEXP (y, 1)) == - size && offset == - size))) | |
28dcb9ed | 2580 | /* Make sure this reg appears only once in this insn. */ |
2581 | && (use = find_use_as_address (PATTERN (insn), addr, offset), | |
2582 | use != 0 && use != (rtx) 1)) | |
2583 | { | |
76d20f22 | 2584 | rtx q = SET_DEST (set); |
3a2c0c0c | 2585 | enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size |
2586 | ? (offset ? PRE_INC : POST_INC) | |
2587 | : (offset ? PRE_DEC : POST_DEC)); | |
28dcb9ed | 2588 | |
2589 | if (dead_or_set_p (incr, addr)) | |
3a2c0c0c | 2590 | { |
2591 | /* This is the simple case. Try to make the auto-inc. If | |
2592 | we can't, we are done. Otherwise, we will do any | |
2593 | needed updates below. */ | |
2594 | if (! validate_change (insn, &XEXP (x, 0), | |
941522d6 | 2595 | gen_rtx_fmt_e (inc_code, Pmode, addr), |
3a2c0c0c | 2596 | 0)) |
2597 | return; | |
2598 | } | |
bf613ca1 | 2599 | else if (GET_CODE (q) == REG |
2600 | /* PREV_INSN used here to check the semi-open interval | |
2601 | [insn,incr). */ | |
3c7cf916 | 2602 | && ! reg_used_between_p (q, PREV_INSN (insn), incr) |
2603 | /* We must also check for sets of q as q may be | |
2604 | a call clobbered hard register and there may | |
2605 | be a call between PREV_INSN (insn) and incr. */ | |
2606 | && ! reg_set_between_p (q, PREV_INSN (insn), incr)) | |
28dcb9ed | 2607 | { |
bf613ca1 | 2608 | /* We have *p followed sometime later by q = p+size. |
28dcb9ed | 2609 | Both p and q must be live afterward, |
9e042f31 | 2610 | and q is not used between INSN and its assignment. |
28dcb9ed | 2611 | Change it to q = p, ...*q..., q = q+size. |
2612 | Then fall into the usual case. */ | |
2613 | rtx insns, temp; | |
2614 | ||
2615 | start_sequence (); | |
2616 | emit_move_insn (q, addr); | |
2617 | insns = get_insns (); | |
2618 | end_sequence (); | |
2619 | ||
2620 | /* If anything in INSNS have UID's that don't fit within the | |
2621 | extra space we allocate earlier, we can't make this auto-inc. | |
2622 | This should never happen. */ | |
2623 | for (temp = insns; temp; temp = NEXT_INSN (temp)) | |
2624 | { | |
2625 | if (INSN_UID (temp) > max_uid_for_flow) | |
2626 | return; | |
2627 | BLOCK_NUM (temp) = BLOCK_NUM (insn); | |
2628 | } | |
2629 | ||
3a2c0c0c | 2630 | /* If we can't make the auto-inc, or can't make the |
2631 | replacement into Y, exit. There's no point in making | |
2632 | the change below if we can't do the auto-inc and doing | |
2633 | so is not correct in the pre-inc case. */ | |
2634 | ||
2635 | validate_change (insn, &XEXP (x, 0), | |
941522d6 | 2636 | gen_rtx_fmt_e (inc_code, Pmode, q), |
3a2c0c0c | 2637 | 1); |
2638 | validate_change (incr, &XEXP (y, 0), q, 1); | |
2639 | if (! apply_change_group ()) | |
2640 | return; | |
2641 | ||
2642 | /* We now know we'll be doing this change, so emit the | |
2643 | new insn(s) and do the updates. */ | |
28dcb9ed | 2644 | emit_insns_before (insns, insn); |
c8b98347 | 2645 | |
68676d00 | 2646 | if (BLOCK_HEAD (BLOCK_NUM (insn)) == insn) |
2647 | BLOCK_HEAD (BLOCK_NUM (insn)) = insns; | |
c8b98347 | 2648 | |
28dcb9ed | 2649 | /* INCR will become a NOTE and INSN won't contain a |
2650 | use of ADDR. If a use of ADDR was just placed in | |
2651 | the insn before INSN, make that the next use. | |
2652 | Otherwise, invalidate it. */ | |
2653 | if (GET_CODE (PREV_INSN (insn)) == INSN | |
2654 | && GET_CODE (PATTERN (PREV_INSN (insn))) == SET | |
2655 | && SET_SRC (PATTERN (PREV_INSN (insn))) == addr) | |
2656 | reg_next_use[regno] = PREV_INSN (insn); | |
2657 | else | |
2658 | reg_next_use[regno] = 0; | |
2659 | ||
2660 | addr = q; | |
2661 | regno = REGNO (q); | |
28dcb9ed | 2662 | |
2663 | /* REGNO is now used in INCR which is below INSN, but | |
2664 | it previously wasn't live here. If we don't mark | |
2665 | it as needed, we'll put a REG_DEAD note for it | |
2666 | on this insn, which is incorrect. */ | |
74666a14 | 2667 | SET_REGNO_REG_SET (needed, regno); |
28dcb9ed | 2668 | |
2669 | /* If there are any calls between INSN and INCR, show | |
2670 | that REGNO now crosses them. */ | |
2671 | for (temp = insn; temp != incr; temp = NEXT_INSN (temp)) | |
2672 | if (GET_CODE (temp) == CALL_INSN) | |
394685a4 | 2673 | REG_N_CALLS_CROSSED (regno)++; |
28dcb9ed | 2674 | } |
b21be717 | 2675 | else |
2676 | return; | |
28dcb9ed | 2677 | |
3a2c0c0c | 2678 | /* If we haven't returned, it means we were able to make the |
2679 | auto-inc, so update the status. First, record that this insn | |
2680 | has an implicit side effect. */ | |
2681 | ||
2682 | REG_NOTES (insn) | |
941522d6 | 2683 | = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn)); |
3a2c0c0c | 2684 | |
2685 | /* Modify the old increment-insn to simply copy | |
2686 | the already-incremented value of our register. */ | |
2687 | if (! validate_change (incr, &SET_SRC (set), addr, 0)) | |
2688 | abort (); | |
2689 | ||
2690 | /* If that makes it a no-op (copying the register into itself) delete | |
2691 | it so it won't appear to be a "use" and a "set" of this | |
2692 | register. */ | |
2693 | if (SET_DEST (set) == addr) | |
28dcb9ed | 2694 | { |
3a2c0c0c | 2695 | PUT_CODE (incr, NOTE); |
2696 | NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED; | |
2697 | NOTE_SOURCE_FILE (incr) = 0; | |
2698 | } | |
28dcb9ed | 2699 | |
3a2c0c0c | 2700 | if (regno >= FIRST_PSEUDO_REGISTER) |
2701 | { | |
2702 | /* Count an extra reference to the reg. When a reg is | |
2703 | incremented, spilling it is worse, so we want to make | |
2704 | that less likely. */ | |
394685a4 | 2705 | REG_N_REFS (regno) += loop_depth; |
3a2c0c0c | 2706 | |
2707 | /* Count the increment as a setting of the register, | |
2708 | even though it isn't a SET in rtl. */ | |
394685a4 | 2709 | REG_N_SETS (regno)++; |
28dcb9ed | 2710 | } |
2711 | } | |
2712 | } | |
2713 | } | |
2714 | #endif /* AUTO_INC_DEC */ | |
2715 | \f | |
2716 | /* Scan expression X and store a 1-bit in LIVE for each reg it uses. | |
2717 | This is done assuming the registers needed from X | |
2718 | are those that have 1-bits in NEEDED. | |
2719 | ||
2720 | On the final pass, FINAL is 1. This means try for autoincrement | |
2721 | and count the uses and deaths of each pseudo-reg. | |
2722 | ||
2723 | INSN is the containing instruction. If INSN is dead, this function is not | |
2724 | called. */ | |
2725 | ||
2726 | static void | |
2727 | mark_used_regs (needed, live, x, final, insn) | |
2728 | regset needed; | |
2729 | regset live; | |
2730 | rtx x; | |
28dcb9ed | 2731 | int final; |
a123fe25 | 2732 | rtx insn; |
28dcb9ed | 2733 | { |
2734 | register RTX_CODE code; | |
2735 | register int regno; | |
2736 | int i; | |
2737 | ||
2738 | retry: | |
2739 | code = GET_CODE (x); | |
2740 | switch (code) | |
2741 | { | |
2742 | case LABEL_REF: | |
2743 | case SYMBOL_REF: | |
2744 | case CONST_INT: | |
2745 | case CONST: | |
2746 | case CONST_DOUBLE: | |
2747 | case PC: | |
28dcb9ed | 2748 | case ADDR_VEC: |
2749 | case ADDR_DIFF_VEC: | |
2750 | case ASM_INPUT: | |
2751 | return; | |
2752 | ||
2753 | #ifdef HAVE_cc0 | |
2754 | case CC0: | |
2755 | cc0_live = 1; | |
2756 | return; | |
2757 | #endif | |
2758 | ||
6d6214e0 | 2759 | case CLOBBER: |
2760 | /* If we are clobbering a MEM, mark any registers inside the address | |
2761 | as being used. */ | |
2762 | if (GET_CODE (XEXP (x, 0)) == MEM) | |
2763 | mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn); | |
2764 | return; | |
2765 | ||
28dcb9ed | 2766 | case MEM: |
fc1ef759 | 2767 | /* Invalidate the data for the last MEM stored, but only if MEM is |
2768 | something that can be stored into. */ | |
2769 | if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF | |
2770 | && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0))) | |
d29fc2f4 | 2771 | ; /* needn't clear the memory set list */ |
fc1ef759 | 2772 | else |
d29fc2f4 | 2773 | { |
2774 | rtx temp = mem_set_list; | |
2775 | rtx prev = NULL_RTX; | |
2776 | ||
2777 | while (temp) | |
2778 | { | |
54817caf | 2779 | if (anti_dependence (XEXP (temp, 0), x)) |
d29fc2f4 | 2780 | { |
2781 | /* Splice temp out of the list. */ | |
2782 | if (prev) | |
2783 | XEXP (prev, 1) = XEXP (temp, 1); | |
2784 | else | |
2785 | mem_set_list = XEXP (temp, 1); | |
2786 | } | |
2787 | else | |
2788 | prev = temp; | |
2789 | temp = XEXP (temp, 1); | |
2790 | } | |
2791 | } | |
28dcb9ed | 2792 | |
a447bb5e | 2793 | /* If the memory reference had embedded side effects (autoincrement |
2794 | address modes. Then we may need to kill some entries on the | |
2795 | memory set list. */ | |
2796 | if (insn) | |
2797 | invalidate_mems_from_autoinc (insn); | |
2798 | ||
28dcb9ed | 2799 | #ifdef AUTO_INC_DEC |
2800 | if (final) | |
2801 | find_auto_inc (needed, x, insn); | |
2802 | #endif | |
2803 | break; | |
2804 | ||
2ed76ddf | 2805 | case SUBREG: |
2806 | if (GET_CODE (SUBREG_REG (x)) == REG | |
2807 | && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER | |
2808 | && (GET_MODE_SIZE (GET_MODE (x)) | |
1d886710 | 2809 | != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))) |
394685a4 | 2810 | REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1; |
2ed76ddf | 2811 | |
2812 | /* While we're here, optimize this case. */ | |
2813 | x = SUBREG_REG (x); | |
2814 | ||
5ad6b0e6 | 2815 | /* In case the SUBREG is not of a register, don't optimize */ |
6f783da1 | 2816 | if (GET_CODE (x) != REG) |
5ad6b0e6 | 2817 | { |
2818 | mark_used_regs (needed, live, x, final, insn); | |
2819 | return; | |
2820 | } | |
6f783da1 | 2821 | |
a92771b8 | 2822 | /* ... fall through ... */ |
2ed76ddf | 2823 | |
28dcb9ed | 2824 | case REG: |
2825 | /* See a register other than being set | |
2826 | => mark it as needed. */ | |
2827 | ||
2828 | regno = REGNO (x); | |
2829 | { | |
7ee969d0 | 2830 | int some_needed = REGNO_REG_SET_P (needed, regno); |
2831 | int some_not_needed = ! some_needed; | |
28dcb9ed | 2832 | |
74666a14 | 2833 | SET_REGNO_REG_SET (live, regno); |
d0f99e96 | 2834 | |
28dcb9ed | 2835 | /* A hard reg in a wide mode may really be multiple registers. |
2836 | If so, mark all of them just like the first. */ | |
2837 | if (regno < FIRST_PSEUDO_REGISTER) | |
2838 | { | |
2839 | int n; | |
2840 | ||
05b74099 | 2841 | /* For stack ptr or fixed arg pointer, |
28dcb9ed | 2842 | nothing below can be necessary, so waste no more time. */ |
2843 | if (regno == STACK_POINTER_REGNUM | |
4dd5e02b | 2844 | #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM |
2845 | || regno == HARD_FRAME_POINTER_REGNUM | |
2846 | #endif | |
05b74099 | 2847 | #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM |
2848 | || (regno == ARG_POINTER_REGNUM && fixed_regs[regno]) | |
2849 | #endif | |
28dcb9ed | 2850 | || regno == FRAME_POINTER_REGNUM) |
2851 | { | |
2852 | /* If this is a register we are going to try to eliminate, | |
2853 | don't mark it live here. If we are successful in | |
2854 | eliminating it, it need not be live unless it is used for | |
2855 | pseudos, in which case it will have been set live when | |
2856 | it was allocated to the pseudos. If the register will not | |
2857 | be eliminated, reload will set it live at that point. */ | |
2858 | ||
2859 | if (! TEST_HARD_REG_BIT (elim_reg_set, regno)) | |
2860 | regs_ever_live[regno] = 1; | |
2861 | return; | |
2862 | } | |
2863 | /* No death notes for global register variables; | |
2864 | their values are live after this function exits. */ | |
2865 | if (global_regs[regno]) | |
c8e3d518 | 2866 | { |
2867 | if (final) | |
2868 | reg_next_use[regno] = insn; | |
2869 | return; | |
2870 | } | |
28dcb9ed | 2871 | |
2872 | n = HARD_REGNO_NREGS (regno, GET_MODE (x)); | |
2873 | while (--n > 0) | |
2874 | { | |
74666a14 | 2875 | int regno_n = regno + n; |
2876 | int needed_regno = REGNO_REG_SET_P (needed, regno_n); | |
d0f99e96 | 2877 | |
74666a14 | 2878 | SET_REGNO_REG_SET (live, regno_n); |
2879 | some_needed |= needed_regno; | |
5299b3c0 | 2880 | some_not_needed |= ! needed_regno; |
28dcb9ed | 2881 | } |
2882 | } | |
2883 | if (final) | |
2884 | { | |
2885 | /* Record where each reg is used, so when the reg | |
2886 | is set we know the next insn that uses it. */ | |
2887 | ||
2888 | reg_next_use[regno] = insn; | |
2889 | ||
2890 | if (regno < FIRST_PSEUDO_REGISTER) | |
2891 | { | |
2892 | /* If a hard reg is being used, | |
2893 | record that this function does use it. */ | |
2894 | ||
2895 | i = HARD_REGNO_NREGS (regno, GET_MODE (x)); | |
2896 | if (i == 0) | |
2897 | i = 1; | |
2898 | do | |
2899 | regs_ever_live[regno + --i] = 1; | |
2900 | while (i > 0); | |
2901 | } | |
2902 | else | |
2903 | { | |
2904 | /* Keep track of which basic block each reg appears in. */ | |
2905 | ||
2906 | register int blocknum = BLOCK_NUM (insn); | |
2907 | ||
394685a4 | 2908 | if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN) |
2909 | REG_BASIC_BLOCK (regno) = blocknum; | |
2910 | else if (REG_BASIC_BLOCK (regno) != blocknum) | |
2911 | REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL; | |
28dcb9ed | 2912 | |
2913 | /* Count (weighted) number of uses of each reg. */ | |
2914 | ||
394685a4 | 2915 | REG_N_REFS (regno) += loop_depth; |
28dcb9ed | 2916 | } |
2917 | ||
2918 | /* Record and count the insns in which a reg dies. | |
2919 | If it is used in this insn and was dead below the insn | |
2920 | then it dies in this insn. If it was set in this insn, | |
2921 | we do not make a REG_DEAD note; likewise if we already | |
2922 | made such a note. */ | |
2923 | ||
d0f99e96 | 2924 | if (some_not_needed |
28dcb9ed | 2925 | && ! dead_or_set_p (insn, x) |
2926 | #if 0 | |
2927 | && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno]) | |
2928 | #endif | |
2929 | ) | |
2930 | { | |
7e64826f | 2931 | /* Check for the case where the register dying partially |
2932 | overlaps the register set by this insn. */ | |
2933 | if (regno < FIRST_PSEUDO_REGISTER | |
2934 | && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1) | |
2935 | { | |
85d4d7c8 | 2936 | int n = HARD_REGNO_NREGS (regno, GET_MODE (x)); |
7e64826f | 2937 | while (--n >= 0) |
2938 | some_needed |= dead_or_set_regno_p (insn, regno + n); | |
2939 | } | |
2940 | ||
28dcb9ed | 2941 | /* If none of the words in X is needed, make a REG_DEAD |
2942 | note. Otherwise, we must make partial REG_DEAD notes. */ | |
2943 | if (! some_needed) | |
2944 | { | |
2945 | REG_NOTES (insn) | |
941522d6 | 2946 | = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn)); |
394685a4 | 2947 | REG_N_DEATHS (regno)++; |
28dcb9ed | 2948 | } |
2949 | else | |
2950 | { | |
2951 | int i; | |
2952 | ||
2953 | /* Don't make a REG_DEAD note for a part of a register | |
2954 | that is set in the insn. */ | |
2955 | ||
2956 | for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1; | |
2957 | i >= 0; i--) | |
74666a14 | 2958 | if (!REGNO_REG_SET_P (needed, regno + i) |
28dcb9ed | 2959 | && ! dead_or_set_regno_p (insn, regno + i)) |
2960 | REG_NOTES (insn) | |
941522d6 | 2961 | = gen_rtx_EXPR_LIST (REG_DEAD, |
2962 | gen_rtx_REG (reg_raw_mode[regno + i], | |
2963 | regno + i), | |
2964 | REG_NOTES (insn)); | |
28dcb9ed | 2965 | } |
2966 | } | |
2967 | } | |
2968 | } | |
2969 | return; | |
2970 | ||
2971 | case SET: | |
2972 | { | |
2973 | register rtx testreg = SET_DEST (x); | |
2974 | int mark_dest = 0; | |
2975 | ||
2976 | /* If storing into MEM, don't show it as being used. But do | |
2977 | show the address as being used. */ | |
2978 | if (GET_CODE (testreg) == MEM) | |
2979 | { | |
2980 | #ifdef AUTO_INC_DEC | |
2981 | if (final) | |
2982 | find_auto_inc (needed, testreg, insn); | |
2983 | #endif | |
2984 | mark_used_regs (needed, live, XEXP (testreg, 0), final, insn); | |
2985 | mark_used_regs (needed, live, SET_SRC (x), final, insn); | |
2986 | return; | |
2987 | } | |
2988 | ||
2989 | /* Storing in STRICT_LOW_PART is like storing in a reg | |
2990 | in that this SET might be dead, so ignore it in TESTREG. | |
2991 | but in some other ways it is like using the reg. | |
2992 | ||
2993 | Storing in a SUBREG or a bit field is like storing the entire | |
2994 | register in that if the register's value is not used | |
2995 | then this SET is not needed. */ | |
2996 | while (GET_CODE (testreg) == STRICT_LOW_PART | |
2997 | || GET_CODE (testreg) == ZERO_EXTRACT | |
2998 | || GET_CODE (testreg) == SIGN_EXTRACT | |
2999 | || GET_CODE (testreg) == SUBREG) | |
3000 | { | |
1d886710 | 3001 | if (GET_CODE (testreg) == SUBREG |
3002 | && GET_CODE (SUBREG_REG (testreg)) == REG | |
3003 | && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER | |
3004 | && (GET_MODE_SIZE (GET_MODE (testreg)) | |
3005 | != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg))))) | |
394685a4 | 3006 | REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1; |
1d886710 | 3007 | |
28dcb9ed | 3008 | /* Modifying a single register in an alternate mode |
3009 | does not use any of the old value. But these other | |
3010 | ways of storing in a register do use the old value. */ | |
3011 | if (GET_CODE (testreg) == SUBREG | |
3012 | && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg))) | |
3013 | ; | |
3014 | else | |
3015 | mark_dest = 1; | |
3016 | ||
3017 | testreg = XEXP (testreg, 0); | |
3018 | } | |
3019 | ||
3020 | /* If this is a store into a register, | |
3021 | recursively scan the value being stored. */ | |
3022 | ||
53309b20 | 3023 | if ((GET_CODE (testreg) == PARALLEL |
3024 | && GET_MODE (testreg) == BLKmode) | |
3025 | || (GET_CODE (testreg) == REG | |
3026 | && (regno = REGNO (testreg), regno != FRAME_POINTER_REGNUM) | |
4dd5e02b | 3027 | #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM |
53309b20 | 3028 | && regno != HARD_FRAME_POINTER_REGNUM |
4dd5e02b | 3029 | #endif |
05b74099 | 3030 | #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM |
53309b20 | 3031 | && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno]) |
05b74099 | 3032 | #endif |
53309b20 | 3033 | )) |
c8e3d518 | 3034 | /* We used to exclude global_regs here, but that seems wrong. |
3035 | Storing in them is like storing in mem. */ | |
28dcb9ed | 3036 | { |
3037 | mark_used_regs (needed, live, SET_SRC (x), final, insn); | |
3038 | if (mark_dest) | |
3039 | mark_used_regs (needed, live, SET_DEST (x), final, insn); | |
3040 | return; | |
3041 | } | |
3042 | } | |
3043 | break; | |
3044 | ||
3045 | case RETURN: | |
3046 | /* If exiting needs the right stack value, consider this insn as | |
3047 | using the stack pointer. In any event, consider it as using | |
557bdddf | 3048 | all global registers and all registers used by return. */ |
28dcb9ed | 3049 | |
3050 | #ifdef EXIT_IGNORE_STACK | |
3051 | if (! EXIT_IGNORE_STACK | |
79a120e3 | 3052 | || (! FRAME_POINTER_REQUIRED |
3053 | && ! current_function_calls_alloca | |
9cb667bc | 3054 | && flag_omit_frame_pointer) |
3055 | || current_function_sp_is_unchanging) | |
28dcb9ed | 3056 | #endif |
74666a14 | 3057 | SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM); |
28dcb9ed | 3058 | |
3059 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) | |
557bdddf | 3060 | if (global_regs[i] |
3061 | #ifdef EPILOGUE_USES | |
3062 | || EPILOGUE_USES (i) | |
3063 | #endif | |
3064 | ) | |
74666a14 | 3065 | SET_REGNO_REG_SET (live, i); |
28dcb9ed | 3066 | break; |
0dbd1c74 | 3067 | |
3068 | default: | |
3069 | break; | |
28dcb9ed | 3070 | } |
3071 | ||
3072 | /* Recursively scan the operands of this expression. */ | |
3073 | ||
3074 | { | |
3075 | register char *fmt = GET_RTX_FORMAT (code); | |
3076 | register int i; | |
3077 | ||
3078 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
3079 | { | |
3080 | if (fmt[i] == 'e') | |
3081 | { | |
3082 | /* Tail recursive case: save a function call level. */ | |
3083 | if (i == 0) | |
3084 | { | |
3085 | x = XEXP (x, 0); | |
3086 | goto retry; | |
3087 | } | |
3088 | mark_used_regs (needed, live, XEXP (x, i), final, insn); | |
3089 | } | |
3090 | else if (fmt[i] == 'E') | |
3091 | { | |
3092 | register int j; | |
3093 | for (j = 0; j < XVECLEN (x, i); j++) | |
3094 | mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn); | |
3095 | } | |
3096 | } | |
3097 | } | |
3098 | } | |
3099 | \f | |
3100 | #ifdef AUTO_INC_DEC | |
3101 | ||
3102 | static int | |
3103 | try_pre_increment_1 (insn) | |
3104 | rtx insn; | |
3105 | { | |
3106 | /* Find the next use of this reg. If in same basic block, | |
3107 | make it do pre-increment or pre-decrement if appropriate. */ | |
ad87de1e | 3108 | rtx x = single_set (insn); |
1bb04728 | 3109 | HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1) |
28dcb9ed | 3110 | * INTVAL (XEXP (SET_SRC (x), 1))); |
3111 | int regno = REGNO (SET_DEST (x)); | |
3112 | rtx y = reg_next_use[regno]; | |
3113 | if (y != 0 | |
3114 | && BLOCK_NUM (y) == BLOCK_NUM (insn) | |
3409bb3d | 3115 | /* Don't do this if the reg dies, or gets set in y; a standard addressing |
a92771b8 | 3116 | mode would be better. */ |
3409bb3d | 3117 | && ! dead_or_set_p (y, SET_DEST (x)) |
ad87de1e | 3118 | && try_pre_increment (y, SET_DEST (x), amount)) |
28dcb9ed | 3119 | { |
3120 | /* We have found a suitable auto-increment | |
3121 | and already changed insn Y to do it. | |
3122 | So flush this increment-instruction. */ | |
3123 | PUT_CODE (insn, NOTE); | |
3124 | NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED; | |
3125 | NOTE_SOURCE_FILE (insn) = 0; | |
3126 | /* Count a reference to this reg for the increment | |
3127 | insn we are deleting. When a reg is incremented. | |
3128 | spilling it is worse, so we want to make that | |
3129 | less likely. */ | |
3130 | if (regno >= FIRST_PSEUDO_REGISTER) | |
3131 | { | |
394685a4 | 3132 | REG_N_REFS (regno) += loop_depth; |
3133 | REG_N_SETS (regno)++; | |
28dcb9ed | 3134 | } |
3135 | return 1; | |
3136 | } | |
3137 | return 0; | |
3138 | } | |
3139 | ||
3140 | /* Try to change INSN so that it does pre-increment or pre-decrement | |
3141 | addressing on register REG in order to add AMOUNT to REG. | |
3142 | AMOUNT is negative for pre-decrement. | |
3143 | Returns 1 if the change could be made. | |
3144 | This checks all about the validity of the result of modifying INSN. */ | |
3145 | ||
3146 | static int | |
3147 | try_pre_increment (insn, reg, amount) | |
3148 | rtx insn, reg; | |
1bb04728 | 3149 | HOST_WIDE_INT amount; |
28dcb9ed | 3150 | { |
3151 | register rtx use; | |
3152 | ||
3153 | /* Nonzero if we can try to make a pre-increment or pre-decrement. | |
3154 | For example, addl $4,r1; movl (r1),... can become movl +(r1),... */ | |
3155 | int pre_ok = 0; | |
3156 | /* Nonzero if we can try to make a post-increment or post-decrement. | |
3157 | For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,... | |
3158 | It is possible for both PRE_OK and POST_OK to be nonzero if the machine | |
3159 | supports both pre-inc and post-inc, or both pre-dec and post-dec. */ | |
3160 | int post_ok = 0; | |
3161 | ||
3162 | /* Nonzero if the opportunity actually requires post-inc or post-dec. */ | |
3163 | int do_post = 0; | |
3164 | ||
3165 | /* From the sign of increment, see which possibilities are conceivable | |
3166 | on this target machine. */ | |
e4e498cf | 3167 | if (HAVE_PRE_INCREMENT && amount > 0) |
28dcb9ed | 3168 | pre_ok = 1; |
e4e498cf | 3169 | if (HAVE_POST_INCREMENT && amount > 0) |
28dcb9ed | 3170 | post_ok = 1; |
28dcb9ed | 3171 | |
e4e498cf | 3172 | if (HAVE_PRE_DECREMENT && amount < 0) |
28dcb9ed | 3173 | pre_ok = 1; |
e4e498cf | 3174 | if (HAVE_POST_DECREMENT && amount < 0) |
28dcb9ed | 3175 | post_ok = 1; |
28dcb9ed | 3176 | |
3177 | if (! (pre_ok || post_ok)) | |
3178 | return 0; | |
3179 | ||
3180 | /* It is not safe to add a side effect to a jump insn | |
3181 | because if the incremented register is spilled and must be reloaded | |
3182 | there would be no way to store the incremented value back in memory. */ | |
3183 | ||
3184 | if (GET_CODE (insn) == JUMP_INSN) | |
3185 | return 0; | |
3186 | ||
3187 | use = 0; | |
3188 | if (pre_ok) | |
3189 | use = find_use_as_address (PATTERN (insn), reg, 0); | |
3190 | if (post_ok && (use == 0 || use == (rtx) 1)) | |
3191 | { | |
3192 | use = find_use_as_address (PATTERN (insn), reg, -amount); | |
3193 | do_post = 1; | |
3194 | } | |
3195 | ||
3196 | if (use == 0 || use == (rtx) 1) | |
3197 | return 0; | |
3198 | ||
3199 | if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount)) | |
3200 | return 0; | |
3201 | ||
c879208e | 3202 | /* See if this combination of instruction and addressing mode exists. */ |
3203 | if (! validate_change (insn, &XEXP (use, 0), | |
941522d6 | 3204 | gen_rtx_fmt_e (amount > 0 |
3205 | ? (do_post ? POST_INC : PRE_INC) | |
3206 | : (do_post ? POST_DEC : PRE_DEC), | |
3207 | Pmode, reg), 0)) | |
c879208e | 3208 | return 0; |
28dcb9ed | 3209 | |
3210 | /* Record that this insn now has an implicit side effect on X. */ | |
941522d6 | 3211 | REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn)); |
28dcb9ed | 3212 | return 1; |
3213 | } | |
3214 | ||
3215 | #endif /* AUTO_INC_DEC */ | |
3216 | \f | |
3217 | /* Find the place in the rtx X where REG is used as a memory address. | |
3218 | Return the MEM rtx that so uses it. | |
3219 | If PLUSCONST is nonzero, search instead for a memory address equivalent to | |
3220 | (plus REG (const_int PLUSCONST)). | |
3221 | ||
3222 | If such an address does not appear, return 0. | |
3223 | If REG appears more than once, or is used other than in such an address, | |
3224 | return (rtx)1. */ | |
3225 | ||
3eb9a99d | 3226 | rtx |
28dcb9ed | 3227 | find_use_as_address (x, reg, plusconst) |
3228 | register rtx x; | |
3229 | rtx reg; | |
a123fe25 | 3230 | HOST_WIDE_INT plusconst; |
28dcb9ed | 3231 | { |
3232 | enum rtx_code code = GET_CODE (x); | |
3233 | char *fmt = GET_RTX_FORMAT (code); | |
3234 | register int i; | |
3235 | register rtx value = 0; | |
3236 | register rtx tem; | |
3237 | ||
3238 | if (code == MEM && XEXP (x, 0) == reg && plusconst == 0) | |
3239 | return x; | |
3240 | ||
3241 | if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS | |
3242 | && XEXP (XEXP (x, 0), 0) == reg | |
3243 | && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT | |
3244 | && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst) | |
3245 | return x; | |
3246 | ||
3247 | if (code == SIGN_EXTRACT || code == ZERO_EXTRACT) | |
3248 | { | |
3249 | /* If REG occurs inside a MEM used in a bit-field reference, | |
3250 | that is unacceptable. */ | |
3251 | if (find_use_as_address (XEXP (x, 0), reg, 0) != 0) | |
1ac1757b | 3252 | return (rtx) (HOST_WIDE_INT) 1; |
28dcb9ed | 3253 | } |
3254 | ||
3255 | if (x == reg) | |
1ac1757b | 3256 | return (rtx) (HOST_WIDE_INT) 1; |
28dcb9ed | 3257 | |
3258 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
3259 | { | |
3260 | if (fmt[i] == 'e') | |
3261 | { | |
3262 | tem = find_use_as_address (XEXP (x, i), reg, plusconst); | |
3263 | if (value == 0) | |
3264 | value = tem; | |
3265 | else if (tem != 0) | |
1ac1757b | 3266 | return (rtx) (HOST_WIDE_INT) 1; |
28dcb9ed | 3267 | } |
3268 | if (fmt[i] == 'E') | |
3269 | { | |
3270 | register int j; | |
3271 | for (j = XVECLEN (x, i) - 1; j >= 0; j--) | |
3272 | { | |
3273 | tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst); | |
3274 | if (value == 0) | |
3275 | value = tem; | |
3276 | else if (tem != 0) | |
1ac1757b | 3277 | return (rtx) (HOST_WIDE_INT) 1; |
28dcb9ed | 3278 | } |
3279 | } | |
3280 | } | |
3281 | ||
3282 | return value; | |
3283 | } | |
3284 | \f | |
3285 | /* Write information about registers and basic blocks into FILE. | |
3286 | This is part of making a debugging dump. */ | |
3287 | ||
3288 | void | |
3289 | dump_flow_info (file) | |
3290 | FILE *file; | |
3291 | { | |
3292 | register int i; | |
3293 | static char *reg_class_names[] = REG_CLASS_NAMES; | |
3294 | ||
3295 | fprintf (file, "%d registers.\n", max_regno); | |
3296 | ||
3297 | for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++) | |
394685a4 | 3298 | if (REG_N_REFS (i)) |
28dcb9ed | 3299 | { |
4023cea7 | 3300 | enum reg_class class, altclass; |
28dcb9ed | 3301 | fprintf (file, "\nRegister %d used %d times across %d insns", |
394685a4 | 3302 | i, REG_N_REFS (i), REG_LIVE_LENGTH (i)); |
3303 | if (REG_BASIC_BLOCK (i) >= 0) | |
3304 | fprintf (file, " in block %d", REG_BASIC_BLOCK (i)); | |
2fe63c7b | 3305 | if (REG_N_SETS (i)) |
3306 | fprintf (file, "; set %d time%s", REG_N_SETS (i), | |
3307 | (REG_N_SETS (i) == 1) ? "" : "s"); | |
3308 | if (REG_USERVAR_P (regno_reg_rtx[i])) | |
3309 | fprintf (file, "; user var"); | |
394685a4 | 3310 | if (REG_N_DEATHS (i) != 1) |
3311 | fprintf (file, "; dies in %d places", REG_N_DEATHS (i)); | |
3312 | if (REG_N_CALLS_CROSSED (i) == 1) | |
28dcb9ed | 3313 | fprintf (file, "; crosses 1 call"); |
394685a4 | 3314 | else if (REG_N_CALLS_CROSSED (i)) |
3315 | fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i)); | |
28dcb9ed | 3316 | if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD) |
3317 | fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i)); | |
3318 | class = reg_preferred_class (i); | |
4023cea7 | 3319 | altclass = reg_alternate_class (i); |
3320 | if (class != GENERAL_REGS || altclass != ALL_REGS) | |
28dcb9ed | 3321 | { |
4023cea7 | 3322 | if (altclass == ALL_REGS || class == ALL_REGS) |
3323 | fprintf (file, "; pref %s", reg_class_names[(int) class]); | |
3324 | else if (altclass == NO_REGS) | |
28dcb9ed | 3325 | fprintf (file, "; %s or none", reg_class_names[(int) class]); |
3326 | else | |
4023cea7 | 3327 | fprintf (file, "; pref %s, else %s", |
3328 | reg_class_names[(int) class], | |
3329 | reg_class_names[(int) altclass]); | |
28dcb9ed | 3330 | } |
3331 | if (REGNO_POINTER_FLAG (i)) | |
3332 | fprintf (file, "; pointer"); | |
3333 | fprintf (file, ".\n"); | |
3334 | } | |
3335 | fprintf (file, "\n%d basic blocks.\n", n_basic_blocks); | |
22548064 | 3336 | dump_bb_data (file, basic_block_pred, basic_block_succ, 1); |
28dcb9ed | 3337 | } |
f34a52ce | 3338 | |
3339 | \f | |
3340 | /* Like print_rtl, but also print out live information for the start of each | |
3341 | basic block. */ | |
3342 | ||
3343 | void | |
3344 | print_rtl_with_bb (outf, rtx_first) | |
3345 | FILE *outf; | |
3346 | rtx rtx_first; | |
3347 | { | |
3348 | register rtx tmp_rtx; | |
3349 | ||
3350 | if (rtx_first == 0) | |
3351 | fprintf (outf, "(nil)\n"); | |
3352 | ||
3353 | else | |
3354 | { | |
3355 | int i, bb; | |
3356 | enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB }; | |
3357 | int max_uid = get_max_uid (); | |
5673e443 | 3358 | int *start = (int *) alloca (max_uid * sizeof (int)); |
3359 | int *end = (int *) alloca (max_uid * sizeof (int)); | |
7426c2df | 3360 | enum bb_state *in_bb_p = (enum bb_state *) |
3361 | alloca (max_uid * sizeof (enum bb_state)); | |
f34a52ce | 3362 | |
3363 | for (i = 0; i < max_uid; i++) | |
3364 | { | |
3365 | start[i] = end[i] = -1; | |
3366 | in_bb_p[i] = NOT_IN_BB; | |
3367 | } | |
3368 | ||
3369 | for (i = n_basic_blocks-1; i >= 0; i--) | |
3370 | { | |
3371 | rtx x; | |
68676d00 | 3372 | start[INSN_UID (BLOCK_HEAD (i))] = i; |
3373 | end[INSN_UID (BLOCK_END (i))] = i; | |
3374 | for (x = BLOCK_HEAD (i); x != NULL_RTX; x = NEXT_INSN (x)) | |
f34a52ce | 3375 | { |
8cd7064e | 3376 | in_bb_p[ INSN_UID(x)] |
3377 | = (in_bb_p[ INSN_UID(x)] == NOT_IN_BB) | |
aed23a83 | 3378 | ? IN_ONE_BB : IN_MULTIPLE_BB; |
68676d00 | 3379 | if (x == BLOCK_END (i)) |
f34a52ce | 3380 | break; |
3381 | } | |
3382 | } | |
3383 | ||
3384 | for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx)) | |
3385 | { | |
fd63ca43 | 3386 | int did_output; |
3387 | ||
f34a52ce | 3388 | if ((bb = start[INSN_UID (tmp_rtx)]) >= 0) |
3389 | { | |
3390 | fprintf (outf, ";; Start of basic block %d, registers live:", | |
3391 | bb); | |
3392 | ||
3393 | EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i, | |
3394 | { | |
3395 | fprintf (outf, " %d", i); | |
3396 | if (i < FIRST_PSEUDO_REGISTER) | |
3397 | fprintf (outf, " [%s]", | |
3398 | reg_names[i]); | |
3399 | }); | |
3400 | putc ('\n', outf); | |
3401 | } | |
3402 | ||
be2828ce | 3403 | if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB |
f34a52ce | 3404 | && GET_CODE (tmp_rtx) != NOTE |
be2828ce | 3405 | && GET_CODE (tmp_rtx) != BARRIER |
3406 | && ! obey_regdecls) | |
f34a52ce | 3407 | fprintf (outf, ";; Insn is not within a basic block\n"); |
3408 | else if (in_bb_p[ INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB) | |
3409 | fprintf (outf, ";; Insn is in multiple basic blocks\n"); | |
3410 | ||
fd63ca43 | 3411 | did_output = print_rtl_single (outf, tmp_rtx); |
f34a52ce | 3412 | |
3413 | if ((bb = end[INSN_UID (tmp_rtx)]) >= 0) | |
3414 | fprintf (outf, ";; End of basic block %d\n", bb); | |
3415 | ||
fd63ca43 | 3416 | if (did_output) |
9e042f31 | 3417 | putc ('\n', outf); |
f34a52ce | 3418 | } |
3419 | } | |
3420 | } | |
61e82936 | 3421 | |
3422 | \f | |
3423 | /* Integer list support. */ | |
3424 | ||
3425 | /* Allocate a node from list *HEAD_PTR. */ | |
3426 | ||
3427 | static int_list_ptr | |
3428 | alloc_int_list_node (head_ptr) | |
3429 | int_list_block **head_ptr; | |
3430 | { | |
3431 | struct int_list_block *first_blk = *head_ptr; | |
3432 | ||
3433 | if (first_blk == NULL || first_blk->nodes_left <= 0) | |
3434 | { | |
3435 | first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block)); | |
3436 | first_blk->nodes_left = INT_LIST_NODES_IN_BLK; | |
3437 | first_blk->next = *head_ptr; | |
3438 | *head_ptr = first_blk; | |
3439 | } | |
3440 | ||
3441 | first_blk->nodes_left--; | |
3442 | return &first_blk->nodes[first_blk->nodes_left]; | |
3443 | } | |
3444 | ||
3445 | /* Pointer to head of predecessor/successor block list. */ | |
3446 | static int_list_block *pred_int_list_blocks; | |
3447 | ||
3448 | /* Add a new node to integer list LIST with value VAL. | |
3449 | LIST is a pointer to a list object to allow for different implementations. | |
3450 | If *LIST is initially NULL, the list is empty. | |
3451 | The caller must not care whether the element is added to the front or | |
3452 | to the end of the list (to allow for different implementations). */ | |
3453 | ||
3454 | static int_list_ptr | |
3455 | add_int_list_node (blk_list, list, val) | |
3456 | int_list_block **blk_list; | |
3457 | int_list **list; | |
3458 | int val; | |
3459 | { | |
3460 | int_list_ptr p = alloc_int_list_node (blk_list); | |
3461 | ||
3462 | p->val = val; | |
3463 | p->next = *list; | |
3464 | *list = p; | |
3465 | return p; | |
3466 | } | |
3467 | ||
3468 | /* Free the blocks of lists at BLK_LIST. */ | |
3469 | ||
3470 | void | |
3471 | free_int_list (blk_list) | |
3472 | int_list_block **blk_list; | |
3473 | { | |
3474 | int_list_block *p, *next; | |
3475 | ||
3476 | for (p = *blk_list; p != NULL; p = next) | |
3477 | { | |
3478 | next = p->next; | |
3479 | free (p); | |
3480 | } | |
3481 | ||
3482 | /* Mark list as empty for the next function we compile. */ | |
3483 | *blk_list = NULL; | |
3484 | } | |
3485 | \f | |
3486 | /* Predecessor/successor computation. */ | |
3487 | ||
3488 | /* Mark PRED_BB a precessor of SUCC_BB, | |
3489 | and conversely SUCC_BB a successor of PRED_BB. */ | |
3490 | ||
3491 | static void | |
3492 | add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs) | |
3493 | int pred_bb; | |
3494 | int succ_bb; | |
3495 | int_list_ptr *s_preds; | |
3496 | int_list_ptr *s_succs; | |
3497 | int *num_preds; | |
3498 | int *num_succs; | |
3499 | { | |
3500 | if (succ_bb != EXIT_BLOCK) | |
3501 | { | |
3502 | add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb); | |
3503 | num_preds[succ_bb]++; | |
3504 | } | |
3505 | if (pred_bb != ENTRY_BLOCK) | |
3506 | { | |
3507 | add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb); | |
3508 | num_succs[pred_bb]++; | |
3509 | } | |
3510 | } | |
3511 | ||
3512 | /* Compute the predecessors and successors for each block. */ | |
b03b2e2f | 3513 | void |
61e82936 | 3514 | compute_preds_succs (s_preds, s_succs, num_preds, num_succs) |
3515 | int_list_ptr *s_preds; | |
3516 | int_list_ptr *s_succs; | |
3517 | int *num_preds; | |
3518 | int *num_succs; | |
3519 | { | |
22548064 | 3520 | int bb; |
61e82936 | 3521 | |
3522 | bzero ((char *) s_preds, n_basic_blocks * sizeof (int_list_ptr)); | |
3523 | bzero ((char *) s_succs, n_basic_blocks * sizeof (int_list_ptr)); | |
3524 | bzero ((char *) num_preds, n_basic_blocks * sizeof (int)); | |
3525 | bzero ((char *) num_succs, n_basic_blocks * sizeof (int)); | |
3526 | ||
22548064 | 3527 | /* It's somewhat stupid to simply copy the information. The passes |
3528 | which use this function ought to be changed to refer directly to | |
3529 | basic_block_succ and its relatives. */ | |
61e82936 | 3530 | for (bb = 0; bb < n_basic_blocks; bb++) |
3531 | { | |
22548064 | 3532 | rtx jump = BLOCK_END (bb); |
3533 | enum rtx_code code = GET_CODE (jump); | |
3534 | int_list_ptr p; | |
61e82936 | 3535 | |
22548064 | 3536 | for (p = basic_block_succ[bb]; p; p = p->next) |
3537 | add_pred_succ (bb, INT_LIST_VAL (p), s_preds, s_succs, num_preds, | |
3538 | num_succs); | |
61e82936 | 3539 | |
61e82936 | 3540 | /* If this is a RETURN insn or a conditional jump in the last |
3541 | basic block, or a non-jump insn in the last basic block, then | |
3542 | this block reaches the exit block. */ | |
22548064 | 3543 | if ((code == JUMP_INSN && GET_CODE (PATTERN (jump)) == RETURN) |
3544 | || (((code == JUMP_INSN | |
61e82936 | 3545 | && condjump_p (jump) && !simplejump_p (jump)) |
22548064 | 3546 | || code != JUMP_INSN) |
3547 | && bb == n_basic_blocks - 1)) | |
61e82936 | 3548 | add_pred_succ (bb, EXIT_BLOCK, s_preds, s_succs, num_preds, num_succs); |
61e82936 | 3549 | } |
3550 | ||
3551 | add_pred_succ (ENTRY_BLOCK, 0, s_preds, s_succs, num_preds, num_succs); | |
61e82936 | 3552 | } |
3553 | ||
3554 | void | |
22548064 | 3555 | dump_bb_data (file, preds, succs, live_info) |
61e82936 | 3556 | FILE *file; |
3557 | int_list_ptr *preds; | |
3558 | int_list_ptr *succs; | |
22548064 | 3559 | int live_info; |
61e82936 | 3560 | { |
3561 | int bb; | |
3562 | int_list_ptr p; | |
3563 | ||
3564 | fprintf (file, "BB data\n\n"); | |
3565 | for (bb = 0; bb < n_basic_blocks; bb++) | |
3566 | { | |
3567 | fprintf (file, "BB %d, start %d, end %d\n", bb, | |
3568 | INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb))); | |
3569 | fprintf (file, " preds:"); | |
3570 | for (p = preds[bb]; p != NULL; p = p->next) | |
3571 | { | |
3572 | int pred_bb = INT_LIST_VAL (p); | |
3573 | if (pred_bb == ENTRY_BLOCK) | |
3574 | fprintf (file, " entry"); | |
3575 | else | |
3576 | fprintf (file, " %d", pred_bb); | |
3577 | } | |
3578 | fprintf (file, "\n"); | |
3579 | fprintf (file, " succs:"); | |
3580 | for (p = succs[bb]; p != NULL; p = p->next) | |
3581 | { | |
3582 | int succ_bb = INT_LIST_VAL (p); | |
3583 | if (succ_bb == EXIT_BLOCK) | |
3584 | fprintf (file, " exit"); | |
3585 | else | |
3586 | fprintf (file, " %d", succ_bb); | |
3587 | } | |
22548064 | 3588 | if (live_info) |
3589 | { | |
3590 | int regno; | |
3591 | fprintf (file, "\nRegisters live at start:"); | |
3592 | for (regno = 0; regno < max_regno; regno++) | |
3593 | if (REGNO_REG_SET_P (basic_block_live_at_start[bb], regno)) | |
3594 | fprintf (file, " %d", regno); | |
3595 | fprintf (file, "\n"); | |
3596 | } | |
61e82936 | 3597 | fprintf (file, "\n"); |
3598 | } | |
3599 | fprintf (file, "\n"); | |
3600 | } | |
3601 | ||
3602 | /* Free basic block data storage. */ | |
3603 | ||
3604 | void | |
3605 | free_bb_mem () | |
3606 | { | |
3607 | free_int_list (&pred_int_list_blocks); | |
3608 | } | |
a05974ba | 3609 | |
61e82936 | 3610 | /* Compute dominator relationships. */ |
3611 | void | |
3612 | compute_dominators (dominators, post_dominators, s_preds, s_succs) | |
3613 | sbitmap *dominators; | |
3614 | sbitmap *post_dominators; | |
3615 | int_list_ptr *s_preds; | |
3616 | int_list_ptr *s_succs; | |
3617 | { | |
3618 | int bb, changed, passes; | |
3619 | sbitmap *temp_bitmap; | |
3620 | ||
3621 | temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks); | |
3622 | sbitmap_vector_ones (dominators, n_basic_blocks); | |
3623 | sbitmap_vector_ones (post_dominators, n_basic_blocks); | |
3624 | sbitmap_vector_zero (temp_bitmap, n_basic_blocks); | |
3625 | ||
3626 | sbitmap_zero (dominators[0]); | |
3627 | SET_BIT (dominators[0], 0); | |
3628 | ||
3629 | sbitmap_zero (post_dominators[n_basic_blocks-1]); | |
3630 | SET_BIT (post_dominators[n_basic_blocks-1], 0); | |
3631 | ||
3632 | passes = 0; | |
3633 | changed = 1; | |
3634 | while (changed) | |
3635 | { | |
3636 | changed = 0; | |
3637 | for (bb = 1; bb < n_basic_blocks; bb++) | |
3638 | { | |
3639 | sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators, | |
3640 | bb, s_preds); | |
3641 | SET_BIT (temp_bitmap[bb], bb); | |
3642 | changed |= sbitmap_a_and_b (dominators[bb], | |
3643 | dominators[bb], | |
3644 | temp_bitmap[bb]); | |
3645 | sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators, | |
3646 | bb, s_succs); | |
3647 | SET_BIT (temp_bitmap[bb], bb); | |
3648 | changed |= sbitmap_a_and_b (post_dominators[bb], | |
3649 | post_dominators[bb], | |
3650 | temp_bitmap[bb]); | |
3651 | } | |
3652 | passes++; | |
3653 | } | |
3654 | ||
3655 | free (temp_bitmap); | |
3656 | } | |
23f5e759 | 3657 | |
3658 | /* Count for a single SET rtx, X. */ | |
3659 | ||
3660 | static void | |
3661 | count_reg_sets_1 (x) | |
3662 | rtx x; | |
3663 | { | |
3664 | register int regno; | |
3665 | register rtx reg = SET_DEST (x); | |
3666 | ||
3667 | /* Find the register that's set/clobbered. */ | |
3668 | while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT | |
3669 | || GET_CODE (reg) == SIGN_EXTRACT | |
3670 | || GET_CODE (reg) == STRICT_LOW_PART) | |
3671 | reg = XEXP (reg, 0); | |
3672 | ||
53309b20 | 3673 | if (GET_CODE (reg) == PARALLEL |
3674 | && GET_MODE (reg) == BLKmode) | |
3675 | { | |
3676 | register int i; | |
3677 | for (i = XVECLEN (reg, 0) - 1; i >= 0; i--) | |
3678 | count_reg_sets_1 (XVECEXP (reg, 0, i)); | |
3679 | return; | |
3680 | } | |
3681 | ||
23f5e759 | 3682 | if (GET_CODE (reg) == REG) |
3683 | { | |
3684 | regno = REGNO (reg); | |
3685 | if (regno >= FIRST_PSEUDO_REGISTER) | |
3686 | { | |
3687 | /* Count (weighted) references, stores, etc. This counts a | |
3688 | register twice if it is modified, but that is correct. */ | |
3689 | REG_N_SETS (regno)++; | |
3690 | ||
3691 | REG_N_REFS (regno) += loop_depth; | |
3692 | } | |
3693 | } | |
3694 | } | |
3695 | ||
3696 | /* Increment REG_N_SETS for each SET or CLOBBER found in X; also increment | |
3697 | REG_N_REFS by the current loop depth for each SET or CLOBBER found. */ | |
3698 | ||
3699 | static void | |
3700 | count_reg_sets (x) | |
3701 | rtx x; | |
3702 | { | |
3703 | register RTX_CODE code = GET_CODE (x); | |
3704 | ||
3705 | if (code == SET || code == CLOBBER) | |
3706 | count_reg_sets_1 (x); | |
3707 | else if (code == PARALLEL) | |
3708 | { | |
3709 | register int i; | |
3710 | for (i = XVECLEN (x, 0) - 1; i >= 0; i--) | |
3711 | { | |
3712 | code = GET_CODE (XVECEXP (x, 0, i)); | |
3713 | if (code == SET || code == CLOBBER) | |
3714 | count_reg_sets_1 (XVECEXP (x, 0, i)); | |
3715 | } | |
3716 | } | |
3717 | } | |
3718 | ||
3719 | /* Increment REG_N_REFS by the current loop depth each register reference | |
3720 | found in X. */ | |
3721 | ||
3722 | static void | |
3723 | count_reg_references (x) | |
3724 | rtx x; | |
3725 | { | |
3726 | register RTX_CODE code; | |
23f5e759 | 3727 | |
3728 | retry: | |
3729 | code = GET_CODE (x); | |
3730 | switch (code) | |
3731 | { | |
3732 | case LABEL_REF: | |
3733 | case SYMBOL_REF: | |
3734 | case CONST_INT: | |
3735 | case CONST: | |
3736 | case CONST_DOUBLE: | |
3737 | case PC: | |
3738 | case ADDR_VEC: | |
3739 | case ADDR_DIFF_VEC: | |
3740 | case ASM_INPUT: | |
3741 | return; | |
3742 | ||
3743 | #ifdef HAVE_cc0 | |
3744 | case CC0: | |
3745 | return; | |
3746 | #endif | |
3747 | ||
3748 | case CLOBBER: | |
3749 | /* If we are clobbering a MEM, mark any registers inside the address | |
3750 | as being used. */ | |
3751 | if (GET_CODE (XEXP (x, 0)) == MEM) | |
3752 | count_reg_references (XEXP (XEXP (x, 0), 0)); | |
3753 | return; | |
3754 | ||
3755 | case SUBREG: | |
3756 | /* While we're here, optimize this case. */ | |
3757 | x = SUBREG_REG (x); | |
3758 | ||
3759 | /* In case the SUBREG is not of a register, don't optimize */ | |
3760 | if (GET_CODE (x) != REG) | |
3761 | { | |
3762 | count_reg_references (x); | |
3763 | return; | |
3764 | } | |
3765 | ||
3766 | /* ... fall through ... */ | |
3767 | ||
3768 | case REG: | |
3769 | if (REGNO (x) >= FIRST_PSEUDO_REGISTER) | |
3770 | REG_N_REFS (REGNO (x)) += loop_depth; | |
3771 | return; | |
3772 | ||
3773 | case SET: | |
3774 | { | |
3775 | register rtx testreg = SET_DEST (x); | |
3776 | int mark_dest = 0; | |
3777 | ||
3778 | /* If storing into MEM, don't show it as being used. But do | |
3779 | show the address as being used. */ | |
3780 | if (GET_CODE (testreg) == MEM) | |
3781 | { | |
3782 | count_reg_references (XEXP (testreg, 0)); | |
3783 | count_reg_references (SET_SRC (x)); | |
3784 | return; | |
3785 | } | |
3786 | ||
3787 | /* Storing in STRICT_LOW_PART is like storing in a reg | |
3788 | in that this SET might be dead, so ignore it in TESTREG. | |
3789 | but in some other ways it is like using the reg. | |
3790 | ||
3791 | Storing in a SUBREG or a bit field is like storing the entire | |
3792 | register in that if the register's value is not used | |
3793 | then this SET is not needed. */ | |
3794 | while (GET_CODE (testreg) == STRICT_LOW_PART | |
3795 | || GET_CODE (testreg) == ZERO_EXTRACT | |
3796 | || GET_CODE (testreg) == SIGN_EXTRACT | |
3797 | || GET_CODE (testreg) == SUBREG) | |
3798 | { | |
3799 | /* Modifying a single register in an alternate mode | |
3800 | does not use any of the old value. But these other | |
3801 | ways of storing in a register do use the old value. */ | |
3802 | if (GET_CODE (testreg) == SUBREG | |
3803 | && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg))) | |
3804 | ; | |
3805 | else | |
3806 | mark_dest = 1; | |
3807 | ||
3808 | testreg = XEXP (testreg, 0); | |
3809 | } | |
3810 | ||
3811 | /* If this is a store into a register, | |
3812 | recursively scan the value being stored. */ | |
3813 | ||
53309b20 | 3814 | if ((GET_CODE (testreg) == PARALLEL |
3815 | && GET_MODE (testreg) == BLKmode) | |
3816 | || GET_CODE (testreg) == REG) | |
23f5e759 | 3817 | { |
3818 | count_reg_references (SET_SRC (x)); | |
3819 | if (mark_dest) | |
3820 | count_reg_references (SET_DEST (x)); | |
3821 | return; | |
3822 | } | |
3823 | } | |
3824 | break; | |
3825 | ||
3826 | default: | |
3827 | break; | |
3828 | } | |
3829 | ||
3830 | /* Recursively scan the operands of this expression. */ | |
3831 | ||
3832 | { | |
3833 | register char *fmt = GET_RTX_FORMAT (code); | |
3834 | register int i; | |
3835 | ||
3836 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
3837 | { | |
3838 | if (fmt[i] == 'e') | |
3839 | { | |
3840 | /* Tail recursive case: save a function call level. */ | |
3841 | if (i == 0) | |
3842 | { | |
3843 | x = XEXP (x, 0); | |
3844 | goto retry; | |
3845 | } | |
3846 | count_reg_references (XEXP (x, i)); | |
3847 | } | |
3848 | else if (fmt[i] == 'E') | |
3849 | { | |
3850 | register int j; | |
3851 | for (j = 0; j < XVECLEN (x, i); j++) | |
3852 | count_reg_references (XVECEXP (x, i, j)); | |
3853 | } | |
3854 | } | |
3855 | } | |
3856 | } | |
3857 | ||
3858 | /* Recompute register set/reference counts immediately prior to register | |
3859 | allocation. | |
3860 | ||
3861 | This avoids problems with set/reference counts changing to/from values | |
3862 | which have special meanings to the register allocators. | |
3863 | ||
3864 | Additionally, the reference counts are the primary component used by the | |
3865 | register allocators to prioritize pseudos for allocation to hard regs. | |
3866 | More accurate reference counts generally lead to better register allocation. | |
3867 | ||
13da12d7 | 3868 | F is the first insn to be scanned. |
3869 | LOOP_STEP denotes how much loop_depth should be incremented per | |
3870 | loop nesting level in order to increase the ref count more for references | |
3871 | in a loop. | |
3872 | ||
23f5e759 | 3873 | It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and |
3874 | possibly other information which is used by the register allocators. */ | |
3875 | ||
a2b73329 | 3876 | void |
13da12d7 | 3877 | recompute_reg_usage (f, loop_step) |
23f5e759 | 3878 | rtx f; |
13da12d7 | 3879 | int loop_step; |
23f5e759 | 3880 | { |
3881 | rtx insn; | |
3882 | int i, max_reg; | |
3883 | ||
3884 | /* Clear out the old data. */ | |
3885 | max_reg = max_reg_num (); | |
3886 | for (i = FIRST_PSEUDO_REGISTER; i < max_reg; i++) | |
3887 | { | |
3888 | REG_N_SETS (i) = 0; | |
3889 | REG_N_REFS (i) = 0; | |
3890 | } | |
3891 | ||
3892 | /* Scan each insn in the chain and count how many times each register is | |
3893 | set/used. */ | |
3894 | loop_depth = 1; | |
3895 | for (insn = f; insn; insn = NEXT_INSN (insn)) | |
3896 | { | |
3897 | /* Keep track of loop depth. */ | |
3898 | if (GET_CODE (insn) == NOTE) | |
3899 | { | |
3900 | /* Look for loop boundaries. */ | |
3901 | if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END) | |
13da12d7 | 3902 | loop_depth -= loop_step; |
23f5e759 | 3903 | else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG) |
13da12d7 | 3904 | loop_depth += loop_step; |
23f5e759 | 3905 | |
3906 | /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. | |
3907 | Abort now rather than setting register status incorrectly. */ | |
3908 | if (loop_depth == 0) | |
3909 | abort (); | |
3910 | } | |
3911 | else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i') | |
3912 | { | |
3913 | rtx links; | |
3914 | ||
3915 | /* This call will increment REG_N_SETS for each SET or CLOBBER | |
3916 | of a register in INSN. It will also increment REG_N_REFS | |
3917 | by the loop depth for each set of a register in INSN. */ | |
3918 | count_reg_sets (PATTERN (insn)); | |
3919 | ||
3920 | /* count_reg_sets does not detect autoincrement address modes, so | |
3921 | detect them here by looking at the notes attached to INSN. */ | |
3922 | for (links = REG_NOTES (insn); links; links = XEXP (links, 1)) | |
3923 | { | |
3924 | if (REG_NOTE_KIND (links) == REG_INC) | |
3925 | /* Count (weighted) references, stores, etc. This counts a | |
3926 | register twice if it is modified, but that is correct. */ | |
3927 | REG_N_SETS (REGNO (XEXP (links, 0)))++; | |
3928 | } | |
3929 | ||
3930 | /* This call will increment REG_N_REFS by the current loop depth for | |
3931 | each reference to a register in INSN. */ | |
3932 | count_reg_references (PATTERN (insn)); | |
3933 | ||
3934 | /* count_reg_references will not include counts for arguments to | |
3935 | function calls, so detect them here by examining the | |
3936 | CALL_INSN_FUNCTION_USAGE data. */ | |
3937 | if (GET_CODE (insn) == CALL_INSN) | |
3938 | { | |
3939 | rtx note; | |
3940 | ||
3941 | for (note = CALL_INSN_FUNCTION_USAGE (insn); | |
3942 | note; | |
3943 | note = XEXP (note, 1)) | |
3944 | if (GET_CODE (XEXP (note, 0)) == USE) | |
3945 | count_reg_references (SET_DEST (XEXP (note, 0))); | |
3946 | } | |
3947 | } | |
3948 | } | |
3949 | } |