Do not do src->dest copy if register would not be allocated a normal register
[official-gcc.git] / gcc / flow.c
blob8021b4e2c89432e637641a08d6bfda7ea592a0a4
1 /* Data flow analysis for GNU compiler.
2 Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
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.
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.
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.
35 ** find_basic_blocks **
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.
42 find_basic_blocks also finds any unreachable loops
43 and deletes them.
45 ** life_analysis **
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.
51 ** live-register info **
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.
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.
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).
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.
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.
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.
90 ** Other actions of life_analysis **
92 life_analysis sets up the LOG_LINKS fields of insns because the
93 information needed to do so is readily available.
95 life_analysis deletes insns whose only effect is to store a value
96 that is never used.
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.
104 Each time an incrementing or decrementing address is created,
105 a REG_INC element is added to the insn's REG_NOTES list.
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. */
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"
122 #include "obstack.h"
123 #define obstack_chunk_alloc xmalloc
124 #define obstack_chunk_free free
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. */
131 extern struct obstack *function_obstack;
133 /* List of labels that must never be deleted. */
134 extern rtx forced_labels;
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. */
140 /* This is the limit of the allocated space in the following two arrays. */
142 static int max_uid_for_flow;
144 #define BLOCK_NUM(INSN) uid_block_number[INSN_UID (INSN)]
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. */
150 int *uid_block_number;
152 /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
154 #define INSN_VOLATILE(INSN) uid_volatile[INSN_UID (INSN)]
155 static char *uid_volatile;
157 /* Number of basic blocks in the current function. */
159 int n_basic_blocks;
161 /* Maximum register number used in this function, plus one. */
163 int max_regno;
165 /* Maximum number of SCRATCH rtx's used in any basic block of this
166 function. */
168 int max_scratch;
170 /* Number of SCRATCH rtx's in the current block. */
172 static int num_scratch;
174 /* Indexed by n, giving various register information */
176 reg_info *reg_n_info;
178 /* Size of the reg_n_info table. */
180 unsigned int reg_n_max;
182 /* Element N is the next insn that uses (hard or pseudo) register number N
183 within the current basic block; or zero, if there is no such insn.
184 This is valid only during the final backward scan in propagate_block. */
186 static rtx *reg_next_use;
188 /* Size of a regset for the current function,
189 in (1) bytes and (2) elements. */
191 int regset_bytes;
192 int regset_size;
194 /* Element N is first insn in basic block N.
195 This info lasts until we finish compiling the function. */
197 rtx *basic_block_head;
199 /* Element N is last insn in basic block N.
200 This info lasts until we finish compiling the function. */
202 rtx *basic_block_end;
204 /* Element N indicates whether basic block N can be reached through a
205 computed jump. */
207 char *basic_block_computed_jump_target;
209 /* Element N is a regset describing the registers live
210 at the start of basic block N.
211 This info lasts until we finish compiling the function. */
213 regset *basic_block_live_at_start;
215 /* Regset of regs live when calls to `setjmp'-like functions happen. */
217 regset regs_live_at_setjmp;
219 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
220 that have to go in the same hard reg.
221 The first two regs in the list are a pair, and the next two
222 are another pair, etc. */
223 rtx regs_may_share;
225 /* Element N is nonzero if control can drop into basic block N
226 from the preceding basic block. Freed after life_analysis. */
228 static char *basic_block_drops_in;
230 /* Element N is depth within loops of the last insn in basic block number N.
231 Freed after life_analysis. */
233 static short *basic_block_loop_depth;
235 /* Element N nonzero if basic block N can actually be reached.
236 Vector exists only during find_basic_blocks. */
238 static char *block_live_static;
240 /* Depth within loops of basic block being scanned for lifetime analysis,
241 plus one. This is the weight attached to references to registers. */
243 static int loop_depth;
245 /* During propagate_block, this is non-zero if the value of CC0 is live. */
247 static int cc0_live;
249 /* During propagate_block, this contains the last MEM stored into. It
250 is used to eliminate consecutive stores to the same location. */
252 static rtx last_mem_set;
254 /* Set of registers that may be eliminable. These are handled specially
255 in updating regs_ever_live. */
257 static HARD_REG_SET elim_reg_set;
259 /* Forward declarations */
260 static void find_basic_blocks_1 PROTO((rtx, rtx, int));
261 static void mark_label_ref PROTO((rtx, rtx, int));
262 static void life_analysis_1 PROTO((rtx, int));
263 void allocate_for_life_analysis PROTO((void));
264 void init_regset_vector PROTO((regset *, int, struct obstack *));
265 static void propagate_block PROTO((regset, rtx, rtx, int,
266 regset, int));
267 static rtx flow_delete_insn PROTO((rtx));
268 static int insn_dead_p PROTO((rtx, regset, int));
269 static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
270 static void mark_set_regs PROTO((regset, regset, rtx,
271 rtx, regset));
272 static void mark_set_1 PROTO((regset, regset, rtx,
273 rtx, regset));
274 #ifdef AUTO_INC_DEC
275 static void find_auto_inc PROTO((regset, rtx, rtx));
276 static int try_pre_increment_1 PROTO((rtx));
277 static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
278 #endif
279 static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
280 void dump_flow_info PROTO((FILE *));
281 static void add_pred_succ PROTO ((int, int, int_list_ptr *,
282 int_list_ptr *, int *, int *));
283 static int_list_ptr alloc_int_list_node PROTO ((int_list_block **));
284 static int_list_ptr add_int_list_node PROTO ((int_list_block **,
285 int_list **, int));
287 /* Find basic blocks of the current function.
288 F is the first insn of the function and NREGS the number of register numbers
289 in use.
290 LIVE_REACHABLE_P is non-zero if the caller needs all live blocks to
291 be reachable. This turns on a kludge that causes the control flow
292 information to be inaccurate and not suitable for passes like GCSE. */
294 void
295 find_basic_blocks (f, nregs, file, live_reachable_p)
296 rtx f;
297 int nregs;
298 FILE *file;
299 int live_reachable_p;
301 register rtx insn;
302 register int i;
303 rtx nonlocal_label_list = nonlocal_label_rtx_list ();
304 int in_libcall_block = 0;
306 /* Count the basic blocks. Also find maximum insn uid value used. */
309 register RTX_CODE prev_code = JUMP_INSN;
310 register RTX_CODE code;
311 int eh_region = 0;
313 max_uid_for_flow = 0;
315 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
318 /* Track when we are inside in LIBCALL block. */
319 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
320 && find_reg_note (insn, REG_LIBCALL, NULL_RTX))
321 in_libcall_block = 1;
323 code = GET_CODE (insn);
324 if (INSN_UID (insn) > max_uid_for_flow)
325 max_uid_for_flow = INSN_UID (insn);
326 if (code == CODE_LABEL
327 || (GET_RTX_CLASS (code) == 'i'
328 && (prev_code == JUMP_INSN
329 || (prev_code == CALL_INSN
330 && (nonlocal_label_list != 0 || eh_region)
331 && ! in_libcall_block)
332 || prev_code == BARRIER)))
333 i++;
335 if (code == CALL_INSN && find_reg_note (insn, REG_RETVAL, NULL_RTX))
336 code = INSN;
338 if (code != NOTE)
339 prev_code = code;
340 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
341 ++eh_region;
342 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
343 --eh_region;
345 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
346 && find_reg_note (insn, REG_RETVAL, NULL_RTX))
347 in_libcall_block = 0;
351 n_basic_blocks = i;
353 #ifdef AUTO_INC_DEC
354 /* Leave space for insns life_analysis makes in some cases for auto-inc.
355 These cases are rare, so we don't need too much space. */
356 max_uid_for_flow += max_uid_for_flow / 10;
357 #endif
359 /* Allocate some tables that last till end of compiling this function
360 and some needed only in find_basic_blocks and life_analysis. */
362 basic_block_head = (rtx *) xmalloc (n_basic_blocks * sizeof (rtx));
363 basic_block_end = (rtx *) xmalloc (n_basic_blocks * sizeof (rtx));
364 basic_block_drops_in = (char *) xmalloc (n_basic_blocks);
365 basic_block_computed_jump_target = (char *) oballoc (n_basic_blocks);
366 basic_block_loop_depth = (short *) xmalloc (n_basic_blocks * sizeof (short));
367 uid_block_number
368 = (int *) xmalloc ((max_uid_for_flow + 1) * sizeof (int));
369 uid_volatile = (char *) xmalloc (max_uid_for_flow + 1);
370 bzero (uid_volatile, max_uid_for_flow + 1);
372 find_basic_blocks_1 (f, nonlocal_label_list, live_reachable_p);
375 /* Find all basic blocks of the function whose first insn is F.
376 Store the correct data in the tables that describe the basic blocks,
377 set up the chains of references for each CODE_LABEL, and
378 delete any entire basic blocks that cannot be reached.
380 NONLOCAL_LABEL_LIST is a list of non-local labels in the function.
381 Blocks that are otherwise unreachable may be reachable with a non-local
382 goto.
383 LIVE_REACHABLE_P is non-zero if the caller needs all live blocks to
384 be reachable. This turns on a kludge that causes the control flow
385 information to be inaccurate and not suitable for passes like GCSE. */
387 static void
388 find_basic_blocks_1 (f, nonlocal_label_list, live_reachable_p)
389 rtx f, nonlocal_label_list;
390 int live_reachable_p;
392 register rtx insn;
393 register int i;
394 register char *block_live = (char *) alloca (n_basic_blocks);
395 register char *block_marked = (char *) alloca (n_basic_blocks);
396 /* An array of CODE_LABELs, indexed by UID for the start of the active
397 EH handler for each insn in F. */
398 int *active_eh_region;
399 int *nested_eh_region;
400 /* List of label_refs to all labels whose addresses are taken
401 and used as data. */
402 rtx label_value_list;
403 rtx x, note, eh_note;
404 enum rtx_code prev_code, code;
405 int depth, pass;
406 int in_libcall_block = 0;
407 int deleted_handler = 0;
409 pass = 1;
410 active_eh_region = (int *) alloca ((max_uid_for_flow + 1) * sizeof (int));
411 nested_eh_region = (int *) alloca ((max_label_num () + 1) * sizeof (int));
412 restart:
414 label_value_list = 0;
415 block_live_static = block_live;
416 bzero (block_live, n_basic_blocks);
417 bzero (block_marked, n_basic_blocks);
418 bzero (basic_block_computed_jump_target, n_basic_blocks);
419 bzero ((char *) active_eh_region, (max_uid_for_flow + 1) * sizeof (int));
420 bzero ((char *) nested_eh_region, (max_label_num () + 1) * sizeof (int));
421 current_function_has_computed_jump = 0;
423 /* Initialize with just block 0 reachable and no blocks marked. */
424 if (n_basic_blocks > 0)
425 block_live[0] = 1;
427 /* Initialize the ref chain of each label to 0. Record where all the
428 blocks start and end and their depth in loops. For each insn, record
429 the block it is in. Also mark as reachable any blocks headed by labels
430 that must not be deleted. */
432 for (eh_note = NULL_RTX, insn = f, i = -1, prev_code = JUMP_INSN, depth = 1;
433 insn; insn = NEXT_INSN (insn))
436 /* Track when we are inside in LIBCALL block. */
437 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
438 && find_reg_note (insn, REG_LIBCALL, NULL_RTX))
439 in_libcall_block = 1;
441 code = GET_CODE (insn);
442 if (code == NOTE)
444 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
445 depth++;
446 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
447 depth--;
450 /* A basic block starts at label, or after something that can jump. */
451 else if (code == CODE_LABEL
452 || (GET_RTX_CLASS (code) == 'i'
453 && (prev_code == JUMP_INSN
454 || (prev_code == CALL_INSN
455 && (nonlocal_label_list != 0 || eh_note)
456 && ! in_libcall_block)
457 || prev_code == BARRIER)))
459 basic_block_head[++i] = insn;
460 basic_block_end[i] = insn;
461 basic_block_loop_depth[i] = depth;
463 if (code == CODE_LABEL)
465 LABEL_REFS (insn) = insn;
466 /* Any label that cannot be deleted
467 is considered to start a reachable block. */
468 if (LABEL_PRESERVE_P (insn))
469 block_live[i] = 1;
473 else if (GET_RTX_CLASS (code) == 'i')
475 basic_block_end[i] = insn;
476 basic_block_loop_depth[i] = depth;
479 if (GET_RTX_CLASS (code) == 'i')
481 /* Make a list of all labels referred to other than by jumps. */
482 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
483 if (REG_NOTE_KIND (note) == REG_LABEL)
484 label_value_list = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
485 label_value_list);
488 /* Keep a lifo list of the currently active exception notes. */
489 if (GET_CODE (insn) == NOTE)
491 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
493 if (eh_note)
494 nested_eh_region [NOTE_BLOCK_NUMBER (insn)] =
495 NOTE_BLOCK_NUMBER (XEXP (eh_note, 0));
496 else
497 nested_eh_region [NOTE_BLOCK_NUMBER (insn)] = 0;
498 eh_note = gen_rtx_EXPR_LIST (VOIDmode,
499 insn, eh_note);
501 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
502 eh_note = XEXP (eh_note, 1);
504 /* If we encounter a CALL_INSN, note which exception handler it
505 might pass control to.
507 If doing asynchronous exceptions, record the active EH handler
508 for every insn, since most insns can throw. */
509 else if (eh_note
510 && (asynchronous_exceptions
511 || (GET_CODE (insn) == CALL_INSN
512 && ! in_libcall_block)))
513 active_eh_region[INSN_UID (insn)] =
514 NOTE_BLOCK_NUMBER (XEXP (eh_note, 0));
515 BLOCK_NUM (insn) = i;
517 if (code != NOTE)
518 prev_code = code;
520 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
521 && find_reg_note (insn, REG_RETVAL, NULL_RTX))
522 in_libcall_block = 0;
525 /* During the second pass, `n_basic_blocks' is only an upper bound.
526 Only perform the sanity check for the first pass, and on the second
527 pass ensure `n_basic_blocks' is set to the correct value. */
528 if (pass == 1 && i + 1 != n_basic_blocks)
529 abort ();
530 n_basic_blocks = i + 1;
532 /* Record which basic blocks control can drop in to. */
534 for (i = 0; i < n_basic_blocks; i++)
536 for (insn = PREV_INSN (basic_block_head[i]);
537 insn && GET_CODE (insn) == NOTE; insn = PREV_INSN (insn))
540 basic_block_drops_in[i] = insn && GET_CODE (insn) != BARRIER;
543 /* Now find which basic blocks can actually be reached
544 and put all jump insns' LABEL_REFS onto the ref-chains
545 of their target labels. */
547 if (n_basic_blocks > 0)
549 int something_marked = 1;
550 int deleted;
552 /* Pass over all blocks, marking each block that is reachable
553 and has not yet been marked.
554 Keep doing this until, in one pass, no blocks have been marked.
555 Then blocks_live and blocks_marked are identical and correct.
556 In addition, all jumps actually reachable have been marked. */
558 while (something_marked)
560 something_marked = 0;
561 for (i = 0; i < n_basic_blocks; i++)
562 if (block_live[i] && !block_marked[i])
564 block_marked[i] = 1;
565 something_marked = 1;
566 if (i + 1 < n_basic_blocks && basic_block_drops_in[i + 1])
567 block_live[i + 1] = 1;
568 insn = basic_block_end[i];
569 if (GET_CODE (insn) == JUMP_INSN)
570 mark_label_ref (PATTERN (insn), insn, 0);
572 /* If we have any forced labels, mark them as potentially
573 reachable from this block. */
574 for (x = forced_labels; x; x = XEXP (x, 1))
575 if (! LABEL_REF_NONLOCAL_P (x))
576 mark_label_ref (gen_rtx_LABEL_REF (VOIDmode, XEXP (x, 0)),
577 insn, 0);
579 /* Now scan the insns for this block, we may need to make
580 edges for some of them to various non-obvious locations
581 (exception handlers, nonlocal labels, etc). */
582 for (insn = basic_block_head[i];
583 insn != NEXT_INSN (basic_block_end[i]);
584 insn = NEXT_INSN (insn))
586 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
589 /* References to labels in non-jumping insns have
590 REG_LABEL notes attached to them.
592 This can happen for computed gotos; we don't care
593 about them here since the values are also on the
594 label_value_list and will be marked live if we find
595 a live computed goto.
597 This can also happen when we take the address of
598 a label to pass as an argument to __throw. Note
599 throw only uses the value to determine what handler
600 should be called -- ie the label is not used as
601 a jump target, it just marks regions in the code.
603 In theory we should be able to ignore the REG_LABEL
604 notes, but we have to make sure that the label and
605 associated insns aren't marked dead, so we make
606 the block in question live and create an edge from
607 this insn to the label. This is not strictly
608 correct, but it is close enough for now. */
609 for (note = REG_NOTES (insn);
610 note;
611 note = XEXP (note, 1))
613 if (REG_NOTE_KIND (note) == REG_LABEL)
615 x = XEXP (note, 0);
616 block_live[BLOCK_NUM (x)] = 1;
617 mark_label_ref (gen_rtx_LABEL_REF (VOIDmode, x),
618 insn, 0);
622 /* If this is a computed jump, then mark it as
623 reaching everything on the label_value_list
624 and forced_labels list. */
625 if (computed_jump_p (insn))
627 current_function_has_computed_jump = 1;
628 for (x = label_value_list; x; x = XEXP (x, 1))
630 int b = BLOCK_NUM (XEXP (x, 0));
631 basic_block_computed_jump_target[b] = 1;
632 mark_label_ref (gen_rtx_LABEL_REF (VOIDmode,
633 XEXP (x, 0)),
634 insn, 0);
637 for (x = forced_labels; x; x = XEXP (x, 1))
639 int b = BLOCK_NUM (XEXP (x, 0));
640 basic_block_computed_jump_target[b] = 1;
641 mark_label_ref (gen_rtx_LABEL_REF (VOIDmode,
642 XEXP (x, 0)),
643 insn, 0);
647 /* If this is a CALL_INSN, then mark it as reaching
648 the active EH handler for this CALL_INSN. If
649 we're handling asynchronous exceptions mark every
650 insn as reaching the active EH handler.
652 Also mark the CALL_INSN as reaching any nonlocal
653 goto sites. */
654 else if (asynchronous_exceptions
655 || (GET_CODE (insn) == CALL_INSN
656 && ! find_reg_note (insn, REG_RETVAL,
657 NULL_RTX)))
659 if (active_eh_region[INSN_UID (insn)])
661 int region;
662 handler_info *ptr;
663 region = active_eh_region[INSN_UID (insn)];
664 for ( ; region;
665 region = nested_eh_region[region])
667 ptr = get_first_handler (region);
668 for ( ; ptr ; ptr = ptr->next)
669 mark_label_ref (gen_rtx_LABEL_REF
670 (VOIDmode, ptr->handler_label), insn, 0);
673 if (!asynchronous_exceptions)
675 for (x = nonlocal_label_list;
677 x = XEXP (x, 1))
678 mark_label_ref (gen_rtx_LABEL_REF (VOIDmode,
679 XEXP (x, 0)),
680 insn, 0);
682 /* ??? This could be made smarter:
683 in some cases it's possible to tell that
684 certain calls will not do a nonlocal goto.
686 For example, if the nested functions that
687 do the nonlocal gotos do not have their
688 addresses taken, then only calls to those
689 functions or to other nested functions that
690 use them could possibly do nonlocal gotos. */
697 /* This should never happen. If it does that means we've computed an
698 incorrect flow graph, which can lead to aborts/crashes later in the
699 compiler or incorrect code generation.
701 We used to try and continue here, but that's just asking for trouble
702 later during the compile or at runtime. It's easier to debug the
703 problem here than later! */
704 for (i = 1; i < n_basic_blocks; i++)
705 if (block_live[i] && ! basic_block_drops_in[i]
706 && GET_CODE (basic_block_head[i]) == CODE_LABEL
707 && LABEL_REFS (basic_block_head[i]) == basic_block_head[i])
708 abort ();
710 /* Now delete the code for any basic blocks that can't be reached.
711 They can occur because jump_optimize does not recognize
712 unreachable loops as unreachable. */
714 deleted = 0;
715 for (i = 0; i < n_basic_blocks; i++)
716 if (!block_live[i])
718 deleted++;
720 /* Delete the insns in a (non-live) block. We physically delete
721 every non-note insn except the start and end (so
722 basic_block_head/end needn't be updated), we turn the latter
723 into NOTE_INSN_DELETED notes.
724 We use to "delete" the insns by turning them into notes, but
725 we may be deleting lots of insns that subsequent passes would
726 otherwise have to process. Secondly, lots of deleted blocks in
727 a row can really slow down propagate_block since it will
728 otherwise process insn-turned-notes multiple times when it
729 looks for loop begin/end notes. */
730 if (basic_block_head[i] != basic_block_end[i])
732 /* It would be quicker to delete all of these with a single
733 unchaining, rather than one at a time, but we need to keep
734 the NOTE's. */
735 insn = NEXT_INSN (basic_block_head[i]);
736 while (insn != basic_block_end[i])
738 if (GET_CODE (insn) == BARRIER)
739 abort ();
740 else if (GET_CODE (insn) != NOTE)
741 insn = flow_delete_insn (insn);
742 else
743 insn = NEXT_INSN (insn);
746 insn = basic_block_head[i];
747 if (GET_CODE (insn) != NOTE)
749 /* Turn the head into a deleted insn note. */
750 if (GET_CODE (insn) == BARRIER)
751 abort ();
753 /* If the head of this block is a CODE_LABEL, then it might
754 be the label for an exception handler which can't be
755 reached.
757 We need to remove the label from the exception_handler_label
758 list and remove the associated NOTE_EH_REGION_BEG and
759 NOTE_EH_REGION_END notes. */
760 if (GET_CODE (insn) == CODE_LABEL)
762 rtx x, *prev = &exception_handler_labels;
764 for (x = exception_handler_labels; x; x = XEXP (x, 1))
766 if (XEXP (x, 0) == insn)
768 /* Found a match, splice this label out of the
769 EH label list. */
770 *prev = XEXP (x, 1);
771 XEXP (x, 1) = NULL_RTX;
772 XEXP (x, 0) = NULL_RTX;
774 /* Remove the handler from all regions */
775 remove_handler (insn);
776 deleted_handler = 1;
777 break;
779 prev = &XEXP (x, 1);
783 PUT_CODE (insn, NOTE);
784 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
785 NOTE_SOURCE_FILE (insn) = 0;
787 insn = basic_block_end[i];
788 if (GET_CODE (insn) != NOTE)
790 /* Turn the tail into a deleted insn note. */
791 if (GET_CODE (insn) == BARRIER)
792 abort ();
793 PUT_CODE (insn, NOTE);
794 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
795 NOTE_SOURCE_FILE (insn) = 0;
797 /* BARRIERs are between basic blocks, not part of one.
798 Delete a BARRIER if the preceding jump is deleted.
799 We cannot alter a BARRIER into a NOTE
800 because it is too short; but we can really delete
801 it because it is not part of a basic block. */
802 if (NEXT_INSN (insn) != 0
803 && GET_CODE (NEXT_INSN (insn)) == BARRIER)
804 delete_insn (NEXT_INSN (insn));
806 /* Each time we delete some basic blocks,
807 see if there is a jump around them that is
808 being turned into a no-op. If so, delete it. */
810 if (block_live[i - 1])
812 register int j;
813 for (j = i + 1; j < n_basic_blocks; j++)
814 if (block_live[j])
816 rtx label;
817 insn = basic_block_end[i - 1];
818 if (GET_CODE (insn) == JUMP_INSN
819 /* An unconditional jump is the only possibility
820 we must check for, since a conditional one
821 would make these blocks live. */
822 && simplejump_p (insn)
823 && (label = XEXP (SET_SRC (PATTERN (insn)), 0), 1)
824 && INSN_UID (label) != 0
825 && BLOCK_NUM (label) == j)
827 int k;
829 /* The deleted blocks still show up in the cfg,
830 so we must set basic_block_drops_in for blocks
831 I to J inclusive to keep the cfg accurate. */
832 for (k = i; k <= j; k++)
833 basic_block_drops_in[k] = 1;
835 PUT_CODE (insn, NOTE);
836 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
837 NOTE_SOURCE_FILE (insn) = 0;
838 if (GET_CODE (NEXT_INSN (insn)) != BARRIER)
839 abort ();
840 delete_insn (NEXT_INSN (insn));
842 break;
846 /* If we deleted an exception handler, we may have EH region
847 begin/end blocks to remove as well. */
848 if (deleted_handler)
849 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
850 if (GET_CODE (insn) == NOTE)
852 if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
853 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
855 int num = CODE_LABEL_NUMBER (insn);
856 /* A NULL handler indicates a region is no longer needed */
857 if (get_first_handler (num) == NULL)
859 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
860 NOTE_SOURCE_FILE (insn) = 0;
865 /* There are pathological cases where one function calling hundreds of
866 nested inline functions can generate lots and lots of unreachable
867 blocks that jump can't delete. Since we don't use sparse matrices
868 a lot of memory will be needed to compile such functions.
869 Implementing sparse matrices is a fair bit of work and it is not
870 clear that they win more than they lose (we don't want to
871 unnecessarily slow down compilation of normal code). By making
872 another pass for the pathological case, we can greatly speed up
873 their compilation without hurting normal code. This works because
874 all the insns in the unreachable blocks have either been deleted or
875 turned into notes.
876 Note that we're talking about reducing memory usage by 10's of
877 megabytes and reducing compilation time by several minutes. */
878 /* ??? The choice of when to make another pass is a bit arbitrary,
879 and was derived from empirical data. */
880 if (pass == 1
881 && deleted > 200)
883 pass++;
884 n_basic_blocks -= deleted;
885 /* `n_basic_blocks' may not be correct at this point: two previously
886 separate blocks may now be merged. That's ok though as we
887 recalculate it during the second pass. It certainly can't be
888 any larger than the current value. */
889 goto restart;
894 /* Record INSN's block number as BB. */
896 void
897 set_block_num (insn, bb)
898 rtx insn;
899 int bb;
901 if (INSN_UID (insn) >= max_uid_for_flow)
903 /* Add one-eighth the size so we don't keep calling xrealloc. */
904 max_uid_for_flow = INSN_UID (insn) + (INSN_UID (insn) + 7) / 8;
905 uid_block_number = (int *)
906 xrealloc (uid_block_number, (max_uid_for_flow + 1) * sizeof (int));
908 BLOCK_NUM (insn) = bb;
912 /* Subroutines of find_basic_blocks. */
914 /* Check expression X for label references;
915 if one is found, add INSN to the label's chain of references.
917 CHECKDUP means check for and avoid creating duplicate references
918 from the same insn. Such duplicates do no serious harm but
919 can slow life analysis. CHECKDUP is set only when duplicates
920 are likely. */
922 static void
923 mark_label_ref (x, insn, checkdup)
924 rtx x, insn;
925 int checkdup;
927 register RTX_CODE code;
928 register int i;
929 register char *fmt;
931 /* We can be called with NULL when scanning label_value_list. */
932 if (x == 0)
933 return;
935 code = GET_CODE (x);
936 if (code == LABEL_REF)
938 register rtx label = XEXP (x, 0);
939 register rtx y;
940 if (GET_CODE (label) != CODE_LABEL)
941 abort ();
942 /* If the label was never emitted, this insn is junk,
943 but avoid a crash trying to refer to BLOCK_NUM (label).
944 This can happen as a result of a syntax error
945 and a diagnostic has already been printed. */
946 if (INSN_UID (label) == 0)
947 return;
948 CONTAINING_INSN (x) = insn;
949 /* if CHECKDUP is set, check for duplicate ref from same insn
950 and don't insert. */
951 if (checkdup)
952 for (y = LABEL_REFS (label); y != label; y = LABEL_NEXTREF (y))
953 if (CONTAINING_INSN (y) == insn)
954 return;
955 LABEL_NEXTREF (x) = LABEL_REFS (label);
956 LABEL_REFS (label) = x;
957 block_live_static[BLOCK_NUM (label)] = 1;
958 return;
961 fmt = GET_RTX_FORMAT (code);
962 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
964 if (fmt[i] == 'e')
965 mark_label_ref (XEXP (x, i), insn, 0);
966 if (fmt[i] == 'E')
968 register int j;
969 for (j = 0; j < XVECLEN (x, i); j++)
970 mark_label_ref (XVECEXP (x, i, j), insn, 1);
975 /* Delete INSN by patching it out.
976 Return the next insn. */
978 static rtx
979 flow_delete_insn (insn)
980 rtx insn;
982 /* ??? For the moment we assume we don't have to watch for NULLs here
983 since the start/end of basic blocks aren't deleted like this. */
984 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
985 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
986 return NEXT_INSN (insn);
989 /* Perform data flow analysis.
990 F is the first insn of the function and NREGS the number of register numbers
991 in use. */
993 void
994 life_analysis (f, nregs, file)
995 rtx f;
996 int nregs;
997 FILE *file;
999 #ifdef ELIMINABLE_REGS
1000 register size_t i;
1001 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
1002 #endif
1004 /* Record which registers will be eliminated. We use this in
1005 mark_used_regs. */
1007 CLEAR_HARD_REG_SET (elim_reg_set);
1009 #ifdef ELIMINABLE_REGS
1010 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
1011 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
1012 #else
1013 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
1014 #endif
1016 life_analysis_1 (f, nregs);
1017 if (file)
1018 dump_flow_info (file);
1020 free_basic_block_vars (1);
1023 /* Free the variables allocated by find_basic_blocks.
1025 KEEP_HEAD_END_P is non-zero if basic_block_head and basic_block_end
1026 are not to be freed. */
1028 void
1029 free_basic_block_vars (keep_head_end_p)
1030 int keep_head_end_p;
1032 if (basic_block_drops_in)
1034 free (basic_block_drops_in);
1035 /* Tell dump_flow_info this isn't available anymore. */
1036 basic_block_drops_in = 0;
1038 if (basic_block_loop_depth)
1040 free (basic_block_loop_depth);
1041 basic_block_loop_depth = 0;
1043 if (uid_block_number)
1045 free (uid_block_number);
1046 uid_block_number = 0;
1048 if (uid_volatile)
1050 free (uid_volatile);
1051 uid_volatile = 0;
1054 if (! keep_head_end_p && basic_block_head)
1056 free (basic_block_head);
1057 basic_block_head = 0;
1058 free (basic_block_end);
1059 basic_block_end = 0;
1063 /* Determine which registers are live at the start of each
1064 basic block of the function whose first insn is F.
1065 NREGS is the number of registers used in F.
1066 We allocate the vector basic_block_live_at_start
1067 and the regsets that it points to, and fill them with the data.
1068 regset_size and regset_bytes are also set here. */
1070 static void
1071 life_analysis_1 (f, nregs)
1072 rtx f;
1073 int nregs;
1075 int first_pass;
1076 int changed;
1077 /* For each basic block, a bitmask of regs
1078 live on exit from the block. */
1079 regset *basic_block_live_at_end;
1080 /* For each basic block, a bitmask of regs
1081 live on entry to a successor-block of this block.
1082 If this does not match basic_block_live_at_end,
1083 that must be updated, and the block must be rescanned. */
1084 regset *basic_block_new_live_at_end;
1085 /* For each basic block, a bitmask of regs
1086 whose liveness at the end of the basic block
1087 can make a difference in which regs are live on entry to the block.
1088 These are the regs that are set within the basic block,
1089 possibly excluding those that are used after they are set. */
1090 regset *basic_block_significant;
1091 register int i;
1092 rtx insn;
1094 struct obstack flow_obstack;
1096 gcc_obstack_init (&flow_obstack);
1098 max_regno = nregs;
1100 bzero (regs_ever_live, sizeof regs_ever_live);
1102 /* Allocate and zero out many data structures
1103 that will record the data from lifetime analysis. */
1105 allocate_for_life_analysis ();
1107 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
1108 bzero ((char *) reg_next_use, nregs * sizeof (rtx));
1110 /* Set up several regset-vectors used internally within this function.
1111 Their meanings are documented above, with their declarations. */
1113 basic_block_live_at_end
1114 = (regset *) alloca (n_basic_blocks * sizeof (regset));
1116 /* Don't use alloca since that leads to a crash rather than an error message
1117 if there isn't enough space.
1118 Don't use oballoc since we may need to allocate other things during
1119 this function on the temporary obstack. */
1120 init_regset_vector (basic_block_live_at_end, n_basic_blocks, &flow_obstack);
1122 basic_block_new_live_at_end
1123 = (regset *) alloca (n_basic_blocks * sizeof (regset));
1124 init_regset_vector (basic_block_new_live_at_end, n_basic_blocks,
1125 &flow_obstack);
1127 basic_block_significant
1128 = (regset *) alloca (n_basic_blocks * sizeof (regset));
1129 init_regset_vector (basic_block_significant, n_basic_blocks, &flow_obstack);
1131 /* Record which insns refer to any volatile memory
1132 or for any reason can't be deleted just because they are dead stores.
1133 Also, delete any insns that copy a register to itself. */
1135 for (insn = f; insn; insn = NEXT_INSN (insn))
1137 enum rtx_code code1 = GET_CODE (insn);
1138 if (code1 == CALL_INSN)
1139 INSN_VOLATILE (insn) = 1;
1140 else if (code1 == INSN || code1 == JUMP_INSN)
1142 /* Delete (in effect) any obvious no-op moves. */
1143 if (GET_CODE (PATTERN (insn)) == SET
1144 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
1145 && GET_CODE (SET_SRC (PATTERN (insn))) == REG
1146 && (REGNO (SET_DEST (PATTERN (insn)))
1147 == REGNO (SET_SRC (PATTERN (insn))))
1148 /* Insns carrying these notes are useful later on. */
1149 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1151 PUT_CODE (insn, NOTE);
1152 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1153 NOTE_SOURCE_FILE (insn) = 0;
1155 /* Delete (in effect) any obvious no-op moves. */
1156 else if (GET_CODE (PATTERN (insn)) == SET
1157 && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG
1158 && GET_CODE (SUBREG_REG (SET_DEST (PATTERN (insn)))) == REG
1159 && GET_CODE (SET_SRC (PATTERN (insn))) == SUBREG
1160 && GET_CODE (SUBREG_REG (SET_SRC (PATTERN (insn)))) == REG
1161 && (REGNO (SUBREG_REG (SET_DEST (PATTERN (insn))))
1162 == REGNO (SUBREG_REG (SET_SRC (PATTERN (insn)))))
1163 && SUBREG_WORD (SET_DEST (PATTERN (insn))) ==
1164 SUBREG_WORD (SET_SRC (PATTERN (insn)))
1165 /* Insns carrying these notes are useful later on. */
1166 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1168 PUT_CODE (insn, NOTE);
1169 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1170 NOTE_SOURCE_FILE (insn) = 0;
1172 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1174 /* If nothing but SETs of registers to themselves,
1175 this insn can also be deleted. */
1176 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1178 rtx tem = XVECEXP (PATTERN (insn), 0, i);
1180 if (GET_CODE (tem) == USE
1181 || GET_CODE (tem) == CLOBBER)
1182 continue;
1184 if (GET_CODE (tem) != SET
1185 || GET_CODE (SET_DEST (tem)) != REG
1186 || GET_CODE (SET_SRC (tem)) != REG
1187 || REGNO (SET_DEST (tem)) != REGNO (SET_SRC (tem)))
1188 break;
1191 if (i == XVECLEN (PATTERN (insn), 0)
1192 /* Insns carrying these notes are useful later on. */
1193 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1195 PUT_CODE (insn, NOTE);
1196 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1197 NOTE_SOURCE_FILE (insn) = 0;
1199 else
1200 INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
1202 else if (GET_CODE (PATTERN (insn)) != USE)
1203 INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
1204 /* A SET that makes space on the stack cannot be dead.
1205 (Such SETs occur only for allocating variable-size data,
1206 so they will always have a PLUS or MINUS according to the
1207 direction of stack growth.)
1208 Even if this function never uses this stack pointer value,
1209 signal handlers do! */
1210 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
1211 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1212 #ifdef STACK_GROWS_DOWNWARD
1213 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
1214 #else
1215 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1216 #endif
1217 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
1218 INSN_VOLATILE (insn) = 1;
1222 if (n_basic_blocks > 0)
1223 #ifdef EXIT_IGNORE_STACK
1224 if (! EXIT_IGNORE_STACK
1225 || (! FRAME_POINTER_REQUIRED
1226 && ! current_function_calls_alloca
1227 && flag_omit_frame_pointer))
1228 #endif
1230 /* If exiting needs the right stack value,
1231 consider the stack pointer live at the end of the function. */
1232 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1233 STACK_POINTER_REGNUM);
1234 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1235 STACK_POINTER_REGNUM);
1238 /* Mark the frame pointer is needed at the end of the function. If
1239 we end up eliminating it, it will be removed from the live list
1240 of each basic block by reload. */
1242 if (n_basic_blocks > 0)
1244 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1245 FRAME_POINTER_REGNUM);
1246 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1247 FRAME_POINTER_REGNUM);
1248 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1249 /* If they are different, also mark the hard frame pointer as live */
1250 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1251 HARD_FRAME_POINTER_REGNUM);
1252 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1253 HARD_FRAME_POINTER_REGNUM);
1254 #endif
1257 /* Mark all global registers and all registers used by the epilogue
1258 as being live at the end of the function since they may be
1259 referenced by our caller. */
1261 if (n_basic_blocks > 0)
1262 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1263 if (global_regs[i]
1264 #ifdef EPILOGUE_USES
1265 || EPILOGUE_USES (i)
1266 #endif
1269 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1], i);
1270 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1], i);
1273 /* Propagate life info through the basic blocks
1274 around the graph of basic blocks.
1276 This is a relaxation process: each time a new register
1277 is live at the end of the basic block, we must scan the block
1278 to determine which registers are, as a consequence, live at the beginning
1279 of that block. These registers must then be marked live at the ends
1280 of all the blocks that can transfer control to that block.
1281 The process continues until it reaches a fixed point. */
1283 first_pass = 1;
1284 changed = 1;
1285 while (changed)
1287 changed = 0;
1288 for (i = n_basic_blocks - 1; i >= 0; i--)
1290 int consider = first_pass;
1291 int must_rescan = first_pass;
1292 register int j;
1294 if (!first_pass)
1296 /* Set CONSIDER if this block needs thinking about at all
1297 (that is, if the regs live now at the end of it
1298 are not the same as were live at the end of it when
1299 we last thought about it).
1300 Set must_rescan if it needs to be thought about
1301 instruction by instruction (that is, if any additional
1302 reg that is live at the end now but was not live there before
1303 is one of the significant regs of this basic block). */
1305 EXECUTE_IF_AND_COMPL_IN_REG_SET
1306 (basic_block_new_live_at_end[i],
1307 basic_block_live_at_end[i], 0, j,
1309 consider = 1;
1310 if (REGNO_REG_SET_P (basic_block_significant[i], j))
1312 must_rescan = 1;
1313 goto done;
1316 done:
1317 if (! consider)
1318 continue;
1321 /* The live_at_start of this block may be changing,
1322 so another pass will be required after this one. */
1323 changed = 1;
1325 if (! must_rescan)
1327 /* No complete rescan needed;
1328 just record those variables newly known live at end
1329 as live at start as well. */
1330 IOR_AND_COMPL_REG_SET (basic_block_live_at_start[i],
1331 basic_block_new_live_at_end[i],
1332 basic_block_live_at_end[i]);
1334 IOR_AND_COMPL_REG_SET (basic_block_live_at_end[i],
1335 basic_block_new_live_at_end[i],
1336 basic_block_live_at_end[i]);
1338 else
1340 /* Update the basic_block_live_at_start
1341 by propagation backwards through the block. */
1342 COPY_REG_SET (basic_block_live_at_end[i],
1343 basic_block_new_live_at_end[i]);
1344 COPY_REG_SET (basic_block_live_at_start[i],
1345 basic_block_live_at_end[i]);
1346 propagate_block (basic_block_live_at_start[i],
1347 basic_block_head[i], basic_block_end[i], 0,
1348 first_pass ? basic_block_significant[i]
1349 : (regset) 0,
1354 register rtx jump, head;
1356 /* Update the basic_block_new_live_at_end's of the block
1357 that falls through into this one (if any). */
1358 head = basic_block_head[i];
1359 if (basic_block_drops_in[i])
1360 IOR_REG_SET (basic_block_new_live_at_end[i-1],
1361 basic_block_live_at_start[i]);
1363 /* Update the basic_block_new_live_at_end's of
1364 all the blocks that jump to this one. */
1365 if (GET_CODE (head) == CODE_LABEL)
1366 for (jump = LABEL_REFS (head);
1367 jump != head;
1368 jump = LABEL_NEXTREF (jump))
1370 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
1371 IOR_REG_SET (basic_block_new_live_at_end[from_block],
1372 basic_block_live_at_start[i]);
1375 #ifdef USE_C_ALLOCA
1376 alloca (0);
1377 #endif
1379 first_pass = 0;
1382 /* The only pseudos that are live at the beginning of the function are
1383 those that were not set anywhere in the function. local-alloc doesn't
1384 know how to handle these correctly, so mark them as not local to any
1385 one basic block. */
1387 if (n_basic_blocks > 0)
1388 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[0],
1389 FIRST_PSEUDO_REGISTER, i,
1391 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1394 /* Now the life information is accurate.
1395 Make one more pass over each basic block
1396 to delete dead stores, create autoincrement addressing
1397 and record how many times each register is used, is set, or dies.
1399 To save time, we operate directly in basic_block_live_at_end[i],
1400 thus destroying it (in fact, converting it into a copy of
1401 basic_block_live_at_start[i]). This is ok now because
1402 basic_block_live_at_end[i] is no longer used past this point. */
1404 max_scratch = 0;
1406 for (i = 0; i < n_basic_blocks; i++)
1408 propagate_block (basic_block_live_at_end[i],
1409 basic_block_head[i], basic_block_end[i], 1,
1410 (regset) 0, i);
1411 #ifdef USE_C_ALLOCA
1412 alloca (0);
1413 #endif
1416 #if 0
1417 /* Something live during a setjmp should not be put in a register
1418 on certain machines which restore regs from stack frames
1419 rather than from the jmpbuf.
1420 But we don't need to do this for the user's variables, since
1421 ANSI says only volatile variables need this. */
1422 #ifdef LONGJMP_RESTORE_FROM_STACK
1423 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1424 FIRST_PSEUDO_REGISTER, i,
1426 if (regno_reg_rtx[i] != 0
1427 && ! REG_USERVAR_P (regno_reg_rtx[i]))
1429 REG_LIVE_LENGTH (i) = -1;
1430 REG_BASIC_BLOCK (i) = -1;
1433 #endif
1434 #endif
1436 /* We have a problem with any pseudoreg that
1437 lives across the setjmp. ANSI says that if a
1438 user variable does not change in value
1439 between the setjmp and the longjmp, then the longjmp preserves it.
1440 This includes longjmp from a place where the pseudo appears dead.
1441 (In principle, the value still exists if it is in scope.)
1442 If the pseudo goes in a hard reg, some other value may occupy
1443 that hard reg where this pseudo is dead, thus clobbering the pseudo.
1444 Conclusion: such a pseudo must not go in a hard reg. */
1445 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1446 FIRST_PSEUDO_REGISTER, i,
1448 if (regno_reg_rtx[i] != 0)
1450 REG_LIVE_LENGTH (i) = -1;
1451 REG_BASIC_BLOCK (i) = -1;
1456 free_regset_vector (basic_block_live_at_end, n_basic_blocks);
1457 free_regset_vector (basic_block_new_live_at_end, n_basic_blocks);
1458 free_regset_vector (basic_block_significant, n_basic_blocks);
1459 basic_block_live_at_end = (regset *)0;
1460 basic_block_new_live_at_end = (regset *)0;
1461 basic_block_significant = (regset *)0;
1463 obstack_free (&flow_obstack, NULL_PTR);
1466 /* Subroutines of life analysis. */
1468 /* Allocate the permanent data structures that represent the results
1469 of life analysis. Not static since used also for stupid life analysis. */
1471 void
1472 allocate_for_life_analysis ()
1474 register int i;
1476 /* Recalculate the register space, in case it has grown. Old style
1477 vector oriented regsets would set regset_{size,bytes} here also. */
1478 allocate_reg_info (max_regno, FALSE, FALSE);
1480 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
1481 information, explicitly reset it here. The allocation should have
1482 already happened on the previous reg_scan pass. Make sure in case
1483 some more registers were allocated. */
1484 for (i = 0; i < max_regno; i++)
1485 REG_N_SETS (i) = 0;
1487 basic_block_live_at_start
1488 = (regset *) oballoc (n_basic_blocks * sizeof (regset));
1489 init_regset_vector (basic_block_live_at_start, n_basic_blocks,
1490 function_obstack);
1492 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
1493 CLEAR_REG_SET (regs_live_at_setjmp);
1496 /* Make each element of VECTOR point at a regset. The vector has
1497 NELTS elements, and space is allocated from the ALLOC_OBSTACK
1498 obstack. */
1500 void
1501 init_regset_vector (vector, nelts, alloc_obstack)
1502 regset *vector;
1503 int nelts;
1504 struct obstack *alloc_obstack;
1506 register int i;
1508 for (i = 0; i < nelts; i++)
1510 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
1511 CLEAR_REG_SET (vector[i]);
1515 /* Release any additional space allocated for each element of VECTOR point
1516 other than the regset header itself. The vector has NELTS elements. */
1518 void
1519 free_regset_vector (vector, nelts)
1520 regset *vector;
1521 int nelts;
1523 register int i;
1525 for (i = 0; i < nelts; i++)
1526 FREE_REG_SET (vector[i]);
1529 /* Compute the registers live at the beginning of a basic block
1530 from those live at the end.
1532 When called, OLD contains those live at the end.
1533 On return, it contains those live at the beginning.
1534 FIRST and LAST are the first and last insns of the basic block.
1536 FINAL is nonzero if we are doing the final pass which is not
1537 for computing the life info (since that has already been done)
1538 but for acting on it. On this pass, we delete dead stores,
1539 set up the logical links and dead-variables lists of instructions,
1540 and merge instructions for autoincrement and autodecrement addresses.
1542 SIGNIFICANT is nonzero only the first time for each basic block.
1543 If it is nonzero, it points to a regset in which we store
1544 a 1 for each register that is set within the block.
1546 BNUM is the number of the basic block. */
1548 static void
1549 propagate_block (old, first, last, final, significant, bnum)
1550 register regset old;
1551 rtx first;
1552 rtx last;
1553 int final;
1554 regset significant;
1555 int bnum;
1557 register rtx insn;
1558 rtx prev;
1559 regset live;
1560 regset dead;
1562 /* The following variables are used only if FINAL is nonzero. */
1563 /* This vector gets one element for each reg that has been live
1564 at any point in the basic block that has been scanned so far.
1565 SOMETIMES_MAX says how many elements are in use so far. */
1566 register int *regs_sometimes_live;
1567 int sometimes_max = 0;
1568 /* This regset has 1 for each reg that we have seen live so far.
1569 It and REGS_SOMETIMES_LIVE are updated together. */
1570 regset maxlive;
1572 /* The loop depth may change in the middle of a basic block. Since we
1573 scan from end to beginning, we start with the depth at the end of the
1574 current basic block, and adjust as we pass ends and starts of loops. */
1575 loop_depth = basic_block_loop_depth[bnum];
1577 dead = ALLOCA_REG_SET ();
1578 live = ALLOCA_REG_SET ();
1580 cc0_live = 0;
1581 last_mem_set = 0;
1583 /* Include any notes at the end of the block in the scan.
1584 This is in case the block ends with a call to setjmp. */
1586 while (NEXT_INSN (last) != 0 && GET_CODE (NEXT_INSN (last)) == NOTE)
1588 /* Look for loop boundaries, we are going forward here. */
1589 last = NEXT_INSN (last);
1590 if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_BEG)
1591 loop_depth++;
1592 else if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_END)
1593 loop_depth--;
1596 if (final)
1598 register int i;
1600 num_scratch = 0;
1601 maxlive = ALLOCA_REG_SET ();
1602 COPY_REG_SET (maxlive, old);
1603 regs_sometimes_live = (int *) alloca (max_regno * sizeof (int));
1605 /* Process the regs live at the end of the block.
1606 Enter them in MAXLIVE and REGS_SOMETIMES_LIVE.
1607 Also mark them as not local to any one basic block. */
1608 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
1610 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1611 regs_sometimes_live[sometimes_max] = i;
1612 sometimes_max++;
1616 /* Scan the block an insn at a time from end to beginning. */
1618 for (insn = last; ; insn = prev)
1620 prev = PREV_INSN (insn);
1622 if (GET_CODE (insn) == NOTE)
1624 /* Look for loop boundaries, remembering that we are going
1625 backwards. */
1626 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1627 loop_depth++;
1628 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1629 loop_depth--;
1631 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
1632 Abort now rather than setting register status incorrectly. */
1633 if (loop_depth == 0)
1634 abort ();
1636 /* If this is a call to `setjmp' et al,
1637 warn if any non-volatile datum is live. */
1639 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
1640 IOR_REG_SET (regs_live_at_setjmp, old);
1643 /* Update the life-status of regs for this insn.
1644 First DEAD gets which regs are set in this insn
1645 then LIVE gets which regs are used in this insn.
1646 Then the regs live before the insn
1647 are those live after, with DEAD regs turned off,
1648 and then LIVE regs turned on. */
1650 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1652 register int i;
1653 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1654 int insn_is_dead
1655 = (insn_dead_p (PATTERN (insn), old, 0)
1656 /* Don't delete something that refers to volatile storage! */
1657 && ! INSN_VOLATILE (insn));
1658 int libcall_is_dead
1659 = (insn_is_dead && note != 0
1660 && libcall_dead_p (PATTERN (insn), old, note, insn));
1662 /* If an instruction consists of just dead store(s) on final pass,
1663 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
1664 We could really delete it with delete_insn, but that
1665 can cause trouble for first or last insn in a basic block. */
1666 if (final && insn_is_dead)
1668 PUT_CODE (insn, NOTE);
1669 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1670 NOTE_SOURCE_FILE (insn) = 0;
1672 /* CC0 is now known to be dead. Either this insn used it,
1673 in which case it doesn't anymore, or clobbered it,
1674 so the next insn can't use it. */
1675 cc0_live = 0;
1677 /* If this insn is copying the return value from a library call,
1678 delete the entire library call. */
1679 if (libcall_is_dead)
1681 rtx first = XEXP (note, 0);
1682 rtx p = insn;
1683 while (INSN_DELETED_P (first))
1684 first = NEXT_INSN (first);
1685 while (p != first)
1687 p = PREV_INSN (p);
1688 PUT_CODE (p, NOTE);
1689 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
1690 NOTE_SOURCE_FILE (p) = 0;
1693 goto flushed;
1696 CLEAR_REG_SET (dead);
1697 CLEAR_REG_SET (live);
1699 /* See if this is an increment or decrement that can be
1700 merged into a following memory address. */
1701 #ifdef AUTO_INC_DEC
1703 register rtx x = single_set (insn);
1705 /* Does this instruction increment or decrement a register? */
1706 if (final && x != 0
1707 && GET_CODE (SET_DEST (x)) == REG
1708 && (GET_CODE (SET_SRC (x)) == PLUS
1709 || GET_CODE (SET_SRC (x)) == MINUS)
1710 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1711 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1712 /* Ok, look for a following memory ref we can combine with.
1713 If one is found, change the memory ref to a PRE_INC
1714 or PRE_DEC, cancel this insn, and return 1.
1715 Return 0 if nothing has been done. */
1716 && try_pre_increment_1 (insn))
1717 goto flushed;
1719 #endif /* AUTO_INC_DEC */
1721 /* If this is not the final pass, and this insn is copying the
1722 value of a library call and it's dead, don't scan the
1723 insns that perform the library call, so that the call's
1724 arguments are not marked live. */
1725 if (libcall_is_dead)
1727 /* Mark the dest reg as `significant'. */
1728 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
1730 insn = XEXP (note, 0);
1731 prev = PREV_INSN (insn);
1733 else if (GET_CODE (PATTERN (insn)) == SET
1734 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1735 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1736 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1737 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1738 /* We have an insn to pop a constant amount off the stack.
1739 (Such insns use PLUS regardless of the direction of the stack,
1740 and any insn to adjust the stack by a constant is always a pop.)
1741 These insns, if not dead stores, have no effect on life. */
1743 else
1745 /* LIVE gets the regs used in INSN;
1746 DEAD gets those set by it. Dead insns don't make anything
1747 live. */
1749 mark_set_regs (old, dead, PATTERN (insn),
1750 final ? insn : NULL_RTX, significant);
1752 /* If an insn doesn't use CC0, it becomes dead since we
1753 assume that every insn clobbers it. So show it dead here;
1754 mark_used_regs will set it live if it is referenced. */
1755 cc0_live = 0;
1757 if (! insn_is_dead)
1758 mark_used_regs (old, live, PATTERN (insn), final, insn);
1760 /* Sometimes we may have inserted something before INSN (such as
1761 a move) when we make an auto-inc. So ensure we will scan
1762 those insns. */
1763 #ifdef AUTO_INC_DEC
1764 prev = PREV_INSN (insn);
1765 #endif
1767 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
1769 register int i;
1771 rtx note;
1773 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1774 note;
1775 note = XEXP (note, 1))
1776 if (GET_CODE (XEXP (note, 0)) == USE)
1777 mark_used_regs (old, live, SET_DEST (XEXP (note, 0)),
1778 final, insn);
1780 /* Each call clobbers all call-clobbered regs that are not
1781 global or fixed. Note that the function-value reg is a
1782 call-clobbered reg, and mark_set_regs has already had
1783 a chance to handle it. */
1785 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1786 if (call_used_regs[i] && ! global_regs[i]
1787 && ! fixed_regs[i])
1788 SET_REGNO_REG_SET (dead, i);
1790 /* The stack ptr is used (honorarily) by a CALL insn. */
1791 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
1793 /* Calls may also reference any of the global registers,
1794 so they are made live. */
1795 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1796 if (global_regs[i])
1797 mark_used_regs (old, live,
1798 gen_rtx_REG (reg_raw_mode[i], i),
1799 final, insn);
1801 /* Calls also clobber memory. */
1802 last_mem_set = 0;
1805 /* Update OLD for the registers used or set. */
1806 AND_COMPL_REG_SET (old, dead);
1807 IOR_REG_SET (old, live);
1809 if (GET_CODE (insn) == CALL_INSN && final)
1811 /* Any regs live at the time of a call instruction
1812 must not go in a register clobbered by calls.
1813 Find all regs now live and record this for them. */
1815 register int *p = regs_sometimes_live;
1817 for (i = 0; i < sometimes_max; i++, p++)
1818 if (REGNO_REG_SET_P (old, *p))
1819 REG_N_CALLS_CROSSED (*p)++;
1823 /* On final pass, add any additional sometimes-live regs
1824 into MAXLIVE and REGS_SOMETIMES_LIVE.
1825 Also update counts of how many insns each reg is live at. */
1827 if (final)
1829 register int regno;
1830 register int *p;
1832 EXECUTE_IF_AND_COMPL_IN_REG_SET
1833 (live, maxlive, 0, regno,
1835 regs_sometimes_live[sometimes_max++] = regno;
1836 SET_REGNO_REG_SET (maxlive, regno);
1839 p = regs_sometimes_live;
1840 for (i = 0; i < sometimes_max; i++)
1842 regno = *p++;
1843 if (REGNO_REG_SET_P (old, regno))
1844 REG_LIVE_LENGTH (regno)++;
1848 flushed: ;
1849 if (insn == first)
1850 break;
1853 FREE_REG_SET (dead);
1854 FREE_REG_SET (live);
1855 if (final)
1856 FREE_REG_SET (maxlive);
1858 if (num_scratch > max_scratch)
1859 max_scratch = num_scratch;
1862 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
1863 (SET expressions whose destinations are registers dead after the insn).
1864 NEEDED is the regset that says which regs are alive after the insn.
1866 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL. */
1868 static int
1869 insn_dead_p (x, needed, call_ok)
1870 rtx x;
1871 regset needed;
1872 int call_ok;
1874 enum rtx_code code = GET_CODE (x);
1876 /* If setting something that's a reg or part of one,
1877 see if that register's altered value will be live. */
1879 if (code == SET)
1881 rtx r = SET_DEST (x);
1883 /* A SET that is a subroutine call cannot be dead. */
1884 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
1885 return 0;
1887 #ifdef HAVE_cc0
1888 if (GET_CODE (r) == CC0)
1889 return ! cc0_live;
1890 #endif
1892 if (GET_CODE (r) == MEM && last_mem_set && ! MEM_VOLATILE_P (r)
1893 && rtx_equal_p (r, last_mem_set))
1894 return 1;
1896 while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART
1897 || GET_CODE (r) == ZERO_EXTRACT)
1898 r = SUBREG_REG (r);
1900 if (GET_CODE (r) == REG)
1902 int regno = REGNO (r);
1904 /* Don't delete insns to set global regs. */
1905 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1906 /* Make sure insns to set frame pointer aren't deleted. */
1907 || regno == FRAME_POINTER_REGNUM
1908 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1909 || regno == HARD_FRAME_POINTER_REGNUM
1910 #endif
1911 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1912 /* Make sure insns to set arg pointer are never deleted
1913 (if the arg pointer isn't fixed, there will be a USE for
1914 it, so we can treat it normally). */
1915 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1916 #endif
1917 || REGNO_REG_SET_P (needed, regno))
1918 return 0;
1920 /* If this is a hard register, verify that subsequent words are
1921 not needed. */
1922 if (regno < FIRST_PSEUDO_REGISTER)
1924 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
1926 while (--n > 0)
1927 if (REGNO_REG_SET_P (needed, regno+n))
1928 return 0;
1931 return 1;
1935 /* If performing several activities,
1936 insn is dead if each activity is individually dead.
1937 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
1938 that's inside a PARALLEL doesn't make the insn worth keeping. */
1939 else if (code == PARALLEL)
1941 int i = XVECLEN (x, 0);
1943 for (i--; i >= 0; i--)
1944 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
1945 && GET_CODE (XVECEXP (x, 0, i)) != USE
1946 && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok))
1947 return 0;
1949 return 1;
1952 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
1953 is not necessarily true for hard registers. */
1954 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
1955 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
1956 && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0))))
1957 return 1;
1959 /* We do not check other CLOBBER or USE here. An insn consisting of just
1960 a CLOBBER or just a USE should not be deleted. */
1961 return 0;
1964 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
1965 return 1 if the entire library call is dead.
1966 This is true if X copies a register (hard or pseudo)
1967 and if the hard return reg of the call insn is dead.
1968 (The caller should have tested the destination of X already for death.)
1970 If this insn doesn't just copy a register, then we don't
1971 have an ordinary libcall. In that case, cse could not have
1972 managed to substitute the source for the dest later on,
1973 so we can assume the libcall is dead.
1975 NEEDED is the bit vector of pseudoregs live before this insn.
1976 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
1978 static int
1979 libcall_dead_p (x, needed, note, insn)
1980 rtx x;
1981 regset needed;
1982 rtx note;
1983 rtx insn;
1985 register RTX_CODE code = GET_CODE (x);
1987 if (code == SET)
1989 register rtx r = SET_SRC (x);
1990 if (GET_CODE (r) == REG)
1992 rtx call = XEXP (note, 0);
1993 register int i;
1995 /* Find the call insn. */
1996 while (call != insn && GET_CODE (call) != CALL_INSN)
1997 call = NEXT_INSN (call);
1999 /* If there is none, do nothing special,
2000 since ordinary death handling can understand these insns. */
2001 if (call == insn)
2002 return 0;
2004 /* See if the hard reg holding the value is dead.
2005 If this is a PARALLEL, find the call within it. */
2006 call = PATTERN (call);
2007 if (GET_CODE (call) == PARALLEL)
2009 for (i = XVECLEN (call, 0) - 1; i >= 0; i--)
2010 if (GET_CODE (XVECEXP (call, 0, i)) == SET
2011 && GET_CODE (SET_SRC (XVECEXP (call, 0, i))) == CALL)
2012 break;
2014 /* This may be a library call that is returning a value
2015 via invisible pointer. Do nothing special, since
2016 ordinary death handling can understand these insns. */
2017 if (i < 0)
2018 return 0;
2020 call = XVECEXP (call, 0, i);
2023 return insn_dead_p (call, needed, 1);
2026 return 1;
2029 /* Return 1 if register REGNO was used before it was set.
2030 In other words, if it is live at function entry.
2031 Don't count global register variables or variables in registers
2032 that can be used for function arg passing, though. */
2035 regno_uninitialized (regno)
2036 int regno;
2038 if (n_basic_blocks == 0
2039 || (regno < FIRST_PSEUDO_REGISTER
2040 && (global_regs[regno] || FUNCTION_ARG_REGNO_P (regno))))
2041 return 0;
2043 return REGNO_REG_SET_P (basic_block_live_at_start[0], regno);
2046 /* 1 if register REGNO was alive at a place where `setjmp' was called
2047 and was set more than once or is an argument.
2048 Such regs may be clobbered by `longjmp'. */
2051 regno_clobbered_at_setjmp (regno)
2052 int regno;
2054 if (n_basic_blocks == 0)
2055 return 0;
2057 return ((REG_N_SETS (regno) > 1
2058 || REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2059 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
2062 /* Process the registers that are set within X.
2063 Their bits are set to 1 in the regset DEAD,
2064 because they are dead prior to this insn.
2066 If INSN is nonzero, it is the insn being processed
2067 and the fact that it is nonzero implies this is the FINAL pass
2068 in propagate_block. In this case, various info about register
2069 usage is stored, LOG_LINKS fields of insns are set up. */
2071 static void
2072 mark_set_regs (needed, dead, x, insn, significant)
2073 regset needed;
2074 regset dead;
2075 rtx x;
2076 rtx insn;
2077 regset significant;
2079 register RTX_CODE code = GET_CODE (x);
2081 if (code == SET || code == CLOBBER)
2082 mark_set_1 (needed, dead, x, insn, significant);
2083 else if (code == PARALLEL)
2085 register int i;
2086 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2088 code = GET_CODE (XVECEXP (x, 0, i));
2089 if (code == SET || code == CLOBBER)
2090 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
2095 /* Process a single SET rtx, X. */
2097 static void
2098 mark_set_1 (needed, dead, x, insn, significant)
2099 regset needed;
2100 regset dead;
2101 rtx x;
2102 rtx insn;
2103 regset significant;
2105 register int regno;
2106 register rtx reg = SET_DEST (x);
2108 /* Modifying just one hardware register of a multi-reg value
2109 or just a byte field of a register
2110 does not mean the value from before this insn is now dead.
2111 But it does mean liveness of that register at the end of the block
2112 is significant.
2114 Within mark_set_1, however, we treat it as if the register is
2115 indeed modified. mark_used_regs will, however, also treat this
2116 register as being used. Thus, we treat these insns as setting a
2117 new value for the register as a function of its old value. This
2118 cases LOG_LINKS to be made appropriately and this will help combine. */
2120 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
2121 || GET_CODE (reg) == SIGN_EXTRACT
2122 || GET_CODE (reg) == STRICT_LOW_PART)
2123 reg = XEXP (reg, 0);
2125 /* If we are writing into memory or into a register mentioned in the
2126 address of the last thing stored into memory, show we don't know
2127 what the last store was. If we are writing memory, save the address
2128 unless it is volatile. */
2129 if (GET_CODE (reg) == MEM
2130 || (GET_CODE (reg) == REG
2131 && last_mem_set != 0 && reg_overlap_mentioned_p (reg, last_mem_set)))
2132 last_mem_set = 0;
2134 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
2135 /* There are no REG_INC notes for SP, so we can't assume we'll see
2136 everything that invalidates it. To be safe, don't eliminate any
2137 stores though SP; none of them should be redundant anyway. */
2138 && ! reg_mentioned_p (stack_pointer_rtx, reg))
2139 last_mem_set = reg;
2141 if (GET_CODE (reg) == REG
2142 && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM)
2143 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2144 && regno != HARD_FRAME_POINTER_REGNUM
2145 #endif
2146 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2147 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2148 #endif
2149 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
2150 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
2152 int some_needed = REGNO_REG_SET_P (needed, regno);
2153 int some_not_needed = ! some_needed;
2155 /* Mark it as a significant register for this basic block. */
2156 if (significant)
2157 SET_REGNO_REG_SET (significant, regno);
2159 /* Mark it as dead before this insn. */
2160 SET_REGNO_REG_SET (dead, regno);
2162 /* A hard reg in a wide mode may really be multiple registers.
2163 If so, mark all of them just like the first. */
2164 if (regno < FIRST_PSEUDO_REGISTER)
2166 int n;
2168 /* Nothing below is needed for the stack pointer; get out asap.
2169 Eg, log links aren't needed, since combine won't use them. */
2170 if (regno == STACK_POINTER_REGNUM)
2171 return;
2173 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2174 while (--n > 0)
2176 int regno_n = regno + n;
2177 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2178 if (significant)
2179 SET_REGNO_REG_SET (significant, regno_n);
2181 SET_REGNO_REG_SET (dead, regno_n);
2182 some_needed |= needed_regno;
2183 some_not_needed |= ! needed_regno;
2186 /* Additional data to record if this is the final pass. */
2187 if (insn)
2189 register rtx y = reg_next_use[regno];
2190 register int blocknum = BLOCK_NUM (insn);
2192 /* If this is a hard reg, record this function uses the reg. */
2194 if (regno < FIRST_PSEUDO_REGISTER)
2196 register int i;
2197 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
2199 for (i = regno; i < endregno; i++)
2201 /* The next use is no longer "next", since a store
2202 intervenes. */
2203 reg_next_use[i] = 0;
2205 regs_ever_live[i] = 1;
2206 REG_N_SETS (i)++;
2209 else
2211 /* The next use is no longer "next", since a store
2212 intervenes. */
2213 reg_next_use[regno] = 0;
2215 /* Keep track of which basic blocks each reg appears in. */
2217 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2218 REG_BASIC_BLOCK (regno) = blocknum;
2219 else if (REG_BASIC_BLOCK (regno) != blocknum)
2220 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2222 /* Count (weighted) references, stores, etc. This counts a
2223 register twice if it is modified, but that is correct. */
2224 REG_N_SETS (regno)++;
2226 REG_N_REFS (regno) += loop_depth;
2228 /* The insns where a reg is live are normally counted
2229 elsewhere, but we want the count to include the insn
2230 where the reg is set, and the normal counting mechanism
2231 would not count it. */
2232 REG_LIVE_LENGTH (regno)++;
2235 if (! some_not_needed)
2237 /* Make a logical link from the next following insn
2238 that uses this register, back to this insn.
2239 The following insns have already been processed.
2241 We don't build a LOG_LINK for hard registers containing
2242 in ASM_OPERANDs. If these registers get replaced,
2243 we might wind up changing the semantics of the insn,
2244 even if reload can make what appear to be valid assignments
2245 later. */
2246 if (y && (BLOCK_NUM (y) == blocknum)
2247 && (regno >= FIRST_PSEUDO_REGISTER
2248 || asm_noperands (PATTERN (y)) < 0))
2249 LOG_LINKS (y)
2250 = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y));
2252 else if (! some_needed)
2254 /* Note that dead stores have already been deleted when possible
2255 If we get here, we have found a dead store that cannot
2256 be eliminated (because the same insn does something useful).
2257 Indicate this by marking the reg being set as dying here. */
2258 REG_NOTES (insn)
2259 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2260 REG_N_DEATHS (REGNO (reg))++;
2262 else
2264 /* This is a case where we have a multi-word hard register
2265 and some, but not all, of the words of the register are
2266 needed in subsequent insns. Write REG_UNUSED notes
2267 for those parts that were not needed. This case should
2268 be rare. */
2270 int i;
2272 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
2273 i >= 0; i--)
2274 if (!REGNO_REG_SET_P (needed, regno + i))
2275 REG_NOTES (insn)
2276 = gen_rtx_EXPR_LIST (REG_UNUSED,
2277 gen_rtx_REG (reg_raw_mode[regno + i],
2278 regno + i),
2279 REG_NOTES (insn));
2283 else if (GET_CODE (reg) == REG)
2284 reg_next_use[regno] = 0;
2286 /* If this is the last pass and this is a SCRATCH, show it will be dying
2287 here and count it. */
2288 else if (GET_CODE (reg) == SCRATCH && insn != 0)
2290 REG_NOTES (insn)
2291 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
2292 num_scratch++;
2296 #ifdef AUTO_INC_DEC
2298 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
2299 reference. */
2301 static void
2302 find_auto_inc (needed, x, insn)
2303 regset needed;
2304 rtx x;
2305 rtx insn;
2307 rtx addr = XEXP (x, 0);
2308 HOST_WIDE_INT offset = 0;
2309 rtx set;
2311 /* Here we detect use of an index register which might be good for
2312 postincrement, postdecrement, preincrement, or predecrement. */
2314 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
2315 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
2317 if (GET_CODE (addr) == REG)
2319 register rtx y;
2320 register int size = GET_MODE_SIZE (GET_MODE (x));
2321 rtx use;
2322 rtx incr;
2323 int regno = REGNO (addr);
2325 /* Is the next use an increment that might make auto-increment? */
2326 if ((incr = reg_next_use[regno]) != 0
2327 && (set = single_set (incr)) != 0
2328 && GET_CODE (set) == SET
2329 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
2330 /* Can't add side effects to jumps; if reg is spilled and
2331 reloaded, there's no way to store back the altered value. */
2332 && GET_CODE (insn) != JUMP_INSN
2333 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
2334 && XEXP (y, 0) == addr
2335 && GET_CODE (XEXP (y, 1)) == CONST_INT
2336 && (0
2337 #ifdef HAVE_POST_INCREMENT
2338 || (INTVAL (XEXP (y, 1)) == size && offset == 0)
2339 #endif
2340 #ifdef HAVE_POST_DECREMENT
2341 || (INTVAL (XEXP (y, 1)) == - size && offset == 0)
2342 #endif
2343 #ifdef HAVE_PRE_INCREMENT
2344 || (INTVAL (XEXP (y, 1)) == size && offset == size)
2345 #endif
2346 #ifdef HAVE_PRE_DECREMENT
2347 || (INTVAL (XEXP (y, 1)) == - size && offset == - size)
2348 #endif
2350 /* Make sure this reg appears only once in this insn. */
2351 && (use = find_use_as_address (PATTERN (insn), addr, offset),
2352 use != 0 && use != (rtx) 1))
2354 rtx q = SET_DEST (set);
2355 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
2356 ? (offset ? PRE_INC : POST_INC)
2357 : (offset ? PRE_DEC : POST_DEC));
2359 if (dead_or_set_p (incr, addr))
2361 /* This is the simple case. Try to make the auto-inc. If
2362 we can't, we are done. Otherwise, we will do any
2363 needed updates below. */
2364 if (! validate_change (insn, &XEXP (x, 0),
2365 gen_rtx_fmt_e (inc_code, Pmode, addr),
2367 return;
2369 else if (GET_CODE (q) == REG
2370 /* PREV_INSN used here to check the semi-open interval
2371 [insn,incr). */
2372 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
2373 /* We must also check for sets of q as q may be
2374 a call clobbered hard register and there may
2375 be a call between PREV_INSN (insn) and incr. */
2376 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
2378 /* We have *p followed sometime later by q = p+size.
2379 Both p and q must be live afterward,
2380 and q is not used between INSN and it's assignment.
2381 Change it to q = p, ...*q..., q = q+size.
2382 Then fall into the usual case. */
2383 rtx insns, temp;
2385 start_sequence ();
2386 emit_move_insn (q, addr);
2387 insns = get_insns ();
2388 end_sequence ();
2390 /* If anything in INSNS have UID's that don't fit within the
2391 extra space we allocate earlier, we can't make this auto-inc.
2392 This should never happen. */
2393 for (temp = insns; temp; temp = NEXT_INSN (temp))
2395 if (INSN_UID (temp) > max_uid_for_flow)
2396 return;
2397 BLOCK_NUM (temp) = BLOCK_NUM (insn);
2400 /* If we can't make the auto-inc, or can't make the
2401 replacement into Y, exit. There's no point in making
2402 the change below if we can't do the auto-inc and doing
2403 so is not correct in the pre-inc case. */
2405 validate_change (insn, &XEXP (x, 0),
2406 gen_rtx_fmt_e (inc_code, Pmode, q),
2408 validate_change (incr, &XEXP (y, 0), q, 1);
2409 if (! apply_change_group ())
2410 return;
2412 /* We now know we'll be doing this change, so emit the
2413 new insn(s) and do the updates. */
2414 emit_insns_before (insns, insn);
2416 if (basic_block_head[BLOCK_NUM (insn)] == insn)
2417 basic_block_head[BLOCK_NUM (insn)] = insns;
2419 /* INCR will become a NOTE and INSN won't contain a
2420 use of ADDR. If a use of ADDR was just placed in
2421 the insn before INSN, make that the next use.
2422 Otherwise, invalidate it. */
2423 if (GET_CODE (PREV_INSN (insn)) == INSN
2424 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
2425 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
2426 reg_next_use[regno] = PREV_INSN (insn);
2427 else
2428 reg_next_use[regno] = 0;
2430 addr = q;
2431 regno = REGNO (q);
2433 /* REGNO is now used in INCR which is below INSN, but
2434 it previously wasn't live here. If we don't mark
2435 it as needed, we'll put a REG_DEAD note for it
2436 on this insn, which is incorrect. */
2437 SET_REGNO_REG_SET (needed, regno);
2439 /* If there are any calls between INSN and INCR, show
2440 that REGNO now crosses them. */
2441 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
2442 if (GET_CODE (temp) == CALL_INSN)
2443 REG_N_CALLS_CROSSED (regno)++;
2445 else
2446 return;
2448 /* If we haven't returned, it means we were able to make the
2449 auto-inc, so update the status. First, record that this insn
2450 has an implicit side effect. */
2452 REG_NOTES (insn)
2453 = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
2455 /* Modify the old increment-insn to simply copy
2456 the already-incremented value of our register. */
2457 if (! validate_change (incr, &SET_SRC (set), addr, 0))
2458 abort ();
2460 /* If that makes it a no-op (copying the register into itself) delete
2461 it so it won't appear to be a "use" and a "set" of this
2462 register. */
2463 if (SET_DEST (set) == addr)
2465 PUT_CODE (incr, NOTE);
2466 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
2467 NOTE_SOURCE_FILE (incr) = 0;
2470 if (regno >= FIRST_PSEUDO_REGISTER)
2472 /* Count an extra reference to the reg. When a reg is
2473 incremented, spilling it is worse, so we want to make
2474 that less likely. */
2475 REG_N_REFS (regno) += loop_depth;
2477 /* Count the increment as a setting of the register,
2478 even though it isn't a SET in rtl. */
2479 REG_N_SETS (regno)++;
2484 #endif /* AUTO_INC_DEC */
2486 /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
2487 This is done assuming the registers needed from X
2488 are those that have 1-bits in NEEDED.
2490 On the final pass, FINAL is 1. This means try for autoincrement
2491 and count the uses and deaths of each pseudo-reg.
2493 INSN is the containing instruction. If INSN is dead, this function is not
2494 called. */
2496 static void
2497 mark_used_regs (needed, live, x, final, insn)
2498 regset needed;
2499 regset live;
2500 rtx x;
2501 int final;
2502 rtx insn;
2504 register RTX_CODE code;
2505 register int regno;
2506 int i;
2508 retry:
2509 code = GET_CODE (x);
2510 switch (code)
2512 case LABEL_REF:
2513 case SYMBOL_REF:
2514 case CONST_INT:
2515 case CONST:
2516 case CONST_DOUBLE:
2517 case PC:
2518 case ADDR_VEC:
2519 case ADDR_DIFF_VEC:
2520 case ASM_INPUT:
2521 return;
2523 #ifdef HAVE_cc0
2524 case CC0:
2525 cc0_live = 1;
2526 return;
2527 #endif
2529 case CLOBBER:
2530 /* If we are clobbering a MEM, mark any registers inside the address
2531 as being used. */
2532 if (GET_CODE (XEXP (x, 0)) == MEM)
2533 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
2534 return;
2536 case MEM:
2537 /* Invalidate the data for the last MEM stored, but only if MEM is
2538 something that can be stored into. */
2539 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2540 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
2541 ; /* needn't clear last_mem_set */
2542 else
2543 last_mem_set = 0;
2545 #ifdef AUTO_INC_DEC
2546 if (final)
2547 find_auto_inc (needed, x, insn);
2548 #endif
2549 break;
2551 case SUBREG:
2552 if (GET_CODE (SUBREG_REG (x)) == REG
2553 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
2554 && (GET_MODE_SIZE (GET_MODE (x))
2555 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
2556 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
2558 /* While we're here, optimize this case. */
2559 x = SUBREG_REG (x);
2561 /* In case the SUBREG is not of a register, don't optimize */
2562 if (GET_CODE (x) != REG)
2564 mark_used_regs (needed, live, x, final, insn);
2565 return;
2568 /* ... fall through ... */
2570 case REG:
2571 /* See a register other than being set
2572 => mark it as needed. */
2574 regno = REGNO (x);
2576 int some_needed = REGNO_REG_SET_P (needed, regno);
2577 int some_not_needed = ! some_needed;
2579 SET_REGNO_REG_SET (live, regno);
2581 /* A hard reg in a wide mode may really be multiple registers.
2582 If so, mark all of them just like the first. */
2583 if (regno < FIRST_PSEUDO_REGISTER)
2585 int n;
2587 /* For stack ptr or fixed arg pointer,
2588 nothing below can be necessary, so waste no more time. */
2589 if (regno == STACK_POINTER_REGNUM
2590 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2591 || regno == HARD_FRAME_POINTER_REGNUM
2592 #endif
2593 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2594 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2595 #endif
2596 || regno == FRAME_POINTER_REGNUM)
2598 /* If this is a register we are going to try to eliminate,
2599 don't mark it live here. If we are successful in
2600 eliminating it, it need not be live unless it is used for
2601 pseudos, in which case it will have been set live when
2602 it was allocated to the pseudos. If the register will not
2603 be eliminated, reload will set it live at that point. */
2605 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
2606 regs_ever_live[regno] = 1;
2607 return;
2609 /* No death notes for global register variables;
2610 their values are live after this function exits. */
2611 if (global_regs[regno])
2613 if (final)
2614 reg_next_use[regno] = insn;
2615 return;
2618 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2619 while (--n > 0)
2621 int regno_n = regno + n;
2622 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2624 SET_REGNO_REG_SET (live, regno_n);
2625 some_needed |= needed_regno;
2626 some_not_needed |= ! needed_regno;
2629 if (final)
2631 /* Record where each reg is used, so when the reg
2632 is set we know the next insn that uses it. */
2634 reg_next_use[regno] = insn;
2636 if (regno < FIRST_PSEUDO_REGISTER)
2638 /* If a hard reg is being used,
2639 record that this function does use it. */
2641 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
2642 if (i == 0)
2643 i = 1;
2645 regs_ever_live[regno + --i] = 1;
2646 while (i > 0);
2648 else
2650 /* Keep track of which basic block each reg appears in. */
2652 register int blocknum = BLOCK_NUM (insn);
2654 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2655 REG_BASIC_BLOCK (regno) = blocknum;
2656 else if (REG_BASIC_BLOCK (regno) != blocknum)
2657 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2659 /* Count (weighted) number of uses of each reg. */
2661 REG_N_REFS (regno) += loop_depth;
2664 /* Record and count the insns in which a reg dies.
2665 If it is used in this insn and was dead below the insn
2666 then it dies in this insn. If it was set in this insn,
2667 we do not make a REG_DEAD note; likewise if we already
2668 made such a note. */
2670 if (some_not_needed
2671 && ! dead_or_set_p (insn, x)
2672 #if 0
2673 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
2674 #endif
2677 /* Check for the case where the register dying partially
2678 overlaps the register set by this insn. */
2679 if (regno < FIRST_PSEUDO_REGISTER
2680 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
2682 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2683 while (--n >= 0)
2684 some_needed |= dead_or_set_regno_p (insn, regno + n);
2687 /* If none of the words in X is needed, make a REG_DEAD
2688 note. Otherwise, we must make partial REG_DEAD notes. */
2689 if (! some_needed)
2691 REG_NOTES (insn)
2692 = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn));
2693 REG_N_DEATHS (regno)++;
2695 else
2697 int i;
2699 /* Don't make a REG_DEAD note for a part of a register
2700 that is set in the insn. */
2702 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
2703 i >= 0; i--)
2704 if (!REGNO_REG_SET_P (needed, regno + i)
2705 && ! dead_or_set_regno_p (insn, regno + i))
2706 REG_NOTES (insn)
2707 = gen_rtx_EXPR_LIST (REG_DEAD,
2708 gen_rtx_REG (reg_raw_mode[regno + i],
2709 regno + i),
2710 REG_NOTES (insn));
2715 return;
2717 case SET:
2719 register rtx testreg = SET_DEST (x);
2720 int mark_dest = 0;
2722 /* If storing into MEM, don't show it as being used. But do
2723 show the address as being used. */
2724 if (GET_CODE (testreg) == MEM)
2726 #ifdef AUTO_INC_DEC
2727 if (final)
2728 find_auto_inc (needed, testreg, insn);
2729 #endif
2730 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
2731 mark_used_regs (needed, live, SET_SRC (x), final, insn);
2732 return;
2735 /* Storing in STRICT_LOW_PART is like storing in a reg
2736 in that this SET might be dead, so ignore it in TESTREG.
2737 but in some other ways it is like using the reg.
2739 Storing in a SUBREG or a bit field is like storing the entire
2740 register in that if the register's value is not used
2741 then this SET is not needed. */
2742 while (GET_CODE (testreg) == STRICT_LOW_PART
2743 || GET_CODE (testreg) == ZERO_EXTRACT
2744 || GET_CODE (testreg) == SIGN_EXTRACT
2745 || GET_CODE (testreg) == SUBREG)
2747 if (GET_CODE (testreg) == SUBREG
2748 && GET_CODE (SUBREG_REG (testreg)) == REG
2749 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
2750 && (GET_MODE_SIZE (GET_MODE (testreg))
2751 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
2752 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
2754 /* Modifying a single register in an alternate mode
2755 does not use any of the old value. But these other
2756 ways of storing in a register do use the old value. */
2757 if (GET_CODE (testreg) == SUBREG
2758 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
2760 else
2761 mark_dest = 1;
2763 testreg = XEXP (testreg, 0);
2766 /* If this is a store into a register,
2767 recursively scan the value being stored. */
2769 if (GET_CODE (testreg) == REG
2770 && (regno = REGNO (testreg), regno != FRAME_POINTER_REGNUM)
2771 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2772 && regno != HARD_FRAME_POINTER_REGNUM
2773 #endif
2774 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2775 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2776 #endif
2778 /* We used to exclude global_regs here, but that seems wrong.
2779 Storing in them is like storing in mem. */
2781 mark_used_regs (needed, live, SET_SRC (x), final, insn);
2782 if (mark_dest)
2783 mark_used_regs (needed, live, SET_DEST (x), final, insn);
2784 return;
2787 break;
2789 case RETURN:
2790 /* If exiting needs the right stack value, consider this insn as
2791 using the stack pointer. In any event, consider it as using
2792 all global registers and all registers used by return. */
2794 #ifdef EXIT_IGNORE_STACK
2795 if (! EXIT_IGNORE_STACK
2796 || (! FRAME_POINTER_REQUIRED
2797 && ! current_function_calls_alloca
2798 && flag_omit_frame_pointer))
2799 #endif
2800 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
2802 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2803 if (global_regs[i]
2804 #ifdef EPILOGUE_USES
2805 || EPILOGUE_USES (i)
2806 #endif
2808 SET_REGNO_REG_SET (live, i);
2809 break;
2811 default:
2812 break;
2815 /* Recursively scan the operands of this expression. */
2818 register char *fmt = GET_RTX_FORMAT (code);
2819 register int i;
2821 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2823 if (fmt[i] == 'e')
2825 /* Tail recursive case: save a function call level. */
2826 if (i == 0)
2828 x = XEXP (x, 0);
2829 goto retry;
2831 mark_used_regs (needed, live, XEXP (x, i), final, insn);
2833 else if (fmt[i] == 'E')
2835 register int j;
2836 for (j = 0; j < XVECLEN (x, i); j++)
2837 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
2843 #ifdef AUTO_INC_DEC
2845 static int
2846 try_pre_increment_1 (insn)
2847 rtx insn;
2849 /* Find the next use of this reg. If in same basic block,
2850 make it do pre-increment or pre-decrement if appropriate. */
2851 rtx x = single_set (insn);
2852 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
2853 * INTVAL (XEXP (SET_SRC (x), 1)));
2854 int regno = REGNO (SET_DEST (x));
2855 rtx y = reg_next_use[regno];
2856 if (y != 0
2857 && BLOCK_NUM (y) == BLOCK_NUM (insn)
2858 /* Don't do this if the reg dies, or gets set in y; a standard addressing
2859 mode would be better. */
2860 && ! dead_or_set_p (y, SET_DEST (x))
2861 && try_pre_increment (y, SET_DEST (x), amount))
2863 /* We have found a suitable auto-increment
2864 and already changed insn Y to do it.
2865 So flush this increment-instruction. */
2866 PUT_CODE (insn, NOTE);
2867 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2868 NOTE_SOURCE_FILE (insn) = 0;
2869 /* Count a reference to this reg for the increment
2870 insn we are deleting. When a reg is incremented.
2871 spilling it is worse, so we want to make that
2872 less likely. */
2873 if (regno >= FIRST_PSEUDO_REGISTER)
2875 REG_N_REFS (regno) += loop_depth;
2876 REG_N_SETS (regno)++;
2878 return 1;
2880 return 0;
2883 /* Try to change INSN so that it does pre-increment or pre-decrement
2884 addressing on register REG in order to add AMOUNT to REG.
2885 AMOUNT is negative for pre-decrement.
2886 Returns 1 if the change could be made.
2887 This checks all about the validity of the result of modifying INSN. */
2889 static int
2890 try_pre_increment (insn, reg, amount)
2891 rtx insn, reg;
2892 HOST_WIDE_INT amount;
2894 register rtx use;
2896 /* Nonzero if we can try to make a pre-increment or pre-decrement.
2897 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
2898 int pre_ok = 0;
2899 /* Nonzero if we can try to make a post-increment or post-decrement.
2900 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
2901 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
2902 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
2903 int post_ok = 0;
2905 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
2906 int do_post = 0;
2908 /* From the sign of increment, see which possibilities are conceivable
2909 on this target machine. */
2910 #ifdef HAVE_PRE_INCREMENT
2911 if (amount > 0)
2912 pre_ok = 1;
2913 #endif
2914 #ifdef HAVE_POST_INCREMENT
2915 if (amount > 0)
2916 post_ok = 1;
2917 #endif
2919 #ifdef HAVE_PRE_DECREMENT
2920 if (amount < 0)
2921 pre_ok = 1;
2922 #endif
2923 #ifdef HAVE_POST_DECREMENT
2924 if (amount < 0)
2925 post_ok = 1;
2926 #endif
2928 if (! (pre_ok || post_ok))
2929 return 0;
2931 /* It is not safe to add a side effect to a jump insn
2932 because if the incremented register is spilled and must be reloaded
2933 there would be no way to store the incremented value back in memory. */
2935 if (GET_CODE (insn) == JUMP_INSN)
2936 return 0;
2938 use = 0;
2939 if (pre_ok)
2940 use = find_use_as_address (PATTERN (insn), reg, 0);
2941 if (post_ok && (use == 0 || use == (rtx) 1))
2943 use = find_use_as_address (PATTERN (insn), reg, -amount);
2944 do_post = 1;
2947 if (use == 0 || use == (rtx) 1)
2948 return 0;
2950 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
2951 return 0;
2953 /* See if this combination of instruction and addressing mode exists. */
2954 if (! validate_change (insn, &XEXP (use, 0),
2955 gen_rtx_fmt_e (amount > 0
2956 ? (do_post ? POST_INC : PRE_INC)
2957 : (do_post ? POST_DEC : PRE_DEC),
2958 Pmode, reg), 0))
2959 return 0;
2961 /* Record that this insn now has an implicit side effect on X. */
2962 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
2963 return 1;
2966 #endif /* AUTO_INC_DEC */
2968 /* Find the place in the rtx X where REG is used as a memory address.
2969 Return the MEM rtx that so uses it.
2970 If PLUSCONST is nonzero, search instead for a memory address equivalent to
2971 (plus REG (const_int PLUSCONST)).
2973 If such an address does not appear, return 0.
2974 If REG appears more than once, or is used other than in such an address,
2975 return (rtx)1. */
2978 find_use_as_address (x, reg, plusconst)
2979 register rtx x;
2980 rtx reg;
2981 HOST_WIDE_INT plusconst;
2983 enum rtx_code code = GET_CODE (x);
2984 char *fmt = GET_RTX_FORMAT (code);
2985 register int i;
2986 register rtx value = 0;
2987 register rtx tem;
2989 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
2990 return x;
2992 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
2993 && XEXP (XEXP (x, 0), 0) == reg
2994 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2995 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
2996 return x;
2998 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
3000 /* If REG occurs inside a MEM used in a bit-field reference,
3001 that is unacceptable. */
3002 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
3003 return (rtx) (HOST_WIDE_INT) 1;
3006 if (x == reg)
3007 return (rtx) (HOST_WIDE_INT) 1;
3009 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3011 if (fmt[i] == 'e')
3013 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
3014 if (value == 0)
3015 value = tem;
3016 else if (tem != 0)
3017 return (rtx) (HOST_WIDE_INT) 1;
3019 if (fmt[i] == 'E')
3021 register int j;
3022 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3024 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
3025 if (value == 0)
3026 value = tem;
3027 else if (tem != 0)
3028 return (rtx) (HOST_WIDE_INT) 1;
3033 return value;
3036 /* Write information about registers and basic blocks into FILE.
3037 This is part of making a debugging dump. */
3039 void
3040 dump_flow_info (file)
3041 FILE *file;
3043 register int i;
3044 static char *reg_class_names[] = REG_CLASS_NAMES;
3046 fprintf (file, "%d registers.\n", max_regno);
3048 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3049 if (REG_N_REFS (i))
3051 enum reg_class class, altclass;
3052 fprintf (file, "\nRegister %d used %d times across %d insns",
3053 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
3054 if (REG_BASIC_BLOCK (i) >= 0)
3055 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
3056 if (REG_N_SETS (i))
3057 fprintf (file, "; set %d time%s", REG_N_SETS (i),
3058 (REG_N_SETS (i) == 1) ? "" : "s");
3059 if (REG_USERVAR_P (regno_reg_rtx[i]))
3060 fprintf (file, "; user var");
3061 if (REG_N_DEATHS (i) != 1)
3062 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
3063 if (REG_N_CALLS_CROSSED (i) == 1)
3064 fprintf (file, "; crosses 1 call");
3065 else if (REG_N_CALLS_CROSSED (i))
3066 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
3067 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
3068 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
3069 class = reg_preferred_class (i);
3070 altclass = reg_alternate_class (i);
3071 if (class != GENERAL_REGS || altclass != ALL_REGS)
3073 if (altclass == ALL_REGS || class == ALL_REGS)
3074 fprintf (file, "; pref %s", reg_class_names[(int) class]);
3075 else if (altclass == NO_REGS)
3076 fprintf (file, "; %s or none", reg_class_names[(int) class]);
3077 else
3078 fprintf (file, "; pref %s, else %s",
3079 reg_class_names[(int) class],
3080 reg_class_names[(int) altclass]);
3082 if (REGNO_POINTER_FLAG (i))
3083 fprintf (file, "; pointer");
3084 fprintf (file, ".\n");
3086 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
3087 for (i = 0; i < n_basic_blocks; i++)
3089 register rtx head, jump;
3090 register int regno;
3091 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
3093 INSN_UID (basic_block_head[i]),
3094 INSN_UID (basic_block_end[i]));
3095 /* The control flow graph's storage is freed
3096 now when flow_analysis returns.
3097 Don't try to print it if it is gone. */
3098 if (basic_block_drops_in)
3100 fprintf (file, "Reached from blocks: ");
3101 head = basic_block_head[i];
3102 if (GET_CODE (head) == CODE_LABEL)
3103 for (jump = LABEL_REFS (head);
3104 jump != head;
3105 jump = LABEL_NEXTREF (jump))
3107 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
3108 fprintf (file, " %d", from_block);
3110 if (basic_block_drops_in[i])
3111 fprintf (file, " previous");
3113 fprintf (file, "\nRegisters live at start:");
3114 for (regno = 0; regno < max_regno; regno++)
3115 if (REGNO_REG_SET_P (basic_block_live_at_start[i], regno))
3116 fprintf (file, " %d", regno);
3117 fprintf (file, "\n");
3119 fprintf (file, "\n");
3123 /* Like print_rtl, but also print out live information for the start of each
3124 basic block. */
3126 void
3127 print_rtl_with_bb (outf, rtx_first)
3128 FILE *outf;
3129 rtx rtx_first;
3131 register rtx tmp_rtx;
3133 if (rtx_first == 0)
3134 fprintf (outf, "(nil)\n");
3136 else
3138 int i, bb;
3139 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
3140 int max_uid = get_max_uid ();
3141 int *start = (int *) alloca (max_uid * sizeof (int));
3142 int *end = (int *) alloca (max_uid * sizeof (int));
3143 char *in_bb_p = (char *) alloca (max_uid * sizeof (enum bb_state));
3145 for (i = 0; i < max_uid; i++)
3147 start[i] = end[i] = -1;
3148 in_bb_p[i] = NOT_IN_BB;
3151 for (i = n_basic_blocks-1; i >= 0; i--)
3153 rtx x;
3154 start[INSN_UID (basic_block_head[i])] = i;
3155 end[INSN_UID (basic_block_end[i])] = i;
3156 for (x = basic_block_head[i]; x != NULL_RTX; x = NEXT_INSN (x))
3158 in_bb_p[ INSN_UID(x)]
3159 = (in_bb_p[ INSN_UID(x)] == NOT_IN_BB)
3160 ? IN_ONE_BB : IN_MULTIPLE_BB;
3161 if (x == basic_block_end[i])
3162 break;
3166 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
3168 if ((bb = start[INSN_UID (tmp_rtx)]) >= 0)
3170 fprintf (outf, ";; Start of basic block %d, registers live:",
3171 bb);
3173 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
3175 fprintf (outf, " %d", i);
3176 if (i < FIRST_PSEUDO_REGISTER)
3177 fprintf (outf, " [%s]",
3178 reg_names[i]);
3180 putc ('\n', outf);
3183 if (in_bb_p[ INSN_UID(tmp_rtx)] == NOT_IN_BB
3184 && GET_CODE (tmp_rtx) != NOTE
3185 && GET_CODE (tmp_rtx) != BARRIER)
3186 fprintf (outf, ";; Insn is not within a basic block\n");
3187 else if (in_bb_p[ INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
3188 fprintf (outf, ";; Insn is in multiple basic blocks\n");
3190 print_rtl_single (outf, tmp_rtx);
3192 if ((bb = end[INSN_UID (tmp_rtx)]) >= 0)
3193 fprintf (outf, ";; End of basic block %d\n", bb);
3195 putc ('\n', outf);
3201 /* Integer list support. */
3203 /* Allocate a node from list *HEAD_PTR. */
3205 static int_list_ptr
3206 alloc_int_list_node (head_ptr)
3207 int_list_block **head_ptr;
3209 struct int_list_block *first_blk = *head_ptr;
3211 if (first_blk == NULL || first_blk->nodes_left <= 0)
3213 first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block));
3214 first_blk->nodes_left = INT_LIST_NODES_IN_BLK;
3215 first_blk->next = *head_ptr;
3216 *head_ptr = first_blk;
3219 first_blk->nodes_left--;
3220 return &first_blk->nodes[first_blk->nodes_left];
3223 /* Pointer to head of predecessor/successor block list. */
3224 static int_list_block *pred_int_list_blocks;
3226 /* Add a new node to integer list LIST with value VAL.
3227 LIST is a pointer to a list object to allow for different implementations.
3228 If *LIST is initially NULL, the list is empty.
3229 The caller must not care whether the element is added to the front or
3230 to the end of the list (to allow for different implementations). */
3232 static int_list_ptr
3233 add_int_list_node (blk_list, list, val)
3234 int_list_block **blk_list;
3235 int_list **list;
3236 int val;
3238 int_list_ptr p = alloc_int_list_node (blk_list);
3240 p->val = val;
3241 p->next = *list;
3242 *list = p;
3243 return p;
3246 /* Free the blocks of lists at BLK_LIST. */
3248 void
3249 free_int_list (blk_list)
3250 int_list_block **blk_list;
3252 int_list_block *p, *next;
3254 for (p = *blk_list; p != NULL; p = next)
3256 next = p->next;
3257 free (p);
3260 /* Mark list as empty for the next function we compile. */
3261 *blk_list = NULL;
3264 /* Predecessor/successor computation. */
3266 /* Mark PRED_BB a precessor of SUCC_BB,
3267 and conversely SUCC_BB a successor of PRED_BB. */
3269 static void
3270 add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs)
3271 int pred_bb;
3272 int succ_bb;
3273 int_list_ptr *s_preds;
3274 int_list_ptr *s_succs;
3275 int *num_preds;
3276 int *num_succs;
3278 if (succ_bb != EXIT_BLOCK)
3280 add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb);
3281 num_preds[succ_bb]++;
3283 if (pred_bb != ENTRY_BLOCK)
3285 add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb);
3286 num_succs[pred_bb]++;
3290 /* Compute the predecessors and successors for each block. */
3291 void
3292 compute_preds_succs (s_preds, s_succs, num_preds, num_succs)
3293 int_list_ptr *s_preds;
3294 int_list_ptr *s_succs;
3295 int *num_preds;
3296 int *num_succs;
3298 int bb, clear_local_bb_vars = 0;
3300 bzero ((char *) s_preds, n_basic_blocks * sizeof (int_list_ptr));
3301 bzero ((char *) s_succs, n_basic_blocks * sizeof (int_list_ptr));
3302 bzero ((char *) num_preds, n_basic_blocks * sizeof (int));
3303 bzero ((char *) num_succs, n_basic_blocks * sizeof (int));
3305 /* This routine can be called after life analysis; in that case
3306 basic_block_drops_in and uid_block_number will not be available
3307 and we must recompute their values. */
3308 if (basic_block_drops_in == NULL || uid_block_number == NULL)
3310 clear_local_bb_vars = 1;
3311 basic_block_drops_in = (char *) alloca (n_basic_blocks);
3312 uid_block_number = (int *) alloca ((get_max_uid () + 1) * sizeof (int));
3314 bzero ((char *) basic_block_drops_in, n_basic_blocks * sizeof (char));
3315 bzero ((char *) uid_block_number, n_basic_blocks * sizeof (int));
3317 /* Scan each basic block setting basic_block_drops_in and
3318 uid_block_number as needed. */
3319 for (bb = 0; bb < n_basic_blocks; bb++)
3321 rtx insn, stop_insn;
3323 if (bb == 0)
3324 stop_insn = NULL_RTX;
3325 else
3326 stop_insn = basic_block_end[bb-1];
3328 /* Look backwards from the start of this block. Stop if we
3329 hit the start of the function or the end of a previous
3330 block. Don't walk backwards through blocks that are just
3331 deleted insns! */
3332 for (insn = PREV_INSN (basic_block_head[bb]);
3333 insn && insn != stop_insn && GET_CODE (insn) == NOTE;
3334 insn = PREV_INSN (insn))
3337 /* Never set basic_block_drops_in for the first block. It is
3338 implicit.
3340 If we stopped on anything other than a BARRIER, then this
3341 block drops in. */
3342 if (bb != 0)
3343 basic_block_drops_in[bb] = (insn ? GET_CODE (insn) != BARRIER : 1);
3345 insn = basic_block_head[bb];
3346 while (insn)
3348 BLOCK_NUM (insn) = bb;
3349 if (insn == basic_block_end[bb])
3350 break;
3351 insn = NEXT_INSN (insn);
3356 for (bb = 0; bb < n_basic_blocks; bb++)
3358 rtx head;
3359 rtx jump;
3361 head = BLOCK_HEAD (bb);
3363 if (GET_CODE (head) == CODE_LABEL)
3364 for (jump = LABEL_REFS (head);
3365 jump != head;
3366 jump = LABEL_NEXTREF (jump))
3368 if (! INSN_DELETED_P (CONTAINING_INSN (jump))
3369 && (GET_CODE (CONTAINING_INSN (jump)) != NOTE
3370 || (NOTE_LINE_NUMBER (CONTAINING_INSN (jump))
3371 != NOTE_INSN_DELETED)))
3372 add_pred_succ (BLOCK_NUM (CONTAINING_INSN (jump)), bb,
3373 s_preds, s_succs, num_preds, num_succs);
3376 jump = BLOCK_END (bb);
3377 /* If this is a RETURN insn or a conditional jump in the last
3378 basic block, or a non-jump insn in the last basic block, then
3379 this block reaches the exit block. */
3380 if ((GET_CODE (jump) == JUMP_INSN && GET_CODE (PATTERN (jump)) == RETURN)
3381 || (((GET_CODE (jump) == JUMP_INSN
3382 && condjump_p (jump) && !simplejump_p (jump))
3383 || GET_CODE (jump) != JUMP_INSN)
3384 && (bb == n_basic_blocks - 1)))
3385 add_pred_succ (bb, EXIT_BLOCK, s_preds, s_succs, num_preds, num_succs);
3387 if (basic_block_drops_in[bb])
3388 add_pred_succ (bb - 1, bb, s_preds, s_succs, num_preds, num_succs);
3391 add_pred_succ (ENTRY_BLOCK, 0, s_preds, s_succs, num_preds, num_succs);
3394 /* If we allocated any variables in temporary storage, clear out the
3395 pointer to the local storage to avoid dangling pointers. */
3396 if (clear_local_bb_vars)
3398 basic_block_drops_in = NULL;
3399 uid_block_number = NULL;
3404 void
3405 dump_bb_data (file, preds, succs)
3406 FILE *file;
3407 int_list_ptr *preds;
3408 int_list_ptr *succs;
3410 int bb;
3411 int_list_ptr p;
3413 fprintf (file, "BB data\n\n");
3414 for (bb = 0; bb < n_basic_blocks; bb++)
3416 fprintf (file, "BB %d, start %d, end %d\n", bb,
3417 INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb)));
3418 fprintf (file, " preds:");
3419 for (p = preds[bb]; p != NULL; p = p->next)
3421 int pred_bb = INT_LIST_VAL (p);
3422 if (pred_bb == ENTRY_BLOCK)
3423 fprintf (file, " entry");
3424 else
3425 fprintf (file, " %d", pred_bb);
3427 fprintf (file, "\n");
3428 fprintf (file, " succs:");
3429 for (p = succs[bb]; p != NULL; p = p->next)
3431 int succ_bb = INT_LIST_VAL (p);
3432 if (succ_bb == EXIT_BLOCK)
3433 fprintf (file, " exit");
3434 else
3435 fprintf (file, " %d", succ_bb);
3437 fprintf (file, "\n");
3439 fprintf (file, "\n");
3442 void
3443 dump_sbitmap (file, bmap)
3444 FILE *file;
3445 sbitmap bmap;
3447 int i,j,n;
3448 int set_size = bmap->size;
3449 int total_bits = bmap->n_bits;
3451 fprintf (file, " ");
3452 for (i = n = 0; i < set_size && n < total_bits; i++)
3454 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
3456 if (n != 0 && n % 10 == 0)
3457 fprintf (file, " ");
3458 fprintf (file, "%d", (bmap->elms[i] & (1L << j)) != 0);
3461 fprintf (file, "\n");
3464 void
3465 dump_sbitmap_vector (file, title, subtitle, bmaps, n_maps)
3466 FILE *file;
3467 char *title, *subtitle;
3468 sbitmap *bmaps;
3469 int n_maps;
3471 int bb;
3473 fprintf (file, "%s\n", title);
3474 for (bb = 0; bb < n_maps; bb++)
3476 fprintf (file, "%s %d\n", subtitle, bb);
3477 dump_sbitmap (file, bmaps[bb]);
3479 fprintf (file, "\n");
3482 /* Free basic block data storage. */
3484 void
3485 free_bb_mem ()
3487 free_int_list (&pred_int_list_blocks);
3490 /* Bitmap manipulation routines. */
3492 /* Allocate a simple bitmap of N_ELMS bits. */
3494 sbitmap
3495 sbitmap_alloc (n_elms)
3496 int n_elms;
3498 int bytes, size, amt;
3499 sbitmap bmap;
3501 size = SBITMAP_SET_SIZE (n_elms);
3502 bytes = size * sizeof (SBITMAP_ELT_TYPE);
3503 amt = (sizeof (struct simple_bitmap_def)
3504 + bytes - sizeof (SBITMAP_ELT_TYPE));
3505 bmap = (sbitmap) xmalloc (amt);
3506 bmap->n_bits = n_elms;
3507 bmap->size = size;
3508 bmap->bytes = bytes;
3509 return bmap;
3512 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
3514 sbitmap *
3515 sbitmap_vector_alloc (n_vecs, n_elms)
3516 int n_vecs, n_elms;
3518 int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
3519 sbitmap *bitmap_vector;
3521 size = SBITMAP_SET_SIZE (n_elms);
3522 bytes = size * sizeof (SBITMAP_ELT_TYPE);
3523 elm_bytes = (sizeof (struct simple_bitmap_def)
3524 + bytes - sizeof (SBITMAP_ELT_TYPE));
3525 vector_bytes = n_vecs * sizeof (sbitmap *);
3527 /* Round up `vector_bytes' to account for the alignment requirements
3528 of an sbitmap. One could allocate the vector-table and set of sbitmaps
3529 separately, but that requires maintaining two pointers or creating
3530 a cover struct to hold both pointers (so our result is still just
3531 one pointer). Neither is a bad idea, but this is simpler for now. */
3533 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
3534 struct { char x; SBITMAP_ELT_TYPE y; } align;
3535 int alignment = (char *) & align.y - & align.x;
3536 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
3539 amt = vector_bytes + (n_vecs * elm_bytes);
3540 bitmap_vector = (sbitmap *) xmalloc (amt);
3542 for (i = 0, offset = vector_bytes;
3543 i < n_vecs;
3544 i++, offset += elm_bytes)
3546 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
3547 bitmap_vector[i] = b;
3548 b->n_bits = n_elms;
3549 b->size = size;
3550 b->bytes = bytes;
3553 return bitmap_vector;
3556 /* Copy sbitmap SRC to DST. */
3558 void
3559 sbitmap_copy (dst, src)
3560 sbitmap dst, src;
3562 int i;
3563 sbitmap_ptr d,s;
3565 s = src->elms;
3566 d = dst->elms;
3567 for (i = 0; i < dst->size; i++)
3568 *d++ = *s++;
3571 /* Zero all elements in a bitmap. */
3573 void
3574 sbitmap_zero (bmap)
3575 sbitmap bmap;
3577 bzero ((char *) bmap->elms, bmap->bytes);
3580 /* Set to ones all elements in a bitmap. */
3582 void
3583 sbitmap_ones (bmap)
3584 sbitmap bmap;
3586 memset (bmap->elms, -1, bmap->bytes);
3589 /* Zero a vector of N_VECS bitmaps. */
3591 void
3592 sbitmap_vector_zero (bmap, n_vecs)
3593 sbitmap *bmap;
3594 int n_vecs;
3596 int i;
3598 for (i = 0; i < n_vecs; i++)
3599 sbitmap_zero (bmap[i]);
3602 /* Set to ones a vector of N_VECS bitmaps. */
3604 void
3605 sbitmap_vector_ones (bmap, n_vecs)
3606 sbitmap *bmap;
3607 int n_vecs;
3609 int i;
3611 for (i = 0; i < n_vecs; i++)
3612 sbitmap_ones (bmap[i]);
3615 /* Set DST to be A union (B - C).
3616 DST = A | (B & ~C).
3617 Return non-zero if any change is made. */
3620 sbitmap_union_of_diff (dst, a, b, c)
3621 sbitmap dst, a, b, c;
3623 int i,changed;
3624 sbitmap_ptr dstp, ap, bp, cp;
3626 changed = 0;
3627 dstp = dst->elms;
3628 ap = a->elms;
3629 bp = b->elms;
3630 cp = c->elms;
3631 for (i = 0; i < dst->size; i++)
3633 SBITMAP_ELT_TYPE tmp = *ap | (*bp & ~*cp);
3634 if (*dstp != tmp)
3635 changed = 1;
3636 *dstp = tmp;
3637 dstp++; ap++; bp++; cp++;
3639 return changed;
3642 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
3644 void
3645 sbitmap_not (dst, src)
3646 sbitmap dst, src;
3648 int i;
3649 sbitmap_ptr dstp, ap;
3651 dstp = dst->elms;
3652 ap = src->elms;
3653 for (i = 0; i < dst->size; i++)
3655 SBITMAP_ELT_TYPE tmp = ~(*ap);
3656 *dstp = tmp;
3657 dstp++; ap++;
3661 /* Set the bits in DST to be the difference between the bits
3662 in A and the bits in B. i.e. dst = a - b.
3663 The - operator is implemented as a & (~b). */
3665 void
3666 sbitmap_difference (dst, a, b)
3667 sbitmap dst, a, b;
3669 int i;
3670 sbitmap_ptr dstp, ap, bp;
3672 dstp = dst->elms;
3673 ap = a->elms;
3674 bp = b->elms;
3675 for (i = 0; i < dst->size; i++)
3676 *dstp++ = *ap++ & (~*bp++);
3679 /* Set DST to be (A and B)).
3680 Return non-zero if any change is made. */
3683 sbitmap_a_and_b (dst, a, b)
3684 sbitmap dst, a, b;
3686 int i,changed;
3687 sbitmap_ptr dstp, ap, bp;
3689 changed = 0;
3690 dstp = dst->elms;
3691 ap = a->elms;
3692 bp = b->elms;
3693 for (i = 0; i < dst->size; i++)
3695 SBITMAP_ELT_TYPE tmp = *ap & *bp;
3696 if (*dstp != tmp)
3697 changed = 1;
3698 *dstp = tmp;
3699 dstp++; ap++; bp++;
3701 return changed;
3703 /* Set DST to be (A or B)).
3704 Return non-zero if any change is made. */
3707 sbitmap_a_or_b (dst, a, b)
3708 sbitmap dst, a, b;
3710 int i,changed;
3711 sbitmap_ptr dstp, ap, bp;
3713 changed = 0;
3714 dstp = dst->elms;
3715 ap = a->elms;
3716 bp = b->elms;
3717 for (i = 0; i < dst->size; i++)
3719 SBITMAP_ELT_TYPE tmp = *ap | *bp;
3720 if (*dstp != tmp)
3721 changed = 1;
3722 *dstp = tmp;
3723 dstp++; ap++; bp++;
3725 return changed;
3728 /* Set DST to be (A or (B and C)).
3729 Return non-zero if any change is made. */
3732 sbitmap_a_or_b_and_c (dst, a, b, c)
3733 sbitmap dst, a, b, c;
3735 int i,changed;
3736 sbitmap_ptr dstp, ap, bp, cp;
3738 changed = 0;
3739 dstp = dst->elms;
3740 ap = a->elms;
3741 bp = b->elms;
3742 cp = c->elms;
3743 for (i = 0; i < dst->size; i++)
3745 SBITMAP_ELT_TYPE tmp = *ap | (*bp & *cp);
3746 if (*dstp != tmp)
3747 changed = 1;
3748 *dstp = tmp;
3749 dstp++; ap++; bp++; cp++;
3751 return changed;
3754 /* Set DST to be (A ann (B or C)).
3755 Return non-zero if any change is made. */
3758 sbitmap_a_and_b_or_c (dst, a, b, c)
3759 sbitmap dst, a, b, c;
3761 int i,changed;
3762 sbitmap_ptr dstp, ap, bp, cp;
3764 changed = 0;
3765 dstp = dst->elms;
3766 ap = a->elms;
3767 bp = b->elms;
3768 cp = c->elms;
3769 for (i = 0; i < dst->size; i++)
3771 SBITMAP_ELT_TYPE tmp = *ap & (*bp | *cp);
3772 if (*dstp != tmp)
3773 changed = 1;
3774 *dstp = tmp;
3775 dstp++; ap++; bp++; cp++;
3777 return changed;
3780 /* Set the bitmap DST to the intersection of SRC of all predecessors or
3781 successors of block number BB (PRED_SUCC says which). */
3783 void
3784 sbitmap_intersect_of_predsucc (dst, src, bb, pred_succ)
3785 sbitmap dst;
3786 sbitmap *src;
3787 int bb;
3788 int_list_ptr *pred_succ;
3790 int_list_ptr ps;
3791 int ps_bb;
3792 int set_size = dst->size;
3794 ps = pred_succ[bb];
3796 /* It is possible that there are no predecessors(/successors).
3797 This can happen for example in unreachable code. */
3799 if (ps == NULL)
3801 /* In APL-speak this is the `and' reduction of the empty set and thus
3802 the result is the identity for `and'. */
3803 sbitmap_ones (dst);
3804 return;
3807 /* Set result to first predecessor/successor. */
3809 for ( ; ps != NULL; ps = ps->next)
3811 ps_bb = INT_LIST_VAL (ps);
3812 if (ps_bb == ENTRY_BLOCK || ps_bb == EXIT_BLOCK)
3813 continue;
3814 sbitmap_copy (dst, src[ps_bb]);
3815 /* Break out since we're only doing first predecessor. */
3816 break;
3818 if (ps == NULL)
3819 return;
3821 /* Now do the remaining predecessors/successors. */
3823 for (ps = ps->next; ps != NULL; ps = ps->next)
3825 int i;
3826 sbitmap_ptr p,r;
3828 ps_bb = INT_LIST_VAL (ps);
3829 if (ps_bb == ENTRY_BLOCK || ps_bb == EXIT_BLOCK)
3830 continue;
3832 p = src[ps_bb]->elms;
3833 r = dst->elms;
3835 for (i = 0; i < set_size; i++)
3836 *r++ &= *p++;
3840 /* Set the bitmap DST to the intersection of SRC of all predecessors
3841 of block number BB. */
3843 void
3844 sbitmap_intersect_of_predecessors (dst, src, bb, s_preds)
3845 sbitmap dst;
3846 sbitmap *src;
3847 int bb;
3848 int_list_ptr *s_preds;
3850 sbitmap_intersect_of_predsucc (dst, src, bb, s_preds);
3853 /* Set the bitmap DST to the intersection of SRC of all successors
3854 of block number BB. */
3856 void
3857 sbitmap_intersect_of_successors (dst, src, bb, s_succs)
3858 sbitmap dst;
3859 sbitmap *src;
3860 int bb;
3861 int_list_ptr *s_succs;
3863 sbitmap_intersect_of_predsucc (dst, src, bb, s_succs);
3866 /* Set the bitmap DST to the union of SRC of all predecessors/successors of
3867 block number BB. */
3869 void
3870 sbitmap_union_of_predsucc (dst, src, bb, pred_succ)
3871 sbitmap dst;
3872 sbitmap *src;
3873 int bb;
3874 int_list_ptr *pred_succ;
3876 int_list_ptr ps;
3877 int ps_bb;
3878 int set_size = dst->size;
3880 ps = pred_succ[bb];
3882 /* It is possible that there are no predecessors(/successors).
3883 This can happen for example in unreachable code. */
3885 if (ps == NULL)
3887 /* In APL-speak this is the `or' reduction of the empty set and thus
3888 the result is the identity for `or'. */
3889 sbitmap_zero (dst);
3890 return;
3893 /* Set result to first predecessor/successor. */
3895 for ( ; ps != NULL; ps = ps->next)
3897 ps_bb = INT_LIST_VAL (ps);
3898 if (ps_bb == ENTRY_BLOCK || ps_bb == EXIT_BLOCK)
3899 continue;
3900 sbitmap_copy (dst, src[ps_bb]);
3901 /* Break out since we're only doing first predecessor. */
3902 break;
3904 if (ps == NULL)
3905 return;
3907 /* Now do the remaining predecessors/successors. */
3909 for (ps = ps->next; ps != NULL; ps = ps->next)
3911 int i;
3912 sbitmap_ptr p,r;
3914 ps_bb = INT_LIST_VAL (ps);
3915 if (ps_bb == ENTRY_BLOCK || ps_bb == EXIT_BLOCK)
3916 continue;
3918 p = src[ps_bb]->elms;
3919 r = dst->elms;
3921 for (i = 0; i < set_size; i++)
3922 *r++ |= *p++;
3926 /* Set the bitmap DST to the union of SRC of all predecessors of
3927 block number BB. */
3929 void
3930 sbitmap_union_of_predecessors (dst, src, bb, s_preds)
3931 sbitmap dst;
3932 sbitmap *src;
3933 int bb;
3934 int_list_ptr *s_preds;
3936 sbitmap_union_of_predsucc (dst, src, bb, s_preds);
3939 /* Set the bitmap DST to the union of SRC of all predecessors of
3940 block number BB. */
3942 void
3943 sbitmap_union_of_successors (dst, src, bb, s_succ)
3944 sbitmap dst;
3945 sbitmap *src;
3946 int bb;
3947 int_list_ptr *s_succ;
3949 sbitmap_union_of_predsucc (dst, src, bb, s_succ);
3952 /* Compute dominator relationships. */
3953 void
3954 compute_dominators (dominators, post_dominators, s_preds, s_succs)
3955 sbitmap *dominators;
3956 sbitmap *post_dominators;
3957 int_list_ptr *s_preds;
3958 int_list_ptr *s_succs;
3960 int bb, changed, passes;
3961 sbitmap *temp_bitmap;
3963 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
3964 sbitmap_vector_ones (dominators, n_basic_blocks);
3965 sbitmap_vector_ones (post_dominators, n_basic_blocks);
3966 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
3968 sbitmap_zero (dominators[0]);
3969 SET_BIT (dominators[0], 0);
3971 sbitmap_zero (post_dominators[n_basic_blocks-1]);
3972 SET_BIT (post_dominators[n_basic_blocks-1], 0);
3974 passes = 0;
3975 changed = 1;
3976 while (changed)
3978 changed = 0;
3979 for (bb = 1; bb < n_basic_blocks; bb++)
3981 sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators,
3982 bb, s_preds);
3983 SET_BIT (temp_bitmap[bb], bb);
3984 changed |= sbitmap_a_and_b (dominators[bb],
3985 dominators[bb],
3986 temp_bitmap[bb]);
3987 sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators,
3988 bb, s_succs);
3989 SET_BIT (temp_bitmap[bb], bb);
3990 changed |= sbitmap_a_and_b (post_dominators[bb],
3991 post_dominators[bb],
3992 temp_bitmap[bb]);
3994 passes++;
3997 free (temp_bitmap);