1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This is the jump-optimization pass of the compiler.
24 It is run two or three times: once before cse, sometimes once after cse,
25 and once after reload (before final).
27 jump_optimize deletes unreachable code and labels that are not used.
28 It also deletes jumps that jump to the following insn,
29 and simplifies jumps around unconditional jumps and jumps
30 to unconditional jumps.
32 Each CODE_LABEL has a count of the times it is used
33 stored in the LABEL_NUSES internal field, and each JUMP_INSN
34 has one label that it refers to stored in the
35 JUMP_LABEL internal field. With this we can detect labels that
36 become unused because of the deletion of all the jumps that
37 formerly used them. The JUMP_LABEL info is sometimes looked
40 Optionally, cross-jumping can be done. Currently it is done
41 only the last time (when after reload and before final).
42 In fact, the code for cross-jumping now assumes that register
43 allocation has been done, since it uses `rtx_renumbered_equal_p'.
45 Jump optimization is done after cse when cse's constant-propagation
46 causes jumps to become unconditional or to be deleted.
48 Unreachable loops are not detected here, because the labels
49 have references and the insns appear reachable from the labels.
50 find_basic_blocks in flow.c finds and deletes such loops.
52 The subroutines delete_insn, redirect_jump, and invert_jump are used
53 from other passes as well. */
60 #include "hard-reg-set.h"
62 #include "insn-config.h"
63 #include "insn-flags.h"
64 #include "insn-attr.h"
72 /* ??? Eventually must record somehow the labels used by jumps
73 from nested functions. */
74 /* Pre-record the next or previous real insn for each label?
75 No, this pass is very fast anyway. */
76 /* Condense consecutive labels?
77 This would make life analysis faster, maybe. */
78 /* Optimize jump y; x: ... y: jumpif... x?
79 Don't know if it is worth bothering with. */
80 /* Optimize two cases of conditional jump to conditional jump?
81 This can never delete any instruction or make anything dead,
82 or even change what is live at any point.
83 So perhaps let combiner do it. */
85 /* Vector indexed by uid.
86 For each CODE_LABEL, index by its uid to get first unconditional jump
87 that jumps to the label.
88 For each JUMP_INSN, index by its uid to get the next unconditional jump
89 that jumps to the same label.
90 Element 0 is the start of a chain of all return insns.
91 (It is safe to use element 0 because insn uid 0 is not used. */
93 static rtx *jump_chain;
95 /* Maximum index in jump_chain. */
97 static int max_jump_chain;
99 /* Set nonzero by jump_optimize if control can fall through
100 to the end of the function. */
103 /* Indicates whether death notes are significant in cross jump analysis.
104 Normally they are not significant, because of A and B jump to C,
105 and R dies in A, it must die in B. But this might not be true after
106 stack register conversion, and we must compare death notes in that
109 static int cross_jump_death_matters = 0;
111 static int init_label_info PARAMS ((rtx));
112 static void delete_barrier_successors PARAMS ((rtx));
113 static void mark_all_labels PARAMS ((rtx, int));
114 static rtx delete_unreferenced_labels PARAMS ((rtx));
115 static void delete_noop_moves PARAMS ((rtx));
116 static int calculate_can_reach_end PARAMS ((rtx, int));
117 static int duplicate_loop_exit_test PARAMS ((rtx));
118 static void find_cross_jump PARAMS ((rtx, rtx, int, rtx *, rtx *));
119 static void do_cross_jump PARAMS ((rtx, rtx, rtx));
120 static int jump_back_p PARAMS ((rtx, rtx));
121 static int tension_vector_labels PARAMS ((rtx, int));
122 static void mark_jump_label PARAMS ((rtx, rtx, int, int));
123 static void delete_computation PARAMS ((rtx));
124 static void redirect_exp_1 PARAMS ((rtx *, rtx, rtx, rtx));
125 static int redirect_exp PARAMS ((rtx, rtx, rtx));
126 static void invert_exp_1 PARAMS ((rtx));
127 static int invert_exp PARAMS ((rtx));
128 static void delete_from_jump_chain PARAMS ((rtx));
129 static int delete_labelref_insn PARAMS ((rtx, rtx, int));
130 static void mark_modified_reg PARAMS ((rtx, rtx, void *));
131 static void redirect_tablejump PARAMS ((rtx, rtx));
132 static void jump_optimize_1 PARAMS ((rtx, int, int, int, int, int));
133 static int returnjump_p_1 PARAMS ((rtx *, void *));
134 static void delete_prior_computation PARAMS ((rtx, rtx));
136 /* Main external entry point into the jump optimizer. See comments before
137 jump_optimize_1 for descriptions of the arguments. */
139 jump_optimize (f, cross_jump, noop_moves, after_regscan)
145 jump_optimize_1 (f, cross_jump, noop_moves, after_regscan, 0, 0);
148 /* Alternate entry into the jump optimizer. This entry point only rebuilds
149 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
152 rebuild_jump_labels (f)
155 jump_optimize_1 (f, 0, 0, 0, 1, 0);
158 /* Alternate entry into the jump optimizer. Do only trivial optimizations. */
160 jump_optimize_minimal (f)
163 jump_optimize_1 (f, 0, 0, 0, 0, 1);
166 /* Delete no-op jumps and optimize jumps to jumps
167 and jumps around jumps.
168 Delete unused labels and unreachable code.
170 If CROSS_JUMP is 1, detect matching code
171 before a jump and its destination and unify them.
172 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
174 If NOOP_MOVES is nonzero, delete no-op move insns.
176 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
177 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
179 If MARK_LABELS_ONLY is nonzero, then we only rebuild the jump chain
180 and JUMP_LABEL field for jumping insns.
182 If `optimize' is zero, don't change any code,
183 just determine whether control drops off the end of the function.
184 This case occurs when we have -W and not -O.
185 It works because `delete_insn' checks the value of `optimize'
186 and refrains from actually deleting when that is 0.
188 If MINIMAL is nonzero, then we only perform trivial optimizations:
190 * Removal of unreachable code after BARRIERs.
191 * Removal of unreferenced CODE_LABELs.
192 * Removal of a jump to the next instruction.
193 * Removal of a conditional jump followed by an unconditional jump
194 to the same target as the conditional jump.
195 * Simplify a conditional jump around an unconditional jump.
196 * Simplify a jump to a jump.
197 * Delete extraneous line number notes.
201 jump_optimize_1 (f, cross_jump, noop_moves, after_regscan,
202 mark_labels_only, minimal)
207 int mark_labels_only;
210 register rtx insn, next;
217 cross_jump_death_matters = (cross_jump == 2);
218 max_uid = init_label_info (f) + 1;
220 /* If we are performing cross jump optimizations, then initialize
221 tables mapping UIDs to EH regions to avoid incorrect movement
222 of insns from one EH region to another. */
223 if (flag_exceptions && cross_jump)
224 init_insn_eh_region (f, max_uid);
226 if (! mark_labels_only)
227 delete_barrier_successors (f);
229 /* Leave some extra room for labels and duplicate exit test insns
231 max_jump_chain = max_uid * 14 / 10;
232 jump_chain = (rtx *) xcalloc (max_jump_chain, sizeof (rtx));
234 mark_all_labels (f, cross_jump);
236 /* Keep track of labels used from static data; we don't track them
237 closely enough to delete them here, so make sure their reference
238 count doesn't drop to zero. */
240 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
241 if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
242 LABEL_NUSES (XEXP (insn, 0))++;
244 check_exception_handler_labels ();
246 /* Keep track of labels used for marking handlers for exception
247 regions; they cannot usually be deleted. */
249 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
250 if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
251 LABEL_NUSES (XEXP (insn, 0))++;
253 /* Quit now if we just wanted to rebuild the JUMP_LABEL and REG_LABEL
254 notes and recompute LABEL_NUSES. */
255 if (mark_labels_only)
259 exception_optimize ();
261 last_insn = delete_unreferenced_labels (f);
264 delete_noop_moves (f);
266 /* If we haven't yet gotten to reload and we have just run regscan,
267 delete any insn that sets a register that isn't used elsewhere.
268 This helps some of the optimizations below by having less insns
269 being jumped around. */
271 if (optimize && ! reload_completed && after_regscan)
272 for (insn = f; insn; insn = next)
274 rtx set = single_set (insn);
276 next = NEXT_INSN (insn);
278 if (set && GET_CODE (SET_DEST (set)) == REG
279 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
280 && REGNO_FIRST_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
281 /* We use regno_last_note_uid so as not to delete the setting
282 of a reg that's used in notes. A subsequent optimization
283 might arrange to use that reg for real. */
284 && REGNO_LAST_NOTE_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
285 && ! side_effects_p (SET_SRC (set))
286 && ! find_reg_note (insn, REG_RETVAL, 0)
287 /* An ADDRESSOF expression can turn into a use of the internal arg
288 pointer, so do not delete the initialization of the internal
289 arg pointer yet. If it is truly dead, flow will delete the
290 initializing insn. */
291 && SET_DEST (set) != current_function_internal_arg_pointer)
295 /* Now iterate optimizing jumps until nothing changes over one pass. */
297 old_max_reg = max_reg_num ();
302 for (insn = f; insn; insn = next)
305 rtx temp, temp1, temp2 = NULL_RTX;
306 rtx temp4 ATTRIBUTE_UNUSED;
308 int this_is_any_uncondjump;
309 int this_is_any_condjump;
310 int this_is_onlyjump;
312 next = NEXT_INSN (insn);
314 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
315 jump. Try to optimize by duplicating the loop exit test if so.
316 This is only safe immediately after regscan, because it uses
317 the values of regno_first_uid and regno_last_uid. */
318 if (after_regscan && GET_CODE (insn) == NOTE
319 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
320 && (temp1 = next_nonnote_insn (insn)) != 0
321 && any_uncondjump_p (temp1)
322 && onlyjump_p (temp1))
324 temp = PREV_INSN (insn);
325 if (duplicate_loop_exit_test (insn))
328 next = NEXT_INSN (temp);
333 if (GET_CODE (insn) != JUMP_INSN)
336 this_is_any_condjump = any_condjump_p (insn);
337 this_is_any_uncondjump = any_uncondjump_p (insn);
338 this_is_onlyjump = onlyjump_p (insn);
340 /* Tension the labels in dispatch tables. */
342 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
343 changed |= tension_vector_labels (PATTERN (insn), 0);
344 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
345 changed |= tension_vector_labels (PATTERN (insn), 1);
347 /* See if this jump goes to another jump and redirect if so. */
348 nlabel = follow_jumps (JUMP_LABEL (insn));
349 if (nlabel != JUMP_LABEL (insn))
350 changed |= redirect_jump (insn, nlabel, 1);
352 if (! optimize || minimal)
355 /* If a dispatch table always goes to the same place,
356 get rid of it and replace the insn that uses it. */
358 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
359 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
362 rtx pat = PATTERN (insn);
363 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
364 int len = XVECLEN (pat, diff_vec_p);
365 rtx dispatch = prev_real_insn (insn);
368 for (i = 0; i < len; i++)
369 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
370 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
375 && GET_CODE (dispatch) == JUMP_INSN
376 && JUMP_LABEL (dispatch) != 0
377 /* Don't mess with a casesi insn.
378 XXX according to the comment before computed_jump_p(),
379 all casesi insns should be a parallel of the jump
380 and a USE of a LABEL_REF. */
381 && ! ((set = single_set (dispatch)) != NULL
382 && (GET_CODE (SET_SRC (set)) == IF_THEN_ELSE))
383 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
385 redirect_tablejump (dispatch,
386 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
391 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
393 /* Detect jump to following insn. */
394 if (reallabelprev == insn
395 && (this_is_any_condjump || this_is_any_uncondjump)
398 next = next_real_insn (JUMP_LABEL (insn));
401 /* Remove the "inactive" but "real" insns (i.e. uses and
402 clobbers) in between here and there. */
404 while ((temp = next_real_insn (temp)) != next)
411 /* Detect a conditional jump going to the same place
412 as an immediately following unconditional jump. */
413 else if (this_is_any_condjump && this_is_onlyjump
414 && (temp = next_active_insn (insn)) != 0
415 && simplejump_p (temp)
416 && (next_active_insn (JUMP_LABEL (insn))
417 == next_active_insn (JUMP_LABEL (temp))))
419 /* Don't mess up test coverage analysis. */
421 if (flag_test_coverage && !reload_completed)
422 for (temp2 = insn; temp2 != temp; temp2 = NEXT_INSN (temp2))
423 if (GET_CODE (temp2) == NOTE && NOTE_LINE_NUMBER (temp2) > 0)
434 /* Detect a conditional jump jumping over an unconditional jump. */
436 else if (this_is_any_condjump
437 && reallabelprev != 0
438 && GET_CODE (reallabelprev) == JUMP_INSN
439 && prev_active_insn (reallabelprev) == insn
440 && no_labels_between_p (insn, reallabelprev)
441 && any_uncondjump_p (reallabelprev)
442 && onlyjump_p (reallabelprev))
444 /* When we invert the unconditional jump, we will be
445 decrementing the usage count of its old label.
446 Make sure that we don't delete it now because that
447 might cause the following code to be deleted. */
448 rtx prev_uses = prev_nonnote_insn (reallabelprev);
449 rtx prev_label = JUMP_LABEL (insn);
452 ++LABEL_NUSES (prev_label);
454 if (invert_jump (insn, JUMP_LABEL (reallabelprev), 1))
456 /* It is very likely that if there are USE insns before
457 this jump, they hold REG_DEAD notes. These REG_DEAD
458 notes are no longer valid due to this optimization,
459 and will cause the life-analysis that following passes
460 (notably delayed-branch scheduling) to think that
461 these registers are dead when they are not.
463 To prevent this trouble, we just remove the USE insns
464 from the insn chain. */
466 while (prev_uses && GET_CODE (prev_uses) == INSN
467 && GET_CODE (PATTERN (prev_uses)) == USE)
469 rtx useless = prev_uses;
470 prev_uses = prev_nonnote_insn (prev_uses);
471 delete_insn (useless);
474 delete_insn (reallabelprev);
478 /* We can now safely delete the label if it is unreferenced
479 since the delete_insn above has deleted the BARRIER. */
480 if (prev_label && --LABEL_NUSES (prev_label) == 0)
481 delete_insn (prev_label);
483 next = NEXT_INSN (insn);
486 /* If we have an unconditional jump preceded by a USE, try to put
487 the USE before the target and jump there. This simplifies many
488 of the optimizations below since we don't have to worry about
489 dealing with these USE insns. We only do this if the label
490 being branch to already has the identical USE or if code
491 never falls through to that label. */
493 else if (this_is_any_uncondjump
494 && (temp = prev_nonnote_insn (insn)) != 0
495 && GET_CODE (temp) == INSN
496 && GET_CODE (PATTERN (temp)) == USE
497 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
498 && (GET_CODE (temp1) == BARRIER
499 || (GET_CODE (temp1) == INSN
500 && rtx_equal_p (PATTERN (temp), PATTERN (temp1))))
501 /* Don't do this optimization if we have a loop containing
502 only the USE instruction, and the loop start label has
503 a usage count of 1. This is because we will redo this
504 optimization everytime through the outer loop, and jump
505 opt will never exit. */
506 && ! ((temp2 = prev_nonnote_insn (temp)) != 0
507 && temp2 == JUMP_LABEL (insn)
508 && LABEL_NUSES (temp2) == 1))
510 if (GET_CODE (temp1) == BARRIER)
512 emit_insn_after (PATTERN (temp), temp1);
513 temp1 = NEXT_INSN (temp1);
517 redirect_jump (insn, get_label_before (temp1), 1);
518 reallabelprev = prev_real_insn (temp1);
520 next = NEXT_INSN (insn);
524 /* Detect a conditional jump jumping over an unconditional trap. */
526 && this_is_any_condjump && this_is_onlyjump
527 && reallabelprev != 0
528 && GET_CODE (reallabelprev) == INSN
529 && GET_CODE (PATTERN (reallabelprev)) == TRAP_IF
530 && TRAP_CONDITION (PATTERN (reallabelprev)) == const_true_rtx
531 && prev_active_insn (reallabelprev) == insn
532 && no_labels_between_p (insn, reallabelprev)
533 && (temp2 = get_condition (insn, &temp4))
534 && can_reverse_comparison_p (temp2, insn))
536 rtx new = gen_cond_trap (reverse_condition (GET_CODE (temp2)),
537 XEXP (temp2, 0), XEXP (temp2, 1),
538 TRAP_CODE (PATTERN (reallabelprev)));
542 emit_insn_before (new, temp4);
543 delete_insn (reallabelprev);
549 /* Detect a jump jumping to an unconditional trap. */
550 else if (HAVE_trap && this_is_onlyjump
551 && (temp = next_active_insn (JUMP_LABEL (insn)))
552 && GET_CODE (temp) == INSN
553 && GET_CODE (PATTERN (temp)) == TRAP_IF
554 && (this_is_any_uncondjump
555 || (this_is_any_condjump
556 && (temp2 = get_condition (insn, &temp4)))))
558 rtx tc = TRAP_CONDITION (PATTERN (temp));
560 if (tc == const_true_rtx
561 || (! this_is_any_uncondjump && rtx_equal_p (temp2, tc)))
564 /* Replace an unconditional jump to a trap with a trap. */
565 if (this_is_any_uncondjump)
567 emit_barrier_after (emit_insn_before (gen_trap (), insn));
572 new = gen_cond_trap (GET_CODE (temp2), XEXP (temp2, 0),
574 TRAP_CODE (PATTERN (temp)));
577 emit_insn_before (new, temp4);
583 /* If the trap condition and jump condition are mutually
584 exclusive, redirect the jump to the following insn. */
585 else if (GET_RTX_CLASS (GET_CODE (tc)) == '<'
586 && this_is_any_condjump
587 && swap_condition (GET_CODE (temp2)) == GET_CODE (tc)
588 && rtx_equal_p (XEXP (tc, 0), XEXP (temp2, 0))
589 && rtx_equal_p (XEXP (tc, 1), XEXP (temp2, 1))
590 && redirect_jump (insn, get_label_after (temp), 1))
599 /* Now that the jump has been tensioned,
600 try cross jumping: check for identical code
601 before the jump and before its target label. */
603 /* First, cross jumping of conditional jumps: */
605 if (cross_jump && condjump_p (insn))
607 rtx newjpos, newlpos;
608 rtx x = prev_real_insn (JUMP_LABEL (insn));
610 /* A conditional jump may be crossjumped
611 only if the place it jumps to follows
612 an opposing jump that comes back here. */
614 if (x != 0 && ! jump_back_p (x, insn))
615 /* We have no opposing jump;
616 cannot cross jump this insn. */
620 /* TARGET is nonzero if it is ok to cross jump
621 to code before TARGET. If so, see if matches. */
623 find_cross_jump (insn, x,
624 (optimize_size ? 1 : BRANCH_COST) + 1,
629 do_cross_jump (insn, newjpos, newlpos);
630 /* Make the old conditional jump
631 into an unconditional one. */
632 SET_SRC (PATTERN (insn))
633 = gen_rtx_LABEL_REF (VOIDmode, JUMP_LABEL (insn));
634 INSN_CODE (insn) = -1;
635 emit_barrier_after (insn);
636 /* Add to jump_chain unless this is a new label
637 whose UID is too large. */
638 if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
640 jump_chain[INSN_UID (insn)]
641 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
642 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
649 /* Cross jumping of unconditional jumps:
650 a few differences. */
652 if (cross_jump && simplejump_p (insn))
654 rtx newjpos, newlpos;
659 /* TARGET is nonzero if it is ok to cross jump
660 to code before TARGET. If so, see if matches. */
661 find_cross_jump (insn, JUMP_LABEL (insn),
662 optimize_size ? 1 : BRANCH_COST,
665 /* If cannot cross jump to code before the label,
666 see if we can cross jump to another jump to
668 /* Try each other jump to this label. */
669 if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
670 for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
671 target != 0 && newjpos == 0;
672 target = jump_chain[INSN_UID (target)])
674 && JUMP_LABEL (target) == JUMP_LABEL (insn)
675 /* Ignore TARGET if it's deleted. */
676 && ! INSN_DELETED_P (target))
677 find_cross_jump (insn, target,
678 (optimize_size ? 1 : BRANCH_COST) + 1,
683 do_cross_jump (insn, newjpos, newlpos);
689 /* This code was dead in the previous jump.c! */
690 if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
692 /* Return insns all "jump to the same place"
693 so we can cross-jump between any two of them. */
695 rtx newjpos, newlpos, target;
699 /* If cannot cross jump to code before the label,
700 see if we can cross jump to another jump to
702 /* Try each other jump to this label. */
703 for (target = jump_chain[0];
704 target != 0 && newjpos == 0;
705 target = jump_chain[INSN_UID (target)])
707 && ! INSN_DELETED_P (target)
708 && GET_CODE (PATTERN (target)) == RETURN)
709 find_cross_jump (insn, target,
710 (optimize_size ? 1 : BRANCH_COST) + 1,
715 do_cross_jump (insn, newjpos, newlpos);
726 /* Delete extraneous line number notes.
727 Note that two consecutive notes for different lines are not really
728 extraneous. There should be some indication where that line belonged,
729 even if it became empty. */
734 for (insn = f; insn; insn = NEXT_INSN (insn))
735 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
737 /* Delete this note if it is identical to previous note. */
739 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
740 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
750 /* CAN_REACH_END is persistent for each function. Once set it should
751 not be cleared. This is especially true for the case where we
752 delete the NOTE_FUNCTION_END note. CAN_REACH_END is cleared by
753 the front-end before compiling each function. */
754 if (! minimal && calculate_can_reach_end (last_insn, optimize != 0))
763 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
764 notes whose labels don't occur in the insn any more. Returns the
765 largest INSN_UID found. */
773 for (insn = f; insn; insn = NEXT_INSN (insn))
775 if (GET_CODE (insn) == CODE_LABEL)
776 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
777 else if (GET_CODE (insn) == JUMP_INSN)
778 JUMP_LABEL (insn) = 0;
779 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
783 for (note = REG_NOTES (insn); note; note = next)
785 next = XEXP (note, 1);
786 if (REG_NOTE_KIND (note) == REG_LABEL
787 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
788 remove_note (insn, note);
791 if (INSN_UID (insn) > largest_uid)
792 largest_uid = INSN_UID (insn);
798 /* Delete insns following barriers, up to next label.
800 Also delete no-op jumps created by gcse. */
803 delete_barrier_successors (f)
809 for (insn = f; insn;)
811 if (GET_CODE (insn) == BARRIER)
813 insn = NEXT_INSN (insn);
815 never_reached_warning (insn);
817 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
819 if (GET_CODE (insn) == NOTE
820 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
821 insn = NEXT_INSN (insn);
823 insn = delete_insn (insn);
825 /* INSN is now the code_label. */
828 /* Also remove (set (pc) (pc)) insns which can be created by
829 gcse. We eliminate such insns now to avoid having them
830 cause problems later. */
831 else if (GET_CODE (insn) == JUMP_INSN
832 && (set = pc_set (insn)) != NULL
833 && SET_SRC (set) == pc_rtx
834 && SET_DEST (set) == pc_rtx
835 && onlyjump_p (insn))
836 insn = delete_insn (insn);
839 insn = NEXT_INSN (insn);
843 /* Mark the label each jump jumps to.
844 Combine consecutive labels, and count uses of labels.
846 For each label, make a chain (using `jump_chain')
847 of all the *unconditional* jumps that jump to it;
848 also make a chain of all returns.
850 CROSS_JUMP indicates whether we are doing cross jumping
851 and if we are whether we will be paying attention to
852 death notes or not. */
855 mark_all_labels (f, cross_jump)
861 for (insn = f; insn; insn = NEXT_INSN (insn))
862 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
864 if (GET_CODE (insn) == CALL_INSN
865 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
867 mark_all_labels (XEXP (PATTERN (insn), 0), cross_jump);
868 mark_all_labels (XEXP (PATTERN (insn), 1), cross_jump);
869 mark_all_labels (XEXP (PATTERN (insn), 2), cross_jump);
873 mark_jump_label (PATTERN (insn), insn, cross_jump, 0);
874 if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
876 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
878 jump_chain[INSN_UID (insn)]
879 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
880 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
882 if (GET_CODE (PATTERN (insn)) == RETURN)
884 jump_chain[INSN_UID (insn)] = jump_chain[0];
885 jump_chain[0] = insn;
891 /* Delete all labels already not referenced.
892 Also find and return the last insn. */
895 delete_unreferenced_labels (f)
898 rtx final = NULL_RTX;
901 for (insn = f; insn; )
903 if (GET_CODE (insn) == CODE_LABEL
904 && LABEL_NUSES (insn) == 0
905 && LABEL_ALTERNATE_NAME (insn) == NULL)
906 insn = delete_insn (insn);
910 insn = NEXT_INSN (insn);
917 /* Delete various simple forms of moves which have no necessary
921 delete_noop_moves (f)
926 for (insn = f; insn; )
928 next = NEXT_INSN (insn);
930 if (GET_CODE (insn) == INSN)
932 register rtx body = PATTERN (insn);
934 /* Detect and delete no-op move instructions
935 resulting from not allocating a parameter in a register. */
937 if (GET_CODE (body) == SET
938 && (SET_DEST (body) == SET_SRC (body)
939 || (GET_CODE (SET_DEST (body)) == MEM
940 && GET_CODE (SET_SRC (body)) == MEM
941 && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
942 && ! (GET_CODE (SET_DEST (body)) == MEM
943 && MEM_VOLATILE_P (SET_DEST (body)))
944 && ! (GET_CODE (SET_SRC (body)) == MEM
945 && MEM_VOLATILE_P (SET_SRC (body))))
946 delete_computation (insn);
948 /* Detect and ignore no-op move instructions
949 resulting from smart or fortuitous register allocation. */
951 else if (GET_CODE (body) == SET)
953 int sreg = true_regnum (SET_SRC (body));
954 int dreg = true_regnum (SET_DEST (body));
956 if (sreg == dreg && sreg >= 0)
958 else if (sreg >= 0 && dreg >= 0)
961 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
962 sreg, NULL_PTR, dreg,
963 GET_MODE (SET_SRC (body)));
966 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
968 /* DREG may have been the target of a REG_DEAD note in
969 the insn which makes INSN redundant. If so, reorg
970 would still think it is dead. So search for such a
971 note and delete it if we find it. */
972 if (! find_regno_note (insn, REG_UNUSED, dreg))
973 for (trial = prev_nonnote_insn (insn);
974 trial && GET_CODE (trial) != CODE_LABEL;
975 trial = prev_nonnote_insn (trial))
976 if (find_regno_note (trial, REG_DEAD, dreg))
978 remove_death (dreg, trial);
982 /* Deleting insn could lose a death-note for SREG. */
983 if ((trial = find_regno_note (insn, REG_DEAD, sreg)))
985 /* Change this into a USE so that we won't emit
986 code for it, but still can keep the note. */
988 = gen_rtx_USE (VOIDmode, XEXP (trial, 0));
989 INSN_CODE (insn) = -1;
990 /* Remove all reg notes but the REG_DEAD one. */
991 REG_NOTES (insn) = trial;
992 XEXP (trial, 1) = NULL_RTX;
998 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
999 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
1001 GET_MODE (SET_DEST (body))))
1003 /* This handles the case where we have two consecutive
1004 assignments of the same constant to pseudos that didn't
1005 get a hard reg. Each SET from the constant will be
1006 converted into a SET of the spill register and an
1007 output reload will be made following it. This produces
1008 two loads of the same constant into the same spill
1013 /* Look back for a death note for the first reg.
1014 If there is one, it is no longer accurate. */
1015 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
1017 if ((GET_CODE (in_insn) == INSN
1018 || GET_CODE (in_insn) == JUMP_INSN)
1019 && find_regno_note (in_insn, REG_DEAD, dreg))
1021 remove_death (dreg, in_insn);
1024 in_insn = PREV_INSN (in_insn);
1027 /* Delete the second load of the value. */
1031 else if (GET_CODE (body) == PARALLEL)
1033 /* If each part is a set between two identical registers or
1034 a USE or CLOBBER, delete the insn. */
1038 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1040 tem = XVECEXP (body, 0, i);
1041 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
1044 if (GET_CODE (tem) != SET
1045 || (sreg = true_regnum (SET_SRC (tem))) < 0
1046 || (dreg = true_regnum (SET_DEST (tem))) < 0
1054 /* Also delete insns to store bit fields if they are no-ops. */
1055 /* Not worth the hair to detect this in the big-endian case. */
1056 else if (! BYTES_BIG_ENDIAN
1057 && GET_CODE (body) == SET
1058 && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
1059 && XEXP (SET_DEST (body), 2) == const0_rtx
1060 && XEXP (SET_DEST (body), 0) == SET_SRC (body)
1061 && ! (GET_CODE (SET_SRC (body)) == MEM
1062 && MEM_VOLATILE_P (SET_SRC (body))))
1069 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
1070 If so indicate that this function can drop off the end by returning
1073 CHECK_DELETED indicates whether we must check if the note being
1074 searched for has the deleted flag set.
1076 DELETE_FINAL_NOTE indicates whether we should delete the note
1080 calculate_can_reach_end (last, delete_final_note)
1082 int delete_final_note;
1087 while (insn != NULL_RTX)
1091 /* One label can follow the end-note: the return label. */
1092 if (GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
1094 /* Ordinary insns can follow it if returning a structure. */
1095 else if (GET_CODE (insn) == INSN)
1097 /* If machine uses explicit RETURN insns, no epilogue,
1098 then one of them follows the note. */
1099 else if (GET_CODE (insn) == JUMP_INSN
1100 && GET_CODE (PATTERN (insn)) == RETURN)
1102 /* A barrier can follow the return insn. */
1103 else if (GET_CODE (insn) == BARRIER)
1105 /* Other kinds of notes can follow also. */
1106 else if (GET_CODE (insn) == NOTE
1107 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
1113 insn = PREV_INSN (insn);
1116 /* See if we backed up to the appropriate type of note. */
1117 if (insn != NULL_RTX
1118 && GET_CODE (insn) == NOTE
1119 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
1121 if (delete_final_note)
1129 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
1130 jump. Assume that this unconditional jump is to the exit test code. If
1131 the code is sufficiently simple, make a copy of it before INSN,
1132 followed by a jump to the exit of the loop. Then delete the unconditional
1135 Return 1 if we made the change, else 0.
1137 This is only safe immediately after a regscan pass because it uses the
1138 values of regno_first_uid and regno_last_uid. */
1141 duplicate_loop_exit_test (loop_start)
1144 rtx insn, set, reg, p, link;
1145 rtx copy = 0, first_copy = 0;
1147 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
1149 int max_reg = max_reg_num ();
1152 /* Scan the exit code. We do not perform this optimization if any insn:
1156 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
1157 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
1158 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
1161 We also do not do this if we find an insn with ASM_OPERANDS. While
1162 this restriction should not be necessary, copying an insn with
1163 ASM_OPERANDS can confuse asm_noperands in some cases.
1165 Also, don't do this if the exit code is more than 20 insns. */
1167 for (insn = exitcode;
1169 && ! (GET_CODE (insn) == NOTE
1170 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
1171 insn = NEXT_INSN (insn))
1173 switch (GET_CODE (insn))
1179 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
1180 a jump immediately after the loop start that branches outside
1181 the loop but within an outer loop, near the exit test.
1182 If we copied this exit test and created a phony
1183 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
1184 before the exit test look like these could be safely moved
1185 out of the loop even if they actually may be never executed.
1186 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
1188 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1189 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
1193 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
1194 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
1195 /* If we were to duplicate this code, we would not move
1196 the BLOCK notes, and so debugging the moved code would
1197 be difficult. Thus, we only move the code with -O2 or
1204 /* The code below would grossly mishandle REG_WAS_0 notes,
1205 so get rid of them here. */
1206 while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
1207 remove_note (insn, p);
1208 if (++num_insns > 20
1209 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1210 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
1218 /* Unless INSN is zero, we can do the optimization. */
1224 /* See if any insn sets a register only used in the loop exit code and
1225 not a user variable. If so, replace it with a new register. */
1226 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
1227 if (GET_CODE (insn) == INSN
1228 && (set = single_set (insn)) != 0
1229 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
1230 || (GET_CODE (reg) == SUBREG
1231 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
1232 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1233 && REGNO_FIRST_UID (REGNO (reg)) == INSN_UID (insn))
1235 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
1236 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (p))
1241 /* We can do the replacement. Allocate reg_map if this is the
1242 first replacement we found. */
1244 reg_map = (rtx *) xcalloc (max_reg, sizeof (rtx));
1246 REG_LOOP_TEST_P (reg) = 1;
1248 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
1252 /* Now copy each insn. */
1253 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
1255 switch (GET_CODE (insn))
1258 copy = emit_barrier_before (loop_start);
1261 /* Only copy line-number notes. */
1262 if (NOTE_LINE_NUMBER (insn) >= 0)
1264 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
1265 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
1270 copy = emit_insn_before (copy_insn (PATTERN (insn)), loop_start);
1272 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
1274 mark_jump_label (PATTERN (copy), copy, 0, 0);
1276 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
1278 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1279 if (REG_NOTE_KIND (link) != REG_LABEL)
1281 if (GET_CODE (link) == EXPR_LIST)
1283 = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
1288 = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link),
1293 if (reg_map && REG_NOTES (copy))
1294 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
1298 copy = emit_jump_insn_before (copy_insn (PATTERN (insn)), loop_start);
1300 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
1301 mark_jump_label (PATTERN (copy), copy, 0, 0);
1302 if (REG_NOTES (insn))
1304 REG_NOTES (copy) = copy_insn_1 (REG_NOTES (insn));
1306 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
1309 /* If this is a simple jump, add it to the jump chain. */
1311 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
1312 && simplejump_p (copy))
1314 jump_chain[INSN_UID (copy)]
1315 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
1316 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
1324 /* Record the first insn we copied. We need it so that we can
1325 scan the copied insns for new pseudo registers. */
1330 /* Now clean up by emitting a jump to the end label and deleting the jump
1331 at the start of the loop. */
1332 if (! copy || GET_CODE (copy) != BARRIER)
1334 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
1337 /* Record the first insn we copied. We need it so that we can
1338 scan the copied insns for new pseudo registers. This may not
1339 be strictly necessary since we should have copied at least one
1340 insn above. But I am going to be safe. */
1344 mark_jump_label (PATTERN (copy), copy, 0, 0);
1345 if (INSN_UID (copy) < max_jump_chain
1346 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
1348 jump_chain[INSN_UID (copy)]
1349 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
1350 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
1352 emit_barrier_before (loop_start);
1355 /* Now scan from the first insn we copied to the last insn we copied
1356 (copy) for new pseudo registers. Do this after the code to jump to
1357 the end label since that might create a new pseudo too. */
1358 reg_scan_update (first_copy, copy, max_reg);
1360 /* Mark the exit code as the virtual top of the converted loop. */
1361 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
1363 delete_insn (next_nonnote_insn (loop_start));
1372 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
1373 eh-beg, eh-end notes between START and END out before START. Assume that
1374 END is not such a note. START may be such a note. Returns the value
1375 of the new starting insn, which may be different if the original start
1379 squeeze_notes (start, end)
1385 for (insn = start; insn != end; insn = next)
1387 next = NEXT_INSN (insn);
1388 if (GET_CODE (insn) == NOTE
1389 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
1390 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
1391 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1392 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1393 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
1394 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP
1395 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1396 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1402 rtx prev = PREV_INSN (insn);
1403 PREV_INSN (insn) = PREV_INSN (start);
1404 NEXT_INSN (insn) = start;
1405 NEXT_INSN (PREV_INSN (insn)) = insn;
1406 PREV_INSN (NEXT_INSN (insn)) = insn;
1407 NEXT_INSN (prev) = next;
1408 PREV_INSN (next) = prev;
1416 /* Compare the instructions before insn E1 with those before E2
1417 to find an opportunity for cross jumping.
1418 (This means detecting identical sequences of insns followed by
1419 jumps to the same place, or followed by a label and a jump
1420 to that label, and replacing one with a jump to the other.)
1422 Assume E1 is a jump that jumps to label E2
1423 (that is not always true but it might as well be).
1424 Find the longest possible equivalent sequences
1425 and store the first insns of those sequences into *F1 and *F2.
1426 Store zero there if no equivalent preceding instructions are found.
1428 We give up if we find a label in stream 1.
1429 Actually we could transfer that label into stream 2. */
1432 find_cross_jump (e1, e2, minimum, f1, f2)
1437 register rtx i1 = e1, i2 = e2;
1438 register rtx p1, p2;
1441 rtx last1 = 0, last2 = 0;
1442 rtx afterlast1 = 0, afterlast2 = 0;
1449 i1 = prev_nonnote_insn (i1);
1451 i2 = PREV_INSN (i2);
1452 while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
1453 i2 = PREV_INSN (i2);
1458 /* Don't allow the range of insns preceding E1 or E2
1459 to include the other (E2 or E1). */
1460 if (i2 == e1 || i1 == e2)
1463 /* If we will get to this code by jumping, those jumps will be
1464 tensioned to go directly to the new label (before I2),
1465 so this cross-jumping won't cost extra. So reduce the minimum. */
1466 if (GET_CODE (i1) == CODE_LABEL)
1472 if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
1475 /* Avoid moving insns across EH regions if either of the insns
1478 && (asynchronous_exceptions || GET_CODE (i1) == CALL_INSN)
1479 && !in_same_eh_region (i1, i2))
1485 /* If this is a CALL_INSN, compare register usage information.
1486 If we don't check this on stack register machines, the two
1487 CALL_INSNs might be merged leaving reg-stack.c with mismatching
1488 numbers of stack registers in the same basic block.
1489 If we don't check this on machines with delay slots, a delay slot may
1490 be filled that clobbers a parameter expected by the subroutine.
1492 ??? We take the simple route for now and assume that if they're
1493 equal, they were constructed identically. */
1495 if (GET_CODE (i1) == CALL_INSN
1496 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
1497 CALL_INSN_FUNCTION_USAGE (i2)))
1501 /* If cross_jump_death_matters is not 0, the insn's mode
1502 indicates whether or not the insn contains any stack-like
1505 if (!lose && cross_jump_death_matters && stack_regs_mentioned (i1))
1507 /* If register stack conversion has already been done, then
1508 death notes must also be compared before it is certain that
1509 the two instruction streams match. */
1512 HARD_REG_SET i1_regset, i2_regset;
1514 CLEAR_HARD_REG_SET (i1_regset);
1515 CLEAR_HARD_REG_SET (i2_regset);
1517 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1518 if (REG_NOTE_KIND (note) == REG_DEAD
1519 && STACK_REG_P (XEXP (note, 0)))
1520 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1522 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1523 if (REG_NOTE_KIND (note) == REG_DEAD
1524 && STACK_REG_P (XEXP (note, 0)))
1525 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1527 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
1536 /* Don't allow old-style asm or volatile extended asms to be accepted
1537 for cross jumping purposes. It is conceptually correct to allow
1538 them, since cross-jumping preserves the dynamic instruction order
1539 even though it is changing the static instruction order. However,
1540 if an asm is being used to emit an assembler pseudo-op, such as
1541 the MIPS `.set reorder' pseudo-op, then the static instruction order
1542 matters and it must be preserved. */
1543 if (GET_CODE (p1) == ASM_INPUT || GET_CODE (p2) == ASM_INPUT
1544 || (GET_CODE (p1) == ASM_OPERANDS && MEM_VOLATILE_P (p1))
1545 || (GET_CODE (p2) == ASM_OPERANDS && MEM_VOLATILE_P (p2)))
1548 if (lose || GET_CODE (p1) != GET_CODE (p2)
1549 || ! rtx_renumbered_equal_p (p1, p2))
1551 /* The following code helps take care of G++ cleanups. */
1555 if (!lose && GET_CODE (p1) == GET_CODE (p2)
1556 && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
1557 || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
1558 && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
1559 || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
1560 /* If the equivalences are not to a constant, they may
1561 reference pseudos that no longer exist, so we can't
1563 && CONSTANT_P (XEXP (equiv1, 0))
1564 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1566 rtx s1 = single_set (i1);
1567 rtx s2 = single_set (i2);
1568 if (s1 != 0 && s2 != 0
1569 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
1571 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
1572 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
1573 if (! rtx_renumbered_equal_p (p1, p2))
1575 else if (apply_change_group ())
1580 /* Insns fail to match; cross jumping is limited to the following
1584 /* Don't allow the insn after a compare to be shared by
1585 cross-jumping unless the compare is also shared.
1586 Here, if either of these non-matching insns is a compare,
1587 exclude the following insn from possible cross-jumping. */
1588 if (sets_cc0_p (p1) || sets_cc0_p (p2))
1589 last1 = afterlast1, last2 = afterlast2, ++minimum;
1592 /* If cross-jumping here will feed a jump-around-jump
1593 optimization, this jump won't cost extra, so reduce
1595 if (GET_CODE (i1) == JUMP_INSN
1597 && prev_real_insn (JUMP_LABEL (i1)) == e1)
1603 if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
1605 /* Ok, this insn is potentially includable in a cross-jump here. */
1606 afterlast1 = last1, afterlast2 = last2;
1607 last1 = i1, last2 = i2, --minimum;
1611 if (minimum <= 0 && last1 != 0 && last1 != e1)
1612 *f1 = last1, *f2 = last2;
1616 do_cross_jump (insn, newjpos, newlpos)
1617 rtx insn, newjpos, newlpos;
1619 /* Find an existing label at this point
1620 or make a new one if there is none. */
1621 register rtx label = get_label_before (newlpos);
1623 /* Make the same jump insn jump to the new point. */
1624 if (GET_CODE (PATTERN (insn)) == RETURN)
1626 /* Remove from jump chain of returns. */
1627 delete_from_jump_chain (insn);
1628 /* Change the insn. */
1629 PATTERN (insn) = gen_jump (label);
1630 INSN_CODE (insn) = -1;
1631 JUMP_LABEL (insn) = label;
1632 LABEL_NUSES (label)++;
1633 /* Add to new the jump chain. */
1634 if (INSN_UID (label) < max_jump_chain
1635 && INSN_UID (insn) < max_jump_chain)
1637 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
1638 jump_chain[INSN_UID (label)] = insn;
1642 redirect_jump (insn, label, 1);
1644 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
1645 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
1646 the NEWJPOS stream. */
1648 while (newjpos != insn)
1652 for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
1653 if ((REG_NOTE_KIND (lnote) == REG_EQUAL
1654 || REG_NOTE_KIND (lnote) == REG_EQUIV)
1655 && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
1656 && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
1657 remove_note (newlpos, lnote);
1659 delete_insn (newjpos);
1660 newjpos = next_real_insn (newjpos);
1661 newlpos = next_real_insn (newlpos);
1665 /* Return the label before INSN, or put a new label there. */
1668 get_label_before (insn)
1673 /* Find an existing label at this point
1674 or make a new one if there is none. */
1675 label = prev_nonnote_insn (insn);
1677 if (label == 0 || GET_CODE (label) != CODE_LABEL)
1679 rtx prev = PREV_INSN (insn);
1681 label = gen_label_rtx ();
1682 emit_label_after (label, prev);
1683 LABEL_NUSES (label) = 0;
1688 /* Return the label after INSN, or put a new label there. */
1691 get_label_after (insn)
1696 /* Find an existing label at this point
1697 or make a new one if there is none. */
1698 label = next_nonnote_insn (insn);
1700 if (label == 0 || GET_CODE (label) != CODE_LABEL)
1702 label = gen_label_rtx ();
1703 emit_label_after (label, insn);
1704 LABEL_NUSES (label) = 0;
1709 /* Return 1 if INSN is a jump that jumps to right after TARGET
1710 only on the condition that TARGET itself would drop through.
1711 Assumes that TARGET is a conditional jump. */
1714 jump_back_p (insn, target)
1718 enum rtx_code codei, codet;
1721 if (! any_condjump_p (insn)
1722 || any_uncondjump_p (target)
1723 || target != prev_real_insn (JUMP_LABEL (insn)))
1725 set = pc_set (insn);
1726 tset = pc_set (target);
1728 cinsn = XEXP (SET_SRC (set), 0);
1729 ctarget = XEXP (SET_SRC (tset), 0);
1731 codei = GET_CODE (cinsn);
1732 codet = GET_CODE (ctarget);
1734 if (XEXP (SET_SRC (set), 1) == pc_rtx)
1736 if (! can_reverse_comparison_p (cinsn, insn))
1738 codei = reverse_condition (codei);
1741 if (XEXP (SET_SRC (tset), 2) == pc_rtx)
1743 if (! can_reverse_comparison_p (ctarget, target))
1745 codet = reverse_condition (codet);
1748 return (codei == codet
1749 && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
1750 && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
1753 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
1754 return non-zero if it is safe to reverse this comparison. It is if our
1755 floating-point is not IEEE, if this is an NE or EQ comparison, or if
1756 this is known to be an integer comparison. */
1759 can_reverse_comparison_p (comparison, insn)
1765 /* If this is not actually a comparison, we can't reverse it. */
1766 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
1769 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1770 /* If this is an NE comparison, it is safe to reverse it to an EQ
1771 comparison and vice versa, even for floating point. If no operands
1772 are NaNs, the reversal is valid. If some operand is a NaN, EQ is
1773 always false and NE is always true, so the reversal is also valid. */
1775 || GET_CODE (comparison) == NE
1776 || GET_CODE (comparison) == EQ)
1779 arg0 = XEXP (comparison, 0);
1781 /* Make sure ARG0 is one of the actual objects being compared. If we
1782 can't do this, we can't be sure the comparison can be reversed.
1784 Handle cc0 and a MODE_CC register. */
1785 if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
1793 /* First see if the condition code mode alone if enough to say we can
1794 reverse the condition. If not, then search backwards for a set of
1795 ARG0. We do not need to check for an insn clobbering it since valid
1796 code will contain set a set with no intervening clobber. But
1797 stop when we reach a label. */
1798 #ifdef REVERSIBLE_CC_MODE
1799 if (GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC
1800 && REVERSIBLE_CC_MODE (GET_MODE (arg0)))
1807 for (prev = prev_nonnote_insn (insn);
1808 prev != 0 && GET_CODE (prev) != CODE_LABEL;
1809 prev = prev_nonnote_insn (prev))
1810 if ((set = single_set (prev)) != 0
1811 && rtx_equal_p (SET_DEST (set), arg0))
1813 arg0 = SET_SRC (set);
1815 if (GET_CODE (arg0) == COMPARE)
1816 arg0 = XEXP (arg0, 0);
1821 /* We can reverse this if ARG0 is a CONST_INT or if its mode is
1822 not VOIDmode and neither a MODE_CC nor MODE_FLOAT type. */
1823 return (GET_CODE (arg0) == CONST_INT
1824 || (GET_MODE (arg0) != VOIDmode
1825 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
1826 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
1829 /* Given an rtx-code for a comparison, return the code for the negated
1830 comparison. If no such code exists, return UNKNOWN.
1832 WATCH OUT! reverse_condition is not safe to use on a jump that might
1833 be acting on the results of an IEEE floating point comparison, because
1834 of the special treatment of non-signaling nans in comparisons.
1835 Use can_reverse_comparison_p to be sure. */
1838 reverse_condition (code)
1881 /* Similar, but we're allowed to generate unordered comparisons, which
1882 makes it safe for IEEE floating-point. Of course, we have to recognize
1883 that the target will support them too... */
1886 reverse_condition_maybe_unordered (code)
1889 /* Non-IEEE formats don't have unordered conditions. */
1890 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
1891 return reverse_condition (code);
1937 /* Similar, but return the code when two operands of a comparison are swapped.
1938 This IS safe for IEEE floating-point. */
1941 swap_condition (code)
1984 /* Given a comparison CODE, return the corresponding unsigned comparison.
1985 If CODE is an equality comparison or already an unsigned comparison,
1986 CODE is returned. */
1989 unsigned_condition (code)
2016 /* Similarly, return the signed version of a comparison. */
2019 signed_condition (code)
2046 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
2047 truth of CODE1 implies the truth of CODE2. */
2050 comparison_dominates_p (code1, code2)
2051 enum rtx_code code1, code2;
2059 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
2060 || code2 == ORDERED)
2065 if (code2 == LE || code2 == NE || code2 == ORDERED)
2070 if (code2 == GE || code2 == NE || code2 == ORDERED)
2076 if (code2 == ORDERED)
2081 if (code2 == NE || code2 == ORDERED)
2086 if (code2 == LEU || code2 == NE)
2091 if (code2 == GEU || code2 == NE)
2107 /* Return 1 if INSN is an unconditional jump and nothing else. */
2113 return (GET_CODE (insn) == JUMP_INSN
2114 && GET_CODE (PATTERN (insn)) == SET
2115 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
2116 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
2119 /* Return nonzero if INSN is a (possibly) conditional jump
2122 Use this function is deprecated, since we need to support combined
2123 branch and compare insns. Use any_condjump_p instead whenever possible. */
2129 register rtx x = PATTERN (insn);
2131 if (GET_CODE (x) != SET
2132 || GET_CODE (SET_DEST (x)) != PC)
2136 if (GET_CODE (x) == LABEL_REF)
2138 else return (GET_CODE (x) == IF_THEN_ELSE
2139 && ((GET_CODE (XEXP (x, 2)) == PC
2140 && (GET_CODE (XEXP (x, 1)) == LABEL_REF
2141 || GET_CODE (XEXP (x, 1)) == RETURN))
2142 || (GET_CODE (XEXP (x, 1)) == PC
2143 && (GET_CODE (XEXP (x, 2)) == LABEL_REF
2144 || GET_CODE (XEXP (x, 2)) == RETURN))));
2149 /* Return nonzero if INSN is a (possibly) conditional jump inside a
2152 Use this function is deprecated, since we need to support combined
2153 branch and compare insns. Use any_condjump_p instead whenever possible. */
2156 condjump_in_parallel_p (insn)
2159 register rtx x = PATTERN (insn);
2161 if (GET_CODE (x) != PARALLEL)
2164 x = XVECEXP (x, 0, 0);
2166 if (GET_CODE (x) != SET)
2168 if (GET_CODE (SET_DEST (x)) != PC)
2170 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2172 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2174 if (XEXP (SET_SRC (x), 2) == pc_rtx
2175 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2176 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2178 if (XEXP (SET_SRC (x), 1) == pc_rtx
2179 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2180 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2185 /* Return set of PC, otherwise NULL. */
2192 if (GET_CODE (insn) != JUMP_INSN)
2194 pat = PATTERN (insn);
2196 /* The set is allowed to appear either as the insn pattern or
2197 the first set in a PARALLEL. */
2198 if (GET_CODE (pat) == PARALLEL)
2199 pat = XVECEXP (pat, 0, 0);
2200 if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
2206 /* Return true when insn is an unconditional direct jump,
2207 possibly bundled inside a PARALLEL. */
2210 any_uncondjump_p (insn)
2213 rtx x = pc_set (insn);
2216 if (GET_CODE (SET_SRC (x)) != LABEL_REF)
2221 /* Return true when insn is a conditional jump. This function works for
2222 instructions containing PC sets in PARALLELs. The instruction may have
2223 various other effects so before removing the jump you must verify
2224 safe_to_remove_jump_p.
2226 Note that unlike condjump_p it returns false for unconditional jumps. */
2229 any_condjump_p (insn)
2232 rtx x = pc_set (insn);
2237 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2240 a = GET_CODE (XEXP (SET_SRC (x), 1));
2241 b = GET_CODE (XEXP (SET_SRC (x), 2));
2243 return ((b == PC && (a == LABEL_REF || a == RETURN))
2244 || (a == PC && (b == LABEL_REF || b == RETURN)));
2247 /* Return the label of a conditional jump. */
2250 condjump_label (insn)
2253 rtx x = pc_set (insn);
2258 if (GET_CODE (x) == LABEL_REF)
2260 if (GET_CODE (x) != IF_THEN_ELSE)
2262 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
2264 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
2269 /* Return true if INSN is a (possibly conditional) return insn. */
2272 returnjump_p_1 (loc, data)
2274 void *data ATTRIBUTE_UNUSED;
2277 return x && GET_CODE (x) == RETURN;
2284 if (GET_CODE (insn) != JUMP_INSN)
2286 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
2289 /* Return true if INSN is a jump that only transfers control and
2298 if (GET_CODE (insn) != JUMP_INSN)
2301 set = single_set (insn);
2304 if (GET_CODE (SET_DEST (set)) != PC)
2306 if (side_effects_p (SET_SRC (set)))
2314 /* Return 1 if X is an RTX that does nothing but set the condition codes
2315 and CLOBBER or USE registers.
2316 Return -1 if X does explicitly set the condition codes,
2317 but also does other things. */
2321 rtx x ATTRIBUTE_UNUSED;
2323 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
2325 if (GET_CODE (x) == PARALLEL)
2329 int other_things = 0;
2330 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2332 if (GET_CODE (XVECEXP (x, 0, i)) == SET
2333 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
2335 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
2338 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
2344 /* Follow any unconditional jump at LABEL;
2345 return the ultimate label reached by any such chain of jumps.
2346 If LABEL is not followed by a jump, return LABEL.
2347 If the chain loops or we can't find end, return LABEL,
2348 since that tells caller to avoid changing the insn.
2350 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
2351 a USE or CLOBBER. */
2354 follow_jumps (label)
2359 register rtx value = label;
2364 && (insn = next_active_insn (value)) != 0
2365 && GET_CODE (insn) == JUMP_INSN
2366 && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
2367 && onlyjump_p (insn))
2368 || GET_CODE (PATTERN (insn)) == RETURN)
2369 && (next = NEXT_INSN (insn))
2370 && GET_CODE (next) == BARRIER);
2373 /* Don't chain through the insn that jumps into a loop
2374 from outside the loop,
2375 since that would create multiple loop entry jumps
2376 and prevent loop optimization. */
2378 if (!reload_completed)
2379 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
2380 if (GET_CODE (tem) == NOTE
2381 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
2382 /* ??? Optional. Disables some optimizations, but makes
2383 gcov output more accurate with -O. */
2384 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
2387 /* If we have found a cycle, make the insn jump to itself. */
2388 if (JUMP_LABEL (insn) == label)
2391 tem = next_active_insn (JUMP_LABEL (insn));
2392 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
2393 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
2396 value = JUMP_LABEL (insn);
2403 /* Assuming that field IDX of X is a vector of label_refs,
2404 replace each of them by the ultimate label reached by it.
2405 Return nonzero if a change is made.
2406 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
2409 tension_vector_labels (x, idx)
2415 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
2417 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
2418 register rtx nlabel = follow_jumps (olabel);
2419 if (nlabel && nlabel != olabel)
2421 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
2422 ++LABEL_NUSES (nlabel);
2423 if (--LABEL_NUSES (olabel) == 0)
2424 delete_insn (olabel);
2431 /* Find all CODE_LABELs referred to in X, and increment their use counts.
2432 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
2433 in INSN, then store one of them in JUMP_LABEL (INSN).
2434 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
2435 referenced in INSN, add a REG_LABEL note containing that label to INSN.
2436 Also, when there are consecutive labels, canonicalize on the last of them.
2438 Note that two labels separated by a loop-beginning note
2439 must be kept distinct if we have not yet done loop-optimization,
2440 because the gap between them is where loop-optimize
2441 will want to move invariant code to. CROSS_JUMP tells us
2442 that loop-optimization is done with.
2444 Once reload has completed (CROSS_JUMP non-zero), we need not consider
2445 two labels distinct if they are separated by only USE or CLOBBER insns. */
2448 mark_jump_label (x, insn, cross_jump, in_mem)
2454 register RTX_CODE code = GET_CODE (x);
2456 register const char *fmt;
2478 /* If this is a constant-pool reference, see if it is a label. */
2479 if (CONSTANT_POOL_ADDRESS_P (x))
2480 mark_jump_label (get_pool_constant (x), insn, cross_jump, in_mem);
2485 rtx label = XEXP (x, 0);
2490 /* Ignore remaining references to unreachable labels that
2491 have been deleted. */
2492 if (GET_CODE (label) == NOTE
2493 && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
2496 if (GET_CODE (label) != CODE_LABEL)
2499 /* Ignore references to labels of containing functions. */
2500 if (LABEL_REF_NONLOCAL_P (x))
2503 /* If there are other labels following this one,
2504 replace it with the last of the consecutive labels. */
2505 for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
2507 if (GET_CODE (next) == CODE_LABEL)
2509 else if (cross_jump && GET_CODE (next) == INSN
2510 && (GET_CODE (PATTERN (next)) == USE
2511 || GET_CODE (PATTERN (next)) == CLOBBER))
2513 else if (GET_CODE (next) != NOTE)
2515 else if (! cross_jump
2516 && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
2517 || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END
2518 /* ??? Optional. Disables some optimizations, but
2519 makes gcov output more accurate with -O. */
2520 || (flag_test_coverage && NOTE_LINE_NUMBER (next) > 0)))
2524 XEXP (x, 0) = label;
2525 if (! insn || ! INSN_DELETED_P (insn))
2526 ++LABEL_NUSES (label);
2530 if (GET_CODE (insn) == JUMP_INSN)
2531 JUMP_LABEL (insn) = label;
2533 /* If we've changed OLABEL and we had a REG_LABEL note
2534 for it, update it as well. */
2535 else if (label != olabel
2536 && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
2537 XEXP (note, 0) = label;
2539 /* Otherwise, add a REG_LABEL note for LABEL unless there already
2541 else if (! find_reg_note (insn, REG_LABEL, label))
2543 /* This code used to ignore labels which refered to dispatch
2544 tables to avoid flow.c generating worse code.
2546 However, in the presense of global optimizations like
2547 gcse which call find_basic_blocks without calling
2548 life_analysis, not recording such labels will lead
2549 to compiler aborts because of inconsistencies in the
2550 flow graph. So we go ahead and record the label.
2552 It may also be the case that the optimization argument
2553 is no longer valid because of the more accurate cfg
2554 we build in find_basic_blocks -- it no longer pessimizes
2555 code when it finds a REG_LABEL note. */
2556 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
2563 /* Do walk the labels in a vector, but not the first operand of an
2564 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
2567 if (! INSN_DELETED_P (insn))
2569 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
2571 for (i = 0; i < XVECLEN (x, eltnum); i++)
2572 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX,
2573 cross_jump, in_mem);
2581 fmt = GET_RTX_FORMAT (code);
2582 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2585 mark_jump_label (XEXP (x, i), insn, cross_jump, in_mem);
2586 else if (fmt[i] == 'E')
2589 for (j = 0; j < XVECLEN (x, i); j++)
2590 mark_jump_label (XVECEXP (x, i, j), insn, cross_jump, in_mem);
2595 /* If all INSN does is set the pc, delete it,
2596 and delete the insn that set the condition codes for it
2597 if that's what the previous thing was. */
2603 register rtx set = single_set (insn);
2605 if (set && GET_CODE (SET_DEST (set)) == PC)
2606 delete_computation (insn);
2609 /* Verify INSN is a BARRIER and delete it. */
2612 delete_barrier (insn)
2615 if (GET_CODE (insn) != BARRIER)
2621 /* Recursively delete prior insns that compute the value (used only by INSN
2622 which the caller is deleting) stored in the register mentioned by NOTE
2623 which is a REG_DEAD note associated with INSN. */
2626 delete_prior_computation (note, insn)
2631 rtx reg = XEXP (note, 0);
2633 for (our_prev = prev_nonnote_insn (insn);
2634 our_prev && (GET_CODE (our_prev) == INSN
2635 || GET_CODE (our_prev) == CALL_INSN);
2636 our_prev = prev_nonnote_insn (our_prev))
2638 rtx pat = PATTERN (our_prev);
2640 /* If we reach a CALL which is not calling a const function
2641 or the callee pops the arguments, then give up. */
2642 if (GET_CODE (our_prev) == CALL_INSN
2643 && (! CONST_CALL_P (our_prev)
2644 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
2647 /* If we reach a SEQUENCE, it is too complex to try to
2648 do anything with it, so give up. */
2649 if (GET_CODE (pat) == SEQUENCE)
2652 if (GET_CODE (pat) == USE
2653 && GET_CODE (XEXP (pat, 0)) == INSN)
2654 /* reorg creates USEs that look like this. We leave them
2655 alone because reorg needs them for its own purposes. */
2658 if (reg_set_p (reg, pat))
2660 if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
2663 if (GET_CODE (pat) == PARALLEL)
2665 /* If we find a SET of something else, we can't
2670 for (i = 0; i < XVECLEN (pat, 0); i++)
2672 rtx part = XVECEXP (pat, 0, i);
2674 if (GET_CODE (part) == SET
2675 && SET_DEST (part) != reg)
2679 if (i == XVECLEN (pat, 0))
2680 delete_computation (our_prev);
2682 else if (GET_CODE (pat) == SET
2683 && GET_CODE (SET_DEST (pat)) == REG)
2685 int dest_regno = REGNO (SET_DEST (pat));
2687 = dest_regno + (dest_regno < FIRST_PSEUDO_REGISTER
2688 ? HARD_REGNO_NREGS (dest_regno,
2689 GET_MODE (SET_DEST (pat))) : 1);
2690 int regno = REGNO (reg);
2691 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
2692 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
2694 if (dest_regno >= regno
2695 && dest_endregno <= endregno)
2696 delete_computation (our_prev);
2698 /* We may have a multi-word hard register and some, but not
2699 all, of the words of the register are needed in subsequent
2700 insns. Write REG_UNUSED notes for those parts that were not
2702 else if (dest_regno <= regno
2703 && dest_endregno >= endregno)
2707 REG_NOTES (our_prev)
2708 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (our_prev));
2710 for (i = dest_regno; i < dest_endregno; i++)
2711 if (! find_regno_note (our_prev, REG_UNUSED, i))
2714 if (i == dest_endregno)
2715 delete_computation (our_prev);
2722 /* If PAT references the register that dies here, it is an
2723 additional use. Hence any prior SET isn't dead. However, this
2724 insn becomes the new place for the REG_DEAD note. */
2725 if (reg_overlap_mentioned_p (reg, pat))
2727 XEXP (note, 1) = REG_NOTES (our_prev);
2728 REG_NOTES (our_prev) = note;
2734 /* Delete INSN and recursively delete insns that compute values used only
2735 by INSN. This uses the REG_DEAD notes computed during flow analysis.
2736 If we are running before flow.c, we need do nothing since flow.c will
2737 delete dead code. We also can't know if the registers being used are
2738 dead or not at this point.
2740 Otherwise, look at all our REG_DEAD notes. If a previous insn does
2741 nothing other than set a register that dies in this insn, we can delete
2744 On machines with CC0, if CC0 is used in this insn, we may be able to
2745 delete the insn that set it. */
2748 delete_computation (insn)
2755 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
2757 rtx prev = prev_nonnote_insn (insn);
2758 /* We assume that at this stage
2759 CC's are always set explicitly
2760 and always immediately before the jump that
2761 will use them. So if the previous insn
2762 exists to set the CC's, delete it
2763 (unless it performs auto-increments, etc.). */
2764 if (prev && GET_CODE (prev) == INSN
2765 && sets_cc0_p (PATTERN (prev)))
2767 if (sets_cc0_p (PATTERN (prev)) > 0
2768 && ! side_effects_p (PATTERN (prev)))
2769 delete_computation (prev);
2771 /* Otherwise, show that cc0 won't be used. */
2772 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
2773 cc0_rtx, REG_NOTES (prev));
2778 #ifdef INSN_SCHEDULING
2779 /* ?!? The schedulers do not keep REG_DEAD notes accurate after
2780 reload has completed. The schedulers need to be fixed. Until
2781 they are, we must not rely on the death notes here. */
2782 if (reload_completed && flag_schedule_insns_after_reload)
2789 /* The REG_DEAD note may have been omitted for a register
2790 which is both set and used by the insn. */
2791 set = single_set (insn);
2792 if (set && GET_CODE (SET_DEST (set)) == REG)
2794 int dest_regno = REGNO (SET_DEST (set));
2796 = dest_regno + (dest_regno < FIRST_PSEUDO_REGISTER
2797 ? HARD_REGNO_NREGS (dest_regno,
2798 GET_MODE (SET_DEST (set))) : 1);
2801 for (i = dest_regno; i < dest_endregno; i++)
2803 if (! refers_to_regno_p (i, i + 1, SET_SRC (set), NULL_PTR)
2804 || find_regno_note (insn, REG_DEAD, i))
2807 note = gen_rtx_EXPR_LIST (REG_DEAD, (i < FIRST_PSEUDO_REGISTER
2808 ? gen_rtx_REG (reg_raw_mode[i], i)
2809 : SET_DEST (set)), NULL_RTX);
2810 delete_prior_computation (note, insn);
2814 for (note = REG_NOTES (insn); note; note = next)
2816 next = XEXP (note, 1);
2818 if (REG_NOTE_KIND (note) != REG_DEAD
2819 /* Verify that the REG_NOTE is legitimate. */
2820 || GET_CODE (XEXP (note, 0)) != REG)
2823 delete_prior_computation (note, insn);
2829 /* Delete insn INSN from the chain of insns and update label ref counts.
2830 May delete some following insns as a consequence; may even delete
2831 a label elsewhere and insns that follow it.
2833 Returns the first insn after INSN that was not deleted. */
2839 register rtx next = NEXT_INSN (insn);
2840 register rtx prev = PREV_INSN (insn);
2841 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
2842 register int dont_really_delete = 0;
2844 while (next && INSN_DELETED_P (next))
2845 next = NEXT_INSN (next);
2847 /* This insn is already deleted => return first following nondeleted. */
2848 if (INSN_DELETED_P (insn))
2852 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
2854 /* Don't delete user-declared labels. When optimizing, convert them
2855 to special NOTEs instead. When not optimizing, leave them alone. */
2856 if (was_code_label && LABEL_NAME (insn) != 0)
2859 dont_really_delete = 1;
2860 else if (! dont_really_delete)
2862 const char *name = LABEL_NAME (insn);
2863 PUT_CODE (insn, NOTE);
2864 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
2865 NOTE_SOURCE_FILE (insn) = name;
2866 dont_really_delete = 1;
2870 /* Mark this insn as deleted. */
2871 INSN_DELETED_P (insn) = 1;
2873 /* If this is an unconditional jump, delete it from the jump chain. */
2874 if (simplejump_p (insn))
2875 delete_from_jump_chain (insn);
2877 /* If instruction is followed by a barrier,
2878 delete the barrier too. */
2880 if (next != 0 && GET_CODE (next) == BARRIER)
2882 INSN_DELETED_P (next) = 1;
2883 next = NEXT_INSN (next);
2886 /* Patch out INSN (and the barrier if any) */
2888 if (! dont_really_delete)
2892 NEXT_INSN (prev) = next;
2893 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
2894 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
2895 XVECLEN (PATTERN (prev), 0) - 1)) = next;
2900 PREV_INSN (next) = prev;
2901 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
2902 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
2905 if (prev && NEXT_INSN (prev) == 0)
2906 set_last_insn (prev);
2909 /* If deleting a jump, decrement the count of the label,
2910 and delete the label if it is now unused. */
2912 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
2914 rtx lab = JUMP_LABEL (insn), lab_next;
2916 if (--LABEL_NUSES (lab) == 0)
2918 /* This can delete NEXT or PREV,
2919 either directly if NEXT is JUMP_LABEL (INSN),
2920 or indirectly through more levels of jumps. */
2923 /* I feel a little doubtful about this loop,
2924 but I see no clean and sure alternative way
2925 to find the first insn after INSN that is not now deleted.
2926 I hope this works. */
2927 while (next && INSN_DELETED_P (next))
2928 next = NEXT_INSN (next);
2931 else if ((lab_next = next_nonnote_insn (lab)) != NULL
2932 && GET_CODE (lab_next) == JUMP_INSN
2933 && (GET_CODE (PATTERN (lab_next)) == ADDR_VEC
2934 || GET_CODE (PATTERN (lab_next)) == ADDR_DIFF_VEC))
2936 /* If we're deleting the tablejump, delete the dispatch table.
2937 We may not be able to kill the label immediately preceeding
2938 just yet, as it might be referenced in code leading up to
2940 delete_insn (lab_next);
2944 /* Likewise if we're deleting a dispatch table. */
2946 if (GET_CODE (insn) == JUMP_INSN
2947 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
2948 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
2950 rtx pat = PATTERN (insn);
2951 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
2952 int len = XVECLEN (pat, diff_vec_p);
2954 for (i = 0; i < len; i++)
2955 if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
2956 delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
2957 while (next && INSN_DELETED_P (next))
2958 next = NEXT_INSN (next);
2962 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
2963 prev = PREV_INSN (prev);
2965 /* If INSN was a label and a dispatch table follows it,
2966 delete the dispatch table. The tablejump must have gone already.
2967 It isn't useful to fall through into a table. */
2970 && NEXT_INSN (insn) != 0
2971 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
2972 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
2973 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
2974 next = delete_insn (NEXT_INSN (insn));
2976 /* If INSN was a label, delete insns following it if now unreachable. */
2978 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
2980 register RTX_CODE code;
2982 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
2983 || code == NOTE || code == BARRIER
2984 || (code == CODE_LABEL && INSN_DELETED_P (next))))
2987 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
2988 next = NEXT_INSN (next);
2989 /* Keep going past other deleted labels to delete what follows. */
2990 else if (code == CODE_LABEL && INSN_DELETED_P (next))
2991 next = NEXT_INSN (next);
2993 /* Note: if this deletes a jump, it can cause more
2994 deletion of unreachable code, after a different label.
2995 As long as the value from this recursive call is correct,
2996 this invocation functions correctly. */
2997 next = delete_insn (next);
3004 /* Advance from INSN till reaching something not deleted
3005 then return that. May return INSN itself. */
3008 next_nondeleted_insn (insn)
3011 while (INSN_DELETED_P (insn))
3012 insn = NEXT_INSN (insn);
3016 /* Delete a range of insns from FROM to TO, inclusive.
3017 This is for the sake of peephole optimization, so assume
3018 that whatever these insns do will still be done by a new
3019 peephole insn that will replace them. */
3022 delete_for_peephole (from, to)
3023 register rtx from, to;
3025 register rtx insn = from;
3029 register rtx next = NEXT_INSN (insn);
3030 register rtx prev = PREV_INSN (insn);
3032 if (GET_CODE (insn) != NOTE)
3034 INSN_DELETED_P (insn) = 1;
3036 /* Patch this insn out of the chain. */
3037 /* We don't do this all at once, because we
3038 must preserve all NOTEs. */
3040 NEXT_INSN (prev) = next;
3043 PREV_INSN (next) = prev;
3051 /* Note that if TO is an unconditional jump
3052 we *do not* delete the BARRIER that follows,
3053 since the peephole that replaces this sequence
3054 is also an unconditional jump in that case. */
3057 /* We have determined that INSN is never reached, and are about to
3058 delete it. Print a warning if the user asked for one.
3060 To try to make this warning more useful, this should only be called
3061 once per basic block not reached, and it only warns when the basic
3062 block contains more than one line from the current function, and
3063 contains at least one operation. CSE and inlining can duplicate insns,
3064 so it's possible to get spurious warnings from this. */
3067 never_reached_warning (avoided_insn)
3071 rtx a_line_note = NULL;
3072 int two_avoided_lines = 0;
3073 int contains_insn = 0;
3075 if (! warn_notreached)
3078 /* Scan forwards, looking at LINE_NUMBER notes, until
3079 we hit a LABEL or we run out of insns. */
3081 for (insn = avoided_insn; insn != NULL; insn = NEXT_INSN (insn))
3083 if (GET_CODE (insn) == CODE_LABEL)
3085 else if (GET_CODE (insn) == NOTE /* A line number note? */
3086 && NOTE_LINE_NUMBER (insn) >= 0)
3088 if (a_line_note == NULL)
3091 two_avoided_lines |= (NOTE_LINE_NUMBER (a_line_note)
3092 != NOTE_LINE_NUMBER (insn));
3094 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3097 if (two_avoided_lines && contains_insn)
3098 warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note),
3099 NOTE_LINE_NUMBER (a_line_note),
3100 "will never be executed");
3103 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
3104 NLABEL as a return. Accrue modifications into the change group. */
3107 redirect_exp_1 (loc, olabel, nlabel, insn)
3112 register rtx x = *loc;
3113 register RTX_CODE code = GET_CODE (x);
3115 register const char *fmt;
3117 if (code == LABEL_REF)
3119 if (XEXP (x, 0) == olabel)
3123 n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
3125 n = gen_rtx_RETURN (VOIDmode);
3127 validate_change (insn, loc, n, 1);
3131 else if (code == RETURN && olabel == 0)
3133 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
3134 if (loc == &PATTERN (insn))
3135 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
3136 validate_change (insn, loc, x, 1);
3140 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
3141 && GET_CODE (SET_SRC (x)) == LABEL_REF
3142 && XEXP (SET_SRC (x), 0) == olabel)
3144 validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
3148 fmt = GET_RTX_FORMAT (code);
3149 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3152 redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
3153 else if (fmt[i] == 'E')
3156 for (j = 0; j < XVECLEN (x, i); j++)
3157 redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
3162 /* Similar, but apply the change group and report success or failure. */
3165 redirect_exp (olabel, nlabel, insn)
3171 if (GET_CODE (PATTERN (insn)) == PARALLEL)
3172 loc = &XVECEXP (PATTERN (insn), 0, 0);
3174 loc = &PATTERN (insn);
3176 redirect_exp_1 (loc, olabel, nlabel, insn);
3177 if (num_validated_changes () == 0)
3180 return apply_change_group ();
3183 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
3184 the modifications into the change group. Return false if we did
3185 not see how to do that. */
3188 redirect_jump_1 (jump, nlabel)
3191 int ochanges = num_validated_changes ();
3194 if (GET_CODE (PATTERN (jump)) == PARALLEL)
3195 loc = &XVECEXP (PATTERN (jump), 0, 0);
3197 loc = &PATTERN (jump);
3199 redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
3200 return num_validated_changes () > ochanges;
3203 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
3204 jump target label is unused as a result, it and the code following
3207 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3210 The return value will be 1 if the change was made, 0 if it wasn't
3211 (this can only occur for NLABEL == 0). */
3214 redirect_jump (jump, nlabel, delete_unused)
3218 register rtx olabel = JUMP_LABEL (jump);
3220 if (nlabel == olabel)
3223 if (! redirect_exp (olabel, nlabel, jump))
3226 /* If this is an unconditional branch, delete it from the jump_chain of
3227 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3228 have UID's in range and JUMP_CHAIN is valid). */
3229 if (jump_chain && (simplejump_p (jump)
3230 || GET_CODE (PATTERN (jump)) == RETURN))
3232 int label_index = nlabel ? INSN_UID (nlabel) : 0;
3234 delete_from_jump_chain (jump);
3235 if (label_index < max_jump_chain
3236 && INSN_UID (jump) < max_jump_chain)
3238 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
3239 jump_chain[label_index] = jump;
3243 JUMP_LABEL (jump) = nlabel;
3245 ++LABEL_NUSES (nlabel);
3247 /* If we're eliding the jump over exception cleanups at the end of a
3248 function, move the function end note so that -Wreturn-type works. */
3249 if (olabel && NEXT_INSN (olabel)
3250 && GET_CODE (NEXT_INSN (olabel)) == NOTE
3251 && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
3252 emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
3254 if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused)
3255 delete_insn (olabel);
3260 /* Invert the jump condition of rtx X contained in jump insn, INSN.
3261 Accrue the modifications into the change group. */
3267 register RTX_CODE code;
3268 rtx x = pc_set (insn);
3274 code = GET_CODE (x);
3276 if (code == IF_THEN_ELSE)
3278 register rtx comp = XEXP (x, 0);
3281 /* We can do this in two ways: The preferable way, which can only
3282 be done if this is not an integer comparison, is to reverse
3283 the comparison code. Otherwise, swap the THEN-part and ELSE-part
3284 of the IF_THEN_ELSE. If we can't do either, fail. */
3286 if (can_reverse_comparison_p (comp, insn))
3288 validate_change (insn, &XEXP (x, 0),
3289 gen_rtx_fmt_ee (reverse_condition (GET_CODE (comp)),
3290 GET_MODE (comp), XEXP (comp, 0),
3297 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
3298 validate_change (insn, &XEXP (x, 2), tem, 1);
3304 /* Invert the jump condition of conditional jump insn, INSN.
3306 Return 1 if we can do so, 0 if we cannot find a way to do so that
3307 matches a pattern. */
3313 invert_exp_1 (insn);
3314 if (num_validated_changes () == 0)
3317 return apply_change_group ();
3320 /* Invert the condition of the jump JUMP, and make it jump to label
3321 NLABEL instead of where it jumps now. Accrue changes into the
3322 change group. Return false if we didn't see how to perform the
3323 inversion and redirection. */
3326 invert_jump_1 (jump, nlabel)
3331 ochanges = num_validated_changes ();
3332 invert_exp_1 (jump);
3333 if (num_validated_changes () == ochanges)
3336 return redirect_jump_1 (jump, nlabel);
3339 /* Invert the condition of the jump JUMP, and make it jump to label
3340 NLABEL instead of where it jumps now. Return true if successful. */
3343 invert_jump (jump, nlabel, delete_unused)
3347 /* We have to either invert the condition and change the label or
3348 do neither. Either operation could fail. We first try to invert
3349 the jump. If that succeeds, we try changing the label. If that fails,
3350 we invert the jump back to what it was. */
3352 if (! invert_exp (jump))
3355 if (redirect_jump (jump, nlabel, delete_unused))
3357 /* An inverted jump means that a probability taken becomes a
3358 probability not taken. Subtract the branch probability from the
3359 probability base to convert it back to a taken probability. */
3361 rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
3363 XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
3368 if (! invert_exp (jump))
3369 /* This should just be putting it back the way it was. */
3375 /* Delete the instruction JUMP from any jump chain it might be on. */
3378 delete_from_jump_chain (jump)
3382 rtx olabel = JUMP_LABEL (jump);
3384 /* Handle unconditional jumps. */
3385 if (jump_chain && olabel != 0
3386 && INSN_UID (olabel) < max_jump_chain
3387 && simplejump_p (jump))
3388 index = INSN_UID (olabel);
3389 /* Handle return insns. */
3390 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
3394 if (jump_chain[index] == jump)
3395 jump_chain[index] = jump_chain[INSN_UID (jump)];
3400 for (insn = jump_chain[index];
3402 insn = jump_chain[INSN_UID (insn)])
3403 if (jump_chain[INSN_UID (insn)] == jump)
3405 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
3411 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3413 If the old jump target label (before the dispatch table) becomes unused,
3414 it and the dispatch table may be deleted. In that case, find the insn
3415 before the jump references that label and delete it and logical successors
3419 redirect_tablejump (jump, nlabel)
3422 register rtx olabel = JUMP_LABEL (jump);
3424 /* Add this jump to the jump_chain of NLABEL. */
3425 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
3426 && INSN_UID (jump) < max_jump_chain)
3428 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
3429 jump_chain[INSN_UID (nlabel)] = jump;
3432 PATTERN (jump) = gen_jump (nlabel);
3433 JUMP_LABEL (jump) = nlabel;
3434 ++LABEL_NUSES (nlabel);
3435 INSN_CODE (jump) = -1;
3437 if (--LABEL_NUSES (olabel) == 0)
3439 delete_labelref_insn (jump, olabel, 0);
3440 delete_insn (olabel);
3444 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
3445 If we found one, delete it and then delete this insn if DELETE_THIS is
3446 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
3449 delete_labelref_insn (insn, label, delete_this)
3456 if (GET_CODE (insn) != NOTE
3457 && reg_mentioned_p (label, PATTERN (insn)))
3468 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3469 if (delete_labelref_insn (XEXP (link, 0), label, 1))
3483 /* Like rtx_equal_p except that it considers two REGs as equal
3484 if they renumber to the same value and considers two commutative
3485 operations to be the same if the order of the operands has been
3488 ??? Addition is not commutative on the PA due to the weird implicit
3489 space register selection rules for memory addresses. Therefore, we
3490 don't consider a + b == b + a.
3492 We could/should make this test a little tighter. Possibly only
3493 disabling it on the PA via some backend macro or only disabling this
3494 case when the PLUS is inside a MEM. */
3497 rtx_renumbered_equal_p (x, y)
3501 register RTX_CODE code = GET_CODE (x);
3502 register const char *fmt;
3507 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
3508 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
3509 && GET_CODE (SUBREG_REG (y)) == REG)))
3511 int reg_x = -1, reg_y = -1;
3512 int word_x = 0, word_y = 0;
3514 if (GET_MODE (x) != GET_MODE (y))
3517 /* If we haven't done any renumbering, don't
3518 make any assumptions. */
3519 if (reg_renumber == 0)
3520 return rtx_equal_p (x, y);
3524 reg_x = REGNO (SUBREG_REG (x));
3525 word_x = SUBREG_WORD (x);
3527 if (reg_renumber[reg_x] >= 0)
3529 reg_x = reg_renumber[reg_x] + word_x;
3537 if (reg_renumber[reg_x] >= 0)
3538 reg_x = reg_renumber[reg_x];
3541 if (GET_CODE (y) == SUBREG)
3543 reg_y = REGNO (SUBREG_REG (y));
3544 word_y = SUBREG_WORD (y);
3546 if (reg_renumber[reg_y] >= 0)
3548 reg_y = reg_renumber[reg_y];
3556 if (reg_renumber[reg_y] >= 0)
3557 reg_y = reg_renumber[reg_y];
3560 return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
3563 /* Now we have disposed of all the cases
3564 in which different rtx codes can match. */
3565 if (code != GET_CODE (y))
3577 return INTVAL (x) == INTVAL (y);
3580 /* We can't assume nonlocal labels have their following insns yet. */
3581 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
3582 return XEXP (x, 0) == XEXP (y, 0);
3584 /* Two label-refs are equivalent if they point at labels
3585 in the same position in the instruction stream. */
3586 return (next_real_insn (XEXP (x, 0))
3587 == next_real_insn (XEXP (y, 0)));
3590 return XSTR (x, 0) == XSTR (y, 0);
3593 /* If we didn't match EQ equality above, they aren't the same. */
3600 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
3602 if (GET_MODE (x) != GET_MODE (y))
3605 /* For commutative operations, the RTX match if the operand match in any
3606 order. Also handle the simple binary and unary cases without a loop.
3608 ??? Don't consider PLUS a commutative operator; see comments above. */
3609 if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3611 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3612 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
3613 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
3614 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
3615 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
3616 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3617 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
3618 else if (GET_RTX_CLASS (code) == '1')
3619 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
3621 /* Compare the elements. If any pair of corresponding elements
3622 fail to match, return 0 for the whole things. */
3624 fmt = GET_RTX_FORMAT (code);
3625 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3631 if (XWINT (x, i) != XWINT (y, i))
3636 if (XINT (x, i) != XINT (y, i))
3641 if (strcmp (XSTR (x, i), XSTR (y, i)))
3646 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
3651 if (XEXP (x, i) != XEXP (y, i))
3658 if (XVECLEN (x, i) != XVECLEN (y, i))
3660 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3661 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
3672 /* If X is a hard register or equivalent to one or a subregister of one,
3673 return the hard register number. If X is a pseudo register that was not
3674 assigned a hard register, return the pseudo register number. Otherwise,
3675 return -1. Any rtx is valid for X. */
3681 if (GET_CODE (x) == REG)
3683 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
3684 return reg_renumber[REGNO (x)];
3687 if (GET_CODE (x) == SUBREG)
3689 int base = true_regnum (SUBREG_REG (x));
3690 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
3691 return SUBREG_WORD (x) + base;
3696 /* Optimize code of the form:
3698 for (x = a[i]; x; ...)
3700 for (x = a[i]; x; ...)
3704 Loop optimize will change the above code into
3708 { ...; if (! (x = ...)) break; }
3711 { ...; if (! (x = ...)) break; }
3714 In general, if the first test fails, the program can branch
3715 directly to `foo' and skip the second try which is doomed to fail.
3716 We run this after loop optimization and before flow analysis. */
3718 /* When comparing the insn patterns, we track the fact that different
3719 pseudo-register numbers may have been used in each computation.
3720 The following array stores an equivalence -- same_regs[I] == J means
3721 that pseudo register I was used in the first set of tests in a context
3722 where J was used in the second set. We also count the number of such
3723 pending equivalences. If nonzero, the expressions really aren't the
3726 static int *same_regs;
3728 static int num_same_regs;
3730 /* Track any registers modified between the target of the first jump and
3731 the second jump. They never compare equal. */
3733 static char *modified_regs;
3735 /* Record if memory was modified. */
3737 static int modified_mem;
3739 /* Called via note_stores on each insn between the target of the first
3740 branch and the second branch. It marks any changed registers. */
3743 mark_modified_reg (dest, x, data)
3745 rtx x ATTRIBUTE_UNUSED;
3746 void *data ATTRIBUTE_UNUSED;
3751 if (GET_CODE (dest) == SUBREG)
3752 dest = SUBREG_REG (dest);
3754 if (GET_CODE (dest) == MEM)
3757 if (GET_CODE (dest) != REG)
3760 regno = REGNO (dest);
3761 if (regno >= FIRST_PSEUDO_REGISTER)
3762 modified_regs[regno] = 1;
3764 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
3765 modified_regs[regno + i] = 1;
3768 /* F is the first insn in the chain of insns. */
3771 thread_jumps (f, max_reg, flag_before_loop)
3774 int flag_before_loop;
3776 /* Basic algorithm is to find a conditional branch,
3777 the label it may branch to, and the branch after
3778 that label. If the two branches test the same condition,
3779 walk back from both branch paths until the insn patterns
3780 differ, or code labels are hit. If we make it back to
3781 the target of the first branch, then we know that the first branch
3782 will either always succeed or always fail depending on the relative
3783 senses of the two branches. So adjust the first branch accordingly
3786 rtx label, b1, b2, t1, t2;
3787 enum rtx_code code1, code2;
3788 rtx b1op0, b1op1, b2op0, b2op1;
3793 /* Allocate register tables and quick-reset table. */
3794 modified_regs = (char *) xmalloc (max_reg * sizeof (char));
3795 same_regs = (int *) xmalloc (max_reg * sizeof (int));
3796 all_reset = (int *) xmalloc (max_reg * sizeof (int));
3797 for (i = 0; i < max_reg; i++)
3804 for (b1 = f; b1; b1 = NEXT_INSN (b1))
3808 /* Get to a candidate branch insn. */
3809 if (GET_CODE (b1) != JUMP_INSN
3810 || ! any_condjump_p (b1) || JUMP_LABEL (b1) == 0)
3813 bzero (modified_regs, max_reg * sizeof (char));
3816 bcopy ((char *) all_reset, (char *) same_regs,
3817 max_reg * sizeof (int));
3820 label = JUMP_LABEL (b1);
3822 /* Look for a branch after the target. Record any registers and
3823 memory modified between the target and the branch. Stop when we
3824 get to a label since we can't know what was changed there. */
3825 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
3827 if (GET_CODE (b2) == CODE_LABEL)
3830 else if (GET_CODE (b2) == JUMP_INSN)
3832 /* If this is an unconditional jump and is the only use of
3833 its target label, we can follow it. */
3834 if (any_uncondjump_p (b2)
3836 && JUMP_LABEL (b2) != 0
3837 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
3839 b2 = JUMP_LABEL (b2);
3846 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
3849 if (GET_CODE (b2) == CALL_INSN)
3852 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3853 if (call_used_regs[i] && ! fixed_regs[i]
3854 && i != STACK_POINTER_REGNUM
3855 && i != FRAME_POINTER_REGNUM
3856 && i != HARD_FRAME_POINTER_REGNUM
3857 && i != ARG_POINTER_REGNUM)
3858 modified_regs[i] = 1;
3861 note_stores (PATTERN (b2), mark_modified_reg, NULL);
3864 /* Check the next candidate branch insn from the label
3867 || GET_CODE (b2) != JUMP_INSN
3869 || !any_condjump_p (b2)
3870 || !onlyjump_p (b2))
3875 /* Get the comparison codes and operands, reversing the
3876 codes if appropriate. If we don't have comparison codes,
3877 we can't do anything. */
3878 b1op0 = XEXP (XEXP (SET_SRC (set), 0), 0);
3879 b1op1 = XEXP (XEXP (SET_SRC (set), 0), 1);
3880 code1 = GET_CODE (XEXP (SET_SRC (set), 0));
3881 if (XEXP (SET_SRC (set), 1) == pc_rtx)
3882 code1 = reverse_condition (code1);
3884 b2op0 = XEXP (XEXP (SET_SRC (set2), 0), 0);
3885 b2op1 = XEXP (XEXP (SET_SRC (set2), 0), 1);
3886 code2 = GET_CODE (XEXP (SET_SRC (set2), 0));
3887 if (XEXP (SET_SRC (set2), 1) == pc_rtx)
3888 code2 = reverse_condition (code2);
3890 /* If they test the same things and knowing that B1 branches
3891 tells us whether or not B2 branches, check if we
3892 can thread the branch. */
3893 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
3894 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
3895 && (comparison_dominates_p (code1, code2)
3896 || (can_reverse_comparison_p (XEXP (SET_SRC (set), 0), b1)
3897 && comparison_dominates_p (code1, reverse_condition (code2)))))
3900 t1 = prev_nonnote_insn (b1);
3901 t2 = prev_nonnote_insn (b2);
3903 while (t1 != 0 && t2 != 0)
3907 /* We have reached the target of the first branch.
3908 If there are no pending register equivalents,
3909 we know that this branch will either always
3910 succeed (if the senses of the two branches are
3911 the same) or always fail (if not). */
3914 if (num_same_regs != 0)
3917 if (comparison_dominates_p (code1, code2))
3918 new_label = JUMP_LABEL (b2);
3920 new_label = get_label_after (b2);
3922 if (JUMP_LABEL (b1) != new_label)
3924 rtx prev = PREV_INSN (new_label);
3926 if (flag_before_loop
3927 && GET_CODE (prev) == NOTE
3928 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
3930 /* Don't thread to the loop label. If a loop
3931 label is reused, loop optimization will
3932 be disabled for that loop. */
3933 new_label = gen_label_rtx ();
3934 emit_label_after (new_label, PREV_INSN (prev));
3936 changed |= redirect_jump (b1, new_label, 1);
3941 /* If either of these is not a normal insn (it might be
3942 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
3943 have already been skipped above.) Similarly, fail
3944 if the insns are different. */
3945 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
3946 || recog_memoized (t1) != recog_memoized (t2)
3947 || ! rtx_equal_for_thread_p (PATTERN (t1),
3951 t1 = prev_nonnote_insn (t1);
3952 t2 = prev_nonnote_insn (t2);
3959 free (modified_regs);
3964 /* This is like RTX_EQUAL_P except that it knows about our handling of
3965 possibly equivalent registers and knows to consider volatile and
3966 modified objects as not equal.
3968 YINSN is the insn containing Y. */
3971 rtx_equal_for_thread_p (x, y, yinsn)
3977 register enum rtx_code code;
3978 register const char *fmt;
3980 code = GET_CODE (x);
3981 /* Rtx's of different codes cannot be equal. */
3982 if (code != GET_CODE (y))
3985 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
3986 (REG:SI x) and (REG:HI x) are NOT equivalent. */
3988 if (GET_MODE (x) != GET_MODE (y))
3991 /* For floating-point, consider everything unequal. This is a bit
3992 pessimistic, but this pass would only rarely do anything for FP
3994 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3995 && FLOAT_MODE_P (GET_MODE (x)) && ! flag_fast_math)
3998 /* For commutative operations, the RTX match if the operand match in any
3999 order. Also handle the simple binary and unary cases without a loop. */
4000 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4001 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4002 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4003 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4004 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4005 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4006 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4007 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4008 else if (GET_RTX_CLASS (code) == '1')
4009 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4011 /* Handle special-cases first. */
4015 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4018 /* If neither is user variable or hard register, check for possible
4020 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4021 || REGNO (x) < FIRST_PSEUDO_REGISTER
4022 || REGNO (y) < FIRST_PSEUDO_REGISTER)
4025 if (same_regs[REGNO (x)] == -1)
4027 same_regs[REGNO (x)] = REGNO (y);
4030 /* If this is the first time we are seeing a register on the `Y'
4031 side, see if it is the last use. If not, we can't thread the
4032 jump, so mark it as not equivalent. */
4033 if (REGNO_LAST_UID (REGNO (y)) != INSN_UID (yinsn))
4039 return (same_regs[REGNO (x)] == (int) REGNO (y));
4044 /* If memory modified or either volatile, not equivalent.
4045 Else, check address. */
4046 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4049 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4052 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4058 /* Cancel a pending `same_regs' if setting equivalenced registers.
4059 Then process source. */
4060 if (GET_CODE (SET_DEST (x)) == REG
4061 && GET_CODE (SET_DEST (y)) == REG)
4063 if (same_regs[REGNO (SET_DEST (x))] == (int) REGNO (SET_DEST (y)))
4065 same_regs[REGNO (SET_DEST (x))] = -1;
4068 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4072 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4075 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4078 return XEXP (x, 0) == XEXP (y, 0);
4081 return XSTR (x, 0) == XSTR (y, 0);
4090 fmt = GET_RTX_FORMAT (code);
4091 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4096 if (XWINT (x, i) != XWINT (y, i))
4102 if (XINT (x, i) != XINT (y, i))
4108 /* Two vectors must have the same length. */
4109 if (XVECLEN (x, i) != XVECLEN (y, i))
4112 /* And the corresponding elements must match. */
4113 for (j = 0; j < XVECLEN (x, i); j++)
4114 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
4115 XVECEXP (y, i, j), yinsn) == 0)
4120 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
4126 if (strcmp (XSTR (x, i), XSTR (y, i)))
4131 /* These are just backpointers, so they don't matter. */
4138 /* It is believed that rtx's at this level will never
4139 contain anything but integers and other rtx's,
4140 except for within LABEL_REFs and SYMBOL_REFs. */