1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 1998, 1999, 2000, 2001 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. */
22 /* This is the jump-optimization pass of the compiler.
23 It is run two or three times: once before cse, sometimes once after cse,
24 and once after reload (before final).
26 jump_optimize deletes unreachable code and labels that are not used.
27 It also deletes jumps that jump to the following insn,
28 and simplifies jumps around unconditional jumps and jumps
29 to unconditional jumps.
31 Each CODE_LABEL has a count of the times it is used
32 stored in the LABEL_NUSES internal field, and each JUMP_INSN
33 has one label that it refers to stored in the
34 JUMP_LABEL internal field. With this we can detect labels that
35 become unused because of the deletion of all the jumps that
36 formerly used them. The JUMP_LABEL info is sometimes looked
39 Optionally, cross-jumping can be done. Currently it is done
40 only the last time (when after reload and before final).
41 In fact, the code for cross-jumping now assumes that register
42 allocation has been done, since it uses `rtx_renumbered_equal_p'.
44 Jump optimization is done after cse when cse's constant-propagation
45 causes jumps to become unconditional or to be deleted.
47 Unreachable loops are not detected here, because the labels
48 have references and the insns appear reachable from the labels.
49 find_basic_blocks in flow.c finds and deletes such loops.
51 The subroutines delete_insn, redirect_jump, and invert_jump are used
52 from other passes as well. */
59 #include "hard-reg-set.h"
61 #include "insn-config.h"
62 #include "insn-attr.h"
71 /* ??? Eventually must record somehow the labels used by jumps
72 from nested functions. */
73 /* Pre-record the next or previous real insn for each label?
74 No, this pass is very fast anyway. */
75 /* Condense consecutive labels?
76 This would make life analysis faster, maybe. */
77 /* Optimize jump y; x: ... y: jumpif... x?
78 Don't know if it is worth bothering with. */
79 /* Optimize two cases of conditional jump to conditional jump?
80 This can never delete any instruction or make anything dead,
81 or even change what is live at any point.
82 So perhaps let combiner do it. */
84 /* Vector indexed by uid.
85 For each CODE_LABEL, index by its uid to get first unconditional jump
86 that jumps to the label.
87 For each JUMP_INSN, index by its uid to get the next unconditional jump
88 that jumps to the same label.
89 Element 0 is the start of a chain of all return insns.
90 (It is safe to use element 0 because insn uid 0 is not used. */
92 static rtx
*jump_chain
;
94 /* Maximum index in jump_chain. */
96 static int max_jump_chain
;
98 /* Indicates whether death notes are significant in cross jump analysis.
99 Normally they are not significant, because of A and B jump to C,
100 and R dies in A, it must die in B. But this might not be true after
101 stack register conversion, and we must compare death notes in that
104 static int cross_jump_death_matters
= 0;
106 static int init_label_info
PARAMS ((rtx
));
107 static void delete_barrier_successors
PARAMS ((rtx
));
108 static void mark_all_labels
PARAMS ((rtx
, int));
109 static rtx delete_unreferenced_labels
PARAMS ((rtx
));
110 static void delete_noop_moves
PARAMS ((rtx
));
111 static int duplicate_loop_exit_test
PARAMS ((rtx
));
112 static void find_cross_jump
PARAMS ((rtx
, rtx
, int, rtx
*, rtx
*));
113 static void do_cross_jump
PARAMS ((rtx
, rtx
, rtx
));
114 static int jump_back_p
PARAMS ((rtx
, rtx
));
115 static int tension_vector_labels
PARAMS ((rtx
, int));
116 static void delete_computation
PARAMS ((rtx
));
117 static void redirect_exp_1
PARAMS ((rtx
*, rtx
, rtx
, rtx
));
118 static int redirect_exp
PARAMS ((rtx
, rtx
, rtx
));
119 static void invert_exp_1
PARAMS ((rtx
));
120 static int invert_exp
PARAMS ((rtx
));
121 static void delete_from_jump_chain
PARAMS ((rtx
));
122 static int delete_labelref_insn
PARAMS ((rtx
, rtx
, int));
123 static void mark_modified_reg
PARAMS ((rtx
, rtx
, void *));
124 static void redirect_tablejump
PARAMS ((rtx
, rtx
));
125 static void jump_optimize_1
PARAMS ((rtx
, int, int, int, int, int));
126 static int returnjump_p_1
PARAMS ((rtx
*, void *));
127 static void delete_prior_computation
PARAMS ((rtx
, rtx
));
129 /* Main external entry point into the jump optimizer. See comments before
130 jump_optimize_1 for descriptions of the arguments. */
132 jump_optimize (f
, cross_jump
, noop_moves
, after_regscan
)
138 jump_optimize_1 (f
, cross_jump
, noop_moves
, after_regscan
, 0, 0);
141 /* Alternate entry into the jump optimizer. This entry point only rebuilds
142 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
145 rebuild_jump_labels (f
)
148 jump_optimize_1 (f
, 0, 0, 0, 1, 0);
151 /* Alternate entry into the jump optimizer. Do only trivial optimizations. */
154 jump_optimize_minimal (f
)
157 jump_optimize_1 (f
, 0, 0, 0, 0, 1);
160 /* Delete no-op jumps and optimize jumps to jumps
161 and jumps around jumps.
162 Delete unused labels and unreachable code.
164 If CROSS_JUMP is 1, detect matching code
165 before a jump and its destination and unify them.
166 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
168 If NOOP_MOVES is nonzero, delete no-op move insns.
170 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
171 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
173 If MARK_LABELS_ONLY is nonzero, then we only rebuild the jump chain
174 and JUMP_LABEL field for jumping insns.
176 If `optimize' is zero, don't change any code,
177 just determine whether control drops off the end of the function.
178 This case occurs when we have -W and not -O.
179 It works because `delete_insn' checks the value of `optimize'
180 and refrains from actually deleting when that is 0.
182 If MINIMAL is nonzero, then we only perform trivial optimizations:
184 * Removal of unreachable code after BARRIERs.
185 * Removal of unreferenced CODE_LABELs.
186 * Removal of a jump to the next instruction.
187 * Removal of a conditional jump followed by an unconditional jump
188 to the same target as the conditional jump.
189 * Simplify a conditional jump around an unconditional jump.
190 * Simplify a jump to a jump.
191 * Delete extraneous line number notes.
195 jump_optimize_1 (f
, cross_jump
, noop_moves
, after_regscan
,
196 mark_labels_only
, minimal
)
201 int mark_labels_only
;
204 register rtx insn
, next
;
211 enum rtx_code reversed_code
;
214 cross_jump_death_matters
= (cross_jump
== 2);
215 max_uid
= init_label_info (f
) + 1;
217 /* Leave some extra room for labels and duplicate exit test insns
219 max_jump_chain
= max_uid
* 14 / 10;
220 jump_chain
= (rtx
*) xcalloc (max_jump_chain
, sizeof (rtx
));
222 mark_all_labels (f
, cross_jump
);
224 /* Keep track of labels used from static data; we don't track them
225 closely enough to delete them here, so make sure their reference
226 count doesn't drop to zero. */
228 for (insn
= forced_labels
; insn
; insn
= XEXP (insn
, 1))
229 if (GET_CODE (XEXP (insn
, 0)) == CODE_LABEL
)
230 LABEL_NUSES (XEXP (insn
, 0))++;
232 /* Keep track of labels used for marking handlers for exception
233 regions; they cannot usually be deleted. */
235 for (insn
= exception_handler_labels
; insn
; insn
= XEXP (insn
, 1))
236 if (GET_CODE (XEXP (insn
, 0)) == CODE_LABEL
)
237 LABEL_NUSES (XEXP (insn
, 0))++;
239 if (! mark_labels_only
)
240 delete_barrier_successors (f
);
242 /* Quit now if we just wanted to rebuild the JUMP_LABEL and REG_LABEL
243 notes and recompute LABEL_NUSES. */
244 if (mark_labels_only
)
247 last_insn
= delete_unreferenced_labels (f
);
250 delete_noop_moves (f
);
252 /* Now iterate optimizing jumps until nothing changes over one pass. */
254 old_max_reg
= max_reg_num ();
259 for (insn
= f
; insn
; insn
= next
)
262 rtx temp
, temp1
, temp2
= NULL_RTX
;
263 rtx temp4 ATTRIBUTE_UNUSED
;
265 int this_is_any_uncondjump
;
266 int this_is_any_condjump
;
267 int this_is_onlyjump
;
269 next
= NEXT_INSN (insn
);
271 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
272 jump. Try to optimize by duplicating the loop exit test if so.
273 This is only safe immediately after regscan, because it uses
274 the values of regno_first_uid and regno_last_uid. */
275 if (after_regscan
&& GET_CODE (insn
) == NOTE
276 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
277 && (temp1
= next_nonnote_insn (insn
)) != 0
278 && any_uncondjump_p (temp1
)
279 && onlyjump_p (temp1
))
281 temp
= PREV_INSN (insn
);
282 if (duplicate_loop_exit_test (insn
))
285 next
= NEXT_INSN (temp
);
290 if (GET_CODE (insn
) != JUMP_INSN
)
293 this_is_any_condjump
= any_condjump_p (insn
);
294 this_is_any_uncondjump
= any_uncondjump_p (insn
);
295 this_is_onlyjump
= onlyjump_p (insn
);
297 /* Tension the labels in dispatch tables. */
299 if (GET_CODE (PATTERN (insn
)) == ADDR_VEC
)
300 changed
|= tension_vector_labels (PATTERN (insn
), 0);
301 if (GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
)
302 changed
|= tension_vector_labels (PATTERN (insn
), 1);
304 /* See if this jump goes to another jump and redirect if so. */
305 nlabel
= follow_jumps (JUMP_LABEL (insn
));
306 if (nlabel
!= JUMP_LABEL (insn
))
307 changed
|= redirect_jump (insn
, nlabel
, 1);
309 if (! optimize
|| minimal
)
312 /* If a dispatch table always goes to the same place,
313 get rid of it and replace the insn that uses it. */
315 if (GET_CODE (PATTERN (insn
)) == ADDR_VEC
316 || GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
)
319 rtx pat
= PATTERN (insn
);
320 int diff_vec_p
= GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
;
321 int len
= XVECLEN (pat
, diff_vec_p
);
322 rtx dispatch
= prev_real_insn (insn
);
325 for (i
= 0; i
< len
; i
++)
326 if (XEXP (XVECEXP (pat
, diff_vec_p
, i
), 0)
327 != XEXP (XVECEXP (pat
, diff_vec_p
, 0), 0))
332 && GET_CODE (dispatch
) == JUMP_INSN
333 && JUMP_LABEL (dispatch
) != 0
334 /* Don't mess with a casesi insn.
335 XXX according to the comment before computed_jump_p(),
336 all casesi insns should be a parallel of the jump
337 and a USE of a LABEL_REF. */
338 && ! ((set
= single_set (dispatch
)) != NULL
339 && (GET_CODE (SET_SRC (set
)) == IF_THEN_ELSE
))
340 && next_real_insn (JUMP_LABEL (dispatch
)) == insn
)
342 redirect_tablejump (dispatch
,
343 XEXP (XVECEXP (pat
, diff_vec_p
, 0), 0));
348 reallabelprev
= prev_active_insn (JUMP_LABEL (insn
));
350 /* Detect jump to following insn. */
351 if (reallabelprev
== insn
352 && (this_is_any_condjump
|| this_is_any_uncondjump
)
355 next
= next_real_insn (JUMP_LABEL (insn
));
358 /* Remove the "inactive" but "real" insns (i.e. uses and
359 clobbers) in between here and there. */
361 while ((temp
= next_real_insn (temp
)) != next
)
368 /* Detect a conditional jump going to the same place
369 as an immediately following unconditional jump. */
370 else if (this_is_any_condjump
&& this_is_onlyjump
371 && (temp
= next_active_insn (insn
)) != 0
372 && simplejump_p (temp
)
373 && (next_active_insn (JUMP_LABEL (insn
))
374 == next_active_insn (JUMP_LABEL (temp
))))
376 /* Don't mess up test coverage analysis. */
378 if (flag_test_coverage
&& !reload_completed
)
379 for (temp2
= insn
; temp2
!= temp
; temp2
= NEXT_INSN (temp2
))
380 if (GET_CODE (temp2
) == NOTE
&& NOTE_LINE_NUMBER (temp2
) > 0)
385 /* Ensure that we jump to the later of the two labels.
396 If we leave the goto L1, we'll incorrectly leave
397 return-reg dead for TEST true. */
399 temp2
= next_active_insn (JUMP_LABEL (insn
));
401 temp2
= get_last_insn ();
402 if (GET_CODE (temp2
) != CODE_LABEL
)
403 temp2
= prev_label (temp2
);
404 if (temp2
!= JUMP_LABEL (temp
))
405 redirect_jump (temp
, temp2
, 1);
413 /* Detect a conditional jump jumping over an unconditional jump. */
415 else if (this_is_any_condjump
416 && reallabelprev
!= 0
417 && GET_CODE (reallabelprev
) == JUMP_INSN
418 && prev_active_insn (reallabelprev
) == insn
419 && no_labels_between_p (insn
, reallabelprev
)
420 && any_uncondjump_p (reallabelprev
)
421 && onlyjump_p (reallabelprev
))
423 /* When we invert the unconditional jump, we will be
424 decrementing the usage count of its old label.
425 Make sure that we don't delete it now because that
426 might cause the following code to be deleted. */
427 rtx prev_uses
= prev_nonnote_insn (reallabelprev
);
428 rtx prev_label
= JUMP_LABEL (insn
);
431 ++LABEL_NUSES (prev_label
);
433 if (invert_jump (insn
, JUMP_LABEL (reallabelprev
), 1))
435 /* It is very likely that if there are USE insns before
436 this jump, they hold REG_DEAD notes. These REG_DEAD
437 notes are no longer valid due to this optimization,
438 and will cause the life-analysis that following passes
439 (notably delayed-branch scheduling) to think that
440 these registers are dead when they are not.
442 To prevent this trouble, we just remove the USE insns
443 from the insn chain. */
445 while (prev_uses
&& GET_CODE (prev_uses
) == INSN
446 && GET_CODE (PATTERN (prev_uses
)) == USE
)
448 rtx useless
= prev_uses
;
449 prev_uses
= prev_nonnote_insn (prev_uses
);
450 delete_insn (useless
);
453 delete_insn (reallabelprev
);
457 /* We can now safely delete the label if it is unreferenced
458 since the delete_insn above has deleted the BARRIER. */
459 if (prev_label
&& --LABEL_NUSES (prev_label
) == 0)
460 delete_insn (prev_label
);
462 next
= NEXT_INSN (insn
);
465 /* If we have an unconditional jump preceded by a USE, try to put
466 the USE before the target and jump there. This simplifies many
467 of the optimizations below since we don't have to worry about
468 dealing with these USE insns. We only do this if the label
469 being branch to already has the identical USE or if code
470 never falls through to that label. */
472 else if (this_is_any_uncondjump
473 && (temp
= prev_nonnote_insn (insn
)) != 0
474 && GET_CODE (temp
) == INSN
475 && GET_CODE (PATTERN (temp
)) == USE
476 && (temp1
= prev_nonnote_insn (JUMP_LABEL (insn
))) != 0
477 && (GET_CODE (temp1
) == BARRIER
478 || (GET_CODE (temp1
) == INSN
479 && rtx_equal_p (PATTERN (temp
), PATTERN (temp1
))))
480 /* Don't do this optimization if we have a loop containing
481 only the USE instruction, and the loop start label has
482 a usage count of 1. This is because we will redo this
483 optimization everytime through the outer loop, and jump
484 opt will never exit. */
485 && ! ((temp2
= prev_nonnote_insn (temp
)) != 0
486 && temp2
== JUMP_LABEL (insn
)
487 && LABEL_NUSES (temp2
) == 1))
489 if (GET_CODE (temp1
) == BARRIER
)
491 emit_insn_after (PATTERN (temp
), temp1
);
492 temp1
= NEXT_INSN (temp1
);
496 redirect_jump (insn
, get_label_before (temp1
), 1);
497 reallabelprev
= prev_real_insn (temp1
);
499 next
= NEXT_INSN (insn
);
503 /* Detect a conditional jump jumping over an unconditional trap. */
505 && this_is_any_condjump
&& this_is_onlyjump
506 && reallabelprev
!= 0
507 && GET_CODE (reallabelprev
) == INSN
508 && GET_CODE (PATTERN (reallabelprev
)) == TRAP_IF
509 && TRAP_CONDITION (PATTERN (reallabelprev
)) == const_true_rtx
510 && prev_active_insn (reallabelprev
) == insn
511 && no_labels_between_p (insn
, reallabelprev
)
512 && (temp2
= get_condition (insn
, &temp4
))
513 && ((reversed_code
= reversed_comparison_code (temp2
, insn
))
516 rtx
new = gen_cond_trap (reversed_code
,
517 XEXP (temp2
, 0), XEXP (temp2
, 1),
518 TRAP_CODE (PATTERN (reallabelprev
)));
522 emit_insn_before (new, temp4
);
523 delete_insn (reallabelprev
);
529 /* Detect a jump jumping to an unconditional trap. */
530 else if (HAVE_trap
&& this_is_onlyjump
531 && (temp
= next_active_insn (JUMP_LABEL (insn
)))
532 && GET_CODE (temp
) == INSN
533 && GET_CODE (PATTERN (temp
)) == TRAP_IF
534 && (this_is_any_uncondjump
535 || (this_is_any_condjump
536 && (temp2
= get_condition (insn
, &temp4
)))))
538 rtx tc
= TRAP_CONDITION (PATTERN (temp
));
540 if (tc
== const_true_rtx
541 || (! this_is_any_uncondjump
&& rtx_equal_p (temp2
, tc
)))
544 /* Replace an unconditional jump to a trap with a trap. */
545 if (this_is_any_uncondjump
)
547 emit_barrier_after (emit_insn_before (gen_trap (), insn
));
552 new = gen_cond_trap (GET_CODE (temp2
), XEXP (temp2
, 0),
554 TRAP_CODE (PATTERN (temp
)));
557 emit_insn_before (new, temp4
);
563 /* If the trap condition and jump condition are mutually
564 exclusive, redirect the jump to the following insn. */
565 else if (GET_RTX_CLASS (GET_CODE (tc
)) == '<'
566 && this_is_any_condjump
567 && swap_condition (GET_CODE (temp2
)) == GET_CODE (tc
)
568 && rtx_equal_p (XEXP (tc
, 0), XEXP (temp2
, 0))
569 && rtx_equal_p (XEXP (tc
, 1), XEXP (temp2
, 1))
570 && redirect_jump (insn
, get_label_after (temp
), 1))
579 /* Now that the jump has been tensioned,
580 try cross jumping: check for identical code
581 before the jump and before its target label. */
583 /* First, cross jumping of conditional jumps: */
585 if (cross_jump
&& condjump_p (insn
))
587 rtx newjpos
, newlpos
;
588 rtx x
= prev_real_insn (JUMP_LABEL (insn
));
590 /* A conditional jump may be crossjumped
591 only if the place it jumps to follows
592 an opposing jump that comes back here. */
594 if (x
!= 0 && ! jump_back_p (x
, insn
))
595 /* We have no opposing jump;
596 cannot cross jump this insn. */
600 /* TARGET is nonzero if it is ok to cross jump
601 to code before TARGET. If so, see if matches. */
603 find_cross_jump (insn
, x
, 2,
608 do_cross_jump (insn
, newjpos
, newlpos
);
609 /* Make the old conditional jump
610 into an unconditional one. */
611 PATTERN (insn
) = gen_jump (JUMP_LABEL (insn
));
612 INSN_CODE (insn
) = -1;
613 emit_barrier_after (insn
);
614 /* Add to jump_chain unless this is a new label
615 whose UID is too large. */
616 if (INSN_UID (JUMP_LABEL (insn
)) < max_jump_chain
)
618 jump_chain
[INSN_UID (insn
)]
619 = jump_chain
[INSN_UID (JUMP_LABEL (insn
))];
620 jump_chain
[INSN_UID (JUMP_LABEL (insn
))] = insn
;
627 /* Cross jumping of unconditional jumps:
628 a few differences. */
630 if (cross_jump
&& simplejump_p (insn
))
632 rtx newjpos
, newlpos
;
637 /* TARGET is nonzero if it is ok to cross jump
638 to code before TARGET. If so, see if matches. */
639 find_cross_jump (insn
, JUMP_LABEL (insn
), 1,
642 /* If cannot cross jump to code before the label,
643 see if we can cross jump to another jump to
645 /* Try each other jump to this label. */
646 if (INSN_UID (JUMP_LABEL (insn
)) < max_uid
)
647 for (target
= jump_chain
[INSN_UID (JUMP_LABEL (insn
))];
648 target
!= 0 && newjpos
== 0;
649 target
= jump_chain
[INSN_UID (target
)])
651 && JUMP_LABEL (target
) == JUMP_LABEL (insn
)
652 /* Ignore TARGET if it's deleted. */
653 && ! INSN_DELETED_P (target
))
654 find_cross_jump (insn
, target
, 2,
659 do_cross_jump (insn
, newjpos
, newlpos
);
665 /* This code was dead in the previous jump.c! */
666 if (cross_jump
&& GET_CODE (PATTERN (insn
)) == RETURN
)
668 /* Return insns all "jump to the same place"
669 so we can cross-jump between any two of them. */
671 rtx newjpos
, newlpos
, target
;
675 /* If cannot cross jump to code before the label,
676 see if we can cross jump to another jump to
678 /* Try each other jump to this label. */
679 for (target
= jump_chain
[0];
680 target
!= 0 && newjpos
== 0;
681 target
= jump_chain
[INSN_UID (target
)])
683 && ! INSN_DELETED_P (target
)
684 && GET_CODE (PATTERN (target
)) == RETURN
)
685 find_cross_jump (insn
, target
, 2,
690 do_cross_jump (insn
, newjpos
, newlpos
);
701 /* Delete extraneous line number notes.
702 Note that two consecutive notes for different lines are not really
703 extraneous. There should be some indication where that line belonged,
704 even if it became empty. */
709 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
710 if (GET_CODE (insn
) == NOTE
)
712 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_FUNCTION_BEG
)
713 /* Any previous line note was for the prologue; gdb wants a new
714 note after the prologue even if it is for the same line. */
715 last_note
= NULL_RTX
;
716 else if (NOTE_LINE_NUMBER (insn
) >= 0)
718 /* Delete this note if it is identical to previous note. */
720 && NOTE_SOURCE_FILE (insn
) == NOTE_SOURCE_FILE (last_note
)
721 && NOTE_LINE_NUMBER (insn
) == NOTE_LINE_NUMBER (last_note
))
738 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
739 notes whose labels don't occur in the insn any more. Returns the
740 largest INSN_UID found. */
748 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
750 if (GET_CODE (insn
) == CODE_LABEL
)
751 LABEL_NUSES (insn
) = (LABEL_PRESERVE_P (insn
) != 0);
752 else if (GET_CODE (insn
) == JUMP_INSN
)
753 JUMP_LABEL (insn
) = 0;
754 else if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
758 for (note
= REG_NOTES (insn
); note
; note
= next
)
760 next
= XEXP (note
, 1);
761 if (REG_NOTE_KIND (note
) == REG_LABEL
762 && ! reg_mentioned_p (XEXP (note
, 0), PATTERN (insn
)))
763 remove_note (insn
, note
);
766 if (INSN_UID (insn
) > largest_uid
)
767 largest_uid
= INSN_UID (insn
);
773 /* Delete insns following barriers, up to next label.
775 Also delete no-op jumps created by gcse. */
778 delete_barrier_successors (f
)
784 for (insn
= f
; insn
;)
786 if (GET_CODE (insn
) == BARRIER
)
788 insn
= NEXT_INSN (insn
);
790 never_reached_warning (insn
);
792 while (insn
!= 0 && GET_CODE (insn
) != CODE_LABEL
)
794 if (GET_CODE (insn
) == JUMP_INSN
)
796 /* Detect when we're deleting a tablejump; get rid of
797 the jump table as well. */
798 rtx next1
= next_nonnote_insn (insn
);
799 rtx next2
= next1
? next_nonnote_insn (next1
) : 0;
800 if (next2
&& GET_CODE (next1
) == CODE_LABEL
801 && GET_CODE (next2
) == JUMP_INSN
802 && (GET_CODE (PATTERN (next2
)) == ADDR_VEC
803 || GET_CODE (PATTERN (next2
)) == ADDR_DIFF_VEC
))
809 insn
= delete_insn (insn
);
811 else if (GET_CODE (insn
) == NOTE
812 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_FUNCTION_END
)
813 insn
= NEXT_INSN (insn
);
815 insn
= delete_insn (insn
);
817 /* INSN is now the code_label. */
820 /* Also remove (set (pc) (pc)) insns which can be created by
821 gcse. We eliminate such insns now to avoid having them
822 cause problems later. */
823 else if (GET_CODE (insn
) == JUMP_INSN
824 && (set
= pc_set (insn
)) != NULL
825 && SET_SRC (set
) == pc_rtx
826 && SET_DEST (set
) == pc_rtx
827 && onlyjump_p (insn
))
828 insn
= delete_insn (insn
);
831 insn
= NEXT_INSN (insn
);
835 /* Mark the label each jump jumps to.
836 Combine consecutive labels, and count uses of labels.
838 For each label, make a chain (using `jump_chain')
839 of all the *unconditional* jumps that jump to it;
840 also make a chain of all returns.
842 CROSS_JUMP indicates whether we are doing cross jumping
843 and if we are whether we will be paying attention to
844 death notes or not. */
847 mark_all_labels (f
, cross_jump
)
853 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
856 if (GET_CODE (insn
) == CALL_INSN
857 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
859 mark_all_labels (XEXP (PATTERN (insn
), 0), cross_jump
);
860 mark_all_labels (XEXP (PATTERN (insn
), 1), cross_jump
);
861 mark_all_labels (XEXP (PATTERN (insn
), 2), cross_jump
);
863 /* Canonicalize the tail recursion label attached to the
864 CALL_PLACEHOLDER insn. */
865 if (XEXP (PATTERN (insn
), 3))
867 rtx label_ref
= gen_rtx_LABEL_REF (VOIDmode
,
868 XEXP (PATTERN (insn
), 3));
869 mark_jump_label (label_ref
, insn
, cross_jump
, 0);
870 XEXP (PATTERN (insn
), 3) = XEXP (label_ref
, 0);
876 mark_jump_label (PATTERN (insn
), insn
, cross_jump
, 0);
877 if (! INSN_DELETED_P (insn
) && GET_CODE (insn
) == JUMP_INSN
)
879 /* When we know the LABEL_REF contained in a REG used in
880 an indirect jump, we'll have a REG_LABEL note so that
881 flow can tell where it's going. */
882 if (JUMP_LABEL (insn
) == 0)
884 rtx label_note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
887 /* But a LABEL_REF around the REG_LABEL note, so
888 that we can canonicalize it. */
889 rtx label_ref
= gen_rtx_LABEL_REF (VOIDmode
,
890 XEXP (label_note
, 0));
892 mark_jump_label (label_ref
, insn
, cross_jump
, 0);
893 XEXP (label_note
, 0) = XEXP (label_ref
, 0);
894 JUMP_LABEL (insn
) = XEXP (label_note
, 0);
897 if (JUMP_LABEL (insn
) != 0 && simplejump_p (insn
))
899 jump_chain
[INSN_UID (insn
)]
900 = jump_chain
[INSN_UID (JUMP_LABEL (insn
))];
901 jump_chain
[INSN_UID (JUMP_LABEL (insn
))] = insn
;
903 if (GET_CODE (PATTERN (insn
)) == RETURN
)
905 jump_chain
[INSN_UID (insn
)] = jump_chain
[0];
906 jump_chain
[0] = insn
;
912 /* Delete all labels already not referenced.
913 Also find and return the last insn. */
916 delete_unreferenced_labels (f
)
919 rtx final
= NULL_RTX
;
922 for (insn
= f
; insn
;)
924 if (GET_CODE (insn
) == CODE_LABEL
925 && LABEL_NUSES (insn
) == 0
926 && LABEL_ALTERNATE_NAME (insn
) == NULL
)
927 insn
= delete_insn (insn
);
931 insn
= NEXT_INSN (insn
);
938 /* Delete various simple forms of moves which have no necessary
942 delete_noop_moves (f
)
947 for (insn
= f
; insn
;)
949 next
= NEXT_INSN (insn
);
951 if (GET_CODE (insn
) == INSN
)
953 register rtx body
= PATTERN (insn
);
955 /* Detect and delete no-op move instructions
956 resulting from not allocating a parameter in a register. */
958 if (GET_CODE (body
) == SET
&& set_noop_p (body
))
959 delete_computation (insn
);
961 /* Detect and ignore no-op move instructions
962 resulting from smart or fortuitous register allocation. */
964 else if (GET_CODE (body
) == SET
)
966 int sreg
= true_regnum (SET_SRC (body
));
967 int dreg
= true_regnum (SET_DEST (body
));
969 if (sreg
== dreg
&& sreg
>= 0)
971 else if (sreg
>= 0 && dreg
>= 0)
974 rtx tem
= find_equiv_reg (NULL_RTX
, insn
, 0,
976 GET_MODE (SET_SRC (body
)));
979 && GET_MODE (tem
) == GET_MODE (SET_DEST (body
)))
981 /* DREG may have been the target of a REG_DEAD note in
982 the insn which makes INSN redundant. If so, reorg
983 would still think it is dead. So search for such a
984 note and delete it if we find it. */
985 if (! find_regno_note (insn
, REG_UNUSED
, dreg
))
986 for (trial
= prev_nonnote_insn (insn
);
987 trial
&& GET_CODE (trial
) != CODE_LABEL
;
988 trial
= prev_nonnote_insn (trial
))
989 if (find_regno_note (trial
, REG_DEAD
, dreg
))
991 remove_death (dreg
, trial
);
995 /* Deleting insn could lose a death-note for SREG. */
996 if ((trial
= find_regno_note (insn
, REG_DEAD
, sreg
)))
998 /* Change this into a USE so that we won't emit
999 code for it, but still can keep the note. */
1001 = gen_rtx_USE (VOIDmode
, XEXP (trial
, 0));
1002 INSN_CODE (insn
) = -1;
1003 /* Remove all reg notes but the REG_DEAD one. */
1004 REG_NOTES (insn
) = trial
;
1005 XEXP (trial
, 1) = NULL_RTX
;
1011 else if (dreg
>= 0 && CONSTANT_P (SET_SRC (body
))
1012 && find_equiv_reg (SET_SRC (body
), insn
, 0, dreg
,
1013 NULL
, 0, GET_MODE (SET_DEST (body
))))
1015 /* This handles the case where we have two consecutive
1016 assignments of the same constant to pseudos that didn't
1017 get a hard reg. Each SET from the constant will be
1018 converted into a SET of the spill register and an
1019 output reload will be made following it. This produces
1020 two loads of the same constant into the same spill
1025 /* Look back for a death note for the first reg.
1026 If there is one, it is no longer accurate. */
1027 while (in_insn
&& GET_CODE (in_insn
) != CODE_LABEL
)
1029 if ((GET_CODE (in_insn
) == INSN
1030 || GET_CODE (in_insn
) == JUMP_INSN
)
1031 && find_regno_note (in_insn
, REG_DEAD
, dreg
))
1033 remove_death (dreg
, in_insn
);
1036 in_insn
= PREV_INSN (in_insn
);
1039 /* Delete the second load of the value. */
1043 else if (GET_CODE (body
) == PARALLEL
)
1045 /* If each part is a set between two identical registers or
1046 a USE or CLOBBER, delete the insn. */
1050 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; i
--)
1052 tem
= XVECEXP (body
, 0, i
);
1053 if (GET_CODE (tem
) == USE
|| GET_CODE (tem
) == CLOBBER
)
1056 if (GET_CODE (tem
) != SET
1057 || (sreg
= true_regnum (SET_SRC (tem
))) < 0
1058 || (dreg
= true_regnum (SET_DEST (tem
))) < 0
1071 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
1072 jump. Assume that this unconditional jump is to the exit test code. If
1073 the code is sufficiently simple, make a copy of it before INSN,
1074 followed by a jump to the exit of the loop. Then delete the unconditional
1077 Return 1 if we made the change, else 0.
1079 This is only safe immediately after a regscan pass because it uses the
1080 values of regno_first_uid and regno_last_uid. */
1083 duplicate_loop_exit_test (loop_start
)
1086 rtx insn
, set
, reg
, p
, link
;
1087 rtx copy
= 0, first_copy
= 0;
1089 rtx exitcode
= NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start
)));
1091 int max_reg
= max_reg_num ();
1094 /* Scan the exit code. We do not perform this optimization if any insn:
1098 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
1099 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
1100 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
1103 We also do not do this if we find an insn with ASM_OPERANDS. While
1104 this restriction should not be necessary, copying an insn with
1105 ASM_OPERANDS can confuse asm_noperands in some cases.
1107 Also, don't do this if the exit code is more than 20 insns. */
1109 for (insn
= exitcode
;
1111 && ! (GET_CODE (insn
) == NOTE
1112 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
);
1113 insn
= NEXT_INSN (insn
))
1115 switch (GET_CODE (insn
))
1121 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
1122 a jump immediately after the loop start that branches outside
1123 the loop but within an outer loop, near the exit test.
1124 If we copied this exit test and created a phony
1125 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
1126 before the exit test look like these could be safely moved
1127 out of the loop even if they actually may be never executed.
1128 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
1130 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
1131 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_CONT
)
1135 && (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_BEG
1136 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_END
))
1137 /* If we were to duplicate this code, we would not move
1138 the BLOCK notes, and so debugging the moved code would
1139 be difficult. Thus, we only move the code with -O2 or
1146 /* The code below would grossly mishandle REG_WAS_0 notes,
1147 so get rid of them here. */
1148 while ((p
= find_reg_note (insn
, REG_WAS_0
, NULL_RTX
)) != 0)
1149 remove_note (insn
, p
);
1150 if (++num_insns
> 20
1151 || find_reg_note (insn
, REG_RETVAL
, NULL_RTX
)
1152 || find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
))
1160 /* Unless INSN is zero, we can do the optimization. */
1166 /* See if any insn sets a register only used in the loop exit code and
1167 not a user variable. If so, replace it with a new register. */
1168 for (insn
= exitcode
; insn
!= lastexit
; insn
= NEXT_INSN (insn
))
1169 if (GET_CODE (insn
) == INSN
1170 && (set
= single_set (insn
)) != 0
1171 && ((reg
= SET_DEST (set
), GET_CODE (reg
) == REG
)
1172 || (GET_CODE (reg
) == SUBREG
1173 && (reg
= SUBREG_REG (reg
), GET_CODE (reg
) == REG
)))
1174 && REGNO (reg
) >= FIRST_PSEUDO_REGISTER
1175 && REGNO_FIRST_UID (REGNO (reg
)) == INSN_UID (insn
))
1177 for (p
= NEXT_INSN (insn
); p
!= lastexit
; p
= NEXT_INSN (p
))
1178 if (REGNO_LAST_UID (REGNO (reg
)) == INSN_UID (p
))
1183 /* We can do the replacement. Allocate reg_map if this is the
1184 first replacement we found. */
1186 reg_map
= (rtx
*) xcalloc (max_reg
, sizeof (rtx
));
1188 REG_LOOP_TEST_P (reg
) = 1;
1190 reg_map
[REGNO (reg
)] = gen_reg_rtx (GET_MODE (reg
));
1194 /* Now copy each insn. */
1195 for (insn
= exitcode
; insn
!= lastexit
; insn
= NEXT_INSN (insn
))
1197 switch (GET_CODE (insn
))
1200 copy
= emit_barrier_before (loop_start
);
1203 /* Only copy line-number notes. */
1204 if (NOTE_LINE_NUMBER (insn
) >= 0)
1206 copy
= emit_note_before (NOTE_LINE_NUMBER (insn
), loop_start
);
1207 NOTE_SOURCE_FILE (copy
) = NOTE_SOURCE_FILE (insn
);
1212 copy
= emit_insn_before (copy_insn (PATTERN (insn
)), loop_start
);
1214 replace_regs (PATTERN (copy
), reg_map
, max_reg
, 1);
1216 mark_jump_label (PATTERN (copy
), copy
, 0, 0);
1218 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
1220 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
1221 if (REG_NOTE_KIND (link
) != REG_LABEL
)
1223 if (GET_CODE (link
) == EXPR_LIST
)
1225 = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link
),
1230 = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link
),
1235 if (reg_map
&& REG_NOTES (copy
))
1236 replace_regs (REG_NOTES (copy
), reg_map
, max_reg
, 1);
1240 copy
= emit_jump_insn_before (copy_insn (PATTERN (insn
)),
1243 replace_regs (PATTERN (copy
), reg_map
, max_reg
, 1);
1244 mark_jump_label (PATTERN (copy
), copy
, 0, 0);
1245 if (REG_NOTES (insn
))
1247 REG_NOTES (copy
) = copy_insn_1 (REG_NOTES (insn
));
1249 replace_regs (REG_NOTES (copy
), reg_map
, max_reg
, 1);
1252 /* If this is a simple jump, add it to the jump chain. */
1254 if (INSN_UID (copy
) < max_jump_chain
&& JUMP_LABEL (copy
)
1255 && simplejump_p (copy
))
1257 jump_chain
[INSN_UID (copy
)]
1258 = jump_chain
[INSN_UID (JUMP_LABEL (copy
))];
1259 jump_chain
[INSN_UID (JUMP_LABEL (copy
))] = copy
;
1267 /* Record the first insn we copied. We need it so that we can
1268 scan the copied insns for new pseudo registers. */
1273 /* Now clean up by emitting a jump to the end label and deleting the jump
1274 at the start of the loop. */
1275 if (! copy
|| GET_CODE (copy
) != BARRIER
)
1277 copy
= emit_jump_insn_before (gen_jump (get_label_after (insn
)),
1280 /* Record the first insn we copied. We need it so that we can
1281 scan the copied insns for new pseudo registers. This may not
1282 be strictly necessary since we should have copied at least one
1283 insn above. But I am going to be safe. */
1287 mark_jump_label (PATTERN (copy
), copy
, 0, 0);
1288 if (INSN_UID (copy
) < max_jump_chain
1289 && INSN_UID (JUMP_LABEL (copy
)) < max_jump_chain
)
1291 jump_chain
[INSN_UID (copy
)]
1292 = jump_chain
[INSN_UID (JUMP_LABEL (copy
))];
1293 jump_chain
[INSN_UID (JUMP_LABEL (copy
))] = copy
;
1295 emit_barrier_before (loop_start
);
1298 /* Now scan from the first insn we copied to the last insn we copied
1299 (copy) for new pseudo registers. Do this after the code to jump to
1300 the end label since that might create a new pseudo too. */
1301 reg_scan_update (first_copy
, copy
, max_reg
);
1303 /* Mark the exit code as the virtual top of the converted loop. */
1304 emit_note_before (NOTE_INSN_LOOP_VTOP
, exitcode
);
1306 delete_insn (next_nonnote_insn (loop_start
));
1315 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
1316 notes between START and END out before START. Assume that END is not
1317 such a note. START may be such a note. Returns the value of the new
1318 starting insn, which may be different if the original start was such a
1322 squeeze_notes (start
, end
)
1328 for (insn
= start
; insn
!= end
; insn
= next
)
1330 next
= NEXT_INSN (insn
);
1331 if (GET_CODE (insn
) == NOTE
1332 && (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_END
1333 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_BLOCK_BEG
1334 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
1335 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
1336 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_CONT
1337 || NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_VTOP
))
1343 rtx prev
= PREV_INSN (insn
);
1344 PREV_INSN (insn
) = PREV_INSN (start
);
1345 NEXT_INSN (insn
) = start
;
1346 NEXT_INSN (PREV_INSN (insn
)) = insn
;
1347 PREV_INSN (NEXT_INSN (insn
)) = insn
;
1348 NEXT_INSN (prev
) = next
;
1349 PREV_INSN (next
) = prev
;
1357 /* Compare the instructions before insn E1 with those before E2
1358 to find an opportunity for cross jumping.
1359 (This means detecting identical sequences of insns followed by
1360 jumps to the same place, or followed by a label and a jump
1361 to that label, and replacing one with a jump to the other.)
1363 Assume E1 is a jump that jumps to label E2
1364 (that is not always true but it might as well be).
1365 Find the longest possible equivalent sequences
1366 and store the first insns of those sequences into *F1 and *F2.
1367 Store zero there if no equivalent preceding instructions are found.
1369 We give up if we find a label in stream 1.
1370 Actually we could transfer that label into stream 2. */
1373 find_cross_jump (e1
, e2
, minimum
, f1
, f2
)
1378 register rtx i1
= e1
, i2
= e2
;
1379 register rtx p1
, p2
;
1382 rtx last1
= 0, last2
= 0;
1383 rtx afterlast1
= 0, afterlast2
= 0;
1390 i1
= prev_nonnote_insn (i1
);
1392 i2
= PREV_INSN (i2
);
1393 while (i2
&& (GET_CODE (i2
) == NOTE
|| GET_CODE (i2
) == CODE_LABEL
))
1394 i2
= PREV_INSN (i2
);
1399 /* Don't allow the range of insns preceding E1 or E2
1400 to include the other (E2 or E1). */
1401 if (i2
== e1
|| i1
== e2
)
1404 /* If we will get to this code by jumping, those jumps will be
1405 tensioned to go directly to the new label (before I2),
1406 so this cross-jumping won't cost extra. So reduce the minimum. */
1407 if (GET_CODE (i1
) == CODE_LABEL
)
1413 if (i2
== 0 || GET_CODE (i1
) != GET_CODE (i2
))
1419 /* If this is a CALL_INSN, compare register usage information.
1420 If we don't check this on stack register machines, the two
1421 CALL_INSNs might be merged leaving reg-stack.c with mismatching
1422 numbers of stack registers in the same basic block.
1423 If we don't check this on machines with delay slots, a delay slot may
1424 be filled that clobbers a parameter expected by the subroutine.
1426 ??? We take the simple route for now and assume that if they're
1427 equal, they were constructed identically. */
1429 if (GET_CODE (i1
) == CALL_INSN
1430 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1
),
1431 CALL_INSN_FUNCTION_USAGE (i2
)))
1435 /* If cross_jump_death_matters is not 0, the insn's mode
1436 indicates whether or not the insn contains any stack-like
1439 if (!lose
&& cross_jump_death_matters
&& stack_regs_mentioned (i1
))
1441 /* If register stack conversion has already been done, then
1442 death notes must also be compared before it is certain that
1443 the two instruction streams match. */
1446 HARD_REG_SET i1_regset
, i2_regset
;
1448 CLEAR_HARD_REG_SET (i1_regset
);
1449 CLEAR_HARD_REG_SET (i2_regset
);
1451 for (note
= REG_NOTES (i1
); note
; note
= XEXP (note
, 1))
1452 if (REG_NOTE_KIND (note
) == REG_DEAD
1453 && STACK_REG_P (XEXP (note
, 0)))
1454 SET_HARD_REG_BIT (i1_regset
, REGNO (XEXP (note
, 0)));
1456 for (note
= REG_NOTES (i2
); note
; note
= XEXP (note
, 1))
1457 if (REG_NOTE_KIND (note
) == REG_DEAD
1458 && STACK_REG_P (XEXP (note
, 0)))
1459 SET_HARD_REG_BIT (i2_regset
, REGNO (XEXP (note
, 0)));
1461 GO_IF_HARD_REG_EQUAL (i1_regset
, i2_regset
, done
);
1470 /* Don't allow old-style asm or volatile extended asms to be accepted
1471 for cross jumping purposes. It is conceptually correct to allow
1472 them, since cross-jumping preserves the dynamic instruction order
1473 even though it is changing the static instruction order. However,
1474 if an asm is being used to emit an assembler pseudo-op, such as
1475 the MIPS `.set reorder' pseudo-op, then the static instruction order
1476 matters and it must be preserved. */
1477 if (GET_CODE (p1
) == ASM_INPUT
|| GET_CODE (p2
) == ASM_INPUT
1478 || (GET_CODE (p1
) == ASM_OPERANDS
&& MEM_VOLATILE_P (p1
))
1479 || (GET_CODE (p2
) == ASM_OPERANDS
&& MEM_VOLATILE_P (p2
)))
1482 if (lose
|| GET_CODE (p1
) != GET_CODE (p2
)
1483 || ! rtx_renumbered_equal_p (p1
, p2
))
1485 /* The following code helps take care of G++ cleanups. */
1489 if (!lose
&& GET_CODE (p1
) == GET_CODE (p2
)
1490 && ((equiv1
= find_reg_note (i1
, REG_EQUAL
, NULL_RTX
)) != 0
1491 || (equiv1
= find_reg_note (i1
, REG_EQUIV
, NULL_RTX
)) != 0)
1492 && ((equiv2
= find_reg_note (i2
, REG_EQUAL
, NULL_RTX
)) != 0
1493 || (equiv2
= find_reg_note (i2
, REG_EQUIV
, NULL_RTX
)) != 0)
1494 /* If the equivalences are not to a constant, they may
1495 reference pseudos that no longer exist, so we can't
1497 && CONSTANT_P (XEXP (equiv1
, 0))
1498 && rtx_equal_p (XEXP (equiv1
, 0), XEXP (equiv2
, 0)))
1500 rtx s1
= single_set (i1
);
1501 rtx s2
= single_set (i2
);
1502 if (s1
!= 0 && s2
!= 0
1503 && rtx_renumbered_equal_p (SET_DEST (s1
), SET_DEST (s2
)))
1505 validate_change (i1
, &SET_SRC (s1
), XEXP (equiv1
, 0), 1);
1506 validate_change (i2
, &SET_SRC (s2
), XEXP (equiv2
, 0), 1);
1507 if (! rtx_renumbered_equal_p (p1
, p2
))
1509 else if (apply_change_group ())
1514 /* Insns fail to match; cross jumping is limited to the following
1518 /* Don't allow the insn after a compare to be shared by
1519 cross-jumping unless the compare is also shared.
1520 Here, if either of these non-matching insns is a compare,
1521 exclude the following insn from possible cross-jumping. */
1522 if (sets_cc0_p (p1
) || sets_cc0_p (p2
))
1523 last1
= afterlast1
, last2
= afterlast2
, ++minimum
;
1526 /* If cross-jumping here will feed a jump-around-jump
1527 optimization, this jump won't cost extra, so reduce
1529 if (GET_CODE (i1
) == JUMP_INSN
1531 && prev_real_insn (JUMP_LABEL (i1
)) == e1
)
1537 if (GET_CODE (p1
) != USE
&& GET_CODE (p1
) != CLOBBER
)
1539 /* Ok, this insn is potentially includable in a cross-jump here. */
1540 afterlast1
= last1
, afterlast2
= last2
;
1541 last1
= i1
, last2
= i2
, --minimum
;
1545 if (minimum
<= 0 && last1
!= 0 && last1
!= e1
)
1546 *f1
= last1
, *f2
= last2
;
1550 do_cross_jump (insn
, newjpos
, newlpos
)
1551 rtx insn
, newjpos
, newlpos
;
1553 /* Find an existing label at this point
1554 or make a new one if there is none. */
1555 register rtx label
= get_label_before (newlpos
);
1557 /* Make the same jump insn jump to the new point. */
1558 if (GET_CODE (PATTERN (insn
)) == RETURN
)
1560 /* Remove from jump chain of returns. */
1561 delete_from_jump_chain (insn
);
1562 /* Change the insn. */
1563 PATTERN (insn
) = gen_jump (label
);
1564 INSN_CODE (insn
) = -1;
1565 JUMP_LABEL (insn
) = label
;
1566 LABEL_NUSES (label
)++;
1567 /* Add to new the jump chain. */
1568 if (INSN_UID (label
) < max_jump_chain
1569 && INSN_UID (insn
) < max_jump_chain
)
1571 jump_chain
[INSN_UID (insn
)] = jump_chain
[INSN_UID (label
)];
1572 jump_chain
[INSN_UID (label
)] = insn
;
1576 redirect_jump (insn
, label
, 1);
1578 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
1579 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
1580 the NEWJPOS stream. */
1582 while (newjpos
!= insn
)
1586 for (lnote
= REG_NOTES (newlpos
); lnote
; lnote
= XEXP (lnote
, 1))
1587 if ((REG_NOTE_KIND (lnote
) == REG_EQUAL
1588 || REG_NOTE_KIND (lnote
) == REG_EQUIV
)
1589 && ! find_reg_note (newjpos
, REG_EQUAL
, XEXP (lnote
, 0))
1590 && ! find_reg_note (newjpos
, REG_EQUIV
, XEXP (lnote
, 0)))
1591 remove_note (newlpos
, lnote
);
1593 delete_insn (newjpos
);
1594 newjpos
= next_real_insn (newjpos
);
1595 newlpos
= next_real_insn (newlpos
);
1599 /* Return the label before INSN, or put a new label there. */
1602 get_label_before (insn
)
1607 /* Find an existing label at this point
1608 or make a new one if there is none. */
1609 label
= prev_nonnote_insn (insn
);
1611 if (label
== 0 || GET_CODE (label
) != CODE_LABEL
)
1613 rtx prev
= PREV_INSN (insn
);
1615 label
= gen_label_rtx ();
1616 emit_label_after (label
, prev
);
1617 LABEL_NUSES (label
) = 0;
1622 /* Return the label after INSN, or put a new label there. */
1625 get_label_after (insn
)
1630 /* Find an existing label at this point
1631 or make a new one if there is none. */
1632 label
= next_nonnote_insn (insn
);
1634 if (label
== 0 || GET_CODE (label
) != CODE_LABEL
)
1636 label
= gen_label_rtx ();
1637 emit_label_after (label
, insn
);
1638 LABEL_NUSES (label
) = 0;
1643 /* Return 1 if INSN is a jump that jumps to right after TARGET
1644 only on the condition that TARGET itself would drop through.
1645 Assumes that TARGET is a conditional jump. */
1648 jump_back_p (insn
, target
)
1652 enum rtx_code codei
, codet
;
1655 if (! any_condjump_p (insn
)
1656 || any_uncondjump_p (target
)
1657 || target
!= prev_real_insn (JUMP_LABEL (insn
)))
1659 set
= pc_set (insn
);
1660 tset
= pc_set (target
);
1662 cinsn
= XEXP (SET_SRC (set
), 0);
1663 ctarget
= XEXP (SET_SRC (tset
), 0);
1665 codei
= GET_CODE (cinsn
);
1666 codet
= GET_CODE (ctarget
);
1668 if (XEXP (SET_SRC (set
), 1) == pc_rtx
)
1670 codei
= reversed_comparison_code (cinsn
, insn
);
1671 if (codei
== UNKNOWN
)
1675 if (XEXP (SET_SRC (tset
), 2) == pc_rtx
)
1677 codet
= reversed_comparison_code (ctarget
, target
);
1678 if (codei
== UNKNOWN
)
1682 return (codei
== codet
1683 && rtx_renumbered_equal_p (XEXP (cinsn
, 0), XEXP (ctarget
, 0))
1684 && rtx_renumbered_equal_p (XEXP (cinsn
, 1), XEXP (ctarget
, 1)));
1687 /* Given a comparison (CODE ARG0 ARG1), inside a insn, INSN, return an code
1688 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
1689 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
1690 know whether it's source is floating point or integer comparison. Machine
1691 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
1692 to help this function avoid overhead in these cases. */
1694 reversed_comparison_code_parts (code
, arg0
, arg1
, insn
)
1695 rtx insn
, arg0
, arg1
;
1698 enum machine_mode mode
;
1700 /* If this is not actually a comparison, we can't reverse it. */
1701 if (GET_RTX_CLASS (code
) != '<')
1704 mode
= GET_MODE (arg0
);
1705 if (mode
== VOIDmode
)
1706 mode
= GET_MODE (arg1
);
1708 /* First see if machine description supply us way to reverse the comparison.
1709 Give it priority over everything else to allow machine description to do
1711 #ifdef REVERSIBLE_CC_MODE
1712 if (GET_MODE_CLASS (mode
) == MODE_CC
1713 && REVERSIBLE_CC_MODE (mode
))
1715 #ifdef REVERSE_CONDITION
1716 return REVERSE_CONDITION (code
, mode
);
1718 return reverse_condition (code
);
1722 /* Try few special cases based on the comparison code. */
1731 /* It is always safe to reverse EQ and NE, even for the floating
1732 point. Similary the unsigned comparisons are never used for
1733 floating point so we can reverse them in the default way. */
1734 return reverse_condition (code
);
1739 /* In case we already see unordered comparison, we can be sure to
1740 be dealing with floating point so we don't need any more tests. */
1741 return reverse_condition_maybe_unordered (code
);
1746 /* We don't have safe way to reverse these yet. */
1752 /* In case we give up IEEE compatibility, all comparisons are reversible. */
1753 if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
1754 || flag_unsafe_math_optimizations
)
1755 return reverse_condition (code
);
1757 if (GET_MODE_CLASS (mode
) == MODE_CC
1764 /* Try to search for the comparison to determine the real mode.
1765 This code is expensive, but with sane machine description it
1766 will be never used, since REVERSIBLE_CC_MODE will return true
1771 for (prev
= prev_nonnote_insn (insn
);
1772 prev
!= 0 && GET_CODE (prev
) != CODE_LABEL
;
1773 prev
= prev_nonnote_insn (prev
))
1775 rtx set
= set_of (arg0
, prev
);
1776 if (set
&& GET_CODE (set
) == SET
1777 && rtx_equal_p (SET_DEST (set
), arg0
))
1779 rtx src
= SET_SRC (set
);
1781 if (GET_CODE (src
) == COMPARE
)
1783 rtx comparison
= src
;
1784 arg0
= XEXP (src
, 0);
1785 mode
= GET_MODE (arg0
);
1786 if (mode
== VOIDmode
)
1787 mode
= GET_MODE (XEXP (comparison
, 1));
1790 /* We can get past reg-reg moves. This may be usefull for model
1791 of i387 comparisons that first move flag registers around. */
1798 /* If register is clobbered in some ununderstandable way,
1805 /* An integer condition. */
1806 if (GET_CODE (arg0
) == CONST_INT
1807 || (GET_MODE (arg0
) != VOIDmode
1808 && GET_MODE_CLASS (mode
) != MODE_CC
1809 && ! FLOAT_MODE_P (mode
)))
1810 return reverse_condition (code
);
1815 /* An wrapper around the previous function to take COMPARISON as rtx
1816 expression. This simplifies many callers. */
1818 reversed_comparison_code (comparison
, insn
)
1819 rtx comparison
, insn
;
1821 if (GET_RTX_CLASS (GET_CODE (comparison
)) != '<')
1823 return reversed_comparison_code_parts (GET_CODE (comparison
),
1824 XEXP (comparison
, 0),
1825 XEXP (comparison
, 1), insn
);
1828 /* Given an rtx-code for a comparison, return the code for the negated
1829 comparison. If no such code exists, return UNKNOWN.
1831 WATCH OUT! reverse_condition is not safe to use on a jump that might
1832 be acting on the results of an IEEE floating point comparison, because
1833 of the special treatment of non-signaling nans in comparisons.
1834 Use reversed_comparison_code instead. */
1837 reverse_condition (code
)
1880 /* Similar, but we're allowed to generate unordered comparisons, which
1881 makes it safe for IEEE floating-point. Of course, we have to recognize
1882 that the target will support them too... */
1885 reverse_condition_maybe_unordered (code
)
1888 /* Non-IEEE formats don't have unordered conditions. */
1889 if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
)
1890 return reverse_condition (code
);
1928 /* Similar, but return the code when two operands of a comparison are swapped.
1929 This IS safe for IEEE floating-point. */
1932 swap_condition (code
)
1975 /* Given a comparison CODE, return the corresponding unsigned comparison.
1976 If CODE is an equality comparison or already an unsigned comparison,
1977 CODE is returned. */
1980 unsigned_condition (code
)
2007 /* Similarly, return the signed version of a comparison. */
2010 signed_condition (code
)
2037 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
2038 truth of CODE1 implies the truth of CODE2. */
2041 comparison_dominates_p (code1
, code2
)
2042 enum rtx_code code1
, code2
;
2044 /* UNKNOWN comparison codes can happen as a result of trying to revert
2046 They can't match anything, so we have to reject them here. */
2047 if (code1
== UNKNOWN
|| code2
== UNKNOWN
)
2056 if (code2
== UNLE
|| code2
== UNGE
)
2061 if (code2
== LE
|| code2
== LEU
|| code2
== GE
|| code2
== GEU
2062 || code2
== ORDERED
)
2067 if (code2
== UNLE
|| code2
== NE
)
2072 if (code2
== LE
|| code2
== NE
|| code2
== ORDERED
|| code2
== LTGT
)
2077 if (code2
== UNGE
|| code2
== NE
)
2082 if (code2
== GE
|| code2
== NE
|| code2
== ORDERED
|| code2
== LTGT
)
2088 if (code2
== ORDERED
)
2093 if (code2
== NE
|| code2
== ORDERED
)
2098 if (code2
== LEU
|| code2
== NE
)
2103 if (code2
== GEU
|| code2
== NE
)
2108 if (code2
== NE
|| code2
== UNEQ
|| code2
== UNLE
|| code2
== UNLT
2109 || code2
== UNGE
|| code2
== UNGT
)
2120 /* Return 1 if INSN is an unconditional jump and nothing else. */
2126 return (GET_CODE (insn
) == JUMP_INSN
2127 && GET_CODE (PATTERN (insn
)) == SET
2128 && GET_CODE (SET_DEST (PATTERN (insn
))) == PC
2129 && GET_CODE (SET_SRC (PATTERN (insn
))) == LABEL_REF
);
2132 /* Return nonzero if INSN is a (possibly) conditional jump
2135 Use this function is deprecated, since we need to support combined
2136 branch and compare insns. Use any_condjump_p instead whenever possible. */
2142 register rtx x
= PATTERN (insn
);
2144 if (GET_CODE (x
) != SET
2145 || GET_CODE (SET_DEST (x
)) != PC
)
2149 if (GET_CODE (x
) == LABEL_REF
)
2152 return (GET_CODE (x
) == IF_THEN_ELSE
2153 && ((GET_CODE (XEXP (x
, 2)) == PC
2154 && (GET_CODE (XEXP (x
, 1)) == LABEL_REF
2155 || GET_CODE (XEXP (x
, 1)) == RETURN
))
2156 || (GET_CODE (XEXP (x
, 1)) == PC
2157 && (GET_CODE (XEXP (x
, 2)) == LABEL_REF
2158 || GET_CODE (XEXP (x
, 2)) == RETURN
))));
2163 /* Return nonzero if INSN is a (possibly) conditional jump inside a
2166 Use this function is deprecated, since we need to support combined
2167 branch and compare insns. Use any_condjump_p instead whenever possible. */
2170 condjump_in_parallel_p (insn
)
2173 register rtx x
= PATTERN (insn
);
2175 if (GET_CODE (x
) != PARALLEL
)
2178 x
= XVECEXP (x
, 0, 0);
2180 if (GET_CODE (x
) != SET
)
2182 if (GET_CODE (SET_DEST (x
)) != PC
)
2184 if (GET_CODE (SET_SRC (x
)) == LABEL_REF
)
2186 if (GET_CODE (SET_SRC (x
)) != IF_THEN_ELSE
)
2188 if (XEXP (SET_SRC (x
), 2) == pc_rtx
2189 && (GET_CODE (XEXP (SET_SRC (x
), 1)) == LABEL_REF
2190 || GET_CODE (XEXP (SET_SRC (x
), 1)) == RETURN
))
2192 if (XEXP (SET_SRC (x
), 1) == pc_rtx
2193 && (GET_CODE (XEXP (SET_SRC (x
), 2)) == LABEL_REF
2194 || GET_CODE (XEXP (SET_SRC (x
), 2)) == RETURN
))
2199 /* Return set of PC, otherwise NULL. */
2206 if (GET_CODE (insn
) != JUMP_INSN
)
2208 pat
= PATTERN (insn
);
2210 /* The set is allowed to appear either as the insn pattern or
2211 the first set in a PARALLEL. */
2212 if (GET_CODE (pat
) == PARALLEL
)
2213 pat
= XVECEXP (pat
, 0, 0);
2214 if (GET_CODE (pat
) == SET
&& GET_CODE (SET_DEST (pat
)) == PC
)
2220 /* Return true when insn is an unconditional direct jump,
2221 possibly bundled inside a PARALLEL. */
2224 any_uncondjump_p (insn
)
2227 rtx x
= pc_set (insn
);
2230 if (GET_CODE (SET_SRC (x
)) != LABEL_REF
)
2235 /* Return true when insn is a conditional jump. This function works for
2236 instructions containing PC sets in PARALLELs. The instruction may have
2237 various other effects so before removing the jump you must verify
2240 Note that unlike condjump_p it returns false for unconditional jumps. */
2243 any_condjump_p (insn
)
2246 rtx x
= pc_set (insn
);
2251 if (GET_CODE (SET_SRC (x
)) != IF_THEN_ELSE
)
2254 a
= GET_CODE (XEXP (SET_SRC (x
), 1));
2255 b
= GET_CODE (XEXP (SET_SRC (x
), 2));
2257 return ((b
== PC
&& (a
== LABEL_REF
|| a
== RETURN
))
2258 || (a
== PC
&& (b
== LABEL_REF
|| b
== RETURN
)));
2261 /* Return the label of a conditional jump. */
2264 condjump_label (insn
)
2267 rtx x
= pc_set (insn
);
2272 if (GET_CODE (x
) == LABEL_REF
)
2274 if (GET_CODE (x
) != IF_THEN_ELSE
)
2276 if (XEXP (x
, 2) == pc_rtx
&& GET_CODE (XEXP (x
, 1)) == LABEL_REF
)
2278 if (XEXP (x
, 1) == pc_rtx
&& GET_CODE (XEXP (x
, 2)) == LABEL_REF
)
2283 /* Return true if INSN is a (possibly conditional) return insn. */
2286 returnjump_p_1 (loc
, data
)
2288 void *data ATTRIBUTE_UNUSED
;
2291 return x
&& GET_CODE (x
) == RETURN
;
2298 if (GET_CODE (insn
) != JUMP_INSN
)
2300 return for_each_rtx (&PATTERN (insn
), returnjump_p_1
, NULL
);
2303 /* Return true if INSN is a jump that only transfers control and
2312 if (GET_CODE (insn
) != JUMP_INSN
)
2315 set
= single_set (insn
);
2318 if (GET_CODE (SET_DEST (set
)) != PC
)
2320 if (side_effects_p (SET_SRC (set
)))
2328 /* Return 1 if X is an RTX that does nothing but set the condition codes
2329 and CLOBBER or USE registers.
2330 Return -1 if X does explicitly set the condition codes,
2331 but also does other things. */
2335 rtx x ATTRIBUTE_UNUSED
;
2337 if (GET_CODE (x
) == SET
&& SET_DEST (x
) == cc0_rtx
)
2339 if (GET_CODE (x
) == PARALLEL
)
2343 int other_things
= 0;
2344 for (i
= XVECLEN (x
, 0) - 1; i
>= 0; i
--)
2346 if (GET_CODE (XVECEXP (x
, 0, i
)) == SET
2347 && SET_DEST (XVECEXP (x
, 0, i
)) == cc0_rtx
)
2349 else if (GET_CODE (XVECEXP (x
, 0, i
)) == SET
)
2352 return ! sets_cc0
? 0 : other_things
? -1 : 1;
2358 /* Follow any unconditional jump at LABEL;
2359 return the ultimate label reached by any such chain of jumps.
2360 If LABEL is not followed by a jump, return LABEL.
2361 If the chain loops or we can't find end, return LABEL,
2362 since that tells caller to avoid changing the insn.
2364 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
2365 a USE or CLOBBER. */
2368 follow_jumps (label
)
2373 register rtx value
= label
;
2378 && (insn
= next_active_insn (value
)) != 0
2379 && GET_CODE (insn
) == JUMP_INSN
2380 && ((JUMP_LABEL (insn
) != 0 && any_uncondjump_p (insn
)
2381 && onlyjump_p (insn
))
2382 || GET_CODE (PATTERN (insn
)) == RETURN
)
2383 && (next
= NEXT_INSN (insn
))
2384 && GET_CODE (next
) == BARRIER
);
2387 /* Don't chain through the insn that jumps into a loop
2388 from outside the loop,
2389 since that would create multiple loop entry jumps
2390 and prevent loop optimization. */
2392 if (!reload_completed
)
2393 for (tem
= value
; tem
!= insn
; tem
= NEXT_INSN (tem
))
2394 if (GET_CODE (tem
) == NOTE
2395 && (NOTE_LINE_NUMBER (tem
) == NOTE_INSN_LOOP_BEG
2396 /* ??? Optional. Disables some optimizations, but makes
2397 gcov output more accurate with -O. */
2398 || (flag_test_coverage
&& NOTE_LINE_NUMBER (tem
) > 0)))
2401 /* If we have found a cycle, make the insn jump to itself. */
2402 if (JUMP_LABEL (insn
) == label
)
2405 tem
= next_active_insn (JUMP_LABEL (insn
));
2406 if (tem
&& (GET_CODE (PATTERN (tem
)) == ADDR_VEC
2407 || GET_CODE (PATTERN (tem
)) == ADDR_DIFF_VEC
))
2410 value
= JUMP_LABEL (insn
);
2417 /* Assuming that field IDX of X is a vector of label_refs,
2418 replace each of them by the ultimate label reached by it.
2419 Return nonzero if a change is made.
2420 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
2423 tension_vector_labels (x
, idx
)
2429 for (i
= XVECLEN (x
, idx
) - 1; i
>= 0; i
--)
2431 register rtx olabel
= XEXP (XVECEXP (x
, idx
, i
), 0);
2432 register rtx nlabel
= follow_jumps (olabel
);
2433 if (nlabel
&& nlabel
!= olabel
)
2435 XEXP (XVECEXP (x
, idx
, i
), 0) = nlabel
;
2436 ++LABEL_NUSES (nlabel
);
2437 if (--LABEL_NUSES (olabel
) == 0)
2438 delete_insn (olabel
);
2445 /* Find all CODE_LABELs referred to in X, and increment their use counts.
2446 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
2447 in INSN, then store one of them in JUMP_LABEL (INSN).
2448 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
2449 referenced in INSN, add a REG_LABEL note containing that label to INSN.
2450 Also, when there are consecutive labels, canonicalize on the last of them.
2452 Note that two labels separated by a loop-beginning note
2453 must be kept distinct if we have not yet done loop-optimization,
2454 because the gap between them is where loop-optimize
2455 will want to move invariant code to. CROSS_JUMP tells us
2456 that loop-optimization is done with.
2458 Once reload has completed (CROSS_JUMP non-zero), we need not consider
2459 two labels distinct if they are separated by only USE or CLOBBER insns. */
2462 mark_jump_label (x
, insn
, cross_jump
, in_mem
)
2468 register RTX_CODE code
= GET_CODE (x
);
2470 register const char *fmt
;
2492 /* If this is a constant-pool reference, see if it is a label. */
2493 if (CONSTANT_POOL_ADDRESS_P (x
))
2494 mark_jump_label (get_pool_constant (x
), insn
, cross_jump
, in_mem
);
2499 rtx label
= XEXP (x
, 0);
2504 /* Ignore remaining references to unreachable labels that
2505 have been deleted. */
2506 if (GET_CODE (label
) == NOTE
2507 && NOTE_LINE_NUMBER (label
) == NOTE_INSN_DELETED_LABEL
)
2510 if (GET_CODE (label
) != CODE_LABEL
)
2513 /* Ignore references to labels of containing functions. */
2514 if (LABEL_REF_NONLOCAL_P (x
))
2517 /* If there are other labels following this one,
2518 replace it with the last of the consecutive labels. */
2519 for (next
= NEXT_INSN (label
); next
; next
= NEXT_INSN (next
))
2521 if (GET_CODE (next
) == CODE_LABEL
)
2523 else if (cross_jump
&& GET_CODE (next
) == INSN
2524 && (GET_CODE (PATTERN (next
)) == USE
2525 || GET_CODE (PATTERN (next
)) == CLOBBER
))
2527 else if (GET_CODE (next
) != NOTE
)
2529 else if (! cross_jump
2530 && (NOTE_LINE_NUMBER (next
) == NOTE_INSN_LOOP_BEG
2531 || NOTE_LINE_NUMBER (next
) == NOTE_INSN_FUNCTION_END
2532 /* ??? Optional. Disables some optimizations, but
2533 makes gcov output more accurate with -O. */
2534 || (flag_test_coverage
2535 && NOTE_LINE_NUMBER (next
) > 0)))
2539 XEXP (x
, 0) = label
;
2540 if (! insn
|| ! INSN_DELETED_P (insn
))
2541 ++LABEL_NUSES (label
);
2545 if (GET_CODE (insn
) == JUMP_INSN
)
2546 JUMP_LABEL (insn
) = label
;
2548 /* If we've changed OLABEL and we had a REG_LABEL note
2549 for it, update it as well. */
2550 else if (label
!= olabel
2551 && (note
= find_reg_note (insn
, REG_LABEL
, olabel
)) != 0)
2552 XEXP (note
, 0) = label
;
2554 /* Otherwise, add a REG_LABEL note for LABEL unless there already
2556 else if (! find_reg_note (insn
, REG_LABEL
, label
))
2558 /* This code used to ignore labels which refered to dispatch
2559 tables to avoid flow.c generating worse code.
2561 However, in the presense of global optimizations like
2562 gcse which call find_basic_blocks without calling
2563 life_analysis, not recording such labels will lead
2564 to compiler aborts because of inconsistencies in the
2565 flow graph. So we go ahead and record the label.
2567 It may also be the case that the optimization argument
2568 is no longer valid because of the more accurate cfg
2569 we build in find_basic_blocks -- it no longer pessimizes
2570 code when it finds a REG_LABEL note. */
2571 REG_NOTES (insn
) = gen_rtx_INSN_LIST (REG_LABEL
, label
,
2578 /* Do walk the labels in a vector, but not the first operand of an
2579 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
2582 if (! INSN_DELETED_P (insn
))
2584 int eltnum
= code
== ADDR_DIFF_VEC
? 1 : 0;
2586 for (i
= 0; i
< XVECLEN (x
, eltnum
); i
++)
2587 mark_jump_label (XVECEXP (x
, eltnum
, i
), NULL_RTX
,
2588 cross_jump
, in_mem
);
2596 fmt
= GET_RTX_FORMAT (code
);
2597 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2600 mark_jump_label (XEXP (x
, i
), insn
, cross_jump
, in_mem
);
2601 else if (fmt
[i
] == 'E')
2604 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2605 mark_jump_label (XVECEXP (x
, i
, j
), insn
, cross_jump
, in_mem
);
2610 /* If all INSN does is set the pc, delete it,
2611 and delete the insn that set the condition codes for it
2612 if that's what the previous thing was. */
2618 register rtx set
= single_set (insn
);
2620 if (set
&& GET_CODE (SET_DEST (set
)) == PC
)
2621 delete_computation (insn
);
2624 /* Verify INSN is a BARRIER and delete it. */
2627 delete_barrier (insn
)
2630 if (GET_CODE (insn
) != BARRIER
)
2636 /* Recursively delete prior insns that compute the value (used only by INSN
2637 which the caller is deleting) stored in the register mentioned by NOTE
2638 which is a REG_DEAD note associated with INSN. */
2641 delete_prior_computation (note
, insn
)
2646 rtx reg
= XEXP (note
, 0);
2648 for (our_prev
= prev_nonnote_insn (insn
);
2649 our_prev
&& (GET_CODE (our_prev
) == INSN
2650 || GET_CODE (our_prev
) == CALL_INSN
);
2651 our_prev
= prev_nonnote_insn (our_prev
))
2653 rtx pat
= PATTERN (our_prev
);
2655 /* If we reach a CALL which is not calling a const function
2656 or the callee pops the arguments, then give up. */
2657 if (GET_CODE (our_prev
) == CALL_INSN
2658 && (! CONST_CALL_P (our_prev
)
2659 || GET_CODE (pat
) != SET
|| GET_CODE (SET_SRC (pat
)) != CALL
))
2662 /* If we reach a SEQUENCE, it is too complex to try to
2663 do anything with it, so give up. */
2664 if (GET_CODE (pat
) == SEQUENCE
)
2667 if (GET_CODE (pat
) == USE
2668 && GET_CODE (XEXP (pat
, 0)) == INSN
)
2669 /* reorg creates USEs that look like this. We leave them
2670 alone because reorg needs them for its own purposes. */
2673 if (reg_set_p (reg
, pat
))
2675 if (side_effects_p (pat
) && GET_CODE (our_prev
) != CALL_INSN
)
2678 if (GET_CODE (pat
) == PARALLEL
)
2680 /* If we find a SET of something else, we can't
2685 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
2687 rtx part
= XVECEXP (pat
, 0, i
);
2689 if (GET_CODE (part
) == SET
2690 && SET_DEST (part
) != reg
)
2694 if (i
== XVECLEN (pat
, 0))
2695 delete_computation (our_prev
);
2697 else if (GET_CODE (pat
) == SET
2698 && GET_CODE (SET_DEST (pat
)) == REG
)
2700 int dest_regno
= REGNO (SET_DEST (pat
));
2703 + (dest_regno
< FIRST_PSEUDO_REGISTER
2704 ? HARD_REGNO_NREGS (dest_regno
,
2705 GET_MODE (SET_DEST (pat
))) : 1));
2706 int regno
= REGNO (reg
);
2709 + (regno
< FIRST_PSEUDO_REGISTER
2710 ? HARD_REGNO_NREGS (regno
, GET_MODE (reg
)) : 1));
2712 if (dest_regno
>= regno
2713 && dest_endregno
<= endregno
)
2714 delete_computation (our_prev
);
2716 /* We may have a multi-word hard register and some, but not
2717 all, of the words of the register are needed in subsequent
2718 insns. Write REG_UNUSED notes for those parts that were not
2720 else if (dest_regno
<= regno
2721 && dest_endregno
>= endregno
)
2725 REG_NOTES (our_prev
)
2726 = gen_rtx_EXPR_LIST (REG_UNUSED
, reg
,
2727 REG_NOTES (our_prev
));
2729 for (i
= dest_regno
; i
< dest_endregno
; i
++)
2730 if (! find_regno_note (our_prev
, REG_UNUSED
, i
))
2733 if (i
== dest_endregno
)
2734 delete_computation (our_prev
);
2741 /* If PAT references the register that dies here, it is an
2742 additional use. Hence any prior SET isn't dead. However, this
2743 insn becomes the new place for the REG_DEAD note. */
2744 if (reg_overlap_mentioned_p (reg
, pat
))
2746 XEXP (note
, 1) = REG_NOTES (our_prev
);
2747 REG_NOTES (our_prev
) = note
;
2753 /* Delete INSN and recursively delete insns that compute values used only
2754 by INSN. This uses the REG_DEAD notes computed during flow analysis.
2755 If we are running before flow.c, we need do nothing since flow.c will
2756 delete dead code. We also can't know if the registers being used are
2757 dead or not at this point.
2759 Otherwise, look at all our REG_DEAD notes. If a previous insn does
2760 nothing other than set a register that dies in this insn, we can delete
2763 On machines with CC0, if CC0 is used in this insn, we may be able to
2764 delete the insn that set it. */
2767 delete_computation (insn
)
2773 if (reg_referenced_p (cc0_rtx
, PATTERN (insn
)))
2775 rtx prev
= prev_nonnote_insn (insn
);
2776 /* We assume that at this stage
2777 CC's are always set explicitly
2778 and always immediately before the jump that
2779 will use them. So if the previous insn
2780 exists to set the CC's, delete it
2781 (unless it performs auto-increments, etc.). */
2782 if (prev
&& GET_CODE (prev
) == INSN
2783 && sets_cc0_p (PATTERN (prev
)))
2785 if (sets_cc0_p (PATTERN (prev
)) > 0
2786 && ! side_effects_p (PATTERN (prev
)))
2787 delete_computation (prev
);
2789 /* Otherwise, show that cc0 won't be used. */
2790 REG_NOTES (prev
) = gen_rtx_EXPR_LIST (REG_UNUSED
,
2791 cc0_rtx
, REG_NOTES (prev
));
2796 for (note
= REG_NOTES (insn
); note
; note
= next
)
2798 next
= XEXP (note
, 1);
2800 if (REG_NOTE_KIND (note
) != REG_DEAD
2801 /* Verify that the REG_NOTE is legitimate. */
2802 || GET_CODE (XEXP (note
, 0)) != REG
)
2805 delete_prior_computation (note
, insn
);
2811 /* Delete insn INSN from the chain of insns and update label ref counts.
2812 May delete some following insns as a consequence; may even delete
2813 a label elsewhere and insns that follow it.
2815 Returns the first insn after INSN that was not deleted. */
2821 register rtx next
= NEXT_INSN (insn
);
2822 register rtx prev
= PREV_INSN (insn
);
2823 register int was_code_label
= (GET_CODE (insn
) == CODE_LABEL
);
2824 register int dont_really_delete
= 0;
2827 while (next
&& INSN_DELETED_P (next
))
2828 next
= NEXT_INSN (next
);
2830 /* This insn is already deleted => return first following nondeleted. */
2831 if (INSN_DELETED_P (insn
))
2835 remove_node_from_expr_list (insn
, &nonlocal_goto_handler_labels
);
2837 /* Don't delete user-declared labels. When optimizing, convert them
2838 to special NOTEs instead. When not optimizing, leave them alone. */
2839 if (was_code_label
&& LABEL_NAME (insn
) != 0)
2843 const char *name
= LABEL_NAME (insn
);
2844 PUT_CODE (insn
, NOTE
);
2845 NOTE_LINE_NUMBER (insn
) = NOTE_INSN_DELETED_LABEL
;
2846 NOTE_SOURCE_FILE (insn
) = name
;
2849 dont_really_delete
= 1;
2852 /* Mark this insn as deleted. */
2853 INSN_DELETED_P (insn
) = 1;
2855 /* If this is an unconditional jump, delete it from the jump chain. */
2856 if (simplejump_p (insn
))
2857 delete_from_jump_chain (insn
);
2859 /* If instruction is followed by a barrier,
2860 delete the barrier too. */
2862 if (next
!= 0 && GET_CODE (next
) == BARRIER
)
2864 INSN_DELETED_P (next
) = 1;
2865 next
= NEXT_INSN (next
);
2868 /* Patch out INSN (and the barrier if any) */
2870 if (! dont_really_delete
)
2874 NEXT_INSN (prev
) = next
;
2875 if (GET_CODE (prev
) == INSN
&& GET_CODE (PATTERN (prev
)) == SEQUENCE
)
2876 NEXT_INSN (XVECEXP (PATTERN (prev
), 0,
2877 XVECLEN (PATTERN (prev
), 0) - 1)) = next
;
2882 PREV_INSN (next
) = prev
;
2883 if (GET_CODE (next
) == INSN
&& GET_CODE (PATTERN (next
)) == SEQUENCE
)
2884 PREV_INSN (XVECEXP (PATTERN (next
), 0, 0)) = prev
;
2887 if (prev
&& NEXT_INSN (prev
) == 0)
2888 set_last_insn (prev
);
2891 /* If deleting a jump, decrement the count of the label,
2892 and delete the label if it is now unused. */
2894 if (GET_CODE (insn
) == JUMP_INSN
&& JUMP_LABEL (insn
))
2896 rtx lab
= JUMP_LABEL (insn
), lab_next
;
2898 if (--LABEL_NUSES (lab
) == 0)
2900 /* This can delete NEXT or PREV,
2901 either directly if NEXT is JUMP_LABEL (INSN),
2902 or indirectly through more levels of jumps. */
2905 /* I feel a little doubtful about this loop,
2906 but I see no clean and sure alternative way
2907 to find the first insn after INSN that is not now deleted.
2908 I hope this works. */
2909 while (next
&& INSN_DELETED_P (next
))
2910 next
= NEXT_INSN (next
);
2913 else if ((lab_next
= next_nonnote_insn (lab
)) != NULL
2914 && GET_CODE (lab_next
) == JUMP_INSN
2915 && (GET_CODE (PATTERN (lab_next
)) == ADDR_VEC
2916 || GET_CODE (PATTERN (lab_next
)) == ADDR_DIFF_VEC
))
2918 /* If we're deleting the tablejump, delete the dispatch table.
2919 We may not be able to kill the label immediately preceeding
2920 just yet, as it might be referenced in code leading up to
2922 delete_insn (lab_next
);
2926 /* Likewise if we're deleting a dispatch table. */
2928 if (GET_CODE (insn
) == JUMP_INSN
2929 && (GET_CODE (PATTERN (insn
)) == ADDR_VEC
2930 || GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
))
2932 rtx pat
= PATTERN (insn
);
2933 int i
, diff_vec_p
= GET_CODE (pat
) == ADDR_DIFF_VEC
;
2934 int len
= XVECLEN (pat
, diff_vec_p
);
2936 for (i
= 0; i
< len
; i
++)
2937 if (--LABEL_NUSES (XEXP (XVECEXP (pat
, diff_vec_p
, i
), 0)) == 0)
2938 delete_insn (XEXP (XVECEXP (pat
, diff_vec_p
, i
), 0));
2939 while (next
&& INSN_DELETED_P (next
))
2940 next
= NEXT_INSN (next
);
2944 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
2945 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
2946 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
2947 if (REG_NOTE_KIND (note
) == REG_LABEL
2948 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
2949 && GET_CODE (XEXP (note
, 0)) == CODE_LABEL
)
2950 if (--LABEL_NUSES (XEXP (note
, 0)) == 0)
2951 delete_insn (XEXP (note
, 0));
2953 while (prev
&& (INSN_DELETED_P (prev
) || GET_CODE (prev
) == NOTE
))
2954 prev
= PREV_INSN (prev
);
2956 /* If INSN was a label and a dispatch table follows it,
2957 delete the dispatch table. The tablejump must have gone already.
2958 It isn't useful to fall through into a table. */
2961 && NEXT_INSN (insn
) != 0
2962 && GET_CODE (NEXT_INSN (insn
)) == JUMP_INSN
2963 && (GET_CODE (PATTERN (NEXT_INSN (insn
))) == ADDR_VEC
2964 || GET_CODE (PATTERN (NEXT_INSN (insn
))) == ADDR_DIFF_VEC
))
2965 next
= delete_insn (NEXT_INSN (insn
));
2967 /* If INSN was a label, delete insns following it if now unreachable. */
2969 if (was_code_label
&& prev
&& GET_CODE (prev
) == BARRIER
)
2971 register RTX_CODE code
;
2973 && (GET_RTX_CLASS (code
= GET_CODE (next
)) == 'i'
2974 || code
== NOTE
|| code
== BARRIER
2975 || (code
== CODE_LABEL
&& INSN_DELETED_P (next
))))
2978 && NOTE_LINE_NUMBER (next
) != NOTE_INSN_FUNCTION_END
)
2979 next
= NEXT_INSN (next
);
2980 /* Keep going past other deleted labels to delete what follows. */
2981 else if (code
== CODE_LABEL
&& INSN_DELETED_P (next
))
2982 next
= NEXT_INSN (next
);
2984 /* Note: if this deletes a jump, it can cause more
2985 deletion of unreachable code, after a different label.
2986 As long as the value from this recursive call is correct,
2987 this invocation functions correctly. */
2988 next
= delete_insn (next
);
2995 /* Advance from INSN till reaching something not deleted
2996 then return that. May return INSN itself. */
2999 next_nondeleted_insn (insn
)
3002 while (INSN_DELETED_P (insn
))
3003 insn
= NEXT_INSN (insn
);
3007 /* Delete a range of insns from FROM to TO, inclusive.
3008 This is for the sake of peephole optimization, so assume
3009 that whatever these insns do will still be done by a new
3010 peephole insn that will replace them. */
3013 delete_for_peephole (from
, to
)
3014 register rtx from
, to
;
3016 register rtx insn
= from
;
3020 register rtx next
= NEXT_INSN (insn
);
3021 register rtx prev
= PREV_INSN (insn
);
3023 if (GET_CODE (insn
) != NOTE
)
3025 INSN_DELETED_P (insn
) = 1;
3027 /* Patch this insn out of the chain. */
3028 /* We don't do this all at once, because we
3029 must preserve all NOTEs. */
3031 NEXT_INSN (prev
) = next
;
3034 PREV_INSN (next
) = prev
;
3042 /* Note that if TO is an unconditional jump
3043 we *do not* delete the BARRIER that follows,
3044 since the peephole that replaces this sequence
3045 is also an unconditional jump in that case. */
3048 /* We have determined that INSN is never reached, and are about to
3049 delete it. Print a warning if the user asked for one.
3051 To try to make this warning more useful, this should only be called
3052 once per basic block not reached, and it only warns when the basic
3053 block contains more than one line from the current function, and
3054 contains at least one operation. CSE and inlining can duplicate insns,
3055 so it's possible to get spurious warnings from this. */
3058 never_reached_warning (avoided_insn
)
3062 rtx a_line_note
= NULL
;
3063 int two_avoided_lines
= 0;
3064 int contains_insn
= 0;
3066 if (! warn_notreached
)
3069 /* Scan forwards, looking at LINE_NUMBER notes, until
3070 we hit a LABEL or we run out of insns. */
3072 for (insn
= avoided_insn
; insn
!= NULL
; insn
= NEXT_INSN (insn
))
3074 if (GET_CODE (insn
) == CODE_LABEL
)
3076 else if (GET_CODE (insn
) == NOTE
/* A line number note? */
3077 && NOTE_LINE_NUMBER (insn
) >= 0)
3079 if (a_line_note
== NULL
)
3082 two_avoided_lines
|= (NOTE_LINE_NUMBER (a_line_note
)
3083 != NOTE_LINE_NUMBER (insn
));
3085 else if (INSN_P (insn
))
3088 if (two_avoided_lines
&& contains_insn
)
3089 warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note
),
3090 NOTE_LINE_NUMBER (a_line_note
),
3091 "will never be executed");
3094 /* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
3095 NLABEL as a return. Accrue modifications into the change group. */
3098 redirect_exp_1 (loc
, olabel
, nlabel
, insn
)
3103 register rtx x
= *loc
;
3104 register RTX_CODE code
= GET_CODE (x
);
3106 register const char *fmt
;
3108 if (code
== LABEL_REF
)
3110 if (XEXP (x
, 0) == olabel
)
3114 n
= gen_rtx_LABEL_REF (VOIDmode
, nlabel
);
3116 n
= gen_rtx_RETURN (VOIDmode
);
3118 validate_change (insn
, loc
, n
, 1);
3122 else if (code
== RETURN
&& olabel
== 0)
3124 x
= gen_rtx_LABEL_REF (VOIDmode
, nlabel
);
3125 if (loc
== &PATTERN (insn
))
3126 x
= gen_rtx_SET (VOIDmode
, pc_rtx
, x
);
3127 validate_change (insn
, loc
, x
, 1);
3131 if (code
== SET
&& nlabel
== 0 && SET_DEST (x
) == pc_rtx
3132 && GET_CODE (SET_SRC (x
)) == LABEL_REF
3133 && XEXP (SET_SRC (x
), 0) == olabel
)
3135 validate_change (insn
, loc
, gen_rtx_RETURN (VOIDmode
), 1);
3139 fmt
= GET_RTX_FORMAT (code
);
3140 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3143 redirect_exp_1 (&XEXP (x
, i
), olabel
, nlabel
, insn
);
3144 else if (fmt
[i
] == 'E')
3147 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3148 redirect_exp_1 (&XVECEXP (x
, i
, j
), olabel
, nlabel
, insn
);
3153 /* Similar, but apply the change group and report success or failure. */
3156 redirect_exp (olabel
, nlabel
, insn
)
3162 if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
3163 loc
= &XVECEXP (PATTERN (insn
), 0, 0);
3165 loc
= &PATTERN (insn
);
3167 redirect_exp_1 (loc
, olabel
, nlabel
, insn
);
3168 if (num_validated_changes () == 0)
3171 return apply_change_group ();
3174 /* Make JUMP go to NLABEL instead of where it jumps now. Accrue
3175 the modifications into the change group. Return false if we did
3176 not see how to do that. */
3179 redirect_jump_1 (jump
, nlabel
)
3182 int ochanges
= num_validated_changes ();
3185 if (GET_CODE (PATTERN (jump
)) == PARALLEL
)
3186 loc
= &XVECEXP (PATTERN (jump
), 0, 0);
3188 loc
= &PATTERN (jump
);
3190 redirect_exp_1 (loc
, JUMP_LABEL (jump
), nlabel
, jump
);
3191 return num_validated_changes () > ochanges
;
3194 /* Make JUMP go to NLABEL instead of where it jumps now. If the old
3195 jump target label is unused as a result, it and the code following
3198 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3201 The return value will be 1 if the change was made, 0 if it wasn't
3202 (this can only occur for NLABEL == 0). */
3205 redirect_jump (jump
, nlabel
, delete_unused
)
3209 register rtx olabel
= JUMP_LABEL (jump
);
3211 if (nlabel
== olabel
)
3214 if (! redirect_exp (olabel
, nlabel
, jump
))
3217 /* If this is an unconditional branch, delete it from the jump_chain of
3218 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3219 have UID's in range and JUMP_CHAIN is valid). */
3220 if (jump_chain
&& (simplejump_p (jump
)
3221 || GET_CODE (PATTERN (jump
)) == RETURN
))
3223 int label_index
= nlabel
? INSN_UID (nlabel
) : 0;
3225 delete_from_jump_chain (jump
);
3226 if (label_index
< max_jump_chain
3227 && INSN_UID (jump
) < max_jump_chain
)
3229 jump_chain
[INSN_UID (jump
)] = jump_chain
[label_index
];
3230 jump_chain
[label_index
] = jump
;
3234 JUMP_LABEL (jump
) = nlabel
;
3236 ++LABEL_NUSES (nlabel
);
3238 /* If we're eliding the jump over exception cleanups at the end of a
3239 function, move the function end note so that -Wreturn-type works. */
3240 if (olabel
&& nlabel
3241 && NEXT_INSN (olabel
)
3242 && GET_CODE (NEXT_INSN (olabel
)) == NOTE
3243 && NOTE_LINE_NUMBER (NEXT_INSN (olabel
)) == NOTE_INSN_FUNCTION_END
)
3244 emit_note_after (NOTE_INSN_FUNCTION_END
, nlabel
);
3246 if (olabel
&& --LABEL_NUSES (olabel
) == 0 && delete_unused
)
3247 delete_insn (olabel
);
3252 /* Invert the jump condition of rtx X contained in jump insn, INSN.
3253 Accrue the modifications into the change group. */
3259 register RTX_CODE code
;
3260 rtx x
= pc_set (insn
);
3266 code
= GET_CODE (x
);
3268 if (code
== IF_THEN_ELSE
)
3270 register rtx comp
= XEXP (x
, 0);
3272 enum rtx_code reversed_code
;
3274 /* We can do this in two ways: The preferable way, which can only
3275 be done if this is not an integer comparison, is to reverse
3276 the comparison code. Otherwise, swap the THEN-part and ELSE-part
3277 of the IF_THEN_ELSE. If we can't do either, fail. */
3279 reversed_code
= reversed_comparison_code (comp
, insn
);
3281 if (reversed_code
!= UNKNOWN
)
3283 validate_change (insn
, &XEXP (x
, 0),
3284 gen_rtx_fmt_ee (reversed_code
,
3285 GET_MODE (comp
), XEXP (comp
, 0),
3292 validate_change (insn
, &XEXP (x
, 1), XEXP (x
, 2), 1);
3293 validate_change (insn
, &XEXP (x
, 2), tem
, 1);
3299 /* Invert the jump condition of conditional jump insn, INSN.
3301 Return 1 if we can do so, 0 if we cannot find a way to do so that
3302 matches a pattern. */
3308 invert_exp_1 (insn
);
3309 if (num_validated_changes () == 0)
3312 return apply_change_group ();
3315 /* Invert the condition of the jump JUMP, and make it jump to label
3316 NLABEL instead of where it jumps now. Accrue changes into the
3317 change group. Return false if we didn't see how to perform the
3318 inversion and redirection. */
3321 invert_jump_1 (jump
, nlabel
)
3326 ochanges
= num_validated_changes ();
3327 invert_exp_1 (jump
);
3328 if (num_validated_changes () == ochanges
)
3331 return redirect_jump_1 (jump
, nlabel
);
3334 /* Invert the condition of the jump JUMP, and make it jump to label
3335 NLABEL instead of where it jumps now. Return true if successful. */
3338 invert_jump (jump
, nlabel
, delete_unused
)
3342 /* We have to either invert the condition and change the label or
3343 do neither. Either operation could fail. We first try to invert
3344 the jump. If that succeeds, we try changing the label. If that fails,
3345 we invert the jump back to what it was. */
3347 if (! invert_exp (jump
))
3350 if (redirect_jump (jump
, nlabel
, delete_unused
))
3352 /* An inverted jump means that a probability taken becomes a
3353 probability not taken. Subtract the branch probability from the
3354 probability base to convert it back to a taken probability. */
3356 rtx note
= find_reg_note (jump
, REG_BR_PROB
, NULL_RTX
);
3358 XEXP (note
, 0) = GEN_INT (REG_BR_PROB_BASE
- INTVAL (XEXP (note
, 0)));
3363 if (! invert_exp (jump
))
3364 /* This should just be putting it back the way it was. */
3370 /* Delete the instruction JUMP from any jump chain it might be on. */
3373 delete_from_jump_chain (jump
)
3377 rtx olabel
= JUMP_LABEL (jump
);
3379 /* Handle unconditional jumps. */
3380 if (jump_chain
&& olabel
!= 0
3381 && INSN_UID (olabel
) < max_jump_chain
3382 && simplejump_p (jump
))
3383 index
= INSN_UID (olabel
);
3384 /* Handle return insns. */
3385 else if (jump_chain
&& GET_CODE (PATTERN (jump
)) == RETURN
)
3390 if (jump_chain
[index
] == jump
)
3391 jump_chain
[index
] = jump_chain
[INSN_UID (jump
)];
3396 for (insn
= jump_chain
[index
];
3398 insn
= jump_chain
[INSN_UID (insn
)])
3399 if (jump_chain
[INSN_UID (insn
)] == jump
)
3401 jump_chain
[INSN_UID (insn
)] = jump_chain
[INSN_UID (jump
)];
3407 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3409 If the old jump target label (before the dispatch table) becomes unused,
3410 it and the dispatch table may be deleted. In that case, find the insn
3411 before the jump references that label and delete it and logical successors
3415 redirect_tablejump (jump
, nlabel
)
3418 register rtx olabel
= JUMP_LABEL (jump
);
3419 rtx
*notep
, note
, next
;
3421 /* Add this jump to the jump_chain of NLABEL. */
3422 if (jump_chain
&& INSN_UID (nlabel
) < max_jump_chain
3423 && INSN_UID (jump
) < max_jump_chain
)
3425 jump_chain
[INSN_UID (jump
)] = jump_chain
[INSN_UID (nlabel
)];
3426 jump_chain
[INSN_UID (nlabel
)] = jump
;
3429 for (notep
= ®_NOTES (jump
), note
= *notep
; note
; note
= next
)
3431 next
= XEXP (note
, 1);
3433 if (REG_NOTE_KIND (note
) != REG_DEAD
3434 /* Verify that the REG_NOTE is legitimate. */
3435 || GET_CODE (XEXP (note
, 0)) != REG
3436 || ! reg_mentioned_p (XEXP (note
, 0), PATTERN (jump
)))
3437 notep
= &XEXP (note
, 1);
3440 delete_prior_computation (note
, jump
);
3445 PATTERN (jump
) = gen_jump (nlabel
);
3446 JUMP_LABEL (jump
) = nlabel
;
3447 ++LABEL_NUSES (nlabel
);
3448 INSN_CODE (jump
) = -1;
3450 if (--LABEL_NUSES (olabel
) == 0)
3452 delete_labelref_insn (jump
, olabel
, 0);
3453 delete_insn (olabel
);
3457 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
3458 If we found one, delete it and then delete this insn if DELETE_THIS is
3459 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
3462 delete_labelref_insn (insn
, label
, delete_this
)
3469 if (GET_CODE (insn
) != NOTE
3470 && reg_mentioned_p (label
, PATTERN (insn
)))
3481 for (link
= LOG_LINKS (insn
); link
; link
= XEXP (link
, 1))
3482 if (delete_labelref_insn (XEXP (link
, 0), label
, 1))
3496 /* Like rtx_equal_p except that it considers two REGs as equal
3497 if they renumber to the same value and considers two commutative
3498 operations to be the same if the order of the operands has been
3501 ??? Addition is not commutative on the PA due to the weird implicit
3502 space register selection rules for memory addresses. Therefore, we
3503 don't consider a + b == b + a.
3505 We could/should make this test a little tighter. Possibly only
3506 disabling it on the PA via some backend macro or only disabling this
3507 case when the PLUS is inside a MEM. */
3510 rtx_renumbered_equal_p (x
, y
)
3514 register RTX_CODE code
= GET_CODE (x
);
3515 register const char *fmt
;
3520 if ((code
== REG
|| (code
== SUBREG
&& GET_CODE (SUBREG_REG (x
)) == REG
))
3521 && (GET_CODE (y
) == REG
|| (GET_CODE (y
) == SUBREG
3522 && GET_CODE (SUBREG_REG (y
)) == REG
)))
3524 int reg_x
= -1, reg_y
= -1;
3525 int byte_x
= 0, byte_y
= 0;
3527 if (GET_MODE (x
) != GET_MODE (y
))
3530 /* If we haven't done any renumbering, don't
3531 make any assumptions. */
3532 if (reg_renumber
== 0)
3533 return rtx_equal_p (x
, y
);
3537 reg_x
= REGNO (SUBREG_REG (x
));
3538 byte_x
= SUBREG_BYTE (x
);
3540 if (reg_renumber
[reg_x
] >= 0)
3542 reg_x
= subreg_regno_offset (reg_renumber
[reg_x
],
3543 GET_MODE (SUBREG_REG (x
)),
3552 if (reg_renumber
[reg_x
] >= 0)
3553 reg_x
= reg_renumber
[reg_x
];
3556 if (GET_CODE (y
) == SUBREG
)
3558 reg_y
= REGNO (SUBREG_REG (y
));
3559 byte_y
= SUBREG_BYTE (y
);
3561 if (reg_renumber
[reg_y
] >= 0)
3563 reg_y
= subreg_regno_offset (reg_renumber
[reg_y
],
3564 GET_MODE (SUBREG_REG (y
)),
3573 if (reg_renumber
[reg_y
] >= 0)
3574 reg_y
= reg_renumber
[reg_y
];
3577 return reg_x
>= 0 && reg_x
== reg_y
&& byte_x
== byte_y
;
3580 /* Now we have disposed of all the cases
3581 in which different rtx codes can match. */
3582 if (code
!= GET_CODE (y
))
3594 return INTVAL (x
) == INTVAL (y
);
3597 /* We can't assume nonlocal labels have their following insns yet. */
3598 if (LABEL_REF_NONLOCAL_P (x
) || LABEL_REF_NONLOCAL_P (y
))
3599 return XEXP (x
, 0) == XEXP (y
, 0);
3601 /* Two label-refs are equivalent if they point at labels
3602 in the same position in the instruction stream. */
3603 return (next_real_insn (XEXP (x
, 0))
3604 == next_real_insn (XEXP (y
, 0)));
3607 return XSTR (x
, 0) == XSTR (y
, 0);
3610 /* If we didn't match EQ equality above, they aren't the same. */
3617 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
3619 if (GET_MODE (x
) != GET_MODE (y
))
3622 /* For commutative operations, the RTX match if the operand match in any
3623 order. Also handle the simple binary and unary cases without a loop.
3625 ??? Don't consider PLUS a commutative operator; see comments above. */
3626 if ((code
== EQ
|| code
== NE
|| GET_RTX_CLASS (code
) == 'c')
3628 return ((rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 0))
3629 && rtx_renumbered_equal_p (XEXP (x
, 1), XEXP (y
, 1)))
3630 || (rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 1))
3631 && rtx_renumbered_equal_p (XEXP (x
, 1), XEXP (y
, 0))));
3632 else if (GET_RTX_CLASS (code
) == '<' || GET_RTX_CLASS (code
) == '2')
3633 return (rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 0))
3634 && rtx_renumbered_equal_p (XEXP (x
, 1), XEXP (y
, 1)));
3635 else if (GET_RTX_CLASS (code
) == '1')
3636 return rtx_renumbered_equal_p (XEXP (x
, 0), XEXP (y
, 0));
3638 /* Compare the elements. If any pair of corresponding elements
3639 fail to match, return 0 for the whole things. */
3641 fmt
= GET_RTX_FORMAT (code
);
3642 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3648 if (XWINT (x
, i
) != XWINT (y
, i
))
3653 if (XINT (x
, i
) != XINT (y
, i
))
3658 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
3663 if (! rtx_renumbered_equal_p (XEXP (x
, i
), XEXP (y
, i
)))
3668 if (XEXP (x
, i
) != XEXP (y
, i
))
3675 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
3677 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
3678 if (!rtx_renumbered_equal_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
)))
3689 /* If X is a hard register or equivalent to one or a subregister of one,
3690 return the hard register number. If X is a pseudo register that was not
3691 assigned a hard register, return the pseudo register number. Otherwise,
3692 return -1. Any rtx is valid for X. */
3698 if (GET_CODE (x
) == REG
)
3700 if (REGNO (x
) >= FIRST_PSEUDO_REGISTER
&& reg_renumber
[REGNO (x
)] >= 0)
3701 return reg_renumber
[REGNO (x
)];
3704 if (GET_CODE (x
) == SUBREG
)
3706 int base
= true_regnum (SUBREG_REG (x
));
3707 if (base
>= 0 && base
< FIRST_PSEUDO_REGISTER
)
3708 return base
+ subreg_regno_offset (REGNO (SUBREG_REG (x
)),
3709 GET_MODE (SUBREG_REG (x
)),
3710 SUBREG_BYTE (x
), GET_MODE (x
));
3715 /* Optimize code of the form:
3717 for (x = a[i]; x; ...)
3719 for (x = a[i]; x; ...)
3723 Loop optimize will change the above code into
3727 { ...; if (! (x = ...)) break; }
3730 { ...; if (! (x = ...)) break; }
3733 In general, if the first test fails, the program can branch
3734 directly to `foo' and skip the second try which is doomed to fail.
3735 We run this after loop optimization and before flow analysis. */
3737 /* When comparing the insn patterns, we track the fact that different
3738 pseudo-register numbers may have been used in each computation.
3739 The following array stores an equivalence -- same_regs[I] == J means
3740 that pseudo register I was used in the first set of tests in a context
3741 where J was used in the second set. We also count the number of such
3742 pending equivalences. If nonzero, the expressions really aren't the
3745 static int *same_regs
;
3747 static int num_same_regs
;
3749 /* Track any registers modified between the target of the first jump and
3750 the second jump. They never compare equal. */
3752 static char *modified_regs
;
3754 /* Record if memory was modified. */
3756 static int modified_mem
;
3758 /* Called via note_stores on each insn between the target of the first
3759 branch and the second branch. It marks any changed registers. */
3762 mark_modified_reg (dest
, x
, data
)
3764 rtx x ATTRIBUTE_UNUSED
;
3765 void *data ATTRIBUTE_UNUSED
;
3770 if (GET_CODE (dest
) == SUBREG
)
3771 dest
= SUBREG_REG (dest
);
3773 if (GET_CODE (dest
) == MEM
)
3776 if (GET_CODE (dest
) != REG
)
3779 regno
= REGNO (dest
);
3780 if (regno
>= FIRST_PSEUDO_REGISTER
)
3781 modified_regs
[regno
] = 1;
3783 for (i
= 0; i
< HARD_REGNO_NREGS (regno
, GET_MODE (dest
)); i
++)
3784 modified_regs
[regno
+ i
] = 1;
3787 /* F is the first insn in the chain of insns. */
3790 thread_jumps (f
, max_reg
, flag_before_loop
)
3793 int flag_before_loop
;
3795 /* Basic algorithm is to find a conditional branch,
3796 the label it may branch to, and the branch after
3797 that label. If the two branches test the same condition,
3798 walk back from both branch paths until the insn patterns
3799 differ, or code labels are hit. If we make it back to
3800 the target of the first branch, then we know that the first branch
3801 will either always succeed or always fail depending on the relative
3802 senses of the two branches. So adjust the first branch accordingly
3805 rtx label
, b1
, b2
, t1
, t2
;
3806 enum rtx_code code1
, code2
;
3807 rtx b1op0
, b1op1
, b2op0
, b2op1
;
3811 enum rtx_code reversed_code1
, reversed_code2
;
3813 /* Allocate register tables and quick-reset table. */
3814 modified_regs
= (char *) xmalloc (max_reg
* sizeof (char));
3815 same_regs
= (int *) xmalloc (max_reg
* sizeof (int));
3816 all_reset
= (int *) xmalloc (max_reg
* sizeof (int));
3817 for (i
= 0; i
< max_reg
; i
++)
3824 for (b1
= f
; b1
; b1
= NEXT_INSN (b1
))
3829 /* Get to a candidate branch insn. */
3830 if (GET_CODE (b1
) != JUMP_INSN
3831 || ! any_condjump_p (b1
) || JUMP_LABEL (b1
) == 0)
3834 memset (modified_regs
, 0, max_reg
* sizeof (char));
3837 memcpy (same_regs
, all_reset
, max_reg
* sizeof (int));
3840 label
= JUMP_LABEL (b1
);
3842 /* Look for a branch after the target. Record any registers and
3843 memory modified between the target and the branch. Stop when we
3844 get to a label since we can't know what was changed there. */
3845 for (b2
= NEXT_INSN (label
); b2
; b2
= NEXT_INSN (b2
))
3847 if (GET_CODE (b2
) == CODE_LABEL
)
3850 else if (GET_CODE (b2
) == JUMP_INSN
)
3852 /* If this is an unconditional jump and is the only use of
3853 its target label, we can follow it. */
3854 if (any_uncondjump_p (b2
)
3856 && JUMP_LABEL (b2
) != 0
3857 && LABEL_NUSES (JUMP_LABEL (b2
)) == 1)
3859 b2
= JUMP_LABEL (b2
);
3866 if (GET_CODE (b2
) != CALL_INSN
&& GET_CODE (b2
) != INSN
)
3869 if (GET_CODE (b2
) == CALL_INSN
)
3872 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
3873 if (call_used_regs
[i
] && ! fixed_regs
[i
]
3874 && i
!= STACK_POINTER_REGNUM
3875 && i
!= FRAME_POINTER_REGNUM
3876 && i
!= HARD_FRAME_POINTER_REGNUM
3877 && i
!= ARG_POINTER_REGNUM
)
3878 modified_regs
[i
] = 1;
3881 note_stores (PATTERN (b2
), mark_modified_reg
, NULL
);
3884 /* Check the next candidate branch insn from the label
3887 || GET_CODE (b2
) != JUMP_INSN
3889 || !any_condjump_p (b2
)
3890 || !onlyjump_p (b2
))
3895 /* Get the comparison codes and operands, reversing the
3896 codes if appropriate. If we don't have comparison codes,
3897 we can't do anything. */
3898 b1op0
= XEXP (XEXP (SET_SRC (set
), 0), 0);
3899 b1op1
= XEXP (XEXP (SET_SRC (set
), 0), 1);
3900 code1
= GET_CODE (XEXP (SET_SRC (set
), 0));
3901 reversed_code1
= code1
;
3902 if (XEXP (SET_SRC (set
), 1) == pc_rtx
)
3903 code1
= reversed_comparison_code (XEXP (SET_SRC (set
), 0), b1
);
3905 reversed_code1
= reversed_comparison_code (XEXP (SET_SRC (set
), 0), b1
);
3907 b2op0
= XEXP (XEXP (SET_SRC (set2
), 0), 0);
3908 b2op1
= XEXP (XEXP (SET_SRC (set2
), 0), 1);
3909 code2
= GET_CODE (XEXP (SET_SRC (set2
), 0));
3910 reversed_code2
= code2
;
3911 if (XEXP (SET_SRC (set2
), 1) == pc_rtx
)
3912 code2
= reversed_comparison_code (XEXP (SET_SRC (set2
), 0), b2
);
3914 reversed_code2
= reversed_comparison_code (XEXP (SET_SRC (set2
), 0), b2
);
3916 /* If they test the same things and knowing that B1 branches
3917 tells us whether or not B2 branches, check if we
3918 can thread the branch. */
3919 if (rtx_equal_for_thread_p (b1op0
, b2op0
, b2
)
3920 && rtx_equal_for_thread_p (b1op1
, b2op1
, b2
)
3921 && (comparison_dominates_p (code1
, code2
)
3922 || comparison_dominates_p (code1
, reversed_code2
)))
3925 t1
= prev_nonnote_insn (b1
);
3926 t2
= prev_nonnote_insn (b2
);
3928 while (t1
!= 0 && t2
!= 0)
3932 /* We have reached the target of the first branch.
3933 If there are no pending register equivalents,
3934 we know that this branch will either always
3935 succeed (if the senses of the two branches are
3936 the same) or always fail (if not). */
3939 if (num_same_regs
!= 0)
3942 if (comparison_dominates_p (code1
, code2
))
3943 new_label
= JUMP_LABEL (b2
);
3945 new_label
= get_label_after (b2
);
3947 if (JUMP_LABEL (b1
) != new_label
)
3949 rtx prev
= PREV_INSN (new_label
);
3951 if (flag_before_loop
3952 && GET_CODE (prev
) == NOTE
3953 && NOTE_LINE_NUMBER (prev
) == NOTE_INSN_LOOP_BEG
)
3955 /* Don't thread to the loop label. If a loop
3956 label is reused, loop optimization will
3957 be disabled for that loop. */
3958 new_label
= gen_label_rtx ();
3959 emit_label_after (new_label
, PREV_INSN (prev
));
3961 changed
|= redirect_jump (b1
, new_label
, 1);
3966 /* If either of these is not a normal insn (it might be
3967 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
3968 have already been skipped above.) Similarly, fail
3969 if the insns are different. */
3970 if (GET_CODE (t1
) != INSN
|| GET_CODE (t2
) != INSN
3971 || recog_memoized (t1
) != recog_memoized (t2
)
3972 || ! rtx_equal_for_thread_p (PATTERN (t1
),
3976 t1
= prev_nonnote_insn (t1
);
3977 t2
= prev_nonnote_insn (t2
);
3984 free (modified_regs
);
3989 /* This is like RTX_EQUAL_P except that it knows about our handling of
3990 possibly equivalent registers and knows to consider volatile and
3991 modified objects as not equal.
3993 YINSN is the insn containing Y. */
3996 rtx_equal_for_thread_p (x
, y
, yinsn
)
4002 register enum rtx_code code
;
4003 register const char *fmt
;
4005 code
= GET_CODE (x
);
4006 /* Rtx's of different codes cannot be equal. */
4007 if (code
!= GET_CODE (y
))
4010 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4011 (REG:SI x) and (REG:HI x) are NOT equivalent. */
4013 if (GET_MODE (x
) != GET_MODE (y
))
4016 /* For floating-point, consider everything unequal. This is a bit
4017 pessimistic, but this pass would only rarely do anything for FP
4019 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
4020 && FLOAT_MODE_P (GET_MODE (x
)) && ! flag_unsafe_math_optimizations
)
4023 /* For commutative operations, the RTX match if the operand match in any
4024 order. Also handle the simple binary and unary cases without a loop. */
4025 if (code
== EQ
|| code
== NE
|| GET_RTX_CLASS (code
) == 'c')
4026 return ((rtx_equal_for_thread_p (XEXP (x
, 0), XEXP (y
, 0), yinsn
)
4027 && rtx_equal_for_thread_p (XEXP (x
, 1), XEXP (y
, 1), yinsn
))
4028 || (rtx_equal_for_thread_p (XEXP (x
, 0), XEXP (y
, 1), yinsn
)
4029 && rtx_equal_for_thread_p (XEXP (x
, 1), XEXP (y
, 0), yinsn
)));
4030 else if (GET_RTX_CLASS (code
) == '<' || GET_RTX_CLASS (code
) == '2')
4031 return (rtx_equal_for_thread_p (XEXP (x
, 0), XEXP (y
, 0), yinsn
)
4032 && rtx_equal_for_thread_p (XEXP (x
, 1), XEXP (y
, 1), yinsn
));
4033 else if (GET_RTX_CLASS (code
) == '1')
4034 return rtx_equal_for_thread_p (XEXP (x
, 0), XEXP (y
, 0), yinsn
);
4036 /* Handle special-cases first. */
4040 if (REGNO (x
) == REGNO (y
) && ! modified_regs
[REGNO (x
)])
4043 /* If neither is user variable or hard register, check for possible
4045 if (REG_USERVAR_P (x
) || REG_USERVAR_P (y
)
4046 || REGNO (x
) < FIRST_PSEUDO_REGISTER
4047 || REGNO (y
) < FIRST_PSEUDO_REGISTER
)
4050 if (same_regs
[REGNO (x
)] == -1)
4052 same_regs
[REGNO (x
)] = REGNO (y
);
4055 /* If this is the first time we are seeing a register on the `Y'
4056 side, see if it is the last use. If not, we can't thread the
4057 jump, so mark it as not equivalent. */
4058 if (REGNO_LAST_UID (REGNO (y
)) != INSN_UID (yinsn
))
4064 return (same_regs
[REGNO (x
)] == (int) REGNO (y
));
4069 /* If memory modified or either volatile, not equivalent.
4070 Else, check address. */
4071 if (modified_mem
|| MEM_VOLATILE_P (x
) || MEM_VOLATILE_P (y
))
4074 return rtx_equal_for_thread_p (XEXP (x
, 0), XEXP (y
, 0), yinsn
);
4077 if (MEM_VOLATILE_P (x
) || MEM_VOLATILE_P (y
))
4083 /* Cancel a pending `same_regs' if setting equivalenced registers.
4084 Then process source. */
4085 if (GET_CODE (SET_DEST (x
)) == REG
4086 && GET_CODE (SET_DEST (y
)) == REG
)
4088 if (same_regs
[REGNO (SET_DEST (x
))] == (int) REGNO (SET_DEST (y
)))
4090 same_regs
[REGNO (SET_DEST (x
))] = -1;
4093 else if (REGNO (SET_DEST (x
)) != REGNO (SET_DEST (y
)))
4098 if (rtx_equal_for_thread_p (SET_DEST (x
), SET_DEST (y
), yinsn
) == 0)
4102 return rtx_equal_for_thread_p (SET_SRC (x
), SET_SRC (y
), yinsn
);
4105 return XEXP (x
, 0) == XEXP (y
, 0);
4108 return XSTR (x
, 0) == XSTR (y
, 0);
4117 fmt
= GET_RTX_FORMAT (code
);
4118 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
4123 if (XWINT (x
, i
) != XWINT (y
, i
))
4129 if (XINT (x
, i
) != XINT (y
, i
))
4135 /* Two vectors must have the same length. */
4136 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
4139 /* And the corresponding elements must match. */
4140 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
4141 if (rtx_equal_for_thread_p (XVECEXP (x
, i
, j
),
4142 XVECEXP (y
, i
, j
), yinsn
) == 0)
4147 if (rtx_equal_for_thread_p (XEXP (x
, i
), XEXP (y
, i
), yinsn
) == 0)
4153 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
4158 /* These are just backpointers, so they don't matter. */
4165 /* It is believed that rtx's at this level will never
4166 contain anything but integers and other rtx's,
4167 except for within LABEL_REFs and SYMBOL_REFs. */