]> gcc.gnu.org Git - gcc.git/blame - gcc/flow.c
Udo previous delta
[gcc.git] / gcc / flow.c
CommitLineData
d7429b6a 1/* Data flow analysis for GNU compiler.
a5cad800 2 Copyright (C) 1987, 88, 92-98, 1999 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
e881bb1b
RH
22/* This file contains the data flow analysis pass of the compiler. It
23 computes data flow information which tells combine_instructions
24 which insns to consider combining and controls register allocation.
d7429b6a 25
e881bb1b
RH
26 Additional data flow information that is too bulky to record is
27 generated during the analysis, and is used at that time to create
28 autoincrement and autodecrement addressing.
d7429b6a
RK
29
30 The first step is dividing the function into basic blocks.
31 find_basic_blocks does this. Then life_analysis determines
32 where each register is live and where it is dead.
33
34 ** find_basic_blocks **
35
e881bb1b
RH
36 find_basic_blocks divides the current function's rtl into basic
37 blocks and constructs the CFG. The blocks are recorded in the
38 basic_block_info array; the CFG exists in the edge structures
39 referenced by the blocks.
d7429b6a 40
e881bb1b 41 find_basic_blocks also finds any unreachable loops and deletes them.
d7429b6a
RK
42
43 ** life_analysis **
44
45 life_analysis is called immediately after find_basic_blocks.
46 It uses the basic block information to determine where each
47 hard or pseudo register is live.
48
49 ** live-register info **
50
51 The information about where each register is live is in two parts:
e881bb1b 52 the REG_NOTES of insns, and the vector basic_block->global_live_at_start.
d7429b6a 53
e881bb1b
RH
54 basic_block->global_live_at_start has an element for each basic
55 block, and the element is a bit-vector with a bit for each hard or
56 pseudo register. The bit is 1 if the register is live at the
57 beginning of the basic block.
d7429b6a
RK
58
59 Two types of elements can be added to an insn's REG_NOTES.
60 A REG_DEAD note is added to an insn's REG_NOTES for any register
61 that meets both of two conditions: The value in the register is not
62 needed in subsequent insns and the insn does not replace the value in
63 the register (in the case of multi-word hard registers, the value in
64 each register must be replaced by the insn to avoid a REG_DEAD note).
65
66 In the vast majority of cases, an object in a REG_DEAD note will be
67 used somewhere in the insn. The (rare) exception to this is if an
68 insn uses a multi-word hard register and only some of the registers are
69 needed in subsequent insns. In that case, REG_DEAD notes will be
70 provided for those hard registers that are not subsequently needed.
71 Partial REG_DEAD notes of this type do not occur when an insn sets
72 only some of the hard registers used in such a multi-word operand;
73 omitting REG_DEAD notes for objects stored in an insn is optional and
74 the desire to do so does not justify the complexity of the partial
75 REG_DEAD notes.
76
77 REG_UNUSED notes are added for each register that is set by the insn
78 but is unused subsequently (if every register set by the insn is unused
79 and the insn does not reference memory or have some other side-effect,
80 the insn is deleted instead). If only part of a multi-word hard
81 register is used in a subsequent insn, REG_UNUSED notes are made for
82 the parts that will not be used.
83
84 To determine which registers are live after any insn, one can
85 start from the beginning of the basic block and scan insns, noting
86 which registers are set by each insn and which die there.
87
88 ** Other actions of life_analysis **
89
90 life_analysis sets up the LOG_LINKS fields of insns because the
91 information needed to do so is readily available.
92
93 life_analysis deletes insns whose only effect is to store a value
94 that is never used.
95
96 life_analysis notices cases where a reference to a register as
97 a memory address can be combined with a preceding or following
98 incrementation or decrementation of the register. The separate
99 instruction to increment or decrement is deleted and the address
100 is changed to a POST_INC or similar rtx.
101
102 Each time an incrementing or decrementing address is created,
103 a REG_INC element is added to the insn's REG_NOTES list.
104
105 life_analysis fills in certain vectors containing information about
106 register usage: reg_n_refs, reg_n_deaths, reg_n_sets, reg_live_length,
fdb8a883
JW
107 reg_n_calls_crosses and reg_basic_block.
108
109 life_analysis sets current_function_sp_is_unchanging if the function
110 doesn't modify the stack pointer. */
e881bb1b
RH
111
112/* TODO:
113
114 Split out from life_analysis:
115 - local property discovery (bb->local_live, bb->local_set)
116 - global property computation
117 - log links creation
118 - pre/post modify transformation
119*/
d7429b6a 120\f
d7429b6a 121#include "config.h"
670ee920 122#include "system.h"
d7429b6a 123#include "rtl.h"
6baf1cc8 124#include "tm_p.h"
d7429b6a
RK
125#include "basic-block.h"
126#include "insn-config.h"
127#include "regs.h"
128#include "hard-reg-set.h"
129#include "flags.h"
130#include "output.h"
b384405b 131#include "function.h"
3d195391 132#include "except.h"
2e107e9e 133#include "toplev.h"
79c9824e 134#include "recog.h"
e881bb1b 135#include "insn-flags.h"
d7429b6a
RK
136
137#include "obstack.h"
c5c76735 138
d7429b6a
RK
139#define obstack_chunk_alloc xmalloc
140#define obstack_chunk_free free
141
e881bb1b
RH
142
143/* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
144 the stack pointer does not matter. The value is tested only in
145 functions that have frame pointers.
146 No definition is equivalent to always zero. */
147#ifndef EXIT_IGNORE_STACK
148#define EXIT_IGNORE_STACK 0
149#endif
150
421382ac 151
7eb136d6
MM
152/* The contents of the current function definition are allocated
153 in this obstack, and all are freed at the end of the function.
154 For top-level functions, this is temporary_obstack.
155 Separate obstacks are made for nested functions. */
156
157extern struct obstack *function_obstack;
158
e881bb1b 159/* Number of basic blocks in the current function. */
d7429b6a 160
e881bb1b 161int n_basic_blocks;
d7429b6a 162
e881bb1b 163/* The basic block array. */
d7429b6a 164
e881bb1b 165varray_type basic_block_info;
d7429b6a 166
e881bb1b 167/* The special entry and exit blocks. */
d7429b6a 168
e881bb1b
RH
169struct basic_block_def entry_exit_blocks[2] =
170{
171 {
172 NULL, /* head */
173 NULL, /* end */
174 NULL, /* pred */
175 NULL, /* succ */
176 NULL, /* local_set */
177 NULL, /* global_live_at_start */
178 NULL, /* global_live_at_end */
179 NULL, /* aux */
180 ENTRY_BLOCK, /* index */
336a6399
RH
181 0, /* loop_depth */
182 -1, -1 /* eh_beg, eh_end */
e881bb1b
RH
183 },
184 {
185 NULL, /* head */
186 NULL, /* end */
187 NULL, /* pred */
188 NULL, /* succ */
189 NULL, /* local_set */
190 NULL, /* global_live_at_start */
191 NULL, /* global_live_at_end */
192 NULL, /* aux */
193 EXIT_BLOCK, /* index */
336a6399
RH
194 0, /* loop_depth */
195 -1, -1 /* eh_beg, eh_end */
e881bb1b
RH
196 }
197};
d7429b6a 198
56744d1a
JL
199/* Nonzero if the second flow pass has completed. */
200int flow2_completed;
201
d7429b6a
RK
202/* Maximum register number used in this function, plus one. */
203
204int max_regno;
205
b1f21e0a 206/* Indexed by n, giving various register information */
d7429b6a 207
6feacd09 208varray_type reg_n_info;
d7429b6a 209
a494747c
MM
210/* Size of the reg_n_info table. */
211
212unsigned int reg_n_max;
213
d7429b6a
RK
214/* Element N is the next insn that uses (hard or pseudo) register number N
215 within the current basic block; or zero, if there is no such insn.
216 This is valid only during the final backward scan in propagate_block. */
217
218static rtx *reg_next_use;
219
220/* Size of a regset for the current function,
221 in (1) bytes and (2) elements. */
222
223int regset_bytes;
224int regset_size;
225
d7429b6a 226/* Regset of regs live when calls to `setjmp'-like functions happen. */
e881bb1b 227/* ??? Does this exist only for the setjmp-clobbered warning message? */
d7429b6a
RK
228
229regset regs_live_at_setjmp;
230
231/* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
232 that have to go in the same hard reg.
233 The first two regs in the list are a pair, and the next two
234 are another pair, etc. */
235rtx regs_may_share;
236
d7429b6a
RK
237/* Depth within loops of basic block being scanned for lifetime analysis,
238 plus one. This is the weight attached to references to registers. */
239
240static int loop_depth;
241
242/* During propagate_block, this is non-zero if the value of CC0 is live. */
243
244static int cc0_live;
245
db3a887b 246/* During propagate_block, this contains a list of all the MEMs we are
40b5a77c
JL
247 tracking for dead store elimination.
248
249 ?!? Note we leak memory by not free-ing items on this list. We need to
250 write some generic routines to operate on memory lists since cse, gcse,
251 loop, sched, flow and possibly other passes all need to do basically the
252 same operations on these lists. */
d7429b6a 253
db3a887b 254static rtx mem_set_list;
d7429b6a
RK
255
256/* Set of registers that may be eliminable. These are handled specially
257 in updating regs_ever_live. */
258
259static HARD_REG_SET elim_reg_set;
260
e881bb1b
RH
261/* The basic block structure for every insn, indexed by uid. */
262
263varray_type basic_block_for_insn;
264
265/* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
266/* ??? Should probably be using LABEL_NUSES instead. It would take a
267 bit of surgery to be able to use or co-opt the routines in jump. */
268
269static rtx label_value_list;
270
271/* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
272
273#define INSN_VOLATILE(INSN) bitmap_bit_p (uid_volatile, INSN_UID (INSN))
274#define SET_INSN_VOLATILE(INSN) bitmap_set_bit (uid_volatile, INSN_UID (INSN))
275static bitmap uid_volatile;
276
d7429b6a 277/* Forward declarations */
e881bb1b 278static int count_basic_blocks PROTO((rtx));
336a6399 279static rtx find_basic_blocks_1 PROTO((rtx));
e881bb1b 280static void create_basic_block PROTO((int, rtx, rtx, rtx));
e881bb1b 281static void clear_edges PROTO((void));
336a6399 282static void make_edges PROTO((rtx));
e881bb1b
RH
283static void make_edge PROTO((basic_block, basic_block, int));
284static void make_label_edge PROTO((basic_block, rtx, int));
336a6399
RH
285static void make_eh_edge PROTO((eh_nesting_info *, basic_block,
286 rtx, int));
e881bb1b 287static void mark_critical_edges PROTO((void));
336a6399
RH
288static void move_stray_eh_region_notes PROTO((void));
289static void record_active_eh_regions PROTO((rtx));
e881bb1b
RH
290
291static void commit_one_edge_insertion PROTO((edge));
292
421382ac 293static void delete_unreachable_blocks PROTO((void));
e881bb1b 294static void delete_eh_regions PROTO((void));
eeea333e 295static int can_delete_note_p PROTO((rtx));
5aabad00 296static void flow_delete_insn_chain PROTO((rtx, rtx));
e881bb1b
RH
297static int delete_block PROTO((basic_block));
298static void expunge_block PROTO((basic_block));
299static rtx flow_delete_insn PROTO((rtx));
300static int can_delete_label_p PROTO((rtx));
336a6399
RH
301static int merge_blocks_move_predecessor_nojumps PROTO((basic_block,
302 basic_block));
303static int merge_blocks_move_successor_nojumps PROTO((basic_block,
304 basic_block));
e881bb1b
RH
305static void merge_blocks_nomove PROTO((basic_block, basic_block));
306static int merge_blocks PROTO((edge,basic_block,basic_block));
336a6399 307static void try_merge_blocks PROTO((void));
e881bb1b
RH
308static void tidy_fallthru_edge PROTO((edge,basic_block,basic_block));
309static void calculate_loop_depth PROTO((rtx));
310
dc2ede84
BS
311static int set_noop_p PROTO((rtx));
312static int noop_move_p PROTO((rtx));
e881bb1b 313static void notice_stack_pointer_modification PROTO ((rtx, rtx));
dc2ede84
BS
314static void record_volatile_insns PROTO((rtx));
315static void mark_regs_live_at_end PROTO((regset));
11f246f6 316static void life_analysis_1 PROTO((rtx, int, int));
e881bb1b
RH
317static void init_regset_vector PROTO ((regset *, int,
318 struct obstack *));
319static void propagate_block PROTO((regset, rtx, rtx, int,
11f246f6 320 regset, int, int));
e398aa80 321static int insn_dead_p PROTO((rtx, regset, int, rtx));
e658434c
RK
322static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
323static void mark_set_regs PROTO((regset, regset, rtx,
324 rtx, regset));
325static void mark_set_1 PROTO((regset, regset, rtx,
326 rtx, regset));
1d300e19 327#ifdef AUTO_INC_DEC
e658434c 328static void find_auto_inc PROTO((regset, rtx, rtx));
e658434c
RK
329static int try_pre_increment_1 PROTO((rtx));
330static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
1d300e19
KG
331#endif
332static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
e658434c 333void dump_flow_info PROTO((FILE *));
e881bb1b
RH
334static void dump_edge_info PROTO((FILE *, edge, int));
335
5ece9746
JL
336static int_list_ptr alloc_int_list_node PROTO ((int_list_block **));
337static int_list_ptr add_int_list_node PROTO ((int_list_block **,
338 int_list **, int));
e881bb1b
RH
339
340static void add_pred_succ PROTO ((int, int, int_list_ptr *,
341 int_list_ptr *, int *, int *));
342
4c649323
JL
343static void count_reg_sets_1 PROTO ((rtx));
344static void count_reg_sets PROTO ((rtx));
345static void count_reg_references PROTO ((rtx));
fdb8a883 346static void notice_stack_pointer_modification PROTO ((rtx, rtx));
15e088b2 347static void invalidate_mems_from_autoinc PROTO ((rtx));
f2a1bc02
BM
348static void maybe_remove_dead_notes PROTO ((rtx, rtx, rtx, rtx,
349 rtx, rtx));
350static int maybe_add_dead_note_use PROTO ((rtx, rtx));
351static int maybe_add_dead_note PROTO ((rtx, rtx, rtx));
352static int sets_reg_or_subreg PROTO ((rtx, rtx));
353static void update_n_sets PROTO ((rtx, int));
354static void new_insn_dead_notes PROTO ((rtx, rtx, rtx, rtx, rtx, rtx));
34487bf8 355void verify_flow_info PROTO ((void));
d7429b6a 356\f
5ece9746 357/* Find basic blocks of the current function.
e881bb1b
RH
358 F is the first insn of the function and NREGS the number of register
359 numbers in use. */
d7429b6a
RK
360
361void
359da67d 362find_basic_blocks (f, nregs, file, do_cleanup)
d7429b6a 363 rtx f;
e881bb1b
RH
364 int nregs ATTRIBUTE_UNUSED;
365 FILE *file ATTRIBUTE_UNUSED;
359da67d 366 int do_cleanup;
d7429b6a 367{
e881bb1b 368 int max_uid;
d7429b6a 369
e881bb1b
RH
370 /* Flush out existing data. */
371 if (basic_block_info != NULL)
372 {
373 int i;
421382ac 374
e881bb1b 375 clear_edges ();
d7429b6a 376
e881bb1b
RH
377 /* Clear bb->aux on all extant basic blocks. We'll use this as a
378 tag for reuse during create_basic_block, just in case some pass
379 copies around basic block notes improperly. */
380 for (i = 0; i < n_basic_blocks; ++i)
381 BASIC_BLOCK (i)->aux = NULL;
d7429b6a 382
e881bb1b
RH
383 VARRAY_FREE (basic_block_info);
384 }
27249135 385
e881bb1b 386 n_basic_blocks = count_basic_blocks (f);
27249135 387
e881bb1b
RH
388 /* Size the basic block table. The actual structures will be allocated
389 by find_basic_blocks_1, since we want to keep the structure pointers
390 stable across calls to find_basic_blocks. */
391 /* ??? This whole issue would be much simpler if we called find_basic_blocks
392 exactly once, and thereafter we don't have a single long chain of
393 instructions at all until close to the end of compilation when we
394 actually lay them out. */
8cfe18d6 395
e881bb1b
RH
396 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
397
336a6399 398 label_value_list = find_basic_blocks_1 (f);
088e7160 399
e881bb1b
RH
400 /* Record the block to which an insn belongs. */
401 /* ??? This should be done another way, by which (perhaps) a label is
402 tagged directly with the basic block that it starts. It is used for
403 more than that currently, but IMO that is the only valid use. */
404
405 max_uid = get_max_uid ();
d7429b6a 406#ifdef AUTO_INC_DEC
5ece9746
JL
407 /* Leave space for insns life_analysis makes in some cases for auto-inc.
408 These cases are rare, so we don't need too much space. */
e881bb1b 409 max_uid += max_uid / 10;
d7429b6a
RK
410#endif
411
2307e372 412 compute_bb_for_insn (max_uid);
e881bb1b
RH
413
414 /* Discover the edges of our cfg. */
d7429b6a 415
336a6399
RH
416 record_active_eh_regions (f);
417 make_edges (label_value_list);
421382ac 418
336a6399 419 /* Delete unreachable blocks, then merge blocks when possible. */
d7429b6a 420
359da67d 421 if (do_cleanup)
336a6399
RH
422 {
423 delete_unreachable_blocks ();
424 move_stray_eh_region_notes ();
425 record_active_eh_regions (f);
426 try_merge_blocks ();
427 }
e881bb1b
RH
428
429 /* Mark critical edges. */
430
431 mark_critical_edges ();
432
433 /* Discover the loop depth at the start of each basic block to aid
434 register allocation. */
435 calculate_loop_depth (f);
436
437 /* Kill the data we won't maintain. */
438 label_value_list = 0;
34487bf8
RH
439
440#ifdef ENABLE_CHECKING
441 verify_flow_info ();
442#endif
d7429b6a 443}
5ece9746 444
e881bb1b 445/* Count the basic blocks of the function. */
dc2ede84 446
e881bb1b
RH
447static int
448count_basic_blocks (f)
449 rtx f;
450{
451 register rtx insn;
452 register RTX_CODE prev_code;
453 register int count = 0;
454 int eh_region = 0;
e881bb1b
RH
455 int call_had_abnormal_edge = 0;
456 rtx prev_call = NULL_RTX;
dc2ede84 457
e881bb1b
RH
458 prev_code = JUMP_INSN;
459 for (insn = f; insn; insn = NEXT_INSN (insn))
460 {
461 register RTX_CODE code = GET_CODE (insn);
462
e881bb1b
RH
463 if (code == CODE_LABEL
464 || (GET_RTX_CLASS (code) == 'i'
465 && (prev_code == JUMP_INSN
466 || prev_code == BARRIER
467 || (prev_code == CALL_INSN && call_had_abnormal_edge))))
468 {
469 count++;
dc2ede84 470
e881bb1b
RH
471 /* If the previous insn was a call that did not create an
472 abnormal edge, we want to add a nop so that the CALL_INSN
473 itself is not at basic_block_end. This allows us to
474 easily distinguish between normal calls and those which
475 create abnormal edges in the flow graph. */
dc2ede84 476
e881bb1b
RH
477 if (count > 0 && prev_call != 0 && !call_had_abnormal_edge)
478 {
479 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
480 emit_insn_after (nop, prev_call);
481 }
482 }
dc2ede84 483
e881bb1b
RH
484 /* Record whether this call created an edge. */
485 if (code == CALL_INSN)
486 {
6af57aae 487 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
ef178af3 488 int region = (note ? XWINT (XEXP (note, 0), 0) : 1);
e881bb1b
RH
489 prev_call = insn;
490 call_had_abnormal_edge = 0;
6af57aae
AM
491
492 /* If there is a specified EH region, we have an edge. */
493 if (eh_region && region > 0)
494 call_had_abnormal_edge = 1;
495 else
e881bb1b 496 {
6af57aae
AM
497 /* If there is a nonlocal goto label and the specified
498 region number isn't -1, we have an edge. (0 means
499 no throw, but might have a nonlocal goto). */
500 if (nonlocal_goto_handler_labels && region >= 0)
e881bb1b
RH
501 call_had_abnormal_edge = 1;
502 }
503 }
504 else if (code != NOTE)
505 prev_call = NULL_RTX;
506
507 if (code != NOTE)
508 prev_code = code;
509 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
510 ++eh_region;
511 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
512 --eh_region;
513
e881bb1b
RH
514 }
515
516 /* The rest of the compiler works a bit smoother when we don't have to
517 check for the edge case of do-nothing functions with no basic blocks. */
518 if (count == 0)
519 {
520 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
521 count = 1;
522 }
523
524 return count;
525}
dc2ede84 526
d7429b6a 527/* Find all basic blocks of the function whose first insn is F.
d7429b6a 528
336a6399
RH
529 Collect and return a list of labels whose addresses are taken. This
530 will be used in make_edges for use with computed gotos. */
8329b5ec 531
e881bb1b 532static rtx
336a6399 533find_basic_blocks_1 (f)
e881bb1b 534 rtx f;
e881bb1b
RH
535{
536 register rtx insn, next;
e881bb1b
RH
537 int call_has_abnormal_edge = 0;
538 int i = 0;
539 rtx bb_note = NULL_RTX;
540 rtx eh_list = NULL_RTX;
541 rtx label_value_list = NULL_RTX;
542 rtx head = NULL_RTX;
543 rtx end = NULL_RTX;
544
545 /* We process the instructions in a slightly different way than we did
546 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
547 closed out the previous block, so that it gets attached at the proper
548 place. Since this form should be equivalent to the previous,
336a6399 549 count_basic_blocks continues to use the old form as a check. */
d7429b6a 550
e881bb1b
RH
551 for (insn = f; insn; insn = next)
552 {
553 enum rtx_code code = GET_CODE (insn);
d7429b6a 554
e881bb1b 555 next = NEXT_INSN (insn);
d7429b6a 556
e881bb1b 557 if (code == CALL_INSN)
e658434c 558 {
e881bb1b 559 /* Record whether this call created an edge. */
6af57aae 560 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
ef178af3 561 int region = (note ? XWINT (XEXP (note, 0), 0) : 1);
e881bb1b 562 call_has_abnormal_edge = 0;
6af57aae
AM
563
564 /* If there is an EH region, we have an edge. */
565 if (eh_list && region > 0)
566 call_has_abnormal_edge = 1;
567 else
e658434c 568 {
6af57aae
AM
569 /* If there is a nonlocal goto label and the specified
570 region number isn't -1, we have an edge. (0 means
571 no throw, but might have a nonlocal goto). */
572 if (nonlocal_goto_handler_labels && region >= 0)
e881bb1b 573 call_has_abnormal_edge = 1;
5c35539b 574 }
e658434c 575 }
d7429b6a 576
e881bb1b 577 switch (code)
e658434c 578 {
e881bb1b
RH
579 case NOTE:
580 {
581 int kind = NOTE_LINE_NUMBER (insn);
582
583 /* Keep a LIFO list of the currently active exception notes. */
584 if (kind == NOTE_INSN_EH_REGION_BEG)
336a6399 585 eh_list = alloc_INSN_LIST (insn, eh_list);
e881bb1b 586 else if (kind == NOTE_INSN_EH_REGION_END)
336a6399
RH
587 {
588 rtx t = eh_list;
589 eh_list = XEXP (eh_list, 1);
590 free_INSN_LIST_node (t);
591 }
e881bb1b
RH
592
593 /* Look for basic block notes with which to keep the
594 basic_block_info pointers stable. Unthread the note now;
595 we'll put it back at the right place in create_basic_block.
596 Or not at all if we've already found a note in this block. */
597 else if (kind == NOTE_INSN_BASIC_BLOCK)
598 {
599 if (bb_note == NULL_RTX)
600 bb_note = insn;
601 next = flow_delete_insn (insn);
602 }
e658434c 603
e881bb1b
RH
604 break;
605 }
d7429b6a 606
e881bb1b
RH
607 case CODE_LABEL:
608 /* A basic block starts at a label. If we've closed one off due
609 to a barrier or some such, no need to do it again. */
610 if (head != NULL_RTX)
2ec1535d 611 {
e881bb1b
RH
612 /* While we now have edge lists with which other portions of
613 the compiler might determine a call ending a basic block
614 does not imply an abnormal edge, it will be a bit before
615 everything can be updated. So continue to emit a noop at
616 the end of such a block. */
617 if (GET_CODE (end) == CALL_INSN)
618 {
619 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
620 end = emit_insn_after (nop, end);
621 }
622
e881bb1b
RH
623 create_basic_block (i++, head, end, bb_note);
624 bb_note = NULL_RTX;
2ec1535d 625 }
e881bb1b
RH
626 head = end = insn;
627 break;
d06c6389 628
e881bb1b
RH
629 case JUMP_INSN:
630 /* A basic block ends at a jump. */
631 if (head == NULL_RTX)
632 head = insn;
633 else
634 {
635 /* ??? Make a special check for table jumps. The way this
636 happens is truely and amazingly gross. We are about to
637 create a basic block that contains just a code label and
638 an addr*vec jump insn. Worse, an addr_diff_vec creates
639 its own natural loop.
5c35539b 640
e881bb1b
RH
641 Prevent this bit of brain damage, pasting things together
642 correctly in make_edges.
2c3a56ad 643
e881bb1b
RH
644 The correct solution involves emitting the table directly
645 on the tablejump instruction as a note, or JUMP_LABEL. */
e658434c 646
e881bb1b
RH
647 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
648 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
649 {
650 head = end = NULL;
651 n_basic_blocks--;
652 break;
653 }
654 }
655 end = insn;
656 goto new_bb_inclusive;
d7429b6a 657
e881bb1b
RH
658 case BARRIER:
659 /* A basic block ends at a barrier. It may be that an unconditional
660 jump already closed the basic block -- no need to do it again. */
661 if (head == NULL_RTX)
662 break;
d7429b6a 663
e881bb1b
RH
664 /* While we now have edge lists with which other portions of the
665 compiler might determine a call ending a basic block does not
666 imply an abnormal edge, it will be a bit before everything can
667 be updated. So continue to emit a noop at the end of such a
668 block. */
669 if (GET_CODE (end) == CALL_INSN)
670 {
671 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
672 end = emit_insn_after (nop, end);
673 }
674 goto new_bb_exclusive;
675
676 case CALL_INSN:
677 /* A basic block ends at a call that can either throw or
678 do a non-local goto. */
679 if (call_has_abnormal_edge)
680 {
681 new_bb_inclusive:
682 if (head == NULL_RTX)
683 head = insn;
684 end = insn;
685
686 new_bb_exclusive:
e881bb1b
RH
687 create_basic_block (i++, head, end, bb_note);
688 head = end = NULL_RTX;
689 bb_note = NULL_RTX;
690 break;
691 }
692 /* FALLTHRU */
d7429b6a 693
e881bb1b
RH
694 default:
695 if (GET_RTX_CLASS (code) == 'i')
696 {
697 if (head == NULL_RTX)
698 head = insn;
699 end = insn;
700 }
701 break;
702 }
d7429b6a 703
e881bb1b 704 if (GET_RTX_CLASS (code) == 'i')
d7429b6a 705 {
e881bb1b 706 rtx note;
421382ac 707
e881bb1b
RH
708 /* Make a list of all labels referred to other than by jumps
709 (which just don't have the REG_LABEL notes).
2ec1535d 710
e881bb1b
RH
711 Make a special exception for labels followed by an ADDR*VEC,
712 as this would be a part of the tablejump setup code.
421382ac 713
e881bb1b
RH
714 Make a special exception for the eh_return_stub_label, which
715 we know isn't part of any otherwise visible control flow. */
716
717 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
718 if (REG_NOTE_KIND (note) == REG_LABEL)
719 {
720 rtx lab = XEXP (note, 0), next;
721
722 if (lab == eh_return_stub_label)
723 ;
724 else if ((next = next_nonnote_insn (lab)) != NULL
725 && GET_CODE (next) == JUMP_INSN
726 && (GET_CODE (PATTERN (next)) == ADDR_VEC
727 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
728 ;
729 else
730 label_value_list
731 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
732 label_value_list);
d7429b6a
RK
733 }
734 }
e881bb1b 735 }
d7429b6a 736
e881bb1b 737 if (head != NULL_RTX)
336a6399 738 create_basic_block (i++, head, end, bb_note);
af14ce9c 739
e881bb1b
RH
740 if (i != n_basic_blocks)
741 abort ();
af14ce9c 742
e881bb1b 743 return label_value_list;
d7429b6a 744}
5ece9746 745
e881bb1b
RH
746/* Create a new basic block consisting of the instructions between
747 HEAD and END inclusive. Reuses the note and basic block struct
748 in BB_NOTE, if any. */
5ece9746 749
e881bb1b
RH
750static void
751create_basic_block (index, head, end, bb_note)
752 int index;
753 rtx head, end, bb_note;
5ece9746 754{
e881bb1b
RH
755 basic_block bb;
756
757 if (bb_note
b3bf5bde 758 && ! RTX_INTEGRATED_P (bb_note)
e881bb1b
RH
759 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
760 && bb->aux == NULL)
5ece9746 761 {
e881bb1b
RH
762 /* If we found an existing note, thread it back onto the chain. */
763
764 if (GET_CODE (head) == CODE_LABEL)
765 add_insn_after (bb_note, head);
766 else
767 {
768 add_insn_before (bb_note, head);
769 head = bb_note;
770 }
5ece9746 771 }
e881bb1b
RH
772 else
773 {
774 /* Otherwise we must create a note and a basic block structure.
775 Since we allow basic block structs in rtl, give the struct
776 the same lifetime by allocating it off the function obstack
777 rather than using malloc. */
8329b5ec 778
e881bb1b
RH
779 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
780 memset (bb, 0, sizeof (*bb));
421382ac 781
e881bb1b
RH
782 if (GET_CODE (head) == CODE_LABEL)
783 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
784 else
785 {
786 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
787 head = bb_note;
788 }
789 NOTE_BASIC_BLOCK (bb_note) = bb;
790 }
791
eeea333e
RH
792 /* Always include the bb note in the block. */
793 if (NEXT_INSN (end) == bb_note)
794 end = bb_note;
795
e881bb1b
RH
796 bb->head = head;
797 bb->end = end;
798 bb->index = index;
799 BASIC_BLOCK (index) = bb;
800
801 /* Tag the block so that we know it has been used when considering
802 other basic block notes. */
803 bb->aux = bb;
421382ac 804}
e881bb1b
RH
805\f
806/* Records the basic block struct in BB_FOR_INSN, for every instruction
807 indexed by INSN_UID. MAX is the size of the array. */
421382ac 808
2307e372
RH
809void
810compute_bb_for_insn (max)
e881bb1b 811 int max;
421382ac 812{
e881bb1b 813 int i;
421382ac 814
2307e372
RH
815 VARRAY_BB_INIT (basic_block_for_insn, max, "basic_block_for_insn");
816
e881bb1b
RH
817 for (i = 0; i < n_basic_blocks; ++i)
818 {
819 basic_block bb = BASIC_BLOCK (i);
820 rtx insn, end;
821
822 end = bb->end;
823 insn = bb->head;
824 while (1)
825 {
826 int uid = INSN_UID (insn);
827 if (uid < max)
2307e372 828 VARRAY_BB (basic_block_for_insn, uid) = bb;
e881bb1b
RH
829 if (insn == end)
830 break;
831 insn = NEXT_INSN (insn);
832 }
833 }
421382ac
BS
834}
835
e881bb1b 836/* Free the memory associated with the edge structures. */
421382ac
BS
837
838static void
e881bb1b 839clear_edges ()
421382ac 840{
e881bb1b
RH
841 int i;
842 edge n, e;
421382ac 843
e881bb1b 844 for (i = 0; i < n_basic_blocks; ++i)
421382ac 845 {
e881bb1b 846 basic_block bb = BASIC_BLOCK (i);
421382ac 847
e881bb1b 848 for (e = bb->succ; e ; e = n)
421382ac 849 {
e881bb1b
RH
850 n = e->succ_next;
851 free (e);
421382ac 852 }
e881bb1b
RH
853
854 bb->succ = 0;
855 bb->pred = 0;
856 }
857
858 for (e = ENTRY_BLOCK_PTR->succ; e ; e = n)
859 {
860 n = e->succ_next;
861 free (e);
421382ac 862 }
e881bb1b
RH
863
864 ENTRY_BLOCK_PTR->succ = 0;
865 EXIT_BLOCK_PTR->pred = 0;
421382ac
BS
866}
867
e881bb1b
RH
868/* Identify the edges between basic blocks.
869
870 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
871 that are otherwise unreachable may be reachable with a non-local goto.
872
873 BB_EH_END is an array indexed by basic block number in which we record
874 the list of exception regions active at the end of the basic block. */
875
dc2ede84 876static void
336a6399 877make_edges (label_value_list)
e881bb1b 878 rtx label_value_list;
dc2ede84 879{
e881bb1b 880 int i;
1ef1bf06 881 eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
e881bb1b
RH
882
883 /* Assume no computed jump; revise as we create edges. */
884 current_function_has_computed_jump = 0;
885
886 /* By nature of the way these get numbered, block 0 is always the entry. */
887 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
dc2ede84 888
e881bb1b 889 for (i = 0; i < n_basic_blocks; ++i)
421382ac 890 {
e881bb1b 891 basic_block bb = BASIC_BLOCK (i);
336a6399 892 rtx insn, x;
e881bb1b 893 enum rtx_code code;
4b523fc4 894 int force_fallthru = 0;
421382ac 895
336a6399 896 /* Examine the last instruction of the block, and discover the
e881bb1b
RH
897 ways we can leave the block. */
898
899 insn = bb->end;
900 code = GET_CODE (insn);
901
902 /* A branch. */
903 if (code == JUMP_INSN)
904 {
905 rtx tmp;
906
907 /* ??? Recognize a tablejump and do the right thing. */
908 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
909 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
910 && GET_CODE (tmp) == JUMP_INSN
911 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
912 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
913 {
914 rtvec vec;
915 int j;
916
917 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
918 vec = XVEC (PATTERN (tmp), 0);
919 else
920 vec = XVEC (PATTERN (tmp), 1);
921
922 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
923 make_label_edge (bb, XEXP (RTVEC_ELT (vec, j), 0), 0);
4b523fc4
RE
924
925 /* Some targets (eg, ARM) emit a conditional jump that also
926 contains the out-of-range target. Scan for these and
927 add an edge if necessary. */
928 if ((tmp = single_set (insn)) != NULL
929 && SET_DEST (tmp) == pc_rtx
930 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
931 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
932 make_label_edge (bb, XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
933
934#ifdef CASE_DROPS_THROUGH
935 /* Silly VAXen. The ADDR_VEC is going to be in the way of
936 us naturally detecting fallthru into the next block. */
937 force_fallthru = 1;
938#endif
e881bb1b
RH
939 }
940
941 /* If this is a computed jump, then mark it as reaching
942 everything on the label_value_list and forced_labels list. */
943 else if (computed_jump_p (insn))
944 {
dc2ede84 945 current_function_has_computed_jump = 1;
dc2ede84 946
e881bb1b
RH
947 for (x = label_value_list; x; x = XEXP (x, 1))
948 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
949
dc2ede84 950 for (x = forced_labels; x; x = XEXP (x, 1))
e881bb1b 951 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
dc2ede84
BS
952 }
953
e881bb1b
RH
954 /* Returns create an exit out. */
955 else if (returnjump_p (insn))
956 make_edge (bb, EXIT_BLOCK_PTR, 0);
957
958 /* Otherwise, we have a plain conditional or unconditional jump. */
959 else
dc2ede84 960 {
e881bb1b
RH
961 if (! JUMP_LABEL (insn))
962 abort ();
963 make_label_edge (bb, JUMP_LABEL (insn), 0);
964 }
965 }
966
967 /* If this is a CALL_INSN, then mark it as reaching the active EH
968 handler for this CALL_INSN. If we're handling asynchronous
969 exceptions then any insn can reach any of the active handlers.
b472794d 970
e881bb1b 971 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
b472794d 972
a3e924fc 973 if (code == CALL_INSN || asynchronous_exceptions)
e881bb1b 974 {
336a6399
RH
975 /* If there's an EH region active at the end of a block,
976 add the appropriate edges. */
977 if (bb->eh_end >= 0)
978 make_eh_edge (eh_nest_info, bb, insn, bb->eh_end);
979
980 /* If we have asynchronous exceptions, do the same for *all*
981 exception regions active in the block. */
982 if (asynchronous_exceptions
983 && bb->eh_beg != bb->eh_end)
e881bb1b 984 {
336a6399
RH
985 if (bb->eh_beg >= 0)
986 make_eh_edge (eh_nest_info, bb, NULL_RTX, bb->eh_beg);
987
988 for (x = bb->head; x != bb->end; x = PREV_INSN (x))
989 if (GET_CODE (x) == NOTE
990 && (NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_BEG
991 || NOTE_LINE_NUMBER (x) == NOTE_INSN_EH_REGION_END))
992 {
993 int region = NOTE_EH_HANDLER (x);
994 make_eh_edge (eh_nest_info, bb, NULL_RTX, region);
995 }
e881bb1b
RH
996 }
997
998 if (code == CALL_INSN && nonlocal_goto_handler_labels)
999 {
dc2ede84
BS
1000 /* ??? This could be made smarter: in some cases it's possible
1001 to tell that certain calls will not do a nonlocal goto.
1002
1003 For example, if the nested functions that do the nonlocal
1004 gotos do not have their addresses taken, then only calls to
1005 those functions or to other nested functions that use them
1006 could possibly do nonlocal gotos. */
1ef1bf06
AM
1007 /* We do know that a REG_EH_REGION note with a value less
1008 than 0 is guaranteed not to perform a non-local goto. */
1009 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1010 if (!note || XINT (XEXP (note, 0), 0) >= 0)
1011 for (x = nonlocal_goto_handler_labels; x ; x = XEXP (x, 1))
1012 make_label_edge (bb, XEXP (x, 0),
1013 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
dc2ede84
BS
1014 }
1015 }
e881bb1b
RH
1016
1017 /* We know something about the structure of the function __throw in
1018 libgcc2.c. It is the only function that ever contains eh_stub
1019 labels. It modifies its return address so that the last block
1020 returns to one of the eh_stub labels within it. So we have to
1021 make additional edges in the flow graph. */
1022 if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1023 make_label_edge (bb, eh_return_stub_label, EDGE_EH);
1024
1025 /* Find out if we can drop through to the next block. */
1026 insn = next_nonnote_insn (insn);
4b523fc4 1027 if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
e881bb1b
RH
1028 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1029 else if (i + 1 < n_basic_blocks)
1030 {
1031 rtx tmp = BLOCK_HEAD (i + 1);
1032 if (GET_CODE (tmp) == NOTE)
1033 tmp = next_nonnote_insn (tmp);
4b523fc4 1034 if (force_fallthru || insn == tmp)
e881bb1b
RH
1035 make_edge (bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1036 }
dc2ede84 1037 }
1ef1bf06 1038 free_eh_nesting_info (eh_nest_info);
e881bb1b
RH
1039}
1040
1041/* Create an edge between two basic blocks. FLAGS are auxiliary information
1042 about the edge that is accumulated between calls. */
1043
1044static void
1045make_edge (src, dst, flags)
1046 basic_block src, dst;
1047 int flags;
1048{
1049 edge e;
1050
1051 /* Make sure we don't add duplicate edges. */
1052
1053 for (e = src->succ; e ; e = e->succ_next)
1054 if (e->dest == dst)
1055 {
1056 e->flags |= flags;
1057 return;
1058 }
1059
1060 e = (edge) xcalloc (1, sizeof (*e));
1061
1062 e->succ_next = src->succ;
1063 e->pred_next = dst->pred;
1064 e->src = src;
1065 e->dest = dst;
1066 e->flags = flags;
1067
1068 src->succ = e;
1069 dst->pred = e;
1070}
1071
1072/* Create an edge from a basic block to a label. */
1073
1074static void
1075make_label_edge (src, label, flags)
1076 basic_block src;
1077 rtx label;
1078 int flags;
1079{
1080 if (GET_CODE (label) != CODE_LABEL)
1081 abort ();
1082
1083 /* If the label was never emitted, this insn is junk, but avoid a
1084 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
1085 as a result of a syntax error and a diagnostic has already been
1086 printed. */
1087
1088 if (INSN_UID (label) == 0)
1089 return;
1090
1091 make_edge (src, BLOCK_FOR_INSN (label), flags);
1092}
e6cfb550 1093
336a6399
RH
1094/* Create the edges generated by INSN in REGION. */
1095
1096static void
1097make_eh_edge (eh_nest_info, src, insn, region)
1098 eh_nesting_info *eh_nest_info;
1099 basic_block src;
1100 rtx insn;
1101 int region;
1102{
1103 handler_info **handler_list;
1104 int num, is_call;
1105
1106 is_call = (insn && GET_CODE (insn) == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
1107 num = reachable_handlers (region, eh_nest_info, insn, &handler_list);
1108 while (--num >= 0)
1109 {
1110 make_label_edge (src, handler_list[num]->handler_label,
1111 EDGE_ABNORMAL | EDGE_EH | is_call);
1112 }
1113}
1114
1115/* EH_REGION notes appearing between basic blocks is ambiguous, and even
1116 dangerous if we intend to move basic blocks around. Move such notes
1117 into the following block. */
1118
1119static void
1120move_stray_eh_region_notes ()
1121{
1122 int i;
1123 basic_block b1, b2;
1124
1125 if (n_basic_blocks < 2)
1126 return;
1127
1128 b2 = BASIC_BLOCK (n_basic_blocks - 1);
1129 for (i = n_basic_blocks - 2; i >= 0; --i, b2 = b1)
1130 {
1131 rtx insn, next, list = NULL_RTX;
1132
1133 b1 = BASIC_BLOCK (i);
1134 for (insn = NEXT_INSN (b1->end); insn != b2->head; insn = next)
1135 {
1136 next = NEXT_INSN (insn);
1137 if (GET_CODE (insn) == NOTE
1138 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1139 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1140 {
1141 /* Unlink from the insn chain. */
1142 NEXT_INSN (PREV_INSN (insn)) = next;
1143 PREV_INSN (next) = PREV_INSN (insn);
1144
1145 /* Queue it. */
1146 NEXT_INSN (insn) = list;
1147 list = insn;
1148 }
1149 }
1150
1151 if (list == NULL_RTX)
1152 continue;
1153
1154 /* Find where to insert these things. */
1155 insn = b2->head;
1156 if (GET_CODE (insn) == CODE_LABEL)
1157 insn = NEXT_INSN (insn);
1158
1159 while (list)
1160 {
1161 next = NEXT_INSN (list);
1162 add_insn_after (list, insn);
1163 list = next;
1164 }
1165 }
1166}
1167
1168/* Recompute eh_beg/eh_end for each basic block. */
1169
1170static void
1171record_active_eh_regions (f)
1172 rtx f;
1173{
1174 rtx insn, eh_list = NULL_RTX;
1175 int i = 0;
1176 basic_block bb = BASIC_BLOCK (0);
1177
1178 for (insn = f; insn ; insn = NEXT_INSN (insn))
1179 {
1180 if (bb->head == insn)
1181 bb->eh_beg = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1182
1183 if (GET_CODE (insn) == NOTE)
1184 {
1185 int kind = NOTE_LINE_NUMBER (insn);
1186 if (kind == NOTE_INSN_EH_REGION_BEG)
1187 eh_list = alloc_INSN_LIST (insn, eh_list);
1188 else if (kind == NOTE_INSN_EH_REGION_END)
1189 {
1190 rtx t = XEXP (eh_list, 1);
1191 free_INSN_LIST_node (eh_list);
1192 eh_list = t;
1193 }
1194 }
1195
1196 if (bb->end == insn)
1197 {
1198 bb->eh_end = (eh_list ? NOTE_EH_HANDLER (XEXP (eh_list, 0)) : -1);
1199 i += 1;
1200 bb = BASIC_BLOCK (i);
1201 }
1202 }
1203}
1204
e881bb1b 1205/* Identify critical edges and set the bits appropriately. */
336a6399 1206
e881bb1b
RH
1207static void
1208mark_critical_edges ()
1209{
1210 int i, n = n_basic_blocks;
1211 basic_block bb;
1212
1213 /* We begin with the entry block. This is not terribly important now,
1214 but could be if a front end (Fortran) implemented alternate entry
1215 points. */
1216 bb = ENTRY_BLOCK_PTR;
1217 i = -1;
1218
1219 while (1)
e6cfb550 1220 {
e881bb1b
RH
1221 edge e;
1222
1223 /* (1) Critical edges must have a source with multiple successors. */
1224 if (bb->succ && bb->succ->succ_next)
1225 {
1226 for (e = bb->succ; e ; e = e->succ_next)
1227 {
1228 /* (2) Critical edges must have a destination with multiple
1229 predecessors. Note that we know there is at least one
1230 predecessor -- the edge we followed to get here. */
1231 if (e->dest->pred->pred_next)
1232 e->flags |= EDGE_CRITICAL;
1233 else
1234 e->flags &= ~EDGE_CRITICAL;
1235 }
1236 }
1237 else
1238 {
1239 for (e = bb->succ; e ; e = e->succ_next)
1240 e->flags &= ~EDGE_CRITICAL;
1241 }
1242
1243 if (++i >= n)
1244 break;
1245 bb = BASIC_BLOCK (i);
e6cfb550 1246 }
e881bb1b
RH
1247}
1248\f
1249/* Split a (typically critical) edge. Return the new block.
1250 Abort on abnormal edges.
1251
1252 ??? The code generally expects to be called on critical edges.
1253 The case of a block ending in an unconditional jump to a
1254 block with multiple predecessors is not handled optimally. */
1255
1256basic_block
1257split_edge (edge_in)
1258 edge edge_in;
1259{
1260 basic_block old_pred, bb, old_succ;
1261 edge edge_out;
1262 rtx bb_note;
abb14ef5 1263 int i, j;
e881bb1b
RH
1264
1265 /* Abnormal edges cannot be split. */
1266 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1267 abort ();
1268
1269 old_pred = edge_in->src;
1270 old_succ = edge_in->dest;
1271
1272 /* Remove the existing edge from the destination's pred list. */
1273 {
1274 edge *pp;
1275 for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1276 continue;
1277 *pp = edge_in->pred_next;
1e7d57a3 1278 edge_in->pred_next = NULL;
e881bb1b
RH
1279 }
1280
1281 /* Create the new structures. */
1282 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
1283 edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1284
1285 memset (bb, 0, sizeof (*bb));
1286 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
1287 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
1288 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
1289
1290 /* ??? This info is likely going to be out of date very soon. */
1291 CLEAR_REG_SET (bb->local_set);
1292 if (old_succ->global_live_at_start)
1293 {
1294 COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1295 COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1296 }
1297 else
1298 {
1299 CLEAR_REG_SET (bb->global_live_at_start);
1300 CLEAR_REG_SET (bb->global_live_at_end);
1301 }
1302
1303 /* Wire them up. */
1304 bb->pred = edge_in;
1305 bb->succ = edge_out;
1e7d57a3 1306
e881bb1b 1307 edge_in->dest = bb;
1e7d57a3
JH
1308 edge_in->flags &= ~EDGE_CRITICAL;
1309
1310 edge_out->pred_next = old_succ->pred;
1311 edge_out->succ_next = NULL;
e881bb1b
RH
1312 edge_out->src = bb;
1313 edge_out->dest = old_succ;
1e7d57a3
JH
1314 edge_out->flags = EDGE_FALLTHRU;
1315 edge_out->probability = REG_BR_PROB_BASE;
1316
1317 old_succ->pred = edge_out;
e881bb1b
RH
1318
1319 /* Tricky case -- if there existed a fallthru into the successor
1320 (and we're not it) we must add a new unconditional jump around
1321 the new block we're actually interested in.
1322
1323 Further, if that edge is critical, this means a second new basic
1324 block must be created to hold it. In order to simplify correct
1325 insn placement, do this before we touch the existing basic block
1326 ordering for the block we were really wanting. */
1327 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1328 {
1329 edge e;
1e7d57a3 1330 for (e = edge_out->pred_next; e ; e = e->pred_next)
e881bb1b
RH
1331 if (e->flags & EDGE_FALLTHRU)
1332 break;
1333
1334 if (e)
1335 {
1336 basic_block jump_block;
1337 rtx pos;
1338
1339 if ((e->flags & EDGE_CRITICAL) == 0)
1340 {
1341 /* Non critical -- we can simply add a jump to the end
1342 of the existing predecessor. */
1343 jump_block = e->src;
e881bb1b
RH
1344 }
1345 else
1346 {
1347 /* We need a new block to hold the jump. The simplest
1348 way to do the bulk of the work here is to recursively
1349 call ourselves. */
1350 jump_block = split_edge (e);
1351 e = jump_block->succ;
e881bb1b
RH
1352 }
1353
1e7d57a3
JH
1354 /* Now add the jump insn ... */
1355 pos = emit_jump_insn_after (gen_jump (old_succ->head),
1356 jump_block->end);
e881bb1b
RH
1357 jump_block->end = pos;
1358 emit_barrier_after (pos);
1e7d57a3
JH
1359
1360 /* ... let jump know that label is in use, ... */
a8688bd6 1361 JUMP_LABEL (pos) = old_succ->head;
1e7d57a3 1362 ++LABEL_NUSES (old_succ->head);
088e7160 1363
e881bb1b
RH
1364 /* ... and clear fallthru on the outgoing edge. */
1365 e->flags &= ~EDGE_FALLTHRU;
1366
1367 /* Continue splitting the interesting edge. */
1368 }
1369 }
1370
1371 /* Place the new block just in front of the successor. */
1372 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
abb14ef5
AM
1373 if (old_succ == EXIT_BLOCK_PTR)
1374 j = n_basic_blocks - 1;
1375 else
1376 j = old_succ->index;
1377 for (i = n_basic_blocks - 1; i > j; --i)
e881bb1b
RH
1378 {
1379 basic_block tmp = BASIC_BLOCK (i - 1);
1380 BASIC_BLOCK (i) = tmp;
1381 tmp->index = i;
1382 }
1383 BASIC_BLOCK (i) = bb;
1384 bb->index = i;
1385
1386 /* Create the basic block note. */
abb14ef5
AM
1387 if (old_succ != EXIT_BLOCK_PTR)
1388 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1389 else
1390 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
e881bb1b
RH
1391 NOTE_BASIC_BLOCK (bb_note) = bb;
1392 bb->head = bb->end = bb_note;
1393
1394 /* Not quite simple -- for non-fallthru edges, we must adjust the
1395 predecessor's jump instruction to target our new block. */
1396 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1397 {
1398 rtx tmp, insn = old_pred->end;
1399 rtx old_label = old_succ->head;
1400 rtx new_label = gen_label_rtx ();
1401
1402 if (GET_CODE (insn) != JUMP_INSN)
1403 abort ();
1404
1405 /* ??? Recognize a tablejump and adjust all matching cases. */
1406 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1407 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1408 && GET_CODE (tmp) == JUMP_INSN
1409 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1410 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1411 {
1412 rtvec vec;
1413 int j;
1414
1415 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1416 vec = XVEC (PATTERN (tmp), 0);
1417 else
1418 vec = XVEC (PATTERN (tmp), 1);
1419
1420 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1421 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1422 {
1423 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1424 --LABEL_NUSES (old_label);
1425 ++LABEL_NUSES (new_label);
1426 }
1427 }
1428 else
1429 {
1430 /* This would have indicated an abnormal edge. */
1431 if (computed_jump_p (insn))
1432 abort ();
1433
1434 /* A return instruction can't be redirected. */
1435 if (returnjump_p (insn))
1436 abort ();
1437
1438 /* If the insn doesn't go where we think, we're confused. */
1439 if (JUMP_LABEL (insn) != old_label)
1440 abort ();
1441
1442 redirect_jump (insn, new_label);
1443 }
1444
1445 emit_label_before (new_label, bb_note);
1446 bb->head = new_label;
1447 }
1448
e881bb1b
RH
1449 return bb;
1450}
1451
1452/* Queue instructions for insertion on an edge between two basic blocks.
1453 The new instructions and basic blocks (if any) will not appear in the
1454 CFG until commit_edge_insertions is called. */
1455
1456void
1457insert_insn_on_edge (pattern, e)
1458 rtx pattern;
1459 edge e;
1460{
1461 /* We cannot insert instructions on an abnormal critical edge.
1462 It will be easier to find the culprit if we die now. */
1463 if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1464 == (EDGE_ABNORMAL|EDGE_CRITICAL))
1465 abort ();
1466
1467 if (e->insns == NULL_RTX)
1468 start_sequence ();
1469 else
1470 push_to_sequence (e->insns);
1471
1472 emit_insn (pattern);
1473
1474 e->insns = get_insns ();
1475 end_sequence();
1476}
1477
1478/* Update the CFG for the instructions queued on edge E. */
1479
1480static void
1481commit_one_edge_insertion (e)
1482 edge e;
1483{
1484 rtx before = NULL_RTX, after = NULL_RTX, tmp;
1485 basic_block bb;
1486
1487 /* Figure out where to put these things. If the destination has
1488 one predecessor, insert there. Except for the exit block. */
1489 if (e->dest->pred->pred_next == NULL
1490 && e->dest != EXIT_BLOCK_PTR)
1491 {
1492 bb = e->dest;
1493
1494 /* Get the location correct wrt a code label, and "nice" wrt
1495 a basic block note, and before everything else. */
1496 tmp = bb->head;
1497 if (GET_CODE (tmp) == CODE_LABEL)
1498 tmp = NEXT_INSN (tmp);
1499 if (GET_CODE (tmp) == NOTE
1500 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BASIC_BLOCK)
1501 tmp = NEXT_INSN (tmp);
1502 if (tmp == bb->head)
1503 before = tmp;
1504 else
1505 after = PREV_INSN (tmp);
1506 }
1507
1508 /* If the source has one successor and the edge is not abnormal,
1509 insert there. Except for the entry block. */
1510 else if ((e->flags & EDGE_ABNORMAL) == 0
1511 && e->src->succ->succ_next == NULL
1512 && e->src != ENTRY_BLOCK_PTR)
1513 {
1514 bb = e->src;
1515 if (GET_CODE (bb->end) == JUMP_INSN)
1516 {
1517 /* ??? Is it possible to wind up with non-simple jumps? Perhaps
1518 a jump with delay slots already filled? */
1519 if (! simplejump_p (bb->end))
1520 abort ();
1521
1522 before = bb->end;
1523 }
1524 else
1525 {
1526 /* We'd better be fallthru, or we've lost track of what's what. */
1527 if ((e->flags & EDGE_FALLTHRU) == 0)
1528 abort ();
1529
1530 after = bb->end;
1531 }
1532 }
1533
1534 /* Otherwise we must split the edge. */
1535 else
1536 {
1537 bb = split_edge (e);
1538 after = bb->end;
1539 }
1540
1541 /* Now that we've found the spot, do the insertion. */
1542 tmp = e->insns;
1543 e->insns = NULL_RTX;
a8688bd6
AM
1544
1545 /* Set the new block number for these insns, if structure is allocated. */
1546 if (basic_block_for_insn)
1547 {
1548 rtx i;
1549 for (i = tmp; i != NULL_RTX; i = NEXT_INSN (i))
1550 set_block_for_insn (i, bb);
1551 }
1552
e881bb1b
RH
1553 if (before)
1554 {
1555 emit_insns_before (tmp, before);
1556 if (before == bb->head)
a8688bd6 1557 bb->head = tmp;
e881bb1b
RH
1558 }
1559 else
1560 {
1561 tmp = emit_insns_after (tmp, after);
1562 if (after == bb->end)
1563 bb->end = tmp;
1564 }
1565}
1566
1567/* Update the CFG for all queued instructions. */
1568
1569void
1570commit_edge_insertions ()
1571{
1572 int i;
1573 basic_block bb;
1574
1575 i = -1;
1576 bb = ENTRY_BLOCK_PTR;
1577 while (1)
1578 {
1579 edge e, next;
1580
1581 for (e = bb->succ; e ; e = next)
1582 {
1583 next = e->succ_next;
1584 if (e->insns)
1585 commit_one_edge_insertion (e);
1586 }
1587
1588 if (++i >= n_basic_blocks)
1589 break;
1590 bb = BASIC_BLOCK (i);
1591 }
1592}
1593\f
1594/* Delete all unreachable basic blocks. */
1595
1596static void
1597delete_unreachable_blocks ()
1598{
1599 basic_block *worklist, *tos;
1600 int deleted_handler;
1601 edge e;
1602 int i, n;
1603
1604 n = n_basic_blocks;
1605 tos = worklist = (basic_block *) alloca (sizeof (basic_block) * n);
1606
1607 /* Use basic_block->aux as a marker. Clear them all. */
1608
1609 for (i = 0; i < n; ++i)
1610 BASIC_BLOCK (i)->aux = NULL;
1611
1612 /* Add our starting points to the worklist. Almost always there will
1613 be only one. It isn't inconcievable that we might one day directly
1614 support Fortran alternate entry points. */
1615
1616 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
aa3d4bf9
RH
1617 {
1618 *tos++ = e->dest;
1619
1620 /* Mark the block with a handy non-null value. */
1621 e->dest->aux = e;
1622 }
e881bb1b
RH
1623
1624 /* Iterate: find everything reachable from what we've already seen. */
1625
1626 while (tos != worklist)
1627 {
1628 basic_block b = *--tos;
1629
e881bb1b
RH
1630 for (e = b->succ; e ; e = e->succ_next)
1631 if (!e->dest->aux)
aa3d4bf9
RH
1632 {
1633 *tos++ = e->dest;
1634 e->dest->aux = e;
1635 }
e881bb1b
RH
1636 }
1637
1638 /* Delete all unreachable basic blocks. Count down so that we don't
1639 interfere with the block renumbering that happens in delete_block. */
1640
1641 deleted_handler = 0;
1642
1643 for (i = n - 1; i >= 0; --i)
1644 {
1645 basic_block b = BASIC_BLOCK (i);
1646
1647 if (b->aux != NULL)
1648 /* This block was found. Tidy up the mark. */
1649 b->aux = NULL;
1650 else
1651 deleted_handler |= delete_block (b);
1652 }
1653
1654 /* Fix up edges that now fall through, or rather should now fall through
1655 but previously required a jump around now deleted blocks. Simplify
1656 the search by only examining blocks numerically adjacent, since this
1657 is how find_basic_blocks created them. */
1658
1659 for (i = 1; i < n_basic_blocks; ++i)
1660 {
1661 basic_block b = BASIC_BLOCK (i - 1);
1662 basic_block c = BASIC_BLOCK (i);
1663 edge s;
1664
abb3f0a9
JL
1665 /* We care about simple conditional or unconditional jumps with
1666 a single successor.
1667
1668 If we had a conditional branch to the next instruction when
1669 find_basic_blocks was called, then there will only be one
1670 out edge for the block which ended with the conditional
1671 branch (since we do not create duplicate edges).
1672
4f282ba1
JL
1673 Furthermore, the edge will be marked as a fallthru because we
1674 merge the flags for the duplicate edges. So we do not want to
1675 check that the edge is not a FALLTHRU edge. */
e881bb1b
RH
1676 if ((s = b->succ) != NULL
1677 && s->succ_next == NULL
e8fe3cc3 1678 && s->dest == c
d0e80719
RH
1679 /* If the jump insn has side effects, we can't tidy the edge. */
1680 && (GET_CODE (b->end) != JUMP_INSN
1681 || onlyjump_p (b->end)))
e881bb1b
RH
1682 tidy_fallthru_edge (s, b, c);
1683 }
1684
e881bb1b
RH
1685 /* If we deleted an exception handler, we may have EH region begin/end
1686 blocks to remove as well. */
1687 if (deleted_handler)
1688 delete_eh_regions ();
1689}
1690
1691/* Find EH regions for which there is no longer a handler, and delete them. */
1692
1693static void
1694delete_eh_regions ()
1695{
1696 rtx insn;
1697
1ef1bf06
AM
1698 update_rethrow_references ();
1699
e881bb1b
RH
1700 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1701 if (GET_CODE (insn) == NOTE)
1702 {
1703 if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
1704 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1705 {
ef178af3 1706 int num = NOTE_EH_HANDLER (insn);
1ef1bf06
AM
1707 /* A NULL handler indicates a region is no longer needed,
1708 as long as it isn't the target of a rethrow. */
1709 if (get_first_handler (num) == NULL && ! rethrow_used (num))
e881bb1b
RH
1710 {
1711 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1712 NOTE_SOURCE_FILE (insn) = 0;
1713 }
1714 }
1715 }
1716}
1717
1718/* Return true if NOTE is not one of the ones that must be kept paired,
1719 so that we may simply delete them. */
1720
1721static int
eeea333e 1722can_delete_note_p (note)
e881bb1b
RH
1723 rtx note;
1724{
1725 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
1726 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
1727}
1728
1729/* Unlink a chain of insns between START and FINISH, leaving notes
1730 that must be paired. */
1731
1732static void
5aabad00 1733flow_delete_insn_chain (start, finish)
e881bb1b
RH
1734 rtx start, finish;
1735{
1736 /* Unchain the insns one by one. It would be quicker to delete all
1737 of these with a single unchaining, rather than one at a time, but
1738 we need to keep the NOTE's. */
1739
1740 rtx next;
1741
1742 while (1)
1743 {
1744 next = NEXT_INSN (start);
eeea333e
RH
1745 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
1746 ;
1747 else if (GET_CODE (start) == CODE_LABEL && !can_delete_label_p (start))
1748 ;
1749 else
e881bb1b
RH
1750 next = flow_delete_insn (start);
1751
1752 if (start == finish)
1753 break;
1754 start = next;
1755 }
1756}
1757
1758/* Delete the insns in a (non-live) block. We physically delete every
1759 non-deleted-note insn, and update the flow graph appropriately.
1760
1761 Return nonzero if we deleted an exception handler. */
1762
1763/* ??? Preserving all such notes strikes me as wrong. It would be nice
1764 to post-process the stream to remove empty blocks, loops, ranges, etc. */
1765
1766static int
1767delete_block (b)
1768 basic_block b;
1769{
1770 int deleted_handler = 0;
1771 rtx insn, end;
1772
1773 /* If the head of this block is a CODE_LABEL, then it might be the
1774 label for an exception handler which can't be reached.
1775
1776 We need to remove the label from the exception_handler_label list
3ad47811
MM
1777 and remove the associated NOTE_INSN_EH_REGION_BEG and
1778 NOTE_INSN_EH_REGION_END notes. */
e881bb1b
RH
1779
1780 insn = b->head;
088e7160 1781
312f6255
GK
1782 never_reached_warning (insn);
1783
e881bb1b
RH
1784 if (GET_CODE (insn) == CODE_LABEL)
1785 {
1786 rtx x, *prev = &exception_handler_labels;
1787
1788 for (x = exception_handler_labels; x; x = XEXP (x, 1))
1789 {
1790 if (XEXP (x, 0) == insn)
1791 {
1792 /* Found a match, splice this label out of the EH label list. */
1793 *prev = XEXP (x, 1);
1794 XEXP (x, 1) = NULL_RTX;
1795 XEXP (x, 0) = NULL_RTX;
1796
1797 /* Remove the handler from all regions */
1798 remove_handler (insn);
1799 deleted_handler = 1;
1800 break;
1801 }
1802 prev = &XEXP (x, 1);
1803 }
1804
1805 /* This label may be referenced by code solely for its value, or
1806 referenced by static data, or something. We have determined
1807 that it is not reachable, but cannot delete the label itself.
1808 Save code space and continue to delete the balance of the block,
1809 along with properly updating the cfg. */
1810 if (!can_delete_label_p (insn))
1811 {
1812 /* If we've only got one of these, skip the whole deleting
1813 insns thing. */
1814 if (insn == b->end)
1815 goto no_delete_insns;
1816 insn = NEXT_INSN (insn);
1817 }
1818 }
1819
1820 /* Selectively unlink the insn chain. Include any BARRIER that may
1821 follow the basic block. */
1822 end = next_nonnote_insn (b->end);
1823 if (!end || GET_CODE (end) != BARRIER)
1824 end = b->end;
5aabad00 1825 flow_delete_insn_chain (insn, end);
e881bb1b
RH
1826
1827no_delete_insns:
1828
1829 /* Remove the edges into and out of this block. Note that there may
1830 indeed be edges in, if we are removing an unreachable loop. */
1831 {
1832 edge e, next, *q;
1833
1834 for (e = b->pred; e ; e = next)
1835 {
1836 for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
1837 continue;
1838 *q = e->succ_next;
1839 next = e->pred_next;
1840 free (e);
1841 }
1842 for (e = b->succ; e ; e = next)
1843 {
1844 for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
1845 continue;
1846 *q = e->pred_next;
1847 next = e->succ_next;
1848 free (e);
1849 }
1850
1851 b->pred = NULL;
1852 b->succ = NULL;
1853 }
1854
1855 /* Remove the basic block from the array, and compact behind it. */
1856 expunge_block (b);
1857
1858 return deleted_handler;
1859}
1860
1861/* Remove block B from the basic block array and compact behind it. */
1862
1863static void
1864expunge_block (b)
1865 basic_block b;
1866{
1867 int i, n = n_basic_blocks;
1868
1869 for (i = b->index; i + 1 < n; ++i)
1870 {
1871 basic_block x = BASIC_BLOCK (i + 1);
1872 BASIC_BLOCK (i) = x;
1873 x->index = i;
1874 }
1875
1876 basic_block_info->num_elements--;
1877 n_basic_blocks--;
1878}
1879
1880/* Delete INSN by patching it out. Return the next insn. */
1881
1882static rtx
1883flow_delete_insn (insn)
1884 rtx insn;
1885{
1886 rtx prev = PREV_INSN (insn);
1887 rtx next = NEXT_INSN (insn);
1888
1889 PREV_INSN (insn) = NULL_RTX;
1890 NEXT_INSN (insn) = NULL_RTX;
1891
1892 if (prev)
1893 NEXT_INSN (prev) = next;
1894 if (next)
1895 PREV_INSN (next) = prev;
1896 else
1897 set_last_insn (prev);
e6cfb550 1898
55a98783
JL
1899 if (GET_CODE (insn) == CODE_LABEL)
1900 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
1901
e881bb1b
RH
1902 /* If deleting a jump, decrement the use count of the label. Deleting
1903 the label itself should happen in the normal course of block merging. */
1904 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1905 LABEL_NUSES (JUMP_LABEL (insn))--;
1906
1907 return next;
d7429b6a 1908}
8329b5ec 1909
e881bb1b
RH
1910/* True if a given label can be deleted. */
1911
1912static int
1913can_delete_label_p (label)
1914 rtx label;
dc2ede84 1915{
e881bb1b 1916 rtx x;
dc2ede84 1917
e881bb1b
RH
1918 if (LABEL_PRESERVE_P (label))
1919 return 0;
421382ac 1920
e881bb1b
RH
1921 for (x = forced_labels; x ; x = XEXP (x, 1))
1922 if (label == XEXP (x, 0))
1923 return 0;
1924 for (x = label_value_list; x ; x = XEXP (x, 1))
1925 if (label == XEXP (x, 0))
1926 return 0;
1927 for (x = exception_handler_labels; x ; x = XEXP (x, 1))
1928 if (label == XEXP (x, 0))
1929 return 0;
dc2ede84 1930
abb3f0a9 1931 /* User declared labels must be preserved. */
088e7160 1932 if (LABEL_NAME (label) != 0)
abb3f0a9 1933 return 0;
088e7160 1934
e881bb1b
RH
1935 return 1;
1936}
421382ac 1937
558389e3
JL
1938/* Blocks A and B are to be merged into a single block. A has no incoming
1939 fallthru edge, so it can be moved before B without adding or modifying
1940 any jumps (aside from the jump from A to B). */
1941
1942static int
336a6399 1943merge_blocks_move_predecessor_nojumps (a, b)
558389e3
JL
1944 basic_block a, b;
1945{
1946 rtx start, end, insertpoint, barrier;
1947
1948 start = a->head;
1949 end = a->end;
1950 insertpoint = PREV_INSN (b->head);
1951
1952 /* We want to delete the BARRIER after the end of the insns we are
1953 going to move. If we don't find a BARRIER, then do nothing. This
1954 can happen in some cases if we have labels we can not delete.
1955
1956 Similarly, do nothing if we can not delete the label at the start
1957 of the target block. */
1958 barrier = next_nonnote_insn (end);
1959 if (GET_CODE (barrier) != BARRIER
1960 || (GET_CODE (b->head) == CODE_LABEL
1961 && ! can_delete_label_p (b->head)))
1962 return 0;
1963 else
1964 flow_delete_insn (barrier);
1965
1966 /* Move block and loop notes out of the chain so that we do not
1967 disturb their order.
1968
1969 ??? A better solution would be to squeeze out all the non-nested notes
1970 and adjust the block trees appropriately. Even better would be to have
1971 a tighter connection between block trees and rtl so that this is not
1972 necessary. */
1973 start = squeeze_notes (start, end);
1974
1975 /* Scramble the insn chain. */
1976 reorder_insns (start, end, insertpoint);
1977
1978 /* Now blocks A and B are contiguous. Merge them. */
1979 merge_blocks_nomove (a, b);
336a6399
RH
1980
1981 if (rtl_dump_file)
1982 {
1983 fprintf (rtl_dump_file, "Moved block %d before %d and merged.\n",
1984 a->index, b->index);
1985 }
1986
558389e3
JL
1987 return 1;
1988}
1989
1990/* Blocks A and B are to be merged into a single block. B has no outgoing
1991 fallthru edge, so it can be moved after A without adding or modifying
1992 any jumps (aside from the jump from A to B). */
1993
1994static int
336a6399 1995merge_blocks_move_successor_nojumps (a, b)
558389e3
JL
1996 basic_block a, b;
1997{
1998 rtx start, end, insertpoint, barrier;
1999
2000 start = b->head;
2001 end = b->end;
2002 insertpoint = a->end;
2003
8288909f 2004 /* We want to delete the BARRIER after the end of the insns we are
558389e3 2005 going to move. If we don't find a BARRIER, then do nothing. This
8288909f
JL
2006 can happen in some cases if we have labels we can not delete.
2007
2008 Similarly, do nothing if we can not delete the label at the start
2009 of the target block. */
2010 barrier = next_nonnote_insn (end);
558389e3
JL
2011 if (GET_CODE (barrier) != BARRIER
2012 || (GET_CODE (b->head) == CODE_LABEL
2013 && ! can_delete_label_p (b->head)))
2014 return 0;
2015 else
2016 flow_delete_insn (barrier);
2017
2018 /* Move block and loop notes out of the chain so that we do not
2019 disturb their order.
2020
2021 ??? A better solution would be to squeeze out all the non-nested notes
2022 and adjust the block trees appropriately. Even better would be to have
2023 a tighter connection between block trees and rtl so that this is not
2024 necessary. */
2025 start = squeeze_notes (start, end);
2026
2027 /* Scramble the insn chain. */
2028 reorder_insns (start, end, insertpoint);
2029
2030 /* Now blocks A and B are contiguous. Merge them. */
2031 merge_blocks_nomove (a, b);
336a6399
RH
2032
2033 if (rtl_dump_file)
2034 {
2035 fprintf (rtl_dump_file, "Moved block %d after %d and merged.\n",
2036 b->index, a->index);
2037 }
2038
558389e3
JL
2039 return 1;
2040}
2041
e881bb1b
RH
2042/* Blocks A and B are to be merged into a single block. The insns
2043 are already contiguous, hence `nomove'. */
421382ac 2044
e881bb1b
RH
2045static void
2046merge_blocks_nomove (a, b)
2047 basic_block a, b;
2048{
2049 edge e;
f5c14c21
RH
2050 rtx b_head, b_end, a_end;
2051 int b_empty = 0;
2052
2053 /* If there was a CODE_LABEL beginning B, delete it. */
2054 b_head = b->head;
2055 b_end = b->end;
2056 if (GET_CODE (b_head) == CODE_LABEL)
2057 {
2058 /* Detect basic blocks with nothing but a label. This can happen
2059 in particular at the end of a function. */
2060 if (b_head == b_end)
2061 b_empty = 1;
2062 b_head = flow_delete_insn (b_head);
2063 }
2064
2065 /* Delete the basic block note. */
2066 if (GET_CODE (b_head) == NOTE
2067 && NOTE_LINE_NUMBER (b_head) == NOTE_INSN_BASIC_BLOCK)
2068 {
2069 if (b_head == b_end)
2070 b_empty = 1;
2071 b_head = flow_delete_insn (b_head);
2072 }
421382ac 2073
e881bb1b 2074 /* If there was a jump out of A, delete it. */
f5c14c21
RH
2075 a_end = a->end;
2076 if (GET_CODE (a_end) == JUMP_INSN)
e881bb1b 2077 {
f5c14c21 2078 rtx prev;
86879c21 2079
f5c14c21
RH
2080 prev = prev_nonnote_insn (a_end);
2081 if (!prev)
2082 prev = a->head;
86879c21
JL
2083
2084#ifdef HAVE_cc0
f5c14c21
RH
2085 /* If this was a conditional jump, we need to also delete
2086 the insn that set cc0. */
86879c21 2087
f5c14c21
RH
2088 if (prev && sets_cc0_p (prev))
2089 {
2090 rtx tmp = prev;
2091 prev = prev_nonnote_insn (prev);
2092 if (!prev)
2093 prev = a->head;
e881bb1b 2094 flow_delete_insn (tmp);
421382ac 2095 }
f5c14c21
RH
2096#endif
2097
2098 /* Note that a->head != a->end, since we should have at least a
2099 bb note plus the jump, so prev != insn. */
2100 flow_delete_insn (a_end);
2101 a_end = prev;
421382ac 2102 }
421382ac 2103
e881bb1b
RH
2104 /* By definition, there should only be one successor of A, and that is
2105 B. Free that edge struct. */
2106 free (a->succ);
2107
2108 /* Adjust the edges out of B for the new owner. */
2109 for (e = b->succ; e ; e = e->succ_next)
2110 e->src = a;
2111 a->succ = b->succ;
2112
e881bb1b 2113 /* Reassociate the insns of B with A. */
f5c14c21 2114 if (!b_empty)
e881bb1b 2115 {
f5c14c21
RH
2116 BLOCK_FOR_INSN (b_head) = a;
2117 while (b_head != b_end)
dc2ede84 2118 {
f5c14c21
RH
2119 b_head = NEXT_INSN (b_head);
2120 BLOCK_FOR_INSN (b_head) = a;
dc2ede84 2121 }
f5c14c21 2122 a_end = b_head;
e881bb1b 2123 }
f5c14c21 2124 a->end = a_end;
e881bb1b
RH
2125
2126 /* Compact the basic block array. */
2127 expunge_block (b);
dc2ede84
BS
2128}
2129
e881bb1b
RH
2130/* Attempt to merge basic blocks that are potentially non-adjacent.
2131 Return true iff the attempt succeeded. */
dc2ede84 2132
dc2ede84 2133static int
e881bb1b
RH
2134merge_blocks (e, b, c)
2135 edge e;
2136 basic_block b, c;
dc2ede84 2137{
e881bb1b 2138 /* If B has a fallthru edge to C, no need to move anything. */
336a6399 2139 if (e->flags & EDGE_FALLTHRU)
e881bb1b 2140 {
336a6399
RH
2141 /* If a label still appears somewhere and we cannot delete the label,
2142 then we cannot merge the blocks. The edge was tidied already. */
558389e3 2143
336a6399
RH
2144 rtx insn, stop = NEXT_INSN (c->head);
2145 for (insn = NEXT_INSN (b->end); insn != stop; insn = NEXT_INSN (insn))
2146 if (GET_CODE (insn) == CODE_LABEL && !can_delete_label_p (insn))
2147 return 0;
558389e3 2148
336a6399 2149 merge_blocks_nomove (b, c);
558389e3 2150
336a6399
RH
2151 if (rtl_dump_file)
2152 {
2153 fprintf (rtl_dump_file, "Merged %d and %d without moving.\n",
2154 b->index, c->index);
2155 }
e881bb1b 2156
336a6399
RH
2157 return 1;
2158 }
2159 else
2160 {
2161 edge tmp_edge;
2162 basic_block d;
2163 int c_has_outgoing_fallthru;
2164 int b_has_incoming_fallthru;
e881bb1b 2165
336a6399
RH
2166 /* We must make sure to not munge nesting of exception regions,
2167 lexical blocks, and loop notes.
2168
2169 The first is taken care of by requiring that the active eh
2170 region at the end of one block always matches the active eh
2171 region at the beginning of the next block.
2172
2173 The later two are taken care of by squeezing out all the notes. */
2174
2175 /* ??? A throw/catch edge (or any abnormal edge) should be rarely
2176 executed and we may want to treat blocks which have two out
2177 edges, one normal, one abnormal as only having one edge for
2178 block merging purposes. */
558389e3
JL
2179
2180 for (tmp_edge = c->succ; tmp_edge ; tmp_edge = tmp_edge->succ_next)
2181 if (tmp_edge->flags & EDGE_FALLTHRU)
2182 break;
2183 c_has_outgoing_fallthru = (tmp_edge != NULL);
2184
2185 for (tmp_edge = b->pred; tmp_edge ; tmp_edge = tmp_edge->pred_next)
2186 if (tmp_edge->flags & EDGE_FALLTHRU)
2187 break;
2188 b_has_incoming_fallthru = (tmp_edge != NULL);
2189
336a6399
RH
2190 /* If B does not have an incoming fallthru, and the exception regions
2191 match, then it can be moved immediately before C without introducing
2192 or modifying jumps. */
2193 d = BASIC_BLOCK (c->index - 1);
2194 if (! b_has_incoming_fallthru
2195 && d->eh_end == b->eh_beg
2196 && b->eh_end == c->eh_beg)
2197 return merge_blocks_move_predecessor_nojumps (b, c);
2198
2199 /* Otherwise, we're going to try to move C after B. Make sure the
2200 exception regions match. */
2201 d = BASIC_BLOCK (b->index + 1);
2202 if (b->eh_end == c->eh_beg
2203 && c->eh_end == d->eh_beg)
2204 {
2205 /* If C does not have an outgoing fallthru, then it can be moved
2206 immediately after B without introducing or modifying jumps. */
2207 if (! c_has_outgoing_fallthru)
2208 return merge_blocks_move_successor_nojumps (b, c);
2209
2210 /* Otherwise, we'll need to insert an extra jump, and possibly
2211 a new block to contain it. */
2212 /* ??? Not implemented yet. */
2213 }
558389e3 2214
336a6399 2215 return 0;
e881bb1b 2216 }
336a6399 2217}
dc2ede84 2218
336a6399 2219/* Top level driver for merge_blocks. */
421382ac 2220
336a6399
RH
2221static void
2222try_merge_blocks ()
2223{
2224 int i;
2225
2226 /* Attempt to merge blocks as made possible by edge removal. If a block
2227 has only one successor, and the successor has only one predecessor,
2228 they may be combined. */
2229
2230 for (i = 0; i < n_basic_blocks; )
2231 {
2232 basic_block c, b = BASIC_BLOCK (i);
2233 edge s;
2234
2235 /* A loop because chains of blocks might be combineable. */
2236 while ((s = b->succ) != NULL
2237 && s->succ_next == NULL
2238 && (s->flags & EDGE_EH) == 0
2239 && (c = s->dest) != EXIT_BLOCK_PTR
2240 && c->pred->pred_next == NULL
2241 /* If the jump insn has side effects, we can't kill the edge. */
2242 && (GET_CODE (b->end) != JUMP_INSN
2243 || onlyjump_p (b->end))
2244 && merge_blocks (s, b, c))
2245 continue;
2246
2247 /* Don't get confused by the index shift caused by deleting blocks. */
2248 i = b->index + 1;
2249 }
e881bb1b 2250}
421382ac 2251
e881bb1b
RH
2252/* The given edge should potentially a fallthru edge. If that is in
2253 fact true, delete the unconditional jump and barriers that are in
2254 the way. */
2255
2256static void
2257tidy_fallthru_edge (e, b, c)
2258 edge e;
2259 basic_block b, c;
2260{
eeea333e 2261 rtx q;
e881bb1b
RH
2262
2263 /* ??? In a late-running flow pass, other folks may have deleted basic
2264 blocks by nopping out blocks, leaving multiple BARRIERs between here
2265 and the target label. They ought to be chastized and fixed.
2266
eeea333e
RH
2267 We can also wind up with a sequence of undeletable labels between
2268 one block and the next.
dc2ede84 2269
eeea333e
RH
2270 So search through a sequence of barriers, labels, and notes for
2271 the head of block C and assert that we really do fall through. */
421382ac 2272
eeea333e 2273 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
e881bb1b 2274 return;
421382ac 2275
e881bb1b
RH
2276 /* Remove what will soon cease being the jump insn from the source block.
2277 If block B consisted only of this single jump, turn it into a deleted
2278 note. */
2279 q = b->end;
2280 if (GET_CODE (q) == JUMP_INSN)
421382ac 2281 {
86a1db60
RH
2282#ifdef HAVE_cc0
2283 /* If this was a conditional jump, we need to also delete
2284 the insn that set cc0. */
b30f05db 2285 if (! simplejump_p (q) && condjump_p (q) && sets_cc0_p (PREV_INSN (q)))
86a1db60
RH
2286 q = PREV_INSN (q);
2287#endif
2288
e881bb1b
RH
2289 if (b->head == q)
2290 {
2291 PUT_CODE (q, NOTE);
2292 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2293 NOTE_SOURCE_FILE (q) = 0;
2294 }
e3f6ee23 2295 else
e881bb1b 2296 b->end = q = PREV_INSN (q);
421382ac 2297 }
421382ac 2298
e881bb1b 2299 /* Selectively unlink the sequence. */
86a1db60 2300 if (q != PREV_INSN (c->head))
5aabad00 2301 flow_delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
b7f7462b 2302
e881bb1b
RH
2303 e->flags |= EDGE_FALLTHRU;
2304}
dc2ede84 2305
e881bb1b
RH
2306/* Discover and record the loop depth at the head of each basic block. */
2307
2308static void
2309calculate_loop_depth (insns)
2310 rtx insns;
2311{
2312 basic_block bb;
2313 rtx insn;
2314 int i = 0, depth = 1;
dc2ede84 2315
e881bb1b
RH
2316 bb = BASIC_BLOCK (i);
2317 for (insn = insns; insn ; insn = NEXT_INSN (insn))
dc2ede84 2318 {
e881bb1b
RH
2319 if (insn == bb->head)
2320 {
2321 bb->loop_depth = depth;
2322 if (++i >= n_basic_blocks)
dc2ede84 2323 break;
e881bb1b
RH
2324 bb = BASIC_BLOCK (i);
2325 }
dc2ede84 2326
e881bb1b
RH
2327 if (GET_CODE (insn) == NOTE)
2328 {
2329 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2330 depth++;
2331 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2332 depth--;
2333
2334 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. */
2335 if (depth == 0)
2336 abort ();
2337 }
2338 }
dc2ede84 2339}
d7429b6a 2340\f
5ece9746
JL
2341/* Perform data flow analysis.
2342 F is the first insn of the function and NREGS the number of register numbers
2343 in use. */
2344
2345void
11f246f6 2346life_analysis (f, nregs, file, remove_dead_code)
5ece9746
JL
2347 rtx f;
2348 int nregs;
2349 FILE *file;
11f246f6 2350 int remove_dead_code;
5ece9746 2351{
5ece9746 2352#ifdef ELIMINABLE_REGS
ecb06768 2353 register size_t i;
5ece9746
JL
2354 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2355#endif
2356
2357 /* Record which registers will be eliminated. We use this in
2358 mark_used_regs. */
2359
2360 CLEAR_HARD_REG_SET (elim_reg_set);
2361
2362#ifdef ELIMINABLE_REGS
2363 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2364 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2365#else
2366 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2367#endif
2368
e881bb1b
RH
2369 /* Allocate a bitmap to be filled in by record_volatile_insns. */
2370 uid_volatile = BITMAP_ALLOCA ();
2371
db3a887b
CB
2372 /* We want alias analysis information for local dead store elimination. */
2373 init_alias_analysis ();
7790df19 2374
11f246f6 2375 life_analysis_1 (f, nregs, remove_dead_code);
7790df19
JW
2376
2377 if (! reload_completed)
2378 mark_constant_function ();
2379
db3a887b
CB
2380 end_alias_analysis ();
2381
5ece9746
JL
2382 if (file)
2383 dump_flow_info (file);
2384
e881bb1b 2385 BITMAP_FREE (uid_volatile);
5ece9746
JL
2386 free_basic_block_vars (1);
2387}
2388
2389/* Free the variables allocated by find_basic_blocks.
2390
e881bb1b 2391 KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed. */
5ece9746
JL
2392
2393void
2394free_basic_block_vars (keep_head_end_p)
2395 int keep_head_end_p;
2396{
e881bb1b 2397 if (basic_block_for_insn)
5ece9746 2398 {
e881bb1b
RH
2399 VARRAY_FREE (basic_block_for_insn);
2400 basic_block_for_insn = NULL;
5ece9746
JL
2401 }
2402
e881bb1b 2403 if (! keep_head_end_p)
5ece9746 2404 {
e881bb1b
RH
2405 clear_edges ();
2406 VARRAY_FREE (basic_block_info);
2407 n_basic_blocks = 0;
359da67d
RH
2408
2409 ENTRY_BLOCK_PTR->aux = NULL;
2410 ENTRY_BLOCK_PTR->global_live_at_end = NULL;
2411 EXIT_BLOCK_PTR->aux = NULL;
2412 EXIT_BLOCK_PTR->global_live_at_start = NULL;
5ece9746
JL
2413 }
2414}
2415
dc2ede84
BS
2416/* Return nonzero if the destination of SET equals the source. */
2417static int
2418set_noop_p (set)
2419 rtx set;
2420{
2421 rtx src = SET_SRC (set);
2422 rtx dst = SET_DEST (set);
2423 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2424 && REGNO (src) == REGNO (dst))
2425 return 1;
2426 if (GET_CODE (src) != SUBREG || GET_CODE (dst) != SUBREG
2427 || SUBREG_WORD (src) != SUBREG_WORD (dst))
2428 return 0;
2429 src = SUBREG_REG (src);
2430 dst = SUBREG_REG (dst);
2431 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2432 && REGNO (src) == REGNO (dst))
2433 return 1;
2434 return 0;
2435}
2436
2437/* Return nonzero if an insn consists only of SETs, each of which only sets a
2438 value to itself. */
2439static int
2440noop_move_p (insn)
2441 rtx insn;
2442{
2443 rtx pat = PATTERN (insn);
2444
2445 /* Insns carrying these notes are useful later on. */
2446 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
2447 return 0;
2448
2449 if (GET_CODE (pat) == SET && set_noop_p (pat))
2450 return 1;
2451
2452 if (GET_CODE (pat) == PARALLEL)
2453 {
2454 int i;
2455 /* If nothing but SETs of registers to themselves,
2456 this insn can also be deleted. */
2457 for (i = 0; i < XVECLEN (pat, 0); i++)
2458 {
2459 rtx tem = XVECEXP (pat, 0, i);
2460
2461 if (GET_CODE (tem) == USE
2462 || GET_CODE (tem) == CLOBBER)
2463 continue;
2464
2465 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
2466 return 0;
2467 }
2468
2469 return 1;
2470 }
2471 return 0;
2472}
2473
fdb8a883
JW
2474static void
2475notice_stack_pointer_modification (x, pat)
2476 rtx x;
2477 rtx pat ATTRIBUTE_UNUSED;
2478{
2479 if (x == stack_pointer_rtx
2480 /* The stack pointer is only modified indirectly as the result
2481 of a push until later in flow. See the comments in rtl.texi
2482 regarding Embedded Side-Effects on Addresses. */
2483 || (GET_CODE (x) == MEM
2484 && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2485 || GET_CODE (XEXP (x, 0)) == PRE_INC
2486 || GET_CODE (XEXP (x, 0)) == POST_DEC
2487 || GET_CODE (XEXP (x, 0)) == POST_INC)
2488 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
2489 current_function_sp_is_unchanging = 0;
2490}
2491
dc2ede84
BS
2492/* Record which insns refer to any volatile memory
2493 or for any reason can't be deleted just because they are dead stores.
fdb8a883
JW
2494 Also, delete any insns that copy a register to itself.
2495 And see if the stack pointer is modified. */
dc2ede84
BS
2496static void
2497record_volatile_insns (f)
2498 rtx f;
2499{
2500 rtx insn;
2501 for (insn = f; insn; insn = NEXT_INSN (insn))
2502 {
2503 enum rtx_code code1 = GET_CODE (insn);
2504 if (code1 == CALL_INSN)
e881bb1b 2505 SET_INSN_VOLATILE (insn);
dc2ede84
BS
2506 else if (code1 == INSN || code1 == JUMP_INSN)
2507 {
2508 if (GET_CODE (PATTERN (insn)) != USE
2509 && volatile_refs_p (PATTERN (insn)))
e881bb1b 2510 SET_INSN_VOLATILE (insn);
dc2ede84
BS
2511
2512 /* A SET that makes space on the stack cannot be dead.
2513 (Such SETs occur only for allocating variable-size data,
2514 so they will always have a PLUS or MINUS according to the
2515 direction of stack growth.)
2516 Even if this function never uses this stack pointer value,
2517 signal handlers do! */
2518 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
2519 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2520#ifdef STACK_GROWS_DOWNWARD
2521 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
2522#else
2523 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2524#endif
2525 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
e881bb1b 2526 SET_INSN_VOLATILE (insn);
dc2ede84
BS
2527
2528 /* Delete (in effect) any obvious no-op moves. */
2529 else if (noop_move_p (insn))
2530 {
2531 PUT_CODE (insn, NOTE);
2532 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2533 NOTE_SOURCE_FILE (insn) = 0;
2534 }
2535 }
fdb8a883
JW
2536
2537 /* Check if insn modifies the stack pointer. */
2538 if ( current_function_sp_is_unchanging
2539 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2540 note_stores (PATTERN (insn), notice_stack_pointer_modification);
dc2ede84
BS
2541 }
2542}
2543
2544/* Mark those regs which are needed at the end of the function as live
2545 at the end of the last basic block. */
2546static void
2547mark_regs_live_at_end (set)
2548 regset set;
2549{
2550 int i;
2551
e881bb1b
RH
2552 /* If exiting needs the right stack value, consider the stack pointer
2553 live at the end of the function. */
dc2ede84
BS
2554 if (! EXIT_IGNORE_STACK
2555 || (! FRAME_POINTER_REQUIRED
2556 && ! current_function_calls_alloca
fdb8a883
JW
2557 && flag_omit_frame_pointer)
2558 || current_function_sp_is_unchanging)
e881bb1b
RH
2559 {
2560 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
2561 }
dc2ede84 2562
e881bb1b 2563 /* Mark the frame pointer if needed at the end of the function. If
dc2ede84
BS
2564 we end up eliminating it, it will be removed from the live list
2565 of each basic block by reload. */
2566
e4b8a413
JW
2567 if (! reload_completed || frame_pointer_needed)
2568 {
2569 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
dc2ede84 2570#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
2571 /* If they are different, also mark the hard frame pointer as live */
2572 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
dc2ede84 2573#endif
e4b8a413 2574 }
dc2ede84 2575
e881bb1b 2576 /* Mark all global registers, and all registers used by the epilogue
dc2ede84
BS
2577 as being live at the end of the function since they may be
2578 referenced by our caller. */
2579 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2580 if (global_regs[i]
2581#ifdef EPILOGUE_USES
2582 || EPILOGUE_USES (i)
2583#endif
2584 )
2585 SET_REGNO_REG_SET (set, i);
e881bb1b
RH
2586
2587 /* ??? Mark function return value here rather than as uses. */
dc2ede84
BS
2588}
2589
d7429b6a
RK
2590/* Determine which registers are live at the start of each
2591 basic block of the function whose first insn is F.
2592 NREGS is the number of registers used in F.
2593 We allocate the vector basic_block_live_at_start
2594 and the regsets that it points to, and fill them with the data.
2595 regset_size and regset_bytes are also set here. */
2596
2597static void
11f246f6 2598life_analysis_1 (f, nregs, remove_dead_code)
d7429b6a
RK
2599 rtx f;
2600 int nregs;
11f246f6 2601 int remove_dead_code;
d7429b6a 2602{
d7429b6a
RK
2603 int first_pass;
2604 int changed;
d7429b6a 2605 register int i;
6764d250 2606 char save_regs_ever_live[FIRST_PSEUDO_REGISTER];
e881bb1b 2607 regset *new_live_at_end;
d7429b6a
RK
2608
2609 struct obstack flow_obstack;
2610
2611 gcc_obstack_init (&flow_obstack);
2612
2613 max_regno = nregs;
2614
d7429b6a
RK
2615 /* Allocate and zero out many data structures
2616 that will record the data from lifetime analysis. */
2617
359da67d
RH
2618 allocate_reg_life_data ();
2619 allocate_bb_life_data ();
d7429b6a
RK
2620
2621 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
e881bb1b 2622 memset (reg_next_use, 0, nregs * sizeof (rtx));
d7429b6a 2623
e881bb1b 2624 /* Set up regset-vectors used internally within this function.
d7429b6a
RK
2625 Their meanings are documented above, with their declarations. */
2626
e881bb1b
RH
2627 new_live_at_end = (regset *) alloca ((n_basic_blocks + 1) * sizeof (regset));
2628 init_regset_vector (new_live_at_end, n_basic_blocks + 1, &flow_obstack);
4c9a05bc 2629
e881bb1b
RH
2630 /* Stick these vectors into the AUX field of the basic block, so that
2631 we don't have to keep going through the index. */
d7429b6a 2632
e881bb1b
RH
2633 for (i = 0; i < n_basic_blocks; ++i)
2634 BASIC_BLOCK (i)->aux = new_live_at_end[i];
2635 ENTRY_BLOCK_PTR->aux = new_live_at_end[i];
d7429b6a 2636
fdb8a883
JW
2637 /* Assume that the stack pointer is unchanging if alloca hasn't been used.
2638 This will be cleared by record_volatile_insns if it encounters an insn
2639 which modifies the stack pointer. */
2640 current_function_sp_is_unchanging = !current_function_calls_alloca;
2641
dc2ede84 2642 record_volatile_insns (f);
fe0f9c4b
RK
2643
2644 if (n_basic_blocks > 0)
2645 {
e881bb1b
RH
2646 regset theend;
2647 register edge e;
2648
2649 theend = EXIT_BLOCK_PTR->global_live_at_start;
2650 mark_regs_live_at_end (theend);
2651
2652 /* Propogate this exit data to each of EXIT's predecessors. */
2653 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2654 {
2655 COPY_REG_SET (e->src->global_live_at_end, theend);
2656 COPY_REG_SET ((regset) e->src->aux, theend);
2657 }
dc2ede84 2658 }
d7429b6a 2659
e881bb1b
RH
2660 /* The post-reload life analysis have (on a global basis) the same registers
2661 live as was computed by reload itself.
2662
2663 Otherwise elimination offsets and such may be incorrect.
2664
2665 Reload will make some registers as live even though they do not appear
2666 in the rtl. */
2667 if (reload_completed)
2668 memcpy (save_regs_ever_live, regs_ever_live, sizeof (regs_ever_live));
2669 memset (regs_ever_live, 0, sizeof regs_ever_live);
2670
d7429b6a
RK
2671 /* Propagate life info through the basic blocks
2672 around the graph of basic blocks.
2673
2674 This is a relaxation process: each time a new register
2675 is live at the end of the basic block, we must scan the block
2676 to determine which registers are, as a consequence, live at the beginning
2677 of that block. These registers must then be marked live at the ends
2678 of all the blocks that can transfer control to that block.
2679 The process continues until it reaches a fixed point. */
2680
2681 first_pass = 1;
2682 changed = 1;
2683 while (changed)
2684 {
2685 changed = 0;
2686 for (i = n_basic_blocks - 1; i >= 0; i--)
2687 {
e881bb1b 2688 basic_block bb = BASIC_BLOCK (i);
d7429b6a
RK
2689 int consider = first_pass;
2690 int must_rescan = first_pass;
2691 register int j;
2692
2693 if (!first_pass)
2694 {
2695 /* Set CONSIDER if this block needs thinking about at all
2696 (that is, if the regs live now at the end of it
2697 are not the same as were live at the end of it when
2698 we last thought about it).
2699 Set must_rescan if it needs to be thought about
2700 instruction by instruction (that is, if any additional
2701 reg that is live at the end now but was not live there before
2702 is one of the significant regs of this basic block). */
2703
b5835272 2704 EXECUTE_IF_AND_COMPL_IN_REG_SET
e881bb1b 2705 ((regset) bb->aux, bb->global_live_at_end, 0, j,
b5835272
RK
2706 {
2707 consider = 1;
e881bb1b 2708 if (REGNO_REG_SET_P (bb->local_set, j))
b5835272
RK
2709 {
2710 must_rescan = 1;
2711 goto done;
2712 }
2713 });
916b1701 2714 done:
d7429b6a
RK
2715 if (! consider)
2716 continue;
2717 }
2718
2719 /* The live_at_start of this block may be changing,
2720 so another pass will be required after this one. */
2721 changed = 1;
2722
2723 if (! must_rescan)
2724 {
2725 /* No complete rescan needed;
2726 just record those variables newly known live at end
2727 as live at start as well. */
e881bb1b
RH
2728 IOR_AND_COMPL_REG_SET (bb->global_live_at_start,
2729 (regset) bb->aux,
2730 bb->global_live_at_end);
916b1701 2731
e881bb1b
RH
2732 IOR_AND_COMPL_REG_SET (bb->global_live_at_end,
2733 (regset) bb->aux,
2734 bb->global_live_at_end);
d7429b6a
RK
2735 }
2736 else
2737 {
2738 /* Update the basic_block_live_at_start
2739 by propagation backwards through the block. */
e881bb1b
RH
2740 COPY_REG_SET (bb->global_live_at_end, (regset) bb->aux);
2741 COPY_REG_SET (bb->global_live_at_start,
2742 bb->global_live_at_end);
2743 propagate_block (bb->global_live_at_start,
2744 bb->head, bb->end, 0,
2745 first_pass ? bb->local_set : (regset) 0,
11f246f6 2746 i, remove_dead_code);
d7429b6a
RK
2747 }
2748
e881bb1b 2749 /* Update the new_live_at_end's of the block's predecessors. */
d7429b6a 2750 {
e881bb1b 2751 register edge e;
af14ce9c 2752
e881bb1b
RH
2753 for (e = bb->pred; e ; e = e->pred_next)
2754 IOR_REG_SET ((regset) e->src->aux, bb->global_live_at_start);
d7429b6a 2755 }
e881bb1b 2756
d7429b6a
RK
2757#ifdef USE_C_ALLOCA
2758 alloca (0);
2759#endif
2760 }
2761 first_pass = 0;
2762 }
2763
2764 /* The only pseudos that are live at the beginning of the function are
2765 those that were not set anywhere in the function. local-alloc doesn't
2766 know how to handle these correctly, so mark them as not local to any
2767 one basic block. */
2768
2769 if (n_basic_blocks > 0)
e881bb1b 2770 EXECUTE_IF_SET_IN_REG_SET (BASIC_BLOCK (0)->global_live_at_start,
916b1701
MM
2771 FIRST_PSEUDO_REGISTER, i,
2772 {
2773 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2774 });
d7429b6a 2775
e881bb1b
RH
2776 /* Now the life information is accurate. Make one more pass over each
2777 basic block to delete dead stores, create autoincrement addressing
2778 and record how many times each register is used, is set, or dies. */
d7429b6a 2779
d7429b6a
RK
2780 for (i = 0; i < n_basic_blocks; i++)
2781 {
e881bb1b
RH
2782 basic_block bb = BASIC_BLOCK (i);
2783
2784 /* We start with global_live_at_end to determine which stores are
2785 dead. This process is destructive, and we wish to preserve the
2786 contents of global_live_at_end for posterity. Fortunately,
2787 new_live_at_end, due to the way we converged on a solution,
2788 contains a duplicate of global_live_at_end that we can kill. */
11f246f6 2789 propagate_block ((regset) bb->aux, bb->head, bb->end, 1, (regset) 0, i, remove_dead_code);
e881bb1b 2790
d7429b6a
RK
2791#ifdef USE_C_ALLOCA
2792 alloca (0);
2793#endif
2794 }
2795
e881bb1b
RH
2796 /* We have a problem with any pseudoreg that lives across the setjmp.
2797 ANSI says that if a user variable does not change in value between
2798 the setjmp and the longjmp, then the longjmp preserves it. This
2799 includes longjmp from a place where the pseudo appears dead.
d7429b6a
RK
2800 (In principle, the value still exists if it is in scope.)
2801 If the pseudo goes in a hard reg, some other value may occupy
2802 that hard reg where this pseudo is dead, thus clobbering the pseudo.
2803 Conclusion: such a pseudo must not go in a hard reg. */
916b1701
MM
2804 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
2805 FIRST_PSEUDO_REGISTER, i,
2806 {
2807 if (regno_reg_rtx[i] != 0)
2808 {
2809 REG_LIVE_LENGTH (i) = -1;
2810 REG_BASIC_BLOCK (i) = -1;
2811 }
2812 });
d7429b6a 2813
6764d250
BS
2814 /* Restore regs_ever_live that was provided by reload. */
2815 if (reload_completed)
e881bb1b 2816 memcpy (regs_ever_live, save_regs_ever_live, sizeof (regs_ever_live));
67f0e213 2817
e881bb1b 2818 free_regset_vector (new_live_at_end, n_basic_blocks);
5f4f0e22 2819 obstack_free (&flow_obstack, NULL_PTR);
e881bb1b
RH
2820
2821 for (i = 0; i < n_basic_blocks; ++i)
2822 BASIC_BLOCK (i)->aux = NULL;
2823 ENTRY_BLOCK_PTR->aux = NULL;
d7429b6a
RK
2824}
2825\f
2826/* Subroutines of life analysis. */
2827
2828/* Allocate the permanent data structures that represent the results
2829 of life analysis. Not static since used also for stupid life analysis. */
2830
2831void
359da67d 2832allocate_bb_life_data ()
d7429b6a
RK
2833{
2834 register int i;
d7429b6a 2835
e881bb1b
RH
2836 for (i = 0; i < n_basic_blocks; i++)
2837 {
2838 basic_block bb = BASIC_BLOCK (i);
2839
2840 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
2841 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
2842 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
2843 }
2844
2845 ENTRY_BLOCK_PTR->global_live_at_end
2846 = OBSTACK_ALLOC_REG_SET (function_obstack);
2847 EXIT_BLOCK_PTR->global_live_at_start
2848 = OBSTACK_ALLOC_REG_SET (function_obstack);
d7429b6a 2849
7eb136d6 2850 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
359da67d
RH
2851}
2852
2853void
2854allocate_reg_life_data ()
2855{
2856 int i;
2857
2858 /* Recalculate the register space, in case it has grown. Old style
2859 vector oriented regsets would set regset_{size,bytes} here also. */
2860 allocate_reg_info (max_regno, FALSE, FALSE);
2861
2862 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
2863 information, explicitly reset it here. The allocation should have
2864 already happened on the previous reg_scan pass. Make sure in case
2865 some more registers were allocated. */
2866 for (i = 0; i < max_regno; i++)
2867 REG_N_SETS (i) = 0;
d7429b6a
RK
2868}
2869
67f0e213
RK
2870/* Make each element of VECTOR point at a regset. The vector has
2871 NELTS elements, and space is allocated from the ALLOC_OBSTACK
2872 obstack. */
d7429b6a 2873
04821e98 2874static void
67f0e213 2875init_regset_vector (vector, nelts, alloc_obstack)
d7429b6a 2876 regset *vector;
d7429b6a 2877 int nelts;
7eb136d6 2878 struct obstack *alloc_obstack;
d7429b6a
RK
2879{
2880 register int i;
d7429b6a
RK
2881
2882 for (i = 0; i < nelts; i++)
2883 {
7eb136d6
MM
2884 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
2885 CLEAR_REG_SET (vector[i]);
d7429b6a
RK
2886 }
2887}
e658434c 2888
67f0e213
RK
2889/* Release any additional space allocated for each element of VECTOR point
2890 other than the regset header itself. The vector has NELTS elements. */
2891
2892void
2893free_regset_vector (vector, nelts)
2894 regset *vector;
2895 int nelts;
2896{
2897 register int i;
2898
2899 for (i = 0; i < nelts; i++)
2900 FREE_REG_SET (vector[i]);
2901}
2902
d7429b6a
RK
2903/* Compute the registers live at the beginning of a basic block
2904 from those live at the end.
2905
2906 When called, OLD contains those live at the end.
2907 On return, it contains those live at the beginning.
2908 FIRST and LAST are the first and last insns of the basic block.
2909
2910 FINAL is nonzero if we are doing the final pass which is not
2911 for computing the life info (since that has already been done)
2912 but for acting on it. On this pass, we delete dead stores,
2913 set up the logical links and dead-variables lists of instructions,
2914 and merge instructions for autoincrement and autodecrement addresses.
2915
2916 SIGNIFICANT is nonzero only the first time for each basic block.
2917 If it is nonzero, it points to a regset in which we store
2918 a 1 for each register that is set within the block.
2919
2920 BNUM is the number of the basic block. */
2921
2922static void
11f246f6 2923propagate_block (old, first, last, final, significant, bnum, remove_dead_code)
d7429b6a
RK
2924 register regset old;
2925 rtx first;
2926 rtx last;
2927 int final;
2928 regset significant;
2929 int bnum;
11f246f6 2930 int remove_dead_code;
d7429b6a
RK
2931{
2932 register rtx insn;
2933 rtx prev;
2934 regset live;
2935 regset dead;
2936
e881bb1b
RH
2937 /* Find the loop depth for this block. Ignore loop level changes in the
2938 middle of the basic block -- for register allocation purposes, the
2939 important uses will be in the blocks wholely contained within the loop
2940 not in the loop pre-header or post-trailer. */
2941 loop_depth = BASIC_BLOCK (bnum)->loop_depth;
d7429b6a 2942
7eb136d6
MM
2943 dead = ALLOCA_REG_SET ();
2944 live = ALLOCA_REG_SET ();
d7429b6a
RK
2945
2946 cc0_live = 0;
db3a887b 2947 mem_set_list = NULL_RTX;
d7429b6a 2948
d7429b6a
RK
2949 if (final)
2950 {
916b1701 2951 register int i;
d7429b6a 2952
d7429b6a 2953 /* Process the regs live at the end of the block.
f8dd7f98 2954 Mark them as not local to any one basic block. */
916b1701
MM
2955 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2956 {
2957 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
916b1701 2958 });
d7429b6a
RK
2959 }
2960
2961 /* Scan the block an insn at a time from end to beginning. */
2962
2963 for (insn = last; ; insn = prev)
2964 {
2965 prev = PREV_INSN (insn);
2966
8329b5ec 2967 if (GET_CODE (insn) == NOTE)
d7429b6a 2968 {
8329b5ec
DE
2969 /* If this is a call to `setjmp' et al,
2970 warn if any non-volatile datum is live. */
2971
2972 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
916b1701 2973 IOR_REG_SET (regs_live_at_setjmp, old);
d7429b6a
RK
2974 }
2975
2976 /* Update the life-status of regs for this insn.
2977 First DEAD gets which regs are set in this insn
2978 then LIVE gets which regs are used in this insn.
2979 Then the regs live before the insn
2980 are those live after, with DEAD regs turned off,
2981 and then LIVE regs turned on. */
2982
8329b5ec 2983 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
d7429b6a
RK
2984 {
2985 register int i;
5f4f0e22 2986 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
11f246f6
JH
2987 int insn_is_dead = 0;
2988 int libcall_is_dead = 0;
2989
2990 if (remove_dead_code)
2991 {
2992 insn_is_dead = (insn_dead_p (PATTERN (insn), old, 0, REG_NOTES (insn))
2993 /* Don't delete something that refers to volatile storage! */
2994 && ! INSN_VOLATILE (insn));
2995 libcall_is_dead = (insn_is_dead && note != 0
2996 && libcall_dead_p (PATTERN (insn), old, note, insn));
2997 }
d7429b6a
RK
2998
2999 /* If an instruction consists of just dead store(s) on final pass,
3000 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
3001 We could really delete it with delete_insn, but that
3002 can cause trouble for first or last insn in a basic block. */
b590bbfd 3003 if (final && insn_is_dead)
d7429b6a
RK
3004 {
3005 PUT_CODE (insn, NOTE);
3006 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3007 NOTE_SOURCE_FILE (insn) = 0;
3008
e5df1ea3
RK
3009 /* CC0 is now known to be dead. Either this insn used it,
3010 in which case it doesn't anymore, or clobbered it,
3011 so the next insn can't use it. */
3012 cc0_live = 0;
3013
d7429b6a
RK
3014 /* If this insn is copying the return value from a library call,
3015 delete the entire library call. */
3016 if (libcall_is_dead)
3017 {
3018 rtx first = XEXP (note, 0);
3019 rtx p = insn;
3020 while (INSN_DELETED_P (first))
3021 first = NEXT_INSN (first);
3022 while (p != first)
3023 {
3024 p = PREV_INSN (p);
3025 PUT_CODE (p, NOTE);
3026 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3027 NOTE_SOURCE_FILE (p) = 0;
3028 }
3029 }
3030 goto flushed;
3031 }
3032
916b1701
MM
3033 CLEAR_REG_SET (dead);
3034 CLEAR_REG_SET (live);
d7429b6a
RK
3035
3036 /* See if this is an increment or decrement that can be
3037 merged into a following memory address. */
3038#ifdef AUTO_INC_DEC
3039 {
956d6950
JL
3040 register rtx x = single_set (insn);
3041
d7429b6a 3042 /* Does this instruction increment or decrement a register? */
6764d250
BS
3043 if (!reload_completed
3044 && final && x != 0
d7429b6a
RK
3045 && GET_CODE (SET_DEST (x)) == REG
3046 && (GET_CODE (SET_SRC (x)) == PLUS
3047 || GET_CODE (SET_SRC (x)) == MINUS)
3048 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
3049 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3050 /* Ok, look for a following memory ref we can combine with.
3051 If one is found, change the memory ref to a PRE_INC
3052 or PRE_DEC, cancel this insn, and return 1.
3053 Return 0 if nothing has been done. */
3054 && try_pre_increment_1 (insn))
3055 goto flushed;
3056 }
3057#endif /* AUTO_INC_DEC */
3058
3059 /* If this is not the final pass, and this insn is copying the
3060 value of a library call and it's dead, don't scan the
3061 insns that perform the library call, so that the call's
3062 arguments are not marked live. */
3063 if (libcall_is_dead)
3064 {
3065 /* Mark the dest reg as `significant'. */
5f4f0e22 3066 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
d7429b6a
RK
3067
3068 insn = XEXP (note, 0);
3069 prev = PREV_INSN (insn);
3070 }
3071 else if (GET_CODE (PATTERN (insn)) == SET
3072 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
3073 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
3074 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
3075 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
3076 /* We have an insn to pop a constant amount off the stack.
3077 (Such insns use PLUS regardless of the direction of the stack,
3078 and any insn to adjust the stack by a constant is always a pop.)
3079 These insns, if not dead stores, have no effect on life. */
3080 ;
3081 else
3082 {
f8dd7f98
BS
3083 /* Any regs live at the time of a call instruction
3084 must not go in a register clobbered by calls.
3085 Find all regs now live and record this for them. */
3086
3087 if (GET_CODE (insn) == CALL_INSN && final)
3088 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
3089 {
3090 REG_N_CALLS_CROSSED (i)++;
3091 });
3092
d7429b6a
RK
3093 /* LIVE gets the regs used in INSN;
3094 DEAD gets those set by it. Dead insns don't make anything
3095 live. */
3096
5f4f0e22
CH
3097 mark_set_regs (old, dead, PATTERN (insn),
3098 final ? insn : NULL_RTX, significant);
d7429b6a
RK
3099
3100 /* If an insn doesn't use CC0, it becomes dead since we
3101 assume that every insn clobbers it. So show it dead here;
3102 mark_used_regs will set it live if it is referenced. */
3103 cc0_live = 0;
3104
3105 if (! insn_is_dead)
3106 mark_used_regs (old, live, PATTERN (insn), final, insn);
3107
3108 /* Sometimes we may have inserted something before INSN (such as
3109 a move) when we make an auto-inc. So ensure we will scan
3110 those insns. */
3111#ifdef AUTO_INC_DEC
3112 prev = PREV_INSN (insn);
3113#endif
3114
3115 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
3116 {
3117 register int i;
3118
6b67ec08
RK
3119 rtx note;
3120
3121 for (note = CALL_INSN_FUNCTION_USAGE (insn);
3122 note;
3123 note = XEXP (note, 1))
3124 if (GET_CODE (XEXP (note, 0)) == USE)
83ab3839 3125 mark_used_regs (old, live, XEXP (XEXP (note, 0), 0),
6b67ec08
RK
3126 final, insn);
3127
d7429b6a 3128 /* Each call clobbers all call-clobbered regs that are not
e4329280 3129 global or fixed. Note that the function-value reg is a
d7429b6a
RK
3130 call-clobbered reg, and mark_set_regs has already had
3131 a chance to handle it. */
3132
3133 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
e4329280
RK
3134 if (call_used_regs[i] && ! global_regs[i]
3135 && ! fixed_regs[i])
916b1701 3136 SET_REGNO_REG_SET (dead, i);
d7429b6a
RK
3137
3138 /* The stack ptr is used (honorarily) by a CALL insn. */
916b1701 3139 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
d7429b6a
RK
3140
3141 /* Calls may also reference any of the global registers,
3142 so they are made live. */
d7429b6a
RK
3143 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3144 if (global_regs[i])
9b316aa2 3145 mark_used_regs (old, live,
38a448ca 3146 gen_rtx_REG (reg_raw_mode[i], i),
9b316aa2 3147 final, insn);
d7429b6a
RK
3148
3149 /* Calls also clobber memory. */
db3a887b 3150 mem_set_list = NULL_RTX;
d7429b6a
RK
3151 }
3152
3153 /* Update OLD for the registers used or set. */
916b1701
MM
3154 AND_COMPL_REG_SET (old, dead);
3155 IOR_REG_SET (old, live);
d7429b6a 3156
d7429b6a
RK
3157 }
3158
f8dd7f98
BS
3159 /* On final pass, update counts of how many insns each reg is live
3160 at. */
d7429b6a 3161 if (final)
f8dd7f98
BS
3162 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
3163 { REG_LIVE_LENGTH (i)++; });
d7429b6a
RK
3164 }
3165 flushed: ;
3166 if (insn == first)
3167 break;
3168 }
3169
67f0e213
RK
3170 FREE_REG_SET (dead);
3171 FREE_REG_SET (live);
d7429b6a
RK
3172}
3173\f
3174/* Return 1 if X (the body of an insn, or part of it) is just dead stores
3175 (SET expressions whose destinations are registers dead after the insn).
3176 NEEDED is the regset that says which regs are alive after the insn.
3177
e398aa80
R
3178 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
3179
3180 If X is the entire body of an insn, NOTES contains the reg notes
3181 pertaining to the insn. */
d7429b6a
RK
3182
3183static int
e398aa80 3184insn_dead_p (x, needed, call_ok, notes)
d7429b6a
RK
3185 rtx x;
3186 regset needed;
3187 int call_ok;
e398aa80 3188 rtx notes ATTRIBUTE_UNUSED;
d7429b6a 3189{
e5e809f4
JL
3190 enum rtx_code code = GET_CODE (x);
3191
e398aa80
R
3192#ifdef AUTO_INC_DEC
3193 /* If flow is invoked after reload, we must take existing AUTO_INC
3194 expresions into account. */
3195 if (reload_completed)
3196 {
3197 for ( ; notes; notes = XEXP (notes, 1))
3198 {
3199 if (REG_NOTE_KIND (notes) == REG_INC)
3200 {
3201 int regno = REGNO (XEXP (notes, 0));
3202
3203 /* Don't delete insns to set global regs. */
3204 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
3205 || REGNO_REG_SET_P (needed, regno))
3206 return 0;
3207 }
3208 }
3209 }
3210#endif
3211
d7429b6a
RK
3212 /* If setting something that's a reg or part of one,
3213 see if that register's altered value will be live. */
3214
3215 if (code == SET)
3216 {
e5e809f4
JL
3217 rtx r = SET_DEST (x);
3218
d7429b6a
RK
3219 /* A SET that is a subroutine call cannot be dead. */
3220 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
3221 return 0;
3222
3223#ifdef HAVE_cc0
3224 if (GET_CODE (r) == CC0)
3225 return ! cc0_live;
3226#endif
3227
db3a887b
CB
3228 if (GET_CODE (r) == MEM && ! MEM_VOLATILE_P (r))
3229 {
3230 rtx temp;
3231 /* Walk the set of memory locations we are currently tracking
3232 and see if one is an identical match to this memory location.
3233 If so, this memory write is dead (remember, we're walking
3234 backwards from the end of the block to the start. */
3235 temp = mem_set_list;
3236 while (temp)
3237 {
3238 if (rtx_equal_p (XEXP (temp, 0), r))
3239 return 1;
3240 temp = XEXP (temp, 1);
3241 }
3242 }
d7429b6a 3243
e5e809f4
JL
3244 while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART
3245 || GET_CODE (r) == ZERO_EXTRACT)
355fca3e 3246 r = XEXP (r, 0);
d7429b6a
RK
3247
3248 if (GET_CODE (r) == REG)
3249 {
e5e809f4 3250 int regno = REGNO (r);
d7429b6a 3251
d8c8b8e3 3252 /* Don't delete insns to set global regs. */
d7429b6a
RK
3253 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
3254 /* Make sure insns to set frame pointer aren't deleted. */
e4b8a413
JW
3255 || (regno == FRAME_POINTER_REGNUM
3256 && (! reload_completed || frame_pointer_needed))
73a187c1 3257#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
3258 || (regno == HARD_FRAME_POINTER_REGNUM
3259 && (! reload_completed || frame_pointer_needed))
73a187c1 3260#endif
d7e4fe8b
RS
3261#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3262 /* Make sure insns to set arg pointer are never deleted
3263 (if the arg pointer isn't fixed, there will be a USE for
0f41302f 3264 it, so we can treat it normally). */
d7e4fe8b
RS
3265 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3266#endif
916b1701 3267 || REGNO_REG_SET_P (needed, regno))
d7429b6a
RK
3268 return 0;
3269
3270 /* If this is a hard register, verify that subsequent words are
3271 not needed. */
3272 if (regno < FIRST_PSEUDO_REGISTER)
3273 {
3274 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
3275
3276 while (--n > 0)
916b1701 3277 if (REGNO_REG_SET_P (needed, regno+n))
d7429b6a
RK
3278 return 0;
3279 }
3280
3281 return 1;
3282 }
3283 }
e5e809f4 3284
d7429b6a
RK
3285 /* If performing several activities,
3286 insn is dead if each activity is individually dead.
3287 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
3288 that's inside a PARALLEL doesn't make the insn worth keeping. */
3289 else if (code == PARALLEL)
3290 {
e5e809f4
JL
3291 int i = XVECLEN (x, 0);
3292
d7429b6a 3293 for (i--; i >= 0; i--)
e5e809f4
JL
3294 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
3295 && GET_CODE (XVECEXP (x, 0, i)) != USE
e398aa80 3296 && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok, NULL_RTX))
e5e809f4
JL
3297 return 0;
3298
d7429b6a
RK
3299 return 1;
3300 }
e5e809f4
JL
3301
3302 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
3303 is not necessarily true for hard registers. */
3304 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
3305 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
3306 && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0))))
3307 return 1;
3308
3309 /* We do not check other CLOBBER or USE here. An insn consisting of just
3310 a CLOBBER or just a USE should not be deleted. */
d7429b6a
RK
3311 return 0;
3312}
3313
3314/* If X is the pattern of the last insn in a libcall, and assuming X is dead,
3315 return 1 if the entire library call is dead.
3316 This is true if X copies a register (hard or pseudo)
3317 and if the hard return reg of the call insn is dead.
3318 (The caller should have tested the destination of X already for death.)
3319
3320 If this insn doesn't just copy a register, then we don't
3321 have an ordinary libcall. In that case, cse could not have
3322 managed to substitute the source for the dest later on,
3323 so we can assume the libcall is dead.
3324
3325 NEEDED is the bit vector of pseudoregs live before this insn.
3326 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
3327
3328static int
3329libcall_dead_p (x, needed, note, insn)
3330 rtx x;
3331 regset needed;
3332 rtx note;
3333 rtx insn;
3334{
3335 register RTX_CODE code = GET_CODE (x);
3336
3337 if (code == SET)
3338 {
3339 register rtx r = SET_SRC (x);
3340 if (GET_CODE (r) == REG)
3341 {
3342 rtx call = XEXP (note, 0);
e398aa80 3343 rtx call_pat;
d7429b6a
RK
3344 register int i;
3345
3346 /* Find the call insn. */
3347 while (call != insn && GET_CODE (call) != CALL_INSN)
3348 call = NEXT_INSN (call);
3349
3350 /* If there is none, do nothing special,
3351 since ordinary death handling can understand these insns. */
3352 if (call == insn)
3353 return 0;
3354
3355 /* See if the hard reg holding the value is dead.
3356 If this is a PARALLEL, find the call within it. */
e398aa80
R
3357 call_pat = PATTERN (call);
3358 if (GET_CODE (call_pat) == PARALLEL)
d7429b6a 3359 {
e398aa80
R
3360 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
3361 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
3362 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
d7429b6a
RK
3363 break;
3364
761a5bcd
JW
3365 /* This may be a library call that is returning a value
3366 via invisible pointer. Do nothing special, since
3367 ordinary death handling can understand these insns. */
d7429b6a 3368 if (i < 0)
761a5bcd 3369 return 0;
d7429b6a 3370
e398aa80 3371 call_pat = XVECEXP (call_pat, 0, i);
d7429b6a
RK
3372 }
3373
e398aa80 3374 return insn_dead_p (call_pat, needed, 1, REG_NOTES (call));
d7429b6a
RK
3375 }
3376 }
3377 return 1;
3378}
3379
bd80fbde
RH
3380/* Return 1 if register REGNO was used before it was set, i.e. if it is
3381 live at function entry. Don't count global register variables, variables
3382 in registers that can be used for function arg passing, or variables in
3383 fixed hard registers. */
d7429b6a
RK
3384
3385int
3386regno_uninitialized (regno)
3387 int regno;
3388{
b0b7b46a 3389 if (n_basic_blocks == 0
6a45254e 3390 || (regno < FIRST_PSEUDO_REGISTER
bd80fbde
RH
3391 && (global_regs[regno]
3392 || fixed_regs[regno]
3393 || FUNCTION_ARG_REGNO_P (regno))))
d7429b6a
RK
3394 return 0;
3395
e881bb1b 3396 return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
d7429b6a
RK
3397}
3398
3399/* 1 if register REGNO was alive at a place where `setjmp' was called
3400 and was set more than once or is an argument.
3401 Such regs may be clobbered by `longjmp'. */
3402
3403int
3404regno_clobbered_at_setjmp (regno)
3405 int regno;
3406{
3407 if (n_basic_blocks == 0)
3408 return 0;
3409
b1f21e0a 3410 return ((REG_N_SETS (regno) > 1
e881bb1b 3411 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
916b1701 3412 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
d7429b6a
RK
3413}
3414\f
15e088b2
JL
3415/* INSN references memory, possibly using autoincrement addressing modes.
3416 Find any entries on the mem_set_list that need to be invalidated due
3417 to an address change. */
3418static void
3419invalidate_mems_from_autoinc (insn)
3420 rtx insn;
3421{
3422 rtx note = REG_NOTES (insn);
3423 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3424 {
3425 if (REG_NOTE_KIND (note) == REG_INC)
3426 {
3427 rtx temp = mem_set_list;
3428 rtx prev = NULL_RTX;
3429
3430 while (temp)
3431 {
3432 if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
3433 {
3434 /* Splice temp out of list. */
3435 if (prev)
3436 XEXP (prev, 1) = XEXP (temp, 1);
3437 else
3438 mem_set_list = XEXP (temp, 1);
3439 }
3440 else
3441 prev = temp;
3442 temp = XEXP (temp, 1);
3443 }
3444 }
3445 }
3446}
3447
d7429b6a
RK
3448/* Process the registers that are set within X.
3449 Their bits are set to 1 in the regset DEAD,
3450 because they are dead prior to this insn.
3451
3452 If INSN is nonzero, it is the insn being processed
3453 and the fact that it is nonzero implies this is the FINAL pass
3454 in propagate_block. In this case, various info about register
3455 usage is stored, LOG_LINKS fields of insns are set up. */
3456
d7429b6a
RK
3457static void
3458mark_set_regs (needed, dead, x, insn, significant)
3459 regset needed;
3460 regset dead;
3461 rtx x;
3462 rtx insn;
3463 regset significant;
3464{
3465 register RTX_CODE code = GET_CODE (x);
3466
3467 if (code == SET || code == CLOBBER)
3468 mark_set_1 (needed, dead, x, insn, significant);
3469 else if (code == PARALLEL)
3470 {
3471 register int i;
3472 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3473 {
3474 code = GET_CODE (XVECEXP (x, 0, i));
3475 if (code == SET || code == CLOBBER)
3476 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
3477 }
3478 }
3479}
3480
3481/* Process a single SET rtx, X. */
3482
3483static void
3484mark_set_1 (needed, dead, x, insn, significant)
3485 regset needed;
3486 regset dead;
3487 rtx x;
3488 rtx insn;
3489 regset significant;
3490{
3491 register int regno;
3492 register rtx reg = SET_DEST (x);
3493
86465af7
DM
3494 /* Some targets place small structures in registers for
3495 return values of functions. We have to detect this
3496 case specially here to get correct flow information. */
3497 if (GET_CODE (reg) == PARALLEL
3498 && GET_MODE (reg) == BLKmode)
3499 {
3500 register int i;
3501
3502 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
3503 mark_set_1 (needed, dead, XVECEXP (reg, 0, i), insn, significant);
3504 return;
3505 }
3506
d7429b6a
RK
3507 /* Modifying just one hardware register of a multi-reg value
3508 or just a byte field of a register
3509 does not mean the value from before this insn is now dead.
3510 But it does mean liveness of that register at the end of the block
3511 is significant.
3512
3513 Within mark_set_1, however, we treat it as if the register is
3514 indeed modified. mark_used_regs will, however, also treat this
3515 register as being used. Thus, we treat these insns as setting a
3516 new value for the register as a function of its old value. This
3517 cases LOG_LINKS to be made appropriately and this will help combine. */
3518
3519 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
3520 || GET_CODE (reg) == SIGN_EXTRACT
3521 || GET_CODE (reg) == STRICT_LOW_PART)
3522 reg = XEXP (reg, 0);
3523
db3a887b
CB
3524 /* If this set is a MEM, then it kills any aliased writes.
3525 If this set is a REG, then it kills any MEMs which use the reg. */
d7429b6a 3526 if (GET_CODE (reg) == MEM
db3a887b
CB
3527 || GET_CODE (reg) == REG)
3528 {
3529 rtx temp = mem_set_list;
3530 rtx prev = NULL_RTX;
3531
3532 while (temp)
3533 {
3534 if ((GET_CODE (reg) == MEM
3535 && output_dependence (XEXP (temp, 0), reg))
3536 || (GET_CODE (reg) == REG
3537 && reg_overlap_mentioned_p (reg, XEXP (temp, 0))))
3538 {
3539 /* Splice this entry out of the list. */
3540 if (prev)
3541 XEXP (prev, 1) = XEXP (temp, 1);
3542 else
3543 mem_set_list = XEXP (temp, 1);
3544 }
3545 else
3546 prev = temp;
3547 temp = XEXP (temp, 1);
3548 }
3549 }
15e088b2
JL
3550
3551 /* If the memory reference had embedded side effects (autoincrement
3552 address modes. Then we may need to kill some entries on the
3553 memory set list. */
3554 if (insn && GET_CODE (reg) == MEM)
3555 invalidate_mems_from_autoinc (insn);
3556
d7429b6a 3557 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
3ce7c5a2
JL
3558 /* We do not know the size of a BLKmode store, so we do not track
3559 them for redundant store elimination. */
3560 && GET_MODE (reg) != BLKmode
d7429b6a
RK
3561 /* There are no REG_INC notes for SP, so we can't assume we'll see
3562 everything that invalidates it. To be safe, don't eliminate any
3563 stores though SP; none of them should be redundant anyway. */
3564 && ! reg_mentioned_p (stack_pointer_rtx, reg))
db3a887b 3565 mem_set_list = gen_rtx_EXPR_LIST (VOIDmode, reg, mem_set_list);
d7429b6a
RK
3566
3567 if (GET_CODE (reg) == REG
e4b8a413
JW
3568 && (regno = REGNO (reg), ! (regno == FRAME_POINTER_REGNUM
3569 && (! reload_completed || frame_pointer_needed)))
73a187c1 3570#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
3571 && ! (regno == HARD_FRAME_POINTER_REGNUM
3572 && (! reload_completed || frame_pointer_needed))
73a187c1 3573#endif
d7e4fe8b
RS
3574#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3575 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3576#endif
d7429b6a
RK
3577 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
3578 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
3579 {
916b1701
MM
3580 int some_needed = REGNO_REG_SET_P (needed, regno);
3581 int some_not_needed = ! some_needed;
d7429b6a
RK
3582
3583 /* Mark it as a significant register for this basic block. */
3584 if (significant)
916b1701 3585 SET_REGNO_REG_SET (significant, regno);
d7429b6a 3586
38e01259 3587 /* Mark it as dead before this insn. */
916b1701 3588 SET_REGNO_REG_SET (dead, regno);
d7429b6a
RK
3589
3590 /* A hard reg in a wide mode may really be multiple registers.
3591 If so, mark all of them just like the first. */
3592 if (regno < FIRST_PSEUDO_REGISTER)
3593 {
3594 int n;
3595
3596 /* Nothing below is needed for the stack pointer; get out asap.
3597 Eg, log links aren't needed, since combine won't use them. */
3598 if (regno == STACK_POINTER_REGNUM)
3599 return;
3600
3601 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3602 while (--n > 0)
3603 {
916b1701
MM
3604 int regno_n = regno + n;
3605 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
d7429b6a 3606 if (significant)
916b1701 3607 SET_REGNO_REG_SET (significant, regno_n);
cb9e8ad1 3608
916b1701
MM
3609 SET_REGNO_REG_SET (dead, regno_n);
3610 some_needed |= needed_regno;
3611 some_not_needed |= ! needed_regno;
d7429b6a
RK
3612 }
3613 }
3614 /* Additional data to record if this is the final pass. */
3615 if (insn)
3616 {
3617 register rtx y = reg_next_use[regno];
3618 register int blocknum = BLOCK_NUM (insn);
3619
3620 /* If this is a hard reg, record this function uses the reg. */
3621
3622 if (regno < FIRST_PSEUDO_REGISTER)
3623 {
3624 register int i;
3625 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
3626
3627 for (i = regno; i < endregno; i++)
3628 {
93514916
JW
3629 /* The next use is no longer "next", since a store
3630 intervenes. */
3631 reg_next_use[i] = 0;
3632
d7429b6a 3633 regs_ever_live[i] = 1;
b1f21e0a 3634 REG_N_SETS (i)++;
d7429b6a
RK
3635 }
3636 }
3637 else
3638 {
93514916
JW
3639 /* The next use is no longer "next", since a store
3640 intervenes. */
3641 reg_next_use[regno] = 0;
3642
d7429b6a
RK
3643 /* Keep track of which basic blocks each reg appears in. */
3644
b1f21e0a
MM
3645 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3646 REG_BASIC_BLOCK (regno) = blocknum;
3647 else if (REG_BASIC_BLOCK (regno) != blocknum)
3648 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
d7429b6a
RK
3649
3650 /* Count (weighted) references, stores, etc. This counts a
3651 register twice if it is modified, but that is correct. */
b1f21e0a 3652 REG_N_SETS (regno)++;
d7429b6a 3653
b1f21e0a 3654 REG_N_REFS (regno) += loop_depth;
d7429b6a
RK
3655
3656 /* The insns where a reg is live are normally counted
3657 elsewhere, but we want the count to include the insn
3658 where the reg is set, and the normal counting mechanism
3659 would not count it. */
b1f21e0a 3660 REG_LIVE_LENGTH (regno)++;
d7429b6a
RK
3661 }
3662
cb9e8ad1 3663 if (! some_not_needed)
d7429b6a
RK
3664 {
3665 /* Make a logical link from the next following insn
3666 that uses this register, back to this insn.
3667 The following insns have already been processed.
3668
3669 We don't build a LOG_LINK for hard registers containing
3670 in ASM_OPERANDs. If these registers get replaced,
3671 we might wind up changing the semantics of the insn,
3672 even if reload can make what appear to be valid assignments
3673 later. */
3674 if (y && (BLOCK_NUM (y) == blocknum)
3675 && (regno >= FIRST_PSEUDO_REGISTER
3676 || asm_noperands (PATTERN (y)) < 0))
3677 LOG_LINKS (y)
38a448ca 3678 = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y));
d7429b6a
RK
3679 }
3680 else if (! some_needed)
3681 {
3682 /* Note that dead stores have already been deleted when possible
3683 If we get here, we have found a dead store that cannot
3684 be eliminated (because the same insn does something useful).
3685 Indicate this by marking the reg being set as dying here. */
3686 REG_NOTES (insn)
38a448ca 3687 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
b1f21e0a 3688 REG_N_DEATHS (REGNO (reg))++;
d7429b6a
RK
3689 }
3690 else
3691 {
3692 /* This is a case where we have a multi-word hard register
3693 and some, but not all, of the words of the register are
3694 needed in subsequent insns. Write REG_UNUSED notes
3695 for those parts that were not needed. This case should
3696 be rare. */
3697
3698 int i;
3699
3700 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
3701 i >= 0; i--)
916b1701 3702 if (!REGNO_REG_SET_P (needed, regno + i))
d7429b6a 3703 REG_NOTES (insn)
38a448ca
RH
3704 = gen_rtx_EXPR_LIST (REG_UNUSED,
3705 gen_rtx_REG (reg_raw_mode[regno + i],
3706 regno + i),
3707 REG_NOTES (insn));
d7429b6a
RK
3708 }
3709 }
3710 }
8244fc4f
RS
3711 else if (GET_CODE (reg) == REG)
3712 reg_next_use[regno] = 0;
d7429b6a
RK
3713
3714 /* If this is the last pass and this is a SCRATCH, show it will be dying
3715 here and count it. */
3716 else if (GET_CODE (reg) == SCRATCH && insn != 0)
3717 {
3718 REG_NOTES (insn)
38a448ca 3719 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
d7429b6a
RK
3720 }
3721}
3722\f
3723#ifdef AUTO_INC_DEC
3724
3725/* X is a MEM found in INSN. See if we can convert it into an auto-increment
3726 reference. */
3727
3728static void
3729find_auto_inc (needed, x, insn)
3730 regset needed;
3731 rtx x;
3732 rtx insn;
3733{
3734 rtx addr = XEXP (x, 0);
e658434c 3735 HOST_WIDE_INT offset = 0;
05ed5d57 3736 rtx set;
d7429b6a
RK
3737
3738 /* Here we detect use of an index register which might be good for
3739 postincrement, postdecrement, preincrement, or predecrement. */
3740
3741 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3742 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3743
3744 if (GET_CODE (addr) == REG)
3745 {
3746 register rtx y;
3747 register int size = GET_MODE_SIZE (GET_MODE (x));
3748 rtx use;
3749 rtx incr;
3750 int regno = REGNO (addr);
3751
3752 /* Is the next use an increment that might make auto-increment? */
05ed5d57
RK
3753 if ((incr = reg_next_use[regno]) != 0
3754 && (set = single_set (incr)) != 0
3755 && GET_CODE (set) == SET
d7429b6a
RK
3756 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
3757 /* Can't add side effects to jumps; if reg is spilled and
3758 reloaded, there's no way to store back the altered value. */
3759 && GET_CODE (insn) != JUMP_INSN
05ed5d57 3760 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
d7429b6a
RK
3761 && XEXP (y, 0) == addr
3762 && GET_CODE (XEXP (y, 1)) == CONST_INT
940da324
JL
3763 && ((HAVE_POST_INCREMENT
3764 && (INTVAL (XEXP (y, 1)) == size && offset == 0))
3765 || (HAVE_POST_DECREMENT
3766 && (INTVAL (XEXP (y, 1)) == - size && offset == 0))
3767 || (HAVE_PRE_INCREMENT
3768 && (INTVAL (XEXP (y, 1)) == size && offset == size))
3769 || (HAVE_PRE_DECREMENT
3770 && (INTVAL (XEXP (y, 1)) == - size && offset == - size)))
d7429b6a
RK
3771 /* Make sure this reg appears only once in this insn. */
3772 && (use = find_use_as_address (PATTERN (insn), addr, offset),
3773 use != 0 && use != (rtx) 1))
3774 {
05ed5d57 3775 rtx q = SET_DEST (set);
7280c2a4
RK
3776 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
3777 ? (offset ? PRE_INC : POST_INC)
3778 : (offset ? PRE_DEC : POST_DEC));
d7429b6a
RK
3779
3780 if (dead_or_set_p (incr, addr))
7280c2a4
RK
3781 {
3782 /* This is the simple case. Try to make the auto-inc. If
3783 we can't, we are done. Otherwise, we will do any
3784 needed updates below. */
3785 if (! validate_change (insn, &XEXP (x, 0),
38a448ca 3786 gen_rtx_fmt_e (inc_code, Pmode, addr),
7280c2a4
RK
3787 0))
3788 return;
3789 }
5175ad37
DE
3790 else if (GET_CODE (q) == REG
3791 /* PREV_INSN used here to check the semi-open interval
3792 [insn,incr). */
b24884cd
JL
3793 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3794 /* We must also check for sets of q as q may be
3795 a call clobbered hard register and there may
3796 be a call between PREV_INSN (insn) and incr. */
3797 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
d7429b6a 3798 {
5175ad37 3799 /* We have *p followed sometime later by q = p+size.
d7429b6a 3800 Both p and q must be live afterward,
9ec36da5 3801 and q is not used between INSN and its assignment.
d7429b6a
RK
3802 Change it to q = p, ...*q..., q = q+size.
3803 Then fall into the usual case. */
3804 rtx insns, temp;
e881bb1b 3805 basic_block bb;
d7429b6a
RK
3806
3807 start_sequence ();
3808 emit_move_insn (q, addr);
3809 insns = get_insns ();
3810 end_sequence ();
3811
e881bb1b 3812 bb = BLOCK_FOR_INSN (insn);
d7429b6a 3813 for (temp = insns; temp; temp = NEXT_INSN (temp))
e881bb1b 3814 set_block_for_insn (temp, bb);
d7429b6a 3815
7280c2a4
RK
3816 /* If we can't make the auto-inc, or can't make the
3817 replacement into Y, exit. There's no point in making
3818 the change below if we can't do the auto-inc and doing
3819 so is not correct in the pre-inc case. */
3820
3821 validate_change (insn, &XEXP (x, 0),
38a448ca 3822 gen_rtx_fmt_e (inc_code, Pmode, q),
7280c2a4
RK
3823 1);
3824 validate_change (incr, &XEXP (y, 0), q, 1);
3825 if (! apply_change_group ())
3826 return;
3827
3828 /* We now know we'll be doing this change, so emit the
3829 new insn(s) and do the updates. */
d7429b6a 3830 emit_insns_before (insns, insn);
e8b641a1 3831
e881bb1b
RH
3832 if (BLOCK_FOR_INSN (insn)->head == insn)
3833 BLOCK_FOR_INSN (insn)->head = insns;
e8b641a1 3834
d7429b6a
RK
3835 /* INCR will become a NOTE and INSN won't contain a
3836 use of ADDR. If a use of ADDR was just placed in
3837 the insn before INSN, make that the next use.
3838 Otherwise, invalidate it. */
3839 if (GET_CODE (PREV_INSN (insn)) == INSN
3840 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3841 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
3842 reg_next_use[regno] = PREV_INSN (insn);
3843 else
3844 reg_next_use[regno] = 0;
3845
3846 addr = q;
3847 regno = REGNO (q);
d7429b6a
RK
3848
3849 /* REGNO is now used in INCR which is below INSN, but
3850 it previously wasn't live here. If we don't mark
3851 it as needed, we'll put a REG_DEAD note for it
3852 on this insn, which is incorrect. */
916b1701 3853 SET_REGNO_REG_SET (needed, regno);
d7429b6a
RK
3854
3855 /* If there are any calls between INSN and INCR, show
3856 that REGNO now crosses them. */
3857 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3858 if (GET_CODE (temp) == CALL_INSN)
b1f21e0a 3859 REG_N_CALLS_CROSSED (regno)++;
d7429b6a 3860 }
02df8aba
RK
3861 else
3862 return;
d7429b6a 3863
7280c2a4
RK
3864 /* If we haven't returned, it means we were able to make the
3865 auto-inc, so update the status. First, record that this insn
3866 has an implicit side effect. */
3867
3868 REG_NOTES (insn)
38a448ca 3869 = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
7280c2a4
RK
3870
3871 /* Modify the old increment-insn to simply copy
3872 the already-incremented value of our register. */
3873 if (! validate_change (incr, &SET_SRC (set), addr, 0))
3874 abort ();
3875
3876 /* If that makes it a no-op (copying the register into itself) delete
3877 it so it won't appear to be a "use" and a "set" of this
3878 register. */
3879 if (SET_DEST (set) == addr)
d7429b6a 3880 {
7280c2a4
RK
3881 PUT_CODE (incr, NOTE);
3882 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
3883 NOTE_SOURCE_FILE (incr) = 0;
3884 }
d7429b6a 3885
7280c2a4
RK
3886 if (regno >= FIRST_PSEUDO_REGISTER)
3887 {
3888 /* Count an extra reference to the reg. When a reg is
3889 incremented, spilling it is worse, so we want to make
3890 that less likely. */
b1f21e0a 3891 REG_N_REFS (regno) += loop_depth;
7280c2a4
RK
3892
3893 /* Count the increment as a setting of the register,
3894 even though it isn't a SET in rtl. */
b1f21e0a 3895 REG_N_SETS (regno)++;
d7429b6a
RK
3896 }
3897 }
3898 }
3899}
3900#endif /* AUTO_INC_DEC */
3901\f
3902/* Scan expression X and store a 1-bit in LIVE for each reg it uses.
3903 This is done assuming the registers needed from X
3904 are those that have 1-bits in NEEDED.
3905
3906 On the final pass, FINAL is 1. This means try for autoincrement
3907 and count the uses and deaths of each pseudo-reg.
3908
3909 INSN is the containing instruction. If INSN is dead, this function is not
3910 called. */
3911
3912static void
3913mark_used_regs (needed, live, x, final, insn)
3914 regset needed;
3915 regset live;
3916 rtx x;
d7429b6a 3917 int final;
e658434c 3918 rtx insn;
d7429b6a
RK
3919{
3920 register RTX_CODE code;
3921 register int regno;
3922 int i;
3923
3924 retry:
3925 code = GET_CODE (x);
3926 switch (code)
3927 {
3928 case LABEL_REF:
3929 case SYMBOL_REF:
3930 case CONST_INT:
3931 case CONST:
3932 case CONST_DOUBLE:
3933 case PC:
d7429b6a
RK
3934 case ADDR_VEC:
3935 case ADDR_DIFF_VEC:
d7429b6a
RK
3936 return;
3937
3938#ifdef HAVE_cc0
3939 case CC0:
3940 cc0_live = 1;
3941 return;
3942#endif
3943
2f1553a4
RK
3944 case CLOBBER:
3945 /* If we are clobbering a MEM, mark any registers inside the address
3946 as being used. */
3947 if (GET_CODE (XEXP (x, 0)) == MEM)
3948 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
3949 return;
3950
d7429b6a 3951 case MEM:
7eb136d6
MM
3952 /* Invalidate the data for the last MEM stored, but only if MEM is
3953 something that can be stored into. */
3954 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3955 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
db3a887b 3956 ; /* needn't clear the memory set list */
7eb136d6 3957 else
db3a887b
CB
3958 {
3959 rtx temp = mem_set_list;
3960 rtx prev = NULL_RTX;
3961
3962 while (temp)
3963 {
063cd522 3964 if (anti_dependence (XEXP (temp, 0), x))
db3a887b
CB
3965 {
3966 /* Splice temp out of the list. */
3967 if (prev)
3968 XEXP (prev, 1) = XEXP (temp, 1);
3969 else
3970 mem_set_list = XEXP (temp, 1);
3971 }
3972 else
3973 prev = temp;
3974 temp = XEXP (temp, 1);
3975 }
3976 }
d7429b6a 3977
15e088b2
JL
3978 /* If the memory reference had embedded side effects (autoincrement
3979 address modes. Then we may need to kill some entries on the
3980 memory set list. */
3981 if (insn)
3982 invalidate_mems_from_autoinc (insn);
3983
d7429b6a
RK
3984#ifdef AUTO_INC_DEC
3985 if (final)
3986 find_auto_inc (needed, x, insn);
3987#endif
3988 break;
3989
80f8f04a
RK
3990 case SUBREG:
3991 if (GET_CODE (SUBREG_REG (x)) == REG
3992 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
3993 && (GET_MODE_SIZE (GET_MODE (x))
88285acf 3994 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
b1f21e0a 3995 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
80f8f04a
RK
3996
3997 /* While we're here, optimize this case. */
3998 x = SUBREG_REG (x);
3999
e100a3bb 4000 /* In case the SUBREG is not of a register, don't optimize */
ce79abf3 4001 if (GET_CODE (x) != REG)
e100a3bb
MM
4002 {
4003 mark_used_regs (needed, live, x, final, insn);
4004 return;
4005 }
ce79abf3 4006
0f41302f 4007 /* ... fall through ... */
80f8f04a 4008
d7429b6a
RK
4009 case REG:
4010 /* See a register other than being set
4011 => mark it as needed. */
4012
4013 regno = REGNO (x);
4014 {
67f0e213
RK
4015 int some_needed = REGNO_REG_SET_P (needed, regno);
4016 int some_not_needed = ! some_needed;
d7429b6a 4017
916b1701 4018 SET_REGNO_REG_SET (live, regno);
cb9e8ad1 4019
d7429b6a
RK
4020 /* A hard reg in a wide mode may really be multiple registers.
4021 If so, mark all of them just like the first. */
4022 if (regno < FIRST_PSEUDO_REGISTER)
4023 {
4024 int n;
4025
d7e4fe8b 4026 /* For stack ptr or fixed arg pointer,
d7429b6a
RK
4027 nothing below can be necessary, so waste no more time. */
4028 if (regno == STACK_POINTER_REGNUM
73a187c1 4029#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
4030 || (regno == HARD_FRAME_POINTER_REGNUM
4031 && (! reload_completed || frame_pointer_needed))
73a187c1 4032#endif
d7e4fe8b
RS
4033#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4034 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
4035#endif
e4b8a413
JW
4036 || (regno == FRAME_POINTER_REGNUM
4037 && (! reload_completed || frame_pointer_needed)))
d7429b6a
RK
4038 {
4039 /* If this is a register we are going to try to eliminate,
4040 don't mark it live here. If we are successful in
4041 eliminating it, it need not be live unless it is used for
4042 pseudos, in which case it will have been set live when
4043 it was allocated to the pseudos. If the register will not
4044 be eliminated, reload will set it live at that point. */
4045
4046 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
4047 regs_ever_live[regno] = 1;
4048 return;
4049 }
4050 /* No death notes for global register variables;
4051 their values are live after this function exits. */
4052 if (global_regs[regno])
d8c8b8e3
RS
4053 {
4054 if (final)
4055 reg_next_use[regno] = insn;
4056 return;
4057 }
d7429b6a
RK
4058
4059 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
4060 while (--n > 0)
4061 {
916b1701
MM
4062 int regno_n = regno + n;
4063 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
cb9e8ad1 4064
916b1701
MM
4065 SET_REGNO_REG_SET (live, regno_n);
4066 some_needed |= needed_regno;
931c6c7a 4067 some_not_needed |= ! needed_regno;
d7429b6a
RK
4068 }
4069 }
4070 if (final)
4071 {
4072 /* Record where each reg is used, so when the reg
4073 is set we know the next insn that uses it. */
4074
4075 reg_next_use[regno] = insn;
4076
4077 if (regno < FIRST_PSEUDO_REGISTER)
4078 {
4079 /* If a hard reg is being used,
4080 record that this function does use it. */
4081
4082 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
4083 if (i == 0)
4084 i = 1;
4085 do
4086 regs_ever_live[regno + --i] = 1;
4087 while (i > 0);
4088 }
4089 else
4090 {
4091 /* Keep track of which basic block each reg appears in. */
4092
4093 register int blocknum = BLOCK_NUM (insn);
4094
b1f21e0a
MM
4095 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
4096 REG_BASIC_BLOCK (regno) = blocknum;
4097 else if (REG_BASIC_BLOCK (regno) != blocknum)
4098 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
d7429b6a
RK
4099
4100 /* Count (weighted) number of uses of each reg. */
4101
b1f21e0a 4102 REG_N_REFS (regno) += loop_depth;
d7429b6a
RK
4103 }
4104
4105 /* Record and count the insns in which a reg dies.
4106 If it is used in this insn and was dead below the insn
4107 then it dies in this insn. If it was set in this insn,
4108 we do not make a REG_DEAD note; likewise if we already
4109 made such a note. */
4110
cb9e8ad1 4111 if (some_not_needed
d7429b6a
RK
4112 && ! dead_or_set_p (insn, x)
4113#if 0
4114 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
4115#endif
4116 )
4117 {
ab28041e
JW
4118 /* Check for the case where the register dying partially
4119 overlaps the register set by this insn. */
4120 if (regno < FIRST_PSEUDO_REGISTER
4121 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
4122 {
480eac3b 4123 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
ab28041e
JW
4124 while (--n >= 0)
4125 some_needed |= dead_or_set_regno_p (insn, regno + n);
4126 }
4127
d7429b6a
RK
4128 /* If none of the words in X is needed, make a REG_DEAD
4129 note. Otherwise, we must make partial REG_DEAD notes. */
4130 if (! some_needed)
4131 {
4132 REG_NOTES (insn)
38a448ca 4133 = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn));
b1f21e0a 4134 REG_N_DEATHS (regno)++;
d7429b6a
RK
4135 }
4136 else
4137 {
4138 int i;
4139
4140 /* Don't make a REG_DEAD note for a part of a register
4141 that is set in the insn. */
4142
4143 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
4144 i >= 0; i--)
916b1701 4145 if (!REGNO_REG_SET_P (needed, regno + i)
d7429b6a
RK
4146 && ! dead_or_set_regno_p (insn, regno + i))
4147 REG_NOTES (insn)
38a448ca
RH
4148 = gen_rtx_EXPR_LIST (REG_DEAD,
4149 gen_rtx_REG (reg_raw_mode[regno + i],
4150 regno + i),
4151 REG_NOTES (insn));
d7429b6a
RK
4152 }
4153 }
4154 }
4155 }
4156 return;
4157
4158 case SET:
4159 {
4160 register rtx testreg = SET_DEST (x);
4161 int mark_dest = 0;
4162
4163 /* If storing into MEM, don't show it as being used. But do
4164 show the address as being used. */
4165 if (GET_CODE (testreg) == MEM)
4166 {
4167#ifdef AUTO_INC_DEC
4168 if (final)
4169 find_auto_inc (needed, testreg, insn);
4170#endif
4171 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
4172 mark_used_regs (needed, live, SET_SRC (x), final, insn);
4173 return;
4174 }
4175
4176 /* Storing in STRICT_LOW_PART is like storing in a reg
4177 in that this SET might be dead, so ignore it in TESTREG.
4178 but in some other ways it is like using the reg.
4179
4180 Storing in a SUBREG or a bit field is like storing the entire
4181 register in that if the register's value is not used
4182 then this SET is not needed. */
4183 while (GET_CODE (testreg) == STRICT_LOW_PART
4184 || GET_CODE (testreg) == ZERO_EXTRACT
4185 || GET_CODE (testreg) == SIGN_EXTRACT
4186 || GET_CODE (testreg) == SUBREG)
4187 {
88285acf
RK
4188 if (GET_CODE (testreg) == SUBREG
4189 && GET_CODE (SUBREG_REG (testreg)) == REG
4190 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
4191 && (GET_MODE_SIZE (GET_MODE (testreg))
4192 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
b1f21e0a 4193 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
88285acf 4194
d7429b6a
RK
4195 /* Modifying a single register in an alternate mode
4196 does not use any of the old value. But these other
4197 ways of storing in a register do use the old value. */
4198 if (GET_CODE (testreg) == SUBREG
4199 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
4200 ;
4201 else
4202 mark_dest = 1;
4203
4204 testreg = XEXP (testreg, 0);
4205 }
4206
4207 /* If this is a store into a register,
4208 recursively scan the value being stored. */
4209
86465af7
DM
4210 if ((GET_CODE (testreg) == PARALLEL
4211 && GET_MODE (testreg) == BLKmode)
4212 || (GET_CODE (testreg) == REG
e4b8a413
JW
4213 && (regno = REGNO (testreg), ! (regno == FRAME_POINTER_REGNUM
4214 && (! reload_completed || frame_pointer_needed)))
73a187c1 4215#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
4216 && ! (regno == HARD_FRAME_POINTER_REGNUM
4217 && (! reload_completed || frame_pointer_needed))
73a187c1 4218#endif
d7e4fe8b 4219#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
86465af7 4220 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
d7e4fe8b 4221#endif
86465af7 4222 ))
d8c8b8e3
RS
4223 /* We used to exclude global_regs here, but that seems wrong.
4224 Storing in them is like storing in mem. */
d7429b6a
RK
4225 {
4226 mark_used_regs (needed, live, SET_SRC (x), final, insn);
4227 if (mark_dest)
4228 mark_used_regs (needed, live, SET_DEST (x), final, insn);
4229 return;
4230 }
4231 }
4232 break;
4233
4234 case RETURN:
4235 /* If exiting needs the right stack value, consider this insn as
4236 using the stack pointer. In any event, consider it as using
632c9d9e 4237 all global registers and all registers used by return. */
d7429b6a 4238 if (! EXIT_IGNORE_STACK
0200b5ed
JL
4239 || (! FRAME_POINTER_REQUIRED
4240 && ! current_function_calls_alloca
bfc5000a
JL
4241 && flag_omit_frame_pointer)
4242 || current_function_sp_is_unchanging)
916b1701 4243 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
d7429b6a
RK
4244
4245 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
632c9d9e
MS
4246 if (global_regs[i]
4247#ifdef EPILOGUE_USES
4248 || EPILOGUE_USES (i)
4249#endif
4250 )
916b1701 4251 SET_REGNO_REG_SET (live, i);
d7429b6a 4252 break;
e9a25f70 4253
40b5a77c
JL
4254 case ASM_OPERANDS:
4255 case UNSPEC_VOLATILE:
4256 case TRAP_IF:
4257 case ASM_INPUT:
4258 {
4259 /* Traditional and volatile asm instructions must be considered to use
4260 and clobber all hard registers, all pseudo-registers and all of
4261 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
4262
4263 Consider for instance a volatile asm that changes the fpu rounding
4264 mode. An insn should not be moved across this even if it only uses
4265 pseudo-regs because it might give an incorrectly rounded result.
4266
4267 ?!? Unfortunately, marking all hard registers as live causes massive
4268 problems for the register allocator and marking all pseudos as live
4269 creates mountains of uninitialized variable warnings.
4270
4271 So for now, just clear the memory set list and mark any regs
4272 we can find in ASM_OPERANDS as used. */
4273 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
4274 mem_set_list = NULL_RTX;
4275
4276 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
4277 We can not just fall through here since then we would be confused
4278 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
4279 traditional asms unlike their normal usage. */
4280 if (code == ASM_OPERANDS)
4281 {
4282 int j;
4283
4284 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
4285 mark_used_regs (needed, live, ASM_OPERANDS_INPUT (x, j),
4286 final, insn);
4287 }
4288 break;
4289 }
4290
4291
e9a25f70
JL
4292 default:
4293 break;
d7429b6a
RK
4294 }
4295
4296 /* Recursively scan the operands of this expression. */
4297
4298 {
6f7d635c 4299 register const char *fmt = GET_RTX_FORMAT (code);
d7429b6a
RK
4300 register int i;
4301
4302 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4303 {
4304 if (fmt[i] == 'e')
4305 {
4306 /* Tail recursive case: save a function call level. */
4307 if (i == 0)
4308 {
4309 x = XEXP (x, 0);
4310 goto retry;
4311 }
4312 mark_used_regs (needed, live, XEXP (x, i), final, insn);
4313 }
4314 else if (fmt[i] == 'E')
4315 {
4316 register int j;
4317 for (j = 0; j < XVECLEN (x, i); j++)
4318 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
4319 }
4320 }
4321 }
4322}
4323\f
4324#ifdef AUTO_INC_DEC
4325
4326static int
4327try_pre_increment_1 (insn)
4328 rtx insn;
4329{
4330 /* Find the next use of this reg. If in same basic block,
4331 make it do pre-increment or pre-decrement if appropriate. */
956d6950 4332 rtx x = single_set (insn);
5f4f0e22 4333 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
d7429b6a
RK
4334 * INTVAL (XEXP (SET_SRC (x), 1)));
4335 int regno = REGNO (SET_DEST (x));
4336 rtx y = reg_next_use[regno];
4337 if (y != 0
4338 && BLOCK_NUM (y) == BLOCK_NUM (insn)
89861c38 4339 /* Don't do this if the reg dies, or gets set in y; a standard addressing
0f41302f 4340 mode would be better. */
89861c38 4341 && ! dead_or_set_p (y, SET_DEST (x))
956d6950 4342 && try_pre_increment (y, SET_DEST (x), amount))
d7429b6a
RK
4343 {
4344 /* We have found a suitable auto-increment
4345 and already changed insn Y to do it.
4346 So flush this increment-instruction. */
4347 PUT_CODE (insn, NOTE);
4348 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4349 NOTE_SOURCE_FILE (insn) = 0;
4350 /* Count a reference to this reg for the increment
4351 insn we are deleting. When a reg is incremented.
4352 spilling it is worse, so we want to make that
4353 less likely. */
4354 if (regno >= FIRST_PSEUDO_REGISTER)
4355 {
b1f21e0a
MM
4356 REG_N_REFS (regno) += loop_depth;
4357 REG_N_SETS (regno)++;
d7429b6a
RK
4358 }
4359 return 1;
4360 }
4361 return 0;
4362}
4363
4364/* Try to change INSN so that it does pre-increment or pre-decrement
4365 addressing on register REG in order to add AMOUNT to REG.
4366 AMOUNT is negative for pre-decrement.
4367 Returns 1 if the change could be made.
4368 This checks all about the validity of the result of modifying INSN. */
4369
4370static int
4371try_pre_increment (insn, reg, amount)
4372 rtx insn, reg;
5f4f0e22 4373 HOST_WIDE_INT amount;
d7429b6a
RK
4374{
4375 register rtx use;
4376
4377 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4378 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4379 int pre_ok = 0;
4380 /* Nonzero if we can try to make a post-increment or post-decrement.
4381 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4382 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4383 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4384 int post_ok = 0;
4385
4386 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4387 int do_post = 0;
4388
4389 /* From the sign of increment, see which possibilities are conceivable
4390 on this target machine. */
940da324 4391 if (HAVE_PRE_INCREMENT && amount > 0)
d7429b6a 4392 pre_ok = 1;
940da324 4393 if (HAVE_POST_INCREMENT && amount > 0)
d7429b6a 4394 post_ok = 1;
d7429b6a 4395
940da324 4396 if (HAVE_PRE_DECREMENT && amount < 0)
d7429b6a 4397 pre_ok = 1;
940da324 4398 if (HAVE_POST_DECREMENT && amount < 0)
d7429b6a 4399 post_ok = 1;
d7429b6a
RK
4400
4401 if (! (pre_ok || post_ok))
4402 return 0;
4403
4404 /* It is not safe to add a side effect to a jump insn
4405 because if the incremented register is spilled and must be reloaded
4406 there would be no way to store the incremented value back in memory. */
4407
4408 if (GET_CODE (insn) == JUMP_INSN)
4409 return 0;
4410
4411 use = 0;
4412 if (pre_ok)
4413 use = find_use_as_address (PATTERN (insn), reg, 0);
4414 if (post_ok && (use == 0 || use == (rtx) 1))
4415 {
4416 use = find_use_as_address (PATTERN (insn), reg, -amount);
4417 do_post = 1;
4418 }
4419
4420 if (use == 0 || use == (rtx) 1)
4421 return 0;
4422
4423 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4424 return 0;
4425
a0fbc3a9
SC
4426 /* See if this combination of instruction and addressing mode exists. */
4427 if (! validate_change (insn, &XEXP (use, 0),
38a448ca
RH
4428 gen_rtx_fmt_e (amount > 0
4429 ? (do_post ? POST_INC : PRE_INC)
4430 : (do_post ? POST_DEC : PRE_DEC),
4431 Pmode, reg), 0))
a0fbc3a9 4432 return 0;
d7429b6a
RK
4433
4434 /* Record that this insn now has an implicit side effect on X. */
38a448ca 4435 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
d7429b6a
RK
4436 return 1;
4437}
4438
4439#endif /* AUTO_INC_DEC */
4440\f
4441/* Find the place in the rtx X where REG is used as a memory address.
4442 Return the MEM rtx that so uses it.
4443 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4444 (plus REG (const_int PLUSCONST)).
4445
4446 If such an address does not appear, return 0.
4447 If REG appears more than once, or is used other than in such an address,
4448 return (rtx)1. */
4449
8c660648 4450rtx
d7429b6a
RK
4451find_use_as_address (x, reg, plusconst)
4452 register rtx x;
4453 rtx reg;
e658434c 4454 HOST_WIDE_INT plusconst;
d7429b6a
RK
4455{
4456 enum rtx_code code = GET_CODE (x);
6f7d635c 4457 const char *fmt = GET_RTX_FORMAT (code);
d7429b6a
RK
4458 register int i;
4459 register rtx value = 0;
4460 register rtx tem;
4461
4462 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4463 return x;
4464
4465 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4466 && XEXP (XEXP (x, 0), 0) == reg
4467 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4468 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4469 return x;
4470
4471 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4472 {
4473 /* If REG occurs inside a MEM used in a bit-field reference,
4474 that is unacceptable. */
4475 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
6fa5c106 4476 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4477 }
4478
4479 if (x == reg)
6fa5c106 4480 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4481
4482 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4483 {
4484 if (fmt[i] == 'e')
4485 {
4486 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4487 if (value == 0)
4488 value = tem;
4489 else if (tem != 0)
6fa5c106 4490 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4491 }
4492 if (fmt[i] == 'E')
4493 {
4494 register int j;
4495 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4496 {
4497 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4498 if (value == 0)
4499 value = tem;
4500 else if (tem != 0)
6fa5c106 4501 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4502 }
4503 }
4504 }
4505
4506 return value;
4507}
4508\f
4509/* Write information about registers and basic blocks into FILE.
4510 This is part of making a debugging dump. */
4511
4512void
4513dump_flow_info (file)
4514 FILE *file;
4515{
4516 register int i;
6f7d635c 4517 static const char * const reg_class_names[] = REG_CLASS_NAMES;
d7429b6a
RK
4518
4519 fprintf (file, "%d registers.\n", max_regno);
d7429b6a 4520 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
b1f21e0a 4521 if (REG_N_REFS (i))
d7429b6a 4522 {
e4600702 4523 enum reg_class class, altclass;
d7429b6a 4524 fprintf (file, "\nRegister %d used %d times across %d insns",
b1f21e0a
MM
4525 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
4526 if (REG_BASIC_BLOCK (i) >= 0)
4527 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
6fc4610b
MM
4528 if (REG_N_SETS (i))
4529 fprintf (file, "; set %d time%s", REG_N_SETS (i),
4530 (REG_N_SETS (i) == 1) ? "" : "s");
4531 if (REG_USERVAR_P (regno_reg_rtx[i]))
4532 fprintf (file, "; user var");
b1f21e0a
MM
4533 if (REG_N_DEATHS (i) != 1)
4534 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
4535 if (REG_N_CALLS_CROSSED (i) == 1)
d7429b6a 4536 fprintf (file, "; crosses 1 call");
b1f21e0a
MM
4537 else if (REG_N_CALLS_CROSSED (i))
4538 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
d7429b6a
RK
4539 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
4540 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
4541 class = reg_preferred_class (i);
e4600702
RK
4542 altclass = reg_alternate_class (i);
4543 if (class != GENERAL_REGS || altclass != ALL_REGS)
d7429b6a 4544 {
e4600702
RK
4545 if (altclass == ALL_REGS || class == ALL_REGS)
4546 fprintf (file, "; pref %s", reg_class_names[(int) class]);
4547 else if (altclass == NO_REGS)
d7429b6a
RK
4548 fprintf (file, "; %s or none", reg_class_names[(int) class]);
4549 else
e4600702
RK
4550 fprintf (file, "; pref %s, else %s",
4551 reg_class_names[(int) class],
4552 reg_class_names[(int) altclass]);
d7429b6a
RK
4553 }
4554 if (REGNO_POINTER_FLAG (i))
4555 fprintf (file, "; pointer");
4556 fprintf (file, ".\n");
4557 }
e881bb1b 4558
d7429b6a 4559 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
e881bb1b
RH
4560 for (i = 0; i < n_basic_blocks; i++)
4561 {
4562 register basic_block bb = BASIC_BLOCK (i);
4563 register int regno;
4564 register edge e;
4565
4566 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
4567 i, INSN_UID (bb->head), INSN_UID (bb->end));
4568
4569 fprintf (file, "Predecessors: ");
4570 for (e = bb->pred; e ; e = e->pred_next)
4571 dump_edge_info (file, e, 0);
4572
4573 fprintf (file, "\nSuccessors: ");
4574 for (e = bb->succ; e ; e = e->succ_next)
4575 dump_edge_info (file, e, 1);
4576
4577 fprintf (file, "\nRegisters live at start:");
4578 if (bb->global_live_at_start)
4579 {
4580 for (regno = 0; regno < max_regno; regno++)
4581 if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
4582 fprintf (file, " %d", regno);
4583 }
4584 else
4585 fprintf (file, " n/a");
4586
4587 fprintf (file, "\nRegisters live at end:");
4588 if (bb->global_live_at_end)
4589 {
4590 for (regno = 0; regno < max_regno; regno++)
4591 if (REGNO_REG_SET_P (bb->global_live_at_end, regno))
4592 fprintf (file, " %d", regno);
4593 }
4594 else
4595 fprintf (file, " n/a");
4596
4597 putc('\n', file);
4598 }
4599
4600 putc('\n', file);
4601}
4602
4603static void
4604dump_edge_info (file, e, do_succ)
4605 FILE *file;
4606 edge e;
4607 int do_succ;
4608{
4609 basic_block side = (do_succ ? e->dest : e->src);
4610
4611 if (side == ENTRY_BLOCK_PTR)
4612 fputs (" ENTRY", file);
4613 else if (side == EXIT_BLOCK_PTR)
4614 fputs (" EXIT", file);
4615 else
4616 fprintf (file, " %d", side->index);
4617
4618 if (e->flags)
4619 {
6f7d635c 4620 static const char * const bitnames[] = {
e881bb1b
RH
4621 "fallthru", "crit", "ab", "abcall", "eh", "fake"
4622 };
4623 int comma = 0;
4624 int i, flags = e->flags;
4625
4626 fputc (' ', file);
4627 fputc ('(', file);
4628 for (i = 0; flags; i++)
4629 if (flags & (1 << i))
4630 {
4631 flags &= ~(1 << i);
4632
4633 if (comma)
4634 fputc (',', file);
4635 if (i < (int)(sizeof (bitnames) / sizeof (*bitnames)))
4636 fputs (bitnames[i], file);
4637 else
4638 fprintf (file, "%d", i);
4639 comma = 1;
4640 }
4641 fputc (')', file);
4642 }
d7429b6a 4643}
3e28fe44
MM
4644
4645\f
4646/* Like print_rtl, but also print out live information for the start of each
4647 basic block. */
4648
4649void
4650print_rtl_with_bb (outf, rtx_first)
4651 FILE *outf;
4652 rtx rtx_first;
4653{
4654 register rtx tmp_rtx;
4655
4656 if (rtx_first == 0)
4657 fprintf (outf, "(nil)\n");
3e28fe44
MM
4658 else
4659 {
e881bb1b 4660 int i;
3e28fe44
MM
4661 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
4662 int max_uid = get_max_uid ();
54ea1de9
KG
4663 basic_block *start = (basic_block *)
4664 alloca (max_uid * sizeof (basic_block));
4665 basic_block *end = (basic_block *)
4666 alloca (max_uid * sizeof (basic_block));
2a92c071
GS
4667 enum bb_state *in_bb_p = (enum bb_state *)
4668 alloca (max_uid * sizeof (enum bb_state));
3e28fe44 4669
e881bb1b
RH
4670 memset (start, 0, max_uid * sizeof (basic_block));
4671 memset (end, 0, max_uid * sizeof (basic_block));
4672 memset (in_bb_p, 0, max_uid * sizeof (enum bb_state));
3e28fe44 4673
e881bb1b 4674 for (i = n_basic_blocks - 1; i >= 0; i--)
3e28fe44 4675 {
e881bb1b 4676 basic_block bb = BASIC_BLOCK (i);
3e28fe44 4677 rtx x;
e881bb1b
RH
4678
4679 start[INSN_UID (bb->head)] = bb;
4680 end[INSN_UID (bb->end)] = bb;
4681 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
3e28fe44 4682 {
e881bb1b
RH
4683 enum bb_state state = IN_MULTIPLE_BB;
4684 if (in_bb_p[INSN_UID(x)] == NOT_IN_BB)
4685 state = IN_ONE_BB;
4686 in_bb_p[INSN_UID(x)] = state;
4687
4688 if (x == bb->end)
3e28fe44
MM
4689 break;
4690 }
4691 }
4692
4693 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
4694 {
b707b450 4695 int did_output;
e881bb1b 4696 basic_block bb;
b707b450 4697
e881bb1b 4698 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
3e28fe44
MM
4699 {
4700 fprintf (outf, ";; Start of basic block %d, registers live:",
e881bb1b 4701 bb->index);
3e28fe44 4702
e881bb1b 4703 EXECUTE_IF_SET_IN_REG_SET (bb->global_live_at_start, 0, i,
3e28fe44
MM
4704 {
4705 fprintf (outf, " %d", i);
4706 if (i < FIRST_PSEUDO_REGISTER)
4707 fprintf (outf, " [%s]",
4708 reg_names[i]);
4709 });
4710 putc ('\n', outf);
4711 }
4712
ab87f8c8 4713 if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB
3e28fe44 4714 && GET_CODE (tmp_rtx) != NOTE
ab87f8c8
JL
4715 && GET_CODE (tmp_rtx) != BARRIER
4716 && ! obey_regdecls)
3e28fe44 4717 fprintf (outf, ";; Insn is not within a basic block\n");
e881bb1b 4718 else if (in_bb_p[INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
3e28fe44
MM
4719 fprintf (outf, ";; Insn is in multiple basic blocks\n");
4720
b707b450 4721 did_output = print_rtl_single (outf, tmp_rtx);
3e28fe44 4722
e881bb1b
RH
4723 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
4724 fprintf (outf, ";; End of basic block %d\n", bb->index);
3e28fe44 4725
b707b450 4726 if (did_output)
9ec36da5 4727 putc ('\n', outf);
3e28fe44
MM
4728 }
4729 }
c5c76735
JL
4730
4731 if (current_function_epilogue_delay_list != 0)
4732 {
4733 fprintf (outf, "\n;; Insns in epilogue delay list:\n\n");
4734 for (tmp_rtx = current_function_epilogue_delay_list; tmp_rtx != 0;
4735 tmp_rtx = XEXP (tmp_rtx, 1))
4736 print_rtl_single (outf, XEXP (tmp_rtx, 0));
4737 }
3e28fe44 4738}
5ece9746
JL
4739
4740\f
4741/* Integer list support. */
4742
4743/* Allocate a node from list *HEAD_PTR. */
4744
4745static int_list_ptr
4746alloc_int_list_node (head_ptr)
4747 int_list_block **head_ptr;
4748{
4749 struct int_list_block *first_blk = *head_ptr;
4750
4751 if (first_blk == NULL || first_blk->nodes_left <= 0)
4752 {
4753 first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block));
4754 first_blk->nodes_left = INT_LIST_NODES_IN_BLK;
4755 first_blk->next = *head_ptr;
4756 *head_ptr = first_blk;
4757 }
4758
4759 first_blk->nodes_left--;
4760 return &first_blk->nodes[first_blk->nodes_left];
4761}
4762
4763/* Pointer to head of predecessor/successor block list. */
4764static int_list_block *pred_int_list_blocks;
4765
4766/* Add a new node to integer list LIST with value VAL.
4767 LIST is a pointer to a list object to allow for different implementations.
4768 If *LIST is initially NULL, the list is empty.
4769 The caller must not care whether the element is added to the front or
4770 to the end of the list (to allow for different implementations). */
4771
4772static int_list_ptr
4773add_int_list_node (blk_list, list, val)
4774 int_list_block **blk_list;
4775 int_list **list;
4776 int val;
4777{
4778 int_list_ptr p = alloc_int_list_node (blk_list);
4779
4780 p->val = val;
4781 p->next = *list;
4782 *list = p;
4783 return p;
4784}
4785
4786/* Free the blocks of lists at BLK_LIST. */
4787
4788void
4789free_int_list (blk_list)
4790 int_list_block **blk_list;
4791{
4792 int_list_block *p, *next;
4793
4794 for (p = *blk_list; p != NULL; p = next)
4795 {
4796 next = p->next;
4797 free (p);
4798 }
4799
4800 /* Mark list as empty for the next function we compile. */
4801 *blk_list = NULL;
4802}
4803\f
4804/* Predecessor/successor computation. */
4805
4806/* Mark PRED_BB a precessor of SUCC_BB,
4807 and conversely SUCC_BB a successor of PRED_BB. */
4808
4809static void
4810add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs)
4811 int pred_bb;
4812 int succ_bb;
4813 int_list_ptr *s_preds;
4814 int_list_ptr *s_succs;
4815 int *num_preds;
4816 int *num_succs;
4817{
4818 if (succ_bb != EXIT_BLOCK)
4819 {
4820 add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb);
4821 num_preds[succ_bb]++;
4822 }
4823 if (pred_bb != ENTRY_BLOCK)
4824 {
4825 add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb);
4826 num_succs[pred_bb]++;
4827 }
4828}
4829
e881bb1b
RH
4830/* Convert edge lists into pred/succ lists for backward compatibility. */
4831
743bb12d 4832void
5ece9746
JL
4833compute_preds_succs (s_preds, s_succs, num_preds, num_succs)
4834 int_list_ptr *s_preds;
4835 int_list_ptr *s_succs;
4836 int *num_preds;
4837 int *num_succs;
4838{
e881bb1b
RH
4839 int i, n = n_basic_blocks;
4840 edge e;
5ece9746 4841
e881bb1b
RH
4842 memset (s_preds, 0, n_basic_blocks * sizeof (int_list_ptr));
4843 memset (s_succs, 0, n_basic_blocks * sizeof (int_list_ptr));
4844 memset (num_preds, 0, n_basic_blocks * sizeof (int));
4845 memset (num_succs, 0, n_basic_blocks * sizeof (int));
5ece9746 4846
e881bb1b 4847 for (i = 0; i < n; ++i)
5ece9746 4848 {
e881bb1b
RH
4849 basic_block bb = BASIC_BLOCK (i);
4850
4851 for (e = bb->succ; e ; e = e->succ_next)
4852 add_pred_succ (i, e->dest->index, s_preds, s_succs,
4853 num_preds, num_succs);
5ece9746
JL
4854 }
4855
e881bb1b
RH
4856 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
4857 add_pred_succ (ENTRY_BLOCK, e->dest->index, s_preds, s_succs,
4858 num_preds, num_succs);
5ece9746
JL
4859}
4860
4861void
421382ac 4862dump_bb_data (file, preds, succs, live_info)
5ece9746
JL
4863 FILE *file;
4864 int_list_ptr *preds;
4865 int_list_ptr *succs;
421382ac 4866 int live_info;
5ece9746
JL
4867{
4868 int bb;
4869 int_list_ptr p;
4870
4871 fprintf (file, "BB data\n\n");
4872 for (bb = 0; bb < n_basic_blocks; bb++)
4873 {
4874 fprintf (file, "BB %d, start %d, end %d\n", bb,
4875 INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb)));
4876 fprintf (file, " preds:");
4877 for (p = preds[bb]; p != NULL; p = p->next)
4878 {
4879 int pred_bb = INT_LIST_VAL (p);
4880 if (pred_bb == ENTRY_BLOCK)
4881 fprintf (file, " entry");
4882 else
4883 fprintf (file, " %d", pred_bb);
4884 }
4885 fprintf (file, "\n");
4886 fprintf (file, " succs:");
4887 for (p = succs[bb]; p != NULL; p = p->next)
4888 {
4889 int succ_bb = INT_LIST_VAL (p);
4890 if (succ_bb == EXIT_BLOCK)
4891 fprintf (file, " exit");
4892 else
4893 fprintf (file, " %d", succ_bb);
4894 }
421382ac
BS
4895 if (live_info)
4896 {
4897 int regno;
4898 fprintf (file, "\nRegisters live at start:");
4899 for (regno = 0; regno < max_regno; regno++)
e881bb1b 4900 if (REGNO_REG_SET_P (BASIC_BLOCK (bb)->global_live_at_start, regno))
421382ac
BS
4901 fprintf (file, " %d", regno);
4902 fprintf (file, "\n");
4903 }
5ece9746
JL
4904 fprintf (file, "\n");
4905 }
4906 fprintf (file, "\n");
4907}
4908
4909/* Free basic block data storage. */
4910
4911void
4912free_bb_mem ()
4913{
4914 free_int_list (&pred_int_list_blocks);
4915}
5e89e58b 4916
5ece9746
JL
4917/* Compute dominator relationships. */
4918void
4919compute_dominators (dominators, post_dominators, s_preds, s_succs)
4920 sbitmap *dominators;
4921 sbitmap *post_dominators;
4922 int_list_ptr *s_preds;
4923 int_list_ptr *s_succs;
4924{
4925 int bb, changed, passes;
4926 sbitmap *temp_bitmap;
4927
4928 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4929 sbitmap_vector_ones (dominators, n_basic_blocks);
4930 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4931 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4932
4933 sbitmap_zero (dominators[0]);
4934 SET_BIT (dominators[0], 0);
4935
e881bb1b
RH
4936 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4937 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
5ece9746
JL
4938
4939 passes = 0;
4940 changed = 1;
4941 while (changed)
4942 {
4943 changed = 0;
4944 for (bb = 1; bb < n_basic_blocks; bb++)
4945 {
4946 sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators,
4947 bb, s_preds);
4948 SET_BIT (temp_bitmap[bb], bb);
4949 changed |= sbitmap_a_and_b (dominators[bb],
4950 dominators[bb],
4951 temp_bitmap[bb]);
4952 sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators,
4953 bb, s_succs);
4954 SET_BIT (temp_bitmap[bb], bb);
4955 changed |= sbitmap_a_and_b (post_dominators[bb],
4956 post_dominators[bb],
4957 temp_bitmap[bb]);
4958 }
4959 passes++;
4960 }
4961
4962 free (temp_bitmap);
4963}
4c649323 4964
36349f8b
AM
4965/* Compute dominator relationships using new flow graph structures. */
4966void
4967compute_flow_dominators (dominators, post_dominators)
4968 sbitmap *dominators;
4969 sbitmap *post_dominators;
4970{
4971 int bb, changed, passes;
4972 sbitmap *temp_bitmap;
4973
4974 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4975 sbitmap_vector_ones (dominators, n_basic_blocks);
4976 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4977 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4978
4979 sbitmap_zero (dominators[0]);
4980 SET_BIT (dominators[0], 0);
4981
4982 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4983 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
4984
4985 passes = 0;
4986 changed = 1;
4987 while (changed)
4988 {
4989 changed = 0;
4990 for (bb = 1; bb < n_basic_blocks; bb++)
4991 {
4992 sbitmap_intersection_of_preds (temp_bitmap[bb], dominators, bb);
4993 SET_BIT (temp_bitmap[bb], bb);
4994 changed |= sbitmap_a_and_b (dominators[bb],
4995 dominators[bb],
4996 temp_bitmap[bb]);
4997 sbitmap_intersection_of_succs (temp_bitmap[bb], post_dominators, bb);
4998 SET_BIT (temp_bitmap[bb], bb);
4999 changed |= sbitmap_a_and_b (post_dominators[bb],
5000 post_dominators[bb],
5001 temp_bitmap[bb]);
5002 }
5003 passes++;
5004 }
5005
5006 free (temp_bitmap);
5007}
5008
422d0fb0
RH
5009/* Given DOMINATORS, compute the immediate dominators into IDOM. */
5010
5011void
5012compute_immediate_dominators (idom, dominators)
5013 int *idom;
5014 sbitmap *dominators;
5015{
5016 sbitmap *tmp;
5017 int b;
5018
5019 tmp = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
5020
5021 /* Begin with tmp(n) = dom(n) - { n }. */
5022 for (b = n_basic_blocks; --b >= 0; )
5023 {
5024 sbitmap_copy (tmp[b], dominators[b]);
5025 RESET_BIT (tmp[b], b);
5026 }
5027
5028 /* Subtract out all of our dominator's dominators. */
5029 for (b = n_basic_blocks; --b >= 0; )
5030 {
5031 sbitmap tmp_b = tmp[b];
5032 int s;
5033
5034 for (s = n_basic_blocks; --s >= 0; )
5035 if (TEST_BIT (tmp_b, s))
5036 sbitmap_difference (tmp_b, tmp_b, tmp[s]);
5037 }
5038
5039 /* Find the one bit set in the bitmap and put it in the output array. */
5040 for (b = n_basic_blocks; --b >= 0; )
5041 {
5042 int t;
5043 EXECUTE_IF_SET_IN_SBITMAP (tmp[b], 0, t, { idom[b] = t; });
5044 }
5045
5046 sbitmap_vector_free (tmp);
5047}
5048
4c649323
JL
5049/* Count for a single SET rtx, X. */
5050
5051static void
5052count_reg_sets_1 (x)
5053 rtx x;
5054{
5055 register int regno;
5056 register rtx reg = SET_DEST (x);
5057
5058 /* Find the register that's set/clobbered. */
5059 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
5060 || GET_CODE (reg) == SIGN_EXTRACT
5061 || GET_CODE (reg) == STRICT_LOW_PART)
5062 reg = XEXP (reg, 0);
5063
86465af7
DM
5064 if (GET_CODE (reg) == PARALLEL
5065 && GET_MODE (reg) == BLKmode)
5066 {
5067 register int i;
5068 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
5069 count_reg_sets_1 (XVECEXP (reg, 0, i));
5070 return;
5071 }
5072
4c649323
JL
5073 if (GET_CODE (reg) == REG)
5074 {
5075 regno = REGNO (reg);
5076 if (regno >= FIRST_PSEUDO_REGISTER)
5077 {
5078 /* Count (weighted) references, stores, etc. This counts a
5079 register twice if it is modified, but that is correct. */
5080 REG_N_SETS (regno)++;
5081
5082 REG_N_REFS (regno) += loop_depth;
5083 }
5084 }
5085}
5086
5087/* Increment REG_N_SETS for each SET or CLOBBER found in X; also increment
5088 REG_N_REFS by the current loop depth for each SET or CLOBBER found. */
5089
5090static void
5091count_reg_sets (x)
5092 rtx x;
5093{
5094 register RTX_CODE code = GET_CODE (x);
5095
5096 if (code == SET || code == CLOBBER)
5097 count_reg_sets_1 (x);
5098 else if (code == PARALLEL)
5099 {
5100 register int i;
5101 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
5102 {
5103 code = GET_CODE (XVECEXP (x, 0, i));
5104 if (code == SET || code == CLOBBER)
5105 count_reg_sets_1 (XVECEXP (x, 0, i));
5106 }
5107 }
5108}
5109
5110/* Increment REG_N_REFS by the current loop depth each register reference
5111 found in X. */
5112
5113static void
5114count_reg_references (x)
5115 rtx x;
5116{
5117 register RTX_CODE code;
4c649323
JL
5118
5119 retry:
5120 code = GET_CODE (x);
5121 switch (code)
5122 {
5123 case LABEL_REF:
5124 case SYMBOL_REF:
5125 case CONST_INT:
5126 case CONST:
5127 case CONST_DOUBLE:
5128 case PC:
5129 case ADDR_VEC:
5130 case ADDR_DIFF_VEC:
5131 case ASM_INPUT:
5132 return;
5133
5134#ifdef HAVE_cc0
5135 case CC0:
5136 return;
5137#endif
5138
5139 case CLOBBER:
5140 /* If we are clobbering a MEM, mark any registers inside the address
5141 as being used. */
5142 if (GET_CODE (XEXP (x, 0)) == MEM)
5143 count_reg_references (XEXP (XEXP (x, 0), 0));
5144 return;
5145
5146 case SUBREG:
5147 /* While we're here, optimize this case. */
5148 x = SUBREG_REG (x);
5149
5150 /* In case the SUBREG is not of a register, don't optimize */
5151 if (GET_CODE (x) != REG)
5152 {
5153 count_reg_references (x);
5154 return;
5155 }
5156
5157 /* ... fall through ... */
5158
5159 case REG:
5160 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
5161 REG_N_REFS (REGNO (x)) += loop_depth;
5162 return;
5163
5164 case SET:
5165 {
5166 register rtx testreg = SET_DEST (x);
5167 int mark_dest = 0;
5168
5169 /* If storing into MEM, don't show it as being used. But do
5170 show the address as being used. */
5171 if (GET_CODE (testreg) == MEM)
5172 {
5173 count_reg_references (XEXP (testreg, 0));
5174 count_reg_references (SET_SRC (x));
5175 return;
5176 }
5177
5178 /* Storing in STRICT_LOW_PART is like storing in a reg
5179 in that this SET might be dead, so ignore it in TESTREG.
5180 but in some other ways it is like using the reg.
5181
5182 Storing in a SUBREG or a bit field is like storing the entire
5183 register in that if the register's value is not used
5184 then this SET is not needed. */
5185 while (GET_CODE (testreg) == STRICT_LOW_PART
5186 || GET_CODE (testreg) == ZERO_EXTRACT
5187 || GET_CODE (testreg) == SIGN_EXTRACT
5188 || GET_CODE (testreg) == SUBREG)
5189 {
5190 /* Modifying a single register in an alternate mode
5191 does not use any of the old value. But these other
5192 ways of storing in a register do use the old value. */
5193 if (GET_CODE (testreg) == SUBREG
5194 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
5195 ;
5196 else
5197 mark_dest = 1;
5198
5199 testreg = XEXP (testreg, 0);
5200 }
5201
5202 /* If this is a store into a register,
5203 recursively scan the value being stored. */
5204
86465af7
DM
5205 if ((GET_CODE (testreg) == PARALLEL
5206 && GET_MODE (testreg) == BLKmode)
5207 || GET_CODE (testreg) == REG)
4c649323
JL
5208 {
5209 count_reg_references (SET_SRC (x));
5210 if (mark_dest)
5211 count_reg_references (SET_DEST (x));
5212 return;
5213 }
5214 }
5215 break;
5216
5217 default:
5218 break;
5219 }
5220
5221 /* Recursively scan the operands of this expression. */
5222
5223 {
6f7d635c 5224 register const char *fmt = GET_RTX_FORMAT (code);
4c649323
JL
5225 register int i;
5226
5227 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5228 {
5229 if (fmt[i] == 'e')
5230 {
5231 /* Tail recursive case: save a function call level. */
5232 if (i == 0)
5233 {
5234 x = XEXP (x, 0);
5235 goto retry;
5236 }
5237 count_reg_references (XEXP (x, i));
5238 }
5239 else if (fmt[i] == 'E')
5240 {
5241 register int j;
5242 for (j = 0; j < XVECLEN (x, i); j++)
5243 count_reg_references (XVECEXP (x, i, j));
5244 }
5245 }
5246 }
5247}
5248
5249/* Recompute register set/reference counts immediately prior to register
5250 allocation.
5251
5252 This avoids problems with set/reference counts changing to/from values
5253 which have special meanings to the register allocators.
5254
5255 Additionally, the reference counts are the primary component used by the
5256 register allocators to prioritize pseudos for allocation to hard regs.
5257 More accurate reference counts generally lead to better register allocation.
5258
213c4983
R
5259 F is the first insn to be scanned.
5260 LOOP_STEP denotes how much loop_depth should be incremented per
5261 loop nesting level in order to increase the ref count more for references
5262 in a loop.
5263
4c649323
JL
5264 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
5265 possibly other information which is used by the register allocators. */
5266
762a1d90 5267void
213c4983 5268recompute_reg_usage (f, loop_step)
4c649323 5269 rtx f;
213c4983 5270 int loop_step;
4c649323
JL
5271{
5272 rtx insn;
5273 int i, max_reg;
5274
5275 /* Clear out the old data. */
5276 max_reg = max_reg_num ();
5277 for (i = FIRST_PSEUDO_REGISTER; i < max_reg; i++)
5278 {
5279 REG_N_SETS (i) = 0;
5280 REG_N_REFS (i) = 0;
5281 }
5282
5283 /* Scan each insn in the chain and count how many times each register is
5284 set/used. */
5285 loop_depth = 1;
5286 for (insn = f; insn; insn = NEXT_INSN (insn))
5287 {
5288 /* Keep track of loop depth. */
5289 if (GET_CODE (insn) == NOTE)
5290 {
5291 /* Look for loop boundaries. */
5292 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
213c4983 5293 loop_depth -= loop_step;
4c649323 5294 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
213c4983 5295 loop_depth += loop_step;
4c649323
JL
5296
5297 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
5298 Abort now rather than setting register status incorrectly. */
5299 if (loop_depth == 0)
5300 abort ();
5301 }
5302 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5303 {
5304 rtx links;
5305
5306 /* This call will increment REG_N_SETS for each SET or CLOBBER
5307 of a register in INSN. It will also increment REG_N_REFS
5308 by the loop depth for each set of a register in INSN. */
5309 count_reg_sets (PATTERN (insn));
5310
5311 /* count_reg_sets does not detect autoincrement address modes, so
5312 detect them here by looking at the notes attached to INSN. */
5313 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
5314 {
5315 if (REG_NOTE_KIND (links) == REG_INC)
5316 /* Count (weighted) references, stores, etc. This counts a
5317 register twice if it is modified, but that is correct. */
5318 REG_N_SETS (REGNO (XEXP (links, 0)))++;
5319 }
5320
5321 /* This call will increment REG_N_REFS by the current loop depth for
5322 each reference to a register in INSN. */
5323 count_reg_references (PATTERN (insn));
5324
5325 /* count_reg_references will not include counts for arguments to
5326 function calls, so detect them here by examining the
5327 CALL_INSN_FUNCTION_USAGE data. */
5328 if (GET_CODE (insn) == CALL_INSN)
5329 {
5330 rtx note;
5331
5332 for (note = CALL_INSN_FUNCTION_USAGE (insn);
5333 note;
5334 note = XEXP (note, 1))
5335 if (GET_CODE (XEXP (note, 0)) == USE)
83ab3839 5336 count_reg_references (XEXP (XEXP (note, 0), 0));
4c649323
JL
5337 }
5338 }
5339 }
5340}
e881bb1b
RH
5341
5342/* Record INSN's block as BB. */
5343
5344void
5345set_block_for_insn (insn, bb)
5346 rtx insn;
5347 basic_block bb;
5348{
5349 size_t uid = INSN_UID (insn);
5350 if (uid >= basic_block_for_insn->num_elements)
5351 {
5352 int new_size;
5353
5354 /* Add one-eighth the size so we don't keep calling xrealloc. */
5355 new_size = uid + (uid + 7) / 8;
5356
5357 VARRAY_GROW (basic_block_for_insn, new_size);
5358 }
5359 VARRAY_BB (basic_block_for_insn, uid) = bb;
5360}
5361
5362/* Record INSN's block number as BB. */
5363/* ??? This has got to go. */
5364
5365void
5366set_block_num (insn, bb)
5367 rtx insn;
5368 int bb;
5369{
5370 set_block_for_insn (insn, BASIC_BLOCK (bb));
5371}
34487bf8 5372\f
f2a1bc02
BM
5373/* Unlink a chain of insns between START and FINISH inclusive, leaving notes
5374 that must be paired, and return the new chain. */
5375
5376rtx
5377unlink_insn_chain (start, finish)
5378 rtx start, finish;
5379{
5380 rtx insert_point = PREV_INSN (start);
5381 rtx chain = NULL_RTX, curr;
5382
5383 /* Unchain the insns one by one. It would be quicker to delete all
5384 of these with a single unchaining, rather than one at a time, but
5385 we need to keep the NOTE's. */
5386
5387 while (1)
5388 {
5389 rtx next = NEXT_INSN (start);
5390
5391 remove_insn (start);
5392
5393 /* ??? Despite the fact that we're patching out the insn, it's
5394 still referenced in LOG_LINKS. Rather than try and track
5395 them all down and remove them, just mark the insn deleted. */
5396 INSN_DELETED_P (start) = 1;
5397
5398 if (GET_CODE (start) == NOTE && ! can_delete_note_p (start))
5399 {
5400 add_insn_after (start, insert_point);
5401 insert_point = start;
5402 }
5403 else
5404 {
5405 if (chain != NULL)
5406 {
5407 NEXT_INSN (curr) = start;
5408 PREV_INSN (start) = curr;
5409 curr = start;
5410 }
5411 else
5412 {
5413 chain = start;
5414 curr = start;
5415 PREV_INSN (chain) = NULL_RTX;
5416 }
5417 }
5418
5419 if (start == finish)
5420 break;
5421 start = next;
5422 }
5423
5424 if (chain != NULL_RTX)
5425 NEXT_INSN (curr) = NULL_RTX;
5426
5427 return chain;
5428}
5429
5430/* Subroutine of update_life_info. Determines whether multiple
5431 REG_NOTEs need to be distributed for the hard register mentioned in
5432 NOTE. This can happen if a reference to a hard register in the
5433 original insns was split into several smaller hard register
5434 references in the new insns. */
5435
5436static void
5437split_hard_reg_notes (curr_insn, note, first, last)
5438 rtx curr_insn, note, first, last;
5439{
5440 rtx reg, temp, link;
5441 rtx insn;
5442 int n_regs, i, new_reg;
5443
5444 reg = XEXP (note, 0);
5445
5446 if (REG_NOTE_KIND (note) != REG_DEAD
5447 || GET_CODE (reg) != REG
5448 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
5449 || HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) == 1)
5450 {
5451 XEXP (note, 1) = REG_NOTES (curr_insn);
5452 REG_NOTES (curr_insn) = note;
5453 return;
5454 }
5455
5456 n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
5457
5458 for (i = 0; i < n_regs; i++)
5459 {
5460 new_reg = REGNO (reg) + i;
5461
5462 /* Check for references to new_reg in the split insns. */
5463 for (insn = last; ; insn = PREV_INSN (insn))
5464 {
5465 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5466 && (temp = regno_use_in (new_reg, PATTERN (insn))))
5467 {
5468 /* Create a new reg dead note here. */
5469 link = rtx_alloc (EXPR_LIST);
5470 PUT_REG_NOTE_KIND (link, REG_DEAD);
5471 XEXP (link, 0) = temp;
5472 XEXP (link, 1) = REG_NOTES (insn);
5473 REG_NOTES (insn) = link;
5474
5475 /* If killed multiple registers here, then add in the excess. */
5476 i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
5477
5478 break;
5479 }
5480 /* It isn't mentioned anywhere, so no new reg note is needed for
5481 this register. */
5482 if (insn == first)
5483 break;
5484 }
5485 }
5486}
5487
5488/* SET_INSN kills REG; add a REG_DEAD note mentioning REG to the last
5489 use of REG in the insns after SET_INSN and before or including
5490 LAST, if necessary.
5491
5492 A non-zero value is returned if we added a REG_DEAD note, or if we
5493 determined that a REG_DEAD note because of this particular SET
5494 wasn't necessary. */
5495
5496static int
5497maybe_add_dead_note (reg, set_insn, last)
5498 rtx reg, set_insn, last;
5499{
5500 rtx insn;
5501
5502 for (insn = last; insn != set_insn; insn = PREV_INSN (insn))
5503 {
5504 rtx set;
5505
5506 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5507 && reg_overlap_mentioned_p (reg, PATTERN (insn))
5508 && (set = single_set (insn)))
5509 {
5510 rtx insn_dest = SET_DEST (set);
5511
5512 while (GET_CODE (insn_dest) == ZERO_EXTRACT
5513 || GET_CODE (insn_dest) == SUBREG
5514 || GET_CODE (insn_dest) == STRICT_LOW_PART
5515 || GET_CODE (insn_dest) == SIGN_EXTRACT)
5516 insn_dest = XEXP (insn_dest, 0);
5517
5518 if (! rtx_equal_p (insn_dest, reg))
5519 {
5520 /* Use the same scheme as combine.c, don't put both REG_DEAD
5521 and REG_UNUSED notes on the same insn. */
5522 if (! find_regno_note (insn, REG_UNUSED, REGNO (reg))
5523 && ! find_regno_note (insn, REG_DEAD, REGNO (reg)))
5524 {
5525 rtx note = rtx_alloc (EXPR_LIST);
5526 PUT_REG_NOTE_KIND (note, REG_DEAD);
5527 XEXP (note, 0) = reg;
5528 XEXP (note, 1) = REG_NOTES (insn);
5529 REG_NOTES (insn) = note;
5530 }
5531 return 1;
5532 }
5533 else if (reg_overlap_mentioned_p (reg, SET_SRC (set)))
5534 {
5535 /* We found an instruction that both uses the register and
5536 sets it, so no new REG_NOTE is needed for the previous
5537 set. */
5538 return 0;
5539 }
5540 }
5541 }
5542 return 0;
5543}
5544
5545static int
5546maybe_add_dead_note_use (insn, dest)
5547 rtx insn, dest;
5548{
5549 rtx set;
5550
5551 /* We need to add a REG_DEAD note to the last place DEST is
5552 referenced. */
5553
5554 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5555 && reg_mentioned_p (dest, PATTERN (insn))
5556 && (set = single_set (insn)))
5557 {
5558 rtx insn_dest = SET_DEST (set);
5559
5560 while (GET_CODE (insn_dest) == ZERO_EXTRACT
5561 || GET_CODE (insn_dest) == SUBREG
5562 || GET_CODE (insn_dest) == STRICT_LOW_PART
5563 || GET_CODE (insn_dest) == SIGN_EXTRACT)
5564 insn_dest = XEXP (insn_dest, 0);
5565
5566 if (! rtx_equal_p (insn_dest, dest))
5567 {
5568 /* Use the same scheme as combine.c, don't put both REG_DEAD
5569 and REG_UNUSED notes on the same insn. */
5570 if (! find_regno_note (insn, REG_UNUSED, REGNO (dest))
5571 && ! find_regno_note (insn, REG_DEAD, REGNO (dest)))
5572 {
5573 rtx note = rtx_alloc (EXPR_LIST);
5574 PUT_REG_NOTE_KIND (note, REG_DEAD);
5575 XEXP (note, 0) = dest;
5576 XEXP (note, 1) = REG_NOTES (insn);
5577 REG_NOTES (insn) = note;
5578 }
5579 return 1;
5580 }
5581 }
5582 return 0;
5583}
5584
5585/* Find the first insn in the set of insns from FIRST to LAST inclusive
5586 that contains the note NOTE. */
5587rtx
5588find_insn_with_note (note, first, last)
5589 rtx note, first, last;
5590{
5591 rtx insn;
5592
5593 for (insn = first; insn != NULL_RTX; insn = NEXT_INSN (insn))
5594 {
5595 rtx temp = find_reg_note (insn, REG_NOTE_KIND (note), XEXP (note, 0));
5596 if (temp == note)
5597 {
5598 return insn;
5599 }
5600 if (insn == last)
5601 {
5602 break;
5603 }
5604 }
5605 return NULL_RTX;
5606}
5607
5608/* Subroutine of update_life_info. Determines whether a SET or
5609 CLOBBER in an insn created by splitting needs a REG_DEAD or
5610 REG_UNUSED note added. */
5611
5612static void
5613new_insn_dead_notes (pat, insn, first, last, orig_first_insn, orig_last_insn)
5614 rtx pat, insn, first, last, orig_first_insn, orig_last_insn;
5615{
5616 rtx dest, tem;
5617
5618 if (GET_CODE (pat) != CLOBBER && GET_CODE (pat) != SET)
5619 abort ();
5620
5621 dest = XEXP (pat, 0);
5622
5623 while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
5624 || GET_CODE (dest) == STRICT_LOW_PART
5625 || GET_CODE (dest) == SIGN_EXTRACT)
5626 dest = XEXP (dest, 0);
5627
5628 if (GET_CODE (dest) == REG)
5629 {
9831d640 5630#if 0
f2a1bc02
BM
5631 /* If the original insns already used this register, we may not
5632 add new notes for it. One example for a replacement that
5633 needs this test is when a multi-word memory access with
5634 register-indirect addressing is changed into multiple memory
5635 accesses with auto-increment and one adjusting add
5636 instruction for the address register.
5637
5638 However, there is a problem with this code. We're assuming
5639 that any registers that are set in the new insns are either
5640 set/referenced in the old insns (and thus "inherit" the
5641 liveness of the old insns), or are registers that are dead
5642 before we enter this part of the stream (and thus should be
5643 dead when we leave).
5644
5645 To do this absolutely correctly, we must determine the actual
5646 liveness of the registers before we go randomly adding
5647 REG_DEAD notes. This can probably be accurately done by
5648 calling mark_referenced_resources() on the old stream before
5649 replacing the old insns. */
9831d640
RH
5650 /* ??? The conclusion reached here -- that we can't add DEAD notes
5651 when the register is preexisting -- is false. I can't envision
5652 a sequence postulated above that wouldn't be properly handled
5653 by the code below. In the meantime, consider the 1->2 split
5654
5655 (set (reg:SI 100) (ne:SI (reg:SI 100) (const_int 0)))
5656 to
5657 (set (reg:CC icc) (compare:CC (reg:SI 100) (const_int 0)))
5658 (set (reg:SI 100) (ne:SI (reg:CC icc) (const_int 0)))
5659
5660 We do in fact need a new DEAD note on the first insn for reg 100. */
f2a1bc02
BM
5661
5662 for (tem = orig_first_insn; tem != NULL_RTX; tem = NEXT_INSN (tem))
5663 {
5664 if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
5665 && reg_referenced_p (dest, PATTERN (tem)))
5666 return;
5667 if (tem == orig_last_insn)
5668 break;
5669 }
9831d640 5670#endif
963d4411 5671
f2a1bc02
BM
5672 /* So it's a new register, presumably only used within this
5673 group of insns. Find the last insn in the set of new insns
5674 that DEST is referenced in, and add a dead note to it. */
5675 if (! maybe_add_dead_note (dest, insn, last))
5676 {
5677 /* If this is a set, it must die somewhere, unless it is the
5678 dest of the original insn, and thus is live after the
5679 original insn. Abort if it isn't supposed to be live after
5680 the original insn.
5681
5682 If this is a clobber, then just add a REG_UNUSED note. */
5683 if (GET_CODE (pat) == CLOBBER)
5684 {
5685 rtx note = rtx_alloc (EXPR_LIST);
5686 PUT_REG_NOTE_KIND (note, REG_UNUSED);
5687 XEXP (note, 0) = dest;
5688 XEXP (note, 1) = REG_NOTES (insn);
5689 REG_NOTES (insn) = note;
5690 return;
5691 }
5692 else
5693 {
f2a1bc02 5694 rtx curr;
963d4411 5695 int got_set = 0;
f2a1bc02 5696
963d4411 5697 for (curr = orig_first_insn; curr; curr = NEXT_INSN (curr))
f2a1bc02 5698 {
963d4411
RH
5699 got_set = sets_reg_or_subreg (curr, dest);
5700 if (got_set)
f2a1bc02
BM
5701 break;
5702 if (curr == orig_last_insn)
5703 break;
5704 }
5705
5706 /* In case reg was not used later, it is dead store.
5707 add REG_UNUSED note. */
963d4411 5708 if (! got_set)
f2a1bc02
BM
5709 {
5710 rtx note = rtx_alloc (EXPR_LIST);
5711 PUT_REG_NOTE_KIND (note, REG_UNUSED);
5712 XEXP (note, 0) = dest;
5713 XEXP (note, 1) = REG_NOTES (insn);
5714 REG_NOTES (insn) = note;
5715 return;
5716 }
5717 }
5718 }
963d4411 5719
f2a1bc02
BM
5720 if (insn != first)
5721 {
5722 rtx set = single_set (insn);
963d4411 5723
f2a1bc02
BM
5724 /* If this is a set, scan backwards for a previous
5725 reference, and attach a REG_DEAD note to it. But we don't
5726 want to do it if the insn is both using and setting the
5727 register.
5728
5729 Global registers are always live. */
5730 if (set && ! reg_overlap_mentioned_p (dest, SET_SRC (pat))
5731 && (REGNO (dest) >= FIRST_PSEUDO_REGISTER
5732 || ! global_regs[REGNO (dest)]))
5733 {
5734 for (tem = PREV_INSN (insn);
5735 tem != NULL_RTX; tem = PREV_INSN (tem))
5736 {
5737 if (maybe_add_dead_note_use (tem, dest))
5738 break;
5739 if (tem == first)
5740 break;
5741 }
5742 }
5743 }
5744 }
5745}
5746
5747/* Subroutine of update_life_info. Update the value of reg_n_sets for all
5748 registers modified by X. INC is -1 if the containing insn is being deleted,
5749 and is 1 if the containing insn is a newly generated insn. */
5750
5751static void
5752update_n_sets (x, inc)
5753 rtx x;
5754 int inc;
5755{
5756 rtx dest = SET_DEST (x);
5757
5758 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
5759 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
5760 dest = SUBREG_REG (dest);
5761
5762 if (GET_CODE (dest) == REG)
5763 {
5764 int regno = REGNO (dest);
5765
5766 if (regno < FIRST_PSEUDO_REGISTER)
5767 {
5768 register int i;
5769 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
5770
5771 for (i = regno; i < endregno; i++)
5772 REG_N_SETS (i) += inc;
5773 }
5774 else
5775 REG_N_SETS (regno) += inc;
5776 }
5777}
5778
5779/* Scan INSN for a SET that sets REG. If it sets REG via a SUBREG,
5780 then return 2. If it sets REG directly, return 1. Otherwise, return
5781 0. */
5782
5783static int sets_reg_or_subreg_ret;
5784static rtx sets_reg_or_subreg_rtx;
5785
5786static void
5787sets_reg_or_subreg_1 (x, set)
5788 rtx x, set;
5789{
5790 if (rtx_equal_p (x, sets_reg_or_subreg_rtx))
5791 {
5792 if (x == XEXP (set, 0))
5793 sets_reg_or_subreg_ret = 1;
5794 else if (GET_CODE (XEXP (set, 0)) == SUBREG)
5795 sets_reg_or_subreg_ret = 2;
5796 }
5797}
5798
5799static int
5800sets_reg_or_subreg (insn, reg)
5801 rtx insn;
5802 rtx reg;
5803{
5804 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5805 return 0;
5806
5807 sets_reg_or_subreg_ret = 0;
5808 sets_reg_or_subreg_rtx = reg;
5809 note_stores (PATTERN (insn), sets_reg_or_subreg_1);
5810 return sets_reg_or_subreg_ret;
5811}
5812
5813/* If a replaced SET_INSN (which is part of the insns between
5814 OLD_FIRST_INSN and OLD_LAST_INSN inclusive) is modifying a multiple
5815 register target, and the original dest is now set in the new insns
5816 (between FIRST_INSN and LAST_INSN inclusive) by one or more subreg
5817 sets, then the new insns no longer kill the destination of the
5818 original insn.
5819
5820 We may also be directly using the register in the new insns before
5821 setting it.
5822
5823 In either case, if there exists an instruction in the same basic
5824 block before the replaced insns which uses the original dest (and
5825 contains a corresponding REG_DEAD note), then we must remove this
5826 REG_DEAD note.
5827
5828 SET_INSN is the insn that contains the SET; it may be a PARALLEL
5829 containing the SET insn.
5830
5831 SET is the actual SET insn proper. */
5832
5833static void
5834maybe_remove_dead_notes (set_insn, set, first_insn, last_insn,
5835 old_first_insn, old_last_insn)
5836 rtx set_insn, set;
5837 rtx first_insn, last_insn;
5838 rtx old_first_insn, old_last_insn;
5839{
5840 rtx insn;
5841 rtx stop_insn = NEXT_INSN (last_insn);
5842 int set_type = 0;
5843 rtx set_dest;
5844 rtx set_pattern;
5845
5846 if (GET_RTX_CLASS (GET_CODE (set)) != 'i')
5847 return;
5848
5849 set_pattern = PATTERN (set);
5850
5851 if (GET_CODE (set_pattern) == PARALLEL)
5852 {
5853 int i;
5854
5855 for (i = 0; i < XVECLEN (set_pattern, 0); i++)
5856 {
5857 maybe_remove_dead_notes (set_insn, XVECEXP (set_pattern, 0, i),
5858 first_insn, last_insn,
5859 old_first_insn, old_last_insn);
5860 }
5861 return;
5862 }
5863
5864 if (GET_CODE (set_pattern) != SET)
5865 {
5866 return;
5867 }
5868
5869 set_dest = SET_DEST (set_pattern);
5870
5871 if (GET_CODE (set_dest) != REG)
5872 {
5873 return;
5874 }
5875
5876 /* We have a set of a REG. First we need to determine if this set is
5877 both using and setting the register. (FIXME: if this is in a
5878 PARALLEL, we will have to check the other exprs as well.) */
5879 if (reg_overlap_mentioned_p (set_dest, SET_SRC (set_pattern)))
5880 {
5881 return;
5882 }
5883
5884 /* Now determine if we used or set the register in the old insns
5885 previous to this one. */
5886
5887 for (insn = old_first_insn; insn != set_insn; insn = NEXT_INSN (insn))
5888 {
5889 if (reg_overlap_mentioned_p (set_dest, insn))
5890 {
5891 return;
5892 }
5893 }
5894
5895 /* Now determine if we're setting it in the new insns, or using
5896 it. */
5897 for (insn = first_insn; insn != stop_insn; insn = NEXT_INSN (insn))
5898 {
5899 set_type = sets_reg_or_subreg (insn, set_dest);
5900 if (set_type != 0)
5901 {
5902 break;
5903 }
5904 else if (reg_overlap_mentioned_p (set_dest, insn))
5905 {
5906 /* Is the reg now used in this new insn? -- This is probably an
5907 error. */
5908 set_type = 2;
5909 break;
5910 }
5911 }
5912 if (set_type == 2)
5913 {
5914 /* The register is being set via a SUBREG or is being used in
5915 some other way, so it's no longer dead.
5916
5917 Search backwards from first_insn, looking for the first insn
5918 that uses the original dest. Stop if we pass a CODE_LABEL or
5919 a JUMP_INSN.
5920
5921 If we find such an insn and it has a REG_DEAD note referring
5922 to the original dest, then delete the note. */
5923
5924 for (insn = first_insn; insn != NULL_RTX; insn = PREV_INSN (insn))
5925 {
5926 if (GET_CODE (insn) == CODE_LABEL
5927 || GET_CODE (insn) == JUMP_INSN)
5928 break;
5929 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
5930 && reg_mentioned_p (set_dest, insn))
5931 {
5932 rtx note = find_regno_note (insn, REG_DEAD, REGNO (set_dest));
5933 if (note != NULL_RTX)
5934 {
5935 remove_note (insn, note);
5936 }
5937 /* ??? -- Is this right? */
5938 break;
5939 }
5940 }
5941 }
5942 else if (set_type == 0)
5943 {
5944 /* The reg is not being set or used in the new insns at all. */
5945 int i, regno;
5946
5947 /* Should never reach here for a pseudo reg. */
5948 if (REGNO (set_dest) >= FIRST_PSEUDO_REGISTER)
5949 abort ();
5950
5951 /* This can happen for a hard register, if the new insns do not
5952 contain instructions which would be no-ops referring to the
5953 old registers.
5954
5955 We try to verify that this is the case by checking to see if
5956 the original instruction uses all of the registers that it
5957 set. This case is OK, because deleting a no-op can not affect
5958 REG_DEAD notes on other insns. If this is not the case, then
5959 abort. */
5960
5961 regno = REGNO (set_dest);
5962 for (i = HARD_REGNO_NREGS (regno, GET_MODE (set_dest)) - 1;
5963 i >= 0; i--)
5964 {
5965 if (! refers_to_regno_p (regno + i, regno + i + 1, set,
5966 NULL_PTR))
5967 break;
5968 }
5969 if (i >= 0)
5970 abort ();
5971 }
5972}
5973
5974/* Updates all flow-analysis related quantities (including REG_NOTES) for
5975 the insns from FIRST to LAST inclusive that were created by replacing
5976 the insns from ORIG_INSN_FIRST to ORIG_INSN_LAST inclusive. NOTES
5977 are the original REG_NOTES. */
5978
5979void
5980update_life_info (notes, first, last, orig_first_insn, orig_last_insn)
5981 rtx notes;
5982 rtx first, last;
5983 rtx orig_first_insn, orig_last_insn;
5984{
5985 rtx insn, note;
5986 rtx next;
5987 rtx orig_dest, temp;
5988 rtx orig_insn;
5989 rtx tem;
5990
5991 /* Get and save the destination set by the original insn, if there
5992 was only one insn replaced. */
5993
5994 if (orig_first_insn == orig_last_insn)
5995 {
5996 orig_insn = orig_first_insn;
5997 orig_dest = single_set (orig_insn);
5998 if (orig_dest)
5999 orig_dest = SET_DEST (orig_dest);
6000 }
6001 else
6002 {
6003 orig_insn = NULL_RTX;
6004 orig_dest = NULL_RTX;
6005 }
6006
6007 /* Move REG_NOTES from the original insns to where they now belong. */
6008
6009 for (note = notes; note; note = next)
6010 {
6011 next = XEXP (note, 1);
6012 switch (REG_NOTE_KIND (note))
6013 {
6014 case REG_DEAD:
6015 case REG_UNUSED:
6016 /* Move these notes from the original insn to the last new
6017 insn where the register is mentioned. */
6018
6019 for (insn = last; ; insn = PREV_INSN (insn))
6020 {
6021 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6022 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
6023 {
6024 /* Sometimes need to convert REG_UNUSED notes to
6025 REG_DEAD notes. */
6026 if (REG_NOTE_KIND (note) == REG_UNUSED
6027 && GET_CODE (XEXP (note, 0)) == REG
6028 && ! dead_or_set_p (insn, XEXP (note, 0)))
6029 {
6030 PUT_REG_NOTE_KIND (note, REG_DEAD);
6031 }
6032 split_hard_reg_notes (insn, note, first, last);
6033 /* The reg only dies in one insn, the last one that uses
6034 it. */
6035 break;
6036 }
6037 /* It must die somewhere, fail if we couldn't find where it died.
6038
6039 We abort because otherwise the register will be live
6040 longer than it should, and we'll probably take an
6041 abort later. What we should do instead is search back
6042 and find the appropriate places to insert the note. */
6043 if (insn == first)
6044 {
6045 if (REG_NOTE_KIND (note) == REG_DEAD)
6046 {
6047 abort ();
6048 }
6049 break;
6050 }
6051 }
6052 break;
6053
6054 case REG_WAS_0:
6055 {
6056 rtx note_dest;
6057
6058 /* If the insn that set the register to 0 was deleted, this
6059 note cannot be relied on any longer. The destination might
6060 even have been moved to memory.
6061 This was observed for SH4 with execute/920501-6.c compilation,
6062 -O2 -fomit-frame-pointer -finline-functions . */
6063
6064 if (GET_CODE (XEXP (note, 0)) == NOTE
6065 || INSN_DELETED_P (XEXP (note, 0)))
6066 break;
6067 if (orig_insn != NULL_RTX)
6068 {
6069 note_dest = orig_dest;
6070 }
6071 else
6072 {
5aabad00
RH
6073 note_dest = find_insn_with_note (note, orig_first_insn,
6074 orig_last_insn);
f2a1bc02
BM
6075 if (note_dest != NULL_RTX)
6076 {
5aabad00 6077 note_dest = single_set (note_dest);
f2a1bc02 6078 if (note_dest != NULL_RTX)
5aabad00 6079 note_dest = SET_DEST (note_dest);
f2a1bc02
BM
6080 }
6081 }
6082 /* This note applies to the dest of the original insn. Find the
6083 first new insn that now has the same dest, and move the note
6084 there. */
6085
6086 if (! note_dest)
5aabad00 6087 break;
f2a1bc02
BM
6088
6089 for (insn = first; ; insn = NEXT_INSN (insn))
6090 {
6091 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6092 && (temp = single_set (insn))
6093 && rtx_equal_p (SET_DEST (temp), note_dest))
6094 {
6095 XEXP (note, 1) = REG_NOTES (insn);
6096 REG_NOTES (insn) = note;
6097 /* The reg is only zero before one insn, the first that
6098 uses it. */
6099 break;
6100 }
6101 /* If this note refers to a multiple word hard
6102 register, it may have been split into several smaller
6103 hard register references. We could split the notes,
6104 but simply dropping them is good enough. */
6105 if (GET_CODE (note_dest) == REG
6106 && REGNO (note_dest) < FIRST_PSEUDO_REGISTER
6107 && HARD_REGNO_NREGS (REGNO (note_dest),
6108 GET_MODE (note_dest)) > 1)
6109 break;
5aabad00
RH
6110
6111 /* It must be set somewhere; bail if we couldn't find
f2a1bc02 6112 where it was set. */
f2a1bc02
BM
6113 }
6114 }
6115 break;
6116
6117 case REG_EQUAL:
6118 case REG_EQUIV:
6119 /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
6120 set is meaningless. Just drop the note. */
6121 if (! orig_dest)
6122 break;
6123
6124 case REG_NO_CONFLICT:
5aabad00 6125 case REG_NOALIAS:
f2a1bc02
BM
6126 /* These notes apply to the dest of the original insn. Find the last
6127 new insn that now has the same dest, and move the note there.
6128
6129 If we are replacing multiple insns, just drop the note. */
6130
6131 if (! orig_insn)
6132 break;
6133
6134 if (! orig_dest)
6135 abort ();
6136
6137 for (insn = last; ; insn = PREV_INSN (insn))
6138 {
6139 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6140 && (temp = single_set (insn))
6141 && rtx_equal_p (SET_DEST (temp), orig_dest))
6142 {
6143 XEXP (note, 1) = REG_NOTES (insn);
6144 REG_NOTES (insn) = note;
6145 /* Only put this note on one of the new insns. */
6146 break;
6147 }
6148
6149 /* The original dest must still be set someplace. Abort if we
6150 couldn't find it. */
6151 if (insn == first)
6152 {
6153 /* However, if this note refers to a multiple word hard
6154 register, it may have been split into several smaller
6155 hard register references. We could split the notes,
6156 but simply dropping them is good enough. */
6157 if (GET_CODE (orig_dest) == REG
6158 && REGNO (orig_dest) < FIRST_PSEUDO_REGISTER
6159 && HARD_REGNO_NREGS (REGNO (orig_dest),
6160 GET_MODE (orig_dest)) > 1)
6161 break;
6162 /* Likewise for multi-word memory references. */
6163 if (GET_CODE (orig_dest) == MEM
6164 && GET_MODE_SIZE (GET_MODE (orig_dest)) > MOVE_MAX)
6165 break;
6166 abort ();
6167 }
6168 }
6169 break;
6170
6171 case REG_LIBCALL:
6172 /* Move a REG_LIBCALL note to the first insn created, and update
6173 the corresponding REG_RETVAL note. */
6174 XEXP (note, 1) = REG_NOTES (first);
6175 REG_NOTES (first) = note;
6176
6177 insn = XEXP (note, 0);
6178 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
6179 if (note)
6180 XEXP (note, 0) = first;
6181 break;
6182
6183 case REG_EXEC_COUNT:
6184 /* Move a REG_EXEC_COUNT note to the first insn created. */
6185 XEXP (note, 1) = REG_NOTES (first);
6186 REG_NOTES (first) = note;
6187 break;
6188
6189 case REG_RETVAL:
6190 /* Move a REG_RETVAL note to the last insn created, and update
6191 the corresponding REG_LIBCALL note. */
6192 XEXP (note, 1) = REG_NOTES (last);
6193 REG_NOTES (last) = note;
6194
6195 insn = XEXP (note, 0);
6196 note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
6197 if (note)
6198 XEXP (note, 0) = last;
6199 break;
6200
6201 case REG_NONNEG:
6202 case REG_BR_PROB:
6203 /* This should be moved to whichever instruction is a JUMP_INSN. */
6204
6205 for (insn = last; ; insn = PREV_INSN (insn))
6206 {
6207 if (GET_CODE (insn) == JUMP_INSN)
6208 {
6209 XEXP (note, 1) = REG_NOTES (insn);
6210 REG_NOTES (insn) = note;
6211 /* Only put this note on one of the new insns. */
6212 break;
6213 }
6214 /* Fail if we couldn't find a JUMP_INSN. */
6215 if (insn == first)
6216 abort ();
6217 }
6218 break;
6219
6220 case REG_INC:
6221 /* reload sometimes leaves obsolete REG_INC notes around. */
6222 if (reload_completed)
6223 break;
6224 /* This should be moved to whichever instruction now has the
6225 increment operation. */
6226 abort ();
6227
6228 case REG_LABEL:
6229 /* Should be moved to the new insn(s) which use the label. */
6230 for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
6231 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6232 && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
6233 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL,
6234 XEXP (note, 0),
6235 REG_NOTES (insn));
6236 break;
6237
6238 case REG_CC_SETTER:
6239 case REG_CC_USER:
6240 /* These two notes will never appear until after reorg, so we don't
6241 have to handle them here. */
6242 default:
6243 abort ();
6244 }
6245 }
6246
6247 /* Each new insn created has a new set. If the destination is a
6248 register, then this reg is now live across several insns, whereas
6249 previously the dest reg was born and died within the same insn.
6250 To reflect this, we now need a REG_DEAD note on the insn where
6251 this dest reg dies.
6252
6253 Similarly, the new insns may have clobbers that need REG_UNUSED
6254 notes. */
6255
6256 for (insn = first; ;insn = NEXT_INSN (insn))
6257 {
6258 rtx pat;
6259 int i;
6260
6261 pat = PATTERN (insn);
6262 if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
6263 new_insn_dead_notes (pat, insn, first, last,
6264 orig_first_insn, orig_last_insn);
6265 else if (GET_CODE (pat) == PARALLEL)
6266 {
6267 for (i = 0; i < XVECLEN (pat, 0); i++)
6268 {
6269 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
6270 || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
6271 {
6272 rtx parpat = XVECEXP (pat, 0, i);
6273
6274 new_insn_dead_notes (parpat, insn, first, last,
6275 orig_first_insn, orig_last_insn);
6276 }
6277 }
6278 }
6279 if (insn == last)
6280 {
6281 break;
6282 }
6283 }
6284
6285 /* Check to see if we have any REG_DEAD notes on insns previous to
6286 the new ones that are now incorrect and need to be removed. */
6287
6288 for (insn = orig_first_insn; ; insn = NEXT_INSN (insn))
6289 {
6290 maybe_remove_dead_notes (insn, insn, first, last,
6291 orig_first_insn, orig_last_insn);
6292
6293 if (insn == orig_last_insn)
6294 break;
6295 }
6296
6297 /* Update reg_n_sets. This is necessary to prevent local alloc from
6298 converting REG_EQUAL notes to REG_EQUIV when the new insns are setting
6299 a reg multiple times instead of once. */
6300
6301 for (tem = orig_first_insn; tem != NULL_RTX; tem = NEXT_INSN (tem))
6302 {
6303 rtx x;
6304 RTX_CODE code;
6305
6306 if (GET_RTX_CLASS (GET_CODE (tem)) != 'i')
6307 continue;
6308
6309 x = PATTERN (tem);
6310 code = GET_CODE (x);
6311 if (code == SET || code == CLOBBER)
6312 update_n_sets (x, -1);
6313 else if (code == PARALLEL)
6314 {
6315 int i;
6316 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6317 {
6318 code = GET_CODE (XVECEXP (x, 0, i));
6319 if (code == SET || code == CLOBBER)
6320 update_n_sets (XVECEXP (x, 0, i), -1);
6321 }
6322 }
6323 if (tem == orig_last_insn)
6324 break;
6325 }
6326
6327 for (insn = first; ; insn = NEXT_INSN (insn))
6328 {
6329 rtx x = PATTERN (insn);
6330 RTX_CODE code = GET_CODE (x);
6331
6332 if (code == SET || code == CLOBBER)
6333 update_n_sets (x, 1);
6334 else if (code == PARALLEL)
6335 {
6336 int i;
6337 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6338 {
6339 code = GET_CODE (XVECEXP (x, 0, i));
6340 if (code == SET || code == CLOBBER)
6341 update_n_sets (XVECEXP (x, 0, i), 1);
6342 }
6343 }
6344
6345 if (insn == last)
6346 break;
6347 }
6348}
6349\f
6350/* Prepends the set of REG_NOTES in NEW to NOTES, and returns NEW. */
6351static rtx
6352prepend_reg_notes (notes, new)
6353 rtx notes, new;
6354{
6355 rtx end;
6356
6357 if (new == NULL_RTX)
6358 {
6359 return notes;
6360 }
6361 if (notes == NULL_RTX)
6362 {
6363 return new;
6364 }
6365 end = new;
6366 while (XEXP (end, 1) != NULL_RTX)
6367 {
6368 end = XEXP (end, 1);
6369 }
6370 XEXP (end, 1) = notes;
6371 return new;
6372}
6373\f
6374/* Replace the insns from FIRST to LAST inclusive with the set of insns in
6375 NEW, and update the life analysis info accordingly. */
6376void
6377replace_insns (first, last, first_new, notes)
6378 rtx first, last, first_new, notes;
6379{
6380 rtx stop = NEXT_INSN (last);
f2a1bc02 6381 rtx prev = PREV_INSN (first);
e2bef702 6382 rtx last_new, curr;
f2a1bc02
BM
6383 int i;
6384
6385 if (notes == NULL_RTX)
6386 {
6387 for (curr = first; curr != stop; curr = NEXT_INSN (curr))
5aabad00 6388 if (GET_RTX_CLASS (GET_CODE (curr)) == 'i')
f2a1bc02 6389 notes = prepend_reg_notes (notes, REG_NOTES (curr));
f2a1bc02 6390 }
5aabad00 6391
f2a1bc02
BM
6392 last_new = emit_insn_after (first_new, prev);
6393 first_new = NEXT_INSN (prev);
5aabad00 6394
f2a1bc02
BM
6395 for (i = 0; i < n_basic_blocks; i++)
6396 {
6397 if (BLOCK_HEAD (i) == first)
5aabad00 6398 BLOCK_HEAD (i) = first_new;
f2a1bc02 6399 if (BLOCK_END (i) == last)
5aabad00 6400 BLOCK_END (i) = last_new;
f2a1bc02
BM
6401 }
6402 /* This is probably bogus. */
6403 if (first_new == last_new)
6404 {
6405 if (GET_CODE (first_new) == SEQUENCE)
6406 {
6407 first_new = XVECEXP (first_new, 0, 0);
6408 last_new = XVECEXP (last_new, 0, XVECLEN (last_new, 0) - 1);
6409 }
6410 }
6411 update_life_info (notes, first_new, last_new, first, last);
5aabad00 6412 flow_delete_insn_chain (first, last);
f2a1bc02
BM
6413}
6414\f
34487bf8
RH
6415/* Verify the CFG consistency. This function check some CFG invariants and
6416 aborts when something is wrong. Hope that this function will help to
6417 convert many optimization passes to preserve CFG consistent.
6418
6419 Currently it does following checks:
6420
6421 - test head/end pointers
6422 - overlapping of basic blocks
6423 - edge list corectness
6424 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
6425 - tails of basic blocks (ensure that boundary is necesary)
6426 - scans body of the basic block for JUMP_INSN, CODE_LABEL
6427 and NOTE_INSN_BASIC_BLOCK
6428 - check that all insns are in the basic blocks
6429 (except the switch handling code, barriers and notes)
6430
6431 In future it can be extended check a lot of other stuff as well
6432 (reachability of basic blocks, life information, etc. etc.). */
6433
6434void
6435verify_flow_info ()
6436{
6437 const int max_uid = get_max_uid ();
6438 const rtx rtx_first = get_insns ();
6439 basic_block *bb_info;
6440 rtx x;
6441 int i;
6442
6443 bb_info = (basic_block *) alloca (max_uid * sizeof (basic_block));
6444 memset (bb_info, 0, max_uid * sizeof (basic_block));
6445
6446 /* First pass check head/end pointers and set bb_info array used by
6447 later passes. */
6448 for (i = n_basic_blocks - 1; i >= 0; i--)
6449 {
6450 basic_block bb = BASIC_BLOCK (i);
6451
6452 /* Check the head pointer and make sure that it is pointing into
6453 insn list. */
6454 for (x = rtx_first; x != NULL_RTX; x = NEXT_INSN (x))
6455 if (x == bb->head)
6456 break;
6457 if (!x)
6458 {
987009bf 6459 error ("Head insn %d for block %d not found in the insn stream.",
34487bf8 6460 INSN_UID (bb->head), bb->index);
987009bf 6461 abort ();
34487bf8
RH
6462 }
6463
6464 /* Check the end pointer and make sure that it is pointing into
6465 insn list. */
6466 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
6467 {
6468 if (bb_info[INSN_UID (x)] != NULL)
6469 {
987009bf 6470 error ("Insn %d is in multiple basic blocks (%d and %d)",
34487bf8 6471 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
987009bf 6472 abort ();
34487bf8
RH
6473 }
6474 bb_info[INSN_UID (x)] = bb;
6475
6476 if (x == bb->end)
6477 break;
6478 }
6479 if (!x)
6480 {
987009bf 6481 error ("End insn %d for block %d not found in the insn stream.",
34487bf8 6482 INSN_UID (bb->end), bb->index);
987009bf 6483 abort ();
34487bf8
RH
6484 }
6485 }
6486
6487 /* Now check the basic blocks (boundaries etc.) */
6488 for (i = n_basic_blocks - 1; i >= 0; i--)
6489 {
6490 basic_block bb = BASIC_BLOCK (i);
6491 /* Check corectness of edge lists */
6492 edge e;
6493
6494 e = bb->succ;
6495 while (e)
6496 {
6497 if (e->src != bb)
6498 {
6499 fprintf (stderr, "verify_flow_info: Basic block %d succ edge is corrupted\n",
6500 bb->index);
6501 fprintf (stderr, "Predecessor: ");
6502 dump_edge_info (stderr, e, 0);
6503 fprintf (stderr, "\nSuccessor: ");
6504 dump_edge_info (stderr, e, 1);
6505 fflush (stderr);
6506 abort ();
6507 }
6508 if (e->dest != EXIT_BLOCK_PTR)
6509 {
6510 edge e2 = e->dest->pred;
6511 while (e2 && e2 != e)
6512 e2 = e2->pred_next;
6513 if (!e2)
6514 {
987009bf
ZW
6515 error ("Basic block %i edge lists are corrupted", bb->index);
6516 abort ();
34487bf8
RH
6517 }
6518 }
6519 e = e->succ_next;
6520 }
6521
6522 e = bb->pred;
6523 while (e)
6524 {
6525 if (e->dest != bb)
6526 {
987009bf
ZW
6527 error ("Basic block %d pred edge is corrupted", bb->index);
6528 fputs ("Predecessor: ", stderr);
34487bf8 6529 dump_edge_info (stderr, e, 0);
987009bf 6530 fputs ("\nSuccessor: ", stderr);
34487bf8 6531 dump_edge_info (stderr, e, 1);
987009bf 6532 fputc ('\n', stderr);
34487bf8
RH
6533 abort ();
6534 }
6535 if (e->src != ENTRY_BLOCK_PTR)
6536 {
6537 edge e2 = e->src->succ;
6538 while (e2 && e2 != e)
6539 e2 = e2->succ_next;
6540 if (!e2)
6541 {
987009bf 6542 error ("Basic block %i edge lists are corrupted", bb->index);
b8024b59 6543 abort ();
34487bf8
RH
6544 }
6545 }
6546 e = e->pred_next;
6547 }
6548
6549 /* OK pointers are correct. Now check the header of basic
6550 block. It ought to contain optional CODE_LABEL followed
6551 by NOTE_BASIC_BLOCK. */
6552 x = bb->head;
6553 if (GET_CODE (x) == CODE_LABEL)
6554 {
6555 if (bb->end == x)
6556 {
987009bf
ZW
6557 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
6558 bb->index);
6559 abort ();
34487bf8
RH
6560 }
6561 x = NEXT_INSN (x);
6562 }
6563 if (GET_CODE (x) != NOTE
6564 || NOTE_LINE_NUMBER (x) != NOTE_INSN_BASIC_BLOCK
6565 || NOTE_BASIC_BLOCK (x) != bb)
6566 {
987009bf 6567 error ("NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
34487bf8 6568 bb->index);
987009bf 6569 abort ();
34487bf8
RH
6570 }
6571
6572 if (bb->end == x)
6573 {
6574 /* Do checks for empty blocks here */
6575 }
6576 else
6577 {
6578 x = NEXT_INSN (x);
6579 while (x)
6580 {
6581 if (GET_CODE (x) == NOTE
6582 && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
6583 {
987009bf 6584 error ("NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d",
34487bf8 6585 INSN_UID (x), bb->index);
987009bf 6586 abort ();
34487bf8
RH
6587 }
6588
6589 if (x == bb->end)
6590 break;
6591
6592 if (GET_CODE (x) == JUMP_INSN
6593 || GET_CODE (x) == CODE_LABEL
6594 || GET_CODE (x) == BARRIER)
6595 {
987009bf
ZW
6596 error ("In basic block %d:", bb->index);
6597 fatal_insn ("Flow control insn inside a basic block", x);
34487bf8
RH
6598 }
6599
6600 x = NEXT_INSN (x);
6601 }
6602 }
6603 }
6604
6605 x = rtx_first;
6606 while (x)
6607 {
6608 if (!bb_info[INSN_UID (x)])
6609 {
6610 switch (GET_CODE (x))
6611 {
6612 case BARRIER:
6613 case NOTE:
6614 break;
6615
6616 case CODE_LABEL:
6617 /* An addr_vec is placed outside any block block. */
6618 if (NEXT_INSN (x)
6619 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
6620 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
6621 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
6622 {
6623 x = NEXT_INSN (x);
6624 }
6625
6626 /* But in any case, non-deletable labels can appear anywhere. */
6627 break;
6628
6629 default:
987009bf 6630 fatal_insn ("Insn outside basic block", x);
34487bf8
RH
6631 }
6632 }
6633
6634 x = NEXT_INSN (x);
6635 }
6636}
410538ea
AM
6637\f
6638/* Functions to access an edge list with a vector representation.
6639 Enough data is kept such that given an index number, the
6640 pred and succ that edge reprsents can be determined, or
6641 given a pred and a succ, it's index number can be returned.
6642 This allows algorithms which comsume a lot of memory to
6643 represent the normally full matrix of edge (pred,succ) with a
6644 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
6645 wasted space in the client code due to sparse flow graphs. */
6646
6647/* This functions initializes the edge list. Basically the entire
6648 flowgraph is processed, and all edges are assigned a number,
6649 and the data structure is filed in. */
6650struct edge_list *
6651create_edge_list ()
6652{
6653 struct edge_list *elist;
6654 edge e;
6655 int num_edges;
e2bef702 6656 int x;
410538ea
AM
6657 int block_count;
6658
6659 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
6660
6661 num_edges = 0;
6662
6663 /* Determine the number of edges in the flow graph by counting successor
6664 edges on each basic block. */
6665 for (x = 0; x < n_basic_blocks; x++)
6666 {
6667 basic_block bb = BASIC_BLOCK (x);
6668
6669 for (e = bb->succ; e; e = e->succ_next)
6670 num_edges++;
6671 }
6672 /* Don't forget successors of the entry block. */
6673 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6674 num_edges++;
6675
57ad4479 6676 elist = xmalloc (sizeof (struct edge_list));
410538ea
AM
6677 elist->num_blocks = block_count;
6678 elist->num_edges = num_edges;
57ad4479 6679 elist->index_to_edge = xmalloc (sizeof (edge) * num_edges);
410538ea
AM
6680
6681 num_edges = 0;
6682
6683 /* Follow successors of the entry block, and register these edges. */
6684 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6685 {
6686 elist->index_to_edge[num_edges] = e;
6687 num_edges++;
6688 }
6689
6690 for (x = 0; x < n_basic_blocks; x++)
6691 {
6692 basic_block bb = BASIC_BLOCK (x);
6693
6694 /* Follow all successors of blocks, and register these edges. */
6695 for (e = bb->succ; e; e = e->succ_next)
6696 {
6697 elist->index_to_edge[num_edges] = e;
6698 num_edges++;
6699 }
6700 }
6701 return elist;
6702}
6703
6704/* This function free's memory associated with an edge list. */
6705void
6706free_edge_list (elist)
6707 struct edge_list *elist;
6708{
6709 if (elist)
6710 {
6711 free (elist->index_to_edge);
6712 free (elist);
6713 }
6714}
6715
6716/* This function provides debug output showing an edge list. */
6717void
6718print_edge_list (f, elist)
6719 FILE *f;
6720 struct edge_list *elist;
6721{
6722 int x;
6723 fprintf(f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
6724 elist->num_blocks - 2, elist->num_edges);
6725
6726 for (x = 0; x < elist->num_edges; x++)
6727 {
6728 fprintf (f, " %-4d - edge(", x);
6729 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
6730 fprintf (f,"entry,");
6731 else
6732 fprintf (f,"%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
6733
6734 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
6735 fprintf (f,"exit)\n");
6736 else
6737 fprintf (f,"%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
6738 }
6739}
6740
6741/* This function provides an internal consistancy check of an edge list,
6742 verifying that all edges are present, and that there are no
6743 extra edges. */
6744void
6745verify_edge_list (f, elist)
6746 FILE *f;
6747 struct edge_list *elist;
6748{
6749 int x, pred, succ, index;
410538ea
AM
6750 edge e;
6751
6752 for (x = 0; x < n_basic_blocks; x++)
6753 {
6754 basic_block bb = BASIC_BLOCK (x);
6755
6756 for (e = bb->succ; e; e = e->succ_next)
6757 {
6758 pred = e->src->index;
6759 succ = e->dest->index;
6760 index = EDGE_INDEX (elist, pred, succ);
6761 if (index == EDGE_INDEX_NO_EDGE)
6762 {
6763 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6764 continue;
6765 }
6766 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6767 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6768 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6769 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6770 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6771 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6772 }
6773 }
6774 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
6775 {
6776 pred = e->src->index;
6777 succ = e->dest->index;
6778 index = EDGE_INDEX (elist, pred, succ);
6779 if (index == EDGE_INDEX_NO_EDGE)
6780 {
6781 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
6782 continue;
6783 }
6784 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
6785 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
6786 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
6787 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
6788 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
6789 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
6790 }
6791 /* We've verified that all the edges are in the list, no lets make sure
6792 there are no spurious edges in the list. */
6793
6794 for (pred = 0 ; pred < n_basic_blocks; pred++)
6795 for (succ = 0 ; succ < n_basic_blocks; succ++)
6796 {
6797 basic_block p = BASIC_BLOCK (pred);
6798 basic_block s = BASIC_BLOCK (succ);
6799
6800 int found_edge = 0;
6801
6802 for (e = p->succ; e; e = e->succ_next)
6803 if (e->dest == s)
6804 {
6805 found_edge = 1;
6806 break;
6807 }
6808 for (e = s->pred; e; e = e->pred_next)
6809 if (e->src == p)
6810 {
6811 found_edge = 1;
6812 break;
6813 }
6814 if (EDGE_INDEX (elist, pred, succ) == EDGE_INDEX_NO_EDGE
6815 && found_edge != 0)
6816 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
6817 pred, succ);
6818 if (EDGE_INDEX (elist, pred, succ) != EDGE_INDEX_NO_EDGE
6819 && found_edge == 0)
6820 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
6821 pred, succ, EDGE_INDEX (elist, pred, succ));
6822 }
6823 for (succ = 0 ; succ < n_basic_blocks; succ++)
6824 {
6825 basic_block p = ENTRY_BLOCK_PTR;
6826 basic_block s = BASIC_BLOCK (succ);
6827
6828 int found_edge = 0;
6829
6830 for (e = p->succ; e; e = e->succ_next)
6831 if (e->dest == s)
6832 {
6833 found_edge = 1;
6834 break;
6835 }
6836 for (e = s->pred; e; e = e->pred_next)
6837 if (e->src == p)
6838 {
6839 found_edge = 1;
6840 break;
6841 }
6842 if (EDGE_INDEX (elist, ENTRY_BLOCK, succ) == EDGE_INDEX_NO_EDGE
6843 && found_edge != 0)
6844 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
6845 succ);
6846 if (EDGE_INDEX (elist, ENTRY_BLOCK, succ) != EDGE_INDEX_NO_EDGE
6847 && found_edge == 0)
6848 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
6849 succ, EDGE_INDEX (elist, ENTRY_BLOCK, succ));
6850 }
6851 for (pred = 0 ; pred < n_basic_blocks; pred++)
6852 {
6853 basic_block p = BASIC_BLOCK (pred);
6854 basic_block s = EXIT_BLOCK_PTR;
6855
6856 int found_edge = 0;
6857
6858 for (e = p->succ; e; e = e->succ_next)
6859 if (e->dest == s)
6860 {
6861 found_edge = 1;
6862 break;
6863 }
6864 for (e = s->pred; e; e = e->pred_next)
6865 if (e->src == p)
6866 {
6867 found_edge = 1;
6868 break;
6869 }
6870 if (EDGE_INDEX (elist, pred, EXIT_BLOCK) == EDGE_INDEX_NO_EDGE
6871 && found_edge != 0)
6872 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
6873 pred);
6874 if (EDGE_INDEX (elist, pred, EXIT_BLOCK) != EDGE_INDEX_NO_EDGE
6875 && found_edge == 0)
6876 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
6877 pred, EDGE_INDEX (elist, pred, EXIT_BLOCK));
6878 }
6879}
6880
6881/* This routine will determine what, if any, edge there is between
6882 a specified predecessor and successor. */
6883
6884int
6885find_edge_index (edge_list, pred, succ)
6886 struct edge_list *edge_list;
6887 int pred, succ;
6888{
6889 int x;
6890 for (x = 0; x < NUM_EDGES (edge_list); x++)
6891 {
6892 if (INDEX_EDGE_PRED_BB (edge_list, x)->index == pred
6893 && INDEX_EDGE_SUCC_BB (edge_list, x)->index == succ)
6894 return x;
6895 }
6896 return (EDGE_INDEX_NO_EDGE);
6897}
6898
87fdf7ff
AM
6899/* This function will remove an edge from the flow graph. */
6900static void
6901remove_edge (e)
6902 edge e;
6903{
6904 edge last_pred = NULL;
6905 edge last_succ = NULL;
6906 edge tmp;
6907 basic_block src, dest;
6908 src = e->src;
6909 dest = e->dest;
6910 for (tmp = src->succ; tmp && tmp != e; tmp = tmp->succ_next)
6911 last_succ = tmp;
6912
6913 if (!tmp)
6914 abort ();
6915 if (last_succ)
6916 last_succ->succ_next = e->succ_next;
6917 else
6918 src->succ = e->succ_next;
6919
6920 for (tmp = dest->pred; tmp && tmp != e; tmp = tmp->pred_next)
6921 last_pred = tmp;
6922
6923 if (!tmp)
6924 abort ();
6925 if (last_pred)
6926 last_pred->pred_next = e->pred_next;
6927 else
6928 dest->pred = e->pred_next;
6929
6930 free (e);
6931
6932}
6933
6934/* This routine will remove any fake successor edges for a basic block.
6935 When the edge is removed, it is also removed from whatever predecessor
6936 list it is in. */
6937static void
6938remove_fake_successors (bb)
6939 basic_block bb;
6940{
6941 edge e;
6942 for (e = bb->succ; e ; )
6943 {
6944 edge tmp = e;
6945 e = e->succ_next;
6946 if ((tmp->flags & EDGE_FAKE) == EDGE_FAKE)
6947 remove_edge (tmp);
6948 }
6949}
6950
6951/* This routine will remove all fake edges from the flow graph. If
6952 we remove all fake successors, it will automatically remove all
6953 fake predecessors. */
6954void
6955remove_fake_edges ()
6956{
6957 int x;
6958 edge e;
6959 basic_block bb;
6960
6961 for (x = 0; x < n_basic_blocks; x++)
6962 {
6963 edge tmp, last = NULL;
6964 bb = BASIC_BLOCK (x);
6965 remove_fake_successors (bb);
6966 }
6967 /* we've handled all successors except the entry block's. */
6968 remove_fake_successors (ENTRY_BLOCK_PTR);
6969}
6970
6971/* This functions will add a fake edge between any block which has no
6972 successors, and the exit block. Some data flow equations require these
6973 edges to exist. */
6974void
6975add_fake_exit_edges ()
6976{
6977 int x;
6978
6979 for (x = 0; x < n_basic_blocks; x++)
6980 if (BASIC_BLOCK (x)->succ == NULL)
6981 make_edge (BASIC_BLOCK (x), EXIT_BLOCK_PTR, EDGE_FAKE);
6982}
This page took 1.522382 seconds and 5 git commands to generate.