]> gcc.gnu.org Git - gcc.git/blob - gcc/flow.c
bitmap.c (bitmap_clear): Ensure `inline' is at the beginning of the declaration.
[gcc.git] / gcc / flow.c
1 /* Data flow analysis for GNU compiler.
2 Copyright (C) 1987, 88, 92-96, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 /* This file contains the data flow analysis pass of the compiler.
23 It computes data flow information
24 which tells combine_instructions which insns to consider combining
25 and controls register allocation.
26
27 Additional data flow information that is too bulky to record
28 is generated during the analysis, and is used at that time to
29 create autoincrement and autodecrement addressing.
30
31 The first step is dividing the function into basic blocks.
32 find_basic_blocks does this. Then life_analysis determines
33 where each register is live and where it is dead.
34
35 ** find_basic_blocks **
36
37 find_basic_blocks divides the current function's rtl
38 into basic blocks. It records the beginnings and ends of the
39 basic blocks in the vectors basic_block_head and basic_block_end,
40 and the number of blocks in n_basic_blocks.
41
42 find_basic_blocks also finds any unreachable loops
43 and deletes them.
44
45 ** life_analysis **
46
47 life_analysis is called immediately after find_basic_blocks.
48 It uses the basic block information to determine where each
49 hard or pseudo register is live.
50
51 ** live-register info **
52
53 The information about where each register is live is in two parts:
54 the REG_NOTES of insns, and the vector basic_block_live_at_start.
55
56 basic_block_live_at_start has an element for each basic block,
57 and the element is a bit-vector with a bit for each hard or pseudo
58 register. The bit is 1 if the register is live at the beginning
59 of the basic block.
60
61 Two types of elements can be added to an insn's REG_NOTES.
62 A REG_DEAD note is added to an insn's REG_NOTES for any register
63 that meets both of two conditions: The value in the register is not
64 needed in subsequent insns and the insn does not replace the value in
65 the register (in the case of multi-word hard registers, the value in
66 each register must be replaced by the insn to avoid a REG_DEAD note).
67
68 In the vast majority of cases, an object in a REG_DEAD note will be
69 used somewhere in the insn. The (rare) exception to this is if an
70 insn uses a multi-word hard register and only some of the registers are
71 needed in subsequent insns. In that case, REG_DEAD notes will be
72 provided for those hard registers that are not subsequently needed.
73 Partial REG_DEAD notes of this type do not occur when an insn sets
74 only some of the hard registers used in such a multi-word operand;
75 omitting REG_DEAD notes for objects stored in an insn is optional and
76 the desire to do so does not justify the complexity of the partial
77 REG_DEAD notes.
78
79 REG_UNUSED notes are added for each register that is set by the insn
80 but is unused subsequently (if every register set by the insn is unused
81 and the insn does not reference memory or have some other side-effect,
82 the insn is deleted instead). If only part of a multi-word hard
83 register is used in a subsequent insn, REG_UNUSED notes are made for
84 the parts that will not be used.
85
86 To determine which registers are live after any insn, one can
87 start from the beginning of the basic block and scan insns, noting
88 which registers are set by each insn and which die there.
89
90 ** Other actions of life_analysis **
91
92 life_analysis sets up the LOG_LINKS fields of insns because the
93 information needed to do so is readily available.
94
95 life_analysis deletes insns whose only effect is to store a value
96 that is never used.
97
98 life_analysis notices cases where a reference to a register as
99 a memory address can be combined with a preceding or following
100 incrementation or decrementation of the register. The separate
101 instruction to increment or decrement is deleted and the address
102 is changed to a POST_INC or similar rtx.
103
104 Each time an incrementing or decrementing address is created,
105 a REG_INC element is added to the insn's REG_NOTES list.
106
107 life_analysis fills in certain vectors containing information about
108 register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length,
109 reg_n_calls_crosses and reg_basic_block. */
110 \f
111 #include "config.h"
112 #include <stdio.h>
113 #include "rtl.h"
114 #include "basic-block.h"
115 #include "insn-config.h"
116 #include "regs.h"
117 #include "hard-reg-set.h"
118 #include "flags.h"
119 #include "output.h"
120 #include "except.h"
121
122 #include "obstack.h"
123 #define obstack_chunk_alloc xmalloc
124 #define obstack_chunk_free free
125
126 /* The contents of the current function definition are allocated
127 in this obstack, and all are freed at the end of the function.
128 For top-level functions, this is temporary_obstack.
129 Separate obstacks are made for nested functions. */
130
131 extern struct obstack *function_obstack;
132
133 /* List of labels that must never be deleted. */
134 extern rtx forced_labels;
135
136 /* Get the basic block number of an insn.
137 This info should not be expected to remain available
138 after the end of life_analysis. */
139
140 /* This is the limit of the allocated space in the following two arrays. */
141
142 static int max_uid_for_flow;
143
144 #define BLOCK_NUM(INSN) uid_block_number[INSN_UID (INSN)]
145
146 /* This is where the BLOCK_NUM values are really stored.
147 This is set up by find_basic_blocks and used there and in life_analysis,
148 and then freed. */
149
150 static int *uid_block_number;
151
152 /* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
153
154 #define INSN_VOLATILE(INSN) uid_volatile[INSN_UID (INSN)]
155 static char *uid_volatile;
156
157 /* Number of basic blocks in the current function. */
158
159 int n_basic_blocks;
160
161 /* Maximum register number used in this function, plus one. */
162
163 int max_regno;
164
165 /* Maximum number of SCRATCH rtx's used in any basic block of this
166 function. */
167
168 int max_scratch;
169
170 /* Number of SCRATCH rtx's in the current block. */
171
172 static int num_scratch;
173
174 /* Indexed by n, giving various register information */
175
176 reg_info *reg_n_info;
177
178 /* Element N is the next insn that uses (hard or pseudo) register number N
179 within the current basic block; or zero, if there is no such insn.
180 This is valid only during the final backward scan in propagate_block. */
181
182 static rtx *reg_next_use;
183
184 /* Size of a regset for the current function,
185 in (1) bytes and (2) elements. */
186
187 int regset_bytes;
188 int regset_size;
189
190 /* Element N is first insn in basic block N.
191 This info lasts until we finish compiling the function. */
192
193 rtx *basic_block_head;
194
195 /* Element N is last insn in basic block N.
196 This info lasts until we finish compiling the function. */
197
198 rtx *basic_block_end;
199
200 /* Element N is a regset describing the registers live
201 at the start of basic block N.
202 This info lasts until we finish compiling the function. */
203
204 regset *basic_block_live_at_start;
205
206 /* Regset of regs live when calls to `setjmp'-like functions happen. */
207
208 regset regs_live_at_setjmp;
209
210 /* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
211 that have to go in the same hard reg.
212 The first two regs in the list are a pair, and the next two
213 are another pair, etc. */
214 rtx regs_may_share;
215
216 /* Element N is nonzero if control can drop into basic block N
217 from the preceding basic block. Freed after life_analysis. */
218
219 static char *basic_block_drops_in;
220
221 /* Element N is depth within loops of the last insn in basic block number N.
222 Freed after life_analysis. */
223
224 static short *basic_block_loop_depth;
225
226 /* Element N nonzero if basic block N can actually be reached.
227 Vector exists only during find_basic_blocks. */
228
229 static char *block_live_static;
230
231 /* Depth within loops of basic block being scanned for lifetime analysis,
232 plus one. This is the weight attached to references to registers. */
233
234 static int loop_depth;
235
236 /* During propagate_block, this is non-zero if the value of CC0 is live. */
237
238 static int cc0_live;
239
240 /* During propagate_block, this contains the last MEM stored into. It
241 is used to eliminate consecutive stores to the same location. */
242
243 static rtx last_mem_set;
244
245 /* Set of registers that may be eliminable. These are handled specially
246 in updating regs_ever_live. */
247
248 static HARD_REG_SET elim_reg_set;
249
250 /* Forward declarations */
251 static void find_basic_blocks PROTO((rtx, rtx));
252 static void mark_label_ref PROTO((rtx, rtx, int));
253 static void life_analysis PROTO((rtx, int));
254 void allocate_for_life_analysis PROTO((void));
255 void init_regset_vector PROTO((regset *, int, struct obstack *));
256 void free_regset_vector PROTO((regset *, int));
257 static void propagate_block PROTO((regset, rtx, rtx, int,
258 regset, int));
259 static rtx flow_delete_insn PROTO((rtx));
260 static int insn_dead_p PROTO((rtx, regset, int));
261 static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
262 static void mark_set_regs PROTO((regset, regset, rtx,
263 rtx, regset));
264 static void mark_set_1 PROTO((regset, regset, rtx,
265 rtx, regset));
266 #ifdef AUTO_INC_DEC
267 static void find_auto_inc PROTO((regset, rtx, rtx));
268 static int try_pre_increment_1 PROTO((rtx));
269 static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
270 #endif
271 static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
272 void dump_flow_info PROTO((FILE *));
273 \f
274 /* Find basic blocks of the current function and perform data flow analysis.
275 F is the first insn of the function and NREGS the number of register numbers
276 in use. */
277
278 void
279 flow_analysis (f, nregs, file)
280 rtx f;
281 int nregs;
282 FILE *file;
283 {
284 register rtx insn;
285 register int i;
286 rtx nonlocal_label_list = nonlocal_label_rtx_list ();
287
288 #ifdef ELIMINABLE_REGS
289 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
290 #endif
291
292 /* Record which registers will be eliminated. We use this in
293 mark_used_regs. */
294
295 CLEAR_HARD_REG_SET (elim_reg_set);
296
297 #ifdef ELIMINABLE_REGS
298 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
299 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
300 #else
301 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
302 #endif
303
304 /* Count the basic blocks. Also find maximum insn uid value used. */
305
306 {
307 register RTX_CODE prev_code = JUMP_INSN;
308 register RTX_CODE code;
309 int eh_region = 0;
310
311 max_uid_for_flow = 0;
312
313 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
314 {
315 code = GET_CODE (insn);
316 if (INSN_UID (insn) > max_uid_for_flow)
317 max_uid_for_flow = INSN_UID (insn);
318 if (code == CODE_LABEL
319 || (GET_RTX_CLASS (code) == 'i'
320 && (prev_code == JUMP_INSN
321 || (prev_code == CALL_INSN
322 && (nonlocal_label_list != 0 || eh_region)
323 && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
324 || prev_code == BARRIER)))
325 i++;
326
327 if (code == CALL_INSN && find_reg_note (insn, REG_RETVAL, NULL_RTX))
328 code = INSN;
329
330 if (code != NOTE)
331 prev_code = code;
332 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
333 ++eh_region;
334 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
335 --eh_region;
336 }
337 }
338
339 #ifdef AUTO_INC_DEC
340 /* Leave space for insns we make in some cases for auto-inc. These cases
341 are rare, so we don't need too much space. */
342 max_uid_for_flow += max_uid_for_flow / 10;
343 #endif
344
345 /* Allocate some tables that last till end of compiling this function
346 and some needed only in find_basic_blocks and life_analysis. */
347
348 n_basic_blocks = i;
349 basic_block_head = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
350 basic_block_end = (rtx *) oballoc (n_basic_blocks * sizeof (rtx));
351 basic_block_drops_in = (char *) alloca (n_basic_blocks);
352 basic_block_loop_depth = (short *) alloca (n_basic_blocks * sizeof (short));
353 uid_block_number
354 = (int *) alloca ((max_uid_for_flow + 1) * sizeof (int));
355 uid_volatile = (char *) alloca (max_uid_for_flow + 1);
356 bzero (uid_volatile, max_uid_for_flow + 1);
357
358 find_basic_blocks (f, nonlocal_label_list);
359 life_analysis (f, nregs);
360 if (file)
361 dump_flow_info (file);
362
363 basic_block_drops_in = 0;
364 uid_block_number = 0;
365 basic_block_loop_depth = 0;
366 }
367 \f
368 /* Find all basic blocks of the function whose first insn is F.
369 Store the correct data in the tables that describe the basic blocks,
370 set up the chains of references for each CODE_LABEL, and
371 delete any entire basic blocks that cannot be reached.
372
373 NONLOCAL_LABEL_LIST is the same local variable from flow_analysis. */
374
375 static void
376 find_basic_blocks (f, nonlocal_label_list)
377 rtx f, nonlocal_label_list;
378 {
379 register rtx insn;
380 register int i;
381 register char *block_live = (char *) alloca (n_basic_blocks);
382 register char *block_marked = (char *) alloca (n_basic_blocks);
383 /* An array of CODE_LABELs, indexed by UID for the start of the active
384 EH handler for each insn in F. */
385 rtx *active_eh_handler;
386 /* List of label_refs to all labels whose addresses are taken
387 and used as data. */
388 rtx label_value_list;
389 rtx x, note, eh_note;
390 enum rtx_code prev_code, code;
391 int depth, pass;
392
393 pass = 1;
394 active_eh_handler = (rtx *) alloca ((max_uid_for_flow + 1) * sizeof (rtx));
395 restart:
396
397 label_value_list = 0;
398 block_live_static = block_live;
399 bzero (block_live, n_basic_blocks);
400 bzero (block_marked, n_basic_blocks);
401 bzero (active_eh_handler, (max_uid_for_flow + 1) * sizeof (rtx));
402
403 /* Initialize with just block 0 reachable and no blocks marked. */
404 if (n_basic_blocks > 0)
405 block_live[0] = 1;
406
407 /* Initialize the ref chain of each label to 0. Record where all the
408 blocks start and end and their depth in loops. For each insn, record
409 the block it is in. Also mark as reachable any blocks headed by labels
410 that must not be deleted. */
411
412 for (eh_note = NULL_RTX, insn = f, i = -1, prev_code = JUMP_INSN, depth = 1;
413 insn; insn = NEXT_INSN (insn))
414 {
415 code = GET_CODE (insn);
416 if (code == NOTE)
417 {
418 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
419 depth++;
420 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
421 depth--;
422 }
423
424 /* A basic block starts at label, or after something that can jump. */
425 else if (code == CODE_LABEL
426 || (GET_RTX_CLASS (code) == 'i'
427 && (prev_code == JUMP_INSN
428 || (prev_code == CALL_INSN
429 && (nonlocal_label_list != 0 || eh_note)
430 && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
431 || prev_code == BARRIER)))
432 {
433 basic_block_head[++i] = insn;
434 basic_block_end[i] = insn;
435 basic_block_loop_depth[i] = depth;
436
437 if (code == CODE_LABEL)
438 {
439 LABEL_REFS (insn) = insn;
440 /* Any label that cannot be deleted
441 is considered to start a reachable block. */
442 if (LABEL_PRESERVE_P (insn))
443 block_live[i] = 1;
444 }
445 }
446
447 else if (GET_RTX_CLASS (code) == 'i')
448 {
449 basic_block_end[i] = insn;
450 basic_block_loop_depth[i] = depth;
451 }
452
453 if (GET_RTX_CLASS (code) == 'i')
454 {
455 /* Make a list of all labels referred to other than by jumps. */
456 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
457 if (REG_NOTE_KIND (note) == REG_LABEL)
458 label_value_list = gen_rtx (EXPR_LIST, VOIDmode, XEXP (note, 0),
459 label_value_list);
460 }
461
462 /* Keep a lifo list of the currently active exception handlers. */
463 if (GET_CODE (insn) == NOTE)
464 {
465 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
466 {
467 for (x = exception_handler_labels; x; x = XEXP (x, 1))
468 if (CODE_LABEL_NUMBER (XEXP (x, 0)) == NOTE_BLOCK_NUMBER (insn))
469 {
470 eh_note = gen_rtx (EXPR_LIST, VOIDmode,
471 XEXP (x, 0), eh_note);
472 break;
473 }
474 if (x == NULL_RTX)
475 abort ();
476 }
477 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
478 eh_note = XEXP (eh_note, 1);
479 }
480 /* If we encounter a CALL_INSN, note which exception handler it
481 might pass control to.
482
483 If doing asynchronous exceptions, record the active EH handler
484 for every insn, since most insns can throw. */
485 else if (eh_note
486 && (asynchronous_exceptions
487 || (GET_CODE (insn) == CALL_INSN
488 && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))))
489 active_eh_handler[INSN_UID (insn)] = XEXP (eh_note, 0);
490
491 BLOCK_NUM (insn) = i;
492
493 if (code != NOTE)
494 prev_code = code;
495 }
496
497 /* During the second pass, `n_basic_blocks' is only an upper bound.
498 Only perform the sanity check for the first pass, and on the second
499 pass ensure `n_basic_blocks' is set to the correct value. */
500 if (pass == 1 && i + 1 != n_basic_blocks)
501 abort ();
502 n_basic_blocks = i + 1;
503
504 /* Record which basic blocks control can drop in to. */
505
506 for (i = 0; i < n_basic_blocks; i++)
507 {
508 for (insn = PREV_INSN (basic_block_head[i]);
509 insn && GET_CODE (insn) == NOTE; insn = PREV_INSN (insn))
510 ;
511
512 basic_block_drops_in[i] = insn && GET_CODE (insn) != BARRIER;
513 }
514
515 /* Now find which basic blocks can actually be reached
516 and put all jump insns' LABEL_REFS onto the ref-chains
517 of their target labels. */
518
519 if (n_basic_blocks > 0)
520 {
521 int something_marked = 1;
522 int deleted;
523
524 /* Pass over all blocks, marking each block that is reachable
525 and has not yet been marked.
526 Keep doing this until, in one pass, no blocks have been marked.
527 Then blocks_live and blocks_marked are identical and correct.
528 In addition, all jumps actually reachable have been marked. */
529
530 while (something_marked)
531 {
532 something_marked = 0;
533 for (i = 0; i < n_basic_blocks; i++)
534 if (block_live[i] && !block_marked[i])
535 {
536 block_marked[i] = 1;
537 something_marked = 1;
538 if (i + 1 < n_basic_blocks && basic_block_drops_in[i + 1])
539 block_live[i + 1] = 1;
540 insn = basic_block_end[i];
541 if (GET_CODE (insn) == JUMP_INSN)
542 mark_label_ref (PATTERN (insn), insn, 0);
543
544 /* If we have any forced labels, mark them as potentially
545 reachable from this block. */
546 for (x = forced_labels; x; x = XEXP (x, 1))
547 if (! LABEL_REF_NONLOCAL_P (x))
548 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode, XEXP (x, 0)),
549 insn, 0);
550
551 /* Now scan the insns for this block, we may need to make
552 edges for some of them to various non-obvious locations
553 (exception handlers, nonlocal labels, etc). */
554 for (insn = basic_block_head[i];
555 insn != NEXT_INSN (basic_block_end[i]);
556 insn = NEXT_INSN (insn))
557 {
558 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
559 {
560
561 /* References to labels in non-jumping insns have
562 REG_LABEL notes attached to them.
563
564 This can happen for computed gotos; we don't care
565 about them here since the values are also on the
566 label_value_list and will be marked live if we find
567 a live computed goto.
568
569 This can also happen when we take the address of
570 a label to pass as an argument to __throw. Note
571 throw only uses the value to determine what handler
572 should be called -- ie the label is not used as
573 a jump target, it just marks regions in the code.
574
575 In theory we should be able to ignore the REG_LABEL
576 notes, but we have to make sure that the label and
577 associated insns aren't marked dead, so we make
578 the block in question live and create an edge from
579 this insn to the label. This is not strictly
580 correct, but it is close enough for now. */
581 for (note = REG_NOTES (insn);
582 note;
583 note = XEXP (note, 1))
584 {
585 if (REG_NOTE_KIND (note) == REG_LABEL)
586 {
587 x = XEXP (note, 0);
588 block_live[BLOCK_NUM (x)] = 1;
589 mark_label_ref (gen_rtx (LABEL_REF,
590 VOIDmode, x),
591 insn, 0);
592 }
593 }
594
595 /* If this is a computed jump, then mark it as
596 reaching everything on the label_value_list
597 and forced_labels list. */
598 if (computed_jump_p (insn))
599 {
600 for (x = label_value_list; x; x = XEXP (x, 1))
601 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
602 XEXP (x, 0)),
603 insn, 0);
604
605 for (x = forced_labels; x; x = XEXP (x, 1))
606 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
607 XEXP (x, 0)),
608 insn, 0);
609 }
610
611 /* If this is a CALL_INSN, then mark it as reaching
612 the active EH handler for this CALL_INSN. If
613 we're handling asynchronous exceptions mark every
614 insn as reaching the active EH handler.
615
616 Also mark the CALL_INSN as reaching any nonlocal
617 goto sites. */
618 else if (asynchronous_exceptions
619 || (GET_CODE (insn) == CALL_INSN
620 && ! find_reg_note (insn, REG_RETVAL,
621 NULL_RTX)))
622 {
623 if (active_eh_handler[INSN_UID (insn)])
624 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
625 active_eh_handler[INSN_UID (insn)]),
626 insn, 0);
627
628 if (!asynchronous_exceptions)
629 {
630 for (x = nonlocal_label_list;
631 x;
632 x = XEXP (x, 1))
633 mark_label_ref (gen_rtx (LABEL_REF, VOIDmode,
634 XEXP (x, 0)),
635 insn, 0);
636 }
637 /* ??? This could be made smarter:
638 in some cases it's possible to tell that
639 certain calls will not do a nonlocal goto.
640
641 For example, if the nested functions that
642 do the nonlocal gotos do not have their
643 addresses taken, then only calls to those
644 functions or to other nested functions that
645 use them could possibly do nonlocal gotos. */
646 }
647 }
648 }
649 }
650 }
651
652 /* This should never happen. If it does that means we've computed an
653 incorrect flow graph, which can lead to aborts/crashes later in the
654 compiler or incorrect code generation.
655
656 We used to try and continue here, but that's just asking for trouble
657 later during the compile or at runtime. It's easier to debug the
658 problem here than later! */
659 for (i = 1; i < n_basic_blocks; i++)
660 if (block_live[i] && ! basic_block_drops_in[i]
661 && GET_CODE (basic_block_head[i]) == CODE_LABEL
662 && LABEL_REFS (basic_block_head[i]) == basic_block_head[i])
663 abort ();
664
665 /* Now delete the code for any basic blocks that can't be reached.
666 They can occur because jump_optimize does not recognize
667 unreachable loops as unreachable. */
668
669 deleted = 0;
670 for (i = 0; i < n_basic_blocks; i++)
671 if (!block_live[i])
672 {
673 deleted++;
674
675 /* Delete the insns in a (non-live) block. We physically delete
676 every non-note insn except the start and end (so
677 basic_block_head/end needn't be updated), we turn the latter
678 into NOTE_INSN_DELETED notes.
679 We use to "delete" the insns by turning them into notes, but
680 we may be deleting lots of insns that subsequent passes would
681 otherwise have to process. Secondly, lots of deleted blocks in
682 a row can really slow down propagate_block since it will
683 otherwise process insn-turned-notes multiple times when it
684 looks for loop begin/end notes. */
685 if (basic_block_head[i] != basic_block_end[i])
686 {
687 /* It would be quicker to delete all of these with a single
688 unchaining, rather than one at a time, but we need to keep
689 the NOTE's. */
690 insn = NEXT_INSN (basic_block_head[i]);
691 while (insn != basic_block_end[i])
692 {
693 if (GET_CODE (insn) == BARRIER)
694 abort ();
695 else if (GET_CODE (insn) != NOTE)
696 insn = flow_delete_insn (insn);
697 else
698 insn = NEXT_INSN (insn);
699 }
700 }
701 insn = basic_block_head[i];
702 if (GET_CODE (insn) != NOTE)
703 {
704 /* Turn the head into a deleted insn note. */
705 if (GET_CODE (insn) == BARRIER)
706 abort ();
707
708 /* If the head of this block is a CODE_LABEL, then it might
709 be the label for an exception handler which can't be
710 reached.
711
712 We need to remove the label from the exception_handler_label
713 list and remove the associated NOTE_EH_REGION_BEG and
714 NOTE_EH_REGION_END notes. */
715 if (GET_CODE (insn) == CODE_LABEL)
716 {
717 rtx x, *prev = &exception_handler_labels;
718
719 for (x = exception_handler_labels; x; x = XEXP (x, 1))
720 {
721 if (XEXP (x, 0) == insn)
722 {
723 /* Found a match, splice this label out of the
724 EH label list. */
725 *prev = XEXP (x, 1);
726 XEXP (x, 1) = NULL_RTX;
727 XEXP (x, 0) = NULL_RTX;
728
729 /* Now we have to find the EH_BEG and EH_END notes
730 associated with this label and remove them. */
731
732 for (x = get_insns (); x; x = NEXT_INSN (x))
733 {
734 if (GET_CODE (x) == NOTE
735 && ((NOTE_LINE_NUMBER (x)
736 == NOTE_INSN_EH_REGION_BEG)
737 || (NOTE_LINE_NUMBER (x)
738 == NOTE_INSN_EH_REGION_END))
739 && (NOTE_BLOCK_NUMBER (x)
740 == CODE_LABEL_NUMBER (insn)))
741 {
742 NOTE_LINE_NUMBER (x) = NOTE_INSN_DELETED;
743 NOTE_SOURCE_FILE (x) = 0;
744 }
745 }
746 break;
747 }
748 prev = &XEXP (x, 1);
749 }
750 }
751
752 PUT_CODE (insn, NOTE);
753 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
754 NOTE_SOURCE_FILE (insn) = 0;
755 }
756 insn = basic_block_end[i];
757 if (GET_CODE (insn) != NOTE)
758 {
759 /* Turn the tail into a deleted insn note. */
760 if (GET_CODE (insn) == BARRIER)
761 abort ();
762 PUT_CODE (insn, NOTE);
763 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
764 NOTE_SOURCE_FILE (insn) = 0;
765 }
766 /* BARRIERs are between basic blocks, not part of one.
767 Delete a BARRIER if the preceding jump is deleted.
768 We cannot alter a BARRIER into a NOTE
769 because it is too short; but we can really delete
770 it because it is not part of a basic block. */
771 if (NEXT_INSN (insn) != 0
772 && GET_CODE (NEXT_INSN (insn)) == BARRIER)
773 delete_insn (NEXT_INSN (insn));
774
775 /* Each time we delete some basic blocks,
776 see if there is a jump around them that is
777 being turned into a no-op. If so, delete it. */
778
779 if (block_live[i - 1])
780 {
781 register int j;
782 for (j = i + 1; j < n_basic_blocks; j++)
783 if (block_live[j])
784 {
785 rtx label;
786 insn = basic_block_end[i - 1];
787 if (GET_CODE (insn) == JUMP_INSN
788 /* An unconditional jump is the only possibility
789 we must check for, since a conditional one
790 would make these blocks live. */
791 && simplejump_p (insn)
792 && (label = XEXP (SET_SRC (PATTERN (insn)), 0), 1)
793 && INSN_UID (label) != 0
794 && BLOCK_NUM (label) == j)
795 {
796 PUT_CODE (insn, NOTE);
797 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
798 NOTE_SOURCE_FILE (insn) = 0;
799 if (GET_CODE (NEXT_INSN (insn)) != BARRIER)
800 abort ();
801 delete_insn (NEXT_INSN (insn));
802 }
803 break;
804 }
805 }
806 }
807
808 /* There are pathological cases where one function calling hundreds of
809 nested inline functions can generate lots and lots of unreachable
810 blocks that jump can't delete. Since we don't use sparse matrices
811 a lot of memory will be needed to compile such functions.
812 Implementing sparse matrices is a fair bit of work and it is not
813 clear that they win more than they lose (we don't want to
814 unnecessarily slow down compilation of normal code). By making
815 another pass for the pathological case, we can greatly speed up
816 their compilation without hurting normal code. This works because
817 all the insns in the unreachable blocks have either been deleted or
818 turned into notes.
819 Note that we're talking about reducing memory usage by 10's of
820 megabytes and reducing compilation time by several minutes. */
821 /* ??? The choice of when to make another pass is a bit arbitrary,
822 and was derived from empirical data. */
823 if (pass == 1
824 && deleted > 200)
825 {
826 pass++;
827 n_basic_blocks -= deleted;
828 /* `n_basic_blocks' may not be correct at this point: two previously
829 separate blocks may now be merged. That's ok though as we
830 recalculate it during the second pass. It certainly can't be
831 any larger than the current value. */
832 goto restart;
833 }
834 }
835 }
836 \f
837 /* Subroutines of find_basic_blocks. */
838
839 /* Check expression X for label references;
840 if one is found, add INSN to the label's chain of references.
841
842 CHECKDUP means check for and avoid creating duplicate references
843 from the same insn. Such duplicates do no serious harm but
844 can slow life analysis. CHECKDUP is set only when duplicates
845 are likely. */
846
847 static void
848 mark_label_ref (x, insn, checkdup)
849 rtx x, insn;
850 int checkdup;
851 {
852 register RTX_CODE code;
853 register int i;
854 register char *fmt;
855
856 /* We can be called with NULL when scanning label_value_list. */
857 if (x == 0)
858 return;
859
860 code = GET_CODE (x);
861 if (code == LABEL_REF)
862 {
863 register rtx label = XEXP (x, 0);
864 register rtx y;
865 if (GET_CODE (label) != CODE_LABEL)
866 abort ();
867 /* If the label was never emitted, this insn is junk,
868 but avoid a crash trying to refer to BLOCK_NUM (label).
869 This can happen as a result of a syntax error
870 and a diagnostic has already been printed. */
871 if (INSN_UID (label) == 0)
872 return;
873 CONTAINING_INSN (x) = insn;
874 /* if CHECKDUP is set, check for duplicate ref from same insn
875 and don't insert. */
876 if (checkdup)
877 for (y = LABEL_REFS (label); y != label; y = LABEL_NEXTREF (y))
878 if (CONTAINING_INSN (y) == insn)
879 return;
880 LABEL_NEXTREF (x) = LABEL_REFS (label);
881 LABEL_REFS (label) = x;
882 block_live_static[BLOCK_NUM (label)] = 1;
883 return;
884 }
885
886 fmt = GET_RTX_FORMAT (code);
887 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
888 {
889 if (fmt[i] == 'e')
890 mark_label_ref (XEXP (x, i), insn, 0);
891 if (fmt[i] == 'E')
892 {
893 register int j;
894 for (j = 0; j < XVECLEN (x, i); j++)
895 mark_label_ref (XVECEXP (x, i, j), insn, 1);
896 }
897 }
898 }
899
900 /* Delete INSN by patching it out.
901 Return the next insn. */
902
903 static rtx
904 flow_delete_insn (insn)
905 rtx insn;
906 {
907 /* ??? For the moment we assume we don't have to watch for NULLs here
908 since the start/end of basic blocks aren't deleted like this. */
909 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
910 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
911 return NEXT_INSN (insn);
912 }
913 \f
914 /* Determine which registers are live at the start of each
915 basic block of the function whose first insn is F.
916 NREGS is the number of registers used in F.
917 We allocate the vector basic_block_live_at_start
918 and the regsets that it points to, and fill them with the data.
919 regset_size and regset_bytes are also set here. */
920
921 static void
922 life_analysis (f, nregs)
923 rtx f;
924 int nregs;
925 {
926 int first_pass;
927 int changed;
928 /* For each basic block, a bitmask of regs
929 live on exit from the block. */
930 regset *basic_block_live_at_end;
931 /* For each basic block, a bitmask of regs
932 live on entry to a successor-block of this block.
933 If this does not match basic_block_live_at_end,
934 that must be updated, and the block must be rescanned. */
935 regset *basic_block_new_live_at_end;
936 /* For each basic block, a bitmask of regs
937 whose liveness at the end of the basic block
938 can make a difference in which regs are live on entry to the block.
939 These are the regs that are set within the basic block,
940 possibly excluding those that are used after they are set. */
941 regset *basic_block_significant;
942 register int i;
943 rtx insn;
944
945 struct obstack flow_obstack;
946
947 gcc_obstack_init (&flow_obstack);
948
949 max_regno = nregs;
950
951 bzero (regs_ever_live, sizeof regs_ever_live);
952
953 /* Allocate and zero out many data structures
954 that will record the data from lifetime analysis. */
955
956 allocate_for_life_analysis ();
957
958 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
959 bzero ((char *) reg_next_use, nregs * sizeof (rtx));
960
961 /* Set up several regset-vectors used internally within this function.
962 Their meanings are documented above, with their declarations. */
963
964 basic_block_live_at_end
965 = (regset *) alloca (n_basic_blocks * sizeof (regset));
966
967 /* Don't use alloca since that leads to a crash rather than an error message
968 if there isn't enough space.
969 Don't use oballoc since we may need to allocate other things during
970 this function on the temporary obstack. */
971 init_regset_vector (basic_block_live_at_end, n_basic_blocks, &flow_obstack);
972
973 basic_block_new_live_at_end
974 = (regset *) alloca (n_basic_blocks * sizeof (regset));
975 init_regset_vector (basic_block_new_live_at_end, n_basic_blocks,
976 &flow_obstack);
977
978 basic_block_significant
979 = (regset *) alloca (n_basic_blocks * sizeof (regset));
980 init_regset_vector (basic_block_significant, n_basic_blocks, &flow_obstack);
981
982 /* Record which insns refer to any volatile memory
983 or for any reason can't be deleted just because they are dead stores.
984 Also, delete any insns that copy a register to itself. */
985
986 for (insn = f; insn; insn = NEXT_INSN (insn))
987 {
988 enum rtx_code code1 = GET_CODE (insn);
989 if (code1 == CALL_INSN)
990 INSN_VOLATILE (insn) = 1;
991 else if (code1 == INSN || code1 == JUMP_INSN)
992 {
993 /* Delete (in effect) any obvious no-op moves. */
994 if (GET_CODE (PATTERN (insn)) == SET
995 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
996 && GET_CODE (SET_SRC (PATTERN (insn))) == REG
997 && (REGNO (SET_DEST (PATTERN (insn)))
998 == REGNO (SET_SRC (PATTERN (insn))))
999 /* Insns carrying these notes are useful later on. */
1000 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1001 {
1002 PUT_CODE (insn, NOTE);
1003 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1004 NOTE_SOURCE_FILE (insn) = 0;
1005 }
1006 /* Delete (in effect) any obvious no-op moves. */
1007 else if (GET_CODE (PATTERN (insn)) == SET
1008 && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG
1009 && GET_CODE (SUBREG_REG (SET_DEST (PATTERN (insn)))) == REG
1010 && GET_CODE (SET_SRC (PATTERN (insn))) == SUBREG
1011 && GET_CODE (SUBREG_REG (SET_SRC (PATTERN (insn)))) == REG
1012 && (REGNO (SUBREG_REG (SET_DEST (PATTERN (insn))))
1013 == REGNO (SUBREG_REG (SET_SRC (PATTERN (insn)))))
1014 && SUBREG_WORD (SET_DEST (PATTERN (insn))) ==
1015 SUBREG_WORD (SET_SRC (PATTERN (insn)))
1016 /* Insns carrying these notes are useful later on. */
1017 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1018 {
1019 PUT_CODE (insn, NOTE);
1020 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1021 NOTE_SOURCE_FILE (insn) = 0;
1022 }
1023 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
1024 {
1025 /* If nothing but SETs of registers to themselves,
1026 this insn can also be deleted. */
1027 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1028 {
1029 rtx tem = XVECEXP (PATTERN (insn), 0, i);
1030
1031 if (GET_CODE (tem) == USE
1032 || GET_CODE (tem) == CLOBBER)
1033 continue;
1034
1035 if (GET_CODE (tem) != SET
1036 || GET_CODE (SET_DEST (tem)) != REG
1037 || GET_CODE (SET_SRC (tem)) != REG
1038 || REGNO (SET_DEST (tem)) != REGNO (SET_SRC (tem)))
1039 break;
1040 }
1041
1042 if (i == XVECLEN (PATTERN (insn), 0)
1043 /* Insns carrying these notes are useful later on. */
1044 && ! find_reg_note (insn, REG_EQUAL, NULL_RTX))
1045 {
1046 PUT_CODE (insn, NOTE);
1047 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1048 NOTE_SOURCE_FILE (insn) = 0;
1049 }
1050 else
1051 INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
1052 }
1053 else if (GET_CODE (PATTERN (insn)) != USE)
1054 INSN_VOLATILE (insn) = volatile_refs_p (PATTERN (insn));
1055 /* A SET that makes space on the stack cannot be dead.
1056 (Such SETs occur only for allocating variable-size data,
1057 so they will always have a PLUS or MINUS according to the
1058 direction of stack growth.)
1059 Even if this function never uses this stack pointer value,
1060 signal handlers do! */
1061 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
1062 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1063 #ifdef STACK_GROWS_DOWNWARD
1064 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
1065 #else
1066 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1067 #endif
1068 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
1069 INSN_VOLATILE (insn) = 1;
1070 }
1071 }
1072
1073 if (n_basic_blocks > 0)
1074 #ifdef EXIT_IGNORE_STACK
1075 if (! EXIT_IGNORE_STACK
1076 || (! FRAME_POINTER_REQUIRED && flag_omit_frame_pointer))
1077 #endif
1078 {
1079 /* If exiting needs the right stack value,
1080 consider the stack pointer live at the end of the function. */
1081 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1082 STACK_POINTER_REGNUM);
1083 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1084 STACK_POINTER_REGNUM);
1085 }
1086
1087 /* Mark the frame pointer is needed at the end of the function. If
1088 we end up eliminating it, it will be removed from the live list
1089 of each basic block by reload. */
1090
1091 if (n_basic_blocks > 0)
1092 {
1093 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1094 FRAME_POINTER_REGNUM);
1095 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1096 FRAME_POINTER_REGNUM);
1097 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1098 /* If they are different, also mark the hard frame pointer as live */
1099 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1],
1100 HARD_FRAME_POINTER_REGNUM);
1101 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1],
1102 HARD_FRAME_POINTER_REGNUM);
1103 #endif
1104 }
1105
1106 /* Mark all global registers and all registers used by the epilogue
1107 as being live at the end of the function since they may be
1108 referenced by our caller. */
1109
1110 if (n_basic_blocks > 0)
1111 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1112 if (global_regs[i]
1113 #ifdef EPILOGUE_USES
1114 || EPILOGUE_USES (i)
1115 #endif
1116 )
1117 {
1118 SET_REGNO_REG_SET (basic_block_live_at_end[n_basic_blocks - 1], i);
1119 SET_REGNO_REG_SET (basic_block_new_live_at_end[n_basic_blocks - 1], i);
1120 }
1121
1122 /* Propagate life info through the basic blocks
1123 around the graph of basic blocks.
1124
1125 This is a relaxation process: each time a new register
1126 is live at the end of the basic block, we must scan the block
1127 to determine which registers are, as a consequence, live at the beginning
1128 of that block. These registers must then be marked live at the ends
1129 of all the blocks that can transfer control to that block.
1130 The process continues until it reaches a fixed point. */
1131
1132 first_pass = 1;
1133 changed = 1;
1134 while (changed)
1135 {
1136 changed = 0;
1137 for (i = n_basic_blocks - 1; i >= 0; i--)
1138 {
1139 int consider = first_pass;
1140 int must_rescan = first_pass;
1141 register int j;
1142
1143 if (!first_pass)
1144 {
1145 /* Set CONSIDER if this block needs thinking about at all
1146 (that is, if the regs live now at the end of it
1147 are not the same as were live at the end of it when
1148 we last thought about it).
1149 Set must_rescan if it needs to be thought about
1150 instruction by instruction (that is, if any additional
1151 reg that is live at the end now but was not live there before
1152 is one of the significant regs of this basic block). */
1153
1154 EXECUTE_IF_AND_COMPL_IN_REG_SET
1155 (basic_block_new_live_at_end[i],
1156 basic_block_live_at_end[i], 0, j,
1157 {
1158 consider = 1;
1159 if (REGNO_REG_SET_P (basic_block_significant[i], j))
1160 {
1161 must_rescan = 1;
1162 goto done;
1163 }
1164 });
1165 done:
1166 if (! consider)
1167 continue;
1168 }
1169
1170 /* The live_at_start of this block may be changing,
1171 so another pass will be required after this one. */
1172 changed = 1;
1173
1174 if (! must_rescan)
1175 {
1176 /* No complete rescan needed;
1177 just record those variables newly known live at end
1178 as live at start as well. */
1179 IOR_AND_COMPL_REG_SET (basic_block_live_at_start[i],
1180 basic_block_new_live_at_end[i],
1181 basic_block_live_at_end[i]);
1182
1183 IOR_AND_COMPL_REG_SET (basic_block_live_at_end[i],
1184 basic_block_new_live_at_end[i],
1185 basic_block_live_at_end[i]);
1186 }
1187 else
1188 {
1189 /* Update the basic_block_live_at_start
1190 by propagation backwards through the block. */
1191 COPY_REG_SET (basic_block_live_at_end[i],
1192 basic_block_new_live_at_end[i]);
1193 COPY_REG_SET (basic_block_live_at_start[i],
1194 basic_block_live_at_end[i]);
1195 propagate_block (basic_block_live_at_start[i],
1196 basic_block_head[i], basic_block_end[i], 0,
1197 first_pass ? basic_block_significant[i]
1198 : (regset) 0,
1199 i);
1200 }
1201
1202 {
1203 register rtx jump, head;
1204
1205 /* Update the basic_block_new_live_at_end's of the block
1206 that falls through into this one (if any). */
1207 head = basic_block_head[i];
1208 if (basic_block_drops_in[i])
1209 IOR_REG_SET (basic_block_new_live_at_end[i-1],
1210 basic_block_live_at_start[i]);
1211
1212 /* Update the basic_block_new_live_at_end's of
1213 all the blocks that jump to this one. */
1214 if (GET_CODE (head) == CODE_LABEL)
1215 for (jump = LABEL_REFS (head);
1216 jump != head;
1217 jump = LABEL_NEXTREF (jump))
1218 {
1219 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
1220 IOR_REG_SET (basic_block_new_live_at_end[from_block],
1221 basic_block_live_at_start[i]);
1222 }
1223 }
1224 #ifdef USE_C_ALLOCA
1225 alloca (0);
1226 #endif
1227 }
1228 first_pass = 0;
1229 }
1230
1231 /* The only pseudos that are live at the beginning of the function are
1232 those that were not set anywhere in the function. local-alloc doesn't
1233 know how to handle these correctly, so mark them as not local to any
1234 one basic block. */
1235
1236 if (n_basic_blocks > 0)
1237 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[0],
1238 FIRST_PSEUDO_REGISTER, i,
1239 {
1240 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1241 });
1242
1243 /* Now the life information is accurate.
1244 Make one more pass over each basic block
1245 to delete dead stores, create autoincrement addressing
1246 and record how many times each register is used, is set, or dies.
1247
1248 To save time, we operate directly in basic_block_live_at_end[i],
1249 thus destroying it (in fact, converting it into a copy of
1250 basic_block_live_at_start[i]). This is ok now because
1251 basic_block_live_at_end[i] is no longer used past this point. */
1252
1253 max_scratch = 0;
1254
1255 for (i = 0; i < n_basic_blocks; i++)
1256 {
1257 propagate_block (basic_block_live_at_end[i],
1258 basic_block_head[i], basic_block_end[i], 1,
1259 (regset) 0, i);
1260 #ifdef USE_C_ALLOCA
1261 alloca (0);
1262 #endif
1263 }
1264
1265 #if 0
1266 /* Something live during a setjmp should not be put in a register
1267 on certain machines which restore regs from stack frames
1268 rather than from the jmpbuf.
1269 But we don't need to do this for the user's variables, since
1270 ANSI says only volatile variables need this. */
1271 #ifdef LONGJMP_RESTORE_FROM_STACK
1272 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1273 FIRST_PSEUDO_REGISTER, i,
1274 {
1275 if (regno_reg_rtx[i] != 0
1276 && ! REG_USERVAR_P (regno_reg_rtx[i]))
1277 {
1278 REG_LIVE_LENGTH (i) = -1;
1279 REG_BASIC_BLOCK (i) = -1;
1280 }
1281 });
1282 #endif
1283 #endif
1284
1285 /* We have a problem with any pseudoreg that
1286 lives across the setjmp. ANSI says that if a
1287 user variable does not change in value
1288 between the setjmp and the longjmp, then the longjmp preserves it.
1289 This includes longjmp from a place where the pseudo appears dead.
1290 (In principle, the value still exists if it is in scope.)
1291 If the pseudo goes in a hard reg, some other value may occupy
1292 that hard reg where this pseudo is dead, thus clobbering the pseudo.
1293 Conclusion: such a pseudo must not go in a hard reg. */
1294 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
1295 FIRST_PSEUDO_REGISTER, i,
1296 {
1297 if (regno_reg_rtx[i] != 0)
1298 {
1299 REG_LIVE_LENGTH (i) = -1;
1300 REG_BASIC_BLOCK (i) = -1;
1301 }
1302 });
1303
1304
1305 free_regset_vector (basic_block_live_at_end, n_basic_blocks);
1306 free_regset_vector (basic_block_new_live_at_end, n_basic_blocks);
1307 free_regset_vector (basic_block_significant, n_basic_blocks);
1308 basic_block_live_at_end = (regset *)0;
1309 basic_block_new_live_at_end = (regset *)0;
1310 basic_block_significant = (regset *)0;
1311
1312 obstack_free (&flow_obstack, NULL_PTR);
1313 }
1314 \f
1315 /* Subroutines of life analysis. */
1316
1317 /* Allocate the permanent data structures that represent the results
1318 of life analysis. Not static since used also for stupid life analysis. */
1319
1320 void
1321 allocate_for_life_analysis ()
1322 {
1323 register int i;
1324
1325 /* Recalculate the register space, in case it has grown. Old style
1326 vector oriented regsets would set regset_{size,bytes} here also. */
1327 allocate_reg_info (max_regno, FALSE, FALSE);
1328
1329 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
1330 information, explicitly reset it here. The allocation should have
1331 already happened on the previous reg_scan pass. Make sure in case
1332 some more registers were allocated. */
1333 for (i = 0; i < max_regno; i++)
1334 REG_N_SETS (i) = 0;
1335
1336 basic_block_live_at_start
1337 = (regset *) oballoc (n_basic_blocks * sizeof (regset));
1338 init_regset_vector (basic_block_live_at_start, n_basic_blocks,
1339 function_obstack);
1340
1341 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
1342 CLEAR_REG_SET (regs_live_at_setjmp);
1343 }
1344
1345 /* Make each element of VECTOR point at a regset. The vector has
1346 NELTS elements, and space is allocated from the ALLOC_OBSTACK
1347 obstack. */
1348
1349 void
1350 init_regset_vector (vector, nelts, alloc_obstack)
1351 regset *vector;
1352 int nelts;
1353 struct obstack *alloc_obstack;
1354 {
1355 register int i;
1356
1357 for (i = 0; i < nelts; i++)
1358 {
1359 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
1360 CLEAR_REG_SET (vector[i]);
1361 }
1362 }
1363
1364 /* Release any additional space allocated for each element of VECTOR point
1365 other than the regset header itself. The vector has NELTS elements. */
1366
1367 void
1368 free_regset_vector (vector, nelts)
1369 regset *vector;
1370 int nelts;
1371 {
1372 register int i;
1373
1374 for (i = 0; i < nelts; i++)
1375 FREE_REG_SET (vector[i]);
1376 }
1377
1378 /* Compute the registers live at the beginning of a basic block
1379 from those live at the end.
1380
1381 When called, OLD contains those live at the end.
1382 On return, it contains those live at the beginning.
1383 FIRST and LAST are the first and last insns of the basic block.
1384
1385 FINAL is nonzero if we are doing the final pass which is not
1386 for computing the life info (since that has already been done)
1387 but for acting on it. On this pass, we delete dead stores,
1388 set up the logical links and dead-variables lists of instructions,
1389 and merge instructions for autoincrement and autodecrement addresses.
1390
1391 SIGNIFICANT is nonzero only the first time for each basic block.
1392 If it is nonzero, it points to a regset in which we store
1393 a 1 for each register that is set within the block.
1394
1395 BNUM is the number of the basic block. */
1396
1397 static void
1398 propagate_block (old, first, last, final, significant, bnum)
1399 register regset old;
1400 rtx first;
1401 rtx last;
1402 int final;
1403 regset significant;
1404 int bnum;
1405 {
1406 register rtx insn;
1407 rtx prev;
1408 regset live;
1409 regset dead;
1410
1411 /* The following variables are used only if FINAL is nonzero. */
1412 /* This vector gets one element for each reg that has been live
1413 at any point in the basic block that has been scanned so far.
1414 SOMETIMES_MAX says how many elements are in use so far. */
1415 register int *regs_sometimes_live;
1416 int sometimes_max = 0;
1417 /* This regset has 1 for each reg that we have seen live so far.
1418 It and REGS_SOMETIMES_LIVE are updated together. */
1419 regset maxlive;
1420
1421 /* The loop depth may change in the middle of a basic block. Since we
1422 scan from end to beginning, we start with the depth at the end of the
1423 current basic block, and adjust as we pass ends and starts of loops. */
1424 loop_depth = basic_block_loop_depth[bnum];
1425
1426 dead = ALLOCA_REG_SET ();
1427 live = ALLOCA_REG_SET ();
1428
1429 cc0_live = 0;
1430 last_mem_set = 0;
1431
1432 /* Include any notes at the end of the block in the scan.
1433 This is in case the block ends with a call to setjmp. */
1434
1435 while (NEXT_INSN (last) != 0 && GET_CODE (NEXT_INSN (last)) == NOTE)
1436 {
1437 /* Look for loop boundaries, we are going forward here. */
1438 last = NEXT_INSN (last);
1439 if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_BEG)
1440 loop_depth++;
1441 else if (NOTE_LINE_NUMBER (last) == NOTE_INSN_LOOP_END)
1442 loop_depth--;
1443 }
1444
1445 if (final)
1446 {
1447 register int i;
1448
1449 num_scratch = 0;
1450 maxlive = ALLOCA_REG_SET ();
1451 COPY_REG_SET (maxlive, old);
1452 regs_sometimes_live = (int *) alloca (max_regno * sizeof (int));
1453
1454 /* Process the regs live at the end of the block.
1455 Enter them in MAXLIVE and REGS_SOMETIMES_LIVE.
1456 Also mark them as not local to any one basic block. */
1457 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
1458 {
1459 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
1460 regs_sometimes_live[sometimes_max] = i;
1461 sometimes_max++;
1462 });
1463 }
1464
1465 /* Scan the block an insn at a time from end to beginning. */
1466
1467 for (insn = last; ; insn = prev)
1468 {
1469 prev = PREV_INSN (insn);
1470
1471 if (GET_CODE (insn) == NOTE)
1472 {
1473 /* Look for loop boundaries, remembering that we are going
1474 backwards. */
1475 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
1476 loop_depth++;
1477 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
1478 loop_depth--;
1479
1480 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
1481 Abort now rather than setting register status incorrectly. */
1482 if (loop_depth == 0)
1483 abort ();
1484
1485 /* If this is a call to `setjmp' et al,
1486 warn if any non-volatile datum is live. */
1487
1488 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
1489 IOR_REG_SET (regs_live_at_setjmp, old);
1490 }
1491
1492 /* Update the life-status of regs for this insn.
1493 First DEAD gets which regs are set in this insn
1494 then LIVE gets which regs are used in this insn.
1495 Then the regs live before the insn
1496 are those live after, with DEAD regs turned off,
1497 and then LIVE regs turned on. */
1498
1499 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1500 {
1501 register int i;
1502 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1503 int insn_is_dead
1504 = (insn_dead_p (PATTERN (insn), old, 0)
1505 /* Don't delete something that refers to volatile storage! */
1506 && ! INSN_VOLATILE (insn));
1507 int libcall_is_dead
1508 = (insn_is_dead && note != 0
1509 && libcall_dead_p (PATTERN (insn), old, note, insn));
1510
1511 /* If an instruction consists of just dead store(s) on final pass,
1512 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
1513 We could really delete it with delete_insn, but that
1514 can cause trouble for first or last insn in a basic block. */
1515 if (final && insn_is_dead)
1516 {
1517 PUT_CODE (insn, NOTE);
1518 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1519 NOTE_SOURCE_FILE (insn) = 0;
1520
1521 /* CC0 is now known to be dead. Either this insn used it,
1522 in which case it doesn't anymore, or clobbered it,
1523 so the next insn can't use it. */
1524 cc0_live = 0;
1525
1526 /* If this insn is copying the return value from a library call,
1527 delete the entire library call. */
1528 if (libcall_is_dead)
1529 {
1530 rtx first = XEXP (note, 0);
1531 rtx p = insn;
1532 while (INSN_DELETED_P (first))
1533 first = NEXT_INSN (first);
1534 while (p != first)
1535 {
1536 p = PREV_INSN (p);
1537 PUT_CODE (p, NOTE);
1538 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
1539 NOTE_SOURCE_FILE (p) = 0;
1540 }
1541 }
1542 goto flushed;
1543 }
1544
1545 CLEAR_REG_SET (dead);
1546 CLEAR_REG_SET (live);
1547
1548 /* See if this is an increment or decrement that can be
1549 merged into a following memory address. */
1550 #ifdef AUTO_INC_DEC
1551 {
1552 register rtx x = single_set (insn);
1553
1554 /* Does this instruction increment or decrement a register? */
1555 if (final && x != 0
1556 && GET_CODE (SET_DEST (x)) == REG
1557 && (GET_CODE (SET_SRC (x)) == PLUS
1558 || GET_CODE (SET_SRC (x)) == MINUS)
1559 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
1560 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
1561 /* Ok, look for a following memory ref we can combine with.
1562 If one is found, change the memory ref to a PRE_INC
1563 or PRE_DEC, cancel this insn, and return 1.
1564 Return 0 if nothing has been done. */
1565 && try_pre_increment_1 (insn))
1566 goto flushed;
1567 }
1568 #endif /* AUTO_INC_DEC */
1569
1570 /* If this is not the final pass, and this insn is copying the
1571 value of a library call and it's dead, don't scan the
1572 insns that perform the library call, so that the call's
1573 arguments are not marked live. */
1574 if (libcall_is_dead)
1575 {
1576 /* Mark the dest reg as `significant'. */
1577 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
1578
1579 insn = XEXP (note, 0);
1580 prev = PREV_INSN (insn);
1581 }
1582 else if (GET_CODE (PATTERN (insn)) == SET
1583 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
1584 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
1585 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
1586 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
1587 /* We have an insn to pop a constant amount off the stack.
1588 (Such insns use PLUS regardless of the direction of the stack,
1589 and any insn to adjust the stack by a constant is always a pop.)
1590 These insns, if not dead stores, have no effect on life. */
1591 ;
1592 else
1593 {
1594 /* LIVE gets the regs used in INSN;
1595 DEAD gets those set by it. Dead insns don't make anything
1596 live. */
1597
1598 mark_set_regs (old, dead, PATTERN (insn),
1599 final ? insn : NULL_RTX, significant);
1600
1601 /* If an insn doesn't use CC0, it becomes dead since we
1602 assume that every insn clobbers it. So show it dead here;
1603 mark_used_regs will set it live if it is referenced. */
1604 cc0_live = 0;
1605
1606 if (! insn_is_dead)
1607 mark_used_regs (old, live, PATTERN (insn), final, insn);
1608
1609 /* Sometimes we may have inserted something before INSN (such as
1610 a move) when we make an auto-inc. So ensure we will scan
1611 those insns. */
1612 #ifdef AUTO_INC_DEC
1613 prev = PREV_INSN (insn);
1614 #endif
1615
1616 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
1617 {
1618 register int i;
1619
1620 rtx note;
1621
1622 for (note = CALL_INSN_FUNCTION_USAGE (insn);
1623 note;
1624 note = XEXP (note, 1))
1625 if (GET_CODE (XEXP (note, 0)) == USE)
1626 mark_used_regs (old, live, SET_DEST (XEXP (note, 0)),
1627 final, insn);
1628
1629 /* Each call clobbers all call-clobbered regs that are not
1630 global or fixed. Note that the function-value reg is a
1631 call-clobbered reg, and mark_set_regs has already had
1632 a chance to handle it. */
1633
1634 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1635 if (call_used_regs[i] && ! global_regs[i]
1636 && ! fixed_regs[i])
1637 SET_REGNO_REG_SET (dead, i);
1638
1639 /* The stack ptr is used (honorarily) by a CALL insn. */
1640 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
1641
1642 /* Calls may also reference any of the global registers,
1643 so they are made live. */
1644 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1645 if (global_regs[i])
1646 mark_used_regs (old, live,
1647 gen_rtx (REG, reg_raw_mode[i], i),
1648 final, insn);
1649
1650 /* Calls also clobber memory. */
1651 last_mem_set = 0;
1652 }
1653
1654 /* Update OLD for the registers used or set. */
1655 AND_COMPL_REG_SET (old, dead);
1656 IOR_REG_SET (old, live);
1657
1658 if (GET_CODE (insn) == CALL_INSN && final)
1659 {
1660 /* Any regs live at the time of a call instruction
1661 must not go in a register clobbered by calls.
1662 Find all regs now live and record this for them. */
1663
1664 register int *p = regs_sometimes_live;
1665
1666 for (i = 0; i < sometimes_max; i++, p++)
1667 if (REGNO_REG_SET_P (old, *p))
1668 REG_N_CALLS_CROSSED (*p)++;
1669 }
1670 }
1671
1672 /* On final pass, add any additional sometimes-live regs
1673 into MAXLIVE and REGS_SOMETIMES_LIVE.
1674 Also update counts of how many insns each reg is live at. */
1675
1676 if (final)
1677 {
1678 register int regno;
1679 register int *p;
1680
1681 EXECUTE_IF_AND_COMPL_IN_REG_SET
1682 (live, maxlive, 0, regno,
1683 {
1684 regs_sometimes_live[sometimes_max++] = regno;
1685 SET_REGNO_REG_SET (maxlive, regno);
1686 });
1687
1688 p = regs_sometimes_live;
1689 for (i = 0; i < sometimes_max; i++)
1690 {
1691 regno = *p++;
1692 if (REGNO_REG_SET_P (old, regno))
1693 REG_LIVE_LENGTH (regno)++;
1694 }
1695 }
1696 }
1697 flushed: ;
1698 if (insn == first)
1699 break;
1700 }
1701
1702 FREE_REG_SET (dead);
1703 FREE_REG_SET (live);
1704 if (final)
1705 FREE_REG_SET (maxlive);
1706
1707 if (num_scratch > max_scratch)
1708 max_scratch = num_scratch;
1709 }
1710 \f
1711 /* Return 1 if X (the body of an insn, or part of it) is just dead stores
1712 (SET expressions whose destinations are registers dead after the insn).
1713 NEEDED is the regset that says which regs are alive after the insn.
1714
1715 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL. */
1716
1717 static int
1718 insn_dead_p (x, needed, call_ok)
1719 rtx x;
1720 regset needed;
1721 int call_ok;
1722 {
1723 register RTX_CODE code = GET_CODE (x);
1724 /* If setting something that's a reg or part of one,
1725 see if that register's altered value will be live. */
1726
1727 if (code == SET)
1728 {
1729 register rtx r = SET_DEST (x);
1730 /* A SET that is a subroutine call cannot be dead. */
1731 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
1732 return 0;
1733
1734 #ifdef HAVE_cc0
1735 if (GET_CODE (r) == CC0)
1736 return ! cc0_live;
1737 #endif
1738
1739 if (GET_CODE (r) == MEM && last_mem_set && ! MEM_VOLATILE_P (r)
1740 && rtx_equal_p (r, last_mem_set))
1741 return 1;
1742
1743 while (GET_CODE (r) == SUBREG
1744 || GET_CODE (r) == STRICT_LOW_PART
1745 || GET_CODE (r) == ZERO_EXTRACT
1746 || GET_CODE (r) == SIGN_EXTRACT)
1747 r = SUBREG_REG (r);
1748
1749 if (GET_CODE (r) == REG)
1750 {
1751 register int regno = REGNO (r);
1752
1753 /* Don't delete insns to set global regs. */
1754 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1755 /* Make sure insns to set frame pointer aren't deleted. */
1756 || regno == FRAME_POINTER_REGNUM
1757 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1758 || regno == HARD_FRAME_POINTER_REGNUM
1759 #endif
1760 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1761 /* Make sure insns to set arg pointer are never deleted
1762 (if the arg pointer isn't fixed, there will be a USE for
1763 it, so we can treat it normally). */
1764 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1765 #endif
1766 || REGNO_REG_SET_P (needed, regno))
1767 return 0;
1768
1769 /* If this is a hard register, verify that subsequent words are
1770 not needed. */
1771 if (regno < FIRST_PSEUDO_REGISTER)
1772 {
1773 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
1774
1775 while (--n > 0)
1776 if (REGNO_REG_SET_P (needed, regno+n))
1777 return 0;
1778 }
1779
1780 return 1;
1781 }
1782 }
1783 /* If performing several activities,
1784 insn is dead if each activity is individually dead.
1785 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
1786 that's inside a PARALLEL doesn't make the insn worth keeping. */
1787 else if (code == PARALLEL)
1788 {
1789 register int i = XVECLEN (x, 0);
1790 for (i--; i >= 0; i--)
1791 {
1792 rtx elt = XVECEXP (x, 0, i);
1793 if (!insn_dead_p (elt, needed, call_ok)
1794 && GET_CODE (elt) != CLOBBER
1795 && GET_CODE (elt) != USE)
1796 return 0;
1797 }
1798 return 1;
1799 }
1800 /* We do not check CLOBBER or USE here.
1801 An insn consisting of just a CLOBBER or just a USE
1802 should not be deleted. */
1803 return 0;
1804 }
1805
1806 /* If X is the pattern of the last insn in a libcall, and assuming X is dead,
1807 return 1 if the entire library call is dead.
1808 This is true if X copies a register (hard or pseudo)
1809 and if the hard return reg of the call insn is dead.
1810 (The caller should have tested the destination of X already for death.)
1811
1812 If this insn doesn't just copy a register, then we don't
1813 have an ordinary libcall. In that case, cse could not have
1814 managed to substitute the source for the dest later on,
1815 so we can assume the libcall is dead.
1816
1817 NEEDED is the bit vector of pseudoregs live before this insn.
1818 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
1819
1820 static int
1821 libcall_dead_p (x, needed, note, insn)
1822 rtx x;
1823 regset needed;
1824 rtx note;
1825 rtx insn;
1826 {
1827 register RTX_CODE code = GET_CODE (x);
1828
1829 if (code == SET)
1830 {
1831 register rtx r = SET_SRC (x);
1832 if (GET_CODE (r) == REG)
1833 {
1834 rtx call = XEXP (note, 0);
1835 register int i;
1836
1837 /* Find the call insn. */
1838 while (call != insn && GET_CODE (call) != CALL_INSN)
1839 call = NEXT_INSN (call);
1840
1841 /* If there is none, do nothing special,
1842 since ordinary death handling can understand these insns. */
1843 if (call == insn)
1844 return 0;
1845
1846 /* See if the hard reg holding the value is dead.
1847 If this is a PARALLEL, find the call within it. */
1848 call = PATTERN (call);
1849 if (GET_CODE (call) == PARALLEL)
1850 {
1851 for (i = XVECLEN (call, 0) - 1; i >= 0; i--)
1852 if (GET_CODE (XVECEXP (call, 0, i)) == SET
1853 && GET_CODE (SET_SRC (XVECEXP (call, 0, i))) == CALL)
1854 break;
1855
1856 /* This may be a library call that is returning a value
1857 via invisible pointer. Do nothing special, since
1858 ordinary death handling can understand these insns. */
1859 if (i < 0)
1860 return 0;
1861
1862 call = XVECEXP (call, 0, i);
1863 }
1864
1865 return insn_dead_p (call, needed, 1);
1866 }
1867 }
1868 return 1;
1869 }
1870
1871 /* Return 1 if register REGNO was used before it was set.
1872 In other words, if it is live at function entry.
1873 Don't count global register variables or variables in registers
1874 that can be used for function arg passing, though. */
1875
1876 int
1877 regno_uninitialized (regno)
1878 int regno;
1879 {
1880 if (n_basic_blocks == 0
1881 || (regno < FIRST_PSEUDO_REGISTER
1882 && (global_regs[regno] || FUNCTION_ARG_REGNO_P (regno))))
1883 return 0;
1884
1885 return REGNO_REG_SET_P (basic_block_live_at_start[0], regno);
1886 }
1887
1888 /* 1 if register REGNO was alive at a place where `setjmp' was called
1889 and was set more than once or is an argument.
1890 Such regs may be clobbered by `longjmp'. */
1891
1892 int
1893 regno_clobbered_at_setjmp (regno)
1894 int regno;
1895 {
1896 if (n_basic_blocks == 0)
1897 return 0;
1898
1899 return ((REG_N_SETS (regno) > 1
1900 || REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
1901 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
1902 }
1903 \f
1904 /* Process the registers that are set within X.
1905 Their bits are set to 1 in the regset DEAD,
1906 because they are dead prior to this insn.
1907
1908 If INSN is nonzero, it is the insn being processed
1909 and the fact that it is nonzero implies this is the FINAL pass
1910 in propagate_block. In this case, various info about register
1911 usage is stored, LOG_LINKS fields of insns are set up. */
1912
1913 static void
1914 mark_set_regs (needed, dead, x, insn, significant)
1915 regset needed;
1916 regset dead;
1917 rtx x;
1918 rtx insn;
1919 regset significant;
1920 {
1921 register RTX_CODE code = GET_CODE (x);
1922
1923 if (code == SET || code == CLOBBER)
1924 mark_set_1 (needed, dead, x, insn, significant);
1925 else if (code == PARALLEL)
1926 {
1927 register int i;
1928 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1929 {
1930 code = GET_CODE (XVECEXP (x, 0, i));
1931 if (code == SET || code == CLOBBER)
1932 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
1933 }
1934 }
1935 }
1936
1937 /* Process a single SET rtx, X. */
1938
1939 static void
1940 mark_set_1 (needed, dead, x, insn, significant)
1941 regset needed;
1942 regset dead;
1943 rtx x;
1944 rtx insn;
1945 regset significant;
1946 {
1947 register int regno;
1948 register rtx reg = SET_DEST (x);
1949
1950 /* Modifying just one hardware register of a multi-reg value
1951 or just a byte field of a register
1952 does not mean the value from before this insn is now dead.
1953 But it does mean liveness of that register at the end of the block
1954 is significant.
1955
1956 Within mark_set_1, however, we treat it as if the register is
1957 indeed modified. mark_used_regs will, however, also treat this
1958 register as being used. Thus, we treat these insns as setting a
1959 new value for the register as a function of its old value. This
1960 cases LOG_LINKS to be made appropriately and this will help combine. */
1961
1962 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
1963 || GET_CODE (reg) == SIGN_EXTRACT
1964 || GET_CODE (reg) == STRICT_LOW_PART)
1965 reg = XEXP (reg, 0);
1966
1967 /* If we are writing into memory or into a register mentioned in the
1968 address of the last thing stored into memory, show we don't know
1969 what the last store was. If we are writing memory, save the address
1970 unless it is volatile. */
1971 if (GET_CODE (reg) == MEM
1972 || (GET_CODE (reg) == REG
1973 && last_mem_set != 0 && reg_overlap_mentioned_p (reg, last_mem_set)))
1974 last_mem_set = 0;
1975
1976 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
1977 /* There are no REG_INC notes for SP, so we can't assume we'll see
1978 everything that invalidates it. To be safe, don't eliminate any
1979 stores though SP; none of them should be redundant anyway. */
1980 && ! reg_mentioned_p (stack_pointer_rtx, reg))
1981 last_mem_set = reg;
1982
1983 if (GET_CODE (reg) == REG
1984 && (regno = REGNO (reg), regno != FRAME_POINTER_REGNUM)
1985 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1986 && regno != HARD_FRAME_POINTER_REGNUM
1987 #endif
1988 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1989 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1990 #endif
1991 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
1992 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
1993 {
1994 int some_needed = REGNO_REG_SET_P (needed, regno);
1995 int some_not_needed = ! some_needed;
1996
1997 /* Mark it as a significant register for this basic block. */
1998 if (significant)
1999 SET_REGNO_REG_SET (significant, regno);
2000
2001 /* Mark it as as dead before this insn. */
2002 SET_REGNO_REG_SET (dead, regno);
2003
2004 /* A hard reg in a wide mode may really be multiple registers.
2005 If so, mark all of them just like the first. */
2006 if (regno < FIRST_PSEUDO_REGISTER)
2007 {
2008 int n;
2009
2010 /* Nothing below is needed for the stack pointer; get out asap.
2011 Eg, log links aren't needed, since combine won't use them. */
2012 if (regno == STACK_POINTER_REGNUM)
2013 return;
2014
2015 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
2016 while (--n > 0)
2017 {
2018 int regno_n = regno + n;
2019 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2020 if (significant)
2021 SET_REGNO_REG_SET (significant, regno_n);
2022
2023 SET_REGNO_REG_SET (dead, regno_n);
2024 some_needed |= needed_regno;
2025 some_not_needed |= ! needed_regno;
2026 }
2027 }
2028 /* Additional data to record if this is the final pass. */
2029 if (insn)
2030 {
2031 register rtx y = reg_next_use[regno];
2032 register int blocknum = BLOCK_NUM (insn);
2033
2034 /* If this is a hard reg, record this function uses the reg. */
2035
2036 if (regno < FIRST_PSEUDO_REGISTER)
2037 {
2038 register int i;
2039 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
2040
2041 for (i = regno; i < endregno; i++)
2042 {
2043 /* The next use is no longer "next", since a store
2044 intervenes. */
2045 reg_next_use[i] = 0;
2046
2047 regs_ever_live[i] = 1;
2048 REG_N_SETS (i)++;
2049 }
2050 }
2051 else
2052 {
2053 /* The next use is no longer "next", since a store
2054 intervenes. */
2055 reg_next_use[regno] = 0;
2056
2057 /* Keep track of which basic blocks each reg appears in. */
2058
2059 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2060 REG_BASIC_BLOCK (regno) = blocknum;
2061 else if (REG_BASIC_BLOCK (regno) != blocknum)
2062 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2063
2064 /* Count (weighted) references, stores, etc. This counts a
2065 register twice if it is modified, but that is correct. */
2066 REG_N_SETS (regno)++;
2067
2068 REG_N_REFS (regno) += loop_depth;
2069
2070 /* The insns where a reg is live are normally counted
2071 elsewhere, but we want the count to include the insn
2072 where the reg is set, and the normal counting mechanism
2073 would not count it. */
2074 REG_LIVE_LENGTH (regno)++;
2075 }
2076
2077 if (! some_not_needed)
2078 {
2079 /* Make a logical link from the next following insn
2080 that uses this register, back to this insn.
2081 The following insns have already been processed.
2082
2083 We don't build a LOG_LINK for hard registers containing
2084 in ASM_OPERANDs. If these registers get replaced,
2085 we might wind up changing the semantics of the insn,
2086 even if reload can make what appear to be valid assignments
2087 later. */
2088 if (y && (BLOCK_NUM (y) == blocknum)
2089 && (regno >= FIRST_PSEUDO_REGISTER
2090 || asm_noperands (PATTERN (y)) < 0))
2091 LOG_LINKS (y)
2092 = gen_rtx (INSN_LIST, VOIDmode, insn, LOG_LINKS (y));
2093 }
2094 else if (! some_needed)
2095 {
2096 /* Note that dead stores have already been deleted when possible
2097 If we get here, we have found a dead store that cannot
2098 be eliminated (because the same insn does something useful).
2099 Indicate this by marking the reg being set as dying here. */
2100 REG_NOTES (insn)
2101 = gen_rtx (EXPR_LIST, REG_UNUSED, reg, REG_NOTES (insn));
2102 REG_N_DEATHS (REGNO (reg))++;
2103 }
2104 else
2105 {
2106 /* This is a case where we have a multi-word hard register
2107 and some, but not all, of the words of the register are
2108 needed in subsequent insns. Write REG_UNUSED notes
2109 for those parts that were not needed. This case should
2110 be rare. */
2111
2112 int i;
2113
2114 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
2115 i >= 0; i--)
2116 if (!REGNO_REG_SET_P (needed, regno + i))
2117 REG_NOTES (insn)
2118 = gen_rtx (EXPR_LIST, REG_UNUSED,
2119 gen_rtx (REG, reg_raw_mode[regno + i],
2120 regno + i),
2121 REG_NOTES (insn));
2122 }
2123 }
2124 }
2125 else if (GET_CODE (reg) == REG)
2126 reg_next_use[regno] = 0;
2127
2128 /* If this is the last pass and this is a SCRATCH, show it will be dying
2129 here and count it. */
2130 else if (GET_CODE (reg) == SCRATCH && insn != 0)
2131 {
2132 REG_NOTES (insn)
2133 = gen_rtx (EXPR_LIST, REG_UNUSED, reg, REG_NOTES (insn));
2134 num_scratch++;
2135 }
2136 }
2137 \f
2138 #ifdef AUTO_INC_DEC
2139
2140 /* X is a MEM found in INSN. See if we can convert it into an auto-increment
2141 reference. */
2142
2143 static void
2144 find_auto_inc (needed, x, insn)
2145 regset needed;
2146 rtx x;
2147 rtx insn;
2148 {
2149 rtx addr = XEXP (x, 0);
2150 HOST_WIDE_INT offset = 0;
2151 rtx set;
2152
2153 /* Here we detect use of an index register which might be good for
2154 postincrement, postdecrement, preincrement, or predecrement. */
2155
2156 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
2157 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
2158
2159 if (GET_CODE (addr) == REG)
2160 {
2161 register rtx y;
2162 register int size = GET_MODE_SIZE (GET_MODE (x));
2163 rtx use;
2164 rtx incr;
2165 int regno = REGNO (addr);
2166
2167 /* Is the next use an increment that might make auto-increment? */
2168 if ((incr = reg_next_use[regno]) != 0
2169 && (set = single_set (incr)) != 0
2170 && GET_CODE (set) == SET
2171 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
2172 /* Can't add side effects to jumps; if reg is spilled and
2173 reloaded, there's no way to store back the altered value. */
2174 && GET_CODE (insn) != JUMP_INSN
2175 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
2176 && XEXP (y, 0) == addr
2177 && GET_CODE (XEXP (y, 1)) == CONST_INT
2178 && (0
2179 #ifdef HAVE_POST_INCREMENT
2180 || (INTVAL (XEXP (y, 1)) == size && offset == 0)
2181 #endif
2182 #ifdef HAVE_POST_DECREMENT
2183 || (INTVAL (XEXP (y, 1)) == - size && offset == 0)
2184 #endif
2185 #ifdef HAVE_PRE_INCREMENT
2186 || (INTVAL (XEXP (y, 1)) == size && offset == size)
2187 #endif
2188 #ifdef HAVE_PRE_DECREMENT
2189 || (INTVAL (XEXP (y, 1)) == - size && offset == - size)
2190 #endif
2191 )
2192 /* Make sure this reg appears only once in this insn. */
2193 && (use = find_use_as_address (PATTERN (insn), addr, offset),
2194 use != 0 && use != (rtx) 1))
2195 {
2196 rtx q = SET_DEST (set);
2197 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
2198 ? (offset ? PRE_INC : POST_INC)
2199 : (offset ? PRE_DEC : POST_DEC));
2200
2201 if (dead_or_set_p (incr, addr))
2202 {
2203 /* This is the simple case. Try to make the auto-inc. If
2204 we can't, we are done. Otherwise, we will do any
2205 needed updates below. */
2206 if (! validate_change (insn, &XEXP (x, 0),
2207 gen_rtx (inc_code, Pmode, addr),
2208 0))
2209 return;
2210 }
2211 else if (GET_CODE (q) == REG
2212 /* PREV_INSN used here to check the semi-open interval
2213 [insn,incr). */
2214 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
2215 /* We must also check for sets of q as q may be
2216 a call clobbered hard register and there may
2217 be a call between PREV_INSN (insn) and incr. */
2218 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
2219 {
2220 /* We have *p followed sometime later by q = p+size.
2221 Both p and q must be live afterward,
2222 and q is not used between INSN and it's assignment.
2223 Change it to q = p, ...*q..., q = q+size.
2224 Then fall into the usual case. */
2225 rtx insns, temp;
2226
2227 start_sequence ();
2228 emit_move_insn (q, addr);
2229 insns = get_insns ();
2230 end_sequence ();
2231
2232 /* If anything in INSNS have UID's that don't fit within the
2233 extra space we allocate earlier, we can't make this auto-inc.
2234 This should never happen. */
2235 for (temp = insns; temp; temp = NEXT_INSN (temp))
2236 {
2237 if (INSN_UID (temp) > max_uid_for_flow)
2238 return;
2239 BLOCK_NUM (temp) = BLOCK_NUM (insn);
2240 }
2241
2242 /* If we can't make the auto-inc, or can't make the
2243 replacement into Y, exit. There's no point in making
2244 the change below if we can't do the auto-inc and doing
2245 so is not correct in the pre-inc case. */
2246
2247 validate_change (insn, &XEXP (x, 0),
2248 gen_rtx (inc_code, Pmode, q),
2249 1);
2250 validate_change (incr, &XEXP (y, 0), q, 1);
2251 if (! apply_change_group ())
2252 return;
2253
2254 /* We now know we'll be doing this change, so emit the
2255 new insn(s) and do the updates. */
2256 emit_insns_before (insns, insn);
2257
2258 if (basic_block_head[BLOCK_NUM (insn)] == insn)
2259 basic_block_head[BLOCK_NUM (insn)] = insns;
2260
2261 /* INCR will become a NOTE and INSN won't contain a
2262 use of ADDR. If a use of ADDR was just placed in
2263 the insn before INSN, make that the next use.
2264 Otherwise, invalidate it. */
2265 if (GET_CODE (PREV_INSN (insn)) == INSN
2266 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
2267 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
2268 reg_next_use[regno] = PREV_INSN (insn);
2269 else
2270 reg_next_use[regno] = 0;
2271
2272 addr = q;
2273 regno = REGNO (q);
2274
2275 /* REGNO is now used in INCR which is below INSN, but
2276 it previously wasn't live here. If we don't mark
2277 it as needed, we'll put a REG_DEAD note for it
2278 on this insn, which is incorrect. */
2279 SET_REGNO_REG_SET (needed, regno);
2280
2281 /* If there are any calls between INSN and INCR, show
2282 that REGNO now crosses them. */
2283 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
2284 if (GET_CODE (temp) == CALL_INSN)
2285 REG_N_CALLS_CROSSED (regno)++;
2286 }
2287 else
2288 return;
2289
2290 /* If we haven't returned, it means we were able to make the
2291 auto-inc, so update the status. First, record that this insn
2292 has an implicit side effect. */
2293
2294 REG_NOTES (insn)
2295 = gen_rtx (EXPR_LIST, REG_INC, addr, REG_NOTES (insn));
2296
2297 /* Modify the old increment-insn to simply copy
2298 the already-incremented value of our register. */
2299 if (! validate_change (incr, &SET_SRC (set), addr, 0))
2300 abort ();
2301
2302 /* If that makes it a no-op (copying the register into itself) delete
2303 it so it won't appear to be a "use" and a "set" of this
2304 register. */
2305 if (SET_DEST (set) == addr)
2306 {
2307 PUT_CODE (incr, NOTE);
2308 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
2309 NOTE_SOURCE_FILE (incr) = 0;
2310 }
2311
2312 if (regno >= FIRST_PSEUDO_REGISTER)
2313 {
2314 /* Count an extra reference to the reg. When a reg is
2315 incremented, spilling it is worse, so we want to make
2316 that less likely. */
2317 REG_N_REFS (regno) += loop_depth;
2318
2319 /* Count the increment as a setting of the register,
2320 even though it isn't a SET in rtl. */
2321 REG_N_SETS (regno)++;
2322 }
2323 }
2324 }
2325 }
2326 #endif /* AUTO_INC_DEC */
2327 \f
2328 /* Scan expression X and store a 1-bit in LIVE for each reg it uses.
2329 This is done assuming the registers needed from X
2330 are those that have 1-bits in NEEDED.
2331
2332 On the final pass, FINAL is 1. This means try for autoincrement
2333 and count the uses and deaths of each pseudo-reg.
2334
2335 INSN is the containing instruction. If INSN is dead, this function is not
2336 called. */
2337
2338 static void
2339 mark_used_regs (needed, live, x, final, insn)
2340 regset needed;
2341 regset live;
2342 rtx x;
2343 int final;
2344 rtx insn;
2345 {
2346 register RTX_CODE code;
2347 register int regno;
2348 int i;
2349
2350 retry:
2351 code = GET_CODE (x);
2352 switch (code)
2353 {
2354 case LABEL_REF:
2355 case SYMBOL_REF:
2356 case CONST_INT:
2357 case CONST:
2358 case CONST_DOUBLE:
2359 case PC:
2360 case ADDR_VEC:
2361 case ADDR_DIFF_VEC:
2362 case ASM_INPUT:
2363 return;
2364
2365 #ifdef HAVE_cc0
2366 case CC0:
2367 cc0_live = 1;
2368 return;
2369 #endif
2370
2371 case CLOBBER:
2372 /* If we are clobbering a MEM, mark any registers inside the address
2373 as being used. */
2374 if (GET_CODE (XEXP (x, 0)) == MEM)
2375 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
2376 return;
2377
2378 case MEM:
2379 /* Invalidate the data for the last MEM stored, but only if MEM is
2380 something that can be stored into. */
2381 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2382 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
2383 ; /* needn't clear last_mem_set */
2384 else
2385 last_mem_set = 0;
2386
2387 #ifdef AUTO_INC_DEC
2388 if (final)
2389 find_auto_inc (needed, x, insn);
2390 #endif
2391 break;
2392
2393 case SUBREG:
2394 if (GET_CODE (SUBREG_REG (x)) == REG
2395 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
2396 && (GET_MODE_SIZE (GET_MODE (x))
2397 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
2398 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
2399
2400 /* While we're here, optimize this case. */
2401 x = SUBREG_REG (x);
2402
2403 /* In case the SUBREG is not of a register, don't optimize */
2404 if (GET_CODE (x) != REG)
2405 {
2406 mark_used_regs (needed, live, x, final, insn);
2407 return;
2408 }
2409
2410 /* ... fall through ... */
2411
2412 case REG:
2413 /* See a register other than being set
2414 => mark it as needed. */
2415
2416 regno = REGNO (x);
2417 {
2418 int some_needed = REGNO_REG_SET_P (needed, regno);
2419 int some_not_needed = ! some_needed;
2420
2421 SET_REGNO_REG_SET (live, regno);
2422
2423 /* A hard reg in a wide mode may really be multiple registers.
2424 If so, mark all of them just like the first. */
2425 if (regno < FIRST_PSEUDO_REGISTER)
2426 {
2427 int n;
2428
2429 /* For stack ptr or fixed arg pointer,
2430 nothing below can be necessary, so waste no more time. */
2431 if (regno == STACK_POINTER_REGNUM
2432 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2433 || regno == HARD_FRAME_POINTER_REGNUM
2434 #endif
2435 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2436 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2437 #endif
2438 || regno == FRAME_POINTER_REGNUM)
2439 {
2440 /* If this is a register we are going to try to eliminate,
2441 don't mark it live here. If we are successful in
2442 eliminating it, it need not be live unless it is used for
2443 pseudos, in which case it will have been set live when
2444 it was allocated to the pseudos. If the register will not
2445 be eliminated, reload will set it live at that point. */
2446
2447 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
2448 regs_ever_live[regno] = 1;
2449 return;
2450 }
2451 /* No death notes for global register variables;
2452 their values are live after this function exits. */
2453 if (global_regs[regno])
2454 {
2455 if (final)
2456 reg_next_use[regno] = insn;
2457 return;
2458 }
2459
2460 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2461 while (--n > 0)
2462 {
2463 int regno_n = regno + n;
2464 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
2465
2466 SET_REGNO_REG_SET (live, regno_n);
2467 some_needed |= needed_regno;
2468 some_not_needed |= ! needed_regno;
2469 }
2470 }
2471 if (final)
2472 {
2473 /* Record where each reg is used, so when the reg
2474 is set we know the next insn that uses it. */
2475
2476 reg_next_use[regno] = insn;
2477
2478 if (regno < FIRST_PSEUDO_REGISTER)
2479 {
2480 /* If a hard reg is being used,
2481 record that this function does use it. */
2482
2483 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
2484 if (i == 0)
2485 i = 1;
2486 do
2487 regs_ever_live[regno + --i] = 1;
2488 while (i > 0);
2489 }
2490 else
2491 {
2492 /* Keep track of which basic block each reg appears in. */
2493
2494 register int blocknum = BLOCK_NUM (insn);
2495
2496 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
2497 REG_BASIC_BLOCK (regno) = blocknum;
2498 else if (REG_BASIC_BLOCK (regno) != blocknum)
2499 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
2500
2501 /* Count (weighted) number of uses of each reg. */
2502
2503 REG_N_REFS (regno) += loop_depth;
2504 }
2505
2506 /* Record and count the insns in which a reg dies.
2507 If it is used in this insn and was dead below the insn
2508 then it dies in this insn. If it was set in this insn,
2509 we do not make a REG_DEAD note; likewise if we already
2510 made such a note. */
2511
2512 if (some_not_needed
2513 && ! dead_or_set_p (insn, x)
2514 #if 0
2515 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
2516 #endif
2517 )
2518 {
2519 /* Check for the case where the register dying partially
2520 overlaps the register set by this insn. */
2521 if (regno < FIRST_PSEUDO_REGISTER
2522 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
2523 {
2524 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
2525 while (--n >= 0)
2526 some_needed |= dead_or_set_regno_p (insn, regno + n);
2527 }
2528
2529 /* If none of the words in X is needed, make a REG_DEAD
2530 note. Otherwise, we must make partial REG_DEAD notes. */
2531 if (! some_needed)
2532 {
2533 REG_NOTES (insn)
2534 = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (insn));
2535 REG_N_DEATHS (regno)++;
2536 }
2537 else
2538 {
2539 int i;
2540
2541 /* Don't make a REG_DEAD note for a part of a register
2542 that is set in the insn. */
2543
2544 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
2545 i >= 0; i--)
2546 if (!REGNO_REG_SET_P (needed, regno + i)
2547 && ! dead_or_set_regno_p (insn, regno + i))
2548 REG_NOTES (insn)
2549 = gen_rtx (EXPR_LIST, REG_DEAD,
2550 gen_rtx (REG, reg_raw_mode[regno + i],
2551 regno + i),
2552 REG_NOTES (insn));
2553 }
2554 }
2555 }
2556 }
2557 return;
2558
2559 case SET:
2560 {
2561 register rtx testreg = SET_DEST (x);
2562 int mark_dest = 0;
2563
2564 /* If storing into MEM, don't show it as being used. But do
2565 show the address as being used. */
2566 if (GET_CODE (testreg) == MEM)
2567 {
2568 #ifdef AUTO_INC_DEC
2569 if (final)
2570 find_auto_inc (needed, testreg, insn);
2571 #endif
2572 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
2573 mark_used_regs (needed, live, SET_SRC (x), final, insn);
2574 return;
2575 }
2576
2577 /* Storing in STRICT_LOW_PART is like storing in a reg
2578 in that this SET might be dead, so ignore it in TESTREG.
2579 but in some other ways it is like using the reg.
2580
2581 Storing in a SUBREG or a bit field is like storing the entire
2582 register in that if the register's value is not used
2583 then this SET is not needed. */
2584 while (GET_CODE (testreg) == STRICT_LOW_PART
2585 || GET_CODE (testreg) == ZERO_EXTRACT
2586 || GET_CODE (testreg) == SIGN_EXTRACT
2587 || GET_CODE (testreg) == SUBREG)
2588 {
2589 if (GET_CODE (testreg) == SUBREG
2590 && GET_CODE (SUBREG_REG (testreg)) == REG
2591 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
2592 && (GET_MODE_SIZE (GET_MODE (testreg))
2593 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
2594 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
2595
2596 /* Modifying a single register in an alternate mode
2597 does not use any of the old value. But these other
2598 ways of storing in a register do use the old value. */
2599 if (GET_CODE (testreg) == SUBREG
2600 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
2601 ;
2602 else
2603 mark_dest = 1;
2604
2605 testreg = XEXP (testreg, 0);
2606 }
2607
2608 /* If this is a store into a register,
2609 recursively scan the value being stored. */
2610
2611 if (GET_CODE (testreg) == REG
2612 && (regno = REGNO (testreg), regno != FRAME_POINTER_REGNUM)
2613 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2614 && regno != HARD_FRAME_POINTER_REGNUM
2615 #endif
2616 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2617 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2618 #endif
2619 )
2620 /* We used to exclude global_regs here, but that seems wrong.
2621 Storing in them is like storing in mem. */
2622 {
2623 mark_used_regs (needed, live, SET_SRC (x), final, insn);
2624 if (mark_dest)
2625 mark_used_regs (needed, live, SET_DEST (x), final, insn);
2626 return;
2627 }
2628 }
2629 break;
2630
2631 case RETURN:
2632 /* If exiting needs the right stack value, consider this insn as
2633 using the stack pointer. In any event, consider it as using
2634 all global registers and all registers used by return. */
2635
2636 #ifdef EXIT_IGNORE_STACK
2637 if (! EXIT_IGNORE_STACK
2638 || (! FRAME_POINTER_REQUIRED && flag_omit_frame_pointer))
2639 #endif
2640 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
2641
2642 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2643 if (global_regs[i]
2644 #ifdef EPILOGUE_USES
2645 || EPILOGUE_USES (i)
2646 #endif
2647 )
2648 SET_REGNO_REG_SET (live, i);
2649 break;
2650
2651 default:
2652 break;
2653 }
2654
2655 /* Recursively scan the operands of this expression. */
2656
2657 {
2658 register char *fmt = GET_RTX_FORMAT (code);
2659 register int i;
2660
2661 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2662 {
2663 if (fmt[i] == 'e')
2664 {
2665 /* Tail recursive case: save a function call level. */
2666 if (i == 0)
2667 {
2668 x = XEXP (x, 0);
2669 goto retry;
2670 }
2671 mark_used_regs (needed, live, XEXP (x, i), final, insn);
2672 }
2673 else if (fmt[i] == 'E')
2674 {
2675 register int j;
2676 for (j = 0; j < XVECLEN (x, i); j++)
2677 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
2678 }
2679 }
2680 }
2681 }
2682 \f
2683 #ifdef AUTO_INC_DEC
2684
2685 static int
2686 try_pre_increment_1 (insn)
2687 rtx insn;
2688 {
2689 /* Find the next use of this reg. If in same basic block,
2690 make it do pre-increment or pre-decrement if appropriate. */
2691 rtx x = single_set (insn);
2692 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
2693 * INTVAL (XEXP (SET_SRC (x), 1)));
2694 int regno = REGNO (SET_DEST (x));
2695 rtx y = reg_next_use[regno];
2696 if (y != 0
2697 && BLOCK_NUM (y) == BLOCK_NUM (insn)
2698 /* Don't do this if the reg dies, or gets set in y; a standard addressing
2699 mode would be better. */
2700 && ! dead_or_set_p (y, SET_DEST (x))
2701 && try_pre_increment (y, SET_DEST (x), amount))
2702 {
2703 /* We have found a suitable auto-increment
2704 and already changed insn Y to do it.
2705 So flush this increment-instruction. */
2706 PUT_CODE (insn, NOTE);
2707 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2708 NOTE_SOURCE_FILE (insn) = 0;
2709 /* Count a reference to this reg for the increment
2710 insn we are deleting. When a reg is incremented.
2711 spilling it is worse, so we want to make that
2712 less likely. */
2713 if (regno >= FIRST_PSEUDO_REGISTER)
2714 {
2715 REG_N_REFS (regno) += loop_depth;
2716 REG_N_SETS (regno)++;
2717 }
2718 return 1;
2719 }
2720 return 0;
2721 }
2722
2723 /* Try to change INSN so that it does pre-increment or pre-decrement
2724 addressing on register REG in order to add AMOUNT to REG.
2725 AMOUNT is negative for pre-decrement.
2726 Returns 1 if the change could be made.
2727 This checks all about the validity of the result of modifying INSN. */
2728
2729 static int
2730 try_pre_increment (insn, reg, amount)
2731 rtx insn, reg;
2732 HOST_WIDE_INT amount;
2733 {
2734 register rtx use;
2735
2736 /* Nonzero if we can try to make a pre-increment or pre-decrement.
2737 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
2738 int pre_ok = 0;
2739 /* Nonzero if we can try to make a post-increment or post-decrement.
2740 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
2741 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
2742 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
2743 int post_ok = 0;
2744
2745 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
2746 int do_post = 0;
2747
2748 /* From the sign of increment, see which possibilities are conceivable
2749 on this target machine. */
2750 #ifdef HAVE_PRE_INCREMENT
2751 if (amount > 0)
2752 pre_ok = 1;
2753 #endif
2754 #ifdef HAVE_POST_INCREMENT
2755 if (amount > 0)
2756 post_ok = 1;
2757 #endif
2758
2759 #ifdef HAVE_PRE_DECREMENT
2760 if (amount < 0)
2761 pre_ok = 1;
2762 #endif
2763 #ifdef HAVE_POST_DECREMENT
2764 if (amount < 0)
2765 post_ok = 1;
2766 #endif
2767
2768 if (! (pre_ok || post_ok))
2769 return 0;
2770
2771 /* It is not safe to add a side effect to a jump insn
2772 because if the incremented register is spilled and must be reloaded
2773 there would be no way to store the incremented value back in memory. */
2774
2775 if (GET_CODE (insn) == JUMP_INSN)
2776 return 0;
2777
2778 use = 0;
2779 if (pre_ok)
2780 use = find_use_as_address (PATTERN (insn), reg, 0);
2781 if (post_ok && (use == 0 || use == (rtx) 1))
2782 {
2783 use = find_use_as_address (PATTERN (insn), reg, -amount);
2784 do_post = 1;
2785 }
2786
2787 if (use == 0 || use == (rtx) 1)
2788 return 0;
2789
2790 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
2791 return 0;
2792
2793 /* See if this combination of instruction and addressing mode exists. */
2794 if (! validate_change (insn, &XEXP (use, 0),
2795 gen_rtx (amount > 0
2796 ? (do_post ? POST_INC : PRE_INC)
2797 : (do_post ? POST_DEC : PRE_DEC),
2798 Pmode, reg), 0))
2799 return 0;
2800
2801 /* Record that this insn now has an implicit side effect on X. */
2802 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_INC, reg, REG_NOTES (insn));
2803 return 1;
2804 }
2805
2806 #endif /* AUTO_INC_DEC */
2807 \f
2808 /* Find the place in the rtx X where REG is used as a memory address.
2809 Return the MEM rtx that so uses it.
2810 If PLUSCONST is nonzero, search instead for a memory address equivalent to
2811 (plus REG (const_int PLUSCONST)).
2812
2813 If such an address does not appear, return 0.
2814 If REG appears more than once, or is used other than in such an address,
2815 return (rtx)1. */
2816
2817 rtx
2818 find_use_as_address (x, reg, plusconst)
2819 register rtx x;
2820 rtx reg;
2821 HOST_WIDE_INT plusconst;
2822 {
2823 enum rtx_code code = GET_CODE (x);
2824 char *fmt = GET_RTX_FORMAT (code);
2825 register int i;
2826 register rtx value = 0;
2827 register rtx tem;
2828
2829 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
2830 return x;
2831
2832 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
2833 && XEXP (XEXP (x, 0), 0) == reg
2834 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2835 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
2836 return x;
2837
2838 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
2839 {
2840 /* If REG occurs inside a MEM used in a bit-field reference,
2841 that is unacceptable. */
2842 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
2843 return (rtx) (HOST_WIDE_INT) 1;
2844 }
2845
2846 if (x == reg)
2847 return (rtx) (HOST_WIDE_INT) 1;
2848
2849 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2850 {
2851 if (fmt[i] == 'e')
2852 {
2853 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
2854 if (value == 0)
2855 value = tem;
2856 else if (tem != 0)
2857 return (rtx) (HOST_WIDE_INT) 1;
2858 }
2859 if (fmt[i] == 'E')
2860 {
2861 register int j;
2862 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2863 {
2864 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
2865 if (value == 0)
2866 value = tem;
2867 else if (tem != 0)
2868 return (rtx) (HOST_WIDE_INT) 1;
2869 }
2870 }
2871 }
2872
2873 return value;
2874 }
2875 \f
2876 /* Write information about registers and basic blocks into FILE.
2877 This is part of making a debugging dump. */
2878
2879 void
2880 dump_flow_info (file)
2881 FILE *file;
2882 {
2883 register int i;
2884 static char *reg_class_names[] = REG_CLASS_NAMES;
2885
2886 fprintf (file, "%d registers.\n", max_regno);
2887
2888 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
2889 if (REG_N_REFS (i))
2890 {
2891 enum reg_class class, altclass;
2892 fprintf (file, "\nRegister %d used %d times across %d insns",
2893 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
2894 if (REG_BASIC_BLOCK (i) >= 0)
2895 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
2896 if (REG_N_DEATHS (i) != 1)
2897 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
2898 if (REG_N_CALLS_CROSSED (i) == 1)
2899 fprintf (file, "; crosses 1 call");
2900 else if (REG_N_CALLS_CROSSED (i))
2901 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
2902 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
2903 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
2904 class = reg_preferred_class (i);
2905 altclass = reg_alternate_class (i);
2906 if (class != GENERAL_REGS || altclass != ALL_REGS)
2907 {
2908 if (altclass == ALL_REGS || class == ALL_REGS)
2909 fprintf (file, "; pref %s", reg_class_names[(int) class]);
2910 else if (altclass == NO_REGS)
2911 fprintf (file, "; %s or none", reg_class_names[(int) class]);
2912 else
2913 fprintf (file, "; pref %s, else %s",
2914 reg_class_names[(int) class],
2915 reg_class_names[(int) altclass]);
2916 }
2917 if (REGNO_POINTER_FLAG (i))
2918 fprintf (file, "; pointer");
2919 fprintf (file, ".\n");
2920 }
2921 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
2922 for (i = 0; i < n_basic_blocks; i++)
2923 {
2924 register rtx head, jump;
2925 register int regno;
2926 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
2927 i,
2928 INSN_UID (basic_block_head[i]),
2929 INSN_UID (basic_block_end[i]));
2930 /* The control flow graph's storage is freed
2931 now when flow_analysis returns.
2932 Don't try to print it if it is gone. */
2933 if (basic_block_drops_in)
2934 {
2935 fprintf (file, "Reached from blocks: ");
2936 head = basic_block_head[i];
2937 if (GET_CODE (head) == CODE_LABEL)
2938 for (jump = LABEL_REFS (head);
2939 jump != head;
2940 jump = LABEL_NEXTREF (jump))
2941 {
2942 register int from_block = BLOCK_NUM (CONTAINING_INSN (jump));
2943 fprintf (file, " %d", from_block);
2944 }
2945 if (basic_block_drops_in[i])
2946 fprintf (file, " previous");
2947 }
2948 fprintf (file, "\nRegisters live at start:");
2949 for (regno = 0; regno < max_regno; regno++)
2950 if (REGNO_REG_SET_P (basic_block_live_at_start[i], regno))
2951 fprintf (file, " %d", regno);
2952 fprintf (file, "\n");
2953 }
2954 fprintf (file, "\n");
2955 }
2956
2957 \f
2958 /* Like print_rtl, but also print out live information for the start of each
2959 basic block. */
2960
2961 void
2962 print_rtl_with_bb (outf, rtx_first)
2963 FILE *outf;
2964 rtx rtx_first;
2965 {
2966 register rtx tmp_rtx;
2967
2968 if (rtx_first == 0)
2969 fprintf (outf, "(nil)\n");
2970
2971 else
2972 {
2973 int i, bb;
2974 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
2975 int max_uid = get_max_uid ();
2976 int *start = (int *) alloca (max_uid * sizeof (int));
2977 int *end = (int *) alloca (max_uid * sizeof (int));
2978 char *in_bb_p = (char *) alloca (max_uid * sizeof (enum bb_state));
2979
2980 for (i = 0; i < max_uid; i++)
2981 {
2982 start[i] = end[i] = -1;
2983 in_bb_p[i] = NOT_IN_BB;
2984 }
2985
2986 for (i = n_basic_blocks-1; i >= 0; i--)
2987 {
2988 rtx x;
2989 start[INSN_UID (basic_block_head[i])] = i;
2990 end[INSN_UID (basic_block_end[i])] = i;
2991 for (x = basic_block_head[i]; x != NULL_RTX; x = NEXT_INSN (x))
2992 {
2993 in_bb_p[ INSN_UID(x)]
2994 = (in_bb_p[ INSN_UID(x)] == NOT_IN_BB)
2995 ? IN_ONE_BB : IN_MULTIPLE_BB;
2996 if (x == basic_block_end[i])
2997 break;
2998 }
2999 }
3000
3001 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
3002 {
3003 if ((bb = start[INSN_UID (tmp_rtx)]) >= 0)
3004 {
3005 fprintf (outf, ";; Start of basic block %d, registers live:",
3006 bb);
3007
3008 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
3009 {
3010 fprintf (outf, " %d", i);
3011 if (i < FIRST_PSEUDO_REGISTER)
3012 fprintf (outf, " [%s]",
3013 reg_names[i]);
3014 });
3015 putc ('\n', outf);
3016 }
3017
3018 if (in_bb_p[ INSN_UID(tmp_rtx)] == NOT_IN_BB
3019 && GET_CODE (tmp_rtx) != NOTE
3020 && GET_CODE (tmp_rtx) != BARRIER)
3021 fprintf (outf, ";; Insn is not within a basic block\n");
3022 else if (in_bb_p[ INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
3023 fprintf (outf, ";; Insn is in multiple basic blocks\n");
3024
3025 print_rtl_single (outf, tmp_rtx);
3026
3027 if ((bb = end[INSN_UID (tmp_rtx)]) >= 0)
3028 fprintf (outf, ";; End of basic block %d\n", bb);
3029
3030 putc ('\n', outf);
3031 }
3032 }
3033 }
This page took 0.186268 seconds and 6 git commands to generate.