* m68k/m68k.c (m68k_last_compare_had_fp_operands): New variable.
[official-gcc.git] / gcc / jump.c
blobe1382b38e87755920d8b477f99c70e5ce7c61aad
1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 88, 89, 91-96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 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
37 at by later passes.
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. */
54 #include "config.h"
55 #include "rtl.h"
56 #include "flags.h"
57 #include "hard-reg-set.h"
58 #include "regs.h"
59 #include "insn-config.h"
60 #include "insn-flags.h"
61 #include "expr.h"
62 #include "real.h"
63 #include "except.h"
65 /* ??? Eventually must record somehow the labels used by jumps
66 from nested functions. */
67 /* Pre-record the next or previous real insn for each label?
68 No, this pass is very fast anyway. */
69 /* Condense consecutive labels?
70 This would make life analysis faster, maybe. */
71 /* Optimize jump y; x: ... y: jumpif... x?
72 Don't know if it is worth bothering with. */
73 /* Optimize two cases of conditional jump to conditional jump?
74 This can never delete any instruction or make anything dead,
75 or even change what is live at any point.
76 So perhaps let combiner do it. */
78 /* Vector indexed by uid.
79 For each CODE_LABEL, index by its uid to get first unconditional jump
80 that jumps to the label.
81 For each JUMP_INSN, index by its uid to get the next unconditional jump
82 that jumps to the same label.
83 Element 0 is the start of a chain of all return insns.
84 (It is safe to use element 0 because insn uid 0 is not used. */
86 static rtx *jump_chain;
88 /* List of labels referred to from initializers.
89 These can never be deleted. */
90 rtx forced_labels;
92 /* Maximum index in jump_chain. */
94 static int max_jump_chain;
96 /* Set nonzero by jump_optimize if control can fall through
97 to the end of the function. */
98 int can_reach_end;
100 /* Indicates whether death notes are significant in cross jump analysis.
101 Normally they are not significant, because of A and B jump to C,
102 and R dies in A, it must die in B. But this might not be true after
103 stack register conversion, and we must compare death notes in that
104 case. */
106 static int cross_jump_death_matters = 0;
108 static int duplicate_loop_exit_test PROTO((rtx));
109 static void find_cross_jump PROTO((rtx, rtx, int, rtx *, rtx *));
110 static void do_cross_jump PROTO((rtx, rtx, rtx));
111 static int jump_back_p PROTO((rtx, rtx));
112 static int tension_vector_labels PROTO((rtx, int));
113 static void mark_jump_label PROTO((rtx, rtx, int));
114 static void delete_computation PROTO((rtx));
115 static void delete_from_jump_chain PROTO((rtx));
116 static int delete_labelref_insn PROTO((rtx, rtx, int));
117 static void redirect_tablejump PROTO((rtx, rtx));
119 /* Delete no-op jumps and optimize jumps to jumps
120 and jumps around jumps.
121 Delete unused labels and unreachable code.
123 If CROSS_JUMP is 1, detect matching code
124 before a jump and its destination and unify them.
125 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
127 If NOOP_MOVES is nonzero, delete no-op move insns.
129 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
130 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
132 If `optimize' is zero, don't change any code,
133 just determine whether control drops off the end of the function.
134 This case occurs when we have -W and not -O.
135 It works because `delete_insn' checks the value of `optimize'
136 and refrains from actually deleting when that is 0. */
138 void
139 jump_optimize (f, cross_jump, noop_moves, after_regscan)
140 rtx f;
141 int cross_jump;
142 int noop_moves;
143 int after_regscan;
145 register rtx insn, next, note;
146 int changed;
147 int first = 1;
148 int max_uid = 0;
149 rtx last_insn;
151 cross_jump_death_matters = (cross_jump == 2);
153 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
154 notes whose labels don't occur in the insn any more. */
156 for (insn = f; insn; insn = NEXT_INSN (insn))
158 if (GET_CODE (insn) == CODE_LABEL)
159 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
160 else if (GET_CODE (insn) == JUMP_INSN)
161 JUMP_LABEL (insn) = 0;
162 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
163 for (note = REG_NOTES (insn); note; note = next)
165 next = XEXP (note, 1);
166 if (REG_NOTE_KIND (note) == REG_LABEL
167 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
168 remove_note (insn, note);
171 if (INSN_UID (insn) > max_uid)
172 max_uid = INSN_UID (insn);
175 max_uid++;
177 /* Delete insns following barriers, up to next label. */
179 for (insn = f; insn;)
181 if (GET_CODE (insn) == BARRIER)
183 insn = NEXT_INSN (insn);
184 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
186 if (GET_CODE (insn) == NOTE
187 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
188 insn = NEXT_INSN (insn);
189 else
190 insn = delete_insn (insn);
192 /* INSN is now the code_label. */
194 else
195 insn = NEXT_INSN (insn);
198 /* Leave some extra room for labels and duplicate exit test insns
199 we make. */
200 max_jump_chain = max_uid * 14 / 10;
201 jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
202 bzero ((char *) jump_chain, max_jump_chain * sizeof (rtx));
204 /* Mark the label each jump jumps to.
205 Combine consecutive labels, and count uses of labels.
207 For each label, make a chain (using `jump_chain')
208 of all the *unconditional* jumps that jump to it;
209 also make a chain of all returns. */
211 for (insn = f; insn; insn = NEXT_INSN (insn))
212 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
213 && ! INSN_DELETED_P (insn))
215 mark_jump_label (PATTERN (insn), insn, cross_jump);
216 if (GET_CODE (insn) == JUMP_INSN)
218 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
220 jump_chain[INSN_UID (insn)]
221 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
222 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
224 if (GET_CODE (PATTERN (insn)) == RETURN)
226 jump_chain[INSN_UID (insn)] = jump_chain[0];
227 jump_chain[0] = insn;
232 /* Keep track of labels used from static data;
233 they cannot ever be deleted. */
235 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
236 LABEL_NUSES (XEXP (insn, 0))++;
238 check_exception_handler_labels ();
240 /* Keep track of labels used for marking handlers for exception
241 regions; they cannot usually be deleted. */
243 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
244 LABEL_NUSES (XEXP (insn, 0))++;
246 exception_optimize ();
248 /* Delete all labels already not referenced.
249 Also find the last insn. */
251 last_insn = 0;
252 for (insn = f; insn; )
254 if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
255 insn = delete_insn (insn);
256 else
258 last_insn = insn;
259 insn = NEXT_INSN (insn);
263 if (!optimize)
265 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
266 If so record that this function can drop off the end. */
268 insn = last_insn;
270 int n_labels = 1;
271 while (insn
272 /* One label can follow the end-note: the return label. */
273 && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
274 /* Ordinary insns can follow it if returning a structure. */
275 || GET_CODE (insn) == INSN
276 /* If machine uses explicit RETURN insns, no epilogue,
277 then one of them follows the note. */
278 || (GET_CODE (insn) == JUMP_INSN
279 && GET_CODE (PATTERN (insn)) == RETURN)
280 /* A barrier can follow the return insn. */
281 || GET_CODE (insn) == BARRIER
282 /* Other kinds of notes can follow also. */
283 || (GET_CODE (insn) == NOTE
284 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
285 insn = PREV_INSN (insn);
288 /* Report if control can fall through at the end of the function. */
289 if (insn && GET_CODE (insn) == NOTE
290 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
291 && ! INSN_DELETED_P (insn))
292 can_reach_end = 1;
294 /* Zero the "deleted" flag of all the "deleted" insns. */
295 for (insn = f; insn; insn = NEXT_INSN (insn))
296 INSN_DELETED_P (insn) = 0;
297 return;
300 #ifdef HAVE_return
301 if (HAVE_return)
303 /* If we fall through to the epilogue, see if we can insert a RETURN insn
304 in front of it. If the machine allows it at this point (we might be
305 after reload for a leaf routine), it will improve optimization for it
306 to be there. */
307 insn = get_last_insn ();
308 while (insn && GET_CODE (insn) == NOTE)
309 insn = PREV_INSN (insn);
311 if (insn && GET_CODE (insn) != BARRIER)
313 emit_jump_insn (gen_return ());
314 emit_barrier ();
317 #endif
319 if (noop_moves)
320 for (insn = f; insn; )
322 next = NEXT_INSN (insn);
324 if (GET_CODE (insn) == INSN)
326 register rtx body = PATTERN (insn);
328 /* Combine stack_adjusts with following push_insns. */
329 #ifdef PUSH_ROUNDING
330 if (GET_CODE (body) == SET
331 && SET_DEST (body) == stack_pointer_rtx
332 && GET_CODE (SET_SRC (body)) == PLUS
333 && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
334 && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
335 && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
337 rtx p;
338 rtx stack_adjust_insn = insn;
339 int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
340 int total_pushed = 0;
341 int pushes = 0;
343 /* Find all successive push insns. */
344 p = insn;
345 /* Don't convert more than three pushes;
346 that starts adding too many displaced addresses
347 and the whole thing starts becoming a losing
348 proposition. */
349 while (pushes < 3)
351 rtx pbody, dest;
352 p = next_nonnote_insn (p);
353 if (p == 0 || GET_CODE (p) != INSN)
354 break;
355 pbody = PATTERN (p);
356 if (GET_CODE (pbody) != SET)
357 break;
358 dest = SET_DEST (pbody);
359 /* Allow a no-op move between the adjust and the push. */
360 if (GET_CODE (dest) == REG
361 && GET_CODE (SET_SRC (pbody)) == REG
362 && REGNO (dest) == REGNO (SET_SRC (pbody)))
363 continue;
364 if (! (GET_CODE (dest) == MEM
365 && GET_CODE (XEXP (dest, 0)) == POST_INC
366 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
367 break;
368 pushes++;
369 if (total_pushed + GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)))
370 > stack_adjust_amount)
371 break;
372 total_pushed += GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
375 /* Discard the amount pushed from the stack adjust;
376 maybe eliminate it entirely. */
377 if (total_pushed >= stack_adjust_amount)
379 delete_computation (stack_adjust_insn);
380 total_pushed = stack_adjust_amount;
382 else
383 XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
384 = GEN_INT (stack_adjust_amount - total_pushed);
386 /* Change the appropriate push insns to ordinary stores. */
387 p = insn;
388 while (total_pushed > 0)
390 rtx pbody, dest;
391 p = next_nonnote_insn (p);
392 if (GET_CODE (p) != INSN)
393 break;
394 pbody = PATTERN (p);
395 if (GET_CODE (pbody) == SET)
396 break;
397 dest = SET_DEST (pbody);
398 if (! (GET_CODE (dest) == MEM
399 && GET_CODE (XEXP (dest, 0)) == POST_INC
400 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
401 break;
402 total_pushed -= GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
403 /* If this push doesn't fully fit in the space
404 of the stack adjust that we deleted,
405 make another stack adjust here for what we
406 didn't use up. There should be peepholes
407 to recognize the resulting sequence of insns. */
408 if (total_pushed < 0)
410 emit_insn_before (gen_add2_insn (stack_pointer_rtx,
411 GEN_INT (- total_pushed)),
413 break;
415 XEXP (dest, 0)
416 = plus_constant (stack_pointer_rtx, total_pushed);
419 #endif
421 /* Detect and delete no-op move instructions
422 resulting from not allocating a parameter in a register. */
424 if (GET_CODE (body) == SET
425 && (SET_DEST (body) == SET_SRC (body)
426 || (GET_CODE (SET_DEST (body)) == MEM
427 && GET_CODE (SET_SRC (body)) == MEM
428 && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
429 && ! (GET_CODE (SET_DEST (body)) == MEM
430 && MEM_VOLATILE_P (SET_DEST (body)))
431 && ! (GET_CODE (SET_SRC (body)) == MEM
432 && MEM_VOLATILE_P (SET_SRC (body))))
433 delete_computation (insn);
435 /* Detect and ignore no-op move instructions
436 resulting from smart or fortuitous register allocation. */
438 else if (GET_CODE (body) == SET)
440 int sreg = true_regnum (SET_SRC (body));
441 int dreg = true_regnum (SET_DEST (body));
443 if (sreg == dreg && sreg >= 0)
444 delete_insn (insn);
445 else if (sreg >= 0 && dreg >= 0)
447 rtx trial;
448 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
449 sreg, NULL_PTR, dreg,
450 GET_MODE (SET_SRC (body)));
452 if (tem != 0 &&
453 GET_MODE (tem) == GET_MODE (SET_DEST (body)))
455 /* DREG may have been the target of a REG_DEAD note in
456 the insn which makes INSN redundant. If so, reorg
457 would still think it is dead. So search for such a
458 note and delete it if we find it. */
459 if (! find_regno_note (insn, REG_UNUSED, dreg))
460 for (trial = prev_nonnote_insn (insn);
461 trial && GET_CODE (trial) != CODE_LABEL;
462 trial = prev_nonnote_insn (trial))
463 if (find_regno_note (trial, REG_DEAD, dreg))
465 remove_death (dreg, trial);
466 break;
468 #ifdef PRESERVE_DEATH_INFO_REGNO_P
469 /* Deleting insn could lose a death-note for SREG
470 so don't do it if final needs accurate
471 death-notes. */
472 if (PRESERVE_DEATH_INFO_REGNO_P (sreg)
473 && (trial = find_regno_note (insn, REG_DEAD, sreg)))
475 /* Change this into a USE so that we won't emit
476 code for it, but still can keep the note. */
477 PATTERN (insn)
478 = gen_rtx (USE, VOIDmode, XEXP (trial, 0));
479 INSN_CODE (insn) = -1;
480 /* Remove all reg notes but the REG_DEAD one. */
481 REG_NOTES (insn) = trial;
482 XEXP (trial, 1) = NULL_RTX;
484 else
485 #endif
486 delete_insn (insn);
489 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
490 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
491 NULL_PTR, 0,
492 GET_MODE (SET_DEST (body))))
494 /* This handles the case where we have two consecutive
495 assignments of the same constant to pseudos that didn't
496 get a hard reg. Each SET from the constant will be
497 converted into a SET of the spill register and an
498 output reload will be made following it. This produces
499 two loads of the same constant into the same spill
500 register. */
502 rtx in_insn = insn;
504 /* Look back for a death note for the first reg.
505 If there is one, it is no longer accurate. */
506 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
508 if ((GET_CODE (in_insn) == INSN
509 || GET_CODE (in_insn) == JUMP_INSN)
510 && find_regno_note (in_insn, REG_DEAD, dreg))
512 remove_death (dreg, in_insn);
513 break;
515 in_insn = PREV_INSN (in_insn);
518 /* Delete the second load of the value. */
519 delete_insn (insn);
522 else if (GET_CODE (body) == PARALLEL)
524 /* If each part is a set between two identical registers or
525 a USE or CLOBBER, delete the insn. */
526 int i, sreg, dreg;
527 rtx tem;
529 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
531 tem = XVECEXP (body, 0, i);
532 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
533 continue;
535 if (GET_CODE (tem) != SET
536 || (sreg = true_regnum (SET_SRC (tem))) < 0
537 || (dreg = true_regnum (SET_DEST (tem))) < 0
538 || dreg != sreg)
539 break;
542 if (i < 0)
543 delete_insn (insn);
545 /* Also delete insns to store bit fields if they are no-ops. */
546 /* Not worth the hair to detect this in the big-endian case. */
547 else if (! BYTES_BIG_ENDIAN
548 && GET_CODE (body) == SET
549 && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
550 && XEXP (SET_DEST (body), 2) == const0_rtx
551 && XEXP (SET_DEST (body), 0) == SET_SRC (body)
552 && ! (GET_CODE (SET_SRC (body)) == MEM
553 && MEM_VOLATILE_P (SET_SRC (body))))
554 delete_insn (insn);
556 insn = next;
559 /* If we haven't yet gotten to reload and we have just run regscan,
560 delete any insn that sets a register that isn't used elsewhere.
561 This helps some of the optimizations below by having less insns
562 being jumped around. */
564 if (! reload_completed && after_regscan)
565 for (insn = f; insn; insn = next)
567 rtx set = single_set (insn);
569 next = NEXT_INSN (insn);
571 if (set && GET_CODE (SET_DEST (set)) == REG
572 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
573 && regno_first_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
574 /* We use regno_last_note_uid so as not to delete the setting
575 of a reg that's used in notes. A subsequent optimization
576 might arrange to use that reg for real. */
577 && regno_last_note_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
578 && ! side_effects_p (SET_SRC (set))
579 && ! find_reg_note (insn, REG_RETVAL, 0))
580 delete_insn (insn);
583 /* Now iterate optimizing jumps until nothing changes over one pass. */
584 changed = 1;
585 while (changed)
587 changed = 0;
589 for (insn = f; insn; insn = next)
591 rtx reallabelprev;
592 rtx temp, temp1, temp2, temp3, temp4, temp5, temp6;
593 rtx nlabel;
594 int this_is_simplejump, this_is_condjump, reversep;
595 int this_is_condjump_in_parallel;
596 #if 0
597 /* If NOT the first iteration, if this is the last jump pass
598 (just before final), do the special peephole optimizations.
599 Avoiding the first iteration gives ordinary jump opts
600 a chance to work before peephole opts. */
602 if (reload_completed && !first && !flag_no_peephole)
603 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
604 peephole (insn);
605 #endif
607 /* That could have deleted some insns after INSN, so check now
608 what the following insn is. */
610 next = NEXT_INSN (insn);
612 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
613 jump. Try to optimize by duplicating the loop exit test if so.
614 This is only safe immediately after regscan, because it uses
615 the values of regno_first_uid and regno_last_uid. */
616 if (after_regscan && GET_CODE (insn) == NOTE
617 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
618 && (temp1 = next_nonnote_insn (insn)) != 0
619 && simplejump_p (temp1))
621 temp = PREV_INSN (insn);
622 if (duplicate_loop_exit_test (insn))
624 changed = 1;
625 next = NEXT_INSN (temp);
626 continue;
630 if (GET_CODE (insn) != JUMP_INSN)
631 continue;
633 this_is_simplejump = simplejump_p (insn);
634 this_is_condjump = condjump_p (insn);
635 this_is_condjump_in_parallel = condjump_in_parallel_p (insn);
637 /* Tension the labels in dispatch tables. */
639 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
640 changed |= tension_vector_labels (PATTERN (insn), 0);
641 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
642 changed |= tension_vector_labels (PATTERN (insn), 1);
644 /* If a dispatch table always goes to the same place,
645 get rid of it and replace the insn that uses it. */
647 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
648 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
650 int i;
651 rtx pat = PATTERN (insn);
652 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
653 int len = XVECLEN (pat, diff_vec_p);
654 rtx dispatch = prev_real_insn (insn);
656 for (i = 0; i < len; i++)
657 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
658 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
659 break;
660 if (i == len
661 && dispatch != 0
662 && GET_CODE (dispatch) == JUMP_INSN
663 && JUMP_LABEL (dispatch) != 0
664 /* Don't mess with a casesi insn. */
665 && !(GET_CODE (PATTERN (dispatch)) == SET
666 && (GET_CODE (SET_SRC (PATTERN (dispatch)))
667 == IF_THEN_ELSE))
668 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
670 redirect_tablejump (dispatch,
671 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
672 changed = 1;
676 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
678 /* If a jump references the end of the function, try to turn
679 it into a RETURN insn, possibly a conditional one. */
680 if (JUMP_LABEL (insn)
681 && (next_active_insn (JUMP_LABEL (insn)) == 0
682 || GET_CODE (PATTERN (next_active_insn (JUMP_LABEL (insn))))
683 == RETURN))
684 changed |= redirect_jump (insn, NULL_RTX);
686 /* Detect jump to following insn. */
687 if (reallabelprev == insn && condjump_p (insn))
689 next = next_real_insn (JUMP_LABEL (insn));
690 delete_jump (insn);
691 changed = 1;
692 continue;
695 /* If we have an unconditional jump preceded by a USE, try to put
696 the USE before the target and jump there. This simplifies many
697 of the optimizations below since we don't have to worry about
698 dealing with these USE insns. We only do this if the label
699 being branch to already has the identical USE or if code
700 never falls through to that label. */
702 if (this_is_simplejump
703 && (temp = prev_nonnote_insn (insn)) != 0
704 && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
705 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
706 && (GET_CODE (temp1) == BARRIER
707 || (GET_CODE (temp1) == INSN
708 && rtx_equal_p (PATTERN (temp), PATTERN (temp1))))
709 /* Don't do this optimization if we have a loop containing only
710 the USE instruction, and the loop start label has a usage
711 count of 1. This is because we will redo this optimization
712 everytime through the outer loop, and jump opt will never
713 exit. */
714 && ! ((temp2 = prev_nonnote_insn (temp)) != 0
715 && temp2 == JUMP_LABEL (insn)
716 && LABEL_NUSES (temp2) == 1))
718 if (GET_CODE (temp1) == BARRIER)
720 emit_insn_after (PATTERN (temp), temp1);
721 temp1 = NEXT_INSN (temp1);
724 delete_insn (temp);
725 redirect_jump (insn, get_label_before (temp1));
726 reallabelprev = prev_real_insn (temp1);
727 changed = 1;
730 /* Simplify if (...) x = a; else x = b; by converting it
731 to x = b; if (...) x = a;
732 if B is sufficiently simple, the test doesn't involve X,
733 and nothing in the test modifies B or X.
735 If we have small register classes, we also can't do this if X
736 is a hard register.
738 If the "x = b;" insn has any REG_NOTES, we don't do this because
739 of the possibility that we are running after CSE and there is a
740 REG_EQUAL note that is only valid if the branch has already been
741 taken. If we move the insn with the REG_EQUAL note, we may
742 fold the comparison to always be false in a later CSE pass.
743 (We could also delete the REG_NOTES when moving the insn, but it
744 seems simpler to not move it.) An exception is that we can move
745 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
746 value is the same as "b".
748 INSN is the branch over the `else' part.
750 We set:
752 TEMP to the jump insn preceding "x = a;"
753 TEMP1 to X
754 TEMP2 to the insn that sets "x = b;"
755 TEMP3 to the insn that sets "x = a;"
756 TEMP4 to the set of "x = b"; */
758 if (this_is_simplejump
759 && (temp3 = prev_active_insn (insn)) != 0
760 && GET_CODE (temp3) == INSN
761 && (temp4 = single_set (temp3)) != 0
762 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
763 #ifdef SMALL_REGISTER_CLASSES
764 && (! SMALL_REGISTER_CLASSES
765 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
766 #endif
767 && (temp2 = next_active_insn (insn)) != 0
768 && GET_CODE (temp2) == INSN
769 && (temp4 = single_set (temp2)) != 0
770 && rtx_equal_p (SET_DEST (temp4), temp1)
771 && (GET_CODE (SET_SRC (temp4)) == REG
772 || GET_CODE (SET_SRC (temp4)) == SUBREG
773 || CONSTANT_P (SET_SRC (temp4)))
774 && (REG_NOTES (temp2) == 0
775 || ((REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUAL
776 || REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUIV)
777 && XEXP (REG_NOTES (temp2), 1) == 0
778 && rtx_equal_p (XEXP (REG_NOTES (temp2), 0),
779 SET_SRC (temp4))))
780 && (temp = prev_active_insn (temp3)) != 0
781 && condjump_p (temp) && ! simplejump_p (temp)
782 /* TEMP must skip over the "x = a;" insn */
783 && prev_real_insn (JUMP_LABEL (temp)) == insn
784 && no_labels_between_p (insn, JUMP_LABEL (temp))
785 /* There must be no other entries to the "x = b;" insn. */
786 && no_labels_between_p (JUMP_LABEL (temp), temp2)
787 /* INSN must either branch to the insn after TEMP2 or the insn
788 after TEMP2 must branch to the same place as INSN. */
789 && (reallabelprev == temp2
790 || ((temp5 = next_active_insn (temp2)) != 0
791 && simplejump_p (temp5)
792 && JUMP_LABEL (temp5) == JUMP_LABEL (insn))))
794 /* The test expression, X, may be a complicated test with
795 multiple branches. See if we can find all the uses of
796 the label that TEMP branches to without hitting a CALL_INSN
797 or a jump to somewhere else. */
798 rtx target = JUMP_LABEL (temp);
799 int nuses = LABEL_NUSES (target);
800 rtx p, q;
802 /* Set P to the first jump insn that goes around "x = a;". */
803 for (p = temp; nuses && p; p = prev_nonnote_insn (p))
805 if (GET_CODE (p) == JUMP_INSN)
807 if (condjump_p (p) && ! simplejump_p (p)
808 && JUMP_LABEL (p) == target)
810 nuses--;
811 if (nuses == 0)
812 break;
814 else
815 break;
817 else if (GET_CODE (p) == CALL_INSN)
818 break;
821 #ifdef HAVE_cc0
822 /* We cannot insert anything between a set of cc and its use
823 so if P uses cc0, we must back up to the previous insn. */
824 q = prev_nonnote_insn (p);
825 if (q && GET_RTX_CLASS (GET_CODE (q)) == 'i'
826 && sets_cc0_p (PATTERN (q)))
827 p = q;
828 #endif
830 if (p)
831 p = PREV_INSN (p);
833 /* If we found all the uses and there was no data conflict, we
834 can move the assignment unless we can branch into the middle
835 from somewhere. */
836 if (nuses == 0 && p
837 && no_labels_between_p (p, insn)
838 && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
839 && ! reg_set_between_p (temp1, p, temp3)
840 && (GET_CODE (SET_SRC (temp4)) == CONST_INT
841 || ! reg_set_between_p (SET_SRC (temp4), p, temp2)))
843 emit_insn_after_with_line_notes (PATTERN (temp2), p, temp2);
844 delete_insn (temp2);
846 /* Set NEXT to an insn that we know won't go away. */
847 next = next_active_insn (insn);
849 /* Delete the jump around the set. Note that we must do
850 this before we redirect the test jumps so that it won't
851 delete the code immediately following the assignment
852 we moved (which might be a jump). */
854 delete_insn (insn);
856 /* We either have two consecutive labels or a jump to
857 a jump, so adjust all the JUMP_INSNs to branch to where
858 INSN branches to. */
859 for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
860 if (GET_CODE (p) == JUMP_INSN)
861 redirect_jump (p, target);
863 changed = 1;
864 continue;
868 /* Simplify if (...) { x = a; goto l; } x = b; by converting it
869 to x = a; if (...) goto l; x = b;
870 if A is sufficiently simple, the test doesn't involve X,
871 and nothing in the test modifies A or X.
873 If we have small register classes, we also can't do this if X
874 is a hard register.
876 If the "x = a;" insn has any REG_NOTES, we don't do this because
877 of the possibility that we are running after CSE and there is a
878 REG_EQUAL note that is only valid if the branch has already been
879 taken. If we move the insn with the REG_EQUAL note, we may
880 fold the comparison to always be false in a later CSE pass.
881 (We could also delete the REG_NOTES when moving the insn, but it
882 seems simpler to not move it.) An exception is that we can move
883 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
884 value is the same as "a".
886 INSN is the goto.
888 We set:
890 TEMP to the jump insn preceding "x = a;"
891 TEMP1 to X
892 TEMP2 to the insn that sets "x = b;"
893 TEMP3 to the insn that sets "x = a;"
894 TEMP4 to the set of "x = a"; */
896 if (this_is_simplejump
897 && (temp2 = next_active_insn (insn)) != 0
898 && GET_CODE (temp2) == INSN
899 && (temp4 = single_set (temp2)) != 0
900 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
901 #ifdef SMALL_REGISTER_CLASSES
902 && (! SMALL_REGISTER_CLASSES
903 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
904 #endif
906 && (temp3 = prev_active_insn (insn)) != 0
907 && GET_CODE (temp3) == INSN
908 && (temp4 = single_set (temp3)) != 0
909 && rtx_equal_p (SET_DEST (temp4), temp1)
910 && (GET_CODE (SET_SRC (temp4)) == REG
911 || GET_CODE (SET_SRC (temp4)) == SUBREG
912 || CONSTANT_P (SET_SRC (temp4)))
913 && (REG_NOTES (temp3) == 0
914 || ((REG_NOTE_KIND (REG_NOTES (temp3)) == REG_EQUAL
915 || REG_NOTE_KIND (REG_NOTES (temp3)) == REG_EQUIV)
916 && XEXP (REG_NOTES (temp3), 1) == 0
917 && rtx_equal_p (XEXP (REG_NOTES (temp3), 0),
918 SET_SRC (temp4))))
919 && (temp = prev_active_insn (temp3)) != 0
920 && condjump_p (temp) && ! simplejump_p (temp)
921 /* TEMP must skip over the "x = a;" insn */
922 && prev_real_insn (JUMP_LABEL (temp)) == insn
923 && no_labels_between_p (temp, insn))
925 rtx prev_label = JUMP_LABEL (temp);
926 rtx insert_after = prev_nonnote_insn (temp);
928 #ifdef HAVE_cc0
929 /* We cannot insert anything between a set of cc and its use. */
930 if (insert_after && GET_RTX_CLASS (GET_CODE (insert_after)) == 'i'
931 && sets_cc0_p (PATTERN (insert_after)))
932 insert_after = prev_nonnote_insn (insert_after);
933 #endif
934 ++LABEL_NUSES (prev_label);
936 if (insert_after
937 && no_labels_between_p (insert_after, temp)
938 && ! reg_referenced_between_p (temp1, insert_after, temp3)
939 && ! reg_referenced_between_p (temp1, temp3,
940 NEXT_INSN (temp2))
941 && ! reg_set_between_p (temp1, insert_after, temp)
942 && (GET_CODE (SET_SRC (temp4)) == CONST_INT
943 || ! reg_set_between_p (SET_SRC (temp4),
944 insert_after, temp))
945 && invert_jump (temp, JUMP_LABEL (insn)))
947 emit_insn_after_with_line_notes (PATTERN (temp3),
948 insert_after, temp3);
949 delete_insn (temp3);
950 delete_insn (insn);
951 /* Set NEXT to an insn that we know won't go away. */
952 next = temp2;
953 changed = 1;
955 if (prev_label && --LABEL_NUSES (prev_label) == 0)
956 delete_insn (prev_label);
957 if (changed)
958 continue;
961 #ifndef HAVE_cc0
962 /* If we have if (...) x = exp; and branches are expensive,
963 EXP is a single insn, does not have any side effects, cannot
964 trap, and is not too costly, convert this to
965 t = exp; if (...) x = t;
967 Don't do this when we have CC0 because it is unlikely to help
968 and we'd need to worry about where to place the new insn and
969 the potential for conflicts. We also can't do this when we have
970 notes on the insn for the same reason as above.
972 We set:
974 TEMP to the "x = exp;" insn.
975 TEMP1 to the single set in the "x = exp; insn.
976 TEMP2 to "x". */
978 if (! reload_completed
979 && this_is_condjump && ! this_is_simplejump
980 && BRANCH_COST >= 3
981 && (temp = next_nonnote_insn (insn)) != 0
982 && GET_CODE (temp) == INSN
983 && REG_NOTES (temp) == 0
984 && (reallabelprev == temp
985 || ((temp2 = next_active_insn (temp)) != 0
986 && simplejump_p (temp2)
987 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
988 && (temp1 = single_set (temp)) != 0
989 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
990 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
991 #ifdef SMALL_REGISTER_CLASSES
992 && (! SMALL_REGISTER_CLASSES
993 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
994 #endif
995 && GET_CODE (SET_SRC (temp1)) != REG
996 && GET_CODE (SET_SRC (temp1)) != SUBREG
997 && GET_CODE (SET_SRC (temp1)) != CONST_INT
998 && ! side_effects_p (SET_SRC (temp1))
999 && ! may_trap_p (SET_SRC (temp1))
1000 && rtx_cost (SET_SRC (temp1), SET) < 10)
1002 rtx new = gen_reg_rtx (GET_MODE (temp2));
1004 if (validate_change (temp, &SET_DEST (temp1), new, 0))
1006 next = emit_insn_after (gen_move_insn (temp2, new), insn);
1007 emit_insn_after_with_line_notes (PATTERN (temp),
1008 PREV_INSN (insn), temp);
1009 delete_insn (temp);
1010 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
1014 /* Similarly, if it takes two insns to compute EXP but they
1015 have the same destination. Here TEMP3 will be the second
1016 insn and TEMP4 the SET from that insn. */
1018 if (! reload_completed
1019 && this_is_condjump && ! this_is_simplejump
1020 && BRANCH_COST >= 4
1021 && (temp = next_nonnote_insn (insn)) != 0
1022 && GET_CODE (temp) == INSN
1023 && REG_NOTES (temp) == 0
1024 && (temp3 = next_nonnote_insn (temp)) != 0
1025 && GET_CODE (temp3) == INSN
1026 && REG_NOTES (temp3) == 0
1027 && (reallabelprev == temp3
1028 || ((temp2 = next_active_insn (temp3)) != 0
1029 && simplejump_p (temp2)
1030 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
1031 && (temp1 = single_set (temp)) != 0
1032 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
1033 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
1034 #ifdef SMALL_REGISTER_CLASSES
1035 && (! SMALL_REGISTER_CLASSES
1036 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
1037 #endif
1038 && ! side_effects_p (SET_SRC (temp1))
1039 && ! may_trap_p (SET_SRC (temp1))
1040 && rtx_cost (SET_SRC (temp1), SET) < 10
1041 && (temp4 = single_set (temp3)) != 0
1042 && rtx_equal_p (SET_DEST (temp4), temp2)
1043 && ! side_effects_p (SET_SRC (temp4))
1044 && ! may_trap_p (SET_SRC (temp4))
1045 && rtx_cost (SET_SRC (temp4), SET) < 10)
1047 rtx new = gen_reg_rtx (GET_MODE (temp2));
1049 if (validate_change (temp, &SET_DEST (temp1), new, 0))
1051 next = emit_insn_after (gen_move_insn (temp2, new), insn);
1052 emit_insn_after_with_line_notes (PATTERN (temp),
1053 PREV_INSN (insn), temp);
1054 emit_insn_after_with_line_notes
1055 (replace_rtx (PATTERN (temp3), temp2, new),
1056 PREV_INSN (insn), temp3);
1057 delete_insn (temp);
1058 delete_insn (temp3);
1059 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
1063 /* Finally, handle the case where two insns are used to
1064 compute EXP but a temporary register is used. Here we must
1065 ensure that the temporary register is not used anywhere else. */
1067 if (! reload_completed
1068 && after_regscan
1069 && this_is_condjump && ! this_is_simplejump
1070 && BRANCH_COST >= 4
1071 && (temp = next_nonnote_insn (insn)) != 0
1072 && GET_CODE (temp) == INSN
1073 && REG_NOTES (temp) == 0
1074 && (temp3 = next_nonnote_insn (temp)) != 0
1075 && GET_CODE (temp3) == INSN
1076 && REG_NOTES (temp3) == 0
1077 && (reallabelprev == temp3
1078 || ((temp2 = next_active_insn (temp3)) != 0
1079 && simplejump_p (temp2)
1080 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
1081 && (temp1 = single_set (temp)) != 0
1082 && (temp5 = SET_DEST (temp1),
1083 (GET_CODE (temp5) == REG
1084 || (GET_CODE (temp5) == SUBREG
1085 && (temp5 = SUBREG_REG (temp5),
1086 GET_CODE (temp5) == REG))))
1087 && REGNO (temp5) >= FIRST_PSEUDO_REGISTER
1088 && regno_first_uid[REGNO (temp5)] == INSN_UID (temp)
1089 && regno_last_uid[REGNO (temp5)] == INSN_UID (temp3)
1090 && ! side_effects_p (SET_SRC (temp1))
1091 && ! may_trap_p (SET_SRC (temp1))
1092 && rtx_cost (SET_SRC (temp1), SET) < 10
1093 && (temp4 = single_set (temp3)) != 0
1094 && (temp2 = SET_DEST (temp4), GET_CODE (temp2) == REG)
1095 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
1096 #ifdef SMALL_REGISTER_CLASSES
1097 && (! SMALL_REGISTER_CLASSES
1098 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
1099 #endif
1100 && rtx_equal_p (SET_DEST (temp4), temp2)
1101 && ! side_effects_p (SET_SRC (temp4))
1102 && ! may_trap_p (SET_SRC (temp4))
1103 && rtx_cost (SET_SRC (temp4), SET) < 10)
1105 rtx new = gen_reg_rtx (GET_MODE (temp2));
1107 if (validate_change (temp3, &SET_DEST (temp4), new, 0))
1109 next = emit_insn_after (gen_move_insn (temp2, new), insn);
1110 emit_insn_after_with_line_notes (PATTERN (temp),
1111 PREV_INSN (insn), temp);
1112 emit_insn_after_with_line_notes (PATTERN (temp3),
1113 PREV_INSN (insn), temp3);
1114 delete_insn (temp);
1115 delete_insn (temp3);
1116 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
1119 #endif /* HAVE_cc0 */
1121 /* Try to use a conditional move (if the target has them), or a
1122 store-flag insn. The general case is:
1124 1) x = a; if (...) x = b; and
1125 2) if (...) x = b;
1127 If the jump would be faster, the machine should not have defined
1128 the movcc or scc insns!. These cases are often made by the
1129 previous optimization.
1131 The second case is treated as x = x; if (...) x = b;.
1133 INSN here is the jump around the store. We set:
1135 TEMP to the "x = b;" insn.
1136 TEMP1 to X.
1137 TEMP2 to B.
1138 TEMP3 to A (X in the second case).
1139 TEMP4 to the condition being tested.
1140 TEMP5 to the earliest insn used to find the condition. */
1142 if (/* We can't do this after reload has completed. */
1143 ! reload_completed
1144 && this_is_condjump && ! this_is_simplejump
1145 /* Set TEMP to the "x = b;" insn. */
1146 && (temp = next_nonnote_insn (insn)) != 0
1147 && GET_CODE (temp) == INSN
1148 && GET_CODE (PATTERN (temp)) == SET
1149 && GET_CODE (temp1 = SET_DEST (PATTERN (temp))) == REG
1150 #ifdef SMALL_REGISTER_CLASSES
1151 && (! SMALL_REGISTER_CLASSES
1152 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
1153 #endif
1154 && (GET_CODE (temp2 = SET_SRC (PATTERN (temp))) == REG
1155 || GET_CODE (temp2) == SUBREG
1156 /* ??? How about floating point constants? */
1157 || GET_CODE (temp2) == CONST_INT)
1158 /* Allow either form, but prefer the former if both apply.
1159 There is no point in using the old value of TEMP1 if
1160 it is a register, since cse will alias them. It can
1161 lose if the old value were a hard register since CSE
1162 won't replace hard registers. */
1163 && (((temp3 = reg_set_last (temp1, insn)) != 0)
1164 /* Make the latter case look like x = x; if (...) x = b; */
1165 || (temp3 = temp1, 1))
1166 /* INSN must either branch to the insn after TEMP or the insn
1167 after TEMP must branch to the same place as INSN. */
1168 && (reallabelprev == temp
1169 || ((temp4 = next_active_insn (temp)) != 0
1170 && simplejump_p (temp4)
1171 && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
1172 && (temp4 = get_condition (insn, &temp5)) != 0
1173 /* We must be comparing objects whose modes imply the size.
1174 We could handle BLKmode if (1) emit_store_flag could
1175 and (2) we could find the size reliably. */
1176 && GET_MODE (XEXP (temp4, 0)) != BLKmode
1177 /* Even if branches are cheap, the store_flag optimization
1178 can win when the operation to be performed can be
1179 expressed directly. */
1180 #ifdef HAVE_cc0
1181 /* If the previous insn sets CC0 and something else, we can't
1182 do this since we are going to delete that insn. */
1184 && ! ((temp6 = prev_nonnote_insn (insn)) != 0
1185 && GET_CODE (temp6) == INSN
1186 && (sets_cc0_p (PATTERN (temp6)) == -1
1187 || (sets_cc0_p (PATTERN (temp6)) == 1
1188 && FIND_REG_INC_NOTE (temp6, NULL_RTX))))
1189 #endif
1192 #ifdef HAVE_conditional_move
1193 /* First try a conditional move. */
1195 enum rtx_code code = GET_CODE (temp4);
1196 rtx var = temp1;
1197 rtx cond0, cond1, aval, bval;
1198 rtx target;
1200 /* Copy the compared variables into cond0 and cond1, so that
1201 any side effects performed in or after the old comparison,
1202 will not affect our compare which will come later. */
1203 /* ??? Is it possible to just use the comparison in the jump
1204 insn? After all, we're going to delete it. We'd have
1205 to modify emit_conditional_move to take a comparison rtx
1206 instead or write a new function. */
1207 cond0 = gen_reg_rtx (GET_MODE (XEXP (temp4, 0)));
1208 /* We want the target to be able to simplify comparisons with
1209 zero (and maybe other constants as well), so don't create
1210 pseudos for them. There's no need to either. */
1211 if (GET_CODE (XEXP (temp4, 1)) == CONST_INT
1212 || GET_CODE (XEXP (temp4, 1)) == CONST_DOUBLE)
1213 cond1 = XEXP (temp4, 1);
1214 else
1215 cond1 = gen_reg_rtx (GET_MODE (XEXP (temp4, 1)));
1217 aval = temp3;
1218 bval = temp2;
1220 start_sequence ();
1221 target = emit_conditional_move (var, code,
1222 cond0, cond1, VOIDmode,
1223 aval, bval, GET_MODE (var),
1224 (code == LTU || code == GEU
1225 || code == LEU || code == GTU));
1227 if (target)
1229 rtx seq1,seq2;
1231 /* Save the conditional move sequence but don't emit it
1232 yet. On some machines, like the alpha, it is possible
1233 that temp5 == insn, so next generate the sequence that
1234 saves the compared values and then emit both
1235 sequences ensuring seq1 occurs before seq2. */
1236 seq2 = get_insns ();
1237 end_sequence ();
1239 /* Now that we can't fail, generate the copy insns that
1240 preserve the compared values. */
1241 start_sequence ();
1242 emit_move_insn (cond0, XEXP (temp4, 0));
1243 if (cond1 != XEXP (temp4, 1))
1244 emit_move_insn (cond1, XEXP (temp4, 1));
1245 seq1 = get_insns ();
1246 end_sequence ();
1248 emit_insns_before (seq1, temp5);
1249 /* Insert conditional move after insn, to be sure that
1250 the jump and a possible compare won't be separated */
1251 emit_insns_after (seq2, insn);
1253 /* ??? We can also delete the insn that sets X to A.
1254 Flow will do it too though. */
1255 delete_insn (temp);
1256 next = NEXT_INSN (insn);
1257 delete_jump (insn);
1258 changed = 1;
1259 continue;
1261 else
1262 end_sequence ();
1264 #endif
1266 /* That didn't work, try a store-flag insn.
1268 We further divide the cases into:
1270 1) x = a; if (...) x = b; and either A or B is zero,
1271 2) if (...) x = 0; and jumps are expensive,
1272 3) x = a; if (...) x = b; and A and B are constants where all
1273 the set bits in A are also set in B and jumps are expensive,
1274 4) x = a; if (...) x = b; and A and B non-zero, and jumps are
1275 more expensive, and
1276 5) if (...) x = b; if jumps are even more expensive. */
1278 if (GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
1279 && ((GET_CODE (temp3) == CONST_INT)
1280 /* Make the latter case look like
1281 x = x; if (...) x = 0; */
1282 || (temp3 = temp1,
1283 ((BRANCH_COST >= 2
1284 && temp2 == const0_rtx)
1285 || BRANCH_COST >= 3)))
1286 /* If B is zero, OK; if A is zero, can only do (1) if we
1287 can reverse the condition. See if (3) applies possibly
1288 by reversing the condition. Prefer reversing to (4) when
1289 branches are very expensive. */
1290 && (((BRANCH_COST >= 2
1291 || STORE_FLAG_VALUE == -1
1292 || (STORE_FLAG_VALUE == 1
1293 /* Check that the mask is a power of two,
1294 so that it can probably be generated
1295 with a shift. */
1296 && exact_log2 (INTVAL (temp3)) >= 0))
1297 && (reversep = 0, temp2 == const0_rtx))
1298 || ((BRANCH_COST >= 2
1299 || STORE_FLAG_VALUE == -1
1300 || (STORE_FLAG_VALUE == 1
1301 && exact_log2 (INTVAL (temp2)) >= 0))
1302 && temp3 == const0_rtx
1303 && (reversep = can_reverse_comparison_p (temp4, insn)))
1304 || (BRANCH_COST >= 2
1305 && GET_CODE (temp2) == CONST_INT
1306 && GET_CODE (temp3) == CONST_INT
1307 && ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp2)
1308 || ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp3)
1309 && (reversep = can_reverse_comparison_p (temp4,
1310 insn)))))
1311 || BRANCH_COST >= 3)
1314 enum rtx_code code = GET_CODE (temp4);
1315 rtx uval, cval, var = temp1;
1316 int normalizep;
1317 rtx target;
1319 /* If necessary, reverse the condition. */
1320 if (reversep)
1321 code = reverse_condition (code), uval = temp2, cval = temp3;
1322 else
1323 uval = temp3, cval = temp2;
1325 /* If CVAL is non-zero, normalize to -1. Otherwise, if UVAL
1326 is the constant 1, it is best to just compute the result
1327 directly. If UVAL is constant and STORE_FLAG_VALUE
1328 includes all of its bits, it is best to compute the flag
1329 value unnormalized and `and' it with UVAL. Otherwise,
1330 normalize to -1 and `and' with UVAL. */
1331 normalizep = (cval != const0_rtx ? -1
1332 : (uval == const1_rtx ? 1
1333 : (GET_CODE (uval) == CONST_INT
1334 && (INTVAL (uval) & ~STORE_FLAG_VALUE) == 0)
1335 ? 0 : -1));
1337 /* We will be putting the store-flag insn immediately in
1338 front of the comparison that was originally being done,
1339 so we know all the variables in TEMP4 will be valid.
1340 However, this might be in front of the assignment of
1341 A to VAR. If it is, it would clobber the store-flag
1342 we will be emitting.
1344 Therefore, emit into a temporary which will be copied to
1345 VAR immediately after TEMP. */
1347 start_sequence ();
1348 target = emit_store_flag (gen_reg_rtx (GET_MODE (var)), code,
1349 XEXP (temp4, 0), XEXP (temp4, 1),
1350 VOIDmode,
1351 (code == LTU || code == LEU
1352 || code == GEU || code == GTU),
1353 normalizep);
1354 if (target)
1356 rtx seq;
1357 rtx before = insn;
1359 seq = get_insns ();
1360 end_sequence ();
1362 /* Put the store-flag insns in front of the first insn
1363 used to compute the condition to ensure that we
1364 use the same values of them as the current
1365 comparison. However, the remainder of the insns we
1366 generate will be placed directly in front of the
1367 jump insn, in case any of the pseudos we use
1368 are modified earlier. */
1370 emit_insns_before (seq, temp5);
1372 start_sequence ();
1374 /* Both CVAL and UVAL are non-zero. */
1375 if (cval != const0_rtx && uval != const0_rtx)
1377 rtx tem1, tem2;
1379 tem1 = expand_and (uval, target, NULL_RTX);
1380 if (GET_CODE (cval) == CONST_INT
1381 && GET_CODE (uval) == CONST_INT
1382 && (INTVAL (cval) & INTVAL (uval)) == INTVAL (cval))
1383 tem2 = cval;
1384 else
1386 tem2 = expand_unop (GET_MODE (var), one_cmpl_optab,
1387 target, NULL_RTX, 0);
1388 tem2 = expand_and (cval, tem2,
1389 (GET_CODE (tem2) == REG
1390 ? tem2 : 0));
1393 /* If we usually make new pseudos, do so here. This
1394 turns out to help machines that have conditional
1395 move insns. */
1396 /* ??? Conditional moves have already been handled.
1397 This may be obsolete. */
1399 if (flag_expensive_optimizations)
1400 target = 0;
1402 target = expand_binop (GET_MODE (var), ior_optab,
1403 tem1, tem2, target,
1404 1, OPTAB_WIDEN);
1406 else if (normalizep != 1)
1408 /* We know that either CVAL or UVAL is zero. If
1409 UVAL is zero, negate TARGET and `and' with CVAL.
1410 Otherwise, `and' with UVAL. */
1411 if (uval == const0_rtx)
1413 target = expand_unop (GET_MODE (var), one_cmpl_optab,
1414 target, NULL_RTX, 0);
1415 uval = cval;
1418 target = expand_and (uval, target,
1419 (GET_CODE (target) == REG
1420 && ! preserve_subexpressions_p ()
1421 ? target : NULL_RTX));
1424 emit_move_insn (var, target);
1425 seq = get_insns ();
1426 end_sequence ();
1427 #ifdef HAVE_cc0
1428 /* If INSN uses CC0, we must not separate it from the
1429 insn that sets cc0. */
1430 if (reg_mentioned_p (cc0_rtx, PATTERN (before)))
1431 before = prev_nonnote_insn (before);
1432 #endif
1433 emit_insns_before (seq, before);
1435 delete_insn (temp);
1436 next = NEXT_INSN (insn);
1437 delete_jump (insn);
1438 changed = 1;
1439 continue;
1441 else
1442 end_sequence ();
1446 /* If branches are expensive, convert
1447 if (foo) bar++; to bar += (foo != 0);
1448 and similarly for "bar--;"
1450 INSN is the conditional branch around the arithmetic. We set:
1452 TEMP is the arithmetic insn.
1453 TEMP1 is the SET doing the arithmetic.
1454 TEMP2 is the operand being incremented or decremented.
1455 TEMP3 to the condition being tested.
1456 TEMP4 to the earliest insn used to find the condition. */
1458 if ((BRANCH_COST >= 2
1459 #ifdef HAVE_incscc
1460 || HAVE_incscc
1461 #endif
1462 #ifdef HAVE_decscc
1463 || HAVE_decscc
1464 #endif
1466 && ! reload_completed
1467 && this_is_condjump && ! this_is_simplejump
1468 && (temp = next_nonnote_insn (insn)) != 0
1469 && (temp1 = single_set (temp)) != 0
1470 && (temp2 = SET_DEST (temp1),
1471 GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT)
1472 && GET_CODE (SET_SRC (temp1)) == PLUS
1473 && (XEXP (SET_SRC (temp1), 1) == const1_rtx
1474 || XEXP (SET_SRC (temp1), 1) == constm1_rtx)
1475 && rtx_equal_p (temp2, XEXP (SET_SRC (temp1), 0))
1476 && ! side_effects_p (temp2)
1477 && ! may_trap_p (temp2)
1478 /* INSN must either branch to the insn after TEMP or the insn
1479 after TEMP must branch to the same place as INSN. */
1480 && (reallabelprev == temp
1481 || ((temp3 = next_active_insn (temp)) != 0
1482 && simplejump_p (temp3)
1483 && JUMP_LABEL (temp3) == JUMP_LABEL (insn)))
1484 && (temp3 = get_condition (insn, &temp4)) != 0
1485 /* We must be comparing objects whose modes imply the size.
1486 We could handle BLKmode if (1) emit_store_flag could
1487 and (2) we could find the size reliably. */
1488 && GET_MODE (XEXP (temp3, 0)) != BLKmode
1489 && can_reverse_comparison_p (temp3, insn))
1491 rtx temp6, target = 0, seq, init_insn = 0, init = temp2;
1492 enum rtx_code code = reverse_condition (GET_CODE (temp3));
1494 start_sequence ();
1496 /* It must be the case that TEMP2 is not modified in the range
1497 [TEMP4, INSN). The one exception we make is if the insn
1498 before INSN sets TEMP2 to something which is also unchanged
1499 in that range. In that case, we can move the initialization
1500 into our sequence. */
1502 if ((temp5 = prev_active_insn (insn)) != 0
1503 && no_labels_between_p (temp5, insn)
1504 && GET_CODE (temp5) == INSN
1505 && (temp6 = single_set (temp5)) != 0
1506 && rtx_equal_p (temp2, SET_DEST (temp6))
1507 && (CONSTANT_P (SET_SRC (temp6))
1508 || GET_CODE (SET_SRC (temp6)) == REG
1509 || GET_CODE (SET_SRC (temp6)) == SUBREG))
1511 emit_insn (PATTERN (temp5));
1512 init_insn = temp5;
1513 init = SET_SRC (temp6);
1516 if (CONSTANT_P (init)
1517 || ! reg_set_between_p (init, PREV_INSN (temp4), insn))
1518 target = emit_store_flag (gen_reg_rtx (GET_MODE (temp2)), code,
1519 XEXP (temp3, 0), XEXP (temp3, 1),
1520 VOIDmode,
1521 (code == LTU || code == LEU
1522 || code == GTU || code == GEU), 1);
1524 /* If we can do the store-flag, do the addition or
1525 subtraction. */
1527 if (target)
1528 target = expand_binop (GET_MODE (temp2),
1529 (XEXP (SET_SRC (temp1), 1) == const1_rtx
1530 ? add_optab : sub_optab),
1531 temp2, target, temp2, 0, OPTAB_WIDEN);
1533 if (target != 0)
1535 /* Put the result back in temp2 in case it isn't already.
1536 Then replace the jump, possible a CC0-setting insn in
1537 front of the jump, and TEMP, with the sequence we have
1538 made. */
1540 if (target != temp2)
1541 emit_move_insn (temp2, target);
1543 seq = get_insns ();
1544 end_sequence ();
1546 emit_insns_before (seq, temp4);
1547 delete_insn (temp);
1549 if (init_insn)
1550 delete_insn (init_insn);
1552 next = NEXT_INSN (insn);
1553 #ifdef HAVE_cc0
1554 delete_insn (prev_nonnote_insn (insn));
1555 #endif
1556 delete_insn (insn);
1557 changed = 1;
1558 continue;
1560 else
1561 end_sequence ();
1564 /* Simplify if (...) x = 1; else {...} if (x) ...
1565 We recognize this case scanning backwards as well.
1567 TEMP is the assignment to x;
1568 TEMP1 is the label at the head of the second if. */
1569 /* ?? This should call get_condition to find the values being
1570 compared, instead of looking for a COMPARE insn when HAVE_cc0
1571 is not defined. This would allow it to work on the m88k. */
1572 /* ?? This optimization is only safe before cse is run if HAVE_cc0
1573 is not defined and the condition is tested by a separate compare
1574 insn. This is because the code below assumes that the result
1575 of the compare dies in the following branch.
1577 Not only that, but there might be other insns between the
1578 compare and branch whose results are live. Those insns need
1579 to be executed.
1581 A way to fix this is to move the insns at JUMP_LABEL (insn)
1582 to before INSN. If we are running before flow, they will
1583 be deleted if they aren't needed. But this doesn't work
1584 well after flow.
1586 This is really a special-case of jump threading, anyway. The
1587 right thing to do is to replace this and jump threading with
1588 much simpler code in cse.
1590 This code has been turned off in the non-cc0 case in the
1591 meantime. */
1593 #ifdef HAVE_cc0
1594 else if (this_is_simplejump
1595 /* Safe to skip USE and CLOBBER insns here
1596 since they will not be deleted. */
1597 && (temp = prev_active_insn (insn))
1598 && no_labels_between_p (temp, insn)
1599 && GET_CODE (temp) == INSN
1600 && GET_CODE (PATTERN (temp)) == SET
1601 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1602 && CONSTANT_P (SET_SRC (PATTERN (temp)))
1603 && (temp1 = next_active_insn (JUMP_LABEL (insn)))
1604 /* If we find that the next value tested is `x'
1605 (TEMP1 is the insn where this happens), win. */
1606 && GET_CODE (temp1) == INSN
1607 && GET_CODE (PATTERN (temp1)) == SET
1608 #ifdef HAVE_cc0
1609 /* Does temp1 `tst' the value of x? */
1610 && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
1611 && SET_DEST (PATTERN (temp1)) == cc0_rtx
1612 && (temp1 = next_nonnote_insn (temp1))
1613 #else
1614 /* Does temp1 compare the value of x against zero? */
1615 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1616 && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
1617 && (XEXP (SET_SRC (PATTERN (temp1)), 0)
1618 == SET_DEST (PATTERN (temp)))
1619 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1620 && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1621 #endif
1622 && condjump_p (temp1))
1624 /* Get the if_then_else from the condjump. */
1625 rtx choice = SET_SRC (PATTERN (temp1));
1626 if (GET_CODE (choice) == IF_THEN_ELSE)
1628 enum rtx_code code = GET_CODE (XEXP (choice, 0));
1629 rtx val = SET_SRC (PATTERN (temp));
1630 rtx cond
1631 = simplify_relational_operation (code, GET_MODE (SET_DEST (PATTERN (temp))),
1632 val, const0_rtx);
1633 rtx ultimate;
1635 if (cond == const_true_rtx)
1636 ultimate = XEXP (choice, 1);
1637 else if (cond == const0_rtx)
1638 ultimate = XEXP (choice, 2);
1639 else
1640 ultimate = 0;
1642 if (ultimate == pc_rtx)
1643 ultimate = get_label_after (temp1);
1644 else if (ultimate && GET_CODE (ultimate) != RETURN)
1645 ultimate = XEXP (ultimate, 0);
1647 if (ultimate && JUMP_LABEL(insn) != ultimate)
1648 changed |= redirect_jump (insn, ultimate);
1651 #endif
1653 #if 0
1654 /* @@ This needs a bit of work before it will be right.
1656 Any type of comparison can be accepted for the first and
1657 second compare. When rewriting the first jump, we must
1658 compute the what conditions can reach label3, and use the
1659 appropriate code. We can not simply reverse/swap the code
1660 of the first jump. In some cases, the second jump must be
1661 rewritten also.
1663 For example,
1664 < == converts to > ==
1665 < != converts to == >
1666 etc.
1668 If the code is written to only accept an '==' test for the second
1669 compare, then all that needs to be done is to swap the condition
1670 of the first branch.
1672 It is questionable whether we want this optimization anyways,
1673 since if the user wrote code like this because he/she knew that
1674 the jump to label1 is taken most of the time, then rewriting
1675 this gives slower code. */
1676 /* @@ This should call get_condition to find the values being
1677 compared, instead of looking for a COMPARE insn when HAVE_cc0
1678 is not defined. This would allow it to work on the m88k. */
1679 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1680 is not defined and the condition is tested by a separate compare
1681 insn. This is because the code below assumes that the result
1682 of the compare dies in the following branch. */
1684 /* Simplify test a ~= b
1685 condjump label1;
1686 test a == b
1687 condjump label2;
1688 jump label3;
1689 label1:
1691 rewriting as
1692 test a ~~= b
1693 condjump label3
1694 test a == b
1695 condjump label2
1696 label1:
1698 where ~= is an inequality, e.g. >, and ~~= is the swapped
1699 inequality, e.g. <.
1701 We recognize this case scanning backwards.
1703 TEMP is the conditional jump to `label2';
1704 TEMP1 is the test for `a == b';
1705 TEMP2 is the conditional jump to `label1';
1706 TEMP3 is the test for `a ~= b'. */
1707 else if (this_is_simplejump
1708 && (temp = prev_active_insn (insn))
1709 && no_labels_between_p (temp, insn)
1710 && condjump_p (temp)
1711 && (temp1 = prev_active_insn (temp))
1712 && no_labels_between_p (temp1, temp)
1713 && GET_CODE (temp1) == INSN
1714 && GET_CODE (PATTERN (temp1)) == SET
1715 #ifdef HAVE_cc0
1716 && sets_cc0_p (PATTERN (temp1)) == 1
1717 #else
1718 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1719 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1720 && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1721 #endif
1722 && (temp2 = prev_active_insn (temp1))
1723 && no_labels_between_p (temp2, temp1)
1724 && condjump_p (temp2)
1725 && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
1726 && (temp3 = prev_active_insn (temp2))
1727 && no_labels_between_p (temp3, temp2)
1728 && GET_CODE (PATTERN (temp3)) == SET
1729 && rtx_equal_p (SET_DEST (PATTERN (temp3)),
1730 SET_DEST (PATTERN (temp1)))
1731 && rtx_equal_p (SET_SRC (PATTERN (temp1)),
1732 SET_SRC (PATTERN (temp3)))
1733 && ! inequality_comparisons_p (PATTERN (temp))
1734 && inequality_comparisons_p (PATTERN (temp2)))
1736 rtx fallthrough_label = JUMP_LABEL (temp2);
1738 ++LABEL_NUSES (fallthrough_label);
1739 if (swap_jump (temp2, JUMP_LABEL (insn)))
1741 delete_insn (insn);
1742 changed = 1;
1745 if (--LABEL_NUSES (fallthrough_label) == 0)
1746 delete_insn (fallthrough_label);
1748 #endif
1749 /* Simplify if (...) {... x = 1;} if (x) ...
1751 We recognize this case backwards.
1753 TEMP is the test of `x';
1754 TEMP1 is the assignment to `x' at the end of the
1755 previous statement. */
1756 /* @@ This should call get_condition to find the values being
1757 compared, instead of looking for a COMPARE insn when HAVE_cc0
1758 is not defined. This would allow it to work on the m88k. */
1759 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1760 is not defined and the condition is tested by a separate compare
1761 insn. This is because the code below assumes that the result
1762 of the compare dies in the following branch. */
1764 /* ??? This has to be turned off. The problem is that the
1765 unconditional jump might indirectly end up branching to the
1766 label between TEMP1 and TEMP. We can't detect this, in general,
1767 since it may become a jump to there after further optimizations.
1768 If that jump is done, it will be deleted, so we will retry
1769 this optimization in the next pass, thus an infinite loop.
1771 The present code prevents this by putting the jump after the
1772 label, but this is not logically correct. */
1773 #if 0
1774 else if (this_is_condjump
1775 /* Safe to skip USE and CLOBBER insns here
1776 since they will not be deleted. */
1777 && (temp = prev_active_insn (insn))
1778 && no_labels_between_p (temp, insn)
1779 && GET_CODE (temp) == INSN
1780 && GET_CODE (PATTERN (temp)) == SET
1781 #ifdef HAVE_cc0
1782 && sets_cc0_p (PATTERN (temp)) == 1
1783 && GET_CODE (SET_SRC (PATTERN (temp))) == REG
1784 #else
1785 /* Temp must be a compare insn, we can not accept a register
1786 to register move here, since it may not be simply a
1787 tst insn. */
1788 && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
1789 && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
1790 && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
1791 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1792 && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
1793 #endif
1794 /* May skip USE or CLOBBER insns here
1795 for checking for opportunity, since we
1796 take care of them later. */
1797 && (temp1 = prev_active_insn (temp))
1798 && GET_CODE (temp1) == INSN
1799 && GET_CODE (PATTERN (temp1)) == SET
1800 #ifdef HAVE_cc0
1801 && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
1802 #else
1803 && (XEXP (SET_SRC (PATTERN (temp)), 0)
1804 == SET_DEST (PATTERN (temp1)))
1805 #endif
1806 && CONSTANT_P (SET_SRC (PATTERN (temp1)))
1807 /* If this isn't true, cse will do the job. */
1808 && ! no_labels_between_p (temp1, temp))
1810 /* Get the if_then_else from the condjump. */
1811 rtx choice = SET_SRC (PATTERN (insn));
1812 if (GET_CODE (choice) == IF_THEN_ELSE
1813 && (GET_CODE (XEXP (choice, 0)) == EQ
1814 || GET_CODE (XEXP (choice, 0)) == NE))
1816 int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
1817 rtx last_insn;
1818 rtx ultimate;
1819 rtx p;
1821 /* Get the place that condjump will jump to
1822 if it is reached from here. */
1823 if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
1824 == want_nonzero)
1825 ultimate = XEXP (choice, 1);
1826 else
1827 ultimate = XEXP (choice, 2);
1828 /* Get it as a CODE_LABEL. */
1829 if (ultimate == pc_rtx)
1830 ultimate = get_label_after (insn);
1831 else
1832 /* Get the label out of the LABEL_REF. */
1833 ultimate = XEXP (ultimate, 0);
1835 /* Insert the jump immediately before TEMP, specifically
1836 after the label that is between TEMP1 and TEMP. */
1837 last_insn = PREV_INSN (temp);
1839 /* If we would be branching to the next insn, the jump
1840 would immediately be deleted and the re-inserted in
1841 a subsequent pass over the code. So don't do anything
1842 in that case. */
1843 if (next_active_insn (last_insn)
1844 != next_active_insn (ultimate))
1846 emit_barrier_after (last_insn);
1847 p = emit_jump_insn_after (gen_jump (ultimate),
1848 last_insn);
1849 JUMP_LABEL (p) = ultimate;
1850 ++LABEL_NUSES (ultimate);
1851 if (INSN_UID (ultimate) < max_jump_chain
1852 && INSN_CODE (p) < max_jump_chain)
1854 jump_chain[INSN_UID (p)]
1855 = jump_chain[INSN_UID (ultimate)];
1856 jump_chain[INSN_UID (ultimate)] = p;
1858 changed = 1;
1859 continue;
1863 #endif
1864 /* Detect a conditional jump going to the same place
1865 as an immediately following unconditional jump. */
1866 else if (this_is_condjump
1867 && (temp = next_active_insn (insn)) != 0
1868 && simplejump_p (temp)
1869 && (next_active_insn (JUMP_LABEL (insn))
1870 == next_active_insn (JUMP_LABEL (temp))))
1872 delete_jump (insn);
1873 changed = 1;
1874 continue;
1876 /* Detect a conditional jump jumping over an unconditional jump. */
1878 else if ((this_is_condjump || this_is_condjump_in_parallel)
1879 && ! this_is_simplejump
1880 && reallabelprev != 0
1881 && GET_CODE (reallabelprev) == JUMP_INSN
1882 && prev_active_insn (reallabelprev) == insn
1883 && no_labels_between_p (insn, reallabelprev)
1884 && simplejump_p (reallabelprev))
1886 /* When we invert the unconditional jump, we will be
1887 decrementing the usage count of its old label.
1888 Make sure that we don't delete it now because that
1889 might cause the following code to be deleted. */
1890 rtx prev_uses = prev_nonnote_insn (reallabelprev);
1891 rtx prev_label = JUMP_LABEL (insn);
1893 if (prev_label)
1894 ++LABEL_NUSES (prev_label);
1896 if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
1898 /* It is very likely that if there are USE insns before
1899 this jump, they hold REG_DEAD notes. These REG_DEAD
1900 notes are no longer valid due to this optimization,
1901 and will cause the life-analysis that following passes
1902 (notably delayed-branch scheduling) to think that
1903 these registers are dead when they are not.
1905 To prevent this trouble, we just remove the USE insns
1906 from the insn chain. */
1908 while (prev_uses && GET_CODE (prev_uses) == INSN
1909 && GET_CODE (PATTERN (prev_uses)) == USE)
1911 rtx useless = prev_uses;
1912 prev_uses = prev_nonnote_insn (prev_uses);
1913 delete_insn (useless);
1916 delete_insn (reallabelprev);
1917 next = insn;
1918 changed = 1;
1921 /* We can now safely delete the label if it is unreferenced
1922 since the delete_insn above has deleted the BARRIER. */
1923 if (prev_label && --LABEL_NUSES (prev_label) == 0)
1924 delete_insn (prev_label);
1925 continue;
1927 else
1929 /* Detect a jump to a jump. */
1931 nlabel = follow_jumps (JUMP_LABEL (insn));
1932 if (nlabel != JUMP_LABEL (insn)
1933 && redirect_jump (insn, nlabel))
1935 changed = 1;
1936 next = insn;
1939 /* Look for if (foo) bar; else break; */
1940 /* The insns look like this:
1941 insn = condjump label1;
1942 ...range1 (some insns)...
1943 jump label2;
1944 label1:
1945 ...range2 (some insns)...
1946 jump somewhere unconditionally
1947 label2: */
1949 rtx label1 = next_label (insn);
1950 rtx range1end = label1 ? prev_active_insn (label1) : 0;
1951 /* Don't do this optimization on the first round, so that
1952 jump-around-a-jump gets simplified before we ask here
1953 whether a jump is unconditional.
1955 Also don't do it when we are called after reload since
1956 it will confuse reorg. */
1957 if (! first
1958 && (reload_completed ? ! flag_delayed_branch : 1)
1959 /* Make sure INSN is something we can invert. */
1960 && condjump_p (insn)
1961 && label1 != 0
1962 && JUMP_LABEL (insn) == label1
1963 && LABEL_NUSES (label1) == 1
1964 && GET_CODE (range1end) == JUMP_INSN
1965 && simplejump_p (range1end))
1967 rtx label2 = next_label (label1);
1968 rtx range2end = label2 ? prev_active_insn (label2) : 0;
1969 if (range1end != range2end
1970 && JUMP_LABEL (range1end) == label2
1971 && GET_CODE (range2end) == JUMP_INSN
1972 && GET_CODE (NEXT_INSN (range2end)) == BARRIER
1973 /* Invert the jump condition, so we
1974 still execute the same insns in each case. */
1975 && invert_jump (insn, label1))
1977 rtx range1beg = next_active_insn (insn);
1978 rtx range2beg = next_active_insn (label1);
1979 rtx range1after, range2after;
1980 rtx range1before, range2before;
1981 rtx rangenext;
1983 /* Include in each range any notes before it, to be
1984 sure that we get the line number note if any, even
1985 if there are other notes here. */
1986 while (PREV_INSN (range1beg)
1987 && GET_CODE (PREV_INSN (range1beg)) == NOTE)
1988 range1beg = PREV_INSN (range1beg);
1990 while (PREV_INSN (range2beg)
1991 && GET_CODE (PREV_INSN (range2beg)) == NOTE)
1992 range2beg = PREV_INSN (range2beg);
1994 /* Don't move NOTEs for blocks or loops; shift them
1995 outside the ranges, where they'll stay put. */
1996 range1beg = squeeze_notes (range1beg, range1end);
1997 range2beg = squeeze_notes (range2beg, range2end);
1999 /* Get current surrounds of the 2 ranges. */
2000 range1before = PREV_INSN (range1beg);
2001 range2before = PREV_INSN (range2beg);
2002 range1after = NEXT_INSN (range1end);
2003 range2after = NEXT_INSN (range2end);
2005 /* Splice range2 where range1 was. */
2006 NEXT_INSN (range1before) = range2beg;
2007 PREV_INSN (range2beg) = range1before;
2008 NEXT_INSN (range2end) = range1after;
2009 PREV_INSN (range1after) = range2end;
2010 /* Splice range1 where range2 was. */
2011 NEXT_INSN (range2before) = range1beg;
2012 PREV_INSN (range1beg) = range2before;
2013 NEXT_INSN (range1end) = range2after;
2014 PREV_INSN (range2after) = range1end;
2016 /* Check for a loop end note between the end of
2017 range2, and the next code label. If there is one,
2018 then what we have really seen is
2019 if (foo) break; end_of_loop;
2020 and moved the break sequence outside the loop.
2021 We must move the LOOP_END note to where the
2022 loop really ends now, or we will confuse loop
2023 optimization. Stop if we find a LOOP_BEG note
2024 first, since we don't want to move the LOOP_END
2025 note in that case. */
2026 for (;range2after != label2; range2after = rangenext)
2028 rangenext = NEXT_INSN (range2after);
2029 if (GET_CODE (range2after) == NOTE)
2031 if (NOTE_LINE_NUMBER (range2after)
2032 == NOTE_INSN_LOOP_END)
2034 NEXT_INSN (PREV_INSN (range2after))
2035 = rangenext;
2036 PREV_INSN (rangenext)
2037 = PREV_INSN (range2after);
2038 PREV_INSN (range2after)
2039 = PREV_INSN (range1beg);
2040 NEXT_INSN (range2after) = range1beg;
2041 NEXT_INSN (PREV_INSN (range1beg))
2042 = range2after;
2043 PREV_INSN (range1beg) = range2after;
2045 else if (NOTE_LINE_NUMBER (range2after)
2046 == NOTE_INSN_LOOP_BEG)
2047 break;
2050 changed = 1;
2051 continue;
2056 /* Now that the jump has been tensioned,
2057 try cross jumping: check for identical code
2058 before the jump and before its target label. */
2060 /* First, cross jumping of conditional jumps: */
2062 if (cross_jump && condjump_p (insn))
2064 rtx newjpos, newlpos;
2065 rtx x = prev_real_insn (JUMP_LABEL (insn));
2067 /* A conditional jump may be crossjumped
2068 only if the place it jumps to follows
2069 an opposing jump that comes back here. */
2071 if (x != 0 && ! jump_back_p (x, insn))
2072 /* We have no opposing jump;
2073 cannot cross jump this insn. */
2074 x = 0;
2076 newjpos = 0;
2077 /* TARGET is nonzero if it is ok to cross jump
2078 to code before TARGET. If so, see if matches. */
2079 if (x != 0)
2080 find_cross_jump (insn, x, 2,
2081 &newjpos, &newlpos);
2083 if (newjpos != 0)
2085 do_cross_jump (insn, newjpos, newlpos);
2086 /* Make the old conditional jump
2087 into an unconditional one. */
2088 SET_SRC (PATTERN (insn))
2089 = gen_rtx (LABEL_REF, VOIDmode, JUMP_LABEL (insn));
2090 INSN_CODE (insn) = -1;
2091 emit_barrier_after (insn);
2092 /* Add to jump_chain unless this is a new label
2093 whose UID is too large. */
2094 if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
2096 jump_chain[INSN_UID (insn)]
2097 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
2098 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
2100 changed = 1;
2101 next = insn;
2105 /* Cross jumping of unconditional jumps:
2106 a few differences. */
2108 if (cross_jump && simplejump_p (insn))
2110 rtx newjpos, newlpos;
2111 rtx target;
2113 newjpos = 0;
2115 /* TARGET is nonzero if it is ok to cross jump
2116 to code before TARGET. If so, see if matches. */
2117 find_cross_jump (insn, JUMP_LABEL (insn), 1,
2118 &newjpos, &newlpos);
2120 /* If cannot cross jump to code before the label,
2121 see if we can cross jump to another jump to
2122 the same label. */
2123 /* Try each other jump to this label. */
2124 if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
2125 for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
2126 target != 0 && newjpos == 0;
2127 target = jump_chain[INSN_UID (target)])
2128 if (target != insn
2129 && JUMP_LABEL (target) == JUMP_LABEL (insn)
2130 /* Ignore TARGET if it's deleted. */
2131 && ! INSN_DELETED_P (target))
2132 find_cross_jump (insn, target, 2,
2133 &newjpos, &newlpos);
2135 if (newjpos != 0)
2137 do_cross_jump (insn, newjpos, newlpos);
2138 changed = 1;
2139 next = insn;
2143 /* This code was dead in the previous jump.c! */
2144 if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
2146 /* Return insns all "jump to the same place"
2147 so we can cross-jump between any two of them. */
2149 rtx newjpos, newlpos, target;
2151 newjpos = 0;
2153 /* If cannot cross jump to code before the label,
2154 see if we can cross jump to another jump to
2155 the same label. */
2156 /* Try each other jump to this label. */
2157 for (target = jump_chain[0];
2158 target != 0 && newjpos == 0;
2159 target = jump_chain[INSN_UID (target)])
2160 if (target != insn
2161 && ! INSN_DELETED_P (target)
2162 && GET_CODE (PATTERN (target)) == RETURN)
2163 find_cross_jump (insn, target, 2,
2164 &newjpos, &newlpos);
2166 if (newjpos != 0)
2168 do_cross_jump (insn, newjpos, newlpos);
2169 changed = 1;
2170 next = insn;
2176 first = 0;
2179 /* Delete extraneous line number notes.
2180 Note that two consecutive notes for different lines are not really
2181 extraneous. There should be some indication where that line belonged,
2182 even if it became empty. */
2185 rtx last_note = 0;
2187 for (insn = f; insn; insn = NEXT_INSN (insn))
2188 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
2190 /* Delete this note if it is identical to previous note. */
2191 if (last_note
2192 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
2193 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
2195 delete_insn (insn);
2196 continue;
2199 last_note = insn;
2203 #ifdef HAVE_return
2204 if (HAVE_return)
2206 /* If we fall through to the epilogue, see if we can insert a RETURN insn
2207 in front of it. If the machine allows it at this point (we might be
2208 after reload for a leaf routine), it will improve optimization for it
2209 to be there. We do this both here and at the start of this pass since
2210 the RETURN might have been deleted by some of our optimizations. */
2211 insn = get_last_insn ();
2212 while (insn && GET_CODE (insn) == NOTE)
2213 insn = PREV_INSN (insn);
2215 if (insn && GET_CODE (insn) != BARRIER)
2217 emit_jump_insn (gen_return ());
2218 emit_barrier ();
2221 #endif
2223 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
2224 If so, delete it, and record that this function can drop off the end. */
2226 insn = last_insn;
2228 int n_labels = 1;
2229 while (insn
2230 /* One label can follow the end-note: the return label. */
2231 && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
2232 /* Ordinary insns can follow it if returning a structure. */
2233 || GET_CODE (insn) == INSN
2234 /* If machine uses explicit RETURN insns, no epilogue,
2235 then one of them follows the note. */
2236 || (GET_CODE (insn) == JUMP_INSN
2237 && GET_CODE (PATTERN (insn)) == RETURN)
2238 /* A barrier can follow the return insn. */
2239 || GET_CODE (insn) == BARRIER
2240 /* Other kinds of notes can follow also. */
2241 || (GET_CODE (insn) == NOTE
2242 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
2243 insn = PREV_INSN (insn);
2246 /* Report if control can fall through at the end of the function. */
2247 if (insn && GET_CODE (insn) == NOTE
2248 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
2250 can_reach_end = 1;
2251 delete_insn (insn);
2254 /* Show JUMP_CHAIN no longer valid. */
2255 jump_chain = 0;
2258 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
2259 jump. Assume that this unconditional jump is to the exit test code. If
2260 the code is sufficiently simple, make a copy of it before INSN,
2261 followed by a jump to the exit of the loop. Then delete the unconditional
2262 jump after INSN.
2264 Return 1 if we made the change, else 0.
2266 This is only safe immediately after a regscan pass because it uses the
2267 values of regno_first_uid and regno_last_uid. */
2269 static int
2270 duplicate_loop_exit_test (loop_start)
2271 rtx loop_start;
2273 rtx insn, set, reg, p, link;
2274 rtx copy = 0;
2275 int num_insns = 0;
2276 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
2277 rtx lastexit;
2278 int max_reg = max_reg_num ();
2279 rtx *reg_map = 0;
2281 /* Scan the exit code. We do not perform this optimization if any insn:
2283 is a CALL_INSN
2284 is a CODE_LABEL
2285 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
2286 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
2287 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
2288 are not valid
2290 Also, don't do this if the exit code is more than 20 insns. */
2292 for (insn = exitcode;
2293 insn
2294 && ! (GET_CODE (insn) == NOTE
2295 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
2296 insn = NEXT_INSN (insn))
2298 switch (GET_CODE (insn))
2300 case CODE_LABEL:
2301 case CALL_INSN:
2302 return 0;
2303 case NOTE:
2304 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
2305 a jump immediately after the loop start that branches outside
2306 the loop but within an outer loop, near the exit test.
2307 If we copied this exit test and created a phony
2308 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
2309 before the exit test look like these could be safely moved
2310 out of the loop even if they actually may be never executed.
2311 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
2313 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2314 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2315 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2316 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2317 return 0;
2318 break;
2319 case JUMP_INSN:
2320 case INSN:
2321 if (++num_insns > 20
2322 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
2323 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2324 return 0;
2325 break;
2329 /* Unless INSN is zero, we can do the optimization. */
2330 if (insn == 0)
2331 return 0;
2333 lastexit = insn;
2335 /* See if any insn sets a register only used in the loop exit code and
2336 not a user variable. If so, replace it with a new register. */
2337 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2338 if (GET_CODE (insn) == INSN
2339 && (set = single_set (insn)) != 0
2340 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
2341 || (GET_CODE (reg) == SUBREG
2342 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
2343 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
2344 && regno_first_uid[REGNO (reg)] == INSN_UID (insn))
2346 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
2347 if (regno_last_uid[REGNO (reg)] == INSN_UID (p))
2348 break;
2350 if (p != lastexit)
2352 /* We can do the replacement. Allocate reg_map if this is the
2353 first replacement we found. */
2354 if (reg_map == 0)
2356 reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
2357 bzero ((char *) reg_map, max_reg * sizeof (rtx));
2360 REG_LOOP_TEST_P (reg) = 1;
2362 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
2366 /* Now copy each insn. */
2367 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2368 switch (GET_CODE (insn))
2370 case BARRIER:
2371 copy = emit_barrier_before (loop_start);
2372 break;
2373 case NOTE:
2374 /* Only copy line-number notes. */
2375 if (NOTE_LINE_NUMBER (insn) >= 0)
2377 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
2378 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
2380 break;
2382 case INSN:
2383 copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2384 if (reg_map)
2385 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2387 mark_jump_label (PATTERN (copy), copy, 0);
2389 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
2390 make them. */
2391 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2392 if (REG_NOTE_KIND (link) != REG_LABEL)
2393 REG_NOTES (copy)
2394 = copy_rtx (gen_rtx (EXPR_LIST, REG_NOTE_KIND (link),
2395 XEXP (link, 0), REG_NOTES (copy)));
2396 if (reg_map && REG_NOTES (copy))
2397 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2398 break;
2400 case JUMP_INSN:
2401 copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2402 if (reg_map)
2403 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2404 mark_jump_label (PATTERN (copy), copy, 0);
2405 if (REG_NOTES (insn))
2407 REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
2408 if (reg_map)
2409 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2412 /* If this is a simple jump, add it to the jump chain. */
2414 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
2415 && simplejump_p (copy))
2417 jump_chain[INSN_UID (copy)]
2418 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2419 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2421 break;
2423 default:
2424 abort ();
2427 /* Now clean up by emitting a jump to the end label and deleting the jump
2428 at the start of the loop. */
2429 if (! copy || GET_CODE (copy) != BARRIER)
2431 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
2432 loop_start);
2433 mark_jump_label (PATTERN (copy), copy, 0);
2434 if (INSN_UID (copy) < max_jump_chain
2435 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
2437 jump_chain[INSN_UID (copy)]
2438 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2439 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2441 emit_barrier_before (loop_start);
2444 /* Mark the exit code as the virtual top of the converted loop. */
2445 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
2447 delete_insn (next_nonnote_insn (loop_start));
2449 return 1;
2452 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
2453 loop-end notes between START and END out before START. Assume that
2454 END is not such a note. START may be such a note. Returns the value
2455 of the new starting insn, which may be different if the original start
2456 was such a note. */
2459 squeeze_notes (start, end)
2460 rtx start, end;
2462 rtx insn;
2463 rtx next;
2465 for (insn = start; insn != end; insn = next)
2467 next = NEXT_INSN (insn);
2468 if (GET_CODE (insn) == NOTE
2469 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2470 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2471 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2472 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
2473 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
2474 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
2476 if (insn == start)
2477 start = next;
2478 else
2480 rtx prev = PREV_INSN (insn);
2481 PREV_INSN (insn) = PREV_INSN (start);
2482 NEXT_INSN (insn) = start;
2483 NEXT_INSN (PREV_INSN (insn)) = insn;
2484 PREV_INSN (NEXT_INSN (insn)) = insn;
2485 NEXT_INSN (prev) = next;
2486 PREV_INSN (next) = prev;
2491 return start;
2494 /* Compare the instructions before insn E1 with those before E2
2495 to find an opportunity for cross jumping.
2496 (This means detecting identical sequences of insns followed by
2497 jumps to the same place, or followed by a label and a jump
2498 to that label, and replacing one with a jump to the other.)
2500 Assume E1 is a jump that jumps to label E2
2501 (that is not always true but it might as well be).
2502 Find the longest possible equivalent sequences
2503 and store the first insns of those sequences into *F1 and *F2.
2504 Store zero there if no equivalent preceding instructions are found.
2506 We give up if we find a label in stream 1.
2507 Actually we could transfer that label into stream 2. */
2509 static void
2510 find_cross_jump (e1, e2, minimum, f1, f2)
2511 rtx e1, e2;
2512 int minimum;
2513 rtx *f1, *f2;
2515 register rtx i1 = e1, i2 = e2;
2516 register rtx p1, p2;
2517 int lose = 0;
2519 rtx last1 = 0, last2 = 0;
2520 rtx afterlast1 = 0, afterlast2 = 0;
2521 rtx prev1;
2523 *f1 = 0;
2524 *f2 = 0;
2526 while (1)
2528 i1 = prev_nonnote_insn (i1);
2530 i2 = PREV_INSN (i2);
2531 while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
2532 i2 = PREV_INSN (i2);
2534 if (i1 == 0)
2535 break;
2537 /* Don't allow the range of insns preceding E1 or E2
2538 to include the other (E2 or E1). */
2539 if (i2 == e1 || i1 == e2)
2540 break;
2542 /* If we will get to this code by jumping, those jumps will be
2543 tensioned to go directly to the new label (before I2),
2544 so this cross-jumping won't cost extra. So reduce the minimum. */
2545 if (GET_CODE (i1) == CODE_LABEL)
2547 --minimum;
2548 break;
2551 if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
2552 break;
2554 p1 = PATTERN (i1);
2555 p2 = PATTERN (i2);
2557 /* If this is a CALL_INSN, compare register usage information.
2558 If we don't check this on stack register machines, the two
2559 CALL_INSNs might be merged leaving reg-stack.c with mismatching
2560 numbers of stack registers in the same basic block.
2561 If we don't check this on machines with delay slots, a delay slot may
2562 be filled that clobbers a parameter expected by the subroutine.
2564 ??? We take the simple route for now and assume that if they're
2565 equal, they were constructed identically. */
2567 if (GET_CODE (i1) == CALL_INSN
2568 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
2569 CALL_INSN_FUNCTION_USAGE (i2)))
2570 lose = 1;
2572 #ifdef STACK_REGS
2573 /* If cross_jump_death_matters is not 0, the insn's mode
2574 indicates whether or not the insn contains any stack-like
2575 regs. */
2577 if (!lose && cross_jump_death_matters && GET_MODE (i1) == QImode)
2579 /* If register stack conversion has already been done, then
2580 death notes must also be compared before it is certain that
2581 the two instruction streams match. */
2583 rtx note;
2584 HARD_REG_SET i1_regset, i2_regset;
2586 CLEAR_HARD_REG_SET (i1_regset);
2587 CLEAR_HARD_REG_SET (i2_regset);
2589 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
2590 if (REG_NOTE_KIND (note) == REG_DEAD
2591 && STACK_REG_P (XEXP (note, 0)))
2592 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
2594 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
2595 if (REG_NOTE_KIND (note) == REG_DEAD
2596 && STACK_REG_P (XEXP (note, 0)))
2597 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
2599 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
2601 lose = 1;
2603 done:
2606 #endif
2608 /* Don't allow old-style asm or volatile extended asms to be accepted
2609 for cross jumping purposes. It is conceptually correct to allow
2610 them, since cross-jumping preserves the dynamic instruction order
2611 even though it is changing the static instruction order. However,
2612 if an asm is being used to emit an assembler pseudo-op, such as
2613 the MIPS `.set reorder' pseudo-op, then the static instruction order
2614 matters and it must be preserved. */
2615 if (GET_CODE (p1) == ASM_INPUT || GET_CODE (p2) == ASM_INPUT
2616 || (GET_CODE (p1) == ASM_OPERANDS && MEM_VOLATILE_P (p1))
2617 || (GET_CODE (p2) == ASM_OPERANDS && MEM_VOLATILE_P (p2)))
2618 lose = 1;
2620 if (lose || GET_CODE (p1) != GET_CODE (p2)
2621 || ! rtx_renumbered_equal_p (p1, p2))
2623 /* The following code helps take care of G++ cleanups. */
2624 rtx equiv1;
2625 rtx equiv2;
2627 if (!lose && GET_CODE (p1) == GET_CODE (p2)
2628 && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
2629 || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
2630 && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
2631 || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
2632 /* If the equivalences are not to a constant, they may
2633 reference pseudos that no longer exist, so we can't
2634 use them. */
2635 && CONSTANT_P (XEXP (equiv1, 0))
2636 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
2638 rtx s1 = single_set (i1);
2639 rtx s2 = single_set (i2);
2640 if (s1 != 0 && s2 != 0
2641 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
2643 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
2644 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
2645 if (! rtx_renumbered_equal_p (p1, p2))
2646 cancel_changes (0);
2647 else if (apply_change_group ())
2648 goto win;
2652 /* Insns fail to match; cross jumping is limited to the following
2653 insns. */
2655 #ifdef HAVE_cc0
2656 /* Don't allow the insn after a compare to be shared by
2657 cross-jumping unless the compare is also shared.
2658 Here, if either of these non-matching insns is a compare,
2659 exclude the following insn from possible cross-jumping. */
2660 if (sets_cc0_p (p1) || sets_cc0_p (p2))
2661 last1 = afterlast1, last2 = afterlast2, ++minimum;
2662 #endif
2664 /* If cross-jumping here will feed a jump-around-jump
2665 optimization, this jump won't cost extra, so reduce
2666 the minimum. */
2667 if (GET_CODE (i1) == JUMP_INSN
2668 && JUMP_LABEL (i1)
2669 && prev_real_insn (JUMP_LABEL (i1)) == e1)
2670 --minimum;
2671 break;
2674 win:
2675 if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
2677 /* Ok, this insn is potentially includable in a cross-jump here. */
2678 afterlast1 = last1, afterlast2 = last2;
2679 last1 = i1, last2 = i2, --minimum;
2683 if (minimum <= 0 && last1 != 0 && last1 != e1)
2684 *f1 = last1, *f2 = last2;
2687 static void
2688 do_cross_jump (insn, newjpos, newlpos)
2689 rtx insn, newjpos, newlpos;
2691 /* Find an existing label at this point
2692 or make a new one if there is none. */
2693 register rtx label = get_label_before (newlpos);
2695 /* Make the same jump insn jump to the new point. */
2696 if (GET_CODE (PATTERN (insn)) == RETURN)
2698 /* Remove from jump chain of returns. */
2699 delete_from_jump_chain (insn);
2700 /* Change the insn. */
2701 PATTERN (insn) = gen_jump (label);
2702 INSN_CODE (insn) = -1;
2703 JUMP_LABEL (insn) = label;
2704 LABEL_NUSES (label)++;
2705 /* Add to new the jump chain. */
2706 if (INSN_UID (label) < max_jump_chain
2707 && INSN_UID (insn) < max_jump_chain)
2709 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
2710 jump_chain[INSN_UID (label)] = insn;
2713 else
2714 redirect_jump (insn, label);
2716 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
2717 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
2718 the NEWJPOS stream. */
2720 while (newjpos != insn)
2722 rtx lnote;
2724 for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
2725 if ((REG_NOTE_KIND (lnote) == REG_EQUAL
2726 || REG_NOTE_KIND (lnote) == REG_EQUIV)
2727 && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
2728 && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
2729 remove_note (newlpos, lnote);
2731 delete_insn (newjpos);
2732 newjpos = next_real_insn (newjpos);
2733 newlpos = next_real_insn (newlpos);
2737 /* Return the label before INSN, or put a new label there. */
2740 get_label_before (insn)
2741 rtx insn;
2743 rtx label;
2745 /* Find an existing label at this point
2746 or make a new one if there is none. */
2747 label = prev_nonnote_insn (insn);
2749 if (label == 0 || GET_CODE (label) != CODE_LABEL)
2751 rtx prev = PREV_INSN (insn);
2753 label = gen_label_rtx ();
2754 emit_label_after (label, prev);
2755 LABEL_NUSES (label) = 0;
2757 return label;
2760 /* Return the label after INSN, or put a new label there. */
2763 get_label_after (insn)
2764 rtx insn;
2766 rtx label;
2768 /* Find an existing label at this point
2769 or make a new one if there is none. */
2770 label = next_nonnote_insn (insn);
2772 if (label == 0 || GET_CODE (label) != CODE_LABEL)
2774 label = gen_label_rtx ();
2775 emit_label_after (label, insn);
2776 LABEL_NUSES (label) = 0;
2778 return label;
2781 /* Return 1 if INSN is a jump that jumps to right after TARGET
2782 only on the condition that TARGET itself would drop through.
2783 Assumes that TARGET is a conditional jump. */
2785 static int
2786 jump_back_p (insn, target)
2787 rtx insn, target;
2789 rtx cinsn, ctarget;
2790 enum rtx_code codei, codet;
2792 if (simplejump_p (insn) || ! condjump_p (insn)
2793 || simplejump_p (target)
2794 || target != prev_real_insn (JUMP_LABEL (insn)))
2795 return 0;
2797 cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
2798 ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
2800 codei = GET_CODE (cinsn);
2801 codet = GET_CODE (ctarget);
2803 if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
2805 if (! can_reverse_comparison_p (cinsn, insn))
2806 return 0;
2807 codei = reverse_condition (codei);
2810 if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
2812 if (! can_reverse_comparison_p (ctarget, target))
2813 return 0;
2814 codet = reverse_condition (codet);
2817 return (codei == codet
2818 && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
2819 && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
2822 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
2823 return non-zero if it is safe to reverse this comparison. It is if our
2824 floating-point is not IEEE, if this is an NE or EQ comparison, or if
2825 this is known to be an integer comparison. */
2828 can_reverse_comparison_p (comparison, insn)
2829 rtx comparison;
2830 rtx insn;
2832 rtx arg0;
2834 /* If this is not actually a comparison, we can't reverse it. */
2835 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
2836 return 0;
2838 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
2839 /* If this is an NE comparison, it is safe to reverse it to an EQ
2840 comparison and vice versa, even for floating point. If no operands
2841 are NaNs, the reversal is valid. If some operand is a NaN, EQ is
2842 always false and NE is always true, so the reversal is also valid. */
2843 || flag_fast_math
2844 || GET_CODE (comparison) == NE
2845 || GET_CODE (comparison) == EQ)
2846 return 1;
2848 arg0 = XEXP (comparison, 0);
2850 /* Make sure ARG0 is one of the actual objects being compared. If we
2851 can't do this, we can't be sure the comparison can be reversed.
2853 Handle cc0 and a MODE_CC register. */
2854 if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
2855 #ifdef HAVE_cc0
2856 || arg0 == cc0_rtx
2857 #endif
2860 rtx prev = prev_nonnote_insn (insn);
2861 rtx set = single_set (prev);
2863 if (set == 0 || SET_DEST (set) != arg0)
2864 return 0;
2866 arg0 = SET_SRC (set);
2868 if (GET_CODE (arg0) == COMPARE)
2869 arg0 = XEXP (arg0, 0);
2872 /* We can reverse this if ARG0 is a CONST_INT or if its mode is
2873 not VOIDmode and neither a MODE_CC nor MODE_FLOAT type. */
2874 return (GET_CODE (arg0) == CONST_INT
2875 || (GET_MODE (arg0) != VOIDmode
2876 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
2877 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
2880 /* Given an rtx-code for a comparison, return the code
2881 for the negated comparison.
2882 WATCH OUT! reverse_condition is not safe to use on a jump
2883 that might be acting on the results of an IEEE floating point comparison,
2884 because of the special treatment of non-signaling nans in comparisons.
2885 Use can_reverse_comparison_p to be sure. */
2887 enum rtx_code
2888 reverse_condition (code)
2889 enum rtx_code code;
2891 switch (code)
2893 case EQ:
2894 return NE;
2896 case NE:
2897 return EQ;
2899 case GT:
2900 return LE;
2902 case GE:
2903 return LT;
2905 case LT:
2906 return GE;
2908 case LE:
2909 return GT;
2911 case GTU:
2912 return LEU;
2914 case GEU:
2915 return LTU;
2917 case LTU:
2918 return GEU;
2920 case LEU:
2921 return GTU;
2923 default:
2924 abort ();
2925 return UNKNOWN;
2929 /* Similar, but return the code when two operands of a comparison are swapped.
2930 This IS safe for IEEE floating-point. */
2932 enum rtx_code
2933 swap_condition (code)
2934 enum rtx_code code;
2936 switch (code)
2938 case EQ:
2939 case NE:
2940 return code;
2942 case GT:
2943 return LT;
2945 case GE:
2946 return LE;
2948 case LT:
2949 return GT;
2951 case LE:
2952 return GE;
2954 case GTU:
2955 return LTU;
2957 case GEU:
2958 return LEU;
2960 case LTU:
2961 return GTU;
2963 case LEU:
2964 return GEU;
2966 default:
2967 abort ();
2968 return UNKNOWN;
2972 /* Given a comparison CODE, return the corresponding unsigned comparison.
2973 If CODE is an equality comparison or already an unsigned comparison,
2974 CODE is returned. */
2976 enum rtx_code
2977 unsigned_condition (code)
2978 enum rtx_code code;
2980 switch (code)
2982 case EQ:
2983 case NE:
2984 case GTU:
2985 case GEU:
2986 case LTU:
2987 case LEU:
2988 return code;
2990 case GT:
2991 return GTU;
2993 case GE:
2994 return GEU;
2996 case LT:
2997 return LTU;
2999 case LE:
3000 return LEU;
3002 default:
3003 abort ();
3007 /* Similarly, return the signed version of a comparison. */
3009 enum rtx_code
3010 signed_condition (code)
3011 enum rtx_code code;
3013 switch (code)
3015 case EQ:
3016 case NE:
3017 case GT:
3018 case GE:
3019 case LT:
3020 case LE:
3021 return code;
3023 case GTU:
3024 return GT;
3026 case GEU:
3027 return GE;
3029 case LTU:
3030 return LT;
3032 case LEU:
3033 return LE;
3035 default:
3036 abort ();
3040 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
3041 truth of CODE1 implies the truth of CODE2. */
3044 comparison_dominates_p (code1, code2)
3045 enum rtx_code code1, code2;
3047 if (code1 == code2)
3048 return 1;
3050 switch (code1)
3052 case EQ:
3053 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
3054 return 1;
3055 break;
3057 case LT:
3058 if (code2 == LE || code2 == NE)
3059 return 1;
3060 break;
3062 case GT:
3063 if (code2 == GE || code2 == NE)
3064 return 1;
3065 break;
3067 case LTU:
3068 if (code2 == LEU || code2 == NE)
3069 return 1;
3070 break;
3072 case GTU:
3073 if (code2 == GEU || code2 == NE)
3074 return 1;
3075 break;
3078 return 0;
3081 /* Return 1 if INSN is an unconditional jump and nothing else. */
3084 simplejump_p (insn)
3085 rtx insn;
3087 return (GET_CODE (insn) == JUMP_INSN
3088 && GET_CODE (PATTERN (insn)) == SET
3089 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
3090 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
3093 /* Return nonzero if INSN is a (possibly) conditional jump
3094 and nothing more. */
3097 condjump_p (insn)
3098 rtx insn;
3100 register rtx x = PATTERN (insn);
3101 if (GET_CODE (x) != SET)
3102 return 0;
3103 if (GET_CODE (SET_DEST (x)) != PC)
3104 return 0;
3105 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
3106 return 1;
3107 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3108 return 0;
3109 if (XEXP (SET_SRC (x), 2) == pc_rtx
3110 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
3111 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
3112 return 1;
3113 if (XEXP (SET_SRC (x), 1) == pc_rtx
3114 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
3115 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
3116 return 1;
3117 return 0;
3120 /* Return nonzero if INSN is a (possibly) conditional jump
3121 and nothing more. */
3124 condjump_in_parallel_p (insn)
3125 rtx insn;
3127 register rtx x = PATTERN (insn);
3129 if (GET_CODE (x) != PARALLEL)
3130 return 0;
3131 else
3132 x = XVECEXP (x, 0, 0);
3134 if (GET_CODE (x) != SET)
3135 return 0;
3136 if (GET_CODE (SET_DEST (x)) != PC)
3137 return 0;
3138 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
3139 return 1;
3140 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3141 return 0;
3142 if (XEXP (SET_SRC (x), 2) == pc_rtx
3143 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
3144 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
3145 return 1;
3146 if (XEXP (SET_SRC (x), 1) == pc_rtx
3147 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
3148 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
3149 return 1;
3150 return 0;
3153 /* Return 1 if X is an RTX that does nothing but set the condition codes
3154 and CLOBBER or USE registers.
3155 Return -1 if X does explicitly set the condition codes,
3156 but also does other things. */
3159 sets_cc0_p (x)
3160 rtx x;
3162 #ifdef HAVE_cc0
3163 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
3164 return 1;
3165 if (GET_CODE (x) == PARALLEL)
3167 int i;
3168 int sets_cc0 = 0;
3169 int other_things = 0;
3170 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3172 if (GET_CODE (XVECEXP (x, 0, i)) == SET
3173 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
3174 sets_cc0 = 1;
3175 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
3176 other_things = 1;
3178 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
3180 return 0;
3181 #else
3182 abort ();
3183 #endif
3186 /* Follow any unconditional jump at LABEL;
3187 return the ultimate label reached by any such chain of jumps.
3188 If LABEL is not followed by a jump, return LABEL.
3189 If the chain loops or we can't find end, return LABEL,
3190 since that tells caller to avoid changing the insn.
3192 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
3193 a USE or CLOBBER. */
3196 follow_jumps (label)
3197 rtx label;
3199 register rtx insn;
3200 register rtx next;
3201 register rtx value = label;
3202 register int depth;
3204 for (depth = 0;
3205 (depth < 10
3206 && (insn = next_active_insn (value)) != 0
3207 && GET_CODE (insn) == JUMP_INSN
3208 && ((JUMP_LABEL (insn) != 0 && simplejump_p (insn))
3209 || GET_CODE (PATTERN (insn)) == RETURN)
3210 && (next = NEXT_INSN (insn))
3211 && GET_CODE (next) == BARRIER);
3212 depth++)
3214 /* Don't chain through the insn that jumps into a loop
3215 from outside the loop,
3216 since that would create multiple loop entry jumps
3217 and prevent loop optimization. */
3218 rtx tem;
3219 if (!reload_completed)
3220 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
3221 if (GET_CODE (tem) == NOTE
3222 && NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG)
3223 return value;
3225 /* If we have found a cycle, make the insn jump to itself. */
3226 if (JUMP_LABEL (insn) == label)
3227 return label;
3229 tem = next_active_insn (JUMP_LABEL (insn));
3230 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
3231 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
3232 break;
3234 value = JUMP_LABEL (insn);
3236 if (depth == 10)
3237 return label;
3238 return value;
3241 /* Assuming that field IDX of X is a vector of label_refs,
3242 replace each of them by the ultimate label reached by it.
3243 Return nonzero if a change is made.
3244 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
3246 static int
3247 tension_vector_labels (x, idx)
3248 register rtx x;
3249 register int idx;
3251 int changed = 0;
3252 register int i;
3253 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
3255 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
3256 register rtx nlabel = follow_jumps (olabel);
3257 if (nlabel && nlabel != olabel)
3259 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
3260 ++LABEL_NUSES (nlabel);
3261 if (--LABEL_NUSES (olabel) == 0)
3262 delete_insn (olabel);
3263 changed = 1;
3266 return changed;
3269 /* Find all CODE_LABELs referred to in X, and increment their use counts.
3270 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
3271 in INSN, then store one of them in JUMP_LABEL (INSN).
3272 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
3273 referenced in INSN, add a REG_LABEL note containing that label to INSN.
3274 Also, when there are consecutive labels, canonicalize on the last of them.
3276 Note that two labels separated by a loop-beginning note
3277 must be kept distinct if we have not yet done loop-optimization,
3278 because the gap between them is where loop-optimize
3279 will want to move invariant code to. CROSS_JUMP tells us
3280 that loop-optimization is done with.
3282 Once reload has completed (CROSS_JUMP non-zero), we need not consider
3283 two labels distinct if they are separated by only USE or CLOBBER insns. */
3285 static void
3286 mark_jump_label (x, insn, cross_jump)
3287 register rtx x;
3288 rtx insn;
3289 int cross_jump;
3291 register RTX_CODE code = GET_CODE (x);
3292 register int i;
3293 register char *fmt;
3295 switch (code)
3297 case PC:
3298 case CC0:
3299 case REG:
3300 case SUBREG:
3301 case CONST_INT:
3302 case SYMBOL_REF:
3303 case CONST_DOUBLE:
3304 case CLOBBER:
3305 case CALL:
3306 return;
3308 case MEM:
3309 /* If this is a constant-pool reference, see if it is a label. */
3310 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3311 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3312 mark_jump_label (get_pool_constant (XEXP (x, 0)), insn, cross_jump);
3313 break;
3315 case LABEL_REF:
3317 rtx label = XEXP (x, 0);
3318 rtx olabel = label;
3319 rtx note;
3320 rtx next;
3322 if (GET_CODE (label) != CODE_LABEL)
3323 abort ();
3325 /* Ignore references to labels of containing functions. */
3326 if (LABEL_REF_NONLOCAL_P (x))
3327 break;
3329 /* If there are other labels following this one,
3330 replace it with the last of the consecutive labels. */
3331 for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
3333 if (GET_CODE (next) == CODE_LABEL)
3334 label = next;
3335 else if (cross_jump && GET_CODE (next) == INSN
3336 && (GET_CODE (PATTERN (next)) == USE
3337 || GET_CODE (PATTERN (next)) == CLOBBER))
3338 continue;
3339 else if (GET_CODE (next) != NOTE)
3340 break;
3341 else if (! cross_jump
3342 && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
3343 || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END))
3344 break;
3347 XEXP (x, 0) = label;
3348 ++LABEL_NUSES (label);
3350 if (insn)
3352 if (GET_CODE (insn) == JUMP_INSN)
3353 JUMP_LABEL (insn) = label;
3355 /* If we've changed OLABEL and we had a REG_LABEL note
3356 for it, update it as well. */
3357 else if (label != olabel
3358 && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
3359 XEXP (note, 0) = label;
3361 /* Otherwise, add a REG_LABEL note for LABEL unless there already
3362 is one. */
3363 else if (! find_reg_note (insn, REG_LABEL, label))
3365 rtx next = next_real_insn (label);
3366 /* Don't record labels that refer to dispatch tables.
3367 This is not necessary, since the tablejump
3368 references the same label.
3369 And if we did record them, flow.c would make worse code. */
3370 if (next == 0
3371 || ! (GET_CODE (next) == JUMP_INSN
3372 && (GET_CODE (PATTERN (next)) == ADDR_VEC
3373 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
3374 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
3375 REG_NOTES (insn));
3378 return;
3381 /* Do walk the labels in a vector, but not the first operand of an
3382 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
3383 case ADDR_VEC:
3384 case ADDR_DIFF_VEC:
3386 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
3388 for (i = 0; i < XVECLEN (x, eltnum); i++)
3389 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, cross_jump);
3390 return;
3394 fmt = GET_RTX_FORMAT (code);
3395 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3397 if (fmt[i] == 'e')
3398 mark_jump_label (XEXP (x, i), insn, cross_jump);
3399 else if (fmt[i] == 'E')
3401 register int j;
3402 for (j = 0; j < XVECLEN (x, i); j++)
3403 mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
3408 /* If all INSN does is set the pc, delete it,
3409 and delete the insn that set the condition codes for it
3410 if that's what the previous thing was. */
3412 void
3413 delete_jump (insn)
3414 rtx insn;
3416 register rtx set = single_set (insn);
3418 if (set && GET_CODE (SET_DEST (set)) == PC)
3419 delete_computation (insn);
3422 /* Delete INSN and recursively delete insns that compute values used only
3423 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3424 If we are running before flow.c, we need do nothing since flow.c will
3425 delete dead code. We also can't know if the registers being used are
3426 dead or not at this point.
3428 Otherwise, look at all our REG_DEAD notes. If a previous insn does
3429 nothing other than set a register that dies in this insn, we can delete
3430 that insn as well.
3432 On machines with CC0, if CC0 is used in this insn, we may be able to
3433 delete the insn that set it. */
3435 static void
3436 delete_computation (insn)
3437 rtx insn;
3439 rtx note, next;
3441 #ifdef HAVE_cc0
3442 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3444 rtx prev = prev_nonnote_insn (insn);
3445 /* We assume that at this stage
3446 CC's are always set explicitly
3447 and always immediately before the jump that
3448 will use them. So if the previous insn
3449 exists to set the CC's, delete it
3450 (unless it performs auto-increments, etc.). */
3451 if (prev && GET_CODE (prev) == INSN
3452 && sets_cc0_p (PATTERN (prev)))
3454 if (sets_cc0_p (PATTERN (prev)) > 0
3455 && !FIND_REG_INC_NOTE (prev, NULL_RTX))
3456 delete_computation (prev);
3457 else
3458 /* Otherwise, show that cc0 won't be used. */
3459 REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_UNUSED,
3460 cc0_rtx, REG_NOTES (prev));
3463 #endif
3465 for (note = REG_NOTES (insn); note; note = next)
3467 rtx our_prev;
3469 next = XEXP (note, 1);
3471 if (REG_NOTE_KIND (note) != REG_DEAD
3472 /* Verify that the REG_NOTE is legitimate. */
3473 || GET_CODE (XEXP (note, 0)) != REG)
3474 continue;
3476 for (our_prev = prev_nonnote_insn (insn);
3477 our_prev && GET_CODE (our_prev) == INSN;
3478 our_prev = prev_nonnote_insn (our_prev))
3480 /* If we reach a SEQUENCE, it is too complex to try to
3481 do anything with it, so give up. */
3482 if (GET_CODE (PATTERN (our_prev)) == SEQUENCE)
3483 break;
3485 if (GET_CODE (PATTERN (our_prev)) == USE
3486 && GET_CODE (XEXP (PATTERN (our_prev), 0)) == INSN)
3487 /* reorg creates USEs that look like this. We leave them
3488 alone because reorg needs them for its own purposes. */
3489 break;
3491 if (reg_set_p (XEXP (note, 0), PATTERN (our_prev)))
3493 if (FIND_REG_INC_NOTE (our_prev, NULL_RTX))
3494 break;
3496 if (GET_CODE (PATTERN (our_prev)) == PARALLEL)
3498 /* If we find a SET of something else, we can't
3499 delete the insn. */
3501 int i;
3503 for (i = 0; i < XVECLEN (PATTERN (our_prev), 0); i++)
3505 rtx part = XVECEXP (PATTERN (our_prev), 0, i);
3507 if (GET_CODE (part) == SET
3508 && SET_DEST (part) != XEXP (note, 0))
3509 break;
3512 if (i == XVECLEN (PATTERN (our_prev), 0))
3513 delete_computation (our_prev);
3515 else if (GET_CODE (PATTERN (our_prev)) == SET
3516 && SET_DEST (PATTERN (our_prev)) == XEXP (note, 0))
3517 delete_computation (our_prev);
3519 break;
3522 /* If OUR_PREV references the register that dies here, it is an
3523 additional use. Hence any prior SET isn't dead. However, this
3524 insn becomes the new place for the REG_DEAD note. */
3525 if (reg_overlap_mentioned_p (XEXP (note, 0),
3526 PATTERN (our_prev)))
3528 XEXP (note, 1) = REG_NOTES (our_prev);
3529 REG_NOTES (our_prev) = note;
3530 break;
3535 delete_insn (insn);
3538 /* Delete insn INSN from the chain of insns and update label ref counts.
3539 May delete some following insns as a consequence; may even delete
3540 a label elsewhere and insns that follow it.
3542 Returns the first insn after INSN that was not deleted. */
3545 delete_insn (insn)
3546 register rtx insn;
3548 register rtx next = NEXT_INSN (insn);
3549 register rtx prev = PREV_INSN (insn);
3550 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
3551 register int dont_really_delete = 0;
3553 while (next && INSN_DELETED_P (next))
3554 next = NEXT_INSN (next);
3556 /* This insn is already deleted => return first following nondeleted. */
3557 if (INSN_DELETED_P (insn))
3558 return next;
3560 /* Don't delete user-declared labels. Convert them to special NOTEs
3561 instead. */
3562 if (was_code_label && LABEL_NAME (insn) != 0
3563 && optimize && ! dont_really_delete)
3565 PUT_CODE (insn, NOTE);
3566 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
3567 NOTE_SOURCE_FILE (insn) = 0;
3568 dont_really_delete = 1;
3570 else
3571 /* Mark this insn as deleted. */
3572 INSN_DELETED_P (insn) = 1;
3574 /* If this is an unconditional jump, delete it from the jump chain. */
3575 if (simplejump_p (insn))
3576 delete_from_jump_chain (insn);
3578 /* If instruction is followed by a barrier,
3579 delete the barrier too. */
3581 if (next != 0 && GET_CODE (next) == BARRIER)
3583 INSN_DELETED_P (next) = 1;
3584 next = NEXT_INSN (next);
3587 /* Patch out INSN (and the barrier if any) */
3589 if (optimize && ! dont_really_delete)
3591 if (prev)
3593 NEXT_INSN (prev) = next;
3594 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3595 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
3596 XVECLEN (PATTERN (prev), 0) - 1)) = next;
3599 if (next)
3601 PREV_INSN (next) = prev;
3602 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3603 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3606 if (prev && NEXT_INSN (prev) == 0)
3607 set_last_insn (prev);
3610 /* If deleting a jump, decrement the count of the label,
3611 and delete the label if it is now unused. */
3613 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
3614 if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
3616 /* This can delete NEXT or PREV,
3617 either directly if NEXT is JUMP_LABEL (INSN),
3618 or indirectly through more levels of jumps. */
3619 delete_insn (JUMP_LABEL (insn));
3620 /* I feel a little doubtful about this loop,
3621 but I see no clean and sure alternative way
3622 to find the first insn after INSN that is not now deleted.
3623 I hope this works. */
3624 while (next && INSN_DELETED_P (next))
3625 next = NEXT_INSN (next);
3626 return next;
3629 /* Likewise if we're deleting a dispatch table. */
3631 if (GET_CODE (insn) == JUMP_INSN
3632 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3633 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3635 rtx pat = PATTERN (insn);
3636 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
3637 int len = XVECLEN (pat, diff_vec_p);
3639 for (i = 0; i < len; i++)
3640 if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
3641 delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
3642 while (next && INSN_DELETED_P (next))
3643 next = NEXT_INSN (next);
3644 return next;
3647 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
3648 prev = PREV_INSN (prev);
3650 /* If INSN was a label and a dispatch table follows it,
3651 delete the dispatch table. The tablejump must have gone already.
3652 It isn't useful to fall through into a table. */
3654 if (was_code_label
3655 && NEXT_INSN (insn) != 0
3656 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3657 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
3658 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
3659 next = delete_insn (NEXT_INSN (insn));
3661 /* If INSN was a label, delete insns following it if now unreachable. */
3663 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
3665 register RTX_CODE code;
3666 while (next != 0
3667 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
3668 || code == NOTE || code == BARRIER
3669 || (code == CODE_LABEL && INSN_DELETED_P (next))))
3671 if (code == NOTE
3672 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
3673 next = NEXT_INSN (next);
3674 /* Keep going past other deleted labels to delete what follows. */
3675 else if (code == CODE_LABEL && INSN_DELETED_P (next))
3676 next = NEXT_INSN (next);
3677 else
3678 /* Note: if this deletes a jump, it can cause more
3679 deletion of unreachable code, after a different label.
3680 As long as the value from this recursive call is correct,
3681 this invocation functions correctly. */
3682 next = delete_insn (next);
3686 return next;
3689 /* Advance from INSN till reaching something not deleted
3690 then return that. May return INSN itself. */
3693 next_nondeleted_insn (insn)
3694 rtx insn;
3696 while (INSN_DELETED_P (insn))
3697 insn = NEXT_INSN (insn);
3698 return insn;
3701 /* Delete a range of insns from FROM to TO, inclusive.
3702 This is for the sake of peephole optimization, so assume
3703 that whatever these insns do will still be done by a new
3704 peephole insn that will replace them. */
3706 void
3707 delete_for_peephole (from, to)
3708 register rtx from, to;
3710 register rtx insn = from;
3712 while (1)
3714 register rtx next = NEXT_INSN (insn);
3715 register rtx prev = PREV_INSN (insn);
3717 if (GET_CODE (insn) != NOTE)
3719 INSN_DELETED_P (insn) = 1;
3721 /* Patch this insn out of the chain. */
3722 /* We don't do this all at once, because we
3723 must preserve all NOTEs. */
3724 if (prev)
3725 NEXT_INSN (prev) = next;
3727 if (next)
3728 PREV_INSN (next) = prev;
3731 if (insn == to)
3732 break;
3733 insn = next;
3736 /* Note that if TO is an unconditional jump
3737 we *do not* delete the BARRIER that follows,
3738 since the peephole that replaces this sequence
3739 is also an unconditional jump in that case. */
3742 /* Invert the condition of the jump JUMP, and make it jump
3743 to label NLABEL instead of where it jumps now. */
3746 invert_jump (jump, nlabel)
3747 rtx jump, nlabel;
3749 /* We have to either invert the condition and change the label or
3750 do neither. Either operation could fail. We first try to invert
3751 the jump. If that succeeds, we try changing the label. If that fails,
3752 we invert the jump back to what it was. */
3754 if (! invert_exp (PATTERN (jump), jump))
3755 return 0;
3757 if (redirect_jump (jump, nlabel))
3758 return 1;
3760 if (! invert_exp (PATTERN (jump), jump))
3761 /* This should just be putting it back the way it was. */
3762 abort ();
3764 return 0;
3767 /* Invert the jump condition of rtx X contained in jump insn, INSN.
3769 Return 1 if we can do so, 0 if we cannot find a way to do so that
3770 matches a pattern. */
3773 invert_exp (x, insn)
3774 rtx x;
3775 rtx insn;
3777 register RTX_CODE code;
3778 register int i;
3779 register char *fmt;
3781 code = GET_CODE (x);
3783 if (code == IF_THEN_ELSE)
3785 register rtx comp = XEXP (x, 0);
3786 register rtx tem;
3788 /* We can do this in two ways: The preferable way, which can only
3789 be done if this is not an integer comparison, is to reverse
3790 the comparison code. Otherwise, swap the THEN-part and ELSE-part
3791 of the IF_THEN_ELSE. If we can't do either, fail. */
3793 if (can_reverse_comparison_p (comp, insn)
3794 && validate_change (insn, &XEXP (x, 0),
3795 gen_rtx (reverse_condition (GET_CODE (comp)),
3796 GET_MODE (comp), XEXP (comp, 0),
3797 XEXP (comp, 1)), 0))
3798 return 1;
3800 tem = XEXP (x, 1);
3801 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
3802 validate_change (insn, &XEXP (x, 2), tem, 1);
3803 return apply_change_group ();
3806 fmt = GET_RTX_FORMAT (code);
3807 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3809 if (fmt[i] == 'e')
3810 if (! invert_exp (XEXP (x, i), insn))
3811 return 0;
3812 if (fmt[i] == 'E')
3814 register int j;
3815 for (j = 0; j < XVECLEN (x, i); j++)
3816 if (!invert_exp (XVECEXP (x, i, j), insn))
3817 return 0;
3821 return 1;
3824 /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
3825 If the old jump target label is unused as a result,
3826 it and the code following it may be deleted.
3828 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3829 RETURN insn.
3831 The return value will be 1 if the change was made, 0 if it wasn't (this
3832 can only occur for NLABEL == 0). */
3835 redirect_jump (jump, nlabel)
3836 rtx jump, nlabel;
3838 register rtx olabel = JUMP_LABEL (jump);
3840 if (nlabel == olabel)
3841 return 1;
3843 if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
3844 return 0;
3846 /* If this is an unconditional branch, delete it from the jump_chain of
3847 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3848 have UID's in range and JUMP_CHAIN is valid). */
3849 if (jump_chain && (simplejump_p (jump)
3850 || GET_CODE (PATTERN (jump)) == RETURN))
3852 int label_index = nlabel ? INSN_UID (nlabel) : 0;
3854 delete_from_jump_chain (jump);
3855 if (label_index < max_jump_chain
3856 && INSN_UID (jump) < max_jump_chain)
3858 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
3859 jump_chain[label_index] = jump;
3863 JUMP_LABEL (jump) = nlabel;
3864 if (nlabel)
3865 ++LABEL_NUSES (nlabel);
3867 if (olabel && --LABEL_NUSES (olabel) == 0)
3868 delete_insn (olabel);
3870 return 1;
3873 /* Delete the instruction JUMP from any jump chain it might be on. */
3875 static void
3876 delete_from_jump_chain (jump)
3877 rtx jump;
3879 int index;
3880 rtx olabel = JUMP_LABEL (jump);
3882 /* Handle unconditional jumps. */
3883 if (jump_chain && olabel != 0
3884 && INSN_UID (olabel) < max_jump_chain
3885 && simplejump_p (jump))
3886 index = INSN_UID (olabel);
3887 /* Handle return insns. */
3888 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
3889 index = 0;
3890 else return;
3892 if (jump_chain[index] == jump)
3893 jump_chain[index] = jump_chain[INSN_UID (jump)];
3894 else
3896 rtx insn;
3898 for (insn = jump_chain[index];
3899 insn != 0;
3900 insn = jump_chain[INSN_UID (insn)])
3901 if (jump_chain[INSN_UID (insn)] == jump)
3903 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
3904 break;
3909 /* If NLABEL is nonzero, throughout the rtx at LOC,
3910 alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL). If OLABEL is
3911 zero, alter (RETURN) to (LABEL_REF NLABEL).
3913 If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
3914 validity with validate_change. Convert (set (pc) (label_ref olabel))
3915 to (return).
3917 Return 0 if we found a change we would like to make but it is invalid.
3918 Otherwise, return 1. */
3921 redirect_exp (loc, olabel, nlabel, insn)
3922 rtx *loc;
3923 rtx olabel, nlabel;
3924 rtx insn;
3926 register rtx x = *loc;
3927 register RTX_CODE code = GET_CODE (x);
3928 register int i;
3929 register char *fmt;
3931 if (code == LABEL_REF)
3933 if (XEXP (x, 0) == olabel)
3935 if (nlabel)
3936 XEXP (x, 0) = nlabel;
3937 else
3938 return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3939 return 1;
3942 else if (code == RETURN && olabel == 0)
3944 x = gen_rtx (LABEL_REF, VOIDmode, nlabel);
3945 if (loc == &PATTERN (insn))
3946 x = gen_rtx (SET, VOIDmode, pc_rtx, x);
3947 return validate_change (insn, loc, x, 0);
3950 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
3951 && GET_CODE (SET_SRC (x)) == LABEL_REF
3952 && XEXP (SET_SRC (x), 0) == olabel)
3953 return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3955 fmt = GET_RTX_FORMAT (code);
3956 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3958 if (fmt[i] == 'e')
3959 if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
3960 return 0;
3961 if (fmt[i] == 'E')
3963 register int j;
3964 for (j = 0; j < XVECLEN (x, i); j++)
3965 if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
3966 return 0;
3970 return 1;
3973 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3975 If the old jump target label (before the dispatch table) becomes unused,
3976 it and the dispatch table may be deleted. In that case, find the insn
3977 before the jump references that label and delete it and logical successors
3978 too. */
3980 static void
3981 redirect_tablejump (jump, nlabel)
3982 rtx jump, nlabel;
3984 register rtx olabel = JUMP_LABEL (jump);
3986 /* Add this jump to the jump_chain of NLABEL. */
3987 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
3988 && INSN_UID (jump) < max_jump_chain)
3990 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
3991 jump_chain[INSN_UID (nlabel)] = jump;
3994 PATTERN (jump) = gen_jump (nlabel);
3995 JUMP_LABEL (jump) = nlabel;
3996 ++LABEL_NUSES (nlabel);
3997 INSN_CODE (jump) = -1;
3999 if (--LABEL_NUSES (olabel) == 0)
4001 delete_labelref_insn (jump, olabel, 0);
4002 delete_insn (olabel);
4006 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
4007 If we found one, delete it and then delete this insn if DELETE_THIS is
4008 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
4010 static int
4011 delete_labelref_insn (insn, label, delete_this)
4012 rtx insn, label;
4013 int delete_this;
4015 int deleted = 0;
4016 rtx link;
4018 if (GET_CODE (insn) != NOTE
4019 && reg_mentioned_p (label, PATTERN (insn)))
4021 if (delete_this)
4023 delete_insn (insn);
4024 deleted = 1;
4026 else
4027 return 1;
4030 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
4031 if (delete_labelref_insn (XEXP (link, 0), label, 1))
4033 if (delete_this)
4035 delete_insn (insn);
4036 deleted = 1;
4038 else
4039 return 1;
4042 return deleted;
4045 /* Like rtx_equal_p except that it considers two REGs as equal
4046 if they renumber to the same value and considers two commutative
4047 operations to be the same if the order of the operands has been
4048 reversed. */
4051 rtx_renumbered_equal_p (x, y)
4052 rtx x, y;
4054 register int i;
4055 register RTX_CODE code = GET_CODE (x);
4056 register char *fmt;
4058 if (x == y)
4059 return 1;
4061 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
4062 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
4063 && GET_CODE (SUBREG_REG (y)) == REG)))
4065 int reg_x = -1, reg_y = -1;
4066 int word_x = 0, word_y = 0;
4068 if (GET_MODE (x) != GET_MODE (y))
4069 return 0;
4071 /* If we haven't done any renumbering, don't
4072 make any assumptions. */
4073 if (reg_renumber == 0)
4074 return rtx_equal_p (x, y);
4076 if (code == SUBREG)
4078 reg_x = REGNO (SUBREG_REG (x));
4079 word_x = SUBREG_WORD (x);
4081 if (reg_renumber[reg_x] >= 0)
4083 reg_x = reg_renumber[reg_x] + word_x;
4084 word_x = 0;
4088 else
4090 reg_x = REGNO (x);
4091 if (reg_renumber[reg_x] >= 0)
4092 reg_x = reg_renumber[reg_x];
4095 if (GET_CODE (y) == SUBREG)
4097 reg_y = REGNO (SUBREG_REG (y));
4098 word_y = SUBREG_WORD (y);
4100 if (reg_renumber[reg_y] >= 0)
4102 reg_y = reg_renumber[reg_y];
4103 word_y = 0;
4107 else
4109 reg_y = REGNO (y);
4110 if (reg_renumber[reg_y] >= 0)
4111 reg_y = reg_renumber[reg_y];
4114 return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
4117 /* Now we have disposed of all the cases
4118 in which different rtx codes can match. */
4119 if (code != GET_CODE (y))
4120 return 0;
4122 switch (code)
4124 case PC:
4125 case CC0:
4126 case ADDR_VEC:
4127 case ADDR_DIFF_VEC:
4128 return 0;
4130 case CONST_INT:
4131 return INTVAL (x) == INTVAL (y);
4133 case LABEL_REF:
4134 /* We can't assume nonlocal labels have their following insns yet. */
4135 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
4136 return XEXP (x, 0) == XEXP (y, 0);
4138 /* Two label-refs are equivalent if they point at labels
4139 in the same position in the instruction stream. */
4140 return (next_real_insn (XEXP (x, 0))
4141 == next_real_insn (XEXP (y, 0)));
4143 case SYMBOL_REF:
4144 return XSTR (x, 0) == XSTR (y, 0);
4147 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
4149 if (GET_MODE (x) != GET_MODE (y))
4150 return 0;
4152 /* For commutative operations, the RTX match if the operand match in any
4153 order. Also handle the simple binary and unary cases without a loop. */
4154 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4155 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4156 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
4157 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
4158 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
4159 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4160 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4161 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
4162 else if (GET_RTX_CLASS (code) == '1')
4163 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
4165 /* Compare the elements. If any pair of corresponding elements
4166 fail to match, return 0 for the whole things. */
4168 fmt = GET_RTX_FORMAT (code);
4169 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4171 register int j;
4172 switch (fmt[i])
4174 case 'w':
4175 if (XWINT (x, i) != XWINT (y, i))
4176 return 0;
4177 break;
4179 case 'i':
4180 if (XINT (x, i) != XINT (y, i))
4181 return 0;
4182 break;
4184 case 's':
4185 if (strcmp (XSTR (x, i), XSTR (y, i)))
4186 return 0;
4187 break;
4189 case 'e':
4190 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
4191 return 0;
4192 break;
4194 case 'u':
4195 if (XEXP (x, i) != XEXP (y, i))
4196 return 0;
4197 /* fall through. */
4198 case '0':
4199 break;
4201 case 'E':
4202 if (XVECLEN (x, i) != XVECLEN (y, i))
4203 return 0;
4204 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4205 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
4206 return 0;
4207 break;
4209 default:
4210 abort ();
4213 return 1;
4216 /* If X is a hard register or equivalent to one or a subregister of one,
4217 return the hard register number. If X is a pseudo register that was not
4218 assigned a hard register, return the pseudo register number. Otherwise,
4219 return -1. Any rtx is valid for X. */
4222 true_regnum (x)
4223 rtx x;
4225 if (GET_CODE (x) == REG)
4227 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
4228 return reg_renumber[REGNO (x)];
4229 return REGNO (x);
4231 if (GET_CODE (x) == SUBREG)
4233 int base = true_regnum (SUBREG_REG (x));
4234 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
4235 return SUBREG_WORD (x) + base;
4237 return -1;
4240 /* Optimize code of the form:
4242 for (x = a[i]; x; ...)
4244 for (x = a[i]; x; ...)
4246 foo:
4248 Loop optimize will change the above code into
4250 if (x = a[i])
4251 for (;;)
4252 { ...; if (! (x = ...)) break; }
4253 if (x = a[i])
4254 for (;;)
4255 { ...; if (! (x = ...)) break; }
4256 foo:
4258 In general, if the first test fails, the program can branch
4259 directly to `foo' and skip the second try which is doomed to fail.
4260 We run this after loop optimization and before flow analysis. */
4262 /* When comparing the insn patterns, we track the fact that different
4263 pseudo-register numbers may have been used in each computation.
4264 The following array stores an equivalence -- same_regs[I] == J means
4265 that pseudo register I was used in the first set of tests in a context
4266 where J was used in the second set. We also count the number of such
4267 pending equivalences. If nonzero, the expressions really aren't the
4268 same. */
4270 static int *same_regs;
4272 static int num_same_regs;
4274 /* Track any registers modified between the target of the first jump and
4275 the second jump. They never compare equal. */
4277 static char *modified_regs;
4279 /* Record if memory was modified. */
4281 static int modified_mem;
4283 /* Called via note_stores on each insn between the target of the first
4284 branch and the second branch. It marks any changed registers. */
4286 static void
4287 mark_modified_reg (dest, x)
4288 rtx dest;
4289 rtx x;
4291 int regno, i;
4293 if (GET_CODE (dest) == SUBREG)
4294 dest = SUBREG_REG (dest);
4296 if (GET_CODE (dest) == MEM)
4297 modified_mem = 1;
4299 if (GET_CODE (dest) != REG)
4300 return;
4302 regno = REGNO (dest);
4303 if (regno >= FIRST_PSEUDO_REGISTER)
4304 modified_regs[regno] = 1;
4305 else
4306 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
4307 modified_regs[regno + i] = 1;
4310 /* F is the first insn in the chain of insns. */
4312 void
4313 thread_jumps (f, max_reg, flag_before_loop)
4314 rtx f;
4315 int max_reg;
4316 int flag_before_loop;
4318 /* Basic algorithm is to find a conditional branch,
4319 the label it may branch to, and the branch after
4320 that label. If the two branches test the same condition,
4321 walk back from both branch paths until the insn patterns
4322 differ, or code labels are hit. If we make it back to
4323 the target of the first branch, then we know that the first branch
4324 will either always succeed or always fail depending on the relative
4325 senses of the two branches. So adjust the first branch accordingly
4326 in this case. */
4328 rtx label, b1, b2, t1, t2;
4329 enum rtx_code code1, code2;
4330 rtx b1op0, b1op1, b2op0, b2op1;
4331 int changed = 1;
4332 int i;
4333 int *all_reset;
4335 /* Allocate register tables and quick-reset table. */
4336 modified_regs = (char *) alloca (max_reg * sizeof (char));
4337 same_regs = (int *) alloca (max_reg * sizeof (int));
4338 all_reset = (int *) alloca (max_reg * sizeof (int));
4339 for (i = 0; i < max_reg; i++)
4340 all_reset[i] = -1;
4342 while (changed)
4344 changed = 0;
4346 for (b1 = f; b1; b1 = NEXT_INSN (b1))
4348 /* Get to a candidate branch insn. */
4349 if (GET_CODE (b1) != JUMP_INSN
4350 || ! condjump_p (b1) || simplejump_p (b1)
4351 || JUMP_LABEL (b1) == 0)
4352 continue;
4354 bzero (modified_regs, max_reg * sizeof (char));
4355 modified_mem = 0;
4357 bcopy ((char *) all_reset, (char *) same_regs,
4358 max_reg * sizeof (int));
4359 num_same_regs = 0;
4361 label = JUMP_LABEL (b1);
4363 /* Look for a branch after the target. Record any registers and
4364 memory modified between the target and the branch. Stop when we
4365 get to a label since we can't know what was changed there. */
4366 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
4368 if (GET_CODE (b2) == CODE_LABEL)
4369 break;
4371 else if (GET_CODE (b2) == JUMP_INSN)
4373 /* If this is an unconditional jump and is the only use of
4374 its target label, we can follow it. */
4375 if (simplejump_p (b2)
4376 && JUMP_LABEL (b2) != 0
4377 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
4379 b2 = JUMP_LABEL (b2);
4380 continue;
4382 else
4383 break;
4386 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
4387 continue;
4389 if (GET_CODE (b2) == CALL_INSN)
4391 modified_mem = 1;
4392 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4393 if (call_used_regs[i] && ! fixed_regs[i]
4394 && i != STACK_POINTER_REGNUM
4395 && i != FRAME_POINTER_REGNUM
4396 && i != HARD_FRAME_POINTER_REGNUM
4397 && i != ARG_POINTER_REGNUM)
4398 modified_regs[i] = 1;
4401 note_stores (PATTERN (b2), mark_modified_reg);
4404 /* Check the next candidate branch insn from the label
4405 of the first. */
4406 if (b2 == 0
4407 || GET_CODE (b2) != JUMP_INSN
4408 || b2 == b1
4409 || ! condjump_p (b2)
4410 || simplejump_p (b2))
4411 continue;
4413 /* Get the comparison codes and operands, reversing the
4414 codes if appropriate. If we don't have comparison codes,
4415 we can't do anything. */
4416 b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
4417 b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
4418 code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
4419 if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
4420 code1 = reverse_condition (code1);
4422 b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
4423 b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
4424 code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
4425 if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
4426 code2 = reverse_condition (code2);
4428 /* If they test the same things and knowing that B1 branches
4429 tells us whether or not B2 branches, check if we
4430 can thread the branch. */
4431 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
4432 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
4433 && (comparison_dominates_p (code1, code2)
4434 || comparison_dominates_p (code1, reverse_condition (code2))))
4436 t1 = prev_nonnote_insn (b1);
4437 t2 = prev_nonnote_insn (b2);
4439 while (t1 != 0 && t2 != 0)
4441 if (t2 == label)
4443 /* We have reached the target of the first branch.
4444 If there are no pending register equivalents,
4445 we know that this branch will either always
4446 succeed (if the senses of the two branches are
4447 the same) or always fail (if not). */
4448 rtx new_label;
4450 if (num_same_regs != 0)
4451 break;
4453 if (comparison_dominates_p (code1, code2))
4454 new_label = JUMP_LABEL (b2);
4455 else
4456 new_label = get_label_after (b2);
4458 if (JUMP_LABEL (b1) != new_label)
4460 rtx prev = PREV_INSN (new_label);
4462 if (flag_before_loop
4463 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
4465 /* Don't thread to the loop label. If a loop
4466 label is reused, loop optimization will
4467 be disabled for that loop. */
4468 new_label = gen_label_rtx ();
4469 emit_label_after (new_label, PREV_INSN (prev));
4471 changed |= redirect_jump (b1, new_label);
4473 break;
4476 /* If either of these is not a normal insn (it might be
4477 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
4478 have already been skipped above.) Similarly, fail
4479 if the insns are different. */
4480 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
4481 || recog_memoized (t1) != recog_memoized (t2)
4482 || ! rtx_equal_for_thread_p (PATTERN (t1),
4483 PATTERN (t2), t2))
4484 break;
4486 t1 = prev_nonnote_insn (t1);
4487 t2 = prev_nonnote_insn (t2);
4494 /* This is like RTX_EQUAL_P except that it knows about our handling of
4495 possibly equivalent registers and knows to consider volatile and
4496 modified objects as not equal.
4498 YINSN is the insn containing Y. */
4501 rtx_equal_for_thread_p (x, y, yinsn)
4502 rtx x, y;
4503 rtx yinsn;
4505 register int i;
4506 register int j;
4507 register enum rtx_code code;
4508 register char *fmt;
4510 code = GET_CODE (x);
4511 /* Rtx's of different codes cannot be equal. */
4512 if (code != GET_CODE (y))
4513 return 0;
4515 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4516 (REG:SI x) and (REG:HI x) are NOT equivalent. */
4518 if (GET_MODE (x) != GET_MODE (y))
4519 return 0;
4521 /* For floating-point, consider everything unequal. This is a bit
4522 pessimistic, but this pass would only rarely do anything for FP
4523 anyway. */
4524 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
4525 && FLOAT_MODE_P (GET_MODE (x)) && ! flag_fast_math)
4526 return 0;
4528 /* For commutative operations, the RTX match if the operand match in any
4529 order. Also handle the simple binary and unary cases without a loop. */
4530 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4531 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4532 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4533 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4534 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4535 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4536 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4537 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4538 else if (GET_RTX_CLASS (code) == '1')
4539 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4541 /* Handle special-cases first. */
4542 switch (code)
4544 case REG:
4545 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4546 return 1;
4548 /* If neither is user variable or hard register, check for possible
4549 equivalence. */
4550 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4551 || REGNO (x) < FIRST_PSEUDO_REGISTER
4552 || REGNO (y) < FIRST_PSEUDO_REGISTER)
4553 return 0;
4555 if (same_regs[REGNO (x)] == -1)
4557 same_regs[REGNO (x)] = REGNO (y);
4558 num_same_regs++;
4560 /* If this is the first time we are seeing a register on the `Y'
4561 side, see if it is the last use. If not, we can't thread the
4562 jump, so mark it as not equivalent. */
4563 if (regno_last_uid[REGNO (y)] != INSN_UID (yinsn))
4564 return 0;
4566 return 1;
4568 else
4569 return (same_regs[REGNO (x)] == REGNO (y));
4571 break;
4573 case MEM:
4574 /* If memory modified or either volatile, not equivalent.
4575 Else, check address. */
4576 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4577 return 0;
4579 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4581 case ASM_INPUT:
4582 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4583 return 0;
4585 break;
4587 case SET:
4588 /* Cancel a pending `same_regs' if setting equivalenced registers.
4589 Then process source. */
4590 if (GET_CODE (SET_DEST (x)) == REG
4591 && GET_CODE (SET_DEST (y)) == REG)
4593 if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
4595 same_regs[REGNO (SET_DEST (x))] = -1;
4596 num_same_regs--;
4598 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4599 return 0;
4601 else
4602 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4603 return 0;
4605 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4607 case LABEL_REF:
4608 return XEXP (x, 0) == XEXP (y, 0);
4610 case SYMBOL_REF:
4611 return XSTR (x, 0) == XSTR (y, 0);
4614 if (x == y)
4615 return 1;
4617 fmt = GET_RTX_FORMAT (code);
4618 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4620 switch (fmt[i])
4622 case 'w':
4623 if (XWINT (x, i) != XWINT (y, i))
4624 return 0;
4625 break;
4627 case 'n':
4628 case 'i':
4629 if (XINT (x, i) != XINT (y, i))
4630 return 0;
4631 break;
4633 case 'V':
4634 case 'E':
4635 /* Two vectors must have the same length. */
4636 if (XVECLEN (x, i) != XVECLEN (y, i))
4637 return 0;
4639 /* And the corresponding elements must match. */
4640 for (j = 0; j < XVECLEN (x, i); j++)
4641 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
4642 XVECEXP (y, i, j), yinsn) == 0)
4643 return 0;
4644 break;
4646 case 'e':
4647 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
4648 return 0;
4649 break;
4651 case 'S':
4652 case 's':
4653 if (strcmp (XSTR (x, i), XSTR (y, i)))
4654 return 0;
4655 break;
4657 case 'u':
4658 /* These are just backpointers, so they don't matter. */
4659 break;
4661 case '0':
4662 break;
4664 /* It is believed that rtx's at this level will never
4665 contain anything but integers and other rtx's,
4666 except for within LABEL_REFs and SYMBOL_REFs. */
4667 default:
4668 abort ();
4671 return 1;