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