]> gcc.gnu.org Git - gcc.git/blame - gcc/flow.c
Daily bump.
[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
RK
123#include "rtl.h"
124#include "basic-block.h"
125#include "insn-config.h"
126#include "regs.h"
127#include "hard-reg-set.h"
128#include "flags.h"
129#include "output.h"
b384405b 130#include "function.h"
3d195391 131#include "except.h"
2e107e9e 132#include "toplev.h"
79c9824e 133#include "recog.h"
e881bb1b 134#include "insn-flags.h"
d7429b6a
RK
135
136#include "obstack.h"
137#define obstack_chunk_alloc xmalloc
138#define obstack_chunk_free free
139
e881bb1b
RH
140
141/* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
142 the stack pointer does not matter. The value is tested only in
143 functions that have frame pointers.
144 No definition is equivalent to always zero. */
145#ifndef EXIT_IGNORE_STACK
146#define EXIT_IGNORE_STACK 0
147#endif
148
421382ac 149
7eb136d6
MM
150/* The contents of the current function definition are allocated
151 in this obstack, and all are freed at the end of the function.
152 For top-level functions, this is temporary_obstack.
153 Separate obstacks are made for nested functions. */
154
155extern struct obstack *function_obstack;
156
e881bb1b 157/* Number of basic blocks in the current function. */
d7429b6a 158
e881bb1b 159int n_basic_blocks;
d7429b6a 160
e881bb1b 161/* The basic block array. */
d7429b6a 162
e881bb1b 163varray_type basic_block_info;
d7429b6a 164
e881bb1b 165/* The special entry and exit blocks. */
d7429b6a 166
e881bb1b
RH
167struct basic_block_def entry_exit_blocks[2] =
168{
169 {
170 NULL, /* head */
171 NULL, /* end */
172 NULL, /* pred */
173 NULL, /* succ */
174 NULL, /* local_set */
175 NULL, /* global_live_at_start */
176 NULL, /* global_live_at_end */
177 NULL, /* aux */
178 ENTRY_BLOCK, /* index */
179 0 /* loop_depth */
180 },
181 {
182 NULL, /* head */
183 NULL, /* end */
184 NULL, /* pred */
185 NULL, /* succ */
186 NULL, /* local_set */
187 NULL, /* global_live_at_start */
188 NULL, /* global_live_at_end */
189 NULL, /* aux */
190 EXIT_BLOCK, /* index */
191 0 /* loop_depth */
192 }
193};
d7429b6a 194
56744d1a
JL
195/* Nonzero if the second flow pass has completed. */
196int flow2_completed;
197
d7429b6a
RK
198/* Maximum register number used in this function, plus one. */
199
200int max_regno;
201
b1f21e0a 202/* Indexed by n, giving various register information */
d7429b6a 203
6feacd09 204varray_type reg_n_info;
d7429b6a 205
a494747c
MM
206/* Size of the reg_n_info table. */
207
208unsigned int reg_n_max;
209
d7429b6a
RK
210/* Element N is the next insn that uses (hard or pseudo) register number N
211 within the current basic block; or zero, if there is no such insn.
212 This is valid only during the final backward scan in propagate_block. */
213
214static rtx *reg_next_use;
215
216/* Size of a regset for the current function,
217 in (1) bytes and (2) elements. */
218
219int regset_bytes;
220int regset_size;
221
d7429b6a 222/* Regset of regs live when calls to `setjmp'-like functions happen. */
e881bb1b 223/* ??? Does this exist only for the setjmp-clobbered warning message? */
d7429b6a
RK
224
225regset regs_live_at_setjmp;
226
227/* List made of EXPR_LIST rtx's which gives pairs of pseudo registers
228 that have to go in the same hard reg.
229 The first two regs in the list are a pair, and the next two
230 are another pair, etc. */
231rtx regs_may_share;
232
d7429b6a
RK
233/* Depth within loops of basic block being scanned for lifetime analysis,
234 plus one. This is the weight attached to references to registers. */
235
236static int loop_depth;
237
238/* During propagate_block, this is non-zero if the value of CC0 is live. */
239
240static int cc0_live;
241
db3a887b 242/* During propagate_block, this contains a list of all the MEMs we are
40b5a77c
JL
243 tracking for dead store elimination.
244
245 ?!? Note we leak memory by not free-ing items on this list. We need to
246 write some generic routines to operate on memory lists since cse, gcse,
247 loop, sched, flow and possibly other passes all need to do basically the
248 same operations on these lists. */
d7429b6a 249
db3a887b 250static rtx mem_set_list;
d7429b6a
RK
251
252/* Set of registers that may be eliminable. These are handled specially
253 in updating regs_ever_live. */
254
255static HARD_REG_SET elim_reg_set;
256
e881bb1b
RH
257/* The basic block structure for every insn, indexed by uid. */
258
259varray_type basic_block_for_insn;
260
261/* The labels mentioned in non-jump rtl. Valid during find_basic_blocks. */
262/* ??? Should probably be using LABEL_NUSES instead. It would take a
263 bit of surgery to be able to use or co-opt the routines in jump. */
264
265static rtx label_value_list;
266
267/* INSN_VOLATILE (insn) is 1 if the insn refers to anything volatile. */
268
269#define INSN_VOLATILE(INSN) bitmap_bit_p (uid_volatile, INSN_UID (INSN))
270#define SET_INSN_VOLATILE(INSN) bitmap_set_bit (uid_volatile, INSN_UID (INSN))
271static bitmap uid_volatile;
272
d7429b6a 273/* Forward declarations */
e881bb1b
RH
274static int count_basic_blocks PROTO((rtx));
275static rtx find_basic_blocks_1 PROTO((rtx, rtx*));
276static void create_basic_block PROTO((int, rtx, rtx, rtx));
277static void compute_bb_for_insn PROTO((varray_type, int));
278static void clear_edges PROTO((void));
279static void make_edges PROTO((rtx, rtx*));
280static void make_edge PROTO((basic_block, basic_block, int));
281static void make_label_edge PROTO((basic_block, rtx, int));
282static void mark_critical_edges PROTO((void));
283
284static void commit_one_edge_insertion PROTO((edge));
285
421382ac 286static void delete_unreachable_blocks PROTO((void));
e881bb1b 287static void delete_eh_regions PROTO((void));
eeea333e 288static int can_delete_note_p PROTO((rtx));
e881bb1b
RH
289static void delete_insn_chain PROTO((rtx, rtx));
290static int delete_block PROTO((basic_block));
291static void expunge_block PROTO((basic_block));
292static rtx flow_delete_insn PROTO((rtx));
293static int can_delete_label_p PROTO((rtx));
294static void merge_blocks_nomove PROTO((basic_block, basic_block));
295static int merge_blocks PROTO((edge,basic_block,basic_block));
296static void tidy_fallthru_edge PROTO((edge,basic_block,basic_block));
297static void calculate_loop_depth PROTO((rtx));
298
dc2ede84
BS
299static int set_noop_p PROTO((rtx));
300static int noop_move_p PROTO((rtx));
e881bb1b 301static void notice_stack_pointer_modification PROTO ((rtx, rtx));
dc2ede84
BS
302static void record_volatile_insns PROTO((rtx));
303static void mark_regs_live_at_end PROTO((regset));
11f246f6 304static void life_analysis_1 PROTO((rtx, int, int));
e881bb1b
RH
305static void init_regset_vector PROTO ((regset *, int,
306 struct obstack *));
307static void propagate_block PROTO((regset, rtx, rtx, int,
11f246f6 308 regset, int, int));
e398aa80 309static int insn_dead_p PROTO((rtx, regset, int, rtx));
e658434c
RK
310static int libcall_dead_p PROTO((rtx, regset, rtx, rtx));
311static void mark_set_regs PROTO((regset, regset, rtx,
312 rtx, regset));
313static void mark_set_1 PROTO((regset, regset, rtx,
314 rtx, regset));
1d300e19 315#ifdef AUTO_INC_DEC
e658434c 316static void find_auto_inc PROTO((regset, rtx, rtx));
e658434c
RK
317static int try_pre_increment_1 PROTO((rtx));
318static int try_pre_increment PROTO((rtx, rtx, HOST_WIDE_INT));
1d300e19
KG
319#endif
320static void mark_used_regs PROTO((regset, regset, rtx, int, rtx));
e658434c 321void dump_flow_info PROTO((FILE *));
e881bb1b
RH
322static void dump_edge_info PROTO((FILE *, edge, int));
323
5ece9746
JL
324static int_list_ptr alloc_int_list_node PROTO ((int_list_block **));
325static int_list_ptr add_int_list_node PROTO ((int_list_block **,
326 int_list **, int));
e881bb1b
RH
327
328static void add_pred_succ PROTO ((int, int, int_list_ptr *,
329 int_list_ptr *, int *, int *));
330
4c649323
JL
331static void count_reg_sets_1 PROTO ((rtx));
332static void count_reg_sets PROTO ((rtx));
333static void count_reg_references PROTO ((rtx));
fdb8a883 334static void notice_stack_pointer_modification PROTO ((rtx, rtx));
15e088b2 335static void invalidate_mems_from_autoinc PROTO ((rtx));
34487bf8 336void verify_flow_info PROTO ((void));
d7429b6a 337\f
5ece9746 338/* Find basic blocks of the current function.
e881bb1b
RH
339 F is the first insn of the function and NREGS the number of register
340 numbers in use. */
d7429b6a
RK
341
342void
359da67d 343find_basic_blocks (f, nregs, file, do_cleanup)
d7429b6a 344 rtx f;
e881bb1b
RH
345 int nregs ATTRIBUTE_UNUSED;
346 FILE *file ATTRIBUTE_UNUSED;
359da67d 347 int do_cleanup;
d7429b6a 348{
e881bb1b
RH
349 rtx *bb_eh_end;
350 int max_uid;
d7429b6a 351
e881bb1b
RH
352 /* Flush out existing data. */
353 if (basic_block_info != NULL)
354 {
355 int i;
421382ac 356
e881bb1b 357 clear_edges ();
d7429b6a 358
e881bb1b
RH
359 /* Clear bb->aux on all extant basic blocks. We'll use this as a
360 tag for reuse during create_basic_block, just in case some pass
361 copies around basic block notes improperly. */
362 for (i = 0; i < n_basic_blocks; ++i)
363 BASIC_BLOCK (i)->aux = NULL;
d7429b6a 364
e881bb1b
RH
365 VARRAY_FREE (basic_block_info);
366 }
27249135 367
e881bb1b 368 n_basic_blocks = count_basic_blocks (f);
27249135 369
e881bb1b
RH
370 /* Size the basic block table. The actual structures will be allocated
371 by find_basic_blocks_1, since we want to keep the structure pointers
372 stable across calls to find_basic_blocks. */
373 /* ??? This whole issue would be much simpler if we called find_basic_blocks
374 exactly once, and thereafter we don't have a single long chain of
375 instructions at all until close to the end of compilation when we
376 actually lay them out. */
8cfe18d6 377
e881bb1b
RH
378 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
379
380 /* An array to record the active exception region at the end of each
381 basic block. It is filled in by find_basic_blocks_1 for make_edges. */
382 bb_eh_end = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
d7429b6a 383
e881bb1b 384 label_value_list = find_basic_blocks_1 (f, bb_eh_end);
088e7160 385
e881bb1b
RH
386 /* Record the block to which an insn belongs. */
387 /* ??? This should be done another way, by which (perhaps) a label is
388 tagged directly with the basic block that it starts. It is used for
389 more than that currently, but IMO that is the only valid use. */
390
391 max_uid = get_max_uid ();
d7429b6a 392#ifdef AUTO_INC_DEC
5ece9746
JL
393 /* Leave space for insns life_analysis makes in some cases for auto-inc.
394 These cases are rare, so we don't need too much space. */
e881bb1b 395 max_uid += max_uid / 10;
d7429b6a
RK
396#endif
397
e881bb1b
RH
398 VARRAY_BB_INIT (basic_block_for_insn, max_uid, "basic_block_for_insn");
399 compute_bb_for_insn (basic_block_for_insn, max_uid);
400
401 /* Discover the edges of our cfg. */
d7429b6a 402
e881bb1b 403 make_edges (label_value_list, bb_eh_end);
421382ac 404
e881bb1b 405 /* Delete unreachable blocks. */
d7429b6a 406
359da67d
RH
407 if (do_cleanup)
408 delete_unreachable_blocks ();
e881bb1b
RH
409
410 /* Mark critical edges. */
411
412 mark_critical_edges ();
413
414 /* Discover the loop depth at the start of each basic block to aid
415 register allocation. */
416 calculate_loop_depth (f);
417
418 /* Kill the data we won't maintain. */
419 label_value_list = 0;
34487bf8
RH
420
421#ifdef ENABLE_CHECKING
422 verify_flow_info ();
423#endif
d7429b6a 424}
5ece9746 425
e881bb1b 426/* Count the basic blocks of the function. */
dc2ede84 427
e881bb1b
RH
428static int
429count_basic_blocks (f)
430 rtx f;
431{
432 register rtx insn;
433 register RTX_CODE prev_code;
434 register int count = 0;
435 int eh_region = 0;
e881bb1b
RH
436 int call_had_abnormal_edge = 0;
437 rtx prev_call = NULL_RTX;
dc2ede84 438
e881bb1b
RH
439 prev_code = JUMP_INSN;
440 for (insn = f; insn; insn = NEXT_INSN (insn))
441 {
442 register RTX_CODE code = GET_CODE (insn);
443
e881bb1b
RH
444 if (code == CODE_LABEL
445 || (GET_RTX_CLASS (code) == 'i'
446 && (prev_code == JUMP_INSN
447 || prev_code == BARRIER
448 || (prev_code == CALL_INSN && call_had_abnormal_edge))))
449 {
450 count++;
dc2ede84 451
e881bb1b
RH
452 /* If the previous insn was a call that did not create an
453 abnormal edge, we want to add a nop so that the CALL_INSN
454 itself is not at basic_block_end. This allows us to
455 easily distinguish between normal calls and those which
456 create abnormal edges in the flow graph. */
dc2ede84 457
e881bb1b
RH
458 if (count > 0 && prev_call != 0 && !call_had_abnormal_edge)
459 {
460 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
461 emit_insn_after (nop, prev_call);
462 }
463 }
dc2ede84 464
e881bb1b
RH
465 /* Record whether this call created an edge. */
466 if (code == CALL_INSN)
467 {
6af57aae
AM
468 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
469 int region = (note ? XINT (XEXP (note, 0), 0) : 1);
e881bb1b
RH
470 prev_call = insn;
471 call_had_abnormal_edge = 0;
6af57aae
AM
472
473 /* If there is a specified EH region, we have an edge. */
474 if (eh_region && region > 0)
475 call_had_abnormal_edge = 1;
476 else
e881bb1b 477 {
6af57aae
AM
478 /* If there is a nonlocal goto label and the specified
479 region number isn't -1, we have an edge. (0 means
480 no throw, but might have a nonlocal goto). */
481 if (nonlocal_goto_handler_labels && region >= 0)
e881bb1b
RH
482 call_had_abnormal_edge = 1;
483 }
484 }
485 else if (code != NOTE)
486 prev_call = NULL_RTX;
487
488 if (code != NOTE)
489 prev_code = code;
490 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
491 ++eh_region;
492 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
493 --eh_region;
494
e881bb1b
RH
495 }
496
497 /* The rest of the compiler works a bit smoother when we don't have to
498 check for the edge case of do-nothing functions with no basic blocks. */
499 if (count == 0)
500 {
501 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
502 count = 1;
503 }
504
505 return count;
506}
dc2ede84 507
d7429b6a
RK
508/* Find all basic blocks of the function whose first insn is F.
509 Store the correct data in the tables that describe the basic blocks,
510 set up the chains of references for each CODE_LABEL, and
d7e4fe8b
RS
511 delete any entire basic blocks that cannot be reached.
512
e881bb1b
RH
513 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
514 that are otherwise unreachable may be reachable with a non-local goto.
d7429b6a 515
e881bb1b
RH
516 BB_EH_END is an array in which we record the list of exception regions
517 active at the end of every basic block. */
8329b5ec 518
e881bb1b
RH
519static rtx
520find_basic_blocks_1 (f, bb_eh_end)
521 rtx f;
522 rtx *bb_eh_end;
523{
524 register rtx insn, next;
e881bb1b
RH
525 int call_has_abnormal_edge = 0;
526 int i = 0;
527 rtx bb_note = NULL_RTX;
528 rtx eh_list = NULL_RTX;
529 rtx label_value_list = NULL_RTX;
530 rtx head = NULL_RTX;
531 rtx end = NULL_RTX;
532
533 /* We process the instructions in a slightly different way than we did
534 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
535 closed out the previous block, so that it gets attached at the proper
536 place. Since this form should be equivalent to the previous,
537 find_basic_blocks_0 continues to use the old form as a check. */
d7429b6a 538
e881bb1b
RH
539 for (insn = f; insn; insn = next)
540 {
541 enum rtx_code code = GET_CODE (insn);
d7429b6a 542
e881bb1b 543 next = NEXT_INSN (insn);
d7429b6a 544
e881bb1b 545 if (code == CALL_INSN)
e658434c 546 {
e881bb1b 547 /* Record whether this call created an edge. */
6af57aae
AM
548 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
549 int region = (note ? XINT (XEXP (note, 0), 0) : 1);
e881bb1b 550 call_has_abnormal_edge = 0;
6af57aae
AM
551
552 /* If there is an EH region, we have an edge. */
553 if (eh_list && region > 0)
554 call_has_abnormal_edge = 1;
555 else
e658434c 556 {
6af57aae
AM
557 /* If there is a nonlocal goto label and the specified
558 region number isn't -1, we have an edge. (0 means
559 no throw, but might have a nonlocal goto). */
560 if (nonlocal_goto_handler_labels && region >= 0)
e881bb1b 561 call_has_abnormal_edge = 1;
5c35539b 562 }
e658434c 563 }
d7429b6a 564
e881bb1b 565 switch (code)
e658434c 566 {
e881bb1b
RH
567 case NOTE:
568 {
569 int kind = NOTE_LINE_NUMBER (insn);
570
571 /* Keep a LIFO list of the currently active exception notes. */
572 if (kind == NOTE_INSN_EH_REGION_BEG)
573 eh_list = gen_rtx_INSN_LIST (VOIDmode, insn, eh_list);
574 else if (kind == NOTE_INSN_EH_REGION_END)
575 eh_list = XEXP (eh_list, 1);
576
577 /* Look for basic block notes with which to keep the
578 basic_block_info pointers stable. Unthread the note now;
579 we'll put it back at the right place in create_basic_block.
580 Or not at all if we've already found a note in this block. */
581 else if (kind == NOTE_INSN_BASIC_BLOCK)
582 {
583 if (bb_note == NULL_RTX)
584 bb_note = insn;
585 next = flow_delete_insn (insn);
586 }
e658434c 587
e881bb1b
RH
588 break;
589 }
d7429b6a 590
e881bb1b
RH
591 case CODE_LABEL:
592 /* A basic block starts at a label. If we've closed one off due
593 to a barrier or some such, no need to do it again. */
594 if (head != NULL_RTX)
2ec1535d 595 {
e881bb1b
RH
596 /* While we now have edge lists with which other portions of
597 the compiler might determine a call ending a basic block
598 does not imply an abnormal edge, it will be a bit before
599 everything can be updated. So continue to emit a noop at
600 the end of such a block. */
601 if (GET_CODE (end) == CALL_INSN)
602 {
603 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
604 end = emit_insn_after (nop, end);
605 }
606
607 bb_eh_end[i] = eh_list;
608 create_basic_block (i++, head, end, bb_note);
609 bb_note = NULL_RTX;
2ec1535d 610 }
e881bb1b
RH
611 head = end = insn;
612 break;
d06c6389 613
e881bb1b
RH
614 case JUMP_INSN:
615 /* A basic block ends at a jump. */
616 if (head == NULL_RTX)
617 head = insn;
618 else
619 {
620 /* ??? Make a special check for table jumps. The way this
621 happens is truely and amazingly gross. We are about to
622 create a basic block that contains just a code label and
623 an addr*vec jump insn. Worse, an addr_diff_vec creates
624 its own natural loop.
5c35539b 625
e881bb1b
RH
626 Prevent this bit of brain damage, pasting things together
627 correctly in make_edges.
2c3a56ad 628
e881bb1b
RH
629 The correct solution involves emitting the table directly
630 on the tablejump instruction as a note, or JUMP_LABEL. */
e658434c 631
e881bb1b
RH
632 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
633 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
634 {
635 head = end = NULL;
636 n_basic_blocks--;
637 break;
638 }
639 }
640 end = insn;
641 goto new_bb_inclusive;
d7429b6a 642
e881bb1b
RH
643 case BARRIER:
644 /* A basic block ends at a barrier. It may be that an unconditional
645 jump already closed the basic block -- no need to do it again. */
646 if (head == NULL_RTX)
647 break;
d7429b6a 648
e881bb1b
RH
649 /* While we now have edge lists with which other portions of the
650 compiler might determine a call ending a basic block does not
651 imply an abnormal edge, it will be a bit before everything can
652 be updated. So continue to emit a noop at the end of such a
653 block. */
654 if (GET_CODE (end) == CALL_INSN)
655 {
656 rtx nop = gen_rtx_USE (VOIDmode, const0_rtx);
657 end = emit_insn_after (nop, end);
658 }
659 goto new_bb_exclusive;
660
661 case CALL_INSN:
662 /* A basic block ends at a call that can either throw or
663 do a non-local goto. */
664 if (call_has_abnormal_edge)
665 {
666 new_bb_inclusive:
667 if (head == NULL_RTX)
668 head = insn;
669 end = insn;
670
671 new_bb_exclusive:
672 bb_eh_end[i] = eh_list;
673 create_basic_block (i++, head, end, bb_note);
674 head = end = NULL_RTX;
675 bb_note = NULL_RTX;
676 break;
677 }
678 /* FALLTHRU */
d7429b6a 679
e881bb1b
RH
680 default:
681 if (GET_RTX_CLASS (code) == 'i')
682 {
683 if (head == NULL_RTX)
684 head = insn;
685 end = insn;
686 }
687 break;
688 }
d7429b6a 689
e881bb1b 690 if (GET_RTX_CLASS (code) == 'i')
d7429b6a 691 {
e881bb1b 692 rtx note;
421382ac 693
e881bb1b
RH
694 /* Make a list of all labels referred to other than by jumps
695 (which just don't have the REG_LABEL notes).
2ec1535d 696
e881bb1b
RH
697 Make a special exception for labels followed by an ADDR*VEC,
698 as this would be a part of the tablejump setup code.
421382ac 699
e881bb1b
RH
700 Make a special exception for the eh_return_stub_label, which
701 we know isn't part of any otherwise visible control flow. */
702
703 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
704 if (REG_NOTE_KIND (note) == REG_LABEL)
705 {
706 rtx lab = XEXP (note, 0), next;
707
708 if (lab == eh_return_stub_label)
709 ;
710 else if ((next = next_nonnote_insn (lab)) != NULL
711 && GET_CODE (next) == JUMP_INSN
712 && (GET_CODE (PATTERN (next)) == ADDR_VEC
713 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
714 ;
715 else
716 label_value_list
717 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (note, 0),
718 label_value_list);
d7429b6a
RK
719 }
720 }
e881bb1b 721 }
d7429b6a 722
e881bb1b
RH
723 if (head != NULL_RTX)
724 {
725 bb_eh_end[i] = eh_list;
726 create_basic_block (i++, head, end, bb_note);
727 }
af14ce9c 728
e881bb1b
RH
729 if (i != n_basic_blocks)
730 abort ();
af14ce9c 731
e881bb1b 732 return label_value_list;
d7429b6a 733}
5ece9746 734
e881bb1b
RH
735/* Create a new basic block consisting of the instructions between
736 HEAD and END inclusive. Reuses the note and basic block struct
737 in BB_NOTE, if any. */
5ece9746 738
e881bb1b
RH
739static void
740create_basic_block (index, head, end, bb_note)
741 int index;
742 rtx head, end, bb_note;
5ece9746 743{
e881bb1b
RH
744 basic_block bb;
745
746 if (bb_note
b3bf5bde 747 && ! RTX_INTEGRATED_P (bb_note)
e881bb1b
RH
748 && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
749 && bb->aux == NULL)
5ece9746 750 {
e881bb1b
RH
751 /* If we found an existing note, thread it back onto the chain. */
752
753 if (GET_CODE (head) == CODE_LABEL)
754 add_insn_after (bb_note, head);
755 else
756 {
757 add_insn_before (bb_note, head);
758 head = bb_note;
759 }
5ece9746 760 }
e881bb1b
RH
761 else
762 {
763 /* Otherwise we must create a note and a basic block structure.
764 Since we allow basic block structs in rtl, give the struct
765 the same lifetime by allocating it off the function obstack
766 rather than using malloc. */
8329b5ec 767
e881bb1b
RH
768 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
769 memset (bb, 0, sizeof (*bb));
421382ac 770
e881bb1b
RH
771 if (GET_CODE (head) == CODE_LABEL)
772 bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
773 else
774 {
775 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
776 head = bb_note;
777 }
778 NOTE_BASIC_BLOCK (bb_note) = bb;
779 }
780
eeea333e
RH
781 /* Always include the bb note in the block. */
782 if (NEXT_INSN (end) == bb_note)
783 end = bb_note;
784
e881bb1b
RH
785 bb->head = head;
786 bb->end = end;
787 bb->index = index;
788 BASIC_BLOCK (index) = bb;
789
790 /* Tag the block so that we know it has been used when considering
791 other basic block notes. */
792 bb->aux = bb;
421382ac 793}
e881bb1b
RH
794\f
795/* Records the basic block struct in BB_FOR_INSN, for every instruction
796 indexed by INSN_UID. MAX is the size of the array. */
421382ac 797
421382ac 798static void
e881bb1b
RH
799compute_bb_for_insn (bb_for_insn, max)
800 varray_type bb_for_insn;
801 int max;
421382ac 802{
e881bb1b 803 int i;
421382ac 804
e881bb1b
RH
805 for (i = 0; i < n_basic_blocks; ++i)
806 {
807 basic_block bb = BASIC_BLOCK (i);
808 rtx insn, end;
809
810 end = bb->end;
811 insn = bb->head;
812 while (1)
813 {
814 int uid = INSN_UID (insn);
815 if (uid < max)
816 VARRAY_BB (bb_for_insn, uid) = bb;
817 if (insn == end)
818 break;
819 insn = NEXT_INSN (insn);
820 }
821 }
421382ac
BS
822}
823
e881bb1b 824/* Free the memory associated with the edge structures. */
421382ac
BS
825
826static void
e881bb1b 827clear_edges ()
421382ac 828{
e881bb1b
RH
829 int i;
830 edge n, e;
421382ac 831
e881bb1b 832 for (i = 0; i < n_basic_blocks; ++i)
421382ac 833 {
e881bb1b 834 basic_block bb = BASIC_BLOCK (i);
421382ac 835
e881bb1b 836 for (e = bb->succ; e ; e = n)
421382ac 837 {
e881bb1b
RH
838 n = e->succ_next;
839 free (e);
421382ac 840 }
e881bb1b
RH
841
842 bb->succ = 0;
843 bb->pred = 0;
844 }
845
846 for (e = ENTRY_BLOCK_PTR->succ; e ; e = n)
847 {
848 n = e->succ_next;
849 free (e);
421382ac 850 }
e881bb1b
RH
851
852 ENTRY_BLOCK_PTR->succ = 0;
853 EXIT_BLOCK_PTR->pred = 0;
421382ac
BS
854}
855
e881bb1b
RH
856/* Identify the edges between basic blocks.
857
858 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
859 that are otherwise unreachable may be reachable with a non-local goto.
860
861 BB_EH_END is an array indexed by basic block number in which we record
862 the list of exception regions active at the end of the basic block. */
863
dc2ede84 864static void
e881bb1b
RH
865make_edges (label_value_list, bb_eh_end)
866 rtx label_value_list;
867 rtx *bb_eh_end;
dc2ede84 868{
e881bb1b 869 int i;
1ef1bf06 870 eh_nesting_info *eh_nest_info = init_eh_nesting_info ();
e881bb1b
RH
871
872 /* Assume no computed jump; revise as we create edges. */
873 current_function_has_computed_jump = 0;
874
875 /* By nature of the way these get numbered, block 0 is always the entry. */
876 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
dc2ede84 877
e881bb1b 878 for (i = 0; i < n_basic_blocks; ++i)
421382ac 879 {
e881bb1b
RH
880 basic_block bb = BASIC_BLOCK (i);
881 rtx insn, x, eh_list;
882 enum rtx_code code;
4b523fc4 883 int force_fallthru = 0;
421382ac 884
e881bb1b
RH
885 /* If we have asynchronous exceptions, scan the notes for all exception
886 regions active in the block. In the normal case, we only need the
887 one active at the end of the block, which is bb_eh_end[i]. */
421382ac 888
e881bb1b
RH
889 eh_list = bb_eh_end[i];
890 if (asynchronous_exceptions)
dc2ede84 891 {
e881bb1b 892 for (insn = bb->end; insn != bb->head; insn = PREV_INSN (insn))
dc2ede84 893 {
e881bb1b
RH
894 if (GET_CODE (insn) == NOTE
895 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
896 eh_list = gen_rtx_INSN_LIST (VOIDmode, insn, eh_list);
dc2ede84 897 }
e881bb1b 898 }
dc2ede84 899
e881bb1b
RH
900 /* Now examine the last instruction of the block, and discover the
901 ways we can leave the block. */
902
903 insn = bb->end;
904 code = GET_CODE (insn);
905
906 /* A branch. */
907 if (code == JUMP_INSN)
908 {
909 rtx tmp;
910
911 /* ??? Recognize a tablejump and do the right thing. */
912 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
913 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
914 && GET_CODE (tmp) == JUMP_INSN
915 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
916 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
917 {
918 rtvec vec;
919 int j;
920
921 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
922 vec = XVEC (PATTERN (tmp), 0);
923 else
924 vec = XVEC (PATTERN (tmp), 1);
925
926 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
927 make_label_edge (bb, XEXP (RTVEC_ELT (vec, j), 0), 0);
4b523fc4
RE
928
929 /* Some targets (eg, ARM) emit a conditional jump that also
930 contains the out-of-range target. Scan for these and
931 add an edge if necessary. */
932 if ((tmp = single_set (insn)) != NULL
933 && SET_DEST (tmp) == pc_rtx
934 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
935 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
936 make_label_edge (bb, XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
937
938#ifdef CASE_DROPS_THROUGH
939 /* Silly VAXen. The ADDR_VEC is going to be in the way of
940 us naturally detecting fallthru into the next block. */
941 force_fallthru = 1;
942#endif
e881bb1b
RH
943 }
944
945 /* If this is a computed jump, then mark it as reaching
946 everything on the label_value_list and forced_labels list. */
947 else if (computed_jump_p (insn))
948 {
dc2ede84 949 current_function_has_computed_jump = 1;
dc2ede84 950
e881bb1b
RH
951 for (x = label_value_list; x; x = XEXP (x, 1))
952 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
953
dc2ede84 954 for (x = forced_labels; x; x = XEXP (x, 1))
e881bb1b 955 make_label_edge (bb, XEXP (x, 0), EDGE_ABNORMAL);
dc2ede84
BS
956 }
957
e881bb1b
RH
958 /* Returns create an exit out. */
959 else if (returnjump_p (insn))
960 make_edge (bb, EXIT_BLOCK_PTR, 0);
961
962 /* Otherwise, we have a plain conditional or unconditional jump. */
963 else
dc2ede84 964 {
e881bb1b
RH
965 if (! JUMP_LABEL (insn))
966 abort ();
967 make_label_edge (bb, JUMP_LABEL (insn), 0);
968 }
969 }
970
971 /* If this is a CALL_INSN, then mark it as reaching the active EH
972 handler for this CALL_INSN. If we're handling asynchronous
973 exceptions then any insn can reach any of the active handlers.
b472794d 974
e881bb1b 975 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
b472794d 976
a3e924fc 977 if (code == CALL_INSN || asynchronous_exceptions)
e881bb1b
RH
978 {
979 int is_call = (code == CALL_INSN ? EDGE_ABNORMAL_CALL : 0);
1ef1bf06
AM
980 handler_info **handler_list;
981 int eh_region = -1;
982 int num;
e881bb1b 983
1ef1bf06
AM
984 if (eh_list)
985 eh_region = NOTE_BLOCK_NUMBER (XEXP (eh_list, 0));
e881bb1b 986
1ef1bf06
AM
987 num = reachable_handlers (eh_region, eh_nest_info,
988 insn, &handler_list);
989 for ( ; num > 0; num--)
e881bb1b 990 {
1ef1bf06
AM
991 make_label_edge (bb, handler_list[num - 1]->handler_label,
992 EDGE_ABNORMAL | EDGE_EH | is_call);
e881bb1b
RH
993 }
994
995 if (code == CALL_INSN && nonlocal_goto_handler_labels)
996 {
dc2ede84
BS
997 /* ??? This could be made smarter: in some cases it's possible
998 to tell that certain calls will not do a nonlocal goto.
999
1000 For example, if the nested functions that do the nonlocal
1001 gotos do not have their addresses taken, then only calls to
1002 those functions or to other nested functions that use them
1003 could possibly do nonlocal gotos. */
1ef1bf06
AM
1004 /* We do know that a REG_EH_REGION note with a value less
1005 than 0 is guaranteed not to perform a non-local goto. */
1006 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1007 if (!note || XINT (XEXP (note, 0), 0) >= 0)
1008 for (x = nonlocal_goto_handler_labels; x ; x = XEXP (x, 1))
1009 make_label_edge (bb, XEXP (x, 0),
1010 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
dc2ede84
BS
1011 }
1012 }
e881bb1b
RH
1013
1014 /* We know something about the structure of the function __throw in
1015 libgcc2.c. It is the only function that ever contains eh_stub
1016 labels. It modifies its return address so that the last block
1017 returns to one of the eh_stub labels within it. So we have to
1018 make additional edges in the flow graph. */
1019 if (i + 1 == n_basic_blocks && eh_return_stub_label != 0)
1020 make_label_edge (bb, eh_return_stub_label, EDGE_EH);
1021
1022 /* Find out if we can drop through to the next block. */
1023 insn = next_nonnote_insn (insn);
4b523fc4 1024 if (!insn || (i + 1 == n_basic_blocks && force_fallthru))
e881bb1b
RH
1025 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1026 else if (i + 1 < n_basic_blocks)
1027 {
1028 rtx tmp = BLOCK_HEAD (i + 1);
1029 if (GET_CODE (tmp) == NOTE)
1030 tmp = next_nonnote_insn (tmp);
4b523fc4 1031 if (force_fallthru || insn == tmp)
e881bb1b
RH
1032 make_edge (bb, BASIC_BLOCK (i + 1), EDGE_FALLTHRU);
1033 }
dc2ede84 1034 }
1ef1bf06 1035 free_eh_nesting_info (eh_nest_info);
e881bb1b
RH
1036}
1037
1038/* Create an edge between two basic blocks. FLAGS are auxiliary information
1039 about the edge that is accumulated between calls. */
1040
1041static void
1042make_edge (src, dst, flags)
1043 basic_block src, dst;
1044 int flags;
1045{
1046 edge e;
1047
1048 /* Make sure we don't add duplicate edges. */
1049
1050 for (e = src->succ; e ; e = e->succ_next)
1051 if (e->dest == dst)
1052 {
1053 e->flags |= flags;
1054 return;
1055 }
1056
1057 e = (edge) xcalloc (1, sizeof (*e));
1058
1059 e->succ_next = src->succ;
1060 e->pred_next = dst->pred;
1061 e->src = src;
1062 e->dest = dst;
1063 e->flags = flags;
1064
1065 src->succ = e;
1066 dst->pred = e;
1067}
1068
1069/* Create an edge from a basic block to a label. */
1070
1071static void
1072make_label_edge (src, label, flags)
1073 basic_block src;
1074 rtx label;
1075 int flags;
1076{
1077 if (GET_CODE (label) != CODE_LABEL)
1078 abort ();
1079
1080 /* If the label was never emitted, this insn is junk, but avoid a
1081 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
1082 as a result of a syntax error and a diagnostic has already been
1083 printed. */
1084
1085 if (INSN_UID (label) == 0)
1086 return;
1087
1088 make_edge (src, BLOCK_FOR_INSN (label), flags);
1089}
e6cfb550 1090
e881bb1b
RH
1091/* Identify critical edges and set the bits appropriately. */
1092static void
1093mark_critical_edges ()
1094{
1095 int i, n = n_basic_blocks;
1096 basic_block bb;
1097
1098 /* We begin with the entry block. This is not terribly important now,
1099 but could be if a front end (Fortran) implemented alternate entry
1100 points. */
1101 bb = ENTRY_BLOCK_PTR;
1102 i = -1;
1103
1104 while (1)
e6cfb550 1105 {
e881bb1b
RH
1106 edge e;
1107
1108 /* (1) Critical edges must have a source with multiple successors. */
1109 if (bb->succ && bb->succ->succ_next)
1110 {
1111 for (e = bb->succ; e ; e = e->succ_next)
1112 {
1113 /* (2) Critical edges must have a destination with multiple
1114 predecessors. Note that we know there is at least one
1115 predecessor -- the edge we followed to get here. */
1116 if (e->dest->pred->pred_next)
1117 e->flags |= EDGE_CRITICAL;
1118 else
1119 e->flags &= ~EDGE_CRITICAL;
1120 }
1121 }
1122 else
1123 {
1124 for (e = bb->succ; e ; e = e->succ_next)
1125 e->flags &= ~EDGE_CRITICAL;
1126 }
1127
1128 if (++i >= n)
1129 break;
1130 bb = BASIC_BLOCK (i);
e6cfb550 1131 }
e881bb1b
RH
1132}
1133\f
1134/* Split a (typically critical) edge. Return the new block.
1135 Abort on abnormal edges.
1136
1137 ??? The code generally expects to be called on critical edges.
1138 The case of a block ending in an unconditional jump to a
1139 block with multiple predecessors is not handled optimally. */
1140
1141basic_block
1142split_edge (edge_in)
1143 edge edge_in;
1144{
1145 basic_block old_pred, bb, old_succ;
1146 edge edge_out;
1147 rtx bb_note;
1148 int i;
1149
1150 /* Abnormal edges cannot be split. */
1151 if ((edge_in->flags & EDGE_ABNORMAL) != 0)
1152 abort ();
1153
1154 old_pred = edge_in->src;
1155 old_succ = edge_in->dest;
1156
1157 /* Remove the existing edge from the destination's pred list. */
1158 {
1159 edge *pp;
1160 for (pp = &old_succ->pred; *pp != edge_in; pp = &(*pp)->pred_next)
1161 continue;
1162 *pp = edge_in->pred_next;
1e7d57a3 1163 edge_in->pred_next = NULL;
e881bb1b
RH
1164 }
1165
1166 /* Create the new structures. */
1167 bb = (basic_block) obstack_alloc (function_obstack, sizeof (*bb));
1168 edge_out = (edge) xcalloc (1, sizeof (*edge_out));
1169
1170 memset (bb, 0, sizeof (*bb));
1171 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
1172 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
1173 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
1174
1175 /* ??? This info is likely going to be out of date very soon. */
1176 CLEAR_REG_SET (bb->local_set);
1177 if (old_succ->global_live_at_start)
1178 {
1179 COPY_REG_SET (bb->global_live_at_start, old_succ->global_live_at_start);
1180 COPY_REG_SET (bb->global_live_at_end, old_succ->global_live_at_start);
1181 }
1182 else
1183 {
1184 CLEAR_REG_SET (bb->global_live_at_start);
1185 CLEAR_REG_SET (bb->global_live_at_end);
1186 }
1187
1188 /* Wire them up. */
1189 bb->pred = edge_in;
1190 bb->succ = edge_out;
1e7d57a3 1191
e881bb1b 1192 edge_in->dest = bb;
1e7d57a3
JH
1193 edge_in->flags &= ~EDGE_CRITICAL;
1194
1195 edge_out->pred_next = old_succ->pred;
1196 edge_out->succ_next = NULL;
e881bb1b
RH
1197 edge_out->src = bb;
1198 edge_out->dest = old_succ;
1e7d57a3
JH
1199 edge_out->flags = EDGE_FALLTHRU;
1200 edge_out->probability = REG_BR_PROB_BASE;
1201
1202 old_succ->pred = edge_out;
e881bb1b
RH
1203
1204 /* Tricky case -- if there existed a fallthru into the successor
1205 (and we're not it) we must add a new unconditional jump around
1206 the new block we're actually interested in.
1207
1208 Further, if that edge is critical, this means a second new basic
1209 block must be created to hold it. In order to simplify correct
1210 insn placement, do this before we touch the existing basic block
1211 ordering for the block we were really wanting. */
1212 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1213 {
1214 edge e;
1e7d57a3 1215 for (e = edge_out->pred_next; e ; e = e->pred_next)
e881bb1b
RH
1216 if (e->flags & EDGE_FALLTHRU)
1217 break;
1218
1219 if (e)
1220 {
1221 basic_block jump_block;
1222 rtx pos;
1223
1224 if ((e->flags & EDGE_CRITICAL) == 0)
1225 {
1226 /* Non critical -- we can simply add a jump to the end
1227 of the existing predecessor. */
1228 jump_block = e->src;
e881bb1b
RH
1229 }
1230 else
1231 {
1232 /* We need a new block to hold the jump. The simplest
1233 way to do the bulk of the work here is to recursively
1234 call ourselves. */
1235 jump_block = split_edge (e);
1236 e = jump_block->succ;
e881bb1b
RH
1237 }
1238
1e7d57a3
JH
1239 /* Now add the jump insn ... */
1240 pos = emit_jump_insn_after (gen_jump (old_succ->head),
1241 jump_block->end);
e881bb1b
RH
1242 jump_block->end = pos;
1243 emit_barrier_after (pos);
1e7d57a3
JH
1244
1245 /* ... let jump know that label is in use, ... */
a8688bd6 1246 JUMP_LABEL (pos) = old_succ->head;
1e7d57a3 1247 ++LABEL_NUSES (old_succ->head);
088e7160 1248
e881bb1b
RH
1249 /* ... and clear fallthru on the outgoing edge. */
1250 e->flags &= ~EDGE_FALLTHRU;
1251
1252 /* Continue splitting the interesting edge. */
1253 }
1254 }
1255
1256 /* Place the new block just in front of the successor. */
1257 VARRAY_GROW (basic_block_info, ++n_basic_blocks);
1258 for (i = n_basic_blocks - 1; i > old_succ->index; --i)
1259 {
1260 basic_block tmp = BASIC_BLOCK (i - 1);
1261 BASIC_BLOCK (i) = tmp;
1262 tmp->index = i;
1263 }
1264 BASIC_BLOCK (i) = bb;
1265 bb->index = i;
1266
1267 /* Create the basic block note. */
1268 bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, old_succ->head);
1269 NOTE_BASIC_BLOCK (bb_note) = bb;
1270 bb->head = bb->end = bb_note;
1271
1272 /* Not quite simple -- for non-fallthru edges, we must adjust the
1273 predecessor's jump instruction to target our new block. */
1274 if ((edge_in->flags & EDGE_FALLTHRU) == 0)
1275 {
1276 rtx tmp, insn = old_pred->end;
1277 rtx old_label = old_succ->head;
1278 rtx new_label = gen_label_rtx ();
1279
1280 if (GET_CODE (insn) != JUMP_INSN)
1281 abort ();
1282
1283 /* ??? Recognize a tablejump and adjust all matching cases. */
1284 if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
1285 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
1286 && GET_CODE (tmp) == JUMP_INSN
1287 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
1288 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
1289 {
1290 rtvec vec;
1291 int j;
1292
1293 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
1294 vec = XVEC (PATTERN (tmp), 0);
1295 else
1296 vec = XVEC (PATTERN (tmp), 1);
1297
1298 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
1299 if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
1300 {
1301 RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (VOIDmode, new_label);
1302 --LABEL_NUSES (old_label);
1303 ++LABEL_NUSES (new_label);
1304 }
1305 }
1306 else
1307 {
1308 /* This would have indicated an abnormal edge. */
1309 if (computed_jump_p (insn))
1310 abort ();
1311
1312 /* A return instruction can't be redirected. */
1313 if (returnjump_p (insn))
1314 abort ();
1315
1316 /* If the insn doesn't go where we think, we're confused. */
1317 if (JUMP_LABEL (insn) != old_label)
1318 abort ();
1319
1320 redirect_jump (insn, new_label);
1321 }
1322
1323 emit_label_before (new_label, bb_note);
1324 bb->head = new_label;
1325 }
1326
e881bb1b
RH
1327 return bb;
1328}
1329
1330/* Queue instructions for insertion on an edge between two basic blocks.
1331 The new instructions and basic blocks (if any) will not appear in the
1332 CFG until commit_edge_insertions is called. */
1333
1334void
1335insert_insn_on_edge (pattern, e)
1336 rtx pattern;
1337 edge e;
1338{
1339 /* We cannot insert instructions on an abnormal critical edge.
1340 It will be easier to find the culprit if we die now. */
1341 if ((e->flags & (EDGE_ABNORMAL|EDGE_CRITICAL))
1342 == (EDGE_ABNORMAL|EDGE_CRITICAL))
1343 abort ();
1344
1345 if (e->insns == NULL_RTX)
1346 start_sequence ();
1347 else
1348 push_to_sequence (e->insns);
1349
1350 emit_insn (pattern);
1351
1352 e->insns = get_insns ();
1353 end_sequence();
1354}
1355
1356/* Update the CFG for the instructions queued on edge E. */
1357
1358static void
1359commit_one_edge_insertion (e)
1360 edge e;
1361{
1362 rtx before = NULL_RTX, after = NULL_RTX, tmp;
1363 basic_block bb;
1364
1365 /* Figure out where to put these things. If the destination has
1366 one predecessor, insert there. Except for the exit block. */
1367 if (e->dest->pred->pred_next == NULL
1368 && e->dest != EXIT_BLOCK_PTR)
1369 {
1370 bb = e->dest;
1371
1372 /* Get the location correct wrt a code label, and "nice" wrt
1373 a basic block note, and before everything else. */
1374 tmp = bb->head;
1375 if (GET_CODE (tmp) == CODE_LABEL)
1376 tmp = NEXT_INSN (tmp);
1377 if (GET_CODE (tmp) == NOTE
1378 && NOTE_LINE_NUMBER (tmp) == NOTE_INSN_BASIC_BLOCK)
1379 tmp = NEXT_INSN (tmp);
1380 if (tmp == bb->head)
1381 before = tmp;
1382 else
1383 after = PREV_INSN (tmp);
1384 }
1385
1386 /* If the source has one successor and the edge is not abnormal,
1387 insert there. Except for the entry block. */
1388 else if ((e->flags & EDGE_ABNORMAL) == 0
1389 && e->src->succ->succ_next == NULL
1390 && e->src != ENTRY_BLOCK_PTR)
1391 {
1392 bb = e->src;
1393 if (GET_CODE (bb->end) == JUMP_INSN)
1394 {
1395 /* ??? Is it possible to wind up with non-simple jumps? Perhaps
1396 a jump with delay slots already filled? */
1397 if (! simplejump_p (bb->end))
1398 abort ();
1399
1400 before = bb->end;
1401 }
1402 else
1403 {
1404 /* We'd better be fallthru, or we've lost track of what's what. */
1405 if ((e->flags & EDGE_FALLTHRU) == 0)
1406 abort ();
1407
1408 after = bb->end;
1409 }
1410 }
1411
1412 /* Otherwise we must split the edge. */
1413 else
1414 {
1415 bb = split_edge (e);
1416 after = bb->end;
1417 }
1418
1419 /* Now that we've found the spot, do the insertion. */
1420 tmp = e->insns;
1421 e->insns = NULL_RTX;
a8688bd6
AM
1422
1423 /* Set the new block number for these insns, if structure is allocated. */
1424 if (basic_block_for_insn)
1425 {
1426 rtx i;
1427 for (i = tmp; i != NULL_RTX; i = NEXT_INSN (i))
1428 set_block_for_insn (i, bb);
1429 }
1430
e881bb1b
RH
1431 if (before)
1432 {
1433 emit_insns_before (tmp, before);
1434 if (before == bb->head)
a8688bd6 1435 bb->head = tmp;
e881bb1b
RH
1436 }
1437 else
1438 {
1439 tmp = emit_insns_after (tmp, after);
1440 if (after == bb->end)
1441 bb->end = tmp;
1442 }
1443}
1444
1445/* Update the CFG for all queued instructions. */
1446
1447void
1448commit_edge_insertions ()
1449{
1450 int i;
1451 basic_block bb;
1452
1453 i = -1;
1454 bb = ENTRY_BLOCK_PTR;
1455 while (1)
1456 {
1457 edge e, next;
1458
1459 for (e = bb->succ; e ; e = next)
1460 {
1461 next = e->succ_next;
1462 if (e->insns)
1463 commit_one_edge_insertion (e);
1464 }
1465
1466 if (++i >= n_basic_blocks)
1467 break;
1468 bb = BASIC_BLOCK (i);
1469 }
1470}
1471\f
1472/* Delete all unreachable basic blocks. */
1473
1474static void
1475delete_unreachable_blocks ()
1476{
1477 basic_block *worklist, *tos;
1478 int deleted_handler;
1479 edge e;
1480 int i, n;
1481
1482 n = n_basic_blocks;
1483 tos = worklist = (basic_block *) alloca (sizeof (basic_block) * n);
1484
1485 /* Use basic_block->aux as a marker. Clear them all. */
1486
1487 for (i = 0; i < n; ++i)
1488 BASIC_BLOCK (i)->aux = NULL;
1489
1490 /* Add our starting points to the worklist. Almost always there will
1491 be only one. It isn't inconcievable that we might one day directly
1492 support Fortran alternate entry points. */
1493
1494 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
aa3d4bf9
RH
1495 {
1496 *tos++ = e->dest;
1497
1498 /* Mark the block with a handy non-null value. */
1499 e->dest->aux = e;
1500 }
e881bb1b
RH
1501
1502 /* Iterate: find everything reachable from what we've already seen. */
1503
1504 while (tos != worklist)
1505 {
1506 basic_block b = *--tos;
1507
e881bb1b
RH
1508 for (e = b->succ; e ; e = e->succ_next)
1509 if (!e->dest->aux)
aa3d4bf9
RH
1510 {
1511 *tos++ = e->dest;
1512 e->dest->aux = e;
1513 }
e881bb1b
RH
1514 }
1515
1516 /* Delete all unreachable basic blocks. Count down so that we don't
1517 interfere with the block renumbering that happens in delete_block. */
1518
1519 deleted_handler = 0;
1520
1521 for (i = n - 1; i >= 0; --i)
1522 {
1523 basic_block b = BASIC_BLOCK (i);
1524
1525 if (b->aux != NULL)
1526 /* This block was found. Tidy up the mark. */
1527 b->aux = NULL;
1528 else
1529 deleted_handler |= delete_block (b);
1530 }
1531
1532 /* Fix up edges that now fall through, or rather should now fall through
1533 but previously required a jump around now deleted blocks. Simplify
1534 the search by only examining blocks numerically adjacent, since this
1535 is how find_basic_blocks created them. */
1536
1537 for (i = 1; i < n_basic_blocks; ++i)
1538 {
1539 basic_block b = BASIC_BLOCK (i - 1);
1540 basic_block c = BASIC_BLOCK (i);
1541 edge s;
1542
abb3f0a9
JL
1543 /* We care about simple conditional or unconditional jumps with
1544 a single successor.
1545
1546 If we had a conditional branch to the next instruction when
1547 find_basic_blocks was called, then there will only be one
1548 out edge for the block which ended with the conditional
1549 branch (since we do not create duplicate edges).
1550
4f282ba1
JL
1551 Furthermore, the edge will be marked as a fallthru because we
1552 merge the flags for the duplicate edges. So we do not want to
1553 check that the edge is not a FALLTHRU edge. */
e881bb1b
RH
1554 if ((s = b->succ) != NULL
1555 && s->succ_next == NULL
e8fe3cc3 1556 && s->dest == c
d0e80719
RH
1557 /* If the jump insn has side effects, we can't tidy the edge. */
1558 && (GET_CODE (b->end) != JUMP_INSN
1559 || onlyjump_p (b->end)))
e881bb1b
RH
1560 tidy_fallthru_edge (s, b, c);
1561 }
1562
1563 /* Attempt to merge blocks as made possible by edge removal. If a block
1564 has only one successor, and the successor has only one predecessor,
1565 they may be combined. */
1566
1567 for (i = 0; i < n_basic_blocks; )
1568 {
1569 basic_block c, b = BASIC_BLOCK (i);
1570 edge s;
1571
1572 /* A loop because chains of blocks might be combineable. */
1573 while ((s = b->succ) != NULL
1574 && s->succ_next == NULL
9a2cab6e 1575 && (s->flags & EDGE_EH) == 0
e881bb1b
RH
1576 && (c = s->dest) != EXIT_BLOCK_PTR
1577 && c->pred->pred_next == NULL
d0e80719
RH
1578 /* If the jump insn has side effects, we can't kill the edge. */
1579 && (GET_CODE (b->end) != JUMP_INSN
1580 || onlyjump_p (b->end))
e881bb1b
RH
1581 && merge_blocks (s, b, c))
1582 continue;
1583
1584 /* Don't get confused by the index shift caused by deleting blocks. */
1585 i = b->index + 1;
1586 }
1587
1588 /* If we deleted an exception handler, we may have EH region begin/end
1589 blocks to remove as well. */
1590 if (deleted_handler)
1591 delete_eh_regions ();
1592}
1593
1594/* Find EH regions for which there is no longer a handler, and delete them. */
1595
1596static void
1597delete_eh_regions ()
1598{
1599 rtx insn;
1600
1ef1bf06
AM
1601 update_rethrow_references ();
1602
e881bb1b
RH
1603 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1604 if (GET_CODE (insn) == NOTE)
1605 {
1606 if ((NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG) ||
1607 (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1608 {
1609 int num = CODE_LABEL_NUMBER (insn);
1ef1bf06
AM
1610 /* A NULL handler indicates a region is no longer needed,
1611 as long as it isn't the target of a rethrow. */
1612 if (get_first_handler (num) == NULL && ! rethrow_used (num))
e881bb1b
RH
1613 {
1614 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1615 NOTE_SOURCE_FILE (insn) = 0;
1616 }
1617 }
1618 }
1619}
1620
1621/* Return true if NOTE is not one of the ones that must be kept paired,
1622 so that we may simply delete them. */
1623
1624static int
eeea333e 1625can_delete_note_p (note)
e881bb1b
RH
1626 rtx note;
1627{
1628 return (NOTE_LINE_NUMBER (note) == NOTE_INSN_DELETED
1629 || NOTE_LINE_NUMBER (note) == NOTE_INSN_BASIC_BLOCK);
1630}
1631
1632/* Unlink a chain of insns between START and FINISH, leaving notes
1633 that must be paired. */
1634
1635static void
1636delete_insn_chain (start, finish)
1637 rtx start, finish;
1638{
1639 /* Unchain the insns one by one. It would be quicker to delete all
1640 of these with a single unchaining, rather than one at a time, but
1641 we need to keep the NOTE's. */
1642
1643 rtx next;
1644
1645 while (1)
1646 {
1647 next = NEXT_INSN (start);
eeea333e
RH
1648 if (GET_CODE (start) == NOTE && !can_delete_note_p (start))
1649 ;
1650 else if (GET_CODE (start) == CODE_LABEL && !can_delete_label_p (start))
1651 ;
1652 else
e881bb1b
RH
1653 next = flow_delete_insn (start);
1654
1655 if (start == finish)
1656 break;
1657 start = next;
1658 }
1659}
1660
1661/* Delete the insns in a (non-live) block. We physically delete every
1662 non-deleted-note insn, and update the flow graph appropriately.
1663
1664 Return nonzero if we deleted an exception handler. */
1665
1666/* ??? Preserving all such notes strikes me as wrong. It would be nice
1667 to post-process the stream to remove empty blocks, loops, ranges, etc. */
1668
1669static int
1670delete_block (b)
1671 basic_block b;
1672{
1673 int deleted_handler = 0;
1674 rtx insn, end;
1675
1676 /* If the head of this block is a CODE_LABEL, then it might be the
1677 label for an exception handler which can't be reached.
1678
1679 We need to remove the label from the exception_handler_label list
1680 and remove the associated NOTE_EH_REGION_BEG and NOTE_EH_REGION_END
1681 notes. */
1682
1683 insn = b->head;
088e7160 1684
e881bb1b
RH
1685 if (GET_CODE (insn) == CODE_LABEL)
1686 {
1687 rtx x, *prev = &exception_handler_labels;
1688
1689 for (x = exception_handler_labels; x; x = XEXP (x, 1))
1690 {
1691 if (XEXP (x, 0) == insn)
1692 {
1693 /* Found a match, splice this label out of the EH label list. */
1694 *prev = XEXP (x, 1);
1695 XEXP (x, 1) = NULL_RTX;
1696 XEXP (x, 0) = NULL_RTX;
1697
1698 /* Remove the handler from all regions */
1699 remove_handler (insn);
1700 deleted_handler = 1;
1701 break;
1702 }
1703 prev = &XEXP (x, 1);
1704 }
1705
1706 /* This label may be referenced by code solely for its value, or
1707 referenced by static data, or something. We have determined
1708 that it is not reachable, but cannot delete the label itself.
1709 Save code space and continue to delete the balance of the block,
1710 along with properly updating the cfg. */
1711 if (!can_delete_label_p (insn))
1712 {
1713 /* If we've only got one of these, skip the whole deleting
1714 insns thing. */
1715 if (insn == b->end)
1716 goto no_delete_insns;
1717 insn = NEXT_INSN (insn);
1718 }
1719 }
1720
1721 /* Selectively unlink the insn chain. Include any BARRIER that may
1722 follow the basic block. */
1723 end = next_nonnote_insn (b->end);
1724 if (!end || GET_CODE (end) != BARRIER)
1725 end = b->end;
1726 delete_insn_chain (insn, end);
1727
1728no_delete_insns:
1729
1730 /* Remove the edges into and out of this block. Note that there may
1731 indeed be edges in, if we are removing an unreachable loop. */
1732 {
1733 edge e, next, *q;
1734
1735 for (e = b->pred; e ; e = next)
1736 {
1737 for (q = &e->src->succ; *q != e; q = &(*q)->succ_next)
1738 continue;
1739 *q = e->succ_next;
1740 next = e->pred_next;
1741 free (e);
1742 }
1743 for (e = b->succ; e ; e = next)
1744 {
1745 for (q = &e->dest->pred; *q != e; q = &(*q)->pred_next)
1746 continue;
1747 *q = e->pred_next;
1748 next = e->succ_next;
1749 free (e);
1750 }
1751
1752 b->pred = NULL;
1753 b->succ = NULL;
1754 }
1755
1756 /* Remove the basic block from the array, and compact behind it. */
1757 expunge_block (b);
1758
1759 return deleted_handler;
1760}
1761
1762/* Remove block B from the basic block array and compact behind it. */
1763
1764static void
1765expunge_block (b)
1766 basic_block b;
1767{
1768 int i, n = n_basic_blocks;
1769
1770 for (i = b->index; i + 1 < n; ++i)
1771 {
1772 basic_block x = BASIC_BLOCK (i + 1);
1773 BASIC_BLOCK (i) = x;
1774 x->index = i;
1775 }
1776
1777 basic_block_info->num_elements--;
1778 n_basic_blocks--;
1779}
1780
1781/* Delete INSN by patching it out. Return the next insn. */
1782
1783static rtx
1784flow_delete_insn (insn)
1785 rtx insn;
1786{
1787 rtx prev = PREV_INSN (insn);
1788 rtx next = NEXT_INSN (insn);
1789
1790 PREV_INSN (insn) = NULL_RTX;
1791 NEXT_INSN (insn) = NULL_RTX;
1792
1793 if (prev)
1794 NEXT_INSN (prev) = next;
1795 if (next)
1796 PREV_INSN (next) = prev;
1797 else
1798 set_last_insn (prev);
e6cfb550 1799
55a98783
JL
1800 if (GET_CODE (insn) == CODE_LABEL)
1801 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
1802
e881bb1b
RH
1803 /* If deleting a jump, decrement the use count of the label. Deleting
1804 the label itself should happen in the normal course of block merging. */
1805 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1806 LABEL_NUSES (JUMP_LABEL (insn))--;
1807
1808 return next;
d7429b6a 1809}
8329b5ec 1810
e881bb1b
RH
1811/* True if a given label can be deleted. */
1812
1813static int
1814can_delete_label_p (label)
1815 rtx label;
dc2ede84 1816{
e881bb1b 1817 rtx x;
dc2ede84 1818
e881bb1b
RH
1819 if (LABEL_PRESERVE_P (label))
1820 return 0;
421382ac 1821
e881bb1b
RH
1822 for (x = forced_labels; x ; x = XEXP (x, 1))
1823 if (label == XEXP (x, 0))
1824 return 0;
1825 for (x = label_value_list; x ; x = XEXP (x, 1))
1826 if (label == XEXP (x, 0))
1827 return 0;
1828 for (x = exception_handler_labels; x ; x = XEXP (x, 1))
1829 if (label == XEXP (x, 0))
1830 return 0;
dc2ede84 1831
abb3f0a9 1832 /* User declared labels must be preserved. */
088e7160 1833 if (LABEL_NAME (label) != 0)
abb3f0a9 1834 return 0;
088e7160 1835
e881bb1b
RH
1836 return 1;
1837}
421382ac 1838
e881bb1b
RH
1839/* Blocks A and B are to be merged into a single block. The insns
1840 are already contiguous, hence `nomove'. */
421382ac 1841
e881bb1b
RH
1842static void
1843merge_blocks_nomove (a, b)
1844 basic_block a, b;
1845{
1846 edge e;
f5c14c21
RH
1847 rtx b_head, b_end, a_end;
1848 int b_empty = 0;
1849
1850 /* If there was a CODE_LABEL beginning B, delete it. */
1851 b_head = b->head;
1852 b_end = b->end;
1853 if (GET_CODE (b_head) == CODE_LABEL)
1854 {
1855 /* Detect basic blocks with nothing but a label. This can happen
1856 in particular at the end of a function. */
1857 if (b_head == b_end)
1858 b_empty = 1;
1859 b_head = flow_delete_insn (b_head);
1860 }
1861
1862 /* Delete the basic block note. */
1863 if (GET_CODE (b_head) == NOTE
1864 && NOTE_LINE_NUMBER (b_head) == NOTE_INSN_BASIC_BLOCK)
1865 {
1866 if (b_head == b_end)
1867 b_empty = 1;
1868 b_head = flow_delete_insn (b_head);
1869 }
421382ac 1870
e881bb1b 1871 /* If there was a jump out of A, delete it. */
f5c14c21
RH
1872 a_end = a->end;
1873 if (GET_CODE (a_end) == JUMP_INSN)
e881bb1b 1874 {
f5c14c21 1875 rtx prev;
86879c21 1876
f5c14c21
RH
1877 prev = prev_nonnote_insn (a_end);
1878 if (!prev)
1879 prev = a->head;
86879c21
JL
1880
1881#ifdef HAVE_cc0
f5c14c21
RH
1882 /* If this was a conditional jump, we need to also delete
1883 the insn that set cc0. */
86879c21 1884
f5c14c21
RH
1885 if (prev && sets_cc0_p (prev))
1886 {
1887 rtx tmp = prev;
1888 prev = prev_nonnote_insn (prev);
1889 if (!prev)
1890 prev = a->head;
e881bb1b 1891 flow_delete_insn (tmp);
421382ac 1892 }
f5c14c21
RH
1893#endif
1894
1895 /* Note that a->head != a->end, since we should have at least a
1896 bb note plus the jump, so prev != insn. */
1897 flow_delete_insn (a_end);
1898 a_end = prev;
421382ac 1899 }
421382ac 1900
e881bb1b
RH
1901 /* By definition, there should only be one successor of A, and that is
1902 B. Free that edge struct. */
1903 free (a->succ);
1904
1905 /* Adjust the edges out of B for the new owner. */
1906 for (e = b->succ; e ; e = e->succ_next)
1907 e->src = a;
1908 a->succ = b->succ;
1909
e881bb1b 1910 /* Reassociate the insns of B with A. */
f5c14c21 1911 if (!b_empty)
e881bb1b 1912 {
f5c14c21
RH
1913 BLOCK_FOR_INSN (b_head) = a;
1914 while (b_head != b_end)
dc2ede84 1915 {
f5c14c21
RH
1916 b_head = NEXT_INSN (b_head);
1917 BLOCK_FOR_INSN (b_head) = a;
dc2ede84 1918 }
f5c14c21 1919 a_end = b_head;
e881bb1b 1920 }
f5c14c21 1921 a->end = a_end;
e881bb1b
RH
1922
1923 /* Compact the basic block array. */
1924 expunge_block (b);
dc2ede84
BS
1925}
1926
e881bb1b
RH
1927/* Attempt to merge basic blocks that are potentially non-adjacent.
1928 Return true iff the attempt succeeded. */
dc2ede84 1929
dc2ede84 1930static int
e881bb1b
RH
1931merge_blocks (e, b, c)
1932 edge e;
1933 basic_block b, c;
dc2ede84 1934{
e881bb1b
RH
1935 /* If B has a fallthru edge to C, no need to move anything. */
1936 if (!(e->flags & EDGE_FALLTHRU))
1937 {
1938 /* ??? From here on out we must make sure to not munge nesting
1939 of exception regions and lexical blocks. Need to think about
1940 these cases before this gets implemented. */
1941 return 0;
1942
1943 /* If C has an outgoing fallthru, and B does not have an incoming
1944 fallthru, move B before C. The later clause is somewhat arbitrary,
1945 but avoids modifying blocks other than the two we've been given. */
1946
1947 /* Otherwise, move C after B. If C had a fallthru, which doesn't
1948 happen to be the physical successor to B, insert an unconditional
1949 branch. If C already ended with a conditional branch, the new
1950 jump must go in a new basic block D. */
1951 }
dc2ede84 1952
34487bf8
RH
1953 /* If a label still appears somewhere and we cannot delete the label,
1954 then we cannot merge the blocks. The edge was tidied already. */
1955 {
1956 rtx insn, stop = NEXT_INSN (c->head);
1957 for (insn = NEXT_INSN (b->end); insn != stop; insn = NEXT_INSN (insn))
1958 if (GET_CODE (insn) == CODE_LABEL && !can_delete_label_p (insn))
1959 return 0;
1960 }
421382ac 1961
e881bb1b
RH
1962 merge_blocks_nomove (b, c);
1963 return 1;
1964}
421382ac 1965
e881bb1b
RH
1966/* The given edge should potentially a fallthru edge. If that is in
1967 fact true, delete the unconditional jump and barriers that are in
1968 the way. */
1969
1970static void
1971tidy_fallthru_edge (e, b, c)
1972 edge e;
1973 basic_block b, c;
1974{
eeea333e 1975 rtx q;
e881bb1b
RH
1976
1977 /* ??? In a late-running flow pass, other folks may have deleted basic
1978 blocks by nopping out blocks, leaving multiple BARRIERs between here
1979 and the target label. They ought to be chastized and fixed.
1980
eeea333e
RH
1981 We can also wind up with a sequence of undeletable labels between
1982 one block and the next.
dc2ede84 1983
eeea333e
RH
1984 So search through a sequence of barriers, labels, and notes for
1985 the head of block C and assert that we really do fall through. */
421382ac 1986
eeea333e 1987 if (next_real_insn (b->end) != next_real_insn (PREV_INSN (c->head)))
e881bb1b 1988 return;
421382ac 1989
e881bb1b
RH
1990 /* Remove what will soon cease being the jump insn from the source block.
1991 If block B consisted only of this single jump, turn it into a deleted
1992 note. */
1993 q = b->end;
1994 if (GET_CODE (q) == JUMP_INSN)
421382ac 1995 {
86a1db60
RH
1996#ifdef HAVE_cc0
1997 /* If this was a conditional jump, we need to also delete
1998 the insn that set cc0. */
1999 if (! simplejump_p (q) && condjump_p (q))
2000 q = PREV_INSN (q);
2001#endif
2002
e881bb1b
RH
2003 if (b->head == q)
2004 {
2005 PUT_CODE (q, NOTE);
2006 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
2007 NOTE_SOURCE_FILE (q) = 0;
2008 }
e3f6ee23 2009 else
e881bb1b 2010 b->end = q = PREV_INSN (q);
421382ac 2011 }
421382ac 2012
e881bb1b 2013 /* Selectively unlink the sequence. */
86a1db60
RH
2014 if (q != PREV_INSN (c->head))
2015 delete_insn_chain (NEXT_INSN (q), PREV_INSN (c->head));
b7f7462b 2016
e881bb1b
RH
2017 e->flags |= EDGE_FALLTHRU;
2018}
dc2ede84 2019
e881bb1b
RH
2020/* Discover and record the loop depth at the head of each basic block. */
2021
2022static void
2023calculate_loop_depth (insns)
2024 rtx insns;
2025{
2026 basic_block bb;
2027 rtx insn;
2028 int i = 0, depth = 1;
dc2ede84 2029
e881bb1b
RH
2030 bb = BASIC_BLOCK (i);
2031 for (insn = insns; insn ; insn = NEXT_INSN (insn))
dc2ede84 2032 {
e881bb1b
RH
2033 if (insn == bb->head)
2034 {
2035 bb->loop_depth = depth;
2036 if (++i >= n_basic_blocks)
dc2ede84 2037 break;
e881bb1b
RH
2038 bb = BASIC_BLOCK (i);
2039 }
dc2ede84 2040
e881bb1b
RH
2041 if (GET_CODE (insn) == NOTE)
2042 {
2043 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2044 depth++;
2045 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2046 depth--;
2047
2048 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error. */
2049 if (depth == 0)
2050 abort ();
2051 }
2052 }
dc2ede84 2053}
d7429b6a 2054\f
5ece9746
JL
2055/* Perform data flow analysis.
2056 F is the first insn of the function and NREGS the number of register numbers
2057 in use. */
2058
2059void
11f246f6 2060life_analysis (f, nregs, file, remove_dead_code)
5ece9746
JL
2061 rtx f;
2062 int nregs;
2063 FILE *file;
11f246f6 2064 int remove_dead_code;
5ece9746 2065{
5ece9746 2066#ifdef ELIMINABLE_REGS
ecb06768 2067 register size_t i;
5ece9746
JL
2068 static struct {int from, to; } eliminables[] = ELIMINABLE_REGS;
2069#endif
2070
2071 /* Record which registers will be eliminated. We use this in
2072 mark_used_regs. */
2073
2074 CLEAR_HARD_REG_SET (elim_reg_set);
2075
2076#ifdef ELIMINABLE_REGS
2077 for (i = 0; i < sizeof eliminables / sizeof eliminables[0]; i++)
2078 SET_HARD_REG_BIT (elim_reg_set, eliminables[i].from);
2079#else
2080 SET_HARD_REG_BIT (elim_reg_set, FRAME_POINTER_REGNUM);
2081#endif
2082
e881bb1b
RH
2083 /* Allocate a bitmap to be filled in by record_volatile_insns. */
2084 uid_volatile = BITMAP_ALLOCA ();
2085
db3a887b
CB
2086 /* We want alias analysis information for local dead store elimination. */
2087 init_alias_analysis ();
11f246f6 2088 life_analysis_1 (f, nregs, remove_dead_code);
db3a887b
CB
2089 end_alias_analysis ();
2090
5ece9746
JL
2091 if (file)
2092 dump_flow_info (file);
2093
e881bb1b 2094 BITMAP_FREE (uid_volatile);
5ece9746
JL
2095 free_basic_block_vars (1);
2096}
2097
2098/* Free the variables allocated by find_basic_blocks.
2099
e881bb1b 2100 KEEP_HEAD_END_P is non-zero if basic_block_info is not to be freed. */
5ece9746
JL
2101
2102void
2103free_basic_block_vars (keep_head_end_p)
2104 int keep_head_end_p;
2105{
e881bb1b 2106 if (basic_block_for_insn)
5ece9746 2107 {
e881bb1b
RH
2108 VARRAY_FREE (basic_block_for_insn);
2109 basic_block_for_insn = NULL;
5ece9746
JL
2110 }
2111
e881bb1b 2112 if (! keep_head_end_p)
5ece9746 2113 {
e881bb1b
RH
2114 clear_edges ();
2115 VARRAY_FREE (basic_block_info);
2116 n_basic_blocks = 0;
359da67d
RH
2117
2118 ENTRY_BLOCK_PTR->aux = NULL;
2119 ENTRY_BLOCK_PTR->global_live_at_end = NULL;
2120 EXIT_BLOCK_PTR->aux = NULL;
2121 EXIT_BLOCK_PTR->global_live_at_start = NULL;
5ece9746
JL
2122 }
2123}
2124
dc2ede84
BS
2125/* Return nonzero if the destination of SET equals the source. */
2126static int
2127set_noop_p (set)
2128 rtx set;
2129{
2130 rtx src = SET_SRC (set);
2131 rtx dst = SET_DEST (set);
2132 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2133 && REGNO (src) == REGNO (dst))
2134 return 1;
2135 if (GET_CODE (src) != SUBREG || GET_CODE (dst) != SUBREG
2136 || SUBREG_WORD (src) != SUBREG_WORD (dst))
2137 return 0;
2138 src = SUBREG_REG (src);
2139 dst = SUBREG_REG (dst);
2140 if (GET_CODE (src) == REG && GET_CODE (dst) == REG
2141 && REGNO (src) == REGNO (dst))
2142 return 1;
2143 return 0;
2144}
2145
2146/* Return nonzero if an insn consists only of SETs, each of which only sets a
2147 value to itself. */
2148static int
2149noop_move_p (insn)
2150 rtx insn;
2151{
2152 rtx pat = PATTERN (insn);
2153
2154 /* Insns carrying these notes are useful later on. */
2155 if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
2156 return 0;
2157
2158 if (GET_CODE (pat) == SET && set_noop_p (pat))
2159 return 1;
2160
2161 if (GET_CODE (pat) == PARALLEL)
2162 {
2163 int i;
2164 /* If nothing but SETs of registers to themselves,
2165 this insn can also be deleted. */
2166 for (i = 0; i < XVECLEN (pat, 0); i++)
2167 {
2168 rtx tem = XVECEXP (pat, 0, i);
2169
2170 if (GET_CODE (tem) == USE
2171 || GET_CODE (tem) == CLOBBER)
2172 continue;
2173
2174 if (GET_CODE (tem) != SET || ! set_noop_p (tem))
2175 return 0;
2176 }
2177
2178 return 1;
2179 }
2180 return 0;
2181}
2182
fdb8a883
JW
2183static void
2184notice_stack_pointer_modification (x, pat)
2185 rtx x;
2186 rtx pat ATTRIBUTE_UNUSED;
2187{
2188 if (x == stack_pointer_rtx
2189 /* The stack pointer is only modified indirectly as the result
2190 of a push until later in flow. See the comments in rtl.texi
2191 regarding Embedded Side-Effects on Addresses. */
2192 || (GET_CODE (x) == MEM
2193 && (GET_CODE (XEXP (x, 0)) == PRE_DEC
2194 || GET_CODE (XEXP (x, 0)) == PRE_INC
2195 || GET_CODE (XEXP (x, 0)) == POST_DEC
2196 || GET_CODE (XEXP (x, 0)) == POST_INC)
2197 && XEXP (XEXP (x, 0), 0) == stack_pointer_rtx))
2198 current_function_sp_is_unchanging = 0;
2199}
2200
dc2ede84
BS
2201/* Record which insns refer to any volatile memory
2202 or for any reason can't be deleted just because they are dead stores.
fdb8a883
JW
2203 Also, delete any insns that copy a register to itself.
2204 And see if the stack pointer is modified. */
dc2ede84
BS
2205static void
2206record_volatile_insns (f)
2207 rtx f;
2208{
2209 rtx insn;
2210 for (insn = f; insn; insn = NEXT_INSN (insn))
2211 {
2212 enum rtx_code code1 = GET_CODE (insn);
2213 if (code1 == CALL_INSN)
e881bb1b 2214 SET_INSN_VOLATILE (insn);
dc2ede84
BS
2215 else if (code1 == INSN || code1 == JUMP_INSN)
2216 {
2217 if (GET_CODE (PATTERN (insn)) != USE
2218 && volatile_refs_p (PATTERN (insn)))
e881bb1b 2219 SET_INSN_VOLATILE (insn);
dc2ede84
BS
2220
2221 /* A SET that makes space on the stack cannot be dead.
2222 (Such SETs occur only for allocating variable-size data,
2223 so they will always have a PLUS or MINUS according to the
2224 direction of stack growth.)
2225 Even if this function never uses this stack pointer value,
2226 signal handlers do! */
2227 else if (code1 == INSN && GET_CODE (PATTERN (insn)) == SET
2228 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2229#ifdef STACK_GROWS_DOWNWARD
2230 && GET_CODE (SET_SRC (PATTERN (insn))) == MINUS
2231#else
2232 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2233#endif
2234 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx)
e881bb1b 2235 SET_INSN_VOLATILE (insn);
dc2ede84
BS
2236
2237 /* Delete (in effect) any obvious no-op moves. */
2238 else if (noop_move_p (insn))
2239 {
2240 PUT_CODE (insn, NOTE);
2241 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2242 NOTE_SOURCE_FILE (insn) = 0;
2243 }
2244 }
fdb8a883
JW
2245
2246 /* Check if insn modifies the stack pointer. */
2247 if ( current_function_sp_is_unchanging
2248 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2249 note_stores (PATTERN (insn), notice_stack_pointer_modification);
dc2ede84
BS
2250 }
2251}
2252
2253/* Mark those regs which are needed at the end of the function as live
2254 at the end of the last basic block. */
2255static void
2256mark_regs_live_at_end (set)
2257 regset set;
2258{
2259 int i;
2260
e881bb1b
RH
2261 /* If exiting needs the right stack value, consider the stack pointer
2262 live at the end of the function. */
dc2ede84
BS
2263 if (! EXIT_IGNORE_STACK
2264 || (! FRAME_POINTER_REQUIRED
2265 && ! current_function_calls_alloca
fdb8a883
JW
2266 && flag_omit_frame_pointer)
2267 || current_function_sp_is_unchanging)
e881bb1b
RH
2268 {
2269 SET_REGNO_REG_SET (set, STACK_POINTER_REGNUM);
2270 }
dc2ede84 2271
e881bb1b 2272 /* Mark the frame pointer if needed at the end of the function. If
dc2ede84
BS
2273 we end up eliminating it, it will be removed from the live list
2274 of each basic block by reload. */
2275
e4b8a413
JW
2276 if (! reload_completed || frame_pointer_needed)
2277 {
2278 SET_REGNO_REG_SET (set, FRAME_POINTER_REGNUM);
dc2ede84 2279#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
2280 /* If they are different, also mark the hard frame pointer as live */
2281 SET_REGNO_REG_SET (set, HARD_FRAME_POINTER_REGNUM);
dc2ede84 2282#endif
e4b8a413 2283 }
dc2ede84 2284
e881bb1b 2285 /* Mark all global registers, and all registers used by the epilogue
dc2ede84
BS
2286 as being live at the end of the function since they may be
2287 referenced by our caller. */
2288 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2289 if (global_regs[i]
2290#ifdef EPILOGUE_USES
2291 || EPILOGUE_USES (i)
2292#endif
2293 )
2294 SET_REGNO_REG_SET (set, i);
e881bb1b
RH
2295
2296 /* ??? Mark function return value here rather than as uses. */
dc2ede84
BS
2297}
2298
d7429b6a
RK
2299/* Determine which registers are live at the start of each
2300 basic block of the function whose first insn is F.
2301 NREGS is the number of registers used in F.
2302 We allocate the vector basic_block_live_at_start
2303 and the regsets that it points to, and fill them with the data.
2304 regset_size and regset_bytes are also set here. */
2305
2306static void
11f246f6 2307life_analysis_1 (f, nregs, remove_dead_code)
d7429b6a
RK
2308 rtx f;
2309 int nregs;
11f246f6 2310 int remove_dead_code;
d7429b6a 2311{
d7429b6a
RK
2312 int first_pass;
2313 int changed;
d7429b6a 2314 register int i;
6764d250 2315 char save_regs_ever_live[FIRST_PSEUDO_REGISTER];
e881bb1b 2316 regset *new_live_at_end;
d7429b6a
RK
2317
2318 struct obstack flow_obstack;
2319
2320 gcc_obstack_init (&flow_obstack);
2321
2322 max_regno = nregs;
2323
d7429b6a
RK
2324 /* Allocate and zero out many data structures
2325 that will record the data from lifetime analysis. */
2326
359da67d
RH
2327 allocate_reg_life_data ();
2328 allocate_bb_life_data ();
d7429b6a
RK
2329
2330 reg_next_use = (rtx *) alloca (nregs * sizeof (rtx));
e881bb1b 2331 memset (reg_next_use, 0, nregs * sizeof (rtx));
d7429b6a 2332
e881bb1b 2333 /* Set up regset-vectors used internally within this function.
d7429b6a
RK
2334 Their meanings are documented above, with their declarations. */
2335
e881bb1b
RH
2336 new_live_at_end = (regset *) alloca ((n_basic_blocks + 1) * sizeof (regset));
2337 init_regset_vector (new_live_at_end, n_basic_blocks + 1, &flow_obstack);
4c9a05bc 2338
e881bb1b
RH
2339 /* Stick these vectors into the AUX field of the basic block, so that
2340 we don't have to keep going through the index. */
d7429b6a 2341
e881bb1b
RH
2342 for (i = 0; i < n_basic_blocks; ++i)
2343 BASIC_BLOCK (i)->aux = new_live_at_end[i];
2344 ENTRY_BLOCK_PTR->aux = new_live_at_end[i];
d7429b6a 2345
fdb8a883
JW
2346 /* Assume that the stack pointer is unchanging if alloca hasn't been used.
2347 This will be cleared by record_volatile_insns if it encounters an insn
2348 which modifies the stack pointer. */
2349 current_function_sp_is_unchanging = !current_function_calls_alloca;
2350
dc2ede84 2351 record_volatile_insns (f);
fe0f9c4b
RK
2352
2353 if (n_basic_blocks > 0)
2354 {
e881bb1b
RH
2355 regset theend;
2356 register edge e;
2357
2358 theend = EXIT_BLOCK_PTR->global_live_at_start;
2359 mark_regs_live_at_end (theend);
2360
2361 /* Propogate this exit data to each of EXIT's predecessors. */
2362 for (e = EXIT_BLOCK_PTR->pred; e ; e = e->pred_next)
2363 {
2364 COPY_REG_SET (e->src->global_live_at_end, theend);
2365 COPY_REG_SET ((regset) e->src->aux, theend);
2366 }
dc2ede84 2367 }
d7429b6a 2368
e881bb1b
RH
2369 /* The post-reload life analysis have (on a global basis) the same registers
2370 live as was computed by reload itself.
2371
2372 Otherwise elimination offsets and such may be incorrect.
2373
2374 Reload will make some registers as live even though they do not appear
2375 in the rtl. */
2376 if (reload_completed)
2377 memcpy (save_regs_ever_live, regs_ever_live, sizeof (regs_ever_live));
2378 memset (regs_ever_live, 0, sizeof regs_ever_live);
2379
d7429b6a
RK
2380 /* Propagate life info through the basic blocks
2381 around the graph of basic blocks.
2382
2383 This is a relaxation process: each time a new register
2384 is live at the end of the basic block, we must scan the block
2385 to determine which registers are, as a consequence, live at the beginning
2386 of that block. These registers must then be marked live at the ends
2387 of all the blocks that can transfer control to that block.
2388 The process continues until it reaches a fixed point. */
2389
2390 first_pass = 1;
2391 changed = 1;
2392 while (changed)
2393 {
2394 changed = 0;
2395 for (i = n_basic_blocks - 1; i >= 0; i--)
2396 {
e881bb1b 2397 basic_block bb = BASIC_BLOCK (i);
d7429b6a
RK
2398 int consider = first_pass;
2399 int must_rescan = first_pass;
2400 register int j;
2401
2402 if (!first_pass)
2403 {
2404 /* Set CONSIDER if this block needs thinking about at all
2405 (that is, if the regs live now at the end of it
2406 are not the same as were live at the end of it when
2407 we last thought about it).
2408 Set must_rescan if it needs to be thought about
2409 instruction by instruction (that is, if any additional
2410 reg that is live at the end now but was not live there before
2411 is one of the significant regs of this basic block). */
2412
b5835272 2413 EXECUTE_IF_AND_COMPL_IN_REG_SET
e881bb1b 2414 ((regset) bb->aux, bb->global_live_at_end, 0, j,
b5835272
RK
2415 {
2416 consider = 1;
e881bb1b 2417 if (REGNO_REG_SET_P (bb->local_set, j))
b5835272
RK
2418 {
2419 must_rescan = 1;
2420 goto done;
2421 }
2422 });
916b1701 2423 done:
d7429b6a
RK
2424 if (! consider)
2425 continue;
2426 }
2427
2428 /* The live_at_start of this block may be changing,
2429 so another pass will be required after this one. */
2430 changed = 1;
2431
2432 if (! must_rescan)
2433 {
2434 /* No complete rescan needed;
2435 just record those variables newly known live at end
2436 as live at start as well. */
e881bb1b
RH
2437 IOR_AND_COMPL_REG_SET (bb->global_live_at_start,
2438 (regset) bb->aux,
2439 bb->global_live_at_end);
916b1701 2440
e881bb1b
RH
2441 IOR_AND_COMPL_REG_SET (bb->global_live_at_end,
2442 (regset) bb->aux,
2443 bb->global_live_at_end);
d7429b6a
RK
2444 }
2445 else
2446 {
2447 /* Update the basic_block_live_at_start
2448 by propagation backwards through the block. */
e881bb1b
RH
2449 COPY_REG_SET (bb->global_live_at_end, (regset) bb->aux);
2450 COPY_REG_SET (bb->global_live_at_start,
2451 bb->global_live_at_end);
2452 propagate_block (bb->global_live_at_start,
2453 bb->head, bb->end, 0,
2454 first_pass ? bb->local_set : (regset) 0,
11f246f6 2455 i, remove_dead_code);
d7429b6a
RK
2456 }
2457
e881bb1b 2458 /* Update the new_live_at_end's of the block's predecessors. */
d7429b6a 2459 {
e881bb1b 2460 register edge e;
af14ce9c 2461
e881bb1b
RH
2462 for (e = bb->pred; e ; e = e->pred_next)
2463 IOR_REG_SET ((regset) e->src->aux, bb->global_live_at_start);
d7429b6a 2464 }
e881bb1b 2465
d7429b6a
RK
2466#ifdef USE_C_ALLOCA
2467 alloca (0);
2468#endif
2469 }
2470 first_pass = 0;
2471 }
2472
2473 /* The only pseudos that are live at the beginning of the function are
2474 those that were not set anywhere in the function. local-alloc doesn't
2475 know how to handle these correctly, so mark them as not local to any
2476 one basic block. */
2477
2478 if (n_basic_blocks > 0)
e881bb1b 2479 EXECUTE_IF_SET_IN_REG_SET (BASIC_BLOCK (0)->global_live_at_start,
916b1701
MM
2480 FIRST_PSEUDO_REGISTER, i,
2481 {
2482 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
2483 });
d7429b6a 2484
e881bb1b
RH
2485 /* Now the life information is accurate. Make one more pass over each
2486 basic block to delete dead stores, create autoincrement addressing
2487 and record how many times each register is used, is set, or dies. */
d7429b6a 2488
d7429b6a
RK
2489 for (i = 0; i < n_basic_blocks; i++)
2490 {
e881bb1b
RH
2491 basic_block bb = BASIC_BLOCK (i);
2492
2493 /* We start with global_live_at_end to determine which stores are
2494 dead. This process is destructive, and we wish to preserve the
2495 contents of global_live_at_end for posterity. Fortunately,
2496 new_live_at_end, due to the way we converged on a solution,
2497 contains a duplicate of global_live_at_end that we can kill. */
11f246f6 2498 propagate_block ((regset) bb->aux, bb->head, bb->end, 1, (regset) 0, i, remove_dead_code);
e881bb1b 2499
d7429b6a
RK
2500#ifdef USE_C_ALLOCA
2501 alloca (0);
2502#endif
2503 }
2504
e881bb1b
RH
2505 /* We have a problem with any pseudoreg that lives across the setjmp.
2506 ANSI says that if a user variable does not change in value between
2507 the setjmp and the longjmp, then the longjmp preserves it. This
2508 includes longjmp from a place where the pseudo appears dead.
d7429b6a
RK
2509 (In principle, the value still exists if it is in scope.)
2510 If the pseudo goes in a hard reg, some other value may occupy
2511 that hard reg where this pseudo is dead, thus clobbering the pseudo.
2512 Conclusion: such a pseudo must not go in a hard reg. */
916b1701
MM
2513 EXECUTE_IF_SET_IN_REG_SET (regs_live_at_setjmp,
2514 FIRST_PSEUDO_REGISTER, i,
2515 {
2516 if (regno_reg_rtx[i] != 0)
2517 {
2518 REG_LIVE_LENGTH (i) = -1;
2519 REG_BASIC_BLOCK (i) = -1;
2520 }
2521 });
d7429b6a 2522
6764d250
BS
2523 /* Restore regs_ever_live that was provided by reload. */
2524 if (reload_completed)
e881bb1b 2525 memcpy (regs_ever_live, save_regs_ever_live, sizeof (regs_ever_live));
67f0e213 2526
e881bb1b 2527 free_regset_vector (new_live_at_end, n_basic_blocks);
5f4f0e22 2528 obstack_free (&flow_obstack, NULL_PTR);
e881bb1b
RH
2529
2530 for (i = 0; i < n_basic_blocks; ++i)
2531 BASIC_BLOCK (i)->aux = NULL;
2532 ENTRY_BLOCK_PTR->aux = NULL;
d7429b6a
RK
2533}
2534\f
2535/* Subroutines of life analysis. */
2536
2537/* Allocate the permanent data structures that represent the results
2538 of life analysis. Not static since used also for stupid life analysis. */
2539
2540void
359da67d 2541allocate_bb_life_data ()
d7429b6a
RK
2542{
2543 register int i;
d7429b6a 2544
e881bb1b
RH
2545 for (i = 0; i < n_basic_blocks; i++)
2546 {
2547 basic_block bb = BASIC_BLOCK (i);
2548
2549 bb->local_set = OBSTACK_ALLOC_REG_SET (function_obstack);
2550 bb->global_live_at_start = OBSTACK_ALLOC_REG_SET (function_obstack);
2551 bb->global_live_at_end = OBSTACK_ALLOC_REG_SET (function_obstack);
2552 }
2553
2554 ENTRY_BLOCK_PTR->global_live_at_end
2555 = OBSTACK_ALLOC_REG_SET (function_obstack);
2556 EXIT_BLOCK_PTR->global_live_at_start
2557 = OBSTACK_ALLOC_REG_SET (function_obstack);
d7429b6a 2558
7eb136d6 2559 regs_live_at_setjmp = OBSTACK_ALLOC_REG_SET (function_obstack);
359da67d
RH
2560}
2561
2562void
2563allocate_reg_life_data ()
2564{
2565 int i;
2566
2567 /* Recalculate the register space, in case it has grown. Old style
2568 vector oriented regsets would set regset_{size,bytes} here also. */
2569 allocate_reg_info (max_regno, FALSE, FALSE);
2570
2571 /* Because both reg_scan and flow_analysis want to set up the REG_N_SETS
2572 information, explicitly reset it here. The allocation should have
2573 already happened on the previous reg_scan pass. Make sure in case
2574 some more registers were allocated. */
2575 for (i = 0; i < max_regno; i++)
2576 REG_N_SETS (i) = 0;
d7429b6a
RK
2577}
2578
67f0e213
RK
2579/* Make each element of VECTOR point at a regset. The vector has
2580 NELTS elements, and space is allocated from the ALLOC_OBSTACK
2581 obstack. */
d7429b6a 2582
04821e98 2583static void
67f0e213 2584init_regset_vector (vector, nelts, alloc_obstack)
d7429b6a 2585 regset *vector;
d7429b6a 2586 int nelts;
7eb136d6 2587 struct obstack *alloc_obstack;
d7429b6a
RK
2588{
2589 register int i;
d7429b6a
RK
2590
2591 for (i = 0; i < nelts; i++)
2592 {
7eb136d6
MM
2593 vector[i] = OBSTACK_ALLOC_REG_SET (alloc_obstack);
2594 CLEAR_REG_SET (vector[i]);
d7429b6a
RK
2595 }
2596}
e658434c 2597
67f0e213
RK
2598/* Release any additional space allocated for each element of VECTOR point
2599 other than the regset header itself. The vector has NELTS elements. */
2600
2601void
2602free_regset_vector (vector, nelts)
2603 regset *vector;
2604 int nelts;
2605{
2606 register int i;
2607
2608 for (i = 0; i < nelts; i++)
2609 FREE_REG_SET (vector[i]);
2610}
2611
d7429b6a
RK
2612/* Compute the registers live at the beginning of a basic block
2613 from those live at the end.
2614
2615 When called, OLD contains those live at the end.
2616 On return, it contains those live at the beginning.
2617 FIRST and LAST are the first and last insns of the basic block.
2618
2619 FINAL is nonzero if we are doing the final pass which is not
2620 for computing the life info (since that has already been done)
2621 but for acting on it. On this pass, we delete dead stores,
2622 set up the logical links and dead-variables lists of instructions,
2623 and merge instructions for autoincrement and autodecrement addresses.
2624
2625 SIGNIFICANT is nonzero only the first time for each basic block.
2626 If it is nonzero, it points to a regset in which we store
2627 a 1 for each register that is set within the block.
2628
2629 BNUM is the number of the basic block. */
2630
2631static void
11f246f6 2632propagate_block (old, first, last, final, significant, bnum, remove_dead_code)
d7429b6a
RK
2633 register regset old;
2634 rtx first;
2635 rtx last;
2636 int final;
2637 regset significant;
2638 int bnum;
11f246f6 2639 int remove_dead_code;
d7429b6a
RK
2640{
2641 register rtx insn;
2642 rtx prev;
2643 regset live;
2644 regset dead;
2645
e881bb1b
RH
2646 /* Find the loop depth for this block. Ignore loop level changes in the
2647 middle of the basic block -- for register allocation purposes, the
2648 important uses will be in the blocks wholely contained within the loop
2649 not in the loop pre-header or post-trailer. */
2650 loop_depth = BASIC_BLOCK (bnum)->loop_depth;
d7429b6a 2651
7eb136d6
MM
2652 dead = ALLOCA_REG_SET ();
2653 live = ALLOCA_REG_SET ();
d7429b6a
RK
2654
2655 cc0_live = 0;
db3a887b 2656 mem_set_list = NULL_RTX;
d7429b6a 2657
d7429b6a
RK
2658 if (final)
2659 {
916b1701 2660 register int i;
d7429b6a 2661
d7429b6a 2662 /* Process the regs live at the end of the block.
f8dd7f98 2663 Mark them as not local to any one basic block. */
916b1701
MM
2664 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2665 {
2666 REG_BASIC_BLOCK (i) = REG_BLOCK_GLOBAL;
916b1701 2667 });
d7429b6a
RK
2668 }
2669
2670 /* Scan the block an insn at a time from end to beginning. */
2671
2672 for (insn = last; ; insn = prev)
2673 {
2674 prev = PREV_INSN (insn);
2675
8329b5ec 2676 if (GET_CODE (insn) == NOTE)
d7429b6a 2677 {
8329b5ec
DE
2678 /* If this is a call to `setjmp' et al,
2679 warn if any non-volatile datum is live. */
2680
2681 if (final && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
916b1701 2682 IOR_REG_SET (regs_live_at_setjmp, old);
d7429b6a
RK
2683 }
2684
2685 /* Update the life-status of regs for this insn.
2686 First DEAD gets which regs are set in this insn
2687 then LIVE gets which regs are used in this insn.
2688 Then the regs live before the insn
2689 are those live after, with DEAD regs turned off,
2690 and then LIVE regs turned on. */
2691
8329b5ec 2692 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
d7429b6a
RK
2693 {
2694 register int i;
5f4f0e22 2695 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
11f246f6
JH
2696 int insn_is_dead = 0;
2697 int libcall_is_dead = 0;
2698
2699 if (remove_dead_code)
2700 {
2701 insn_is_dead = (insn_dead_p (PATTERN (insn), old, 0, REG_NOTES (insn))
2702 /* Don't delete something that refers to volatile storage! */
2703 && ! INSN_VOLATILE (insn));
2704 libcall_is_dead = (insn_is_dead && note != 0
2705 && libcall_dead_p (PATTERN (insn), old, note, insn));
2706 }
d7429b6a
RK
2707
2708 /* If an instruction consists of just dead store(s) on final pass,
2709 "delete" it by turning it into a NOTE of type NOTE_INSN_DELETED.
2710 We could really delete it with delete_insn, but that
2711 can cause trouble for first or last insn in a basic block. */
b590bbfd 2712 if (final && insn_is_dead)
d7429b6a
RK
2713 {
2714 PUT_CODE (insn, NOTE);
2715 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2716 NOTE_SOURCE_FILE (insn) = 0;
2717
e5df1ea3
RK
2718 /* CC0 is now known to be dead. Either this insn used it,
2719 in which case it doesn't anymore, or clobbered it,
2720 so the next insn can't use it. */
2721 cc0_live = 0;
2722
d7429b6a
RK
2723 /* If this insn is copying the return value from a library call,
2724 delete the entire library call. */
2725 if (libcall_is_dead)
2726 {
2727 rtx first = XEXP (note, 0);
2728 rtx p = insn;
2729 while (INSN_DELETED_P (first))
2730 first = NEXT_INSN (first);
2731 while (p != first)
2732 {
2733 p = PREV_INSN (p);
2734 PUT_CODE (p, NOTE);
2735 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
2736 NOTE_SOURCE_FILE (p) = 0;
2737 }
2738 }
2739 goto flushed;
2740 }
2741
916b1701
MM
2742 CLEAR_REG_SET (dead);
2743 CLEAR_REG_SET (live);
d7429b6a
RK
2744
2745 /* See if this is an increment or decrement that can be
2746 merged into a following memory address. */
2747#ifdef AUTO_INC_DEC
2748 {
956d6950
JL
2749 register rtx x = single_set (insn);
2750
d7429b6a 2751 /* Does this instruction increment or decrement a register? */
6764d250
BS
2752 if (!reload_completed
2753 && final && x != 0
d7429b6a
RK
2754 && GET_CODE (SET_DEST (x)) == REG
2755 && (GET_CODE (SET_SRC (x)) == PLUS
2756 || GET_CODE (SET_SRC (x)) == MINUS)
2757 && XEXP (SET_SRC (x), 0) == SET_DEST (x)
2758 && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2759 /* Ok, look for a following memory ref we can combine with.
2760 If one is found, change the memory ref to a PRE_INC
2761 or PRE_DEC, cancel this insn, and return 1.
2762 Return 0 if nothing has been done. */
2763 && try_pre_increment_1 (insn))
2764 goto flushed;
2765 }
2766#endif /* AUTO_INC_DEC */
2767
2768 /* If this is not the final pass, and this insn is copying the
2769 value of a library call and it's dead, don't scan the
2770 insns that perform the library call, so that the call's
2771 arguments are not marked live. */
2772 if (libcall_is_dead)
2773 {
2774 /* Mark the dest reg as `significant'. */
5f4f0e22 2775 mark_set_regs (old, dead, PATTERN (insn), NULL_RTX, significant);
d7429b6a
RK
2776
2777 insn = XEXP (note, 0);
2778 prev = PREV_INSN (insn);
2779 }
2780 else if (GET_CODE (PATTERN (insn)) == SET
2781 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx
2782 && GET_CODE (SET_SRC (PATTERN (insn))) == PLUS
2783 && XEXP (SET_SRC (PATTERN (insn)), 0) == stack_pointer_rtx
2784 && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == CONST_INT)
2785 /* We have an insn to pop a constant amount off the stack.
2786 (Such insns use PLUS regardless of the direction of the stack,
2787 and any insn to adjust the stack by a constant is always a pop.)
2788 These insns, if not dead stores, have no effect on life. */
2789 ;
2790 else
2791 {
f8dd7f98
BS
2792 /* Any regs live at the time of a call instruction
2793 must not go in a register clobbered by calls.
2794 Find all regs now live and record this for them. */
2795
2796 if (GET_CODE (insn) == CALL_INSN && final)
2797 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2798 {
2799 REG_N_CALLS_CROSSED (i)++;
2800 });
2801
d7429b6a
RK
2802 /* LIVE gets the regs used in INSN;
2803 DEAD gets those set by it. Dead insns don't make anything
2804 live. */
2805
5f4f0e22
CH
2806 mark_set_regs (old, dead, PATTERN (insn),
2807 final ? insn : NULL_RTX, significant);
d7429b6a
RK
2808
2809 /* If an insn doesn't use CC0, it becomes dead since we
2810 assume that every insn clobbers it. So show it dead here;
2811 mark_used_regs will set it live if it is referenced. */
2812 cc0_live = 0;
2813
2814 if (! insn_is_dead)
2815 mark_used_regs (old, live, PATTERN (insn), final, insn);
2816
2817 /* Sometimes we may have inserted something before INSN (such as
2818 a move) when we make an auto-inc. So ensure we will scan
2819 those insns. */
2820#ifdef AUTO_INC_DEC
2821 prev = PREV_INSN (insn);
2822#endif
2823
2824 if (! insn_is_dead && GET_CODE (insn) == CALL_INSN)
2825 {
2826 register int i;
2827
6b67ec08
RK
2828 rtx note;
2829
2830 for (note = CALL_INSN_FUNCTION_USAGE (insn);
2831 note;
2832 note = XEXP (note, 1))
2833 if (GET_CODE (XEXP (note, 0)) == USE)
2834 mark_used_regs (old, live, SET_DEST (XEXP (note, 0)),
2835 final, insn);
2836
d7429b6a 2837 /* Each call clobbers all call-clobbered regs that are not
e4329280 2838 global or fixed. Note that the function-value reg is a
d7429b6a
RK
2839 call-clobbered reg, and mark_set_regs has already had
2840 a chance to handle it. */
2841
2842 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
e4329280
RK
2843 if (call_used_regs[i] && ! global_regs[i]
2844 && ! fixed_regs[i])
916b1701 2845 SET_REGNO_REG_SET (dead, i);
d7429b6a
RK
2846
2847 /* The stack ptr is used (honorarily) by a CALL insn. */
916b1701 2848 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
d7429b6a
RK
2849
2850 /* Calls may also reference any of the global registers,
2851 so they are made live. */
d7429b6a
RK
2852 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2853 if (global_regs[i])
9b316aa2 2854 mark_used_regs (old, live,
38a448ca 2855 gen_rtx_REG (reg_raw_mode[i], i),
9b316aa2 2856 final, insn);
d7429b6a
RK
2857
2858 /* Calls also clobber memory. */
db3a887b 2859 mem_set_list = NULL_RTX;
d7429b6a
RK
2860 }
2861
2862 /* Update OLD for the registers used or set. */
916b1701
MM
2863 AND_COMPL_REG_SET (old, dead);
2864 IOR_REG_SET (old, live);
d7429b6a 2865
d7429b6a
RK
2866 }
2867
f8dd7f98
BS
2868 /* On final pass, update counts of how many insns each reg is live
2869 at. */
d7429b6a 2870 if (final)
f8dd7f98
BS
2871 EXECUTE_IF_SET_IN_REG_SET (old, 0, i,
2872 { REG_LIVE_LENGTH (i)++; });
d7429b6a
RK
2873 }
2874 flushed: ;
2875 if (insn == first)
2876 break;
2877 }
2878
67f0e213
RK
2879 FREE_REG_SET (dead);
2880 FREE_REG_SET (live);
d7429b6a
RK
2881}
2882\f
2883/* Return 1 if X (the body of an insn, or part of it) is just dead stores
2884 (SET expressions whose destinations are registers dead after the insn).
2885 NEEDED is the regset that says which regs are alive after the insn.
2886
e398aa80
R
2887 Unless CALL_OK is non-zero, an insn is needed if it contains a CALL.
2888
2889 If X is the entire body of an insn, NOTES contains the reg notes
2890 pertaining to the insn. */
d7429b6a
RK
2891
2892static int
e398aa80 2893insn_dead_p (x, needed, call_ok, notes)
d7429b6a
RK
2894 rtx x;
2895 regset needed;
2896 int call_ok;
e398aa80 2897 rtx notes ATTRIBUTE_UNUSED;
d7429b6a 2898{
e5e809f4
JL
2899 enum rtx_code code = GET_CODE (x);
2900
e398aa80
R
2901#ifdef AUTO_INC_DEC
2902 /* If flow is invoked after reload, we must take existing AUTO_INC
2903 expresions into account. */
2904 if (reload_completed)
2905 {
2906 for ( ; notes; notes = XEXP (notes, 1))
2907 {
2908 if (REG_NOTE_KIND (notes) == REG_INC)
2909 {
2910 int regno = REGNO (XEXP (notes, 0));
2911
2912 /* Don't delete insns to set global regs. */
2913 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2914 || REGNO_REG_SET_P (needed, regno))
2915 return 0;
2916 }
2917 }
2918 }
2919#endif
2920
d7429b6a
RK
2921 /* If setting something that's a reg or part of one,
2922 see if that register's altered value will be live. */
2923
2924 if (code == SET)
2925 {
e5e809f4
JL
2926 rtx r = SET_DEST (x);
2927
d7429b6a
RK
2928 /* A SET that is a subroutine call cannot be dead. */
2929 if (! call_ok && GET_CODE (SET_SRC (x)) == CALL)
2930 return 0;
2931
2932#ifdef HAVE_cc0
2933 if (GET_CODE (r) == CC0)
2934 return ! cc0_live;
2935#endif
2936
db3a887b
CB
2937 if (GET_CODE (r) == MEM && ! MEM_VOLATILE_P (r))
2938 {
2939 rtx temp;
2940 /* Walk the set of memory locations we are currently tracking
2941 and see if one is an identical match to this memory location.
2942 If so, this memory write is dead (remember, we're walking
2943 backwards from the end of the block to the start. */
2944 temp = mem_set_list;
2945 while (temp)
2946 {
2947 if (rtx_equal_p (XEXP (temp, 0), r))
2948 return 1;
2949 temp = XEXP (temp, 1);
2950 }
2951 }
d7429b6a 2952
e5e809f4
JL
2953 while (GET_CODE (r) == SUBREG || GET_CODE (r) == STRICT_LOW_PART
2954 || GET_CODE (r) == ZERO_EXTRACT)
d7429b6a
RK
2955 r = SUBREG_REG (r);
2956
2957 if (GET_CODE (r) == REG)
2958 {
e5e809f4 2959 int regno = REGNO (r);
d7429b6a 2960
d8c8b8e3 2961 /* Don't delete insns to set global regs. */
d7429b6a
RK
2962 if ((regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2963 /* Make sure insns to set frame pointer aren't deleted. */
e4b8a413
JW
2964 || (regno == FRAME_POINTER_REGNUM
2965 && (! reload_completed || frame_pointer_needed))
73a187c1 2966#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
2967 || (regno == HARD_FRAME_POINTER_REGNUM
2968 && (! reload_completed || frame_pointer_needed))
73a187c1 2969#endif
d7e4fe8b
RS
2970#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2971 /* Make sure insns to set arg pointer are never deleted
2972 (if the arg pointer isn't fixed, there will be a USE for
0f41302f 2973 it, so we can treat it normally). */
d7e4fe8b
RS
2974 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2975#endif
916b1701 2976 || REGNO_REG_SET_P (needed, regno))
d7429b6a
RK
2977 return 0;
2978
2979 /* If this is a hard register, verify that subsequent words are
2980 not needed. */
2981 if (regno < FIRST_PSEUDO_REGISTER)
2982 {
2983 int n = HARD_REGNO_NREGS (regno, GET_MODE (r));
2984
2985 while (--n > 0)
916b1701 2986 if (REGNO_REG_SET_P (needed, regno+n))
d7429b6a
RK
2987 return 0;
2988 }
2989
2990 return 1;
2991 }
2992 }
e5e809f4 2993
d7429b6a
RK
2994 /* If performing several activities,
2995 insn is dead if each activity is individually dead.
2996 Also, CLOBBERs and USEs can be ignored; a CLOBBER or USE
2997 that's inside a PARALLEL doesn't make the insn worth keeping. */
2998 else if (code == PARALLEL)
2999 {
e5e809f4
JL
3000 int i = XVECLEN (x, 0);
3001
d7429b6a 3002 for (i--; i >= 0; i--)
e5e809f4
JL
3003 if (GET_CODE (XVECEXP (x, 0, i)) != CLOBBER
3004 && GET_CODE (XVECEXP (x, 0, i)) != USE
e398aa80 3005 && ! insn_dead_p (XVECEXP (x, 0, i), needed, call_ok, NULL_RTX))
e5e809f4
JL
3006 return 0;
3007
d7429b6a
RK
3008 return 1;
3009 }
e5e809f4
JL
3010
3011 /* A CLOBBER of a pseudo-register that is dead serves no purpose. That
3012 is not necessarily true for hard registers. */
3013 else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == REG
3014 && REGNO (XEXP (x, 0)) >= FIRST_PSEUDO_REGISTER
3015 && ! REGNO_REG_SET_P (needed, REGNO (XEXP (x, 0))))
3016 return 1;
3017
3018 /* We do not check other CLOBBER or USE here. An insn consisting of just
3019 a CLOBBER or just a USE should not be deleted. */
d7429b6a
RK
3020 return 0;
3021}
3022
3023/* If X is the pattern of the last insn in a libcall, and assuming X is dead,
3024 return 1 if the entire library call is dead.
3025 This is true if X copies a register (hard or pseudo)
3026 and if the hard return reg of the call insn is dead.
3027 (The caller should have tested the destination of X already for death.)
3028
3029 If this insn doesn't just copy a register, then we don't
3030 have an ordinary libcall. In that case, cse could not have
3031 managed to substitute the source for the dest later on,
3032 so we can assume the libcall is dead.
3033
3034 NEEDED is the bit vector of pseudoregs live before this insn.
3035 NOTE is the REG_RETVAL note of the insn. INSN is the insn itself. */
3036
3037static int
3038libcall_dead_p (x, needed, note, insn)
3039 rtx x;
3040 regset needed;
3041 rtx note;
3042 rtx insn;
3043{
3044 register RTX_CODE code = GET_CODE (x);
3045
3046 if (code == SET)
3047 {
3048 register rtx r = SET_SRC (x);
3049 if (GET_CODE (r) == REG)
3050 {
3051 rtx call = XEXP (note, 0);
e398aa80 3052 rtx call_pat;
d7429b6a
RK
3053 register int i;
3054
3055 /* Find the call insn. */
3056 while (call != insn && GET_CODE (call) != CALL_INSN)
3057 call = NEXT_INSN (call);
3058
3059 /* If there is none, do nothing special,
3060 since ordinary death handling can understand these insns. */
3061 if (call == insn)
3062 return 0;
3063
3064 /* See if the hard reg holding the value is dead.
3065 If this is a PARALLEL, find the call within it. */
e398aa80
R
3066 call_pat = PATTERN (call);
3067 if (GET_CODE (call_pat) == PARALLEL)
d7429b6a 3068 {
e398aa80
R
3069 for (i = XVECLEN (call_pat, 0) - 1; i >= 0; i--)
3070 if (GET_CODE (XVECEXP (call_pat, 0, i)) == SET
3071 && GET_CODE (SET_SRC (XVECEXP (call_pat, 0, i))) == CALL)
d7429b6a
RK
3072 break;
3073
761a5bcd
JW
3074 /* This may be a library call that is returning a value
3075 via invisible pointer. Do nothing special, since
3076 ordinary death handling can understand these insns. */
d7429b6a 3077 if (i < 0)
761a5bcd 3078 return 0;
d7429b6a 3079
e398aa80 3080 call_pat = XVECEXP (call_pat, 0, i);
d7429b6a
RK
3081 }
3082
e398aa80 3083 return insn_dead_p (call_pat, needed, 1, REG_NOTES (call));
d7429b6a
RK
3084 }
3085 }
3086 return 1;
3087}
3088
bd80fbde
RH
3089/* Return 1 if register REGNO was used before it was set, i.e. if it is
3090 live at function entry. Don't count global register variables, variables
3091 in registers that can be used for function arg passing, or variables in
3092 fixed hard registers. */
d7429b6a
RK
3093
3094int
3095regno_uninitialized (regno)
3096 int regno;
3097{
b0b7b46a 3098 if (n_basic_blocks == 0
6a45254e 3099 || (regno < FIRST_PSEUDO_REGISTER
bd80fbde
RH
3100 && (global_regs[regno]
3101 || fixed_regs[regno]
3102 || FUNCTION_ARG_REGNO_P (regno))))
d7429b6a
RK
3103 return 0;
3104
e881bb1b 3105 return REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno);
d7429b6a
RK
3106}
3107
3108/* 1 if register REGNO was alive at a place where `setjmp' was called
3109 and was set more than once or is an argument.
3110 Such regs may be clobbered by `longjmp'. */
3111
3112int
3113regno_clobbered_at_setjmp (regno)
3114 int regno;
3115{
3116 if (n_basic_blocks == 0)
3117 return 0;
3118
b1f21e0a 3119 return ((REG_N_SETS (regno) > 1
e881bb1b 3120 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
916b1701 3121 && REGNO_REG_SET_P (regs_live_at_setjmp, regno));
d7429b6a
RK
3122}
3123\f
15e088b2
JL
3124/* INSN references memory, possibly using autoincrement addressing modes.
3125 Find any entries on the mem_set_list that need to be invalidated due
3126 to an address change. */
3127static void
3128invalidate_mems_from_autoinc (insn)
3129 rtx insn;
3130{
3131 rtx note = REG_NOTES (insn);
3132 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3133 {
3134 if (REG_NOTE_KIND (note) == REG_INC)
3135 {
3136 rtx temp = mem_set_list;
3137 rtx prev = NULL_RTX;
3138
3139 while (temp)
3140 {
3141 if (reg_overlap_mentioned_p (XEXP (note, 0), XEXP (temp, 0)))
3142 {
3143 /* Splice temp out of list. */
3144 if (prev)
3145 XEXP (prev, 1) = XEXP (temp, 1);
3146 else
3147 mem_set_list = XEXP (temp, 1);
3148 }
3149 else
3150 prev = temp;
3151 temp = XEXP (temp, 1);
3152 }
3153 }
3154 }
3155}
3156
d7429b6a
RK
3157/* Process the registers that are set within X.
3158 Their bits are set to 1 in the regset DEAD,
3159 because they are dead prior to this insn.
3160
3161 If INSN is nonzero, it is the insn being processed
3162 and the fact that it is nonzero implies this is the FINAL pass
3163 in propagate_block. In this case, various info about register
3164 usage is stored, LOG_LINKS fields of insns are set up. */
3165
d7429b6a
RK
3166static void
3167mark_set_regs (needed, dead, x, insn, significant)
3168 regset needed;
3169 regset dead;
3170 rtx x;
3171 rtx insn;
3172 regset significant;
3173{
3174 register RTX_CODE code = GET_CODE (x);
3175
3176 if (code == SET || code == CLOBBER)
3177 mark_set_1 (needed, dead, x, insn, significant);
3178 else if (code == PARALLEL)
3179 {
3180 register int i;
3181 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3182 {
3183 code = GET_CODE (XVECEXP (x, 0, i));
3184 if (code == SET || code == CLOBBER)
3185 mark_set_1 (needed, dead, XVECEXP (x, 0, i), insn, significant);
3186 }
3187 }
3188}
3189
3190/* Process a single SET rtx, X. */
3191
3192static void
3193mark_set_1 (needed, dead, x, insn, significant)
3194 regset needed;
3195 regset dead;
3196 rtx x;
3197 rtx insn;
3198 regset significant;
3199{
3200 register int regno;
3201 register rtx reg = SET_DEST (x);
3202
86465af7
DM
3203 /* Some targets place small structures in registers for
3204 return values of functions. We have to detect this
3205 case specially here to get correct flow information. */
3206 if (GET_CODE (reg) == PARALLEL
3207 && GET_MODE (reg) == BLKmode)
3208 {
3209 register int i;
3210
3211 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
3212 mark_set_1 (needed, dead, XVECEXP (reg, 0, i), insn, significant);
3213 return;
3214 }
3215
d7429b6a
RK
3216 /* Modifying just one hardware register of a multi-reg value
3217 or just a byte field of a register
3218 does not mean the value from before this insn is now dead.
3219 But it does mean liveness of that register at the end of the block
3220 is significant.
3221
3222 Within mark_set_1, however, we treat it as if the register is
3223 indeed modified. mark_used_regs will, however, also treat this
3224 register as being used. Thus, we treat these insns as setting a
3225 new value for the register as a function of its old value. This
3226 cases LOG_LINKS to be made appropriately and this will help combine. */
3227
3228 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
3229 || GET_CODE (reg) == SIGN_EXTRACT
3230 || GET_CODE (reg) == STRICT_LOW_PART)
3231 reg = XEXP (reg, 0);
3232
db3a887b
CB
3233 /* If this set is a MEM, then it kills any aliased writes.
3234 If this set is a REG, then it kills any MEMs which use the reg. */
d7429b6a 3235 if (GET_CODE (reg) == MEM
db3a887b
CB
3236 || GET_CODE (reg) == REG)
3237 {
3238 rtx temp = mem_set_list;
3239 rtx prev = NULL_RTX;
3240
3241 while (temp)
3242 {
3243 if ((GET_CODE (reg) == MEM
3244 && output_dependence (XEXP (temp, 0), reg))
3245 || (GET_CODE (reg) == REG
3246 && reg_overlap_mentioned_p (reg, XEXP (temp, 0))))
3247 {
3248 /* Splice this entry out of the list. */
3249 if (prev)
3250 XEXP (prev, 1) = XEXP (temp, 1);
3251 else
3252 mem_set_list = XEXP (temp, 1);
3253 }
3254 else
3255 prev = temp;
3256 temp = XEXP (temp, 1);
3257 }
3258 }
15e088b2
JL
3259
3260 /* If the memory reference had embedded side effects (autoincrement
3261 address modes. Then we may need to kill some entries on the
3262 memory set list. */
3263 if (insn && GET_CODE (reg) == MEM)
3264 invalidate_mems_from_autoinc (insn);
3265
d7429b6a 3266 if (GET_CODE (reg) == MEM && ! side_effects_p (reg)
3ce7c5a2
JL
3267 /* We do not know the size of a BLKmode store, so we do not track
3268 them for redundant store elimination. */
3269 && GET_MODE (reg) != BLKmode
d7429b6a
RK
3270 /* There are no REG_INC notes for SP, so we can't assume we'll see
3271 everything that invalidates it. To be safe, don't eliminate any
3272 stores though SP; none of them should be redundant anyway. */
3273 && ! reg_mentioned_p (stack_pointer_rtx, reg))
db3a887b 3274 mem_set_list = gen_rtx_EXPR_LIST (VOIDmode, reg, mem_set_list);
d7429b6a
RK
3275
3276 if (GET_CODE (reg) == REG
e4b8a413
JW
3277 && (regno = REGNO (reg), ! (regno == FRAME_POINTER_REGNUM
3278 && (! reload_completed || frame_pointer_needed)))
73a187c1 3279#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
3280 && ! (regno == HARD_FRAME_POINTER_REGNUM
3281 && (! reload_completed || frame_pointer_needed))
73a187c1 3282#endif
d7e4fe8b
RS
3283#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3284 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3285#endif
d7429b6a
RK
3286 && ! (regno < FIRST_PSEUDO_REGISTER && global_regs[regno]))
3287 /* && regno != STACK_POINTER_REGNUM) -- let's try without this. */
3288 {
916b1701
MM
3289 int some_needed = REGNO_REG_SET_P (needed, regno);
3290 int some_not_needed = ! some_needed;
d7429b6a
RK
3291
3292 /* Mark it as a significant register for this basic block. */
3293 if (significant)
916b1701 3294 SET_REGNO_REG_SET (significant, regno);
d7429b6a 3295
38e01259 3296 /* Mark it as dead before this insn. */
916b1701 3297 SET_REGNO_REG_SET (dead, regno);
d7429b6a
RK
3298
3299 /* A hard reg in a wide mode may really be multiple registers.
3300 If so, mark all of them just like the first. */
3301 if (regno < FIRST_PSEUDO_REGISTER)
3302 {
3303 int n;
3304
3305 /* Nothing below is needed for the stack pointer; get out asap.
3306 Eg, log links aren't needed, since combine won't use them. */
3307 if (regno == STACK_POINTER_REGNUM)
3308 return;
3309
3310 n = HARD_REGNO_NREGS (regno, GET_MODE (reg));
3311 while (--n > 0)
3312 {
916b1701
MM
3313 int regno_n = regno + n;
3314 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
d7429b6a 3315 if (significant)
916b1701 3316 SET_REGNO_REG_SET (significant, regno_n);
cb9e8ad1 3317
916b1701
MM
3318 SET_REGNO_REG_SET (dead, regno_n);
3319 some_needed |= needed_regno;
3320 some_not_needed |= ! needed_regno;
d7429b6a
RK
3321 }
3322 }
3323 /* Additional data to record if this is the final pass. */
3324 if (insn)
3325 {
3326 register rtx y = reg_next_use[regno];
3327 register int blocknum = BLOCK_NUM (insn);
3328
3329 /* If this is a hard reg, record this function uses the reg. */
3330
3331 if (regno < FIRST_PSEUDO_REGISTER)
3332 {
3333 register int i;
3334 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
3335
3336 for (i = regno; i < endregno; i++)
3337 {
93514916
JW
3338 /* The next use is no longer "next", since a store
3339 intervenes. */
3340 reg_next_use[i] = 0;
3341
d7429b6a 3342 regs_ever_live[i] = 1;
b1f21e0a 3343 REG_N_SETS (i)++;
d7429b6a
RK
3344 }
3345 }
3346 else
3347 {
93514916
JW
3348 /* The next use is no longer "next", since a store
3349 intervenes. */
3350 reg_next_use[regno] = 0;
3351
d7429b6a
RK
3352 /* Keep track of which basic blocks each reg appears in. */
3353
b1f21e0a
MM
3354 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3355 REG_BASIC_BLOCK (regno) = blocknum;
3356 else if (REG_BASIC_BLOCK (regno) != blocknum)
3357 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
d7429b6a
RK
3358
3359 /* Count (weighted) references, stores, etc. This counts a
3360 register twice if it is modified, but that is correct. */
b1f21e0a 3361 REG_N_SETS (regno)++;
d7429b6a 3362
b1f21e0a 3363 REG_N_REFS (regno) += loop_depth;
d7429b6a
RK
3364
3365 /* The insns where a reg is live are normally counted
3366 elsewhere, but we want the count to include the insn
3367 where the reg is set, and the normal counting mechanism
3368 would not count it. */
b1f21e0a 3369 REG_LIVE_LENGTH (regno)++;
d7429b6a
RK
3370 }
3371
cb9e8ad1 3372 if (! some_not_needed)
d7429b6a
RK
3373 {
3374 /* Make a logical link from the next following insn
3375 that uses this register, back to this insn.
3376 The following insns have already been processed.
3377
3378 We don't build a LOG_LINK for hard registers containing
3379 in ASM_OPERANDs. If these registers get replaced,
3380 we might wind up changing the semantics of the insn,
3381 even if reload can make what appear to be valid assignments
3382 later. */
3383 if (y && (BLOCK_NUM (y) == blocknum)
3384 && (regno >= FIRST_PSEUDO_REGISTER
3385 || asm_noperands (PATTERN (y)) < 0))
3386 LOG_LINKS (y)
38a448ca 3387 = gen_rtx_INSN_LIST (VOIDmode, insn, LOG_LINKS (y));
d7429b6a
RK
3388 }
3389 else if (! some_needed)
3390 {
3391 /* Note that dead stores have already been deleted when possible
3392 If we get here, we have found a dead store that cannot
3393 be eliminated (because the same insn does something useful).
3394 Indicate this by marking the reg being set as dying here. */
3395 REG_NOTES (insn)
38a448ca 3396 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
b1f21e0a 3397 REG_N_DEATHS (REGNO (reg))++;
d7429b6a
RK
3398 }
3399 else
3400 {
3401 /* This is a case where we have a multi-word hard register
3402 and some, but not all, of the words of the register are
3403 needed in subsequent insns. Write REG_UNUSED notes
3404 for those parts that were not needed. This case should
3405 be rare. */
3406
3407 int i;
3408
3409 for (i = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1;
3410 i >= 0; i--)
916b1701 3411 if (!REGNO_REG_SET_P (needed, regno + i))
d7429b6a 3412 REG_NOTES (insn)
38a448ca
RH
3413 = gen_rtx_EXPR_LIST (REG_UNUSED,
3414 gen_rtx_REG (reg_raw_mode[regno + i],
3415 regno + i),
3416 REG_NOTES (insn));
d7429b6a
RK
3417 }
3418 }
3419 }
8244fc4f
RS
3420 else if (GET_CODE (reg) == REG)
3421 reg_next_use[regno] = 0;
d7429b6a
RK
3422
3423 /* If this is the last pass and this is a SCRATCH, show it will be dying
3424 here and count it. */
3425 else if (GET_CODE (reg) == SCRATCH && insn != 0)
3426 {
3427 REG_NOTES (insn)
38a448ca 3428 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (insn));
d7429b6a
RK
3429 }
3430}
3431\f
3432#ifdef AUTO_INC_DEC
3433
3434/* X is a MEM found in INSN. See if we can convert it into an auto-increment
3435 reference. */
3436
3437static void
3438find_auto_inc (needed, x, insn)
3439 regset needed;
3440 rtx x;
3441 rtx insn;
3442{
3443 rtx addr = XEXP (x, 0);
e658434c 3444 HOST_WIDE_INT offset = 0;
05ed5d57 3445 rtx set;
d7429b6a
RK
3446
3447 /* Here we detect use of an index register which might be good for
3448 postincrement, postdecrement, preincrement, or predecrement. */
3449
3450 if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
3451 offset = INTVAL (XEXP (addr, 1)), addr = XEXP (addr, 0);
3452
3453 if (GET_CODE (addr) == REG)
3454 {
3455 register rtx y;
3456 register int size = GET_MODE_SIZE (GET_MODE (x));
3457 rtx use;
3458 rtx incr;
3459 int regno = REGNO (addr);
3460
3461 /* Is the next use an increment that might make auto-increment? */
05ed5d57
RK
3462 if ((incr = reg_next_use[regno]) != 0
3463 && (set = single_set (incr)) != 0
3464 && GET_CODE (set) == SET
d7429b6a
RK
3465 && BLOCK_NUM (incr) == BLOCK_NUM (insn)
3466 /* Can't add side effects to jumps; if reg is spilled and
3467 reloaded, there's no way to store back the altered value. */
3468 && GET_CODE (insn) != JUMP_INSN
05ed5d57 3469 && (y = SET_SRC (set), GET_CODE (y) == PLUS)
d7429b6a
RK
3470 && XEXP (y, 0) == addr
3471 && GET_CODE (XEXP (y, 1)) == CONST_INT
940da324
JL
3472 && ((HAVE_POST_INCREMENT
3473 && (INTVAL (XEXP (y, 1)) == size && offset == 0))
3474 || (HAVE_POST_DECREMENT
3475 && (INTVAL (XEXP (y, 1)) == - size && offset == 0))
3476 || (HAVE_PRE_INCREMENT
3477 && (INTVAL (XEXP (y, 1)) == size && offset == size))
3478 || (HAVE_PRE_DECREMENT
3479 && (INTVAL (XEXP (y, 1)) == - size && offset == - size)))
d7429b6a
RK
3480 /* Make sure this reg appears only once in this insn. */
3481 && (use = find_use_as_address (PATTERN (insn), addr, offset),
3482 use != 0 && use != (rtx) 1))
3483 {
05ed5d57 3484 rtx q = SET_DEST (set);
7280c2a4
RK
3485 enum rtx_code inc_code = (INTVAL (XEXP (y, 1)) == size
3486 ? (offset ? PRE_INC : POST_INC)
3487 : (offset ? PRE_DEC : POST_DEC));
d7429b6a
RK
3488
3489 if (dead_or_set_p (incr, addr))
7280c2a4
RK
3490 {
3491 /* This is the simple case. Try to make the auto-inc. If
3492 we can't, we are done. Otherwise, we will do any
3493 needed updates below. */
3494 if (! validate_change (insn, &XEXP (x, 0),
38a448ca 3495 gen_rtx_fmt_e (inc_code, Pmode, addr),
7280c2a4
RK
3496 0))
3497 return;
3498 }
5175ad37
DE
3499 else if (GET_CODE (q) == REG
3500 /* PREV_INSN used here to check the semi-open interval
3501 [insn,incr). */
b24884cd
JL
3502 && ! reg_used_between_p (q, PREV_INSN (insn), incr)
3503 /* We must also check for sets of q as q may be
3504 a call clobbered hard register and there may
3505 be a call between PREV_INSN (insn) and incr. */
3506 && ! reg_set_between_p (q, PREV_INSN (insn), incr))
d7429b6a 3507 {
5175ad37 3508 /* We have *p followed sometime later by q = p+size.
d7429b6a 3509 Both p and q must be live afterward,
9ec36da5 3510 and q is not used between INSN and its assignment.
d7429b6a
RK
3511 Change it to q = p, ...*q..., q = q+size.
3512 Then fall into the usual case. */
3513 rtx insns, temp;
e881bb1b 3514 basic_block bb;
d7429b6a
RK
3515
3516 start_sequence ();
3517 emit_move_insn (q, addr);
3518 insns = get_insns ();
3519 end_sequence ();
3520
e881bb1b 3521 bb = BLOCK_FOR_INSN (insn);
d7429b6a 3522 for (temp = insns; temp; temp = NEXT_INSN (temp))
e881bb1b 3523 set_block_for_insn (temp, bb);
d7429b6a 3524
7280c2a4
RK
3525 /* If we can't make the auto-inc, or can't make the
3526 replacement into Y, exit. There's no point in making
3527 the change below if we can't do the auto-inc and doing
3528 so is not correct in the pre-inc case. */
3529
3530 validate_change (insn, &XEXP (x, 0),
38a448ca 3531 gen_rtx_fmt_e (inc_code, Pmode, q),
7280c2a4
RK
3532 1);
3533 validate_change (incr, &XEXP (y, 0), q, 1);
3534 if (! apply_change_group ())
3535 return;
3536
3537 /* We now know we'll be doing this change, so emit the
3538 new insn(s) and do the updates. */
d7429b6a 3539 emit_insns_before (insns, insn);
e8b641a1 3540
e881bb1b
RH
3541 if (BLOCK_FOR_INSN (insn)->head == insn)
3542 BLOCK_FOR_INSN (insn)->head = insns;
e8b641a1 3543
d7429b6a
RK
3544 /* INCR will become a NOTE and INSN won't contain a
3545 use of ADDR. If a use of ADDR was just placed in
3546 the insn before INSN, make that the next use.
3547 Otherwise, invalidate it. */
3548 if (GET_CODE (PREV_INSN (insn)) == INSN
3549 && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
3550 && SET_SRC (PATTERN (PREV_INSN (insn))) == addr)
3551 reg_next_use[regno] = PREV_INSN (insn);
3552 else
3553 reg_next_use[regno] = 0;
3554
3555 addr = q;
3556 regno = REGNO (q);
d7429b6a
RK
3557
3558 /* REGNO is now used in INCR which is below INSN, but
3559 it previously wasn't live here. If we don't mark
3560 it as needed, we'll put a REG_DEAD note for it
3561 on this insn, which is incorrect. */
916b1701 3562 SET_REGNO_REG_SET (needed, regno);
d7429b6a
RK
3563
3564 /* If there are any calls between INSN and INCR, show
3565 that REGNO now crosses them. */
3566 for (temp = insn; temp != incr; temp = NEXT_INSN (temp))
3567 if (GET_CODE (temp) == CALL_INSN)
b1f21e0a 3568 REG_N_CALLS_CROSSED (regno)++;
d7429b6a 3569 }
02df8aba
RK
3570 else
3571 return;
d7429b6a 3572
7280c2a4
RK
3573 /* If we haven't returned, it means we were able to make the
3574 auto-inc, so update the status. First, record that this insn
3575 has an implicit side effect. */
3576
3577 REG_NOTES (insn)
38a448ca 3578 = gen_rtx_EXPR_LIST (REG_INC, addr, REG_NOTES (insn));
7280c2a4
RK
3579
3580 /* Modify the old increment-insn to simply copy
3581 the already-incremented value of our register. */
3582 if (! validate_change (incr, &SET_SRC (set), addr, 0))
3583 abort ();
3584
3585 /* If that makes it a no-op (copying the register into itself) delete
3586 it so it won't appear to be a "use" and a "set" of this
3587 register. */
3588 if (SET_DEST (set) == addr)
d7429b6a 3589 {
7280c2a4
RK
3590 PUT_CODE (incr, NOTE);
3591 NOTE_LINE_NUMBER (incr) = NOTE_INSN_DELETED;
3592 NOTE_SOURCE_FILE (incr) = 0;
3593 }
d7429b6a 3594
7280c2a4
RK
3595 if (regno >= FIRST_PSEUDO_REGISTER)
3596 {
3597 /* Count an extra reference to the reg. When a reg is
3598 incremented, spilling it is worse, so we want to make
3599 that less likely. */
b1f21e0a 3600 REG_N_REFS (regno) += loop_depth;
7280c2a4
RK
3601
3602 /* Count the increment as a setting of the register,
3603 even though it isn't a SET in rtl. */
b1f21e0a 3604 REG_N_SETS (regno)++;
d7429b6a
RK
3605 }
3606 }
3607 }
3608}
3609#endif /* AUTO_INC_DEC */
3610\f
3611/* Scan expression X and store a 1-bit in LIVE for each reg it uses.
3612 This is done assuming the registers needed from X
3613 are those that have 1-bits in NEEDED.
3614
3615 On the final pass, FINAL is 1. This means try for autoincrement
3616 and count the uses and deaths of each pseudo-reg.
3617
3618 INSN is the containing instruction. If INSN is dead, this function is not
3619 called. */
3620
3621static void
3622mark_used_regs (needed, live, x, final, insn)
3623 regset needed;
3624 regset live;
3625 rtx x;
d7429b6a 3626 int final;
e658434c 3627 rtx insn;
d7429b6a
RK
3628{
3629 register RTX_CODE code;
3630 register int regno;
3631 int i;
3632
3633 retry:
3634 code = GET_CODE (x);
3635 switch (code)
3636 {
3637 case LABEL_REF:
3638 case SYMBOL_REF:
3639 case CONST_INT:
3640 case CONST:
3641 case CONST_DOUBLE:
3642 case PC:
d7429b6a
RK
3643 case ADDR_VEC:
3644 case ADDR_DIFF_VEC:
d7429b6a
RK
3645 return;
3646
3647#ifdef HAVE_cc0
3648 case CC0:
3649 cc0_live = 1;
3650 return;
3651#endif
3652
2f1553a4
RK
3653 case CLOBBER:
3654 /* If we are clobbering a MEM, mark any registers inside the address
3655 as being used. */
3656 if (GET_CODE (XEXP (x, 0)) == MEM)
3657 mark_used_regs (needed, live, XEXP (XEXP (x, 0), 0), final, insn);
3658 return;
3659
d7429b6a 3660 case MEM:
7eb136d6
MM
3661 /* Invalidate the data for the last MEM stored, but only if MEM is
3662 something that can be stored into. */
3663 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3664 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
db3a887b 3665 ; /* needn't clear the memory set list */
7eb136d6 3666 else
db3a887b
CB
3667 {
3668 rtx temp = mem_set_list;
3669 rtx prev = NULL_RTX;
3670
3671 while (temp)
3672 {
063cd522 3673 if (anti_dependence (XEXP (temp, 0), x))
db3a887b
CB
3674 {
3675 /* Splice temp out of the list. */
3676 if (prev)
3677 XEXP (prev, 1) = XEXP (temp, 1);
3678 else
3679 mem_set_list = XEXP (temp, 1);
3680 }
3681 else
3682 prev = temp;
3683 temp = XEXP (temp, 1);
3684 }
3685 }
d7429b6a 3686
15e088b2
JL
3687 /* If the memory reference had embedded side effects (autoincrement
3688 address modes. Then we may need to kill some entries on the
3689 memory set list. */
3690 if (insn)
3691 invalidate_mems_from_autoinc (insn);
3692
d7429b6a
RK
3693#ifdef AUTO_INC_DEC
3694 if (final)
3695 find_auto_inc (needed, x, insn);
3696#endif
3697 break;
3698
80f8f04a
RK
3699 case SUBREG:
3700 if (GET_CODE (SUBREG_REG (x)) == REG
3701 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER
3702 && (GET_MODE_SIZE (GET_MODE (x))
88285acf 3703 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
b1f21e0a 3704 REG_CHANGES_SIZE (REGNO (SUBREG_REG (x))) = 1;
80f8f04a
RK
3705
3706 /* While we're here, optimize this case. */
3707 x = SUBREG_REG (x);
3708
e100a3bb 3709 /* In case the SUBREG is not of a register, don't optimize */
ce79abf3 3710 if (GET_CODE (x) != REG)
e100a3bb
MM
3711 {
3712 mark_used_regs (needed, live, x, final, insn);
3713 return;
3714 }
ce79abf3 3715
0f41302f 3716 /* ... fall through ... */
80f8f04a 3717
d7429b6a
RK
3718 case REG:
3719 /* See a register other than being set
3720 => mark it as needed. */
3721
3722 regno = REGNO (x);
3723 {
67f0e213
RK
3724 int some_needed = REGNO_REG_SET_P (needed, regno);
3725 int some_not_needed = ! some_needed;
d7429b6a 3726
916b1701 3727 SET_REGNO_REG_SET (live, regno);
cb9e8ad1 3728
d7429b6a
RK
3729 /* A hard reg in a wide mode may really be multiple registers.
3730 If so, mark all of them just like the first. */
3731 if (regno < FIRST_PSEUDO_REGISTER)
3732 {
3733 int n;
3734
d7e4fe8b 3735 /* For stack ptr or fixed arg pointer,
d7429b6a
RK
3736 nothing below can be necessary, so waste no more time. */
3737 if (regno == STACK_POINTER_REGNUM
73a187c1 3738#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
3739 || (regno == HARD_FRAME_POINTER_REGNUM
3740 && (! reload_completed || frame_pointer_needed))
73a187c1 3741#endif
d7e4fe8b
RS
3742#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3743 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
3744#endif
e4b8a413
JW
3745 || (regno == FRAME_POINTER_REGNUM
3746 && (! reload_completed || frame_pointer_needed)))
d7429b6a
RK
3747 {
3748 /* If this is a register we are going to try to eliminate,
3749 don't mark it live here. If we are successful in
3750 eliminating it, it need not be live unless it is used for
3751 pseudos, in which case it will have been set live when
3752 it was allocated to the pseudos. If the register will not
3753 be eliminated, reload will set it live at that point. */
3754
3755 if (! TEST_HARD_REG_BIT (elim_reg_set, regno))
3756 regs_ever_live[regno] = 1;
3757 return;
3758 }
3759 /* No death notes for global register variables;
3760 their values are live after this function exits. */
3761 if (global_regs[regno])
d8c8b8e3
RS
3762 {
3763 if (final)
3764 reg_next_use[regno] = insn;
3765 return;
3766 }
d7429b6a
RK
3767
3768 n = HARD_REGNO_NREGS (regno, GET_MODE (x));
3769 while (--n > 0)
3770 {
916b1701
MM
3771 int regno_n = regno + n;
3772 int needed_regno = REGNO_REG_SET_P (needed, regno_n);
cb9e8ad1 3773
916b1701
MM
3774 SET_REGNO_REG_SET (live, regno_n);
3775 some_needed |= needed_regno;
931c6c7a 3776 some_not_needed |= ! needed_regno;
d7429b6a
RK
3777 }
3778 }
3779 if (final)
3780 {
3781 /* Record where each reg is used, so when the reg
3782 is set we know the next insn that uses it. */
3783
3784 reg_next_use[regno] = insn;
3785
3786 if (regno < FIRST_PSEUDO_REGISTER)
3787 {
3788 /* If a hard reg is being used,
3789 record that this function does use it. */
3790
3791 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
3792 if (i == 0)
3793 i = 1;
3794 do
3795 regs_ever_live[regno + --i] = 1;
3796 while (i > 0);
3797 }
3798 else
3799 {
3800 /* Keep track of which basic block each reg appears in. */
3801
3802 register int blocknum = BLOCK_NUM (insn);
3803
b1f21e0a
MM
3804 if (REG_BASIC_BLOCK (regno) == REG_BLOCK_UNKNOWN)
3805 REG_BASIC_BLOCK (regno) = blocknum;
3806 else if (REG_BASIC_BLOCK (regno) != blocknum)
3807 REG_BASIC_BLOCK (regno) = REG_BLOCK_GLOBAL;
d7429b6a
RK
3808
3809 /* Count (weighted) number of uses of each reg. */
3810
b1f21e0a 3811 REG_N_REFS (regno) += loop_depth;
d7429b6a
RK
3812 }
3813
3814 /* Record and count the insns in which a reg dies.
3815 If it is used in this insn and was dead below the insn
3816 then it dies in this insn. If it was set in this insn,
3817 we do not make a REG_DEAD note; likewise if we already
3818 made such a note. */
3819
cb9e8ad1 3820 if (some_not_needed
d7429b6a
RK
3821 && ! dead_or_set_p (insn, x)
3822#if 0
3823 && (regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
3824#endif
3825 )
3826 {
ab28041e
JW
3827 /* Check for the case where the register dying partially
3828 overlaps the register set by this insn. */
3829 if (regno < FIRST_PSEUDO_REGISTER
3830 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
3831 {
480eac3b 3832 int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
ab28041e
JW
3833 while (--n >= 0)
3834 some_needed |= dead_or_set_regno_p (insn, regno + n);
3835 }
3836
d7429b6a
RK
3837 /* If none of the words in X is needed, make a REG_DEAD
3838 note. Otherwise, we must make partial REG_DEAD notes. */
3839 if (! some_needed)
3840 {
3841 REG_NOTES (insn)
38a448ca 3842 = gen_rtx_EXPR_LIST (REG_DEAD, x, REG_NOTES (insn));
b1f21e0a 3843 REG_N_DEATHS (regno)++;
d7429b6a
RK
3844 }
3845 else
3846 {
3847 int i;
3848
3849 /* Don't make a REG_DEAD note for a part of a register
3850 that is set in the insn. */
3851
3852 for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
3853 i >= 0; i--)
916b1701 3854 if (!REGNO_REG_SET_P (needed, regno + i)
d7429b6a
RK
3855 && ! dead_or_set_regno_p (insn, regno + i))
3856 REG_NOTES (insn)
38a448ca
RH
3857 = gen_rtx_EXPR_LIST (REG_DEAD,
3858 gen_rtx_REG (reg_raw_mode[regno + i],
3859 regno + i),
3860 REG_NOTES (insn));
d7429b6a
RK
3861 }
3862 }
3863 }
3864 }
3865 return;
3866
3867 case SET:
3868 {
3869 register rtx testreg = SET_DEST (x);
3870 int mark_dest = 0;
3871
3872 /* If storing into MEM, don't show it as being used. But do
3873 show the address as being used. */
3874 if (GET_CODE (testreg) == MEM)
3875 {
3876#ifdef AUTO_INC_DEC
3877 if (final)
3878 find_auto_inc (needed, testreg, insn);
3879#endif
3880 mark_used_regs (needed, live, XEXP (testreg, 0), final, insn);
3881 mark_used_regs (needed, live, SET_SRC (x), final, insn);
3882 return;
3883 }
3884
3885 /* Storing in STRICT_LOW_PART is like storing in a reg
3886 in that this SET might be dead, so ignore it in TESTREG.
3887 but in some other ways it is like using the reg.
3888
3889 Storing in a SUBREG or a bit field is like storing the entire
3890 register in that if the register's value is not used
3891 then this SET is not needed. */
3892 while (GET_CODE (testreg) == STRICT_LOW_PART
3893 || GET_CODE (testreg) == ZERO_EXTRACT
3894 || GET_CODE (testreg) == SIGN_EXTRACT
3895 || GET_CODE (testreg) == SUBREG)
3896 {
88285acf
RK
3897 if (GET_CODE (testreg) == SUBREG
3898 && GET_CODE (SUBREG_REG (testreg)) == REG
3899 && REGNO (SUBREG_REG (testreg)) >= FIRST_PSEUDO_REGISTER
3900 && (GET_MODE_SIZE (GET_MODE (testreg))
3901 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (testreg)))))
b1f21e0a 3902 REG_CHANGES_SIZE (REGNO (SUBREG_REG (testreg))) = 1;
88285acf 3903
d7429b6a
RK
3904 /* Modifying a single register in an alternate mode
3905 does not use any of the old value. But these other
3906 ways of storing in a register do use the old value. */
3907 if (GET_CODE (testreg) == SUBREG
3908 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
3909 ;
3910 else
3911 mark_dest = 1;
3912
3913 testreg = XEXP (testreg, 0);
3914 }
3915
3916 /* If this is a store into a register,
3917 recursively scan the value being stored. */
3918
86465af7
DM
3919 if ((GET_CODE (testreg) == PARALLEL
3920 && GET_MODE (testreg) == BLKmode)
3921 || (GET_CODE (testreg) == REG
e4b8a413
JW
3922 && (regno = REGNO (testreg), ! (regno == FRAME_POINTER_REGNUM
3923 && (! reload_completed || frame_pointer_needed)))
73a187c1 3924#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
e4b8a413
JW
3925 && ! (regno == HARD_FRAME_POINTER_REGNUM
3926 && (! reload_completed || frame_pointer_needed))
73a187c1 3927#endif
d7e4fe8b 3928#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
86465af7 3929 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
d7e4fe8b 3930#endif
86465af7 3931 ))
d8c8b8e3
RS
3932 /* We used to exclude global_regs here, but that seems wrong.
3933 Storing in them is like storing in mem. */
d7429b6a
RK
3934 {
3935 mark_used_regs (needed, live, SET_SRC (x), final, insn);
3936 if (mark_dest)
3937 mark_used_regs (needed, live, SET_DEST (x), final, insn);
3938 return;
3939 }
3940 }
3941 break;
3942
3943 case RETURN:
3944 /* If exiting needs the right stack value, consider this insn as
3945 using the stack pointer. In any event, consider it as using
632c9d9e 3946 all global registers and all registers used by return. */
d7429b6a 3947 if (! EXIT_IGNORE_STACK
0200b5ed
JL
3948 || (! FRAME_POINTER_REQUIRED
3949 && ! current_function_calls_alloca
bfc5000a
JL
3950 && flag_omit_frame_pointer)
3951 || current_function_sp_is_unchanging)
916b1701 3952 SET_REGNO_REG_SET (live, STACK_POINTER_REGNUM);
d7429b6a
RK
3953
3954 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
632c9d9e
MS
3955 if (global_regs[i]
3956#ifdef EPILOGUE_USES
3957 || EPILOGUE_USES (i)
3958#endif
3959 )
916b1701 3960 SET_REGNO_REG_SET (live, i);
d7429b6a 3961 break;
e9a25f70 3962
40b5a77c
JL
3963 case ASM_OPERANDS:
3964 case UNSPEC_VOLATILE:
3965 case TRAP_IF:
3966 case ASM_INPUT:
3967 {
3968 /* Traditional and volatile asm instructions must be considered to use
3969 and clobber all hard registers, all pseudo-registers and all of
3970 memory. So must TRAP_IF and UNSPEC_VOLATILE operations.
3971
3972 Consider for instance a volatile asm that changes the fpu rounding
3973 mode. An insn should not be moved across this even if it only uses
3974 pseudo-regs because it might give an incorrectly rounded result.
3975
3976 ?!? Unfortunately, marking all hard registers as live causes massive
3977 problems for the register allocator and marking all pseudos as live
3978 creates mountains of uninitialized variable warnings.
3979
3980 So for now, just clear the memory set list and mark any regs
3981 we can find in ASM_OPERANDS as used. */
3982 if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
3983 mem_set_list = NULL_RTX;
3984
3985 /* For all ASM_OPERANDS, we must traverse the vector of input operands.
3986 We can not just fall through here since then we would be confused
3987 by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
3988 traditional asms unlike their normal usage. */
3989 if (code == ASM_OPERANDS)
3990 {
3991 int j;
3992
3993 for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
3994 mark_used_regs (needed, live, ASM_OPERANDS_INPUT (x, j),
3995 final, insn);
3996 }
3997 break;
3998 }
3999
4000
e9a25f70
JL
4001 default:
4002 break;
d7429b6a
RK
4003 }
4004
4005 /* Recursively scan the operands of this expression. */
4006
4007 {
4008 register char *fmt = GET_RTX_FORMAT (code);
4009 register int i;
4010
4011 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4012 {
4013 if (fmt[i] == 'e')
4014 {
4015 /* Tail recursive case: save a function call level. */
4016 if (i == 0)
4017 {
4018 x = XEXP (x, 0);
4019 goto retry;
4020 }
4021 mark_used_regs (needed, live, XEXP (x, i), final, insn);
4022 }
4023 else if (fmt[i] == 'E')
4024 {
4025 register int j;
4026 for (j = 0; j < XVECLEN (x, i); j++)
4027 mark_used_regs (needed, live, XVECEXP (x, i, j), final, insn);
4028 }
4029 }
4030 }
4031}
4032\f
4033#ifdef AUTO_INC_DEC
4034
4035static int
4036try_pre_increment_1 (insn)
4037 rtx insn;
4038{
4039 /* Find the next use of this reg. If in same basic block,
4040 make it do pre-increment or pre-decrement if appropriate. */
956d6950 4041 rtx x = single_set (insn);
5f4f0e22 4042 HOST_WIDE_INT amount = ((GET_CODE (SET_SRC (x)) == PLUS ? 1 : -1)
d7429b6a
RK
4043 * INTVAL (XEXP (SET_SRC (x), 1)));
4044 int regno = REGNO (SET_DEST (x));
4045 rtx y = reg_next_use[regno];
4046 if (y != 0
4047 && BLOCK_NUM (y) == BLOCK_NUM (insn)
89861c38 4048 /* Don't do this if the reg dies, or gets set in y; a standard addressing
0f41302f 4049 mode would be better. */
89861c38 4050 && ! dead_or_set_p (y, SET_DEST (x))
956d6950 4051 && try_pre_increment (y, SET_DEST (x), amount))
d7429b6a
RK
4052 {
4053 /* We have found a suitable auto-increment
4054 and already changed insn Y to do it.
4055 So flush this increment-instruction. */
4056 PUT_CODE (insn, NOTE);
4057 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
4058 NOTE_SOURCE_FILE (insn) = 0;
4059 /* Count a reference to this reg for the increment
4060 insn we are deleting. When a reg is incremented.
4061 spilling it is worse, so we want to make that
4062 less likely. */
4063 if (regno >= FIRST_PSEUDO_REGISTER)
4064 {
b1f21e0a
MM
4065 REG_N_REFS (regno) += loop_depth;
4066 REG_N_SETS (regno)++;
d7429b6a
RK
4067 }
4068 return 1;
4069 }
4070 return 0;
4071}
4072
4073/* Try to change INSN so that it does pre-increment or pre-decrement
4074 addressing on register REG in order to add AMOUNT to REG.
4075 AMOUNT is negative for pre-decrement.
4076 Returns 1 if the change could be made.
4077 This checks all about the validity of the result of modifying INSN. */
4078
4079static int
4080try_pre_increment (insn, reg, amount)
4081 rtx insn, reg;
5f4f0e22 4082 HOST_WIDE_INT amount;
d7429b6a
RK
4083{
4084 register rtx use;
4085
4086 /* Nonzero if we can try to make a pre-increment or pre-decrement.
4087 For example, addl $4,r1; movl (r1),... can become movl +(r1),... */
4088 int pre_ok = 0;
4089 /* Nonzero if we can try to make a post-increment or post-decrement.
4090 For example, addl $4,r1; movl -4(r1),... can become movl (r1)+,...
4091 It is possible for both PRE_OK and POST_OK to be nonzero if the machine
4092 supports both pre-inc and post-inc, or both pre-dec and post-dec. */
4093 int post_ok = 0;
4094
4095 /* Nonzero if the opportunity actually requires post-inc or post-dec. */
4096 int do_post = 0;
4097
4098 /* From the sign of increment, see which possibilities are conceivable
4099 on this target machine. */
940da324 4100 if (HAVE_PRE_INCREMENT && amount > 0)
d7429b6a 4101 pre_ok = 1;
940da324 4102 if (HAVE_POST_INCREMENT && amount > 0)
d7429b6a 4103 post_ok = 1;
d7429b6a 4104
940da324 4105 if (HAVE_PRE_DECREMENT && amount < 0)
d7429b6a 4106 pre_ok = 1;
940da324 4107 if (HAVE_POST_DECREMENT && amount < 0)
d7429b6a 4108 post_ok = 1;
d7429b6a
RK
4109
4110 if (! (pre_ok || post_ok))
4111 return 0;
4112
4113 /* It is not safe to add a side effect to a jump insn
4114 because if the incremented register is spilled and must be reloaded
4115 there would be no way to store the incremented value back in memory. */
4116
4117 if (GET_CODE (insn) == JUMP_INSN)
4118 return 0;
4119
4120 use = 0;
4121 if (pre_ok)
4122 use = find_use_as_address (PATTERN (insn), reg, 0);
4123 if (post_ok && (use == 0 || use == (rtx) 1))
4124 {
4125 use = find_use_as_address (PATTERN (insn), reg, -amount);
4126 do_post = 1;
4127 }
4128
4129 if (use == 0 || use == (rtx) 1)
4130 return 0;
4131
4132 if (GET_MODE_SIZE (GET_MODE (use)) != (amount > 0 ? amount : - amount))
4133 return 0;
4134
a0fbc3a9
SC
4135 /* See if this combination of instruction and addressing mode exists. */
4136 if (! validate_change (insn, &XEXP (use, 0),
38a448ca
RH
4137 gen_rtx_fmt_e (amount > 0
4138 ? (do_post ? POST_INC : PRE_INC)
4139 : (do_post ? POST_DEC : PRE_DEC),
4140 Pmode, reg), 0))
a0fbc3a9 4141 return 0;
d7429b6a
RK
4142
4143 /* Record that this insn now has an implicit side effect on X. */
38a448ca 4144 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_INC, reg, REG_NOTES (insn));
d7429b6a
RK
4145 return 1;
4146}
4147
4148#endif /* AUTO_INC_DEC */
4149\f
4150/* Find the place in the rtx X where REG is used as a memory address.
4151 Return the MEM rtx that so uses it.
4152 If PLUSCONST is nonzero, search instead for a memory address equivalent to
4153 (plus REG (const_int PLUSCONST)).
4154
4155 If such an address does not appear, return 0.
4156 If REG appears more than once, or is used other than in such an address,
4157 return (rtx)1. */
4158
8c660648 4159rtx
d7429b6a
RK
4160find_use_as_address (x, reg, plusconst)
4161 register rtx x;
4162 rtx reg;
e658434c 4163 HOST_WIDE_INT plusconst;
d7429b6a
RK
4164{
4165 enum rtx_code code = GET_CODE (x);
4166 char *fmt = GET_RTX_FORMAT (code);
4167 register int i;
4168 register rtx value = 0;
4169 register rtx tem;
4170
4171 if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
4172 return x;
4173
4174 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
4175 && XEXP (XEXP (x, 0), 0) == reg
4176 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4177 && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
4178 return x;
4179
4180 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
4181 {
4182 /* If REG occurs inside a MEM used in a bit-field reference,
4183 that is unacceptable. */
4184 if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
6fa5c106 4185 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4186 }
4187
4188 if (x == reg)
6fa5c106 4189 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4190
4191 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4192 {
4193 if (fmt[i] == 'e')
4194 {
4195 tem = find_use_as_address (XEXP (x, i), reg, plusconst);
4196 if (value == 0)
4197 value = tem;
4198 else if (tem != 0)
6fa5c106 4199 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4200 }
4201 if (fmt[i] == 'E')
4202 {
4203 register int j;
4204 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4205 {
4206 tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
4207 if (value == 0)
4208 value = tem;
4209 else if (tem != 0)
6fa5c106 4210 return (rtx) (HOST_WIDE_INT) 1;
d7429b6a
RK
4211 }
4212 }
4213 }
4214
4215 return value;
4216}
4217\f
4218/* Write information about registers and basic blocks into FILE.
4219 This is part of making a debugging dump. */
4220
4221void
4222dump_flow_info (file)
4223 FILE *file;
4224{
4225 register int i;
4226 static char *reg_class_names[] = REG_CLASS_NAMES;
4227
4228 fprintf (file, "%d registers.\n", max_regno);
d7429b6a 4229 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
b1f21e0a 4230 if (REG_N_REFS (i))
d7429b6a 4231 {
e4600702 4232 enum reg_class class, altclass;
d7429b6a 4233 fprintf (file, "\nRegister %d used %d times across %d insns",
b1f21e0a
MM
4234 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
4235 if (REG_BASIC_BLOCK (i) >= 0)
4236 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
6fc4610b
MM
4237 if (REG_N_SETS (i))
4238 fprintf (file, "; set %d time%s", REG_N_SETS (i),
4239 (REG_N_SETS (i) == 1) ? "" : "s");
4240 if (REG_USERVAR_P (regno_reg_rtx[i]))
4241 fprintf (file, "; user var");
b1f21e0a
MM
4242 if (REG_N_DEATHS (i) != 1)
4243 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
4244 if (REG_N_CALLS_CROSSED (i) == 1)
d7429b6a 4245 fprintf (file, "; crosses 1 call");
b1f21e0a
MM
4246 else if (REG_N_CALLS_CROSSED (i))
4247 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
d7429b6a
RK
4248 if (PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
4249 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
4250 class = reg_preferred_class (i);
e4600702
RK
4251 altclass = reg_alternate_class (i);
4252 if (class != GENERAL_REGS || altclass != ALL_REGS)
d7429b6a 4253 {
e4600702
RK
4254 if (altclass == ALL_REGS || class == ALL_REGS)
4255 fprintf (file, "; pref %s", reg_class_names[(int) class]);
4256 else if (altclass == NO_REGS)
d7429b6a
RK
4257 fprintf (file, "; %s or none", reg_class_names[(int) class]);
4258 else
e4600702
RK
4259 fprintf (file, "; pref %s, else %s",
4260 reg_class_names[(int) class],
4261 reg_class_names[(int) altclass]);
d7429b6a
RK
4262 }
4263 if (REGNO_POINTER_FLAG (i))
4264 fprintf (file, "; pointer");
4265 fprintf (file, ".\n");
4266 }
e881bb1b 4267
d7429b6a 4268 fprintf (file, "\n%d basic blocks.\n", n_basic_blocks);
e881bb1b
RH
4269 for (i = 0; i < n_basic_blocks; i++)
4270 {
4271 register basic_block bb = BASIC_BLOCK (i);
4272 register int regno;
4273 register edge e;
4274
4275 fprintf (file, "\nBasic block %d: first insn %d, last %d.\n",
4276 i, INSN_UID (bb->head), INSN_UID (bb->end));
4277
4278 fprintf (file, "Predecessors: ");
4279 for (e = bb->pred; e ; e = e->pred_next)
4280 dump_edge_info (file, e, 0);
4281
4282 fprintf (file, "\nSuccessors: ");
4283 for (e = bb->succ; e ; e = e->succ_next)
4284 dump_edge_info (file, e, 1);
4285
4286 fprintf (file, "\nRegisters live at start:");
4287 if (bb->global_live_at_start)
4288 {
4289 for (regno = 0; regno < max_regno; regno++)
4290 if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
4291 fprintf (file, " %d", regno);
4292 }
4293 else
4294 fprintf (file, " n/a");
4295
4296 fprintf (file, "\nRegisters live at end:");
4297 if (bb->global_live_at_end)
4298 {
4299 for (regno = 0; regno < max_regno; regno++)
4300 if (REGNO_REG_SET_P (bb->global_live_at_end, regno))
4301 fprintf (file, " %d", regno);
4302 }
4303 else
4304 fprintf (file, " n/a");
4305
4306 putc('\n', file);
4307 }
4308
4309 putc('\n', file);
4310}
4311
4312static void
4313dump_edge_info (file, e, do_succ)
4314 FILE *file;
4315 edge e;
4316 int do_succ;
4317{
4318 basic_block side = (do_succ ? e->dest : e->src);
4319
4320 if (side == ENTRY_BLOCK_PTR)
4321 fputs (" ENTRY", file);
4322 else if (side == EXIT_BLOCK_PTR)
4323 fputs (" EXIT", file);
4324 else
4325 fprintf (file, " %d", side->index);
4326
4327 if (e->flags)
4328 {
4329 static char * bitnames[] = {
4330 "fallthru", "crit", "ab", "abcall", "eh", "fake"
4331 };
4332 int comma = 0;
4333 int i, flags = e->flags;
4334
4335 fputc (' ', file);
4336 fputc ('(', file);
4337 for (i = 0; flags; i++)
4338 if (flags & (1 << i))
4339 {
4340 flags &= ~(1 << i);
4341
4342 if (comma)
4343 fputc (',', file);
4344 if (i < (int)(sizeof (bitnames) / sizeof (*bitnames)))
4345 fputs (bitnames[i], file);
4346 else
4347 fprintf (file, "%d", i);
4348 comma = 1;
4349 }
4350 fputc (')', file);
4351 }
d7429b6a 4352}
3e28fe44
MM
4353
4354\f
4355/* Like print_rtl, but also print out live information for the start of each
4356 basic block. */
4357
4358void
4359print_rtl_with_bb (outf, rtx_first)
4360 FILE *outf;
4361 rtx rtx_first;
4362{
4363 register rtx tmp_rtx;
4364
4365 if (rtx_first == 0)
4366 fprintf (outf, "(nil)\n");
3e28fe44
MM
4367 else
4368 {
e881bb1b 4369 int i;
3e28fe44
MM
4370 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
4371 int max_uid = get_max_uid ();
54ea1de9
KG
4372 basic_block *start = (basic_block *)
4373 alloca (max_uid * sizeof (basic_block));
4374 basic_block *end = (basic_block *)
4375 alloca (max_uid * sizeof (basic_block));
2a92c071
GS
4376 enum bb_state *in_bb_p = (enum bb_state *)
4377 alloca (max_uid * sizeof (enum bb_state));
3e28fe44 4378
e881bb1b
RH
4379 memset (start, 0, max_uid * sizeof (basic_block));
4380 memset (end, 0, max_uid * sizeof (basic_block));
4381 memset (in_bb_p, 0, max_uid * sizeof (enum bb_state));
3e28fe44 4382
e881bb1b 4383 for (i = n_basic_blocks - 1; i >= 0; i--)
3e28fe44 4384 {
e881bb1b 4385 basic_block bb = BASIC_BLOCK (i);
3e28fe44 4386 rtx x;
e881bb1b
RH
4387
4388 start[INSN_UID (bb->head)] = bb;
4389 end[INSN_UID (bb->end)] = bb;
4390 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
3e28fe44 4391 {
e881bb1b
RH
4392 enum bb_state state = IN_MULTIPLE_BB;
4393 if (in_bb_p[INSN_UID(x)] == NOT_IN_BB)
4394 state = IN_ONE_BB;
4395 in_bb_p[INSN_UID(x)] = state;
4396
4397 if (x == bb->end)
3e28fe44
MM
4398 break;
4399 }
4400 }
4401
4402 for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
4403 {
b707b450 4404 int did_output;
e881bb1b 4405 basic_block bb;
b707b450 4406
e881bb1b 4407 if ((bb = start[INSN_UID (tmp_rtx)]) != NULL)
3e28fe44
MM
4408 {
4409 fprintf (outf, ";; Start of basic block %d, registers live:",
e881bb1b 4410 bb->index);
3e28fe44 4411
e881bb1b 4412 EXECUTE_IF_SET_IN_REG_SET (bb->global_live_at_start, 0, i,
3e28fe44
MM
4413 {
4414 fprintf (outf, " %d", i);
4415 if (i < FIRST_PSEUDO_REGISTER)
4416 fprintf (outf, " [%s]",
4417 reg_names[i]);
4418 });
4419 putc ('\n', outf);
4420 }
4421
ab87f8c8 4422 if (in_bb_p[INSN_UID(tmp_rtx)] == NOT_IN_BB
3e28fe44 4423 && GET_CODE (tmp_rtx) != NOTE
ab87f8c8
JL
4424 && GET_CODE (tmp_rtx) != BARRIER
4425 && ! obey_regdecls)
3e28fe44 4426 fprintf (outf, ";; Insn is not within a basic block\n");
e881bb1b 4427 else if (in_bb_p[INSN_UID(tmp_rtx)] == IN_MULTIPLE_BB)
3e28fe44
MM
4428 fprintf (outf, ";; Insn is in multiple basic blocks\n");
4429
b707b450 4430 did_output = print_rtl_single (outf, tmp_rtx);
3e28fe44 4431
e881bb1b
RH
4432 if ((bb = end[INSN_UID (tmp_rtx)]) != NULL)
4433 fprintf (outf, ";; End of basic block %d\n", bb->index);
3e28fe44 4434
b707b450 4435 if (did_output)
9ec36da5 4436 putc ('\n', outf);
3e28fe44
MM
4437 }
4438 }
4439}
5ece9746
JL
4440
4441\f
4442/* Integer list support. */
4443
4444/* Allocate a node from list *HEAD_PTR. */
4445
4446static int_list_ptr
4447alloc_int_list_node (head_ptr)
4448 int_list_block **head_ptr;
4449{
4450 struct int_list_block *first_blk = *head_ptr;
4451
4452 if (first_blk == NULL || first_blk->nodes_left <= 0)
4453 {
4454 first_blk = (struct int_list_block *) xmalloc (sizeof (struct int_list_block));
4455 first_blk->nodes_left = INT_LIST_NODES_IN_BLK;
4456 first_blk->next = *head_ptr;
4457 *head_ptr = first_blk;
4458 }
4459
4460 first_blk->nodes_left--;
4461 return &first_blk->nodes[first_blk->nodes_left];
4462}
4463
4464/* Pointer to head of predecessor/successor block list. */
4465static int_list_block *pred_int_list_blocks;
4466
4467/* Add a new node to integer list LIST with value VAL.
4468 LIST is a pointer to a list object to allow for different implementations.
4469 If *LIST is initially NULL, the list is empty.
4470 The caller must not care whether the element is added to the front or
4471 to the end of the list (to allow for different implementations). */
4472
4473static int_list_ptr
4474add_int_list_node (blk_list, list, val)
4475 int_list_block **blk_list;
4476 int_list **list;
4477 int val;
4478{
4479 int_list_ptr p = alloc_int_list_node (blk_list);
4480
4481 p->val = val;
4482 p->next = *list;
4483 *list = p;
4484 return p;
4485}
4486
4487/* Free the blocks of lists at BLK_LIST. */
4488
4489void
4490free_int_list (blk_list)
4491 int_list_block **blk_list;
4492{
4493 int_list_block *p, *next;
4494
4495 for (p = *blk_list; p != NULL; p = next)
4496 {
4497 next = p->next;
4498 free (p);
4499 }
4500
4501 /* Mark list as empty for the next function we compile. */
4502 *blk_list = NULL;
4503}
4504\f
4505/* Predecessor/successor computation. */
4506
4507/* Mark PRED_BB a precessor of SUCC_BB,
4508 and conversely SUCC_BB a successor of PRED_BB. */
4509
4510static void
4511add_pred_succ (pred_bb, succ_bb, s_preds, s_succs, num_preds, num_succs)
4512 int pred_bb;
4513 int succ_bb;
4514 int_list_ptr *s_preds;
4515 int_list_ptr *s_succs;
4516 int *num_preds;
4517 int *num_succs;
4518{
4519 if (succ_bb != EXIT_BLOCK)
4520 {
4521 add_int_list_node (&pred_int_list_blocks, &s_preds[succ_bb], pred_bb);
4522 num_preds[succ_bb]++;
4523 }
4524 if (pred_bb != ENTRY_BLOCK)
4525 {
4526 add_int_list_node (&pred_int_list_blocks, &s_succs[pred_bb], succ_bb);
4527 num_succs[pred_bb]++;
4528 }
4529}
4530
e881bb1b
RH
4531/* Convert edge lists into pred/succ lists for backward compatibility. */
4532
743bb12d 4533void
5ece9746
JL
4534compute_preds_succs (s_preds, s_succs, num_preds, num_succs)
4535 int_list_ptr *s_preds;
4536 int_list_ptr *s_succs;
4537 int *num_preds;
4538 int *num_succs;
4539{
e881bb1b
RH
4540 int i, n = n_basic_blocks;
4541 edge e;
5ece9746 4542
e881bb1b
RH
4543 memset (s_preds, 0, n_basic_blocks * sizeof (int_list_ptr));
4544 memset (s_succs, 0, n_basic_blocks * sizeof (int_list_ptr));
4545 memset (num_preds, 0, n_basic_blocks * sizeof (int));
4546 memset (num_succs, 0, n_basic_blocks * sizeof (int));
5ece9746 4547
e881bb1b 4548 for (i = 0; i < n; ++i)
5ece9746 4549 {
e881bb1b
RH
4550 basic_block bb = BASIC_BLOCK (i);
4551
4552 for (e = bb->succ; e ; e = e->succ_next)
4553 add_pred_succ (i, e->dest->index, s_preds, s_succs,
4554 num_preds, num_succs);
5ece9746
JL
4555 }
4556
e881bb1b
RH
4557 for (e = ENTRY_BLOCK_PTR->succ; e ; e = e->succ_next)
4558 add_pred_succ (ENTRY_BLOCK, e->dest->index, s_preds, s_succs,
4559 num_preds, num_succs);
5ece9746
JL
4560}
4561
4562void
421382ac 4563dump_bb_data (file, preds, succs, live_info)
5ece9746
JL
4564 FILE *file;
4565 int_list_ptr *preds;
4566 int_list_ptr *succs;
421382ac 4567 int live_info;
5ece9746
JL
4568{
4569 int bb;
4570 int_list_ptr p;
4571
4572 fprintf (file, "BB data\n\n");
4573 for (bb = 0; bb < n_basic_blocks; bb++)
4574 {
4575 fprintf (file, "BB %d, start %d, end %d\n", bb,
4576 INSN_UID (BLOCK_HEAD (bb)), INSN_UID (BLOCK_END (bb)));
4577 fprintf (file, " preds:");
4578 for (p = preds[bb]; p != NULL; p = p->next)
4579 {
4580 int pred_bb = INT_LIST_VAL (p);
4581 if (pred_bb == ENTRY_BLOCK)
4582 fprintf (file, " entry");
4583 else
4584 fprintf (file, " %d", pred_bb);
4585 }
4586 fprintf (file, "\n");
4587 fprintf (file, " succs:");
4588 for (p = succs[bb]; p != NULL; p = p->next)
4589 {
4590 int succ_bb = INT_LIST_VAL (p);
4591 if (succ_bb == EXIT_BLOCK)
4592 fprintf (file, " exit");
4593 else
4594 fprintf (file, " %d", succ_bb);
4595 }
421382ac
BS
4596 if (live_info)
4597 {
4598 int regno;
4599 fprintf (file, "\nRegisters live at start:");
4600 for (regno = 0; regno < max_regno; regno++)
e881bb1b 4601 if (REGNO_REG_SET_P (BASIC_BLOCK (bb)->global_live_at_start, regno))
421382ac
BS
4602 fprintf (file, " %d", regno);
4603 fprintf (file, "\n");
4604 }
5ece9746
JL
4605 fprintf (file, "\n");
4606 }
4607 fprintf (file, "\n");
4608}
4609
4610/* Free basic block data storage. */
4611
4612void
4613free_bb_mem ()
4614{
4615 free_int_list (&pred_int_list_blocks);
4616}
5e89e58b 4617
5ece9746
JL
4618/* Compute dominator relationships. */
4619void
4620compute_dominators (dominators, post_dominators, s_preds, s_succs)
4621 sbitmap *dominators;
4622 sbitmap *post_dominators;
4623 int_list_ptr *s_preds;
4624 int_list_ptr *s_succs;
4625{
4626 int bb, changed, passes;
4627 sbitmap *temp_bitmap;
4628
4629 temp_bitmap = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4630 sbitmap_vector_ones (dominators, n_basic_blocks);
4631 sbitmap_vector_ones (post_dominators, n_basic_blocks);
4632 sbitmap_vector_zero (temp_bitmap, n_basic_blocks);
4633
4634 sbitmap_zero (dominators[0]);
4635 SET_BIT (dominators[0], 0);
4636
e881bb1b
RH
4637 sbitmap_zero (post_dominators[n_basic_blocks - 1]);
4638 SET_BIT (post_dominators[n_basic_blocks - 1], 0);
5ece9746
JL
4639
4640 passes = 0;
4641 changed = 1;
4642 while (changed)
4643 {
4644 changed = 0;
4645 for (bb = 1; bb < n_basic_blocks; bb++)
4646 {
4647 sbitmap_intersect_of_predecessors (temp_bitmap[bb], dominators,
4648 bb, s_preds);
4649 SET_BIT (temp_bitmap[bb], bb);
4650 changed |= sbitmap_a_and_b (dominators[bb],
4651 dominators[bb],
4652 temp_bitmap[bb]);
4653 sbitmap_intersect_of_successors (temp_bitmap[bb], post_dominators,
4654 bb, s_succs);
4655 SET_BIT (temp_bitmap[bb], bb);
4656 changed |= sbitmap_a_and_b (post_dominators[bb],
4657 post_dominators[bb],
4658 temp_bitmap[bb]);
4659 }
4660 passes++;
4661 }
4662
4663 free (temp_bitmap);
4664}
4c649323 4665
422d0fb0
RH
4666/* Given DOMINATORS, compute the immediate dominators into IDOM. */
4667
4668void
4669compute_immediate_dominators (idom, dominators)
4670 int *idom;
4671 sbitmap *dominators;
4672{
4673 sbitmap *tmp;
4674 int b;
4675
4676 tmp = sbitmap_vector_alloc (n_basic_blocks, n_basic_blocks);
4677
4678 /* Begin with tmp(n) = dom(n) - { n }. */
4679 for (b = n_basic_blocks; --b >= 0; )
4680 {
4681 sbitmap_copy (tmp[b], dominators[b]);
4682 RESET_BIT (tmp[b], b);
4683 }
4684
4685 /* Subtract out all of our dominator's dominators. */
4686 for (b = n_basic_blocks; --b >= 0; )
4687 {
4688 sbitmap tmp_b = tmp[b];
4689 int s;
4690
4691 for (s = n_basic_blocks; --s >= 0; )
4692 if (TEST_BIT (tmp_b, s))
4693 sbitmap_difference (tmp_b, tmp_b, tmp[s]);
4694 }
4695
4696 /* Find the one bit set in the bitmap and put it in the output array. */
4697 for (b = n_basic_blocks; --b >= 0; )
4698 {
4699 int t;
4700 EXECUTE_IF_SET_IN_SBITMAP (tmp[b], 0, t, { idom[b] = t; });
4701 }
4702
4703 sbitmap_vector_free (tmp);
4704}
4705
4c649323
JL
4706/* Count for a single SET rtx, X. */
4707
4708static void
4709count_reg_sets_1 (x)
4710 rtx x;
4711{
4712 register int regno;
4713 register rtx reg = SET_DEST (x);
4714
4715 /* Find the register that's set/clobbered. */
4716 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
4717 || GET_CODE (reg) == SIGN_EXTRACT
4718 || GET_CODE (reg) == STRICT_LOW_PART)
4719 reg = XEXP (reg, 0);
4720
86465af7
DM
4721 if (GET_CODE (reg) == PARALLEL
4722 && GET_MODE (reg) == BLKmode)
4723 {
4724 register int i;
4725 for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
4726 count_reg_sets_1 (XVECEXP (reg, 0, i));
4727 return;
4728 }
4729
4c649323
JL
4730 if (GET_CODE (reg) == REG)
4731 {
4732 regno = REGNO (reg);
4733 if (regno >= FIRST_PSEUDO_REGISTER)
4734 {
4735 /* Count (weighted) references, stores, etc. This counts a
4736 register twice if it is modified, but that is correct. */
4737 REG_N_SETS (regno)++;
4738
4739 REG_N_REFS (regno) += loop_depth;
4740 }
4741 }
4742}
4743
4744/* Increment REG_N_SETS for each SET or CLOBBER found in X; also increment
4745 REG_N_REFS by the current loop depth for each SET or CLOBBER found. */
4746
4747static void
4748count_reg_sets (x)
4749 rtx x;
4750{
4751 register RTX_CODE code = GET_CODE (x);
4752
4753 if (code == SET || code == CLOBBER)
4754 count_reg_sets_1 (x);
4755 else if (code == PARALLEL)
4756 {
4757 register int i;
4758 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
4759 {
4760 code = GET_CODE (XVECEXP (x, 0, i));
4761 if (code == SET || code == CLOBBER)
4762 count_reg_sets_1 (XVECEXP (x, 0, i));
4763 }
4764 }
4765}
4766
4767/* Increment REG_N_REFS by the current loop depth each register reference
4768 found in X. */
4769
4770static void
4771count_reg_references (x)
4772 rtx x;
4773{
4774 register RTX_CODE code;
4c649323
JL
4775
4776 retry:
4777 code = GET_CODE (x);
4778 switch (code)
4779 {
4780 case LABEL_REF:
4781 case SYMBOL_REF:
4782 case CONST_INT:
4783 case CONST:
4784 case CONST_DOUBLE:
4785 case PC:
4786 case ADDR_VEC:
4787 case ADDR_DIFF_VEC:
4788 case ASM_INPUT:
4789 return;
4790
4791#ifdef HAVE_cc0
4792 case CC0:
4793 return;
4794#endif
4795
4796 case CLOBBER:
4797 /* If we are clobbering a MEM, mark any registers inside the address
4798 as being used. */
4799 if (GET_CODE (XEXP (x, 0)) == MEM)
4800 count_reg_references (XEXP (XEXP (x, 0), 0));
4801 return;
4802
4803 case SUBREG:
4804 /* While we're here, optimize this case. */
4805 x = SUBREG_REG (x);
4806
4807 /* In case the SUBREG is not of a register, don't optimize */
4808 if (GET_CODE (x) != REG)
4809 {
4810 count_reg_references (x);
4811 return;
4812 }
4813
4814 /* ... fall through ... */
4815
4816 case REG:
4817 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
4818 REG_N_REFS (REGNO (x)) += loop_depth;
4819 return;
4820
4821 case SET:
4822 {
4823 register rtx testreg = SET_DEST (x);
4824 int mark_dest = 0;
4825
4826 /* If storing into MEM, don't show it as being used. But do
4827 show the address as being used. */
4828 if (GET_CODE (testreg) == MEM)
4829 {
4830 count_reg_references (XEXP (testreg, 0));
4831 count_reg_references (SET_SRC (x));
4832 return;
4833 }
4834
4835 /* Storing in STRICT_LOW_PART is like storing in a reg
4836 in that this SET might be dead, so ignore it in TESTREG.
4837 but in some other ways it is like using the reg.
4838
4839 Storing in a SUBREG or a bit field is like storing the entire
4840 register in that if the register's value is not used
4841 then this SET is not needed. */
4842 while (GET_CODE (testreg) == STRICT_LOW_PART
4843 || GET_CODE (testreg) == ZERO_EXTRACT
4844 || GET_CODE (testreg) == SIGN_EXTRACT
4845 || GET_CODE (testreg) == SUBREG)
4846 {
4847 /* Modifying a single register in an alternate mode
4848 does not use any of the old value. But these other
4849 ways of storing in a register do use the old value. */
4850 if (GET_CODE (testreg) == SUBREG
4851 && !(REG_SIZE (SUBREG_REG (testreg)) > REG_SIZE (testreg)))
4852 ;
4853 else
4854 mark_dest = 1;
4855
4856 testreg = XEXP (testreg, 0);
4857 }
4858
4859 /* If this is a store into a register,
4860 recursively scan the value being stored. */
4861
86465af7
DM
4862 if ((GET_CODE (testreg) == PARALLEL
4863 && GET_MODE (testreg) == BLKmode)
4864 || GET_CODE (testreg) == REG)
4c649323
JL
4865 {
4866 count_reg_references (SET_SRC (x));
4867 if (mark_dest)
4868 count_reg_references (SET_DEST (x));
4869 return;
4870 }
4871 }
4872 break;
4873
4874 default:
4875 break;
4876 }
4877
4878 /* Recursively scan the operands of this expression. */
4879
4880 {
4881 register char *fmt = GET_RTX_FORMAT (code);
4882 register int i;
4883
4884 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4885 {
4886 if (fmt[i] == 'e')
4887 {
4888 /* Tail recursive case: save a function call level. */
4889 if (i == 0)
4890 {
4891 x = XEXP (x, 0);
4892 goto retry;
4893 }
4894 count_reg_references (XEXP (x, i));
4895 }
4896 else if (fmt[i] == 'E')
4897 {
4898 register int j;
4899 for (j = 0; j < XVECLEN (x, i); j++)
4900 count_reg_references (XVECEXP (x, i, j));
4901 }
4902 }
4903 }
4904}
4905
4906/* Recompute register set/reference counts immediately prior to register
4907 allocation.
4908
4909 This avoids problems with set/reference counts changing to/from values
4910 which have special meanings to the register allocators.
4911
4912 Additionally, the reference counts are the primary component used by the
4913 register allocators to prioritize pseudos for allocation to hard regs.
4914 More accurate reference counts generally lead to better register allocation.
4915
213c4983
R
4916 F is the first insn to be scanned.
4917 LOOP_STEP denotes how much loop_depth should be incremented per
4918 loop nesting level in order to increase the ref count more for references
4919 in a loop.
4920
4c649323
JL
4921 It might be worthwhile to update REG_LIVE_LENGTH, REG_BASIC_BLOCK and
4922 possibly other information which is used by the register allocators. */
4923
762a1d90 4924void
213c4983 4925recompute_reg_usage (f, loop_step)
4c649323 4926 rtx f;
213c4983 4927 int loop_step;
4c649323
JL
4928{
4929 rtx insn;
4930 int i, max_reg;
4931
4932 /* Clear out the old data. */
4933 max_reg = max_reg_num ();
4934 for (i = FIRST_PSEUDO_REGISTER; i < max_reg; i++)
4935 {
4936 REG_N_SETS (i) = 0;
4937 REG_N_REFS (i) = 0;
4938 }
4939
4940 /* Scan each insn in the chain and count how many times each register is
4941 set/used. */
4942 loop_depth = 1;
4943 for (insn = f; insn; insn = NEXT_INSN (insn))
4944 {
4945 /* Keep track of loop depth. */
4946 if (GET_CODE (insn) == NOTE)
4947 {
4948 /* Look for loop boundaries. */
4949 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
213c4983 4950 loop_depth -= loop_step;
4c649323 4951 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
213c4983 4952 loop_depth += loop_step;
4c649323
JL
4953
4954 /* If we have LOOP_DEPTH == 0, there has been a bookkeeping error.
4955 Abort now rather than setting register status incorrectly. */
4956 if (loop_depth == 0)
4957 abort ();
4958 }
4959 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4960 {
4961 rtx links;
4962
4963 /* This call will increment REG_N_SETS for each SET or CLOBBER
4964 of a register in INSN. It will also increment REG_N_REFS
4965 by the loop depth for each set of a register in INSN. */
4966 count_reg_sets (PATTERN (insn));
4967
4968 /* count_reg_sets does not detect autoincrement address modes, so
4969 detect them here by looking at the notes attached to INSN. */
4970 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
4971 {
4972 if (REG_NOTE_KIND (links) == REG_INC)
4973 /* Count (weighted) references, stores, etc. This counts a
4974 register twice if it is modified, but that is correct. */
4975 REG_N_SETS (REGNO (XEXP (links, 0)))++;
4976 }
4977
4978 /* This call will increment REG_N_REFS by the current loop depth for
4979 each reference to a register in INSN. */
4980 count_reg_references (PATTERN (insn));
4981
4982 /* count_reg_references will not include counts for arguments to
4983 function calls, so detect them here by examining the
4984 CALL_INSN_FUNCTION_USAGE data. */
4985 if (GET_CODE (insn) == CALL_INSN)
4986 {
4987 rtx note;
4988
4989 for (note = CALL_INSN_FUNCTION_USAGE (insn);
4990 note;
4991 note = XEXP (note, 1))
4992 if (GET_CODE (XEXP (note, 0)) == USE)
4993 count_reg_references (SET_DEST (XEXP (note, 0)));
4994 }
4995 }
4996 }
4997}
e881bb1b
RH
4998
4999/* Record INSN's block as BB. */
5000
5001void
5002set_block_for_insn (insn, bb)
5003 rtx insn;
5004 basic_block bb;
5005{
5006 size_t uid = INSN_UID (insn);
5007 if (uid >= basic_block_for_insn->num_elements)
5008 {
5009 int new_size;
5010
5011 /* Add one-eighth the size so we don't keep calling xrealloc. */
5012 new_size = uid + (uid + 7) / 8;
5013
5014 VARRAY_GROW (basic_block_for_insn, new_size);
5015 }
5016 VARRAY_BB (basic_block_for_insn, uid) = bb;
5017}
5018
5019/* Record INSN's block number as BB. */
5020/* ??? This has got to go. */
5021
5022void
5023set_block_num (insn, bb)
5024 rtx insn;
5025 int bb;
5026{
5027 set_block_for_insn (insn, BASIC_BLOCK (bb));
5028}
34487bf8
RH
5029\f
5030/* Verify the CFG consistency. This function check some CFG invariants and
5031 aborts when something is wrong. Hope that this function will help to
5032 convert many optimization passes to preserve CFG consistent.
5033
5034 Currently it does following checks:
5035
5036 - test head/end pointers
5037 - overlapping of basic blocks
5038 - edge list corectness
5039 - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
5040 - tails of basic blocks (ensure that boundary is necesary)
5041 - scans body of the basic block for JUMP_INSN, CODE_LABEL
5042 and NOTE_INSN_BASIC_BLOCK
5043 - check that all insns are in the basic blocks
5044 (except the switch handling code, barriers and notes)
5045
5046 In future it can be extended check a lot of other stuff as well
5047 (reachability of basic blocks, life information, etc. etc.). */
5048
5049void
5050verify_flow_info ()
5051{
5052 const int max_uid = get_max_uid ();
5053 const rtx rtx_first = get_insns ();
5054 basic_block *bb_info;
5055 rtx x;
5056 int i;
5057
5058 bb_info = (basic_block *) alloca (max_uid * sizeof (basic_block));
5059 memset (bb_info, 0, max_uid * sizeof (basic_block));
5060
5061 /* First pass check head/end pointers and set bb_info array used by
5062 later passes. */
5063 for (i = n_basic_blocks - 1; i >= 0; i--)
5064 {
5065 basic_block bb = BASIC_BLOCK (i);
5066
5067 /* Check the head pointer and make sure that it is pointing into
5068 insn list. */
5069 for (x = rtx_first; x != NULL_RTX; x = NEXT_INSN (x))
5070 if (x == bb->head)
5071 break;
5072 if (!x)
5073 {
5074 fatal ("verify_flow_info: Head insn %d for block %d not found in the insn stream.\n",
5075 INSN_UID (bb->head), bb->index);
5076 }
5077
5078 /* Check the end pointer and make sure that it is pointing into
5079 insn list. */
5080 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
5081 {
5082 if (bb_info[INSN_UID (x)] != NULL)
5083 {
5084 fatal ("verify_flow_info: Insn %d is in multiple basic blocks (%d and %d)",
5085 INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
5086 }
5087 bb_info[INSN_UID (x)] = bb;
5088
5089 if (x == bb->end)
5090 break;
5091 }
5092 if (!x)
5093 {
5094 fatal ("verify_flow_info: End insn %d for block %d not found in the insn stream.\n",
5095 INSN_UID (bb->end), bb->index);
5096 }
5097 }
5098
5099 /* Now check the basic blocks (boundaries etc.) */
5100 for (i = n_basic_blocks - 1; i >= 0; i--)
5101 {
5102 basic_block bb = BASIC_BLOCK (i);
5103 /* Check corectness of edge lists */
5104 edge e;
5105
5106 e = bb->succ;
5107 while (e)
5108 {
5109 if (e->src != bb)
5110 {
5111 fprintf (stderr, "verify_flow_info: Basic block %d succ edge is corrupted\n",
5112 bb->index);
5113 fprintf (stderr, "Predecessor: ");
5114 dump_edge_info (stderr, e, 0);
5115 fprintf (stderr, "\nSuccessor: ");
5116 dump_edge_info (stderr, e, 1);
5117 fflush (stderr);
5118 abort ();
5119 }
5120 if (e->dest != EXIT_BLOCK_PTR)
5121 {
5122 edge e2 = e->dest->pred;
5123 while (e2 && e2 != e)
5124 e2 = e2->pred_next;
5125 if (!e2)
5126 {
5127 fatal ("verify_flow_info: Basic block %i edge lists are corrupted\n",
5128 bb->index);
5129 }
5130 }
5131 e = e->succ_next;
5132 }
5133
5134 e = bb->pred;
5135 while (e)
5136 {
5137 if (e->dest != bb)
5138 {
5139 fprintf (stderr, "verify_flow_info: Basic block %d pred edge is corrupted\n",
5140 bb->index);
5141 fprintf (stderr, "Predecessor: ");
5142 dump_edge_info (stderr, e, 0);
5143 fprintf (stderr, "\nSuccessor: ");
5144 dump_edge_info (stderr, e, 1);
5145 fflush (stderr);
5146 abort ();
5147 }
5148 if (e->src != ENTRY_BLOCK_PTR)
5149 {
5150 edge e2 = e->src->succ;
5151 while (e2 && e2 != e)
5152 e2 = e2->succ_next;
5153 if (!e2)
5154 {
5155 fatal ("verify_flow_info: Basic block %i edge lists are corrupted\n",
5156 bb->index);
5157 }
5158 }
5159 e = e->pred_next;
5160 }
5161
5162 /* OK pointers are correct. Now check the header of basic
5163 block. It ought to contain optional CODE_LABEL followed
5164 by NOTE_BASIC_BLOCK. */
5165 x = bb->head;
5166 if (GET_CODE (x) == CODE_LABEL)
5167 {
5168 if (bb->end == x)
5169 {
5170 fatal ("verify_flow_info: Basic block contains only CODE_LABEL and no NOTE_INSN_BASIC_BLOCK note\n");
5171 }
5172 x = NEXT_INSN (x);
5173 }
5174 if (GET_CODE (x) != NOTE
5175 || NOTE_LINE_NUMBER (x) != NOTE_INSN_BASIC_BLOCK
5176 || NOTE_BASIC_BLOCK (x) != bb)
5177 {
5178 fatal ("verify_flow_info: NOTE_INSN_BASIC_BLOCK is missing for block %d\n",
5179 bb->index);
5180 }
5181
5182 if (bb->end == x)
5183 {
5184 /* Do checks for empty blocks here */
5185 }
5186 else
5187 {
5188 x = NEXT_INSN (x);
5189 while (x)
5190 {
5191 if (GET_CODE (x) == NOTE
5192 && NOTE_LINE_NUMBER (x) == NOTE_INSN_BASIC_BLOCK)
5193 {
5194 fatal ("verify_flow_info: NOTE_INSN_BASIC_BLOCK %d in the middle of basic block %d\n",
5195 INSN_UID (x), bb->index);
5196 }
5197
5198 if (x == bb->end)
5199 break;
5200
5201 if (GET_CODE (x) == JUMP_INSN
5202 || GET_CODE (x) == CODE_LABEL
5203 || GET_CODE (x) == BARRIER)
5204 {
5205 fatal_insn ("verify_flow_info: Incorrect insn in the middle of basic block %d\n",
5206 x, bb->index);
5207 }
5208
5209 x = NEXT_INSN (x);
5210 }
5211 }
5212 }
5213
5214 x = rtx_first;
5215 while (x)
5216 {
5217 if (!bb_info[INSN_UID (x)])
5218 {
5219 switch (GET_CODE (x))
5220 {
5221 case BARRIER:
5222 case NOTE:
5223 break;
5224
5225 case CODE_LABEL:
5226 /* An addr_vec is placed outside any block block. */
5227 if (NEXT_INSN (x)
5228 && GET_CODE (NEXT_INSN (x)) == JUMP_INSN
5229 && (GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_DIFF_VEC
5230 || GET_CODE (PATTERN (NEXT_INSN (x))) == ADDR_VEC))
5231 {
5232 x = NEXT_INSN (x);
5233 }
5234
5235 /* But in any case, non-deletable labels can appear anywhere. */
5236 break;
5237
5238 default:
5239 fatal_insn ("verify_flow_info: Insn outside basic block\n", x);
5240 }
5241 }
5242
5243 x = NEXT_INSN (x);
5244 }
5245}
410538ea
AM
5246\f
5247/* Functions to access an edge list with a vector representation.
5248 Enough data is kept such that given an index number, the
5249 pred and succ that edge reprsents can be determined, or
5250 given a pred and a succ, it's index number can be returned.
5251 This allows algorithms which comsume a lot of memory to
5252 represent the normally full matrix of edge (pred,succ) with a
5253 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
5254 wasted space in the client code due to sparse flow graphs. */
5255
5256/* This functions initializes the edge list. Basically the entire
5257 flowgraph is processed, and all edges are assigned a number,
5258 and the data structure is filed in. */
5259struct edge_list *
5260create_edge_list ()
5261{
5262 struct edge_list *elist;
5263 edge e;
5264 int num_edges;
5265 int x,y;
5266 int_list_ptr ptr;
5267 int block_count;
5268
5269 block_count = n_basic_blocks + 2; /* Include the entry and exit blocks. */
5270
5271 num_edges = 0;
5272
5273 /* Determine the number of edges in the flow graph by counting successor
5274 edges on each basic block. */
5275 for (x = 0; x < n_basic_blocks; x++)
5276 {
5277 basic_block bb = BASIC_BLOCK (x);
5278
5279 for (e = bb->succ; e; e = e->succ_next)
5280 num_edges++;
5281 }
5282 /* Don't forget successors of the entry block. */
5283 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
5284 num_edges++;
5285
57ad4479 5286 elist = xmalloc (sizeof (struct edge_list));
410538ea
AM
5287 elist->num_blocks = block_count;
5288 elist->num_edges = num_edges;
57ad4479 5289 elist->index_to_edge = xmalloc (sizeof (edge) * num_edges);
410538ea
AM
5290
5291 num_edges = 0;
5292
5293 /* Follow successors of the entry block, and register these edges. */
5294 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
5295 {
5296 elist->index_to_edge[num_edges] = e;
5297 num_edges++;
5298 }
5299
5300 for (x = 0; x < n_basic_blocks; x++)
5301 {
5302 basic_block bb = BASIC_BLOCK (x);
5303
5304 /* Follow all successors of blocks, and register these edges. */
5305 for (e = bb->succ; e; e = e->succ_next)
5306 {
5307 elist->index_to_edge[num_edges] = e;
5308 num_edges++;
5309 }
5310 }
5311 return elist;
5312}
5313
5314/* This function free's memory associated with an edge list. */
5315void
5316free_edge_list (elist)
5317 struct edge_list *elist;
5318{
5319 if (elist)
5320 {
5321 free (elist->index_to_edge);
5322 free (elist);
5323 }
5324}
5325
5326/* This function provides debug output showing an edge list. */
5327void
5328print_edge_list (f, elist)
5329 FILE *f;
5330 struct edge_list *elist;
5331{
5332 int x;
5333 fprintf(f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
5334 elist->num_blocks - 2, elist->num_edges);
5335
5336 for (x = 0; x < elist->num_edges; x++)
5337 {
5338 fprintf (f, " %-4d - edge(", x);
5339 if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR)
5340 fprintf (f,"entry,");
5341 else
5342 fprintf (f,"%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
5343
5344 if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR)
5345 fprintf (f,"exit)\n");
5346 else
5347 fprintf (f,"%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
5348 }
5349}
5350
5351/* This function provides an internal consistancy check of an edge list,
5352 verifying that all edges are present, and that there are no
5353 extra edges. */
5354void
5355verify_edge_list (f, elist)
5356 FILE *f;
5357 struct edge_list *elist;
5358{
5359 int x, pred, succ, index;
5360 int_list_ptr ptr;
5361 int flawed = 0;
5362 edge e;
5363
5364 for (x = 0; x < n_basic_blocks; x++)
5365 {
5366 basic_block bb = BASIC_BLOCK (x);
5367
5368 for (e = bb->succ; e; e = e->succ_next)
5369 {
5370 pred = e->src->index;
5371 succ = e->dest->index;
5372 index = EDGE_INDEX (elist, pred, succ);
5373 if (index == EDGE_INDEX_NO_EDGE)
5374 {
5375 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
5376 continue;
5377 }
5378 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
5379 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
5380 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
5381 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
5382 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
5383 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
5384 }
5385 }
5386 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
5387 {
5388 pred = e->src->index;
5389 succ = e->dest->index;
5390 index = EDGE_INDEX (elist, pred, succ);
5391 if (index == EDGE_INDEX_NO_EDGE)
5392 {
5393 fprintf (f, "*p* No index for edge from %d to %d\n",pred, succ);
5394 continue;
5395 }
5396 if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
5397 fprintf (f, "*p* Pred for index %d should be %d not %d\n",
5398 index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
5399 if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
5400 fprintf (f, "*p* Succ for index %d should be %d not %d\n",
5401 index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
5402 }
5403 /* We've verified that all the edges are in the list, no lets make sure
5404 there are no spurious edges in the list. */
5405
5406 for (pred = 0 ; pred < n_basic_blocks; pred++)
5407 for (succ = 0 ; succ < n_basic_blocks; succ++)
5408 {
5409 basic_block p = BASIC_BLOCK (pred);
5410 basic_block s = BASIC_BLOCK (succ);
5411
5412 int found_edge = 0;
5413
5414 for (e = p->succ; e; e = e->succ_next)
5415 if (e->dest == s)
5416 {
5417 found_edge = 1;
5418 break;
5419 }
5420 for (e = s->pred; e; e = e->pred_next)
5421 if (e->src == p)
5422 {
5423 found_edge = 1;
5424 break;
5425 }
5426 if (EDGE_INDEX (elist, pred, succ) == EDGE_INDEX_NO_EDGE
5427 && found_edge != 0)
5428 fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
5429 pred, succ);
5430 if (EDGE_INDEX (elist, pred, succ) != EDGE_INDEX_NO_EDGE
5431 && found_edge == 0)
5432 fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
5433 pred, succ, EDGE_INDEX (elist, pred, succ));
5434 }
5435 for (succ = 0 ; succ < n_basic_blocks; succ++)
5436 {
5437 basic_block p = ENTRY_BLOCK_PTR;
5438 basic_block s = BASIC_BLOCK (succ);
5439
5440 int found_edge = 0;
5441
5442 for (e = p->succ; e; e = e->succ_next)
5443 if (e->dest == s)
5444 {
5445 found_edge = 1;
5446 break;
5447 }
5448 for (e = s->pred; e; e = e->pred_next)
5449 if (e->src == p)
5450 {
5451 found_edge = 1;
5452 break;
5453 }
5454 if (EDGE_INDEX (elist, ENTRY_BLOCK, succ) == EDGE_INDEX_NO_EDGE
5455 && found_edge != 0)
5456 fprintf (f, "*** Edge (entry, %d) appears to not have an index\n",
5457 succ);
5458 if (EDGE_INDEX (elist, ENTRY_BLOCK, succ) != EDGE_INDEX_NO_EDGE
5459 && found_edge == 0)
5460 fprintf (f, "*** Edge (entry, %d) has index %d, but no edge exists\n",
5461 succ, EDGE_INDEX (elist, ENTRY_BLOCK, succ));
5462 }
5463 for (pred = 0 ; pred < n_basic_blocks; pred++)
5464 {
5465 basic_block p = BASIC_BLOCK (pred);
5466 basic_block s = EXIT_BLOCK_PTR;
5467
5468 int found_edge = 0;
5469
5470 for (e = p->succ; e; e = e->succ_next)
5471 if (e->dest == s)
5472 {
5473 found_edge = 1;
5474 break;
5475 }
5476 for (e = s->pred; e; e = e->pred_next)
5477 if (e->src == p)
5478 {
5479 found_edge = 1;
5480 break;
5481 }
5482 if (EDGE_INDEX (elist, pred, EXIT_BLOCK) == EDGE_INDEX_NO_EDGE
5483 && found_edge != 0)
5484 fprintf (f, "*** Edge (%d, exit) appears to not have an index\n",
5485 pred);
5486 if (EDGE_INDEX (elist, pred, EXIT_BLOCK) != EDGE_INDEX_NO_EDGE
5487 && found_edge == 0)
5488 fprintf (f, "*** Edge (%d, exit) has index %d, but no edge exists\n",
5489 pred, EDGE_INDEX (elist, pred, EXIT_BLOCK));
5490 }
5491}
5492
5493/* This routine will determine what, if any, edge there is between
5494 a specified predecessor and successor. */
5495
5496int
5497find_edge_index (edge_list, pred, succ)
5498 struct edge_list *edge_list;
5499 int pred, succ;
5500{
5501 int x;
5502 for (x = 0; x < NUM_EDGES (edge_list); x++)
5503 {
5504 if (INDEX_EDGE_PRED_BB (edge_list, x)->index == pred
5505 && INDEX_EDGE_SUCC_BB (edge_list, x)->index == succ)
5506 return x;
5507 }
5508 return (EDGE_INDEX_NO_EDGE);
5509}
5510
This page took 1.332535 seconds and 5 git commands to generate.