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